Fix date
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobc2edde696f7d87bd7ecc43ba282566861551117c
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, Statements (Stmt));
717 elsif Nkind (Stmt) = N_Case_Statement then
718 declare
719 Alt : Node_Id;
720 begin
721 Alt := First (Alternatives (Stmt));
722 while Present (Alt) loop
723 Replace_Returns (Param_Id, Statements (Alt));
724 Next (Alt);
725 end loop;
726 end;
728 elsif Nkind (Stmt) = N_Extended_Return_Statement then
729 declare
730 Ret_Obj : constant Entity_Id :=
731 Defining_Entity
732 (First (Return_Object_Declarations (Stmt)));
733 Assign : constant Node_Id :=
734 Make_Assignment_Statement (Sloc (Stmt),
735 Name =>
736 New_Occurrence_Of (Param_Id, Loc),
737 Expression =>
738 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
739 Stmts : List_Id;
741 begin
742 -- The extended return may just contain the declaration
744 if Present (Handled_Statement_Sequence (Stmt)) then
745 Stmts := Statements (Handled_Statement_Sequence (Stmt));
746 else
747 Stmts := New_List;
748 end if;
750 Set_Assignment_OK (Name (Assign));
752 Rewrite (Stmt,
753 Make_Block_Statement (Sloc (Stmt),
754 Declarations =>
755 Return_Object_Declarations (Stmt),
756 Handled_Statement_Sequence =>
757 Make_Handled_Sequence_Of_Statements (Loc,
758 Statements => Stmts)));
760 Replace_Returns (Param_Id, Stmts);
762 Append_To (Stmts, Assign);
763 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
764 end;
766 elsif Nkind (Stmt) = N_If_Statement then
767 Replace_Returns (Param_Id, Then_Statements (Stmt));
768 Replace_Returns (Param_Id, Else_Statements (Stmt));
770 declare
771 Part : Node_Id;
772 begin
773 Part := First (Elsif_Parts (Stmt));
774 while Present (Part) loop
775 Replace_Returns (Param_Id, Then_Statements (Part));
776 Next (Part);
777 end loop;
778 end;
780 elsif Nkind (Stmt) = N_Loop_Statement then
781 Replace_Returns (Param_Id, Statements (Stmt));
783 elsif Nkind (Stmt) = N_Simple_Return_Statement then
785 -- Generate:
786 -- Param := Expr;
787 -- return;
789 Rewrite (Stmt,
790 Make_Assignment_Statement (Sloc (Stmt),
791 Name => New_Occurrence_Of (Param_Id, Loc),
792 Expression => Relocate_Node (Expression (Stmt))));
794 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
796 -- Skip the added return
798 Next (Stmt);
799 end if;
801 Next (Stmt);
802 end loop;
803 end Replace_Returns;
805 -- Local variables
807 Stmts : List_Id;
808 New_Body : Node_Id;
810 -- Start of processing for Build_Procedure_Body_Form
812 begin
813 -- This routine replaces the original function body:
815 -- function F (...) return Array_Typ is
816 -- begin
817 -- ...
818 -- return Something;
819 -- end F;
821 -- with the following:
823 -- procedure P (..., Result : out Array_Typ) is
824 -- begin
825 -- ...
826 -- Result := Something;
827 -- end P;
829 Stmts :=
830 Statements (Handled_Statement_Sequence (Func_Body));
831 Replace_Returns (Last_Entity (Proc_Id), Stmts);
833 New_Body :=
834 Make_Subprogram_Body (Loc,
835 Specification =>
836 Copy_Subprogram_Spec (Specification (Proc_Decl)),
837 Declarations => Declarations (Func_Body),
838 Handled_Statement_Sequence =>
839 Make_Handled_Sequence_Of_Statements (Loc,
840 Statements => Stmts));
842 -- If the function is a generic instance, so is the new procedure.
843 -- Set flag accordingly so that the proper renaming declarations are
844 -- generated.
846 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
847 return New_Body;
848 end Build_Procedure_Body_Form;
850 -----------------------
851 -- Caller_Known_Size --
852 -----------------------
854 function Caller_Known_Size
855 (Func_Call : Node_Id;
856 Result_Subt : Entity_Id) return Boolean
858 begin
859 return
860 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
861 and then No (Controlling_Argument (Func_Call)))
862 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
863 end Caller_Known_Size;
865 --------------------------------
866 -- Check_Overriding_Operation --
867 --------------------------------
869 procedure Check_Overriding_Operation (Subp : Entity_Id) is
870 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
871 Op_List : constant Elist_Id := Primitive_Operations (Typ);
872 Op_Elmt : Elmt_Id;
873 Prim_Op : Entity_Id;
874 Par_Op : Entity_Id;
876 begin
877 if Is_Derived_Type (Typ)
878 and then not Is_Private_Type (Typ)
879 and then In_Open_Scopes (Scope (Etype (Typ)))
880 and then Is_Base_Type (Typ)
881 then
882 -- Subp overrides an inherited private operation if there is an
883 -- inherited operation with a different name than Subp (see
884 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
885 -- same name as Subp.
887 Op_Elmt := First_Elmt (Op_List);
888 while Present (Op_Elmt) loop
889 Prim_Op := Node (Op_Elmt);
890 Par_Op := Alias (Prim_Op);
892 if Present (Par_Op)
893 and then not Comes_From_Source (Prim_Op)
894 and then Chars (Prim_Op) /= Chars (Par_Op)
895 and then Chars (Par_Op) = Chars (Subp)
896 and then Is_Hidden (Par_Op)
897 and then Type_Conformant (Prim_Op, Subp)
898 then
899 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
900 end if;
902 Next_Elmt (Op_Elmt);
903 end loop;
904 end if;
905 end Check_Overriding_Operation;
907 -------------------------------
908 -- Detect_Infinite_Recursion --
909 -------------------------------
911 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
912 Loc : constant Source_Ptr := Sloc (N);
914 Var_List : constant Elist_Id := New_Elmt_List;
915 -- List of globals referenced by body of procedure
917 Call_List : constant Elist_Id := New_Elmt_List;
918 -- List of recursive calls in body of procedure
920 Shad_List : constant Elist_Id := New_Elmt_List;
921 -- List of entity id's for entities created to capture the value of
922 -- referenced globals on entry to the procedure.
924 Scop : constant Uint := Scope_Depth (Spec);
925 -- This is used to record the scope depth of the current procedure, so
926 -- that we can identify global references.
928 Max_Vars : constant := 4;
929 -- Do not test more than four global variables
931 Count_Vars : Natural := 0;
932 -- Count variables found so far
934 Var : Entity_Id;
935 Elm : Elmt_Id;
936 Ent : Entity_Id;
937 Call : Elmt_Id;
938 Decl : Node_Id;
939 Test : Node_Id;
940 Elm1 : Elmt_Id;
941 Elm2 : Elmt_Id;
942 Last : Node_Id;
944 function Process (Nod : Node_Id) return Traverse_Result;
945 -- Function to traverse the subprogram body (using Traverse_Func)
947 -------------
948 -- Process --
949 -------------
951 function Process (Nod : Node_Id) return Traverse_Result is
952 begin
953 -- Procedure call
955 if Nkind (Nod) = N_Procedure_Call_Statement then
957 -- Case of one of the detected recursive calls
959 if Is_Entity_Name (Name (Nod))
960 and then Has_Recursive_Call (Entity (Name (Nod)))
961 and then Entity (Name (Nod)) = Spec
962 then
963 Append_Elmt (Nod, Call_List);
964 return Skip;
966 -- Any other procedure call may have side effects
968 else
969 return Abandon;
970 end if;
972 -- A call to a pure function can always be ignored
974 elsif Nkind (Nod) = N_Function_Call
975 and then Is_Entity_Name (Name (Nod))
976 and then Is_Pure (Entity (Name (Nod)))
977 then
978 return Skip;
980 -- Case of an identifier reference
982 elsif Nkind (Nod) = N_Identifier then
983 Ent := Entity (Nod);
985 -- If no entity, then ignore the reference
987 -- Not clear why this can happen. To investigate, remove this
988 -- test and look at the crash that occurs here in 3401-004 ???
990 if No (Ent) then
991 return Skip;
993 -- Ignore entities with no Scope, again not clear how this
994 -- can happen, to investigate, look at 4108-008 ???
996 elsif No (Scope (Ent)) then
997 return Skip;
999 -- Ignore the reference if not to a more global object
1001 elsif Scope_Depth (Scope (Ent)) >= Scop then
1002 return Skip;
1004 -- References to types, exceptions and constants are always OK
1006 elsif Is_Type (Ent)
1007 or else Ekind (Ent) = E_Exception
1008 or else Ekind (Ent) = E_Constant
1009 then
1010 return Skip;
1012 -- If other than a non-volatile scalar variable, we have some
1013 -- kind of global reference (e.g. to a function) that we cannot
1014 -- deal with so we forget the attempt.
1016 elsif Ekind (Ent) /= E_Variable
1017 or else not Is_Scalar_Type (Etype (Ent))
1018 or else Treat_As_Volatile (Ent)
1019 then
1020 return Abandon;
1022 -- Otherwise we have a reference to a global scalar
1024 else
1025 -- Loop through global entities already detected
1027 Elm := First_Elmt (Var_List);
1028 loop
1029 -- If not detected before, record this new global reference
1031 if No (Elm) then
1032 Count_Vars := Count_Vars + 1;
1034 if Count_Vars <= Max_Vars then
1035 Append_Elmt (Entity (Nod), Var_List);
1036 else
1037 return Abandon;
1038 end if;
1040 exit;
1042 -- If recorded before, ignore
1044 elsif Node (Elm) = Entity (Nod) then
1045 return Skip;
1047 -- Otherwise keep looking
1049 else
1050 Next_Elmt (Elm);
1051 end if;
1052 end loop;
1054 return Skip;
1055 end if;
1057 -- For all other node kinds, recursively visit syntactic children
1059 else
1060 return OK;
1061 end if;
1062 end Process;
1064 function Traverse_Body is new Traverse_Func (Process);
1066 -- Start of processing for Detect_Infinite_Recursion
1068 begin
1069 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1070 -- won't be able to generate the code to handle the recursion in any
1071 -- case.
1073 if Restriction_Active (No_Implicit_Conditionals) then
1074 return;
1075 end if;
1077 -- Otherwise do traversal and quit if we get abandon signal
1079 if Traverse_Body (N) = Abandon then
1080 return;
1082 -- We must have a call, since Has_Recursive_Call was set. If not just
1083 -- ignore (this is only an error check, so if we have a funny situation,
1084 -- due to bugs or errors, we do not want to bomb).
1086 elsif Is_Empty_Elmt_List (Call_List) then
1087 return;
1088 end if;
1090 -- Here is the case where we detect recursion at compile time
1092 -- Push our current scope for analyzing the declarations and code that
1093 -- we will insert for the checking.
1095 Push_Scope (Spec);
1097 -- This loop builds temporary variables for each of the referenced
1098 -- globals, so that at the end of the loop the list Shad_List contains
1099 -- these temporaries in one-to-one correspondence with the elements in
1100 -- Var_List.
1102 Last := Empty;
1103 Elm := First_Elmt (Var_List);
1104 while Present (Elm) loop
1105 Var := Node (Elm);
1106 Ent := Make_Temporary (Loc, 'S');
1107 Append_Elmt (Ent, Shad_List);
1109 -- Insert a declaration for this temporary at the start of the
1110 -- declarations for the procedure. The temporaries are declared as
1111 -- constant objects initialized to the current values of the
1112 -- corresponding temporaries.
1114 Decl :=
1115 Make_Object_Declaration (Loc,
1116 Defining_Identifier => Ent,
1117 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1118 Constant_Present => True,
1119 Expression => New_Occurrence_Of (Var, Loc));
1121 if No (Last) then
1122 Prepend (Decl, Declarations (N));
1123 else
1124 Insert_After (Last, Decl);
1125 end if;
1127 Last := Decl;
1128 Analyze (Decl);
1129 Next_Elmt (Elm);
1130 end loop;
1132 -- Loop through calls
1134 Call := First_Elmt (Call_List);
1135 while Present (Call) loop
1137 -- Build a predicate expression of the form
1139 -- True
1140 -- and then global1 = temp1
1141 -- and then global2 = temp2
1142 -- ...
1144 -- This predicate determines if any of the global values
1145 -- referenced by the procedure have changed since the
1146 -- current call, if not an infinite recursion is assured.
1148 Test := New_Occurrence_Of (Standard_True, Loc);
1150 Elm1 := First_Elmt (Var_List);
1151 Elm2 := First_Elmt (Shad_List);
1152 while Present (Elm1) loop
1153 Test :=
1154 Make_And_Then (Loc,
1155 Left_Opnd => Test,
1156 Right_Opnd =>
1157 Make_Op_Eq (Loc,
1158 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1159 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1161 Next_Elmt (Elm1);
1162 Next_Elmt (Elm2);
1163 end loop;
1165 -- Now we replace the call with the sequence
1167 -- if no-changes (see above) then
1168 -- raise Storage_Error;
1169 -- else
1170 -- original-call
1171 -- end if;
1173 Rewrite (Node (Call),
1174 Make_If_Statement (Loc,
1175 Condition => Test,
1176 Then_Statements => New_List (
1177 Make_Raise_Storage_Error (Loc,
1178 Reason => SE_Infinite_Recursion)),
1180 Else_Statements => New_List (
1181 Relocate_Node (Node (Call)))));
1183 Analyze (Node (Call));
1185 Next_Elmt (Call);
1186 end loop;
1188 -- Remove temporary scope stack entry used for analysis
1190 Pop_Scope;
1191 end Detect_Infinite_Recursion;
1193 --------------------
1194 -- Expand_Actuals --
1195 --------------------
1197 procedure Expand_Actuals
1198 (N : Node_Id;
1199 Subp : Entity_Id;
1200 Post_Call : out List_Id)
1202 Loc : constant Source_Ptr := Sloc (N);
1203 Actual : Node_Id;
1204 Formal : Entity_Id;
1205 N_Node : Node_Id;
1206 E_Actual : Entity_Id;
1207 E_Formal : Entity_Id;
1209 procedure Add_Call_By_Copy_Code;
1210 -- For cases where the parameter must be passed by copy, this routine
1211 -- generates a temporary variable into which the actual is copied and
1212 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1213 -- an assignment is also generated to copy the result back. The call
1214 -- also takes care of any constraint checks required for the type
1215 -- conversion case (on both the way in and the way out).
1217 procedure Add_Simple_Call_By_Copy_Code;
1218 -- This is similar to the above, but is used in cases where we know
1219 -- that all that is needed is to simply create a temporary and copy
1220 -- the value in and out of the temporary.
1222 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1223 -- Perform copy-back for actual parameter Act which denotes a validation
1224 -- variable.
1226 procedure Check_Fortran_Logical;
1227 -- A value of type Logical that is passed through a formal parameter
1228 -- must be normalized because .TRUE. usually does not have the same
1229 -- representation as True. We assume that .FALSE. = False = 0.
1230 -- What about functions that return a logical type ???
1232 function Is_Legal_Copy return Boolean;
1233 -- Check that an actual can be copied before generating the temporary
1234 -- to be used in the call. If the actual is of a by_reference type then
1235 -- the program is illegal (this can only happen in the presence of
1236 -- rep. clauses that force an incorrect alignment). If the formal is
1237 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1238 -- the effect that this might lead to unaligned arguments.
1240 function Make_Var (Actual : Node_Id) return Entity_Id;
1241 -- Returns an entity that refers to the given actual parameter, Actual
1242 -- (not including any type conversion). If Actual is an entity name,
1243 -- then this entity is returned unchanged, otherwise a renaming is
1244 -- created to provide an entity for the actual.
1246 procedure Reset_Packed_Prefix;
1247 -- The expansion of a packed array component reference is delayed in
1248 -- the context of a call. Now we need to complete the expansion, so we
1249 -- unmark the analyzed bits in all prefixes.
1251 ---------------------------
1252 -- Add_Call_By_Copy_Code --
1253 ---------------------------
1255 procedure Add_Call_By_Copy_Code is
1256 Crep : Boolean;
1257 Expr : Node_Id;
1258 F_Typ : Entity_Id := Etype (Formal);
1259 Indic : Node_Id;
1260 Init : Node_Id;
1261 Temp : Entity_Id;
1262 V_Typ : Entity_Id;
1263 Var : Entity_Id;
1265 begin
1266 if not Is_Legal_Copy then
1267 return;
1268 end if;
1270 Temp := Make_Temporary (Loc, 'T', Actual);
1272 -- Handle formals whose type comes from the limited view
1274 if From_Limited_With (F_Typ)
1275 and then Has_Non_Limited_View (F_Typ)
1276 then
1277 F_Typ := Non_Limited_View (F_Typ);
1278 end if;
1280 -- Use formal type for temp, unless formal type is an unconstrained
1281 -- array, in which case we don't have to worry about bounds checks,
1282 -- and we use the actual type, since that has appropriate bounds.
1284 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1285 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1286 else
1287 Indic := New_Occurrence_Of (F_Typ, Loc);
1288 end if;
1290 if Nkind (Actual) = N_Type_Conversion then
1291 V_Typ := Etype (Expression (Actual));
1293 -- If the formal is an (in-)out parameter, capture the name
1294 -- of the variable in order to build the post-call assignment.
1296 Var := Make_Var (Expression (Actual));
1298 Crep := not Same_Representation
1299 (F_Typ, Etype (Expression (Actual)));
1301 else
1302 V_Typ := Etype (Actual);
1303 Var := Make_Var (Actual);
1304 Crep := False;
1305 end if;
1307 -- Setup initialization for case of in out parameter, or an out
1308 -- parameter where the formal is an unconstrained array (in the
1309 -- latter case, we have to pass in an object with bounds).
1311 -- If this is an out parameter, the initial copy is wasteful, so as
1312 -- an optimization for the one-dimensional case we extract the
1313 -- bounds of the actual and build an uninitialized temporary of the
1314 -- right size.
1316 if Ekind (Formal) = E_In_Out_Parameter
1317 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1318 then
1319 if Nkind (Actual) = N_Type_Conversion then
1320 if Conversion_OK (Actual) then
1321 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1322 else
1323 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1324 end if;
1326 elsif Ekind (Formal) = E_Out_Parameter
1327 and then Is_Array_Type (F_Typ)
1328 and then Number_Dimensions (F_Typ) = 1
1329 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1330 then
1331 -- Actual is a one-dimensional array or slice, and the type
1332 -- requires no initialization. Create a temporary of the
1333 -- right size, but do not copy actual into it (optimization).
1335 Init := Empty;
1336 Indic :=
1337 Make_Subtype_Indication (Loc,
1338 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1339 Constraint =>
1340 Make_Index_Or_Discriminant_Constraint (Loc,
1341 Constraints => New_List (
1342 Make_Range (Loc,
1343 Low_Bound =>
1344 Make_Attribute_Reference (Loc,
1345 Prefix => New_Occurrence_Of (Var, Loc),
1346 Attribute_Name => Name_First),
1347 High_Bound =>
1348 Make_Attribute_Reference (Loc,
1349 Prefix => New_Occurrence_Of (Var, Loc),
1350 Attribute_Name => Name_Last)))));
1352 else
1353 Init := New_Occurrence_Of (Var, Loc);
1354 end if;
1356 -- An initialization is created for packed conversions as
1357 -- actuals for out parameters to enable Make_Object_Declaration
1358 -- to determine the proper subtype for N_Node. Note that this
1359 -- is wasteful because the extra copying on the call side is
1360 -- not required for such out parameters. ???
1362 elsif Ekind (Formal) = E_Out_Parameter
1363 and then Nkind (Actual) = N_Type_Conversion
1364 and then (Is_Bit_Packed_Array (F_Typ)
1365 or else
1366 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1367 then
1368 if Conversion_OK (Actual) then
1369 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1370 else
1371 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1372 end if;
1374 elsif Ekind (Formal) = E_In_Parameter then
1376 -- Handle the case in which the actual is a type conversion
1378 if Nkind (Actual) = N_Type_Conversion then
1379 if Conversion_OK (Actual) then
1380 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1381 else
1382 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1383 end if;
1384 else
1385 Init := New_Occurrence_Of (Var, Loc);
1386 end if;
1388 else
1389 Init := Empty;
1390 end if;
1392 N_Node :=
1393 Make_Object_Declaration (Loc,
1394 Defining_Identifier => Temp,
1395 Object_Definition => Indic,
1396 Expression => Init);
1397 Set_Assignment_OK (N_Node);
1398 Insert_Action (N, N_Node);
1400 -- Now, normally the deal here is that we use the defining
1401 -- identifier created by that object declaration. There is
1402 -- one exception to this. In the change of representation case
1403 -- the above declaration will end up looking like:
1405 -- temp : type := identifier;
1407 -- And in this case we might as well use the identifier directly
1408 -- and eliminate the temporary. Note that the analysis of the
1409 -- declaration was not a waste of time in that case, since it is
1410 -- what generated the necessary change of representation code. If
1411 -- the change of representation introduced additional code, as in
1412 -- a fixed-integer conversion, the expression is not an identifier
1413 -- and must be kept.
1415 if Crep
1416 and then Present (Expression (N_Node))
1417 and then Is_Entity_Name (Expression (N_Node))
1418 then
1419 Temp := Entity (Expression (N_Node));
1420 Rewrite (N_Node, Make_Null_Statement (Loc));
1421 end if;
1423 -- For IN parameter, all we do is to replace the actual
1425 if Ekind (Formal) = E_In_Parameter then
1426 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1427 Analyze (Actual);
1429 -- Processing for OUT or IN OUT parameter
1431 else
1432 -- Kill current value indications for the temporary variable we
1433 -- created, since we just passed it as an OUT parameter.
1435 Kill_Current_Values (Temp);
1436 Set_Is_Known_Valid (Temp, False);
1438 -- If type conversion, use reverse conversion on exit
1440 if Nkind (Actual) = N_Type_Conversion then
1441 if Conversion_OK (Actual) then
1442 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1443 else
1444 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1445 end if;
1446 else
1447 Expr := New_Occurrence_Of (Temp, Loc);
1448 end if;
1450 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1451 Analyze (Actual);
1453 -- If the actual is a conversion of a packed reference, it may
1454 -- already have been expanded by Remove_Side_Effects, and the
1455 -- resulting variable is a temporary which does not designate
1456 -- the proper out-parameter, which may not be addressable. In
1457 -- that case, generate an assignment to the original expression
1458 -- (before expansion of the packed reference) so that the proper
1459 -- expansion of assignment to a packed component can take place.
1461 declare
1462 Obj : Node_Id;
1463 Lhs : Node_Id;
1465 begin
1466 if Is_Renaming_Of_Object (Var)
1467 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1468 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1469 = N_Indexed_Component
1470 and then
1471 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1472 then
1473 Obj := Renamed_Object (Var);
1474 Lhs :=
1475 Make_Selected_Component (Loc,
1476 Prefix =>
1477 New_Copy_Tree (Original_Node (Prefix (Obj))),
1478 Selector_Name => New_Copy (Selector_Name (Obj)));
1479 Reset_Analyzed_Flags (Lhs);
1481 else
1482 Lhs := New_Occurrence_Of (Var, Loc);
1483 end if;
1485 Set_Assignment_OK (Lhs);
1487 if Is_Access_Type (E_Formal)
1488 and then Is_Entity_Name (Lhs)
1489 and then
1490 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1491 then
1492 -- Copyback target is an Ada 2012 stand-alone object of an
1493 -- anonymous access type.
1495 pragma Assert (Ada_Version >= Ada_2012);
1497 if Type_Access_Level (E_Formal) >
1498 Object_Access_Level (Lhs)
1499 then
1500 Append_To (Post_Call,
1501 Make_Raise_Program_Error (Loc,
1502 Reason => PE_Accessibility_Check_Failed));
1503 end if;
1505 Append_To (Post_Call,
1506 Make_Assignment_Statement (Loc,
1507 Name => Lhs,
1508 Expression => Expr));
1510 -- We would like to somehow suppress generation of the
1511 -- extra_accessibility assignment generated by the expansion
1512 -- of the above assignment statement. It's not a correctness
1513 -- issue because the following assignment renders it dead,
1514 -- but generating back-to-back assignments to the same
1515 -- target is undesirable. ???
1517 Append_To (Post_Call,
1518 Make_Assignment_Statement (Loc,
1519 Name => New_Occurrence_Of (
1520 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1521 Expression => Make_Integer_Literal (Loc,
1522 Type_Access_Level (E_Formal))));
1524 else
1525 Append_To (Post_Call,
1526 Make_Assignment_Statement (Loc,
1527 Name => Lhs,
1528 Expression => Expr));
1529 end if;
1530 end;
1531 end if;
1532 end Add_Call_By_Copy_Code;
1534 ----------------------------------
1535 -- Add_Simple_Call_By_Copy_Code --
1536 ----------------------------------
1538 procedure Add_Simple_Call_By_Copy_Code is
1539 Decl : Node_Id;
1540 F_Typ : Entity_Id := Etype (Formal);
1541 Incod : Node_Id;
1542 Indic : Node_Id;
1543 Lhs : Node_Id;
1544 Outcod : Node_Id;
1545 Rhs : Node_Id;
1546 Temp : Entity_Id;
1548 begin
1549 if not Is_Legal_Copy then
1550 return;
1551 end if;
1553 -- Handle formals whose type comes from the limited view
1555 if From_Limited_With (F_Typ)
1556 and then Has_Non_Limited_View (F_Typ)
1557 then
1558 F_Typ := Non_Limited_View (F_Typ);
1559 end if;
1561 -- Use formal type for temp, unless formal type is an unconstrained
1562 -- array, in which case we don't have to worry about bounds checks,
1563 -- and we use the actual type, since that has appropriate bounds.
1565 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1566 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1567 else
1568 Indic := New_Occurrence_Of (F_Typ, Loc);
1569 end if;
1571 -- Prepare to generate code
1573 Reset_Packed_Prefix;
1575 Temp := Make_Temporary (Loc, 'T', Actual);
1576 Incod := Relocate_Node (Actual);
1577 Outcod := New_Copy_Tree (Incod);
1579 -- Generate declaration of temporary variable, initializing it
1580 -- with the input parameter unless we have an OUT formal or
1581 -- this is an initialization call.
1583 -- If the formal is an out parameter with discriminants, the
1584 -- discriminants must be captured even if the rest of the object
1585 -- is in principle uninitialized, because the discriminants may
1586 -- be read by the called subprogram.
1588 if Ekind (Formal) = E_Out_Parameter then
1589 Incod := Empty;
1591 if Has_Discriminants (F_Typ) then
1592 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1593 end if;
1595 elsif Inside_Init_Proc then
1597 -- Could use a comment here to match comment below ???
1599 if Nkind (Actual) /= N_Selected_Component
1600 or else
1601 not Has_Discriminant_Dependent_Constraint
1602 (Entity (Selector_Name (Actual)))
1603 then
1604 Incod := Empty;
1606 -- Otherwise, keep the component in order to generate the proper
1607 -- actual subtype, that depends on enclosing discriminants.
1609 else
1610 null;
1611 end if;
1612 end if;
1614 Decl :=
1615 Make_Object_Declaration (Loc,
1616 Defining_Identifier => Temp,
1617 Object_Definition => Indic,
1618 Expression => Incod);
1620 if Inside_Init_Proc
1621 and then No (Incod)
1622 then
1623 -- If the call is to initialize a component of a composite type,
1624 -- and the component does not depend on discriminants, use the
1625 -- actual type of the component. This is required in case the
1626 -- component is constrained, because in general the formal of the
1627 -- initialization procedure will be unconstrained. Note that if
1628 -- the component being initialized is constrained by an enclosing
1629 -- discriminant, the presence of the initialization in the
1630 -- declaration will generate an expression for the actual subtype.
1632 Set_No_Initialization (Decl);
1633 Set_Object_Definition (Decl,
1634 New_Occurrence_Of (Etype (Actual), Loc));
1635 end if;
1637 Insert_Action (N, Decl);
1639 -- The actual is simply a reference to the temporary
1641 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1643 -- Generate copy out if OUT or IN OUT parameter
1645 if Ekind (Formal) /= E_In_Parameter then
1646 Lhs := Outcod;
1647 Rhs := New_Occurrence_Of (Temp, Loc);
1649 -- Deal with conversion
1651 if Nkind (Lhs) = N_Type_Conversion then
1652 Lhs := Expression (Lhs);
1653 Rhs := Convert_To (Etype (Actual), Rhs);
1654 end if;
1656 Append_To (Post_Call,
1657 Make_Assignment_Statement (Loc,
1658 Name => Lhs,
1659 Expression => Rhs));
1660 Set_Assignment_OK (Name (Last (Post_Call)));
1661 end if;
1662 end Add_Simple_Call_By_Copy_Code;
1664 --------------------------------------
1665 -- Add_Validation_Call_By_Copy_Code --
1666 --------------------------------------
1668 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
1669 Expr : Node_Id;
1670 Obj : Node_Id;
1671 Obj_Typ : Entity_Id;
1672 Var : constant Node_Id := Unqual_Conv (Act);
1673 Var_Id : Entity_Id;
1675 begin
1676 -- Copy the value of the validation variable back into the object
1677 -- being validated.
1679 if Is_Entity_Name (Var) then
1680 Var_Id := Entity (Var);
1681 Obj := Validated_Object (Var_Id);
1682 Obj_Typ := Etype (Obj);
1684 Expr := New_Occurrence_Of (Var_Id, Loc);
1686 -- A type conversion is needed when the validation variable and
1687 -- the validated object carry different types. This case occurs
1688 -- when the actual is qualified in some fashion.
1690 -- Common:
1691 -- subtype Int is Integer range ...;
1692 -- procedure Call (Val : in out Integer);
1694 -- Original:
1695 -- Object : Int;
1696 -- Call (Integer (Object));
1698 -- Expanded:
1699 -- Object : Int;
1700 -- Var : Integer := Object; -- conversion to base type
1701 -- if not Var'Valid then -- validity check
1702 -- Call (Var); -- modify Var
1703 -- Object := Int (Var); -- conversion to subtype
1705 if Etype (Var_Id) /= Obj_Typ then
1706 Expr :=
1707 Make_Type_Conversion (Loc,
1708 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
1709 Expression => Expr);
1710 end if;
1712 -- Generate:
1713 -- Object := Var;
1714 -- <or>
1715 -- Object := Object_Type (Var);
1717 Append_To (Post_Call,
1718 Make_Assignment_Statement (Loc,
1719 Name => Obj,
1720 Expression => Expr));
1722 -- If the flow reaches this point, then this routine was invoked with
1723 -- an actual which does not denote a validation variable.
1725 else
1726 pragma Assert (False);
1727 null;
1728 end if;
1729 end Add_Validation_Call_By_Copy_Code;
1731 ---------------------------
1732 -- Check_Fortran_Logical --
1733 ---------------------------
1735 procedure Check_Fortran_Logical is
1736 Logical : constant Entity_Id := Etype (Formal);
1737 Var : Entity_Id;
1739 -- Note: this is very incomplete, e.g. it does not handle arrays
1740 -- of logical values. This is really not the right approach at all???)
1742 begin
1743 if Convention (Subp) = Convention_Fortran
1744 and then Root_Type (Etype (Formal)) = Standard_Boolean
1745 and then Ekind (Formal) /= E_In_Parameter
1746 then
1747 Var := Make_Var (Actual);
1748 Append_To (Post_Call,
1749 Make_Assignment_Statement (Loc,
1750 Name => New_Occurrence_Of (Var, Loc),
1751 Expression =>
1752 Unchecked_Convert_To (
1753 Logical,
1754 Make_Op_Ne (Loc,
1755 Left_Opnd => New_Occurrence_Of (Var, Loc),
1756 Right_Opnd =>
1757 Unchecked_Convert_To (
1758 Logical,
1759 New_Occurrence_Of (Standard_False, Loc))))));
1760 end if;
1761 end Check_Fortran_Logical;
1763 -------------------
1764 -- Is_Legal_Copy --
1765 -------------------
1767 function Is_Legal_Copy return Boolean is
1768 begin
1769 -- An attempt to copy a value of such a type can only occur if
1770 -- representation clauses give the actual a misaligned address.
1772 if Is_By_Reference_Type (Etype (Formal)) then
1774 -- The actual may in fact be properly aligned but there is not
1775 -- enough front-end information to determine this. In that case
1776 -- gigi will emit an error if a copy is not legal, or generate
1777 -- the proper code.
1779 return False;
1781 -- For users of Starlet, we assume that the specification of by-
1782 -- reference mechanism is mandatory. This may lead to unaligned
1783 -- objects but at least for DEC legacy code it is known to work.
1784 -- The warning will alert users of this code that a problem may
1785 -- be lurking.
1787 elsif Mechanism (Formal) = By_Reference
1788 and then Is_Valued_Procedure (Scope (Formal))
1789 then
1790 Error_Msg_N
1791 ("by_reference actual may be misaligned??", Actual);
1792 return False;
1794 else
1795 return True;
1796 end if;
1797 end Is_Legal_Copy;
1799 --------------
1800 -- Make_Var --
1801 --------------
1803 function Make_Var (Actual : Node_Id) return Entity_Id is
1804 Var : Entity_Id;
1806 begin
1807 if Is_Entity_Name (Actual) then
1808 return Entity (Actual);
1810 else
1811 Var := Make_Temporary (Loc, 'T', Actual);
1813 N_Node :=
1814 Make_Object_Renaming_Declaration (Loc,
1815 Defining_Identifier => Var,
1816 Subtype_Mark =>
1817 New_Occurrence_Of (Etype (Actual), Loc),
1818 Name => Relocate_Node (Actual));
1820 Insert_Action (N, N_Node);
1821 return Var;
1822 end if;
1823 end Make_Var;
1825 -------------------------
1826 -- Reset_Packed_Prefix --
1827 -------------------------
1829 procedure Reset_Packed_Prefix is
1830 Pfx : Node_Id := Actual;
1831 begin
1832 loop
1833 Set_Analyzed (Pfx, False);
1834 exit when
1835 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1836 Pfx := Prefix (Pfx);
1837 end loop;
1838 end Reset_Packed_Prefix;
1840 -- Start of processing for Expand_Actuals
1842 begin
1843 Post_Call := New_List;
1845 Formal := First_Formal (Subp);
1846 Actual := First_Actual (N);
1847 while Present (Formal) loop
1848 E_Formal := Etype (Formal);
1849 E_Actual := Etype (Actual);
1851 -- Handle formals whose type comes from the limited view
1853 if From_Limited_With (E_Formal)
1854 and then Has_Non_Limited_View (E_Formal)
1855 then
1856 E_Formal := Non_Limited_View (E_Formal);
1857 end if;
1859 if Is_Scalar_Type (E_Formal)
1860 or else Nkind (Actual) = N_Slice
1861 then
1862 Check_Fortran_Logical;
1864 -- RM 6.4.1 (11)
1866 elsif Ekind (Formal) /= E_Out_Parameter then
1868 -- The unusual case of the current instance of a protected type
1869 -- requires special handling. This can only occur in the context
1870 -- of a call within the body of a protected operation.
1872 if Is_Entity_Name (Actual)
1873 and then Ekind (Entity (Actual)) = E_Protected_Type
1874 and then In_Open_Scopes (Entity (Actual))
1875 then
1876 if Scope (Subp) /= Entity (Actual) then
1877 Error_Msg_N
1878 ("operation outside protected type may not "
1879 & "call back its protected operations??", Actual);
1880 end if;
1882 Rewrite (Actual,
1883 Expand_Protected_Object_Reference (N, Entity (Actual)));
1884 end if;
1886 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1887 -- build-in-place function, then a temporary return object needs
1888 -- to be created and access to it must be passed to the function.
1889 -- Currently we limit such functions to those with inherently
1890 -- limited result subtypes, but eventually we plan to expand the
1891 -- functions that are treated as build-in-place to include other
1892 -- composite result types.
1894 if Is_Build_In_Place_Function_Call (Actual) then
1895 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1897 -- Ada 2005 (AI-318-02): Specialization of the previous case for
1898 -- actuals containing build-in-place function calls whose returned
1899 -- object covers interface types.
1901 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
1902 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
1903 end if;
1905 Apply_Constraint_Check (Actual, E_Formal);
1907 -- Out parameter case. No constraint checks on access type
1908 -- RM 6.4.1 (13)
1910 elsif Is_Access_Type (E_Formal) then
1911 null;
1913 -- RM 6.4.1 (14)
1915 elsif Has_Discriminants (Base_Type (E_Formal))
1916 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1917 then
1918 Apply_Constraint_Check (Actual, E_Formal);
1920 -- RM 6.4.1 (15)
1922 else
1923 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1924 end if;
1926 -- Processing for IN-OUT and OUT parameters
1928 if Ekind (Formal) /= E_In_Parameter then
1930 -- For type conversions of arrays, apply length/range checks
1932 if Is_Array_Type (E_Formal)
1933 and then Nkind (Actual) = N_Type_Conversion
1934 then
1935 if Is_Constrained (E_Formal) then
1936 Apply_Length_Check (Expression (Actual), E_Formal);
1937 else
1938 Apply_Range_Check (Expression (Actual), E_Formal);
1939 end if;
1940 end if;
1942 -- The actual denotes a variable which captures the value of an
1943 -- object for validation purposes. Add a copy-back to reflect any
1944 -- potential changes in value back into the original object.
1946 -- Var : ... := Object;
1947 -- if not Var'Valid then -- validity check
1948 -- Call (Var); -- modify var
1949 -- Object := Var; -- update Object
1951 -- This case is given higher priority because the subsequent check
1952 -- for type conversion may add an extra copy of the variable and
1953 -- prevent proper value propagation back in the original object.
1955 if Is_Validation_Variable_Reference (Actual) then
1956 Add_Validation_Call_By_Copy_Code (Actual);
1958 -- If argument is a type conversion for a type that is passed by
1959 -- copy, then we must pass the parameter by copy.
1961 elsif Nkind (Actual) = N_Type_Conversion
1962 and then
1963 (Is_Numeric_Type (E_Formal)
1964 or else Is_Access_Type (E_Formal)
1965 or else Is_Enumeration_Type (E_Formal)
1966 or else Is_Bit_Packed_Array (Etype (Formal))
1967 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1969 -- Also pass by copy if change of representation
1971 or else not Same_Representation
1972 (Etype (Formal),
1973 Etype (Expression (Actual))))
1974 then
1975 Add_Call_By_Copy_Code;
1977 -- References to components of bit-packed arrays are expanded
1978 -- at this point, rather than at the point of analysis of the
1979 -- actuals, to handle the expansion of the assignment to
1980 -- [in] out parameters.
1982 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1983 Add_Simple_Call_By_Copy_Code;
1985 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1986 -- because the back-end cannot cope with such objects. In other
1987 -- cases where alignment forces a copy, the back-end generates
1988 -- it properly. It should not be generated unconditionally in the
1989 -- front-end because it does not know precisely the alignment
1990 -- requirements of the target, and makes too conservative an
1991 -- estimate, leading to superfluous copies or spurious errors
1992 -- on by-reference parameters.
1994 elsif Nkind (Actual) = N_Selected_Component
1995 and then
1996 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1997 and then not Represented_As_Scalar (Etype (Formal))
1998 then
1999 Add_Simple_Call_By_Copy_Code;
2001 -- References to slices of bit-packed arrays are expanded
2003 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2004 Add_Call_By_Copy_Code;
2006 -- References to possibly unaligned slices of arrays are expanded
2008 elsif Is_Possibly_Unaligned_Slice (Actual) then
2009 Add_Call_By_Copy_Code;
2011 -- Deal with access types where the actual subtype and the
2012 -- formal subtype are not the same, requiring a check.
2014 -- It is necessary to exclude tagged types because of "downward
2015 -- conversion" errors.
2017 elsif Is_Access_Type (E_Formal)
2018 and then not Same_Type (E_Formal, E_Actual)
2019 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2020 then
2021 Add_Call_By_Copy_Code;
2023 -- If the actual is not a scalar and is marked for volatile
2024 -- treatment, whereas the formal is not volatile, then pass
2025 -- by copy unless it is a by-reference type.
2027 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
2028 -- because this is the enforcement of a language rule that applies
2029 -- only to "real" volatile variables, not e.g. to the address
2030 -- clause overlay case.
2032 elsif Is_Entity_Name (Actual)
2033 and then Is_Volatile (Entity (Actual))
2034 and then not Is_By_Reference_Type (E_Actual)
2035 and then not Is_Scalar_Type (Etype (Entity (Actual)))
2036 and then not Is_Volatile (E_Formal)
2037 then
2038 Add_Call_By_Copy_Code;
2040 elsif Nkind (Actual) = N_Indexed_Component
2041 and then Is_Entity_Name (Prefix (Actual))
2042 and then Has_Volatile_Components (Entity (Prefix (Actual)))
2043 then
2044 Add_Call_By_Copy_Code;
2046 -- Add call-by-copy code for the case of scalar out parameters
2047 -- when it is not known at compile time that the subtype of the
2048 -- formal is a subrange of the subtype of the actual (or vice
2049 -- versa for in out parameters), in order to get range checks
2050 -- on such actuals. (Maybe this case should be handled earlier
2051 -- in the if statement???)
2053 elsif Is_Scalar_Type (E_Formal)
2054 and then
2055 (not In_Subrange_Of (E_Formal, E_Actual)
2056 or else
2057 (Ekind (Formal) = E_In_Out_Parameter
2058 and then not In_Subrange_Of (E_Actual, E_Formal)))
2059 then
2060 -- Perhaps the setting back to False should be done within
2061 -- Add_Call_By_Copy_Code, since it could get set on other
2062 -- cases occurring above???
2064 if Do_Range_Check (Actual) then
2065 Set_Do_Range_Check (Actual, False);
2066 end if;
2068 Add_Call_By_Copy_Code;
2069 end if;
2071 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2072 -- by-reference parameters on exit from the call. If the actual
2073 -- is a derived type and the operation is inherited, the body
2074 -- of the operation will not contain a call to the predicate
2075 -- function, so it must be done explicitly after the call. Ditto
2076 -- if the actual is an entity of a predicated subtype.
2078 -- The rule refers to by-reference types, but a check is needed
2079 -- for by-copy types as well. That check is subsumed by the rule
2080 -- for subtype conversion on assignment, but we can generate the
2081 -- required check now.
2083 -- Note also that Subp may be either a subprogram entity for
2084 -- direct calls, or a type entity for indirect calls, which must
2085 -- be handled separately because the name does not denote an
2086 -- overloadable entity.
2088 By_Ref_Predicate_Check : declare
2089 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2090 Atyp : Entity_Id;
2092 function Is_Public_Subp return Boolean;
2093 -- Check whether the subprogram being called is a visible
2094 -- operation of the type of the actual. Used to determine
2095 -- whether an invariant check must be generated on the
2096 -- caller side.
2098 ---------------------
2099 -- Is_Public_Subp --
2100 ---------------------
2102 function Is_Public_Subp return Boolean is
2103 Pack : constant Entity_Id := Scope (Subp);
2104 Subp_Decl : Node_Id;
2106 begin
2107 if not Is_Subprogram (Subp) then
2108 return False;
2110 -- The operation may be inherited, or a primitive of the
2111 -- root type.
2113 elsif
2114 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
2115 N_Full_Type_Declaration)
2116 then
2117 Subp_Decl := Parent (Subp);
2119 else
2120 Subp_Decl := Unit_Declaration_Node (Subp);
2121 end if;
2123 return Ekind (Pack) = E_Package
2124 and then
2125 List_Containing (Subp_Decl) =
2126 Visible_Declarations
2127 (Specification (Unit_Declaration_Node (Pack)));
2128 end Is_Public_Subp;
2130 -- Start of processing for By_Ref_Predicate_Check
2132 begin
2133 if No (Aund) then
2134 Atyp := E_Actual;
2135 else
2136 Atyp := Aund;
2137 end if;
2139 if Has_Predicates (Atyp)
2140 and then Present (Predicate_Function (Atyp))
2142 -- Skip predicate checks for special cases
2144 and then Predicate_Tests_On_Arguments (Subp)
2145 then
2146 Append_To (Post_Call,
2147 Make_Predicate_Check (Atyp, Actual));
2148 end if;
2150 -- We generated caller-side invariant checks in two cases:
2152 -- a) when calling an inherited operation, where there is an
2153 -- implicit view conversion of the actual to the parent type.
2155 -- b) When the conversion is explicit
2157 -- We treat these cases separately because the required
2158 -- conversion for a) is added later when expanding the call.
2160 if Has_Invariants (Etype (Actual))
2161 and then
2162 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2163 then
2164 if Comes_From_Source (N) and then Is_Public_Subp then
2165 Append_To (Post_Call, Make_Invariant_Call (Actual));
2166 end if;
2168 elsif Nkind (Actual) = N_Type_Conversion
2169 and then Has_Invariants (Etype (Expression (Actual)))
2170 then
2171 if Comes_From_Source (N) and then Is_Public_Subp then
2172 Append_To (Post_Call,
2173 Make_Invariant_Call (Expression (Actual)));
2174 end if;
2175 end if;
2176 end By_Ref_Predicate_Check;
2178 -- Processing for IN parameters
2180 else
2181 -- For IN parameters in the bit-packed array case, we expand an
2182 -- indexed component (the circuit in Exp_Ch4 deliberately left
2183 -- indexed components appearing as actuals untouched, so that
2184 -- the special processing above for the OUT and IN OUT cases
2185 -- could be performed. We could make the test in Exp_Ch4 more
2186 -- complex and have it detect the parameter mode, but it is
2187 -- easier simply to handle all cases here.)
2189 if Nkind (Actual) = N_Indexed_Component
2190 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2191 then
2192 Reset_Packed_Prefix;
2193 Expand_Packed_Element_Reference (Actual);
2195 -- If we have a reference to a bit-packed array, we copy it, since
2196 -- the actual must be byte aligned.
2198 -- Is this really necessary in all cases???
2200 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2201 Add_Simple_Call_By_Copy_Code;
2203 -- If a non-scalar actual is possibly unaligned, we need a copy
2205 elsif Is_Possibly_Unaligned_Object (Actual)
2206 and then not Represented_As_Scalar (Etype (Formal))
2207 then
2208 Add_Simple_Call_By_Copy_Code;
2210 -- Similarly, we have to expand slices of packed arrays here
2211 -- because the result must be byte aligned.
2213 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2214 Add_Call_By_Copy_Code;
2216 -- Only processing remaining is to pass by copy if this is a
2217 -- reference to a possibly unaligned slice, since the caller
2218 -- expects an appropriately aligned argument.
2220 elsif Is_Possibly_Unaligned_Slice (Actual) then
2221 Add_Call_By_Copy_Code;
2223 -- An unusual case: a current instance of an enclosing task can be
2224 -- an actual, and must be replaced by a reference to self.
2226 elsif Is_Entity_Name (Actual)
2227 and then Is_Task_Type (Entity (Actual))
2228 then
2229 if In_Open_Scopes (Entity (Actual)) then
2230 Rewrite (Actual,
2231 (Make_Function_Call (Loc,
2232 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2233 Analyze (Actual);
2235 -- A task type cannot otherwise appear as an actual
2237 else
2238 raise Program_Error;
2239 end if;
2240 end if;
2241 end if;
2243 Next_Formal (Formal);
2244 Next_Actual (Actual);
2245 end loop;
2246 end Expand_Actuals;
2248 -----------------
2249 -- Expand_Call --
2250 -----------------
2252 procedure Expand_Call (N : Node_Id) is
2253 Post_Call : List_Id;
2254 begin
2255 Expand_Call_Helper (N, Post_Call);
2256 Insert_Post_Call_Actions (N, Post_Call);
2257 end Expand_Call;
2259 ------------------------
2260 -- Expand_Call_Helper --
2261 ------------------------
2263 -- This procedure handles expansion of function calls and procedure call
2264 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2265 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2267 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2268 -- Provide values of actuals for all formals in Extra_Formals list
2269 -- Replace "call" to enumeration literal function by literal itself
2270 -- Rewrite call to predefined operator as operator
2271 -- Replace actuals to in-out parameters that are numeric conversions,
2272 -- with explicit assignment to temporaries before and after the call.
2274 -- Note that the list of actuals has been filled with default expressions
2275 -- during semantic analysis of the call. Only the extra actuals required
2276 -- for the 'Constrained attribute and for accessibility checks are added
2277 -- at this point.
2279 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2280 Loc : constant Source_Ptr := Sloc (N);
2281 Call_Node : Node_Id := N;
2282 Extra_Actuals : List_Id := No_List;
2283 Prev : Node_Id := Empty;
2285 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2286 -- Adds one entry to the end of the actual parameter list. Used for
2287 -- default parameters and for extra actuals (for Extra_Formals). The
2288 -- argument is an N_Parameter_Association node.
2290 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2291 -- Adds an extra actual to the list of extra actuals. Expr is the
2292 -- expression for the value of the actual, EF is the entity for the
2293 -- extra formal.
2295 procedure Add_View_Conversion_Invariants
2296 (Formal : Entity_Id;
2297 Actual : Node_Id);
2298 -- Adds invariant checks for every intermediate type between the range
2299 -- of a view converted argument to its ancestor (from parent to child).
2301 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2302 -- Within an instance, a type derived from an untagged formal derived
2303 -- type inherits from the original parent, not from the actual. The
2304 -- current derivation mechanism has the derived type inherit from the
2305 -- actual, which is only correct outside of the instance. If the
2306 -- subprogram is inherited, we test for this particular case through a
2307 -- convoluted tree traversal before setting the proper subprogram to be
2308 -- called.
2310 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2311 -- Return true if E comes from an instance that is not yet frozen
2313 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2314 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2316 function New_Value (From : Node_Id) return Node_Id;
2317 -- From is the original Expression. New_Value is equivalent to a call
2318 -- to Duplicate_Subexpr with an explicit dereference when From is an
2319 -- access parameter.
2321 --------------------------
2322 -- Add_Actual_Parameter --
2323 --------------------------
2325 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2326 Actual_Expr : constant Node_Id :=
2327 Explicit_Actual_Parameter (Insert_Param);
2329 begin
2330 -- Case of insertion is first named actual
2332 if No (Prev) or else
2333 Nkind (Parent (Prev)) /= N_Parameter_Association
2334 then
2335 Set_Next_Named_Actual
2336 (Insert_Param, First_Named_Actual (Call_Node));
2337 Set_First_Named_Actual (Call_Node, Actual_Expr);
2339 if No (Prev) then
2340 if No (Parameter_Associations (Call_Node)) then
2341 Set_Parameter_Associations (Call_Node, New_List);
2342 end if;
2344 Append (Insert_Param, Parameter_Associations (Call_Node));
2346 else
2347 Insert_After (Prev, Insert_Param);
2348 end if;
2350 -- Case of insertion is not first named actual
2352 else
2353 Set_Next_Named_Actual
2354 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2355 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2356 Append (Insert_Param, Parameter_Associations (Call_Node));
2357 end if;
2359 Prev := Actual_Expr;
2360 end Add_Actual_Parameter;
2362 ----------------------
2363 -- Add_Extra_Actual --
2364 ----------------------
2366 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2367 Loc : constant Source_Ptr := Sloc (Expr);
2369 begin
2370 if Extra_Actuals = No_List then
2371 Extra_Actuals := New_List;
2372 Set_Parent (Extra_Actuals, Call_Node);
2373 end if;
2375 Append_To (Extra_Actuals,
2376 Make_Parameter_Association (Loc,
2377 Selector_Name => New_Occurrence_Of (EF, Loc),
2378 Explicit_Actual_Parameter => Expr));
2380 Analyze_And_Resolve (Expr, Etype (EF));
2382 if Nkind (Call_Node) = N_Function_Call then
2383 Set_Is_Accessibility_Actual (Parent (Expr));
2384 end if;
2385 end Add_Extra_Actual;
2387 ------------------------------------
2388 -- Add_View_Conversion_Invariants --
2389 ------------------------------------
2391 procedure Add_View_Conversion_Invariants
2392 (Formal : Entity_Id;
2393 Actual : Node_Id)
2395 Arg : Entity_Id;
2396 Curr_Typ : Entity_Id;
2397 Inv_Checks : List_Id;
2398 Par_Typ : Entity_Id;
2400 begin
2401 Inv_Checks := No_List;
2403 -- Extract the argument from a potentially nested set of view
2404 -- conversions.
2406 Arg := Actual;
2407 while Nkind (Arg) = N_Type_Conversion loop
2408 Arg := Expression (Arg);
2409 end loop;
2411 -- Move up the derivation chain starting with the type of the formal
2412 -- parameter down to the type of the actual object.
2414 Curr_Typ := Empty;
2415 Par_Typ := Etype (Arg);
2416 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2417 Curr_Typ := Par_Typ;
2419 if Has_Invariants (Curr_Typ)
2420 and then Present (Invariant_Procedure (Curr_Typ))
2421 then
2422 -- Verify the invariate of the current type. Generate:
2424 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2426 Prepend_New_To (Inv_Checks,
2427 Make_Procedure_Call_Statement (Loc,
2428 Name =>
2429 New_Occurrence_Of
2430 (Invariant_Procedure (Curr_Typ), Loc),
2431 Parameter_Associations => New_List (
2432 Make_Type_Conversion (Loc,
2433 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2434 Expression => New_Copy_Tree (Arg)))));
2435 end if;
2437 Par_Typ := Base_Type (Etype (Curr_Typ));
2438 end loop;
2440 if not Is_Empty_List (Inv_Checks) then
2441 Insert_Actions_After (N, Inv_Checks);
2442 end if;
2443 end Add_View_Conversion_Invariants;
2445 ---------------------------
2446 -- Inherited_From_Formal --
2447 ---------------------------
2449 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2450 Par : Entity_Id;
2451 Gen_Par : Entity_Id;
2452 Gen_Prim : Elist_Id;
2453 Elmt : Elmt_Id;
2454 Indic : Node_Id;
2456 begin
2457 -- If the operation is inherited, it is attached to the corresponding
2458 -- type derivation. If the parent in the derivation is a generic
2459 -- actual, it is a subtype of the actual, and we have to recover the
2460 -- original derived type declaration to find the proper parent.
2462 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2463 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2464 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2465 N_Derived_Type_Definition
2466 or else not In_Instance
2467 then
2468 return Empty;
2470 else
2471 Indic :=
2472 Subtype_Indication
2473 (Type_Definition (Original_Node (Parent (S))));
2475 if Nkind (Indic) = N_Subtype_Indication then
2476 Par := Entity (Subtype_Mark (Indic));
2477 else
2478 Par := Entity (Indic);
2479 end if;
2480 end if;
2482 if not Is_Generic_Actual_Type (Par)
2483 or else Is_Tagged_Type (Par)
2484 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2485 or else not In_Open_Scopes (Scope (Par))
2486 then
2487 return Empty;
2488 else
2489 Gen_Par := Generic_Parent_Type (Parent (Par));
2490 end if;
2492 -- If the actual has no generic parent type, the formal is not
2493 -- a formal derived type, so nothing to inherit.
2495 if No (Gen_Par) then
2496 return Empty;
2497 end if;
2499 -- If the generic parent type is still the generic type, this is a
2500 -- private formal, not a derived formal, and there are no operations
2501 -- inherited from the formal.
2503 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2504 return Empty;
2505 end if;
2507 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2509 Elmt := First_Elmt (Gen_Prim);
2510 while Present (Elmt) loop
2511 if Chars (Node (Elmt)) = Chars (S) then
2512 declare
2513 F1 : Entity_Id;
2514 F2 : Entity_Id;
2516 begin
2517 F1 := First_Formal (S);
2518 F2 := First_Formal (Node (Elmt));
2519 while Present (F1)
2520 and then Present (F2)
2521 loop
2522 if Etype (F1) = Etype (F2)
2523 or else Etype (F2) = Gen_Par
2524 then
2525 Next_Formal (F1);
2526 Next_Formal (F2);
2527 else
2528 Next_Elmt (Elmt);
2529 exit; -- not the right subprogram
2530 end if;
2532 return Node (Elmt);
2533 end loop;
2534 end;
2536 else
2537 Next_Elmt (Elmt);
2538 end if;
2539 end loop;
2541 raise Program_Error;
2542 end Inherited_From_Formal;
2544 --------------------------
2545 -- In_Unfrozen_Instance --
2546 --------------------------
2548 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2549 S : Entity_Id;
2551 begin
2552 S := E;
2553 while Present (S) and then S /= Standard_Standard loop
2554 if Is_Generic_Instance (S)
2555 and then Present (Freeze_Node (S))
2556 and then not Analyzed (Freeze_Node (S))
2557 then
2558 return True;
2559 end if;
2561 S := Scope (S);
2562 end loop;
2564 return False;
2565 end In_Unfrozen_Instance;
2567 -------------------------
2568 -- Is_Direct_Deep_Call --
2569 -------------------------
2571 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2572 begin
2573 if Is_TSS (Subp, TSS_Deep_Adjust)
2574 or else Is_TSS (Subp, TSS_Deep_Finalize)
2575 or else Is_TSS (Subp, TSS_Deep_Initialize)
2576 then
2577 declare
2578 Actual : Node_Id;
2579 Formal : Node_Id;
2581 begin
2582 Actual := First (Parameter_Associations (N));
2583 Formal := First_Formal (Subp);
2584 while Present (Actual)
2585 and then Present (Formal)
2586 loop
2587 if Nkind (Actual) = N_Identifier
2588 and then Is_Controlling_Actual (Actual)
2589 and then Etype (Actual) = Etype (Formal)
2590 then
2591 return True;
2592 end if;
2594 Next (Actual);
2595 Next_Formal (Formal);
2596 end loop;
2597 end;
2598 end if;
2600 return False;
2601 end Is_Direct_Deep_Call;
2603 ---------------
2604 -- New_Value --
2605 ---------------
2607 function New_Value (From : Node_Id) return Node_Id is
2608 Res : constant Node_Id := Duplicate_Subexpr (From);
2609 begin
2610 if Is_Access_Type (Etype (From)) then
2611 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2612 else
2613 return Res;
2614 end if;
2615 end New_Value;
2617 -- Local variables
2619 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2620 Actual : Node_Id;
2621 Formal : Entity_Id;
2622 Orig_Subp : Entity_Id := Empty;
2623 Param_Count : Natural := 0;
2624 Parent_Formal : Entity_Id;
2625 Parent_Subp : Entity_Id;
2626 Pref_Entity : Entity_Id;
2627 Scop : Entity_Id;
2628 Subp : Entity_Id;
2630 Prev_Orig : Node_Id;
2631 -- Original node for an actual, which may have been rewritten. If the
2632 -- actual is a function call that has been transformed from a selected
2633 -- component, the original node is unanalyzed. Otherwise, it carries
2634 -- semantic information used to generate additional actuals.
2636 CW_Interface_Formals_Present : Boolean := False;
2638 -- Start of processing for Expand_Call_Helper
2640 begin
2641 Post_Call := New_List;
2643 -- Expand the function or procedure call if the first actual has a
2644 -- declared dimension aspect, and the subprogram is declared in one
2645 -- of the dimension I/O packages.
2647 if Ada_Version >= Ada_2012
2648 and then
2649 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2650 and then Present (Parameter_Associations (Call_Node))
2651 then
2652 Expand_Put_Call_With_Symbol (Call_Node);
2653 end if;
2655 -- Ignore if previous error
2657 if Nkind (Call_Node) in N_Has_Etype
2658 and then Etype (Call_Node) = Any_Type
2659 then
2660 return;
2661 end if;
2663 -- Call using access to subprogram with explicit dereference
2665 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2666 Subp := Etype (Name (Call_Node));
2667 Parent_Subp := Empty;
2669 -- Case of call to simple entry, where the Name is a selected component
2670 -- whose prefix is the task, and whose selector name is the entry name
2672 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2673 Subp := Entity (Selector_Name (Name (Call_Node)));
2674 Parent_Subp := Empty;
2676 -- Case of call to member of entry family, where Name is an indexed
2677 -- component, with the prefix being a selected component giving the
2678 -- task and entry family name, and the index being the entry index.
2680 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2681 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2682 Parent_Subp := Empty;
2684 -- Normal case
2686 else
2687 Subp := Entity (Name (Call_Node));
2688 Parent_Subp := Alias (Subp);
2690 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2691 -- if we can tell that the first parameter cannot possibly be null.
2692 -- This improves efficiency by avoiding a run-time test.
2694 -- We do not do this if Raise_Exception_Always does not exist, which
2695 -- can happen in configurable run time profiles which provide only a
2696 -- Raise_Exception.
2698 if Is_RTE (Subp, RE_Raise_Exception)
2699 and then RTE_Available (RE_Raise_Exception_Always)
2700 then
2701 declare
2702 FA : constant Node_Id :=
2703 Original_Node (First_Actual (Call_Node));
2705 begin
2706 -- The case we catch is where the first argument is obtained
2707 -- using the Identity attribute (which must always be
2708 -- non-null).
2710 if Nkind (FA) = N_Attribute_Reference
2711 and then Attribute_Name (FA) = Name_Identity
2712 then
2713 Subp := RTE (RE_Raise_Exception_Always);
2714 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2715 end if;
2716 end;
2717 end if;
2719 if Ekind (Subp) = E_Entry then
2720 Parent_Subp := Empty;
2721 end if;
2722 end if;
2724 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2725 -- alternative in an asynchronous select or as an entry call in
2726 -- a conditional or timed select. Check whether the procedure call
2727 -- is a renaming of an entry and rewrite it as an entry call.
2729 if Ada_Version >= Ada_2005
2730 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2731 and then
2732 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2733 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2734 or else
2735 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2736 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2737 then
2738 declare
2739 Ren_Decl : Node_Id;
2740 Ren_Root : Entity_Id := Subp;
2742 begin
2743 -- This may be a chain of renamings, find the root
2745 if Present (Alias (Ren_Root)) then
2746 Ren_Root := Alias (Ren_Root);
2747 end if;
2749 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2750 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2752 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2753 Rewrite (Call_Node,
2754 Make_Entry_Call_Statement (Loc,
2755 Name =>
2756 New_Copy_Tree (Name (Ren_Decl)),
2757 Parameter_Associations =>
2758 New_Copy_List_Tree
2759 (Parameter_Associations (Call_Node))));
2761 return;
2762 end if;
2763 end if;
2764 end;
2765 end if;
2767 if Modify_Tree_For_C
2768 and then Nkind (Call_Node) = N_Function_Call
2769 and then Is_Entity_Name (Name (Call_Node))
2770 then
2771 declare
2772 Func_Id : constant Entity_Id :=
2773 Ultimate_Alias (Entity (Name (Call_Node)));
2774 begin
2775 -- When generating C code, transform a function call that returns
2776 -- a constrained array type into procedure form.
2778 if Rewritten_For_C (Func_Id) then
2780 -- For internally generated calls ensure that they reference
2781 -- the entity of the spec of the called function (needed since
2782 -- the expander may generate calls using the entity of their
2783 -- body). See for example Expand_Boolean_Operator().
2785 if not (Comes_From_Source (Call_Node))
2786 and then Nkind (Unit_Declaration_Node (Func_Id)) =
2787 N_Subprogram_Body
2788 then
2789 Set_Entity (Name (Call_Node),
2790 Corresponding_Function
2791 (Corresponding_Procedure (Func_Id)));
2792 end if;
2794 Rewrite_Function_Call_For_C (Call_Node);
2795 return;
2797 -- Also introduce a temporary for functions that return a record
2798 -- called within another procedure or function call, since records
2799 -- are passed by pointer in the generated C code, and we cannot
2800 -- take a pointer from a subprogram call.
2802 elsif Nkind (Parent (Call_Node)) in N_Subprogram_Call
2803 and then Is_Record_Type (Etype (Func_Id))
2804 then
2805 declare
2806 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
2807 Decl : Node_Id;
2809 begin
2810 -- Generate:
2811 -- Temp : ... := Func_Call (...);
2813 Decl :=
2814 Make_Object_Declaration (Loc,
2815 Defining_Identifier => Temp_Id,
2816 Object_Definition =>
2817 New_Occurrence_Of (Etype (Func_Id), Loc),
2818 Expression =>
2819 Make_Function_Call (Loc,
2820 Name =>
2821 New_Occurrence_Of (Func_Id, Loc),
2822 Parameter_Associations =>
2823 Parameter_Associations (Call_Node)));
2825 Insert_Action (Parent (Call_Node), Decl);
2826 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
2827 return;
2828 end;
2829 end if;
2830 end;
2831 end if;
2833 -- First step, compute extra actuals, corresponding to any Extra_Formals
2834 -- present. Note that we do not access Extra_Formals directly, instead
2835 -- we simply note the presence of the extra formals as we process the
2836 -- regular formals collecting corresponding actuals in Extra_Actuals.
2838 -- We also generate any required range checks for actuals for in formals
2839 -- as we go through the loop, since this is a convenient place to do it.
2840 -- (Though it seems that this would be better done in Expand_Actuals???)
2842 -- Special case: Thunks must not compute the extra actuals; they must
2843 -- just propagate to the target primitive their extra actuals.
2845 if Is_Thunk (Current_Scope)
2846 and then Thunk_Entity (Current_Scope) = Subp
2847 and then Present (Extra_Formals (Subp))
2848 then
2849 pragma Assert (Present (Extra_Formals (Current_Scope)));
2851 declare
2852 Target_Formal : Entity_Id;
2853 Thunk_Formal : Entity_Id;
2855 begin
2856 Target_Formal := Extra_Formals (Subp);
2857 Thunk_Formal := Extra_Formals (Current_Scope);
2858 while Present (Target_Formal) loop
2859 Add_Extra_Actual
2860 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
2861 EF => Thunk_Formal);
2863 Target_Formal := Extra_Formal (Target_Formal);
2864 Thunk_Formal := Extra_Formal (Thunk_Formal);
2865 end loop;
2867 while Is_Non_Empty_List (Extra_Actuals) loop
2868 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2869 end loop;
2871 Expand_Actuals (Call_Node, Subp, Post_Call);
2872 pragma Assert (Is_Empty_List (Post_Call));
2873 return;
2874 end;
2875 end if;
2877 Formal := First_Formal (Subp);
2878 Actual := First_Actual (Call_Node);
2879 Param_Count := 1;
2880 while Present (Formal) loop
2882 -- Generate range check if required
2884 if Do_Range_Check (Actual)
2885 and then Ekind (Formal) = E_In_Parameter
2886 then
2887 Generate_Range_Check
2888 (Actual, Etype (Formal), CE_Range_Check_Failed);
2889 end if;
2891 -- Prepare to examine current entry
2893 Prev := Actual;
2894 Prev_Orig := Original_Node (Prev);
2896 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2897 -- to expand it in a further round.
2899 CW_Interface_Formals_Present :=
2900 CW_Interface_Formals_Present
2901 or else
2902 (Is_Class_Wide_Type (Etype (Formal))
2903 and then Is_Interface (Etype (Etype (Formal))))
2904 or else
2905 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2906 and then Is_Class_Wide_Type (Directly_Designated_Type
2907 (Etype (Etype (Formal))))
2908 and then Is_Interface (Directly_Designated_Type
2909 (Etype (Etype (Formal)))));
2911 -- Create possible extra actual for constrained case. Usually, the
2912 -- extra actual is of the form actual'constrained, but since this
2913 -- attribute is only available for unconstrained records, TRUE is
2914 -- expanded if the type of the formal happens to be constrained (for
2915 -- instance when this procedure is inherited from an unconstrained
2916 -- record to a constrained one) or if the actual has no discriminant
2917 -- (its type is constrained). An exception to this is the case of a
2918 -- private type without discriminants. In this case we pass FALSE
2919 -- because the object has underlying discriminants with defaults.
2921 if Present (Extra_Constrained (Formal)) then
2922 if Ekind (Etype (Prev)) in Private_Kind
2923 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2924 then
2925 Add_Extra_Actual
2926 (Expr => New_Occurrence_Of (Standard_False, Loc),
2927 EF => Extra_Constrained (Formal));
2929 elsif Is_Constrained (Etype (Formal))
2930 or else not Has_Discriminants (Etype (Prev))
2931 then
2932 Add_Extra_Actual
2933 (Expr => New_Occurrence_Of (Standard_True, Loc),
2934 EF => Extra_Constrained (Formal));
2936 -- Do not produce extra actuals for Unchecked_Union parameters.
2937 -- Jump directly to the end of the loop.
2939 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2940 goto Skip_Extra_Actual_Generation;
2942 else
2943 -- If the actual is a type conversion, then the constrained
2944 -- test applies to the actual, not the target type.
2946 declare
2947 Act_Prev : Node_Id;
2949 begin
2950 -- Test for unchecked conversions as well, which can occur
2951 -- as out parameter actuals on calls to stream procedures.
2953 Act_Prev := Prev;
2954 while Nkind_In (Act_Prev, N_Type_Conversion,
2955 N_Unchecked_Type_Conversion)
2956 loop
2957 Act_Prev := Expression (Act_Prev);
2958 end loop;
2960 -- If the expression is a conversion of a dereference, this
2961 -- is internally generated code that manipulates addresses,
2962 -- e.g. when building interface tables. No check should
2963 -- occur in this case, and the discriminated object is not
2964 -- directly a hand.
2966 if not Comes_From_Source (Actual)
2967 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2968 and then Nkind (Act_Prev) = N_Explicit_Dereference
2969 then
2970 Add_Extra_Actual
2971 (Expr => New_Occurrence_Of (Standard_False, Loc),
2972 EF => Extra_Constrained (Formal));
2974 else
2975 Add_Extra_Actual
2976 (Expr =>
2977 Make_Attribute_Reference (Sloc (Prev),
2978 Prefix =>
2979 Duplicate_Subexpr_No_Checks
2980 (Act_Prev, Name_Req => True),
2981 Attribute_Name => Name_Constrained),
2982 EF => Extra_Constrained (Formal));
2983 end if;
2984 end;
2985 end if;
2986 end if;
2988 -- Create possible extra actual for accessibility level
2990 if Present (Extra_Accessibility (Formal)) then
2992 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2993 -- attribute, then the original actual may be an aliased object
2994 -- occurring as the prefix in a call using "Object.Operation"
2995 -- notation. In that case we must pass the level of the object,
2996 -- so Prev_Orig is reset to Prev and the attribute will be
2997 -- processed by the code for Access attributes further below.
2999 if Prev_Orig /= Prev
3000 and then Nkind (Prev) = N_Attribute_Reference
3001 and then
3002 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
3003 and then Is_Aliased_View (Prev_Orig)
3004 then
3005 Prev_Orig := Prev;
3007 -- If the actual is a formal of an enclosing subprogram it is
3008 -- the right entity, even if it is a rewriting. This happens
3009 -- when the call is within an inherited condition or predicate.
3011 elsif Is_Entity_Name (Actual)
3012 and then Is_Formal (Entity (Actual))
3013 and then In_Open_Scopes (Scope (Entity (Actual)))
3014 then
3015 Prev_Orig := Prev;
3017 elsif Nkind (Prev_Orig) = N_Type_Conversion then
3018 Prev_Orig := Expression (Prev_Orig);
3019 end if;
3021 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3022 -- accessibility levels.
3024 if Is_Thunk (Current_Scope) then
3025 declare
3026 Parm_Ent : Entity_Id;
3028 begin
3029 if Is_Controlling_Actual (Actual) then
3031 -- Find the corresponding actual of the thunk
3033 Parm_Ent := First_Entity (Current_Scope);
3034 for J in 2 .. Param_Count loop
3035 Next_Entity (Parm_Ent);
3036 end loop;
3038 -- Handle unchecked conversion of access types generated
3039 -- in thunks (cf. Expand_Interface_Thunk).
3041 elsif Is_Access_Type (Etype (Actual))
3042 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3043 then
3044 Parm_Ent := Entity (Expression (Actual));
3046 else pragma Assert (Is_Entity_Name (Actual));
3047 Parm_Ent := Entity (Actual);
3048 end if;
3050 Add_Extra_Actual
3051 (Expr =>
3052 New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
3053 EF => Extra_Accessibility (Formal));
3054 end;
3056 elsif Is_Entity_Name (Prev_Orig) then
3058 -- When passing an access parameter, or a renaming of an access
3059 -- parameter, as the actual to another access parameter we need
3060 -- to pass along the actual's own access level parameter. This
3061 -- is done if we are within the scope of the formal access
3062 -- parameter (if this is an inlined body the extra formal is
3063 -- irrelevant).
3065 if (Is_Formal (Entity (Prev_Orig))
3066 or else
3067 (Present (Renamed_Object (Entity (Prev_Orig)))
3068 and then
3069 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3070 and then
3071 Is_Formal
3072 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3073 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3074 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3075 then
3076 declare
3077 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3079 begin
3080 pragma Assert (Present (Parm_Ent));
3082 if Present (Extra_Accessibility (Parm_Ent)) then
3083 Add_Extra_Actual
3084 (Expr =>
3085 New_Occurrence_Of
3086 (Extra_Accessibility (Parm_Ent), Loc),
3087 EF => Extra_Accessibility (Formal));
3089 -- If the actual access parameter does not have an
3090 -- associated extra formal providing its scope level,
3091 -- then treat the actual as having library-level
3092 -- accessibility.
3094 else
3095 Add_Extra_Actual
3096 (Expr =>
3097 Make_Integer_Literal (Loc,
3098 Intval => Scope_Depth (Standard_Standard)),
3099 EF => Extra_Accessibility (Formal));
3100 end if;
3101 end;
3103 -- The actual is a normal access value, so just pass the level
3104 -- of the actual's access type.
3106 else
3107 Add_Extra_Actual
3108 (Expr => Dynamic_Accessibility_Level (Prev_Orig),
3109 EF => Extra_Accessibility (Formal));
3110 end if;
3112 -- If the actual is an access discriminant, then pass the level
3113 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3115 elsif Nkind (Prev_Orig) = N_Selected_Component
3116 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3117 E_Discriminant
3118 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3119 E_Anonymous_Access_Type
3120 then
3121 Add_Extra_Actual
3122 (Expr =>
3123 Make_Integer_Literal (Loc,
3124 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3125 EF => Extra_Accessibility (Formal));
3127 -- All other cases
3129 else
3130 case Nkind (Prev_Orig) is
3131 when N_Attribute_Reference =>
3132 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3134 -- For X'Access, pass on the level of the prefix X
3136 when Attribute_Access =>
3138 -- Accessibility level of S'Access is that of A
3140 Prev_Orig := Prefix (Prev_Orig);
3142 -- If the expression is a view conversion, the
3143 -- accessibility level is that of the expression.
3145 if Nkind (Original_Node (Prev_Orig)) =
3146 N_Type_Conversion
3147 and then
3148 Nkind (Expression (Original_Node (Prev_Orig))) =
3149 N_Explicit_Dereference
3150 then
3151 Prev_Orig :=
3152 Expression (Original_Node (Prev_Orig));
3153 end if;
3155 -- If this is an Access attribute applied to the
3156 -- the current instance object passed to a type
3157 -- initialization procedure, then use the level
3158 -- of the type itself. This is not really correct,
3159 -- as there should be an extra level parameter
3160 -- passed in with _init formals (only in the case
3161 -- where the type is immutably limited), but we
3162 -- don't have an easy way currently to create such
3163 -- an extra formal (init procs aren't ever frozen).
3164 -- For now we just use the level of the type,
3165 -- which may be too shallow, but that works better
3166 -- than passing Object_Access_Level of the type,
3167 -- which can be one level too deep in some cases.
3168 -- ???
3170 -- A further case that requires special handling
3171 -- is the common idiom E.all'access. If E is a
3172 -- formal of the enclosing subprogram, the
3173 -- accessibility of the expression is that of E.
3175 if Is_Entity_Name (Prev_Orig) then
3176 Pref_Entity := Entity (Prev_Orig);
3178 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3179 and then Is_Entity_Name (Prefix (Prev_Orig))
3180 then
3181 Pref_Entity := Entity (Prefix ((Prev_Orig)));
3183 else
3184 Pref_Entity := Empty;
3185 end if;
3187 if Is_Entity_Name (Prev_Orig)
3188 and then Is_Type (Entity (Prev_Orig))
3189 then
3190 Add_Extra_Actual
3191 (Expr =>
3192 Make_Integer_Literal (Loc,
3193 Intval =>
3194 Type_Access_Level (Pref_Entity)),
3195 EF => Extra_Accessibility (Formal));
3197 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3198 and then Present (Pref_Entity)
3199 and then Is_Formal (Pref_Entity)
3200 and then Present
3201 (Extra_Accessibility (Pref_Entity))
3202 then
3203 Add_Extra_Actual
3204 (Expr =>
3205 New_Occurrence_Of
3206 (Extra_Accessibility (Pref_Entity), Loc),
3207 EF => Extra_Accessibility (Formal));
3209 else
3210 Add_Extra_Actual
3211 (Expr =>
3212 Make_Integer_Literal (Loc,
3213 Intval =>
3214 Object_Access_Level (Prev_Orig)),
3215 EF => Extra_Accessibility (Formal));
3216 end if;
3218 -- Treat the unchecked attributes as library-level
3220 when Attribute_Unchecked_Access
3221 | Attribute_Unrestricted_Access
3223 Add_Extra_Actual
3224 (Expr =>
3225 Make_Integer_Literal (Loc,
3226 Intval => Scope_Depth (Standard_Standard)),
3227 EF => Extra_Accessibility (Formal));
3229 -- No other cases of attributes returning access
3230 -- values that can be passed to access parameters.
3232 when others =>
3233 raise Program_Error;
3235 end case;
3237 -- For allocators we pass the level of the execution of the
3238 -- called subprogram, which is one greater than the current
3239 -- scope level.
3241 when N_Allocator =>
3242 Add_Extra_Actual
3243 (Expr =>
3244 Make_Integer_Literal (Loc,
3245 Intval => Scope_Depth (Current_Scope) + 1),
3246 EF => Extra_Accessibility (Formal));
3248 -- For most other cases we simply pass the level of the
3249 -- actual's access type. The type is retrieved from
3250 -- Prev rather than Prev_Orig, because in some cases
3251 -- Prev_Orig denotes an original expression that has
3252 -- not been analyzed.
3254 when others =>
3255 Add_Extra_Actual
3256 (Expr => Dynamic_Accessibility_Level (Prev),
3257 EF => Extra_Accessibility (Formal));
3258 end case;
3259 end if;
3260 end if;
3262 -- Perform the check of 4.6(49) that prevents a null value from being
3263 -- passed as an actual to an access parameter. Note that the check
3264 -- is elided in the common cases of passing an access attribute or
3265 -- access parameter as an actual. Also, we currently don't enforce
3266 -- this check for expander-generated actuals and when -gnatdj is set.
3268 if Ada_Version >= Ada_2005 then
3270 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3271 -- the intent of 6.4.1(13) is that null-exclusion checks should
3272 -- not be done for 'out' parameters, even though it refers only
3273 -- to constraint checks, and a null_exclusion is not a constraint.
3274 -- Note that AI05-0196-1 corrects this mistake in the RM.
3276 if Is_Access_Type (Etype (Formal))
3277 and then Can_Never_Be_Null (Etype (Formal))
3278 and then Ekind (Formal) /= E_Out_Parameter
3279 and then Nkind (Prev) /= N_Raise_Constraint_Error
3280 and then (Known_Null (Prev)
3281 or else not Can_Never_Be_Null (Etype (Prev)))
3282 then
3283 Install_Null_Excluding_Check (Prev);
3284 end if;
3286 -- Ada_Version < Ada_2005
3288 else
3289 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3290 or else Access_Checks_Suppressed (Subp)
3291 then
3292 null;
3294 elsif Debug_Flag_J then
3295 null;
3297 elsif not Comes_From_Source (Prev) then
3298 null;
3300 elsif Is_Entity_Name (Prev)
3301 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3302 then
3303 null;
3305 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3306 null;
3308 else
3309 Install_Null_Excluding_Check (Prev);
3310 end if;
3311 end if;
3313 -- Perform appropriate validity checks on parameters that
3314 -- are entities.
3316 if Validity_Checks_On then
3317 if (Ekind (Formal) = E_In_Parameter
3318 and then Validity_Check_In_Params)
3319 or else
3320 (Ekind (Formal) = E_In_Out_Parameter
3321 and then Validity_Check_In_Out_Params)
3322 then
3323 -- If the actual is an indexed component of a packed type (or
3324 -- is an indexed or selected component whose prefix recursively
3325 -- meets this condition), it has not been expanded yet. It will
3326 -- be copied in the validity code that follows, and has to be
3327 -- expanded appropriately, so reanalyze it.
3329 -- What we do is just to unset analyzed bits on prefixes till
3330 -- we reach something that does not have a prefix.
3332 declare
3333 Nod : Node_Id;
3335 begin
3336 Nod := Actual;
3337 while Nkind_In (Nod, N_Indexed_Component,
3338 N_Selected_Component)
3339 loop
3340 Set_Analyzed (Nod, False);
3341 Nod := Prefix (Nod);
3342 end loop;
3343 end;
3345 Ensure_Valid (Actual);
3346 end if;
3347 end if;
3349 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3350 -- since this is a left side reference. We only do this for calls
3351 -- from the source program since we assume that compiler generated
3352 -- calls explicitly generate any required checks. We also need it
3353 -- only if we are doing standard validity checks, since clearly it is
3354 -- not needed if validity checks are off, and in subscript validity
3355 -- checking mode, all indexed components are checked with a call
3356 -- directly from Expand_N_Indexed_Component.
3358 if Comes_From_Source (Call_Node)
3359 and then Ekind (Formal) /= E_In_Parameter
3360 and then Validity_Checks_On
3361 and then Validity_Check_Default
3362 and then not Validity_Check_Subscripts
3363 then
3364 Check_Valid_Lvalue_Subscripts (Actual);
3365 end if;
3367 -- Mark any scalar OUT parameter that is a simple variable as no
3368 -- longer known to be valid (unless the type is always valid). This
3369 -- reflects the fact that if an OUT parameter is never set in a
3370 -- procedure, then it can become invalid on the procedure return.
3372 if Ekind (Formal) = E_Out_Parameter
3373 and then Is_Entity_Name (Actual)
3374 and then Ekind (Entity (Actual)) = E_Variable
3375 and then not Is_Known_Valid (Etype (Actual))
3376 then
3377 Set_Is_Known_Valid (Entity (Actual), False);
3378 end if;
3380 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3381 -- clear current values, since they can be clobbered. We are probably
3382 -- doing this in more places than we need to, but better safe than
3383 -- sorry when it comes to retaining bad current values.
3385 if Ekind (Formal) /= E_In_Parameter
3386 and then Is_Entity_Name (Actual)
3387 and then Present (Entity (Actual))
3388 then
3389 declare
3390 Ent : constant Entity_Id := Entity (Actual);
3391 Sav : Node_Id;
3393 begin
3394 -- For an OUT or IN OUT parameter that is an assignable entity,
3395 -- we do not want to clobber the Last_Assignment field, since
3396 -- if it is set, it was precisely because it is indeed an OUT
3397 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3398 -- since the subprogram could have returned in invalid value.
3400 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3401 and then Is_Assignable (Ent)
3402 then
3403 Sav := Last_Assignment (Ent);
3404 Kill_Current_Values (Ent);
3405 Set_Last_Assignment (Ent, Sav);
3406 Set_Is_Known_Valid (Ent, False);
3408 -- For all other cases, just kill the current values
3410 else
3411 Kill_Current_Values (Ent);
3412 end if;
3413 end;
3414 end if;
3416 -- If the formal is class wide and the actual is an aggregate, force
3417 -- evaluation so that the back end who does not know about class-wide
3418 -- type, does not generate a temporary of the wrong size.
3420 if not Is_Class_Wide_Type (Etype (Formal)) then
3421 null;
3423 elsif Nkind (Actual) = N_Aggregate
3424 or else (Nkind (Actual) = N_Qualified_Expression
3425 and then Nkind (Expression (Actual)) = N_Aggregate)
3426 then
3427 Force_Evaluation (Actual);
3428 end if;
3430 -- In a remote call, if the formal is of a class-wide type, check
3431 -- that the actual meets the requirements described in E.4(18).
3433 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3434 Insert_Action (Actual,
3435 Make_Transportable_Check (Loc,
3436 Duplicate_Subexpr_Move_Checks (Actual)));
3437 end if;
3439 -- Perform invariant checks for all intermediate types in a view
3440 -- conversion after successful return from a call that passes the
3441 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3442 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3443 -- generating spurious checks on complex expansion such as object
3444 -- initialization through an extension aggregate.
3446 if Comes_From_Source (N)
3447 and then Ekind (Formal) /= E_In_Parameter
3448 and then Nkind (Actual) = N_Type_Conversion
3449 then
3450 Add_View_Conversion_Invariants (Formal, Actual);
3451 end if;
3453 -- Generating C the initialization of an allocator is performed by
3454 -- means of individual statements, and hence it must be done before
3455 -- the call.
3457 if Modify_Tree_For_C
3458 and then Nkind (Actual) = N_Allocator
3459 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3460 then
3461 Remove_Side_Effects (Actual);
3462 end if;
3464 -- This label is required when skipping extra actual generation for
3465 -- Unchecked_Union parameters.
3467 <<Skip_Extra_Actual_Generation>>
3469 Param_Count := Param_Count + 1;
3470 Next_Actual (Actual);
3471 Next_Formal (Formal);
3472 end loop;
3474 -- If we are calling an Ada 2012 function which needs to have the
3475 -- "accessibility level determined by the point of call" (AI05-0234)
3476 -- passed in to it, then pass it in.
3478 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3479 and then
3480 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3481 then
3482 declare
3483 Ancestor : Node_Id := Parent (Call_Node);
3484 Level : Node_Id := Empty;
3485 Defer : Boolean := False;
3487 begin
3488 -- Unimplemented: if Subp returns an anonymous access type, then
3490 -- a) if the call is the operand of an explict conversion, then
3491 -- the target type of the conversion (a named access type)
3492 -- determines the accessibility level pass in;
3494 -- b) if the call defines an access discriminant of an object
3495 -- (e.g., the discriminant of an object being created by an
3496 -- allocator, or the discriminant of a function result),
3497 -- then the accessibility level to pass in is that of the
3498 -- discriminated object being initialized).
3500 -- ???
3502 while Nkind (Ancestor) = N_Qualified_Expression
3503 loop
3504 Ancestor := Parent (Ancestor);
3505 end loop;
3507 case Nkind (Ancestor) is
3508 when N_Allocator =>
3510 -- At this point, we'd like to assign
3512 -- Level := Dynamic_Accessibility_Level (Ancestor);
3514 -- but Etype of Ancestor may not have been set yet,
3515 -- so that doesn't work.
3517 -- Handle this later in Expand_Allocator_Expression.
3519 Defer := True;
3521 when N_Object_Declaration
3522 | N_Object_Renaming_Declaration
3524 declare
3525 Def_Id : constant Entity_Id :=
3526 Defining_Identifier (Ancestor);
3528 begin
3529 if Is_Return_Object (Def_Id) then
3530 if Present (Extra_Accessibility_Of_Result
3531 (Return_Applies_To (Scope (Def_Id))))
3532 then
3533 -- Pass along value that was passed in if the
3534 -- routine we are returning from also has an
3535 -- Accessibility_Of_Result formal.
3537 Level :=
3538 New_Occurrence_Of
3539 (Extra_Accessibility_Of_Result
3540 (Return_Applies_To (Scope (Def_Id))), Loc);
3541 end if;
3542 else
3543 Level :=
3544 Make_Integer_Literal (Loc,
3545 Intval => Object_Access_Level (Def_Id));
3546 end if;
3547 end;
3549 when N_Simple_Return_Statement =>
3550 if Present (Extra_Accessibility_Of_Result
3551 (Return_Applies_To
3552 (Return_Statement_Entity (Ancestor))))
3553 then
3554 -- Pass along value that was passed in if the returned
3555 -- routine also has an Accessibility_Of_Result formal.
3557 Level :=
3558 New_Occurrence_Of
3559 (Extra_Accessibility_Of_Result
3560 (Return_Applies_To
3561 (Return_Statement_Entity (Ancestor))), Loc);
3562 end if;
3564 when others =>
3565 null;
3566 end case;
3568 if not Defer then
3569 if not Present (Level) then
3571 -- The "innermost master that evaluates the function call".
3573 -- ??? - Should we use Integer'Last here instead in order
3574 -- to deal with (some of) the problems associated with
3575 -- calls to subps whose enclosing scope is unknown (e.g.,
3576 -- Anon_Access_To_Subp_Param.all)?
3578 Level :=
3579 Make_Integer_Literal (Loc,
3580 Intval => Scope_Depth (Current_Scope) + 1);
3581 end if;
3583 Add_Extra_Actual
3584 (Expr => Level,
3585 EF =>
3586 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3587 end if;
3588 end;
3589 end if;
3591 -- If we are expanding the RHS of an assignment we need to check if tag
3592 -- propagation is needed. You might expect this processing to be in
3593 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3594 -- assignment might be transformed to a declaration for an unconstrained
3595 -- value if the expression is classwide.
3597 if Nkind (Call_Node) = N_Function_Call
3598 and then Is_Tag_Indeterminate (Call_Node)
3599 and then Is_Entity_Name (Name (Call_Node))
3600 then
3601 declare
3602 Ass : Node_Id := Empty;
3604 begin
3605 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3606 Ass := Parent (Call_Node);
3608 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3609 and then Nkind (Parent (Parent (Call_Node))) =
3610 N_Assignment_Statement
3611 then
3612 Ass := Parent (Parent (Call_Node));
3614 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3615 and then Nkind (Parent (Parent (Call_Node))) =
3616 N_Assignment_Statement
3617 then
3618 Ass := Parent (Parent (Call_Node));
3619 end if;
3621 if Present (Ass)
3622 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3623 then
3624 if Is_Access_Type (Etype (Call_Node)) then
3625 if Designated_Type (Etype (Call_Node)) /=
3626 Root_Type (Etype (Name (Ass)))
3627 then
3628 Error_Msg_NE
3629 ("tag-indeterminate expression must have designated "
3630 & "type& (RM 5.2 (6))",
3631 Call_Node, Root_Type (Etype (Name (Ass))));
3632 else
3633 Propagate_Tag (Name (Ass), Call_Node);
3634 end if;
3636 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3637 Error_Msg_NE
3638 ("tag-indeterminate expression must have type & "
3639 & "(RM 5.2 (6))",
3640 Call_Node, Root_Type (Etype (Name (Ass))));
3642 else
3643 Propagate_Tag (Name (Ass), Call_Node);
3644 end if;
3646 -- The call will be rewritten as a dispatching call, and
3647 -- expanded as such.
3649 return;
3650 end if;
3651 end;
3652 end if;
3654 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3655 -- it to point to the correct secondary virtual table
3657 if Nkind (Call_Node) in N_Subprogram_Call
3658 and then CW_Interface_Formals_Present
3659 then
3660 Expand_Interface_Actuals (Call_Node);
3661 end if;
3663 -- Deals with Dispatch_Call if we still have a call, before expanding
3664 -- extra actuals since this will be done on the re-analysis of the
3665 -- dispatching call. Note that we do not try to shorten the actual list
3666 -- for a dispatching call, it would not make sense to do so. Expansion
3667 -- of dispatching calls is suppressed for VM targets, because the VM
3668 -- back-ends directly handle the generation of dispatching calls and
3669 -- would have to undo any expansion to an indirect call.
3671 if Nkind (Call_Node) in N_Subprogram_Call
3672 and then Present (Controlling_Argument (Call_Node))
3673 then
3674 declare
3675 Call_Typ : constant Entity_Id := Etype (Call_Node);
3676 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3677 Eq_Prim_Op : Entity_Id := Empty;
3678 New_Call : Node_Id;
3679 Param : Node_Id;
3680 Prev_Call : Node_Id;
3682 begin
3683 if not Is_Limited_Type (Typ) then
3684 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3685 end if;
3687 if Tagged_Type_Expansion then
3688 Expand_Dispatching_Call (Call_Node);
3690 -- The following return is worrisome. Is it really OK to skip
3691 -- all remaining processing in this procedure ???
3693 return;
3695 -- VM targets
3697 else
3698 Apply_Tag_Checks (Call_Node);
3700 -- If this is a dispatching "=", we must first compare the
3701 -- tags so we generate: x.tag = y.tag and then x = y
3703 if Subp = Eq_Prim_Op then
3705 -- Mark the node as analyzed to avoid reanalyzing this
3706 -- dispatching call (which would cause a never-ending loop)
3708 Prev_Call := Relocate_Node (Call_Node);
3709 Set_Analyzed (Prev_Call);
3711 Param := First_Actual (Call_Node);
3712 New_Call :=
3713 Make_And_Then (Loc,
3714 Left_Opnd =>
3715 Make_Op_Eq (Loc,
3716 Left_Opnd =>
3717 Make_Selected_Component (Loc,
3718 Prefix => New_Value (Param),
3719 Selector_Name =>
3720 New_Occurrence_Of
3721 (First_Tag_Component (Typ), Loc)),
3723 Right_Opnd =>
3724 Make_Selected_Component (Loc,
3725 Prefix =>
3726 Unchecked_Convert_To (Typ,
3727 New_Value (Next_Actual (Param))),
3728 Selector_Name =>
3729 New_Occurrence_Of
3730 (First_Tag_Component (Typ), Loc))),
3731 Right_Opnd => Prev_Call);
3733 Rewrite (Call_Node, New_Call);
3735 Analyze_And_Resolve
3736 (Call_Node, Call_Typ, Suppress => All_Checks);
3737 end if;
3739 -- Expansion of a dispatching call results in an indirect call,
3740 -- which in turn causes current values to be killed (see
3741 -- Resolve_Call), so on VM targets we do the call here to
3742 -- ensure consistent warnings between VM and non-VM targets.
3744 Kill_Current_Values;
3745 end if;
3747 -- If this is a dispatching "=" then we must update the reference
3748 -- to the call node because we generated:
3749 -- x.tag = y.tag and then x = y
3751 if Subp = Eq_Prim_Op then
3752 Call_Node := Right_Opnd (Call_Node);
3753 end if;
3754 end;
3755 end if;
3757 -- Similarly, expand calls to RCI subprograms on which pragma
3758 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3759 -- later. Do this only when the call comes from source since we
3760 -- do not want such a rewriting to occur in expanded code.
3762 if Is_All_Remote_Call (Call_Node) then
3763 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3765 -- Similarly, do not add extra actuals for an entry call whose entity
3766 -- is a protected procedure, or for an internal protected subprogram
3767 -- call, because it will be rewritten as a protected subprogram call
3768 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3770 elsif Is_Protected_Type (Scope (Subp))
3771 and then (Ekind (Subp) = E_Procedure
3772 or else Ekind (Subp) = E_Function)
3773 then
3774 null;
3776 -- During that loop we gathered the extra actuals (the ones that
3777 -- correspond to Extra_Formals), so now they can be appended.
3779 else
3780 while Is_Non_Empty_List (Extra_Actuals) loop
3781 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3782 end loop;
3783 end if;
3785 -- At this point we have all the actuals, so this is the point at which
3786 -- the various expansion activities for actuals is carried out.
3788 Expand_Actuals (Call_Node, Subp, Post_Call);
3790 -- Verify that the actuals do not share storage. This check must be done
3791 -- on the caller side rather that inside the subprogram to avoid issues
3792 -- of parameter passing.
3794 if Check_Aliasing_Of_Parameters then
3795 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3796 end if;
3798 -- If the subprogram is a renaming, or if it is inherited, replace it in
3799 -- the call with the name of the actual subprogram being called. If this
3800 -- is a dispatching call, the run-time decides what to call. The Alias
3801 -- attribute does not apply to entries.
3803 if Nkind (Call_Node) /= N_Entry_Call_Statement
3804 and then No (Controlling_Argument (Call_Node))
3805 and then Present (Parent_Subp)
3806 and then not Is_Direct_Deep_Call (Subp)
3807 then
3808 if Present (Inherited_From_Formal (Subp)) then
3809 Parent_Subp := Inherited_From_Formal (Subp);
3810 else
3811 Parent_Subp := Ultimate_Alias (Parent_Subp);
3812 end if;
3814 -- The below setting of Entity is suspect, see F109-018 discussion???
3816 Set_Entity (Name (Call_Node), Parent_Subp);
3818 if Is_Abstract_Subprogram (Parent_Subp)
3819 and then not In_Instance
3820 then
3821 Error_Msg_NE
3822 ("cannot call abstract subprogram &!",
3823 Name (Call_Node), Parent_Subp);
3824 end if;
3826 -- Inspect all formals of derived subprogram Subp. Compare parameter
3827 -- types with the parent subprogram and check whether an actual may
3828 -- need a type conversion to the corresponding formal of the parent
3829 -- subprogram.
3831 -- Not clear whether intrinsic subprograms need such conversions. ???
3833 if not Is_Intrinsic_Subprogram (Parent_Subp)
3834 or else Is_Generic_Instance (Parent_Subp)
3835 then
3836 declare
3837 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3838 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3839 -- and resolve the newly generated construct.
3841 -------------
3842 -- Convert --
3843 -------------
3845 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3846 begin
3847 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3848 Analyze (Act);
3849 Resolve (Act, Typ);
3850 end Convert;
3852 -- Local variables
3854 Actual_Typ : Entity_Id;
3855 Formal_Typ : Entity_Id;
3856 Parent_Typ : Entity_Id;
3858 begin
3859 Actual := First_Actual (Call_Node);
3860 Formal := First_Formal (Subp);
3861 Parent_Formal := First_Formal (Parent_Subp);
3862 while Present (Formal) loop
3863 Actual_Typ := Etype (Actual);
3864 Formal_Typ := Etype (Formal);
3865 Parent_Typ := Etype (Parent_Formal);
3867 -- For an IN parameter of a scalar type, the parent formal
3868 -- type and derived formal type differ or the parent formal
3869 -- type and actual type do not match statically.
3871 if Is_Scalar_Type (Formal_Typ)
3872 and then Ekind (Formal) = E_In_Parameter
3873 and then Formal_Typ /= Parent_Typ
3874 and then
3875 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3876 and then not Raises_Constraint_Error (Actual)
3877 then
3878 Convert (Actual, Parent_Typ);
3879 Enable_Range_Check (Actual);
3881 -- If the actual has been marked as requiring a range
3882 -- check, then generate it here.
3884 if Do_Range_Check (Actual) then
3885 Generate_Range_Check
3886 (Actual, Etype (Formal), CE_Range_Check_Failed);
3887 end if;
3889 -- For access types, the parent formal type and actual type
3890 -- differ.
3892 elsif Is_Access_Type (Formal_Typ)
3893 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3894 then
3895 if Ekind (Formal) /= E_In_Parameter then
3896 Convert (Actual, Parent_Typ);
3898 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3899 and then Designated_Type (Parent_Typ) /=
3900 Designated_Type (Actual_Typ)
3901 and then not Is_Controlling_Formal (Formal)
3902 then
3903 -- This unchecked conversion is not necessary unless
3904 -- inlining is enabled, because in that case the type
3905 -- mismatch may become visible in the body about to be
3906 -- inlined.
3908 Rewrite (Actual,
3909 Unchecked_Convert_To (Parent_Typ,
3910 Relocate_Node (Actual)));
3911 Analyze (Actual);
3912 Resolve (Actual, Parent_Typ);
3913 end if;
3915 -- If there is a change of representation, then generate a
3916 -- warning, and do the change of representation.
3918 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3919 Error_Msg_N
3920 ("??change of representation required", Actual);
3921 Convert (Actual, Parent_Typ);
3923 -- For array and record types, the parent formal type and
3924 -- derived formal type have different sizes or pragma Pack
3925 -- status.
3927 elsif ((Is_Array_Type (Formal_Typ)
3928 and then Is_Array_Type (Parent_Typ))
3929 or else
3930 (Is_Record_Type (Formal_Typ)
3931 and then Is_Record_Type (Parent_Typ)))
3932 and then
3933 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3934 or else Has_Pragma_Pack (Formal_Typ) /=
3935 Has_Pragma_Pack (Parent_Typ))
3936 then
3937 Convert (Actual, Parent_Typ);
3938 end if;
3940 Next_Actual (Actual);
3941 Next_Formal (Formal);
3942 Next_Formal (Parent_Formal);
3943 end loop;
3944 end;
3945 end if;
3947 Orig_Subp := Subp;
3948 Subp := Parent_Subp;
3949 end if;
3951 -- Deal with case where call is an explicit dereference
3953 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3955 -- Handle case of access to protected subprogram type
3957 if Is_Access_Protected_Subprogram_Type
3958 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3959 then
3960 -- If this is a call through an access to protected operation, the
3961 -- prefix has the form (object'address, operation'access). Rewrite
3962 -- as a for other protected calls: the object is the 1st parameter
3963 -- of the list of actuals.
3965 declare
3966 Call : Node_Id;
3967 Parm : List_Id;
3968 Nam : Node_Id;
3969 Obj : Node_Id;
3970 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3972 T : constant Entity_Id :=
3973 Equivalent_Type (Base_Type (Etype (Ptr)));
3975 D_T : constant Entity_Id :=
3976 Designated_Type (Base_Type (Etype (Ptr)));
3978 begin
3979 Obj :=
3980 Make_Selected_Component (Loc,
3981 Prefix => Unchecked_Convert_To (T, Ptr),
3982 Selector_Name =>
3983 New_Occurrence_Of (First_Entity (T), Loc));
3985 Nam :=
3986 Make_Selected_Component (Loc,
3987 Prefix => Unchecked_Convert_To (T, Ptr),
3988 Selector_Name =>
3989 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3991 Nam :=
3992 Make_Explicit_Dereference (Loc,
3993 Prefix => Nam);
3995 if Present (Parameter_Associations (Call_Node)) then
3996 Parm := Parameter_Associations (Call_Node);
3997 else
3998 Parm := New_List;
3999 end if;
4001 Prepend (Obj, Parm);
4003 if Etype (D_T) = Standard_Void_Type then
4004 Call :=
4005 Make_Procedure_Call_Statement (Loc,
4006 Name => Nam,
4007 Parameter_Associations => Parm);
4008 else
4009 Call :=
4010 Make_Function_Call (Loc,
4011 Name => Nam,
4012 Parameter_Associations => Parm);
4013 end if;
4015 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4016 Set_Etype (Call, Etype (D_T));
4018 -- We do not re-analyze the call to avoid infinite recursion.
4019 -- We analyze separately the prefix and the object, and set
4020 -- the checks on the prefix that would otherwise be emitted
4021 -- when resolving a call.
4023 Rewrite (Call_Node, Call);
4024 Analyze (Nam);
4025 Apply_Access_Check (Nam);
4026 Analyze (Obj);
4027 return;
4028 end;
4029 end if;
4030 end if;
4032 -- If this is a call to an intrinsic subprogram, then perform the
4033 -- appropriate expansion to the corresponding tree node and we
4034 -- are all done (since after that the call is gone).
4036 -- In the case where the intrinsic is to be processed by the back end,
4037 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4038 -- since the idea in this case is to pass the call unchanged. If the
4039 -- intrinsic is an inherited unchecked conversion, and the derived type
4040 -- is the target type of the conversion, we must retain it as the return
4041 -- type of the expression. Otherwise the expansion below, which uses the
4042 -- parent operation, will yield the wrong type.
4044 if Is_Intrinsic_Subprogram (Subp) then
4045 Expand_Intrinsic_Call (Call_Node, Subp);
4047 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4048 and then Parent_Subp /= Orig_Subp
4049 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4050 then
4051 Set_Etype (Call_Node, Etype (Orig_Subp));
4052 end if;
4054 return;
4055 end if;
4057 if Ekind_In (Subp, E_Function, E_Procedure) then
4059 -- We perform a simple optimization on calls for To_Address by
4060 -- replacing them with an unchecked conversion. Not only is this
4061 -- efficient, but it also avoids order of elaboration problems when
4062 -- address clauses are inlined (address expression elaborated at the
4063 -- wrong point).
4065 -- We perform this optimization regardless of whether we are in the
4066 -- main unit or in a unit in the context of the main unit, to ensure
4067 -- that the generated tree is the same in both cases, for CodePeer
4068 -- use.
4070 if Is_RTE (Subp, RE_To_Address) then
4071 Rewrite (Call_Node,
4072 Unchecked_Convert_To
4073 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4074 return;
4076 -- A call to a null procedure is replaced by a null statement, but we
4077 -- are not allowed to ignore possible side effects of the call, so we
4078 -- make sure that actuals are evaluated.
4079 -- We also suppress this optimization for GNATCoverage.
4081 elsif Is_Null_Procedure (Subp)
4082 and then not Opt.Suppress_Control_Flow_Optimizations
4083 then
4084 Actual := First_Actual (Call_Node);
4085 while Present (Actual) loop
4086 Remove_Side_Effects (Actual);
4087 Next_Actual (Actual);
4088 end loop;
4090 Rewrite (Call_Node, Make_Null_Statement (Loc));
4091 return;
4092 end if;
4094 -- Handle inlining. No action needed if the subprogram is not inlined
4096 if not Is_Inlined (Subp) then
4097 null;
4099 -- Frontend inlining of expression functions (performed also when
4100 -- backend inlining is enabled).
4102 elsif Is_Inlinable_Expression_Function (Subp) then
4103 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
4104 Analyze (N);
4105 return;
4107 -- Handle frontend inlining
4109 elsif not Back_End_Inlining then
4110 Inlined_Subprogram : declare
4111 Bod : Node_Id;
4112 Must_Inline : Boolean := False;
4113 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4115 begin
4116 -- Verify that the body to inline has already been seen, and
4117 -- that if the body is in the current unit the inlining does
4118 -- not occur earlier. This avoids order-of-elaboration problems
4119 -- in the back end.
4121 -- This should be documented in sinfo/einfo ???
4123 if No (Spec)
4124 or else Nkind (Spec) /= N_Subprogram_Declaration
4125 or else No (Body_To_Inline (Spec))
4126 then
4127 Must_Inline := False;
4129 -- If this an inherited function that returns a private type,
4130 -- do not inline if the full view is an unconstrained array,
4131 -- because such calls cannot be inlined.
4133 elsif Present (Orig_Subp)
4134 and then Is_Array_Type (Etype (Orig_Subp))
4135 and then not Is_Constrained (Etype (Orig_Subp))
4136 then
4137 Must_Inline := False;
4139 elsif In_Unfrozen_Instance (Scope (Subp)) then
4140 Must_Inline := False;
4142 else
4143 Bod := Body_To_Inline (Spec);
4145 if (In_Extended_Main_Code_Unit (Call_Node)
4146 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4147 or else Has_Pragma_Inline_Always (Subp))
4148 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4149 or else
4150 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4151 then
4152 Must_Inline := True;
4154 -- If we are compiling a package body that is not the main
4155 -- unit, it must be for inlining/instantiation purposes,
4156 -- in which case we inline the call to insure that the same
4157 -- temporaries are generated when compiling the body by
4158 -- itself. Otherwise link errors can occur.
4160 -- If the function being called is itself in the main unit,
4161 -- we cannot inline, because there is a risk of double
4162 -- elaboration and/or circularity: the inlining can make
4163 -- visible a private entity in the body of the main unit,
4164 -- that gigi will see before its sees its proper definition.
4166 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4167 and then In_Package_Body
4168 then
4169 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4171 -- Inline calls to _postconditions when generating C code
4173 elsif Modify_Tree_For_C
4174 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4175 and then Chars (Name (N)) = Name_uPostconditions
4176 then
4177 Must_Inline := True;
4178 end if;
4179 end if;
4181 if Must_Inline then
4182 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4184 else
4185 -- Let the back end handle it
4187 Add_Inlined_Body (Subp, Call_Node);
4189 if Front_End_Inlining
4190 and then Nkind (Spec) = N_Subprogram_Declaration
4191 and then (In_Extended_Main_Code_Unit (Call_Node))
4192 and then No (Body_To_Inline (Spec))
4193 and then not Has_Completion (Subp)
4194 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4195 then
4196 Cannot_Inline
4197 ("cannot inline& (body not seen yet)?",
4198 Call_Node, Subp);
4199 end if;
4200 end if;
4201 end Inlined_Subprogram;
4203 -- Back end inlining: let the back end handle it
4205 elsif No (Unit_Declaration_Node (Subp))
4206 or else Nkind (Unit_Declaration_Node (Subp)) /=
4207 N_Subprogram_Declaration
4208 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4209 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4210 N_Entity
4211 then
4212 Add_Inlined_Body (Subp, Call_Node);
4214 -- If the inlined call appears within an instantiation and some
4215 -- level of optimization is required, ensure that the enclosing
4216 -- instance body is available so that the back-end can actually
4217 -- perform the inlining.
4219 if In_Instance
4220 and then Comes_From_Source (Subp)
4221 and then Optimization_Level > 0
4222 then
4223 declare
4224 Decl : Node_Id;
4225 Inst : Entity_Id;
4226 Inst_Node : Node_Id;
4228 begin
4229 Inst := Scope (Subp);
4231 -- Find enclosing instance
4233 while Present (Inst) and then Inst /= Standard_Standard loop
4234 exit when Is_Generic_Instance (Inst);
4235 Inst := Scope (Inst);
4236 end loop;
4238 if Present (Inst)
4239 and then Is_Generic_Instance (Inst)
4240 and then not Is_Inlined (Inst)
4241 then
4242 Set_Is_Inlined (Inst);
4243 Decl := Unit_Declaration_Node (Inst);
4245 -- Do not add a pending instantiation if the body exits
4246 -- already, or if the instance is a compilation unit, or
4247 -- the instance node is missing.
4249 if Present (Corresponding_Body (Decl))
4250 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4251 or else No (Next (Decl))
4252 then
4253 null;
4255 else
4256 -- The instantiation node usually follows the package
4257 -- declaration for the instance. If the generic unit
4258 -- has aspect specifications, they are transformed
4259 -- into pragmas in the instance, and the instance node
4260 -- appears after them.
4262 Inst_Node := Next (Decl);
4264 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4265 Inst_Node := Next (Inst_Node);
4266 end loop;
4268 Add_Pending_Instantiation (Inst_Node, Decl);
4269 end if;
4270 end if;
4271 end;
4272 end if;
4274 -- Front end expansion of simple functions returning unconstrained
4275 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4276 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4277 -- also Build_Renamed_Body) cannot be expanded here because this may
4278 -- give rise to order-of-elaboration issues for the types of the
4279 -- parameters of the subprogram, if any.
4281 else
4282 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4283 end if;
4284 end if;
4286 -- Check for protected subprogram. This is either an intra-object call,
4287 -- or a protected function call. Protected procedure calls are rewritten
4288 -- as entry calls and handled accordingly.
4290 -- In Ada 2005, this may be an indirect call to an access parameter that
4291 -- is an access_to_subprogram. In that case the anonymous type has a
4292 -- scope that is a protected operation, but the call is a regular one.
4293 -- In either case do not expand call if subprogram is eliminated.
4295 Scop := Scope (Subp);
4297 if Nkind (Call_Node) /= N_Entry_Call_Statement
4298 and then Is_Protected_Type (Scop)
4299 and then Ekind (Subp) /= E_Subprogram_Type
4300 and then not Is_Eliminated (Subp)
4301 then
4302 -- If the call is an internal one, it is rewritten as a call to the
4303 -- corresponding unprotected subprogram.
4305 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4306 end if;
4308 -- Functions returning controlled objects need special attention. If
4309 -- the return type is limited, then the context is initialization and
4310 -- different processing applies. If the call is to a protected function,
4311 -- the expansion above will call Expand_Call recursively. Otherwise the
4312 -- function call is transformed into a temporary which obtains the
4313 -- result from the secondary stack.
4315 if Needs_Finalization (Etype (Subp)) then
4316 if not Is_Limited_View (Etype (Subp))
4317 and then
4318 (No (First_Formal (Subp))
4319 or else
4320 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4321 then
4322 Expand_Ctrl_Function_Call (Call_Node);
4324 -- Build-in-place function calls which appear in anonymous contexts
4325 -- need a transient scope to ensure the proper finalization of the
4326 -- intermediate result after its use.
4328 elsif Is_Build_In_Place_Function_Call (Call_Node)
4329 and then
4330 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
4331 N_Function_Call,
4332 N_Indexed_Component,
4333 N_Object_Renaming_Declaration,
4334 N_Procedure_Call_Statement,
4335 N_Selected_Component,
4336 N_Slice)
4337 then
4338 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4339 end if;
4340 end if;
4341 end Expand_Call_Helper;
4343 -------------------------------
4344 -- Expand_Ctrl_Function_Call --
4345 -------------------------------
4347 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4348 function Is_Element_Reference (N : Node_Id) return Boolean;
4349 -- Determine whether node N denotes a reference to an Ada 2012 container
4350 -- element.
4352 --------------------------
4353 -- Is_Element_Reference --
4354 --------------------------
4356 function Is_Element_Reference (N : Node_Id) return Boolean is
4357 Ref : constant Node_Id := Original_Node (N);
4359 begin
4360 -- Analysis marks an element reference by setting the generalized
4361 -- indexing attribute of an indexed component before the component
4362 -- is rewritten into a function call.
4364 return
4365 Nkind (Ref) = N_Indexed_Component
4366 and then Present (Generalized_Indexing (Ref));
4367 end Is_Element_Reference;
4369 -- Start of processing for Expand_Ctrl_Function_Call
4371 begin
4372 -- Optimization, if the returned value (which is on the sec-stack) is
4373 -- returned again, no need to copy/readjust/finalize, we can just pass
4374 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4375 -- attachment is needed
4377 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4378 return;
4379 end if;
4381 -- Resolution is now finished, make sure we don't start analysis again
4382 -- because of the duplication.
4384 Set_Analyzed (N);
4386 -- A function which returns a controlled object uses the secondary
4387 -- stack. Rewrite the call into a temporary which obtains the result of
4388 -- the function using 'reference.
4390 Remove_Side_Effects (N);
4392 -- The side effect removal of the function call produced a temporary.
4393 -- When the context is a case expression, if expression, or expression
4394 -- with actions, the lifetime of the temporary must be extended to match
4395 -- that of the context. Otherwise the function result will be finalized
4396 -- too early and affect the result of the expression. To prevent this
4397 -- unwanted effect, the temporary should not be considered for clean up
4398 -- actions by the general finalization machinery.
4400 -- Exception to this rule are references to Ada 2012 container elements.
4401 -- Such references must be finalized at the end of each iteration of the
4402 -- related quantified expression, otherwise the container will remain
4403 -- busy.
4405 if Nkind (N) = N_Explicit_Dereference
4406 and then Within_Case_Or_If_Expression (N)
4407 and then not Is_Element_Reference (N)
4408 then
4409 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4410 end if;
4411 end Expand_Ctrl_Function_Call;
4413 ----------------------------------------
4414 -- Expand_N_Extended_Return_Statement --
4415 ----------------------------------------
4417 -- If there is a Handled_Statement_Sequence, we rewrite this:
4419 -- return Result : T := <expression> do
4420 -- <handled_seq_of_stms>
4421 -- end return;
4423 -- to be:
4425 -- declare
4426 -- Result : T := <expression>;
4427 -- begin
4428 -- <handled_seq_of_stms>
4429 -- return Result;
4430 -- end;
4432 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4434 -- return Result : T := <expression>;
4436 -- to be:
4438 -- return <expression>;
4440 -- unless it's build-in-place or there's no <expression>, in which case
4441 -- we generate:
4443 -- declare
4444 -- Result : T := <expression>;
4445 -- begin
4446 -- return Result;
4447 -- end;
4449 -- Note that this case could have been written by the user as an extended
4450 -- return statement, or could have been transformed to this from a simple
4451 -- return statement.
4453 -- That is, we need to have a reified return object if there are statements
4454 -- (which might refer to it) or if we're doing build-in-place (so we can
4455 -- set its address to the final resting place or if there is no expression
4456 -- (in which case default initial values might need to be set).
4458 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4459 Loc : constant Source_Ptr := Sloc (N);
4461 function Build_Heap_Allocator
4462 (Temp_Id : Entity_Id;
4463 Temp_Typ : Entity_Id;
4464 Func_Id : Entity_Id;
4465 Ret_Typ : Entity_Id;
4466 Alloc_Expr : Node_Id) return Node_Id;
4467 -- Create the statements necessary to allocate a return object on the
4468 -- caller's master. The master is available through implicit parameter
4469 -- BIPfinalizationmaster.
4471 -- if BIPfinalizationmaster /= null then
4472 -- declare
4473 -- type Ptr_Typ is access Ret_Typ;
4474 -- for Ptr_Typ'Storage_Pool use
4475 -- Base_Pool (BIPfinalizationmaster.all).all;
4476 -- Local : Ptr_Typ;
4478 -- begin
4479 -- procedure Allocate (...) is
4480 -- begin
4481 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4482 -- end Allocate;
4484 -- Local := <Alloc_Expr>;
4485 -- Temp_Id := Temp_Typ (Local);
4486 -- end;
4487 -- end if;
4489 -- Temp_Id is the temporary which is used to reference the internally
4490 -- created object in all allocation forms. Temp_Typ is the type of the
4491 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4492 -- type of Func_Id. Alloc_Expr is the actual allocator.
4494 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4495 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4496 -- with parameters:
4497 -- From current activation chain
4498 -- To activation chain passed in by the caller
4499 -- New_Master master passed in by the caller
4501 -- Func_Id is the entity of the function where the extended return
4502 -- statement appears.
4504 --------------------------
4505 -- Build_Heap_Allocator --
4506 --------------------------
4508 function Build_Heap_Allocator
4509 (Temp_Id : Entity_Id;
4510 Temp_Typ : Entity_Id;
4511 Func_Id : Entity_Id;
4512 Ret_Typ : Entity_Id;
4513 Alloc_Expr : Node_Id) return Node_Id
4515 begin
4516 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4518 -- Processing for build-in-place object allocation.
4520 if Needs_Finalization (Ret_Typ) then
4521 declare
4522 Decls : constant List_Id := New_List;
4523 Fin_Mas_Id : constant Entity_Id :=
4524 Build_In_Place_Formal
4525 (Func_Id, BIP_Finalization_Master);
4526 Stmts : constant List_Id := New_List;
4527 Desig_Typ : Entity_Id;
4528 Local_Id : Entity_Id;
4529 Pool_Id : Entity_Id;
4530 Ptr_Typ : Entity_Id;
4532 begin
4533 -- Generate:
4534 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4536 Pool_Id := Make_Temporary (Loc, 'P');
4538 Append_To (Decls,
4539 Make_Object_Renaming_Declaration (Loc,
4540 Defining_Identifier => Pool_Id,
4541 Subtype_Mark =>
4542 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4543 Name =>
4544 Make_Explicit_Dereference (Loc,
4545 Prefix =>
4546 Make_Function_Call (Loc,
4547 Name =>
4548 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4549 Parameter_Associations => New_List (
4550 Make_Explicit_Dereference (Loc,
4551 Prefix =>
4552 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4554 -- Create an access type which uses the storage pool of the
4555 -- caller's master. This additional type is necessary because
4556 -- the finalization master cannot be associated with the type
4557 -- of the temporary. Otherwise the secondary stack allocation
4558 -- will fail.
4560 Desig_Typ := Ret_Typ;
4562 -- Ensure that the build-in-place machinery uses a fat pointer
4563 -- when allocating an unconstrained array on the heap. In this
4564 -- case the result object type is a constrained array type even
4565 -- though the function type is unconstrained.
4567 if Ekind (Desig_Typ) = E_Array_Subtype then
4568 Desig_Typ := Base_Type (Desig_Typ);
4569 end if;
4571 -- Generate:
4572 -- type Ptr_Typ is access Desig_Typ;
4574 Ptr_Typ := Make_Temporary (Loc, 'P');
4576 Append_To (Decls,
4577 Make_Full_Type_Declaration (Loc,
4578 Defining_Identifier => Ptr_Typ,
4579 Type_Definition =>
4580 Make_Access_To_Object_Definition (Loc,
4581 Subtype_Indication =>
4582 New_Occurrence_Of (Desig_Typ, Loc))));
4584 -- Perform minor decoration in order to set the master and the
4585 -- storage pool attributes.
4587 Set_Ekind (Ptr_Typ, E_Access_Type);
4588 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4589 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4591 -- Create the temporary, generate:
4592 -- Local_Id : Ptr_Typ;
4594 Local_Id := Make_Temporary (Loc, 'T');
4596 Append_To (Decls,
4597 Make_Object_Declaration (Loc,
4598 Defining_Identifier => Local_Id,
4599 Object_Definition =>
4600 New_Occurrence_Of (Ptr_Typ, Loc)));
4602 -- Allocate the object, generate:
4603 -- Local_Id := <Alloc_Expr>;
4605 Append_To (Stmts,
4606 Make_Assignment_Statement (Loc,
4607 Name => New_Occurrence_Of (Local_Id, Loc),
4608 Expression => Alloc_Expr));
4610 -- Generate:
4611 -- Temp_Id := Temp_Typ (Local_Id);
4613 Append_To (Stmts,
4614 Make_Assignment_Statement (Loc,
4615 Name => New_Occurrence_Of (Temp_Id, Loc),
4616 Expression =>
4617 Unchecked_Convert_To (Temp_Typ,
4618 New_Occurrence_Of (Local_Id, Loc))));
4620 -- Wrap the allocation in a block. This is further conditioned
4621 -- by checking the caller finalization master at runtime. A
4622 -- null value indicates a non-existent master, most likely due
4623 -- to a Finalize_Storage_Only allocation.
4625 -- Generate:
4626 -- if BIPfinalizationmaster /= null then
4627 -- declare
4628 -- <Decls>
4629 -- begin
4630 -- <Stmts>
4631 -- end;
4632 -- end if;
4634 return
4635 Make_If_Statement (Loc,
4636 Condition =>
4637 Make_Op_Ne (Loc,
4638 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4639 Right_Opnd => Make_Null (Loc)),
4641 Then_Statements => New_List (
4642 Make_Block_Statement (Loc,
4643 Declarations => Decls,
4644 Handled_Statement_Sequence =>
4645 Make_Handled_Sequence_Of_Statements (Loc,
4646 Statements => Stmts))));
4647 end;
4649 -- For all other cases, generate:
4650 -- Temp_Id := <Alloc_Expr>;
4652 else
4653 return
4654 Make_Assignment_Statement (Loc,
4655 Name => New_Occurrence_Of (Temp_Id, Loc),
4656 Expression => Alloc_Expr);
4657 end if;
4658 end Build_Heap_Allocator;
4660 ---------------------------
4661 -- Move_Activation_Chain --
4662 ---------------------------
4664 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4665 begin
4666 return
4667 Make_Procedure_Call_Statement (Loc,
4668 Name =>
4669 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4671 Parameter_Associations => New_List (
4673 -- Source chain
4675 Make_Attribute_Reference (Loc,
4676 Prefix => Make_Identifier (Loc, Name_uChain),
4677 Attribute_Name => Name_Unrestricted_Access),
4679 -- Destination chain
4681 New_Occurrence_Of
4682 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4684 -- New master
4686 New_Occurrence_Of
4687 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4688 end Move_Activation_Chain;
4690 -- Local variables
4692 Func_Id : constant Entity_Id :=
4693 Return_Applies_To (Return_Statement_Entity (N));
4694 Is_BIP_Func : constant Boolean :=
4695 Is_Build_In_Place_Function (Func_Id);
4696 Ret_Obj_Id : constant Entity_Id :=
4697 First_Entity (Return_Statement_Entity (N));
4698 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4699 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4701 Exp : Node_Id;
4702 HSS : Node_Id;
4703 Result : Node_Id;
4704 Return_Stmt : Node_Id;
4705 Stmts : List_Id;
4707 -- Start of processing for Expand_N_Extended_Return_Statement
4709 begin
4710 -- Given that functionality of interface thunks is simple (just displace
4711 -- the pointer to the object) they are always handled by means of
4712 -- simple return statements.
4714 pragma Assert (not Is_Thunk (Current_Scope));
4716 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4717 Exp := Expression (Ret_Obj_Decl);
4718 else
4719 Exp := Empty;
4720 end if;
4722 HSS := Handled_Statement_Sequence (N);
4724 -- If the returned object needs finalization actions, the function must
4725 -- perform the appropriate cleanup should it fail to return. The state
4726 -- of the function itself is tracked through a flag which is coupled
4727 -- with the scope finalizer. There is one flag per each return object
4728 -- in case of multiple returns.
4730 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4731 declare
4732 Flag_Decl : Node_Id;
4733 Flag_Id : Entity_Id;
4734 Func_Bod : Node_Id;
4736 begin
4737 -- Recover the function body
4739 Func_Bod := Unit_Declaration_Node (Func_Id);
4741 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4742 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4743 end if;
4745 -- Create a flag to track the function state
4747 Flag_Id := Make_Temporary (Loc, 'F');
4748 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4750 -- Insert the flag at the beginning of the function declarations,
4751 -- generate:
4752 -- Fnn : Boolean := False;
4754 Flag_Decl :=
4755 Make_Object_Declaration (Loc,
4756 Defining_Identifier => Flag_Id,
4757 Object_Definition =>
4758 New_Occurrence_Of (Standard_Boolean, Loc),
4759 Expression =>
4760 New_Occurrence_Of (Standard_False, Loc));
4762 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4763 Analyze (Flag_Decl);
4764 end;
4765 end if;
4767 -- Build a simple_return_statement that returns the return object when
4768 -- there is a statement sequence, or no expression, or the result will
4769 -- be built in place. Note however that we currently do this for all
4770 -- composite cases, even though nonlimited composite results are not yet
4771 -- built in place (though we plan to do so eventually).
4773 if Present (HSS)
4774 or else Is_Composite_Type (Ret_Typ)
4775 or else No (Exp)
4776 then
4777 if No (HSS) then
4778 Stmts := New_List;
4780 -- If the extended return has a handled statement sequence, then wrap
4781 -- it in a block and use the block as the first statement.
4783 else
4784 Stmts := New_List (
4785 Make_Block_Statement (Loc,
4786 Declarations => New_List,
4787 Handled_Statement_Sequence => HSS));
4788 end if;
4790 -- If the result type contains tasks, we call Move_Activation_Chain.
4791 -- Later, the cleanup code will call Complete_Master, which will
4792 -- terminate any unactivated tasks belonging to the return statement
4793 -- master. But Move_Activation_Chain updates their master to be that
4794 -- of the caller, so they will not be terminated unless the return
4795 -- statement completes unsuccessfully due to exception, abort, goto,
4796 -- or exit. As a formality, we test whether the function requires the
4797 -- result to be built in place, though that's necessarily true for
4798 -- the case of result types with task parts.
4800 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4802 -- The return expression is an aggregate for a complex type which
4803 -- contains tasks. This particular case is left unexpanded since
4804 -- the regular expansion would insert all temporaries and
4805 -- initialization code in the wrong block.
4807 if Nkind (Exp) = N_Aggregate then
4808 Expand_N_Aggregate (Exp);
4809 end if;
4811 -- Do not move the activation chain if the return object does not
4812 -- contain tasks.
4814 if Has_Task (Etype (Ret_Obj_Id)) then
4815 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4816 end if;
4817 end if;
4819 -- Update the state of the function right before the object is
4820 -- returned.
4822 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4823 declare
4824 Flag_Id : constant Entity_Id :=
4825 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4827 begin
4828 -- Generate:
4829 -- Fnn := True;
4831 Append_To (Stmts,
4832 Make_Assignment_Statement (Loc,
4833 Name => New_Occurrence_Of (Flag_Id, Loc),
4834 Expression => New_Occurrence_Of (Standard_True, Loc)));
4835 end;
4836 end if;
4838 -- Build a simple_return_statement that returns the return object
4840 Return_Stmt :=
4841 Make_Simple_Return_Statement (Loc,
4842 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4843 Append_To (Stmts, Return_Stmt);
4845 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4846 end if;
4848 -- Case where we build a return statement block
4850 if Present (HSS) then
4851 Result :=
4852 Make_Block_Statement (Loc,
4853 Declarations => Return_Object_Declarations (N),
4854 Handled_Statement_Sequence => HSS);
4856 -- We set the entity of the new block statement to be that of the
4857 -- return statement. This is necessary so that various fields, such
4858 -- as Finalization_Chain_Entity carry over from the return statement
4859 -- to the block. Note that this block is unusual, in that its entity
4860 -- is an E_Return_Statement rather than an E_Block.
4862 Set_Identifier
4863 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4865 -- If the object decl was already rewritten as a renaming, then we
4866 -- don't want to do the object allocation and transformation of
4867 -- the return object declaration to a renaming. This case occurs
4868 -- when the return object is initialized by a call to another
4869 -- build-in-place function, and that function is responsible for
4870 -- the allocation of the return object.
4872 if Is_BIP_Func
4873 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4874 then
4875 pragma Assert
4876 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4877 and then
4879 -- It is a regular BIP object declaration
4881 (Is_Build_In_Place_Function_Call
4882 (Expression (Original_Node (Ret_Obj_Decl)))
4884 -- It is a BIP object declaration that displaces the pointer
4885 -- to the object to reference a convered interface type.
4887 or else
4888 Present (Unqual_BIP_Iface_Function_Call
4889 (Expression (Original_Node (Ret_Obj_Decl))))));
4891 -- Return the build-in-place result by reference
4893 Set_By_Ref (Return_Stmt);
4895 elsif Is_BIP_Func then
4897 -- Locate the implicit access parameter associated with the
4898 -- caller-supplied return object and convert the return
4899 -- statement's return object declaration to a renaming of a
4900 -- dereference of the access parameter. If the return object's
4901 -- declaration includes an expression that has not already been
4902 -- expanded as separate assignments, then add an assignment
4903 -- statement to ensure the return object gets initialized.
4905 -- declare
4906 -- Result : T [:= <expression>];
4907 -- begin
4908 -- ...
4910 -- is converted to
4912 -- declare
4913 -- Result : T renames FuncRA.all;
4914 -- [Result := <expression;]
4915 -- begin
4916 -- ...
4918 declare
4919 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4920 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4922 Init_Assignment : Node_Id := Empty;
4923 Obj_Acc_Formal : Entity_Id;
4924 Obj_Acc_Deref : Node_Id;
4925 Obj_Alloc_Formal : Entity_Id;
4927 begin
4928 -- Build-in-place results must be returned by reference
4930 Set_By_Ref (Return_Stmt);
4932 -- Retrieve the implicit access parameter passed by the caller
4934 Obj_Acc_Formal :=
4935 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4937 -- If the return object's declaration includes an expression
4938 -- and the declaration isn't marked as No_Initialization, then
4939 -- we need to generate an assignment to the object and insert
4940 -- it after the declaration before rewriting it as a renaming
4941 -- (otherwise we'll lose the initialization). The case where
4942 -- the result type is an interface (or class-wide interface)
4943 -- is also excluded because the context of the function call
4944 -- must be unconstrained, so the initialization will always
4945 -- be done as part of an allocator evaluation (storage pool
4946 -- or secondary stack), never to a constrained target object
4947 -- passed in by the caller. Besides the assignment being
4948 -- unneeded in this case, it avoids problems with trying to
4949 -- generate a dispatching assignment when the return expression
4950 -- is a nonlimited descendant of a limited interface (the
4951 -- interface has no assignment operation).
4953 if Present (Ret_Obj_Expr)
4954 and then not No_Initialization (Ret_Obj_Decl)
4955 and then not Is_Interface (Ret_Obj_Typ)
4956 then
4957 Init_Assignment :=
4958 Make_Assignment_Statement (Loc,
4959 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4960 Expression => New_Copy_Tree (Ret_Obj_Expr));
4962 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4963 Set_Assignment_OK (Name (Init_Assignment));
4964 Set_No_Ctrl_Actions (Init_Assignment);
4966 Set_Parent (Name (Init_Assignment), Init_Assignment);
4967 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4969 Set_Expression (Ret_Obj_Decl, Empty);
4971 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4972 and then not Is_Class_Wide_Type
4973 (Etype (Expression (Init_Assignment)))
4974 then
4975 Rewrite (Expression (Init_Assignment),
4976 Make_Type_Conversion (Loc,
4977 Subtype_Mark =>
4978 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
4979 Expression =>
4980 Relocate_Node (Expression (Init_Assignment))));
4981 end if;
4983 -- In the case of functions where the calling context can
4984 -- determine the form of allocation needed, initialization
4985 -- is done with each part of the if statement that handles
4986 -- the different forms of allocation (this is true for
4987 -- unconstrained and tagged result subtypes).
4989 if Is_Constrained (Ret_Typ)
4990 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
4991 then
4992 Insert_After (Ret_Obj_Decl, Init_Assignment);
4993 end if;
4994 end if;
4996 -- When the function's subtype is unconstrained, a run-time
4997 -- test is needed to determine the form of allocation to use
4998 -- for the return object. The function has an implicit formal
4999 -- parameter indicating this. If the BIP_Alloc_Form formal has
5000 -- the value one, then the caller has passed access to an
5001 -- existing object for use as the return object. If the value
5002 -- is two, then the return object must be allocated on the
5003 -- secondary stack. Otherwise, the object must be allocated in
5004 -- a storage pool (currently only supported for the global
5005 -- heap, user-defined storage pools TBD ???). We generate an
5006 -- if statement to test the implicit allocation formal and
5007 -- initialize a local access value appropriately, creating
5008 -- allocators in the secondary stack and global heap cases.
5009 -- The special formal also exists and must be tested when the
5010 -- function has a tagged result, even when the result subtype
5011 -- is constrained, because in general such functions can be
5012 -- called in dispatching contexts and must be handled similarly
5013 -- to functions with a class-wide result.
5015 if not Is_Constrained (Ret_Typ)
5016 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
5017 then
5018 Obj_Alloc_Formal :=
5019 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5021 declare
5022 Pool_Id : constant Entity_Id :=
5023 Make_Temporary (Loc, 'P');
5024 Alloc_Obj_Id : Entity_Id;
5025 Alloc_Obj_Decl : Node_Id;
5026 Alloc_If_Stmt : Node_Id;
5027 Heap_Allocator : Node_Id;
5028 Pool_Decl : Node_Id;
5029 Pool_Allocator : Node_Id;
5030 Ptr_Type_Decl : Node_Id;
5031 Ref_Type : Entity_Id;
5032 SS_Allocator : Node_Id;
5034 begin
5035 -- Reuse the itype created for the function's implicit
5036 -- access formal. This avoids the need to create a new
5037 -- access type here, plus it allows assigning the access
5038 -- formal directly without applying a conversion.
5040 -- Ref_Type := Etype (Object_Access);
5042 -- Create an access type designating the function's
5043 -- result subtype.
5045 Ref_Type := Make_Temporary (Loc, 'A');
5047 Ptr_Type_Decl :=
5048 Make_Full_Type_Declaration (Loc,
5049 Defining_Identifier => Ref_Type,
5050 Type_Definition =>
5051 Make_Access_To_Object_Definition (Loc,
5052 All_Present => True,
5053 Subtype_Indication =>
5054 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5056 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5058 -- Create an access object that will be initialized to an
5059 -- access value denoting the return object, either coming
5060 -- from an implicit access value passed in by the caller
5061 -- or from the result of an allocator.
5063 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5064 Set_Etype (Alloc_Obj_Id, Ref_Type);
5066 Alloc_Obj_Decl :=
5067 Make_Object_Declaration (Loc,
5068 Defining_Identifier => Alloc_Obj_Id,
5069 Object_Definition =>
5070 New_Occurrence_Of (Ref_Type, Loc));
5072 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5074 -- Create allocators for both the secondary stack and
5075 -- global heap. If there's an initialization expression,
5076 -- then create these as initialized allocators.
5078 if Present (Ret_Obj_Expr)
5079 and then not No_Initialization (Ret_Obj_Decl)
5080 then
5081 -- Always use the type of the expression for the
5082 -- qualified expression, rather than the result type.
5083 -- In general we cannot always use the result type
5084 -- for the allocator, because the expression might be
5085 -- of a specific type, such as in the case of an
5086 -- aggregate or even a nonlimited object when the
5087 -- result type is a limited class-wide interface type.
5089 Heap_Allocator :=
5090 Make_Allocator (Loc,
5091 Expression =>
5092 Make_Qualified_Expression (Loc,
5093 Subtype_Mark =>
5094 New_Occurrence_Of
5095 (Etype (Ret_Obj_Expr), Loc),
5096 Expression => New_Copy_Tree (Ret_Obj_Expr)));
5098 else
5099 -- If the function returns a class-wide type we cannot
5100 -- use the return type for the allocator. Instead we
5101 -- use the type of the expression, which must be an
5102 -- aggregate of a definite type.
5104 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5105 Heap_Allocator :=
5106 Make_Allocator (Loc,
5107 Expression =>
5108 New_Occurrence_Of
5109 (Etype (Ret_Obj_Expr), Loc));
5110 else
5111 Heap_Allocator :=
5112 Make_Allocator (Loc,
5113 Expression =>
5114 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5115 end if;
5117 -- If the object requires default initialization then
5118 -- that will happen later following the elaboration of
5119 -- the object renaming. If we don't turn it off here
5120 -- then the object will be default initialized twice.
5122 Set_No_Initialization (Heap_Allocator);
5123 end if;
5125 -- The Pool_Allocator is just like the Heap_Allocator,
5126 -- except we set Storage_Pool and Procedure_To_Call so
5127 -- it will use the user-defined storage pool.
5129 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5131 -- Do not generate the renaming of the build-in-place
5132 -- pool parameter on ZFP because the parameter is not
5133 -- created in the first place.
5135 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5136 Pool_Decl :=
5137 Make_Object_Renaming_Declaration (Loc,
5138 Defining_Identifier => Pool_Id,
5139 Subtype_Mark =>
5140 New_Occurrence_Of
5141 (RTE (RE_Root_Storage_Pool), Loc),
5142 Name =>
5143 Make_Explicit_Dereference (Loc,
5144 New_Occurrence_Of
5145 (Build_In_Place_Formal
5146 (Func_Id, BIP_Storage_Pool), Loc)));
5147 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5148 Set_Procedure_To_Call
5149 (Pool_Allocator, RTE (RE_Allocate_Any));
5150 else
5151 Pool_Decl := Make_Null_Statement (Loc);
5152 end if;
5154 -- If the No_Allocators restriction is active, then only
5155 -- an allocator for secondary stack allocation is needed.
5156 -- It's OK for such allocators to have Comes_From_Source
5157 -- set to False, because gigi knows not to flag them as
5158 -- being a violation of No_Implicit_Heap_Allocations.
5160 if Restriction_Active (No_Allocators) then
5161 SS_Allocator := Heap_Allocator;
5162 Heap_Allocator := Make_Null (Loc);
5163 Pool_Allocator := Make_Null (Loc);
5165 -- Otherwise the heap and pool allocators may be needed,
5166 -- so we make another allocator for secondary stack
5167 -- allocation.
5169 else
5170 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5172 -- The heap and pool allocators are marked as
5173 -- Comes_From_Source since they correspond to an
5174 -- explicit user-written allocator (that is, it will
5175 -- only be executed on behalf of callers that call the
5176 -- function as initialization for such an allocator).
5177 -- Prevents errors when No_Implicit_Heap_Allocations
5178 -- is in force.
5180 Set_Comes_From_Source (Heap_Allocator, True);
5181 Set_Comes_From_Source (Pool_Allocator, True);
5182 end if;
5184 -- The allocator is returned on the secondary stack.
5186 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5187 Set_Procedure_To_Call
5188 (SS_Allocator, RTE (RE_SS_Allocate));
5190 -- The allocator is returned on the secondary stack,
5191 -- so indicate that the function return, as well as
5192 -- all blocks that encloses the allocator, must not
5193 -- release it. The flags must be set now because
5194 -- the decision to use the secondary stack is done
5195 -- very late in the course of expanding the return
5196 -- statement, past the point where these flags are
5197 -- normally set.
5199 Set_Uses_Sec_Stack (Func_Id);
5200 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5201 Set_Sec_Stack_Needed_For_Return
5202 (Return_Statement_Entity (N));
5203 Set_Enclosing_Sec_Stack_Return (N);
5205 -- Create an if statement to test the BIP_Alloc_Form
5206 -- formal and initialize the access object to either the
5207 -- BIP_Object_Access formal (BIP_Alloc_Form =
5208 -- Caller_Allocation), the result of allocating the
5209 -- object in the secondary stack (BIP_Alloc_Form =
5210 -- Secondary_Stack), or else an allocator to create the
5211 -- return object in the heap or user-defined pool
5212 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5214 -- ??? An unchecked type conversion must be made in the
5215 -- case of assigning the access object formal to the
5216 -- local access object, because a normal conversion would
5217 -- be illegal in some cases (such as converting access-
5218 -- to-unconstrained to access-to-constrained), but the
5219 -- the unchecked conversion will presumably fail to work
5220 -- right in just such cases. It's not clear at all how to
5221 -- handle this. ???
5223 Alloc_If_Stmt :=
5224 Make_If_Statement (Loc,
5225 Condition =>
5226 Make_Op_Eq (Loc,
5227 Left_Opnd =>
5228 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5229 Right_Opnd =>
5230 Make_Integer_Literal (Loc,
5231 UI_From_Int (BIP_Allocation_Form'Pos
5232 (Caller_Allocation)))),
5234 Then_Statements => New_List (
5235 Make_Assignment_Statement (Loc,
5236 Name =>
5237 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5238 Expression =>
5239 Make_Unchecked_Type_Conversion (Loc,
5240 Subtype_Mark =>
5241 New_Occurrence_Of (Ref_Type, Loc),
5242 Expression =>
5243 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5245 Elsif_Parts => New_List (
5246 Make_Elsif_Part (Loc,
5247 Condition =>
5248 Make_Op_Eq (Loc,
5249 Left_Opnd =>
5250 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5251 Right_Opnd =>
5252 Make_Integer_Literal (Loc,
5253 UI_From_Int (BIP_Allocation_Form'Pos
5254 (Secondary_Stack)))),
5256 Then_Statements => New_List (
5257 Make_Assignment_Statement (Loc,
5258 Name =>
5259 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5260 Expression => SS_Allocator))),
5262 Make_Elsif_Part (Loc,
5263 Condition =>
5264 Make_Op_Eq (Loc,
5265 Left_Opnd =>
5266 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5267 Right_Opnd =>
5268 Make_Integer_Literal (Loc,
5269 UI_From_Int (BIP_Allocation_Form'Pos
5270 (Global_Heap)))),
5272 Then_Statements => New_List (
5273 Build_Heap_Allocator
5274 (Temp_Id => Alloc_Obj_Id,
5275 Temp_Typ => Ref_Type,
5276 Func_Id => Func_Id,
5277 Ret_Typ => Ret_Obj_Typ,
5278 Alloc_Expr => Heap_Allocator)))),
5280 Else_Statements => New_List (
5281 Pool_Decl,
5282 Build_Heap_Allocator
5283 (Temp_Id => Alloc_Obj_Id,
5284 Temp_Typ => Ref_Type,
5285 Func_Id => Func_Id,
5286 Ret_Typ => Ret_Obj_Typ,
5287 Alloc_Expr => Pool_Allocator)));
5289 -- If a separate initialization assignment was created
5290 -- earlier, append that following the assignment of the
5291 -- implicit access formal to the access object, to ensure
5292 -- that the return object is initialized in that case. In
5293 -- this situation, the target of the assignment must be
5294 -- rewritten to denote a dereference of the access to the
5295 -- return object passed in by the caller.
5297 if Present (Init_Assignment) then
5298 Rewrite (Name (Init_Assignment),
5299 Make_Explicit_Dereference (Loc,
5300 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5302 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5304 Append_To
5305 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5306 end if;
5308 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5310 -- Remember the local access object for use in the
5311 -- dereference of the renaming created below.
5313 Obj_Acc_Formal := Alloc_Obj_Id;
5314 end;
5315 end if;
5317 -- Replace the return object declaration with a renaming of a
5318 -- dereference of the access value designating the return
5319 -- object.
5321 Obj_Acc_Deref :=
5322 Make_Explicit_Dereference (Loc,
5323 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5325 Rewrite (Ret_Obj_Decl,
5326 Make_Object_Renaming_Declaration (Loc,
5327 Defining_Identifier => Ret_Obj_Id,
5328 Access_Definition => Empty,
5329 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5330 Name => Obj_Acc_Deref));
5332 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5333 end;
5334 end if;
5336 -- Case where we do not build a block
5338 else
5339 -- We're about to drop Return_Object_Declarations on the floor, so
5340 -- we need to insert it, in case it got expanded into useful code.
5341 -- Remove side effects from expression, which may be duplicated in
5342 -- subsequent checks (see Expand_Simple_Function_Return).
5344 Insert_List_Before (N, Return_Object_Declarations (N));
5345 Remove_Side_Effects (Exp);
5347 -- Build simple_return_statement that returns the expression directly
5349 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5350 Result := Return_Stmt;
5351 end if;
5353 -- Set the flag to prevent infinite recursion
5355 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5357 Rewrite (N, Result);
5358 Analyze (N);
5359 end Expand_N_Extended_Return_Statement;
5361 ----------------------------
5362 -- Expand_N_Function_Call --
5363 ----------------------------
5365 procedure Expand_N_Function_Call (N : Node_Id) is
5366 begin
5367 Expand_Call (N);
5368 end Expand_N_Function_Call;
5370 ---------------------------------------
5371 -- Expand_N_Procedure_Call_Statement --
5372 ---------------------------------------
5374 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5375 begin
5376 Expand_Call (N);
5377 end Expand_N_Procedure_Call_Statement;
5379 --------------------------------------
5380 -- Expand_N_Simple_Return_Statement --
5381 --------------------------------------
5383 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5384 begin
5385 -- Defend against previous errors (i.e. the return statement calls a
5386 -- function that is not available in configurable runtime).
5388 if Present (Expression (N))
5389 and then Nkind (Expression (N)) = N_Empty
5390 then
5391 Check_Error_Detected;
5392 return;
5393 end if;
5395 -- Distinguish the function and non-function cases:
5397 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5398 when E_Function
5399 | E_Generic_Function
5401 Expand_Simple_Function_Return (N);
5403 when E_Entry
5404 | E_Entry_Family
5405 | E_Generic_Procedure
5406 | E_Procedure
5407 | E_Return_Statement
5409 Expand_Non_Function_Return (N);
5411 when others =>
5412 raise Program_Error;
5413 end case;
5415 exception
5416 when RE_Not_Available =>
5417 return;
5418 end Expand_N_Simple_Return_Statement;
5420 ------------------------------
5421 -- Expand_N_Subprogram_Body --
5422 ------------------------------
5424 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5425 -- by the back-end.
5427 -- Add dummy push/pop label nodes at start and end to clear any local
5428 -- exception indications if local-exception-to-goto optimization is active.
5430 -- Add return statement if last statement in body is not a return statement
5431 -- (this makes things easier on Gigi which does not want to have to handle
5432 -- a missing return).
5434 -- Add call to Activate_Tasks if body is a task activator
5436 -- Deal with possible detection of infinite recursion
5438 -- Eliminate body completely if convention stubbed
5440 -- Encode entity names within body, since we will not need to reference
5441 -- these entities any longer in the front end.
5443 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5445 -- Reset Pure indication if any parameter has root type System.Address
5446 -- or has any parameters of limited types, where limited means that the
5447 -- run-time view is limited (i.e. the full type is limited).
5449 -- Wrap thread body
5451 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5452 Body_Id : constant Entity_Id := Defining_Entity (N);
5453 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5454 Loc : constant Source_Ptr := Sloc (N);
5456 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5457 -- Append a return statement to the statement sequence Stmts if the last
5458 -- statement is not already a return or a goto statement. Note that the
5459 -- latter test is not critical, it does not matter if we add a few extra
5460 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5461 -- the corresponding spec of the subprogram body.
5463 ----------------
5464 -- Add_Return --
5465 ----------------
5467 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5468 Last_Stmt : Node_Id;
5469 Loc : Source_Ptr;
5470 Stmt : Node_Id;
5472 begin
5473 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5474 -- not relevant in this context since they are not executable.
5476 Last_Stmt := Last (Stmts);
5477 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5478 Prev (Last_Stmt);
5479 end loop;
5481 -- Now insert return unless last statement is a transfer
5483 if not Is_Transfer (Last_Stmt) then
5485 -- The source location for the return is the end label of the
5486 -- procedure if present. Otherwise use the sloc of the last
5487 -- statement in the list. If the list comes from a generated
5488 -- exception handler and we are not debugging generated code,
5489 -- all the statements within the handler are made invisible
5490 -- to the debugger.
5492 if Nkind (Parent (Stmts)) = N_Exception_Handler
5493 and then not Comes_From_Source (Parent (Stmts))
5494 then
5495 Loc := Sloc (Last_Stmt);
5496 elsif Present (End_Label (HSS)) then
5497 Loc := Sloc (End_Label (HSS));
5498 else
5499 Loc := Sloc (Last_Stmt);
5500 end if;
5502 -- Append return statement, and set analyzed manually. We can't
5503 -- call Analyze on this return since the scope is wrong.
5505 -- Note: it almost works to push the scope and then do the Analyze
5506 -- call, but something goes wrong in some weird cases and it is
5507 -- not worth worrying about ???
5509 Stmt := Make_Simple_Return_Statement (Loc);
5511 -- The return statement is handled properly, and the call to the
5512 -- postcondition, inserted below, does not require information
5513 -- from the body either. However, that call is analyzed in the
5514 -- enclosing scope, and an elaboration check might improperly be
5515 -- added to it. A guard in Sem_Elab is needed to prevent that
5516 -- spurious check, see Check_Elab_Call.
5518 Append_To (Stmts, Stmt);
5519 Set_Analyzed (Stmt);
5521 -- Call the _Postconditions procedure if the related subprogram
5522 -- has contract assertions that need to be verified on exit.
5524 if Ekind (Spec_Id) = E_Procedure
5525 and then Present (Postconditions_Proc (Spec_Id))
5526 then
5527 Insert_Action (Stmt,
5528 Make_Procedure_Call_Statement (Loc,
5529 Name =>
5530 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5531 end if;
5532 end if;
5533 end Add_Return;
5535 -- Local variables
5537 Except_H : Node_Id;
5538 L : List_Id;
5539 Spec_Id : Entity_Id;
5541 -- Start of processing for Expand_N_Subprogram_Body
5543 begin
5544 if Present (Corresponding_Spec (N)) then
5545 Spec_Id := Corresponding_Spec (N);
5546 else
5547 Spec_Id := Body_Id;
5548 end if;
5550 -- If this is a Pure function which has any parameters whose root type
5551 -- is System.Address, reset the Pure indication.
5552 -- This check is also performed when the subprogram is frozen, but we
5553 -- repeat it on the body so that the indication is consistent, and so
5554 -- it applies as well to bodies without separate specifications.
5556 if Is_Pure (Spec_Id)
5557 and then Is_Subprogram (Spec_Id)
5558 and then not Has_Pragma_Pure_Function (Spec_Id)
5559 then
5560 Check_Function_With_Address_Parameter (Spec_Id);
5562 if Spec_Id /= Body_Id then
5563 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5564 end if;
5565 end if;
5567 -- Set L to either the list of declarations if present, or to the list
5568 -- of statements if no declarations are present. This is used to insert
5569 -- new stuff at the start.
5571 if Is_Non_Empty_List (Declarations (N)) then
5572 L := Declarations (N);
5573 else
5574 L := Statements (HSS);
5575 end if;
5577 -- If local-exception-to-goto optimization active, insert dummy push
5578 -- statements at start, and dummy pop statements at end, but inhibit
5579 -- this if we have No_Exception_Handlers, since they are useless and
5580 -- intefere with analysis, e.g. by codepeer.
5582 if (Debug_Flag_Dot_G
5583 or else Restriction_Active (No_Exception_Propagation))
5584 and then not Restriction_Active (No_Exception_Handlers)
5585 and then not CodePeer_Mode
5586 and then Is_Non_Empty_List (L)
5587 then
5588 declare
5589 FS : constant Node_Id := First (L);
5590 FL : constant Source_Ptr := Sloc (FS);
5591 LS : Node_Id;
5592 LL : Source_Ptr;
5594 begin
5595 -- LS points to either last statement, if statements are present
5596 -- or to the last declaration if there are no statements present.
5597 -- It is the node after which the pop's are generated.
5599 if Is_Non_Empty_List (Statements (HSS)) then
5600 LS := Last (Statements (HSS));
5601 else
5602 LS := Last (L);
5603 end if;
5605 LL := Sloc (LS);
5607 Insert_List_Before_And_Analyze (FS, New_List (
5608 Make_Push_Constraint_Error_Label (FL),
5609 Make_Push_Program_Error_Label (FL),
5610 Make_Push_Storage_Error_Label (FL)));
5612 Insert_List_After_And_Analyze (LS, New_List (
5613 Make_Pop_Constraint_Error_Label (LL),
5614 Make_Pop_Program_Error_Label (LL),
5615 Make_Pop_Storage_Error_Label (LL)));
5616 end;
5617 end if;
5619 -- Need poll on entry to subprogram if polling enabled. We only do this
5620 -- for non-empty subprograms, since it does not seem necessary to poll
5621 -- for a dummy null subprogram.
5623 if Is_Non_Empty_List (L) then
5625 -- Do not add a polling call if the subprogram is to be inlined by
5626 -- the back-end, to avoid repeated calls with multiple inlinings.
5628 if Is_Inlined (Spec_Id)
5629 and then Front_End_Inlining
5630 and then Optimization_Level > 1
5631 then
5632 null;
5633 else
5634 Generate_Poll_Call (First (L));
5635 end if;
5636 end if;
5638 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5640 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5641 declare
5642 F : Entity_Id;
5643 A : Node_Id;
5645 begin
5646 -- Loop through formals
5648 F := First_Formal (Spec_Id);
5649 while Present (F) loop
5650 if Is_Scalar_Type (Etype (F))
5651 and then Ekind (F) = E_Out_Parameter
5652 then
5653 Check_Restriction (No_Default_Initialization, F);
5655 -- Insert the initialization. We turn off validity checks
5656 -- for this assignment, since we do not want any check on
5657 -- the initial value itself (which may well be invalid).
5658 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5660 A :=
5661 Make_Assignment_Statement (Loc,
5662 Name => New_Occurrence_Of (F, Loc),
5663 Expression => Get_Simple_Init_Val (Etype (F), N));
5664 Set_Suppress_Assignment_Checks (A);
5666 Insert_Before_And_Analyze (First (L),
5667 A, Suppress => Validity_Check);
5668 end if;
5670 Next_Formal (F);
5671 end loop;
5672 end;
5673 end if;
5675 -- Clear out statement list for stubbed procedure
5677 if Present (Corresponding_Spec (N)) then
5678 Set_Elaboration_Flag (N, Spec_Id);
5680 if Convention (Spec_Id) = Convention_Stubbed
5681 or else Is_Eliminated (Spec_Id)
5682 then
5683 Set_Declarations (N, Empty_List);
5684 Set_Handled_Statement_Sequence (N,
5685 Make_Handled_Sequence_Of_Statements (Loc,
5686 Statements => New_List (Make_Null_Statement (Loc))));
5688 return;
5689 end if;
5690 end if;
5692 -- Create a set of discriminals for the next protected subprogram body
5694 if Is_List_Member (N)
5695 and then Present (Parent (List_Containing (N)))
5696 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5697 and then Present (Next_Protected_Operation (N))
5698 then
5699 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5700 end if;
5702 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5703 -- subprograms with no specs are not frozen.
5705 declare
5706 Typ : constant Entity_Id := Etype (Spec_Id);
5707 Utyp : constant Entity_Id := Underlying_Type (Typ);
5709 begin
5710 if Is_Limited_View (Typ) then
5711 Set_Returns_By_Ref (Spec_Id);
5713 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5714 Set_Returns_By_Ref (Spec_Id);
5715 end if;
5716 end;
5718 -- For a procedure, we add a return for all possible syntactic ends of
5719 -- the subprogram.
5721 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5722 Add_Return (Spec_Id, Statements (HSS));
5724 if Present (Exception_Handlers (HSS)) then
5725 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5726 while Present (Except_H) loop
5727 Add_Return (Spec_Id, Statements (Except_H));
5728 Next_Non_Pragma (Except_H);
5729 end loop;
5730 end if;
5732 -- For a function, we must deal with the case where there is at least
5733 -- one missing return. What we do is to wrap the entire body of the
5734 -- function in a block:
5736 -- begin
5737 -- ...
5738 -- end;
5740 -- becomes
5742 -- begin
5743 -- begin
5744 -- ...
5745 -- end;
5747 -- raise Program_Error;
5748 -- end;
5750 -- This approach is necessary because the raise must be signalled to the
5751 -- caller, not handled by any local handler (RM 6.4(11)).
5753 -- Note: we do not need to analyze the constructed sequence here, since
5754 -- it has no handler, and an attempt to analyze the handled statement
5755 -- sequence twice is risky in various ways (e.g. the issue of expanding
5756 -- cleanup actions twice).
5758 elsif Has_Missing_Return (Spec_Id) then
5759 declare
5760 Hloc : constant Source_Ptr := Sloc (HSS);
5761 Blok : constant Node_Id :=
5762 Make_Block_Statement (Hloc,
5763 Handled_Statement_Sequence => HSS);
5764 Rais : constant Node_Id :=
5765 Make_Raise_Program_Error (Hloc,
5766 Reason => PE_Missing_Return);
5768 begin
5769 Set_Handled_Statement_Sequence (N,
5770 Make_Handled_Sequence_Of_Statements (Hloc,
5771 Statements => New_List (Blok, Rais)));
5773 Push_Scope (Spec_Id);
5774 Analyze (Blok);
5775 Analyze (Rais);
5776 Pop_Scope;
5777 end;
5778 end if;
5780 -- If subprogram contains a parameterless recursive call, then we may
5781 -- have an infinite recursion, so see if we can generate code to check
5782 -- for this possibility if storage checks are not suppressed.
5784 if Ekind (Spec_Id) = E_Procedure
5785 and then Has_Recursive_Call (Spec_Id)
5786 and then not Storage_Checks_Suppressed (Spec_Id)
5787 then
5788 Detect_Infinite_Recursion (N, Spec_Id);
5789 end if;
5791 -- Set to encode entity names in package body before gigi is called
5793 Qualify_Entity_Names (N);
5795 -- If the body belongs to a nonabstract library-level source primitive
5796 -- of a tagged type, install an elaboration check which ensures that a
5797 -- dispatching call targeting the primitive will not execute the body
5798 -- without it being previously elaborated.
5800 Install_Primitive_Elaboration_Check (N);
5801 end Expand_N_Subprogram_Body;
5803 -----------------------------------
5804 -- Expand_N_Subprogram_Body_Stub --
5805 -----------------------------------
5807 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5808 Bod : Node_Id;
5810 begin
5811 if Present (Corresponding_Body (N)) then
5812 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5814 -- The body may have been expanded already when it is analyzed
5815 -- through the subunit node. Do no expand again: it interferes
5816 -- with the construction of unnesting tables when generating C.
5818 if not Analyzed (Bod) then
5819 Expand_N_Subprogram_Body (Bod);
5820 end if;
5822 -- Add full qualification to entities that may be created late
5823 -- during unnesting.
5825 Qualify_Entity_Names (N);
5826 end if;
5827 end Expand_N_Subprogram_Body_Stub;
5829 -------------------------------------
5830 -- Expand_N_Subprogram_Declaration --
5831 -------------------------------------
5833 -- If the declaration appears within a protected body, it is a private
5834 -- operation of the protected type. We must create the corresponding
5835 -- protected subprogram an associated formals. For a normal protected
5836 -- operation, this is done when expanding the protected type declaration.
5838 -- If the declaration is for a null procedure, emit null body
5840 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5841 Loc : constant Source_Ptr := Sloc (N);
5842 Subp : constant Entity_Id := Defining_Entity (N);
5844 -- Local variables
5846 Scop : constant Entity_Id := Scope (Subp);
5847 Prot_Bod : Node_Id;
5848 Prot_Decl : Node_Id;
5849 Prot_Id : Entity_Id;
5851 -- Start of processing for Expand_N_Subprogram_Declaration
5853 begin
5854 -- In SPARK, subprogram declarations are only allowed in package
5855 -- specifications.
5857 if Nkind (Parent (N)) /= N_Package_Specification then
5858 if Nkind (Parent (N)) = N_Compilation_Unit then
5859 Check_SPARK_05_Restriction
5860 ("subprogram declaration is not a library item", N);
5862 elsif Present (Next (N))
5863 and then Nkind (Next (N)) = N_Pragma
5864 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5865 then
5866 -- In SPARK, subprogram declarations are also permitted in
5867 -- declarative parts when immediately followed by a corresponding
5868 -- pragma Import. We only check here that there is some pragma
5869 -- Import.
5871 null;
5872 else
5873 Check_SPARK_05_Restriction
5874 ("subprogram declaration is not allowed here", N);
5875 end if;
5876 end if;
5878 -- Deal with case of protected subprogram. Do not generate protected
5879 -- operation if operation is flagged as eliminated.
5881 if Is_List_Member (N)
5882 and then Present (Parent (List_Containing (N)))
5883 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5884 and then Is_Protected_Type (Scop)
5885 then
5886 if No (Protected_Body_Subprogram (Subp))
5887 and then not Is_Eliminated (Subp)
5888 then
5889 Prot_Decl :=
5890 Make_Subprogram_Declaration (Loc,
5891 Specification =>
5892 Build_Protected_Sub_Specification
5893 (N, Scop, Unprotected_Mode));
5895 -- The protected subprogram is declared outside of the protected
5896 -- body. Given that the body has frozen all entities so far, we
5897 -- analyze the subprogram and perform freezing actions explicitly.
5898 -- including the generation of an explicit freeze node, to ensure
5899 -- that gigi has the proper order of elaboration.
5900 -- If the body is a subunit, the insertion point is before the
5901 -- stub in the parent.
5903 Prot_Bod := Parent (List_Containing (N));
5905 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5906 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5907 end if;
5909 Insert_Before (Prot_Bod, Prot_Decl);
5910 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5911 Set_Has_Delayed_Freeze (Prot_Id);
5913 Push_Scope (Scope (Scop));
5914 Analyze (Prot_Decl);
5915 Freeze_Before (N, Prot_Id);
5916 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5918 -- Create protected operation as well. Even though the operation
5919 -- is only accessible within the body, it is possible to make it
5920 -- available outside of the protected object by using 'Access to
5921 -- provide a callback, so build protected version in all cases.
5923 Prot_Decl :=
5924 Make_Subprogram_Declaration (Loc,
5925 Specification =>
5926 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5927 Insert_Before (Prot_Bod, Prot_Decl);
5928 Analyze (Prot_Decl);
5930 Pop_Scope;
5931 end if;
5933 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5934 -- cases this is superfluous because calls to it will be automatically
5935 -- inlined, but we definitely need the body if preconditions for the
5936 -- procedure are present, or if performing coverage analysis.
5938 elsif Nkind (Specification (N)) = N_Procedure_Specification
5939 and then Null_Present (Specification (N))
5940 then
5941 declare
5942 Bod : constant Node_Id := Body_To_Inline (N);
5944 begin
5945 Set_Has_Completion (Subp, False);
5946 Append_Freeze_Action (Subp, Bod);
5948 -- The body now contains raise statements, so calls to it will
5949 -- not be inlined.
5951 Set_Is_Inlined (Subp, False);
5952 end;
5953 end if;
5955 -- When generating C code, transform a function that returns a
5956 -- constrained array type into a procedure with an out parameter
5957 -- that carries the return value.
5959 -- We skip this transformation for unchecked conversions, since they
5960 -- are not needed by the C generator (and this also produces cleaner
5961 -- output).
5963 if Modify_Tree_For_C
5964 and then Nkind (Specification (N)) = N_Function_Specification
5965 and then Is_Array_Type (Etype (Subp))
5966 and then Is_Constrained (Etype (Subp))
5967 and then not Is_Unchecked_Conversion_Instance (Subp)
5968 then
5969 Build_Procedure_Form (N);
5970 end if;
5971 end Expand_N_Subprogram_Declaration;
5973 --------------------------------
5974 -- Expand_Non_Function_Return --
5975 --------------------------------
5977 procedure Expand_Non_Function_Return (N : Node_Id) is
5978 pragma Assert (No (Expression (N)));
5980 Loc : constant Source_Ptr := Sloc (N);
5981 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5982 Kind : constant Entity_Kind := Ekind (Scope_Id);
5983 Call : Node_Id;
5984 Acc_Stat : Node_Id;
5985 Goto_Stat : Node_Id;
5986 Lab_Node : Node_Id;
5988 begin
5989 -- Call the _Postconditions procedure if the related subprogram has
5990 -- contract assertions that need to be verified on exit.
5992 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5993 and then Present (Postconditions_Proc (Scope_Id))
5994 then
5995 Insert_Action (N,
5996 Make_Procedure_Call_Statement (Loc,
5997 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5998 end if;
6000 -- If it is a return from a procedure do no extra steps
6002 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6003 return;
6005 -- If it is a nested return within an extended one, replace it with a
6006 -- return of the previously declared return object.
6008 elsif Kind = E_Return_Statement then
6009 Rewrite (N,
6010 Make_Simple_Return_Statement (Loc,
6011 Expression =>
6012 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6013 Set_Comes_From_Extended_Return_Statement (N);
6014 Set_Return_Statement_Entity (N, Scope_Id);
6015 Expand_Simple_Function_Return (N);
6016 return;
6017 end if;
6019 pragma Assert (Is_Entry (Scope_Id));
6021 -- Look at the enclosing block to see whether the return is from an
6022 -- accept statement or an entry body.
6024 for J in reverse 0 .. Scope_Stack.Last loop
6025 Scope_Id := Scope_Stack.Table (J).Entity;
6026 exit when Is_Concurrent_Type (Scope_Id);
6027 end loop;
6029 -- If it is a return from accept statement it is expanded as call to
6030 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6032 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6033 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6035 if Is_Task_Type (Scope_Id) then
6037 Call :=
6038 Make_Procedure_Call_Statement (Loc,
6039 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6040 Insert_Before (N, Call);
6041 -- why not insert actions here???
6042 Analyze (Call);
6044 Acc_Stat := Parent (N);
6045 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6046 Acc_Stat := Parent (Acc_Stat);
6047 end loop;
6049 Lab_Node := Last (Statements
6050 (Handled_Statement_Sequence (Acc_Stat)));
6052 Goto_Stat := Make_Goto_Statement (Loc,
6053 Name => New_Occurrence_Of
6054 (Entity (Identifier (Lab_Node)), Loc));
6056 Set_Analyzed (Goto_Stat);
6058 Rewrite (N, Goto_Stat);
6059 Analyze (N);
6061 -- If it is a return from an entry body, put a Complete_Entry_Body call
6062 -- in front of the return.
6064 elsif Is_Protected_Type (Scope_Id) then
6065 Call :=
6066 Make_Procedure_Call_Statement (Loc,
6067 Name =>
6068 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6069 Parameter_Associations => New_List (
6070 Make_Attribute_Reference (Loc,
6071 Prefix =>
6072 New_Occurrence_Of
6073 (Find_Protection_Object (Current_Scope), Loc),
6074 Attribute_Name => Name_Unchecked_Access)));
6076 Insert_Before (N, Call);
6077 Analyze (Call);
6078 end if;
6079 end Expand_Non_Function_Return;
6081 ---------------------------------------
6082 -- Expand_Protected_Object_Reference --
6083 ---------------------------------------
6085 function Expand_Protected_Object_Reference
6086 (N : Node_Id;
6087 Scop : Entity_Id) return Node_Id
6089 Loc : constant Source_Ptr := Sloc (N);
6090 Corr : Entity_Id;
6091 Rec : Node_Id;
6092 Param : Entity_Id;
6093 Proc : Entity_Id;
6095 begin
6096 Rec := Make_Identifier (Loc, Name_uObject);
6097 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6099 -- Find enclosing protected operation, and retrieve its first parameter,
6100 -- which denotes the enclosing protected object. If the enclosing
6101 -- operation is an entry, we are immediately within the protected body,
6102 -- and we can retrieve the object from the service entries procedure. A
6103 -- barrier function has the same signature as an entry. A barrier
6104 -- function is compiled within the protected object, but unlike
6105 -- protected operations its never needs locks, so that its protected
6106 -- body subprogram points to itself.
6108 Proc := Current_Scope;
6109 while Present (Proc)
6110 and then Scope (Proc) /= Scop
6111 loop
6112 Proc := Scope (Proc);
6113 end loop;
6115 Corr := Protected_Body_Subprogram (Proc);
6117 if No (Corr) then
6119 -- Previous error left expansion incomplete.
6120 -- Nothing to do on this call.
6122 return Empty;
6123 end if;
6125 Param :=
6126 Defining_Identifier
6127 (First (Parameter_Specifications (Parent (Corr))));
6129 if Is_Subprogram (Proc) and then Proc /= Corr then
6131 -- Protected function or procedure
6133 Set_Entity (Rec, Param);
6135 -- Rec is a reference to an entity which will not be in scope when
6136 -- the call is reanalyzed, and needs no further analysis.
6138 Set_Analyzed (Rec);
6140 else
6141 -- Entry or barrier function for entry body. The first parameter of
6142 -- the entry body procedure is pointer to the object. We create a
6143 -- local variable of the proper type, duplicating what is done to
6144 -- define _object later on.
6146 declare
6147 Decls : List_Id;
6148 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6150 begin
6151 Decls := New_List (
6152 Make_Full_Type_Declaration (Loc,
6153 Defining_Identifier => Obj_Ptr,
6154 Type_Definition =>
6155 Make_Access_To_Object_Definition (Loc,
6156 Subtype_Indication =>
6157 New_Occurrence_Of
6158 (Corresponding_Record_Type (Scop), Loc))));
6160 Insert_Actions (N, Decls);
6161 Freeze_Before (N, Obj_Ptr);
6163 Rec :=
6164 Make_Explicit_Dereference (Loc,
6165 Prefix =>
6166 Unchecked_Convert_To (Obj_Ptr,
6167 New_Occurrence_Of (Param, Loc)));
6169 -- Analyze new actual. Other actuals in calls are already analyzed
6170 -- and the list of actuals is not reanalyzed after rewriting.
6172 Set_Parent (Rec, N);
6173 Analyze (Rec);
6174 end;
6175 end if;
6177 return Rec;
6178 end Expand_Protected_Object_Reference;
6180 --------------------------------------
6181 -- Expand_Protected_Subprogram_Call --
6182 --------------------------------------
6184 procedure Expand_Protected_Subprogram_Call
6185 (N : Node_Id;
6186 Subp : Entity_Id;
6187 Scop : Entity_Id)
6189 Rec : Node_Id;
6191 procedure Expand_Internal_Init_Call;
6192 -- A call to an operation of the type may occur in the initialization
6193 -- of a private component. In that case the prefix of the call is an
6194 -- entity name and the call is treated as internal even though it
6195 -- appears in code outside of the protected type.
6197 procedure Freeze_Called_Function;
6198 -- If it is a function call it can appear in elaboration code and
6199 -- the called entity must be frozen before the call. This must be
6200 -- done before the call is expanded, as the expansion may rewrite it
6201 -- to something other than a call (e.g. a temporary initialized in a
6202 -- transient block).
6204 -------------------------------
6205 -- Expand_Internal_Init_Call --
6206 -------------------------------
6208 procedure Expand_Internal_Init_Call is
6209 begin
6210 -- If the context is a protected object (rather than a protected
6211 -- type) the call itself is bound to raise program_error because
6212 -- the protected body will not have been elaborated yet. This is
6213 -- diagnosed subsequently in Sem_Elab.
6215 Freeze_Called_Function;
6217 -- The target of the internal call is the first formal of the
6218 -- enclosing initialization procedure.
6220 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6221 Build_Protected_Subprogram_Call (N,
6222 Name => Name (N),
6223 Rec => Rec,
6224 External => False);
6225 Analyze (N);
6226 Resolve (N, Etype (Subp));
6227 end Expand_Internal_Init_Call;
6229 ----------------------------
6230 -- Freeze_Called_Function --
6231 ----------------------------
6233 procedure Freeze_Called_Function is
6234 begin
6235 if Ekind (Subp) = E_Function then
6236 Freeze_Expression (Name (N));
6237 end if;
6238 end Freeze_Called_Function;
6240 -- Start of processing for Expand_Protected_Subprogram_Call
6242 begin
6243 -- If the protected object is not an enclosing scope, this is an inter-
6244 -- object function call. Inter-object procedure calls are expanded by
6245 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6246 -- subprogram being called is in the protected body being compiled, and
6247 -- if the protected object in the call is statically the enclosing type.
6248 -- The object may be a component of some other data structure, in which
6249 -- case this must be handled as an inter-object call.
6251 if not In_Open_Scopes (Scop)
6252 or else Is_Entry_Wrapper (Current_Scope)
6253 or else not Is_Entity_Name (Name (N))
6254 then
6255 if Nkind (Name (N)) = N_Selected_Component then
6256 Rec := Prefix (Name (N));
6258 elsif Nkind (Name (N)) = N_Indexed_Component then
6259 Rec := Prefix (Prefix (Name (N)));
6261 -- If this is a call within an entry wrapper, it appears within a
6262 -- precondition that calls another primitive of the synchronized
6263 -- type. The target object of the call is the first actual on the
6264 -- wrapper. Note that this is an external call, because the wrapper
6265 -- is called outside of the synchronized object. This means that
6266 -- an entry call to an entry with preconditions involves two
6267 -- synchronized operations.
6269 elsif Ekind (Current_Scope) = E_Procedure
6270 and then Is_Entry_Wrapper (Current_Scope)
6271 then
6272 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6274 else
6275 -- If the context is the initialization procedure for a protected
6276 -- type, the call is legal because the called entity must be a
6277 -- function of that enclosing type, and this is treated as an
6278 -- internal call.
6280 pragma Assert
6281 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6283 Expand_Internal_Init_Call;
6284 return;
6285 end if;
6287 Freeze_Called_Function;
6288 Build_Protected_Subprogram_Call (N,
6289 Name => New_Occurrence_Of (Subp, Sloc (N)),
6290 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6291 External => True);
6293 else
6294 Rec := Expand_Protected_Object_Reference (N, Scop);
6296 if No (Rec) then
6297 return;
6298 end if;
6300 Freeze_Called_Function;
6301 Build_Protected_Subprogram_Call (N,
6302 Name => Name (N),
6303 Rec => Rec,
6304 External => False);
6305 end if;
6307 -- Analyze and resolve the new call. The actuals have already been
6308 -- resolved, but expansion of a function call will add extra actuals
6309 -- if needed. Analysis of a procedure call already includes resolution.
6311 Analyze (N);
6313 if Ekind (Subp) = E_Function then
6314 Resolve (N, Etype (Subp));
6315 end if;
6316 end Expand_Protected_Subprogram_Call;
6318 -----------------------------------
6319 -- Expand_Simple_Function_Return --
6320 -----------------------------------
6322 -- The "simple" comes from the syntax rule simple_return_statement. The
6323 -- semantics are not at all simple.
6325 procedure Expand_Simple_Function_Return (N : Node_Id) is
6326 Loc : constant Source_Ptr := Sloc (N);
6328 Scope_Id : constant Entity_Id :=
6329 Return_Applies_To (Return_Statement_Entity (N));
6330 -- The function we are returning from
6332 R_Type : constant Entity_Id := Etype (Scope_Id);
6333 -- The result type of the function
6335 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6337 Exp : Node_Id := Expression (N);
6338 pragma Assert (Present (Exp));
6340 Exptyp : constant Entity_Id := Etype (Exp);
6341 -- The type of the expression (not necessarily the same as R_Type)
6343 Subtype_Ind : Node_Id;
6344 -- If the result type of the function is class-wide and the expression
6345 -- has a specific type, then we use the expression's type as the type of
6346 -- the return object. In cases where the expression is an aggregate that
6347 -- is built in place, this avoids the need for an expensive conversion
6348 -- of the return object to the specific type on assignments to the
6349 -- individual components.
6351 begin
6352 if Is_Class_Wide_Type (R_Type)
6353 and then not Is_Class_Wide_Type (Exptyp)
6354 and then Nkind (Exp) /= N_Type_Conversion
6355 then
6356 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6357 else
6358 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6360 -- If the result type is class-wide and the expression is a view
6361 -- conversion, the conversion plays no role in the expansion because
6362 -- it does not modify the tag of the object. Remove the conversion
6363 -- altogether to prevent tag overwriting.
6365 if Is_Class_Wide_Type (R_Type)
6366 and then not Is_Class_Wide_Type (Exptyp)
6367 and then Nkind (Exp) = N_Type_Conversion
6368 then
6369 Exp := Expression (Exp);
6370 end if;
6371 end if;
6373 -- For the case of a simple return that does not come from an extended
6374 -- return, in the case of Ada 2005 where we are returning a limited
6375 -- type, we rewrite "return <expression>;" to be:
6377 -- return _anon_ : <return_subtype> := <expression>
6379 -- The expansion produced by Expand_N_Extended_Return_Statement will
6380 -- contain simple return statements (for example, a block containing
6381 -- simple return of the return object), which brings us back here with
6382 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6383 -- checking for a simple return that does not come from an extended
6384 -- return is to avoid this infinite recursion.
6386 -- The reason for this design is that for Ada 2005 limited returns, we
6387 -- need to reify the return object, so we can build it "in place", and
6388 -- we need a block statement to hang finalization and tasking stuff.
6390 -- ??? In order to avoid disruption, we avoid translating to extended
6391 -- return except in the cases where we really need to (Ada 2005 for
6392 -- inherently limited). We might prefer to do this translation in all
6393 -- cases (except perhaps for the case of Ada 95 inherently limited),
6394 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6395 -- code. This would also allow us to do the build-in-place optimization
6396 -- for efficiency even in cases where it is semantically not required.
6398 -- As before, we check the type of the return expression rather than the
6399 -- return type of the function, because the latter may be a limited
6400 -- class-wide interface type, which is not a limited type, even though
6401 -- the type of the expression may be.
6403 if not Comes_From_Extended_Return_Statement (N)
6404 and then Is_Limited_View (Etype (Expression (N)))
6405 and then Ada_Version >= Ada_2005
6406 and then not Debug_Flag_Dot_L
6408 -- The functionality of interface thunks is simple and it is always
6409 -- handled by means of simple return statements. This leaves their
6410 -- expansion simple and clean.
6412 and then not Is_Thunk (Current_Scope)
6413 then
6414 declare
6415 Return_Object_Entity : constant Entity_Id :=
6416 Make_Temporary (Loc, 'R', Exp);
6418 Obj_Decl : constant Node_Id :=
6419 Make_Object_Declaration (Loc,
6420 Defining_Identifier => Return_Object_Entity,
6421 Object_Definition => Subtype_Ind,
6422 Expression => Exp);
6424 Ext : constant Node_Id :=
6425 Make_Extended_Return_Statement (Loc,
6426 Return_Object_Declarations => New_List (Obj_Decl));
6427 -- Do not perform this high-level optimization if the result type
6428 -- is an interface because the "this" pointer must be displaced.
6430 begin
6431 Rewrite (N, Ext);
6432 Analyze (N);
6433 return;
6434 end;
6435 end if;
6437 -- Here we have a simple return statement that is part of the expansion
6438 -- of an extended return statement (either written by the user, or
6439 -- generated by the above code).
6441 -- Always normalize C/Fortran boolean result. This is not always needed,
6442 -- but it seems a good idea to minimize the passing around of non-
6443 -- normalized values, and in any case this handles the processing of
6444 -- barrier functions for protected types, which turn the condition into
6445 -- a return statement.
6447 if Is_Boolean_Type (Exptyp)
6448 and then Nonzero_Is_True (Exptyp)
6449 then
6450 Adjust_Condition (Exp);
6451 Adjust_Result_Type (Exp, Exptyp);
6452 end if;
6454 -- Do validity check if enabled for returns
6456 if Validity_Checks_On
6457 and then Validity_Check_Returns
6458 then
6459 Ensure_Valid (Exp);
6460 end if;
6462 -- Check the result expression of a scalar function against the subtype
6463 -- of the function by inserting a conversion. This conversion must
6464 -- eventually be performed for other classes of types, but for now it's
6465 -- only done for scalars.
6466 -- ???
6468 if Is_Scalar_Type (Exptyp) then
6469 Rewrite (Exp, Convert_To (R_Type, Exp));
6471 -- The expression is resolved to ensure that the conversion gets
6472 -- expanded to generate a possible constraint check.
6474 Analyze_And_Resolve (Exp, R_Type);
6475 end if;
6477 -- Deal with returning variable length objects and controlled types
6479 -- Nothing to do if we are returning by reference, or this is not a
6480 -- type that requires special processing (indicated by the fact that
6481 -- it requires a cleanup scope for the secondary stack case).
6483 if Is_Limited_View (Exptyp)
6484 or else Is_Limited_Interface (Exptyp)
6485 then
6486 null;
6488 -- No copy needed for thunks returning interface type objects since
6489 -- the object is returned by reference and the maximum functionality
6490 -- required is just to displace the pointer.
6492 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6493 null;
6495 -- If the call is within a thunk and the type is a limited view, the
6496 -- backend will eventually see the non-limited view of the type.
6498 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6499 return;
6501 elsif not Requires_Transient_Scope (R_Type) then
6503 -- Mutable records with variable-length components are not returned
6504 -- on the sec-stack, so we need to make sure that the back end will
6505 -- only copy back the size of the actual value, and not the maximum
6506 -- size. We create an actual subtype for this purpose. However we
6507 -- need not do it if the expression is a function call since this
6508 -- will be done in the called function and doing it here too would
6509 -- cause a temporary with maximum size to be created.
6511 declare
6512 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6513 Decl : Node_Id;
6514 Ent : Entity_Id;
6515 begin
6516 if Nkind (Exp) /= N_Function_Call
6517 and then Has_Discriminants (Ubt)
6518 and then not Is_Constrained (Ubt)
6519 and then not Has_Unchecked_Union (Ubt)
6520 then
6521 Decl := Build_Actual_Subtype (Ubt, Exp);
6522 Ent := Defining_Identifier (Decl);
6523 Insert_Action (Exp, Decl);
6524 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6525 Analyze_And_Resolve (Exp);
6526 end if;
6527 end;
6529 -- Here if secondary stack is used
6531 else
6532 -- Prevent the reclamation of the secondary stack by all enclosing
6533 -- blocks and loops as well as the related function; otherwise the
6534 -- result would be reclaimed too early.
6536 Set_Enclosing_Sec_Stack_Return (N);
6538 -- Optimize the case where the result is a function call. In this
6539 -- case either the result is already on the secondary stack, or is
6540 -- already being returned with the stack pointer depressed and no
6541 -- further processing is required except to set the By_Ref flag
6542 -- to ensure that gigi does not attempt an extra unnecessary copy.
6543 -- (actually not just unnecessary but harmfully wrong in the case
6544 -- of a controlled type, where gigi does not know how to do a copy).
6545 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6546 -- for array types if the constrained status of the target type is
6547 -- different from that of the expression.
6549 if Requires_Transient_Scope (Exptyp)
6550 and then
6551 (not Is_Array_Type (Exptyp)
6552 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6553 or else CW_Or_Has_Controlled_Part (Utyp))
6554 and then Nkind (Exp) = N_Function_Call
6555 then
6556 Set_By_Ref (N);
6558 -- Remove side effects from the expression now so that other parts
6559 -- of the expander do not have to reanalyze this node without this
6560 -- optimization
6562 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6564 -- Ada 2005 (AI-251): If the type of the returned object is
6565 -- an interface then add an implicit type conversion to force
6566 -- displacement of the "this" pointer.
6568 if Is_Interface (R_Type) then
6569 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6570 end if;
6572 Analyze_And_Resolve (Exp, R_Type);
6574 -- For controlled types, do the allocation on the secondary stack
6575 -- manually in order to call adjust at the right time:
6577 -- type Anon1 is access R_Type;
6578 -- for Anon1'Storage_pool use ss_pool;
6579 -- Anon2 : anon1 := new R_Type'(expr);
6580 -- return Anon2.all;
6582 -- We do the same for classwide types that are not potentially
6583 -- controlled (by the virtue of restriction No_Finalization) because
6584 -- gigi is not able to properly allocate class-wide types.
6586 elsif CW_Or_Has_Controlled_Part (Utyp) then
6587 declare
6588 Loc : constant Source_Ptr := Sloc (N);
6589 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6590 Alloc_Node : Node_Id;
6591 Temp : Entity_Id;
6593 begin
6594 Set_Ekind (Acc_Typ, E_Access_Type);
6596 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6598 -- This is an allocator for the secondary stack, and it's fine
6599 -- to have Comes_From_Source set False on it, as gigi knows not
6600 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6602 Alloc_Node :=
6603 Make_Allocator (Loc,
6604 Expression =>
6605 Make_Qualified_Expression (Loc,
6606 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6607 Expression => Relocate_Node (Exp)));
6609 -- We do not want discriminant checks on the declaration,
6610 -- given that it gets its value from the allocator.
6612 Set_No_Initialization (Alloc_Node);
6614 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6616 Insert_List_Before_And_Analyze (N, New_List (
6617 Make_Full_Type_Declaration (Loc,
6618 Defining_Identifier => Acc_Typ,
6619 Type_Definition =>
6620 Make_Access_To_Object_Definition (Loc,
6621 Subtype_Indication => Subtype_Ind)),
6623 Make_Object_Declaration (Loc,
6624 Defining_Identifier => Temp,
6625 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6626 Expression => Alloc_Node)));
6628 Rewrite (Exp,
6629 Make_Explicit_Dereference (Loc,
6630 Prefix => New_Occurrence_Of (Temp, Loc)));
6632 -- Ada 2005 (AI-251): If the type of the returned object is
6633 -- an interface then add an implicit type conversion to force
6634 -- displacement of the "this" pointer.
6636 if Is_Interface (R_Type) then
6637 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6638 end if;
6640 Analyze_And_Resolve (Exp, R_Type);
6641 end;
6643 -- Otherwise use the gigi mechanism to allocate result on the
6644 -- secondary stack.
6646 else
6647 Check_Restriction (No_Secondary_Stack, N);
6648 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6649 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6650 end if;
6651 end if;
6653 -- Implement the rules of 6.5(8-10), which require a tag check in
6654 -- the case of a limited tagged return type, and tag reassignment for
6655 -- nonlimited tagged results. These actions are needed when the return
6656 -- type is a specific tagged type and the result expression is a
6657 -- conversion or a formal parameter, because in that case the tag of
6658 -- the expression might differ from the tag of the specific result type.
6660 if Is_Tagged_Type (Utyp)
6661 and then not Is_Class_Wide_Type (Utyp)
6662 and then (Nkind_In (Exp, N_Type_Conversion,
6663 N_Unchecked_Type_Conversion)
6664 or else (Is_Entity_Name (Exp)
6665 and then Ekind (Entity (Exp)) in Formal_Kind))
6666 then
6667 -- When the return type is limited, perform a check that the tag of
6668 -- the result is the same as the tag of the return type.
6670 if Is_Limited_Type (R_Type) then
6671 Insert_Action (Exp,
6672 Make_Raise_Constraint_Error (Loc,
6673 Condition =>
6674 Make_Op_Ne (Loc,
6675 Left_Opnd =>
6676 Make_Selected_Component (Loc,
6677 Prefix => Duplicate_Subexpr (Exp),
6678 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6679 Right_Opnd =>
6680 Make_Attribute_Reference (Loc,
6681 Prefix =>
6682 New_Occurrence_Of (Base_Type (Utyp), Loc),
6683 Attribute_Name => Name_Tag)),
6684 Reason => CE_Tag_Check_Failed));
6686 -- If the result type is a specific nonlimited tagged type, then we
6687 -- have to ensure that the tag of the result is that of the result
6688 -- type. This is handled by making a copy of the expression in
6689 -- the case where it might have a different tag, namely when the
6690 -- expression is a conversion or a formal parameter. We create a new
6691 -- object of the result type and initialize it from the expression,
6692 -- which will implicitly force the tag to be set appropriately.
6694 else
6695 declare
6696 ExpR : constant Node_Id := Relocate_Node (Exp);
6697 Result_Id : constant Entity_Id :=
6698 Make_Temporary (Loc, 'R', ExpR);
6699 Result_Exp : constant Node_Id :=
6700 New_Occurrence_Of (Result_Id, Loc);
6701 Result_Obj : constant Node_Id :=
6702 Make_Object_Declaration (Loc,
6703 Defining_Identifier => Result_Id,
6704 Object_Definition =>
6705 New_Occurrence_Of (R_Type, Loc),
6706 Constant_Present => True,
6707 Expression => ExpR);
6709 begin
6710 Set_Assignment_OK (Result_Obj);
6711 Insert_Action (Exp, Result_Obj);
6713 Rewrite (Exp, Result_Exp);
6714 Analyze_And_Resolve (Exp, R_Type);
6715 end;
6716 end if;
6718 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6719 -- a check that the level of the return expression's underlying type
6720 -- is not deeper than the level of the master enclosing the function.
6721 -- Always generate the check when the type of the return expression
6722 -- is class-wide, when it's a type conversion, or when it's a formal
6723 -- parameter. Otherwise, suppress the check in the case where the
6724 -- return expression has a specific type whose level is known not to
6725 -- be statically deeper than the function's result type.
6727 -- No runtime check needed in interface thunks since it is performed
6728 -- by the target primitive associated with the thunk.
6730 -- Note: accessibility check is skipped in the VM case, since there
6731 -- does not seem to be any practical way to implement this check.
6733 elsif Ada_Version >= Ada_2005
6734 and then Tagged_Type_Expansion
6735 and then Is_Class_Wide_Type (R_Type)
6736 and then not Is_Thunk (Current_Scope)
6737 and then not Scope_Suppress.Suppress (Accessibility_Check)
6738 and then
6739 (Is_Class_Wide_Type (Etype (Exp))
6740 or else Nkind_In (Exp, N_Type_Conversion,
6741 N_Unchecked_Type_Conversion)
6742 or else (Is_Entity_Name (Exp)
6743 and then Ekind (Entity (Exp)) in Formal_Kind)
6744 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6745 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6746 then
6747 declare
6748 Tag_Node : Node_Id;
6750 begin
6751 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6752 -- "this" to reference the base of the object. This is required to
6753 -- get access to the TSD of the object.
6755 if Is_Class_Wide_Type (Etype (Exp))
6756 and then Is_Interface (Etype (Exp))
6757 then
6758 -- If the expression is an explicit dereference then we can
6759 -- directly displace the pointer to reference the base of
6760 -- the object.
6762 if Nkind (Exp) = N_Explicit_Dereference then
6763 Tag_Node :=
6764 Make_Explicit_Dereference (Loc,
6765 Prefix =>
6766 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6767 Make_Function_Call (Loc,
6768 Name =>
6769 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6770 Parameter_Associations => New_List (
6771 Unchecked_Convert_To (RTE (RE_Address),
6772 Duplicate_Subexpr (Prefix (Exp)))))));
6774 -- Similar case to the previous one but the expression is a
6775 -- renaming of an explicit dereference.
6777 elsif Nkind (Exp) = N_Identifier
6778 and then Present (Renamed_Object (Entity (Exp)))
6779 and then Nkind (Renamed_Object (Entity (Exp)))
6780 = N_Explicit_Dereference
6781 then
6782 Tag_Node :=
6783 Make_Explicit_Dereference (Loc,
6784 Prefix =>
6785 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6786 Make_Function_Call (Loc,
6787 Name =>
6788 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6789 Parameter_Associations => New_List (
6790 Unchecked_Convert_To (RTE (RE_Address),
6791 Duplicate_Subexpr
6792 (Prefix
6793 (Renamed_Object (Entity (Exp)))))))));
6795 -- Common case: obtain the address of the actual object and
6796 -- displace the pointer to reference the base of the object.
6798 else
6799 Tag_Node :=
6800 Make_Explicit_Dereference (Loc,
6801 Prefix =>
6802 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6803 Make_Function_Call (Loc,
6804 Name =>
6805 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6806 Parameter_Associations => New_List (
6807 Make_Attribute_Reference (Loc,
6808 Prefix => Duplicate_Subexpr (Exp),
6809 Attribute_Name => Name_Address)))));
6810 end if;
6811 else
6812 Tag_Node :=
6813 Make_Attribute_Reference (Loc,
6814 Prefix => Duplicate_Subexpr (Exp),
6815 Attribute_Name => Name_Tag);
6816 end if;
6818 -- CodePeer does not do anything useful with
6819 -- Ada.Tags.Type_Specific_Data components.
6821 if not CodePeer_Mode then
6822 Insert_Action (Exp,
6823 Make_Raise_Program_Error (Loc,
6824 Condition =>
6825 Make_Op_Gt (Loc,
6826 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6827 Right_Opnd =>
6828 Make_Integer_Literal (Loc,
6829 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6830 Reason => PE_Accessibility_Check_Failed));
6831 end if;
6832 end;
6834 -- AI05-0073: If function has a controlling access result, check that
6835 -- the tag of the return value, if it is not null, matches designated
6836 -- type of return type.
6838 -- The return expression is referenced twice in the code below, so it
6839 -- must be made free of side effects. Given that different compilers
6840 -- may evaluate these parameters in different order, both occurrences
6841 -- perform a copy.
6843 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6844 and then Has_Controlling_Result (Scope_Id)
6845 then
6846 Insert_Action (N,
6847 Make_Raise_Constraint_Error (Loc,
6848 Condition =>
6849 Make_And_Then (Loc,
6850 Left_Opnd =>
6851 Make_Op_Ne (Loc,
6852 Left_Opnd => Duplicate_Subexpr (Exp),
6853 Right_Opnd => Make_Null (Loc)),
6855 Right_Opnd => Make_Op_Ne (Loc,
6856 Left_Opnd =>
6857 Make_Selected_Component (Loc,
6858 Prefix => Duplicate_Subexpr (Exp),
6859 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6861 Right_Opnd =>
6862 Make_Attribute_Reference (Loc,
6863 Prefix =>
6864 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6865 Attribute_Name => Name_Tag))),
6867 Reason => CE_Tag_Check_Failed),
6868 Suppress => All_Checks);
6869 end if;
6871 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6872 -- ensure that the function result does not outlive an
6873 -- object designated by one of it discriminants.
6875 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6876 and then Has_Unconstrained_Access_Discriminants (R_Type)
6877 then
6878 declare
6879 Discrim_Source : Node_Id;
6881 procedure Check_Against_Result_Level (Level : Node_Id);
6882 -- Check the given accessibility level against the level
6883 -- determined by the point of call. (AI05-0234).
6885 --------------------------------
6886 -- Check_Against_Result_Level --
6887 --------------------------------
6889 procedure Check_Against_Result_Level (Level : Node_Id) is
6890 begin
6891 Insert_Action (N,
6892 Make_Raise_Program_Error (Loc,
6893 Condition =>
6894 Make_Op_Gt (Loc,
6895 Left_Opnd => Level,
6896 Right_Opnd =>
6897 New_Occurrence_Of
6898 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6899 Reason => PE_Accessibility_Check_Failed));
6900 end Check_Against_Result_Level;
6902 begin
6903 Discrim_Source := Exp;
6904 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6905 Discrim_Source := Expression (Discrim_Source);
6906 end loop;
6908 if Nkind (Discrim_Source) = N_Identifier
6909 and then Is_Return_Object (Entity (Discrim_Source))
6910 then
6911 Discrim_Source := Entity (Discrim_Source);
6913 if Is_Constrained (Etype (Discrim_Source)) then
6914 Discrim_Source := Etype (Discrim_Source);
6915 else
6916 Discrim_Source := Expression (Parent (Discrim_Source));
6917 end if;
6919 elsif Nkind (Discrim_Source) = N_Identifier
6920 and then Nkind_In (Original_Node (Discrim_Source),
6921 N_Aggregate, N_Extension_Aggregate)
6922 then
6923 Discrim_Source := Original_Node (Discrim_Source);
6925 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6926 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6927 then
6928 Discrim_Source := Original_Node (Discrim_Source);
6929 end if;
6931 Discrim_Source := Unqual_Conv (Discrim_Source);
6933 case Nkind (Discrim_Source) is
6934 when N_Defining_Identifier =>
6935 pragma Assert (Is_Composite_Type (Discrim_Source)
6936 and then Has_Discriminants (Discrim_Source)
6937 and then Is_Constrained (Discrim_Source));
6939 declare
6940 Discrim : Entity_Id :=
6941 First_Discriminant (Base_Type (R_Type));
6942 Disc_Elmt : Elmt_Id :=
6943 First_Elmt (Discriminant_Constraint
6944 (Discrim_Source));
6945 begin
6946 loop
6947 if Ekind (Etype (Discrim)) =
6948 E_Anonymous_Access_Type
6949 then
6950 Check_Against_Result_Level
6951 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6952 end if;
6954 Next_Elmt (Disc_Elmt);
6955 Next_Discriminant (Discrim);
6956 exit when not Present (Discrim);
6957 end loop;
6958 end;
6960 when N_Aggregate
6961 | N_Extension_Aggregate
6963 -- Unimplemented: extension aggregate case where discrims
6964 -- come from ancestor part, not extension part.
6966 declare
6967 Discrim : Entity_Id :=
6968 First_Discriminant (Base_Type (R_Type));
6970 Disc_Exp : Node_Id := Empty;
6972 Positionals_Exhausted
6973 : Boolean := not Present (Expressions
6974 (Discrim_Source));
6976 function Associated_Expr
6977 (Comp_Id : Entity_Id;
6978 Associations : List_Id) return Node_Id;
6980 -- Given a component and a component associations list,
6981 -- locate the expression for that component; returns
6982 -- Empty if no such expression is found.
6984 ---------------------
6985 -- Associated_Expr --
6986 ---------------------
6988 function Associated_Expr
6989 (Comp_Id : Entity_Id;
6990 Associations : List_Id) return Node_Id
6992 Assoc : Node_Id;
6993 Choice : Node_Id;
6995 begin
6996 -- Simple linear search seems ok here
6998 Assoc := First (Associations);
6999 while Present (Assoc) loop
7000 Choice := First (Choices (Assoc));
7001 while Present (Choice) loop
7002 if (Nkind (Choice) = N_Identifier
7003 and then Chars (Choice) = Chars (Comp_Id))
7004 or else (Nkind (Choice) = N_Others_Choice)
7005 then
7006 return Expression (Assoc);
7007 end if;
7009 Next (Choice);
7010 end loop;
7012 Next (Assoc);
7013 end loop;
7015 return Empty;
7016 end Associated_Expr;
7018 -- Start of processing for Expand_Simple_Function_Return
7020 begin
7021 if not Positionals_Exhausted then
7022 Disc_Exp := First (Expressions (Discrim_Source));
7023 end if;
7025 loop
7026 if Positionals_Exhausted then
7027 Disc_Exp :=
7028 Associated_Expr
7029 (Discrim,
7030 Component_Associations (Discrim_Source));
7031 end if;
7033 if Ekind (Etype (Discrim)) =
7034 E_Anonymous_Access_Type
7035 then
7036 Check_Against_Result_Level
7037 (Dynamic_Accessibility_Level (Disc_Exp));
7038 end if;
7040 Next_Discriminant (Discrim);
7041 exit when not Present (Discrim);
7043 if not Positionals_Exhausted then
7044 Next (Disc_Exp);
7045 Positionals_Exhausted := not Present (Disc_Exp);
7046 end if;
7047 end loop;
7048 end;
7050 when N_Function_Call =>
7052 -- No check needed (check performed by callee)
7054 null;
7056 when others =>
7057 declare
7058 Level : constant Node_Id :=
7059 Make_Integer_Literal (Loc,
7060 Object_Access_Level (Discrim_Source));
7062 begin
7063 -- Unimplemented: check for name prefix that includes
7064 -- a dereference of an access value with a dynamic
7065 -- accessibility level (e.g., an access param or a
7066 -- saooaaat) and use dynamic level in that case. For
7067 -- example:
7068 -- return Access_Param.all(Some_Index).Some_Component;
7069 -- ???
7071 Set_Etype (Level, Standard_Natural);
7072 Check_Against_Result_Level (Level);
7073 end;
7074 end case;
7075 end;
7076 end if;
7078 -- If we are returning an object that may not be bit-aligned, then copy
7079 -- the value into a temporary first. This copy may need to expand to a
7080 -- loop of component operations.
7082 if Is_Possibly_Unaligned_Slice (Exp)
7083 or else Is_Possibly_Unaligned_Object (Exp)
7084 then
7085 declare
7086 ExpR : constant Node_Id := Relocate_Node (Exp);
7087 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7088 begin
7089 Insert_Action (Exp,
7090 Make_Object_Declaration (Loc,
7091 Defining_Identifier => Tnn,
7092 Constant_Present => True,
7093 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7094 Expression => ExpR),
7095 Suppress => All_Checks);
7096 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7097 end;
7098 end if;
7100 -- Call the _Postconditions procedure if the related function has
7101 -- contract assertions that need to be verified on exit.
7103 if Ekind (Scope_Id) = E_Function
7104 and then Present (Postconditions_Proc (Scope_Id))
7105 then
7106 -- In the case of discriminated objects, we have created a
7107 -- constrained subtype above, and used the underlying type. This
7108 -- transformation is post-analysis and harmless, except that now the
7109 -- call to the post-condition will be analyzed and the type kinds
7110 -- have to match.
7112 if Nkind (Exp) = N_Unchecked_Type_Conversion
7113 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7114 then
7115 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7116 end if;
7118 -- We are going to reference the returned value twice in this case,
7119 -- once in the call to _Postconditions, and once in the actual return
7120 -- statement, but we can't have side effects happening twice.
7122 Force_Evaluation (Exp, Mode => Strict);
7124 -- Generate call to _Postconditions
7126 Insert_Action (Exp,
7127 Make_Procedure_Call_Statement (Loc,
7128 Name =>
7129 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
7130 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
7131 end if;
7133 -- Ada 2005 (AI-251): If this return statement corresponds with an
7134 -- simple return statement associated with an extended return statement
7135 -- and the type of the returned object is an interface then generate an
7136 -- implicit conversion to force displacement of the "this" pointer.
7138 if Ada_Version >= Ada_2005
7139 and then Comes_From_Extended_Return_Statement (N)
7140 and then Nkind (Expression (N)) = N_Identifier
7141 and then Is_Interface (Utyp)
7142 and then Utyp /= Underlying_Type (Exptyp)
7143 then
7144 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7145 Analyze_And_Resolve (Exp);
7146 end if;
7147 end Expand_Simple_Function_Return;
7149 --------------------------------------------
7150 -- Has_Unconstrained_Access_Discriminants --
7151 --------------------------------------------
7153 function Has_Unconstrained_Access_Discriminants
7154 (Subtyp : Entity_Id) return Boolean
7156 Discr : Entity_Id;
7158 begin
7159 if Has_Discriminants (Subtyp)
7160 and then not Is_Constrained (Subtyp)
7161 then
7162 Discr := First_Discriminant (Subtyp);
7163 while Present (Discr) loop
7164 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7165 return True;
7166 end if;
7168 Next_Discriminant (Discr);
7169 end loop;
7170 end if;
7172 return False;
7173 end Has_Unconstrained_Access_Discriminants;
7175 --------------------------------
7176 -- Is_Build_In_Place_Function --
7177 --------------------------------
7179 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7180 begin
7181 -- This function is called from Expand_Subtype_From_Expr during
7182 -- semantic analysis, even when expansion is off. In those cases
7183 -- the build_in_place expansion will not take place.
7185 if not Expander_Active then
7186 return False;
7187 end if;
7189 -- For now we test whether E denotes a function or access-to-function
7190 -- type whose result subtype is inherently limited. Later this test
7191 -- may be revised to allow composite nonlimited types. Functions with
7192 -- a foreign convention or whose result type has a foreign convention
7193 -- never qualify.
7195 if Ekind_In (E, E_Function, E_Generic_Function)
7196 or else (Ekind (E) = E_Subprogram_Type
7197 and then Etype (E) /= Standard_Void_Type)
7198 then
7199 -- Note: If the function has a foreign convention, it cannot build
7200 -- its result in place, so you're on your own. On the other hand,
7201 -- if only the return type has a foreign convention, its layout is
7202 -- intended to be compatible with the other language, but the build-
7203 -- in place machinery can ensure that the object is not copied.
7205 if Has_Foreign_Convention (E) then
7206 return False;
7208 -- In Ada 2005 all functions with an inherently limited return type
7209 -- must be handled using a build-in-place profile, including the case
7210 -- of a function with a limited interface result, where the function
7211 -- may return objects of nonlimited descendants.
7213 else
7214 return Is_Limited_View (Etype (E))
7215 and then Ada_Version >= Ada_2005
7216 and then not Debug_Flag_Dot_L;
7217 end if;
7219 else
7220 return False;
7221 end if;
7222 end Is_Build_In_Place_Function;
7224 -------------------------------------
7225 -- Is_Build_In_Place_Function_Call --
7226 -------------------------------------
7228 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7229 Exp_Node : constant Node_Id := Unqual_Conv (N);
7230 Function_Id : Entity_Id;
7232 begin
7233 -- Return False if the expander is currently inactive, since awareness
7234 -- of build-in-place treatment is only relevant during expansion. Note
7235 -- that Is_Build_In_Place_Function, which is called as part of this
7236 -- function, is also conditioned this way, but we need to check here as
7237 -- well to avoid blowing up on processing protected calls when expansion
7238 -- is disabled (such as with -gnatc) since those would trip over the
7239 -- raise of Program_Error below.
7241 -- In SPARK mode, build-in-place calls are not expanded, so that we
7242 -- may end up with a call that is neither resolved to an entity, nor
7243 -- an indirect call.
7245 if not Expander_Active then
7246 return False;
7247 end if;
7249 if Nkind (Exp_Node) /= N_Function_Call then
7250 return False;
7252 else
7253 if Is_Entity_Name (Name (Exp_Node)) then
7254 Function_Id := Entity (Name (Exp_Node));
7256 -- In the case of an explicitly dereferenced call, use the subprogram
7257 -- type generated for the dereference.
7259 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7260 Function_Id := Etype (Name (Exp_Node));
7262 -- This may be a call to a protected function.
7264 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7265 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7267 else
7268 raise Program_Error;
7269 end if;
7271 return Is_Build_In_Place_Function (Function_Id);
7272 end if;
7273 end Is_Build_In_Place_Function_Call;
7275 -----------------------
7276 -- Freeze_Subprogram --
7277 -----------------------
7279 procedure Freeze_Subprogram (N : Node_Id) is
7280 Loc : constant Source_Ptr := Sloc (N);
7282 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7283 -- (Ada 2005): Register a predefined primitive in all the secondary
7284 -- dispatch tables of its primitive type.
7286 ----------------------------------
7287 -- Register_Predefined_DT_Entry --
7288 ----------------------------------
7290 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7291 Iface_DT_Ptr : Elmt_Id;
7292 Tagged_Typ : Entity_Id;
7293 Thunk_Id : Entity_Id;
7294 Thunk_Code : Node_Id;
7296 begin
7297 Tagged_Typ := Find_Dispatching_Type (Prim);
7299 if No (Access_Disp_Table (Tagged_Typ))
7300 or else not Has_Interfaces (Tagged_Typ)
7301 or else not RTE_Available (RE_Interface_Tag)
7302 or else Restriction_Active (No_Dispatching_Calls)
7303 then
7304 return;
7305 end if;
7307 -- Skip the first two access-to-dispatch-table pointers since they
7308 -- leads to the primary dispatch table (predefined DT and user
7309 -- defined DT). We are only concerned with the secondary dispatch
7310 -- table pointers. Note that the access-to- dispatch-table pointer
7311 -- corresponds to the first implemented interface retrieved below.
7313 Iface_DT_Ptr :=
7314 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7316 while Present (Iface_DT_Ptr)
7317 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7318 loop
7319 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7320 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7322 if Present (Thunk_Code) then
7323 Insert_Actions_After (N, New_List (
7324 Thunk_Code,
7326 Build_Set_Predefined_Prim_Op_Address (Loc,
7327 Tag_Node =>
7328 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7329 Position => DT_Position (Prim),
7330 Address_Node =>
7331 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7332 Make_Attribute_Reference (Loc,
7333 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7334 Attribute_Name => Name_Unrestricted_Access))),
7336 Build_Set_Predefined_Prim_Op_Address (Loc,
7337 Tag_Node =>
7338 New_Occurrence_Of
7339 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7340 Loc),
7341 Position => DT_Position (Prim),
7342 Address_Node =>
7343 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7344 Make_Attribute_Reference (Loc,
7345 Prefix => New_Occurrence_Of (Prim, Loc),
7346 Attribute_Name => Name_Unrestricted_Access)))));
7347 end if;
7349 -- Skip the tag of the predefined primitives dispatch table
7351 Next_Elmt (Iface_DT_Ptr);
7352 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7354 -- Skip tag of the no-thunks dispatch table
7356 Next_Elmt (Iface_DT_Ptr);
7357 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7359 -- Skip tag of predefined primitives no-thunks dispatch table
7361 Next_Elmt (Iface_DT_Ptr);
7362 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7364 Next_Elmt (Iface_DT_Ptr);
7365 end loop;
7366 end Register_Predefined_DT_Entry;
7368 -- Local variables
7370 Subp : constant Entity_Id := Entity (N);
7372 -- Start of processing for Freeze_Subprogram
7374 begin
7375 -- We suppress the initialization of the dispatch table entry when
7376 -- not Tagged_Type_Expansion because the dispatching mechanism is
7377 -- handled internally by the target.
7379 if Is_Dispatching_Operation (Subp)
7380 and then not Is_Abstract_Subprogram (Subp)
7381 and then Present (DTC_Entity (Subp))
7382 and then Present (Scope (DTC_Entity (Subp)))
7383 and then Tagged_Type_Expansion
7384 and then not Restriction_Active (No_Dispatching_Calls)
7385 and then RTE_Available (RE_Tag)
7386 then
7387 declare
7388 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7390 begin
7391 -- Handle private overridden primitives
7393 if not Is_CPP_Class (Typ) then
7394 Check_Overriding_Operation (Subp);
7395 end if;
7397 -- We assume that imported CPP primitives correspond with objects
7398 -- whose constructor is in the CPP side; therefore we don't need
7399 -- to generate code to register them in the dispatch table.
7401 if Is_CPP_Class (Typ) then
7402 null;
7404 -- Handle CPP primitives found in derivations of CPP_Class types.
7405 -- These primitives must have been inherited from some parent, and
7406 -- there is no need to register them in the dispatch table because
7407 -- Build_Inherit_Prims takes care of initializing these slots.
7409 elsif Is_Imported (Subp)
7410 and then (Convention (Subp) = Convention_CPP
7411 or else Convention (Subp) = Convention_C)
7412 then
7413 null;
7415 -- Generate code to register the primitive in non statically
7416 -- allocated dispatch tables
7418 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7420 -- When a primitive is frozen, enter its name in its dispatch
7421 -- table slot.
7423 if not Is_Interface (Typ)
7424 or else Present (Interface_Alias (Subp))
7425 then
7426 if Is_Predefined_Dispatching_Operation (Subp) then
7427 Register_Predefined_DT_Entry (Subp);
7428 end if;
7430 Insert_Actions_After (N,
7431 Register_Primitive (Loc, Prim => Subp));
7432 end if;
7433 end if;
7434 end;
7435 end if;
7437 -- Mark functions that return by reference. Note that it cannot be part
7438 -- of the normal semantic analysis of the spec since the underlying
7439 -- returned type may not be known yet (for private types).
7441 declare
7442 Typ : constant Entity_Id := Etype (Subp);
7443 Utyp : constant Entity_Id := Underlying_Type (Typ);
7445 begin
7446 if Is_Limited_View (Typ) then
7447 Set_Returns_By_Ref (Subp);
7449 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7450 Set_Returns_By_Ref (Subp);
7451 end if;
7452 end;
7454 -- Wnen freezing a null procedure, analyze its delayed aspects now
7455 -- because we may not have reached the end of the declarative list when
7456 -- delayed aspects are normally analyzed. This ensures that dispatching
7457 -- calls are properly rewritten when the generated _Postcondition
7458 -- procedure is analyzed in the null procedure body.
7460 if Nkind (Parent (Subp)) = N_Procedure_Specification
7461 and then Null_Present (Parent (Subp))
7462 then
7463 Analyze_Entry_Or_Subprogram_Contract (Subp);
7464 end if;
7465 end Freeze_Subprogram;
7467 ------------------------------
7468 -- Insert_Post_Call_Actions --
7469 ------------------------------
7471 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7472 Context : constant Node_Id := Parent (N);
7474 begin
7475 if Is_Empty_List (Post_Call) then
7476 return;
7477 end if;
7479 -- Cases where the call is not a member of a statement list. This
7480 -- includes the case where the call is an actual in another function
7481 -- call or indexing, i.e. an expression context as well.
7483 if not Is_List_Member (N)
7484 or else Nkind_In (Context, N_Function_Call, N_Indexed_Component)
7485 then
7486 -- In Ada 2012 the call may be a function call in an expression
7487 -- (since OUT and IN OUT parameters are now allowed for such calls).
7488 -- The write-back of (in)-out parameters is handled by the back-end,
7489 -- but the constraint checks generated when subtypes of formal and
7490 -- actual don't match must be inserted in the form of assignments.
7492 if Nkind (Original_Node (N)) = N_Function_Call then
7493 pragma Assert (Ada_Version >= Ada_2012);
7494 -- Functions with '[in] out' parameters are only allowed in Ada
7495 -- 2012.
7497 -- We used to handle this by climbing up parents to a
7498 -- non-statement/declaration and then simply making a call to
7499 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7500 -- for Ada 2012. If we are in the middle of an expression, e.g.
7501 -- the condition of an IF, this call would insert after the IF
7502 -- statement, which is much too late to be doing the write back.
7503 -- For example:
7505 -- if Clobber (X) then
7506 -- Put_Line (X'Img);
7507 -- else
7508 -- goto Junk
7509 -- end if;
7511 -- Now assume Clobber changes X, if we put the write back after
7512 -- the IF, the Put_Line gets the wrong value and the goto causes
7513 -- the write back to be skipped completely.
7515 -- To deal with this, we replace the call by
7517 -- do
7518 -- Tnnn : constant function-result-type := function-call;
7519 -- Post_Call actions
7520 -- in
7521 -- Tnnn;
7522 -- end;
7524 declare
7525 Loc : constant Source_Ptr := Sloc (N);
7526 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7527 FRTyp : constant Entity_Id := Etype (N);
7528 Name : constant Node_Id := Relocate_Node (N);
7530 begin
7531 Prepend_To (Post_Call,
7532 Make_Object_Declaration (Loc,
7533 Defining_Identifier => Tnnn,
7534 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7535 Constant_Present => True,
7536 Expression => Name));
7538 Rewrite (N,
7539 Make_Expression_With_Actions (Loc,
7540 Actions => Post_Call,
7541 Expression => New_Occurrence_Of (Tnnn, Loc)));
7543 -- We don't want to just blindly call Analyze_And_Resolve
7544 -- because that would cause unwanted recursion on the call.
7545 -- So for a moment set the call as analyzed to prevent that
7546 -- recursion, and get the rest analyzed properly, then reset
7547 -- the analyzed flag, so our caller can continue.
7549 Set_Analyzed (Name, True);
7550 Analyze_And_Resolve (N, FRTyp);
7551 Set_Analyzed (Name, False);
7552 end;
7554 -- If not the special Ada 2012 case of a function call, then we must
7555 -- have the triggering statement of a triggering alternative or an
7556 -- entry call alternative, and we can add the post call stuff to the
7557 -- corresponding statement list.
7559 else
7560 pragma Assert (Nkind_In (Context, N_Entry_Call_Alternative,
7561 N_Triggering_Alternative));
7563 if Is_Non_Empty_List (Statements (Context)) then
7564 Insert_List_Before_And_Analyze
7565 (First (Statements (Context)), Post_Call);
7566 else
7567 Set_Statements (Context, Post_Call);
7568 end if;
7569 end if;
7571 -- A procedure call is always part of a declarative or statement list,
7572 -- however a function call may appear nested within a construct. Most
7573 -- cases of function call nesting are handled in the special case above.
7574 -- The only exception is when the function call acts as an actual in a
7575 -- procedure call. In this case the function call is in a list, but the
7576 -- post-call actions must be inserted after the procedure call.
7578 elsif Nkind (Context) = N_Procedure_Call_Statement then
7579 Insert_Actions_After (Context, Post_Call);
7581 -- Otherwise, normal case where N is in a statement sequence, just put
7582 -- the post-call stuff after the call statement.
7584 else
7585 Insert_Actions_After (N, Post_Call);
7586 end if;
7587 end Insert_Post_Call_Actions;
7589 -----------------------
7590 -- Is_Null_Procedure --
7591 -----------------------
7593 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7594 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7596 begin
7597 if Ekind (Subp) /= E_Procedure then
7598 return False;
7600 -- Check if this is a declared null procedure
7602 elsif Nkind (Decl) = N_Subprogram_Declaration then
7603 if not Null_Present (Specification (Decl)) then
7604 return False;
7606 elsif No (Body_To_Inline (Decl)) then
7607 return False;
7609 -- Check if the body contains only a null statement, followed by
7610 -- the return statement added during expansion.
7612 else
7613 declare
7614 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7616 Stat : Node_Id;
7617 Stat2 : Node_Id;
7619 begin
7620 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7621 return False;
7622 else
7623 -- We must skip SCIL nodes because they are currently
7624 -- implemented as special N_Null_Statement nodes.
7626 Stat :=
7627 First_Non_SCIL_Node
7628 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7629 Stat2 := Next_Non_SCIL_Node (Stat);
7631 return
7632 Is_Empty_List (Declarations (Orig_Bod))
7633 and then Nkind (Stat) = N_Null_Statement
7634 and then
7635 (No (Stat2)
7636 or else
7637 (Nkind (Stat2) = N_Simple_Return_Statement
7638 and then No (Next (Stat2))));
7639 end if;
7640 end;
7641 end if;
7643 else
7644 return False;
7645 end if;
7646 end Is_Null_Procedure;
7648 -------------------------------------------
7649 -- Make_Build_In_Place_Call_In_Allocator --
7650 -------------------------------------------
7652 procedure Make_Build_In_Place_Call_In_Allocator
7653 (Allocator : Node_Id;
7654 Function_Call : Node_Id)
7656 Acc_Type : constant Entity_Id := Etype (Allocator);
7657 Loc : Source_Ptr;
7658 Func_Call : Node_Id := Function_Call;
7659 Ref_Func_Call : Node_Id;
7660 Function_Id : Entity_Id;
7661 Result_Subt : Entity_Id;
7662 New_Allocator : Node_Id;
7663 Return_Obj_Access : Entity_Id; -- temp for function result
7664 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7665 Alloc_Form : BIP_Allocation_Form;
7666 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7667 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7668 Chain : Entity_Id; -- activation chain, in case of tasks
7670 begin
7671 -- Step past qualification or unchecked conversion (the latter can occur
7672 -- in cases of calls to 'Input).
7674 if Nkind_In (Func_Call,
7675 N_Qualified_Expression,
7676 N_Type_Conversion,
7677 N_Unchecked_Type_Conversion)
7678 then
7679 Func_Call := Expression (Func_Call);
7680 end if;
7682 -- If the call has already been processed to add build-in-place actuals
7683 -- then return. This should not normally occur in an allocator context,
7684 -- but we add the protection as a defensive measure.
7686 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7687 return;
7688 end if;
7690 -- Mark the call as processed as a build-in-place call
7692 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7694 Loc := Sloc (Function_Call);
7696 if Is_Entity_Name (Name (Func_Call)) then
7697 Function_Id := Entity (Name (Func_Call));
7699 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7700 Function_Id := Etype (Name (Func_Call));
7702 else
7703 raise Program_Error;
7704 end if;
7706 Result_Subt := Available_View (Etype (Function_Id));
7708 -- Create a temp for the function result. In the caller-allocates case,
7709 -- this will be initialized to the result of a new uninitialized
7710 -- allocator. Note: we do not use Allocator as the Related_Node of
7711 -- Return_Obj_Access in call to Make_Temporary below as this would
7712 -- create a sort of infinite "recursion".
7714 Return_Obj_Access := Make_Temporary (Loc, 'R');
7715 Set_Etype (Return_Obj_Access, Acc_Type);
7717 -- When the result subtype is constrained, the return object is
7718 -- allocated on the caller side, and access to it is passed to the
7719 -- function.
7721 -- Here and in related routines, we must examine the full view of the
7722 -- type, because the view at the point of call may differ from that
7723 -- that in the function body, and the expansion mechanism depends on
7724 -- the characteristics of the full view.
7726 if Is_Constrained (Underlying_Type (Result_Subt)) then
7728 -- Replace the initialized allocator of form "new T'(Func (...))"
7729 -- with an uninitialized allocator of form "new T", where T is the
7730 -- result subtype of the called function. The call to the function
7731 -- is handled separately further below.
7733 New_Allocator :=
7734 Make_Allocator (Loc,
7735 Expression => New_Occurrence_Of (Result_Subt, Loc));
7736 Set_No_Initialization (New_Allocator);
7738 -- Copy attributes to new allocator. Note that the new allocator
7739 -- logically comes from source if the original one did, so copy the
7740 -- relevant flag. This ensures proper treatment of the restriction
7741 -- No_Implicit_Heap_Allocations in this case.
7743 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7744 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7745 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7747 Rewrite (Allocator, New_Allocator);
7749 -- Initial value of the temp is the result of the uninitialized
7750 -- allocator
7752 Temp_Init := Relocate_Node (Allocator);
7754 -- Indicate that caller allocates, and pass in the return object
7756 Alloc_Form := Caller_Allocation;
7757 Pool := Make_Null (No_Location);
7758 Return_Obj_Actual :=
7759 Make_Unchecked_Type_Conversion (Loc,
7760 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7761 Expression =>
7762 Make_Explicit_Dereference (Loc,
7763 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7765 -- When the result subtype is unconstrained, the function itself must
7766 -- perform the allocation of the return object, so we pass parameters
7767 -- indicating that.
7769 else
7770 Temp_Init := Empty;
7772 -- Case of a user-defined storage pool. Pass an allocation parameter
7773 -- indicating that the function should allocate its result in the
7774 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7775 -- pool may not be aliased.
7777 if Present (Associated_Storage_Pool (Acc_Type)) then
7778 Alloc_Form := User_Storage_Pool;
7779 Pool :=
7780 Make_Attribute_Reference (Loc,
7781 Prefix =>
7782 New_Occurrence_Of
7783 (Associated_Storage_Pool (Acc_Type), Loc),
7784 Attribute_Name => Name_Unrestricted_Access);
7786 -- No user-defined pool; pass an allocation parameter indicating that
7787 -- the function should allocate its result on the heap.
7789 else
7790 Alloc_Form := Global_Heap;
7791 Pool := Make_Null (No_Location);
7792 end if;
7794 -- The caller does not provide the return object in this case, so we
7795 -- have to pass null for the object access actual.
7797 Return_Obj_Actual := Empty;
7798 end if;
7800 -- Declare the temp object
7802 Insert_Action (Allocator,
7803 Make_Object_Declaration (Loc,
7804 Defining_Identifier => Return_Obj_Access,
7805 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7806 Expression => Temp_Init));
7808 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7810 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7811 -- then generate an implicit conversion to force displacement of the
7812 -- "this" pointer.
7814 if Is_Interface (Designated_Type (Acc_Type)) then
7815 Rewrite
7816 (Ref_Func_Call,
7817 OK_Convert_To (Acc_Type, Ref_Func_Call));
7818 end if;
7820 declare
7821 Assign : constant Node_Id :=
7822 Make_Assignment_Statement (Loc,
7823 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7824 Expression => Ref_Func_Call);
7825 -- Assign the result of the function call into the temp. In the
7826 -- caller-allocates case, this is overwriting the temp with its
7827 -- initial value, which has no effect. In the callee-allocates case,
7828 -- this is setting the temp to point to the object allocated by the
7829 -- callee.
7831 Actions : List_Id;
7832 -- Actions to be inserted. If there are no tasks, this is just the
7833 -- assignment statement. If the allocated object has tasks, we need
7834 -- to wrap the assignment in a block that activates them. The
7835 -- activation chain of that block must be passed to the function,
7836 -- rather than some outer chain.
7837 begin
7838 if Has_Task (Result_Subt) then
7839 Actions := New_List;
7840 Build_Task_Allocate_Block_With_Init_Stmts
7841 (Actions, Allocator, Init_Stmts => New_List (Assign));
7842 Chain := Activation_Chain_Entity (Last (Actions));
7843 else
7844 Actions := New_List (Assign);
7845 Chain := Empty;
7846 end if;
7848 Insert_Actions (Allocator, Actions);
7849 end;
7851 -- When the function has a controlling result, an allocation-form
7852 -- parameter must be passed indicating that the caller is allocating
7853 -- the result object. This is needed because such a function can be
7854 -- called as a dispatching operation and must be treated similarly
7855 -- to functions with unconstrained result subtypes.
7857 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7858 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
7860 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7861 (Func_Call, Function_Id, Acc_Type);
7863 Add_Task_Actuals_To_Build_In_Place_Call
7864 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
7865 Chain => Chain);
7867 -- Add an implicit actual to the function call that provides access
7868 -- to the allocated object. An unchecked conversion to the (specific)
7869 -- result subtype of the function is inserted to handle cases where
7870 -- the access type of the allocator has a class-wide designated type.
7872 Add_Access_Actual_To_Build_In_Place_Call
7873 (Func_Call, Function_Id, Return_Obj_Actual);
7875 -- Finally, replace the allocator node with a reference to the temp
7877 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
7879 Analyze_And_Resolve (Allocator, Acc_Type);
7880 end Make_Build_In_Place_Call_In_Allocator;
7882 ---------------------------------------------------
7883 -- Make_Build_In_Place_Call_In_Anonymous_Context --
7884 ---------------------------------------------------
7886 procedure Make_Build_In_Place_Call_In_Anonymous_Context
7887 (Function_Call : Node_Id)
7889 Loc : Source_Ptr;
7890 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
7891 Function_Id : Entity_Id;
7892 Result_Subt : Entity_Id;
7893 Return_Obj_Id : Entity_Id;
7894 Return_Obj_Decl : Entity_Id;
7896 begin
7897 -- If the call has already been processed to add build-in-place actuals
7898 -- then return. One place this can occur is for calls to build-in-place
7899 -- functions that occur within a call to a protected operation, where
7900 -- due to rewriting and expansion of the protected call there can be
7901 -- more than one call to Expand_Actuals for the same set of actuals.
7903 if Is_Expanded_Build_In_Place_Call (Func_Call) then
7904 return;
7905 end if;
7907 -- Mark the call as processed as a build-in-place call
7909 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7911 Loc := Sloc (Function_Call);
7913 if Is_Entity_Name (Name (Func_Call)) then
7914 Function_Id := Entity (Name (Func_Call));
7916 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7917 Function_Id := Etype (Name (Func_Call));
7919 else
7920 raise Program_Error;
7921 end if;
7923 Result_Subt := Etype (Function_Id);
7925 -- If the build-in-place function returns a controlled object, then the
7926 -- object needs to be finalized immediately after the context. Since
7927 -- this case produces a transient scope, the servicing finalizer needs
7928 -- to name the returned object. Create a temporary which is initialized
7929 -- with the function call:
7931 -- Temp_Id : Func_Type := BIP_Func_Call;
7933 -- The initialization expression of the temporary will be rewritten by
7934 -- the expander using the appropriate mechanism in Make_Build_In_Place_
7935 -- Call_In_Object_Declaration.
7937 if Needs_Finalization (Result_Subt) then
7938 declare
7939 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
7940 Temp_Decl : Node_Id;
7942 begin
7943 -- Reset the guard on the function call since the following does
7944 -- not perform actual call expansion.
7946 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
7948 Temp_Decl :=
7949 Make_Object_Declaration (Loc,
7950 Defining_Identifier => Temp_Id,
7951 Object_Definition =>
7952 New_Occurrence_Of (Result_Subt, Loc),
7953 Expression =>
7954 New_Copy_Tree (Function_Call));
7956 Insert_Action (Function_Call, Temp_Decl);
7958 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
7959 Analyze (Function_Call);
7960 end;
7962 -- When the result subtype is definite, an object of the subtype is
7963 -- declared and an access value designating it is passed as an actual.
7965 elsif Caller_Known_Size (Func_Call, Result_Subt) then
7967 -- Create a temporary object to hold the function result
7969 Return_Obj_Id := Make_Temporary (Loc, 'R');
7970 Set_Etype (Return_Obj_Id, Result_Subt);
7972 Return_Obj_Decl :=
7973 Make_Object_Declaration (Loc,
7974 Defining_Identifier => Return_Obj_Id,
7975 Aliased_Present => True,
7976 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
7978 Set_No_Initialization (Return_Obj_Decl);
7980 Insert_Action (Func_Call, Return_Obj_Decl);
7982 -- When the function has a controlling result, an allocation-form
7983 -- parameter must be passed indicating that the caller is allocating
7984 -- the result object. This is needed because such a function can be
7985 -- called as a dispatching operation and must be treated similarly
7986 -- to functions with unconstrained result subtypes.
7988 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7989 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
7991 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7992 (Func_Call, Function_Id);
7994 Add_Task_Actuals_To_Build_In_Place_Call
7995 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
7997 -- Add an implicit actual to the function call that provides access
7998 -- to the caller's return object.
8000 Add_Access_Actual_To_Build_In_Place_Call
8001 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8003 -- When the result subtype is unconstrained, the function must allocate
8004 -- the return object in the secondary stack, so appropriate implicit
8005 -- parameters are added to the call to indicate that. A transient
8006 -- scope is established to ensure eventual cleanup of the result.
8008 else
8009 -- Pass an allocation parameter indicating that the function should
8010 -- allocate its result on the secondary stack.
8012 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8013 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8015 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8016 (Func_Call, Function_Id);
8018 Add_Task_Actuals_To_Build_In_Place_Call
8019 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8021 -- Pass a null value to the function since no return object is
8022 -- available on the caller side.
8024 Add_Access_Actual_To_Build_In_Place_Call
8025 (Func_Call, Function_Id, Empty);
8026 end if;
8027 end Make_Build_In_Place_Call_In_Anonymous_Context;
8029 --------------------------------------------
8030 -- Make_Build_In_Place_Call_In_Assignment --
8031 --------------------------------------------
8033 procedure Make_Build_In_Place_Call_In_Assignment
8034 (Assign : Node_Id;
8035 Function_Call : Node_Id)
8037 Lhs : constant Node_Id := Name (Assign);
8038 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8039 Func_Id : Entity_Id;
8040 Loc : Source_Ptr;
8041 Obj_Decl : Node_Id;
8042 Obj_Id : Entity_Id;
8043 Ptr_Typ : Entity_Id;
8044 Ptr_Typ_Decl : Node_Id;
8045 New_Expr : Node_Id;
8046 Result_Subt : Entity_Id;
8048 begin
8049 -- If the call has already been processed to add build-in-place actuals
8050 -- then return. This should not normally occur in an assignment context,
8051 -- but we add the protection as a defensive measure.
8053 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8054 return;
8055 end if;
8057 -- Mark the call as processed as a build-in-place call
8059 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8061 Loc := Sloc (Function_Call);
8063 if Is_Entity_Name (Name (Func_Call)) then
8064 Func_Id := Entity (Name (Func_Call));
8066 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8067 Func_Id := Etype (Name (Func_Call));
8069 else
8070 raise Program_Error;
8071 end if;
8073 Result_Subt := Etype (Func_Id);
8075 -- When the result subtype is unconstrained, an additional actual must
8076 -- be passed to indicate that the caller is providing the return object.
8077 -- This parameter must also be passed when the called function has a
8078 -- controlling result, because dispatching calls to the function needs
8079 -- to be treated effectively the same as calls to class-wide functions.
8081 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8082 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8084 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8085 (Func_Call, Func_Id);
8087 Add_Task_Actuals_To_Build_In_Place_Call
8088 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8090 -- Add an implicit actual to the function call that provides access to
8091 -- the caller's return object.
8093 Add_Access_Actual_To_Build_In_Place_Call
8094 (Func_Call,
8095 Func_Id,
8096 Make_Unchecked_Type_Conversion (Loc,
8097 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8098 Expression => Relocate_Node (Lhs)));
8100 -- Create an access type designating the function's result subtype
8102 Ptr_Typ := Make_Temporary (Loc, 'A');
8104 Ptr_Typ_Decl :=
8105 Make_Full_Type_Declaration (Loc,
8106 Defining_Identifier => Ptr_Typ,
8107 Type_Definition =>
8108 Make_Access_To_Object_Definition (Loc,
8109 All_Present => True,
8110 Subtype_Indication =>
8111 New_Occurrence_Of (Result_Subt, Loc)));
8112 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8114 -- Finally, create an access object initialized to a reference to the
8115 -- function call. We know this access value is non-null, so mark the
8116 -- entity accordingly to suppress junk access checks.
8118 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8120 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8121 Set_Etype (Obj_Id, Ptr_Typ);
8122 Set_Is_Known_Non_Null (Obj_Id);
8124 Obj_Decl :=
8125 Make_Object_Declaration (Loc,
8126 Defining_Identifier => Obj_Id,
8127 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8128 Expression => New_Expr);
8129 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8131 Rewrite (Assign, Make_Null_Statement (Loc));
8132 end Make_Build_In_Place_Call_In_Assignment;
8134 ----------------------------------------------------
8135 -- Make_Build_In_Place_Call_In_Object_Declaration --
8136 ----------------------------------------------------
8138 procedure Make_Build_In_Place_Call_In_Object_Declaration
8139 (Obj_Decl : Node_Id;
8140 Function_Call : Node_Id)
8142 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8143 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8144 Loc : constant Source_Ptr := Sloc (Function_Call);
8145 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8147 Call_Deref : Node_Id;
8148 Caller_Object : Node_Id;
8149 Def_Id : Entity_Id;
8150 Fmaster_Actual : Node_Id := Empty;
8151 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8152 Function_Id : Entity_Id;
8153 Pool_Actual : Node_Id;
8154 Ptr_Typ : Entity_Id;
8155 Ptr_Typ_Decl : Node_Id;
8156 Pass_Caller_Acc : Boolean := False;
8157 Res_Decl : Node_Id;
8158 Result_Subt : Entity_Id;
8160 begin
8161 -- If the call has already been processed to add build-in-place actuals
8162 -- then return. This should not normally occur in an object declaration,
8163 -- but we add the protection as a defensive measure.
8165 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8166 return;
8167 end if;
8169 -- Mark the call as processed as a build-in-place call
8171 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8173 if Is_Entity_Name (Name (Func_Call)) then
8174 Function_Id := Entity (Name (Func_Call));
8176 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8177 Function_Id := Etype (Name (Func_Call));
8179 else
8180 raise Program_Error;
8181 end if;
8183 Result_Subt := Etype (Function_Id);
8185 declare
8186 Definite : constant Boolean :=
8187 Caller_Known_Size (Func_Call, Result_Subt);
8189 begin
8190 -- Create an access type designating the function's result subtype.
8191 -- We use the type of the original call because it may be a call to
8192 -- an inherited operation, which the expansion has replaced with the
8193 -- parent operation that yields the parent type. Note that this
8194 -- access type must be declared before we establish a transient
8195 -- scope, so that it receives the proper accessibility level.
8197 Ptr_Typ := Make_Temporary (Loc, 'A');
8198 Ptr_Typ_Decl :=
8199 Make_Full_Type_Declaration (Loc,
8200 Defining_Identifier => Ptr_Typ,
8201 Type_Definition =>
8202 Make_Access_To_Object_Definition (Loc,
8203 All_Present => True,
8204 Subtype_Indication =>
8205 New_Occurrence_Of (Etype (Function_Call), Loc)));
8207 -- The access type and its accompanying object must be inserted after
8208 -- the object declaration in the constrained case, so that the
8209 -- function call can be passed access to the object. In the
8210 -- indefinite case, or if the object declaration is for a return
8211 -- object, the access type and object must be inserted before the
8212 -- object, since the object declaration is rewritten to be a renaming
8213 -- of a dereference of the access object. Note: we need to freeze
8214 -- Ptr_Typ explicitly, because the result object is in a different
8215 -- (transient) scope, so won't cause freezing.
8217 if Definite
8218 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8219 then
8220 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8221 else
8222 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8223 end if;
8225 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8226 -- elaborated in an inner (transient) scope and thus won't cause
8227 -- freezing by itself.
8229 declare
8230 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8231 New_Occurrence_Of (Ptr_Typ, Loc);
8232 begin
8233 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8234 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8235 end;
8237 -- If the object is a return object of an enclosing build-in-place
8238 -- function, then the implicit build-in-place parameters of the
8239 -- enclosing function are simply passed along to the called function.
8240 -- (Unfortunately, this won't cover the case of extension aggregates
8241 -- where the ancestor part is a build-in-place indefinite function
8242 -- call that should be passed along the caller's parameters.
8243 -- Currently those get mishandled by reassigning the result of the
8244 -- call to the aggregate return object, when the call result should
8245 -- really be directly built in place in the aggregate and not in a
8246 -- temporary. ???)
8248 if Is_Return_Object (Defining_Identifier (Obj_Decl)) then
8249 Pass_Caller_Acc := True;
8251 -- When the enclosing function has a BIP_Alloc_Form formal then we
8252 -- pass it along to the callee (such as when the enclosing
8253 -- function has an unconstrained or tagged result type).
8255 if Needs_BIP_Alloc_Form (Encl_Func) then
8256 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8257 Pool_Actual :=
8258 New_Occurrence_Of
8259 (Build_In_Place_Formal
8260 (Encl_Func, BIP_Storage_Pool), Loc);
8262 -- The build-in-place pool formal is not built on e.g. ZFP
8264 else
8265 Pool_Actual := Empty;
8266 end if;
8268 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8269 (Function_Call => Func_Call,
8270 Function_Id => Function_Id,
8271 Alloc_Form_Exp =>
8272 New_Occurrence_Of
8273 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8274 Pool_Actual => Pool_Actual);
8276 -- Otherwise, if enclosing function has a definite result subtype,
8277 -- then caller allocation will be used.
8279 else
8280 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8281 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8282 end if;
8284 if Needs_BIP_Finalization_Master (Encl_Func) then
8285 Fmaster_Actual :=
8286 New_Occurrence_Of
8287 (Build_In_Place_Formal
8288 (Encl_Func, BIP_Finalization_Master), Loc);
8289 end if;
8291 -- Retrieve the BIPacc formal from the enclosing function and
8292 -- convert it to the access type of the callee's BIP_Object_Access
8293 -- formal.
8295 Caller_Object :=
8296 Make_Unchecked_Type_Conversion (Loc,
8297 Subtype_Mark =>
8298 New_Occurrence_Of
8299 (Etype
8300 (Build_In_Place_Formal
8301 (Function_Id, BIP_Object_Access)),
8302 Loc),
8303 Expression =>
8304 New_Occurrence_Of
8305 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8306 Loc));
8308 -- In the definite case, add an implicit actual to the function call
8309 -- that provides access to the declared object. An unchecked
8310 -- conversion to the (specific) result type of the function is
8311 -- inserted to handle the case where the object is declared with a
8312 -- class-wide type.
8314 elsif Definite then
8315 Caller_Object :=
8316 Make_Unchecked_Type_Conversion (Loc,
8317 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8318 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8320 -- When the function has a controlling result, an allocation-form
8321 -- parameter must be passed indicating that the caller is
8322 -- allocating the result object. This is needed because such a
8323 -- function can be called as a dispatching operation and must be
8324 -- treated similarly to functions with indefinite result subtypes.
8326 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8327 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8329 -- The allocation for indefinite library-level objects occurs on the
8330 -- heap as opposed to the secondary stack. This accommodates DLLs
8331 -- where the secondary stack is destroyed after each library
8332 -- unload. This is a hybrid mechanism where a stack-allocated object
8333 -- lives on the heap.
8335 elsif Is_Library_Level_Entity (Defining_Identifier (Obj_Decl))
8336 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8337 then
8338 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8339 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8340 Caller_Object := Empty;
8342 -- Create a finalization master for the access result type to
8343 -- ensure that the heap allocation can properly chain the object
8344 -- and later finalize it when the library unit goes out of scope.
8346 if Needs_Finalization (Etype (Func_Call)) then
8347 Build_Finalization_Master
8348 (Typ => Ptr_Typ,
8349 For_Lib_Level => True,
8350 Insertion_Node => Ptr_Typ_Decl);
8352 Fmaster_Actual :=
8353 Make_Attribute_Reference (Loc,
8354 Prefix =>
8355 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8356 Attribute_Name => Name_Unrestricted_Access);
8357 end if;
8359 -- In other indefinite cases, pass an indication to do the allocation
8360 -- on the secondary stack and set Caller_Object to Empty so that a
8361 -- null value will be passed for the caller's object address. A
8362 -- transient scope is established to ensure eventual cleanup of the
8363 -- result.
8365 else
8366 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8367 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8368 Caller_Object := Empty;
8370 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8371 end if;
8373 -- Pass along any finalization master actual, which is needed in the
8374 -- case where the called function initializes a return object of an
8375 -- enclosing build-in-place function.
8377 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8378 (Func_Call => Func_Call,
8379 Func_Id => Function_Id,
8380 Master_Exp => Fmaster_Actual);
8382 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8383 and then Has_Task (Result_Subt)
8384 then
8385 -- Here we're passing along the master that was passed in to this
8386 -- function.
8388 Add_Task_Actuals_To_Build_In_Place_Call
8389 (Func_Call, Function_Id,
8390 Master_Actual =>
8391 New_Occurrence_Of
8392 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8394 else
8395 Add_Task_Actuals_To_Build_In_Place_Call
8396 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8397 end if;
8399 Add_Access_Actual_To_Build_In_Place_Call
8400 (Func_Call,
8401 Function_Id,
8402 Caller_Object,
8403 Is_Access => Pass_Caller_Acc);
8405 -- Finally, create an access object initialized to a reference to the
8406 -- function call. We know this access value cannot be null, so mark
8407 -- the entity accordingly to suppress the access check.
8409 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8410 Set_Etype (Def_Id, Ptr_Typ);
8411 Set_Is_Known_Non_Null (Def_Id);
8413 Res_Decl :=
8414 Make_Object_Declaration (Loc,
8415 Defining_Identifier => Def_Id,
8416 Constant_Present => True,
8417 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8418 Expression =>
8419 Make_Reference (Loc, Relocate_Node (Func_Call)));
8421 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8423 -- If the result subtype of the called function is definite and is
8424 -- not itself the return expression of an enclosing BIP function,
8425 -- then mark the object as having no initialization.
8427 if Definite
8428 and then not Is_Return_Object (Defining_Identifier (Obj_Decl))
8429 then
8430 -- The related object declaration is encased in a transient block
8431 -- because the build-in-place function call contains at least one
8432 -- nested function call that produces a controlled transient
8433 -- temporary:
8435 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8437 -- Since the build-in-place expansion decouples the call from the
8438 -- object declaration, the finalization machinery lacks the
8439 -- context which prompted the generation of the transient
8440 -- block. To resolve this scenario, store the build-in-place call.
8442 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8443 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8444 end if;
8446 Set_Expression (Obj_Decl, Empty);
8447 Set_No_Initialization (Obj_Decl);
8449 -- In case of an indefinite result subtype, or if the call is the
8450 -- return expression of an enclosing BIP function, rewrite the object
8451 -- declaration as an object renaming where the renamed object is a
8452 -- dereference of <function_Call>'reference:
8454 -- Obj : Subt renames <function_call>'Ref.all;
8456 else
8457 Call_Deref :=
8458 Make_Explicit_Dereference (Obj_Loc,
8459 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8461 Rewrite (Obj_Decl,
8462 Make_Object_Renaming_Declaration (Obj_Loc,
8463 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8464 Subtype_Mark => New_Occurrence_Of (Result_Subt, Obj_Loc),
8465 Name => Call_Deref));
8467 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8469 -- If the original entity comes from source, then mark the new
8470 -- entity as needing debug information, even though it's defined
8471 -- by a generated renaming that does not come from source, so that
8472 -- the Materialize_Entity flag will be set on the entity when
8473 -- Debug_Renaming_Declaration is called during analysis.
8475 if Comes_From_Source (Obj_Def_Id) then
8476 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8477 end if;
8479 Analyze (Obj_Decl);
8480 Replace_Renaming_Declaration_Id
8481 (Obj_Decl, Original_Node (Obj_Decl));
8482 end if;
8483 end;
8485 -- If the object entity has a class-wide Etype, then we need to change
8486 -- it to the result subtype of the function call, because otherwise the
8487 -- object will be class-wide without an explicit initialization and
8488 -- won't be allocated properly by the back end. It seems unclean to make
8489 -- such a revision to the type at this point, and we should try to
8490 -- improve this treatment when build-in-place functions with class-wide
8491 -- results are implemented. ???
8493 if Is_Class_Wide_Type (Etype (Defining_Identifier (Obj_Decl))) then
8494 Set_Etype (Defining_Identifier (Obj_Decl), Result_Subt);
8495 end if;
8496 end Make_Build_In_Place_Call_In_Object_Declaration;
8498 -------------------------------------------------
8499 -- Make_Build_In_Place_Iface_Call_In_Allocator --
8500 -------------------------------------------------
8502 procedure Make_Build_In_Place_Iface_Call_In_Allocator
8503 (Allocator : Node_Id;
8504 Function_Call : Node_Id)
8506 BIP_Func_Call : constant Node_Id :=
8507 Unqual_BIP_Iface_Function_Call (Function_Call);
8508 Loc : constant Source_Ptr := Sloc (Function_Call);
8510 Anon_Type : Entity_Id;
8511 Tmp_Decl : Node_Id;
8512 Tmp_Id : Entity_Id;
8514 begin
8515 -- No action of the call has already been processed
8517 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8518 return;
8519 end if;
8521 Tmp_Id := Make_Temporary (Loc, 'D');
8523 -- Insert a temporary before N initialized with the BIP function call
8524 -- without its enclosing type conversions and analyze it without its
8525 -- expansion. This temporary facilitates us reusing the BIP machinery,
8526 -- which takes care of adding the extra build-in-place actuals and
8527 -- transforms this object declaration into an object renaming
8528 -- declaration.
8530 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
8531 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
8532 Set_Etype (Anon_Type, Anon_Type);
8534 Tmp_Decl :=
8535 Make_Object_Declaration (Loc,
8536 Defining_Identifier => Tmp_Id,
8537 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
8538 Expression =>
8539 Make_Allocator (Loc,
8540 Expression =>
8541 Make_Qualified_Expression (Loc,
8542 Subtype_Mark =>
8543 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8544 Expression => New_Copy_Tree (BIP_Func_Call))));
8546 Expander_Mode_Save_And_Set (False);
8547 Insert_Action (Allocator, Tmp_Decl);
8548 Expander_Mode_Restore;
8550 Make_Build_In_Place_Call_In_Allocator
8551 (Allocator => Expression (Tmp_Decl),
8552 Function_Call => Expression (Expression (Tmp_Decl)));
8554 Rewrite (Allocator, New_Occurrence_Of (Tmp_Id, Loc));
8555 end Make_Build_In_Place_Iface_Call_In_Allocator;
8557 ---------------------------------------------------------
8558 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
8559 ---------------------------------------------------------
8561 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
8562 (Function_Call : Node_Id)
8564 BIP_Func_Call : constant Node_Id :=
8565 Unqual_BIP_Iface_Function_Call (Function_Call);
8566 Loc : constant Source_Ptr := Sloc (Function_Call);
8568 Tmp_Decl : Node_Id;
8569 Tmp_Id : Entity_Id;
8571 begin
8572 -- No action of the call has already been processed
8574 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8575 return;
8576 end if;
8578 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
8580 -- Insert a temporary before the call initialized with function call to
8581 -- reuse the BIP machinery which takes care of adding the extra build-in
8582 -- place actuals and transforms this object declaration into an object
8583 -- renaming declaration.
8585 Tmp_Id := Make_Temporary (Loc, 'D');
8587 Tmp_Decl :=
8588 Make_Object_Declaration (Loc,
8589 Defining_Identifier => Tmp_Id,
8590 Object_Definition =>
8591 New_Occurrence_Of (Etype (Function_Call), Loc),
8592 Expression => Relocate_Node (Function_Call));
8594 Expander_Mode_Save_And_Set (False);
8595 Insert_Action (Function_Call, Tmp_Decl);
8596 Expander_Mode_Restore;
8598 Make_Build_In_Place_Iface_Call_In_Object_Declaration
8599 (Obj_Decl => Tmp_Decl,
8600 Function_Call => Expression (Tmp_Decl));
8601 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
8603 ----------------------------------------------------------
8604 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
8605 ----------------------------------------------------------
8607 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
8608 (Obj_Decl : Node_Id;
8609 Function_Call : Node_Id)
8611 BIP_Func_Call : constant Node_Id :=
8612 Unqual_BIP_Iface_Function_Call (Function_Call);
8613 Loc : constant Source_Ptr := Sloc (Function_Call);
8614 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8616 Tmp_Decl : Node_Id;
8617 Tmp_Id : Entity_Id;
8619 begin
8620 -- No action of the call has already been processed
8622 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8623 return;
8624 end if;
8626 Tmp_Id := Make_Temporary (Loc, 'D');
8628 -- Insert a temporary before N initialized with the BIP function call
8629 -- without its enclosing type conversions and analyze it without its
8630 -- expansion. This temporary facilitates us reusing the BIP machinery,
8631 -- which takes care of adding the extra build-in-place actuals and
8632 -- transforms this object declaration into an object renaming
8633 -- declaration.
8635 Tmp_Decl :=
8636 Make_Object_Declaration (Loc,
8637 Defining_Identifier => Tmp_Id,
8638 Object_Definition =>
8639 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8640 Expression => New_Copy_Tree (BIP_Func_Call));
8642 Expander_Mode_Save_And_Set (False);
8643 Insert_Action (Obj_Decl, Tmp_Decl);
8644 Expander_Mode_Restore;
8646 Make_Build_In_Place_Call_In_Object_Declaration
8647 (Obj_Decl => Tmp_Decl,
8648 Function_Call => Expression (Tmp_Decl));
8650 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
8652 -- Replace the original build-in-place function call by a reference to
8653 -- the resulting temporary object renaming declaration. In this way,
8654 -- all the interface conversions performed in the original Function_Call
8655 -- on the build-in-place object are preserved.
8657 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
8659 -- Replace the original object declaration by an internal object
8660 -- renaming declaration. This leaves the generated code more clean (the
8661 -- build-in-place function call in an object renaming declaration and
8662 -- displacements of the pointer to the build-in-place object in another
8663 -- renaming declaration) and allows us to invoke the routine that takes
8664 -- care of replacing the identifier of the renaming declaration (routine
8665 -- originally developed for the regular build-in-place management).
8667 Rewrite (Obj_Decl,
8668 Make_Object_Renaming_Declaration (Loc,
8669 Defining_Identifier => Make_Temporary (Loc, 'D'),
8670 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
8671 Name => Function_Call));
8672 Analyze (Obj_Decl);
8674 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
8675 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
8677 --------------------------------------------
8678 -- Make_CPP_Constructor_Call_In_Allocator --
8679 --------------------------------------------
8681 procedure Make_CPP_Constructor_Call_In_Allocator
8682 (Allocator : Node_Id;
8683 Function_Call : Node_Id)
8685 Loc : constant Source_Ptr := Sloc (Function_Call);
8686 Acc_Type : constant Entity_Id := Etype (Allocator);
8687 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8688 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8690 New_Allocator : Node_Id;
8691 Return_Obj_Access : Entity_Id;
8692 Tmp_Obj : Node_Id;
8694 begin
8695 pragma Assert (Nkind (Allocator) = N_Allocator
8696 and then Nkind (Function_Call) = N_Function_Call);
8697 pragma Assert (Convention (Function_Id) = Convention_CPP
8698 and then Is_Constructor (Function_Id));
8699 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8701 -- Replace the initialized allocator of form "new T'(Func (...))" with
8702 -- an uninitialized allocator of form "new T", where T is the result
8703 -- subtype of the called function. The call to the function is handled
8704 -- separately further below.
8706 New_Allocator :=
8707 Make_Allocator (Loc,
8708 Expression => New_Occurrence_Of (Result_Subt, Loc));
8709 Set_No_Initialization (New_Allocator);
8711 -- Copy attributes to new allocator. Note that the new allocator
8712 -- logically comes from source if the original one did, so copy the
8713 -- relevant flag. This ensures proper treatment of the restriction
8714 -- No_Implicit_Heap_Allocations in this case.
8716 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8717 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8718 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8720 Rewrite (Allocator, New_Allocator);
8722 -- Create a new access object and initialize it to the result of the
8723 -- new uninitialized allocator. Note: we do not use Allocator as the
8724 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8725 -- as this would create a sort of infinite "recursion".
8727 Return_Obj_Access := Make_Temporary (Loc, 'R');
8728 Set_Etype (Return_Obj_Access, Acc_Type);
8730 -- Generate:
8731 -- Rnnn : constant ptr_T := new (T);
8732 -- Init (Rnn.all,...);
8734 Tmp_Obj :=
8735 Make_Object_Declaration (Loc,
8736 Defining_Identifier => Return_Obj_Access,
8737 Constant_Present => True,
8738 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8739 Expression => Relocate_Node (Allocator));
8740 Insert_Action (Allocator, Tmp_Obj);
8742 Insert_List_After_And_Analyze (Tmp_Obj,
8743 Build_Initialization_Call (Loc,
8744 Id_Ref =>
8745 Make_Explicit_Dereference (Loc,
8746 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8747 Typ => Etype (Function_Id),
8748 Constructor_Ref => Function_Call));
8750 -- Finally, replace the allocator node with a reference to the result of
8751 -- the function call itself (which will effectively be an access to the
8752 -- object created by the allocator).
8754 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8756 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8757 -- generate an implicit conversion to force displacement of the "this"
8758 -- pointer.
8760 if Is_Interface (Designated_Type (Acc_Type)) then
8761 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8762 end if;
8764 Analyze_And_Resolve (Allocator, Acc_Type);
8765 end Make_CPP_Constructor_Call_In_Allocator;
8767 -----------------------------------
8768 -- Needs_BIP_Finalization_Master --
8769 -----------------------------------
8771 function Needs_BIP_Finalization_Master
8772 (Func_Id : Entity_Id) return Boolean
8774 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8775 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8776 begin
8777 -- A formal giving the finalization master is needed for build-in-place
8778 -- functions whose result type needs finalization or is a tagged type.
8779 -- Tagged primitive build-in-place functions need such a formal because
8780 -- they can be called by a dispatching call, and extensions may require
8781 -- finalization even if the root type doesn't. This means they're also
8782 -- needed for tagged nonprimitive build-in-place functions with tagged
8783 -- results, since such functions can be called via access-to-function
8784 -- types, and those can be used to call primitives, so masters have to
8785 -- be passed to all such build-in-place functions, primitive or not.
8787 return
8788 not Restriction_Active (No_Finalization)
8789 and then (Needs_Finalization (Func_Typ)
8790 or else Is_Tagged_Type (Func_Typ));
8791 end Needs_BIP_Finalization_Master;
8793 --------------------------
8794 -- Needs_BIP_Alloc_Form --
8795 --------------------------
8797 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8798 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8799 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8800 begin
8801 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8802 end Needs_BIP_Alloc_Form;
8804 --------------------------------------
8805 -- Needs_Result_Accessibility_Level --
8806 --------------------------------------
8808 function Needs_Result_Accessibility_Level
8809 (Func_Id : Entity_Id) return Boolean
8811 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8813 function Has_Unconstrained_Access_Discriminant_Component
8814 (Comp_Typ : Entity_Id) return Boolean;
8815 -- Returns True if any component of the type has an unconstrained access
8816 -- discriminant.
8818 -----------------------------------------------------
8819 -- Has_Unconstrained_Access_Discriminant_Component --
8820 -----------------------------------------------------
8822 function Has_Unconstrained_Access_Discriminant_Component
8823 (Comp_Typ : Entity_Id) return Boolean
8825 begin
8826 if not Is_Limited_Type (Comp_Typ) then
8827 return False;
8829 -- Only limited types can have access discriminants with
8830 -- defaults.
8832 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8833 return True;
8835 elsif Is_Array_Type (Comp_Typ) then
8836 return Has_Unconstrained_Access_Discriminant_Component
8837 (Underlying_Type (Component_Type (Comp_Typ)));
8839 elsif Is_Record_Type (Comp_Typ) then
8840 declare
8841 Comp : Entity_Id;
8843 begin
8844 Comp := First_Component (Comp_Typ);
8845 while Present (Comp) loop
8846 if Has_Unconstrained_Access_Discriminant_Component
8847 (Underlying_Type (Etype (Comp)))
8848 then
8849 return True;
8850 end if;
8852 Next_Component (Comp);
8853 end loop;
8854 end;
8855 end if;
8857 return False;
8858 end Has_Unconstrained_Access_Discriminant_Component;
8860 Feature_Disabled : constant Boolean := True;
8861 -- Temporary
8863 -- Start of processing for Needs_Result_Accessibility_Level
8865 begin
8866 -- False if completion unavailable (how does this happen???)
8868 if not Present (Func_Typ) then
8869 return False;
8871 elsif Feature_Disabled then
8872 return False;
8874 -- False if not a function, also handle enum-lit renames case
8876 elsif Func_Typ = Standard_Void_Type
8877 or else Is_Scalar_Type (Func_Typ)
8878 then
8879 return False;
8881 -- Handle a corner case, a cross-dialect subp renaming. For example,
8882 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
8883 -- an Ada 2005 (or earlier) unit references predefined run-time units.
8885 elsif Present (Alias (Func_Id)) then
8887 -- Unimplemented: a cross-dialect subp renaming which does not set
8888 -- the Alias attribute (e.g., a rename of a dereference of an access
8889 -- to subprogram value). ???
8891 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
8893 -- Remaining cases require Ada 2012 mode
8895 elsif Ada_Version < Ada_2012 then
8896 return False;
8898 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
8899 or else Is_Tagged_Type (Func_Typ)
8900 then
8901 -- In the case of, say, a null tagged record result type, the need
8902 -- for this extra parameter might not be obvious. This function
8903 -- returns True for all tagged types for compatibility reasons.
8904 -- A function with, say, a tagged null controlling result type might
8905 -- be overridden by a primitive of an extension having an access
8906 -- discriminant and the overrider and overridden must have compatible
8907 -- calling conventions (including implicitly declared parameters).
8908 -- Similarly, values of one access-to-subprogram type might designate
8909 -- both a primitive subprogram of a given type and a function
8910 -- which is, for example, not a primitive subprogram of any type.
8911 -- Again, this requires calling convention compatibility.
8912 -- It might be possible to solve these issues by introducing
8913 -- wrappers, but that is not the approach that was chosen.
8915 return True;
8917 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
8918 return True;
8920 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
8921 return True;
8923 -- False for all other cases
8925 else
8926 return False;
8927 end if;
8928 end Needs_Result_Accessibility_Level;
8930 -------------------------------------
8931 -- Replace_Renaming_Declaration_Id --
8932 -------------------------------------
8934 procedure Replace_Renaming_Declaration_Id
8935 (New_Decl : Node_Id;
8936 Orig_Decl : Node_Id)
8938 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
8939 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
8941 begin
8942 Set_Chars (New_Id, Chars (Orig_Id));
8944 -- Swap next entity links in preparation for exchanging entities
8946 declare
8947 Next_Id : constant Entity_Id := Next_Entity (New_Id);
8948 begin
8949 Set_Next_Entity (New_Id, Next_Entity (Orig_Id));
8950 Set_Next_Entity (Orig_Id, Next_Id);
8951 end;
8953 Set_Homonym (New_Id, Homonym (Orig_Id));
8954 Exchange_Entities (New_Id, Orig_Id);
8956 -- Preserve source indication of original declaration, so that xref
8957 -- information is properly generated for the right entity.
8959 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
8960 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
8962 Set_Comes_From_Source (New_Id, False);
8963 end Replace_Renaming_Declaration_Id;
8965 ---------------------------------
8966 -- Rewrite_Function_Call_For_C --
8967 ---------------------------------
8969 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
8970 Orig_Func : constant Entity_Id := Entity (Name (N));
8971 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
8972 Par : constant Node_Id := Parent (N);
8973 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
8974 Loc : constant Source_Ptr := Sloc (Par);
8975 Actuals : List_Id;
8976 Last_Actual : Node_Id;
8977 Last_Formal : Entity_Id;
8979 -- Start of processing for Rewrite_Function_Call_For_C
8981 begin
8982 -- The actuals may be given by named associations, so the added actual
8983 -- that is the target of the return value of the call must be a named
8984 -- association as well, so we retrieve the name of the generated
8985 -- out_formal.
8987 Last_Formal := First_Formal (Proc_Id);
8988 while Present (Next_Formal (Last_Formal)) loop
8989 Last_Formal := Next_Formal (Last_Formal);
8990 end loop;
8992 Actuals := Parameter_Associations (N);
8994 -- The original function may lack parameters
8996 if No (Actuals) then
8997 Actuals := New_List;
8998 end if;
9000 -- If the function call is the expression of an assignment statement,
9001 -- transform the assignment into a procedure call. Generate:
9003 -- LHS := Func_Call (...);
9005 -- Proc_Call (..., LHS);
9007 -- If function is inherited, a conversion may be necessary.
9009 if Nkind (Par) = N_Assignment_Statement then
9010 Last_Actual := Name (Par);
9012 if not Comes_From_Source (Orig_Func)
9013 and then Etype (Orig_Func) /= Etype (Func_Id)
9014 then
9015 Last_Actual :=
9016 Make_Type_Conversion (Loc,
9017 New_Occurrence_Of (Etype (Func_Id), Loc),
9018 Last_Actual);
9019 end if;
9021 Append_To (Actuals,
9022 Make_Parameter_Association (Loc,
9023 Selector_Name =>
9024 Make_Identifier (Loc, Chars (Last_Formal)),
9025 Explicit_Actual_Parameter => Last_Actual));
9027 Rewrite (Par,
9028 Make_Procedure_Call_Statement (Loc,
9029 Name => New_Occurrence_Of (Proc_Id, Loc),
9030 Parameter_Associations => Actuals));
9031 Analyze (Par);
9033 -- Otherwise the context is an expression. Generate a temporary and a
9034 -- procedure call to obtain the function result. Generate:
9036 -- ... Func_Call (...) ...
9038 -- Temp : ...;
9039 -- Proc_Call (..., Temp);
9040 -- ... Temp ...
9042 else
9043 declare
9044 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9045 Call : Node_Id;
9046 Decl : Node_Id;
9048 begin
9049 -- Generate:
9050 -- Temp : ...;
9052 Decl :=
9053 Make_Object_Declaration (Loc,
9054 Defining_Identifier => Temp_Id,
9055 Object_Definition =>
9056 New_Occurrence_Of (Etype (Func_Id), Loc));
9058 -- Generate:
9059 -- Proc_Call (..., Temp);
9061 Append_To (Actuals,
9062 Make_Parameter_Association (Loc,
9063 Selector_Name =>
9064 Make_Identifier (Loc, Chars (Last_Formal)),
9065 Explicit_Actual_Parameter =>
9066 New_Occurrence_Of (Temp_Id, Loc)));
9068 Call :=
9069 Make_Procedure_Call_Statement (Loc,
9070 Name => New_Occurrence_Of (Proc_Id, Loc),
9071 Parameter_Associations => Actuals);
9073 Insert_Actions (Par, New_List (Decl, Call));
9074 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9075 end;
9076 end if;
9077 end Rewrite_Function_Call_For_C;
9079 ------------------------------------
9080 -- Set_Enclosing_Sec_Stack_Return --
9081 ------------------------------------
9083 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9084 P : Node_Id := N;
9086 begin
9087 -- Due to a possible mix of internally generated blocks, source blocks
9088 -- and loops, the scope stack may not be contiguous as all labels are
9089 -- inserted at the top level within the related function. Instead,
9090 -- perform a parent-based traversal and mark all appropriate constructs.
9092 while Present (P) loop
9094 -- Mark the label of a source or internally generated block or
9095 -- loop.
9097 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
9098 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9100 -- Mark the enclosing function
9102 elsif Nkind (P) = N_Subprogram_Body then
9103 if Present (Corresponding_Spec (P)) then
9104 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9105 else
9106 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9107 end if;
9109 -- Do not go beyond the enclosing function
9111 exit;
9112 end if;
9114 P := Parent (P);
9115 end loop;
9116 end Set_Enclosing_Sec_Stack_Return;
9118 ------------------------------------
9119 -- Unqual_BIP_Iface_Function_Call --
9120 ------------------------------------
9122 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9123 Has_Pointer_Displacement : Boolean := False;
9124 On_Object_Declaration : Boolean := False;
9125 -- Remember if processing the renaming expressions on recursion we have
9126 -- traversed an object declaration, since we can traverse many object
9127 -- declaration renamings but just one regular object declaration.
9129 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9130 -- Search for a build-in-place function call skipping any qualification
9131 -- including qualified expressions, type conversions, references, calls
9132 -- to displace the pointer to the object, and renamings. Return Empty if
9133 -- no build-in-place function call is found.
9135 ------------------------------
9136 -- Unqual_BIP_Function_Call --
9137 ------------------------------
9139 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9140 begin
9141 -- Recurse to handle case of multiple levels of qualification and/or
9142 -- conversion.
9144 if Nkind_In (Expr, N_Qualified_Expression,
9145 N_Type_Conversion,
9146 N_Unchecked_Type_Conversion)
9147 then
9148 return Unqual_BIP_Function_Call (Expression (Expr));
9150 -- Recurse to handle case of multiple levels of references and
9151 -- explicit dereferences.
9153 elsif Nkind_In (Expr, N_Attribute_Reference,
9154 N_Explicit_Dereference,
9155 N_Reference)
9156 then
9157 return Unqual_BIP_Function_Call (Prefix (Expr));
9159 -- Recurse on object renamings
9161 elsif Nkind (Expr) = N_Identifier
9162 and then Present (Entity (Expr))
9163 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9164 and then Nkind (Parent (Entity (Expr))) =
9165 N_Object_Renaming_Declaration
9166 and then Present (Renamed_Object (Entity (Expr)))
9167 then
9168 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9170 -- Recurse on the initializing expression of the first reference of
9171 -- an object declaration.
9173 elsif not On_Object_Declaration
9174 and then Nkind (Expr) = N_Identifier
9175 and then Present (Entity (Expr))
9176 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9177 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9178 and then Present (Expression (Parent (Entity (Expr))))
9179 then
9180 On_Object_Declaration := True;
9181 return
9182 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9184 -- Recurse to handle calls to displace the pointer to the object to
9185 -- reference a secondary dispatch table.
9187 elsif Nkind (Expr) = N_Function_Call
9188 and then Nkind (Name (Expr)) in N_Has_Entity
9189 and then Present (Entity (Name (Expr)))
9190 and then RTU_Loaded (Ada_Tags)
9191 and then RTE_Available (RE_Displace)
9192 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9193 then
9194 Has_Pointer_Displacement := True;
9195 return
9196 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9198 -- Normal case: check if the inner expression is a BIP function call
9199 -- and the pointer to the object is displaced.
9201 elsif Has_Pointer_Displacement
9202 and then Is_Build_In_Place_Function_Call (Expr)
9203 then
9204 return Expr;
9206 else
9207 return Empty;
9208 end if;
9209 end Unqual_BIP_Function_Call;
9211 -- Start of processing for Unqual_BIP_Iface_Function_Call
9213 begin
9214 return Unqual_BIP_Function_Call (Expr);
9215 end Unqual_BIP_Iface_Function_Call;
9217 end Exp_Ch6;