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