[AArch64] Improve scheduling model for X-Gene
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob357979e663ec63247b8ee7ef9ff66532ebcd7d3f
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-2017, 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 Contracts; use Contracts;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Expander; use Expander;
34 with Exp_Aggr; use Exp_Aggr;
35 with Exp_Atag; use Exp_Atag;
36 with Exp_Ch2; use Exp_Ch2;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch7; use Exp_Ch7;
39 with Exp_Ch9; use Exp_Ch9;
40 with Exp_Dbug; use Exp_Dbug;
41 with Exp_Disp; use Exp_Disp;
42 with Exp_Dist; use Exp_Dist;
43 with Exp_Intr; use Exp_Intr;
44 with Exp_Pakd; use Exp_Pakd;
45 with Exp_Tss; use Exp_Tss;
46 with Exp_Util; use Exp_Util;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
49 with Itypes; use Itypes;
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_Ch12; use Sem_Ch12;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_SCIL; use Sem_SCIL;
71 with Sem_Util; use Sem_Util;
72 with Sinfo; use Sinfo;
73 with Snames; use Snames;
74 with Stand; use Stand;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Validsw; use Validsw;
79 package body Exp_Ch6 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Add_Access_Actual_To_Build_In_Place_Call
86 (Function_Call : Node_Id;
87 Function_Id : Entity_Id;
88 Return_Object : Node_Id;
89 Is_Access : Boolean := False);
90 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
91 -- object name given by Return_Object and add the attribute to the end of
92 -- the actual parameter list associated with the build-in-place function
93 -- call denoted by Function_Call. However, if Is_Access is True, then
94 -- Return_Object is already an access expression, in which case it's passed
95 -- along directly to the build-in-place function. Finally, if Return_Object
96 -- is empty, then pass a null literal as the actual.
98 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
99 (Function_Call : Node_Id;
100 Function_Id : Entity_Id;
101 Alloc_Form : BIP_Allocation_Form := Unspecified;
102 Alloc_Form_Exp : Node_Id := Empty;
103 Pool_Actual : Node_Id := Make_Null (No_Location));
104 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
105 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
106 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
107 -- otherwise pass a literal corresponding to the Alloc_Form parameter
108 -- (which must not be Unspecified in that case). Pool_Actual is the
109 -- parameter to pass to BIP_Storage_Pool.
111 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
112 (Func_Call : Node_Id;
113 Func_Id : Entity_Id;
114 Ptr_Typ : Entity_Id := Empty;
115 Master_Exp : Node_Id := Empty);
116 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
117 -- finalization actions, add an actual parameter which is a pointer to the
118 -- finalization master of the caller. If Master_Exp is not Empty, then that
119 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
120 -- will result in an automatic "null" value for the actual.
122 procedure Add_Task_Actuals_To_Build_In_Place_Call
123 (Function_Call : Node_Id;
124 Function_Id : Entity_Id;
125 Master_Actual : Node_Id;
126 Chain : Node_Id := Empty);
127 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
128 -- contains tasks, add two actual parameters: the master, and a pointer to
129 -- the caller's activation chain. Master_Actual is the actual parameter
130 -- expression to pass for the master. In most cases, this is the current
131 -- master (_master). The two exceptions are: If the function call is the
132 -- initialization expression for an allocator, we pass the master of the
133 -- access type. If the function call is the initialization expression for a
134 -- return object, we pass along the master passed in by the caller. In most
135 -- contexts, the activation chain to pass is the local one, which is
136 -- indicated by No (Chain). However, in an allocator, the caller passes in
137 -- the activation Chain. Note: Master_Actual can be Empty, but only if
138 -- there are no tasks.
140 function Caller_Known_Size
141 (Func_Call : Node_Id;
142 Result_Subt : Entity_Id) return Boolean;
143 -- True if result subtype is definite, or has a size that does not require
144 -- secondary stack usage (i.e. no variant part or components whose type
145 -- depends on discriminants). In particular, untagged types with only
146 -- access discriminants do not require secondary stack use. Note we must
147 -- always use the secondary stack for dispatching-on-result calls.
149 procedure Check_Overriding_Operation (Subp : Entity_Id);
150 -- Subp is a dispatching operation. Check whether it may override an
151 -- inherited private operation, in which case its DT entry is that of
152 -- the hidden operation, not the one it may have received earlier.
153 -- This must be done before emitting the code to set the corresponding
154 -- DT to the address of the subprogram. The actual placement of Subp in
155 -- the proper place in the list of primitive operations is done in
156 -- Declare_Inherited_Private_Subprograms, which also has to deal with
157 -- implicit operations. This duplication is unavoidable for now???
159 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
160 -- This procedure is called only if the subprogram body N, whose spec
161 -- has the given entity Spec, contains a parameterless recursive call.
162 -- It attempts to generate runtime code to detect if this a case of
163 -- infinite recursion.
165 -- The body is scanned to determine dependencies. If the only external
166 -- dependencies are on a small set of scalar variables, then the values
167 -- of these variables are captured on entry to the subprogram, and if
168 -- the values are not changed for the call, we know immediately that
169 -- we have an infinite recursion.
171 procedure Expand_Actuals
172 (N : Node_Id;
173 Subp : Entity_Id;
174 Post_Call : out List_Id);
175 -- Return a list of actions to take place after the call in Post_Call. The
176 -- call will later be rewritten as an Expression_With_Actions, with the
177 -- Post_Call actions inserted, and the call inside.
179 -- For each actual of an in-out or out parameter which is a numeric (view)
180 -- conversion of the form T (A), where A denotes a variable, we insert the
181 -- declaration:
183 -- Temp : T[ := T (A)];
185 -- prior to the call. Then we replace the actual with a reference to Temp,
186 -- and append the assignment:
188 -- A := TypeA (Temp);
190 -- after the call. Here TypeA is the actual type of variable A. For out
191 -- parameters, the initial declaration has no expression. If A is not an
192 -- entity name, we generate instead:
194 -- Var : TypeA renames A;
195 -- Temp : T := Var; -- omitting expression for out parameter.
196 -- ...
197 -- Var := TypeA (Temp);
199 -- For other in-out parameters, we emit the required constraint checks
200 -- before and/or after the call.
202 -- For all parameter modes, actuals that denote components and slices of
203 -- packed arrays are expanded into suitable temporaries.
205 -- For non-scalar objects that are possibly unaligned, add call by copy
206 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
208 -- For OUT and IN OUT parameters, add predicate checks after the call
209 -- based on the predicates of the actual type.
211 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
212 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
214 procedure Expand_Ctrl_Function_Call (N : Node_Id);
215 -- N is a function call which returns a controlled object. Transform the
216 -- call into a temporary which retrieves the returned object from the
217 -- secondary stack using 'reference.
219 procedure Expand_Non_Function_Return (N : Node_Id);
220 -- Expand a simple return statement found in a procedure body, entry body,
221 -- accept statement, or an extended return statement. Note that all non-
222 -- function returns are simple return statements.
224 function Expand_Protected_Object_Reference
225 (N : Node_Id;
226 Scop : Entity_Id) return Node_Id;
228 procedure Expand_Protected_Subprogram_Call
229 (N : Node_Id;
230 Subp : Entity_Id;
231 Scop : Entity_Id);
232 -- A call to a protected subprogram within the protected object may appear
233 -- as a regular call. The list of actuals must be expanded to contain a
234 -- reference to the object itself, and the call becomes a call to the
235 -- corresponding protected subprogram.
237 procedure Expand_Simple_Function_Return (N : Node_Id);
238 -- Expand simple return from function. In the case where we are returning
239 -- from a function body this is called by Expand_N_Simple_Return_Statement.
241 function Has_Unconstrained_Access_Discriminants
242 (Subtyp : Entity_Id) return Boolean;
243 -- Returns True if the given subtype is unconstrained and has one or more
244 -- access discriminants.
246 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
247 -- Insert the Post_Call list previously produced by routine Expand_Actuals
248 -- or Expand_Call_Helper into the tree.
250 procedure Replace_Renaming_Declaration_Id
251 (New_Decl : Node_Id;
252 Orig_Decl : Node_Id);
253 -- Replace the internal identifier of the new renaming declaration New_Decl
254 -- with the identifier of its original declaration Orig_Decl exchanging the
255 -- entities containing their defining identifiers to ensure the correct
256 -- replacement of the object declaration by the object renaming declaration
257 -- to avoid homograph conflicts (since the object declaration's defining
258 -- identifier was already entered in the current scope). The Next_Entity
259 -- links of the two entities are also swapped since the entities are part
260 -- of the return scope's entity list and the list structure would otherwise
261 -- be corrupted. The homonym chain is preserved as well.
263 procedure Rewrite_Function_Call_For_C (N : Node_Id);
264 -- When generating C code, replace a call to a function that returns an
265 -- array into the generated procedure with an additional out parameter.
267 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
268 -- N is a return statement for a function that returns its result on the
269 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
270 -- function and all blocks and loops that the return statement is jumping
271 -- out of. This ensures that the secondary stack is not released; otherwise
272 -- the function result would be reclaimed before returning to the caller.
274 ----------------------------------------------
275 -- Add_Access_Actual_To_Build_In_Place_Call --
276 ----------------------------------------------
278 procedure Add_Access_Actual_To_Build_In_Place_Call
279 (Function_Call : Node_Id;
280 Function_Id : Entity_Id;
281 Return_Object : Node_Id;
282 Is_Access : Boolean := False)
284 Loc : constant Source_Ptr := Sloc (Function_Call);
285 Obj_Address : Node_Id;
286 Obj_Acc_Formal : Entity_Id;
288 begin
289 -- Locate the implicit access parameter in the called function
291 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
293 -- If no return object is provided, then pass null
295 if not Present (Return_Object) then
296 Obj_Address := Make_Null (Loc);
297 Set_Parent (Obj_Address, Function_Call);
299 -- If Return_Object is already an expression of an access type, then use
300 -- it directly, since it must be an access value denoting the return
301 -- object, and couldn't possibly be the return object itself.
303 elsif Is_Access then
304 Obj_Address := Return_Object;
305 Set_Parent (Obj_Address, Function_Call);
307 -- Apply Unrestricted_Access to caller's return object
309 else
310 Obj_Address :=
311 Make_Attribute_Reference (Loc,
312 Prefix => Return_Object,
313 Attribute_Name => Name_Unrestricted_Access);
315 Set_Parent (Return_Object, Obj_Address);
316 Set_Parent (Obj_Address, Function_Call);
317 end if;
319 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
321 -- Build the parameter association for the new actual and add it to the
322 -- end of the function's actuals.
324 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
325 end Add_Access_Actual_To_Build_In_Place_Call;
327 ------------------------------------------------------
328 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
329 ------------------------------------------------------
331 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
332 (Function_Call : Node_Id;
333 Function_Id : Entity_Id;
334 Alloc_Form : BIP_Allocation_Form := Unspecified;
335 Alloc_Form_Exp : Node_Id := Empty;
336 Pool_Actual : Node_Id := Make_Null (No_Location))
338 Loc : constant Source_Ptr := Sloc (Function_Call);
339 Alloc_Form_Actual : Node_Id;
340 Alloc_Form_Formal : Node_Id;
341 Pool_Formal : Node_Id;
343 begin
344 -- The allocation form generally doesn't need to be passed in the case
345 -- of a constrained result subtype, since normally the caller performs
346 -- the allocation in that case. However this formal is still needed in
347 -- the case where the function has a tagged result, because generally
348 -- such functions can be called in a dispatching context and such calls
349 -- must be handled like calls to class-wide functions.
351 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
352 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
353 then
354 return;
355 end if;
357 -- Locate the implicit allocation form parameter in the called function.
358 -- Maybe it would be better for each implicit formal of a build-in-place
359 -- function to have a flag or a Uint attribute to identify it. ???
361 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
363 if Present (Alloc_Form_Exp) then
364 pragma Assert (Alloc_Form = Unspecified);
366 Alloc_Form_Actual := Alloc_Form_Exp;
368 else
369 pragma Assert (Alloc_Form /= Unspecified);
371 Alloc_Form_Actual :=
372 Make_Integer_Literal (Loc,
373 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
374 end if;
376 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
378 -- Build the parameter association for the new actual and add it to the
379 -- end of the function's actuals.
381 Add_Extra_Actual_To_Call
382 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
384 -- Pass the Storage_Pool parameter. This parameter is omitted on
385 -- ZFP as those targets do not support pools.
387 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
388 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
389 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
390 Add_Extra_Actual_To_Call
391 (Function_Call, Pool_Formal, Pool_Actual);
392 end if;
393 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
395 -----------------------------------------------------------
396 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
397 -----------------------------------------------------------
399 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
400 (Func_Call : Node_Id;
401 Func_Id : Entity_Id;
402 Ptr_Typ : Entity_Id := Empty;
403 Master_Exp : Node_Id := Empty)
405 begin
406 if not Needs_BIP_Finalization_Master (Func_Id) then
407 return;
408 end if;
410 declare
411 Formal : constant Entity_Id :=
412 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
413 Loc : constant Source_Ptr := Sloc (Func_Call);
415 Actual : Node_Id;
416 Desig_Typ : Entity_Id;
418 begin
419 -- If there is a finalization master actual, such as the implicit
420 -- finalization master of an enclosing build-in-place function,
421 -- then this must be added as an extra actual of the call.
423 if Present (Master_Exp) then
424 Actual := Master_Exp;
426 -- Case where the context does not require an actual master
428 elsif No (Ptr_Typ) then
429 Actual := Make_Null (Loc);
431 else
432 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
434 -- Check for a library-level access type whose designated type has
435 -- suppressed finalization or the access type is subject to pragma
436 -- No_Heap_Finalization. Such an access type lacks a master. Pass
437 -- a null actual to callee in order to signal a missing master.
439 if Is_Library_Level_Entity (Ptr_Typ)
440 and then (Finalize_Storage_Only (Desig_Typ)
441 or else No_Heap_Finalization (Ptr_Typ))
442 then
443 Actual := Make_Null (Loc);
445 -- Types in need of finalization actions
447 elsif Needs_Finalization (Desig_Typ) then
449 -- The general mechanism of creating finalization masters for
450 -- anonymous access types is disabled by default, otherwise
451 -- finalization masters will pop all over the place. Such types
452 -- use context-specific masters.
454 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
455 and then No (Finalization_Master (Ptr_Typ))
456 then
457 Build_Anonymous_Master (Ptr_Typ);
458 end if;
460 -- Access-to-controlled types should always have a master
462 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
464 Actual :=
465 Make_Attribute_Reference (Loc,
466 Prefix =>
467 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
468 Attribute_Name => Name_Unrestricted_Access);
470 -- Tagged types
472 else
473 Actual := Make_Null (Loc);
474 end if;
475 end if;
477 Analyze_And_Resolve (Actual, Etype (Formal));
479 -- Build the parameter association for the new actual and add it to
480 -- the end of the function's actuals.
482 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
483 end;
484 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
486 ------------------------------
487 -- Add_Extra_Actual_To_Call --
488 ------------------------------
490 procedure Add_Extra_Actual_To_Call
491 (Subprogram_Call : Node_Id;
492 Extra_Formal : Entity_Id;
493 Extra_Actual : Node_Id)
495 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
496 Param_Assoc : Node_Id;
498 begin
499 Param_Assoc :=
500 Make_Parameter_Association (Loc,
501 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
502 Explicit_Actual_Parameter => Extra_Actual);
504 Set_Parent (Param_Assoc, Subprogram_Call);
505 Set_Parent (Extra_Actual, Param_Assoc);
507 if Present (Parameter_Associations (Subprogram_Call)) then
508 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
509 N_Parameter_Association
510 then
512 -- Find last named actual, and append
514 declare
515 L : Node_Id;
516 begin
517 L := First_Actual (Subprogram_Call);
518 while Present (L) loop
519 if No (Next_Actual (L)) then
520 Set_Next_Named_Actual (Parent (L), Extra_Actual);
521 exit;
522 end if;
523 Next_Actual (L);
524 end loop;
525 end;
527 else
528 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
529 end if;
531 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
533 else
534 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
535 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
536 end if;
537 end Add_Extra_Actual_To_Call;
539 ---------------------------------------------
540 -- Add_Task_Actuals_To_Build_In_Place_Call --
541 ---------------------------------------------
543 procedure Add_Task_Actuals_To_Build_In_Place_Call
544 (Function_Call : Node_Id;
545 Function_Id : Entity_Id;
546 Master_Actual : Node_Id;
547 Chain : Node_Id := Empty)
549 Loc : constant Source_Ptr := Sloc (Function_Call);
550 Result_Subt : constant Entity_Id :=
551 Available_View (Etype (Function_Id));
552 Actual : Node_Id;
553 Chain_Actual : Node_Id;
554 Chain_Formal : Node_Id;
555 Master_Formal : Node_Id;
557 begin
558 -- No such extra parameters are needed if there are no tasks
560 if not Has_Task (Result_Subt) then
561 return;
562 end if;
564 Actual := Master_Actual;
566 -- Use a dummy _master actual in case of No_Task_Hierarchy
568 if Restriction_Active (No_Task_Hierarchy) then
569 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
571 -- In the case where we use the master associated with an access type,
572 -- the actual is an entity and requires an explicit reference.
574 elsif Nkind (Actual) = N_Defining_Identifier then
575 Actual := New_Occurrence_Of (Actual, Loc);
576 end if;
578 -- Locate the implicit master parameter in the called function
580 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
581 Analyze_And_Resolve (Actual, Etype (Master_Formal));
583 -- Build the parameter association for the new actual and add it to the
584 -- end of the function's actuals.
586 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
588 -- Locate the implicit activation chain parameter in the called function
590 Chain_Formal :=
591 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
593 -- Create the actual which is a pointer to the current activation chain
595 if No (Chain) then
596 Chain_Actual :=
597 Make_Attribute_Reference (Loc,
598 Prefix => Make_Identifier (Loc, Name_uChain),
599 Attribute_Name => Name_Unrestricted_Access);
601 -- Allocator case; make a reference to the Chain passed in by the caller
603 else
604 Chain_Actual :=
605 Make_Attribute_Reference (Loc,
606 Prefix => New_Occurrence_Of (Chain, Loc),
607 Attribute_Name => Name_Unrestricted_Access);
608 end if;
610 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
612 -- Build the parameter association for the new actual and add it to the
613 -- end of the function's actuals.
615 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
616 end Add_Task_Actuals_To_Build_In_Place_Call;
618 -----------------------
619 -- BIP_Formal_Suffix --
620 -----------------------
622 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
623 begin
624 case Kind is
625 when BIP_Alloc_Form =>
626 return "BIPalloc";
628 when BIP_Storage_Pool =>
629 return "BIPstoragepool";
631 when BIP_Finalization_Master =>
632 return "BIPfinalizationmaster";
634 when BIP_Task_Master =>
635 return "BIPtaskmaster";
637 when BIP_Activation_Chain =>
638 return "BIPactivationchain";
640 when BIP_Object_Access =>
641 return "BIPaccess";
642 end case;
643 end BIP_Formal_Suffix;
645 ---------------------------
646 -- Build_In_Place_Formal --
647 ---------------------------
649 function Build_In_Place_Formal
650 (Func : Entity_Id;
651 Kind : BIP_Formal_Kind) return Entity_Id
653 Formal_Name : constant Name_Id :=
654 New_External_Name
655 (Chars (Func), BIP_Formal_Suffix (Kind));
656 Extra_Formal : Entity_Id := Extra_Formals (Func);
658 begin
659 -- Maybe it would be better for each implicit formal of a build-in-place
660 -- function to have a flag or a Uint attribute to identify it. ???
662 -- The return type in the function declaration may have been a limited
663 -- view, and the extra formals for the function were not generated at
664 -- that point. At the point of call the full view must be available and
665 -- the extra formals can be created.
667 if No (Extra_Formal) then
668 Create_Extra_Formals (Func);
669 Extra_Formal := Extra_Formals (Func);
670 end if;
672 loop
673 pragma Assert (Present (Extra_Formal));
674 exit when Chars (Extra_Formal) = Formal_Name;
676 Next_Formal_With_Extras (Extra_Formal);
677 end loop;
679 return Extra_Formal;
680 end Build_In_Place_Formal;
682 -------------------------------
683 -- Build_Procedure_Body_Form --
684 -------------------------------
686 function Build_Procedure_Body_Form
687 (Func_Id : Entity_Id;
688 Func_Body : Node_Id) return Node_Id
690 Loc : constant Source_Ptr := Sloc (Func_Body);
692 Proc_Decl : constant Node_Id :=
693 Next (Unit_Declaration_Node (Func_Id));
694 -- It is assumed that the next node following the declaration of the
695 -- corresponding subprogram spec is the declaration of the procedure
696 -- form.
698 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
700 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
701 -- Replace each return statement found in the list Stmts with an
702 -- assignment of the return expression to parameter Param_Id.
704 ---------------------
705 -- Replace_Returns --
706 ---------------------
708 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
709 Stmt : Node_Id;
711 begin
712 Stmt := First (Stmts);
713 while Present (Stmt) loop
714 if Nkind (Stmt) = N_Block_Statement then
715 Replace_Returns (Param_Id,
716 Statements (Handled_Statement_Sequence (Stmt)));
718 elsif Nkind (Stmt) = N_Case_Statement then
719 declare
720 Alt : Node_Id;
721 begin
722 Alt := First (Alternatives (Stmt));
723 while Present (Alt) loop
724 Replace_Returns (Param_Id, Statements (Alt));
725 Next (Alt);
726 end loop;
727 end;
729 elsif Nkind (Stmt) = N_Extended_Return_Statement then
730 declare
731 Ret_Obj : constant Entity_Id :=
732 Defining_Entity
733 (First (Return_Object_Declarations (Stmt)));
734 Assign : constant Node_Id :=
735 Make_Assignment_Statement (Sloc (Stmt),
736 Name =>
737 New_Occurrence_Of (Param_Id, Loc),
738 Expression =>
739 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
740 Stmts : List_Id;
742 begin
743 -- The extended return may just contain the declaration
745 if Present (Handled_Statement_Sequence (Stmt)) then
746 Stmts := Statements (Handled_Statement_Sequence (Stmt));
747 else
748 Stmts := New_List;
749 end if;
751 Set_Assignment_OK (Name (Assign));
753 Rewrite (Stmt,
754 Make_Block_Statement (Sloc (Stmt),
755 Declarations =>
756 Return_Object_Declarations (Stmt),
757 Handled_Statement_Sequence =>
758 Make_Handled_Sequence_Of_Statements (Loc,
759 Statements => Stmts)));
761 Replace_Returns (Param_Id, Stmts);
763 Append_To (Stmts, Assign);
764 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
765 end;
767 elsif Nkind (Stmt) = N_If_Statement then
768 Replace_Returns (Param_Id, Then_Statements (Stmt));
769 Replace_Returns (Param_Id, Else_Statements (Stmt));
771 declare
772 Part : Node_Id;
773 begin
774 Part := First (Elsif_Parts (Stmt));
775 while Present (Part) loop
776 Replace_Returns (Param_Id, Then_Statements (Part));
777 Next (Part);
778 end loop;
779 end;
781 elsif Nkind (Stmt) = N_Loop_Statement then
782 Replace_Returns (Param_Id, Statements (Stmt));
784 elsif Nkind (Stmt) = N_Simple_Return_Statement then
786 -- Generate:
787 -- Param := Expr;
788 -- return;
790 Rewrite (Stmt,
791 Make_Assignment_Statement (Sloc (Stmt),
792 Name => New_Occurrence_Of (Param_Id, Loc),
793 Expression => Relocate_Node (Expression (Stmt))));
795 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
797 -- Skip the added return
799 Next (Stmt);
800 end if;
802 Next (Stmt);
803 end loop;
804 end Replace_Returns;
806 -- Local variables
808 Stmts : List_Id;
809 New_Body : Node_Id;
811 -- Start of processing for Build_Procedure_Body_Form
813 begin
814 -- This routine replaces the original function body:
816 -- function F (...) return Array_Typ is
817 -- begin
818 -- ...
819 -- return Something;
820 -- end F;
822 -- with the following:
824 -- procedure P (..., Result : out Array_Typ) is
825 -- begin
826 -- ...
827 -- Result := Something;
828 -- end P;
830 Stmts :=
831 Statements (Handled_Statement_Sequence (Func_Body));
832 Replace_Returns (Last_Entity (Proc_Id), Stmts);
834 New_Body :=
835 Make_Subprogram_Body (Loc,
836 Specification =>
837 Copy_Subprogram_Spec (Specification (Proc_Decl)),
838 Declarations => Declarations (Func_Body),
839 Handled_Statement_Sequence =>
840 Make_Handled_Sequence_Of_Statements (Loc,
841 Statements => Stmts));
843 -- If the function is a generic instance, so is the new procedure.
844 -- Set flag accordingly so that the proper renaming declarations are
845 -- generated.
847 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
848 return New_Body;
849 end Build_Procedure_Body_Form;
851 -----------------------
852 -- Caller_Known_Size --
853 -----------------------
855 function Caller_Known_Size
856 (Func_Call : Node_Id;
857 Result_Subt : Entity_Id) return Boolean
859 begin
860 return
861 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
862 and then No (Controlling_Argument (Func_Call)))
863 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
864 end Caller_Known_Size;
866 --------------------------------
867 -- Check_Overriding_Operation --
868 --------------------------------
870 procedure Check_Overriding_Operation (Subp : Entity_Id) is
871 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
872 Op_List : constant Elist_Id := Primitive_Operations (Typ);
873 Op_Elmt : Elmt_Id;
874 Prim_Op : Entity_Id;
875 Par_Op : Entity_Id;
877 begin
878 if Is_Derived_Type (Typ)
879 and then not Is_Private_Type (Typ)
880 and then In_Open_Scopes (Scope (Etype (Typ)))
881 and then Is_Base_Type (Typ)
882 then
883 -- Subp overrides an inherited private operation if there is an
884 -- inherited operation with a different name than Subp (see
885 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
886 -- same name as Subp.
888 Op_Elmt := First_Elmt (Op_List);
889 while Present (Op_Elmt) loop
890 Prim_Op := Node (Op_Elmt);
891 Par_Op := Alias (Prim_Op);
893 if Present (Par_Op)
894 and then not Comes_From_Source (Prim_Op)
895 and then Chars (Prim_Op) /= Chars (Par_Op)
896 and then Chars (Par_Op) = Chars (Subp)
897 and then Is_Hidden (Par_Op)
898 and then Type_Conformant (Prim_Op, Subp)
899 then
900 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
901 end if;
903 Next_Elmt (Op_Elmt);
904 end loop;
905 end if;
906 end Check_Overriding_Operation;
908 -------------------------------
909 -- Detect_Infinite_Recursion --
910 -------------------------------
912 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
913 Loc : constant Source_Ptr := Sloc (N);
915 Var_List : constant Elist_Id := New_Elmt_List;
916 -- List of globals referenced by body of procedure
918 Call_List : constant Elist_Id := New_Elmt_List;
919 -- List of recursive calls in body of procedure
921 Shad_List : constant Elist_Id := New_Elmt_List;
922 -- List of entity id's for entities created to capture the value of
923 -- referenced globals on entry to the procedure.
925 Scop : constant Uint := Scope_Depth (Spec);
926 -- This is used to record the scope depth of the current procedure, so
927 -- that we can identify global references.
929 Max_Vars : constant := 4;
930 -- Do not test more than four global variables
932 Count_Vars : Natural := 0;
933 -- Count variables found so far
935 Var : Entity_Id;
936 Elm : Elmt_Id;
937 Ent : Entity_Id;
938 Call : Elmt_Id;
939 Decl : Node_Id;
940 Test : Node_Id;
941 Elm1 : Elmt_Id;
942 Elm2 : Elmt_Id;
943 Last : Node_Id;
945 function Process (Nod : Node_Id) return Traverse_Result;
946 -- Function to traverse the subprogram body (using Traverse_Func)
948 -------------
949 -- Process --
950 -------------
952 function Process (Nod : Node_Id) return Traverse_Result is
953 begin
954 -- Procedure call
956 if Nkind (Nod) = N_Procedure_Call_Statement then
958 -- Case of one of the detected recursive calls
960 if Is_Entity_Name (Name (Nod))
961 and then Has_Recursive_Call (Entity (Name (Nod)))
962 and then Entity (Name (Nod)) = Spec
963 then
964 Append_Elmt (Nod, Call_List);
965 return Skip;
967 -- Any other procedure call may have side effects
969 else
970 return Abandon;
971 end if;
973 -- A call to a pure function can always be ignored
975 elsif Nkind (Nod) = N_Function_Call
976 and then Is_Entity_Name (Name (Nod))
977 and then Is_Pure (Entity (Name (Nod)))
978 then
979 return Skip;
981 -- Case of an identifier reference
983 elsif Nkind (Nod) = N_Identifier then
984 Ent := Entity (Nod);
986 -- If no entity, then ignore the reference
988 -- Not clear why this can happen. To investigate, remove this
989 -- test and look at the crash that occurs here in 3401-004 ???
991 if No (Ent) then
992 return Skip;
994 -- Ignore entities with no Scope, again not clear how this
995 -- can happen, to investigate, look at 4108-008 ???
997 elsif No (Scope (Ent)) then
998 return Skip;
1000 -- Ignore the reference if not to a more global object
1002 elsif Scope_Depth (Scope (Ent)) >= Scop then
1003 return Skip;
1005 -- References to types, exceptions and constants are always OK
1007 elsif Is_Type (Ent)
1008 or else Ekind (Ent) = E_Exception
1009 or else Ekind (Ent) = E_Constant
1010 then
1011 return Skip;
1013 -- If other than a non-volatile scalar variable, we have some
1014 -- kind of global reference (e.g. to a function) that we cannot
1015 -- deal with so we forget the attempt.
1017 elsif Ekind (Ent) /= E_Variable
1018 or else not Is_Scalar_Type (Etype (Ent))
1019 or else Treat_As_Volatile (Ent)
1020 then
1021 return Abandon;
1023 -- Otherwise we have a reference to a global scalar
1025 else
1026 -- Loop through global entities already detected
1028 Elm := First_Elmt (Var_List);
1029 loop
1030 -- If not detected before, record this new global reference
1032 if No (Elm) then
1033 Count_Vars := Count_Vars + 1;
1035 if Count_Vars <= Max_Vars then
1036 Append_Elmt (Entity (Nod), Var_List);
1037 else
1038 return Abandon;
1039 end if;
1041 exit;
1043 -- If recorded before, ignore
1045 elsif Node (Elm) = Entity (Nod) then
1046 return Skip;
1048 -- Otherwise keep looking
1050 else
1051 Next_Elmt (Elm);
1052 end if;
1053 end loop;
1055 return Skip;
1056 end if;
1058 -- For all other node kinds, recursively visit syntactic children
1060 else
1061 return OK;
1062 end if;
1063 end Process;
1065 function Traverse_Body is new Traverse_Func (Process);
1067 -- Start of processing for Detect_Infinite_Recursion
1069 begin
1070 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1071 -- won't be able to generate the code to handle the recursion in any
1072 -- case.
1074 if Restriction_Active (No_Implicit_Conditionals) then
1075 return;
1076 end if;
1078 -- Otherwise do traversal and quit if we get abandon signal
1080 if Traverse_Body (N) = Abandon then
1081 return;
1083 -- We must have a call, since Has_Recursive_Call was set. If not just
1084 -- ignore (this is only an error check, so if we have a funny situation,
1085 -- due to bugs or errors, we do not want to bomb).
1087 elsif Is_Empty_Elmt_List (Call_List) then
1088 return;
1089 end if;
1091 -- Here is the case where we detect recursion at compile time
1093 -- Push our current scope for analyzing the declarations and code that
1094 -- we will insert for the checking.
1096 Push_Scope (Spec);
1098 -- This loop builds temporary variables for each of the referenced
1099 -- globals, so that at the end of the loop the list Shad_List contains
1100 -- these temporaries in one-to-one correspondence with the elements in
1101 -- Var_List.
1103 Last := Empty;
1104 Elm := First_Elmt (Var_List);
1105 while Present (Elm) loop
1106 Var := Node (Elm);
1107 Ent := Make_Temporary (Loc, 'S');
1108 Append_Elmt (Ent, Shad_List);
1110 -- Insert a declaration for this temporary at the start of the
1111 -- declarations for the procedure. The temporaries are declared as
1112 -- constant objects initialized to the current values of the
1113 -- corresponding temporaries.
1115 Decl :=
1116 Make_Object_Declaration (Loc,
1117 Defining_Identifier => Ent,
1118 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1119 Constant_Present => True,
1120 Expression => New_Occurrence_Of (Var, Loc));
1122 if No (Last) then
1123 Prepend (Decl, Declarations (N));
1124 else
1125 Insert_After (Last, Decl);
1126 end if;
1128 Last := Decl;
1129 Analyze (Decl);
1130 Next_Elmt (Elm);
1131 end loop;
1133 -- Loop through calls
1135 Call := First_Elmt (Call_List);
1136 while Present (Call) loop
1138 -- Build a predicate expression of the form
1140 -- True
1141 -- and then global1 = temp1
1142 -- and then global2 = temp2
1143 -- ...
1145 -- This predicate determines if any of the global values
1146 -- referenced by the procedure have changed since the
1147 -- current call, if not an infinite recursion is assured.
1149 Test := New_Occurrence_Of (Standard_True, Loc);
1151 Elm1 := First_Elmt (Var_List);
1152 Elm2 := First_Elmt (Shad_List);
1153 while Present (Elm1) loop
1154 Test :=
1155 Make_And_Then (Loc,
1156 Left_Opnd => Test,
1157 Right_Opnd =>
1158 Make_Op_Eq (Loc,
1159 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1160 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1162 Next_Elmt (Elm1);
1163 Next_Elmt (Elm2);
1164 end loop;
1166 -- Now we replace the call with the sequence
1168 -- if no-changes (see above) then
1169 -- raise Storage_Error;
1170 -- else
1171 -- original-call
1172 -- end if;
1174 Rewrite (Node (Call),
1175 Make_If_Statement (Loc,
1176 Condition => Test,
1177 Then_Statements => New_List (
1178 Make_Raise_Storage_Error (Loc,
1179 Reason => SE_Infinite_Recursion)),
1181 Else_Statements => New_List (
1182 Relocate_Node (Node (Call)))));
1184 Analyze (Node (Call));
1186 Next_Elmt (Call);
1187 end loop;
1189 -- Remove temporary scope stack entry used for analysis
1191 Pop_Scope;
1192 end Detect_Infinite_Recursion;
1194 --------------------
1195 -- Expand_Actuals --
1196 --------------------
1198 procedure Expand_Actuals
1199 (N : Node_Id;
1200 Subp : Entity_Id;
1201 Post_Call : out List_Id)
1203 Loc : constant Source_Ptr := Sloc (N);
1204 Actual : Node_Id;
1205 Formal : Entity_Id;
1206 N_Node : Node_Id;
1207 E_Actual : Entity_Id;
1208 E_Formal : Entity_Id;
1210 procedure Add_Call_By_Copy_Code;
1211 -- For cases where the parameter must be passed by copy, this routine
1212 -- generates a temporary variable into which the actual is copied and
1213 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1214 -- an assignment is also generated to copy the result back. The call
1215 -- also takes care of any constraint checks required for the type
1216 -- conversion case (on both the way in and the way out).
1218 procedure Add_Simple_Call_By_Copy_Code;
1219 -- This is similar to the above, but is used in cases where we know
1220 -- that all that is needed is to simply create a temporary and copy
1221 -- the value in and out of the temporary.
1223 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1224 -- Perform copy-back for actual parameter Act which denotes a validation
1225 -- variable.
1227 procedure Check_Fortran_Logical;
1228 -- A value of type Logical that is passed through a formal parameter
1229 -- must be normalized because .TRUE. usually does not have the same
1230 -- representation as True. We assume that .FALSE. = False = 0.
1231 -- What about functions that return a logical type ???
1233 function Is_Legal_Copy return Boolean;
1234 -- Check that an actual can be copied before generating the temporary
1235 -- to be used in the call. If the actual is of a by_reference type then
1236 -- the program is illegal (this can only happen in the presence of
1237 -- rep. clauses that force an incorrect alignment). If the formal is
1238 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1239 -- the effect that this might lead to unaligned arguments.
1241 function Make_Var (Actual : Node_Id) return Entity_Id;
1242 -- Returns an entity that refers to the given actual parameter, Actual
1243 -- (not including any type conversion). If Actual is an entity name,
1244 -- then this entity is returned unchanged, otherwise a renaming is
1245 -- created to provide an entity for the actual.
1247 procedure Reset_Packed_Prefix;
1248 -- The expansion of a packed array component reference is delayed in
1249 -- the context of a call. Now we need to complete the expansion, so we
1250 -- unmark the analyzed bits in all prefixes.
1252 ---------------------------
1253 -- Add_Call_By_Copy_Code --
1254 ---------------------------
1256 procedure Add_Call_By_Copy_Code is
1257 Crep : Boolean;
1258 Expr : Node_Id;
1259 F_Typ : Entity_Id := Etype (Formal);
1260 Indic : Node_Id;
1261 Init : Node_Id;
1262 Temp : Entity_Id;
1263 V_Typ : Entity_Id;
1264 Var : Entity_Id;
1266 begin
1267 if not Is_Legal_Copy then
1268 return;
1269 end if;
1271 Temp := Make_Temporary (Loc, 'T', Actual);
1273 -- Handle formals whose type comes from the limited view
1275 if From_Limited_With (F_Typ)
1276 and then Has_Non_Limited_View (F_Typ)
1277 then
1278 F_Typ := Non_Limited_View (F_Typ);
1279 end if;
1281 -- Use formal type for temp, unless formal type is an unconstrained
1282 -- array, in which case we don't have to worry about bounds checks,
1283 -- and we use the actual type, since that has appropriate bounds.
1285 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1286 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1287 else
1288 Indic := New_Occurrence_Of (F_Typ, Loc);
1289 end if;
1291 if Nkind (Actual) = N_Type_Conversion then
1292 V_Typ := Etype (Expression (Actual));
1294 -- If the formal is an (in-)out parameter, capture the name
1295 -- of the variable in order to build the post-call assignment.
1297 Var := Make_Var (Expression (Actual));
1299 Crep := not Same_Representation
1300 (F_Typ, Etype (Expression (Actual)));
1302 else
1303 V_Typ := Etype (Actual);
1304 Var := Make_Var (Actual);
1305 Crep := False;
1306 end if;
1308 -- Setup initialization for case of in out parameter, or an out
1309 -- parameter where the formal is an unconstrained array (in the
1310 -- latter case, we have to pass in an object with bounds).
1312 -- If this is an out parameter, the initial copy is wasteful, so as
1313 -- an optimization for the one-dimensional case we extract the
1314 -- bounds of the actual and build an uninitialized temporary of the
1315 -- right size.
1317 if Ekind (Formal) = E_In_Out_Parameter
1318 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1319 then
1320 if Nkind (Actual) = N_Type_Conversion then
1321 if Conversion_OK (Actual) then
1322 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1323 else
1324 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1325 end if;
1327 elsif Ekind (Formal) = E_Out_Parameter
1328 and then Is_Array_Type (F_Typ)
1329 and then Number_Dimensions (F_Typ) = 1
1330 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1331 then
1332 -- Actual is a one-dimensional array or slice, and the type
1333 -- requires no initialization. Create a temporary of the
1334 -- right size, but do not copy actual into it (optimization).
1336 Init := Empty;
1337 Indic :=
1338 Make_Subtype_Indication (Loc,
1339 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1340 Constraint =>
1341 Make_Index_Or_Discriminant_Constraint (Loc,
1342 Constraints => New_List (
1343 Make_Range (Loc,
1344 Low_Bound =>
1345 Make_Attribute_Reference (Loc,
1346 Prefix => New_Occurrence_Of (Var, Loc),
1347 Attribute_Name => Name_First),
1348 High_Bound =>
1349 Make_Attribute_Reference (Loc,
1350 Prefix => New_Occurrence_Of (Var, Loc),
1351 Attribute_Name => Name_Last)))));
1353 else
1354 Init := New_Occurrence_Of (Var, Loc);
1355 end if;
1357 -- An initialization is created for packed conversions as
1358 -- actuals for out parameters to enable Make_Object_Declaration
1359 -- to determine the proper subtype for N_Node. Note that this
1360 -- is wasteful because the extra copying on the call side is
1361 -- not required for such out parameters. ???
1363 elsif Ekind (Formal) = E_Out_Parameter
1364 and then Nkind (Actual) = N_Type_Conversion
1365 and then (Is_Bit_Packed_Array (F_Typ)
1366 or else
1367 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1368 then
1369 if Conversion_OK (Actual) then
1370 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1371 else
1372 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1373 end if;
1375 elsif Ekind (Formal) = E_In_Parameter then
1377 -- Handle the case in which the actual is a type conversion
1379 if Nkind (Actual) = N_Type_Conversion then
1380 if Conversion_OK (Actual) then
1381 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1382 else
1383 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1384 end if;
1385 else
1386 Init := New_Occurrence_Of (Var, Loc);
1387 end if;
1389 else
1390 Init := Empty;
1391 end if;
1393 N_Node :=
1394 Make_Object_Declaration (Loc,
1395 Defining_Identifier => Temp,
1396 Object_Definition => Indic,
1397 Expression => Init);
1398 Set_Assignment_OK (N_Node);
1399 Insert_Action (N, N_Node);
1401 -- Now, normally the deal here is that we use the defining
1402 -- identifier created by that object declaration. There is
1403 -- one exception to this. In the change of representation case
1404 -- the above declaration will end up looking like:
1406 -- temp : type := identifier;
1408 -- And in this case we might as well use the identifier directly
1409 -- and eliminate the temporary. Note that the analysis of the
1410 -- declaration was not a waste of time in that case, since it is
1411 -- what generated the necessary change of representation code. If
1412 -- the change of representation introduced additional code, as in
1413 -- a fixed-integer conversion, the expression is not an identifier
1414 -- and must be kept.
1416 if Crep
1417 and then Present (Expression (N_Node))
1418 and then Is_Entity_Name (Expression (N_Node))
1419 then
1420 Temp := Entity (Expression (N_Node));
1421 Rewrite (N_Node, Make_Null_Statement (Loc));
1422 end if;
1424 -- For IN parameter, all we do is to replace the actual
1426 if Ekind (Formal) = E_In_Parameter then
1427 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1428 Analyze (Actual);
1430 -- Processing for OUT or IN OUT parameter
1432 else
1433 -- Kill current value indications for the temporary variable we
1434 -- created, since we just passed it as an OUT parameter.
1436 Kill_Current_Values (Temp);
1437 Set_Is_Known_Valid (Temp, False);
1439 -- If type conversion, use reverse conversion on exit
1441 if Nkind (Actual) = N_Type_Conversion then
1442 if Conversion_OK (Actual) then
1443 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1444 else
1445 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1446 end if;
1447 else
1448 Expr := New_Occurrence_Of (Temp, Loc);
1449 end if;
1451 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1452 Analyze (Actual);
1454 -- If the actual is a conversion of a packed reference, it may
1455 -- already have been expanded by Remove_Side_Effects, and the
1456 -- resulting variable is a temporary which does not designate
1457 -- the proper out-parameter, which may not be addressable. In
1458 -- that case, generate an assignment to the original expression
1459 -- (before expansion of the packed reference) so that the proper
1460 -- expansion of assignment to a packed component can take place.
1462 declare
1463 Obj : Node_Id;
1464 Lhs : Node_Id;
1466 begin
1467 if Is_Renaming_Of_Object (Var)
1468 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1469 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1470 = N_Indexed_Component
1471 and then
1472 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1473 then
1474 Obj := Renamed_Object (Var);
1475 Lhs :=
1476 Make_Selected_Component (Loc,
1477 Prefix =>
1478 New_Copy_Tree (Original_Node (Prefix (Obj))),
1479 Selector_Name => New_Copy (Selector_Name (Obj)));
1480 Reset_Analyzed_Flags (Lhs);
1482 else
1483 Lhs := New_Occurrence_Of (Var, Loc);
1484 end if;
1486 Set_Assignment_OK (Lhs);
1488 if Is_Access_Type (E_Formal)
1489 and then Is_Entity_Name (Lhs)
1490 and then
1491 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1492 then
1493 -- Copyback target is an Ada 2012 stand-alone object of an
1494 -- anonymous access type.
1496 pragma Assert (Ada_Version >= Ada_2012);
1498 if Type_Access_Level (E_Formal) >
1499 Object_Access_Level (Lhs)
1500 then
1501 Append_To (Post_Call,
1502 Make_Raise_Program_Error (Loc,
1503 Reason => PE_Accessibility_Check_Failed));
1504 end if;
1506 Append_To (Post_Call,
1507 Make_Assignment_Statement (Loc,
1508 Name => Lhs,
1509 Expression => Expr));
1511 -- We would like to somehow suppress generation of the
1512 -- extra_accessibility assignment generated by the expansion
1513 -- of the above assignment statement. It's not a correctness
1514 -- issue because the following assignment renders it dead,
1515 -- but generating back-to-back assignments to the same
1516 -- target is undesirable. ???
1518 Append_To (Post_Call,
1519 Make_Assignment_Statement (Loc,
1520 Name => New_Occurrence_Of (
1521 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1522 Expression => Make_Integer_Literal (Loc,
1523 Type_Access_Level (E_Formal))));
1525 else
1526 Append_To (Post_Call,
1527 Make_Assignment_Statement (Loc,
1528 Name => Lhs,
1529 Expression => Expr));
1530 end if;
1531 end;
1532 end if;
1533 end Add_Call_By_Copy_Code;
1535 ----------------------------------
1536 -- Add_Simple_Call_By_Copy_Code --
1537 ----------------------------------
1539 procedure Add_Simple_Call_By_Copy_Code is
1540 Decl : Node_Id;
1541 F_Typ : Entity_Id := Etype (Formal);
1542 Incod : Node_Id;
1543 Indic : Node_Id;
1544 Lhs : Node_Id;
1545 Outcod : Node_Id;
1546 Rhs : Node_Id;
1547 Temp : Entity_Id;
1549 begin
1550 if not Is_Legal_Copy then
1551 return;
1552 end if;
1554 -- Handle formals whose type comes from the limited view
1556 if From_Limited_With (F_Typ)
1557 and then Has_Non_Limited_View (F_Typ)
1558 then
1559 F_Typ := Non_Limited_View (F_Typ);
1560 end if;
1562 -- Use formal type for temp, unless formal type is an unconstrained
1563 -- array, in which case we don't have to worry about bounds checks,
1564 -- and we use the actual type, since that has appropriate bounds.
1566 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1567 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1568 else
1569 Indic := New_Occurrence_Of (F_Typ, Loc);
1570 end if;
1572 -- Prepare to generate code
1574 Reset_Packed_Prefix;
1576 Temp := Make_Temporary (Loc, 'T', Actual);
1577 Incod := Relocate_Node (Actual);
1578 Outcod := New_Copy_Tree (Incod);
1580 -- Generate declaration of temporary variable, initializing it
1581 -- with the input parameter unless we have an OUT formal or
1582 -- this is an initialization call.
1584 -- If the formal is an out parameter with discriminants, the
1585 -- discriminants must be captured even if the rest of the object
1586 -- is in principle uninitialized, because the discriminants may
1587 -- be read by the called subprogram.
1589 if Ekind (Formal) = E_Out_Parameter then
1590 Incod := Empty;
1592 if Has_Discriminants (F_Typ) then
1593 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1594 end if;
1596 elsif Inside_Init_Proc then
1598 -- Could use a comment here to match comment below ???
1600 if Nkind (Actual) /= N_Selected_Component
1601 or else
1602 not Has_Discriminant_Dependent_Constraint
1603 (Entity (Selector_Name (Actual)))
1604 then
1605 Incod := Empty;
1607 -- Otherwise, keep the component in order to generate the proper
1608 -- actual subtype, that depends on enclosing discriminants.
1610 else
1611 null;
1612 end if;
1613 end if;
1615 Decl :=
1616 Make_Object_Declaration (Loc,
1617 Defining_Identifier => Temp,
1618 Object_Definition => Indic,
1619 Expression => Incod);
1621 if Inside_Init_Proc
1622 and then No (Incod)
1623 then
1624 -- If the call is to initialize a component of a composite type,
1625 -- and the component does not depend on discriminants, use the
1626 -- actual type of the component. This is required in case the
1627 -- component is constrained, because in general the formal of the
1628 -- initialization procedure will be unconstrained. Note that if
1629 -- the component being initialized is constrained by an enclosing
1630 -- discriminant, the presence of the initialization in the
1631 -- declaration will generate an expression for the actual subtype.
1633 Set_No_Initialization (Decl);
1634 Set_Object_Definition (Decl,
1635 New_Occurrence_Of (Etype (Actual), Loc));
1636 end if;
1638 Insert_Action (N, Decl);
1640 -- The actual is simply a reference to the temporary
1642 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1644 -- Generate copy out if OUT or IN OUT parameter
1646 if Ekind (Formal) /= E_In_Parameter then
1647 Lhs := Outcod;
1648 Rhs := New_Occurrence_Of (Temp, Loc);
1650 -- Deal with conversion
1652 if Nkind (Lhs) = N_Type_Conversion then
1653 Lhs := Expression (Lhs);
1654 Rhs := Convert_To (Etype (Actual), Rhs);
1655 end if;
1657 Append_To (Post_Call,
1658 Make_Assignment_Statement (Loc,
1659 Name => Lhs,
1660 Expression => Rhs));
1661 Set_Assignment_OK (Name (Last (Post_Call)));
1662 end if;
1663 end Add_Simple_Call_By_Copy_Code;
1665 --------------------------------------
1666 -- Add_Validation_Call_By_Copy_Code --
1667 --------------------------------------
1669 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
1670 Expr : Node_Id;
1671 Obj : Node_Id;
1672 Obj_Typ : Entity_Id;
1673 Var : constant Node_Id := Unqual_Conv (Act);
1674 Var_Id : Entity_Id;
1676 begin
1677 -- Copy the value of the validation variable back into the object
1678 -- being validated.
1680 if Is_Entity_Name (Var) then
1681 Var_Id := Entity (Var);
1682 Obj := Validated_Object (Var_Id);
1683 Obj_Typ := Etype (Obj);
1685 Expr := New_Occurrence_Of (Var_Id, Loc);
1687 -- A type conversion is needed when the validation variable and
1688 -- the validated object carry different types. This case occurs
1689 -- when the actual is qualified in some fashion.
1691 -- Common:
1692 -- subtype Int is Integer range ...;
1693 -- procedure Call (Val : in out Integer);
1695 -- Original:
1696 -- Object : Int;
1697 -- Call (Integer (Object));
1699 -- Expanded:
1700 -- Object : Int;
1701 -- Var : Integer := Object; -- conversion to base type
1702 -- if not Var'Valid then -- validity check
1703 -- Call (Var); -- modify Var
1704 -- Object := Int (Var); -- conversion to subtype
1706 if Etype (Var_Id) /= Obj_Typ then
1707 Expr :=
1708 Make_Type_Conversion (Loc,
1709 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
1710 Expression => Expr);
1711 end if;
1713 -- Generate:
1714 -- Object := Var;
1715 -- <or>
1716 -- Object := Object_Type (Var);
1718 Append_To (Post_Call,
1719 Make_Assignment_Statement (Loc,
1720 Name => Obj,
1721 Expression => Expr));
1723 -- If the flow reaches this point, then this routine was invoked with
1724 -- an actual which does not denote a validation variable.
1726 else
1727 pragma Assert (False);
1728 null;
1729 end if;
1730 end Add_Validation_Call_By_Copy_Code;
1732 ---------------------------
1733 -- Check_Fortran_Logical --
1734 ---------------------------
1736 procedure Check_Fortran_Logical is
1737 Logical : constant Entity_Id := Etype (Formal);
1738 Var : Entity_Id;
1740 -- Note: this is very incomplete, e.g. it does not handle arrays
1741 -- of logical values. This is really not the right approach at all???)
1743 begin
1744 if Convention (Subp) = Convention_Fortran
1745 and then Root_Type (Etype (Formal)) = Standard_Boolean
1746 and then Ekind (Formal) /= E_In_Parameter
1747 then
1748 Var := Make_Var (Actual);
1749 Append_To (Post_Call,
1750 Make_Assignment_Statement (Loc,
1751 Name => New_Occurrence_Of (Var, Loc),
1752 Expression =>
1753 Unchecked_Convert_To (
1754 Logical,
1755 Make_Op_Ne (Loc,
1756 Left_Opnd => New_Occurrence_Of (Var, Loc),
1757 Right_Opnd =>
1758 Unchecked_Convert_To (
1759 Logical,
1760 New_Occurrence_Of (Standard_False, Loc))))));
1761 end if;
1762 end Check_Fortran_Logical;
1764 -------------------
1765 -- Is_Legal_Copy --
1766 -------------------
1768 function Is_Legal_Copy return Boolean is
1769 begin
1770 -- An attempt to copy a value of such a type can only occur if
1771 -- representation clauses give the actual a misaligned address.
1773 if Is_By_Reference_Type (Etype (Formal)) then
1775 -- The actual may in fact be properly aligned but there is not
1776 -- enough front-end information to determine this. In that case
1777 -- gigi will emit an error if a copy is not legal, or generate
1778 -- the proper code.
1780 return False;
1782 -- For users of Starlet, we assume that the specification of by-
1783 -- reference mechanism is mandatory. This may lead to unaligned
1784 -- objects but at least for DEC legacy code it is known to work.
1785 -- The warning will alert users of this code that a problem may
1786 -- be lurking.
1788 elsif Mechanism (Formal) = By_Reference
1789 and then Is_Valued_Procedure (Scope (Formal))
1790 then
1791 Error_Msg_N
1792 ("by_reference actual may be misaligned??", Actual);
1793 return False;
1795 else
1796 return True;
1797 end if;
1798 end Is_Legal_Copy;
1800 --------------
1801 -- Make_Var --
1802 --------------
1804 function Make_Var (Actual : Node_Id) return Entity_Id is
1805 Var : Entity_Id;
1807 begin
1808 if Is_Entity_Name (Actual) then
1809 return Entity (Actual);
1811 else
1812 Var := Make_Temporary (Loc, 'T', Actual);
1814 N_Node :=
1815 Make_Object_Renaming_Declaration (Loc,
1816 Defining_Identifier => Var,
1817 Subtype_Mark =>
1818 New_Occurrence_Of (Etype (Actual), Loc),
1819 Name => Relocate_Node (Actual));
1821 Insert_Action (N, N_Node);
1822 return Var;
1823 end if;
1824 end Make_Var;
1826 -------------------------
1827 -- Reset_Packed_Prefix --
1828 -------------------------
1830 procedure Reset_Packed_Prefix is
1831 Pfx : Node_Id := Actual;
1832 begin
1833 loop
1834 Set_Analyzed (Pfx, False);
1835 exit when
1836 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1837 Pfx := Prefix (Pfx);
1838 end loop;
1839 end Reset_Packed_Prefix;
1841 -- Start of processing for Expand_Actuals
1843 begin
1844 Post_Call := New_List;
1846 Formal := First_Formal (Subp);
1847 Actual := First_Actual (N);
1848 while Present (Formal) loop
1849 E_Formal := Etype (Formal);
1850 E_Actual := Etype (Actual);
1852 -- Handle formals whose type comes from the limited view
1854 if From_Limited_With (E_Formal)
1855 and then Has_Non_Limited_View (E_Formal)
1856 then
1857 E_Formal := Non_Limited_View (E_Formal);
1858 end if;
1860 if Is_Scalar_Type (E_Formal)
1861 or else Nkind (Actual) = N_Slice
1862 then
1863 Check_Fortran_Logical;
1865 -- RM 6.4.1 (11)
1867 elsif Ekind (Formal) /= E_Out_Parameter then
1869 -- The unusual case of the current instance of a protected type
1870 -- requires special handling. This can only occur in the context
1871 -- of a call within the body of a protected operation.
1873 if Is_Entity_Name (Actual)
1874 and then Ekind (Entity (Actual)) = E_Protected_Type
1875 and then In_Open_Scopes (Entity (Actual))
1876 then
1877 if Scope (Subp) /= Entity (Actual) then
1878 Error_Msg_N
1879 ("operation outside protected type may not "
1880 & "call back its protected operations??", Actual);
1881 end if;
1883 Rewrite (Actual,
1884 Expand_Protected_Object_Reference (N, Entity (Actual)));
1885 end if;
1887 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1888 -- build-in-place function, then a temporary return object needs
1889 -- to be created and access to it must be passed to the function.
1890 -- Currently we limit such functions to those with inherently
1891 -- limited result subtypes, but eventually we plan to expand the
1892 -- functions that are treated as build-in-place to include other
1893 -- composite result types.
1895 if Is_Build_In_Place_Function_Call (Actual) then
1896 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1898 -- Ada 2005 (AI-318-02): Specialization of the previous case for
1899 -- actuals containing build-in-place function calls whose returned
1900 -- object covers interface types.
1902 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
1903 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
1904 end if;
1906 Apply_Constraint_Check (Actual, E_Formal);
1908 -- Out parameter case. No constraint checks on access type
1909 -- RM 6.4.1 (13)
1911 elsif Is_Access_Type (E_Formal) then
1912 null;
1914 -- RM 6.4.1 (14)
1916 elsif Has_Discriminants (Base_Type (E_Formal))
1917 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1918 then
1919 Apply_Constraint_Check (Actual, E_Formal);
1921 -- RM 6.4.1 (15)
1923 else
1924 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1925 end if;
1927 -- Processing for IN-OUT and OUT parameters
1929 if Ekind (Formal) /= E_In_Parameter then
1931 -- For type conversions of arrays, apply length/range checks
1933 if Is_Array_Type (E_Formal)
1934 and then Nkind (Actual) = N_Type_Conversion
1935 then
1936 if Is_Constrained (E_Formal) then
1937 Apply_Length_Check (Expression (Actual), E_Formal);
1938 else
1939 Apply_Range_Check (Expression (Actual), E_Formal);
1940 end if;
1941 end if;
1943 -- The actual denotes a variable which captures the value of an
1944 -- object for validation purposes. Add a copy-back to reflect any
1945 -- potential changes in value back into the original object.
1947 -- Var : ... := Object;
1948 -- if not Var'Valid then -- validity check
1949 -- Call (Var); -- modify var
1950 -- Object := Var; -- update Object
1952 -- This case is given higher priority because the subsequent check
1953 -- for type conversion may add an extra copy of the variable and
1954 -- prevent proper value propagation back in the original object.
1956 if Is_Validation_Variable_Reference (Actual) then
1957 Add_Validation_Call_By_Copy_Code (Actual);
1959 -- If argument is a type conversion for a type that is passed by
1960 -- copy, then we must pass the parameter by copy.
1962 elsif Nkind (Actual) = N_Type_Conversion
1963 and then
1964 (Is_Numeric_Type (E_Formal)
1965 or else Is_Access_Type (E_Formal)
1966 or else Is_Enumeration_Type (E_Formal)
1967 or else Is_Bit_Packed_Array (Etype (Formal))
1968 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1970 -- Also pass by copy if change of representation
1972 or else not Same_Representation
1973 (Etype (Formal),
1974 Etype (Expression (Actual))))
1975 then
1976 Add_Call_By_Copy_Code;
1978 -- References to components of bit-packed arrays are expanded
1979 -- at this point, rather than at the point of analysis of the
1980 -- actuals, to handle the expansion of the assignment to
1981 -- [in] out parameters.
1983 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1984 Add_Simple_Call_By_Copy_Code;
1986 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1987 -- because the back-end cannot cope with such objects. In other
1988 -- cases where alignment forces a copy, the back-end generates
1989 -- it properly. It should not be generated unconditionally in the
1990 -- front-end because it does not know precisely the alignment
1991 -- requirements of the target, and makes too conservative an
1992 -- estimate, leading to superfluous copies or spurious errors
1993 -- on by-reference parameters.
1995 elsif Nkind (Actual) = N_Selected_Component
1996 and then
1997 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1998 and then not Represented_As_Scalar (Etype (Formal))
1999 then
2000 Add_Simple_Call_By_Copy_Code;
2002 -- References to slices of bit-packed arrays are expanded
2004 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2005 Add_Call_By_Copy_Code;
2007 -- References to possibly unaligned slices of arrays are expanded
2009 elsif Is_Possibly_Unaligned_Slice (Actual) then
2010 Add_Call_By_Copy_Code;
2012 -- Deal with access types where the actual subtype and the
2013 -- formal subtype are not the same, requiring a check.
2015 -- It is necessary to exclude tagged types because of "downward
2016 -- conversion" errors.
2018 elsif Is_Access_Type (E_Formal)
2019 and then not Same_Type (E_Formal, E_Actual)
2020 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2021 then
2022 Add_Call_By_Copy_Code;
2024 -- If the actual is not a scalar and is marked for volatile
2025 -- treatment, whereas the formal is not volatile, then pass
2026 -- by copy unless it is a by-reference type.
2028 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
2029 -- because this is the enforcement of a language rule that applies
2030 -- only to "real" volatile variables, not e.g. to the address
2031 -- clause overlay case.
2033 elsif Is_Entity_Name (Actual)
2034 and then Is_Volatile (Entity (Actual))
2035 and then not Is_By_Reference_Type (E_Actual)
2036 and then not Is_Scalar_Type (Etype (Entity (Actual)))
2037 and then not Is_Volatile (E_Formal)
2038 then
2039 Add_Call_By_Copy_Code;
2041 elsif Nkind (Actual) = N_Indexed_Component
2042 and then Is_Entity_Name (Prefix (Actual))
2043 and then Has_Volatile_Components (Entity (Prefix (Actual)))
2044 then
2045 Add_Call_By_Copy_Code;
2047 -- Add call-by-copy code for the case of scalar out parameters
2048 -- when it is not known at compile time that the subtype of the
2049 -- formal is a subrange of the subtype of the actual (or vice
2050 -- versa for in out parameters), in order to get range checks
2051 -- on such actuals. (Maybe this case should be handled earlier
2052 -- in the if statement???)
2054 elsif Is_Scalar_Type (E_Formal)
2055 and then
2056 (not In_Subrange_Of (E_Formal, E_Actual)
2057 or else
2058 (Ekind (Formal) = E_In_Out_Parameter
2059 and then not In_Subrange_Of (E_Actual, E_Formal)))
2060 then
2061 -- Perhaps the setting back to False should be done within
2062 -- Add_Call_By_Copy_Code, since it could get set on other
2063 -- cases occurring above???
2065 if Do_Range_Check (Actual) then
2066 Set_Do_Range_Check (Actual, False);
2067 end if;
2069 Add_Call_By_Copy_Code;
2070 end if;
2072 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2073 -- by-reference parameters on exit from the call. If the actual
2074 -- is a derived type and the operation is inherited, the body
2075 -- of the operation will not contain a call to the predicate
2076 -- function, so it must be done explicitly after the call. Ditto
2077 -- if the actual is an entity of a predicated subtype.
2079 -- The rule refers to by-reference types, but a check is needed
2080 -- for by-copy types as well. That check is subsumed by the rule
2081 -- for subtype conversion on assignment, but we can generate the
2082 -- required check now.
2084 -- Note also that Subp may be either a subprogram entity for
2085 -- direct calls, or a type entity for indirect calls, which must
2086 -- be handled separately because the name does not denote an
2087 -- overloadable entity.
2089 By_Ref_Predicate_Check : declare
2090 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2091 Atyp : Entity_Id;
2093 function Is_Public_Subp return Boolean;
2094 -- Check whether the subprogram being called is a visible
2095 -- operation of the type of the actual. Used to determine
2096 -- whether an invariant check must be generated on the
2097 -- caller side.
2099 ---------------------
2100 -- Is_Public_Subp --
2101 ---------------------
2103 function Is_Public_Subp return Boolean is
2104 Pack : constant Entity_Id := Scope (Subp);
2105 Subp_Decl : Node_Id;
2107 begin
2108 if not Is_Subprogram (Subp) then
2109 return False;
2111 -- The operation may be inherited, or a primitive of the
2112 -- root type.
2114 elsif
2115 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
2116 N_Full_Type_Declaration)
2117 then
2118 Subp_Decl := Parent (Subp);
2120 else
2121 Subp_Decl := Unit_Declaration_Node (Subp);
2122 end if;
2124 return Ekind (Pack) = E_Package
2125 and then
2126 List_Containing (Subp_Decl) =
2127 Visible_Declarations
2128 (Specification (Unit_Declaration_Node (Pack)));
2129 end Is_Public_Subp;
2131 -- Start of processing for By_Ref_Predicate_Check
2133 begin
2134 if No (Aund) then
2135 Atyp := E_Actual;
2136 else
2137 Atyp := Aund;
2138 end if;
2140 if Has_Predicates (Atyp)
2141 and then Present (Predicate_Function (Atyp))
2143 -- Skip predicate checks for special cases
2145 and then Predicate_Tests_On_Arguments (Subp)
2146 then
2147 Append_To (Post_Call,
2148 Make_Predicate_Check (Atyp, Actual));
2149 end if;
2151 -- We generated caller-side invariant checks in two cases:
2153 -- a) when calling an inherited operation, where there is an
2154 -- implicit view conversion of the actual to the parent type.
2156 -- b) When the conversion is explicit
2158 -- We treat these cases separately because the required
2159 -- conversion for a) is added later when expanding the call.
2161 if Has_Invariants (Etype (Actual))
2162 and then
2163 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2164 then
2165 if Comes_From_Source (N) and then Is_Public_Subp then
2166 Append_To (Post_Call, Make_Invariant_Call (Actual));
2167 end if;
2169 elsif Nkind (Actual) = N_Type_Conversion
2170 and then Has_Invariants (Etype (Expression (Actual)))
2171 then
2172 if Comes_From_Source (N) and then Is_Public_Subp then
2173 Append_To (Post_Call,
2174 Make_Invariant_Call (Expression (Actual)));
2175 end if;
2176 end if;
2177 end By_Ref_Predicate_Check;
2179 -- Processing for IN parameters
2181 else
2182 -- For IN parameters in the bit-packed array case, we expand an
2183 -- indexed component (the circuit in Exp_Ch4 deliberately left
2184 -- indexed components appearing as actuals untouched, so that
2185 -- the special processing above for the OUT and IN OUT cases
2186 -- could be performed. We could make the test in Exp_Ch4 more
2187 -- complex and have it detect the parameter mode, but it is
2188 -- easier simply to handle all cases here.)
2190 if Nkind (Actual) = N_Indexed_Component
2191 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2192 then
2193 Reset_Packed_Prefix;
2194 Expand_Packed_Element_Reference (Actual);
2196 -- If we have a reference to a bit-packed array, we copy it, since
2197 -- the actual must be byte aligned.
2199 -- Is this really necessary in all cases???
2201 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2202 Add_Simple_Call_By_Copy_Code;
2204 -- If a non-scalar actual is possibly unaligned, we need a copy
2206 elsif Is_Possibly_Unaligned_Object (Actual)
2207 and then not Represented_As_Scalar (Etype (Formal))
2208 then
2209 Add_Simple_Call_By_Copy_Code;
2211 -- Similarly, we have to expand slices of packed arrays here
2212 -- because the result must be byte aligned.
2214 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2215 Add_Call_By_Copy_Code;
2217 -- Only processing remaining is to pass by copy if this is a
2218 -- reference to a possibly unaligned slice, since the caller
2219 -- expects an appropriately aligned argument.
2221 elsif Is_Possibly_Unaligned_Slice (Actual) then
2222 Add_Call_By_Copy_Code;
2224 -- An unusual case: a current instance of an enclosing task can be
2225 -- an actual, and must be replaced by a reference to self.
2227 elsif Is_Entity_Name (Actual)
2228 and then Is_Task_Type (Entity (Actual))
2229 then
2230 if In_Open_Scopes (Entity (Actual)) then
2231 Rewrite (Actual,
2232 (Make_Function_Call (Loc,
2233 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2234 Analyze (Actual);
2236 -- A task type cannot otherwise appear as an actual
2238 else
2239 raise Program_Error;
2240 end if;
2241 end if;
2242 end if;
2244 Next_Formal (Formal);
2245 Next_Actual (Actual);
2246 end loop;
2247 end Expand_Actuals;
2249 -----------------
2250 -- Expand_Call --
2251 -----------------
2253 procedure Expand_Call (N : Node_Id) is
2254 Post_Call : List_Id;
2256 begin
2257 pragma Assert (Nkind_In (N, N_Entry_Call_Statement,
2258 N_Function_Call,
2259 N_Procedure_Call_Statement));
2261 Expand_Call_Helper (N, Post_Call);
2262 Insert_Post_Call_Actions (N, Post_Call);
2263 end Expand_Call;
2265 ------------------------
2266 -- Expand_Call_Helper --
2267 ------------------------
2269 -- This procedure handles expansion of function calls and procedure call
2270 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2271 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2273 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2274 -- Provide values of actuals for all formals in Extra_Formals list
2275 -- Replace "call" to enumeration literal function by literal itself
2276 -- Rewrite call to predefined operator as operator
2277 -- Replace actuals to in-out parameters that are numeric conversions,
2278 -- with explicit assignment to temporaries before and after the call.
2280 -- Note that the list of actuals has been filled with default expressions
2281 -- during semantic analysis of the call. Only the extra actuals required
2282 -- for the 'Constrained attribute and for accessibility checks are added
2283 -- at this point.
2285 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2286 Loc : constant Source_Ptr := Sloc (N);
2287 Call_Node : Node_Id := N;
2288 Extra_Actuals : List_Id := No_List;
2289 Prev : Node_Id := Empty;
2291 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2292 -- Adds one entry to the end of the actual parameter list. Used for
2293 -- default parameters and for extra actuals (for Extra_Formals). The
2294 -- argument is an N_Parameter_Association node.
2296 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2297 -- Adds an extra actual to the list of extra actuals. Expr is the
2298 -- expression for the value of the actual, EF is the entity for the
2299 -- extra formal.
2301 procedure Add_View_Conversion_Invariants
2302 (Formal : Entity_Id;
2303 Actual : Node_Id);
2304 -- Adds invariant checks for every intermediate type between the range
2305 -- of a view converted argument to its ancestor (from parent to child).
2307 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2308 -- Within an instance, a type derived from an untagged formal derived
2309 -- type inherits from the original parent, not from the actual. The
2310 -- current derivation mechanism has the derived type inherit from the
2311 -- actual, which is only correct outside of the instance. If the
2312 -- subprogram is inherited, we test for this particular case through a
2313 -- convoluted tree traversal before setting the proper subprogram to be
2314 -- called.
2316 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2317 -- Return true if E comes from an instance that is not yet frozen
2319 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2320 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2322 function New_Value (From : Node_Id) return Node_Id;
2323 -- From is the original Expression. New_Value is equivalent to a call
2324 -- to Duplicate_Subexpr with an explicit dereference when From is an
2325 -- access parameter.
2327 --------------------------
2328 -- Add_Actual_Parameter --
2329 --------------------------
2331 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2332 Actual_Expr : constant Node_Id :=
2333 Explicit_Actual_Parameter (Insert_Param);
2335 begin
2336 -- Case of insertion is first named actual
2338 if No (Prev) or else
2339 Nkind (Parent (Prev)) /= N_Parameter_Association
2340 then
2341 Set_Next_Named_Actual
2342 (Insert_Param, First_Named_Actual (Call_Node));
2343 Set_First_Named_Actual (Call_Node, Actual_Expr);
2345 if No (Prev) then
2346 if No (Parameter_Associations (Call_Node)) then
2347 Set_Parameter_Associations (Call_Node, New_List);
2348 end if;
2350 Append (Insert_Param, Parameter_Associations (Call_Node));
2352 else
2353 Insert_After (Prev, Insert_Param);
2354 end if;
2356 -- Case of insertion is not first named actual
2358 else
2359 Set_Next_Named_Actual
2360 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2361 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2362 Append (Insert_Param, Parameter_Associations (Call_Node));
2363 end if;
2365 Prev := Actual_Expr;
2366 end Add_Actual_Parameter;
2368 ----------------------
2369 -- Add_Extra_Actual --
2370 ----------------------
2372 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2373 Loc : constant Source_Ptr := Sloc (Expr);
2375 begin
2376 if Extra_Actuals = No_List then
2377 Extra_Actuals := New_List;
2378 Set_Parent (Extra_Actuals, Call_Node);
2379 end if;
2381 Append_To (Extra_Actuals,
2382 Make_Parameter_Association (Loc,
2383 Selector_Name => New_Occurrence_Of (EF, Loc),
2384 Explicit_Actual_Parameter => Expr));
2386 Analyze_And_Resolve (Expr, Etype (EF));
2388 if Nkind (Call_Node) = N_Function_Call then
2389 Set_Is_Accessibility_Actual (Parent (Expr));
2390 end if;
2391 end Add_Extra_Actual;
2393 ------------------------------------
2394 -- Add_View_Conversion_Invariants --
2395 ------------------------------------
2397 procedure Add_View_Conversion_Invariants
2398 (Formal : Entity_Id;
2399 Actual : Node_Id)
2401 Arg : Entity_Id;
2402 Curr_Typ : Entity_Id;
2403 Inv_Checks : List_Id;
2404 Par_Typ : Entity_Id;
2406 begin
2407 Inv_Checks := No_List;
2409 -- Extract the argument from a potentially nested set of view
2410 -- conversions.
2412 Arg := Actual;
2413 while Nkind (Arg) = N_Type_Conversion loop
2414 Arg := Expression (Arg);
2415 end loop;
2417 -- Move up the derivation chain starting with the type of the formal
2418 -- parameter down to the type of the actual object.
2420 Curr_Typ := Empty;
2421 Par_Typ := Etype (Arg);
2422 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2423 Curr_Typ := Par_Typ;
2425 if Has_Invariants (Curr_Typ)
2426 and then Present (Invariant_Procedure (Curr_Typ))
2427 then
2428 -- Verify the invariate of the current type. Generate:
2430 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2432 Prepend_New_To (Inv_Checks,
2433 Make_Procedure_Call_Statement (Loc,
2434 Name =>
2435 New_Occurrence_Of
2436 (Invariant_Procedure (Curr_Typ), Loc),
2437 Parameter_Associations => New_List (
2438 Make_Type_Conversion (Loc,
2439 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2440 Expression => New_Copy_Tree (Arg)))));
2441 end if;
2443 Par_Typ := Base_Type (Etype (Curr_Typ));
2444 end loop;
2446 if not Is_Empty_List (Inv_Checks) then
2447 Insert_Actions_After (N, Inv_Checks);
2448 end if;
2449 end Add_View_Conversion_Invariants;
2451 ---------------------------
2452 -- Inherited_From_Formal --
2453 ---------------------------
2455 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2456 Par : Entity_Id;
2457 Gen_Par : Entity_Id;
2458 Gen_Prim : Elist_Id;
2459 Elmt : Elmt_Id;
2460 Indic : Node_Id;
2462 begin
2463 -- If the operation is inherited, it is attached to the corresponding
2464 -- type derivation. If the parent in the derivation is a generic
2465 -- actual, it is a subtype of the actual, and we have to recover the
2466 -- original derived type declaration to find the proper parent.
2468 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2469 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2470 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2471 N_Derived_Type_Definition
2472 or else not In_Instance
2473 then
2474 return Empty;
2476 else
2477 Indic :=
2478 Subtype_Indication
2479 (Type_Definition (Original_Node (Parent (S))));
2481 if Nkind (Indic) = N_Subtype_Indication then
2482 Par := Entity (Subtype_Mark (Indic));
2483 else
2484 Par := Entity (Indic);
2485 end if;
2486 end if;
2488 if not Is_Generic_Actual_Type (Par)
2489 or else Is_Tagged_Type (Par)
2490 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2491 or else not In_Open_Scopes (Scope (Par))
2492 then
2493 return Empty;
2494 else
2495 Gen_Par := Generic_Parent_Type (Parent (Par));
2496 end if;
2498 -- If the actual has no generic parent type, the formal is not
2499 -- a formal derived type, so nothing to inherit.
2501 if No (Gen_Par) then
2502 return Empty;
2503 end if;
2505 -- If the generic parent type is still the generic type, this is a
2506 -- private formal, not a derived formal, and there are no operations
2507 -- inherited from the formal.
2509 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2510 return Empty;
2511 end if;
2513 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2515 Elmt := First_Elmt (Gen_Prim);
2516 while Present (Elmt) loop
2517 if Chars (Node (Elmt)) = Chars (S) then
2518 declare
2519 F1 : Entity_Id;
2520 F2 : Entity_Id;
2522 begin
2523 F1 := First_Formal (S);
2524 F2 := First_Formal (Node (Elmt));
2525 while Present (F1)
2526 and then Present (F2)
2527 loop
2528 if Etype (F1) = Etype (F2)
2529 or else Etype (F2) = Gen_Par
2530 then
2531 Next_Formal (F1);
2532 Next_Formal (F2);
2533 else
2534 Next_Elmt (Elmt);
2535 exit; -- not the right subprogram
2536 end if;
2538 return Node (Elmt);
2539 end loop;
2540 end;
2542 else
2543 Next_Elmt (Elmt);
2544 end if;
2545 end loop;
2547 raise Program_Error;
2548 end Inherited_From_Formal;
2550 --------------------------
2551 -- In_Unfrozen_Instance --
2552 --------------------------
2554 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2555 S : Entity_Id;
2557 begin
2558 S := E;
2559 while Present (S) and then S /= Standard_Standard loop
2560 if Is_Generic_Instance (S)
2561 and then Present (Freeze_Node (S))
2562 and then not Analyzed (Freeze_Node (S))
2563 then
2564 return True;
2565 end if;
2567 S := Scope (S);
2568 end loop;
2570 return False;
2571 end In_Unfrozen_Instance;
2573 -------------------------
2574 -- Is_Direct_Deep_Call --
2575 -------------------------
2577 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2578 begin
2579 if Is_TSS (Subp, TSS_Deep_Adjust)
2580 or else Is_TSS (Subp, TSS_Deep_Finalize)
2581 or else Is_TSS (Subp, TSS_Deep_Initialize)
2582 then
2583 declare
2584 Actual : Node_Id;
2585 Formal : Node_Id;
2587 begin
2588 Actual := First (Parameter_Associations (N));
2589 Formal := First_Formal (Subp);
2590 while Present (Actual)
2591 and then Present (Formal)
2592 loop
2593 if Nkind (Actual) = N_Identifier
2594 and then Is_Controlling_Actual (Actual)
2595 and then Etype (Actual) = Etype (Formal)
2596 then
2597 return True;
2598 end if;
2600 Next (Actual);
2601 Next_Formal (Formal);
2602 end loop;
2603 end;
2604 end if;
2606 return False;
2607 end Is_Direct_Deep_Call;
2609 ---------------
2610 -- New_Value --
2611 ---------------
2613 function New_Value (From : Node_Id) return Node_Id is
2614 Res : constant Node_Id := Duplicate_Subexpr (From);
2615 begin
2616 if Is_Access_Type (Etype (From)) then
2617 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2618 else
2619 return Res;
2620 end if;
2621 end New_Value;
2623 -- Local variables
2625 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2626 Actual : Node_Id;
2627 Formal : Entity_Id;
2628 Orig_Subp : Entity_Id := Empty;
2629 Param_Count : Natural := 0;
2630 Parent_Formal : Entity_Id;
2631 Parent_Subp : Entity_Id;
2632 Pref_Entity : Entity_Id;
2633 Scop : Entity_Id;
2634 Subp : Entity_Id;
2636 Prev_Orig : Node_Id;
2637 -- Original node for an actual, which may have been rewritten. If the
2638 -- actual is a function call that has been transformed from a selected
2639 -- component, the original node is unanalyzed. Otherwise, it carries
2640 -- semantic information used to generate additional actuals.
2642 CW_Interface_Formals_Present : Boolean := False;
2644 -- Start of processing for Expand_Call_Helper
2646 begin
2647 Post_Call := New_List;
2649 -- Expand the function or procedure call if the first actual has a
2650 -- declared dimension aspect, and the subprogram is declared in one
2651 -- of the dimension I/O packages.
2653 if Ada_Version >= Ada_2012
2654 and then
2655 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2656 and then Present (Parameter_Associations (Call_Node))
2657 then
2658 Expand_Put_Call_With_Symbol (Call_Node);
2659 end if;
2661 -- Ignore if previous error
2663 if Nkind (Call_Node) in N_Has_Etype
2664 and then Etype (Call_Node) = Any_Type
2665 then
2666 return;
2667 end if;
2669 -- Call using access to subprogram with explicit dereference
2671 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2672 Subp := Etype (Name (Call_Node));
2673 Parent_Subp := Empty;
2675 -- Case of call to simple entry, where the Name is a selected component
2676 -- whose prefix is the task, and whose selector name is the entry name
2678 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2679 Subp := Entity (Selector_Name (Name (Call_Node)));
2680 Parent_Subp := Empty;
2682 -- Case of call to member of entry family, where Name is an indexed
2683 -- component, with the prefix being a selected component giving the
2684 -- task and entry family name, and the index being the entry index.
2686 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2687 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2688 Parent_Subp := Empty;
2690 -- Normal case
2692 else
2693 Subp := Entity (Name (Call_Node));
2694 Parent_Subp := Alias (Subp);
2696 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2697 -- if we can tell that the first parameter cannot possibly be null.
2698 -- This improves efficiency by avoiding a run-time test.
2700 -- We do not do this if Raise_Exception_Always does not exist, which
2701 -- can happen in configurable run time profiles which provide only a
2702 -- Raise_Exception.
2704 if Is_RTE (Subp, RE_Raise_Exception)
2705 and then RTE_Available (RE_Raise_Exception_Always)
2706 then
2707 declare
2708 FA : constant Node_Id :=
2709 Original_Node (First_Actual (Call_Node));
2711 begin
2712 -- The case we catch is where the first argument is obtained
2713 -- using the Identity attribute (which must always be
2714 -- non-null).
2716 if Nkind (FA) = N_Attribute_Reference
2717 and then Attribute_Name (FA) = Name_Identity
2718 then
2719 Subp := RTE (RE_Raise_Exception_Always);
2720 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2721 end if;
2722 end;
2723 end if;
2725 if Ekind (Subp) = E_Entry then
2726 Parent_Subp := Empty;
2727 end if;
2728 end if;
2730 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2731 -- alternative in an asynchronous select or as an entry call in
2732 -- a conditional or timed select. Check whether the procedure call
2733 -- is a renaming of an entry and rewrite it as an entry call.
2735 if Ada_Version >= Ada_2005
2736 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2737 and then
2738 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2739 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2740 or else
2741 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2742 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2743 then
2744 declare
2745 Ren_Decl : Node_Id;
2746 Ren_Root : Entity_Id := Subp;
2748 begin
2749 -- This may be a chain of renamings, find the root
2751 if Present (Alias (Ren_Root)) then
2752 Ren_Root := Alias (Ren_Root);
2753 end if;
2755 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2756 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2758 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2759 Rewrite (Call_Node,
2760 Make_Entry_Call_Statement (Loc,
2761 Name =>
2762 New_Copy_Tree (Name (Ren_Decl)),
2763 Parameter_Associations =>
2764 New_Copy_List_Tree
2765 (Parameter_Associations (Call_Node))));
2767 return;
2768 end if;
2769 end if;
2770 end;
2771 end if;
2773 if Modify_Tree_For_C
2774 and then Nkind (Call_Node) = N_Function_Call
2775 and then Is_Entity_Name (Name (Call_Node))
2776 then
2777 declare
2778 Func_Id : constant Entity_Id :=
2779 Ultimate_Alias (Entity (Name (Call_Node)));
2780 begin
2781 -- When generating C code, transform a function call that returns
2782 -- a constrained array type into procedure form.
2784 if Rewritten_For_C (Func_Id) then
2786 -- For internally generated calls ensure that they reference
2787 -- the entity of the spec of the called function (needed since
2788 -- the expander may generate calls using the entity of their
2789 -- body). See for example Expand_Boolean_Operator().
2791 if not (Comes_From_Source (Call_Node))
2792 and then Nkind (Unit_Declaration_Node (Func_Id)) =
2793 N_Subprogram_Body
2794 then
2795 Set_Entity (Name (Call_Node),
2796 Corresponding_Function
2797 (Corresponding_Procedure (Func_Id)));
2798 end if;
2800 Rewrite_Function_Call_For_C (Call_Node);
2801 return;
2803 -- Also introduce a temporary for functions that return a record
2804 -- called within another procedure or function call, since records
2805 -- are passed by pointer in the generated C code, and we cannot
2806 -- take a pointer from a subprogram call.
2808 elsif Nkind (Parent (Call_Node)) in N_Subprogram_Call
2809 and then Is_Record_Type (Etype (Func_Id))
2810 then
2811 declare
2812 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
2813 Decl : Node_Id;
2815 begin
2816 -- Generate:
2817 -- Temp : ... := Func_Call (...);
2819 Decl :=
2820 Make_Object_Declaration (Loc,
2821 Defining_Identifier => Temp_Id,
2822 Object_Definition =>
2823 New_Occurrence_Of (Etype (Func_Id), Loc),
2824 Expression =>
2825 Make_Function_Call (Loc,
2826 Name =>
2827 New_Occurrence_Of (Func_Id, Loc),
2828 Parameter_Associations =>
2829 Parameter_Associations (Call_Node)));
2831 Insert_Action (Parent (Call_Node), Decl);
2832 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
2833 return;
2834 end;
2835 end if;
2836 end;
2837 end if;
2839 -- First step, compute extra actuals, corresponding to any Extra_Formals
2840 -- present. Note that we do not access Extra_Formals directly, instead
2841 -- we simply note the presence of the extra formals as we process the
2842 -- regular formals collecting corresponding actuals in Extra_Actuals.
2844 -- We also generate any required range checks for actuals for in formals
2845 -- as we go through the loop, since this is a convenient place to do it.
2846 -- (Though it seems that this would be better done in Expand_Actuals???)
2848 -- Special case: Thunks must not compute the extra actuals; they must
2849 -- just propagate to the target primitive their extra actuals.
2851 if Is_Thunk (Current_Scope)
2852 and then Thunk_Entity (Current_Scope) = Subp
2853 and then Present (Extra_Formals (Subp))
2854 then
2855 pragma Assert (Present (Extra_Formals (Current_Scope)));
2857 declare
2858 Target_Formal : Entity_Id;
2859 Thunk_Formal : Entity_Id;
2861 begin
2862 Target_Formal := Extra_Formals (Subp);
2863 Thunk_Formal := Extra_Formals (Current_Scope);
2864 while Present (Target_Formal) loop
2865 Add_Extra_Actual
2866 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
2867 EF => Thunk_Formal);
2869 Target_Formal := Extra_Formal (Target_Formal);
2870 Thunk_Formal := Extra_Formal (Thunk_Formal);
2871 end loop;
2873 while Is_Non_Empty_List (Extra_Actuals) loop
2874 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2875 end loop;
2877 Expand_Actuals (Call_Node, Subp, Post_Call);
2878 pragma Assert (Is_Empty_List (Post_Call));
2879 return;
2880 end;
2881 end if;
2883 Formal := First_Formal (Subp);
2884 Actual := First_Actual (Call_Node);
2885 Param_Count := 1;
2886 while Present (Formal) loop
2888 -- Generate range check if required
2890 if Do_Range_Check (Actual)
2891 and then Ekind (Formal) = E_In_Parameter
2892 then
2893 Generate_Range_Check
2894 (Actual, Etype (Formal), CE_Range_Check_Failed);
2895 end if;
2897 -- Prepare to examine current entry
2899 Prev := Actual;
2900 Prev_Orig := Original_Node (Prev);
2902 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2903 -- to expand it in a further round.
2905 CW_Interface_Formals_Present :=
2906 CW_Interface_Formals_Present
2907 or else
2908 (Is_Class_Wide_Type (Etype (Formal))
2909 and then Is_Interface (Etype (Etype (Formal))))
2910 or else
2911 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2912 and then Is_Class_Wide_Type (Directly_Designated_Type
2913 (Etype (Etype (Formal))))
2914 and then Is_Interface (Directly_Designated_Type
2915 (Etype (Etype (Formal)))));
2917 -- Create possible extra actual for constrained case. Usually, the
2918 -- extra actual is of the form actual'constrained, but since this
2919 -- attribute is only available for unconstrained records, TRUE is
2920 -- expanded if the type of the formal happens to be constrained (for
2921 -- instance when this procedure is inherited from an unconstrained
2922 -- record to a constrained one) or if the actual has no discriminant
2923 -- (its type is constrained). An exception to this is the case of a
2924 -- private type without discriminants. In this case we pass FALSE
2925 -- because the object has underlying discriminants with defaults.
2927 if Present (Extra_Constrained (Formal)) then
2928 if Ekind (Etype (Prev)) in Private_Kind
2929 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2930 then
2931 Add_Extra_Actual
2932 (Expr => New_Occurrence_Of (Standard_False, Loc),
2933 EF => Extra_Constrained (Formal));
2935 elsif Is_Constrained (Etype (Formal))
2936 or else not Has_Discriminants (Etype (Prev))
2937 then
2938 Add_Extra_Actual
2939 (Expr => New_Occurrence_Of (Standard_True, Loc),
2940 EF => Extra_Constrained (Formal));
2942 -- Do not produce extra actuals for Unchecked_Union parameters.
2943 -- Jump directly to the end of the loop.
2945 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2946 goto Skip_Extra_Actual_Generation;
2948 else
2949 -- If the actual is a type conversion, then the constrained
2950 -- test applies to the actual, not the target type.
2952 declare
2953 Act_Prev : Node_Id;
2955 begin
2956 -- Test for unchecked conversions as well, which can occur
2957 -- as out parameter actuals on calls to stream procedures.
2959 Act_Prev := Prev;
2960 while Nkind_In (Act_Prev, N_Type_Conversion,
2961 N_Unchecked_Type_Conversion)
2962 loop
2963 Act_Prev := Expression (Act_Prev);
2964 end loop;
2966 -- If the expression is a conversion of a dereference, this
2967 -- is internally generated code that manipulates addresses,
2968 -- e.g. when building interface tables. No check should
2969 -- occur in this case, and the discriminated object is not
2970 -- directly a hand.
2972 if not Comes_From_Source (Actual)
2973 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2974 and then Nkind (Act_Prev) = N_Explicit_Dereference
2975 then
2976 Add_Extra_Actual
2977 (Expr => New_Occurrence_Of (Standard_False, Loc),
2978 EF => Extra_Constrained (Formal));
2980 else
2981 Add_Extra_Actual
2982 (Expr =>
2983 Make_Attribute_Reference (Sloc (Prev),
2984 Prefix =>
2985 Duplicate_Subexpr_No_Checks
2986 (Act_Prev, Name_Req => True),
2987 Attribute_Name => Name_Constrained),
2988 EF => Extra_Constrained (Formal));
2989 end if;
2990 end;
2991 end if;
2992 end if;
2994 -- Create possible extra actual for accessibility level
2996 if Present (Extra_Accessibility (Formal)) then
2998 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2999 -- attribute, then the original actual may be an aliased object
3000 -- occurring as the prefix in a call using "Object.Operation"
3001 -- notation. In that case we must pass the level of the object,
3002 -- so Prev_Orig is reset to Prev and the attribute will be
3003 -- processed by the code for Access attributes further below.
3005 if Prev_Orig /= Prev
3006 and then Nkind (Prev) = N_Attribute_Reference
3007 and then Get_Attribute_Id (Attribute_Name (Prev)) =
3008 Attribute_Access
3009 and then Is_Aliased_View (Prev_Orig)
3010 then
3011 Prev_Orig := Prev;
3013 -- A class-wide precondition generates a test in which formals of
3014 -- the subprogram are replaced by actuals that came from source.
3015 -- In that case as well, the accessiblity comes from the actual.
3016 -- This is the one case in which there are references to formals
3017 -- outside of their subprogram.
3019 elsif Prev_Orig /= Prev
3020 and then Is_Entity_Name (Prev_Orig)
3021 and then Present (Entity (Prev_Orig))
3022 and then Is_Formal (Entity (Prev_Orig))
3023 and then not In_Open_Scopes (Scope (Entity (Prev_Orig)))
3024 then
3025 Prev_Orig := Prev;
3027 -- If the actual is a formal of an enclosing subprogram it is
3028 -- the right entity, even if it is a rewriting. This happens
3029 -- when the call is within an inherited condition or predicate.
3031 elsif Is_Entity_Name (Actual)
3032 and then Is_Formal (Entity (Actual))
3033 and then In_Open_Scopes (Scope (Entity (Actual)))
3034 then
3035 Prev_Orig := Prev;
3037 elsif Nkind (Prev_Orig) = N_Type_Conversion then
3038 Prev_Orig := Expression (Prev_Orig);
3039 end if;
3041 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3042 -- accessibility levels.
3044 if Is_Thunk (Current_Scope) then
3045 declare
3046 Parm_Ent : Entity_Id;
3048 begin
3049 if Is_Controlling_Actual (Actual) then
3051 -- Find the corresponding actual of the thunk
3053 Parm_Ent := First_Entity (Current_Scope);
3054 for J in 2 .. Param_Count loop
3055 Next_Entity (Parm_Ent);
3056 end loop;
3058 -- Handle unchecked conversion of access types generated
3059 -- in thunks (cf. Expand_Interface_Thunk).
3061 elsif Is_Access_Type (Etype (Actual))
3062 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3063 then
3064 Parm_Ent := Entity (Expression (Actual));
3066 else pragma Assert (Is_Entity_Name (Actual));
3067 Parm_Ent := Entity (Actual);
3068 end if;
3070 Add_Extra_Actual
3071 (Expr =>
3072 New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
3073 EF => Extra_Accessibility (Formal));
3074 end;
3076 elsif Is_Entity_Name (Prev_Orig) then
3078 -- When passing an access parameter, or a renaming of an access
3079 -- parameter, as the actual to another access parameter we need
3080 -- to pass along the actual's own access level parameter. This
3081 -- is done if we are within the scope of the formal access
3082 -- parameter (if this is an inlined body the extra formal is
3083 -- irrelevant).
3085 if (Is_Formal (Entity (Prev_Orig))
3086 or else
3087 (Present (Renamed_Object (Entity (Prev_Orig)))
3088 and then
3089 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3090 and then
3091 Is_Formal
3092 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3093 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3094 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3095 then
3096 declare
3097 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3099 begin
3100 pragma Assert (Present (Parm_Ent));
3102 if Present (Extra_Accessibility (Parm_Ent)) then
3103 Add_Extra_Actual
3104 (Expr =>
3105 New_Occurrence_Of
3106 (Extra_Accessibility (Parm_Ent), Loc),
3107 EF => Extra_Accessibility (Formal));
3109 -- If the actual access parameter does not have an
3110 -- associated extra formal providing its scope level,
3111 -- then treat the actual as having library-level
3112 -- accessibility.
3114 else
3115 Add_Extra_Actual
3116 (Expr =>
3117 Make_Integer_Literal (Loc,
3118 Intval => Scope_Depth (Standard_Standard)),
3119 EF => Extra_Accessibility (Formal));
3120 end if;
3121 end;
3123 -- The actual is a normal access value, so just pass the level
3124 -- of the actual's access type.
3126 else
3127 Add_Extra_Actual
3128 (Expr => Dynamic_Accessibility_Level (Prev_Orig),
3129 EF => Extra_Accessibility (Formal));
3130 end if;
3132 -- If the actual is an access discriminant, then pass the level
3133 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3135 elsif Nkind (Prev_Orig) = N_Selected_Component
3136 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3137 E_Discriminant
3138 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3139 E_Anonymous_Access_Type
3140 then
3141 Add_Extra_Actual
3142 (Expr =>
3143 Make_Integer_Literal (Loc,
3144 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3145 EF => Extra_Accessibility (Formal));
3147 -- All other cases
3149 else
3150 case Nkind (Prev_Orig) is
3151 when N_Attribute_Reference =>
3152 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3154 -- For X'Access, pass on the level of the prefix X
3156 when Attribute_Access =>
3158 -- Accessibility level of S'Access is that of A
3160 Prev_Orig := Prefix (Prev_Orig);
3162 -- If the expression is a view conversion, the
3163 -- accessibility level is that of the expression.
3165 if Nkind (Original_Node (Prev_Orig)) =
3166 N_Type_Conversion
3167 and then
3168 Nkind (Expression (Original_Node (Prev_Orig))) =
3169 N_Explicit_Dereference
3170 then
3171 Prev_Orig :=
3172 Expression (Original_Node (Prev_Orig));
3173 end if;
3175 -- If this is an Access attribute applied to the
3176 -- the current instance object passed to a type
3177 -- initialization procedure, then use the level
3178 -- of the type itself. This is not really correct,
3179 -- as there should be an extra level parameter
3180 -- passed in with _init formals (only in the case
3181 -- where the type is immutably limited), but we
3182 -- don't have an easy way currently to create such
3183 -- an extra formal (init procs aren't ever frozen).
3184 -- For now we just use the level of the type,
3185 -- which may be too shallow, but that works better
3186 -- than passing Object_Access_Level of the type,
3187 -- which can be one level too deep in some cases.
3188 -- ???
3190 -- A further case that requires special handling
3191 -- is the common idiom E.all'access. If E is a
3192 -- formal of the enclosing subprogram, the
3193 -- accessibility of the expression is that of E.
3195 if Is_Entity_Name (Prev_Orig) then
3196 Pref_Entity := Entity (Prev_Orig);
3198 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3199 and then Is_Entity_Name (Prefix (Prev_Orig))
3200 then
3201 Pref_Entity := Entity (Prefix ((Prev_Orig)));
3203 else
3204 Pref_Entity := Empty;
3205 end if;
3207 if Is_Entity_Name (Prev_Orig)
3208 and then Is_Type (Entity (Prev_Orig))
3209 then
3210 Add_Extra_Actual
3211 (Expr =>
3212 Make_Integer_Literal (Loc,
3213 Intval =>
3214 Type_Access_Level (Pref_Entity)),
3215 EF => Extra_Accessibility (Formal));
3217 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3218 and then Present (Pref_Entity)
3219 and then Is_Formal (Pref_Entity)
3220 and then Present
3221 (Extra_Accessibility (Pref_Entity))
3222 then
3223 Add_Extra_Actual
3224 (Expr =>
3225 New_Occurrence_Of
3226 (Extra_Accessibility (Pref_Entity), Loc),
3227 EF => Extra_Accessibility (Formal));
3229 else
3230 Add_Extra_Actual
3231 (Expr =>
3232 Make_Integer_Literal (Loc,
3233 Intval =>
3234 Object_Access_Level (Prev_Orig)),
3235 EF => Extra_Accessibility (Formal));
3236 end if;
3238 -- Treat the unchecked attributes as library-level
3240 when Attribute_Unchecked_Access
3241 | Attribute_Unrestricted_Access
3243 Add_Extra_Actual
3244 (Expr =>
3245 Make_Integer_Literal (Loc,
3246 Intval => Scope_Depth (Standard_Standard)),
3247 EF => Extra_Accessibility (Formal));
3249 -- No other cases of attributes returning access
3250 -- values that can be passed to access parameters.
3252 when others =>
3253 raise Program_Error;
3255 end case;
3257 -- For allocators we pass the level of the execution of the
3258 -- called subprogram, which is one greater than the current
3259 -- scope level.
3261 when N_Allocator =>
3262 Add_Extra_Actual
3263 (Expr =>
3264 Make_Integer_Literal (Loc,
3265 Intval => Scope_Depth (Current_Scope) + 1),
3266 EF => Extra_Accessibility (Formal));
3268 -- For most other cases we simply pass the level of the
3269 -- actual's access type. The type is retrieved from
3270 -- Prev rather than Prev_Orig, because in some cases
3271 -- Prev_Orig denotes an original expression that has
3272 -- not been analyzed.
3274 when others =>
3275 Add_Extra_Actual
3276 (Expr => Dynamic_Accessibility_Level (Prev),
3277 EF => Extra_Accessibility (Formal));
3278 end case;
3279 end if;
3280 end if;
3282 -- Perform the check of 4.6(49) that prevents a null value from being
3283 -- passed as an actual to an access parameter. Note that the check
3284 -- is elided in the common cases of passing an access attribute or
3285 -- access parameter as an actual. Also, we currently don't enforce
3286 -- this check for expander-generated actuals and when -gnatdj is set.
3288 if Ada_Version >= Ada_2005 then
3290 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3291 -- the intent of 6.4.1(13) is that null-exclusion checks should
3292 -- not be done for 'out' parameters, even though it refers only
3293 -- to constraint checks, and a null_exclusion is not a constraint.
3294 -- Note that AI05-0196-1 corrects this mistake in the RM.
3296 if Is_Access_Type (Etype (Formal))
3297 and then Can_Never_Be_Null (Etype (Formal))
3298 and then Ekind (Formal) /= E_Out_Parameter
3299 and then Nkind (Prev) /= N_Raise_Constraint_Error
3300 and then (Known_Null (Prev)
3301 or else not Can_Never_Be_Null (Etype (Prev)))
3302 then
3303 Install_Null_Excluding_Check (Prev);
3304 end if;
3306 -- Ada_Version < Ada_2005
3308 else
3309 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3310 or else Access_Checks_Suppressed (Subp)
3311 then
3312 null;
3314 elsif Debug_Flag_J then
3315 null;
3317 elsif not Comes_From_Source (Prev) then
3318 null;
3320 elsif Is_Entity_Name (Prev)
3321 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3322 then
3323 null;
3325 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3326 null;
3328 else
3329 Install_Null_Excluding_Check (Prev);
3330 end if;
3331 end if;
3333 -- Perform appropriate validity checks on parameters that
3334 -- are entities.
3336 if Validity_Checks_On then
3337 if (Ekind (Formal) = E_In_Parameter
3338 and then Validity_Check_In_Params)
3339 or else
3340 (Ekind (Formal) = E_In_Out_Parameter
3341 and then Validity_Check_In_Out_Params)
3342 then
3343 -- If the actual is an indexed component of a packed type (or
3344 -- is an indexed or selected component whose prefix recursively
3345 -- meets this condition), it has not been expanded yet. It will
3346 -- be copied in the validity code that follows, and has to be
3347 -- expanded appropriately, so reanalyze it.
3349 -- What we do is just to unset analyzed bits on prefixes till
3350 -- we reach something that does not have a prefix.
3352 declare
3353 Nod : Node_Id;
3355 begin
3356 Nod := Actual;
3357 while Nkind_In (Nod, N_Indexed_Component,
3358 N_Selected_Component)
3359 loop
3360 Set_Analyzed (Nod, False);
3361 Nod := Prefix (Nod);
3362 end loop;
3363 end;
3365 Ensure_Valid (Actual);
3366 end if;
3367 end if;
3369 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3370 -- since this is a left side reference. We only do this for calls
3371 -- from the source program since we assume that compiler generated
3372 -- calls explicitly generate any required checks. We also need it
3373 -- only if we are doing standard validity checks, since clearly it is
3374 -- not needed if validity checks are off, and in subscript validity
3375 -- checking mode, all indexed components are checked with a call
3376 -- directly from Expand_N_Indexed_Component.
3378 if Comes_From_Source (Call_Node)
3379 and then Ekind (Formal) /= E_In_Parameter
3380 and then Validity_Checks_On
3381 and then Validity_Check_Default
3382 and then not Validity_Check_Subscripts
3383 then
3384 Check_Valid_Lvalue_Subscripts (Actual);
3385 end if;
3387 -- Mark any scalar OUT parameter that is a simple variable as no
3388 -- longer known to be valid (unless the type is always valid). This
3389 -- reflects the fact that if an OUT parameter is never set in a
3390 -- procedure, then it can become invalid on the procedure return.
3392 if Ekind (Formal) = E_Out_Parameter
3393 and then Is_Entity_Name (Actual)
3394 and then Ekind (Entity (Actual)) = E_Variable
3395 and then not Is_Known_Valid (Etype (Actual))
3396 then
3397 Set_Is_Known_Valid (Entity (Actual), False);
3398 end if;
3400 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3401 -- clear current values, since they can be clobbered. We are probably
3402 -- doing this in more places than we need to, but better safe than
3403 -- sorry when it comes to retaining bad current values.
3405 if Ekind (Formal) /= E_In_Parameter
3406 and then Is_Entity_Name (Actual)
3407 and then Present (Entity (Actual))
3408 then
3409 declare
3410 Ent : constant Entity_Id := Entity (Actual);
3411 Sav : Node_Id;
3413 begin
3414 -- For an OUT or IN OUT parameter that is an assignable entity,
3415 -- we do not want to clobber the Last_Assignment field, since
3416 -- if it is set, it was precisely because it is indeed an OUT
3417 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3418 -- since the subprogram could have returned in invalid value.
3420 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3421 and then Is_Assignable (Ent)
3422 then
3423 Sav := Last_Assignment (Ent);
3424 Kill_Current_Values (Ent);
3425 Set_Last_Assignment (Ent, Sav);
3426 Set_Is_Known_Valid (Ent, False);
3428 -- For all other cases, just kill the current values
3430 else
3431 Kill_Current_Values (Ent);
3432 end if;
3433 end;
3434 end if;
3436 -- If the formal is class wide and the actual is an aggregate, force
3437 -- evaluation so that the back end who does not know about class-wide
3438 -- type, does not generate a temporary of the wrong size.
3440 if not Is_Class_Wide_Type (Etype (Formal)) then
3441 null;
3443 elsif Nkind (Actual) = N_Aggregate
3444 or else (Nkind (Actual) = N_Qualified_Expression
3445 and then Nkind (Expression (Actual)) = N_Aggregate)
3446 then
3447 Force_Evaluation (Actual);
3448 end if;
3450 -- In a remote call, if the formal is of a class-wide type, check
3451 -- that the actual meets the requirements described in E.4(18).
3453 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3454 Insert_Action (Actual,
3455 Make_Transportable_Check (Loc,
3456 Duplicate_Subexpr_Move_Checks (Actual)));
3457 end if;
3459 -- Perform invariant checks for all intermediate types in a view
3460 -- conversion after successful return from a call that passes the
3461 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3462 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3463 -- generating spurious checks on complex expansion such as object
3464 -- initialization through an extension aggregate.
3466 if Comes_From_Source (N)
3467 and then Ekind (Formal) /= E_In_Parameter
3468 and then Nkind (Actual) = N_Type_Conversion
3469 then
3470 Add_View_Conversion_Invariants (Formal, Actual);
3471 end if;
3473 -- Generating C the initialization of an allocator is performed by
3474 -- means of individual statements, and hence it must be done before
3475 -- the call.
3477 if Modify_Tree_For_C
3478 and then Nkind (Actual) = N_Allocator
3479 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3480 then
3481 Remove_Side_Effects (Actual);
3482 end if;
3484 -- This label is required when skipping extra actual generation for
3485 -- Unchecked_Union parameters.
3487 <<Skip_Extra_Actual_Generation>>
3489 Param_Count := Param_Count + 1;
3490 Next_Actual (Actual);
3491 Next_Formal (Formal);
3492 end loop;
3494 -- If we are calling an Ada 2012 function which needs to have the
3495 -- "accessibility level determined by the point of call" (AI05-0234)
3496 -- passed in to it, then pass it in.
3498 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3499 and then
3500 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3501 then
3502 declare
3503 Ancestor : Node_Id := Parent (Call_Node);
3504 Level : Node_Id := Empty;
3505 Defer : Boolean := False;
3507 begin
3508 -- Unimplemented: if Subp returns an anonymous access type, then
3510 -- a) if the call is the operand of an explict conversion, then
3511 -- the target type of the conversion (a named access type)
3512 -- determines the accessibility level pass in;
3514 -- b) if the call defines an access discriminant of an object
3515 -- (e.g., the discriminant of an object being created by an
3516 -- allocator, or the discriminant of a function result),
3517 -- then the accessibility level to pass in is that of the
3518 -- discriminated object being initialized).
3520 -- ???
3522 while Nkind (Ancestor) = N_Qualified_Expression
3523 loop
3524 Ancestor := Parent (Ancestor);
3525 end loop;
3527 case Nkind (Ancestor) is
3528 when N_Allocator =>
3530 -- At this point, we'd like to assign
3532 -- Level := Dynamic_Accessibility_Level (Ancestor);
3534 -- but Etype of Ancestor may not have been set yet,
3535 -- so that doesn't work.
3537 -- Handle this later in Expand_Allocator_Expression.
3539 Defer := True;
3541 when N_Object_Declaration
3542 | N_Object_Renaming_Declaration
3544 declare
3545 Def_Id : constant Entity_Id :=
3546 Defining_Identifier (Ancestor);
3548 begin
3549 if Is_Return_Object (Def_Id) then
3550 if Present (Extra_Accessibility_Of_Result
3551 (Return_Applies_To (Scope (Def_Id))))
3552 then
3553 -- Pass along value that was passed in if the
3554 -- routine we are returning from also has an
3555 -- Accessibility_Of_Result formal.
3557 Level :=
3558 New_Occurrence_Of
3559 (Extra_Accessibility_Of_Result
3560 (Return_Applies_To (Scope (Def_Id))), Loc);
3561 end if;
3562 else
3563 Level :=
3564 Make_Integer_Literal (Loc,
3565 Intval => Object_Access_Level (Def_Id));
3566 end if;
3567 end;
3569 when N_Simple_Return_Statement =>
3570 if Present (Extra_Accessibility_Of_Result
3571 (Return_Applies_To
3572 (Return_Statement_Entity (Ancestor))))
3573 then
3574 -- Pass along value that was passed in if the returned
3575 -- routine also has an Accessibility_Of_Result formal.
3577 Level :=
3578 New_Occurrence_Of
3579 (Extra_Accessibility_Of_Result
3580 (Return_Applies_To
3581 (Return_Statement_Entity (Ancestor))), Loc);
3582 end if;
3584 when others =>
3585 null;
3586 end case;
3588 if not Defer then
3589 if not Present (Level) then
3591 -- The "innermost master that evaluates the function call".
3593 -- ??? - Should we use Integer'Last here instead in order
3594 -- to deal with (some of) the problems associated with
3595 -- calls to subps whose enclosing scope is unknown (e.g.,
3596 -- Anon_Access_To_Subp_Param.all)?
3598 Level :=
3599 Make_Integer_Literal (Loc,
3600 Intval => Scope_Depth (Current_Scope) + 1);
3601 end if;
3603 Add_Extra_Actual
3604 (Expr => Level,
3605 EF =>
3606 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3607 end if;
3608 end;
3609 end if;
3611 -- If we are expanding the RHS of an assignment we need to check if tag
3612 -- propagation is needed. You might expect this processing to be in
3613 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3614 -- assignment might be transformed to a declaration for an unconstrained
3615 -- value if the expression is classwide.
3617 if Nkind (Call_Node) = N_Function_Call
3618 and then Is_Tag_Indeterminate (Call_Node)
3619 and then Is_Entity_Name (Name (Call_Node))
3620 then
3621 declare
3622 Ass : Node_Id := Empty;
3624 begin
3625 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3626 Ass := Parent (Call_Node);
3628 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3629 and then Nkind (Parent (Parent (Call_Node))) =
3630 N_Assignment_Statement
3631 then
3632 Ass := Parent (Parent (Call_Node));
3634 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3635 and then Nkind (Parent (Parent (Call_Node))) =
3636 N_Assignment_Statement
3637 then
3638 Ass := Parent (Parent (Call_Node));
3639 end if;
3641 if Present (Ass)
3642 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3643 then
3644 if Is_Access_Type (Etype (Call_Node)) then
3645 if Designated_Type (Etype (Call_Node)) /=
3646 Root_Type (Etype (Name (Ass)))
3647 then
3648 Error_Msg_NE
3649 ("tag-indeterminate expression must have designated "
3650 & "type& (RM 5.2 (6))",
3651 Call_Node, Root_Type (Etype (Name (Ass))));
3652 else
3653 Propagate_Tag (Name (Ass), Call_Node);
3654 end if;
3656 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3657 Error_Msg_NE
3658 ("tag-indeterminate expression must have type & "
3659 & "(RM 5.2 (6))",
3660 Call_Node, Root_Type (Etype (Name (Ass))));
3662 else
3663 Propagate_Tag (Name (Ass), Call_Node);
3664 end if;
3666 -- The call will be rewritten as a dispatching call, and
3667 -- expanded as such.
3669 return;
3670 end if;
3671 end;
3672 end if;
3674 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3675 -- it to point to the correct secondary virtual table
3677 if Nkind (Call_Node) in N_Subprogram_Call
3678 and then CW_Interface_Formals_Present
3679 then
3680 Expand_Interface_Actuals (Call_Node);
3681 end if;
3683 -- Deals with Dispatch_Call if we still have a call, before expanding
3684 -- extra actuals since this will be done on the re-analysis of the
3685 -- dispatching call. Note that we do not try to shorten the actual list
3686 -- for a dispatching call, it would not make sense to do so. Expansion
3687 -- of dispatching calls is suppressed for VM targets, because the VM
3688 -- back-ends directly handle the generation of dispatching calls and
3689 -- would have to undo any expansion to an indirect call.
3691 if Nkind (Call_Node) in N_Subprogram_Call
3692 and then Present (Controlling_Argument (Call_Node))
3693 then
3694 declare
3695 Call_Typ : constant Entity_Id := Etype (Call_Node);
3696 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3697 Eq_Prim_Op : Entity_Id := Empty;
3698 New_Call : Node_Id;
3699 Param : Node_Id;
3700 Prev_Call : Node_Id;
3702 begin
3703 if not Is_Limited_Type (Typ) then
3704 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3705 end if;
3707 if Tagged_Type_Expansion then
3708 Expand_Dispatching_Call (Call_Node);
3710 -- The following return is worrisome. Is it really OK to skip
3711 -- all remaining processing in this procedure ???
3713 return;
3715 -- VM targets
3717 else
3718 Apply_Tag_Checks (Call_Node);
3720 -- If this is a dispatching "=", we must first compare the
3721 -- tags so we generate: x.tag = y.tag and then x = y
3723 if Subp = Eq_Prim_Op then
3725 -- Mark the node as analyzed to avoid reanalyzing this
3726 -- dispatching call (which would cause a never-ending loop)
3728 Prev_Call := Relocate_Node (Call_Node);
3729 Set_Analyzed (Prev_Call);
3731 Param := First_Actual (Call_Node);
3732 New_Call :=
3733 Make_And_Then (Loc,
3734 Left_Opnd =>
3735 Make_Op_Eq (Loc,
3736 Left_Opnd =>
3737 Make_Selected_Component (Loc,
3738 Prefix => New_Value (Param),
3739 Selector_Name =>
3740 New_Occurrence_Of
3741 (First_Tag_Component (Typ), Loc)),
3743 Right_Opnd =>
3744 Make_Selected_Component (Loc,
3745 Prefix =>
3746 Unchecked_Convert_To (Typ,
3747 New_Value (Next_Actual (Param))),
3748 Selector_Name =>
3749 New_Occurrence_Of
3750 (First_Tag_Component (Typ), Loc))),
3751 Right_Opnd => Prev_Call);
3753 Rewrite (Call_Node, New_Call);
3755 Analyze_And_Resolve
3756 (Call_Node, Call_Typ, Suppress => All_Checks);
3757 end if;
3759 -- Expansion of a dispatching call results in an indirect call,
3760 -- which in turn causes current values to be killed (see
3761 -- Resolve_Call), so on VM targets we do the call here to
3762 -- ensure consistent warnings between VM and non-VM targets.
3764 Kill_Current_Values;
3765 end if;
3767 -- If this is a dispatching "=" then we must update the reference
3768 -- to the call node because we generated:
3769 -- x.tag = y.tag and then x = y
3771 if Subp = Eq_Prim_Op then
3772 Call_Node := Right_Opnd (Call_Node);
3773 end if;
3774 end;
3775 end if;
3777 -- Similarly, expand calls to RCI subprograms on which pragma
3778 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3779 -- later. Do this only when the call comes from source since we
3780 -- do not want such a rewriting to occur in expanded code.
3782 if Is_All_Remote_Call (Call_Node) then
3783 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3785 -- Similarly, do not add extra actuals for an entry call whose entity
3786 -- is a protected procedure, or for an internal protected subprogram
3787 -- call, because it will be rewritten as a protected subprogram call
3788 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3790 elsif Is_Protected_Type (Scope (Subp))
3791 and then (Ekind (Subp) = E_Procedure
3792 or else Ekind (Subp) = E_Function)
3793 then
3794 null;
3796 -- During that loop we gathered the extra actuals (the ones that
3797 -- correspond to Extra_Formals), so now they can be appended.
3799 else
3800 while Is_Non_Empty_List (Extra_Actuals) loop
3801 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3802 end loop;
3803 end if;
3805 -- At this point we have all the actuals, so this is the point at which
3806 -- the various expansion activities for actuals is carried out.
3808 Expand_Actuals (Call_Node, Subp, Post_Call);
3810 -- Verify that the actuals do not share storage. This check must be done
3811 -- on the caller side rather that inside the subprogram to avoid issues
3812 -- of parameter passing.
3814 if Check_Aliasing_Of_Parameters then
3815 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3816 end if;
3818 -- If the subprogram is a renaming, or if it is inherited, replace it in
3819 -- the call with the name of the actual subprogram being called. If this
3820 -- is a dispatching call, the run-time decides what to call. The Alias
3821 -- attribute does not apply to entries.
3823 if Nkind (Call_Node) /= N_Entry_Call_Statement
3824 and then No (Controlling_Argument (Call_Node))
3825 and then Present (Parent_Subp)
3826 and then not Is_Direct_Deep_Call (Subp)
3827 then
3828 if Present (Inherited_From_Formal (Subp)) then
3829 Parent_Subp := Inherited_From_Formal (Subp);
3830 else
3831 Parent_Subp := Ultimate_Alias (Parent_Subp);
3832 end if;
3834 -- The below setting of Entity is suspect, see F109-018 discussion???
3836 Set_Entity (Name (Call_Node), Parent_Subp);
3838 if Is_Abstract_Subprogram (Parent_Subp)
3839 and then not In_Instance
3840 then
3841 Error_Msg_NE
3842 ("cannot call abstract subprogram &!",
3843 Name (Call_Node), Parent_Subp);
3844 end if;
3846 -- Inspect all formals of derived subprogram Subp. Compare parameter
3847 -- types with the parent subprogram and check whether an actual may
3848 -- need a type conversion to the corresponding formal of the parent
3849 -- subprogram.
3851 -- Not clear whether intrinsic subprograms need such conversions. ???
3853 if not Is_Intrinsic_Subprogram (Parent_Subp)
3854 or else Is_Generic_Instance (Parent_Subp)
3855 then
3856 declare
3857 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3858 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3859 -- and resolve the newly generated construct.
3861 -------------
3862 -- Convert --
3863 -------------
3865 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3866 begin
3867 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3868 Analyze (Act);
3869 Resolve (Act, Typ);
3870 end Convert;
3872 -- Local variables
3874 Actual_Typ : Entity_Id;
3875 Formal_Typ : Entity_Id;
3876 Parent_Typ : Entity_Id;
3878 begin
3879 Actual := First_Actual (Call_Node);
3880 Formal := First_Formal (Subp);
3881 Parent_Formal := First_Formal (Parent_Subp);
3882 while Present (Formal) loop
3883 Actual_Typ := Etype (Actual);
3884 Formal_Typ := Etype (Formal);
3885 Parent_Typ := Etype (Parent_Formal);
3887 -- For an IN parameter of a scalar type, the parent formal
3888 -- type and derived formal type differ or the parent formal
3889 -- type and actual type do not match statically.
3891 if Is_Scalar_Type (Formal_Typ)
3892 and then Ekind (Formal) = E_In_Parameter
3893 and then Formal_Typ /= Parent_Typ
3894 and then
3895 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3896 and then not Raises_Constraint_Error (Actual)
3897 then
3898 Convert (Actual, Parent_Typ);
3899 Enable_Range_Check (Actual);
3901 -- If the actual has been marked as requiring a range
3902 -- check, then generate it here.
3904 if Do_Range_Check (Actual) then
3905 Generate_Range_Check
3906 (Actual, Etype (Formal), CE_Range_Check_Failed);
3907 end if;
3909 -- For access types, the parent formal type and actual type
3910 -- differ.
3912 elsif Is_Access_Type (Formal_Typ)
3913 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3914 then
3915 if Ekind (Formal) /= E_In_Parameter then
3916 Convert (Actual, Parent_Typ);
3918 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3919 and then Designated_Type (Parent_Typ) /=
3920 Designated_Type (Actual_Typ)
3921 and then not Is_Controlling_Formal (Formal)
3922 then
3923 -- This unchecked conversion is not necessary unless
3924 -- inlining is enabled, because in that case the type
3925 -- mismatch may become visible in the body about to be
3926 -- inlined.
3928 Rewrite (Actual,
3929 Unchecked_Convert_To (Parent_Typ,
3930 Relocate_Node (Actual)));
3931 Analyze (Actual);
3932 Resolve (Actual, Parent_Typ);
3933 end if;
3935 -- If there is a change of representation, then generate a
3936 -- warning, and do the change of representation.
3938 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3939 Error_Msg_N
3940 ("??change of representation required", Actual);
3941 Convert (Actual, Parent_Typ);
3943 -- For array and record types, the parent formal type and
3944 -- derived formal type have different sizes or pragma Pack
3945 -- status.
3947 elsif ((Is_Array_Type (Formal_Typ)
3948 and then Is_Array_Type (Parent_Typ))
3949 or else
3950 (Is_Record_Type (Formal_Typ)
3951 and then Is_Record_Type (Parent_Typ)))
3952 and then
3953 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3954 or else Has_Pragma_Pack (Formal_Typ) /=
3955 Has_Pragma_Pack (Parent_Typ))
3956 then
3957 Convert (Actual, Parent_Typ);
3958 end if;
3960 Next_Actual (Actual);
3961 Next_Formal (Formal);
3962 Next_Formal (Parent_Formal);
3963 end loop;
3964 end;
3965 end if;
3967 Orig_Subp := Subp;
3968 Subp := Parent_Subp;
3969 end if;
3971 -- Deal with case where call is an explicit dereference
3973 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3975 -- Handle case of access to protected subprogram type
3977 if Is_Access_Protected_Subprogram_Type
3978 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3979 then
3980 -- If this is a call through an access to protected operation, the
3981 -- prefix has the form (object'address, operation'access). Rewrite
3982 -- as a for other protected calls: the object is the 1st parameter
3983 -- of the list of actuals.
3985 declare
3986 Call : Node_Id;
3987 Parm : List_Id;
3988 Nam : Node_Id;
3989 Obj : Node_Id;
3990 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3992 T : constant Entity_Id :=
3993 Equivalent_Type (Base_Type (Etype (Ptr)));
3995 D_T : constant Entity_Id :=
3996 Designated_Type (Base_Type (Etype (Ptr)));
3998 begin
3999 Obj :=
4000 Make_Selected_Component (Loc,
4001 Prefix => Unchecked_Convert_To (T, Ptr),
4002 Selector_Name =>
4003 New_Occurrence_Of (First_Entity (T), Loc));
4005 Nam :=
4006 Make_Selected_Component (Loc,
4007 Prefix => Unchecked_Convert_To (T, Ptr),
4008 Selector_Name =>
4009 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4011 Nam :=
4012 Make_Explicit_Dereference (Loc,
4013 Prefix => Nam);
4015 if Present (Parameter_Associations (Call_Node)) then
4016 Parm := Parameter_Associations (Call_Node);
4017 else
4018 Parm := New_List;
4019 end if;
4021 Prepend (Obj, Parm);
4023 if Etype (D_T) = Standard_Void_Type then
4024 Call :=
4025 Make_Procedure_Call_Statement (Loc,
4026 Name => Nam,
4027 Parameter_Associations => Parm);
4028 else
4029 Call :=
4030 Make_Function_Call (Loc,
4031 Name => Nam,
4032 Parameter_Associations => Parm);
4033 end if;
4035 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4036 Set_Etype (Call, Etype (D_T));
4038 -- We do not re-analyze the call to avoid infinite recursion.
4039 -- We analyze separately the prefix and the object, and set
4040 -- the checks on the prefix that would otherwise be emitted
4041 -- when resolving a call.
4043 Rewrite (Call_Node, Call);
4044 Analyze (Nam);
4045 Apply_Access_Check (Nam);
4046 Analyze (Obj);
4047 return;
4048 end;
4049 end if;
4050 end if;
4052 -- If this is a call to an intrinsic subprogram, then perform the
4053 -- appropriate expansion to the corresponding tree node and we
4054 -- are all done (since after that the call is gone).
4056 -- In the case where the intrinsic is to be processed by the back end,
4057 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4058 -- since the idea in this case is to pass the call unchanged. If the
4059 -- intrinsic is an inherited unchecked conversion, and the derived type
4060 -- is the target type of the conversion, we must retain it as the return
4061 -- type of the expression. Otherwise the expansion below, which uses the
4062 -- parent operation, will yield the wrong type.
4064 if Is_Intrinsic_Subprogram (Subp) then
4065 Expand_Intrinsic_Call (Call_Node, Subp);
4067 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4068 and then Parent_Subp /= Orig_Subp
4069 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4070 then
4071 Set_Etype (Call_Node, Etype (Orig_Subp));
4072 end if;
4074 return;
4075 end if;
4077 if Ekind_In (Subp, E_Function, E_Procedure) then
4079 -- We perform a simple optimization on calls for To_Address by
4080 -- replacing them with an unchecked conversion. Not only is this
4081 -- efficient, but it also avoids order of elaboration problems when
4082 -- address clauses are inlined (address expression elaborated at the
4083 -- wrong point).
4085 -- We perform this optimization regardless of whether we are in the
4086 -- main unit or in a unit in the context of the main unit, to ensure
4087 -- that the generated tree is the same in both cases, for CodePeer
4088 -- use.
4090 if Is_RTE (Subp, RE_To_Address) then
4091 Rewrite (Call_Node,
4092 Unchecked_Convert_To
4093 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4094 return;
4096 -- A call to a null procedure is replaced by a null statement, but we
4097 -- are not allowed to ignore possible side effects of the call, so we
4098 -- make sure that actuals are evaluated.
4099 -- We also suppress this optimization for GNATCoverage.
4101 elsif Is_Null_Procedure (Subp)
4102 and then not Opt.Suppress_Control_Flow_Optimizations
4103 then
4104 Actual := First_Actual (Call_Node);
4105 while Present (Actual) loop
4106 Remove_Side_Effects (Actual);
4107 Next_Actual (Actual);
4108 end loop;
4110 Rewrite (Call_Node, Make_Null_Statement (Loc));
4111 return;
4112 end if;
4114 -- Handle inlining. No action needed if the subprogram is not inlined
4116 if not Is_Inlined (Subp) then
4117 null;
4119 -- Frontend inlining of expression functions (performed also when
4120 -- backend inlining is enabled).
4122 elsif Is_Inlinable_Expression_Function (Subp) then
4123 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
4124 Analyze (N);
4125 return;
4127 -- Handle frontend inlining
4129 elsif not Back_End_Inlining then
4130 Inlined_Subprogram : declare
4131 Bod : Node_Id;
4132 Must_Inline : Boolean := False;
4133 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4135 begin
4136 -- Verify that the body to inline has already been seen, and
4137 -- that if the body is in the current unit the inlining does
4138 -- not occur earlier. This avoids order-of-elaboration problems
4139 -- in the back end.
4141 -- This should be documented in sinfo/einfo ???
4143 if No (Spec)
4144 or else Nkind (Spec) /= N_Subprogram_Declaration
4145 or else No (Body_To_Inline (Spec))
4146 then
4147 Must_Inline := False;
4149 -- If this an inherited function that returns a private type,
4150 -- do not inline if the full view is an unconstrained array,
4151 -- because such calls cannot be inlined.
4153 elsif Present (Orig_Subp)
4154 and then Is_Array_Type (Etype (Orig_Subp))
4155 and then not Is_Constrained (Etype (Orig_Subp))
4156 then
4157 Must_Inline := False;
4159 elsif In_Unfrozen_Instance (Scope (Subp)) then
4160 Must_Inline := False;
4162 else
4163 Bod := Body_To_Inline (Spec);
4165 if (In_Extended_Main_Code_Unit (Call_Node)
4166 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4167 or else Has_Pragma_Inline_Always (Subp))
4168 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4169 or else
4170 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4171 then
4172 Must_Inline := True;
4174 -- If we are compiling a package body that is not the main
4175 -- unit, it must be for inlining/instantiation purposes,
4176 -- in which case we inline the call to insure that the same
4177 -- temporaries are generated when compiling the body by
4178 -- itself. Otherwise link errors can occur.
4180 -- If the function being called is itself in the main unit,
4181 -- we cannot inline, because there is a risk of double
4182 -- elaboration and/or circularity: the inlining can make
4183 -- visible a private entity in the body of the main unit,
4184 -- that gigi will see before its sees its proper definition.
4186 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4187 and then In_Package_Body
4188 then
4189 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4191 -- Inline calls to _postconditions when generating C code
4193 elsif Modify_Tree_For_C
4194 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4195 and then Chars (Name (N)) = Name_uPostconditions
4196 then
4197 Must_Inline := True;
4198 end if;
4199 end if;
4201 if Must_Inline then
4202 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4204 else
4205 -- Let the back end handle it
4207 Add_Inlined_Body (Subp, Call_Node);
4209 if Front_End_Inlining
4210 and then Nkind (Spec) = N_Subprogram_Declaration
4211 and then (In_Extended_Main_Code_Unit (Call_Node))
4212 and then No (Body_To_Inline (Spec))
4213 and then not Has_Completion (Subp)
4214 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4215 then
4216 Cannot_Inline
4217 ("cannot inline& (body not seen yet)?",
4218 Call_Node, Subp);
4219 end if;
4220 end if;
4221 end Inlined_Subprogram;
4223 -- Back end inlining: let the back end handle it
4225 elsif No (Unit_Declaration_Node (Subp))
4226 or else Nkind (Unit_Declaration_Node (Subp)) /=
4227 N_Subprogram_Declaration
4228 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4229 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4230 N_Entity
4231 then
4232 Add_Inlined_Body (Subp, Call_Node);
4234 -- If the inlined call appears within an instantiation and some
4235 -- level of optimization is required, ensure that the enclosing
4236 -- instance body is available so that the back-end can actually
4237 -- perform the inlining.
4239 if In_Instance
4240 and then Comes_From_Source (Subp)
4241 and then Optimization_Level > 0
4242 then
4243 declare
4244 Decl : Node_Id;
4245 Inst : Entity_Id;
4246 Inst_Node : Node_Id;
4248 begin
4249 Inst := Scope (Subp);
4251 -- Find enclosing instance
4253 while Present (Inst) and then Inst /= Standard_Standard loop
4254 exit when Is_Generic_Instance (Inst);
4255 Inst := Scope (Inst);
4256 end loop;
4258 if Present (Inst)
4259 and then Is_Generic_Instance (Inst)
4260 and then not Is_Inlined (Inst)
4261 then
4262 Set_Is_Inlined (Inst);
4263 Decl := Unit_Declaration_Node (Inst);
4265 -- Do not add a pending instantiation if the body exits
4266 -- already, or if the instance is a compilation unit, or
4267 -- the instance node is missing.
4269 if Present (Corresponding_Body (Decl))
4270 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4271 or else No (Next (Decl))
4272 then
4273 null;
4275 else
4276 -- The instantiation node usually follows the package
4277 -- declaration for the instance. If the generic unit
4278 -- has aspect specifications, they are transformed
4279 -- into pragmas in the instance, and the instance node
4280 -- appears after them.
4282 Inst_Node := Next (Decl);
4284 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4285 Inst_Node := Next (Inst_Node);
4286 end loop;
4288 Add_Pending_Instantiation (Inst_Node, Decl);
4289 end if;
4290 end if;
4291 end;
4292 end if;
4294 -- Front end expansion of simple functions returning unconstrained
4295 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4296 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4297 -- also Build_Renamed_Body) cannot be expanded here because this may
4298 -- give rise to order-of-elaboration issues for the types of the
4299 -- parameters of the subprogram, if any.
4301 else
4302 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4303 end if;
4304 end if;
4306 -- Check for protected subprogram. This is either an intra-object call,
4307 -- or a protected function call. Protected procedure calls are rewritten
4308 -- as entry calls and handled accordingly.
4310 -- In Ada 2005, this may be an indirect call to an access parameter that
4311 -- is an access_to_subprogram. In that case the anonymous type has a
4312 -- scope that is a protected operation, but the call is a regular one.
4313 -- In either case do not expand call if subprogram is eliminated.
4315 Scop := Scope (Subp);
4317 if Nkind (Call_Node) /= N_Entry_Call_Statement
4318 and then Is_Protected_Type (Scop)
4319 and then Ekind (Subp) /= E_Subprogram_Type
4320 and then not Is_Eliminated (Subp)
4321 then
4322 -- If the call is an internal one, it is rewritten as a call to the
4323 -- corresponding unprotected subprogram.
4325 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4326 end if;
4328 -- Functions returning controlled objects need special attention. If
4329 -- the return type is limited, then the context is initialization and
4330 -- different processing applies. If the call is to a protected function,
4331 -- the expansion above will call Expand_Call recursively. Otherwise the
4332 -- function call is transformed into a temporary which obtains the
4333 -- result from the secondary stack.
4335 if Needs_Finalization (Etype (Subp)) then
4336 if not Is_Build_In_Place_Function_Call (Call_Node)
4337 and then
4338 (No (First_Formal (Subp))
4339 or else
4340 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4341 then
4342 Expand_Ctrl_Function_Call (Call_Node);
4344 -- Build-in-place function calls which appear in anonymous contexts
4345 -- need a transient scope to ensure the proper finalization of the
4346 -- intermediate result after its use.
4348 elsif Is_Build_In_Place_Function_Call (Call_Node)
4349 and then Nkind_In (Parent (Unqual_Conv (Call_Node)),
4350 N_Attribute_Reference,
4351 N_Function_Call,
4352 N_Indexed_Component,
4353 N_Object_Renaming_Declaration,
4354 N_Procedure_Call_Statement,
4355 N_Selected_Component,
4356 N_Slice)
4357 then
4358 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4359 end if;
4360 end if;
4361 end Expand_Call_Helper;
4363 -------------------------------
4364 -- Expand_Ctrl_Function_Call --
4365 -------------------------------
4367 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4368 function Is_Element_Reference (N : Node_Id) return Boolean;
4369 -- Determine whether node N denotes a reference to an Ada 2012 container
4370 -- element.
4372 --------------------------
4373 -- Is_Element_Reference --
4374 --------------------------
4376 function Is_Element_Reference (N : Node_Id) return Boolean is
4377 Ref : constant Node_Id := Original_Node (N);
4379 begin
4380 -- Analysis marks an element reference by setting the generalized
4381 -- indexing attribute of an indexed component before the component
4382 -- is rewritten into a function call.
4384 return
4385 Nkind (Ref) = N_Indexed_Component
4386 and then Present (Generalized_Indexing (Ref));
4387 end Is_Element_Reference;
4389 -- Start of processing for Expand_Ctrl_Function_Call
4391 begin
4392 -- Optimization, if the returned value (which is on the sec-stack) is
4393 -- returned again, no need to copy/readjust/finalize, we can just pass
4394 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4395 -- attachment is needed
4397 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4398 return;
4399 end if;
4401 -- Resolution is now finished, make sure we don't start analysis again
4402 -- because of the duplication.
4404 Set_Analyzed (N);
4406 -- A function which returns a controlled object uses the secondary
4407 -- stack. Rewrite the call into a temporary which obtains the result of
4408 -- the function using 'reference.
4410 Remove_Side_Effects (N);
4412 -- The side effect removal of the function call produced a temporary.
4413 -- When the context is a case expression, if expression, or expression
4414 -- with actions, the lifetime of the temporary must be extended to match
4415 -- that of the context. Otherwise the function result will be finalized
4416 -- too early and affect the result of the expression. To prevent this
4417 -- unwanted effect, the temporary should not be considered for clean up
4418 -- actions by the general finalization machinery.
4420 -- Exception to this rule are references to Ada 2012 container elements.
4421 -- Such references must be finalized at the end of each iteration of the
4422 -- related quantified expression, otherwise the container will remain
4423 -- busy.
4425 if Nkind (N) = N_Explicit_Dereference
4426 and then Within_Case_Or_If_Expression (N)
4427 and then not Is_Element_Reference (N)
4428 then
4429 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4430 end if;
4431 end Expand_Ctrl_Function_Call;
4433 ----------------------------------------
4434 -- Expand_N_Extended_Return_Statement --
4435 ----------------------------------------
4437 -- If there is a Handled_Statement_Sequence, we rewrite this:
4439 -- return Result : T := <expression> do
4440 -- <handled_seq_of_stms>
4441 -- end return;
4443 -- to be:
4445 -- declare
4446 -- Result : T := <expression>;
4447 -- begin
4448 -- <handled_seq_of_stms>
4449 -- return Result;
4450 -- end;
4452 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4454 -- return Result : T := <expression>;
4456 -- to be:
4458 -- return <expression>;
4460 -- unless it's build-in-place or there's no <expression>, in which case
4461 -- we generate:
4463 -- declare
4464 -- Result : T := <expression>;
4465 -- begin
4466 -- return Result;
4467 -- end;
4469 -- Note that this case could have been written by the user as an extended
4470 -- return statement, or could have been transformed to this from a simple
4471 -- return statement.
4473 -- That is, we need to have a reified return object if there are statements
4474 -- (which might refer to it) or if we're doing build-in-place (so we can
4475 -- set its address to the final resting place or if there is no expression
4476 -- (in which case default initial values might need to be set).
4478 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4479 Loc : constant Source_Ptr := Sloc (N);
4481 function Build_Heap_Allocator
4482 (Temp_Id : Entity_Id;
4483 Temp_Typ : Entity_Id;
4484 Func_Id : Entity_Id;
4485 Ret_Typ : Entity_Id;
4486 Alloc_Expr : Node_Id) return Node_Id;
4487 -- Create the statements necessary to allocate a return object on the
4488 -- caller's master. The master is available through implicit parameter
4489 -- BIPfinalizationmaster.
4491 -- if BIPfinalizationmaster /= null then
4492 -- declare
4493 -- type Ptr_Typ is access Ret_Typ;
4494 -- for Ptr_Typ'Storage_Pool use
4495 -- Base_Pool (BIPfinalizationmaster.all).all;
4496 -- Local : Ptr_Typ;
4498 -- begin
4499 -- procedure Allocate (...) is
4500 -- begin
4501 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4502 -- end Allocate;
4504 -- Local := <Alloc_Expr>;
4505 -- Temp_Id := Temp_Typ (Local);
4506 -- end;
4507 -- end if;
4509 -- Temp_Id is the temporary which is used to reference the internally
4510 -- created object in all allocation forms. Temp_Typ is the type of the
4511 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4512 -- type of Func_Id. Alloc_Expr is the actual allocator.
4514 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4515 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4516 -- with parameters:
4517 -- From current activation chain
4518 -- To activation chain passed in by the caller
4519 -- New_Master master passed in by the caller
4521 -- Func_Id is the entity of the function where the extended return
4522 -- statement appears.
4524 --------------------------
4525 -- Build_Heap_Allocator --
4526 --------------------------
4528 function Build_Heap_Allocator
4529 (Temp_Id : Entity_Id;
4530 Temp_Typ : Entity_Id;
4531 Func_Id : Entity_Id;
4532 Ret_Typ : Entity_Id;
4533 Alloc_Expr : Node_Id) return Node_Id
4535 begin
4536 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4538 -- Processing for build-in-place object allocation.
4540 if Needs_Finalization (Ret_Typ) then
4541 declare
4542 Decls : constant List_Id := New_List;
4543 Fin_Mas_Id : constant Entity_Id :=
4544 Build_In_Place_Formal
4545 (Func_Id, BIP_Finalization_Master);
4546 Stmts : constant List_Id := New_List;
4547 Desig_Typ : Entity_Id;
4548 Local_Id : Entity_Id;
4549 Pool_Id : Entity_Id;
4550 Ptr_Typ : Entity_Id;
4552 begin
4553 -- Generate:
4554 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4556 Pool_Id := Make_Temporary (Loc, 'P');
4558 Append_To (Decls,
4559 Make_Object_Renaming_Declaration (Loc,
4560 Defining_Identifier => Pool_Id,
4561 Subtype_Mark =>
4562 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4563 Name =>
4564 Make_Explicit_Dereference (Loc,
4565 Prefix =>
4566 Make_Function_Call (Loc,
4567 Name =>
4568 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4569 Parameter_Associations => New_List (
4570 Make_Explicit_Dereference (Loc,
4571 Prefix =>
4572 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4574 -- Create an access type which uses the storage pool of the
4575 -- caller's master. This additional type is necessary because
4576 -- the finalization master cannot be associated with the type
4577 -- of the temporary. Otherwise the secondary stack allocation
4578 -- will fail.
4580 Desig_Typ := Ret_Typ;
4582 -- Ensure that the build-in-place machinery uses a fat pointer
4583 -- when allocating an unconstrained array on the heap. In this
4584 -- case the result object type is a constrained array type even
4585 -- though the function type is unconstrained.
4587 if Ekind (Desig_Typ) = E_Array_Subtype then
4588 Desig_Typ := Base_Type (Desig_Typ);
4589 end if;
4591 -- Generate:
4592 -- type Ptr_Typ is access Desig_Typ;
4594 Ptr_Typ := Make_Temporary (Loc, 'P');
4596 Append_To (Decls,
4597 Make_Full_Type_Declaration (Loc,
4598 Defining_Identifier => Ptr_Typ,
4599 Type_Definition =>
4600 Make_Access_To_Object_Definition (Loc,
4601 Subtype_Indication =>
4602 New_Occurrence_Of (Desig_Typ, Loc))));
4604 -- Perform minor decoration in order to set the master and the
4605 -- storage pool attributes.
4607 Set_Ekind (Ptr_Typ, E_Access_Type);
4608 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4609 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4611 -- Create the temporary, generate:
4612 -- Local_Id : Ptr_Typ;
4614 Local_Id := Make_Temporary (Loc, 'T');
4616 Append_To (Decls,
4617 Make_Object_Declaration (Loc,
4618 Defining_Identifier => Local_Id,
4619 Object_Definition =>
4620 New_Occurrence_Of (Ptr_Typ, Loc)));
4622 -- Allocate the object, generate:
4623 -- Local_Id := <Alloc_Expr>;
4625 Append_To (Stmts,
4626 Make_Assignment_Statement (Loc,
4627 Name => New_Occurrence_Of (Local_Id, Loc),
4628 Expression => Alloc_Expr));
4630 -- Generate:
4631 -- Temp_Id := Temp_Typ (Local_Id);
4633 Append_To (Stmts,
4634 Make_Assignment_Statement (Loc,
4635 Name => New_Occurrence_Of (Temp_Id, Loc),
4636 Expression =>
4637 Unchecked_Convert_To (Temp_Typ,
4638 New_Occurrence_Of (Local_Id, Loc))));
4640 -- Wrap the allocation in a block. This is further conditioned
4641 -- by checking the caller finalization master at runtime. A
4642 -- null value indicates a non-existent master, most likely due
4643 -- to a Finalize_Storage_Only allocation.
4645 -- Generate:
4646 -- if BIPfinalizationmaster /= null then
4647 -- declare
4648 -- <Decls>
4649 -- begin
4650 -- <Stmts>
4651 -- end;
4652 -- end if;
4654 return
4655 Make_If_Statement (Loc,
4656 Condition =>
4657 Make_Op_Ne (Loc,
4658 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4659 Right_Opnd => Make_Null (Loc)),
4661 Then_Statements => New_List (
4662 Make_Block_Statement (Loc,
4663 Declarations => Decls,
4664 Handled_Statement_Sequence =>
4665 Make_Handled_Sequence_Of_Statements (Loc,
4666 Statements => Stmts))));
4667 end;
4669 -- For all other cases, generate:
4670 -- Temp_Id := <Alloc_Expr>;
4672 else
4673 return
4674 Make_Assignment_Statement (Loc,
4675 Name => New_Occurrence_Of (Temp_Id, Loc),
4676 Expression => Alloc_Expr);
4677 end if;
4678 end Build_Heap_Allocator;
4680 ---------------------------
4681 -- Move_Activation_Chain --
4682 ---------------------------
4684 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4685 begin
4686 return
4687 Make_Procedure_Call_Statement (Loc,
4688 Name =>
4689 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4691 Parameter_Associations => New_List (
4693 -- Source chain
4695 Make_Attribute_Reference (Loc,
4696 Prefix => Make_Identifier (Loc, Name_uChain),
4697 Attribute_Name => Name_Unrestricted_Access),
4699 -- Destination chain
4701 New_Occurrence_Of
4702 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4704 -- New master
4706 New_Occurrence_Of
4707 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4708 end Move_Activation_Chain;
4710 -- Local variables
4712 Func_Id : constant Entity_Id :=
4713 Return_Applies_To (Return_Statement_Entity (N));
4714 Is_BIP_Func : constant Boolean :=
4715 Is_Build_In_Place_Function (Func_Id);
4716 Ret_Obj_Id : constant Entity_Id :=
4717 First_Entity (Return_Statement_Entity (N));
4718 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4719 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4721 Exp : Node_Id;
4722 HSS : Node_Id;
4723 Result : Node_Id;
4724 Stmts : List_Id;
4726 Return_Stmt : Node_Id := Empty;
4727 -- Force initialization to facilitate static analysis
4729 -- Start of processing for Expand_N_Extended_Return_Statement
4731 begin
4732 -- Given that functionality of interface thunks is simple (just displace
4733 -- the pointer to the object) they are always handled by means of
4734 -- simple return statements.
4736 pragma Assert (not Is_Thunk (Current_Scope));
4738 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4739 Exp := Expression (Ret_Obj_Decl);
4740 else
4741 Exp := Empty;
4742 end if;
4744 HSS := Handled_Statement_Sequence (N);
4746 -- If the returned object needs finalization actions, the function must
4747 -- perform the appropriate cleanup should it fail to return. The state
4748 -- of the function itself is tracked through a flag which is coupled
4749 -- with the scope finalizer. There is one flag per each return object
4750 -- in case of multiple returns.
4752 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4753 declare
4754 Flag_Decl : Node_Id;
4755 Flag_Id : Entity_Id;
4756 Func_Bod : Node_Id;
4758 begin
4759 -- Recover the function body
4761 Func_Bod := Unit_Declaration_Node (Func_Id);
4763 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4764 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4765 end if;
4767 if Nkind (Func_Bod) = N_Function_Specification then
4768 Func_Bod := Parent (Func_Bod); -- one more level for child units
4769 end if;
4771 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
4773 -- Create a flag to track the function state
4775 Flag_Id := Make_Temporary (Loc, 'F');
4776 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4778 -- Insert the flag at the beginning of the function declarations,
4779 -- generate:
4780 -- Fnn : Boolean := False;
4782 Flag_Decl :=
4783 Make_Object_Declaration (Loc,
4784 Defining_Identifier => Flag_Id,
4785 Object_Definition =>
4786 New_Occurrence_Of (Standard_Boolean, Loc),
4787 Expression =>
4788 New_Occurrence_Of (Standard_False, Loc));
4790 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4791 Analyze (Flag_Decl);
4792 end;
4793 end if;
4795 -- Build a simple_return_statement that returns the return object when
4796 -- there is a statement sequence, or no expression, or the result will
4797 -- be built in place. Note however that we currently do this for all
4798 -- composite cases, even though not all are built in place.
4800 if Present (HSS)
4801 or else Is_Composite_Type (Ret_Typ)
4802 or else No (Exp)
4803 then
4804 if No (HSS) then
4805 Stmts := New_List;
4807 -- If the extended return has a handled statement sequence, then wrap
4808 -- it in a block and use the block as the first statement.
4810 else
4811 Stmts := New_List (
4812 Make_Block_Statement (Loc,
4813 Declarations => New_List,
4814 Handled_Statement_Sequence => HSS));
4815 end if;
4817 -- If the result type contains tasks, we call Move_Activation_Chain.
4818 -- Later, the cleanup code will call Complete_Master, which will
4819 -- terminate any unactivated tasks belonging to the return statement
4820 -- master. But Move_Activation_Chain updates their master to be that
4821 -- of the caller, so they will not be terminated unless the return
4822 -- statement completes unsuccessfully due to exception, abort, goto,
4823 -- or exit. As a formality, we test whether the function requires the
4824 -- result to be built in place, though that's necessarily true for
4825 -- the case of result types with task parts.
4827 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4829 -- The return expression is an aggregate for a complex type which
4830 -- contains tasks. This particular case is left unexpanded since
4831 -- the regular expansion would insert all temporaries and
4832 -- initialization code in the wrong block.
4834 if Nkind (Exp) = N_Aggregate then
4835 Expand_N_Aggregate (Exp);
4836 end if;
4838 -- Do not move the activation chain if the return object does not
4839 -- contain tasks.
4841 if Has_Task (Etype (Ret_Obj_Id)) then
4842 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4843 end if;
4844 end if;
4846 -- Update the state of the function right before the object is
4847 -- returned.
4849 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4850 declare
4851 Flag_Id : constant Entity_Id :=
4852 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4854 begin
4855 -- Generate:
4856 -- Fnn := True;
4858 Append_To (Stmts,
4859 Make_Assignment_Statement (Loc,
4860 Name => New_Occurrence_Of (Flag_Id, Loc),
4861 Expression => New_Occurrence_Of (Standard_True, Loc)));
4862 end;
4863 end if;
4865 -- Build a simple_return_statement that returns the return object
4867 Return_Stmt :=
4868 Make_Simple_Return_Statement (Loc,
4869 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4870 Append_To (Stmts, Return_Stmt);
4872 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4873 end if;
4875 -- Case where we build a return statement block
4877 if Present (HSS) then
4878 Result :=
4879 Make_Block_Statement (Loc,
4880 Declarations => Return_Object_Declarations (N),
4881 Handled_Statement_Sequence => HSS);
4883 -- We set the entity of the new block statement to be that of the
4884 -- return statement. This is necessary so that various fields, such
4885 -- as Finalization_Chain_Entity carry over from the return statement
4886 -- to the block. Note that this block is unusual, in that its entity
4887 -- is an E_Return_Statement rather than an E_Block.
4889 Set_Identifier
4890 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4892 -- If the object decl was already rewritten as a renaming, then we
4893 -- don't want to do the object allocation and transformation of
4894 -- the return object declaration to a renaming. This case occurs
4895 -- when the return object is initialized by a call to another
4896 -- build-in-place function, and that function is responsible for
4897 -- the allocation of the return object.
4899 if Is_BIP_Func
4900 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4901 then
4902 pragma Assert
4903 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4904 and then
4906 -- It is a regular BIP object declaration
4908 (Is_Build_In_Place_Function_Call
4909 (Expression (Original_Node (Ret_Obj_Decl)))
4911 -- It is a BIP object declaration that displaces the pointer
4912 -- to the object to reference a convered interface type.
4914 or else
4915 Present (Unqual_BIP_Iface_Function_Call
4916 (Expression (Original_Node (Ret_Obj_Decl))))));
4918 -- Return the build-in-place result by reference
4920 Set_By_Ref (Return_Stmt);
4922 elsif Is_BIP_Func then
4924 -- Locate the implicit access parameter associated with the
4925 -- caller-supplied return object and convert the return
4926 -- statement's return object declaration to a renaming of a
4927 -- dereference of the access parameter. If the return object's
4928 -- declaration includes an expression that has not already been
4929 -- expanded as separate assignments, then add an assignment
4930 -- statement to ensure the return object gets initialized.
4932 -- declare
4933 -- Result : T [:= <expression>];
4934 -- begin
4935 -- ...
4937 -- is converted to
4939 -- declare
4940 -- Result : T renames FuncRA.all;
4941 -- [Result := <expression;]
4942 -- begin
4943 -- ...
4945 declare
4946 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4947 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4949 Init_Assignment : Node_Id := Empty;
4950 Obj_Acc_Formal : Entity_Id;
4951 Obj_Acc_Deref : Node_Id;
4952 Obj_Alloc_Formal : Entity_Id;
4954 begin
4955 -- Build-in-place results must be returned by reference
4957 Set_By_Ref (Return_Stmt);
4959 -- Retrieve the implicit access parameter passed by the caller
4961 Obj_Acc_Formal :=
4962 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4964 -- If the return object's declaration includes an expression
4965 -- and the declaration isn't marked as No_Initialization, then
4966 -- we need to generate an assignment to the object and insert
4967 -- it after the declaration before rewriting it as a renaming
4968 -- (otherwise we'll lose the initialization). The case where
4969 -- the result type is an interface (or class-wide interface)
4970 -- is also excluded because the context of the function call
4971 -- must be unconstrained, so the initialization will always
4972 -- be done as part of an allocator evaluation (storage pool
4973 -- or secondary stack), never to a constrained target object
4974 -- passed in by the caller. Besides the assignment being
4975 -- unneeded in this case, it avoids problems with trying to
4976 -- generate a dispatching assignment when the return expression
4977 -- is a nonlimited descendant of a limited interface (the
4978 -- interface has no assignment operation).
4980 if Present (Ret_Obj_Expr)
4981 and then not No_Initialization (Ret_Obj_Decl)
4982 and then not Is_Interface (Ret_Obj_Typ)
4983 then
4984 Init_Assignment :=
4985 Make_Assignment_Statement (Loc,
4986 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4987 Expression => New_Copy_Tree (Ret_Obj_Expr));
4989 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4990 Set_Assignment_OK (Name (Init_Assignment));
4991 Set_No_Ctrl_Actions (Init_Assignment);
4993 Set_Parent (Name (Init_Assignment), Init_Assignment);
4994 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4996 Set_Expression (Ret_Obj_Decl, Empty);
4998 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4999 and then not Is_Class_Wide_Type
5000 (Etype (Expression (Init_Assignment)))
5001 then
5002 Rewrite (Expression (Init_Assignment),
5003 Make_Type_Conversion (Loc,
5004 Subtype_Mark =>
5005 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
5006 Expression =>
5007 Relocate_Node (Expression (Init_Assignment))));
5008 end if;
5010 -- In the case of functions where the calling context can
5011 -- determine the form of allocation needed, initialization
5012 -- is done with each part of the if statement that handles
5013 -- the different forms of allocation (this is true for
5014 -- unconstrained and tagged result subtypes).
5016 if Is_Constrained (Ret_Typ)
5017 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
5018 then
5019 Insert_After (Ret_Obj_Decl, Init_Assignment);
5020 end if;
5021 end if;
5023 -- When the function's subtype is unconstrained, a run-time
5024 -- test is needed to determine the form of allocation to use
5025 -- for the return object. The function has an implicit formal
5026 -- parameter indicating this. If the BIP_Alloc_Form formal has
5027 -- the value one, then the caller has passed access to an
5028 -- existing object for use as the return object. If the value
5029 -- is two, then the return object must be allocated on the
5030 -- secondary stack. Otherwise, the object must be allocated in
5031 -- a storage pool. We generate an if statement to test the
5032 -- implicit allocation formal and initialize a local access
5033 -- value appropriately, creating allocators in the secondary
5034 -- stack and global heap cases. The special formal also exists
5035 -- and must be tested when the function has a tagged result,
5036 -- even when the result subtype is constrained, because in
5037 -- general such functions can be called in dispatching contexts
5038 -- and must be handled similarly to functions with a class-wide
5039 -- result.
5041 if not Is_Constrained (Ret_Typ)
5042 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
5043 then
5044 Obj_Alloc_Formal :=
5045 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5047 declare
5048 Pool_Id : constant Entity_Id :=
5049 Make_Temporary (Loc, 'P');
5050 Alloc_Obj_Id : Entity_Id;
5051 Alloc_Obj_Decl : Node_Id;
5052 Alloc_If_Stmt : Node_Id;
5053 Heap_Allocator : Node_Id;
5054 Pool_Decl : Node_Id;
5055 Pool_Allocator : Node_Id;
5056 Ptr_Type_Decl : Node_Id;
5057 Ref_Type : Entity_Id;
5058 SS_Allocator : Node_Id;
5060 begin
5061 -- Reuse the itype created for the function's implicit
5062 -- access formal. This avoids the need to create a new
5063 -- access type here, plus it allows assigning the access
5064 -- formal directly without applying a conversion.
5066 -- Ref_Type := Etype (Object_Access);
5068 -- Create an access type designating the function's
5069 -- result subtype.
5071 Ref_Type := Make_Temporary (Loc, 'A');
5073 Ptr_Type_Decl :=
5074 Make_Full_Type_Declaration (Loc,
5075 Defining_Identifier => Ref_Type,
5076 Type_Definition =>
5077 Make_Access_To_Object_Definition (Loc,
5078 All_Present => True,
5079 Subtype_Indication =>
5080 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5082 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5084 -- Create an access object that will be initialized to an
5085 -- access value denoting the return object, either coming
5086 -- from an implicit access value passed in by the caller
5087 -- or from the result of an allocator.
5089 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5090 Set_Etype (Alloc_Obj_Id, Ref_Type);
5092 Alloc_Obj_Decl :=
5093 Make_Object_Declaration (Loc,
5094 Defining_Identifier => Alloc_Obj_Id,
5095 Object_Definition =>
5096 New_Occurrence_Of (Ref_Type, Loc));
5098 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5100 -- Create allocators for both the secondary stack and
5101 -- global heap. If there's an initialization expression,
5102 -- then create these as initialized allocators.
5104 if Present (Ret_Obj_Expr)
5105 and then not No_Initialization (Ret_Obj_Decl)
5106 then
5107 -- Always use the type of the expression for the
5108 -- qualified expression, rather than the result type.
5109 -- In general we cannot always use the result type
5110 -- for the allocator, because the expression might be
5111 -- of a specific type, such as in the case of an
5112 -- aggregate or even a nonlimited object when the
5113 -- result type is a limited class-wide interface type.
5115 Heap_Allocator :=
5116 Make_Allocator (Loc,
5117 Expression =>
5118 Make_Qualified_Expression (Loc,
5119 Subtype_Mark =>
5120 New_Occurrence_Of
5121 (Etype (Ret_Obj_Expr), Loc),
5122 Expression => New_Copy_Tree (Ret_Obj_Expr)));
5124 else
5125 -- If the function returns a class-wide type we cannot
5126 -- use the return type for the allocator. Instead we
5127 -- use the type of the expression, which must be an
5128 -- aggregate of a definite type.
5130 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5131 Heap_Allocator :=
5132 Make_Allocator (Loc,
5133 Expression =>
5134 New_Occurrence_Of
5135 (Etype (Ret_Obj_Expr), Loc));
5136 else
5137 Heap_Allocator :=
5138 Make_Allocator (Loc,
5139 Expression =>
5140 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5141 end if;
5143 -- If the object requires default initialization then
5144 -- that will happen later following the elaboration of
5145 -- the object renaming. If we don't turn it off here
5146 -- then the object will be default initialized twice.
5148 Set_No_Initialization (Heap_Allocator);
5149 end if;
5151 -- Set the flag indicating that the allocator came from
5152 -- a build-in-place return statement, so we can avoid
5153 -- adjusting the allocated object. Note that this flag
5154 -- will be inherited by the copies made below.
5156 Set_Alloc_For_BIP_Return (Heap_Allocator);
5158 -- The Pool_Allocator is just like the Heap_Allocator,
5159 -- except we set Storage_Pool and Procedure_To_Call so
5160 -- it will use the user-defined storage pool.
5162 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5163 pragma Assert (Alloc_For_BIP_Return (Pool_Allocator));
5165 -- Do not generate the renaming of the build-in-place
5166 -- pool parameter on ZFP because the parameter is not
5167 -- created in the first place.
5169 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5170 Pool_Decl :=
5171 Make_Object_Renaming_Declaration (Loc,
5172 Defining_Identifier => Pool_Id,
5173 Subtype_Mark =>
5174 New_Occurrence_Of
5175 (RTE (RE_Root_Storage_Pool), Loc),
5176 Name =>
5177 Make_Explicit_Dereference (Loc,
5178 New_Occurrence_Of
5179 (Build_In_Place_Formal
5180 (Func_Id, BIP_Storage_Pool), Loc)));
5181 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5182 Set_Procedure_To_Call
5183 (Pool_Allocator, RTE (RE_Allocate_Any));
5184 else
5185 Pool_Decl := Make_Null_Statement (Loc);
5186 end if;
5188 -- If the No_Allocators restriction is active, then only
5189 -- an allocator for secondary stack allocation is needed.
5190 -- It's OK for such allocators to have Comes_From_Source
5191 -- set to False, because gigi knows not to flag them as
5192 -- being a violation of No_Implicit_Heap_Allocations.
5194 if Restriction_Active (No_Allocators) then
5195 SS_Allocator := Heap_Allocator;
5196 Heap_Allocator := Make_Null (Loc);
5197 Pool_Allocator := Make_Null (Loc);
5199 -- Otherwise the heap and pool allocators may be needed,
5200 -- so we make another allocator for secondary stack
5201 -- allocation.
5203 else
5204 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5205 pragma Assert (Alloc_For_BIP_Return (SS_Allocator));
5207 -- The heap and pool allocators are marked as
5208 -- Comes_From_Source since they correspond to an
5209 -- explicit user-written allocator (that is, it will
5210 -- only be executed on behalf of callers that call the
5211 -- function as initialization for such an allocator).
5212 -- Prevents errors when No_Implicit_Heap_Allocations
5213 -- is in force.
5215 Set_Comes_From_Source (Heap_Allocator, True);
5216 Set_Comes_From_Source (Pool_Allocator, True);
5217 end if;
5219 -- The allocator is returned on the secondary stack.
5221 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5222 Set_Procedure_To_Call
5223 (SS_Allocator, RTE (RE_SS_Allocate));
5225 -- The allocator is returned on the secondary stack,
5226 -- so indicate that the function return, as well as
5227 -- all blocks that encloses the allocator, must not
5228 -- release it. The flags must be set now because
5229 -- the decision to use the secondary stack is done
5230 -- very late in the course of expanding the return
5231 -- statement, past the point where these flags are
5232 -- normally set.
5234 Set_Uses_Sec_Stack (Func_Id);
5235 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5236 Set_Sec_Stack_Needed_For_Return
5237 (Return_Statement_Entity (N));
5238 Set_Enclosing_Sec_Stack_Return (N);
5240 -- Create an if statement to test the BIP_Alloc_Form
5241 -- formal and initialize the access object to either the
5242 -- BIP_Object_Access formal (BIP_Alloc_Form =
5243 -- Caller_Allocation), the result of allocating the
5244 -- object in the secondary stack (BIP_Alloc_Form =
5245 -- Secondary_Stack), or else an allocator to create the
5246 -- return object in the heap or user-defined pool
5247 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5249 -- ??? An unchecked type conversion must be made in the
5250 -- case of assigning the access object formal to the
5251 -- local access object, because a normal conversion would
5252 -- be illegal in some cases (such as converting access-
5253 -- to-unconstrained to access-to-constrained), but the
5254 -- the unchecked conversion will presumably fail to work
5255 -- right in just such cases. It's not clear at all how to
5256 -- handle this. ???
5258 Alloc_If_Stmt :=
5259 Make_If_Statement (Loc,
5260 Condition =>
5261 Make_Op_Eq (Loc,
5262 Left_Opnd =>
5263 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5264 Right_Opnd =>
5265 Make_Integer_Literal (Loc,
5266 UI_From_Int (BIP_Allocation_Form'Pos
5267 (Caller_Allocation)))),
5269 Then_Statements => New_List (
5270 Make_Assignment_Statement (Loc,
5271 Name =>
5272 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5273 Expression =>
5274 Make_Unchecked_Type_Conversion (Loc,
5275 Subtype_Mark =>
5276 New_Occurrence_Of (Ref_Type, Loc),
5277 Expression =>
5278 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5280 Elsif_Parts => New_List (
5281 Make_Elsif_Part (Loc,
5282 Condition =>
5283 Make_Op_Eq (Loc,
5284 Left_Opnd =>
5285 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5286 Right_Opnd =>
5287 Make_Integer_Literal (Loc,
5288 UI_From_Int (BIP_Allocation_Form'Pos
5289 (Secondary_Stack)))),
5291 Then_Statements => New_List (
5292 Make_Assignment_Statement (Loc,
5293 Name =>
5294 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5295 Expression => SS_Allocator))),
5297 Make_Elsif_Part (Loc,
5298 Condition =>
5299 Make_Op_Eq (Loc,
5300 Left_Opnd =>
5301 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5302 Right_Opnd =>
5303 Make_Integer_Literal (Loc,
5304 UI_From_Int (BIP_Allocation_Form'Pos
5305 (Global_Heap)))),
5307 Then_Statements => New_List (
5308 Build_Heap_Allocator
5309 (Temp_Id => Alloc_Obj_Id,
5310 Temp_Typ => Ref_Type,
5311 Func_Id => Func_Id,
5312 Ret_Typ => Ret_Obj_Typ,
5313 Alloc_Expr => Heap_Allocator))),
5315 -- ???If all is well, we can put the following
5316 -- 'elsif' in the 'else', but this is a useful
5317 -- self-check in case caller and callee don't agree
5318 -- on whether BIPAlloc and so on should be passed.
5320 Make_Elsif_Part (Loc,
5321 Condition =>
5322 Make_Op_Eq (Loc,
5323 Left_Opnd =>
5324 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5325 Right_Opnd =>
5326 Make_Integer_Literal (Loc,
5327 UI_From_Int (BIP_Allocation_Form'Pos
5328 (User_Storage_Pool)))),
5330 Then_Statements => New_List (
5331 Pool_Decl,
5332 Build_Heap_Allocator
5333 (Temp_Id => Alloc_Obj_Id,
5334 Temp_Typ => Ref_Type,
5335 Func_Id => Func_Id,
5336 Ret_Typ => Ret_Obj_Typ,
5337 Alloc_Expr => Pool_Allocator)))),
5339 -- Raise Program_Error if it's none of the above;
5340 -- this is a compiler bug. ???PE_All_Guards_Closed
5341 -- is bogus; we should have a new code.
5343 Else_Statements => New_List (
5344 Make_Raise_Program_Error (Loc,
5345 Reason => PE_All_Guards_Closed)));
5347 -- If a separate initialization assignment was created
5348 -- earlier, append that following the assignment of the
5349 -- implicit access formal to the access object, to ensure
5350 -- that the return object is initialized in that case. In
5351 -- this situation, the target of the assignment must be
5352 -- rewritten to denote a dereference of the access to the
5353 -- return object passed in by the caller.
5355 if Present (Init_Assignment) then
5356 Rewrite (Name (Init_Assignment),
5357 Make_Explicit_Dereference (Loc,
5358 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5360 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5362 Append_To
5363 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5364 end if;
5366 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5368 -- Remember the local access object for use in the
5369 -- dereference of the renaming created below.
5371 Obj_Acc_Formal := Alloc_Obj_Id;
5372 end;
5373 end if;
5375 -- Replace the return object declaration with a renaming of a
5376 -- dereference of the access value designating the return
5377 -- object.
5379 Obj_Acc_Deref :=
5380 Make_Explicit_Dereference (Loc,
5381 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5383 Rewrite (Ret_Obj_Decl,
5384 Make_Object_Renaming_Declaration (Loc,
5385 Defining_Identifier => Ret_Obj_Id,
5386 Access_Definition => Empty,
5387 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5388 Name => Obj_Acc_Deref));
5390 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5391 end;
5392 end if;
5394 -- Case where we do not build a block
5396 else
5397 -- We're about to drop Return_Object_Declarations on the floor, so
5398 -- we need to insert it, in case it got expanded into useful code.
5399 -- Remove side effects from expression, which may be duplicated in
5400 -- subsequent checks (see Expand_Simple_Function_Return).
5402 Insert_List_Before (N, Return_Object_Declarations (N));
5403 Remove_Side_Effects (Exp);
5405 -- Build simple_return_statement that returns the expression directly
5407 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5408 Result := Return_Stmt;
5409 end if;
5411 -- Set the flag to prevent infinite recursion
5413 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5415 Rewrite (N, Result);
5416 Analyze (N);
5417 end Expand_N_Extended_Return_Statement;
5419 ----------------------------
5420 -- Expand_N_Function_Call --
5421 ----------------------------
5423 procedure Expand_N_Function_Call (N : Node_Id) is
5424 begin
5425 Expand_Call (N);
5426 end Expand_N_Function_Call;
5428 ---------------------------------------
5429 -- Expand_N_Procedure_Call_Statement --
5430 ---------------------------------------
5432 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5433 begin
5434 Expand_Call (N);
5435 end Expand_N_Procedure_Call_Statement;
5437 --------------------------------------
5438 -- Expand_N_Simple_Return_Statement --
5439 --------------------------------------
5441 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5442 begin
5443 -- Defend against previous errors (i.e. the return statement calls a
5444 -- function that is not available in configurable runtime).
5446 if Present (Expression (N))
5447 and then Nkind (Expression (N)) = N_Empty
5448 then
5449 Check_Error_Detected;
5450 return;
5451 end if;
5453 -- Distinguish the function and non-function cases:
5455 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5456 when E_Function
5457 | E_Generic_Function
5459 Expand_Simple_Function_Return (N);
5461 when E_Entry
5462 | E_Entry_Family
5463 | E_Generic_Procedure
5464 | E_Procedure
5465 | E_Return_Statement
5467 Expand_Non_Function_Return (N);
5469 when others =>
5470 raise Program_Error;
5471 end case;
5473 exception
5474 when RE_Not_Available =>
5475 return;
5476 end Expand_N_Simple_Return_Statement;
5478 ------------------------------
5479 -- Expand_N_Subprogram_Body --
5480 ------------------------------
5482 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5483 -- by the back-end.
5485 -- Add dummy push/pop label nodes at start and end to clear any local
5486 -- exception indications if local-exception-to-goto optimization is active.
5488 -- Add return statement if last statement in body is not a return statement
5489 -- (this makes things easier on Gigi which does not want to have to handle
5490 -- a missing return).
5492 -- Add call to Activate_Tasks if body is a task activator
5494 -- Deal with possible detection of infinite recursion
5496 -- Eliminate body completely if convention stubbed
5498 -- Encode entity names within body, since we will not need to reference
5499 -- these entities any longer in the front end.
5501 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5503 -- Reset Pure indication if any parameter has root type System.Address
5504 -- or has any parameters of limited types, where limited means that the
5505 -- run-time view is limited (i.e. the full type is limited).
5507 -- Wrap thread body
5509 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5510 Body_Id : constant Entity_Id := Defining_Entity (N);
5511 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5512 Loc : constant Source_Ptr := Sloc (N);
5514 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5515 -- Append a return statement to the statement sequence Stmts if the last
5516 -- statement is not already a return or a goto statement. Note that the
5517 -- latter test is not critical, it does not matter if we add a few extra
5518 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5519 -- the corresponding spec of the subprogram body.
5521 ----------------
5522 -- Add_Return --
5523 ----------------
5525 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5526 Last_Stmt : Node_Id;
5527 Loc : Source_Ptr;
5528 Stmt : Node_Id;
5530 begin
5531 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5532 -- not relevant in this context since they are not executable.
5534 Last_Stmt := Last (Stmts);
5535 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5536 Prev (Last_Stmt);
5537 end loop;
5539 -- Now insert return unless last statement is a transfer
5541 if not Is_Transfer (Last_Stmt) then
5543 -- The source location for the return is the end label of the
5544 -- procedure if present. Otherwise use the sloc of the last
5545 -- statement in the list. If the list comes from a generated
5546 -- exception handler and we are not debugging generated code,
5547 -- all the statements within the handler are made invisible
5548 -- to the debugger.
5550 if Nkind (Parent (Stmts)) = N_Exception_Handler
5551 and then not Comes_From_Source (Parent (Stmts))
5552 then
5553 Loc := Sloc (Last_Stmt);
5554 elsif Present (End_Label (HSS)) then
5555 Loc := Sloc (End_Label (HSS));
5556 else
5557 Loc := Sloc (Last_Stmt);
5558 end if;
5560 -- Append return statement, and set analyzed manually. We can't
5561 -- call Analyze on this return since the scope is wrong.
5563 -- Note: it almost works to push the scope and then do the Analyze
5564 -- call, but something goes wrong in some weird cases and it is
5565 -- not worth worrying about ???
5567 Stmt := Make_Simple_Return_Statement (Loc);
5569 -- The return statement is handled properly, and the call to the
5570 -- postcondition, inserted below, does not require information
5571 -- from the body either. However, that call is analyzed in the
5572 -- enclosing scope, and an elaboration check might improperly be
5573 -- added to it. A guard in Sem_Elab is needed to prevent that
5574 -- spurious check, see Check_Elab_Call.
5576 Append_To (Stmts, Stmt);
5577 Set_Analyzed (Stmt);
5579 -- Call the _Postconditions procedure if the related subprogram
5580 -- has contract assertions that need to be verified on exit.
5582 if Ekind (Spec_Id) = E_Procedure
5583 and then Present (Postconditions_Proc (Spec_Id))
5584 then
5585 Insert_Action (Stmt,
5586 Make_Procedure_Call_Statement (Loc,
5587 Name =>
5588 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5589 end if;
5590 end if;
5591 end Add_Return;
5593 -- Local variables
5595 Except_H : Node_Id;
5596 L : List_Id;
5597 Spec_Id : Entity_Id;
5599 -- Start of processing for Expand_N_Subprogram_Body
5601 begin
5602 if Present (Corresponding_Spec (N)) then
5603 Spec_Id := Corresponding_Spec (N);
5604 else
5605 Spec_Id := Body_Id;
5606 end if;
5608 -- If this is a Pure function which has any parameters whose root type
5609 -- is System.Address, reset the Pure indication.
5610 -- This check is also performed when the subprogram is frozen, but we
5611 -- repeat it on the body so that the indication is consistent, and so
5612 -- it applies as well to bodies without separate specifications.
5614 if Is_Pure (Spec_Id)
5615 and then Is_Subprogram (Spec_Id)
5616 and then not Has_Pragma_Pure_Function (Spec_Id)
5617 then
5618 Check_Function_With_Address_Parameter (Spec_Id);
5620 if Spec_Id /= Body_Id then
5621 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5622 end if;
5623 end if;
5625 -- Set L to either the list of declarations if present, or to the list
5626 -- of statements if no declarations are present. This is used to insert
5627 -- new stuff at the start.
5629 if Is_Non_Empty_List (Declarations (N)) then
5630 L := Declarations (N);
5631 else
5632 L := Statements (HSS);
5633 end if;
5635 -- If local-exception-to-goto optimization active, insert dummy push
5636 -- statements at start, and dummy pop statements at end, but inhibit
5637 -- this if we have No_Exception_Handlers, since they are useless and
5638 -- intefere with analysis, e.g. by codepeer.
5640 if (Debug_Flag_Dot_G
5641 or else Restriction_Active (No_Exception_Propagation))
5642 and then not Restriction_Active (No_Exception_Handlers)
5643 and then not CodePeer_Mode
5644 and then Is_Non_Empty_List (L)
5645 then
5646 declare
5647 FS : constant Node_Id := First (L);
5648 FL : constant Source_Ptr := Sloc (FS);
5649 LS : Node_Id;
5650 LL : Source_Ptr;
5652 begin
5653 -- LS points to either last statement, if statements are present
5654 -- or to the last declaration if there are no statements present.
5655 -- It is the node after which the pop's are generated.
5657 if Is_Non_Empty_List (Statements (HSS)) then
5658 LS := Last (Statements (HSS));
5659 else
5660 LS := Last (L);
5661 end if;
5663 LL := Sloc (LS);
5665 Insert_List_Before_And_Analyze (FS, New_List (
5666 Make_Push_Constraint_Error_Label (FL),
5667 Make_Push_Program_Error_Label (FL),
5668 Make_Push_Storage_Error_Label (FL)));
5670 Insert_List_After_And_Analyze (LS, New_List (
5671 Make_Pop_Constraint_Error_Label (LL),
5672 Make_Pop_Program_Error_Label (LL),
5673 Make_Pop_Storage_Error_Label (LL)));
5674 end;
5675 end if;
5677 -- Need poll on entry to subprogram if polling enabled. We only do this
5678 -- for non-empty subprograms, since it does not seem necessary to poll
5679 -- for a dummy null subprogram.
5681 if Is_Non_Empty_List (L) then
5683 -- Do not add a polling call if the subprogram is to be inlined by
5684 -- the back-end, to avoid repeated calls with multiple inlinings.
5686 if Is_Inlined (Spec_Id)
5687 and then Front_End_Inlining
5688 and then Optimization_Level > 1
5689 then
5690 null;
5691 else
5692 Generate_Poll_Call (First (L));
5693 end if;
5694 end if;
5696 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5698 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5699 declare
5700 F : Entity_Id;
5701 A : Node_Id;
5703 begin
5704 -- Loop through formals
5706 F := First_Formal (Spec_Id);
5707 while Present (F) loop
5708 if Is_Scalar_Type (Etype (F))
5709 and then Ekind (F) = E_Out_Parameter
5710 then
5711 Check_Restriction (No_Default_Initialization, F);
5713 -- Insert the initialization. We turn off validity checks
5714 -- for this assignment, since we do not want any check on
5715 -- the initial value itself (which may well be invalid).
5716 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5718 A :=
5719 Make_Assignment_Statement (Loc,
5720 Name => New_Occurrence_Of (F, Loc),
5721 Expression => Get_Simple_Init_Val (Etype (F), N));
5722 Set_Suppress_Assignment_Checks (A);
5724 Insert_Before_And_Analyze (First (L),
5725 A, Suppress => Validity_Check);
5726 end if;
5728 Next_Formal (F);
5729 end loop;
5730 end;
5731 end if;
5733 -- Clear out statement list for stubbed procedure
5735 if Present (Corresponding_Spec (N)) then
5736 Set_Elaboration_Flag (N, Spec_Id);
5738 if Convention (Spec_Id) = Convention_Stubbed
5739 or else Is_Eliminated (Spec_Id)
5740 then
5741 Set_Declarations (N, Empty_List);
5742 Set_Handled_Statement_Sequence (N,
5743 Make_Handled_Sequence_Of_Statements (Loc,
5744 Statements => New_List (Make_Null_Statement (Loc))));
5746 return;
5747 end if;
5748 end if;
5750 -- Create a set of discriminals for the next protected subprogram body
5752 if Is_List_Member (N)
5753 and then Present (Parent (List_Containing (N)))
5754 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5755 and then Present (Next_Protected_Operation (N))
5756 then
5757 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5758 end if;
5760 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5761 -- subprograms with no specs are not frozen.
5763 declare
5764 Typ : constant Entity_Id := Etype (Spec_Id);
5765 Utyp : constant Entity_Id := Underlying_Type (Typ);
5767 begin
5768 if Is_Limited_View (Typ) then
5769 Set_Returns_By_Ref (Spec_Id);
5771 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5772 Set_Returns_By_Ref (Spec_Id);
5773 end if;
5774 end;
5776 -- For a procedure, we add a return for all possible syntactic ends of
5777 -- the subprogram.
5779 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5780 Add_Return (Spec_Id, Statements (HSS));
5782 if Present (Exception_Handlers (HSS)) then
5783 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5784 while Present (Except_H) loop
5785 Add_Return (Spec_Id, Statements (Except_H));
5786 Next_Non_Pragma (Except_H);
5787 end loop;
5788 end if;
5790 -- For a function, we must deal with the case where there is at least
5791 -- one missing return. What we do is to wrap the entire body of the
5792 -- function in a block:
5794 -- begin
5795 -- ...
5796 -- end;
5798 -- becomes
5800 -- begin
5801 -- begin
5802 -- ...
5803 -- end;
5805 -- raise Program_Error;
5806 -- end;
5808 -- This approach is necessary because the raise must be signalled to the
5809 -- caller, not handled by any local handler (RM 6.4(11)).
5811 -- Note: we do not need to analyze the constructed sequence here, since
5812 -- it has no handler, and an attempt to analyze the handled statement
5813 -- sequence twice is risky in various ways (e.g. the issue of expanding
5814 -- cleanup actions twice).
5816 elsif Has_Missing_Return (Spec_Id) then
5817 declare
5818 Hloc : constant Source_Ptr := Sloc (HSS);
5819 Blok : constant Node_Id :=
5820 Make_Block_Statement (Hloc,
5821 Handled_Statement_Sequence => HSS);
5822 Rais : constant Node_Id :=
5823 Make_Raise_Program_Error (Hloc,
5824 Reason => PE_Missing_Return);
5826 begin
5827 Set_Handled_Statement_Sequence (N,
5828 Make_Handled_Sequence_Of_Statements (Hloc,
5829 Statements => New_List (Blok, Rais)));
5831 Push_Scope (Spec_Id);
5832 Analyze (Blok);
5833 Analyze (Rais);
5834 Pop_Scope;
5835 end;
5836 end if;
5838 -- If subprogram contains a parameterless recursive call, then we may
5839 -- have an infinite recursion, so see if we can generate code to check
5840 -- for this possibility if storage checks are not suppressed.
5842 if Ekind (Spec_Id) = E_Procedure
5843 and then Has_Recursive_Call (Spec_Id)
5844 and then not Storage_Checks_Suppressed (Spec_Id)
5845 then
5846 Detect_Infinite_Recursion (N, Spec_Id);
5847 end if;
5849 -- Set to encode entity names in package body before gigi is called
5851 Qualify_Entity_Names (N);
5853 -- If the body belongs to a nonabstract library-level source primitive
5854 -- of a tagged type, install an elaboration check which ensures that a
5855 -- dispatching call targeting the primitive will not execute the body
5856 -- without it being previously elaborated.
5858 Install_Primitive_Elaboration_Check (N);
5859 end Expand_N_Subprogram_Body;
5861 -----------------------------------
5862 -- Expand_N_Subprogram_Body_Stub --
5863 -----------------------------------
5865 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5866 Bod : Node_Id;
5868 begin
5869 if Present (Corresponding_Body (N)) then
5870 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5872 -- The body may have been expanded already when it is analyzed
5873 -- through the subunit node. Do no expand again: it interferes
5874 -- with the construction of unnesting tables when generating C.
5876 if not Analyzed (Bod) then
5877 Expand_N_Subprogram_Body (Bod);
5878 end if;
5880 -- Add full qualification to entities that may be created late
5881 -- during unnesting.
5883 Qualify_Entity_Names (N);
5884 end if;
5885 end Expand_N_Subprogram_Body_Stub;
5887 -------------------------------------
5888 -- Expand_N_Subprogram_Declaration --
5889 -------------------------------------
5891 -- If the declaration appears within a protected body, it is a private
5892 -- operation of the protected type. We must create the corresponding
5893 -- protected subprogram an associated formals. For a normal protected
5894 -- operation, this is done when expanding the protected type declaration.
5896 -- If the declaration is for a null procedure, emit null body
5898 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5899 Loc : constant Source_Ptr := Sloc (N);
5900 Subp : constant Entity_Id := Defining_Entity (N);
5902 -- Local variables
5904 Scop : constant Entity_Id := Scope (Subp);
5905 Prot_Bod : Node_Id;
5906 Prot_Decl : Node_Id;
5907 Prot_Id : Entity_Id;
5909 -- Start of processing for Expand_N_Subprogram_Declaration
5911 begin
5912 -- In SPARK, subprogram declarations are only allowed in package
5913 -- specifications.
5915 if Nkind (Parent (N)) /= N_Package_Specification then
5916 if Nkind (Parent (N)) = N_Compilation_Unit then
5917 Check_SPARK_05_Restriction
5918 ("subprogram declaration is not a library item", N);
5920 elsif Present (Next (N))
5921 and then Nkind (Next (N)) = N_Pragma
5922 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5923 then
5924 -- In SPARK, subprogram declarations are also permitted in
5925 -- declarative parts when immediately followed by a corresponding
5926 -- pragma Import. We only check here that there is some pragma
5927 -- Import.
5929 null;
5930 else
5931 Check_SPARK_05_Restriction
5932 ("subprogram declaration is not allowed here", N);
5933 end if;
5934 end if;
5936 -- Deal with case of protected subprogram. Do not generate protected
5937 -- operation if operation is flagged as eliminated.
5939 if Is_List_Member (N)
5940 and then Present (Parent (List_Containing (N)))
5941 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5942 and then Is_Protected_Type (Scop)
5943 then
5944 if No (Protected_Body_Subprogram (Subp))
5945 and then not Is_Eliminated (Subp)
5946 then
5947 Prot_Decl :=
5948 Make_Subprogram_Declaration (Loc,
5949 Specification =>
5950 Build_Protected_Sub_Specification
5951 (N, Scop, Unprotected_Mode));
5953 -- The protected subprogram is declared outside of the protected
5954 -- body. Given that the body has frozen all entities so far, we
5955 -- analyze the subprogram and perform freezing actions explicitly.
5956 -- including the generation of an explicit freeze node, to ensure
5957 -- that gigi has the proper order of elaboration.
5958 -- If the body is a subunit, the insertion point is before the
5959 -- stub in the parent.
5961 Prot_Bod := Parent (List_Containing (N));
5963 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5964 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5965 end if;
5967 Insert_Before (Prot_Bod, Prot_Decl);
5968 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5969 Set_Has_Delayed_Freeze (Prot_Id);
5971 Push_Scope (Scope (Scop));
5972 Analyze (Prot_Decl);
5973 Freeze_Before (N, Prot_Id);
5974 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5976 -- Create protected operation as well. Even though the operation
5977 -- is only accessible within the body, it is possible to make it
5978 -- available outside of the protected object by using 'Access to
5979 -- provide a callback, so build protected version in all cases.
5981 Prot_Decl :=
5982 Make_Subprogram_Declaration (Loc,
5983 Specification =>
5984 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5985 Insert_Before (Prot_Bod, Prot_Decl);
5986 Analyze (Prot_Decl);
5988 Pop_Scope;
5989 end if;
5991 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5992 -- cases this is superfluous because calls to it will be automatically
5993 -- inlined, but we definitely need the body if preconditions for the
5994 -- procedure are present, or if performing coverage analysis.
5996 elsif Nkind (Specification (N)) = N_Procedure_Specification
5997 and then Null_Present (Specification (N))
5998 then
5999 declare
6000 Bod : constant Node_Id := Body_To_Inline (N);
6002 begin
6003 Set_Has_Completion (Subp, False);
6004 Append_Freeze_Action (Subp, Bod);
6006 -- The body now contains raise statements, so calls to it will
6007 -- not be inlined.
6009 Set_Is_Inlined (Subp, False);
6010 end;
6011 end if;
6013 -- When generating C code, transform a function that returns a
6014 -- constrained array type into a procedure with an out parameter
6015 -- that carries the return value.
6017 -- We skip this transformation for unchecked conversions, since they
6018 -- are not needed by the C generator (and this also produces cleaner
6019 -- output).
6021 if Modify_Tree_For_C
6022 and then Nkind (Specification (N)) = N_Function_Specification
6023 and then Is_Array_Type (Etype (Subp))
6024 and then Is_Constrained (Etype (Subp))
6025 and then not Is_Unchecked_Conversion_Instance (Subp)
6026 then
6027 Build_Procedure_Form (N);
6028 end if;
6029 end Expand_N_Subprogram_Declaration;
6031 --------------------------------
6032 -- Expand_Non_Function_Return --
6033 --------------------------------
6035 procedure Expand_Non_Function_Return (N : Node_Id) is
6036 pragma Assert (No (Expression (N)));
6038 Loc : constant Source_Ptr := Sloc (N);
6039 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6040 Kind : constant Entity_Kind := Ekind (Scope_Id);
6041 Call : Node_Id;
6042 Acc_Stat : Node_Id;
6043 Goto_Stat : Node_Id;
6044 Lab_Node : Node_Id;
6046 begin
6047 -- Call the _Postconditions procedure if the related subprogram has
6048 -- contract assertions that need to be verified on exit.
6050 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
6051 and then Present (Postconditions_Proc (Scope_Id))
6052 then
6053 Insert_Action (N,
6054 Make_Procedure_Call_Statement (Loc,
6055 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
6056 end if;
6058 -- If it is a return from a procedure do no extra steps
6060 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6061 return;
6063 -- If it is a nested return within an extended one, replace it with a
6064 -- return of the previously declared return object.
6066 elsif Kind = E_Return_Statement then
6067 Rewrite (N,
6068 Make_Simple_Return_Statement (Loc,
6069 Expression =>
6070 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6071 Set_Comes_From_Extended_Return_Statement (N);
6072 Set_Return_Statement_Entity (N, Scope_Id);
6073 Expand_Simple_Function_Return (N);
6074 return;
6075 end if;
6077 pragma Assert (Is_Entry (Scope_Id));
6079 -- Look at the enclosing block to see whether the return is from an
6080 -- accept statement or an entry body.
6082 for J in reverse 0 .. Scope_Stack.Last loop
6083 Scope_Id := Scope_Stack.Table (J).Entity;
6084 exit when Is_Concurrent_Type (Scope_Id);
6085 end loop;
6087 -- If it is a return from accept statement it is expanded as call to
6088 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6090 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6091 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6093 if Is_Task_Type (Scope_Id) then
6095 Call :=
6096 Make_Procedure_Call_Statement (Loc,
6097 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6098 Insert_Before (N, Call);
6099 -- why not insert actions here???
6100 Analyze (Call);
6102 Acc_Stat := Parent (N);
6103 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6104 Acc_Stat := Parent (Acc_Stat);
6105 end loop;
6107 Lab_Node := Last (Statements
6108 (Handled_Statement_Sequence (Acc_Stat)));
6110 Goto_Stat := Make_Goto_Statement (Loc,
6111 Name => New_Occurrence_Of
6112 (Entity (Identifier (Lab_Node)), Loc));
6114 Set_Analyzed (Goto_Stat);
6116 Rewrite (N, Goto_Stat);
6117 Analyze (N);
6119 -- If it is a return from an entry body, put a Complete_Entry_Body call
6120 -- in front of the return.
6122 elsif Is_Protected_Type (Scope_Id) then
6123 Call :=
6124 Make_Procedure_Call_Statement (Loc,
6125 Name =>
6126 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6127 Parameter_Associations => New_List (
6128 Make_Attribute_Reference (Loc,
6129 Prefix =>
6130 New_Occurrence_Of
6131 (Find_Protection_Object (Current_Scope), Loc),
6132 Attribute_Name => Name_Unchecked_Access)));
6134 Insert_Before (N, Call);
6135 Analyze (Call);
6136 end if;
6137 end Expand_Non_Function_Return;
6139 ---------------------------------------
6140 -- Expand_Protected_Object_Reference --
6141 ---------------------------------------
6143 function Expand_Protected_Object_Reference
6144 (N : Node_Id;
6145 Scop : Entity_Id) return Node_Id
6147 Loc : constant Source_Ptr := Sloc (N);
6148 Corr : Entity_Id;
6149 Rec : Node_Id;
6150 Param : Entity_Id;
6151 Proc : Entity_Id;
6153 begin
6154 Rec := Make_Identifier (Loc, Name_uObject);
6155 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6157 -- Find enclosing protected operation, and retrieve its first parameter,
6158 -- which denotes the enclosing protected object. If the enclosing
6159 -- operation is an entry, we are immediately within the protected body,
6160 -- and we can retrieve the object from the service entries procedure. A
6161 -- barrier function has the same signature as an entry. A barrier
6162 -- function is compiled within the protected object, but unlike
6163 -- protected operations its never needs locks, so that its protected
6164 -- body subprogram points to itself.
6166 Proc := Current_Scope;
6167 while Present (Proc)
6168 and then Scope (Proc) /= Scop
6169 loop
6170 Proc := Scope (Proc);
6171 end loop;
6173 Corr := Protected_Body_Subprogram (Proc);
6175 if No (Corr) then
6177 -- Previous error left expansion incomplete.
6178 -- Nothing to do on this call.
6180 return Empty;
6181 end if;
6183 Param :=
6184 Defining_Identifier
6185 (First (Parameter_Specifications (Parent (Corr))));
6187 if Is_Subprogram (Proc) and then Proc /= Corr then
6189 -- Protected function or procedure
6191 Set_Entity (Rec, Param);
6193 -- Rec is a reference to an entity which will not be in scope when
6194 -- the call is reanalyzed, and needs no further analysis.
6196 Set_Analyzed (Rec);
6198 else
6199 -- Entry or barrier function for entry body. The first parameter of
6200 -- the entry body procedure is pointer to the object. We create a
6201 -- local variable of the proper type, duplicating what is done to
6202 -- define _object later on.
6204 declare
6205 Decls : List_Id;
6206 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6208 begin
6209 Decls := New_List (
6210 Make_Full_Type_Declaration (Loc,
6211 Defining_Identifier => Obj_Ptr,
6212 Type_Definition =>
6213 Make_Access_To_Object_Definition (Loc,
6214 Subtype_Indication =>
6215 New_Occurrence_Of
6216 (Corresponding_Record_Type (Scop), Loc))));
6218 Insert_Actions (N, Decls);
6219 Freeze_Before (N, Obj_Ptr);
6221 Rec :=
6222 Make_Explicit_Dereference (Loc,
6223 Prefix =>
6224 Unchecked_Convert_To (Obj_Ptr,
6225 New_Occurrence_Of (Param, Loc)));
6227 -- Analyze new actual. Other actuals in calls are already analyzed
6228 -- and the list of actuals is not reanalyzed after rewriting.
6230 Set_Parent (Rec, N);
6231 Analyze (Rec);
6232 end;
6233 end if;
6235 return Rec;
6236 end Expand_Protected_Object_Reference;
6238 --------------------------------------
6239 -- Expand_Protected_Subprogram_Call --
6240 --------------------------------------
6242 procedure Expand_Protected_Subprogram_Call
6243 (N : Node_Id;
6244 Subp : Entity_Id;
6245 Scop : Entity_Id)
6247 Rec : Node_Id;
6249 procedure Expand_Internal_Init_Call;
6250 -- A call to an operation of the type may occur in the initialization
6251 -- of a private component. In that case the prefix of the call is an
6252 -- entity name and the call is treated as internal even though it
6253 -- appears in code outside of the protected type.
6255 procedure Freeze_Called_Function;
6256 -- If it is a function call it can appear in elaboration code and
6257 -- the called entity must be frozen before the call. This must be
6258 -- done before the call is expanded, as the expansion may rewrite it
6259 -- to something other than a call (e.g. a temporary initialized in a
6260 -- transient block).
6262 -------------------------------
6263 -- Expand_Internal_Init_Call --
6264 -------------------------------
6266 procedure Expand_Internal_Init_Call is
6267 begin
6268 -- If the context is a protected object (rather than a protected
6269 -- type) the call itself is bound to raise program_error because
6270 -- the protected body will not have been elaborated yet. This is
6271 -- diagnosed subsequently in Sem_Elab.
6273 Freeze_Called_Function;
6275 -- The target of the internal call is the first formal of the
6276 -- enclosing initialization procedure.
6278 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6279 Build_Protected_Subprogram_Call (N,
6280 Name => Name (N),
6281 Rec => Rec,
6282 External => False);
6283 Analyze (N);
6284 Resolve (N, Etype (Subp));
6285 end Expand_Internal_Init_Call;
6287 ----------------------------
6288 -- Freeze_Called_Function --
6289 ----------------------------
6291 procedure Freeze_Called_Function is
6292 begin
6293 if Ekind (Subp) = E_Function then
6294 Freeze_Expression (Name (N));
6295 end if;
6296 end Freeze_Called_Function;
6298 -- Start of processing for Expand_Protected_Subprogram_Call
6300 begin
6301 -- If the protected object is not an enclosing scope, this is an inter-
6302 -- object function call. Inter-object procedure calls are expanded by
6303 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6304 -- subprogram being called is in the protected body being compiled, and
6305 -- if the protected object in the call is statically the enclosing type.
6306 -- The object may be a component of some other data structure, in which
6307 -- case this must be handled as an inter-object call.
6309 if not In_Open_Scopes (Scop)
6310 or else Is_Entry_Wrapper (Current_Scope)
6311 or else not Is_Entity_Name (Name (N))
6312 then
6313 if Nkind (Name (N)) = N_Selected_Component then
6314 Rec := Prefix (Name (N));
6316 elsif Nkind (Name (N)) = N_Indexed_Component then
6317 Rec := Prefix (Prefix (Name (N)));
6319 -- If this is a call within an entry wrapper, it appears within a
6320 -- precondition that calls another primitive of the synchronized
6321 -- type. The target object of the call is the first actual on the
6322 -- wrapper. Note that this is an external call, because the wrapper
6323 -- is called outside of the synchronized object. This means that
6324 -- an entry call to an entry with preconditions involves two
6325 -- synchronized operations.
6327 elsif Ekind (Current_Scope) = E_Procedure
6328 and then Is_Entry_Wrapper (Current_Scope)
6329 then
6330 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6332 else
6333 -- If the context is the initialization procedure for a protected
6334 -- type, the call is legal because the called entity must be a
6335 -- function of that enclosing type, and this is treated as an
6336 -- internal call.
6338 pragma Assert
6339 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6341 Expand_Internal_Init_Call;
6342 return;
6343 end if;
6345 Freeze_Called_Function;
6346 Build_Protected_Subprogram_Call (N,
6347 Name => New_Occurrence_Of (Subp, Sloc (N)),
6348 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6349 External => True);
6351 else
6352 Rec := Expand_Protected_Object_Reference (N, Scop);
6354 if No (Rec) then
6355 return;
6356 end if;
6358 Freeze_Called_Function;
6359 Build_Protected_Subprogram_Call (N,
6360 Name => Name (N),
6361 Rec => Rec,
6362 External => False);
6363 end if;
6365 -- Analyze and resolve the new call. The actuals have already been
6366 -- resolved, but expansion of a function call will add extra actuals
6367 -- if needed. Analysis of a procedure call already includes resolution.
6369 Analyze (N);
6371 if Ekind (Subp) = E_Function then
6372 Resolve (N, Etype (Subp));
6373 end if;
6374 end Expand_Protected_Subprogram_Call;
6376 -----------------------------------
6377 -- Expand_Simple_Function_Return --
6378 -----------------------------------
6380 -- The "simple" comes from the syntax rule simple_return_statement. The
6381 -- semantics are not at all simple.
6383 procedure Expand_Simple_Function_Return (N : Node_Id) is
6384 Loc : constant Source_Ptr := Sloc (N);
6386 Scope_Id : constant Entity_Id :=
6387 Return_Applies_To (Return_Statement_Entity (N));
6388 -- The function we are returning from
6390 R_Type : constant Entity_Id := Etype (Scope_Id);
6391 -- The result type of the function
6393 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6395 Exp : Node_Id := Expression (N);
6396 pragma Assert (Present (Exp));
6398 Exptyp : constant Entity_Id := Etype (Exp);
6399 -- The type of the expression (not necessarily the same as R_Type)
6401 Subtype_Ind : Node_Id;
6402 -- If the result type of the function is class-wide and the expression
6403 -- has a specific type, then we use the expression's type as the type of
6404 -- the return object. In cases where the expression is an aggregate that
6405 -- is built in place, this avoids the need for an expensive conversion
6406 -- of the return object to the specific type on assignments to the
6407 -- individual components.
6409 begin
6410 if Is_Class_Wide_Type (R_Type)
6411 and then not Is_Class_Wide_Type (Exptyp)
6412 and then Nkind (Exp) /= N_Type_Conversion
6413 then
6414 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6415 else
6416 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6418 -- If the result type is class-wide and the expression is a view
6419 -- conversion, the conversion plays no role in the expansion because
6420 -- it does not modify the tag of the object. Remove the conversion
6421 -- altogether to prevent tag overwriting.
6423 if Is_Class_Wide_Type (R_Type)
6424 and then not Is_Class_Wide_Type (Exptyp)
6425 and then Nkind (Exp) = N_Type_Conversion
6426 then
6427 Exp := Expression (Exp);
6428 end if;
6429 end if;
6431 -- For the case of a simple return that does not come from an
6432 -- extended return, in the case of build-in-place, we rewrite
6433 -- "return <expression>;" to be:
6435 -- return _anon_ : <return_subtype> := <expression>
6437 -- The expansion produced by Expand_N_Extended_Return_Statement will
6438 -- contain simple return statements (for example, a block containing
6439 -- simple return of the return object), which brings us back here with
6440 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6441 -- checking for a simple return that does not come from an extended
6442 -- return is to avoid this infinite recursion.
6444 -- The reason for this design is that for Ada 2005 limited returns, we
6445 -- need to reify the return object, so we can build it "in place", and
6446 -- we need a block statement to hang finalization and tasking stuff.
6448 -- ??? In order to avoid disruption, we avoid translating to extended
6449 -- return except in the cases where we really need to (Ada 2005 for
6450 -- inherently limited). We might prefer to do this translation in all
6451 -- cases (except perhaps for the case of Ada 95 inherently limited),
6452 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6453 -- code. This would also allow us to do the build-in-place optimization
6454 -- for efficiency even in cases where it is semantically not required.
6456 -- As before, we check the type of the return expression rather than the
6457 -- return type of the function, because the latter may be a limited
6458 -- class-wide interface type, which is not a limited type, even though
6459 -- the type of the expression may be.
6461 pragma Assert
6462 (Comes_From_Extended_Return_Statement (N)
6463 or else not Is_Build_In_Place_Function_Call (Exp)
6464 or else Is_Build_In_Place_Function (Scope_Id));
6466 if not Comes_From_Extended_Return_Statement (N)
6467 and then Is_Build_In_Place_Function (Scope_Id)
6468 and then not Debug_Flag_Dot_L
6470 -- The functionality of interface thunks is simple and it is always
6471 -- handled by means of simple return statements. This leaves their
6472 -- expansion simple and clean.
6474 and then not Is_Thunk (Current_Scope)
6475 then
6476 declare
6477 Return_Object_Entity : constant Entity_Id :=
6478 Make_Temporary (Loc, 'R', Exp);
6480 Obj_Decl : constant Node_Id :=
6481 Make_Object_Declaration (Loc,
6482 Defining_Identifier => Return_Object_Entity,
6483 Object_Definition => Subtype_Ind,
6484 Expression => Exp);
6486 Ext : constant Node_Id :=
6487 Make_Extended_Return_Statement (Loc,
6488 Return_Object_Declarations => New_List (Obj_Decl));
6489 -- Do not perform this high-level optimization if the result type
6490 -- is an interface because the "this" pointer must be displaced.
6492 begin
6493 Rewrite (N, Ext);
6494 Analyze (N);
6495 return;
6496 end;
6497 end if;
6499 -- Here we have a simple return statement that is part of the expansion
6500 -- of an extended return statement (either written by the user, or
6501 -- generated by the above code).
6503 -- Always normalize C/Fortran boolean result. This is not always needed,
6504 -- but it seems a good idea to minimize the passing around of non-
6505 -- normalized values, and in any case this handles the processing of
6506 -- barrier functions for protected types, which turn the condition into
6507 -- a return statement.
6509 if Is_Boolean_Type (Exptyp)
6510 and then Nonzero_Is_True (Exptyp)
6511 then
6512 Adjust_Condition (Exp);
6513 Adjust_Result_Type (Exp, Exptyp);
6514 end if;
6516 -- Do validity check if enabled for returns
6518 if Validity_Checks_On
6519 and then Validity_Check_Returns
6520 then
6521 Ensure_Valid (Exp);
6522 end if;
6524 -- Check the result expression of a scalar function against the subtype
6525 -- of the function by inserting a conversion. This conversion must
6526 -- eventually be performed for other classes of types, but for now it's
6527 -- only done for scalars.
6528 -- ???
6530 if Is_Scalar_Type (Exptyp) then
6531 Rewrite (Exp, Convert_To (R_Type, Exp));
6533 -- The expression is resolved to ensure that the conversion gets
6534 -- expanded to generate a possible constraint check.
6536 Analyze_And_Resolve (Exp, R_Type);
6537 end if;
6539 -- Deal with returning variable length objects and controlled types
6541 -- Nothing to do if we are returning by reference, or this is not a
6542 -- type that requires special processing (indicated by the fact that
6543 -- it requires a cleanup scope for the secondary stack case).
6545 if Is_Build_In_Place_Function (Scope_Id)
6546 or else Is_Limited_Interface (Exptyp)
6547 then
6548 null;
6550 -- No copy needed for thunks returning interface type objects since
6551 -- the object is returned by reference and the maximum functionality
6552 -- required is just to displace the pointer.
6554 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6555 null;
6557 -- If the call is within a thunk and the type is a limited view, the
6558 -- backend will eventually see the non-limited view of the type.
6560 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6561 return;
6563 elsif not Requires_Transient_Scope (R_Type) then
6565 -- Mutable records with variable-length components are not returned
6566 -- on the sec-stack, so we need to make sure that the back end will
6567 -- only copy back the size of the actual value, and not the maximum
6568 -- size. We create an actual subtype for this purpose. However we
6569 -- need not do it if the expression is a function call since this
6570 -- will be done in the called function and doing it here too would
6571 -- cause a temporary with maximum size to be created.
6573 declare
6574 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6575 Decl : Node_Id;
6576 Ent : Entity_Id;
6577 begin
6578 if Nkind (Exp) /= N_Function_Call
6579 and then Has_Discriminants (Ubt)
6580 and then not Is_Constrained (Ubt)
6581 and then not Has_Unchecked_Union (Ubt)
6582 then
6583 Decl := Build_Actual_Subtype (Ubt, Exp);
6584 Ent := Defining_Identifier (Decl);
6585 Insert_Action (Exp, Decl);
6586 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6587 Analyze_And_Resolve (Exp);
6588 end if;
6589 end;
6591 -- Here if secondary stack is used
6593 else
6594 -- Prevent the reclamation of the secondary stack by all enclosing
6595 -- blocks and loops as well as the related function; otherwise the
6596 -- result would be reclaimed too early.
6598 Set_Enclosing_Sec_Stack_Return (N);
6600 -- Optimize the case where the result is a function call. In this
6601 -- case either the result is already on the secondary stack, or is
6602 -- already being returned with the stack pointer depressed and no
6603 -- further processing is required except to set the By_Ref flag
6604 -- to ensure that gigi does not attempt an extra unnecessary copy.
6605 -- (actually not just unnecessary but harmfully wrong in the case
6606 -- of a controlled type, where gigi does not know how to do a copy).
6607 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6608 -- for array types if the constrained status of the target type is
6609 -- different from that of the expression.
6611 if Requires_Transient_Scope (Exptyp)
6612 and then
6613 (not Is_Array_Type (Exptyp)
6614 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6615 or else CW_Or_Has_Controlled_Part (Utyp))
6616 and then Nkind (Exp) = N_Function_Call
6617 then
6618 Set_By_Ref (N);
6620 -- Remove side effects from the expression now so that other parts
6621 -- of the expander do not have to reanalyze this node without this
6622 -- optimization
6624 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6626 -- Ada 2005 (AI-251): If the type of the returned object is
6627 -- an interface then add an implicit type conversion to force
6628 -- displacement of the "this" pointer.
6630 if Is_Interface (R_Type) then
6631 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6632 end if;
6634 Analyze_And_Resolve (Exp, R_Type);
6636 -- For controlled types, do the allocation on the secondary stack
6637 -- manually in order to call adjust at the right time:
6639 -- type Anon1 is access R_Type;
6640 -- for Anon1'Storage_pool use ss_pool;
6641 -- Anon2 : anon1 := new R_Type'(expr);
6642 -- return Anon2.all;
6644 -- We do the same for classwide types that are not potentially
6645 -- controlled (by the virtue of restriction No_Finalization) because
6646 -- gigi is not able to properly allocate class-wide types.
6648 elsif CW_Or_Has_Controlled_Part (Utyp) then
6649 declare
6650 Loc : constant Source_Ptr := Sloc (N);
6651 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6652 Alloc_Node : Node_Id;
6653 Temp : Entity_Id;
6655 begin
6656 Set_Ekind (Acc_Typ, E_Access_Type);
6658 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6660 -- This is an allocator for the secondary stack, and it's fine
6661 -- to have Comes_From_Source set False on it, as gigi knows not
6662 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6664 Alloc_Node :=
6665 Make_Allocator (Loc,
6666 Expression =>
6667 Make_Qualified_Expression (Loc,
6668 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6669 Expression => Relocate_Node (Exp)));
6671 -- We do not want discriminant checks on the declaration,
6672 -- given that it gets its value from the allocator.
6674 Set_No_Initialization (Alloc_Node);
6676 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6678 Insert_List_Before_And_Analyze (N, New_List (
6679 Make_Full_Type_Declaration (Loc,
6680 Defining_Identifier => Acc_Typ,
6681 Type_Definition =>
6682 Make_Access_To_Object_Definition (Loc,
6683 Subtype_Indication => Subtype_Ind)),
6685 Make_Object_Declaration (Loc,
6686 Defining_Identifier => Temp,
6687 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6688 Expression => Alloc_Node)));
6690 Rewrite (Exp,
6691 Make_Explicit_Dereference (Loc,
6692 Prefix => New_Occurrence_Of (Temp, Loc)));
6694 -- Ada 2005 (AI-251): If the type of the returned object is
6695 -- an interface then add an implicit type conversion to force
6696 -- displacement of the "this" pointer.
6698 if Is_Interface (R_Type) then
6699 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6700 end if;
6702 Analyze_And_Resolve (Exp, R_Type);
6703 end;
6705 -- Otherwise use the gigi mechanism to allocate result on the
6706 -- secondary stack.
6708 else
6709 Check_Restriction (No_Secondary_Stack, N);
6710 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6711 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6712 end if;
6713 end if;
6715 -- Implement the rules of 6.5(8-10), which require a tag check in
6716 -- the case of a limited tagged return type, and tag reassignment for
6717 -- nonlimited tagged results. These actions are needed when the return
6718 -- type is a specific tagged type and the result expression is a
6719 -- conversion or a formal parameter, because in that case the tag of
6720 -- the expression might differ from the tag of the specific result type.
6722 if Is_Tagged_Type (Utyp)
6723 and then not Is_Class_Wide_Type (Utyp)
6724 and then (Nkind_In (Exp, N_Type_Conversion,
6725 N_Unchecked_Type_Conversion)
6726 or else (Is_Entity_Name (Exp)
6727 and then Ekind (Entity (Exp)) in Formal_Kind))
6728 then
6729 -- When the return type is limited, perform a check that the tag of
6730 -- the result is the same as the tag of the return type.
6732 if Is_Limited_Type (R_Type) then
6733 Insert_Action (Exp,
6734 Make_Raise_Constraint_Error (Loc,
6735 Condition =>
6736 Make_Op_Ne (Loc,
6737 Left_Opnd =>
6738 Make_Selected_Component (Loc,
6739 Prefix => Duplicate_Subexpr (Exp),
6740 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6741 Right_Opnd =>
6742 Make_Attribute_Reference (Loc,
6743 Prefix =>
6744 New_Occurrence_Of (Base_Type (Utyp), Loc),
6745 Attribute_Name => Name_Tag)),
6746 Reason => CE_Tag_Check_Failed));
6748 -- If the result type is a specific nonlimited tagged type, then we
6749 -- have to ensure that the tag of the result is that of the result
6750 -- type. This is handled by making a copy of the expression in
6751 -- the case where it might have a different tag, namely when the
6752 -- expression is a conversion or a formal parameter. We create a new
6753 -- object of the result type and initialize it from the expression,
6754 -- which will implicitly force the tag to be set appropriately.
6756 else
6757 declare
6758 ExpR : constant Node_Id := Relocate_Node (Exp);
6759 Result_Id : constant Entity_Id :=
6760 Make_Temporary (Loc, 'R', ExpR);
6761 Result_Exp : constant Node_Id :=
6762 New_Occurrence_Of (Result_Id, Loc);
6763 Result_Obj : constant Node_Id :=
6764 Make_Object_Declaration (Loc,
6765 Defining_Identifier => Result_Id,
6766 Object_Definition =>
6767 New_Occurrence_Of (R_Type, Loc),
6768 Constant_Present => True,
6769 Expression => ExpR);
6771 begin
6772 Set_Assignment_OK (Result_Obj);
6773 Insert_Action (Exp, Result_Obj);
6775 Rewrite (Exp, Result_Exp);
6776 Analyze_And_Resolve (Exp, R_Type);
6777 end;
6778 end if;
6780 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6781 -- a check that the level of the return expression's underlying type
6782 -- is not deeper than the level of the master enclosing the function.
6783 -- Always generate the check when the type of the return expression
6784 -- is class-wide, when it's a type conversion, or when it's a formal
6785 -- parameter. Otherwise, suppress the check in the case where the
6786 -- return expression has a specific type whose level is known not to
6787 -- be statically deeper than the function's result type.
6789 -- No runtime check needed in interface thunks since it is performed
6790 -- by the target primitive associated with the thunk.
6792 -- Note: accessibility check is skipped in the VM case, since there
6793 -- does not seem to be any practical way to implement this check.
6795 elsif Ada_Version >= Ada_2005
6796 and then Tagged_Type_Expansion
6797 and then Is_Class_Wide_Type (R_Type)
6798 and then not Is_Thunk (Current_Scope)
6799 and then not Scope_Suppress.Suppress (Accessibility_Check)
6800 and then
6801 (Is_Class_Wide_Type (Etype (Exp))
6802 or else Nkind_In (Exp, N_Type_Conversion,
6803 N_Unchecked_Type_Conversion)
6804 or else (Is_Entity_Name (Exp)
6805 and then Ekind (Entity (Exp)) in Formal_Kind)
6806 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6807 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6808 then
6809 declare
6810 Tag_Node : Node_Id;
6812 begin
6813 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6814 -- "this" to reference the base of the object. This is required to
6815 -- get access to the TSD of the object.
6817 if Is_Class_Wide_Type (Etype (Exp))
6818 and then Is_Interface (Etype (Exp))
6819 then
6820 -- If the expression is an explicit dereference then we can
6821 -- directly displace the pointer to reference the base of
6822 -- the object.
6824 if Nkind (Exp) = N_Explicit_Dereference then
6825 Tag_Node :=
6826 Make_Explicit_Dereference (Loc,
6827 Prefix =>
6828 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6829 Make_Function_Call (Loc,
6830 Name =>
6831 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6832 Parameter_Associations => New_List (
6833 Unchecked_Convert_To (RTE (RE_Address),
6834 Duplicate_Subexpr (Prefix (Exp)))))));
6836 -- Similar case to the previous one but the expression is a
6837 -- renaming of an explicit dereference.
6839 elsif Nkind (Exp) = N_Identifier
6840 and then Present (Renamed_Object (Entity (Exp)))
6841 and then Nkind (Renamed_Object (Entity (Exp)))
6842 = N_Explicit_Dereference
6843 then
6844 Tag_Node :=
6845 Make_Explicit_Dereference (Loc,
6846 Prefix =>
6847 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6848 Make_Function_Call (Loc,
6849 Name =>
6850 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6851 Parameter_Associations => New_List (
6852 Unchecked_Convert_To (RTE (RE_Address),
6853 Duplicate_Subexpr
6854 (Prefix
6855 (Renamed_Object (Entity (Exp)))))))));
6857 -- Common case: obtain the address of the actual object and
6858 -- displace the pointer to reference the base of the object.
6860 else
6861 Tag_Node :=
6862 Make_Explicit_Dereference (Loc,
6863 Prefix =>
6864 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6865 Make_Function_Call (Loc,
6866 Name =>
6867 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6868 Parameter_Associations => New_List (
6869 Make_Attribute_Reference (Loc,
6870 Prefix => Duplicate_Subexpr (Exp),
6871 Attribute_Name => Name_Address)))));
6872 end if;
6873 else
6874 Tag_Node :=
6875 Make_Attribute_Reference (Loc,
6876 Prefix => Duplicate_Subexpr (Exp),
6877 Attribute_Name => Name_Tag);
6878 end if;
6880 -- CodePeer does not do anything useful with
6881 -- Ada.Tags.Type_Specific_Data components.
6883 if not CodePeer_Mode then
6884 Insert_Action (Exp,
6885 Make_Raise_Program_Error (Loc,
6886 Condition =>
6887 Make_Op_Gt (Loc,
6888 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6889 Right_Opnd =>
6890 Make_Integer_Literal (Loc,
6891 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6892 Reason => PE_Accessibility_Check_Failed));
6893 end if;
6894 end;
6896 -- AI05-0073: If function has a controlling access result, check that
6897 -- the tag of the return value, if it is not null, matches designated
6898 -- type of return type.
6900 -- The return expression is referenced twice in the code below, so it
6901 -- must be made free of side effects. Given that different compilers
6902 -- may evaluate these parameters in different order, both occurrences
6903 -- perform a copy.
6905 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6906 and then Has_Controlling_Result (Scope_Id)
6907 then
6908 Insert_Action (N,
6909 Make_Raise_Constraint_Error (Loc,
6910 Condition =>
6911 Make_And_Then (Loc,
6912 Left_Opnd =>
6913 Make_Op_Ne (Loc,
6914 Left_Opnd => Duplicate_Subexpr (Exp),
6915 Right_Opnd => Make_Null (Loc)),
6917 Right_Opnd => Make_Op_Ne (Loc,
6918 Left_Opnd =>
6919 Make_Selected_Component (Loc,
6920 Prefix => Duplicate_Subexpr (Exp),
6921 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6923 Right_Opnd =>
6924 Make_Attribute_Reference (Loc,
6925 Prefix =>
6926 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6927 Attribute_Name => Name_Tag))),
6929 Reason => CE_Tag_Check_Failed),
6930 Suppress => All_Checks);
6931 end if;
6933 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6934 -- ensure that the function result does not outlive an
6935 -- object designated by one of it discriminants.
6937 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6938 and then Has_Unconstrained_Access_Discriminants (R_Type)
6939 then
6940 declare
6941 Discrim_Source : Node_Id;
6943 procedure Check_Against_Result_Level (Level : Node_Id);
6944 -- Check the given accessibility level against the level
6945 -- determined by the point of call. (AI05-0234).
6947 --------------------------------
6948 -- Check_Against_Result_Level --
6949 --------------------------------
6951 procedure Check_Against_Result_Level (Level : Node_Id) is
6952 begin
6953 Insert_Action (N,
6954 Make_Raise_Program_Error (Loc,
6955 Condition =>
6956 Make_Op_Gt (Loc,
6957 Left_Opnd => Level,
6958 Right_Opnd =>
6959 New_Occurrence_Of
6960 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6961 Reason => PE_Accessibility_Check_Failed));
6962 end Check_Against_Result_Level;
6964 begin
6965 Discrim_Source := Exp;
6966 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6967 Discrim_Source := Expression (Discrim_Source);
6968 end loop;
6970 if Nkind (Discrim_Source) = N_Identifier
6971 and then Is_Return_Object (Entity (Discrim_Source))
6972 then
6973 Discrim_Source := Entity (Discrim_Source);
6975 if Is_Constrained (Etype (Discrim_Source)) then
6976 Discrim_Source := Etype (Discrim_Source);
6977 else
6978 Discrim_Source := Expression (Parent (Discrim_Source));
6979 end if;
6981 elsif Nkind (Discrim_Source) = N_Identifier
6982 and then Nkind_In (Original_Node (Discrim_Source),
6983 N_Aggregate, N_Extension_Aggregate)
6984 then
6985 Discrim_Source := Original_Node (Discrim_Source);
6987 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6988 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6989 then
6990 Discrim_Source := Original_Node (Discrim_Source);
6991 end if;
6993 Discrim_Source := Unqual_Conv (Discrim_Source);
6995 case Nkind (Discrim_Source) is
6996 when N_Defining_Identifier =>
6997 pragma Assert (Is_Composite_Type (Discrim_Source)
6998 and then Has_Discriminants (Discrim_Source)
6999 and then Is_Constrained (Discrim_Source));
7001 declare
7002 Discrim : Entity_Id :=
7003 First_Discriminant (Base_Type (R_Type));
7004 Disc_Elmt : Elmt_Id :=
7005 First_Elmt (Discriminant_Constraint
7006 (Discrim_Source));
7007 begin
7008 loop
7009 if Ekind (Etype (Discrim)) =
7010 E_Anonymous_Access_Type
7011 then
7012 Check_Against_Result_Level
7013 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7014 end if;
7016 Next_Elmt (Disc_Elmt);
7017 Next_Discriminant (Discrim);
7018 exit when not Present (Discrim);
7019 end loop;
7020 end;
7022 when N_Aggregate
7023 | N_Extension_Aggregate
7025 -- Unimplemented: extension aggregate case where discrims
7026 -- come from ancestor part, not extension part.
7028 declare
7029 Discrim : Entity_Id :=
7030 First_Discriminant (Base_Type (R_Type));
7032 Disc_Exp : Node_Id := Empty;
7034 Positionals_Exhausted
7035 : Boolean := not Present (Expressions
7036 (Discrim_Source));
7038 function Associated_Expr
7039 (Comp_Id : Entity_Id;
7040 Associations : List_Id) return Node_Id;
7042 -- Given a component and a component associations list,
7043 -- locate the expression for that component; returns
7044 -- Empty if no such expression is found.
7046 ---------------------
7047 -- Associated_Expr --
7048 ---------------------
7050 function Associated_Expr
7051 (Comp_Id : Entity_Id;
7052 Associations : List_Id) return Node_Id
7054 Assoc : Node_Id;
7055 Choice : Node_Id;
7057 begin
7058 -- Simple linear search seems ok here
7060 Assoc := First (Associations);
7061 while Present (Assoc) loop
7062 Choice := First (Choices (Assoc));
7063 while Present (Choice) loop
7064 if (Nkind (Choice) = N_Identifier
7065 and then Chars (Choice) = Chars (Comp_Id))
7066 or else (Nkind (Choice) = N_Others_Choice)
7067 then
7068 return Expression (Assoc);
7069 end if;
7071 Next (Choice);
7072 end loop;
7074 Next (Assoc);
7075 end loop;
7077 return Empty;
7078 end Associated_Expr;
7080 -- Start of processing for Expand_Simple_Function_Return
7082 begin
7083 if not Positionals_Exhausted then
7084 Disc_Exp := First (Expressions (Discrim_Source));
7085 end if;
7087 loop
7088 if Positionals_Exhausted then
7089 Disc_Exp :=
7090 Associated_Expr
7091 (Discrim,
7092 Component_Associations (Discrim_Source));
7093 end if;
7095 if Ekind (Etype (Discrim)) =
7096 E_Anonymous_Access_Type
7097 then
7098 Check_Against_Result_Level
7099 (Dynamic_Accessibility_Level (Disc_Exp));
7100 end if;
7102 Next_Discriminant (Discrim);
7103 exit when not Present (Discrim);
7105 if not Positionals_Exhausted then
7106 Next (Disc_Exp);
7107 Positionals_Exhausted := not Present (Disc_Exp);
7108 end if;
7109 end loop;
7110 end;
7112 when N_Function_Call =>
7114 -- No check needed (check performed by callee)
7116 null;
7118 when others =>
7119 declare
7120 Level : constant Node_Id :=
7121 Make_Integer_Literal (Loc,
7122 Object_Access_Level (Discrim_Source));
7124 begin
7125 -- Unimplemented: check for name prefix that includes
7126 -- a dereference of an access value with a dynamic
7127 -- accessibility level (e.g., an access param or a
7128 -- saooaaat) and use dynamic level in that case. For
7129 -- example:
7130 -- return Access_Param.all(Some_Index).Some_Component;
7131 -- ???
7133 Set_Etype (Level, Standard_Natural);
7134 Check_Against_Result_Level (Level);
7135 end;
7136 end case;
7137 end;
7138 end if;
7140 -- If we are returning an object that may not be bit-aligned, then copy
7141 -- the value into a temporary first. This copy may need to expand to a
7142 -- loop of component operations.
7144 if Is_Possibly_Unaligned_Slice (Exp)
7145 or else Is_Possibly_Unaligned_Object (Exp)
7146 then
7147 declare
7148 ExpR : constant Node_Id := Relocate_Node (Exp);
7149 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7150 begin
7151 Insert_Action (Exp,
7152 Make_Object_Declaration (Loc,
7153 Defining_Identifier => Tnn,
7154 Constant_Present => True,
7155 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7156 Expression => ExpR),
7157 Suppress => All_Checks);
7158 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7159 end;
7160 end if;
7162 -- Call the _Postconditions procedure if the related function has
7163 -- contract assertions that need to be verified on exit.
7165 if Ekind (Scope_Id) = E_Function
7166 and then Present (Postconditions_Proc (Scope_Id))
7167 then
7168 -- In the case of discriminated objects, we have created a
7169 -- constrained subtype above, and used the underlying type. This
7170 -- transformation is post-analysis and harmless, except that now the
7171 -- call to the post-condition will be analyzed and the type kinds
7172 -- have to match.
7174 if Nkind (Exp) = N_Unchecked_Type_Conversion
7175 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7176 then
7177 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7178 end if;
7180 -- We are going to reference the returned value twice in this case,
7181 -- once in the call to _Postconditions, and once in the actual return
7182 -- statement, but we can't have side effects happening twice.
7184 Force_Evaluation (Exp, Mode => Strict);
7186 -- Generate call to _Postconditions
7188 Insert_Action (Exp,
7189 Make_Procedure_Call_Statement (Loc,
7190 Name =>
7191 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
7192 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
7193 end if;
7195 -- Ada 2005 (AI-251): If this return statement corresponds with an
7196 -- simple return statement associated with an extended return statement
7197 -- and the type of the returned object is an interface then generate an
7198 -- implicit conversion to force displacement of the "this" pointer.
7200 if Ada_Version >= Ada_2005
7201 and then Comes_From_Extended_Return_Statement (N)
7202 and then Nkind (Expression (N)) = N_Identifier
7203 and then Is_Interface (Utyp)
7204 and then Utyp /= Underlying_Type (Exptyp)
7205 then
7206 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7207 Analyze_And_Resolve (Exp);
7208 end if;
7209 end Expand_Simple_Function_Return;
7211 --------------------------------------------
7212 -- Has_Unconstrained_Access_Discriminants --
7213 --------------------------------------------
7215 function Has_Unconstrained_Access_Discriminants
7216 (Subtyp : Entity_Id) return Boolean
7218 Discr : Entity_Id;
7220 begin
7221 if Has_Discriminants (Subtyp)
7222 and then not Is_Constrained (Subtyp)
7223 then
7224 Discr := First_Discriminant (Subtyp);
7225 while Present (Discr) loop
7226 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7227 return True;
7228 end if;
7230 Next_Discriminant (Discr);
7231 end loop;
7232 end if;
7234 return False;
7235 end Has_Unconstrained_Access_Discriminants;
7237 -----------------------------------
7238 -- Is_Build_In_Place_Result_Type --
7239 -----------------------------------
7241 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
7242 begin
7243 if not Expander_Active then
7244 return False;
7245 end if;
7247 -- In Ada 2005 all functions with an inherently limited return type
7248 -- must be handled using a build-in-place profile, including the case
7249 -- of a function with a limited interface result, where the function
7250 -- may return objects of nonlimited descendants.
7252 if Is_Limited_View (Typ) then
7253 return Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L;
7255 else
7256 if Debug_Flag_Dot_9 then
7257 return False;
7258 end if;
7260 if Has_Interfaces (Typ) then
7261 return False;
7262 end if;
7264 declare
7265 T : Entity_Id := Typ;
7266 begin
7267 -- For T'Class, return True if it's True for T. This is necessary
7268 -- because a class-wide function might say "return F (...)", where
7269 -- F returns the corresponding specific type. We need a loop in
7270 -- case T is a subtype of a class-wide type.
7272 while Is_Class_Wide_Type (T) loop
7273 T := Etype (T);
7274 end loop;
7276 -- If this is a generic formal type in an instance, return True if
7277 -- it's True for the generic actual type.
7279 if Nkind (Parent (T)) = N_Subtype_Declaration
7280 and then Present (Generic_Parent_Type (Parent (T)))
7281 then
7282 T := Entity (Subtype_Indication (Parent (T)));
7284 if Present (Full_View (T)) then
7285 T := Full_View (T);
7286 end if;
7287 end if;
7289 if Present (Underlying_Type (T)) then
7290 T := Underlying_Type (T);
7291 end if;
7293 declare
7294 Result : Boolean;
7295 -- So we can stop here in the debugger
7296 begin
7297 -- ???For now, enable build-in-place for a very narrow set of
7298 -- controlled types. Change "if True" to "if False" to
7299 -- experiment more controlled types. Eventually, we would
7300 -- like to enable build-in-place for all tagged types, all
7301 -- types that need finalization, and all caller-unknown-size
7302 -- types.
7304 if True then
7305 Result := Is_Controlled (T)
7306 and then Present (Enclosing_Subprogram (T))
7307 and then not Is_Compilation_Unit (Enclosing_Subprogram (T))
7308 and then Ekind (Enclosing_Subprogram (T)) = E_Procedure;
7309 else
7310 Result := Is_Controlled (T);
7311 end if;
7313 return Result;
7314 end;
7315 end;
7316 end if;
7317 end Is_Build_In_Place_Result_Type;
7319 --------------------------------
7320 -- Is_Build_In_Place_Function --
7321 --------------------------------
7323 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7324 begin
7325 -- This function is called from Expand_Subtype_From_Expr during
7326 -- semantic analysis, even when expansion is off. In those cases
7327 -- the build_in_place expansion will not take place.
7329 if not Expander_Active then
7330 return False;
7331 end if;
7333 -- For now we test whether E denotes a function or access-to-function
7334 -- type whose result subtype is inherently limited. Later this test
7335 -- may be revised to allow composite nonlimited types. Functions with
7336 -- a foreign convention or whose result type has a foreign convention
7337 -- never qualify.
7339 if Ekind_In (E, E_Function, E_Generic_Function)
7340 or else (Ekind (E) = E_Subprogram_Type
7341 and then Etype (E) /= Standard_Void_Type)
7342 then
7343 -- Note: If the function has a foreign convention, it cannot build
7344 -- its result in place, so you're on your own. On the other hand,
7345 -- if only the return type has a foreign convention, its layout is
7346 -- intended to be compatible with the other language, but the build-
7347 -- in place machinery can ensure that the object is not copied.
7349 return Is_Build_In_Place_Result_Type (Etype (E))
7350 and then not Has_Foreign_Convention (E)
7351 and then not Debug_Flag_Dot_L;
7353 else
7354 return False;
7355 end if;
7356 end Is_Build_In_Place_Function;
7358 -------------------------------------
7359 -- Is_Build_In_Place_Function_Call --
7360 -------------------------------------
7362 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7363 Exp_Node : constant Node_Id := Unqual_Conv (N);
7364 Function_Id : Entity_Id;
7366 begin
7367 -- Return False if the expander is currently inactive, since awareness
7368 -- of build-in-place treatment is only relevant during expansion. Note
7369 -- that Is_Build_In_Place_Function, which is called as part of this
7370 -- function, is also conditioned this way, but we need to check here as
7371 -- well to avoid blowing up on processing protected calls when expansion
7372 -- is disabled (such as with -gnatc) since those would trip over the
7373 -- raise of Program_Error below.
7375 -- In SPARK mode, build-in-place calls are not expanded, so that we
7376 -- may end up with a call that is neither resolved to an entity, nor
7377 -- an indirect call.
7379 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7380 return False;
7381 end if;
7383 if Is_Entity_Name (Name (Exp_Node)) then
7384 Function_Id := Entity (Name (Exp_Node));
7386 -- In the case of an explicitly dereferenced call, use the subprogram
7387 -- type generated for the dereference.
7389 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7390 Function_Id := Etype (Name (Exp_Node));
7392 -- This may be a call to a protected function.
7394 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7395 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7397 else
7398 raise Program_Error;
7399 end if;
7401 declare
7402 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
7403 -- So we can stop here in the debugger
7404 begin
7405 return Result;
7406 end;
7407 end Is_Build_In_Place_Function_Call;
7409 -----------------------
7410 -- Freeze_Subprogram --
7411 -----------------------
7413 procedure Freeze_Subprogram (N : Node_Id) is
7414 Loc : constant Source_Ptr := Sloc (N);
7416 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7417 -- (Ada 2005): Register a predefined primitive in all the secondary
7418 -- dispatch tables of its primitive type.
7420 ----------------------------------
7421 -- Register_Predefined_DT_Entry --
7422 ----------------------------------
7424 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7425 Iface_DT_Ptr : Elmt_Id;
7426 Tagged_Typ : Entity_Id;
7427 Thunk_Id : Entity_Id;
7428 Thunk_Code : Node_Id;
7430 begin
7431 Tagged_Typ := Find_Dispatching_Type (Prim);
7433 if No (Access_Disp_Table (Tagged_Typ))
7434 or else not Has_Interfaces (Tagged_Typ)
7435 or else not RTE_Available (RE_Interface_Tag)
7436 or else Restriction_Active (No_Dispatching_Calls)
7437 then
7438 return;
7439 end if;
7441 -- Skip the first two access-to-dispatch-table pointers since they
7442 -- leads to the primary dispatch table (predefined DT and user
7443 -- defined DT). We are only concerned with the secondary dispatch
7444 -- table pointers. Note that the access-to- dispatch-table pointer
7445 -- corresponds to the first implemented interface retrieved below.
7447 Iface_DT_Ptr :=
7448 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7450 while Present (Iface_DT_Ptr)
7451 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7452 loop
7453 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7454 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7456 if Present (Thunk_Code) then
7457 Insert_Actions_After (N, New_List (
7458 Thunk_Code,
7460 Build_Set_Predefined_Prim_Op_Address (Loc,
7461 Tag_Node =>
7462 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7463 Position => DT_Position (Prim),
7464 Address_Node =>
7465 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7466 Make_Attribute_Reference (Loc,
7467 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7468 Attribute_Name => Name_Unrestricted_Access))),
7470 Build_Set_Predefined_Prim_Op_Address (Loc,
7471 Tag_Node =>
7472 New_Occurrence_Of
7473 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7474 Loc),
7475 Position => DT_Position (Prim),
7476 Address_Node =>
7477 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7478 Make_Attribute_Reference (Loc,
7479 Prefix => New_Occurrence_Of (Prim, Loc),
7480 Attribute_Name => Name_Unrestricted_Access)))));
7481 end if;
7483 -- Skip the tag of the predefined primitives dispatch table
7485 Next_Elmt (Iface_DT_Ptr);
7486 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7488 -- Skip tag of the no-thunks dispatch table
7490 Next_Elmt (Iface_DT_Ptr);
7491 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7493 -- Skip tag of predefined primitives no-thunks dispatch table
7495 Next_Elmt (Iface_DT_Ptr);
7496 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7498 Next_Elmt (Iface_DT_Ptr);
7499 end loop;
7500 end Register_Predefined_DT_Entry;
7502 -- Local variables
7504 Subp : constant Entity_Id := Entity (N);
7506 -- Start of processing for Freeze_Subprogram
7508 begin
7509 -- We suppress the initialization of the dispatch table entry when
7510 -- not Tagged_Type_Expansion because the dispatching mechanism is
7511 -- handled internally by the target.
7513 if Is_Dispatching_Operation (Subp)
7514 and then not Is_Abstract_Subprogram (Subp)
7515 and then Present (DTC_Entity (Subp))
7516 and then Present (Scope (DTC_Entity (Subp)))
7517 and then Tagged_Type_Expansion
7518 and then not Restriction_Active (No_Dispatching_Calls)
7519 and then RTE_Available (RE_Tag)
7520 then
7521 declare
7522 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7524 begin
7525 -- Handle private overridden primitives
7527 if not Is_CPP_Class (Typ) then
7528 Check_Overriding_Operation (Subp);
7529 end if;
7531 -- We assume that imported CPP primitives correspond with objects
7532 -- whose constructor is in the CPP side; therefore we don't need
7533 -- to generate code to register them in the dispatch table.
7535 if Is_CPP_Class (Typ) then
7536 null;
7538 -- Handle CPP primitives found in derivations of CPP_Class types.
7539 -- These primitives must have been inherited from some parent, and
7540 -- there is no need to register them in the dispatch table because
7541 -- Build_Inherit_Prims takes care of initializing these slots.
7543 elsif Is_Imported (Subp)
7544 and then (Convention (Subp) = Convention_CPP
7545 or else Convention (Subp) = Convention_C)
7546 then
7547 null;
7549 -- Generate code to register the primitive in non statically
7550 -- allocated dispatch tables
7552 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7554 -- When a primitive is frozen, enter its name in its dispatch
7555 -- table slot.
7557 if not Is_Interface (Typ)
7558 or else Present (Interface_Alias (Subp))
7559 then
7560 if Is_Predefined_Dispatching_Operation (Subp) then
7561 Register_Predefined_DT_Entry (Subp);
7562 end if;
7564 Insert_Actions_After (N,
7565 Register_Primitive (Loc, Prim => Subp));
7566 end if;
7567 end if;
7568 end;
7569 end if;
7571 -- Mark functions that return by reference. Note that it cannot be part
7572 -- of the normal semantic analysis of the spec since the underlying
7573 -- returned type may not be known yet (for private types).
7575 declare
7576 Typ : constant Entity_Id := Etype (Subp);
7577 Utyp : constant Entity_Id := Underlying_Type (Typ);
7579 begin
7580 if Is_Limited_View (Typ) then
7581 Set_Returns_By_Ref (Subp);
7583 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7584 Set_Returns_By_Ref (Subp);
7585 end if;
7586 end;
7588 -- Wnen freezing a null procedure, analyze its delayed aspects now
7589 -- because we may not have reached the end of the declarative list when
7590 -- delayed aspects are normally analyzed. This ensures that dispatching
7591 -- calls are properly rewritten when the generated _Postcondition
7592 -- procedure is analyzed in the null procedure body.
7594 if Nkind (Parent (Subp)) = N_Procedure_Specification
7595 and then Null_Present (Parent (Subp))
7596 then
7597 Analyze_Entry_Or_Subprogram_Contract (Subp);
7598 end if;
7599 end Freeze_Subprogram;
7601 ------------------------------
7602 -- Insert_Post_Call_Actions --
7603 ------------------------------
7605 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7606 Context : constant Node_Id := Parent (N);
7608 begin
7609 if Is_Empty_List (Post_Call) then
7610 return;
7611 end if;
7613 -- Cases where the call is not a member of a statement list. This
7614 -- includes the case where the call is an actual in another function
7615 -- call or indexing, i.e. an expression context as well.
7617 if not Is_List_Member (N)
7618 or else Nkind_In (Context, N_Function_Call, N_Indexed_Component)
7619 then
7620 -- In Ada 2012 the call may be a function call in an expression
7621 -- (since OUT and IN OUT parameters are now allowed for such calls).
7622 -- The write-back of (in)-out parameters is handled by the back-end,
7623 -- but the constraint checks generated when subtypes of formal and
7624 -- actual don't match must be inserted in the form of assignments.
7626 if Nkind (Original_Node (N)) = N_Function_Call then
7627 pragma Assert (Ada_Version >= Ada_2012);
7628 -- Functions with '[in] out' parameters are only allowed in Ada
7629 -- 2012.
7631 -- We used to handle this by climbing up parents to a
7632 -- non-statement/declaration and then simply making a call to
7633 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7634 -- for Ada 2012. If we are in the middle of an expression, e.g.
7635 -- the condition of an IF, this call would insert after the IF
7636 -- statement, which is much too late to be doing the write back.
7637 -- For example:
7639 -- if Clobber (X) then
7640 -- Put_Line (X'Img);
7641 -- else
7642 -- goto Junk
7643 -- end if;
7645 -- Now assume Clobber changes X, if we put the write back after
7646 -- the IF, the Put_Line gets the wrong value and the goto causes
7647 -- the write back to be skipped completely.
7649 -- To deal with this, we replace the call by
7651 -- do
7652 -- Tnnn : constant function-result-type := function-call;
7653 -- Post_Call actions
7654 -- in
7655 -- Tnnn;
7656 -- end;
7658 declare
7659 Loc : constant Source_Ptr := Sloc (N);
7660 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7661 FRTyp : constant Entity_Id := Etype (N);
7662 Name : constant Node_Id := Relocate_Node (N);
7664 begin
7665 Prepend_To (Post_Call,
7666 Make_Object_Declaration (Loc,
7667 Defining_Identifier => Tnnn,
7668 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7669 Constant_Present => True,
7670 Expression => Name));
7672 Rewrite (N,
7673 Make_Expression_With_Actions (Loc,
7674 Actions => Post_Call,
7675 Expression => New_Occurrence_Of (Tnnn, Loc)));
7677 -- We don't want to just blindly call Analyze_And_Resolve
7678 -- because that would cause unwanted recursion on the call.
7679 -- So for a moment set the call as analyzed to prevent that
7680 -- recursion, and get the rest analyzed properly, then reset
7681 -- the analyzed flag, so our caller can continue.
7683 Set_Analyzed (Name, True);
7684 Analyze_And_Resolve (N, FRTyp);
7685 Set_Analyzed (Name, False);
7686 end;
7688 -- If not the special Ada 2012 case of a function call, then we must
7689 -- have the triggering statement of a triggering alternative or an
7690 -- entry call alternative, and we can add the post call stuff to the
7691 -- corresponding statement list.
7693 else
7694 pragma Assert (Nkind_In (Context, N_Entry_Call_Alternative,
7695 N_Triggering_Alternative));
7697 if Is_Non_Empty_List (Statements (Context)) then
7698 Insert_List_Before_And_Analyze
7699 (First (Statements (Context)), Post_Call);
7700 else
7701 Set_Statements (Context, Post_Call);
7702 end if;
7703 end if;
7705 -- A procedure call is always part of a declarative or statement list,
7706 -- however a function call may appear nested within a construct. Most
7707 -- cases of function call nesting are handled in the special case above.
7708 -- The only exception is when the function call acts as an actual in a
7709 -- procedure call. In this case the function call is in a list, but the
7710 -- post-call actions must be inserted after the procedure call.
7712 elsif Nkind (Context) = N_Procedure_Call_Statement then
7713 Insert_Actions_After (Context, Post_Call);
7715 -- Otherwise, normal case where N is in a statement sequence, just put
7716 -- the post-call stuff after the call statement.
7718 else
7719 Insert_Actions_After (N, Post_Call);
7720 end if;
7721 end Insert_Post_Call_Actions;
7723 -----------------------
7724 -- Is_Null_Procedure --
7725 -----------------------
7727 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7728 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7730 begin
7731 if Ekind (Subp) /= E_Procedure then
7732 return False;
7734 -- Check if this is a declared null procedure
7736 elsif Nkind (Decl) = N_Subprogram_Declaration then
7737 if not Null_Present (Specification (Decl)) then
7738 return False;
7740 elsif No (Body_To_Inline (Decl)) then
7741 return False;
7743 -- Check if the body contains only a null statement, followed by
7744 -- the return statement added during expansion.
7746 else
7747 declare
7748 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7750 Stat : Node_Id;
7751 Stat2 : Node_Id;
7753 begin
7754 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7755 return False;
7756 else
7757 -- We must skip SCIL nodes because they are currently
7758 -- implemented as special N_Null_Statement nodes.
7760 Stat :=
7761 First_Non_SCIL_Node
7762 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7763 Stat2 := Next_Non_SCIL_Node (Stat);
7765 return
7766 Is_Empty_List (Declarations (Orig_Bod))
7767 and then Nkind (Stat) = N_Null_Statement
7768 and then
7769 (No (Stat2)
7770 or else
7771 (Nkind (Stat2) = N_Simple_Return_Statement
7772 and then No (Next (Stat2))));
7773 end if;
7774 end;
7775 end if;
7777 else
7778 return False;
7779 end if;
7780 end Is_Null_Procedure;
7782 -------------------------------------------
7783 -- Make_Build_In_Place_Call_In_Allocator --
7784 -------------------------------------------
7786 procedure Make_Build_In_Place_Call_In_Allocator
7787 (Allocator : Node_Id;
7788 Function_Call : Node_Id)
7790 Acc_Type : constant Entity_Id := Etype (Allocator);
7791 Loc : constant Source_Ptr := Sloc (Function_Call);
7792 Func_Call : Node_Id := Function_Call;
7793 Ref_Func_Call : Node_Id;
7794 Function_Id : Entity_Id;
7795 Result_Subt : Entity_Id;
7796 New_Allocator : Node_Id;
7797 Return_Obj_Access : Entity_Id; -- temp for function result
7798 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7799 Alloc_Form : BIP_Allocation_Form;
7800 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7801 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7802 Chain : Entity_Id; -- activation chain, in case of tasks
7804 begin
7805 -- Step past qualification or unchecked conversion (the latter can occur
7806 -- in cases of calls to 'Input).
7808 if Nkind_In (Func_Call,
7809 N_Qualified_Expression,
7810 N_Type_Conversion,
7811 N_Unchecked_Type_Conversion)
7812 then
7813 Func_Call := Expression (Func_Call);
7814 end if;
7816 -- Mark the call as processed as a build-in-place call
7818 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
7819 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7821 if Is_Entity_Name (Name (Func_Call)) then
7822 Function_Id := Entity (Name (Func_Call));
7824 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7825 Function_Id := Etype (Name (Func_Call));
7827 else
7828 raise Program_Error;
7829 end if;
7831 Result_Subt := Available_View (Etype (Function_Id));
7833 -- Create a temp for the function result. In the caller-allocates case,
7834 -- this will be initialized to the result of a new uninitialized
7835 -- allocator. Note: we do not use Allocator as the Related_Node of
7836 -- Return_Obj_Access in call to Make_Temporary below as this would
7837 -- create a sort of infinite "recursion".
7839 Return_Obj_Access := Make_Temporary (Loc, 'R');
7840 Set_Etype (Return_Obj_Access, Acc_Type);
7841 Set_Can_Never_Be_Null (Acc_Type, False);
7842 -- It gets initialized to null, so we can't have that
7844 -- When the result subtype is constrained, the return object is
7845 -- allocated on the caller side, and access to it is passed to the
7846 -- function.
7848 -- Here and in related routines, we must examine the full view of the
7849 -- type, because the view at the point of call may differ from that
7850 -- that in the function body, and the expansion mechanism depends on
7851 -- the characteristics of the full view.
7853 if Is_Constrained (Underlying_Type (Result_Subt)) then
7854 -- Replace the initialized allocator of form "new T'(Func (...))"
7855 -- with an uninitialized allocator of form "new T", where T is the
7856 -- result subtype of the called function. The call to the function
7857 -- is handled separately further below.
7859 New_Allocator :=
7860 Make_Allocator (Loc,
7861 Expression => New_Occurrence_Of (Result_Subt, Loc));
7862 Set_No_Initialization (New_Allocator);
7864 -- Copy attributes to new allocator. Note that the new allocator
7865 -- logically comes from source if the original one did, so copy the
7866 -- relevant flag. This ensures proper treatment of the restriction
7867 -- No_Implicit_Heap_Allocations in this case.
7869 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7870 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7871 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7873 Rewrite (Allocator, New_Allocator);
7875 -- Initial value of the temp is the result of the uninitialized
7876 -- allocator. Unchecked_Convert is needed for T'Input where T is
7877 -- derived from a controlled type.
7879 Temp_Init := Relocate_Node (Allocator);
7881 if Nkind_In
7882 (Function_Call, N_Type_Conversion, N_Unchecked_Type_Conversion)
7883 then
7884 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
7885 end if;
7887 -- Indicate that caller allocates, and pass in the return object
7889 Alloc_Form := Caller_Allocation;
7890 Pool := Make_Null (No_Location);
7891 Return_Obj_Actual :=
7892 Make_Unchecked_Type_Conversion (Loc,
7893 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7894 Expression =>
7895 Make_Explicit_Dereference (Loc,
7896 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7898 -- When the result subtype is unconstrained, the function itself must
7899 -- perform the allocation of the return object, so we pass parameters
7900 -- indicating that.
7902 else
7903 Temp_Init := Empty;
7905 -- Case of a user-defined storage pool. Pass an allocation parameter
7906 -- indicating that the function should allocate its result in the
7907 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7908 -- pool may not be aliased.
7910 if Present (Associated_Storage_Pool (Acc_Type)) then
7911 Alloc_Form := User_Storage_Pool;
7912 Pool :=
7913 Make_Attribute_Reference (Loc,
7914 Prefix =>
7915 New_Occurrence_Of
7916 (Associated_Storage_Pool (Acc_Type), Loc),
7917 Attribute_Name => Name_Unrestricted_Access);
7919 -- No user-defined pool; pass an allocation parameter indicating that
7920 -- the function should allocate its result on the heap.
7922 else
7923 Alloc_Form := Global_Heap;
7924 Pool := Make_Null (No_Location);
7925 end if;
7927 -- The caller does not provide the return object in this case, so we
7928 -- have to pass null for the object access actual.
7930 Return_Obj_Actual := Empty;
7931 end if;
7933 -- Declare the temp object
7935 Insert_Action (Allocator,
7936 Make_Object_Declaration (Loc,
7937 Defining_Identifier => Return_Obj_Access,
7938 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7939 Expression => Temp_Init));
7941 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7943 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7944 -- then generate an implicit conversion to force displacement of the
7945 -- "this" pointer.
7947 if Is_Interface (Designated_Type (Acc_Type)) then
7948 Rewrite
7949 (Ref_Func_Call,
7950 OK_Convert_To (Acc_Type, Ref_Func_Call));
7952 -- If the types are incompatible, we need an unchecked conversion. Note
7953 -- that the full types will be compatible, but the types not visibly
7954 -- compatible.
7956 elsif Nkind_In
7957 (Function_Call, N_Type_Conversion, N_Unchecked_Type_Conversion)
7958 then
7959 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
7960 end if;
7962 declare
7963 Assign : constant Node_Id :=
7964 Make_Assignment_Statement (Loc,
7965 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7966 Expression => Ref_Func_Call);
7967 -- Assign the result of the function call into the temp. In the
7968 -- caller-allocates case, this is overwriting the temp with its
7969 -- initial value, which has no effect. In the callee-allocates case,
7970 -- this is setting the temp to point to the object allocated by the
7971 -- callee. Unchecked_Convert is needed for T'Input where T is derived
7972 -- from a controlled type.
7974 Actions : List_Id;
7975 -- Actions to be inserted. If there are no tasks, this is just the
7976 -- assignment statement. If the allocated object has tasks, we need
7977 -- to wrap the assignment in a block that activates them. The
7978 -- activation chain of that block must be passed to the function,
7979 -- rather than some outer chain.
7980 begin
7981 if Has_Task (Result_Subt) then
7982 Actions := New_List;
7983 Build_Task_Allocate_Block_With_Init_Stmts
7984 (Actions, Allocator, Init_Stmts => New_List (Assign));
7985 Chain := Activation_Chain_Entity (Last (Actions));
7986 else
7987 Actions := New_List (Assign);
7988 Chain := Empty;
7989 end if;
7991 Insert_Actions (Allocator, Actions);
7992 end;
7994 -- When the function has a controlling result, an allocation-form
7995 -- parameter must be passed indicating that the caller is allocating
7996 -- the result object. This is needed because such a function can be
7997 -- called as a dispatching operation and must be treated similarly
7998 -- to functions with unconstrained result subtypes.
8000 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8001 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8003 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8004 (Func_Call, Function_Id, Acc_Type);
8006 Add_Task_Actuals_To_Build_In_Place_Call
8007 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8008 Chain => Chain);
8010 -- Add an implicit actual to the function call that provides access
8011 -- to the allocated object. An unchecked conversion to the (specific)
8012 -- result subtype of the function is inserted to handle cases where
8013 -- the access type of the allocator has a class-wide designated type.
8015 Add_Access_Actual_To_Build_In_Place_Call
8016 (Func_Call, Function_Id, Return_Obj_Actual);
8018 -- Finally, replace the allocator node with a reference to the temp
8020 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8022 Analyze_And_Resolve (Allocator, Acc_Type);
8023 end Make_Build_In_Place_Call_In_Allocator;
8025 ---------------------------------------------------
8026 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8027 ---------------------------------------------------
8029 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8030 (Function_Call : Node_Id)
8032 Loc : constant Source_Ptr := Sloc (Function_Call);
8033 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8034 Function_Id : Entity_Id;
8035 Result_Subt : Entity_Id;
8036 Return_Obj_Id : Entity_Id;
8037 Return_Obj_Decl : Entity_Id;
8039 begin
8040 -- If the call has already been processed to add build-in-place actuals
8041 -- then return. One place this can occur is for calls to build-in-place
8042 -- functions that occur within a call to a protected operation, where
8043 -- due to rewriting and expansion of the protected call there can be
8044 -- more than one call to Expand_Actuals for the same set of actuals.
8046 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8047 return;
8048 end if;
8050 -- Mark the call as processed as a build-in-place call
8052 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8054 if Is_Entity_Name (Name (Func_Call)) then
8055 Function_Id := Entity (Name (Func_Call));
8057 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8058 Function_Id := Etype (Name (Func_Call));
8060 else
8061 raise Program_Error;
8062 end if;
8064 Result_Subt := Etype (Function_Id);
8066 -- If the build-in-place function returns a controlled object, then the
8067 -- object needs to be finalized immediately after the context. Since
8068 -- this case produces a transient scope, the servicing finalizer needs
8069 -- to name the returned object. Create a temporary which is initialized
8070 -- with the function call:
8072 -- Temp_Id : Func_Type := BIP_Func_Call;
8074 -- The initialization expression of the temporary will be rewritten by
8075 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8076 -- Call_In_Object_Declaration.
8078 if Needs_Finalization (Result_Subt) then
8079 declare
8080 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8081 Temp_Decl : Node_Id;
8083 begin
8084 -- Reset the guard on the function call since the following does
8085 -- not perform actual call expansion.
8087 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8089 Temp_Decl :=
8090 Make_Object_Declaration (Loc,
8091 Defining_Identifier => Temp_Id,
8092 Object_Definition =>
8093 New_Occurrence_Of (Result_Subt, Loc),
8094 Expression =>
8095 New_Copy_Tree (Function_Call));
8097 Insert_Action (Function_Call, Temp_Decl);
8099 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8100 Analyze (Function_Call);
8101 end;
8103 -- When the result subtype is definite, an object of the subtype is
8104 -- declared and an access value designating it is passed as an actual.
8106 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8108 -- Create a temporary object to hold the function result
8110 Return_Obj_Id := Make_Temporary (Loc, 'R');
8111 Set_Etype (Return_Obj_Id, Result_Subt);
8113 Return_Obj_Decl :=
8114 Make_Object_Declaration (Loc,
8115 Defining_Identifier => Return_Obj_Id,
8116 Aliased_Present => True,
8117 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8119 Set_No_Initialization (Return_Obj_Decl);
8121 Insert_Action (Func_Call, Return_Obj_Decl);
8123 -- When the function has a controlling result, an allocation-form
8124 -- parameter must be passed indicating that the caller is allocating
8125 -- the result object. This is needed because such a function can be
8126 -- called as a dispatching operation and must be treated similarly
8127 -- to functions with unconstrained result subtypes.
8129 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8130 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8132 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8133 (Func_Call, Function_Id);
8135 Add_Task_Actuals_To_Build_In_Place_Call
8136 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8138 -- Add an implicit actual to the function call that provides access
8139 -- to the caller's return object.
8141 Add_Access_Actual_To_Build_In_Place_Call
8142 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8144 -- When the result subtype is unconstrained, the function must allocate
8145 -- the return object in the secondary stack, so appropriate implicit
8146 -- parameters are added to the call to indicate that. A transient
8147 -- scope is established to ensure eventual cleanup of the result.
8149 else
8150 -- Pass an allocation parameter indicating that the function should
8151 -- allocate its result on the secondary stack.
8153 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8154 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8156 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8157 (Func_Call, Function_Id);
8159 Add_Task_Actuals_To_Build_In_Place_Call
8160 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8162 -- Pass a null value to the function since no return object is
8163 -- available on the caller side.
8165 Add_Access_Actual_To_Build_In_Place_Call
8166 (Func_Call, Function_Id, Empty);
8167 end if;
8168 end Make_Build_In_Place_Call_In_Anonymous_Context;
8170 --------------------------------------------
8171 -- Make_Build_In_Place_Call_In_Assignment --
8172 --------------------------------------------
8174 procedure Make_Build_In_Place_Call_In_Assignment
8175 (Assign : Node_Id;
8176 Function_Call : Node_Id)
8178 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8179 Lhs : constant Node_Id := Name (Assign);
8180 Loc : constant Source_Ptr := Sloc (Function_Call);
8181 Func_Id : Entity_Id;
8182 Obj_Decl : Node_Id;
8183 Obj_Id : Entity_Id;
8184 Ptr_Typ : Entity_Id;
8185 Ptr_Typ_Decl : Node_Id;
8186 New_Expr : Node_Id;
8187 Result_Subt : Entity_Id;
8189 begin
8190 -- Mark the call as processed as a build-in-place call
8192 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8193 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8195 if Is_Entity_Name (Name (Func_Call)) then
8196 Func_Id := Entity (Name (Func_Call));
8198 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8199 Func_Id := Etype (Name (Func_Call));
8201 else
8202 raise Program_Error;
8203 end if;
8205 Result_Subt := Etype (Func_Id);
8207 -- When the result subtype is unconstrained, an additional actual must
8208 -- be passed to indicate that the caller is providing the return object.
8209 -- This parameter must also be passed when the called function has a
8210 -- controlling result, because dispatching calls to the function needs
8211 -- to be treated effectively the same as calls to class-wide functions.
8213 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8214 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8216 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8217 (Func_Call, Func_Id);
8219 Add_Task_Actuals_To_Build_In_Place_Call
8220 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8222 -- Add an implicit actual to the function call that provides access to
8223 -- the caller's return object.
8225 Add_Access_Actual_To_Build_In_Place_Call
8226 (Func_Call,
8227 Func_Id,
8228 Make_Unchecked_Type_Conversion (Loc,
8229 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8230 Expression => Relocate_Node (Lhs)));
8232 -- Create an access type designating the function's result subtype
8234 Ptr_Typ := Make_Temporary (Loc, 'A');
8236 Ptr_Typ_Decl :=
8237 Make_Full_Type_Declaration (Loc,
8238 Defining_Identifier => Ptr_Typ,
8239 Type_Definition =>
8240 Make_Access_To_Object_Definition (Loc,
8241 All_Present => True,
8242 Subtype_Indication =>
8243 New_Occurrence_Of (Result_Subt, Loc)));
8244 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8246 -- Finally, create an access object initialized to a reference to the
8247 -- function call. We know this access value is non-null, so mark the
8248 -- entity accordingly to suppress junk access checks.
8250 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8252 -- Add a conversion if it's the wrong type
8254 if Etype (New_Expr) /= Ptr_Typ then
8255 New_Expr :=
8256 Make_Unchecked_Type_Conversion (Loc,
8257 New_Occurrence_Of (Ptr_Typ, Loc), New_Expr);
8258 end if;
8260 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8261 Set_Etype (Obj_Id, Ptr_Typ);
8262 Set_Is_Known_Non_Null (Obj_Id);
8264 Obj_Decl :=
8265 Make_Object_Declaration (Loc,
8266 Defining_Identifier => Obj_Id,
8267 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8268 Expression => New_Expr);
8269 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8271 Rewrite (Assign, Make_Null_Statement (Loc));
8272 end Make_Build_In_Place_Call_In_Assignment;
8274 ----------------------------------------------------
8275 -- Make_Build_In_Place_Call_In_Object_Declaration --
8276 ----------------------------------------------------
8278 procedure Make_Build_In_Place_Call_In_Object_Declaration
8279 (Obj_Decl : Node_Id;
8280 Function_Call : Node_Id)
8282 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8283 -- Get the value of Function_Id, below
8285 ---------------------
8286 -- Get_Function_Id --
8287 ---------------------
8289 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8290 begin
8291 if Is_Entity_Name (Name (Func_Call)) then
8292 return Entity (Name (Func_Call));
8294 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8295 return Etype (Name (Func_Call));
8297 else
8298 raise Program_Error;
8299 end if;
8300 end Get_Function_Id;
8302 -- Local variables
8304 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8305 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8306 Loc : constant Source_Ptr := Sloc (Function_Call);
8307 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8308 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8309 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8310 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8311 Result_Subt : constant Entity_Id := Etype (Function_Id);
8313 Call_Deref : Node_Id;
8314 Caller_Object : Node_Id;
8315 Def_Id : Entity_Id;
8316 Designated_Type : Entity_Id;
8317 Fmaster_Actual : Node_Id := Empty;
8318 Pool_Actual : Node_Id;
8319 Ptr_Typ : Entity_Id;
8320 Ptr_Typ_Decl : Node_Id;
8321 Pass_Caller_Acc : Boolean := False;
8322 Res_Decl : Node_Id;
8324 Definite : constant Boolean :=
8325 Caller_Known_Size (Func_Call, Result_Subt)
8326 and then not Is_Class_Wide_Type (Obj_Typ);
8327 -- In the case of "X : T'Class := F(...);", where F returns a
8328 -- Caller_Known_Size (specific) tagged type, we treat it as
8329 -- indefinite, because the code for the Definite case below sets the
8330 -- initialization expression of the object to Empty, which would be
8331 -- illegal Ada, and would cause gigi to misallocate X.
8333 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8335 begin
8336 -- If the call has already been processed to add build-in-place actuals
8337 -- then return.
8339 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8340 return;
8341 end if;
8343 -- Mark the call as processed as a build-in-place call
8345 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8347 -- Create an access type designating the function's result subtype.
8348 -- We use the type of the original call because it may be a call to an
8349 -- inherited operation, which the expansion has replaced with the parent
8350 -- operation that yields the parent type. Note that this access type
8351 -- must be declared before we establish a transient scope, so that it
8352 -- receives the proper accessibility level.
8354 if Is_Class_Wide_Type (Obj_Typ)
8355 and then not Is_Interface (Obj_Typ)
8356 and then not Is_Class_Wide_Type (Etype (Function_Call))
8357 then
8358 Designated_Type := Obj_Typ;
8359 else
8360 Designated_Type := Etype (Function_Call);
8361 end if;
8363 Ptr_Typ := Make_Temporary (Loc, 'A');
8364 Ptr_Typ_Decl :=
8365 Make_Full_Type_Declaration (Loc,
8366 Defining_Identifier => Ptr_Typ,
8367 Type_Definition =>
8368 Make_Access_To_Object_Definition (Loc,
8369 All_Present => True,
8370 Subtype_Indication =>
8371 New_Occurrence_Of (Designated_Type, Loc)));
8373 -- The access type and its accompanying object must be inserted after
8374 -- the object declaration in the constrained case, so that the function
8375 -- call can be passed access to the object. In the indefinite case, or
8376 -- if the object declaration is for a return object, the access type and
8377 -- object must be inserted before the object, since the object
8378 -- declaration is rewritten to be a renaming of a dereference of the
8379 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8380 -- the result object is in a different (transient) scope, so won't cause
8381 -- freezing.
8383 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8384 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8385 else
8386 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8387 end if;
8389 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8390 -- elaborated in an inner (transient) scope and thus won't cause
8391 -- freezing by itself. It's not an itype, but it needs to be frozen
8392 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8394 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
8396 -- If the object is a return object of an enclosing build-in-place
8397 -- function, then the implicit build-in-place parameters of the
8398 -- enclosing function are simply passed along to the called function.
8399 -- (Unfortunately, this won't cover the case of extension aggregates
8400 -- where the ancestor part is a build-in-place indefinite function
8401 -- call that should be passed along the caller's parameters.
8402 -- Currently those get mishandled by reassigning the result of the
8403 -- call to the aggregate return object, when the call result should
8404 -- really be directly built in place in the aggregate and not in a
8405 -- temporary. ???)
8407 if Is_Return_Object (Obj_Def_Id) then
8408 Pass_Caller_Acc := True;
8410 -- When the enclosing function has a BIP_Alloc_Form formal then we
8411 -- pass it along to the callee (such as when the enclosing function
8412 -- has an unconstrained or tagged result type).
8414 if Needs_BIP_Alloc_Form (Encl_Func) then
8415 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8416 Pool_Actual :=
8417 New_Occurrence_Of
8418 (Build_In_Place_Formal
8419 (Encl_Func, BIP_Storage_Pool), Loc);
8421 -- The build-in-place pool formal is not built on e.g. ZFP
8423 else
8424 Pool_Actual := Empty;
8425 end if;
8427 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8428 (Function_Call => Func_Call,
8429 Function_Id => Function_Id,
8430 Alloc_Form_Exp =>
8431 New_Occurrence_Of
8432 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8433 Pool_Actual => Pool_Actual);
8435 -- Otherwise, if enclosing function has a definite result subtype,
8436 -- then caller allocation will be used.
8438 else
8439 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8440 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8441 end if;
8443 if Needs_BIP_Finalization_Master (Encl_Func) then
8444 Fmaster_Actual :=
8445 New_Occurrence_Of
8446 (Build_In_Place_Formal
8447 (Encl_Func, BIP_Finalization_Master), Loc);
8448 end if;
8450 -- Retrieve the BIPacc formal from the enclosing function and convert
8451 -- it to the access type of the callee's BIP_Object_Access formal.
8453 Caller_Object :=
8454 Make_Unchecked_Type_Conversion (Loc,
8455 Subtype_Mark =>
8456 New_Occurrence_Of
8457 (Etype (Build_In_Place_Formal
8458 (Function_Id, BIP_Object_Access)),
8459 Loc),
8460 Expression =>
8461 New_Occurrence_Of
8462 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8463 Loc));
8465 -- In the definite case, add an implicit actual to the function call
8466 -- that provides access to the declared object. An unchecked conversion
8467 -- to the (specific) result type of the function is inserted to handle
8468 -- the case where the object is declared with a class-wide type.
8470 elsif Definite then
8471 Caller_Object :=
8472 Make_Unchecked_Type_Conversion (Loc,
8473 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8474 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8476 -- When the function has a controlling result, an allocation-form
8477 -- parameter must be passed indicating that the caller is allocating
8478 -- the result object. This is needed because such a function can be
8479 -- called as a dispatching operation and must be treated similarly to
8480 -- functions with indefinite result subtypes.
8482 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8483 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8485 -- The allocation for indefinite library-level objects occurs on the
8486 -- heap as opposed to the secondary stack. This accommodates DLLs where
8487 -- the secondary stack is destroyed after each library unload. This is a
8488 -- hybrid mechanism where a stack-allocated object lives on the heap.
8490 elsif Is_Library_Level_Entity (Obj_Def_Id)
8491 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8492 then
8493 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8494 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8495 Caller_Object := Empty;
8497 -- Create a finalization master for the access result type to ensure
8498 -- that the heap allocation can properly chain the object and later
8499 -- finalize it when the library unit goes out of scope.
8501 if Needs_Finalization (Etype (Func_Call)) then
8502 Build_Finalization_Master
8503 (Typ => Ptr_Typ,
8504 For_Lib_Level => True,
8505 Insertion_Node => Ptr_Typ_Decl);
8507 Fmaster_Actual :=
8508 Make_Attribute_Reference (Loc,
8509 Prefix =>
8510 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8511 Attribute_Name => Name_Unrestricted_Access);
8512 end if;
8514 -- In other indefinite cases, pass an indication to do the allocation on
8515 -- the secondary stack and set Caller_Object to Empty so that a null
8516 -- value will be passed for the caller's object address. A transient
8517 -- scope is established to ensure eventual cleanup of the result.
8519 else
8520 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8521 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8522 Caller_Object := Empty;
8524 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8525 end if;
8527 -- Pass along any finalization master actual, which is needed in the
8528 -- case where the called function initializes a return object of an
8529 -- enclosing build-in-place function.
8531 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8532 (Func_Call => Func_Call,
8533 Func_Id => Function_Id,
8534 Master_Exp => Fmaster_Actual);
8536 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8537 and then Has_Task (Result_Subt)
8538 then
8539 -- Here we're passing along the master that was passed in to this
8540 -- function.
8542 Add_Task_Actuals_To_Build_In_Place_Call
8543 (Func_Call, Function_Id,
8544 Master_Actual =>
8545 New_Occurrence_Of
8546 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8548 else
8549 Add_Task_Actuals_To_Build_In_Place_Call
8550 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8551 end if;
8553 Add_Access_Actual_To_Build_In_Place_Call
8554 (Func_Call,
8555 Function_Id,
8556 Caller_Object,
8557 Is_Access => Pass_Caller_Acc);
8559 -- Finally, create an access object initialized to a reference to the
8560 -- function call. We know this access value cannot be null, so mark the
8561 -- entity accordingly to suppress the access check.
8563 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8564 Set_Etype (Def_Id, Ptr_Typ);
8565 Set_Is_Known_Non_Null (Def_Id);
8567 if Nkind_In (Function_Call, N_Type_Conversion,
8568 N_Unchecked_Type_Conversion)
8569 then
8570 Res_Decl :=
8571 Make_Object_Declaration (Loc,
8572 Defining_Identifier => Def_Id,
8573 Constant_Present => True,
8574 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8575 Expression =>
8576 Make_Unchecked_Type_Conversion (Loc,
8577 New_Occurrence_Of (Ptr_Typ, Loc),
8578 Make_Reference (Loc, Relocate_Node (Func_Call))));
8579 else
8580 Res_Decl :=
8581 Make_Object_Declaration (Loc,
8582 Defining_Identifier => Def_Id,
8583 Constant_Present => True,
8584 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8585 Expression =>
8586 Make_Reference (Loc, Relocate_Node (Func_Call)));
8587 end if;
8589 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8591 -- If the result subtype of the called function is definite and is not
8592 -- itself the return expression of an enclosing BIP function, then mark
8593 -- the object as having no initialization.
8595 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8597 -- The related object declaration is encased in a transient block
8598 -- because the build-in-place function call contains at least one
8599 -- nested function call that produces a controlled transient
8600 -- temporary:
8602 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8604 -- Since the build-in-place expansion decouples the call from the
8605 -- object declaration, the finalization machinery lacks the context
8606 -- which prompted the generation of the transient block. To resolve
8607 -- this scenario, store the build-in-place call.
8609 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8610 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8611 end if;
8613 Set_Expression (Obj_Decl, Empty);
8614 Set_No_Initialization (Obj_Decl);
8616 -- In case of an indefinite result subtype, or if the call is the
8617 -- return expression of an enclosing BIP function, rewrite the object
8618 -- declaration as an object renaming where the renamed object is a
8619 -- dereference of <function_Call>'reference:
8621 -- Obj : Subt renames <function_call>'Ref.all;
8623 else
8624 Call_Deref :=
8625 Make_Explicit_Dereference (Obj_Loc,
8626 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8628 Rewrite (Obj_Decl,
8629 Make_Object_Renaming_Declaration (Obj_Loc,
8630 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8631 Subtype_Mark =>
8632 New_Occurrence_Of (Designated_Type, Obj_Loc),
8633 Name => Call_Deref));
8635 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
8636 -- to Obj_Def_Id.
8638 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8640 -- If the original entity comes from source, then mark the new
8641 -- entity as needing debug information, even though it's defined
8642 -- by a generated renaming that does not come from source, so that
8643 -- the Materialize_Entity flag will be set on the entity when
8644 -- Debug_Renaming_Declaration is called during analysis.
8646 if Comes_From_Source (Obj_Def_Id) then
8647 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8648 end if;
8650 Analyze (Obj_Decl);
8651 Replace_Renaming_Declaration_Id
8652 (Obj_Decl, Original_Node (Obj_Decl));
8653 end if;
8654 end Make_Build_In_Place_Call_In_Object_Declaration;
8656 -------------------------------------------------
8657 -- Make_Build_In_Place_Iface_Call_In_Allocator --
8658 -------------------------------------------------
8660 procedure Make_Build_In_Place_Iface_Call_In_Allocator
8661 (Allocator : Node_Id;
8662 Function_Call : Node_Id)
8664 BIP_Func_Call : constant Node_Id :=
8665 Unqual_BIP_Iface_Function_Call (Function_Call);
8666 Loc : constant Source_Ptr := Sloc (Function_Call);
8668 Anon_Type : Entity_Id;
8669 Tmp_Decl : Node_Id;
8670 Tmp_Id : Entity_Id;
8672 begin
8673 -- No action of the call has already been processed
8675 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8676 return;
8677 end if;
8679 Tmp_Id := Make_Temporary (Loc, 'D');
8681 -- Insert a temporary before N initialized with the BIP function call
8682 -- without its enclosing type conversions and analyze it without its
8683 -- expansion. This temporary facilitates us reusing the BIP machinery,
8684 -- which takes care of adding the extra build-in-place actuals and
8685 -- transforms this object declaration into an object renaming
8686 -- declaration.
8688 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
8689 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
8690 Set_Etype (Anon_Type, Anon_Type);
8692 Tmp_Decl :=
8693 Make_Object_Declaration (Loc,
8694 Defining_Identifier => Tmp_Id,
8695 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
8696 Expression =>
8697 Make_Allocator (Loc,
8698 Expression =>
8699 Make_Qualified_Expression (Loc,
8700 Subtype_Mark =>
8701 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8702 Expression => New_Copy_Tree (BIP_Func_Call))));
8704 Expander_Mode_Save_And_Set (False);
8705 Insert_Action (Allocator, Tmp_Decl);
8706 Expander_Mode_Restore;
8708 Make_Build_In_Place_Call_In_Allocator
8709 (Allocator => Expression (Tmp_Decl),
8710 Function_Call => Expression (Expression (Tmp_Decl)));
8712 Rewrite (Allocator, New_Occurrence_Of (Tmp_Id, Loc));
8713 end Make_Build_In_Place_Iface_Call_In_Allocator;
8715 ---------------------------------------------------------
8716 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
8717 ---------------------------------------------------------
8719 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
8720 (Function_Call : Node_Id)
8722 BIP_Func_Call : constant Node_Id :=
8723 Unqual_BIP_Iface_Function_Call (Function_Call);
8724 Loc : constant Source_Ptr := Sloc (Function_Call);
8726 Tmp_Decl : Node_Id;
8727 Tmp_Id : Entity_Id;
8729 begin
8730 -- No action of the call has already been processed
8732 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8733 return;
8734 end if;
8736 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
8738 -- Insert a temporary before the call initialized with function call to
8739 -- reuse the BIP machinery which takes care of adding the extra build-in
8740 -- place actuals and transforms this object declaration into an object
8741 -- renaming declaration.
8743 Tmp_Id := Make_Temporary (Loc, 'D');
8745 Tmp_Decl :=
8746 Make_Object_Declaration (Loc,
8747 Defining_Identifier => Tmp_Id,
8748 Object_Definition =>
8749 New_Occurrence_Of (Etype (Function_Call), Loc),
8750 Expression => Relocate_Node (Function_Call));
8752 Expander_Mode_Save_And_Set (False);
8753 Insert_Action (Function_Call, Tmp_Decl);
8754 Expander_Mode_Restore;
8756 Make_Build_In_Place_Iface_Call_In_Object_Declaration
8757 (Obj_Decl => Tmp_Decl,
8758 Function_Call => Expression (Tmp_Decl));
8759 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
8761 ----------------------------------------------------------
8762 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
8763 ----------------------------------------------------------
8765 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
8766 (Obj_Decl : Node_Id;
8767 Function_Call : Node_Id)
8769 BIP_Func_Call : constant Node_Id :=
8770 Unqual_BIP_Iface_Function_Call (Function_Call);
8771 Loc : constant Source_Ptr := Sloc (Function_Call);
8772 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8774 Tmp_Decl : Node_Id;
8775 Tmp_Id : Entity_Id;
8777 begin
8778 -- No action of the call has already been processed
8780 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8781 return;
8782 end if;
8784 Tmp_Id := Make_Temporary (Loc, 'D');
8786 -- Insert a temporary before N initialized with the BIP function call
8787 -- without its enclosing type conversions and analyze it without its
8788 -- expansion. This temporary facilitates us reusing the BIP machinery,
8789 -- which takes care of adding the extra build-in-place actuals and
8790 -- transforms this object declaration into an object renaming
8791 -- declaration.
8793 Tmp_Decl :=
8794 Make_Object_Declaration (Loc,
8795 Defining_Identifier => Tmp_Id,
8796 Object_Definition =>
8797 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8798 Expression => New_Copy_Tree (BIP_Func_Call));
8800 Expander_Mode_Save_And_Set (False);
8801 Insert_Action (Obj_Decl, Tmp_Decl);
8802 Expander_Mode_Restore;
8804 Make_Build_In_Place_Call_In_Object_Declaration
8805 (Obj_Decl => Tmp_Decl,
8806 Function_Call => Expression (Tmp_Decl));
8808 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
8810 -- Replace the original build-in-place function call by a reference to
8811 -- the resulting temporary object renaming declaration. In this way,
8812 -- all the interface conversions performed in the original Function_Call
8813 -- on the build-in-place object are preserved.
8815 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
8817 -- Replace the original object declaration by an internal object
8818 -- renaming declaration. This leaves the generated code more clean (the
8819 -- build-in-place function call in an object renaming declaration and
8820 -- displacements of the pointer to the build-in-place object in another
8821 -- renaming declaration) and allows us to invoke the routine that takes
8822 -- care of replacing the identifier of the renaming declaration (routine
8823 -- originally developed for the regular build-in-place management).
8825 Rewrite (Obj_Decl,
8826 Make_Object_Renaming_Declaration (Loc,
8827 Defining_Identifier => Make_Temporary (Loc, 'D'),
8828 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
8829 Name => Function_Call));
8830 Analyze (Obj_Decl);
8832 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
8833 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
8835 --------------------------------------------
8836 -- Make_CPP_Constructor_Call_In_Allocator --
8837 --------------------------------------------
8839 procedure Make_CPP_Constructor_Call_In_Allocator
8840 (Allocator : Node_Id;
8841 Function_Call : Node_Id)
8843 Loc : constant Source_Ptr := Sloc (Function_Call);
8844 Acc_Type : constant Entity_Id := Etype (Allocator);
8845 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8846 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8848 New_Allocator : Node_Id;
8849 Return_Obj_Access : Entity_Id;
8850 Tmp_Obj : Node_Id;
8852 begin
8853 pragma Assert (Nkind (Allocator) = N_Allocator
8854 and then Nkind (Function_Call) = N_Function_Call);
8855 pragma Assert (Convention (Function_Id) = Convention_CPP
8856 and then Is_Constructor (Function_Id));
8857 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8859 -- Replace the initialized allocator of form "new T'(Func (...))" with
8860 -- an uninitialized allocator of form "new T", where T is the result
8861 -- subtype of the called function. The call to the function is handled
8862 -- separately further below.
8864 New_Allocator :=
8865 Make_Allocator (Loc,
8866 Expression => New_Occurrence_Of (Result_Subt, Loc));
8867 Set_No_Initialization (New_Allocator);
8869 -- Copy attributes to new allocator. Note that the new allocator
8870 -- logically comes from source if the original one did, so copy the
8871 -- relevant flag. This ensures proper treatment of the restriction
8872 -- No_Implicit_Heap_Allocations in this case.
8874 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8875 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8876 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8878 Rewrite (Allocator, New_Allocator);
8880 -- Create a new access object and initialize it to the result of the
8881 -- new uninitialized allocator. Note: we do not use Allocator as the
8882 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8883 -- as this would create a sort of infinite "recursion".
8885 Return_Obj_Access := Make_Temporary (Loc, 'R');
8886 Set_Etype (Return_Obj_Access, Acc_Type);
8888 -- Generate:
8889 -- Rnnn : constant ptr_T := new (T);
8890 -- Init (Rnn.all,...);
8892 Tmp_Obj :=
8893 Make_Object_Declaration (Loc,
8894 Defining_Identifier => Return_Obj_Access,
8895 Constant_Present => True,
8896 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8897 Expression => Relocate_Node (Allocator));
8898 Insert_Action (Allocator, Tmp_Obj);
8900 Insert_List_After_And_Analyze (Tmp_Obj,
8901 Build_Initialization_Call (Loc,
8902 Id_Ref =>
8903 Make_Explicit_Dereference (Loc,
8904 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8905 Typ => Etype (Function_Id),
8906 Constructor_Ref => Function_Call));
8908 -- Finally, replace the allocator node with a reference to the result of
8909 -- the function call itself (which will effectively be an access to the
8910 -- object created by the allocator).
8912 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8914 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8915 -- generate an implicit conversion to force displacement of the "this"
8916 -- pointer.
8918 if Is_Interface (Designated_Type (Acc_Type)) then
8919 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8920 end if;
8922 Analyze_And_Resolve (Allocator, Acc_Type);
8923 end Make_CPP_Constructor_Call_In_Allocator;
8925 -----------------------------------
8926 -- Needs_BIP_Finalization_Master --
8927 -----------------------------------
8929 function Needs_BIP_Finalization_Master
8930 (Func_Id : Entity_Id) return Boolean
8932 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8933 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8934 begin
8935 -- A formal giving the finalization master is needed for build-in-place
8936 -- functions whose result type needs finalization or is a tagged type.
8937 -- Tagged primitive build-in-place functions need such a formal because
8938 -- they can be called by a dispatching call, and extensions may require
8939 -- finalization even if the root type doesn't. This means they're also
8940 -- needed for tagged nonprimitive build-in-place functions with tagged
8941 -- results, since such functions can be called via access-to-function
8942 -- types, and those can be used to call primitives, so masters have to
8943 -- be passed to all such build-in-place functions, primitive or not.
8945 return
8946 not Restriction_Active (No_Finalization)
8947 and then (Needs_Finalization (Func_Typ)
8948 or else Is_Tagged_Type (Func_Typ));
8949 end Needs_BIP_Finalization_Master;
8951 --------------------------
8952 -- Needs_BIP_Alloc_Form --
8953 --------------------------
8955 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8956 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8957 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8958 begin
8959 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8960 end Needs_BIP_Alloc_Form;
8962 --------------------------------------
8963 -- Needs_Result_Accessibility_Level --
8964 --------------------------------------
8966 function Needs_Result_Accessibility_Level
8967 (Func_Id : Entity_Id) return Boolean
8969 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8971 function Has_Unconstrained_Access_Discriminant_Component
8972 (Comp_Typ : Entity_Id) return Boolean;
8973 -- Returns True if any component of the type has an unconstrained access
8974 -- discriminant.
8976 -----------------------------------------------------
8977 -- Has_Unconstrained_Access_Discriminant_Component --
8978 -----------------------------------------------------
8980 function Has_Unconstrained_Access_Discriminant_Component
8981 (Comp_Typ : Entity_Id) return Boolean
8983 begin
8984 if not Is_Limited_Type (Comp_Typ) then
8985 return False;
8987 -- Only limited types can have access discriminants with
8988 -- defaults.
8990 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8991 return True;
8993 elsif Is_Array_Type (Comp_Typ) then
8994 return Has_Unconstrained_Access_Discriminant_Component
8995 (Underlying_Type (Component_Type (Comp_Typ)));
8997 elsif Is_Record_Type (Comp_Typ) then
8998 declare
8999 Comp : Entity_Id;
9001 begin
9002 Comp := First_Component (Comp_Typ);
9003 while Present (Comp) loop
9004 if Has_Unconstrained_Access_Discriminant_Component
9005 (Underlying_Type (Etype (Comp)))
9006 then
9007 return True;
9008 end if;
9010 Next_Component (Comp);
9011 end loop;
9012 end;
9013 end if;
9015 return False;
9016 end Has_Unconstrained_Access_Discriminant_Component;
9018 Feature_Disabled : constant Boolean := True;
9019 -- Temporary
9021 -- Start of processing for Needs_Result_Accessibility_Level
9023 begin
9024 -- False if completion unavailable (how does this happen???)
9026 if not Present (Func_Typ) then
9027 return False;
9029 elsif Feature_Disabled then
9030 return False;
9032 -- False if not a function, also handle enum-lit renames case
9034 elsif Func_Typ = Standard_Void_Type
9035 or else Is_Scalar_Type (Func_Typ)
9036 then
9037 return False;
9039 -- Handle a corner case, a cross-dialect subp renaming. For example,
9040 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9041 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9043 elsif Present (Alias (Func_Id)) then
9045 -- Unimplemented: a cross-dialect subp renaming which does not set
9046 -- the Alias attribute (e.g., a rename of a dereference of an access
9047 -- to subprogram value). ???
9049 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9051 -- Remaining cases require Ada 2012 mode
9053 elsif Ada_Version < Ada_2012 then
9054 return False;
9056 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9057 or else Is_Tagged_Type (Func_Typ)
9058 then
9059 -- In the case of, say, a null tagged record result type, the need
9060 -- for this extra parameter might not be obvious. This function
9061 -- returns True for all tagged types for compatibility reasons.
9062 -- A function with, say, a tagged null controlling result type might
9063 -- be overridden by a primitive of an extension having an access
9064 -- discriminant and the overrider and overridden must have compatible
9065 -- calling conventions (including implicitly declared parameters).
9066 -- Similarly, values of one access-to-subprogram type might designate
9067 -- both a primitive subprogram of a given type and a function
9068 -- which is, for example, not a primitive subprogram of any type.
9069 -- Again, this requires calling convention compatibility.
9070 -- It might be possible to solve these issues by introducing
9071 -- wrappers, but that is not the approach that was chosen.
9073 return True;
9075 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9076 return True;
9078 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9079 return True;
9081 -- False for all other cases
9083 else
9084 return False;
9085 end if;
9086 end Needs_Result_Accessibility_Level;
9088 -------------------------------------
9089 -- Replace_Renaming_Declaration_Id --
9090 -------------------------------------
9092 procedure Replace_Renaming_Declaration_Id
9093 (New_Decl : Node_Id;
9094 Orig_Decl : Node_Id)
9096 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9097 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9099 begin
9100 Set_Chars (New_Id, Chars (Orig_Id));
9102 -- Swap next entity links in preparation for exchanging entities
9104 declare
9105 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9106 begin
9107 Set_Next_Entity (New_Id, Next_Entity (Orig_Id));
9108 Set_Next_Entity (Orig_Id, Next_Id);
9109 end;
9111 Set_Homonym (New_Id, Homonym (Orig_Id));
9112 Exchange_Entities (New_Id, Orig_Id);
9114 -- Preserve source indication of original declaration, so that xref
9115 -- information is properly generated for the right entity.
9117 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9118 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9120 Set_Comes_From_Source (New_Id, False);
9121 end Replace_Renaming_Declaration_Id;
9123 ---------------------------------
9124 -- Rewrite_Function_Call_For_C --
9125 ---------------------------------
9127 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9128 Orig_Func : constant Entity_Id := Entity (Name (N));
9129 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9130 Par : constant Node_Id := Parent (N);
9131 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9132 Loc : constant Source_Ptr := Sloc (Par);
9133 Actuals : List_Id;
9134 Last_Actual : Node_Id;
9135 Last_Formal : Entity_Id;
9137 -- Start of processing for Rewrite_Function_Call_For_C
9139 begin
9140 -- The actuals may be given by named associations, so the added actual
9141 -- that is the target of the return value of the call must be a named
9142 -- association as well, so we retrieve the name of the generated
9143 -- out_formal.
9145 Last_Formal := First_Formal (Proc_Id);
9146 while Present (Next_Formal (Last_Formal)) loop
9147 Last_Formal := Next_Formal (Last_Formal);
9148 end loop;
9150 Actuals := Parameter_Associations (N);
9152 -- The original function may lack parameters
9154 if No (Actuals) then
9155 Actuals := New_List;
9156 end if;
9158 -- If the function call is the expression of an assignment statement,
9159 -- transform the assignment into a procedure call. Generate:
9161 -- LHS := Func_Call (...);
9163 -- Proc_Call (..., LHS);
9165 -- If function is inherited, a conversion may be necessary.
9167 if Nkind (Par) = N_Assignment_Statement then
9168 Last_Actual := Name (Par);
9170 if not Comes_From_Source (Orig_Func)
9171 and then Etype (Orig_Func) /= Etype (Func_Id)
9172 then
9173 Last_Actual :=
9174 Make_Type_Conversion (Loc,
9175 New_Occurrence_Of (Etype (Func_Id), Loc),
9176 Last_Actual);
9177 end if;
9179 Append_To (Actuals,
9180 Make_Parameter_Association (Loc,
9181 Selector_Name =>
9182 Make_Identifier (Loc, Chars (Last_Formal)),
9183 Explicit_Actual_Parameter => Last_Actual));
9185 Rewrite (Par,
9186 Make_Procedure_Call_Statement (Loc,
9187 Name => New_Occurrence_Of (Proc_Id, Loc),
9188 Parameter_Associations => Actuals));
9189 Analyze (Par);
9191 -- Otherwise the context is an expression. Generate a temporary and a
9192 -- procedure call to obtain the function result. Generate:
9194 -- ... Func_Call (...) ...
9196 -- Temp : ...;
9197 -- Proc_Call (..., Temp);
9198 -- ... Temp ...
9200 else
9201 declare
9202 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9203 Call : Node_Id;
9204 Decl : Node_Id;
9206 begin
9207 -- Generate:
9208 -- Temp : ...;
9210 Decl :=
9211 Make_Object_Declaration (Loc,
9212 Defining_Identifier => Temp_Id,
9213 Object_Definition =>
9214 New_Occurrence_Of (Etype (Func_Id), Loc));
9216 -- Generate:
9217 -- Proc_Call (..., Temp);
9219 Append_To (Actuals,
9220 Make_Parameter_Association (Loc,
9221 Selector_Name =>
9222 Make_Identifier (Loc, Chars (Last_Formal)),
9223 Explicit_Actual_Parameter =>
9224 New_Occurrence_Of (Temp_Id, Loc)));
9226 Call :=
9227 Make_Procedure_Call_Statement (Loc,
9228 Name => New_Occurrence_Of (Proc_Id, Loc),
9229 Parameter_Associations => Actuals);
9231 Insert_Actions (Par, New_List (Decl, Call));
9232 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9233 end;
9234 end if;
9235 end Rewrite_Function_Call_For_C;
9237 ------------------------------------
9238 -- Set_Enclosing_Sec_Stack_Return --
9239 ------------------------------------
9241 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9242 P : Node_Id := N;
9244 begin
9245 -- Due to a possible mix of internally generated blocks, source blocks
9246 -- and loops, the scope stack may not be contiguous as all labels are
9247 -- inserted at the top level within the related function. Instead,
9248 -- perform a parent-based traversal and mark all appropriate constructs.
9250 while Present (P) loop
9252 -- Mark the label of a source or internally generated block or
9253 -- loop.
9255 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
9256 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9258 -- Mark the enclosing function
9260 elsif Nkind (P) = N_Subprogram_Body then
9261 if Present (Corresponding_Spec (P)) then
9262 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9263 else
9264 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9265 end if;
9267 -- Do not go beyond the enclosing function
9269 exit;
9270 end if;
9272 P := Parent (P);
9273 end loop;
9274 end Set_Enclosing_Sec_Stack_Return;
9276 ------------------------------------
9277 -- Unqual_BIP_Iface_Function_Call --
9278 ------------------------------------
9280 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9281 Has_Pointer_Displacement : Boolean := False;
9282 On_Object_Declaration : Boolean := False;
9283 -- Remember if processing the renaming expressions on recursion we have
9284 -- traversed an object declaration, since we can traverse many object
9285 -- declaration renamings but just one regular object declaration.
9287 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9288 -- Search for a build-in-place function call skipping any qualification
9289 -- including qualified expressions, type conversions, references, calls
9290 -- to displace the pointer to the object, and renamings. Return Empty if
9291 -- no build-in-place function call is found.
9293 ------------------------------
9294 -- Unqual_BIP_Function_Call --
9295 ------------------------------
9297 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9298 begin
9299 -- Recurse to handle case of multiple levels of qualification and/or
9300 -- conversion.
9302 if Nkind_In (Expr, N_Qualified_Expression,
9303 N_Type_Conversion,
9304 N_Unchecked_Type_Conversion)
9305 then
9306 return Unqual_BIP_Function_Call (Expression (Expr));
9308 -- Recurse to handle case of multiple levels of references and
9309 -- explicit dereferences.
9311 elsif Nkind_In (Expr, N_Attribute_Reference,
9312 N_Explicit_Dereference,
9313 N_Reference)
9314 then
9315 return Unqual_BIP_Function_Call (Prefix (Expr));
9317 -- Recurse on object renamings
9319 elsif Nkind (Expr) = N_Identifier
9320 and then Present (Entity (Expr))
9321 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9322 and then Nkind (Parent (Entity (Expr))) =
9323 N_Object_Renaming_Declaration
9324 and then Present (Renamed_Object (Entity (Expr)))
9325 then
9326 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9328 -- Recurse on the initializing expression of the first reference of
9329 -- an object declaration.
9331 elsif not On_Object_Declaration
9332 and then Nkind (Expr) = N_Identifier
9333 and then Present (Entity (Expr))
9334 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9335 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9336 and then Present (Expression (Parent (Entity (Expr))))
9337 then
9338 On_Object_Declaration := True;
9339 return
9340 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9342 -- Recurse to handle calls to displace the pointer to the object to
9343 -- reference a secondary dispatch table.
9345 elsif Nkind (Expr) = N_Function_Call
9346 and then Nkind (Name (Expr)) in N_Has_Entity
9347 and then Present (Entity (Name (Expr)))
9348 and then RTU_Loaded (Ada_Tags)
9349 and then RTE_Available (RE_Displace)
9350 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9351 then
9352 Has_Pointer_Displacement := True;
9353 return
9354 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9356 -- Normal case: check if the inner expression is a BIP function call
9357 -- and the pointer to the object is displaced.
9359 elsif Has_Pointer_Displacement
9360 and then Is_Build_In_Place_Function_Call (Expr)
9361 then
9362 return Expr;
9364 else
9365 return Empty;
9366 end if;
9367 end Unqual_BIP_Function_Call;
9369 -- Start of processing for Unqual_BIP_Iface_Function_Call
9371 begin
9372 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9374 -- Can happen for X'Elab_Spec in the binder-generated file
9376 return Empty;
9377 end if;
9379 return Unqual_BIP_Function_Call (Expr);
9380 end Unqual_BIP_Iface_Function_Call;
9382 end Exp_Ch6;