gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobef6406d203eebfe74f10fb0d41f04a6e1842d808
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-2018, 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 Aspects; use Aspects;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Elists; use Elists;
34 with Expander; use Expander;
35 with Exp_Aggr; use Exp_Aggr;
36 with Exp_Atag; use Exp_Atag;
37 with Exp_Ch2; use Exp_Ch2;
38 with Exp_Ch3; use Exp_Ch3;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Ch9; use Exp_Ch9;
41 with Exp_Dbug; use Exp_Dbug;
42 with Exp_Disp; use Exp_Disp;
43 with Exp_Dist; use Exp_Dist;
44 with Exp_Intr; use Exp_Intr;
45 with Exp_Pakd; use Exp_Pakd;
46 with Exp_Tss; use Exp_Tss;
47 with Exp_Util; use Exp_Util;
48 with Freeze; use Freeze;
49 with Inline; use Inline;
50 with Itypes; use Itypes;
51 with Lib; use Lib;
52 with Namet; use Namet;
53 with Nlists; use Nlists;
54 with Nmake; use Nmake;
55 with Opt; use Opt;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aux; use Sem_Aux;
61 with Sem_Ch6; use Sem_Ch6;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch12; use Sem_Ch12;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Mech; use Sem_Mech;
70 with Sem_Res; use Sem_Res;
71 with Sem_SCIL; use Sem_SCIL;
72 with Sem_Util; use Sem_Util;
73 with Sinfo; use Sinfo;
74 with Snames; use Snames;
75 with Stand; use Stand;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Validsw; use Validsw;
80 package body Exp_Ch6 is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 procedure Add_Access_Actual_To_Build_In_Place_Call
87 (Function_Call : Node_Id;
88 Function_Id : Entity_Id;
89 Return_Object : Node_Id;
90 Is_Access : Boolean := False);
91 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
92 -- object name given by Return_Object and add the attribute to the end of
93 -- the actual parameter list associated with the build-in-place function
94 -- call denoted by Function_Call. However, if Is_Access is True, then
95 -- Return_Object is already an access expression, in which case it's passed
96 -- along directly to the build-in-place function. Finally, if Return_Object
97 -- is empty, then pass a null literal as the actual.
99 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
100 (Function_Call : Node_Id;
101 Function_Id : Entity_Id;
102 Alloc_Form : BIP_Allocation_Form := Unspecified;
103 Alloc_Form_Exp : Node_Id := Empty;
104 Pool_Actual : Node_Id := Make_Null (No_Location));
105 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
106 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
107 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
108 -- otherwise pass a literal corresponding to the Alloc_Form parameter
109 -- (which must not be Unspecified in that case). Pool_Actual is the
110 -- parameter to pass to BIP_Storage_Pool.
112 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
113 (Func_Call : Node_Id;
114 Func_Id : Entity_Id;
115 Ptr_Typ : Entity_Id := Empty;
116 Master_Exp : Node_Id := Empty);
117 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
118 -- finalization actions, add an actual parameter which is a pointer to the
119 -- finalization master of the caller. If Master_Exp is not Empty, then that
120 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
121 -- will result in an automatic "null" value for the actual.
123 procedure Add_Task_Actuals_To_Build_In_Place_Call
124 (Function_Call : Node_Id;
125 Function_Id : Entity_Id;
126 Master_Actual : Node_Id;
127 Chain : Node_Id := Empty);
128 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
129 -- contains tasks, add two actual parameters: the master, and a pointer to
130 -- the caller's activation chain. Master_Actual is the actual parameter
131 -- expression to pass for the master. In most cases, this is the current
132 -- master (_master). The two exceptions are: If the function call is the
133 -- initialization expression for an allocator, we pass the master of the
134 -- access type. If the function call is the initialization expression for a
135 -- return object, we pass along the master passed in by the caller. In most
136 -- contexts, the activation chain to pass is the local one, which is
137 -- indicated by No (Chain). However, in an allocator, the caller passes in
138 -- the activation Chain. Note: Master_Actual can be Empty, but only if
139 -- there are no tasks.
141 function Caller_Known_Size
142 (Func_Call : Node_Id;
143 Result_Subt : Entity_Id) return Boolean;
144 -- True if result subtype is definite, or has a size that does not require
145 -- secondary stack usage (i.e. no variant part or components whose type
146 -- depends on discriminants). In particular, untagged types with only
147 -- access discriminants do not require secondary stack use. Note we must
148 -- always use the secondary stack for dispatching-on-result calls.
150 procedure Check_Overriding_Operation (Subp : Entity_Id);
151 -- Subp is a dispatching operation. Check whether it may override an
152 -- inherited private operation, in which case its DT entry is that of
153 -- the hidden operation, not the one it may have received earlier.
154 -- This must be done before emitting the code to set the corresponding
155 -- DT to the address of the subprogram. The actual placement of Subp in
156 -- the proper place in the list of primitive operations is done in
157 -- Declare_Inherited_Private_Subprograms, which also has to deal with
158 -- implicit operations. This duplication is unavoidable for now???
160 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
161 -- This procedure is called only if the subprogram body N, whose spec
162 -- has the given entity Spec, contains a parameterless recursive call.
163 -- It attempts to generate runtime code to detect if this a case of
164 -- infinite recursion.
166 -- The body is scanned to determine dependencies. If the only external
167 -- dependencies are on a small set of scalar variables, then the values
168 -- of these variables are captured on entry to the subprogram, and if
169 -- the values are not changed for the call, we know immediately that
170 -- we have an infinite recursion.
172 procedure Expand_Actuals
173 (N : Node_Id;
174 Subp : Entity_Id;
175 Post_Call : out List_Id);
176 -- Return a list of actions to take place after the call in Post_Call. The
177 -- call will later be rewritten as an Expression_With_Actions, with the
178 -- Post_Call actions inserted, and the call inside.
180 -- For each actual of an in-out or out parameter which is a numeric (view)
181 -- conversion of the form T (A), where A denotes a variable, we insert the
182 -- declaration:
184 -- Temp : T[ := T (A)];
186 -- prior to the call. Then we replace the actual with a reference to Temp,
187 -- and append the assignment:
189 -- A := TypeA (Temp);
191 -- after the call. Here TypeA is the actual type of variable A. For out
192 -- parameters, the initial declaration has no expression. If A is not an
193 -- entity name, we generate instead:
195 -- Var : TypeA renames A;
196 -- Temp : T := Var; -- omitting expression for out parameter.
197 -- ...
198 -- Var := TypeA (Temp);
200 -- For other in-out parameters, we emit the required constraint checks
201 -- before and/or after the call.
203 -- For all parameter modes, actuals that denote components and slices of
204 -- packed arrays are expanded into suitable temporaries.
206 -- For non-scalar objects that are possibly unaligned, add call by copy
207 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
209 -- For OUT and IN OUT parameters, add predicate checks after the call
210 -- based on the predicates of the actual type.
212 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id);
213 -- Does the main work of Expand_Call. Post_Call is as for Expand_Actuals.
215 procedure Expand_Ctrl_Function_Call (N : Node_Id);
216 -- N is a function call which returns a controlled object. Transform the
217 -- call into a temporary which retrieves the returned object from the
218 -- secondary stack using 'reference.
220 procedure Expand_Non_Function_Return (N : Node_Id);
221 -- Expand a simple return statement found in a procedure body, entry body,
222 -- accept statement, or an extended return statement. Note that all non-
223 -- function returns are simple return statements.
225 function Expand_Protected_Object_Reference
226 (N : Node_Id;
227 Scop : Entity_Id) return Node_Id;
229 procedure Expand_Protected_Subprogram_Call
230 (N : Node_Id;
231 Subp : Entity_Id;
232 Scop : Entity_Id);
233 -- A call to a protected subprogram within the protected object may appear
234 -- as a regular call. The list of actuals must be expanded to contain a
235 -- reference to the object itself, and the call becomes a call to the
236 -- corresponding protected subprogram.
238 procedure Expand_Simple_Function_Return (N : Node_Id);
239 -- Expand simple return from function. In the case where we are returning
240 -- from a function body this is called by Expand_N_Simple_Return_Statement.
242 function Has_Unconstrained_Access_Discriminants
243 (Subtyp : Entity_Id) return Boolean;
244 -- Returns True if the given subtype is unconstrained and has one or more
245 -- access discriminants.
247 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id);
248 -- Insert the Post_Call list previously produced by routine Expand_Actuals
249 -- or Expand_Call_Helper into the tree.
251 procedure Replace_Renaming_Declaration_Id
252 (New_Decl : Node_Id;
253 Orig_Decl : Node_Id);
254 -- Replace the internal identifier of the new renaming declaration New_Decl
255 -- with the identifier of its original declaration Orig_Decl exchanging the
256 -- entities containing their defining identifiers to ensure the correct
257 -- replacement of the object declaration by the object renaming declaration
258 -- to avoid homograph conflicts (since the object declaration's defining
259 -- identifier was already entered in the current scope). The Next_Entity
260 -- links of the two entities are also swapped since the entities are part
261 -- of the return scope's entity list and the list structure would otherwise
262 -- be corrupted. The homonym chain is preserved as well.
264 procedure Rewrite_Function_Call_For_C (N : Node_Id);
265 -- When generating C code, replace a call to a function that returns an
266 -- array into the generated procedure with an additional out parameter.
268 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id);
269 -- N is a return statement for a function that returns its result on the
270 -- secondary stack. This sets the Sec_Stack_Needed_For_Return flag on the
271 -- function and all blocks and loops that the return statement is jumping
272 -- out of. This ensures that the secondary stack is not released; otherwise
273 -- the function result would be reclaimed before returning to the caller.
275 ----------------------------------------------
276 -- Add_Access_Actual_To_Build_In_Place_Call --
277 ----------------------------------------------
279 procedure Add_Access_Actual_To_Build_In_Place_Call
280 (Function_Call : Node_Id;
281 Function_Id : Entity_Id;
282 Return_Object : Node_Id;
283 Is_Access : Boolean := False)
285 Loc : constant Source_Ptr := Sloc (Function_Call);
286 Obj_Address : Node_Id;
287 Obj_Acc_Formal : Entity_Id;
289 begin
290 -- Locate the implicit access parameter in the called function
292 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
294 -- If no return object is provided, then pass null
296 if not Present (Return_Object) then
297 Obj_Address := Make_Null (Loc);
298 Set_Parent (Obj_Address, Function_Call);
300 -- If Return_Object is already an expression of an access type, then use
301 -- it directly, since it must be an access value denoting the return
302 -- object, and couldn't possibly be the return object itself.
304 elsif Is_Access then
305 Obj_Address := Return_Object;
306 Set_Parent (Obj_Address, Function_Call);
308 -- Apply Unrestricted_Access to caller's return object
310 else
311 Obj_Address :=
312 Make_Attribute_Reference (Loc,
313 Prefix => Return_Object,
314 Attribute_Name => Name_Unrestricted_Access);
316 Set_Parent (Return_Object, Obj_Address);
317 Set_Parent (Obj_Address, Function_Call);
318 end if;
320 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
322 -- Build the parameter association for the new actual and add it to the
323 -- end of the function's actuals.
325 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
326 end Add_Access_Actual_To_Build_In_Place_Call;
328 ------------------------------------------------------
329 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
330 ------------------------------------------------------
332 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
333 (Function_Call : Node_Id;
334 Function_Id : Entity_Id;
335 Alloc_Form : BIP_Allocation_Form := Unspecified;
336 Alloc_Form_Exp : Node_Id := Empty;
337 Pool_Actual : Node_Id := Make_Null (No_Location))
339 Loc : constant Source_Ptr := Sloc (Function_Call);
341 Alloc_Form_Actual : Node_Id;
342 Alloc_Form_Formal : Node_Id;
343 Pool_Formal : Node_Id;
345 begin
346 -- Nothing to do when the size of the object is known, and the caller is
347 -- in charge of allocating it, and the callee doesn't unconditionally
348 -- require an allocation form (such as due to having a tagged result).
350 if not Needs_BIP_Alloc_Form (Function_Id) then
351 return;
352 end if;
354 -- Locate the implicit allocation form parameter in the called function.
355 -- Maybe it would be better for each implicit formal of a build-in-place
356 -- function to have a flag or a Uint attribute to identify it. ???
358 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
360 if Present (Alloc_Form_Exp) then
361 pragma Assert (Alloc_Form = Unspecified);
363 Alloc_Form_Actual := Alloc_Form_Exp;
365 else
366 pragma Assert (Alloc_Form /= Unspecified);
368 Alloc_Form_Actual :=
369 Make_Integer_Literal (Loc,
370 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
371 end if;
373 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
375 -- Build the parameter association for the new actual and add it to the
376 -- end of the function's actuals.
378 Add_Extra_Actual_To_Call
379 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
381 -- Pass the Storage_Pool parameter. This parameter is omitted on ZFP as
382 -- those targets do not support pools.
384 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
385 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
386 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
387 Add_Extra_Actual_To_Call
388 (Function_Call, Pool_Formal, Pool_Actual);
389 end if;
390 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
392 -----------------------------------------------------------
393 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
394 -----------------------------------------------------------
396 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
397 (Func_Call : Node_Id;
398 Func_Id : Entity_Id;
399 Ptr_Typ : Entity_Id := Empty;
400 Master_Exp : Node_Id := Empty)
402 begin
403 if not Needs_BIP_Finalization_Master (Func_Id) then
404 return;
405 end if;
407 declare
408 Formal : constant Entity_Id :=
409 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
410 Loc : constant Source_Ptr := Sloc (Func_Call);
412 Actual : Node_Id;
413 Desig_Typ : Entity_Id;
415 begin
416 -- If there is a finalization master actual, such as the implicit
417 -- finalization master of an enclosing build-in-place function,
418 -- then this must be added as an extra actual of the call.
420 if Present (Master_Exp) then
421 Actual := Master_Exp;
423 -- Case where the context does not require an actual master
425 elsif No (Ptr_Typ) then
426 Actual := Make_Null (Loc);
428 else
429 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
431 -- Check for a library-level access type whose designated type has
432 -- suppressed finalization or the access type is subject to pragma
433 -- No_Heap_Finalization. Such an access type lacks a master. Pass
434 -- a null actual to callee in order to signal a missing master.
436 if Is_Library_Level_Entity (Ptr_Typ)
437 and then (Finalize_Storage_Only (Desig_Typ)
438 or else No_Heap_Finalization (Ptr_Typ))
439 then
440 Actual := Make_Null (Loc);
442 -- Types in need of finalization actions
444 elsif Needs_Finalization (Desig_Typ) then
446 -- The general mechanism of creating finalization masters for
447 -- anonymous access types is disabled by default, otherwise
448 -- finalization masters will pop all over the place. Such types
449 -- use context-specific masters.
451 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
452 and then No (Finalization_Master (Ptr_Typ))
453 then
454 Build_Anonymous_Master (Ptr_Typ);
455 end if;
457 -- Access-to-controlled types should always have a master
459 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
461 Actual :=
462 Make_Attribute_Reference (Loc,
463 Prefix =>
464 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
465 Attribute_Name => Name_Unrestricted_Access);
467 -- Tagged types
469 else
470 Actual := Make_Null (Loc);
471 end if;
472 end if;
474 Analyze_And_Resolve (Actual, Etype (Formal));
476 -- Build the parameter association for the new actual and add it to
477 -- the end of the function's actuals.
479 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
480 end;
481 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
483 ------------------------------
484 -- Add_Extra_Actual_To_Call --
485 ------------------------------
487 procedure Add_Extra_Actual_To_Call
488 (Subprogram_Call : Node_Id;
489 Extra_Formal : Entity_Id;
490 Extra_Actual : Node_Id)
492 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
493 Param_Assoc : Node_Id;
495 begin
496 Param_Assoc :=
497 Make_Parameter_Association (Loc,
498 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
499 Explicit_Actual_Parameter => Extra_Actual);
501 Set_Parent (Param_Assoc, Subprogram_Call);
502 Set_Parent (Extra_Actual, Param_Assoc);
504 if Present (Parameter_Associations (Subprogram_Call)) then
505 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
506 N_Parameter_Association
507 then
509 -- Find last named actual, and append
511 declare
512 L : Node_Id;
513 begin
514 L := First_Actual (Subprogram_Call);
515 while Present (L) loop
516 if No (Next_Actual (L)) then
517 Set_Next_Named_Actual (Parent (L), Extra_Actual);
518 exit;
519 end if;
520 Next_Actual (L);
521 end loop;
522 end;
524 else
525 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
526 end if;
528 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
530 else
531 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
532 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
533 end if;
534 end Add_Extra_Actual_To_Call;
536 ---------------------------------------------
537 -- Add_Task_Actuals_To_Build_In_Place_Call --
538 ---------------------------------------------
540 procedure Add_Task_Actuals_To_Build_In_Place_Call
541 (Function_Call : Node_Id;
542 Function_Id : Entity_Id;
543 Master_Actual : Node_Id;
544 Chain : Node_Id := Empty)
546 Loc : constant Source_Ptr := Sloc (Function_Call);
547 Result_Subt : constant Entity_Id :=
548 Available_View (Etype (Function_Id));
549 Actual : Node_Id;
550 Chain_Actual : Node_Id;
551 Chain_Formal : Node_Id;
552 Master_Formal : Node_Id;
554 begin
555 -- No such extra parameters are needed if there are no tasks
557 if not Has_Task (Result_Subt) then
558 return;
559 end if;
561 Actual := Master_Actual;
563 -- Use a dummy _master actual in case of No_Task_Hierarchy
565 if Restriction_Active (No_Task_Hierarchy) then
566 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
568 -- In the case where we use the master associated with an access type,
569 -- the actual is an entity and requires an explicit reference.
571 elsif Nkind (Actual) = N_Defining_Identifier then
572 Actual := New_Occurrence_Of (Actual, Loc);
573 end if;
575 -- Locate the implicit master parameter in the called function
577 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
578 Analyze_And_Resolve (Actual, Etype (Master_Formal));
580 -- Build the parameter association for the new actual and add it to the
581 -- end of the function's actuals.
583 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
585 -- Locate the implicit activation chain parameter in the called function
587 Chain_Formal :=
588 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
590 -- Create the actual which is a pointer to the current activation chain
592 if No (Chain) then
593 Chain_Actual :=
594 Make_Attribute_Reference (Loc,
595 Prefix => Make_Identifier (Loc, Name_uChain),
596 Attribute_Name => Name_Unrestricted_Access);
598 -- Allocator case; make a reference to the Chain passed in by the caller
600 else
601 Chain_Actual :=
602 Make_Attribute_Reference (Loc,
603 Prefix => New_Occurrence_Of (Chain, Loc),
604 Attribute_Name => Name_Unrestricted_Access);
605 end if;
607 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
609 -- Build the parameter association for the new actual and add it to the
610 -- end of the function's actuals.
612 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
613 end Add_Task_Actuals_To_Build_In_Place_Call;
615 -----------------------
616 -- BIP_Formal_Suffix --
617 -----------------------
619 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
620 begin
621 case Kind is
622 when BIP_Alloc_Form =>
623 return "BIPalloc";
625 when BIP_Storage_Pool =>
626 return "BIPstoragepool";
628 when BIP_Finalization_Master =>
629 return "BIPfinalizationmaster";
631 when BIP_Task_Master =>
632 return "BIPtaskmaster";
634 when BIP_Activation_Chain =>
635 return "BIPactivationchain";
637 when BIP_Object_Access =>
638 return "BIPaccess";
639 end case;
640 end BIP_Formal_Suffix;
642 ---------------------------
643 -- Build_In_Place_Formal --
644 ---------------------------
646 function Build_In_Place_Formal
647 (Func : Entity_Id;
648 Kind : BIP_Formal_Kind) return Entity_Id
650 Formal_Suffix : constant String := BIP_Formal_Suffix (Kind);
651 Extra_Formal : Entity_Id := Extra_Formals (Func);
653 begin
654 -- Maybe it would be better for each implicit formal of a build-in-place
655 -- function to have a flag or a Uint attribute to identify it. ???
657 -- The return type in the function declaration may have been a limited
658 -- view, and the extra formals for the function were not generated at
659 -- that point. At the point of call the full view must be available and
660 -- the extra formals can be created.
662 if No (Extra_Formal) then
663 Create_Extra_Formals (Func);
664 Extra_Formal := Extra_Formals (Func);
665 end if;
667 -- We search for a formal with a matching suffix. We can't search
668 -- for the full name, because of the code at the end of Sem_Ch6.-
669 -- Create_Extra_Formals, which copies the Extra_Formals over to
670 -- the Alias of an instance, which will cause the formals to have
671 -- "incorrect" names.
673 loop
674 pragma Assert (Present (Extra_Formal));
675 declare
676 Name : constant String := Get_Name_String (Chars (Extra_Formal));
677 begin
678 exit when Name'Length >= Formal_Suffix'Length
679 and then Formal_Suffix =
680 Name (Name'Last - Formal_Suffix'Length + 1 .. Name'Last);
681 end;
683 Next_Formal_With_Extras (Extra_Formal);
684 end loop;
686 return Extra_Formal;
687 end Build_In_Place_Formal;
689 -------------------------------
690 -- Build_Procedure_Body_Form --
691 -------------------------------
693 function Build_Procedure_Body_Form
694 (Func_Id : Entity_Id;
695 Func_Body : Node_Id) return Node_Id
697 Loc : constant Source_Ptr := Sloc (Func_Body);
699 Proc_Decl : constant Node_Id :=
700 Next (Unit_Declaration_Node (Func_Id));
701 -- It is assumed that the next node following the declaration of the
702 -- corresponding subprogram spec is the declaration of the procedure
703 -- form.
705 Proc_Id : constant Entity_Id := Defining_Entity (Proc_Decl);
707 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id);
708 -- Replace each return statement found in the list Stmts with an
709 -- assignment of the return expression to parameter Param_Id.
711 ---------------------
712 -- Replace_Returns --
713 ---------------------
715 procedure Replace_Returns (Param_Id : Entity_Id; Stmts : List_Id) is
716 Stmt : Node_Id;
718 begin
719 Stmt := First (Stmts);
720 while Present (Stmt) loop
721 if Nkind (Stmt) = N_Block_Statement then
722 Replace_Returns (Param_Id,
723 Statements (Handled_Statement_Sequence (Stmt)));
725 elsif Nkind (Stmt) = N_Case_Statement then
726 declare
727 Alt : Node_Id;
728 begin
729 Alt := First (Alternatives (Stmt));
730 while Present (Alt) loop
731 Replace_Returns (Param_Id, Statements (Alt));
732 Next (Alt);
733 end loop;
734 end;
736 elsif Nkind (Stmt) = N_Extended_Return_Statement then
737 declare
738 Ret_Obj : constant Entity_Id :=
739 Defining_Entity
740 (First (Return_Object_Declarations (Stmt)));
741 Assign : constant Node_Id :=
742 Make_Assignment_Statement (Sloc (Stmt),
743 Name =>
744 New_Occurrence_Of (Param_Id, Loc),
745 Expression =>
746 New_Occurrence_Of (Ret_Obj, Sloc (Stmt)));
747 Stmts : List_Id;
749 begin
750 -- The extended return may just contain the declaration
752 if Present (Handled_Statement_Sequence (Stmt)) then
753 Stmts := Statements (Handled_Statement_Sequence (Stmt));
754 else
755 Stmts := New_List;
756 end if;
758 Set_Assignment_OK (Name (Assign));
760 Rewrite (Stmt,
761 Make_Block_Statement (Sloc (Stmt),
762 Declarations =>
763 Return_Object_Declarations (Stmt),
764 Handled_Statement_Sequence =>
765 Make_Handled_Sequence_Of_Statements (Loc,
766 Statements => Stmts)));
768 Replace_Returns (Param_Id, Stmts);
770 Append_To (Stmts, Assign);
771 Append_To (Stmts, Make_Simple_Return_Statement (Loc));
772 end;
774 elsif Nkind (Stmt) = N_If_Statement then
775 Replace_Returns (Param_Id, Then_Statements (Stmt));
776 Replace_Returns (Param_Id, Else_Statements (Stmt));
778 declare
779 Part : Node_Id;
780 begin
781 Part := First (Elsif_Parts (Stmt));
782 while Present (Part) loop
783 Replace_Returns (Param_Id, Then_Statements (Part));
784 Next (Part);
785 end loop;
786 end;
788 elsif Nkind (Stmt) = N_Loop_Statement then
789 Replace_Returns (Param_Id, Statements (Stmt));
791 elsif Nkind (Stmt) = N_Simple_Return_Statement then
793 -- Generate:
794 -- Param := Expr;
795 -- return;
797 Rewrite (Stmt,
798 Make_Assignment_Statement (Sloc (Stmt),
799 Name => New_Occurrence_Of (Param_Id, Loc),
800 Expression => Relocate_Node (Expression (Stmt))));
802 Insert_After (Stmt, Make_Simple_Return_Statement (Loc));
804 -- Skip the added return
806 Next (Stmt);
807 end if;
809 Next (Stmt);
810 end loop;
811 end Replace_Returns;
813 -- Local variables
815 Stmts : List_Id;
816 New_Body : Node_Id;
818 -- Start of processing for Build_Procedure_Body_Form
820 begin
821 -- This routine replaces the original function body:
823 -- function F (...) return Array_Typ is
824 -- begin
825 -- ...
826 -- return Something;
827 -- end F;
829 -- with the following:
831 -- procedure P (..., Result : out Array_Typ) is
832 -- begin
833 -- ...
834 -- Result := Something;
835 -- end P;
837 Stmts :=
838 Statements (Handled_Statement_Sequence (Func_Body));
839 Replace_Returns (Last_Entity (Proc_Id), Stmts);
841 New_Body :=
842 Make_Subprogram_Body (Loc,
843 Specification =>
844 Copy_Subprogram_Spec (Specification (Proc_Decl)),
845 Declarations => Declarations (Func_Body),
846 Handled_Statement_Sequence =>
847 Make_Handled_Sequence_Of_Statements (Loc,
848 Statements => Stmts));
850 -- If the function is a generic instance, so is the new procedure.
851 -- Set flag accordingly so that the proper renaming declarations are
852 -- generated.
854 Set_Is_Generic_Instance (Proc_Id, Is_Generic_Instance (Func_Id));
855 return New_Body;
856 end Build_Procedure_Body_Form;
858 -----------------------
859 -- Caller_Known_Size --
860 -----------------------
862 function Caller_Known_Size
863 (Func_Call : Node_Id;
864 Result_Subt : Entity_Id) return Boolean
866 begin
867 return
868 (Is_Definite_Subtype (Underlying_Type (Result_Subt))
869 and then No (Controlling_Argument (Func_Call)))
870 or else not Requires_Transient_Scope (Underlying_Type (Result_Subt));
871 end Caller_Known_Size;
873 --------------------------------
874 -- Check_Overriding_Operation --
875 --------------------------------
877 procedure Check_Overriding_Operation (Subp : Entity_Id) is
878 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
879 Op_List : constant Elist_Id := Primitive_Operations (Typ);
880 Op_Elmt : Elmt_Id;
881 Prim_Op : Entity_Id;
882 Par_Op : Entity_Id;
884 begin
885 if Is_Derived_Type (Typ)
886 and then not Is_Private_Type (Typ)
887 and then In_Open_Scopes (Scope (Etype (Typ)))
888 and then Is_Base_Type (Typ)
889 then
890 -- Subp overrides an inherited private operation if there is an
891 -- inherited operation with a different name than Subp (see
892 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
893 -- same name as Subp.
895 Op_Elmt := First_Elmt (Op_List);
896 while Present (Op_Elmt) loop
897 Prim_Op := Node (Op_Elmt);
898 Par_Op := Alias (Prim_Op);
900 if Present (Par_Op)
901 and then not Comes_From_Source (Prim_Op)
902 and then Chars (Prim_Op) /= Chars (Par_Op)
903 and then Chars (Par_Op) = Chars (Subp)
904 and then Is_Hidden (Par_Op)
905 and then Type_Conformant (Prim_Op, Subp)
906 then
907 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
908 end if;
910 Next_Elmt (Op_Elmt);
911 end loop;
912 end if;
913 end Check_Overriding_Operation;
915 -------------------------------
916 -- Detect_Infinite_Recursion --
917 -------------------------------
919 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
920 Loc : constant Source_Ptr := Sloc (N);
922 Var_List : constant Elist_Id := New_Elmt_List;
923 -- List of globals referenced by body of procedure
925 Call_List : constant Elist_Id := New_Elmt_List;
926 -- List of recursive calls in body of procedure
928 Shad_List : constant Elist_Id := New_Elmt_List;
929 -- List of entity id's for entities created to capture the value of
930 -- referenced globals on entry to the procedure.
932 Scop : constant Uint := Scope_Depth (Spec);
933 -- This is used to record the scope depth of the current procedure, so
934 -- that we can identify global references.
936 Max_Vars : constant := 4;
937 -- Do not test more than four global variables
939 Count_Vars : Natural := 0;
940 -- Count variables found so far
942 Var : Entity_Id;
943 Elm : Elmt_Id;
944 Ent : Entity_Id;
945 Call : Elmt_Id;
946 Decl : Node_Id;
947 Test : Node_Id;
948 Elm1 : Elmt_Id;
949 Elm2 : Elmt_Id;
950 Last : Node_Id;
952 function Process (Nod : Node_Id) return Traverse_Result;
953 -- Function to traverse the subprogram body (using Traverse_Func)
955 -------------
956 -- Process --
957 -------------
959 function Process (Nod : Node_Id) return Traverse_Result is
960 begin
961 -- Procedure call
963 if Nkind (Nod) = N_Procedure_Call_Statement then
965 -- Case of one of the detected recursive calls
967 if Is_Entity_Name (Name (Nod))
968 and then Has_Recursive_Call (Entity (Name (Nod)))
969 and then Entity (Name (Nod)) = Spec
970 then
971 Append_Elmt (Nod, Call_List);
972 return Skip;
974 -- Any other procedure call may have side effects
976 else
977 return Abandon;
978 end if;
980 -- A call to a pure function can always be ignored
982 elsif Nkind (Nod) = N_Function_Call
983 and then Is_Entity_Name (Name (Nod))
984 and then Is_Pure (Entity (Name (Nod)))
985 then
986 return Skip;
988 -- Case of an identifier reference
990 elsif Nkind (Nod) = N_Identifier then
991 Ent := Entity (Nod);
993 -- If no entity, then ignore the reference
995 -- Not clear why this can happen. To investigate, remove this
996 -- test and look at the crash that occurs here in 3401-004 ???
998 if No (Ent) then
999 return Skip;
1001 -- Ignore entities with no Scope, again not clear how this
1002 -- can happen, to investigate, look at 4108-008 ???
1004 elsif No (Scope (Ent)) then
1005 return Skip;
1007 -- Ignore the reference if not to a more global object
1009 elsif Scope_Depth (Scope (Ent)) >= Scop then
1010 return Skip;
1012 -- References to types, exceptions and constants are always OK
1014 elsif Is_Type (Ent)
1015 or else Ekind (Ent) = E_Exception
1016 or else Ekind (Ent) = E_Constant
1017 then
1018 return Skip;
1020 -- If other than a non-volatile scalar variable, we have some
1021 -- kind of global reference (e.g. to a function) that we cannot
1022 -- deal with so we forget the attempt.
1024 elsif Ekind (Ent) /= E_Variable
1025 or else not Is_Scalar_Type (Etype (Ent))
1026 or else Treat_As_Volatile (Ent)
1027 then
1028 return Abandon;
1030 -- Otherwise we have a reference to a global scalar
1032 else
1033 -- Loop through global entities already detected
1035 Elm := First_Elmt (Var_List);
1036 loop
1037 -- If not detected before, record this new global reference
1039 if No (Elm) then
1040 Count_Vars := Count_Vars + 1;
1042 if Count_Vars <= Max_Vars then
1043 Append_Elmt (Entity (Nod), Var_List);
1044 else
1045 return Abandon;
1046 end if;
1048 exit;
1050 -- If recorded before, ignore
1052 elsif Node (Elm) = Entity (Nod) then
1053 return Skip;
1055 -- Otherwise keep looking
1057 else
1058 Next_Elmt (Elm);
1059 end if;
1060 end loop;
1062 return Skip;
1063 end if;
1065 -- For all other node kinds, recursively visit syntactic children
1067 else
1068 return OK;
1069 end if;
1070 end Process;
1072 function Traverse_Body is new Traverse_Func (Process);
1074 -- Start of processing for Detect_Infinite_Recursion
1076 begin
1077 -- Do not attempt detection in No_Implicit_Conditional mode, since we
1078 -- won't be able to generate the code to handle the recursion in any
1079 -- case.
1081 if Restriction_Active (No_Implicit_Conditionals) then
1082 return;
1083 end if;
1085 -- Otherwise do traversal and quit if we get abandon signal
1087 if Traverse_Body (N) = Abandon then
1088 return;
1090 -- We must have a call, since Has_Recursive_Call was set. If not just
1091 -- ignore (this is only an error check, so if we have a funny situation,
1092 -- due to bugs or errors, we do not want to bomb).
1094 elsif Is_Empty_Elmt_List (Call_List) then
1095 return;
1096 end if;
1098 -- Here is the case where we detect recursion at compile time
1100 -- Push our current scope for analyzing the declarations and code that
1101 -- we will insert for the checking.
1103 Push_Scope (Spec);
1105 -- This loop builds temporary variables for each of the referenced
1106 -- globals, so that at the end of the loop the list Shad_List contains
1107 -- these temporaries in one-to-one correspondence with the elements in
1108 -- Var_List.
1110 Last := Empty;
1111 Elm := First_Elmt (Var_List);
1112 while Present (Elm) loop
1113 Var := Node (Elm);
1114 Ent := Make_Temporary (Loc, 'S');
1115 Append_Elmt (Ent, Shad_List);
1117 -- Insert a declaration for this temporary at the start of the
1118 -- declarations for the procedure. The temporaries are declared as
1119 -- constant objects initialized to the current values of the
1120 -- corresponding temporaries.
1122 Decl :=
1123 Make_Object_Declaration (Loc,
1124 Defining_Identifier => Ent,
1125 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
1126 Constant_Present => True,
1127 Expression => New_Occurrence_Of (Var, Loc));
1129 if No (Last) then
1130 Prepend (Decl, Declarations (N));
1131 else
1132 Insert_After (Last, Decl);
1133 end if;
1135 Last := Decl;
1136 Analyze (Decl);
1137 Next_Elmt (Elm);
1138 end loop;
1140 -- Loop through calls
1142 Call := First_Elmt (Call_List);
1143 while Present (Call) loop
1145 -- Build a predicate expression of the form
1147 -- True
1148 -- and then global1 = temp1
1149 -- and then global2 = temp2
1150 -- ...
1152 -- This predicate determines if any of the global values
1153 -- referenced by the procedure have changed since the
1154 -- current call, if not an infinite recursion is assured.
1156 Test := New_Occurrence_Of (Standard_True, Loc);
1158 Elm1 := First_Elmt (Var_List);
1159 Elm2 := First_Elmt (Shad_List);
1160 while Present (Elm1) loop
1161 Test :=
1162 Make_And_Then (Loc,
1163 Left_Opnd => Test,
1164 Right_Opnd =>
1165 Make_Op_Eq (Loc,
1166 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
1167 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
1169 Next_Elmt (Elm1);
1170 Next_Elmt (Elm2);
1171 end loop;
1173 -- Now we replace the call with the sequence
1175 -- if no-changes (see above) then
1176 -- raise Storage_Error;
1177 -- else
1178 -- original-call
1179 -- end if;
1181 Rewrite (Node (Call),
1182 Make_If_Statement (Loc,
1183 Condition => Test,
1184 Then_Statements => New_List (
1185 Make_Raise_Storage_Error (Loc,
1186 Reason => SE_Infinite_Recursion)),
1188 Else_Statements => New_List (
1189 Relocate_Node (Node (Call)))));
1191 Analyze (Node (Call));
1193 Next_Elmt (Call);
1194 end loop;
1196 -- Remove temporary scope stack entry used for analysis
1198 Pop_Scope;
1199 end Detect_Infinite_Recursion;
1201 --------------------
1202 -- Expand_Actuals --
1203 --------------------
1205 procedure Expand_Actuals
1206 (N : Node_Id;
1207 Subp : Entity_Id;
1208 Post_Call : out List_Id)
1210 Loc : constant Source_Ptr := Sloc (N);
1211 Actual : Node_Id;
1212 Formal : Entity_Id;
1213 N_Node : Node_Id;
1214 E_Actual : Entity_Id;
1215 E_Formal : Entity_Id;
1217 procedure Add_Call_By_Copy_Code;
1218 -- For cases where the parameter must be passed by copy, this routine
1219 -- generates a temporary variable into which the actual is copied and
1220 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1221 -- an assignment is also generated to copy the result back. The call
1222 -- also takes care of any constraint checks required for the type
1223 -- conversion case (on both the way in and the way out).
1225 procedure Add_Simple_Call_By_Copy_Code;
1226 -- This is similar to the above, but is used in cases where we know
1227 -- that all that is needed is to simply create a temporary and copy
1228 -- the value in and out of the temporary.
1230 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id);
1231 -- Perform copy-back for actual parameter Act which denotes a validation
1232 -- variable.
1234 procedure Check_Fortran_Logical;
1235 -- A value of type Logical that is passed through a formal parameter
1236 -- must be normalized because .TRUE. usually does not have the same
1237 -- representation as True. We assume that .FALSE. = False = 0.
1238 -- What about functions that return a logical type ???
1240 function Is_Legal_Copy return Boolean;
1241 -- Check that an actual can be copied before generating the temporary
1242 -- to be used in the call. If the actual is of a by_reference type then
1243 -- the program is illegal (this can only happen in the presence of
1244 -- rep. clauses that force an incorrect alignment). If the formal is
1245 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1246 -- the effect that this might lead to unaligned arguments.
1248 function Make_Var (Actual : Node_Id) return Entity_Id;
1249 -- Returns an entity that refers to the given actual parameter, Actual
1250 -- (not including any type conversion). If Actual is an entity name,
1251 -- then this entity is returned unchanged, otherwise a renaming is
1252 -- created to provide an entity for the actual.
1254 procedure Reset_Packed_Prefix;
1255 -- The expansion of a packed array component reference is delayed in
1256 -- the context of a call. Now we need to complete the expansion, so we
1257 -- unmark the analyzed bits in all prefixes.
1259 ---------------------------
1260 -- Add_Call_By_Copy_Code --
1261 ---------------------------
1263 procedure Add_Call_By_Copy_Code is
1264 Crep : Boolean;
1265 Expr : Node_Id;
1266 F_Typ : Entity_Id := Etype (Formal);
1267 Indic : Node_Id;
1268 Init : Node_Id;
1269 Temp : Entity_Id;
1270 V_Typ : Entity_Id;
1271 Var : Entity_Id;
1273 begin
1274 if not Is_Legal_Copy then
1275 return;
1276 end if;
1278 Temp := Make_Temporary (Loc, 'T', Actual);
1280 -- Handle formals whose type comes from the limited view
1282 if From_Limited_With (F_Typ)
1283 and then Has_Non_Limited_View (F_Typ)
1284 then
1285 F_Typ := Non_Limited_View (F_Typ);
1286 end if;
1288 -- Use formal type for temp, unless formal type is an unconstrained
1289 -- array, in which case we don't have to worry about bounds checks,
1290 -- and we use the actual type, since that has appropriate bounds.
1292 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1293 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1294 else
1295 Indic := New_Occurrence_Of (F_Typ, Loc);
1296 end if;
1298 if Nkind (Actual) = N_Type_Conversion then
1299 V_Typ := Etype (Expression (Actual));
1301 -- If the formal is an (in-)out parameter, capture the name
1302 -- of the variable in order to build the post-call assignment.
1304 Var := Make_Var (Expression (Actual));
1306 Crep := not Same_Representation
1307 (F_Typ, Etype (Expression (Actual)));
1309 else
1310 V_Typ := Etype (Actual);
1311 Var := Make_Var (Actual);
1312 Crep := False;
1313 end if;
1315 -- Setup initialization for case of in out parameter, or an out
1316 -- parameter where the formal is an unconstrained array (in the
1317 -- latter case, we have to pass in an object with bounds).
1319 -- If this is an out parameter, the initial copy is wasteful, so as
1320 -- an optimization for the one-dimensional case we extract the
1321 -- bounds of the actual and build an uninitialized temporary of the
1322 -- right size.
1324 if Ekind (Formal) = E_In_Out_Parameter
1325 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1326 then
1327 if Nkind (Actual) = N_Type_Conversion then
1328 if Conversion_OK (Actual) then
1329 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1330 else
1331 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1332 end if;
1334 elsif Ekind (Formal) = E_Out_Parameter
1335 and then Is_Array_Type (F_Typ)
1336 and then Number_Dimensions (F_Typ) = 1
1337 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1338 then
1339 -- Actual is a one-dimensional array or slice, and the type
1340 -- requires no initialization. Create a temporary of the
1341 -- right size, but do not copy actual into it (optimization).
1343 Init := Empty;
1344 Indic :=
1345 Make_Subtype_Indication (Loc,
1346 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1347 Constraint =>
1348 Make_Index_Or_Discriminant_Constraint (Loc,
1349 Constraints => New_List (
1350 Make_Range (Loc,
1351 Low_Bound =>
1352 Make_Attribute_Reference (Loc,
1353 Prefix => New_Occurrence_Of (Var, Loc),
1354 Attribute_Name => Name_First),
1355 High_Bound =>
1356 Make_Attribute_Reference (Loc,
1357 Prefix => New_Occurrence_Of (Var, Loc),
1358 Attribute_Name => Name_Last)))));
1360 else
1361 Init := New_Occurrence_Of (Var, Loc);
1362 end if;
1364 -- An initialization is created for packed conversions as
1365 -- actuals for out parameters to enable Make_Object_Declaration
1366 -- to determine the proper subtype for N_Node. Note that this
1367 -- is wasteful because the extra copying on the call side is
1368 -- not required for such out parameters. ???
1370 elsif Ekind (Formal) = E_Out_Parameter
1371 and then Nkind (Actual) = N_Type_Conversion
1372 and then (Is_Bit_Packed_Array (F_Typ)
1373 or else
1374 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1375 then
1376 if Conversion_OK (Actual) then
1377 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1378 else
1379 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1380 end if;
1382 elsif Ekind (Formal) = E_In_Parameter then
1384 -- Handle the case in which the actual is a type conversion
1386 if Nkind (Actual) = N_Type_Conversion then
1387 if Conversion_OK (Actual) then
1388 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1389 else
1390 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1391 end if;
1392 else
1393 Init := New_Occurrence_Of (Var, Loc);
1394 end if;
1396 else
1397 Init := Empty;
1398 end if;
1400 N_Node :=
1401 Make_Object_Declaration (Loc,
1402 Defining_Identifier => Temp,
1403 Object_Definition => Indic,
1404 Expression => Init);
1405 Set_Assignment_OK (N_Node);
1406 Insert_Action (N, N_Node);
1408 -- Now, normally the deal here is that we use the defining
1409 -- identifier created by that object declaration. There is
1410 -- one exception to this. In the change of representation case
1411 -- the above declaration will end up looking like:
1413 -- temp : type := identifier;
1415 -- And in this case we might as well use the identifier directly
1416 -- and eliminate the temporary. Note that the analysis of the
1417 -- declaration was not a waste of time in that case, since it is
1418 -- what generated the necessary change of representation code. If
1419 -- the change of representation introduced additional code, as in
1420 -- a fixed-integer conversion, the expression is not an identifier
1421 -- and must be kept.
1423 if Crep
1424 and then Present (Expression (N_Node))
1425 and then Is_Entity_Name (Expression (N_Node))
1426 then
1427 Temp := Entity (Expression (N_Node));
1428 Rewrite (N_Node, Make_Null_Statement (Loc));
1429 end if;
1431 -- For IN parameter, all we do is to replace the actual
1433 if Ekind (Formal) = E_In_Parameter then
1434 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1435 Analyze (Actual);
1437 -- Processing for OUT or IN OUT parameter
1439 else
1440 -- Kill current value indications for the temporary variable we
1441 -- created, since we just passed it as an OUT parameter.
1443 Kill_Current_Values (Temp);
1444 Set_Is_Known_Valid (Temp, False);
1446 -- If type conversion, use reverse conversion on exit
1448 if Nkind (Actual) = N_Type_Conversion then
1449 if Conversion_OK (Actual) then
1450 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1451 else
1452 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1453 end if;
1454 else
1455 Expr := New_Occurrence_Of (Temp, Loc);
1456 end if;
1458 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1459 Analyze (Actual);
1461 -- If the actual is a conversion of a packed reference, it may
1462 -- already have been expanded by Remove_Side_Effects, and the
1463 -- resulting variable is a temporary which does not designate
1464 -- the proper out-parameter, which may not be addressable. In
1465 -- that case, generate an assignment to the original expression
1466 -- (before expansion of the packed reference) so that the proper
1467 -- expansion of assignment to a packed component can take place.
1469 declare
1470 Obj : Node_Id;
1471 Lhs : Node_Id;
1473 begin
1474 if Is_Renaming_Of_Object (Var)
1475 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1476 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1477 = N_Indexed_Component
1478 and then
1479 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1480 then
1481 Obj := Renamed_Object (Var);
1482 Lhs :=
1483 Make_Selected_Component (Loc,
1484 Prefix =>
1485 New_Copy_Tree (Original_Node (Prefix (Obj))),
1486 Selector_Name => New_Copy (Selector_Name (Obj)));
1487 Reset_Analyzed_Flags (Lhs);
1489 else
1490 Lhs := New_Occurrence_Of (Var, Loc);
1491 end if;
1493 Set_Assignment_OK (Lhs);
1495 if Is_Access_Type (E_Formal)
1496 and then Is_Entity_Name (Lhs)
1497 and then
1498 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1499 then
1500 -- Copyback target is an Ada 2012 stand-alone object of an
1501 -- anonymous access type.
1503 pragma Assert (Ada_Version >= Ada_2012);
1505 if Type_Access_Level (E_Formal) >
1506 Object_Access_Level (Lhs)
1507 then
1508 Append_To (Post_Call,
1509 Make_Raise_Program_Error (Loc,
1510 Reason => PE_Accessibility_Check_Failed));
1511 end if;
1513 Append_To (Post_Call,
1514 Make_Assignment_Statement (Loc,
1515 Name => Lhs,
1516 Expression => Expr));
1518 -- We would like to somehow suppress generation of the
1519 -- extra_accessibility assignment generated by the expansion
1520 -- of the above assignment statement. It's not a correctness
1521 -- issue because the following assignment renders it dead,
1522 -- but generating back-to-back assignments to the same
1523 -- target is undesirable. ???
1525 Append_To (Post_Call,
1526 Make_Assignment_Statement (Loc,
1527 Name => New_Occurrence_Of (
1528 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1529 Expression => Make_Integer_Literal (Loc,
1530 Type_Access_Level (E_Formal))));
1532 else
1533 Append_To (Post_Call,
1534 Make_Assignment_Statement (Loc,
1535 Name => Lhs,
1536 Expression => Expr));
1537 end if;
1538 end;
1539 end if;
1540 end Add_Call_By_Copy_Code;
1542 ----------------------------------
1543 -- Add_Simple_Call_By_Copy_Code --
1544 ----------------------------------
1546 procedure Add_Simple_Call_By_Copy_Code is
1547 Decl : Node_Id;
1548 F_Typ : Entity_Id := Etype (Formal);
1549 Incod : Node_Id;
1550 Indic : Node_Id;
1551 Lhs : Node_Id;
1552 Outcod : Node_Id;
1553 Rhs : Node_Id;
1554 Temp : Entity_Id;
1556 begin
1557 if not Is_Legal_Copy then
1558 return;
1559 end if;
1561 -- Handle formals whose type comes from the limited view
1563 if From_Limited_With (F_Typ)
1564 and then Has_Non_Limited_View (F_Typ)
1565 then
1566 F_Typ := Non_Limited_View (F_Typ);
1567 end if;
1569 -- Use formal type for temp, unless formal type is an unconstrained
1570 -- array, in which case we don't have to worry about bounds checks,
1571 -- and we use the actual type, since that has appropriate bounds.
1573 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1574 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1575 else
1576 Indic := New_Occurrence_Of (F_Typ, Loc);
1577 end if;
1579 -- Prepare to generate code
1581 Reset_Packed_Prefix;
1583 Temp := Make_Temporary (Loc, 'T', Actual);
1584 Incod := Relocate_Node (Actual);
1585 Outcod := New_Copy_Tree (Incod);
1587 -- Generate declaration of temporary variable, initializing it
1588 -- with the input parameter unless we have an OUT formal or
1589 -- this is an initialization call.
1591 -- If the formal is an out parameter with discriminants, the
1592 -- discriminants must be captured even if the rest of the object
1593 -- is in principle uninitialized, because the discriminants may
1594 -- be read by the called subprogram.
1596 if Ekind (Formal) = E_Out_Parameter then
1597 Incod := Empty;
1599 if Has_Discriminants (F_Typ) then
1600 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1601 end if;
1603 elsif Inside_Init_Proc then
1605 -- Could use a comment here to match comment below ???
1607 if Nkind (Actual) /= N_Selected_Component
1608 or else
1609 not Has_Discriminant_Dependent_Constraint
1610 (Entity (Selector_Name (Actual)))
1611 then
1612 Incod := Empty;
1614 -- Otherwise, keep the component in order to generate the proper
1615 -- actual subtype, that depends on enclosing discriminants.
1617 else
1618 null;
1619 end if;
1620 end if;
1622 Decl :=
1623 Make_Object_Declaration (Loc,
1624 Defining_Identifier => Temp,
1625 Object_Definition => Indic,
1626 Expression => Incod);
1628 if Inside_Init_Proc
1629 and then No (Incod)
1630 then
1631 -- If the call is to initialize a component of a composite type,
1632 -- and the component does not depend on discriminants, use the
1633 -- actual type of the component. This is required in case the
1634 -- component is constrained, because in general the formal of the
1635 -- initialization procedure will be unconstrained. Note that if
1636 -- the component being initialized is constrained by an enclosing
1637 -- discriminant, the presence of the initialization in the
1638 -- declaration will generate an expression for the actual subtype.
1640 Set_No_Initialization (Decl);
1641 Set_Object_Definition (Decl,
1642 New_Occurrence_Of (Etype (Actual), Loc));
1643 end if;
1645 Insert_Action (N, Decl);
1647 -- The actual is simply a reference to the temporary
1649 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1651 -- Generate copy out if OUT or IN OUT parameter
1653 if Ekind (Formal) /= E_In_Parameter then
1654 Lhs := Outcod;
1655 Rhs := New_Occurrence_Of (Temp, Loc);
1657 -- Deal with conversion
1659 if Nkind (Lhs) = N_Type_Conversion then
1660 Lhs := Expression (Lhs);
1661 Rhs := Convert_To (Etype (Actual), Rhs);
1662 end if;
1664 Append_To (Post_Call,
1665 Make_Assignment_Statement (Loc,
1666 Name => Lhs,
1667 Expression => Rhs));
1668 Set_Assignment_OK (Name (Last (Post_Call)));
1669 end if;
1670 end Add_Simple_Call_By_Copy_Code;
1672 --------------------------------------
1673 -- Add_Validation_Call_By_Copy_Code --
1674 --------------------------------------
1676 procedure Add_Validation_Call_By_Copy_Code (Act : Node_Id) is
1677 Expr : Node_Id;
1678 Obj : Node_Id;
1679 Obj_Typ : Entity_Id;
1680 Var : constant Node_Id := Unqual_Conv (Act);
1681 Var_Id : Entity_Id;
1683 begin
1684 -- Copy the value of the validation variable back into the object
1685 -- being validated.
1687 if Is_Entity_Name (Var) then
1688 Var_Id := Entity (Var);
1689 Obj := Validated_Object (Var_Id);
1690 Obj_Typ := Etype (Obj);
1692 Expr := New_Occurrence_Of (Var_Id, Loc);
1694 -- A type conversion is needed when the validation variable and
1695 -- the validated object carry different types. This case occurs
1696 -- when the actual is qualified in some fashion.
1698 -- Common:
1699 -- subtype Int is Integer range ...;
1700 -- procedure Call (Val : in out Integer);
1702 -- Original:
1703 -- Object : Int;
1704 -- Call (Integer (Object));
1706 -- Expanded:
1707 -- Object : Int;
1708 -- Var : Integer := Object; -- conversion to base type
1709 -- if not Var'Valid then -- validity check
1710 -- Call (Var); -- modify Var
1711 -- Object := Int (Var); -- conversion to subtype
1713 if Etype (Var_Id) /= Obj_Typ then
1714 Expr :=
1715 Make_Type_Conversion (Loc,
1716 Subtype_Mark => New_Occurrence_Of (Obj_Typ, Loc),
1717 Expression => Expr);
1718 end if;
1720 -- Generate:
1721 -- Object := Var;
1722 -- <or>
1723 -- Object := Object_Type (Var);
1725 Append_To (Post_Call,
1726 Make_Assignment_Statement (Loc,
1727 Name => Obj,
1728 Expression => Expr));
1730 -- If the flow reaches this point, then this routine was invoked with
1731 -- an actual which does not denote a validation variable.
1733 else
1734 pragma Assert (False);
1735 null;
1736 end if;
1737 end Add_Validation_Call_By_Copy_Code;
1739 ---------------------------
1740 -- Check_Fortran_Logical --
1741 ---------------------------
1743 procedure Check_Fortran_Logical is
1744 Logical : constant Entity_Id := Etype (Formal);
1745 Var : Entity_Id;
1747 -- Note: this is very incomplete, e.g. it does not handle arrays
1748 -- of logical values. This is really not the right approach at all???)
1750 begin
1751 if Convention (Subp) = Convention_Fortran
1752 and then Root_Type (Etype (Formal)) = Standard_Boolean
1753 and then Ekind (Formal) /= E_In_Parameter
1754 then
1755 Var := Make_Var (Actual);
1756 Append_To (Post_Call,
1757 Make_Assignment_Statement (Loc,
1758 Name => New_Occurrence_Of (Var, Loc),
1759 Expression =>
1760 Unchecked_Convert_To (
1761 Logical,
1762 Make_Op_Ne (Loc,
1763 Left_Opnd => New_Occurrence_Of (Var, Loc),
1764 Right_Opnd =>
1765 Unchecked_Convert_To (
1766 Logical,
1767 New_Occurrence_Of (Standard_False, Loc))))));
1768 end if;
1769 end Check_Fortran_Logical;
1771 -------------------
1772 -- Is_Legal_Copy --
1773 -------------------
1775 function Is_Legal_Copy return Boolean is
1776 begin
1777 -- An attempt to copy a value of such a type can only occur if
1778 -- representation clauses give the actual a misaligned address.
1780 if Is_By_Reference_Type (Etype (Formal)) then
1782 -- The actual may in fact be properly aligned but there is not
1783 -- enough front-end information to determine this. In that case
1784 -- gigi will emit an error if a copy is not legal, or generate
1785 -- the proper code.
1787 return False;
1789 -- For users of Starlet, we assume that the specification of by-
1790 -- reference mechanism is mandatory. This may lead to unaligned
1791 -- objects but at least for DEC legacy code it is known to work.
1792 -- The warning will alert users of this code that a problem may
1793 -- be lurking.
1795 elsif Mechanism (Formal) = By_Reference
1796 and then Is_Valued_Procedure (Scope (Formal))
1797 then
1798 Error_Msg_N
1799 ("by_reference actual may be misaligned??", Actual);
1800 return False;
1802 else
1803 return True;
1804 end if;
1805 end Is_Legal_Copy;
1807 --------------
1808 -- Make_Var --
1809 --------------
1811 function Make_Var (Actual : Node_Id) return Entity_Id is
1812 Var : Entity_Id;
1814 begin
1815 if Is_Entity_Name (Actual) then
1816 return Entity (Actual);
1818 else
1819 Var := Make_Temporary (Loc, 'T', Actual);
1821 N_Node :=
1822 Make_Object_Renaming_Declaration (Loc,
1823 Defining_Identifier => Var,
1824 Subtype_Mark =>
1825 New_Occurrence_Of (Etype (Actual), Loc),
1826 Name => Relocate_Node (Actual));
1828 Insert_Action (N, N_Node);
1829 return Var;
1830 end if;
1831 end Make_Var;
1833 -------------------------
1834 -- Reset_Packed_Prefix --
1835 -------------------------
1837 procedure Reset_Packed_Prefix is
1838 Pfx : Node_Id := Actual;
1839 begin
1840 loop
1841 Set_Analyzed (Pfx, False);
1842 exit when
1843 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1844 Pfx := Prefix (Pfx);
1845 end loop;
1846 end Reset_Packed_Prefix;
1848 -- Start of processing for Expand_Actuals
1850 begin
1851 Post_Call := New_List;
1853 Formal := First_Formal (Subp);
1854 Actual := First_Actual (N);
1855 while Present (Formal) loop
1856 E_Formal := Etype (Formal);
1857 E_Actual := Etype (Actual);
1859 -- Handle formals whose type comes from the limited view
1861 if From_Limited_With (E_Formal)
1862 and then Has_Non_Limited_View (E_Formal)
1863 then
1864 E_Formal := Non_Limited_View (E_Formal);
1865 end if;
1867 if Is_Scalar_Type (E_Formal)
1868 or else Nkind (Actual) = N_Slice
1869 then
1870 Check_Fortran_Logical;
1872 -- RM 6.4.1 (11)
1874 elsif Ekind (Formal) /= E_Out_Parameter then
1876 -- The unusual case of the current instance of a protected type
1877 -- requires special handling. This can only occur in the context
1878 -- of a call within the body of a protected operation.
1880 if Is_Entity_Name (Actual)
1881 and then Ekind (Entity (Actual)) = E_Protected_Type
1882 and then In_Open_Scopes (Entity (Actual))
1883 then
1884 if Scope (Subp) /= Entity (Actual) then
1885 Error_Msg_N
1886 ("operation outside protected type may not "
1887 & "call back its protected operations??", Actual);
1888 end if;
1890 Rewrite (Actual,
1891 Expand_Protected_Object_Reference (N, Entity (Actual)));
1892 end if;
1894 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1895 -- build-in-place function, then a temporary return object needs
1896 -- to be created and access to it must be passed to the function.
1897 -- Currently we limit such functions to those with inherently
1898 -- limited result subtypes, but eventually we plan to expand the
1899 -- functions that are treated as build-in-place to include other
1900 -- composite result types.
1902 if Is_Build_In_Place_Function_Call (Actual) then
1903 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1905 -- Ada 2005 (AI-318-02): Specialization of the previous case for
1906 -- actuals containing build-in-place function calls whose returned
1907 -- object covers interface types.
1909 elsif Present (Unqual_BIP_Iface_Function_Call (Actual)) then
1910 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Actual);
1911 end if;
1913 Apply_Constraint_Check (Actual, E_Formal);
1915 -- Out parameter case. No constraint checks on access type
1916 -- RM 6.4.1 (13)
1918 elsif Is_Access_Type (E_Formal) then
1919 null;
1921 -- RM 6.4.1 (14)
1923 elsif Has_Discriminants (Base_Type (E_Formal))
1924 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1925 then
1926 Apply_Constraint_Check (Actual, E_Formal);
1928 -- RM 6.4.1 (15)
1930 else
1931 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1932 end if;
1934 -- Processing for IN-OUT and OUT parameters
1936 if Ekind (Formal) /= E_In_Parameter then
1938 -- For type conversions of arrays, apply length/range checks
1940 if Is_Array_Type (E_Formal)
1941 and then Nkind (Actual) = N_Type_Conversion
1942 then
1943 if Is_Constrained (E_Formal) then
1944 Apply_Length_Check (Expression (Actual), E_Formal);
1945 else
1946 Apply_Range_Check (Expression (Actual), E_Formal);
1947 end if;
1948 end if;
1950 -- The actual denotes a variable which captures the value of an
1951 -- object for validation purposes. Add a copy-back to reflect any
1952 -- potential changes in value back into the original object.
1954 -- Var : ... := Object;
1955 -- if not Var'Valid then -- validity check
1956 -- Call (Var); -- modify var
1957 -- Object := Var; -- update Object
1959 -- This case is given higher priority because the subsequent check
1960 -- for type conversion may add an extra copy of the variable and
1961 -- prevent proper value propagation back in the original object.
1963 if Is_Validation_Variable_Reference (Actual) then
1964 Add_Validation_Call_By_Copy_Code (Actual);
1966 -- If argument is a type conversion for a type that is passed by
1967 -- copy, then we must pass the parameter by copy.
1969 elsif Nkind (Actual) = N_Type_Conversion
1970 and then
1971 (Is_Numeric_Type (E_Formal)
1972 or else Is_Access_Type (E_Formal)
1973 or else Is_Enumeration_Type (E_Formal)
1974 or else Is_Bit_Packed_Array (Etype (Formal))
1975 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1977 -- Also pass by copy if change of representation
1979 or else not Same_Representation
1980 (Etype (Formal),
1981 Etype (Expression (Actual))))
1982 then
1983 Add_Call_By_Copy_Code;
1985 -- References to components of bit-packed arrays are expanded
1986 -- at this point, rather than at the point of analysis of the
1987 -- actuals, to handle the expansion of the assignment to
1988 -- [in] out parameters.
1990 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1991 Add_Simple_Call_By_Copy_Code;
1993 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1994 -- because the back-end cannot cope with such objects. In other
1995 -- cases where alignment forces a copy, the back-end generates
1996 -- it properly. It should not be generated unconditionally in the
1997 -- front-end because it does not know precisely the alignment
1998 -- requirements of the target, and makes too conservative an
1999 -- estimate, leading to superfluous copies or spurious errors
2000 -- on by-reference parameters.
2002 elsif Nkind (Actual) = N_Selected_Component
2003 and then
2004 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
2005 and then not Represented_As_Scalar (Etype (Formal))
2006 then
2007 Add_Simple_Call_By_Copy_Code;
2009 -- References to slices of bit-packed arrays are expanded
2011 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2012 Add_Call_By_Copy_Code;
2014 -- References to possibly unaligned slices of arrays are expanded
2016 elsif Is_Possibly_Unaligned_Slice (Actual) then
2017 Add_Call_By_Copy_Code;
2019 -- Deal with access types where the actual subtype and the
2020 -- formal subtype are not the same, requiring a check.
2022 -- It is necessary to exclude tagged types because of "downward
2023 -- conversion" errors.
2025 elsif Is_Access_Type (E_Formal)
2026 and then not Same_Type (E_Formal, E_Actual)
2027 and then not Is_Tagged_Type (Designated_Type (E_Formal))
2028 then
2029 Add_Call_By_Copy_Code;
2031 -- If the actual is not a scalar and is marked for volatile
2032 -- treatment, whereas the formal is not volatile, then pass
2033 -- by copy unless it is a by-reference type.
2035 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
2036 -- because this is the enforcement of a language rule that applies
2037 -- only to "real" volatile variables, not e.g. to the address
2038 -- clause overlay case.
2040 elsif Is_Entity_Name (Actual)
2041 and then Is_Volatile (Entity (Actual))
2042 and then not Is_By_Reference_Type (E_Actual)
2043 and then not Is_Scalar_Type (Etype (Entity (Actual)))
2044 and then not Is_Volatile (E_Formal)
2045 then
2046 Add_Call_By_Copy_Code;
2048 elsif Nkind (Actual) = N_Indexed_Component
2049 and then Is_Entity_Name (Prefix (Actual))
2050 and then Has_Volatile_Components (Entity (Prefix (Actual)))
2051 then
2052 Add_Call_By_Copy_Code;
2054 -- Add call-by-copy code for the case of scalar out parameters
2055 -- when it is not known at compile time that the subtype of the
2056 -- formal is a subrange of the subtype of the actual (or vice
2057 -- versa for in out parameters), in order to get range checks
2058 -- on such actuals. (Maybe this case should be handled earlier
2059 -- in the if statement???)
2061 elsif Is_Scalar_Type (E_Formal)
2062 and then
2063 (not In_Subrange_Of (E_Formal, E_Actual)
2064 or else
2065 (Ekind (Formal) = E_In_Out_Parameter
2066 and then not In_Subrange_Of (E_Actual, E_Formal)))
2067 then
2068 -- Perhaps the setting back to False should be done within
2069 -- Add_Call_By_Copy_Code, since it could get set on other
2070 -- cases occurring above???
2072 if Do_Range_Check (Actual) then
2073 Set_Do_Range_Check (Actual, False);
2074 end if;
2076 Add_Call_By_Copy_Code;
2077 end if;
2079 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
2080 -- by-reference parameters on exit from the call. If the actual
2081 -- is a derived type and the operation is inherited, the body
2082 -- of the operation will not contain a call to the predicate
2083 -- function, so it must be done explicitly after the call. Ditto
2084 -- if the actual is an entity of a predicated subtype.
2086 -- The rule refers to by-reference types, but a check is needed
2087 -- for by-copy types as well. That check is subsumed by the rule
2088 -- for subtype conversion on assignment, but we can generate the
2089 -- required check now.
2091 -- Note also that Subp may be either a subprogram entity for
2092 -- direct calls, or a type entity for indirect calls, which must
2093 -- be handled separately because the name does not denote an
2094 -- overloadable entity.
2096 By_Ref_Predicate_Check : declare
2097 Aund : constant Entity_Id := Underlying_Type (E_Actual);
2098 Atyp : Entity_Id;
2100 function Is_Public_Subp return Boolean;
2101 -- Check whether the subprogram being called is a visible
2102 -- operation of the type of the actual. Used to determine
2103 -- whether an invariant check must be generated on the
2104 -- caller side.
2106 ---------------------
2107 -- Is_Public_Subp --
2108 ---------------------
2110 function Is_Public_Subp return Boolean is
2111 Pack : constant Entity_Id := Scope (Subp);
2112 Subp_Decl : Node_Id;
2114 begin
2115 if not Is_Subprogram (Subp) then
2116 return False;
2118 -- The operation may be inherited, or a primitive of the
2119 -- root type.
2121 elsif
2122 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
2123 N_Full_Type_Declaration)
2124 then
2125 Subp_Decl := Parent (Subp);
2127 else
2128 Subp_Decl := Unit_Declaration_Node (Subp);
2129 end if;
2131 return Ekind (Pack) = E_Package
2132 and then
2133 List_Containing (Subp_Decl) =
2134 Visible_Declarations
2135 (Specification (Unit_Declaration_Node (Pack)));
2136 end Is_Public_Subp;
2138 -- Start of processing for By_Ref_Predicate_Check
2140 begin
2141 if No (Aund) then
2142 Atyp := E_Actual;
2143 else
2144 Atyp := Aund;
2145 end if;
2147 if Has_Predicates (Atyp)
2148 and then Present (Predicate_Function (Atyp))
2150 -- Skip predicate checks for special cases
2152 and then Predicate_Tests_On_Arguments (Subp)
2153 then
2154 Append_To (Post_Call,
2155 Make_Predicate_Check (Atyp, Actual));
2156 end if;
2158 -- We generated caller-side invariant checks in two cases:
2160 -- a) when calling an inherited operation, where there is an
2161 -- implicit view conversion of the actual to the parent type.
2163 -- b) When the conversion is explicit
2165 -- We treat these cases separately because the required
2166 -- conversion for a) is added later when expanding the call.
2168 if Has_Invariants (Etype (Actual))
2169 and then
2170 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
2171 then
2172 if Comes_From_Source (N) and then Is_Public_Subp then
2173 Append_To (Post_Call, Make_Invariant_Call (Actual));
2174 end if;
2176 elsif Nkind (Actual) = N_Type_Conversion
2177 and then Has_Invariants (Etype (Expression (Actual)))
2178 then
2179 if Comes_From_Source (N) and then Is_Public_Subp then
2180 Append_To (Post_Call,
2181 Make_Invariant_Call (Expression (Actual)));
2182 end if;
2183 end if;
2184 end By_Ref_Predicate_Check;
2186 -- Processing for IN parameters
2188 else
2189 -- For IN parameters in the bit-packed array case, we expand an
2190 -- indexed component (the circuit in Exp_Ch4 deliberately left
2191 -- indexed components appearing as actuals untouched, so that
2192 -- the special processing above for the OUT and IN OUT cases
2193 -- could be performed. We could make the test in Exp_Ch4 more
2194 -- complex and have it detect the parameter mode, but it is
2195 -- easier simply to handle all cases here.)
2197 if Nkind (Actual) = N_Indexed_Component
2198 and then Is_Bit_Packed_Array (Etype (Prefix (Actual)))
2199 then
2200 Reset_Packed_Prefix;
2201 Expand_Packed_Element_Reference (Actual);
2203 -- If we have a reference to a bit-packed array, we copy it, since
2204 -- the actual must be byte aligned.
2206 -- Is this really necessary in all cases???
2208 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
2209 Add_Simple_Call_By_Copy_Code;
2211 -- If a non-scalar actual is possibly unaligned, we need a copy
2213 elsif Is_Possibly_Unaligned_Object (Actual)
2214 and then not Represented_As_Scalar (Etype (Formal))
2215 then
2216 Add_Simple_Call_By_Copy_Code;
2218 -- Similarly, we have to expand slices of packed arrays here
2219 -- because the result must be byte aligned.
2221 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
2222 Add_Call_By_Copy_Code;
2224 -- Only processing remaining is to pass by copy if this is a
2225 -- reference to a possibly unaligned slice, since the caller
2226 -- expects an appropriately aligned argument.
2228 elsif Is_Possibly_Unaligned_Slice (Actual) then
2229 Add_Call_By_Copy_Code;
2231 -- An unusual case: a current instance of an enclosing task can be
2232 -- an actual, and must be replaced by a reference to self.
2234 elsif Is_Entity_Name (Actual)
2235 and then Is_Task_Type (Entity (Actual))
2236 then
2237 if In_Open_Scopes (Entity (Actual)) then
2238 Rewrite (Actual,
2239 (Make_Function_Call (Loc,
2240 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
2241 Analyze (Actual);
2243 -- A task type cannot otherwise appear as an actual
2245 else
2246 raise Program_Error;
2247 end if;
2248 end if;
2249 end if;
2251 Next_Formal (Formal);
2252 Next_Actual (Actual);
2253 end loop;
2254 end Expand_Actuals;
2256 -----------------
2257 -- Expand_Call --
2258 -----------------
2260 procedure Expand_Call (N : Node_Id) is
2261 Post_Call : List_Id;
2263 begin
2264 pragma Assert (Nkind_In (N, N_Entry_Call_Statement,
2265 N_Function_Call,
2266 N_Procedure_Call_Statement));
2268 Expand_Call_Helper (N, Post_Call);
2269 Insert_Post_Call_Actions (N, Post_Call);
2270 end Expand_Call;
2272 ------------------------
2273 -- Expand_Call_Helper --
2274 ------------------------
2276 -- This procedure handles expansion of function calls and procedure call
2277 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2278 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2280 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2281 -- Provide values of actuals for all formals in Extra_Formals list
2282 -- Replace "call" to enumeration literal function by literal itself
2283 -- Rewrite call to predefined operator as operator
2284 -- Replace actuals to in-out parameters that are numeric conversions,
2285 -- with explicit assignment to temporaries before and after the call.
2287 -- Note that the list of actuals has been filled with default expressions
2288 -- during semantic analysis of the call. Only the extra actuals required
2289 -- for the 'Constrained attribute and for accessibility checks are added
2290 -- at this point.
2292 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2293 Loc : constant Source_Ptr := Sloc (N);
2294 Call_Node : Node_Id := N;
2295 Extra_Actuals : List_Id := No_List;
2296 Prev : Node_Id := Empty;
2298 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2299 -- Adds one entry to the end of the actual parameter list. Used for
2300 -- default parameters and for extra actuals (for Extra_Formals). The
2301 -- argument is an N_Parameter_Association node.
2303 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2304 -- Adds an extra actual to the list of extra actuals. Expr is the
2305 -- expression for the value of the actual, EF is the entity for the
2306 -- extra formal.
2308 procedure Add_View_Conversion_Invariants
2309 (Formal : Entity_Id;
2310 Actual : Node_Id);
2311 -- Adds invariant checks for every intermediate type between the range
2312 -- of a view converted argument to its ancestor (from parent to child).
2314 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2315 -- Within an instance, a type derived from an untagged formal derived
2316 -- type inherits from the original parent, not from the actual. The
2317 -- current derivation mechanism has the derived type inherit from the
2318 -- actual, which is only correct outside of the instance. If the
2319 -- subprogram is inherited, we test for this particular case through a
2320 -- convoluted tree traversal before setting the proper subprogram to be
2321 -- called.
2323 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2324 -- Return true if E comes from an instance that is not yet frozen
2326 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2327 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2329 function New_Value (From : Node_Id) return Node_Id;
2330 -- From is the original Expression. New_Value is equivalent to a call
2331 -- to Duplicate_Subexpr with an explicit dereference when From is an
2332 -- access parameter.
2334 --------------------------
2335 -- Add_Actual_Parameter --
2336 --------------------------
2338 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2339 Actual_Expr : constant Node_Id :=
2340 Explicit_Actual_Parameter (Insert_Param);
2342 begin
2343 -- Case of insertion is first named actual
2345 if No (Prev) or else
2346 Nkind (Parent (Prev)) /= N_Parameter_Association
2347 then
2348 Set_Next_Named_Actual
2349 (Insert_Param, First_Named_Actual (Call_Node));
2350 Set_First_Named_Actual (Call_Node, Actual_Expr);
2352 if No (Prev) then
2353 if No (Parameter_Associations (Call_Node)) then
2354 Set_Parameter_Associations (Call_Node, New_List);
2355 end if;
2357 Append (Insert_Param, Parameter_Associations (Call_Node));
2359 else
2360 Insert_After (Prev, Insert_Param);
2361 end if;
2363 -- Case of insertion is not first named actual
2365 else
2366 Set_Next_Named_Actual
2367 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2368 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2369 Append (Insert_Param, Parameter_Associations (Call_Node));
2370 end if;
2372 Prev := Actual_Expr;
2373 end Add_Actual_Parameter;
2375 ----------------------
2376 -- Add_Extra_Actual --
2377 ----------------------
2379 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2380 Loc : constant Source_Ptr := Sloc (Expr);
2382 begin
2383 if Extra_Actuals = No_List then
2384 Extra_Actuals := New_List;
2385 Set_Parent (Extra_Actuals, Call_Node);
2386 end if;
2388 Append_To (Extra_Actuals,
2389 Make_Parameter_Association (Loc,
2390 Selector_Name => New_Occurrence_Of (EF, Loc),
2391 Explicit_Actual_Parameter => Expr));
2393 Analyze_And_Resolve (Expr, Etype (EF));
2395 if Nkind (Call_Node) = N_Function_Call then
2396 Set_Is_Accessibility_Actual (Parent (Expr));
2397 end if;
2398 end Add_Extra_Actual;
2400 ------------------------------------
2401 -- Add_View_Conversion_Invariants --
2402 ------------------------------------
2404 procedure Add_View_Conversion_Invariants
2405 (Formal : Entity_Id;
2406 Actual : Node_Id)
2408 Arg : Entity_Id;
2409 Curr_Typ : Entity_Id;
2410 Inv_Checks : List_Id;
2411 Par_Typ : Entity_Id;
2413 begin
2414 Inv_Checks := No_List;
2416 -- Extract the argument from a potentially nested set of view
2417 -- conversions.
2419 Arg := Actual;
2420 while Nkind (Arg) = N_Type_Conversion loop
2421 Arg := Expression (Arg);
2422 end loop;
2424 -- Move up the derivation chain starting with the type of the formal
2425 -- parameter down to the type of the actual object.
2427 Curr_Typ := Empty;
2428 Par_Typ := Etype (Arg);
2429 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2430 Curr_Typ := Par_Typ;
2432 if Has_Invariants (Curr_Typ)
2433 and then Present (Invariant_Procedure (Curr_Typ))
2434 then
2435 -- Verify the invariate of the current type. Generate:
2437 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2439 Prepend_New_To (Inv_Checks,
2440 Make_Procedure_Call_Statement (Loc,
2441 Name =>
2442 New_Occurrence_Of
2443 (Invariant_Procedure (Curr_Typ), Loc),
2444 Parameter_Associations => New_List (
2445 Make_Type_Conversion (Loc,
2446 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2447 Expression => New_Copy_Tree (Arg)))));
2448 end if;
2450 Par_Typ := Base_Type (Etype (Curr_Typ));
2451 end loop;
2453 if not Is_Empty_List (Inv_Checks) then
2454 Insert_Actions_After (N, Inv_Checks);
2455 end if;
2456 end Add_View_Conversion_Invariants;
2458 ---------------------------
2459 -- Inherited_From_Formal --
2460 ---------------------------
2462 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2463 Par : Entity_Id;
2464 Gen_Par : Entity_Id;
2465 Gen_Prim : Elist_Id;
2466 Elmt : Elmt_Id;
2467 Indic : Node_Id;
2469 begin
2470 -- If the operation is inherited, it is attached to the corresponding
2471 -- type derivation. If the parent in the derivation is a generic
2472 -- actual, it is a subtype of the actual, and we have to recover the
2473 -- original derived type declaration to find the proper parent.
2475 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2476 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2477 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2478 N_Derived_Type_Definition
2479 or else not In_Instance
2480 then
2481 return Empty;
2483 else
2484 Indic :=
2485 Subtype_Indication
2486 (Type_Definition (Original_Node (Parent (S))));
2488 if Nkind (Indic) = N_Subtype_Indication then
2489 Par := Entity (Subtype_Mark (Indic));
2490 else
2491 Par := Entity (Indic);
2492 end if;
2493 end if;
2495 if not Is_Generic_Actual_Type (Par)
2496 or else Is_Tagged_Type (Par)
2497 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2498 or else not In_Open_Scopes (Scope (Par))
2499 then
2500 return Empty;
2501 else
2502 Gen_Par := Generic_Parent_Type (Parent (Par));
2503 end if;
2505 -- If the actual has no generic parent type, the formal is not
2506 -- a formal derived type, so nothing to inherit.
2508 if No (Gen_Par) then
2509 return Empty;
2510 end if;
2512 -- If the generic parent type is still the generic type, this is a
2513 -- private formal, not a derived formal, and there are no operations
2514 -- inherited from the formal.
2516 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2517 return Empty;
2518 end if;
2520 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2522 Elmt := First_Elmt (Gen_Prim);
2523 while Present (Elmt) loop
2524 if Chars (Node (Elmt)) = Chars (S) then
2525 declare
2526 F1 : Entity_Id;
2527 F2 : Entity_Id;
2529 begin
2530 F1 := First_Formal (S);
2531 F2 := First_Formal (Node (Elmt));
2532 while Present (F1)
2533 and then Present (F2)
2534 loop
2535 if Etype (F1) = Etype (F2)
2536 or else Etype (F2) = Gen_Par
2537 then
2538 Next_Formal (F1);
2539 Next_Formal (F2);
2540 else
2541 Next_Elmt (Elmt);
2542 exit; -- not the right subprogram
2543 end if;
2545 return Node (Elmt);
2546 end loop;
2547 end;
2549 else
2550 Next_Elmt (Elmt);
2551 end if;
2552 end loop;
2554 raise Program_Error;
2555 end Inherited_From_Formal;
2557 --------------------------
2558 -- In_Unfrozen_Instance --
2559 --------------------------
2561 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2562 S : Entity_Id;
2564 begin
2565 S := E;
2566 while Present (S) and then S /= Standard_Standard loop
2567 if Is_Generic_Instance (S)
2568 and then Present (Freeze_Node (S))
2569 and then not Analyzed (Freeze_Node (S))
2570 then
2571 return True;
2572 end if;
2574 S := Scope (S);
2575 end loop;
2577 return False;
2578 end In_Unfrozen_Instance;
2580 -------------------------
2581 -- Is_Direct_Deep_Call --
2582 -------------------------
2584 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2585 begin
2586 if Is_TSS (Subp, TSS_Deep_Adjust)
2587 or else Is_TSS (Subp, TSS_Deep_Finalize)
2588 or else Is_TSS (Subp, TSS_Deep_Initialize)
2589 then
2590 declare
2591 Actual : Node_Id;
2592 Formal : Node_Id;
2594 begin
2595 Actual := First (Parameter_Associations (N));
2596 Formal := First_Formal (Subp);
2597 while Present (Actual)
2598 and then Present (Formal)
2599 loop
2600 if Nkind (Actual) = N_Identifier
2601 and then Is_Controlling_Actual (Actual)
2602 and then Etype (Actual) = Etype (Formal)
2603 then
2604 return True;
2605 end if;
2607 Next (Actual);
2608 Next_Formal (Formal);
2609 end loop;
2610 end;
2611 end if;
2613 return False;
2614 end Is_Direct_Deep_Call;
2616 ---------------
2617 -- New_Value --
2618 ---------------
2620 function New_Value (From : Node_Id) return Node_Id is
2621 Res : constant Node_Id := Duplicate_Subexpr (From);
2622 begin
2623 if Is_Access_Type (Etype (From)) then
2624 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2625 else
2626 return Res;
2627 end if;
2628 end New_Value;
2630 -- Local variables
2632 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2633 Actual : Node_Id;
2634 Formal : Entity_Id;
2635 Orig_Subp : Entity_Id := Empty;
2636 Param_Count : Natural := 0;
2637 Parent_Formal : Entity_Id;
2638 Parent_Subp : Entity_Id;
2639 Pref_Entity : Entity_Id;
2640 Scop : Entity_Id;
2641 Subp : Entity_Id;
2643 Prev_Orig : Node_Id;
2644 -- Original node for an actual, which may have been rewritten. If the
2645 -- actual is a function call that has been transformed from a selected
2646 -- component, the original node is unanalyzed. Otherwise, it carries
2647 -- semantic information used to generate additional actuals.
2649 CW_Interface_Formals_Present : Boolean := False;
2651 -- Start of processing for Expand_Call_Helper
2653 begin
2654 Post_Call := New_List;
2656 -- Expand the function or procedure call if the first actual has a
2657 -- declared dimension aspect, and the subprogram is declared in one
2658 -- of the dimension I/O packages.
2660 if Ada_Version >= Ada_2012
2661 and then
2662 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2663 and then Present (Parameter_Associations (Call_Node))
2664 then
2665 Expand_Put_Call_With_Symbol (Call_Node);
2666 end if;
2668 -- Ignore if previous error
2670 if Nkind (Call_Node) in N_Has_Etype
2671 and then Etype (Call_Node) = Any_Type
2672 then
2673 return;
2674 end if;
2676 -- Call using access to subprogram with explicit dereference
2678 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2679 Subp := Etype (Name (Call_Node));
2680 Parent_Subp := Empty;
2682 -- Case of call to simple entry, where the Name is a selected component
2683 -- whose prefix is the task, and whose selector name is the entry name
2685 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2686 Subp := Entity (Selector_Name (Name (Call_Node)));
2687 Parent_Subp := Empty;
2689 -- Case of call to member of entry family, where Name is an indexed
2690 -- component, with the prefix being a selected component giving the
2691 -- task and entry family name, and the index being the entry index.
2693 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2694 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2695 Parent_Subp := Empty;
2697 -- Normal case
2699 else
2700 Subp := Entity (Name (Call_Node));
2701 Parent_Subp := Alias (Subp);
2703 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2704 -- if we can tell that the first parameter cannot possibly be null.
2705 -- This improves efficiency by avoiding a run-time test.
2707 -- We do not do this if Raise_Exception_Always does not exist, which
2708 -- can happen in configurable run time profiles which provide only a
2709 -- Raise_Exception.
2711 if Is_RTE (Subp, RE_Raise_Exception)
2712 and then RTE_Available (RE_Raise_Exception_Always)
2713 then
2714 declare
2715 FA : constant Node_Id :=
2716 Original_Node (First_Actual (Call_Node));
2718 begin
2719 -- The case we catch is where the first argument is obtained
2720 -- using the Identity attribute (which must always be
2721 -- non-null).
2723 if Nkind (FA) = N_Attribute_Reference
2724 and then Attribute_Name (FA) = Name_Identity
2725 then
2726 Subp := RTE (RE_Raise_Exception_Always);
2727 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2728 end if;
2729 end;
2730 end if;
2732 if Ekind (Subp) = E_Entry then
2733 Parent_Subp := Empty;
2734 end if;
2735 end if;
2737 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2738 -- alternative in an asynchronous select or as an entry call in
2739 -- a conditional or timed select. Check whether the procedure call
2740 -- is a renaming of an entry and rewrite it as an entry call.
2742 if Ada_Version >= Ada_2005
2743 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2744 and then
2745 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2746 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2747 or else
2748 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2749 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2750 then
2751 declare
2752 Ren_Decl : Node_Id;
2753 Ren_Root : Entity_Id := Subp;
2755 begin
2756 -- This may be a chain of renamings, find the root
2758 if Present (Alias (Ren_Root)) then
2759 Ren_Root := Alias (Ren_Root);
2760 end if;
2762 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2763 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2765 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2766 Rewrite (Call_Node,
2767 Make_Entry_Call_Statement (Loc,
2768 Name =>
2769 New_Copy_Tree (Name (Ren_Decl)),
2770 Parameter_Associations =>
2771 New_Copy_List_Tree
2772 (Parameter_Associations (Call_Node))));
2774 return;
2775 end if;
2776 end if;
2777 end;
2778 end if;
2780 if Modify_Tree_For_C
2781 and then Nkind (Call_Node) = N_Function_Call
2782 and then Is_Entity_Name (Name (Call_Node))
2783 then
2784 declare
2785 Func_Id : constant Entity_Id :=
2786 Ultimate_Alias (Entity (Name (Call_Node)));
2787 begin
2788 -- When generating C code, transform a function call that returns
2789 -- a constrained array type into procedure form.
2791 if Rewritten_For_C (Func_Id) then
2793 -- For internally generated calls ensure that they reference
2794 -- the entity of the spec of the called function (needed since
2795 -- the expander may generate calls using the entity of their
2796 -- body). See for example Expand_Boolean_Operator().
2798 if not (Comes_From_Source (Call_Node))
2799 and then Nkind (Unit_Declaration_Node (Func_Id)) =
2800 N_Subprogram_Body
2801 then
2802 Set_Entity (Name (Call_Node),
2803 Corresponding_Function
2804 (Corresponding_Procedure (Func_Id)));
2805 end if;
2807 Rewrite_Function_Call_For_C (Call_Node);
2808 return;
2810 -- Also introduce a temporary for functions that return a record
2811 -- called within another procedure or function call, since records
2812 -- are passed by pointer in the generated C code, and we cannot
2813 -- take a pointer from a subprogram call.
2815 elsif Nkind (Parent (Call_Node)) in N_Subprogram_Call
2816 and then Is_Record_Type (Etype (Func_Id))
2817 then
2818 declare
2819 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
2820 Decl : Node_Id;
2822 begin
2823 -- Generate:
2824 -- Temp : ... := Func_Call (...);
2826 Decl :=
2827 Make_Object_Declaration (Loc,
2828 Defining_Identifier => Temp_Id,
2829 Object_Definition =>
2830 New_Occurrence_Of (Etype (Func_Id), Loc),
2831 Expression =>
2832 Make_Function_Call (Loc,
2833 Name =>
2834 New_Occurrence_Of (Func_Id, Loc),
2835 Parameter_Associations =>
2836 Parameter_Associations (Call_Node)));
2838 Insert_Action (Parent (Call_Node), Decl);
2839 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
2840 return;
2841 end;
2842 end if;
2843 end;
2844 end if;
2846 -- First step, compute extra actuals, corresponding to any Extra_Formals
2847 -- present. Note that we do not access Extra_Formals directly, instead
2848 -- we simply note the presence of the extra formals as we process the
2849 -- regular formals collecting corresponding actuals in Extra_Actuals.
2851 -- We also generate any required range checks for actuals for in formals
2852 -- as we go through the loop, since this is a convenient place to do it.
2853 -- (Though it seems that this would be better done in Expand_Actuals???)
2855 -- Special case: Thunks must not compute the extra actuals; they must
2856 -- just propagate to the target primitive their extra actuals.
2858 if Is_Thunk (Current_Scope)
2859 and then Thunk_Entity (Current_Scope) = Subp
2860 and then Present (Extra_Formals (Subp))
2861 then
2862 pragma Assert (Present (Extra_Formals (Current_Scope)));
2864 declare
2865 Target_Formal : Entity_Id;
2866 Thunk_Formal : Entity_Id;
2868 begin
2869 Target_Formal := Extra_Formals (Subp);
2870 Thunk_Formal := Extra_Formals (Current_Scope);
2871 while Present (Target_Formal) loop
2872 Add_Extra_Actual
2873 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
2874 EF => Thunk_Formal);
2876 Target_Formal := Extra_Formal (Target_Formal);
2877 Thunk_Formal := Extra_Formal (Thunk_Formal);
2878 end loop;
2880 while Is_Non_Empty_List (Extra_Actuals) loop
2881 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2882 end loop;
2884 Expand_Actuals (Call_Node, Subp, Post_Call);
2885 pragma Assert (Is_Empty_List (Post_Call));
2886 return;
2887 end;
2888 end if;
2890 Formal := First_Formal (Subp);
2891 Actual := First_Actual (Call_Node);
2892 Param_Count := 1;
2893 while Present (Formal) loop
2895 -- Generate range check if required
2897 if Do_Range_Check (Actual)
2898 and then Ekind (Formal) = E_In_Parameter
2899 then
2900 Generate_Range_Check
2901 (Actual, Etype (Formal), CE_Range_Check_Failed);
2902 end if;
2904 -- Prepare to examine current entry
2906 Prev := Actual;
2907 Prev_Orig := Original_Node (Prev);
2909 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2910 -- to expand it in a further round.
2912 CW_Interface_Formals_Present :=
2913 CW_Interface_Formals_Present
2914 or else
2915 (Is_Class_Wide_Type (Etype (Formal))
2916 and then Is_Interface (Etype (Etype (Formal))))
2917 or else
2918 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2919 and then Is_Class_Wide_Type (Directly_Designated_Type
2920 (Etype (Etype (Formal))))
2921 and then Is_Interface (Directly_Designated_Type
2922 (Etype (Etype (Formal)))));
2924 -- Create possible extra actual for constrained case. Usually, the
2925 -- extra actual is of the form actual'constrained, but since this
2926 -- attribute is only available for unconstrained records, TRUE is
2927 -- expanded if the type of the formal happens to be constrained (for
2928 -- instance when this procedure is inherited from an unconstrained
2929 -- record to a constrained one) or if the actual has no discriminant
2930 -- (its type is constrained). An exception to this is the case of a
2931 -- private type without discriminants. In this case we pass FALSE
2932 -- because the object has underlying discriminants with defaults.
2934 if Present (Extra_Constrained (Formal)) then
2935 if Ekind (Etype (Prev)) in Private_Kind
2936 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2937 then
2938 Add_Extra_Actual
2939 (Expr => New_Occurrence_Of (Standard_False, Loc),
2940 EF => Extra_Constrained (Formal));
2942 elsif Is_Constrained (Etype (Formal))
2943 or else not Has_Discriminants (Etype (Prev))
2944 then
2945 Add_Extra_Actual
2946 (Expr => New_Occurrence_Of (Standard_True, Loc),
2947 EF => Extra_Constrained (Formal));
2949 -- Do not produce extra actuals for Unchecked_Union parameters.
2950 -- Jump directly to the end of the loop.
2952 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2953 goto Skip_Extra_Actual_Generation;
2955 else
2956 -- If the actual is a type conversion, then the constrained
2957 -- test applies to the actual, not the target type.
2959 declare
2960 Act_Prev : Node_Id;
2962 begin
2963 -- Test for unchecked conversions as well, which can occur
2964 -- as out parameter actuals on calls to stream procedures.
2966 Act_Prev := Prev;
2967 while Nkind_In (Act_Prev, N_Type_Conversion,
2968 N_Unchecked_Type_Conversion)
2969 loop
2970 Act_Prev := Expression (Act_Prev);
2971 end loop;
2973 -- If the expression is a conversion of a dereference, this
2974 -- is internally generated code that manipulates addresses,
2975 -- e.g. when building interface tables. No check should
2976 -- occur in this case, and the discriminated object is not
2977 -- directly a hand.
2979 if not Comes_From_Source (Actual)
2980 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2981 and then Nkind (Act_Prev) = N_Explicit_Dereference
2982 then
2983 Add_Extra_Actual
2984 (Expr => New_Occurrence_Of (Standard_False, Loc),
2985 EF => Extra_Constrained (Formal));
2987 else
2988 Add_Extra_Actual
2989 (Expr =>
2990 Make_Attribute_Reference (Sloc (Prev),
2991 Prefix =>
2992 Duplicate_Subexpr_No_Checks
2993 (Act_Prev, Name_Req => True),
2994 Attribute_Name => Name_Constrained),
2995 EF => Extra_Constrained (Formal));
2996 end if;
2997 end;
2998 end if;
2999 end if;
3001 -- Create possible extra actual for accessibility level
3003 if Present (Extra_Accessibility (Formal)) then
3005 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
3006 -- attribute, then the original actual may be an aliased object
3007 -- occurring as the prefix in a call using "Object.Operation"
3008 -- notation. In that case we must pass the level of the object,
3009 -- so Prev_Orig is reset to Prev and the attribute will be
3010 -- processed by the code for Access attributes further below.
3012 if Prev_Orig /= Prev
3013 and then Nkind (Prev) = N_Attribute_Reference
3014 and then Get_Attribute_Id (Attribute_Name (Prev)) =
3015 Attribute_Access
3016 and then Is_Aliased_View (Prev_Orig)
3017 then
3018 Prev_Orig := Prev;
3020 -- A class-wide precondition generates a test in which formals of
3021 -- the subprogram are replaced by actuals that came from source.
3022 -- In that case as well, the accessiblity comes from the actual.
3023 -- This is the one case in which there are references to formals
3024 -- outside of their subprogram.
3026 elsif Prev_Orig /= Prev
3027 and then Is_Entity_Name (Prev_Orig)
3028 and then Present (Entity (Prev_Orig))
3029 and then Is_Formal (Entity (Prev_Orig))
3030 and then not In_Open_Scopes (Scope (Entity (Prev_Orig)))
3031 then
3032 Prev_Orig := Prev;
3034 -- If the actual is a formal of an enclosing subprogram it is
3035 -- the right entity, even if it is a rewriting. This happens
3036 -- when the call is within an inherited condition or predicate.
3038 elsif Is_Entity_Name (Actual)
3039 and then Is_Formal (Entity (Actual))
3040 and then In_Open_Scopes (Scope (Entity (Actual)))
3041 then
3042 Prev_Orig := Prev;
3044 elsif Nkind (Prev_Orig) = N_Type_Conversion then
3045 Prev_Orig := Expression (Prev_Orig);
3046 end if;
3048 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3049 -- accessibility levels.
3051 if Is_Thunk (Current_Scope) then
3052 declare
3053 Parm_Ent : Entity_Id;
3055 begin
3056 if Is_Controlling_Actual (Actual) then
3058 -- Find the corresponding actual of the thunk
3060 Parm_Ent := First_Entity (Current_Scope);
3061 for J in 2 .. Param_Count loop
3062 Next_Entity (Parm_Ent);
3063 end loop;
3065 -- Handle unchecked conversion of access types generated
3066 -- in thunks (cf. Expand_Interface_Thunk).
3068 elsif Is_Access_Type (Etype (Actual))
3069 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3070 then
3071 Parm_Ent := Entity (Expression (Actual));
3073 else pragma Assert (Is_Entity_Name (Actual));
3074 Parm_Ent := Entity (Actual);
3075 end if;
3077 Add_Extra_Actual
3078 (Expr =>
3079 New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
3080 EF => Extra_Accessibility (Formal));
3081 end;
3083 elsif Is_Entity_Name (Prev_Orig) then
3085 -- When passing an access parameter, or a renaming of an access
3086 -- parameter, as the actual to another access parameter we need
3087 -- to pass along the actual's own access level parameter. This
3088 -- is done if we are within the scope of the formal access
3089 -- parameter (if this is an inlined body the extra formal is
3090 -- irrelevant).
3092 if (Is_Formal (Entity (Prev_Orig))
3093 or else
3094 (Present (Renamed_Object (Entity (Prev_Orig)))
3095 and then
3096 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3097 and then
3098 Is_Formal
3099 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3100 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3101 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3102 then
3103 declare
3104 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3106 begin
3107 pragma Assert (Present (Parm_Ent));
3109 if Present (Extra_Accessibility (Parm_Ent)) then
3110 Add_Extra_Actual
3111 (Expr =>
3112 New_Occurrence_Of
3113 (Extra_Accessibility (Parm_Ent), Loc),
3114 EF => Extra_Accessibility (Formal));
3116 -- If the actual access parameter does not have an
3117 -- associated extra formal providing its scope level,
3118 -- then treat the actual as having library-level
3119 -- accessibility.
3121 else
3122 Add_Extra_Actual
3123 (Expr =>
3124 Make_Integer_Literal (Loc,
3125 Intval => Scope_Depth (Standard_Standard)),
3126 EF => Extra_Accessibility (Formal));
3127 end if;
3128 end;
3130 -- The actual is a normal access value, so just pass the level
3131 -- of the actual's access type.
3133 else
3134 Add_Extra_Actual
3135 (Expr => Dynamic_Accessibility_Level (Prev_Orig),
3136 EF => Extra_Accessibility (Formal));
3137 end if;
3139 -- If the actual is an access discriminant, then pass the level
3140 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3142 elsif Nkind (Prev_Orig) = N_Selected_Component
3143 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3144 E_Discriminant
3145 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3146 E_Anonymous_Access_Type
3147 then
3148 Add_Extra_Actual
3149 (Expr =>
3150 Make_Integer_Literal (Loc,
3151 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3152 EF => Extra_Accessibility (Formal));
3154 -- All other cases
3156 else
3157 case Nkind (Prev_Orig) is
3158 when N_Attribute_Reference =>
3159 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3161 -- For X'Access, pass on the level of the prefix X
3163 when Attribute_Access =>
3165 -- Accessibility level of S'Access is that of A
3167 Prev_Orig := Prefix (Prev_Orig);
3169 -- If the expression is a view conversion, the
3170 -- accessibility level is that of the expression.
3172 if Nkind (Original_Node (Prev_Orig)) =
3173 N_Type_Conversion
3174 and then
3175 Nkind (Expression (Original_Node (Prev_Orig))) =
3176 N_Explicit_Dereference
3177 then
3178 Prev_Orig :=
3179 Expression (Original_Node (Prev_Orig));
3180 end if;
3182 -- If this is an Access attribute applied to the
3183 -- the current instance object passed to a type
3184 -- initialization procedure, then use the level
3185 -- of the type itself. This is not really correct,
3186 -- as there should be an extra level parameter
3187 -- passed in with _init formals (only in the case
3188 -- where the type is immutably limited), but we
3189 -- don't have an easy way currently to create such
3190 -- an extra formal (init procs aren't ever frozen).
3191 -- For now we just use the level of the type,
3192 -- which may be too shallow, but that works better
3193 -- than passing Object_Access_Level of the type,
3194 -- which can be one level too deep in some cases.
3195 -- ???
3197 -- A further case that requires special handling
3198 -- is the common idiom E.all'access. If E is a
3199 -- formal of the enclosing subprogram, the
3200 -- accessibility of the expression is that of E.
3202 if Is_Entity_Name (Prev_Orig) then
3203 Pref_Entity := Entity (Prev_Orig);
3205 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3206 and then Is_Entity_Name (Prefix (Prev_Orig))
3207 then
3208 Pref_Entity := Entity (Prefix ((Prev_Orig)));
3210 else
3211 Pref_Entity := Empty;
3212 end if;
3214 if Is_Entity_Name (Prev_Orig)
3215 and then Is_Type (Entity (Prev_Orig))
3216 then
3217 Add_Extra_Actual
3218 (Expr =>
3219 Make_Integer_Literal (Loc,
3220 Intval =>
3221 Type_Access_Level (Pref_Entity)),
3222 EF => Extra_Accessibility (Formal));
3224 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3225 and then Present (Pref_Entity)
3226 and then Is_Formal (Pref_Entity)
3227 and then Present
3228 (Extra_Accessibility (Pref_Entity))
3229 then
3230 Add_Extra_Actual
3231 (Expr =>
3232 New_Occurrence_Of
3233 (Extra_Accessibility (Pref_Entity), Loc),
3234 EF => Extra_Accessibility (Formal));
3236 else
3237 Add_Extra_Actual
3238 (Expr =>
3239 Make_Integer_Literal (Loc,
3240 Intval =>
3241 Object_Access_Level (Prev_Orig)),
3242 EF => Extra_Accessibility (Formal));
3243 end if;
3245 -- Treat the unchecked attributes as library-level
3247 when Attribute_Unchecked_Access
3248 | Attribute_Unrestricted_Access
3250 Add_Extra_Actual
3251 (Expr =>
3252 Make_Integer_Literal (Loc,
3253 Intval => Scope_Depth (Standard_Standard)),
3254 EF => Extra_Accessibility (Formal));
3256 -- No other cases of attributes returning access
3257 -- values that can be passed to access parameters.
3259 when others =>
3260 raise Program_Error;
3262 end case;
3264 -- For allocators we pass the level of the execution of the
3265 -- called subprogram, which is one greater than the current
3266 -- scope level.
3268 when N_Allocator =>
3269 Add_Extra_Actual
3270 (Expr =>
3271 Make_Integer_Literal (Loc,
3272 Intval => Scope_Depth (Current_Scope) + 1),
3273 EF => Extra_Accessibility (Formal));
3275 -- For most other cases we simply pass the level of the
3276 -- actual's access type. The type is retrieved from
3277 -- Prev rather than Prev_Orig, because in some cases
3278 -- Prev_Orig denotes an original expression that has
3279 -- not been analyzed.
3281 when others =>
3282 Add_Extra_Actual
3283 (Expr => Dynamic_Accessibility_Level (Prev),
3284 EF => Extra_Accessibility (Formal));
3285 end case;
3286 end if;
3287 end if;
3289 -- Perform the check of 4.6(49) that prevents a null value from being
3290 -- passed as an actual to an access parameter. Note that the check
3291 -- is elided in the common cases of passing an access attribute or
3292 -- access parameter as an actual. Also, we currently don't enforce
3293 -- this check for expander-generated actuals and when -gnatdj is set.
3295 if Ada_Version >= Ada_2005 then
3297 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3298 -- the intent of 6.4.1(13) is that null-exclusion checks should
3299 -- not be done for 'out' parameters, even though it refers only
3300 -- to constraint checks, and a null_exclusion is not a constraint.
3301 -- Note that AI05-0196-1 corrects this mistake in the RM.
3303 if Is_Access_Type (Etype (Formal))
3304 and then Can_Never_Be_Null (Etype (Formal))
3305 and then Ekind (Formal) /= E_Out_Parameter
3306 and then Nkind (Prev) /= N_Raise_Constraint_Error
3307 and then (Known_Null (Prev)
3308 or else not Can_Never_Be_Null (Etype (Prev)))
3309 then
3310 Install_Null_Excluding_Check (Prev);
3311 end if;
3313 -- Ada_Version < Ada_2005
3315 else
3316 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3317 or else Access_Checks_Suppressed (Subp)
3318 then
3319 null;
3321 elsif Debug_Flag_J then
3322 null;
3324 elsif not Comes_From_Source (Prev) then
3325 null;
3327 elsif Is_Entity_Name (Prev)
3328 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3329 then
3330 null;
3332 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3333 null;
3335 else
3336 Install_Null_Excluding_Check (Prev);
3337 end if;
3338 end if;
3340 -- Perform appropriate validity checks on parameters that
3341 -- are entities.
3343 if Validity_Checks_On then
3344 if (Ekind (Formal) = E_In_Parameter
3345 and then Validity_Check_In_Params)
3346 or else
3347 (Ekind (Formal) = E_In_Out_Parameter
3348 and then Validity_Check_In_Out_Params)
3349 then
3350 -- If the actual is an indexed component of a packed type (or
3351 -- is an indexed or selected component whose prefix recursively
3352 -- meets this condition), it has not been expanded yet. It will
3353 -- be copied in the validity code that follows, and has to be
3354 -- expanded appropriately, so reanalyze it.
3356 -- What we do is just to unset analyzed bits on prefixes till
3357 -- we reach something that does not have a prefix.
3359 declare
3360 Nod : Node_Id;
3362 begin
3363 Nod := Actual;
3364 while Nkind_In (Nod, N_Indexed_Component,
3365 N_Selected_Component)
3366 loop
3367 Set_Analyzed (Nod, False);
3368 Nod := Prefix (Nod);
3369 end loop;
3370 end;
3372 Ensure_Valid (Actual);
3373 end if;
3374 end if;
3376 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3377 -- since this is a left side reference. We only do this for calls
3378 -- from the source program since we assume that compiler generated
3379 -- calls explicitly generate any required checks. We also need it
3380 -- only if we are doing standard validity checks, since clearly it is
3381 -- not needed if validity checks are off, and in subscript validity
3382 -- checking mode, all indexed components are checked with a call
3383 -- directly from Expand_N_Indexed_Component.
3385 if Comes_From_Source (Call_Node)
3386 and then Ekind (Formal) /= E_In_Parameter
3387 and then Validity_Checks_On
3388 and then Validity_Check_Default
3389 and then not Validity_Check_Subscripts
3390 then
3391 Check_Valid_Lvalue_Subscripts (Actual);
3392 end if;
3394 -- Mark any scalar OUT parameter that is a simple variable as no
3395 -- longer known to be valid (unless the type is always valid). This
3396 -- reflects the fact that if an OUT parameter is never set in a
3397 -- procedure, then it can become invalid on the procedure return.
3399 if Ekind (Formal) = E_Out_Parameter
3400 and then Is_Entity_Name (Actual)
3401 and then Ekind (Entity (Actual)) = E_Variable
3402 and then not Is_Known_Valid (Etype (Actual))
3403 then
3404 Set_Is_Known_Valid (Entity (Actual), False);
3405 end if;
3407 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3408 -- clear current values, since they can be clobbered. We are probably
3409 -- doing this in more places than we need to, but better safe than
3410 -- sorry when it comes to retaining bad current values.
3412 if Ekind (Formal) /= E_In_Parameter
3413 and then Is_Entity_Name (Actual)
3414 and then Present (Entity (Actual))
3415 then
3416 declare
3417 Ent : constant Entity_Id := Entity (Actual);
3418 Sav : Node_Id;
3420 begin
3421 -- For an OUT or IN OUT parameter that is an assignable entity,
3422 -- we do not want to clobber the Last_Assignment field, since
3423 -- if it is set, it was precisely because it is indeed an OUT
3424 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3425 -- since the subprogram could have returned in invalid value.
3427 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3428 and then Is_Assignable (Ent)
3429 then
3430 Sav := Last_Assignment (Ent);
3431 Kill_Current_Values (Ent);
3432 Set_Last_Assignment (Ent, Sav);
3433 Set_Is_Known_Valid (Ent, False);
3435 -- For all other cases, just kill the current values
3437 else
3438 Kill_Current_Values (Ent);
3439 end if;
3440 end;
3441 end if;
3443 -- If the formal is class wide and the actual is an aggregate, force
3444 -- evaluation so that the back end who does not know about class-wide
3445 -- type, does not generate a temporary of the wrong size.
3447 if not Is_Class_Wide_Type (Etype (Formal)) then
3448 null;
3450 elsif Nkind (Actual) = N_Aggregate
3451 or else (Nkind (Actual) = N_Qualified_Expression
3452 and then Nkind (Expression (Actual)) = N_Aggregate)
3453 then
3454 Force_Evaluation (Actual);
3455 end if;
3457 -- In a remote call, if the formal is of a class-wide type, check
3458 -- that the actual meets the requirements described in E.4(18).
3460 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3461 Insert_Action (Actual,
3462 Make_Transportable_Check (Loc,
3463 Duplicate_Subexpr_Move_Checks (Actual)));
3464 end if;
3466 -- Perform invariant checks for all intermediate types in a view
3467 -- conversion after successful return from a call that passes the
3468 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3469 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3470 -- generating spurious checks on complex expansion such as object
3471 -- initialization through an extension aggregate.
3473 if Comes_From_Source (N)
3474 and then Ekind (Formal) /= E_In_Parameter
3475 and then Nkind (Actual) = N_Type_Conversion
3476 then
3477 Add_View_Conversion_Invariants (Formal, Actual);
3478 end if;
3480 -- Generating C the initialization of an allocator is performed by
3481 -- means of individual statements, and hence it must be done before
3482 -- the call.
3484 if Modify_Tree_For_C
3485 and then Nkind (Actual) = N_Allocator
3486 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3487 then
3488 Remove_Side_Effects (Actual);
3489 end if;
3491 -- This label is required when skipping extra actual generation for
3492 -- Unchecked_Union parameters.
3494 <<Skip_Extra_Actual_Generation>>
3496 Param_Count := Param_Count + 1;
3497 Next_Actual (Actual);
3498 Next_Formal (Formal);
3499 end loop;
3501 -- If we are calling an Ada 2012 function which needs to have the
3502 -- "accessibility level determined by the point of call" (AI05-0234)
3503 -- passed in to it, then pass it in.
3505 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3506 and then
3507 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3508 then
3509 declare
3510 Ancestor : Node_Id := Parent (Call_Node);
3511 Level : Node_Id := Empty;
3512 Defer : Boolean := False;
3514 begin
3515 -- Unimplemented: if Subp returns an anonymous access type, then
3517 -- a) if the call is the operand of an explict conversion, then
3518 -- the target type of the conversion (a named access type)
3519 -- determines the accessibility level pass in;
3521 -- b) if the call defines an access discriminant of an object
3522 -- (e.g., the discriminant of an object being created by an
3523 -- allocator, or the discriminant of a function result),
3524 -- then the accessibility level to pass in is that of the
3525 -- discriminated object being initialized).
3527 -- ???
3529 while Nkind (Ancestor) = N_Qualified_Expression
3530 loop
3531 Ancestor := Parent (Ancestor);
3532 end loop;
3534 case Nkind (Ancestor) is
3535 when N_Allocator =>
3537 -- At this point, we'd like to assign
3539 -- Level := Dynamic_Accessibility_Level (Ancestor);
3541 -- but Etype of Ancestor may not have been set yet,
3542 -- so that doesn't work.
3544 -- Handle this later in Expand_Allocator_Expression.
3546 Defer := True;
3548 when N_Object_Declaration
3549 | N_Object_Renaming_Declaration
3551 declare
3552 Def_Id : constant Entity_Id :=
3553 Defining_Identifier (Ancestor);
3555 begin
3556 if Is_Return_Object (Def_Id) then
3557 if Present (Extra_Accessibility_Of_Result
3558 (Return_Applies_To (Scope (Def_Id))))
3559 then
3560 -- Pass along value that was passed in if the
3561 -- routine we are returning from also has an
3562 -- Accessibility_Of_Result formal.
3564 Level :=
3565 New_Occurrence_Of
3566 (Extra_Accessibility_Of_Result
3567 (Return_Applies_To (Scope (Def_Id))), Loc);
3568 end if;
3569 else
3570 Level :=
3571 Make_Integer_Literal (Loc,
3572 Intval => Object_Access_Level (Def_Id));
3573 end if;
3574 end;
3576 when N_Simple_Return_Statement =>
3577 if Present (Extra_Accessibility_Of_Result
3578 (Return_Applies_To
3579 (Return_Statement_Entity (Ancestor))))
3580 then
3581 -- Pass along value that was passed in if the returned
3582 -- routine also has an Accessibility_Of_Result formal.
3584 Level :=
3585 New_Occurrence_Of
3586 (Extra_Accessibility_Of_Result
3587 (Return_Applies_To
3588 (Return_Statement_Entity (Ancestor))), Loc);
3589 end if;
3591 when others =>
3592 null;
3593 end case;
3595 if not Defer then
3596 if not Present (Level) then
3598 -- The "innermost master that evaluates the function call".
3600 -- ??? - Should we use Integer'Last here instead in order
3601 -- to deal with (some of) the problems associated with
3602 -- calls to subps whose enclosing scope is unknown (e.g.,
3603 -- Anon_Access_To_Subp_Param.all)?
3605 Level :=
3606 Make_Integer_Literal (Loc,
3607 Intval => Scope_Depth (Current_Scope) + 1);
3608 end if;
3610 Add_Extra_Actual
3611 (Expr => Level,
3612 EF =>
3613 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3614 end if;
3615 end;
3616 end if;
3618 -- If we are expanding the RHS of an assignment we need to check if tag
3619 -- propagation is needed. You might expect this processing to be in
3620 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3621 -- assignment might be transformed to a declaration for an unconstrained
3622 -- value if the expression is classwide.
3624 if Nkind (Call_Node) = N_Function_Call
3625 and then Is_Tag_Indeterminate (Call_Node)
3626 and then Is_Entity_Name (Name (Call_Node))
3627 then
3628 declare
3629 Ass : Node_Id := Empty;
3631 begin
3632 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3633 Ass := Parent (Call_Node);
3635 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3636 and then Nkind (Parent (Parent (Call_Node))) =
3637 N_Assignment_Statement
3638 then
3639 Ass := Parent (Parent (Call_Node));
3641 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3642 and then Nkind (Parent (Parent (Call_Node))) =
3643 N_Assignment_Statement
3644 then
3645 Ass := Parent (Parent (Call_Node));
3646 end if;
3648 if Present (Ass)
3649 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3650 then
3651 if Is_Access_Type (Etype (Call_Node)) then
3652 if Designated_Type (Etype (Call_Node)) /=
3653 Root_Type (Etype (Name (Ass)))
3654 then
3655 Error_Msg_NE
3656 ("tag-indeterminate expression must have designated "
3657 & "type& (RM 5.2 (6))",
3658 Call_Node, Root_Type (Etype (Name (Ass))));
3659 else
3660 Propagate_Tag (Name (Ass), Call_Node);
3661 end if;
3663 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3664 Error_Msg_NE
3665 ("tag-indeterminate expression must have type & "
3666 & "(RM 5.2 (6))",
3667 Call_Node, Root_Type (Etype (Name (Ass))));
3669 else
3670 Propagate_Tag (Name (Ass), Call_Node);
3671 end if;
3673 -- The call will be rewritten as a dispatching call, and
3674 -- expanded as such.
3676 return;
3677 end if;
3678 end;
3679 end if;
3681 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3682 -- it to point to the correct secondary virtual table
3684 if Nkind (Call_Node) in N_Subprogram_Call
3685 and then CW_Interface_Formals_Present
3686 then
3687 Expand_Interface_Actuals (Call_Node);
3688 end if;
3690 -- Deals with Dispatch_Call if we still have a call, before expanding
3691 -- extra actuals since this will be done on the re-analysis of the
3692 -- dispatching call. Note that we do not try to shorten the actual list
3693 -- for a dispatching call, it would not make sense to do so. Expansion
3694 -- of dispatching calls is suppressed for VM targets, because the VM
3695 -- back-ends directly handle the generation of dispatching calls and
3696 -- would have to undo any expansion to an indirect call.
3698 if Nkind (Call_Node) in N_Subprogram_Call
3699 and then Present (Controlling_Argument (Call_Node))
3700 then
3701 declare
3702 Call_Typ : constant Entity_Id := Etype (Call_Node);
3703 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3704 Eq_Prim_Op : Entity_Id := Empty;
3705 New_Call : Node_Id;
3706 Param : Node_Id;
3707 Prev_Call : Node_Id;
3709 begin
3710 if not Is_Limited_Type (Typ) then
3711 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3712 end if;
3714 if Tagged_Type_Expansion then
3715 Expand_Dispatching_Call (Call_Node);
3717 -- The following return is worrisome. Is it really OK to skip
3718 -- all remaining processing in this procedure ???
3720 return;
3722 -- VM targets
3724 else
3725 Apply_Tag_Checks (Call_Node);
3727 -- If this is a dispatching "=", we must first compare the
3728 -- tags so we generate: x.tag = y.tag and then x = y
3730 if Subp = Eq_Prim_Op then
3732 -- Mark the node as analyzed to avoid reanalyzing this
3733 -- dispatching call (which would cause a never-ending loop)
3735 Prev_Call := Relocate_Node (Call_Node);
3736 Set_Analyzed (Prev_Call);
3738 Param := First_Actual (Call_Node);
3739 New_Call :=
3740 Make_And_Then (Loc,
3741 Left_Opnd =>
3742 Make_Op_Eq (Loc,
3743 Left_Opnd =>
3744 Make_Selected_Component (Loc,
3745 Prefix => New_Value (Param),
3746 Selector_Name =>
3747 New_Occurrence_Of
3748 (First_Tag_Component (Typ), Loc)),
3750 Right_Opnd =>
3751 Make_Selected_Component (Loc,
3752 Prefix =>
3753 Unchecked_Convert_To (Typ,
3754 New_Value (Next_Actual (Param))),
3755 Selector_Name =>
3756 New_Occurrence_Of
3757 (First_Tag_Component (Typ), Loc))),
3758 Right_Opnd => Prev_Call);
3760 Rewrite (Call_Node, New_Call);
3762 Analyze_And_Resolve
3763 (Call_Node, Call_Typ, Suppress => All_Checks);
3764 end if;
3766 -- Expansion of a dispatching call results in an indirect call,
3767 -- which in turn causes current values to be killed (see
3768 -- Resolve_Call), so on VM targets we do the call here to
3769 -- ensure consistent warnings between VM and non-VM targets.
3771 Kill_Current_Values;
3772 end if;
3774 -- If this is a dispatching "=" then we must update the reference
3775 -- to the call node because we generated:
3776 -- x.tag = y.tag and then x = y
3778 if Subp = Eq_Prim_Op then
3779 Call_Node := Right_Opnd (Call_Node);
3780 end if;
3781 end;
3782 end if;
3784 -- Similarly, expand calls to RCI subprograms on which pragma
3785 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3786 -- later. Do this only when the call comes from source since we
3787 -- do not want such a rewriting to occur in expanded code.
3789 if Is_All_Remote_Call (Call_Node) then
3790 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3792 -- Similarly, do not add extra actuals for an entry call whose entity
3793 -- is a protected procedure, or for an internal protected subprogram
3794 -- call, because it will be rewritten as a protected subprogram call
3795 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3797 elsif Is_Protected_Type (Scope (Subp))
3798 and then (Ekind (Subp) = E_Procedure
3799 or else Ekind (Subp) = E_Function)
3800 then
3801 null;
3803 -- During that loop we gathered the extra actuals (the ones that
3804 -- correspond to Extra_Formals), so now they can be appended.
3806 else
3807 while Is_Non_Empty_List (Extra_Actuals) loop
3808 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3809 end loop;
3810 end if;
3812 -- At this point we have all the actuals, so this is the point at which
3813 -- the various expansion activities for actuals is carried out.
3815 Expand_Actuals (Call_Node, Subp, Post_Call);
3817 -- Verify that the actuals do not share storage. This check must be done
3818 -- on the caller side rather that inside the subprogram to avoid issues
3819 -- of parameter passing.
3821 if Check_Aliasing_Of_Parameters then
3822 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3823 end if;
3825 -- If the subprogram is a renaming, or if it is inherited, replace it in
3826 -- the call with the name of the actual subprogram being called. If this
3827 -- is a dispatching call, the run-time decides what to call. The Alias
3828 -- attribute does not apply to entries.
3830 if Nkind (Call_Node) /= N_Entry_Call_Statement
3831 and then No (Controlling_Argument (Call_Node))
3832 and then Present (Parent_Subp)
3833 and then not Is_Direct_Deep_Call (Subp)
3834 then
3835 if Present (Inherited_From_Formal (Subp)) then
3836 Parent_Subp := Inherited_From_Formal (Subp);
3837 else
3838 Parent_Subp := Ultimate_Alias (Parent_Subp);
3839 end if;
3841 -- The below setting of Entity is suspect, see F109-018 discussion???
3843 Set_Entity (Name (Call_Node), Parent_Subp);
3845 if Is_Abstract_Subprogram (Parent_Subp)
3846 and then not In_Instance
3847 then
3848 Error_Msg_NE
3849 ("cannot call abstract subprogram &!",
3850 Name (Call_Node), Parent_Subp);
3851 end if;
3853 -- Inspect all formals of derived subprogram Subp. Compare parameter
3854 -- types with the parent subprogram and check whether an actual may
3855 -- need a type conversion to the corresponding formal of the parent
3856 -- subprogram.
3858 -- Not clear whether intrinsic subprograms need such conversions. ???
3860 if not Is_Intrinsic_Subprogram (Parent_Subp)
3861 or else Is_Generic_Instance (Parent_Subp)
3862 then
3863 declare
3864 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3865 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3866 -- and resolve the newly generated construct.
3868 -------------
3869 -- Convert --
3870 -------------
3872 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3873 begin
3874 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3875 Analyze (Act);
3876 Resolve (Act, Typ);
3877 end Convert;
3879 -- Local variables
3881 Actual_Typ : Entity_Id;
3882 Formal_Typ : Entity_Id;
3883 Parent_Typ : Entity_Id;
3885 begin
3886 Actual := First_Actual (Call_Node);
3887 Formal := First_Formal (Subp);
3888 Parent_Formal := First_Formal (Parent_Subp);
3889 while Present (Formal) loop
3890 Actual_Typ := Etype (Actual);
3891 Formal_Typ := Etype (Formal);
3892 Parent_Typ := Etype (Parent_Formal);
3894 -- For an IN parameter of a scalar type, the parent formal
3895 -- type and derived formal type differ or the parent formal
3896 -- type and actual type do not match statically.
3898 if Is_Scalar_Type (Formal_Typ)
3899 and then Ekind (Formal) = E_In_Parameter
3900 and then Formal_Typ /= Parent_Typ
3901 and then
3902 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3903 and then not Raises_Constraint_Error (Actual)
3904 then
3905 Convert (Actual, Parent_Typ);
3906 Enable_Range_Check (Actual);
3908 -- If the actual has been marked as requiring a range
3909 -- check, then generate it here.
3911 if Do_Range_Check (Actual) then
3912 Generate_Range_Check
3913 (Actual, Etype (Formal), CE_Range_Check_Failed);
3914 end if;
3916 -- For access types, the parent formal type and actual type
3917 -- differ.
3919 elsif Is_Access_Type (Formal_Typ)
3920 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3921 then
3922 if Ekind (Formal) /= E_In_Parameter then
3923 Convert (Actual, Parent_Typ);
3925 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3926 and then Designated_Type (Parent_Typ) /=
3927 Designated_Type (Actual_Typ)
3928 and then not Is_Controlling_Formal (Formal)
3929 then
3930 -- This unchecked conversion is not necessary unless
3931 -- inlining is enabled, because in that case the type
3932 -- mismatch may become visible in the body about to be
3933 -- inlined.
3935 Rewrite (Actual,
3936 Unchecked_Convert_To (Parent_Typ,
3937 Relocate_Node (Actual)));
3938 Analyze (Actual);
3939 Resolve (Actual, Parent_Typ);
3940 end if;
3942 -- If there is a change of representation, then generate a
3943 -- warning, and do the change of representation.
3945 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3946 Error_Msg_N
3947 ("??change of representation required", Actual);
3948 Convert (Actual, Parent_Typ);
3950 -- For array and record types, the parent formal type and
3951 -- derived formal type have different sizes or pragma Pack
3952 -- status.
3954 elsif ((Is_Array_Type (Formal_Typ)
3955 and then Is_Array_Type (Parent_Typ))
3956 or else
3957 (Is_Record_Type (Formal_Typ)
3958 and then Is_Record_Type (Parent_Typ)))
3959 and then
3960 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3961 or else Has_Pragma_Pack (Formal_Typ) /=
3962 Has_Pragma_Pack (Parent_Typ))
3963 then
3964 Convert (Actual, Parent_Typ);
3965 end if;
3967 Next_Actual (Actual);
3968 Next_Formal (Formal);
3969 Next_Formal (Parent_Formal);
3970 end loop;
3971 end;
3972 end if;
3974 Orig_Subp := Subp;
3975 Subp := Parent_Subp;
3976 end if;
3978 -- Deal with case where call is an explicit dereference
3980 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3982 -- Handle case of access to protected subprogram type
3984 if Is_Access_Protected_Subprogram_Type
3985 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3986 then
3987 -- If this is a call through an access to protected operation, the
3988 -- prefix has the form (object'address, operation'access). Rewrite
3989 -- as a for other protected calls: the object is the 1st parameter
3990 -- of the list of actuals.
3992 declare
3993 Call : Node_Id;
3994 Parm : List_Id;
3995 Nam : Node_Id;
3996 Obj : Node_Id;
3997 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3999 T : constant Entity_Id :=
4000 Equivalent_Type (Base_Type (Etype (Ptr)));
4002 D_T : constant Entity_Id :=
4003 Designated_Type (Base_Type (Etype (Ptr)));
4005 begin
4006 Obj :=
4007 Make_Selected_Component (Loc,
4008 Prefix => Unchecked_Convert_To (T, Ptr),
4009 Selector_Name =>
4010 New_Occurrence_Of (First_Entity (T), Loc));
4012 Nam :=
4013 Make_Selected_Component (Loc,
4014 Prefix => Unchecked_Convert_To (T, Ptr),
4015 Selector_Name =>
4016 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4018 Nam :=
4019 Make_Explicit_Dereference (Loc,
4020 Prefix => Nam);
4022 if Present (Parameter_Associations (Call_Node)) then
4023 Parm := Parameter_Associations (Call_Node);
4024 else
4025 Parm := New_List;
4026 end if;
4028 Prepend (Obj, Parm);
4030 if Etype (D_T) = Standard_Void_Type then
4031 Call :=
4032 Make_Procedure_Call_Statement (Loc,
4033 Name => Nam,
4034 Parameter_Associations => Parm);
4035 else
4036 Call :=
4037 Make_Function_Call (Loc,
4038 Name => Nam,
4039 Parameter_Associations => Parm);
4040 end if;
4042 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4043 Set_Etype (Call, Etype (D_T));
4045 -- We do not re-analyze the call to avoid infinite recursion.
4046 -- We analyze separately the prefix and the object, and set
4047 -- the checks on the prefix that would otherwise be emitted
4048 -- when resolving a call.
4050 Rewrite (Call_Node, Call);
4051 Analyze (Nam);
4052 Apply_Access_Check (Nam);
4053 Analyze (Obj);
4054 return;
4055 end;
4056 end if;
4057 end if;
4059 -- If this is a call to an intrinsic subprogram, then perform the
4060 -- appropriate expansion to the corresponding tree node and we
4061 -- are all done (since after that the call is gone).
4063 -- In the case where the intrinsic is to be processed by the back end,
4064 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4065 -- since the idea in this case is to pass the call unchanged. If the
4066 -- intrinsic is an inherited unchecked conversion, and the derived type
4067 -- is the target type of the conversion, we must retain it as the return
4068 -- type of the expression. Otherwise the expansion below, which uses the
4069 -- parent operation, will yield the wrong type.
4071 if Is_Intrinsic_Subprogram (Subp) then
4072 Expand_Intrinsic_Call (Call_Node, Subp);
4074 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4075 and then Parent_Subp /= Orig_Subp
4076 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4077 then
4078 Set_Etype (Call_Node, Etype (Orig_Subp));
4079 end if;
4081 return;
4082 end if;
4084 if Ekind_In (Subp, E_Function, E_Procedure) then
4086 -- We perform a simple optimization on calls for To_Address by
4087 -- replacing them with an unchecked conversion. Not only is this
4088 -- efficient, but it also avoids order of elaboration problems when
4089 -- address clauses are inlined (address expression elaborated at the
4090 -- wrong point).
4092 -- We perform this optimization regardless of whether we are in the
4093 -- main unit or in a unit in the context of the main unit, to ensure
4094 -- that the generated tree is the same in both cases, for CodePeer
4095 -- use.
4097 if Is_RTE (Subp, RE_To_Address) then
4098 Rewrite (Call_Node,
4099 Unchecked_Convert_To
4100 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4101 return;
4103 -- A call to a null procedure is replaced by a null statement, but we
4104 -- are not allowed to ignore possible side effects of the call, so we
4105 -- make sure that actuals are evaluated.
4106 -- We also suppress this optimization for GNATCoverage.
4108 elsif Is_Null_Procedure (Subp)
4109 and then not Opt.Suppress_Control_Flow_Optimizations
4110 then
4111 Actual := First_Actual (Call_Node);
4112 while Present (Actual) loop
4113 Remove_Side_Effects (Actual);
4114 Next_Actual (Actual);
4115 end loop;
4117 Rewrite (Call_Node, Make_Null_Statement (Loc));
4118 return;
4119 end if;
4121 -- Handle inlining. No action needed if the subprogram is not inlined
4123 if not Is_Inlined (Subp) then
4124 null;
4126 -- Frontend inlining of expression functions (performed also when
4127 -- backend inlining is enabled).
4129 elsif Is_Inlinable_Expression_Function (Subp) then
4130 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
4131 Analyze (N);
4132 return;
4134 -- Handle frontend inlining
4136 elsif not Back_End_Inlining then
4137 Inlined_Subprogram : declare
4138 Bod : Node_Id;
4139 Must_Inline : Boolean := False;
4140 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4142 begin
4143 -- Verify that the body to inline has already been seen, and
4144 -- that if the body is in the current unit the inlining does
4145 -- not occur earlier. This avoids order-of-elaboration problems
4146 -- in the back end.
4148 -- This should be documented in sinfo/einfo ???
4150 if No (Spec)
4151 or else Nkind (Spec) /= N_Subprogram_Declaration
4152 or else No (Body_To_Inline (Spec))
4153 then
4154 Must_Inline := False;
4156 -- If this an inherited function that returns a private type,
4157 -- do not inline if the full view is an unconstrained array,
4158 -- because such calls cannot be inlined.
4160 elsif Present (Orig_Subp)
4161 and then Is_Array_Type (Etype (Orig_Subp))
4162 and then not Is_Constrained (Etype (Orig_Subp))
4163 then
4164 Must_Inline := False;
4166 elsif In_Unfrozen_Instance (Scope (Subp)) then
4167 Must_Inline := False;
4169 else
4170 Bod := Body_To_Inline (Spec);
4172 if (In_Extended_Main_Code_Unit (Call_Node)
4173 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4174 or else Has_Pragma_Inline_Always (Subp))
4175 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4176 or else
4177 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4178 then
4179 Must_Inline := True;
4181 -- If we are compiling a package body that is not the main
4182 -- unit, it must be for inlining/instantiation purposes,
4183 -- in which case we inline the call to insure that the same
4184 -- temporaries are generated when compiling the body by
4185 -- itself. Otherwise link errors can occur.
4187 -- If the function being called is itself in the main unit,
4188 -- we cannot inline, because there is a risk of double
4189 -- elaboration and/or circularity: the inlining can make
4190 -- visible a private entity in the body of the main unit,
4191 -- that gigi will see before its sees its proper definition.
4193 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4194 and then In_Package_Body
4195 then
4196 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4198 -- Inline calls to _postconditions when generating C code
4200 elsif Modify_Tree_For_C
4201 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4202 and then Chars (Name (N)) = Name_uPostconditions
4203 then
4204 Must_Inline := True;
4205 end if;
4206 end if;
4208 if Must_Inline then
4209 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4211 else
4212 -- Let the back end handle it
4214 Add_Inlined_Body (Subp, Call_Node);
4216 if Front_End_Inlining
4217 and then Nkind (Spec) = N_Subprogram_Declaration
4218 and then (In_Extended_Main_Code_Unit (Call_Node))
4219 and then No (Body_To_Inline (Spec))
4220 and then not Has_Completion (Subp)
4221 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4222 then
4223 Cannot_Inline
4224 ("cannot inline& (body not seen yet)?",
4225 Call_Node, Subp);
4226 end if;
4227 end if;
4228 end Inlined_Subprogram;
4230 -- Back end inlining: let the back end handle it
4232 elsif No (Unit_Declaration_Node (Subp))
4233 or else Nkind (Unit_Declaration_Node (Subp)) /=
4234 N_Subprogram_Declaration
4235 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4236 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4237 N_Entity
4238 then
4239 Add_Inlined_Body (Subp, Call_Node);
4241 -- If the inlined call appears within an instantiation and some
4242 -- level of optimization is required, ensure that the enclosing
4243 -- instance body is available so that the back-end can actually
4244 -- perform the inlining.
4246 if In_Instance
4247 and then Comes_From_Source (Subp)
4248 and then Optimization_Level > 0
4249 then
4250 declare
4251 Decl : Node_Id;
4252 Inst : Entity_Id;
4253 Inst_Node : Node_Id;
4255 begin
4256 Inst := Scope (Subp);
4258 -- Find enclosing instance
4260 while Present (Inst) and then Inst /= Standard_Standard loop
4261 exit when Is_Generic_Instance (Inst);
4262 Inst := Scope (Inst);
4263 end loop;
4265 if Present (Inst)
4266 and then Is_Generic_Instance (Inst)
4267 and then not Is_Inlined (Inst)
4268 then
4269 Set_Is_Inlined (Inst);
4270 Decl := Unit_Declaration_Node (Inst);
4272 -- Do not add a pending instantiation if the body exits
4273 -- already, or if the instance is a compilation unit, or
4274 -- the instance node is missing.
4276 if Present (Corresponding_Body (Decl))
4277 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4278 or else No (Next (Decl))
4279 then
4280 null;
4282 else
4283 -- The instantiation node usually follows the package
4284 -- declaration for the instance. If the generic unit
4285 -- has aspect specifications, they are transformed
4286 -- into pragmas in the instance, and the instance node
4287 -- appears after them.
4289 Inst_Node := Next (Decl);
4291 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4292 Inst_Node := Next (Inst_Node);
4293 end loop;
4295 Add_Pending_Instantiation (Inst_Node, Decl);
4296 end if;
4297 end if;
4298 end;
4299 end if;
4301 -- Front end expansion of simple functions returning unconstrained
4302 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4303 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4304 -- also Build_Renamed_Body) cannot be expanded here because this may
4305 -- give rise to order-of-elaboration issues for the types of the
4306 -- parameters of the subprogram, if any.
4308 else
4309 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4310 end if;
4311 end if;
4313 -- Check for protected subprogram. This is either an intra-object call,
4314 -- or a protected function call. Protected procedure calls are rewritten
4315 -- as entry calls and handled accordingly.
4317 -- In Ada 2005, this may be an indirect call to an access parameter that
4318 -- is an access_to_subprogram. In that case the anonymous type has a
4319 -- scope that is a protected operation, but the call is a regular one.
4320 -- In either case do not expand call if subprogram is eliminated.
4322 Scop := Scope (Subp);
4324 if Nkind (Call_Node) /= N_Entry_Call_Statement
4325 and then Is_Protected_Type (Scop)
4326 and then Ekind (Subp) /= E_Subprogram_Type
4327 and then not Is_Eliminated (Subp)
4328 then
4329 -- If the call is an internal one, it is rewritten as a call to the
4330 -- corresponding unprotected subprogram.
4332 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4333 end if;
4335 -- Functions returning controlled objects need special attention. If
4336 -- the return type is limited, then the context is initialization and
4337 -- different processing applies. If the call is to a protected function,
4338 -- the expansion above will call Expand_Call recursively. Otherwise the
4339 -- function call is transformed into a temporary which obtains the
4340 -- result from the secondary stack.
4342 if Needs_Finalization (Etype (Subp)) then
4343 if not Is_Build_In_Place_Function_Call (Call_Node)
4344 and then
4345 (No (First_Formal (Subp))
4346 or else
4347 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4348 then
4349 Expand_Ctrl_Function_Call (Call_Node);
4351 -- Build-in-place function calls which appear in anonymous contexts
4352 -- need a transient scope to ensure the proper finalization of the
4353 -- intermediate result after its use.
4355 elsif Is_Build_In_Place_Function_Call (Call_Node)
4356 and then Nkind_In (Parent (Unqual_Conv (Call_Node)),
4357 N_Attribute_Reference,
4358 N_Function_Call,
4359 N_Indexed_Component,
4360 N_Object_Renaming_Declaration,
4361 N_Procedure_Call_Statement,
4362 N_Selected_Component,
4363 N_Slice)
4364 and then
4365 (Ekind (Current_Scope) /= E_Loop
4366 or else Nkind (Parent (N)) /= N_Function_Call
4367 or else not Is_Build_In_Place_Function_Call (Parent (N)))
4368 then
4369 Establish_Transient_Scope (Call_Node, Manage_Sec_Stack => True);
4370 end if;
4371 end if;
4372 end Expand_Call_Helper;
4374 -------------------------------
4375 -- Expand_Ctrl_Function_Call --
4376 -------------------------------
4378 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4379 function Is_Element_Reference (N : Node_Id) return Boolean;
4380 -- Determine whether node N denotes a reference to an Ada 2012 container
4381 -- element.
4383 --------------------------
4384 -- Is_Element_Reference --
4385 --------------------------
4387 function Is_Element_Reference (N : Node_Id) return Boolean is
4388 Ref : constant Node_Id := Original_Node (N);
4390 begin
4391 -- Analysis marks an element reference by setting the generalized
4392 -- indexing attribute of an indexed component before the component
4393 -- is rewritten into a function call.
4395 return
4396 Nkind (Ref) = N_Indexed_Component
4397 and then Present (Generalized_Indexing (Ref));
4398 end Is_Element_Reference;
4400 -- Start of processing for Expand_Ctrl_Function_Call
4402 begin
4403 -- Optimization, if the returned value (which is on the sec-stack) is
4404 -- returned again, no need to copy/readjust/finalize, we can just pass
4405 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4406 -- attachment is needed
4408 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4409 return;
4410 end if;
4412 -- Resolution is now finished, make sure we don't start analysis again
4413 -- because of the duplication.
4415 Set_Analyzed (N);
4417 -- A function which returns a controlled object uses the secondary
4418 -- stack. Rewrite the call into a temporary which obtains the result of
4419 -- the function using 'reference.
4421 Remove_Side_Effects (N);
4423 -- The side effect removal of the function call produced a temporary.
4424 -- When the context is a case expression, if expression, or expression
4425 -- with actions, the lifetime of the temporary must be extended to match
4426 -- that of the context. Otherwise the function result will be finalized
4427 -- too early and affect the result of the expression. To prevent this
4428 -- unwanted effect, the temporary should not be considered for clean up
4429 -- actions by the general finalization machinery.
4431 -- Exception to this rule are references to Ada 2012 container elements.
4432 -- Such references must be finalized at the end of each iteration of the
4433 -- related quantified expression, otherwise the container will remain
4434 -- busy.
4436 if Nkind (N) = N_Explicit_Dereference
4437 and then Within_Case_Or_If_Expression (N)
4438 and then not Is_Element_Reference (N)
4439 then
4440 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4441 end if;
4442 end Expand_Ctrl_Function_Call;
4444 ----------------------------------------
4445 -- Expand_N_Extended_Return_Statement --
4446 ----------------------------------------
4448 -- If there is a Handled_Statement_Sequence, we rewrite this:
4450 -- return Result : T := <expression> do
4451 -- <handled_seq_of_stms>
4452 -- end return;
4454 -- to be:
4456 -- declare
4457 -- Result : T := <expression>;
4458 -- begin
4459 -- <handled_seq_of_stms>
4460 -- return Result;
4461 -- end;
4463 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4465 -- return Result : T := <expression>;
4467 -- to be:
4469 -- return <expression>;
4471 -- unless it's build-in-place or there's no <expression>, in which case
4472 -- we generate:
4474 -- declare
4475 -- Result : T := <expression>;
4476 -- begin
4477 -- return Result;
4478 -- end;
4480 -- Note that this case could have been written by the user as an extended
4481 -- return statement, or could have been transformed to this from a simple
4482 -- return statement.
4484 -- That is, we need to have a reified return object if there are statements
4485 -- (which might refer to it) or if we're doing build-in-place (so we can
4486 -- set its address to the final resting place or if there is no expression
4487 -- (in which case default initial values might need to be set)).
4489 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4490 Loc : constant Source_Ptr := Sloc (N);
4492 function Build_Heap_Or_Pool_Allocator
4493 (Temp_Id : Entity_Id;
4494 Temp_Typ : Entity_Id;
4495 Func_Id : Entity_Id;
4496 Ret_Typ : Entity_Id;
4497 Alloc_Expr : Node_Id) return Node_Id;
4498 -- Create the statements necessary to allocate a return object on the
4499 -- heap or user-defined storage pool. The object may need finalization
4500 -- actions depending on the return type.
4502 -- * Controlled case
4504 -- if BIPfinalizationmaster = null then
4505 -- Temp_Id := <Alloc_Expr>;
4506 -- else
4507 -- declare
4508 -- type Ptr_Typ is access Ret_Typ;
4509 -- for Ptr_Typ'Storage_Pool use
4510 -- Base_Pool (BIPfinalizationmaster.all).all;
4511 -- Local : Ptr_Typ;
4513 -- begin
4514 -- procedure Allocate (...) is
4515 -- begin
4516 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4517 -- end Allocate;
4519 -- Local := <Alloc_Expr>;
4520 -- Temp_Id := Temp_Typ (Local);
4521 -- end;
4522 -- end if;
4524 -- * Non-controlled case
4526 -- Temp_Id := <Alloc_Expr>;
4528 -- Temp_Id is the temporary which is used to reference the internally
4529 -- created object in all allocation forms. Temp_Typ is the type of the
4530 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4531 -- type of Func_Id. Alloc_Expr is the actual allocator.
4533 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4534 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4535 -- with parameters:
4536 -- From current activation chain
4537 -- To activation chain passed in by the caller
4538 -- New_Master master passed in by the caller
4540 -- Func_Id is the entity of the function where the extended return
4541 -- statement appears.
4543 ----------------------------------
4544 -- Build_Heap_Or_Pool_Allocator --
4545 ----------------------------------
4547 function Build_Heap_Or_Pool_Allocator
4548 (Temp_Id : Entity_Id;
4549 Temp_Typ : Entity_Id;
4550 Func_Id : Entity_Id;
4551 Ret_Typ : Entity_Id;
4552 Alloc_Expr : Node_Id) return Node_Id
4554 begin
4555 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4557 -- Processing for objects that require finalization actions
4559 if Needs_Finalization (Ret_Typ) then
4560 declare
4561 Decls : constant List_Id := New_List;
4562 Fin_Mas_Id : constant Entity_Id :=
4563 Build_In_Place_Formal
4564 (Func_Id, BIP_Finalization_Master);
4565 Orig_Expr : constant Node_Id :=
4566 New_Copy_Tree
4567 (Source => Alloc_Expr,
4568 Scopes_In_EWA_OK => True);
4569 Stmts : constant List_Id := New_List;
4570 Desig_Typ : Entity_Id;
4571 Local_Id : Entity_Id;
4572 Pool_Id : Entity_Id;
4573 Ptr_Typ : Entity_Id;
4575 begin
4576 -- Generate:
4577 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4579 Pool_Id := Make_Temporary (Loc, 'P');
4581 Append_To (Decls,
4582 Make_Object_Renaming_Declaration (Loc,
4583 Defining_Identifier => Pool_Id,
4584 Subtype_Mark =>
4585 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4586 Name =>
4587 Make_Explicit_Dereference (Loc,
4588 Prefix =>
4589 Make_Function_Call (Loc,
4590 Name =>
4591 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4592 Parameter_Associations => New_List (
4593 Make_Explicit_Dereference (Loc,
4594 Prefix =>
4595 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4597 -- Create an access type which uses the storage pool of the
4598 -- caller's master. This additional type is necessary because
4599 -- the finalization master cannot be associated with the type
4600 -- of the temporary. Otherwise the secondary stack allocation
4601 -- will fail.
4603 Desig_Typ := Ret_Typ;
4605 -- Ensure that the build-in-place machinery uses a fat pointer
4606 -- when allocating an unconstrained array on the heap. In this
4607 -- case the result object type is a constrained array type even
4608 -- though the function type is unconstrained.
4610 if Ekind (Desig_Typ) = E_Array_Subtype then
4611 Desig_Typ := Base_Type (Desig_Typ);
4612 end if;
4614 -- Generate:
4615 -- type Ptr_Typ is access Desig_Typ;
4617 Ptr_Typ := Make_Temporary (Loc, 'P');
4619 Append_To (Decls,
4620 Make_Full_Type_Declaration (Loc,
4621 Defining_Identifier => Ptr_Typ,
4622 Type_Definition =>
4623 Make_Access_To_Object_Definition (Loc,
4624 Subtype_Indication =>
4625 New_Occurrence_Of (Desig_Typ, Loc))));
4627 -- Perform minor decoration in order to set the master and the
4628 -- storage pool attributes.
4630 Set_Ekind (Ptr_Typ, E_Access_Type);
4631 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4632 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4634 -- Create the temporary, generate:
4635 -- Local_Id : Ptr_Typ;
4637 Local_Id := Make_Temporary (Loc, 'T');
4639 Append_To (Decls,
4640 Make_Object_Declaration (Loc,
4641 Defining_Identifier => Local_Id,
4642 Object_Definition =>
4643 New_Occurrence_Of (Ptr_Typ, Loc)));
4645 -- Allocate the object, generate:
4646 -- Local_Id := <Alloc_Expr>;
4648 Append_To (Stmts,
4649 Make_Assignment_Statement (Loc,
4650 Name => New_Occurrence_Of (Local_Id, Loc),
4651 Expression => Alloc_Expr));
4653 -- Generate:
4654 -- Temp_Id := Temp_Typ (Local_Id);
4656 Append_To (Stmts,
4657 Make_Assignment_Statement (Loc,
4658 Name => New_Occurrence_Of (Temp_Id, Loc),
4659 Expression =>
4660 Unchecked_Convert_To (Temp_Typ,
4661 New_Occurrence_Of (Local_Id, Loc))));
4663 -- Wrap the allocation in a block. This is further conditioned
4664 -- by checking the caller finalization master at runtime. A
4665 -- null value indicates a non-existent master, most likely due
4666 -- to a Finalize_Storage_Only allocation.
4668 -- Generate:
4669 -- if BIPfinalizationmaster = null then
4670 -- Temp_Id := <Orig_Expr>;
4671 -- else
4672 -- declare
4673 -- <Decls>
4674 -- begin
4675 -- <Stmts>
4676 -- end;
4677 -- end if;
4679 return
4680 Make_If_Statement (Loc,
4681 Condition =>
4682 Make_Op_Eq (Loc,
4683 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4684 Right_Opnd => Make_Null (Loc)),
4686 Then_Statements => New_List (
4687 Make_Assignment_Statement (Loc,
4688 Name => New_Occurrence_Of (Temp_Id, Loc),
4689 Expression => Orig_Expr)),
4691 Else_Statements => New_List (
4692 Make_Block_Statement (Loc,
4693 Declarations => Decls,
4694 Handled_Statement_Sequence =>
4695 Make_Handled_Sequence_Of_Statements (Loc,
4696 Statements => Stmts))));
4697 end;
4699 -- For all other cases, generate:
4700 -- Temp_Id := <Alloc_Expr>;
4702 else
4703 return
4704 Make_Assignment_Statement (Loc,
4705 Name => New_Occurrence_Of (Temp_Id, Loc),
4706 Expression => Alloc_Expr);
4707 end if;
4708 end Build_Heap_Or_Pool_Allocator;
4710 ---------------------------
4711 -- Move_Activation_Chain --
4712 ---------------------------
4714 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4715 begin
4716 return
4717 Make_Procedure_Call_Statement (Loc,
4718 Name =>
4719 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4721 Parameter_Associations => New_List (
4723 -- Source chain
4725 Make_Attribute_Reference (Loc,
4726 Prefix => Make_Identifier (Loc, Name_uChain),
4727 Attribute_Name => Name_Unrestricted_Access),
4729 -- Destination chain
4731 New_Occurrence_Of
4732 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4734 -- New master
4736 New_Occurrence_Of
4737 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4738 end Move_Activation_Chain;
4740 -- Local variables
4742 Func_Id : constant Entity_Id :=
4743 Return_Applies_To (Return_Statement_Entity (N));
4744 Is_BIP_Func : constant Boolean :=
4745 Is_Build_In_Place_Function (Func_Id);
4746 Ret_Obj_Id : constant Entity_Id :=
4747 First_Entity (Return_Statement_Entity (N));
4748 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4749 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4751 Exp : Node_Id;
4752 HSS : Node_Id;
4753 Result : Node_Id;
4754 Stmts : List_Id;
4756 Return_Stmt : Node_Id := Empty;
4757 -- Force initialization to facilitate static analysis
4759 -- Start of processing for Expand_N_Extended_Return_Statement
4761 begin
4762 -- Given that functionality of interface thunks is simple (just displace
4763 -- the pointer to the object) they are always handled by means of
4764 -- simple return statements.
4766 pragma Assert (not Is_Thunk (Current_Scope));
4768 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4769 Exp := Expression (Ret_Obj_Decl);
4771 -- Assert that if F says "return R : T := G(...) do..."
4772 -- then F and G are both b-i-p, or neither b-i-p.
4774 if Nkind (Exp) = N_Function_Call then
4775 pragma Assert (Ekind (Current_Scope) = E_Function);
4776 pragma Assert
4777 (Is_Build_In_Place_Function (Current_Scope) =
4778 Is_Build_In_Place_Function_Call (Exp));
4779 null;
4780 end if;
4781 else
4782 Exp := Empty;
4783 end if;
4785 HSS := Handled_Statement_Sequence (N);
4787 -- If the returned object needs finalization actions, the function must
4788 -- perform the appropriate cleanup should it fail to return. The state
4789 -- of the function itself is tracked through a flag which is coupled
4790 -- with the scope finalizer. There is one flag per each return object
4791 -- in case of multiple returns.
4793 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4794 declare
4795 Flag_Decl : Node_Id;
4796 Flag_Id : Entity_Id;
4797 Func_Bod : Node_Id;
4799 begin
4800 -- Recover the function body
4802 Func_Bod := Unit_Declaration_Node (Func_Id);
4804 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4805 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4806 end if;
4808 if Nkind (Func_Bod) = N_Function_Specification then
4809 Func_Bod := Parent (Func_Bod); -- one more level for child units
4810 end if;
4812 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
4814 -- Create a flag to track the function state
4816 Flag_Id := Make_Temporary (Loc, 'F');
4817 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4819 -- Insert the flag at the beginning of the function declarations,
4820 -- generate:
4821 -- Fnn : Boolean := False;
4823 Flag_Decl :=
4824 Make_Object_Declaration (Loc,
4825 Defining_Identifier => Flag_Id,
4826 Object_Definition =>
4827 New_Occurrence_Of (Standard_Boolean, Loc),
4828 Expression =>
4829 New_Occurrence_Of (Standard_False, Loc));
4831 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4832 Analyze (Flag_Decl);
4833 end;
4834 end if;
4836 -- Build a simple_return_statement that returns the return object when
4837 -- there is a statement sequence, or no expression, or the result will
4838 -- be built in place. Note however that we currently do this for all
4839 -- composite cases, even though not all are built in place.
4841 if Present (HSS)
4842 or else Is_Composite_Type (Ret_Typ)
4843 or else No (Exp)
4844 then
4845 if No (HSS) then
4846 Stmts := New_List;
4848 -- If the extended return has a handled statement sequence, then wrap
4849 -- it in a block and use the block as the first statement.
4851 else
4852 Stmts := New_List (
4853 Make_Block_Statement (Loc,
4854 Declarations => New_List,
4855 Handled_Statement_Sequence => HSS));
4856 end if;
4858 -- If the result type contains tasks, we call Move_Activation_Chain.
4859 -- Later, the cleanup code will call Complete_Master, which will
4860 -- terminate any unactivated tasks belonging to the return statement
4861 -- master. But Move_Activation_Chain updates their master to be that
4862 -- of the caller, so they will not be terminated unless the return
4863 -- statement completes unsuccessfully due to exception, abort, goto,
4864 -- or exit. As a formality, we test whether the function requires the
4865 -- result to be built in place, though that's necessarily true for
4866 -- the case of result types with task parts.
4868 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4870 -- The return expression is an aggregate for a complex type which
4871 -- contains tasks. This particular case is left unexpanded since
4872 -- the regular expansion would insert all temporaries and
4873 -- initialization code in the wrong block.
4875 if Nkind (Exp) = N_Aggregate then
4876 Expand_N_Aggregate (Exp);
4877 end if;
4879 -- Do not move the activation chain if the return object does not
4880 -- contain tasks.
4882 if Has_Task (Etype (Ret_Obj_Id)) then
4883 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4884 end if;
4885 end if;
4887 -- Update the state of the function right before the object is
4888 -- returned.
4890 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4891 declare
4892 Flag_Id : constant Entity_Id :=
4893 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4895 begin
4896 -- Generate:
4897 -- Fnn := True;
4899 Append_To (Stmts,
4900 Make_Assignment_Statement (Loc,
4901 Name => New_Occurrence_Of (Flag_Id, Loc),
4902 Expression => New_Occurrence_Of (Standard_True, Loc)));
4903 end;
4904 end if;
4906 -- Build a simple_return_statement that returns the return object
4908 Return_Stmt :=
4909 Make_Simple_Return_Statement (Loc,
4910 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4911 Append_To (Stmts, Return_Stmt);
4913 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4914 end if;
4916 -- Case where we build a return statement block
4918 if Present (HSS) then
4919 Result :=
4920 Make_Block_Statement (Loc,
4921 Declarations => Return_Object_Declarations (N),
4922 Handled_Statement_Sequence => HSS);
4924 -- We set the entity of the new block statement to be that of the
4925 -- return statement. This is necessary so that various fields, such
4926 -- as Finalization_Chain_Entity carry over from the return statement
4927 -- to the block. Note that this block is unusual, in that its entity
4928 -- is an E_Return_Statement rather than an E_Block.
4930 Set_Identifier
4931 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4933 -- If the object decl was already rewritten as a renaming, then we
4934 -- don't want to do the object allocation and transformation of
4935 -- the return object declaration to a renaming. This case occurs
4936 -- when the return object is initialized by a call to another
4937 -- build-in-place function, and that function is responsible for
4938 -- the allocation of the return object.
4940 if Is_BIP_Func
4941 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4942 then
4943 pragma Assert
4944 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4945 and then
4947 -- It is a regular BIP object declaration
4949 (Is_Build_In_Place_Function_Call
4950 (Expression (Original_Node (Ret_Obj_Decl)))
4952 -- It is a BIP object declaration that displaces the pointer
4953 -- to the object to reference a convered interface type.
4955 or else
4956 Present (Unqual_BIP_Iface_Function_Call
4957 (Expression (Original_Node (Ret_Obj_Decl))))));
4959 -- Return the build-in-place result by reference
4961 Set_By_Ref (Return_Stmt);
4963 elsif Is_BIP_Func then
4965 -- Locate the implicit access parameter associated with the
4966 -- caller-supplied return object and convert the return
4967 -- statement's return object declaration to a renaming of a
4968 -- dereference of the access parameter. If the return object's
4969 -- declaration includes an expression that has not already been
4970 -- expanded as separate assignments, then add an assignment
4971 -- statement to ensure the return object gets initialized.
4973 -- declare
4974 -- Result : T [:= <expression>];
4975 -- begin
4976 -- ...
4978 -- is converted to
4980 -- declare
4981 -- Result : T renames FuncRA.all;
4982 -- [Result := <expression;]
4983 -- begin
4984 -- ...
4986 declare
4987 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4988 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4990 Init_Assignment : Node_Id := Empty;
4991 Obj_Acc_Formal : Entity_Id;
4992 Obj_Acc_Deref : Node_Id;
4993 Obj_Alloc_Formal : Entity_Id;
4995 begin
4996 -- Build-in-place results must be returned by reference
4998 Set_By_Ref (Return_Stmt);
5000 -- Retrieve the implicit access parameter passed by the caller
5002 Obj_Acc_Formal :=
5003 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
5005 -- If the return object's declaration includes an expression
5006 -- and the declaration isn't marked as No_Initialization, then
5007 -- we need to generate an assignment to the object and insert
5008 -- it after the declaration before rewriting it as a renaming
5009 -- (otherwise we'll lose the initialization). The case where
5010 -- the result type is an interface (or class-wide interface)
5011 -- is also excluded because the context of the function call
5012 -- must be unconstrained, so the initialization will always
5013 -- be done as part of an allocator evaluation (storage pool
5014 -- or secondary stack), never to a constrained target object
5015 -- passed in by the caller. Besides the assignment being
5016 -- unneeded in this case, it avoids problems with trying to
5017 -- generate a dispatching assignment when the return expression
5018 -- is a nonlimited descendant of a limited interface (the
5019 -- interface has no assignment operation).
5021 if Present (Ret_Obj_Expr)
5022 and then not No_Initialization (Ret_Obj_Decl)
5023 and then not Is_Interface (Ret_Obj_Typ)
5024 then
5025 Init_Assignment :=
5026 Make_Assignment_Statement (Loc,
5027 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
5028 Expression =>
5029 New_Copy_Tree
5030 (Source => Ret_Obj_Expr,
5031 Scopes_In_EWA_OK => True));
5033 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5034 Set_Assignment_OK (Name (Init_Assignment));
5035 Set_No_Ctrl_Actions (Init_Assignment);
5037 Set_Parent (Name (Init_Assignment), Init_Assignment);
5038 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5040 Set_Expression (Ret_Obj_Decl, Empty);
5042 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
5043 and then not Is_Class_Wide_Type
5044 (Etype (Expression (Init_Assignment)))
5045 then
5046 Rewrite (Expression (Init_Assignment),
5047 Make_Type_Conversion (Loc,
5048 Subtype_Mark =>
5049 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
5050 Expression =>
5051 Relocate_Node (Expression (Init_Assignment))));
5052 end if;
5054 -- In the case of functions where the calling context can
5055 -- determine the form of allocation needed, initialization
5056 -- is done with each part of the if statement that handles
5057 -- the different forms of allocation (this is true for
5058 -- unconstrained, tagged, and controlled result subtypes).
5060 if not Needs_BIP_Alloc_Form (Func_Id) then
5061 Insert_After (Ret_Obj_Decl, Init_Assignment);
5062 end if;
5063 end if;
5065 -- When the function's subtype is unconstrained, a run-time
5066 -- test is needed to determine the form of allocation to use
5067 -- for the return object. The function has an implicit formal
5068 -- parameter indicating this. If the BIP_Alloc_Form formal has
5069 -- the value one, then the caller has passed access to an
5070 -- existing object for use as the return object. If the value
5071 -- is two, then the return object must be allocated on the
5072 -- secondary stack. Otherwise, the object must be allocated in
5073 -- a storage pool. We generate an if statement to test the
5074 -- implicit allocation formal and initialize a local access
5075 -- value appropriately, creating allocators in the secondary
5076 -- stack and global heap cases. The special formal also exists
5077 -- and must be tested when the function has a tagged result,
5078 -- even when the result subtype is constrained, because in
5079 -- general such functions can be called in dispatching contexts
5080 -- and must be handled similarly to functions with a class-wide
5081 -- result.
5083 if Needs_BIP_Alloc_Form (Func_Id) then
5084 Obj_Alloc_Formal :=
5085 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5087 declare
5088 Pool_Id : constant Entity_Id :=
5089 Make_Temporary (Loc, 'P');
5090 Alloc_Obj_Id : Entity_Id;
5091 Alloc_Obj_Decl : Node_Id;
5092 Alloc_If_Stmt : Node_Id;
5093 Heap_Allocator : Node_Id;
5094 Pool_Decl : Node_Id;
5095 Pool_Allocator : Node_Id;
5096 Ptr_Type_Decl : Node_Id;
5097 Ref_Type : Entity_Id;
5098 SS_Allocator : Node_Id;
5100 begin
5101 -- Reuse the itype created for the function's implicit
5102 -- access formal. This avoids the need to create a new
5103 -- access type here, plus it allows assigning the access
5104 -- formal directly without applying a conversion.
5106 -- Ref_Type := Etype (Object_Access);
5108 -- Create an access type designating the function's
5109 -- result subtype.
5111 Ref_Type := Make_Temporary (Loc, 'A');
5113 Ptr_Type_Decl :=
5114 Make_Full_Type_Declaration (Loc,
5115 Defining_Identifier => Ref_Type,
5116 Type_Definition =>
5117 Make_Access_To_Object_Definition (Loc,
5118 All_Present => True,
5119 Subtype_Indication =>
5120 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5122 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5124 -- Create an access object that will be initialized to an
5125 -- access value denoting the return object, either coming
5126 -- from an implicit access value passed in by the caller
5127 -- or from the result of an allocator.
5129 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5130 Set_Etype (Alloc_Obj_Id, Ref_Type);
5132 Alloc_Obj_Decl :=
5133 Make_Object_Declaration (Loc,
5134 Defining_Identifier => Alloc_Obj_Id,
5135 Object_Definition =>
5136 New_Occurrence_Of (Ref_Type, Loc));
5138 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5140 -- Create allocators for both the secondary stack and
5141 -- global heap. If there's an initialization expression,
5142 -- then create these as initialized allocators.
5144 if Present (Ret_Obj_Expr)
5145 and then not No_Initialization (Ret_Obj_Decl)
5146 then
5147 -- Always use the type of the expression for the
5148 -- qualified expression, rather than the result type.
5149 -- In general we cannot always use the result type
5150 -- for the allocator, because the expression might be
5151 -- of a specific type, such as in the case of an
5152 -- aggregate or even a nonlimited object when the
5153 -- result type is a limited class-wide interface type.
5155 Heap_Allocator :=
5156 Make_Allocator (Loc,
5157 Expression =>
5158 Make_Qualified_Expression (Loc,
5159 Subtype_Mark =>
5160 New_Occurrence_Of
5161 (Etype (Ret_Obj_Expr), Loc),
5162 Expression =>
5163 New_Copy_Tree
5164 (Source => Ret_Obj_Expr,
5165 Scopes_In_EWA_OK => True)));
5167 else
5168 -- If the function returns a class-wide type we cannot
5169 -- use the return type for the allocator. Instead we
5170 -- use the type of the expression, which must be an
5171 -- aggregate of a definite type.
5173 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5174 Heap_Allocator :=
5175 Make_Allocator (Loc,
5176 Expression =>
5177 New_Occurrence_Of
5178 (Etype (Ret_Obj_Expr), Loc));
5179 else
5180 Heap_Allocator :=
5181 Make_Allocator (Loc,
5182 Expression =>
5183 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5184 end if;
5186 -- If the object requires default initialization then
5187 -- that will happen later following the elaboration of
5188 -- the object renaming. If we don't turn it off here
5189 -- then the object will be default initialized twice.
5191 Set_No_Initialization (Heap_Allocator);
5192 end if;
5194 -- Set the flag indicating that the allocator came from
5195 -- a build-in-place return statement, so we can avoid
5196 -- adjusting the allocated object. Note that this flag
5197 -- will be inherited by the copies made below.
5199 Set_Alloc_For_BIP_Return (Heap_Allocator);
5201 -- The Pool_Allocator is just like the Heap_Allocator,
5202 -- except we set Storage_Pool and Procedure_To_Call so
5203 -- it will use the user-defined storage pool.
5205 Pool_Allocator :=
5206 New_Copy_Tree
5207 (Source => Heap_Allocator,
5208 Scopes_In_EWA_OK => True);
5210 pragma Assert (Alloc_For_BIP_Return (Pool_Allocator));
5212 -- Do not generate the renaming of the build-in-place
5213 -- pool parameter on ZFP because the parameter is not
5214 -- created in the first place.
5216 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5217 Pool_Decl :=
5218 Make_Object_Renaming_Declaration (Loc,
5219 Defining_Identifier => Pool_Id,
5220 Subtype_Mark =>
5221 New_Occurrence_Of
5222 (RTE (RE_Root_Storage_Pool), Loc),
5223 Name =>
5224 Make_Explicit_Dereference (Loc,
5225 New_Occurrence_Of
5226 (Build_In_Place_Formal
5227 (Func_Id, BIP_Storage_Pool), Loc)));
5228 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5229 Set_Procedure_To_Call
5230 (Pool_Allocator, RTE (RE_Allocate_Any));
5231 else
5232 Pool_Decl := Make_Null_Statement (Loc);
5233 end if;
5235 -- If the No_Allocators restriction is active, then only
5236 -- an allocator for secondary stack allocation is needed.
5237 -- It's OK for such allocators to have Comes_From_Source
5238 -- set to False, because gigi knows not to flag them as
5239 -- being a violation of No_Implicit_Heap_Allocations.
5241 if Restriction_Active (No_Allocators) then
5242 SS_Allocator := Heap_Allocator;
5243 Heap_Allocator := Make_Null (Loc);
5244 Pool_Allocator := Make_Null (Loc);
5246 -- Otherwise the heap and pool allocators may be needed,
5247 -- so we make another allocator for secondary stack
5248 -- allocation.
5250 else
5251 SS_Allocator :=
5252 New_Copy_Tree
5253 (Source => Heap_Allocator,
5254 Scopes_In_EWA_OK => True);
5256 pragma Assert (Alloc_For_BIP_Return (SS_Allocator));
5258 -- The heap and pool allocators are marked as
5259 -- Comes_From_Source since they correspond to an
5260 -- explicit user-written allocator (that is, it will
5261 -- only be executed on behalf of callers that call the
5262 -- function as initialization for such an allocator).
5263 -- Prevents errors when No_Implicit_Heap_Allocations
5264 -- is in force.
5266 Set_Comes_From_Source (Heap_Allocator, True);
5267 Set_Comes_From_Source (Pool_Allocator, True);
5268 end if;
5270 -- The allocator is returned on the secondary stack.
5272 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5273 Set_Procedure_To_Call
5274 (SS_Allocator, RTE (RE_SS_Allocate));
5276 -- The allocator is returned on the secondary stack,
5277 -- so indicate that the function return, as well as
5278 -- all blocks that encloses the allocator, must not
5279 -- release it. The flags must be set now because
5280 -- the decision to use the secondary stack is done
5281 -- very late in the course of expanding the return
5282 -- statement, past the point where these flags are
5283 -- normally set.
5285 Set_Uses_Sec_Stack (Func_Id);
5286 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5287 Set_Sec_Stack_Needed_For_Return
5288 (Return_Statement_Entity (N));
5289 Set_Enclosing_Sec_Stack_Return (N);
5291 -- Create an if statement to test the BIP_Alloc_Form
5292 -- formal and initialize the access object to either the
5293 -- BIP_Object_Access formal (BIP_Alloc_Form =
5294 -- Caller_Allocation), the result of allocating the
5295 -- object in the secondary stack (BIP_Alloc_Form =
5296 -- Secondary_Stack), or else an allocator to create the
5297 -- return object in the heap or user-defined pool
5298 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5300 -- ??? An unchecked type conversion must be made in the
5301 -- case of assigning the access object formal to the
5302 -- local access object, because a normal conversion would
5303 -- be illegal in some cases (such as converting access-
5304 -- to-unconstrained to access-to-constrained), but the
5305 -- the unchecked conversion will presumably fail to work
5306 -- right in just such cases. It's not clear at all how to
5307 -- handle this. ???
5309 Alloc_If_Stmt :=
5310 Make_If_Statement (Loc,
5311 Condition =>
5312 Make_Op_Eq (Loc,
5313 Left_Opnd =>
5314 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5315 Right_Opnd =>
5316 Make_Integer_Literal (Loc,
5317 UI_From_Int (BIP_Allocation_Form'Pos
5318 (Caller_Allocation)))),
5320 Then_Statements => New_List (
5321 Make_Assignment_Statement (Loc,
5322 Name =>
5323 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5324 Expression =>
5325 Make_Unchecked_Type_Conversion (Loc,
5326 Subtype_Mark =>
5327 New_Occurrence_Of (Ref_Type, Loc),
5328 Expression =>
5329 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5331 Elsif_Parts => New_List (
5332 Make_Elsif_Part (Loc,
5333 Condition =>
5334 Make_Op_Eq (Loc,
5335 Left_Opnd =>
5336 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5337 Right_Opnd =>
5338 Make_Integer_Literal (Loc,
5339 UI_From_Int (BIP_Allocation_Form'Pos
5340 (Secondary_Stack)))),
5342 Then_Statements => New_List (
5343 Make_Assignment_Statement (Loc,
5344 Name =>
5345 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5346 Expression => SS_Allocator))),
5348 Make_Elsif_Part (Loc,
5349 Condition =>
5350 Make_Op_Eq (Loc,
5351 Left_Opnd =>
5352 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5353 Right_Opnd =>
5354 Make_Integer_Literal (Loc,
5355 UI_From_Int (BIP_Allocation_Form'Pos
5356 (Global_Heap)))),
5358 Then_Statements => New_List (
5359 Build_Heap_Or_Pool_Allocator
5360 (Temp_Id => Alloc_Obj_Id,
5361 Temp_Typ => Ref_Type,
5362 Func_Id => Func_Id,
5363 Ret_Typ => Ret_Obj_Typ,
5364 Alloc_Expr => Heap_Allocator))),
5366 -- ???If all is well, we can put the following
5367 -- 'elsif' in the 'else', but this is a useful
5368 -- self-check in case caller and callee don't agree
5369 -- on whether BIPAlloc and so on should be passed.
5371 Make_Elsif_Part (Loc,
5372 Condition =>
5373 Make_Op_Eq (Loc,
5374 Left_Opnd =>
5375 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5376 Right_Opnd =>
5377 Make_Integer_Literal (Loc,
5378 UI_From_Int (BIP_Allocation_Form'Pos
5379 (User_Storage_Pool)))),
5381 Then_Statements => New_List (
5382 Pool_Decl,
5383 Build_Heap_Or_Pool_Allocator
5384 (Temp_Id => Alloc_Obj_Id,
5385 Temp_Typ => Ref_Type,
5386 Func_Id => Func_Id,
5387 Ret_Typ => Ret_Obj_Typ,
5388 Alloc_Expr => Pool_Allocator)))),
5390 -- Raise Program_Error if it's none of the above;
5391 -- this is a compiler bug.
5393 Else_Statements => New_List (
5394 Make_Raise_Program_Error (Loc,
5395 Reason => PE_Build_In_Place_Mismatch)));
5397 -- If a separate initialization assignment was created
5398 -- earlier, append that following the assignment of the
5399 -- implicit access formal to the access object, to ensure
5400 -- that the return object is initialized in that case. In
5401 -- this situation, the target of the assignment must be
5402 -- rewritten to denote a dereference of the access to the
5403 -- return object passed in by the caller.
5405 if Present (Init_Assignment) then
5406 Rewrite (Name (Init_Assignment),
5407 Make_Explicit_Dereference (Loc,
5408 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5409 pragma Assert
5410 (Assignment_OK
5411 (Original_Node (Name (Init_Assignment))));
5412 Set_Assignment_OK (Name (Init_Assignment));
5414 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5416 Append_To
5417 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5418 end if;
5420 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5422 -- Remember the local access object for use in the
5423 -- dereference of the renaming created below.
5425 Obj_Acc_Formal := Alloc_Obj_Id;
5426 end;
5427 end if;
5429 -- Replace the return object declaration with a renaming of a
5430 -- dereference of the access value designating the return
5431 -- object.
5433 Obj_Acc_Deref :=
5434 Make_Explicit_Dereference (Loc,
5435 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5437 Rewrite (Ret_Obj_Decl,
5438 Make_Object_Renaming_Declaration (Loc,
5439 Defining_Identifier => Ret_Obj_Id,
5440 Access_Definition => Empty,
5441 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5442 Name => Obj_Acc_Deref));
5444 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5445 end;
5446 end if;
5448 -- Case where we do not build a block
5450 else
5451 -- We're about to drop Return_Object_Declarations on the floor, so
5452 -- we need to insert it, in case it got expanded into useful code.
5453 -- Remove side effects from expression, which may be duplicated in
5454 -- subsequent checks (see Expand_Simple_Function_Return).
5456 Insert_List_Before (N, Return_Object_Declarations (N));
5457 Remove_Side_Effects (Exp);
5459 -- Build simple_return_statement that returns the expression directly
5461 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5462 Result := Return_Stmt;
5463 end if;
5465 -- Set the flag to prevent infinite recursion
5467 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5469 Rewrite (N, Result);
5470 Analyze (N);
5471 end Expand_N_Extended_Return_Statement;
5473 ----------------------------
5474 -- Expand_N_Function_Call --
5475 ----------------------------
5477 procedure Expand_N_Function_Call (N : Node_Id) is
5478 begin
5479 Expand_Call (N);
5480 end Expand_N_Function_Call;
5482 ---------------------------------------
5483 -- Expand_N_Procedure_Call_Statement --
5484 ---------------------------------------
5486 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5487 begin
5488 Expand_Call (N);
5489 end Expand_N_Procedure_Call_Statement;
5491 --------------------------------------
5492 -- Expand_N_Simple_Return_Statement --
5493 --------------------------------------
5495 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5496 begin
5497 -- Defend against previous errors (i.e. the return statement calls a
5498 -- function that is not available in configurable runtime).
5500 if Present (Expression (N))
5501 and then Nkind (Expression (N)) = N_Empty
5502 then
5503 Check_Error_Detected;
5504 return;
5505 end if;
5507 -- Distinguish the function and non-function cases:
5509 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5510 when E_Function
5511 | E_Generic_Function
5513 Expand_Simple_Function_Return (N);
5515 when E_Entry
5516 | E_Entry_Family
5517 | E_Generic_Procedure
5518 | E_Procedure
5519 | E_Return_Statement
5521 Expand_Non_Function_Return (N);
5523 when others =>
5524 raise Program_Error;
5525 end case;
5527 exception
5528 when RE_Not_Available =>
5529 return;
5530 end Expand_N_Simple_Return_Statement;
5532 ------------------------------
5533 -- Expand_N_Subprogram_Body --
5534 ------------------------------
5536 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5537 -- by the back-end.
5539 -- Add dummy push/pop label nodes at start and end to clear any local
5540 -- exception indications if local-exception-to-goto optimization is active.
5542 -- Add return statement if last statement in body is not a return statement
5543 -- (this makes things easier on Gigi which does not want to have to handle
5544 -- a missing return).
5546 -- Add call to Activate_Tasks if body is a task activator
5548 -- Deal with possible detection of infinite recursion
5550 -- Eliminate body completely if convention stubbed
5552 -- Encode entity names within body, since we will not need to reference
5553 -- these entities any longer in the front end.
5555 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5557 -- Reset Pure indication if any parameter has root type System.Address
5558 -- or has any parameters of limited types, where limited means that the
5559 -- run-time view is limited (i.e. the full type is limited).
5561 -- Wrap thread body
5563 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5564 Body_Id : constant Entity_Id := Defining_Entity (N);
5565 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5566 Loc : constant Source_Ptr := Sloc (N);
5568 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5569 -- Append a return statement to the statement sequence Stmts if the last
5570 -- statement is not already a return or a goto statement. Note that the
5571 -- latter test is not critical, it does not matter if we add a few extra
5572 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5573 -- the corresponding spec of the subprogram body.
5575 ----------------
5576 -- Add_Return --
5577 ----------------
5579 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5580 Last_Stmt : Node_Id;
5581 Loc : Source_Ptr;
5582 Stmt : Node_Id;
5584 begin
5585 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5586 -- not relevant in this context since they are not executable.
5588 Last_Stmt := Last (Stmts);
5589 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5590 Prev (Last_Stmt);
5591 end loop;
5593 -- Now insert return unless last statement is a transfer
5595 if not Is_Transfer (Last_Stmt) then
5597 -- The source location for the return is the end label of the
5598 -- procedure if present. Otherwise use the sloc of the last
5599 -- statement in the list. If the list comes from a generated
5600 -- exception handler and we are not debugging generated code,
5601 -- all the statements within the handler are made invisible
5602 -- to the debugger.
5604 if Nkind (Parent (Stmts)) = N_Exception_Handler
5605 and then not Comes_From_Source (Parent (Stmts))
5606 then
5607 Loc := Sloc (Last_Stmt);
5608 elsif Present (End_Label (HSS)) then
5609 Loc := Sloc (End_Label (HSS));
5610 else
5611 Loc := Sloc (Last_Stmt);
5612 end if;
5614 -- Append return statement, and set analyzed manually. We can't
5615 -- call Analyze on this return since the scope is wrong.
5617 -- Note: it almost works to push the scope and then do the Analyze
5618 -- call, but something goes wrong in some weird cases and it is
5619 -- not worth worrying about ???
5621 Stmt := Make_Simple_Return_Statement (Loc);
5623 -- The return statement is handled properly, and the call to the
5624 -- postcondition, inserted below, does not require information
5625 -- from the body either. However, that call is analyzed in the
5626 -- enclosing scope, and an elaboration check might improperly be
5627 -- added to it. A guard in Sem_Elab is needed to prevent that
5628 -- spurious check, see Check_Elab_Call.
5630 Append_To (Stmts, Stmt);
5631 Set_Analyzed (Stmt);
5633 -- Call the _Postconditions procedure if the related subprogram
5634 -- has contract assertions that need to be verified on exit.
5636 if Ekind (Spec_Id) = E_Procedure
5637 and then Present (Postconditions_Proc (Spec_Id))
5638 then
5639 Insert_Action (Stmt,
5640 Make_Procedure_Call_Statement (Loc,
5641 Name =>
5642 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5643 end if;
5644 end if;
5645 end Add_Return;
5647 -- Local variables
5649 Except_H : Node_Id;
5650 L : List_Id;
5651 Spec_Id : Entity_Id;
5653 -- Start of processing for Expand_N_Subprogram_Body
5655 begin
5656 if Present (Corresponding_Spec (N)) then
5657 Spec_Id := Corresponding_Spec (N);
5658 else
5659 Spec_Id := Body_Id;
5660 end if;
5662 -- If this is a Pure function which has any parameters whose root type
5663 -- is System.Address, reset the Pure indication.
5664 -- This check is also performed when the subprogram is frozen, but we
5665 -- repeat it on the body so that the indication is consistent, and so
5666 -- it applies as well to bodies without separate specifications.
5668 if Is_Pure (Spec_Id)
5669 and then Is_Subprogram (Spec_Id)
5670 and then not Has_Pragma_Pure_Function (Spec_Id)
5671 then
5672 Check_Function_With_Address_Parameter (Spec_Id);
5674 if Spec_Id /= Body_Id then
5675 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5676 end if;
5677 end if;
5679 -- Set L to either the list of declarations if present, or to the list
5680 -- of statements if no declarations are present. This is used to insert
5681 -- new stuff at the start.
5683 if Is_Non_Empty_List (Declarations (N)) then
5684 L := Declarations (N);
5685 else
5686 L := Statements (HSS);
5687 end if;
5689 -- If local-exception-to-goto optimization active, insert dummy push
5690 -- statements at start, and dummy pop statements at end, but inhibit
5691 -- this if we have No_Exception_Handlers, since they are useless and
5692 -- interfere with analysis, e.g. by CodePeer. We also don't need these
5693 -- if we're unnesting subprograms because the only purpose of these
5694 -- nodes is to ensure we don't set a label in one subprogram and branch
5695 -- to it in another.
5697 if (Debug_Flag_Dot_G
5698 or else Restriction_Active (No_Exception_Propagation))
5699 and then not Restriction_Active (No_Exception_Handlers)
5700 and then not CodePeer_Mode
5701 and then not Unnest_Subprogram_Mode
5702 and then Is_Non_Empty_List (L)
5703 then
5704 declare
5705 FS : constant Node_Id := First (L);
5706 FL : constant Source_Ptr := Sloc (FS);
5707 LS : Node_Id;
5708 LL : Source_Ptr;
5710 begin
5711 -- LS points to either last statement, if statements are present
5712 -- or to the last declaration if there are no statements present.
5713 -- It is the node after which the pop's are generated.
5715 if Is_Non_Empty_List (Statements (HSS)) then
5716 LS := Last (Statements (HSS));
5717 else
5718 LS := Last (L);
5719 end if;
5721 LL := Sloc (LS);
5723 Insert_List_Before_And_Analyze (FS, New_List (
5724 Make_Push_Constraint_Error_Label (FL),
5725 Make_Push_Program_Error_Label (FL),
5726 Make_Push_Storage_Error_Label (FL)));
5728 Insert_List_After_And_Analyze (LS, New_List (
5729 Make_Pop_Constraint_Error_Label (LL),
5730 Make_Pop_Program_Error_Label (LL),
5731 Make_Pop_Storage_Error_Label (LL)));
5732 end;
5733 end if;
5735 -- Need poll on entry to subprogram if polling enabled. We only do this
5736 -- for non-empty subprograms, since it does not seem necessary to poll
5737 -- for a dummy null subprogram.
5739 if Is_Non_Empty_List (L) then
5741 -- Do not add a polling call if the subprogram is to be inlined by
5742 -- the back-end, to avoid repeated calls with multiple inlinings.
5744 if Is_Inlined (Spec_Id)
5745 and then Front_End_Inlining
5746 and then Optimization_Level > 1
5747 then
5748 null;
5749 else
5750 Generate_Poll_Call (First (L));
5751 end if;
5752 end if;
5754 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5756 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5757 declare
5758 F : Entity_Id;
5759 A : Node_Id;
5761 begin
5762 -- Loop through formals
5764 F := First_Formal (Spec_Id);
5765 while Present (F) loop
5766 if Is_Scalar_Type (Etype (F))
5767 and then Ekind (F) = E_Out_Parameter
5768 then
5769 Check_Restriction (No_Default_Initialization, F);
5771 -- Insert the initialization. We turn off validity checks
5772 -- for this assignment, since we do not want any check on
5773 -- the initial value itself (which may well be invalid).
5774 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5776 A :=
5777 Make_Assignment_Statement (Loc,
5778 Name => New_Occurrence_Of (F, Loc),
5779 Expression => Get_Simple_Init_Val (Etype (F), N));
5780 Set_Suppress_Assignment_Checks (A);
5782 Insert_Before_And_Analyze (First (L),
5783 A, Suppress => Validity_Check);
5784 end if;
5786 Next_Formal (F);
5787 end loop;
5788 end;
5789 end if;
5791 -- Clear out statement list for stubbed procedure
5793 if Present (Corresponding_Spec (N)) then
5794 Set_Elaboration_Flag (N, Spec_Id);
5796 if Convention (Spec_Id) = Convention_Stubbed
5797 or else Is_Eliminated (Spec_Id)
5798 then
5799 Set_Declarations (N, Empty_List);
5800 Set_Handled_Statement_Sequence (N,
5801 Make_Handled_Sequence_Of_Statements (Loc,
5802 Statements => New_List (Make_Null_Statement (Loc))));
5804 return;
5805 end if;
5806 end if;
5808 -- Create a set of discriminals for the next protected subprogram body
5810 if Is_List_Member (N)
5811 and then Present (Parent (List_Containing (N)))
5812 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5813 and then Present (Next_Protected_Operation (N))
5814 then
5815 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5816 end if;
5818 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5819 -- subprograms with no specs are not frozen.
5821 declare
5822 Typ : constant Entity_Id := Etype (Spec_Id);
5823 Utyp : constant Entity_Id := Underlying_Type (Typ);
5825 begin
5826 if Is_Limited_View (Typ) then
5827 Set_Returns_By_Ref (Spec_Id);
5829 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5830 Set_Returns_By_Ref (Spec_Id);
5831 end if;
5832 end;
5834 -- For a procedure, we add a return for all possible syntactic ends of
5835 -- the subprogram.
5837 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5838 Add_Return (Spec_Id, Statements (HSS));
5840 if Present (Exception_Handlers (HSS)) then
5841 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5842 while Present (Except_H) loop
5843 Add_Return (Spec_Id, Statements (Except_H));
5844 Next_Non_Pragma (Except_H);
5845 end loop;
5846 end if;
5848 -- For a function, we must deal with the case where there is at least
5849 -- one missing return. What we do is to wrap the entire body of the
5850 -- function in a block:
5852 -- begin
5853 -- ...
5854 -- end;
5856 -- becomes
5858 -- begin
5859 -- begin
5860 -- ...
5861 -- end;
5863 -- raise Program_Error;
5864 -- end;
5866 -- This approach is necessary because the raise must be signalled to the
5867 -- caller, not handled by any local handler (RM 6.4(11)).
5869 -- Note: we do not need to analyze the constructed sequence here, since
5870 -- it has no handler, and an attempt to analyze the handled statement
5871 -- sequence twice is risky in various ways (e.g. the issue of expanding
5872 -- cleanup actions twice).
5874 elsif Has_Missing_Return (Spec_Id) then
5875 declare
5876 Hloc : constant Source_Ptr := Sloc (HSS);
5877 Blok : constant Node_Id :=
5878 Make_Block_Statement (Hloc,
5879 Handled_Statement_Sequence => HSS);
5880 Rais : constant Node_Id :=
5881 Make_Raise_Program_Error (Hloc,
5882 Reason => PE_Missing_Return);
5884 begin
5885 Set_Handled_Statement_Sequence (N,
5886 Make_Handled_Sequence_Of_Statements (Hloc,
5887 Statements => New_List (Blok, Rais)));
5889 Push_Scope (Spec_Id);
5890 Analyze (Blok);
5891 Analyze (Rais);
5892 Pop_Scope;
5893 end;
5894 end if;
5896 -- If subprogram contains a parameterless recursive call, then we may
5897 -- have an infinite recursion, so see if we can generate code to check
5898 -- for this possibility if storage checks are not suppressed.
5900 if Ekind (Spec_Id) = E_Procedure
5901 and then Has_Recursive_Call (Spec_Id)
5902 and then not Storage_Checks_Suppressed (Spec_Id)
5903 then
5904 Detect_Infinite_Recursion (N, Spec_Id);
5905 end if;
5907 -- Set to encode entity names in package body before gigi is called
5909 Qualify_Entity_Names (N);
5911 -- If the body belongs to a nonabstract library-level source primitive
5912 -- of a tagged type, install an elaboration check which ensures that a
5913 -- dispatching call targeting the primitive will not execute the body
5914 -- without it being previously elaborated.
5916 Install_Primitive_Elaboration_Check (N);
5917 end Expand_N_Subprogram_Body;
5919 -----------------------------------
5920 -- Expand_N_Subprogram_Body_Stub --
5921 -----------------------------------
5923 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5924 Bod : Node_Id;
5926 begin
5927 if Present (Corresponding_Body (N)) then
5928 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5930 -- The body may have been expanded already when it is analyzed
5931 -- through the subunit node. Do no expand again: it interferes
5932 -- with the construction of unnesting tables when generating C.
5934 if not Analyzed (Bod) then
5935 Expand_N_Subprogram_Body (Bod);
5936 end if;
5938 -- Add full qualification to entities that may be created late
5939 -- during unnesting.
5941 Qualify_Entity_Names (N);
5942 end if;
5943 end Expand_N_Subprogram_Body_Stub;
5945 -------------------------------------
5946 -- Expand_N_Subprogram_Declaration --
5947 -------------------------------------
5949 -- If the declaration appears within a protected body, it is a private
5950 -- operation of the protected type. We must create the corresponding
5951 -- protected subprogram an associated formals. For a normal protected
5952 -- operation, this is done when expanding the protected type declaration.
5954 -- If the declaration is for a null procedure, emit null body
5956 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5957 Loc : constant Source_Ptr := Sloc (N);
5958 Subp : constant Entity_Id := Defining_Entity (N);
5960 -- Local variables
5962 Scop : constant Entity_Id := Scope (Subp);
5963 Prot_Bod : Node_Id;
5964 Prot_Decl : Node_Id;
5965 Prot_Id : Entity_Id;
5967 -- Start of processing for Expand_N_Subprogram_Declaration
5969 begin
5970 -- In SPARK, subprogram declarations are only allowed in package
5971 -- specifications.
5973 if Nkind (Parent (N)) /= N_Package_Specification then
5974 if Nkind (Parent (N)) = N_Compilation_Unit then
5975 Check_SPARK_05_Restriction
5976 ("subprogram declaration is not a library item", N);
5978 elsif Present (Next (N))
5979 and then Nkind (Next (N)) = N_Pragma
5980 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5981 then
5982 -- In SPARK, subprogram declarations are also permitted in
5983 -- declarative parts when immediately followed by a corresponding
5984 -- pragma Import. We only check here that there is some pragma
5985 -- Import.
5987 null;
5988 else
5989 Check_SPARK_05_Restriction
5990 ("subprogram declaration is not allowed here", N);
5991 end if;
5992 end if;
5994 -- Deal with case of protected subprogram. Do not generate protected
5995 -- operation if operation is flagged as eliminated.
5997 if Is_List_Member (N)
5998 and then Present (Parent (List_Containing (N)))
5999 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6000 and then Is_Protected_Type (Scop)
6001 then
6002 if No (Protected_Body_Subprogram (Subp))
6003 and then not Is_Eliminated (Subp)
6004 then
6005 Prot_Decl :=
6006 Make_Subprogram_Declaration (Loc,
6007 Specification =>
6008 Build_Protected_Sub_Specification
6009 (N, Scop, Unprotected_Mode));
6011 -- The protected subprogram is declared outside of the protected
6012 -- body. Given that the body has frozen all entities so far, we
6013 -- analyze the subprogram and perform freezing actions explicitly.
6014 -- including the generation of an explicit freeze node, to ensure
6015 -- that gigi has the proper order of elaboration.
6016 -- If the body is a subunit, the insertion point is before the
6017 -- stub in the parent.
6019 Prot_Bod := Parent (List_Containing (N));
6021 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6022 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6023 end if;
6025 Insert_Before (Prot_Bod, Prot_Decl);
6026 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6027 Set_Has_Delayed_Freeze (Prot_Id);
6029 Push_Scope (Scope (Scop));
6030 Analyze (Prot_Decl);
6031 Freeze_Before (N, Prot_Id);
6032 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6034 -- Create protected operation as well. Even though the operation
6035 -- is only accessible within the body, it is possible to make it
6036 -- available outside of the protected object by using 'Access to
6037 -- provide a callback, so build protected version in all cases.
6039 Prot_Decl :=
6040 Make_Subprogram_Declaration (Loc,
6041 Specification =>
6042 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
6043 Insert_Before (Prot_Bod, Prot_Decl);
6044 Analyze (Prot_Decl);
6046 Pop_Scope;
6047 end if;
6049 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6050 -- cases this is superfluous because calls to it will be automatically
6051 -- inlined, but we definitely need the body if preconditions for the
6052 -- procedure are present, or if performing coverage analysis.
6054 elsif Nkind (Specification (N)) = N_Procedure_Specification
6055 and then Null_Present (Specification (N))
6056 then
6057 declare
6058 Bod : constant Node_Id := Body_To_Inline (N);
6060 begin
6061 Set_Has_Completion (Subp, False);
6062 Append_Freeze_Action (Subp, Bod);
6064 -- The body now contains raise statements, so calls to it will
6065 -- not be inlined.
6067 Set_Is_Inlined (Subp, False);
6068 end;
6069 end if;
6071 -- When generating C code, transform a function that returns a
6072 -- constrained array type into a procedure with an out parameter
6073 -- that carries the return value.
6075 -- We skip this transformation for unchecked conversions, since they
6076 -- are not needed by the C generator (and this also produces cleaner
6077 -- output).
6079 if Modify_Tree_For_C
6080 and then Nkind (Specification (N)) = N_Function_Specification
6081 and then Is_Array_Type (Etype (Subp))
6082 and then Is_Constrained (Etype (Subp))
6083 and then not Is_Unchecked_Conversion_Instance (Subp)
6084 then
6085 Build_Procedure_Form (N);
6086 end if;
6087 end Expand_N_Subprogram_Declaration;
6089 --------------------------------
6090 -- Expand_Non_Function_Return --
6091 --------------------------------
6093 procedure Expand_Non_Function_Return (N : Node_Id) is
6094 pragma Assert (No (Expression (N)));
6096 Loc : constant Source_Ptr := Sloc (N);
6097 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6098 Kind : constant Entity_Kind := Ekind (Scope_Id);
6099 Call : Node_Id;
6100 Acc_Stat : Node_Id;
6101 Goto_Stat : Node_Id;
6102 Lab_Node : Node_Id;
6104 begin
6105 -- Call the _Postconditions procedure if the related subprogram has
6106 -- contract assertions that need to be verified on exit.
6108 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
6109 and then Present (Postconditions_Proc (Scope_Id))
6110 then
6111 Insert_Action (N,
6112 Make_Procedure_Call_Statement (Loc,
6113 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
6114 end if;
6116 -- If it is a return from a procedure do no extra steps
6118 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6119 return;
6121 -- If it is a nested return within an extended one, replace it with a
6122 -- return of the previously declared return object.
6124 elsif Kind = E_Return_Statement then
6125 Rewrite (N,
6126 Make_Simple_Return_Statement (Loc,
6127 Expression =>
6128 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6129 Set_Comes_From_Extended_Return_Statement (N);
6130 Set_Return_Statement_Entity (N, Scope_Id);
6131 Expand_Simple_Function_Return (N);
6132 return;
6133 end if;
6135 pragma Assert (Is_Entry (Scope_Id));
6137 -- Look at the enclosing block to see whether the return is from an
6138 -- accept statement or an entry body.
6140 for J in reverse 0 .. Scope_Stack.Last loop
6141 Scope_Id := Scope_Stack.Table (J).Entity;
6142 exit when Is_Concurrent_Type (Scope_Id);
6143 end loop;
6145 -- If it is a return from accept statement it is expanded as call to
6146 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6148 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6149 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6151 if Is_Task_Type (Scope_Id) then
6153 Call :=
6154 Make_Procedure_Call_Statement (Loc,
6155 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6156 Insert_Before (N, Call);
6157 -- why not insert actions here???
6158 Analyze (Call);
6160 Acc_Stat := Parent (N);
6161 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6162 Acc_Stat := Parent (Acc_Stat);
6163 end loop;
6165 Lab_Node := Last (Statements
6166 (Handled_Statement_Sequence (Acc_Stat)));
6168 Goto_Stat := Make_Goto_Statement (Loc,
6169 Name => New_Occurrence_Of
6170 (Entity (Identifier (Lab_Node)), Loc));
6172 Set_Analyzed (Goto_Stat);
6174 Rewrite (N, Goto_Stat);
6175 Analyze (N);
6177 -- If it is a return from an entry body, put a Complete_Entry_Body call
6178 -- in front of the return.
6180 elsif Is_Protected_Type (Scope_Id) then
6181 Call :=
6182 Make_Procedure_Call_Statement (Loc,
6183 Name =>
6184 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6185 Parameter_Associations => New_List (
6186 Make_Attribute_Reference (Loc,
6187 Prefix =>
6188 New_Occurrence_Of
6189 (Find_Protection_Object (Current_Scope), Loc),
6190 Attribute_Name => Name_Unchecked_Access)));
6192 Insert_Before (N, Call);
6193 Analyze (Call);
6194 end if;
6195 end Expand_Non_Function_Return;
6197 ---------------------------------------
6198 -- Expand_Protected_Object_Reference --
6199 ---------------------------------------
6201 function Expand_Protected_Object_Reference
6202 (N : Node_Id;
6203 Scop : Entity_Id) return Node_Id
6205 Loc : constant Source_Ptr := Sloc (N);
6206 Corr : Entity_Id;
6207 Rec : Node_Id;
6208 Param : Entity_Id;
6209 Proc : Entity_Id;
6211 begin
6212 Rec := Make_Identifier (Loc, Name_uObject);
6213 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6215 -- Find enclosing protected operation, and retrieve its first parameter,
6216 -- which denotes the enclosing protected object. If the enclosing
6217 -- operation is an entry, we are immediately within the protected body,
6218 -- and we can retrieve the object from the service entries procedure. A
6219 -- barrier function has the same signature as an entry. A barrier
6220 -- function is compiled within the protected object, but unlike
6221 -- protected operations its never needs locks, so that its protected
6222 -- body subprogram points to itself.
6224 Proc := Current_Scope;
6225 while Present (Proc)
6226 and then Scope (Proc) /= Scop
6227 loop
6228 Proc := Scope (Proc);
6229 end loop;
6231 Corr := Protected_Body_Subprogram (Proc);
6233 if No (Corr) then
6235 -- Previous error left expansion incomplete.
6236 -- Nothing to do on this call.
6238 return Empty;
6239 end if;
6241 Param :=
6242 Defining_Identifier
6243 (First (Parameter_Specifications (Parent (Corr))));
6245 if Is_Subprogram (Proc) and then Proc /= Corr then
6247 -- Protected function or procedure
6249 Set_Entity (Rec, Param);
6251 -- Rec is a reference to an entity which will not be in scope when
6252 -- the call is reanalyzed, and needs no further analysis.
6254 Set_Analyzed (Rec);
6256 else
6257 -- Entry or barrier function for entry body. The first parameter of
6258 -- the entry body procedure is pointer to the object. We create a
6259 -- local variable of the proper type, duplicating what is done to
6260 -- define _object later on.
6262 declare
6263 Decls : List_Id;
6264 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6266 begin
6267 Decls := New_List (
6268 Make_Full_Type_Declaration (Loc,
6269 Defining_Identifier => Obj_Ptr,
6270 Type_Definition =>
6271 Make_Access_To_Object_Definition (Loc,
6272 Subtype_Indication =>
6273 New_Occurrence_Of
6274 (Corresponding_Record_Type (Scop), Loc))));
6276 Insert_Actions (N, Decls);
6277 Freeze_Before (N, Obj_Ptr);
6279 Rec :=
6280 Make_Explicit_Dereference (Loc,
6281 Prefix =>
6282 Unchecked_Convert_To (Obj_Ptr,
6283 New_Occurrence_Of (Param, Loc)));
6285 -- Analyze new actual. Other actuals in calls are already analyzed
6286 -- and the list of actuals is not reanalyzed after rewriting.
6288 Set_Parent (Rec, N);
6289 Analyze (Rec);
6290 end;
6291 end if;
6293 return Rec;
6294 end Expand_Protected_Object_Reference;
6296 --------------------------------------
6297 -- Expand_Protected_Subprogram_Call --
6298 --------------------------------------
6300 procedure Expand_Protected_Subprogram_Call
6301 (N : Node_Id;
6302 Subp : Entity_Id;
6303 Scop : Entity_Id)
6305 Rec : Node_Id;
6307 procedure Expand_Internal_Init_Call;
6308 -- A call to an operation of the type may occur in the initialization
6309 -- of a private component. In that case the prefix of the call is an
6310 -- entity name and the call is treated as internal even though it
6311 -- appears in code outside of the protected type.
6313 procedure Freeze_Called_Function;
6314 -- If it is a function call it can appear in elaboration code and
6315 -- the called entity must be frozen before the call. This must be
6316 -- done before the call is expanded, as the expansion may rewrite it
6317 -- to something other than a call (e.g. a temporary initialized in a
6318 -- transient block).
6320 -------------------------------
6321 -- Expand_Internal_Init_Call --
6322 -------------------------------
6324 procedure Expand_Internal_Init_Call is
6325 begin
6326 -- If the context is a protected object (rather than a protected
6327 -- type) the call itself is bound to raise program_error because
6328 -- the protected body will not have been elaborated yet. This is
6329 -- diagnosed subsequently in Sem_Elab.
6331 Freeze_Called_Function;
6333 -- The target of the internal call is the first formal of the
6334 -- enclosing initialization procedure.
6336 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6337 Build_Protected_Subprogram_Call (N,
6338 Name => Name (N),
6339 Rec => Rec,
6340 External => False);
6341 Analyze (N);
6342 Resolve (N, Etype (Subp));
6343 end Expand_Internal_Init_Call;
6345 ----------------------------
6346 -- Freeze_Called_Function --
6347 ----------------------------
6349 procedure Freeze_Called_Function is
6350 begin
6351 if Ekind (Subp) = E_Function then
6352 Freeze_Expression (Name (N));
6353 end if;
6354 end Freeze_Called_Function;
6356 -- Start of processing for Expand_Protected_Subprogram_Call
6358 begin
6359 -- If the protected object is not an enclosing scope, this is an inter-
6360 -- object function call. Inter-object procedure calls are expanded by
6361 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6362 -- subprogram being called is in the protected body being compiled, and
6363 -- if the protected object in the call is statically the enclosing type.
6364 -- The object may be a component of some other data structure, in which
6365 -- case this must be handled as an inter-object call.
6367 if not In_Open_Scopes (Scop)
6368 or else Is_Entry_Wrapper (Current_Scope)
6369 or else not Is_Entity_Name (Name (N))
6370 then
6371 if Nkind (Name (N)) = N_Selected_Component then
6372 Rec := Prefix (Name (N));
6374 elsif Nkind (Name (N)) = N_Indexed_Component then
6375 Rec := Prefix (Prefix (Name (N)));
6377 -- If this is a call within an entry wrapper, it appears within a
6378 -- precondition that calls another primitive of the synchronized
6379 -- type. The target object of the call is the first actual on the
6380 -- wrapper. Note that this is an external call, because the wrapper
6381 -- is called outside of the synchronized object. This means that
6382 -- an entry call to an entry with preconditions involves two
6383 -- synchronized operations.
6385 elsif Ekind (Current_Scope) = E_Procedure
6386 and then Is_Entry_Wrapper (Current_Scope)
6387 then
6388 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6390 else
6391 -- If the context is the initialization procedure for a protected
6392 -- type, the call is legal because the called entity must be a
6393 -- function of that enclosing type, and this is treated as an
6394 -- internal call.
6396 pragma Assert
6397 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6399 Expand_Internal_Init_Call;
6400 return;
6401 end if;
6403 Freeze_Called_Function;
6404 Build_Protected_Subprogram_Call (N,
6405 Name => New_Occurrence_Of (Subp, Sloc (N)),
6406 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6407 External => True);
6409 else
6410 Rec := Expand_Protected_Object_Reference (N, Scop);
6412 if No (Rec) then
6413 return;
6414 end if;
6416 Freeze_Called_Function;
6417 Build_Protected_Subprogram_Call (N,
6418 Name => Name (N),
6419 Rec => Rec,
6420 External => False);
6421 end if;
6423 -- Analyze and resolve the new call. The actuals have already been
6424 -- resolved, but expansion of a function call will add extra actuals
6425 -- if needed. Analysis of a procedure call already includes resolution.
6427 Analyze (N);
6429 if Ekind (Subp) = E_Function then
6430 Resolve (N, Etype (Subp));
6431 end if;
6432 end Expand_Protected_Subprogram_Call;
6434 -----------------------------------
6435 -- Expand_Simple_Function_Return --
6436 -----------------------------------
6438 -- The "simple" comes from the syntax rule simple_return_statement. The
6439 -- semantics are not at all simple.
6441 procedure Expand_Simple_Function_Return (N : Node_Id) is
6442 Loc : constant Source_Ptr := Sloc (N);
6444 Scope_Id : constant Entity_Id :=
6445 Return_Applies_To (Return_Statement_Entity (N));
6446 -- The function we are returning from
6448 R_Type : constant Entity_Id := Etype (Scope_Id);
6449 -- The result type of the function
6451 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6453 Exp : Node_Id := Expression (N);
6454 pragma Assert (Present (Exp));
6456 Exptyp : constant Entity_Id := Etype (Exp);
6457 -- The type of the expression (not necessarily the same as R_Type)
6459 Subtype_Ind : Node_Id;
6460 -- If the result type of the function is class-wide and the expression
6461 -- has a specific type, then we use the expression's type as the type of
6462 -- the return object. In cases where the expression is an aggregate that
6463 -- is built in place, this avoids the need for an expensive conversion
6464 -- of the return object to the specific type on assignments to the
6465 -- individual components.
6467 begin
6468 if Is_Class_Wide_Type (R_Type)
6469 and then not Is_Class_Wide_Type (Exptyp)
6470 and then Nkind (Exp) /= N_Type_Conversion
6471 then
6472 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6473 else
6474 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6476 -- If the result type is class-wide and the expression is a view
6477 -- conversion, the conversion plays no role in the expansion because
6478 -- it does not modify the tag of the object. Remove the conversion
6479 -- altogether to prevent tag overwriting.
6481 if Is_Class_Wide_Type (R_Type)
6482 and then not Is_Class_Wide_Type (Exptyp)
6483 and then Nkind (Exp) = N_Type_Conversion
6484 then
6485 Exp := Expression (Exp);
6486 end if;
6487 end if;
6489 -- Assert that if F says "return G(...);"
6490 -- then F and G are both b-i-p, or neither b-i-p.
6492 if Nkind (Exp) = N_Function_Call then
6493 pragma Assert (Ekind (Scope_Id) = E_Function);
6494 pragma Assert
6495 (Is_Build_In_Place_Function (Scope_Id) =
6496 Is_Build_In_Place_Function_Call (Exp));
6497 null;
6498 end if;
6500 -- For the case of a simple return that does not come from an
6501 -- extended return, in the case of build-in-place, we rewrite
6502 -- "return <expression>;" to be:
6504 -- return _anon_ : <return_subtype> := <expression>
6506 -- The expansion produced by Expand_N_Extended_Return_Statement will
6507 -- contain simple return statements (for example, a block containing
6508 -- simple return of the return object), which brings us back here with
6509 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6510 -- checking for a simple return that does not come from an extended
6511 -- return is to avoid this infinite recursion.
6513 -- The reason for this design is that for Ada 2005 limited returns, we
6514 -- need to reify the return object, so we can build it "in place", and
6515 -- we need a block statement to hang finalization and tasking stuff.
6517 -- ??? In order to avoid disruption, we avoid translating to extended
6518 -- return except in the cases where we really need to (Ada 2005 for
6519 -- inherently limited). We might prefer to do this translation in all
6520 -- cases (except perhaps for the case of Ada 95 inherently limited),
6521 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6522 -- code. This would also allow us to do the build-in-place optimization
6523 -- for efficiency even in cases where it is semantically not required.
6525 -- As before, we check the type of the return expression rather than the
6526 -- return type of the function, because the latter may be a limited
6527 -- class-wide interface type, which is not a limited type, even though
6528 -- the type of the expression may be.
6530 pragma Assert
6531 (Comes_From_Extended_Return_Statement (N)
6532 or else not Is_Build_In_Place_Function_Call (Exp)
6533 or else Is_Build_In_Place_Function (Scope_Id));
6535 if not Comes_From_Extended_Return_Statement (N)
6536 and then Is_Build_In_Place_Function (Scope_Id)
6537 and then not Debug_Flag_Dot_L
6539 -- The functionality of interface thunks is simple and it is always
6540 -- handled by means of simple return statements. This leaves their
6541 -- expansion simple and clean.
6543 and then not Is_Thunk (Current_Scope)
6544 then
6545 declare
6546 Return_Object_Entity : constant Entity_Id :=
6547 Make_Temporary (Loc, 'R', Exp);
6549 Obj_Decl : constant Node_Id :=
6550 Make_Object_Declaration (Loc,
6551 Defining_Identifier => Return_Object_Entity,
6552 Object_Definition => Subtype_Ind,
6553 Expression => Exp);
6555 Ext : constant Node_Id :=
6556 Make_Extended_Return_Statement (Loc,
6557 Return_Object_Declarations => New_List (Obj_Decl));
6558 -- Do not perform this high-level optimization if the result type
6559 -- is an interface because the "this" pointer must be displaced.
6561 begin
6562 Rewrite (N, Ext);
6563 Analyze (N);
6564 return;
6565 end;
6566 end if;
6568 -- Here we have a simple return statement that is part of the expansion
6569 -- of an extended return statement (either written by the user, or
6570 -- generated by the above code).
6572 -- Always normalize C/Fortran boolean result. This is not always needed,
6573 -- but it seems a good idea to minimize the passing around of non-
6574 -- normalized values, and in any case this handles the processing of
6575 -- barrier functions for protected types, which turn the condition into
6576 -- a return statement.
6578 if Is_Boolean_Type (Exptyp)
6579 and then Nonzero_Is_True (Exptyp)
6580 then
6581 Adjust_Condition (Exp);
6582 Adjust_Result_Type (Exp, Exptyp);
6583 end if;
6585 -- Do validity check if enabled for returns
6587 if Validity_Checks_On
6588 and then Validity_Check_Returns
6589 then
6590 Ensure_Valid (Exp);
6591 end if;
6593 -- Check the result expression of a scalar function against the subtype
6594 -- of the function by inserting a conversion. This conversion must
6595 -- eventually be performed for other classes of types, but for now it's
6596 -- only done for scalars.
6597 -- ???
6599 if Is_Scalar_Type (Exptyp) then
6600 Rewrite (Exp, Convert_To (R_Type, Exp));
6602 -- The expression is resolved to ensure that the conversion gets
6603 -- expanded to generate a possible constraint check.
6605 Analyze_And_Resolve (Exp, R_Type);
6606 end if;
6608 -- Deal with returning variable length objects and controlled types
6610 -- Nothing to do if we are returning by reference, or this is not a
6611 -- type that requires special processing (indicated by the fact that
6612 -- it requires a cleanup scope for the secondary stack case).
6614 if Is_Build_In_Place_Function (Scope_Id)
6615 or else Is_Limited_Interface (Exptyp)
6616 then
6617 null;
6619 -- No copy needed for thunks returning interface type objects since
6620 -- the object is returned by reference and the maximum functionality
6621 -- required is just to displace the pointer.
6623 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6624 null;
6626 -- If the call is within a thunk and the type is a limited view, the
6627 -- backend will eventually see the non-limited view of the type.
6629 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6630 return;
6632 elsif not Requires_Transient_Scope (R_Type) then
6634 -- Mutable records with variable-length components are not returned
6635 -- on the sec-stack, so we need to make sure that the back end will
6636 -- only copy back the size of the actual value, and not the maximum
6637 -- size. We create an actual subtype for this purpose. However we
6638 -- need not do it if the expression is a function call since this
6639 -- will be done in the called function and doing it here too would
6640 -- cause a temporary with maximum size to be created.
6642 declare
6643 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6644 Decl : Node_Id;
6645 Ent : Entity_Id;
6646 begin
6647 if Nkind (Exp) /= N_Function_Call
6648 and then Has_Discriminants (Ubt)
6649 and then not Is_Constrained (Ubt)
6650 and then not Has_Unchecked_Union (Ubt)
6651 then
6652 Decl := Build_Actual_Subtype (Ubt, Exp);
6653 Ent := Defining_Identifier (Decl);
6654 Insert_Action (Exp, Decl);
6655 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6656 Analyze_And_Resolve (Exp);
6657 end if;
6658 end;
6660 -- Here if secondary stack is used
6662 else
6663 -- Prevent the reclamation of the secondary stack by all enclosing
6664 -- blocks and loops as well as the related function; otherwise the
6665 -- result would be reclaimed too early.
6667 Set_Enclosing_Sec_Stack_Return (N);
6669 -- Optimize the case where the result is a function call. In this
6670 -- case either the result is already on the secondary stack, or is
6671 -- already being returned with the stack pointer depressed and no
6672 -- further processing is required except to set the By_Ref flag
6673 -- to ensure that gigi does not attempt an extra unnecessary copy.
6674 -- (actually not just unnecessary but harmfully wrong in the case
6675 -- of a controlled type, where gigi does not know how to do a copy).
6676 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6677 -- for array types if the constrained status of the target type is
6678 -- different from that of the expression.
6680 if Requires_Transient_Scope (Exptyp)
6681 and then
6682 (not Is_Array_Type (Exptyp)
6683 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6684 or else CW_Or_Has_Controlled_Part (Utyp))
6685 and then Nkind (Exp) = N_Function_Call
6686 then
6687 Set_By_Ref (N);
6689 -- Remove side effects from the expression now so that other parts
6690 -- of the expander do not have to reanalyze this node without this
6691 -- optimization
6693 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6695 -- Ada 2005 (AI-251): If the type of the returned object is
6696 -- an interface then add an implicit type conversion to force
6697 -- displacement of the "this" pointer.
6699 if Is_Interface (R_Type) then
6700 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6701 end if;
6703 Analyze_And_Resolve (Exp, R_Type);
6705 -- For controlled types, do the allocation on the secondary stack
6706 -- manually in order to call adjust at the right time:
6708 -- type Anon1 is access R_Type;
6709 -- for Anon1'Storage_pool use ss_pool;
6710 -- Anon2 : anon1 := new R_Type'(expr);
6711 -- return Anon2.all;
6713 -- We do the same for classwide types that are not potentially
6714 -- controlled (by the virtue of restriction No_Finalization) because
6715 -- gigi is not able to properly allocate class-wide types.
6717 elsif CW_Or_Has_Controlled_Part (Utyp) then
6718 declare
6719 Loc : constant Source_Ptr := Sloc (N);
6720 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6721 Alloc_Node : Node_Id;
6722 Temp : Entity_Id;
6724 begin
6725 Set_Ekind (Acc_Typ, E_Access_Type);
6727 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6729 -- This is an allocator for the secondary stack, and it's fine
6730 -- to have Comes_From_Source set False on it, as gigi knows not
6731 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6733 Alloc_Node :=
6734 Make_Allocator (Loc,
6735 Expression =>
6736 Make_Qualified_Expression (Loc,
6737 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6738 Expression => Relocate_Node (Exp)));
6740 -- We do not want discriminant checks on the declaration,
6741 -- given that it gets its value from the allocator.
6743 Set_No_Initialization (Alloc_Node);
6745 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6747 Insert_List_Before_And_Analyze (N, New_List (
6748 Make_Full_Type_Declaration (Loc,
6749 Defining_Identifier => Acc_Typ,
6750 Type_Definition =>
6751 Make_Access_To_Object_Definition (Loc,
6752 Subtype_Indication => Subtype_Ind)),
6754 Make_Object_Declaration (Loc,
6755 Defining_Identifier => Temp,
6756 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6757 Expression => Alloc_Node)));
6759 Rewrite (Exp,
6760 Make_Explicit_Dereference (Loc,
6761 Prefix => New_Occurrence_Of (Temp, Loc)));
6763 -- Ada 2005 (AI-251): If the type of the returned object is
6764 -- an interface then add an implicit type conversion to force
6765 -- displacement of the "this" pointer.
6767 if Is_Interface (R_Type) then
6768 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6769 end if;
6771 Analyze_And_Resolve (Exp, R_Type);
6772 end;
6774 -- Otherwise use the gigi mechanism to allocate result on the
6775 -- secondary stack.
6777 else
6778 Check_Restriction (No_Secondary_Stack, N);
6779 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6780 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6781 end if;
6782 end if;
6784 -- Implement the rules of 6.5(8-10), which require a tag check in
6785 -- the case of a limited tagged return type, and tag reassignment for
6786 -- nonlimited tagged results. These actions are needed when the return
6787 -- type is a specific tagged type and the result expression is a
6788 -- conversion or a formal parameter, because in that case the tag of
6789 -- the expression might differ from the tag of the specific result type.
6791 -- We must also verify an underlying type exists for the return type in
6792 -- case it is incomplete - in which case is not necessary to generate a
6793 -- check anyway since an incomplete limited tagged return type would
6794 -- qualify as a premature usage.
6796 if Present (Utyp)
6797 and then Is_Tagged_Type (Utyp)
6798 and then not Is_Class_Wide_Type (Utyp)
6799 and then (Nkind_In (Exp, N_Type_Conversion,
6800 N_Unchecked_Type_Conversion)
6801 or else (Is_Entity_Name (Exp)
6802 and then Ekind (Entity (Exp)) in Formal_Kind))
6803 then
6804 -- When the return type is limited, perform a check that the tag of
6805 -- the result is the same as the tag of the return type.
6807 if Is_Limited_Type (R_Type) then
6808 Insert_Action (Exp,
6809 Make_Raise_Constraint_Error (Loc,
6810 Condition =>
6811 Make_Op_Ne (Loc,
6812 Left_Opnd =>
6813 Make_Selected_Component (Loc,
6814 Prefix => Duplicate_Subexpr (Exp),
6815 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6816 Right_Opnd =>
6817 Make_Attribute_Reference (Loc,
6818 Prefix =>
6819 New_Occurrence_Of (Base_Type (Utyp), Loc),
6820 Attribute_Name => Name_Tag)),
6821 Reason => CE_Tag_Check_Failed));
6823 -- If the result type is a specific nonlimited tagged type, then we
6824 -- have to ensure that the tag of the result is that of the result
6825 -- type. This is handled by making a copy of the expression in
6826 -- the case where it might have a different tag, namely when the
6827 -- expression is a conversion or a formal parameter. We create a new
6828 -- object of the result type and initialize it from the expression,
6829 -- which will implicitly force the tag to be set appropriately.
6831 else
6832 declare
6833 ExpR : constant Node_Id := Relocate_Node (Exp);
6834 Result_Id : constant Entity_Id :=
6835 Make_Temporary (Loc, 'R', ExpR);
6836 Result_Exp : constant Node_Id :=
6837 New_Occurrence_Of (Result_Id, Loc);
6838 Result_Obj : constant Node_Id :=
6839 Make_Object_Declaration (Loc,
6840 Defining_Identifier => Result_Id,
6841 Object_Definition =>
6842 New_Occurrence_Of (R_Type, Loc),
6843 Constant_Present => True,
6844 Expression => ExpR);
6846 begin
6847 Set_Assignment_OK (Result_Obj);
6848 Insert_Action (Exp, Result_Obj);
6850 Rewrite (Exp, Result_Exp);
6851 Analyze_And_Resolve (Exp, R_Type);
6852 end;
6853 end if;
6855 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6856 -- a check that the level of the return expression's underlying type
6857 -- is not deeper than the level of the master enclosing the function.
6858 -- Always generate the check when the type of the return expression
6859 -- is class-wide, when it's a type conversion, or when it's a formal
6860 -- parameter. Otherwise, suppress the check in the case where the
6861 -- return expression has a specific type whose level is known not to
6862 -- be statically deeper than the function's result type.
6864 -- No runtime check needed in interface thunks since it is performed
6865 -- by the target primitive associated with the thunk.
6867 -- Note: accessibility check is skipped in the VM case, since there
6868 -- does not seem to be any practical way to implement this check.
6870 elsif Ada_Version >= Ada_2005
6871 and then Tagged_Type_Expansion
6872 and then Is_Class_Wide_Type (R_Type)
6873 and then not Is_Thunk (Current_Scope)
6874 and then not Scope_Suppress.Suppress (Accessibility_Check)
6875 and then
6876 (Is_Class_Wide_Type (Etype (Exp))
6877 or else Nkind_In (Exp, N_Type_Conversion,
6878 N_Unchecked_Type_Conversion)
6879 or else (Is_Entity_Name (Exp)
6880 and then Ekind (Entity (Exp)) in Formal_Kind)
6881 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6882 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6883 then
6884 declare
6885 Tag_Node : Node_Id;
6887 begin
6888 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6889 -- "this" to reference the base of the object. This is required to
6890 -- get access to the TSD of the object.
6892 if Is_Class_Wide_Type (Etype (Exp))
6893 and then Is_Interface (Etype (Exp))
6894 then
6895 -- If the expression is an explicit dereference then we can
6896 -- directly displace the pointer to reference the base of
6897 -- the object.
6899 if Nkind (Exp) = N_Explicit_Dereference then
6900 Tag_Node :=
6901 Make_Explicit_Dereference (Loc,
6902 Prefix =>
6903 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6904 Make_Function_Call (Loc,
6905 Name =>
6906 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6907 Parameter_Associations => New_List (
6908 Unchecked_Convert_To (RTE (RE_Address),
6909 Duplicate_Subexpr (Prefix (Exp)))))));
6911 -- Similar case to the previous one but the expression is a
6912 -- renaming of an explicit dereference.
6914 elsif Nkind (Exp) = N_Identifier
6915 and then Present (Renamed_Object (Entity (Exp)))
6916 and then Nkind (Renamed_Object (Entity (Exp)))
6917 = N_Explicit_Dereference
6918 then
6919 Tag_Node :=
6920 Make_Explicit_Dereference (Loc,
6921 Prefix =>
6922 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6923 Make_Function_Call (Loc,
6924 Name =>
6925 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6926 Parameter_Associations => New_List (
6927 Unchecked_Convert_To (RTE (RE_Address),
6928 Duplicate_Subexpr
6929 (Prefix
6930 (Renamed_Object (Entity (Exp)))))))));
6932 -- Common case: obtain the address of the actual object and
6933 -- displace the pointer to reference the base of the object.
6935 else
6936 Tag_Node :=
6937 Make_Explicit_Dereference (Loc,
6938 Prefix =>
6939 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6940 Make_Function_Call (Loc,
6941 Name =>
6942 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6943 Parameter_Associations => New_List (
6944 Make_Attribute_Reference (Loc,
6945 Prefix => Duplicate_Subexpr (Exp),
6946 Attribute_Name => Name_Address)))));
6947 end if;
6948 else
6949 Tag_Node :=
6950 Make_Attribute_Reference (Loc,
6951 Prefix => Duplicate_Subexpr (Exp),
6952 Attribute_Name => Name_Tag);
6953 end if;
6955 -- CodePeer does not do anything useful with
6956 -- Ada.Tags.Type_Specific_Data components.
6958 if not CodePeer_Mode then
6959 Insert_Action (Exp,
6960 Make_Raise_Program_Error (Loc,
6961 Condition =>
6962 Make_Op_Gt (Loc,
6963 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6964 Right_Opnd =>
6965 Make_Integer_Literal (Loc,
6966 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6967 Reason => PE_Accessibility_Check_Failed));
6968 end if;
6969 end;
6971 -- AI05-0073: If function has a controlling access result, check that
6972 -- the tag of the return value, if it is not null, matches designated
6973 -- type of return type.
6975 -- The return expression is referenced twice in the code below, so it
6976 -- must be made free of side effects. Given that different compilers
6977 -- may evaluate these parameters in different order, both occurrences
6978 -- perform a copy.
6980 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6981 and then Has_Controlling_Result (Scope_Id)
6982 then
6983 Insert_Action (N,
6984 Make_Raise_Constraint_Error (Loc,
6985 Condition =>
6986 Make_And_Then (Loc,
6987 Left_Opnd =>
6988 Make_Op_Ne (Loc,
6989 Left_Opnd => Duplicate_Subexpr (Exp),
6990 Right_Opnd => Make_Null (Loc)),
6992 Right_Opnd => Make_Op_Ne (Loc,
6993 Left_Opnd =>
6994 Make_Selected_Component (Loc,
6995 Prefix => Duplicate_Subexpr (Exp),
6996 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6998 Right_Opnd =>
6999 Make_Attribute_Reference (Loc,
7000 Prefix =>
7001 New_Occurrence_Of (Designated_Type (R_Type), Loc),
7002 Attribute_Name => Name_Tag))),
7004 Reason => CE_Tag_Check_Failed),
7005 Suppress => All_Checks);
7006 end if;
7008 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
7009 -- ensure that the function result does not outlive an
7010 -- object designated by one of it discriminants.
7012 if Present (Extra_Accessibility_Of_Result (Scope_Id))
7013 and then Has_Unconstrained_Access_Discriminants (R_Type)
7014 then
7015 declare
7016 Discrim_Source : Node_Id;
7018 procedure Check_Against_Result_Level (Level : Node_Id);
7019 -- Check the given accessibility level against the level
7020 -- determined by the point of call. (AI05-0234).
7022 --------------------------------
7023 -- Check_Against_Result_Level --
7024 --------------------------------
7026 procedure Check_Against_Result_Level (Level : Node_Id) is
7027 begin
7028 Insert_Action (N,
7029 Make_Raise_Program_Error (Loc,
7030 Condition =>
7031 Make_Op_Gt (Loc,
7032 Left_Opnd => Level,
7033 Right_Opnd =>
7034 New_Occurrence_Of
7035 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
7036 Reason => PE_Accessibility_Check_Failed));
7037 end Check_Against_Result_Level;
7039 begin
7040 Discrim_Source := Exp;
7041 while Nkind (Discrim_Source) = N_Qualified_Expression loop
7042 Discrim_Source := Expression (Discrim_Source);
7043 end loop;
7045 if Nkind (Discrim_Source) = N_Identifier
7046 and then Is_Return_Object (Entity (Discrim_Source))
7047 then
7048 Discrim_Source := Entity (Discrim_Source);
7050 if Is_Constrained (Etype (Discrim_Source)) then
7051 Discrim_Source := Etype (Discrim_Source);
7052 else
7053 Discrim_Source := Expression (Parent (Discrim_Source));
7054 end if;
7056 elsif Nkind (Discrim_Source) = N_Identifier
7057 and then Nkind_In (Original_Node (Discrim_Source),
7058 N_Aggregate, N_Extension_Aggregate)
7059 then
7060 Discrim_Source := Original_Node (Discrim_Source);
7062 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
7063 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
7064 then
7065 Discrim_Source := Original_Node (Discrim_Source);
7066 end if;
7068 Discrim_Source := Unqual_Conv (Discrim_Source);
7070 case Nkind (Discrim_Source) is
7071 when N_Defining_Identifier =>
7072 pragma Assert (Is_Composite_Type (Discrim_Source)
7073 and then Has_Discriminants (Discrim_Source)
7074 and then Is_Constrained (Discrim_Source));
7076 declare
7077 Discrim : Entity_Id :=
7078 First_Discriminant (Base_Type (R_Type));
7079 Disc_Elmt : Elmt_Id :=
7080 First_Elmt (Discriminant_Constraint
7081 (Discrim_Source));
7082 begin
7083 loop
7084 if Ekind (Etype (Discrim)) =
7085 E_Anonymous_Access_Type
7086 then
7087 Check_Against_Result_Level
7088 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7089 end if;
7091 Next_Elmt (Disc_Elmt);
7092 Next_Discriminant (Discrim);
7093 exit when not Present (Discrim);
7094 end loop;
7095 end;
7097 when N_Aggregate
7098 | N_Extension_Aggregate
7100 -- Unimplemented: extension aggregate case where discrims
7101 -- come from ancestor part, not extension part.
7103 declare
7104 Discrim : Entity_Id :=
7105 First_Discriminant (Base_Type (R_Type));
7107 Disc_Exp : Node_Id := Empty;
7109 Positionals_Exhausted
7110 : Boolean := not Present (Expressions
7111 (Discrim_Source));
7113 function Associated_Expr
7114 (Comp_Id : Entity_Id;
7115 Associations : List_Id) return Node_Id;
7117 -- Given a component and a component associations list,
7118 -- locate the expression for that component; returns
7119 -- Empty if no such expression is found.
7121 ---------------------
7122 -- Associated_Expr --
7123 ---------------------
7125 function Associated_Expr
7126 (Comp_Id : Entity_Id;
7127 Associations : List_Id) return Node_Id
7129 Assoc : Node_Id;
7130 Choice : Node_Id;
7132 begin
7133 -- Simple linear search seems ok here
7135 Assoc := First (Associations);
7136 while Present (Assoc) loop
7137 Choice := First (Choices (Assoc));
7138 while Present (Choice) loop
7139 if (Nkind (Choice) = N_Identifier
7140 and then Chars (Choice) = Chars (Comp_Id))
7141 or else (Nkind (Choice) = N_Others_Choice)
7142 then
7143 return Expression (Assoc);
7144 end if;
7146 Next (Choice);
7147 end loop;
7149 Next (Assoc);
7150 end loop;
7152 return Empty;
7153 end Associated_Expr;
7155 begin
7156 if not Positionals_Exhausted then
7157 Disc_Exp := First (Expressions (Discrim_Source));
7158 end if;
7160 loop
7161 if Positionals_Exhausted then
7162 Disc_Exp :=
7163 Associated_Expr
7164 (Discrim,
7165 Component_Associations (Discrim_Source));
7166 end if;
7168 if Ekind (Etype (Discrim)) =
7169 E_Anonymous_Access_Type
7170 then
7171 Check_Against_Result_Level
7172 (Dynamic_Accessibility_Level (Disc_Exp));
7173 end if;
7175 Next_Discriminant (Discrim);
7176 exit when not Present (Discrim);
7178 if not Positionals_Exhausted then
7179 Next (Disc_Exp);
7180 Positionals_Exhausted := not Present (Disc_Exp);
7181 end if;
7182 end loop;
7183 end;
7185 when N_Function_Call =>
7187 -- No check needed (check performed by callee)
7189 null;
7191 when others =>
7192 declare
7193 Level : constant Node_Id :=
7194 Make_Integer_Literal (Loc,
7195 Object_Access_Level (Discrim_Source));
7197 begin
7198 -- Unimplemented: check for name prefix that includes
7199 -- a dereference of an access value with a dynamic
7200 -- accessibility level (e.g., an access param or a
7201 -- saooaaat) and use dynamic level in that case. For
7202 -- example:
7203 -- return Access_Param.all(Some_Index).Some_Component;
7204 -- ???
7206 Set_Etype (Level, Standard_Natural);
7207 Check_Against_Result_Level (Level);
7208 end;
7209 end case;
7210 end;
7211 end if;
7213 -- If we are returning an object that may not be bit-aligned, then copy
7214 -- the value into a temporary first. This copy may need to expand to a
7215 -- loop of component operations.
7217 if Is_Possibly_Unaligned_Slice (Exp)
7218 or else Is_Possibly_Unaligned_Object (Exp)
7219 then
7220 declare
7221 ExpR : constant Node_Id := Relocate_Node (Exp);
7222 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7223 begin
7224 Insert_Action (Exp,
7225 Make_Object_Declaration (Loc,
7226 Defining_Identifier => Tnn,
7227 Constant_Present => True,
7228 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7229 Expression => ExpR),
7230 Suppress => All_Checks);
7231 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7232 end;
7233 end if;
7235 -- Call the _Postconditions procedure if the related function has
7236 -- contract assertions that need to be verified on exit.
7238 if Ekind (Scope_Id) = E_Function
7239 and then Present (Postconditions_Proc (Scope_Id))
7240 then
7241 -- In the case of discriminated objects, we have created a
7242 -- constrained subtype above, and used the underlying type. This
7243 -- transformation is post-analysis and harmless, except that now the
7244 -- call to the post-condition will be analyzed and the type kinds
7245 -- have to match.
7247 if Nkind (Exp) = N_Unchecked_Type_Conversion
7248 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7249 then
7250 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7251 end if;
7253 -- We are going to reference the returned value twice in this case,
7254 -- once in the call to _Postconditions, and once in the actual return
7255 -- statement, but we can't have side effects happening twice.
7257 Force_Evaluation (Exp, Mode => Strict);
7259 -- Generate call to _Postconditions
7261 Insert_Action (Exp,
7262 Make_Procedure_Call_Statement (Loc,
7263 Name =>
7264 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
7265 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
7266 end if;
7268 -- Ada 2005 (AI-251): If this return statement corresponds with an
7269 -- simple return statement associated with an extended return statement
7270 -- and the type of the returned object is an interface then generate an
7271 -- implicit conversion to force displacement of the "this" pointer.
7273 if Ada_Version >= Ada_2005
7274 and then Comes_From_Extended_Return_Statement (N)
7275 and then Nkind (Expression (N)) = N_Identifier
7276 and then Is_Interface (Utyp)
7277 and then Utyp /= Underlying_Type (Exptyp)
7278 then
7279 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7280 Analyze_And_Resolve (Exp);
7281 end if;
7282 end Expand_Simple_Function_Return;
7284 -----------------------
7285 -- Freeze_Subprogram --
7286 -----------------------
7288 procedure Freeze_Subprogram (N : Node_Id) is
7289 Loc : constant Source_Ptr := Sloc (N);
7291 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7292 -- (Ada 2005): Register a predefined primitive in all the secondary
7293 -- dispatch tables of its primitive type.
7295 ----------------------------------
7296 -- Register_Predefined_DT_Entry --
7297 ----------------------------------
7299 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7300 Iface_DT_Ptr : Elmt_Id;
7301 Tagged_Typ : Entity_Id;
7302 Thunk_Id : Entity_Id;
7303 Thunk_Code : Node_Id;
7305 begin
7306 Tagged_Typ := Find_Dispatching_Type (Prim);
7308 if No (Access_Disp_Table (Tagged_Typ))
7309 or else not Has_Interfaces (Tagged_Typ)
7310 or else not RTE_Available (RE_Interface_Tag)
7311 or else Restriction_Active (No_Dispatching_Calls)
7312 then
7313 return;
7314 end if;
7316 -- Skip the first two access-to-dispatch-table pointers since they
7317 -- leads to the primary dispatch table (predefined DT and user
7318 -- defined DT). We are only concerned with the secondary dispatch
7319 -- table pointers. Note that the access-to- dispatch-table pointer
7320 -- corresponds to the first implemented interface retrieved below.
7322 Iface_DT_Ptr :=
7323 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7325 while Present (Iface_DT_Ptr)
7326 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7327 loop
7328 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7329 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7331 if Present (Thunk_Code) then
7332 Insert_Actions_After (N, New_List (
7333 Thunk_Code,
7335 Build_Set_Predefined_Prim_Op_Address (Loc,
7336 Tag_Node =>
7337 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7338 Position => DT_Position (Prim),
7339 Address_Node =>
7340 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7341 Make_Attribute_Reference (Loc,
7342 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7343 Attribute_Name => Name_Unrestricted_Access))),
7345 Build_Set_Predefined_Prim_Op_Address (Loc,
7346 Tag_Node =>
7347 New_Occurrence_Of
7348 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7349 Loc),
7350 Position => DT_Position (Prim),
7351 Address_Node =>
7352 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7353 Make_Attribute_Reference (Loc,
7354 Prefix => New_Occurrence_Of (Prim, Loc),
7355 Attribute_Name => Name_Unrestricted_Access)))));
7356 end if;
7358 -- Skip the tag of the predefined primitives dispatch table
7360 Next_Elmt (Iface_DT_Ptr);
7361 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7363 -- Skip tag of the no-thunks dispatch table
7365 Next_Elmt (Iface_DT_Ptr);
7366 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7368 -- Skip tag of predefined primitives no-thunks dispatch table
7370 Next_Elmt (Iface_DT_Ptr);
7371 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7373 Next_Elmt (Iface_DT_Ptr);
7374 end loop;
7375 end Register_Predefined_DT_Entry;
7377 -- Local variables
7379 Subp : constant Entity_Id := Entity (N);
7381 -- Start of processing for Freeze_Subprogram
7383 begin
7384 -- We suppress the initialization of the dispatch table entry when
7385 -- not Tagged_Type_Expansion because the dispatching mechanism is
7386 -- handled internally by the target.
7388 if Is_Dispatching_Operation (Subp)
7389 and then not Is_Abstract_Subprogram (Subp)
7390 and then Present (DTC_Entity (Subp))
7391 and then Present (Scope (DTC_Entity (Subp)))
7392 and then Tagged_Type_Expansion
7393 and then not Restriction_Active (No_Dispatching_Calls)
7394 and then RTE_Available (RE_Tag)
7395 then
7396 declare
7397 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7399 begin
7400 -- Handle private overridden primitives
7402 if not Is_CPP_Class (Typ) then
7403 Check_Overriding_Operation (Subp);
7404 end if;
7406 -- We assume that imported CPP primitives correspond with objects
7407 -- whose constructor is in the CPP side; therefore we don't need
7408 -- to generate code to register them in the dispatch table.
7410 if Is_CPP_Class (Typ) then
7411 null;
7413 -- Handle CPP primitives found in derivations of CPP_Class types.
7414 -- These primitives must have been inherited from some parent, and
7415 -- there is no need to register them in the dispatch table because
7416 -- Build_Inherit_Prims takes care of initializing these slots.
7418 elsif Is_Imported (Subp)
7419 and then (Convention (Subp) = Convention_CPP
7420 or else Convention (Subp) = Convention_C)
7421 then
7422 null;
7424 -- Generate code to register the primitive in non statically
7425 -- allocated dispatch tables
7427 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7429 -- When a primitive is frozen, enter its name in its dispatch
7430 -- table slot.
7432 if not Is_Interface (Typ)
7433 or else Present (Interface_Alias (Subp))
7434 then
7435 if Is_Predefined_Dispatching_Operation (Subp) then
7436 Register_Predefined_DT_Entry (Subp);
7437 end if;
7439 Insert_Actions_After (N,
7440 Register_Primitive (Loc, Prim => Subp));
7441 end if;
7442 end if;
7443 end;
7444 end if;
7446 -- Mark functions that return by reference. Note that it cannot be part
7447 -- of the normal semantic analysis of the spec since the underlying
7448 -- returned type may not be known yet (for private types).
7450 declare
7451 Typ : constant Entity_Id := Etype (Subp);
7452 Utyp : constant Entity_Id := Underlying_Type (Typ);
7454 begin
7455 if Is_Limited_View (Typ) then
7456 Set_Returns_By_Ref (Subp);
7458 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7459 Set_Returns_By_Ref (Subp);
7460 end if;
7461 end;
7463 -- Wnen freezing a null procedure, analyze its delayed aspects now
7464 -- because we may not have reached the end of the declarative list when
7465 -- delayed aspects are normally analyzed. This ensures that dispatching
7466 -- calls are properly rewritten when the generated _Postcondition
7467 -- procedure is analyzed in the null procedure body.
7469 if Nkind (Parent (Subp)) = N_Procedure_Specification
7470 and then Null_Present (Parent (Subp))
7471 then
7472 Analyze_Entry_Or_Subprogram_Contract (Subp);
7473 end if;
7474 end Freeze_Subprogram;
7476 --------------------------------------------
7477 -- Has_Unconstrained_Access_Discriminants --
7478 --------------------------------------------
7480 function Has_Unconstrained_Access_Discriminants
7481 (Subtyp : Entity_Id) return Boolean
7483 Discr : Entity_Id;
7485 begin
7486 if Has_Discriminants (Subtyp)
7487 and then not Is_Constrained (Subtyp)
7488 then
7489 Discr := First_Discriminant (Subtyp);
7490 while Present (Discr) loop
7491 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7492 return True;
7493 end if;
7495 Next_Discriminant (Discr);
7496 end loop;
7497 end if;
7499 return False;
7500 end Has_Unconstrained_Access_Discriminants;
7502 ------------------------------
7503 -- Insert_Post_Call_Actions --
7504 ------------------------------
7506 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7507 Context : constant Node_Id := Parent (N);
7509 begin
7510 if Is_Empty_List (Post_Call) then
7511 return;
7512 end if;
7514 -- Cases where the call is not a member of a statement list. This
7515 -- includes the case where the call is an actual in another function
7516 -- call or indexing, i.e. an expression context as well.
7518 if not Is_List_Member (N)
7519 or else Nkind_In (Context, N_Function_Call, N_Indexed_Component)
7520 then
7521 -- In Ada 2012 the call may be a function call in an expression
7522 -- (since OUT and IN OUT parameters are now allowed for such calls).
7523 -- The write-back of (in)-out parameters is handled by the back-end,
7524 -- but the constraint checks generated when subtypes of formal and
7525 -- actual don't match must be inserted in the form of assignments.
7527 if Nkind (Original_Node (N)) = N_Function_Call then
7528 pragma Assert (Ada_Version >= Ada_2012);
7529 -- Functions with '[in] out' parameters are only allowed in Ada
7530 -- 2012.
7532 -- We used to handle this by climbing up parents to a
7533 -- non-statement/declaration and then simply making a call to
7534 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7535 -- for Ada 2012. If we are in the middle of an expression, e.g.
7536 -- the condition of an IF, this call would insert after the IF
7537 -- statement, which is much too late to be doing the write back.
7538 -- For example:
7540 -- if Clobber (X) then
7541 -- Put_Line (X'Img);
7542 -- else
7543 -- goto Junk
7544 -- end if;
7546 -- Now assume Clobber changes X, if we put the write back after
7547 -- the IF, the Put_Line gets the wrong value and the goto causes
7548 -- the write back to be skipped completely.
7550 -- To deal with this, we replace the call by
7552 -- do
7553 -- Tnnn : constant function-result-type := function-call;
7554 -- Post_Call actions
7555 -- in
7556 -- Tnnn;
7557 -- end;
7559 declare
7560 Loc : constant Source_Ptr := Sloc (N);
7561 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7562 FRTyp : constant Entity_Id := Etype (N);
7563 Name : constant Node_Id := Relocate_Node (N);
7565 begin
7566 Prepend_To (Post_Call,
7567 Make_Object_Declaration (Loc,
7568 Defining_Identifier => Tnnn,
7569 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7570 Constant_Present => True,
7571 Expression => Name));
7573 Rewrite (N,
7574 Make_Expression_With_Actions (Loc,
7575 Actions => Post_Call,
7576 Expression => New_Occurrence_Of (Tnnn, Loc)));
7578 -- We don't want to just blindly call Analyze_And_Resolve
7579 -- because that would cause unwanted recursion on the call.
7580 -- So for a moment set the call as analyzed to prevent that
7581 -- recursion, and get the rest analyzed properly, then reset
7582 -- the analyzed flag, so our caller can continue.
7584 Set_Analyzed (Name, True);
7585 Analyze_And_Resolve (N, FRTyp);
7586 Set_Analyzed (Name, False);
7587 end;
7589 -- If not the special Ada 2012 case of a function call, then we must
7590 -- have the triggering statement of a triggering alternative or an
7591 -- entry call alternative, and we can add the post call stuff to the
7592 -- corresponding statement list.
7594 else
7595 pragma Assert (Nkind_In (Context, N_Entry_Call_Alternative,
7596 N_Triggering_Alternative));
7598 if Is_Non_Empty_List (Statements (Context)) then
7599 Insert_List_Before_And_Analyze
7600 (First (Statements (Context)), Post_Call);
7601 else
7602 Set_Statements (Context, Post_Call);
7603 end if;
7604 end if;
7606 -- A procedure call is always part of a declarative or statement list,
7607 -- however a function call may appear nested within a construct. Most
7608 -- cases of function call nesting are handled in the special case above.
7609 -- The only exception is when the function call acts as an actual in a
7610 -- procedure call. In this case the function call is in a list, but the
7611 -- post-call actions must be inserted after the procedure call.
7613 elsif Nkind (Context) = N_Procedure_Call_Statement then
7614 Insert_Actions_After (Context, Post_Call);
7616 -- Otherwise, normal case where N is in a statement sequence, just put
7617 -- the post-call stuff after the call statement.
7619 else
7620 Insert_Actions_After (N, Post_Call);
7621 end if;
7622 end Insert_Post_Call_Actions;
7624 -----------------------------------
7625 -- Is_Build_In_Place_Result_Type --
7626 -----------------------------------
7628 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
7629 begin
7630 if not Expander_Active then
7631 return False;
7632 end if;
7634 -- In Ada 2005 all functions with an inherently limited return type
7635 -- must be handled using a build-in-place profile, including the case
7636 -- of a function with a limited interface result, where the function
7637 -- may return objects of nonlimited descendants.
7639 if Is_Limited_View (Typ) then
7640 return Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L;
7642 else
7643 if Debug_Flag_Dot_9 then
7644 return False;
7645 end if;
7647 if Has_Interfaces (Typ) then
7648 return False;
7649 end if;
7651 declare
7652 T : Entity_Id := Typ;
7653 begin
7654 -- For T'Class, return True if it's True for T. This is necessary
7655 -- because a class-wide function might say "return F (...)", where
7656 -- F returns the corresponding specific type. We need a loop in
7657 -- case T is a subtype of a class-wide type.
7659 while Is_Class_Wide_Type (T) loop
7660 T := Etype (T);
7661 end loop;
7663 -- If this is a generic formal type in an instance, return True if
7664 -- it's True for the generic actual type.
7666 if Nkind (Parent (T)) = N_Subtype_Declaration
7667 and then Present (Generic_Parent_Type (Parent (T)))
7668 then
7669 T := Entity (Subtype_Indication (Parent (T)));
7671 if Present (Full_View (T)) then
7672 T := Full_View (T);
7673 end if;
7674 end if;
7676 if Present (Underlying_Type (T)) then
7677 T := Underlying_Type (T);
7678 end if;
7680 declare
7681 Result : Boolean;
7682 -- So we can stop here in the debugger
7683 begin
7684 -- ???For now, enable build-in-place for a very narrow set of
7685 -- controlled types. Change "if True" to "if False" to
7686 -- experiment with more controlled types. Eventually, we might
7687 -- like to enable build-in-place for all tagged types, all
7688 -- types that need finalization, and all caller-unknown-size
7689 -- types.
7691 if True then
7692 Result := Is_Controlled (T)
7693 and then Present (Enclosing_Subprogram (T))
7694 and then not Is_Compilation_Unit (Enclosing_Subprogram (T))
7695 and then Ekind (Enclosing_Subprogram (T)) = E_Procedure;
7696 else
7697 Result := Is_Controlled (T);
7698 end if;
7700 return Result;
7701 end;
7702 end;
7703 end if;
7704 end Is_Build_In_Place_Result_Type;
7706 --------------------------------
7707 -- Is_Build_In_Place_Function --
7708 --------------------------------
7710 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7711 begin
7712 -- This function is called from Expand_Subtype_From_Expr during
7713 -- semantic analysis, even when expansion is off. In those cases
7714 -- the build_in_place expansion will not take place.
7716 if not Expander_Active then
7717 return False;
7718 end if;
7720 -- For now we test whether E denotes a function or access-to-function
7721 -- type whose result subtype is inherently limited. Later this test
7722 -- may be revised to allow composite nonlimited types. Functions with
7723 -- a foreign convention or whose result type has a foreign convention
7724 -- never qualify.
7726 if Ekind_In (E, E_Function, E_Generic_Function)
7727 or else (Ekind (E) = E_Subprogram_Type
7728 and then Etype (E) /= Standard_Void_Type)
7729 then
7730 -- Note: If the function has a foreign convention, it cannot build
7731 -- its result in place, so you're on your own. On the other hand,
7732 -- if only the return type has a foreign convention, its layout is
7733 -- intended to be compatible with the other language, but the build-
7734 -- in place machinery can ensure that the object is not copied.
7736 return Is_Build_In_Place_Result_Type (Etype (E))
7737 and then not Has_Foreign_Convention (E)
7738 and then not Debug_Flag_Dot_L;
7739 else
7740 return False;
7741 end if;
7742 end Is_Build_In_Place_Function;
7744 -------------------------------------
7745 -- Is_Build_In_Place_Function_Call --
7746 -------------------------------------
7748 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7749 Exp_Node : constant Node_Id := Unqual_Conv (N);
7750 Function_Id : Entity_Id;
7752 begin
7753 -- Return False if the expander is currently inactive, since awareness
7754 -- of build-in-place treatment is only relevant during expansion. Note
7755 -- that Is_Build_In_Place_Function, which is called as part of this
7756 -- function, is also conditioned this way, but we need to check here as
7757 -- well to avoid blowing up on processing protected calls when expansion
7758 -- is disabled (such as with -gnatc) since those would trip over the
7759 -- raise of Program_Error below.
7761 -- In SPARK mode, build-in-place calls are not expanded, so that we
7762 -- may end up with a call that is neither resolved to an entity, nor
7763 -- an indirect call.
7765 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7766 return False;
7767 end if;
7769 if Is_Entity_Name (Name (Exp_Node)) then
7770 Function_Id := Entity (Name (Exp_Node));
7772 -- In the case of an explicitly dereferenced call, use the subprogram
7773 -- type generated for the dereference.
7775 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7776 Function_Id := Etype (Name (Exp_Node));
7778 -- This may be a call to a protected function.
7780 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7781 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7783 else
7784 raise Program_Error;
7785 end if;
7787 declare
7788 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
7789 -- So we can stop here in the debugger
7790 begin
7791 return Result;
7792 end;
7793 end Is_Build_In_Place_Function_Call;
7795 -----------------------
7796 -- Is_Null_Procedure --
7797 -----------------------
7799 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7800 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7802 begin
7803 if Ekind (Subp) /= E_Procedure then
7804 return False;
7806 -- Check if this is a declared null procedure
7808 elsif Nkind (Decl) = N_Subprogram_Declaration then
7809 if not Null_Present (Specification (Decl)) then
7810 return False;
7812 elsif No (Body_To_Inline (Decl)) then
7813 return False;
7815 -- Check if the body contains only a null statement, followed by
7816 -- the return statement added during expansion.
7818 else
7819 declare
7820 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7822 Stat : Node_Id;
7823 Stat2 : Node_Id;
7825 begin
7826 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7827 return False;
7828 else
7829 -- We must skip SCIL nodes because they are currently
7830 -- implemented as special N_Null_Statement nodes.
7832 Stat :=
7833 First_Non_SCIL_Node
7834 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7835 Stat2 := Next_Non_SCIL_Node (Stat);
7837 return
7838 Is_Empty_List (Declarations (Orig_Bod))
7839 and then Nkind (Stat) = N_Null_Statement
7840 and then
7841 (No (Stat2)
7842 or else
7843 (Nkind (Stat2) = N_Simple_Return_Statement
7844 and then No (Next (Stat2))));
7845 end if;
7846 end;
7847 end if;
7849 else
7850 return False;
7851 end if;
7852 end Is_Null_Procedure;
7854 -------------------------------------------
7855 -- Make_Build_In_Place_Call_In_Allocator --
7856 -------------------------------------------
7858 procedure Make_Build_In_Place_Call_In_Allocator
7859 (Allocator : Node_Id;
7860 Function_Call : Node_Id)
7862 Acc_Type : constant Entity_Id := Etype (Allocator);
7863 Loc : constant Source_Ptr := Sloc (Function_Call);
7864 Func_Call : Node_Id := Function_Call;
7865 Ref_Func_Call : Node_Id;
7866 Function_Id : Entity_Id;
7867 Result_Subt : Entity_Id;
7868 New_Allocator : Node_Id;
7869 Return_Obj_Access : Entity_Id; -- temp for function result
7870 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7871 Alloc_Form : BIP_Allocation_Form;
7872 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7873 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7874 Chain : Entity_Id; -- activation chain, in case of tasks
7876 begin
7877 -- Step past qualification or unchecked conversion (the latter can occur
7878 -- in cases of calls to 'Input).
7880 if Nkind_In (Func_Call, N_Qualified_Expression,
7881 N_Type_Conversion,
7882 N_Unchecked_Type_Conversion)
7883 then
7884 Func_Call := Expression (Func_Call);
7885 end if;
7887 -- Mark the call as processed as a build-in-place call
7889 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
7890 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7892 if Is_Entity_Name (Name (Func_Call)) then
7893 Function_Id := Entity (Name (Func_Call));
7895 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7896 Function_Id := Etype (Name (Func_Call));
7898 else
7899 raise Program_Error;
7900 end if;
7902 Result_Subt := Available_View (Etype (Function_Id));
7904 -- Create a temp for the function result. In the caller-allocates case,
7905 -- this will be initialized to the result of a new uninitialized
7906 -- allocator. Note: we do not use Allocator as the Related_Node of
7907 -- Return_Obj_Access in call to Make_Temporary below as this would
7908 -- create a sort of infinite "recursion".
7910 Return_Obj_Access := Make_Temporary (Loc, 'R');
7911 Set_Etype (Return_Obj_Access, Acc_Type);
7912 Set_Can_Never_Be_Null (Acc_Type, False);
7913 -- It gets initialized to null, so we can't have that
7915 -- When the result subtype is constrained, the return object is created
7916 -- on the caller side, and access to it is passed to the function. This
7917 -- optimization is disabled when the result subtype needs finalization
7918 -- actions because the caller side allocation may result in undesirable
7919 -- finalization. Consider the following example:
7921 -- function Make_Lim_Ctrl return Lim_Ctrl is
7922 -- begin
7923 -- return Result : Lim_Ctrl := raise Program_Error do
7924 -- null;
7925 -- end return;
7926 -- end Make_Lim_Ctrl;
7928 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
7930 -- Even though the size of limited controlled type Lim_Ctrl is known,
7931 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
7932 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
7933 -- during the initialization actions for Result, which implies that
7934 -- Result (and Obj by extension) should not be finalized. However Obj
7935 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
7936 -- since it is already attached on the related finalization master.
7938 -- Here and in related routines, we must examine the full view of the
7939 -- type, because the view at the point of call may differ from that
7940 -- that in the function body, and the expansion mechanism depends on
7941 -- the characteristics of the full view.
7943 if Is_Constrained (Underlying_Type (Result_Subt))
7944 and then not Needs_Finalization (Underlying_Type (Result_Subt))
7945 then
7946 -- Replace the initialized allocator of form "new T'(Func (...))"
7947 -- with an uninitialized allocator of form "new T", where T is the
7948 -- result subtype of the called function. The call to the function
7949 -- is handled separately further below.
7951 New_Allocator :=
7952 Make_Allocator (Loc,
7953 Expression => New_Occurrence_Of (Result_Subt, Loc));
7954 Set_No_Initialization (New_Allocator);
7956 -- Copy attributes to new allocator. Note that the new allocator
7957 -- logically comes from source if the original one did, so copy the
7958 -- relevant flag. This ensures proper treatment of the restriction
7959 -- No_Implicit_Heap_Allocations in this case.
7961 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7962 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7963 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7965 Rewrite (Allocator, New_Allocator);
7967 -- Initial value of the temp is the result of the uninitialized
7968 -- allocator. Unchecked_Convert is needed for T'Input where T is
7969 -- derived from a controlled type.
7971 Temp_Init := Relocate_Node (Allocator);
7973 if Nkind_In (Function_Call, N_Type_Conversion,
7974 N_Unchecked_Type_Conversion)
7975 then
7976 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
7977 end if;
7979 -- Indicate that caller allocates, and pass in the return object
7981 Alloc_Form := Caller_Allocation;
7982 Pool := Make_Null (No_Location);
7983 Return_Obj_Actual :=
7984 Make_Unchecked_Type_Conversion (Loc,
7985 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7986 Expression =>
7987 Make_Explicit_Dereference (Loc,
7988 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7990 -- When the result subtype is unconstrained, the function itself must
7991 -- perform the allocation of the return object, so we pass parameters
7992 -- indicating that.
7994 else
7995 Temp_Init := Empty;
7997 -- Case of a user-defined storage pool. Pass an allocation parameter
7998 -- indicating that the function should allocate its result in the
7999 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8000 -- pool may not be aliased.
8002 if Present (Associated_Storage_Pool (Acc_Type)) then
8003 Alloc_Form := User_Storage_Pool;
8004 Pool :=
8005 Make_Attribute_Reference (Loc,
8006 Prefix =>
8007 New_Occurrence_Of
8008 (Associated_Storage_Pool (Acc_Type), Loc),
8009 Attribute_Name => Name_Unrestricted_Access);
8011 -- No user-defined pool; pass an allocation parameter indicating that
8012 -- the function should allocate its result on the heap.
8014 else
8015 Alloc_Form := Global_Heap;
8016 Pool := Make_Null (No_Location);
8017 end if;
8019 -- The caller does not provide the return object in this case, so we
8020 -- have to pass null for the object access actual.
8022 Return_Obj_Actual := Empty;
8023 end if;
8025 -- Declare the temp object
8027 Insert_Action (Allocator,
8028 Make_Object_Declaration (Loc,
8029 Defining_Identifier => Return_Obj_Access,
8030 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8031 Expression => Temp_Init));
8033 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8035 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8036 -- then generate an implicit conversion to force displacement of the
8037 -- "this" pointer.
8039 if Is_Interface (Designated_Type (Acc_Type)) then
8040 Rewrite
8041 (Ref_Func_Call,
8042 OK_Convert_To (Acc_Type, Ref_Func_Call));
8044 -- If the types are incompatible, we need an unchecked conversion. Note
8045 -- that the full types will be compatible, but the types not visibly
8046 -- compatible.
8048 elsif Nkind_In (Function_Call, N_Type_Conversion,
8049 N_Unchecked_Type_Conversion)
8050 then
8051 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
8052 end if;
8054 declare
8055 Assign : constant Node_Id :=
8056 Make_Assignment_Statement (Loc,
8057 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8058 Expression => Ref_Func_Call);
8059 -- Assign the result of the function call into the temp. In the
8060 -- caller-allocates case, this is overwriting the temp with its
8061 -- initial value, which has no effect. In the callee-allocates case,
8062 -- this is setting the temp to point to the object allocated by the
8063 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8064 -- from a controlled type.
8066 Actions : List_Id;
8067 -- Actions to be inserted. If there are no tasks, this is just the
8068 -- assignment statement. If the allocated object has tasks, we need
8069 -- to wrap the assignment in a block that activates them. The
8070 -- activation chain of that block must be passed to the function,
8071 -- rather than some outer chain.
8073 begin
8074 if Has_Task (Result_Subt) then
8075 Actions := New_List;
8076 Build_Task_Allocate_Block_With_Init_Stmts
8077 (Actions, Allocator, Init_Stmts => New_List (Assign));
8078 Chain := Activation_Chain_Entity (Last (Actions));
8079 else
8080 Actions := New_List (Assign);
8081 Chain := Empty;
8082 end if;
8084 Insert_Actions (Allocator, Actions);
8085 end;
8087 -- When the function has a controlling result, an allocation-form
8088 -- parameter must be passed indicating that the caller is allocating
8089 -- the result object. This is needed because such a function can be
8090 -- called as a dispatching operation and must be treated similarly
8091 -- to functions with unconstrained result subtypes.
8093 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8094 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8096 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8097 (Func_Call, Function_Id, Acc_Type);
8099 Add_Task_Actuals_To_Build_In_Place_Call
8100 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8101 Chain => Chain);
8103 -- Add an implicit actual to the function call that provides access
8104 -- to the allocated object. An unchecked conversion to the (specific)
8105 -- result subtype of the function is inserted to handle cases where
8106 -- the access type of the allocator has a class-wide designated type.
8108 Add_Access_Actual_To_Build_In_Place_Call
8109 (Func_Call, Function_Id, Return_Obj_Actual);
8111 -- Finally, replace the allocator node with a reference to the temp
8113 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8115 Analyze_And_Resolve (Allocator, Acc_Type);
8116 end Make_Build_In_Place_Call_In_Allocator;
8118 ---------------------------------------------------
8119 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8120 ---------------------------------------------------
8122 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8123 (Function_Call : Node_Id)
8125 Loc : constant Source_Ptr := Sloc (Function_Call);
8126 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8127 Function_Id : Entity_Id;
8128 Result_Subt : Entity_Id;
8129 Return_Obj_Id : Entity_Id;
8130 Return_Obj_Decl : Entity_Id;
8132 begin
8133 -- If the call has already been processed to add build-in-place actuals
8134 -- then return. One place this can occur is for calls to build-in-place
8135 -- functions that occur within a call to a protected operation, where
8136 -- due to rewriting and expansion of the protected call there can be
8137 -- more than one call to Expand_Actuals for the same set of actuals.
8139 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8140 return;
8141 end if;
8143 -- Mark the call as processed as a build-in-place call
8145 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8147 if Is_Entity_Name (Name (Func_Call)) then
8148 Function_Id := Entity (Name (Func_Call));
8150 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8151 Function_Id := Etype (Name (Func_Call));
8153 else
8154 raise Program_Error;
8155 end if;
8157 Result_Subt := Etype (Function_Id);
8159 -- If the build-in-place function returns a controlled object, then the
8160 -- object needs to be finalized immediately after the context. Since
8161 -- this case produces a transient scope, the servicing finalizer needs
8162 -- to name the returned object. Create a temporary which is initialized
8163 -- with the function call:
8165 -- Temp_Id : Func_Type := BIP_Func_Call;
8167 -- The initialization expression of the temporary will be rewritten by
8168 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8169 -- Call_In_Object_Declaration.
8171 if Needs_Finalization (Result_Subt) then
8172 declare
8173 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8174 Temp_Decl : Node_Id;
8176 begin
8177 -- Reset the guard on the function call since the following does
8178 -- not perform actual call expansion.
8180 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8182 Temp_Decl :=
8183 Make_Object_Declaration (Loc,
8184 Defining_Identifier => Temp_Id,
8185 Object_Definition =>
8186 New_Occurrence_Of (Result_Subt, Loc),
8187 Expression =>
8188 New_Copy_Tree (Function_Call));
8190 Insert_Action (Function_Call, Temp_Decl);
8192 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8193 Analyze (Function_Call);
8194 end;
8196 -- When the result subtype is definite, an object of the subtype is
8197 -- declared and an access value designating it is passed as an actual.
8199 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8201 -- Create a temporary object to hold the function result
8203 Return_Obj_Id := Make_Temporary (Loc, 'R');
8204 Set_Etype (Return_Obj_Id, Result_Subt);
8206 Return_Obj_Decl :=
8207 Make_Object_Declaration (Loc,
8208 Defining_Identifier => Return_Obj_Id,
8209 Aliased_Present => True,
8210 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8212 Set_No_Initialization (Return_Obj_Decl);
8214 Insert_Action (Func_Call, Return_Obj_Decl);
8216 -- When the function has a controlling result, an allocation-form
8217 -- parameter must be passed indicating that the caller is allocating
8218 -- the result object. This is needed because such a function can be
8219 -- called as a dispatching operation and must be treated similarly
8220 -- to functions with unconstrained result subtypes.
8222 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8223 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8225 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8226 (Func_Call, Function_Id);
8228 Add_Task_Actuals_To_Build_In_Place_Call
8229 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8231 -- Add an implicit actual to the function call that provides access
8232 -- to the caller's return object.
8234 Add_Access_Actual_To_Build_In_Place_Call
8235 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8237 -- When the result subtype is unconstrained, the function must allocate
8238 -- the return object in the secondary stack, so appropriate implicit
8239 -- parameters are added to the call to indicate that. A transient
8240 -- scope is established to ensure eventual cleanup of the result.
8242 else
8243 -- Pass an allocation parameter indicating that the function should
8244 -- allocate its result on the secondary stack.
8246 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8247 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8249 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8250 (Func_Call, Function_Id);
8252 Add_Task_Actuals_To_Build_In_Place_Call
8253 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8255 -- Pass a null value to the function since no return object is
8256 -- available on the caller side.
8258 Add_Access_Actual_To_Build_In_Place_Call
8259 (Func_Call, Function_Id, Empty);
8260 end if;
8261 end Make_Build_In_Place_Call_In_Anonymous_Context;
8263 --------------------------------------------
8264 -- Make_Build_In_Place_Call_In_Assignment --
8265 --------------------------------------------
8267 procedure Make_Build_In_Place_Call_In_Assignment
8268 (Assign : Node_Id;
8269 Function_Call : Node_Id)
8271 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8272 Lhs : constant Node_Id := Name (Assign);
8273 Loc : constant Source_Ptr := Sloc (Function_Call);
8274 Func_Id : Entity_Id;
8275 Obj_Decl : Node_Id;
8276 Obj_Id : Entity_Id;
8277 Ptr_Typ : Entity_Id;
8278 Ptr_Typ_Decl : Node_Id;
8279 New_Expr : Node_Id;
8280 Result_Subt : Entity_Id;
8282 begin
8283 -- Mark the call as processed as a build-in-place call
8285 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8286 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8288 if Is_Entity_Name (Name (Func_Call)) then
8289 Func_Id := Entity (Name (Func_Call));
8291 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8292 Func_Id := Etype (Name (Func_Call));
8294 else
8295 raise Program_Error;
8296 end if;
8298 Result_Subt := Etype (Func_Id);
8300 -- When the result subtype is unconstrained, an additional actual must
8301 -- be passed to indicate that the caller is providing the return object.
8302 -- This parameter must also be passed when the called function has a
8303 -- controlling result, because dispatching calls to the function needs
8304 -- to be treated effectively the same as calls to class-wide functions.
8306 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8307 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8309 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8310 (Func_Call, Func_Id);
8312 Add_Task_Actuals_To_Build_In_Place_Call
8313 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8315 -- Add an implicit actual to the function call that provides access to
8316 -- the caller's return object.
8318 Add_Access_Actual_To_Build_In_Place_Call
8319 (Func_Call,
8320 Func_Id,
8321 Make_Unchecked_Type_Conversion (Loc,
8322 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8323 Expression => Relocate_Node (Lhs)));
8325 -- Create an access type designating the function's result subtype
8327 Ptr_Typ := Make_Temporary (Loc, 'A');
8329 Ptr_Typ_Decl :=
8330 Make_Full_Type_Declaration (Loc,
8331 Defining_Identifier => Ptr_Typ,
8332 Type_Definition =>
8333 Make_Access_To_Object_Definition (Loc,
8334 All_Present => True,
8335 Subtype_Indication =>
8336 New_Occurrence_Of (Result_Subt, Loc)));
8337 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8339 -- Finally, create an access object initialized to a reference to the
8340 -- function call. We know this access value is non-null, so mark the
8341 -- entity accordingly to suppress junk access checks.
8343 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8345 -- Add a conversion if it's the wrong type
8347 if Etype (New_Expr) /= Ptr_Typ then
8348 New_Expr :=
8349 Make_Unchecked_Type_Conversion (Loc,
8350 New_Occurrence_Of (Ptr_Typ, Loc), New_Expr);
8351 end if;
8353 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8354 Set_Etype (Obj_Id, Ptr_Typ);
8355 Set_Is_Known_Non_Null (Obj_Id);
8357 Obj_Decl :=
8358 Make_Object_Declaration (Loc,
8359 Defining_Identifier => Obj_Id,
8360 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8361 Expression => New_Expr);
8362 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8364 Rewrite (Assign, Make_Null_Statement (Loc));
8365 end Make_Build_In_Place_Call_In_Assignment;
8367 ----------------------------------------------------
8368 -- Make_Build_In_Place_Call_In_Object_Declaration --
8369 ----------------------------------------------------
8371 procedure Make_Build_In_Place_Call_In_Object_Declaration
8372 (Obj_Decl : Node_Id;
8373 Function_Call : Node_Id)
8375 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8376 -- Get the value of Function_Id, below
8378 ---------------------
8379 -- Get_Function_Id --
8380 ---------------------
8382 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8383 begin
8384 if Is_Entity_Name (Name (Func_Call)) then
8385 return Entity (Name (Func_Call));
8387 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8388 return Etype (Name (Func_Call));
8390 else
8391 raise Program_Error;
8392 end if;
8393 end Get_Function_Id;
8395 -- Local variables
8397 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8398 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8399 Loc : constant Source_Ptr := Sloc (Function_Call);
8400 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8401 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8402 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8403 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8404 Result_Subt : constant Entity_Id := Etype (Function_Id);
8406 Call_Deref : Node_Id;
8407 Caller_Object : Node_Id;
8408 Def_Id : Entity_Id;
8409 Designated_Type : Entity_Id;
8410 Fmaster_Actual : Node_Id := Empty;
8411 Pool_Actual : Node_Id;
8412 Ptr_Typ : Entity_Id;
8413 Ptr_Typ_Decl : Node_Id;
8414 Pass_Caller_Acc : Boolean := False;
8415 Res_Decl : Node_Id;
8417 Definite : constant Boolean :=
8418 Caller_Known_Size (Func_Call, Result_Subt)
8419 and then not Is_Class_Wide_Type (Obj_Typ);
8420 -- In the case of "X : T'Class := F(...);", where F returns a
8421 -- Caller_Known_Size (specific) tagged type, we treat it as
8422 -- indefinite, because the code for the Definite case below sets the
8423 -- initialization expression of the object to Empty, which would be
8424 -- illegal Ada, and would cause gigi to misallocate X.
8426 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8428 begin
8429 -- If the call has already been processed to add build-in-place actuals
8430 -- then return.
8432 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8433 return;
8434 end if;
8436 -- Mark the call as processed as a build-in-place call
8438 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8440 -- Create an access type designating the function's result subtype.
8441 -- We use the type of the original call because it may be a call to an
8442 -- inherited operation, which the expansion has replaced with the parent
8443 -- operation that yields the parent type. Note that this access type
8444 -- must be declared before we establish a transient scope, so that it
8445 -- receives the proper accessibility level.
8447 if Is_Class_Wide_Type (Obj_Typ)
8448 and then not Is_Interface (Obj_Typ)
8449 and then not Is_Class_Wide_Type (Etype (Function_Call))
8450 then
8451 Designated_Type := Obj_Typ;
8452 else
8453 Designated_Type := Etype (Function_Call);
8454 end if;
8456 Ptr_Typ := Make_Temporary (Loc, 'A');
8457 Ptr_Typ_Decl :=
8458 Make_Full_Type_Declaration (Loc,
8459 Defining_Identifier => Ptr_Typ,
8460 Type_Definition =>
8461 Make_Access_To_Object_Definition (Loc,
8462 All_Present => True,
8463 Subtype_Indication =>
8464 New_Occurrence_Of (Designated_Type, Loc)));
8466 -- The access type and its accompanying object must be inserted after
8467 -- the object declaration in the constrained case, so that the function
8468 -- call can be passed access to the object. In the indefinite case, or
8469 -- if the object declaration is for a return object, the access type and
8470 -- object must be inserted before the object, since the object
8471 -- declaration is rewritten to be a renaming of a dereference of the
8472 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8473 -- the result object is in a different (transient) scope, so won't cause
8474 -- freezing.
8476 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8478 -- The presence of an address clause complicates the build-in-place
8479 -- expansion because the indicated address must be processed before
8480 -- the indirect call is generated (including the definition of a
8481 -- local pointer to the object). The address clause may come from
8482 -- an aspect specification or from an explicit attribute
8483 -- specification appearing after the object declaration. These two
8484 -- cases require different processing.
8486 if Has_Aspect (Obj_Def_Id, Aspect_Address) then
8488 -- Skip non-delayed pragmas that correspond to other aspects, if
8489 -- any, to find proper insertion point for freeze node of object.
8491 declare
8492 D : Node_Id := Obj_Decl;
8493 N : Node_Id := Next (D);
8495 begin
8496 while Present (N)
8497 and then Nkind_In (N, N_Attribute_Reference, N_Pragma)
8498 loop
8499 Analyze (N);
8500 D := N;
8501 Next (N);
8502 end loop;
8504 Insert_After (D, Ptr_Typ_Decl);
8506 -- Freeze object before pointer declaration, to ensure that
8507 -- generated attribute for address is inserted at the proper
8508 -- place.
8510 Freeze_Before (Ptr_Typ_Decl, Obj_Def_Id);
8511 end;
8513 Analyze (Ptr_Typ_Decl);
8515 elsif Present (Following_Address_Clause (Obj_Decl)) then
8517 -- Locate explicit address clause, which may also follow pragmas
8518 -- generated by other aspect specifications.
8520 declare
8521 Addr : constant Node_Id := Following_Address_Clause (Obj_Decl);
8522 D : Node_Id := Next (Obj_Decl);
8524 begin
8525 while Present (D) loop
8526 Analyze (D);
8527 exit when D = Addr;
8528 Next (D);
8529 end loop;
8531 Insert_After_And_Analyze (Addr, Ptr_Typ_Decl);
8532 end;
8534 else
8535 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8536 end if;
8537 else
8538 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8539 end if;
8541 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8542 -- elaborated in an inner (transient) scope and thus won't cause
8543 -- freezing by itself. It's not an itype, but it needs to be frozen
8544 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8546 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
8548 -- If the object is a return object of an enclosing build-in-place
8549 -- function, then the implicit build-in-place parameters of the
8550 -- enclosing function are simply passed along to the called function.
8551 -- (Unfortunately, this won't cover the case of extension aggregates
8552 -- where the ancestor part is a build-in-place indefinite function
8553 -- call that should be passed along the caller's parameters.
8554 -- Currently those get mishandled by reassigning the result of the
8555 -- call to the aggregate return object, when the call result should
8556 -- really be directly built in place in the aggregate and not in a
8557 -- temporary. ???)
8559 if Is_Return_Object (Obj_Def_Id) then
8560 Pass_Caller_Acc := True;
8562 -- When the enclosing function has a BIP_Alloc_Form formal then we
8563 -- pass it along to the callee (such as when the enclosing function
8564 -- has an unconstrained or tagged result type).
8566 if Needs_BIP_Alloc_Form (Encl_Func) then
8567 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8568 Pool_Actual :=
8569 New_Occurrence_Of
8570 (Build_In_Place_Formal
8571 (Encl_Func, BIP_Storage_Pool), Loc);
8573 -- The build-in-place pool formal is not built on e.g. ZFP
8575 else
8576 Pool_Actual := Empty;
8577 end if;
8579 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8580 (Function_Call => Func_Call,
8581 Function_Id => Function_Id,
8582 Alloc_Form_Exp =>
8583 New_Occurrence_Of
8584 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8585 Pool_Actual => Pool_Actual);
8587 -- Otherwise, if enclosing function has a definite result subtype,
8588 -- then caller allocation will be used.
8590 else
8591 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8592 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8593 end if;
8595 if Needs_BIP_Finalization_Master (Encl_Func) then
8596 Fmaster_Actual :=
8597 New_Occurrence_Of
8598 (Build_In_Place_Formal
8599 (Encl_Func, BIP_Finalization_Master), Loc);
8600 end if;
8602 -- Retrieve the BIPacc formal from the enclosing function and convert
8603 -- it to the access type of the callee's BIP_Object_Access formal.
8605 Caller_Object :=
8606 Make_Unchecked_Type_Conversion (Loc,
8607 Subtype_Mark =>
8608 New_Occurrence_Of
8609 (Etype (Build_In_Place_Formal
8610 (Function_Id, BIP_Object_Access)),
8611 Loc),
8612 Expression =>
8613 New_Occurrence_Of
8614 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8615 Loc));
8617 -- In the definite case, add an implicit actual to the function call
8618 -- that provides access to the declared object. An unchecked conversion
8619 -- to the (specific) result type of the function is inserted to handle
8620 -- the case where the object is declared with a class-wide type.
8622 elsif Definite then
8623 Caller_Object :=
8624 Make_Unchecked_Type_Conversion (Loc,
8625 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8626 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8628 -- When the function has a controlling result, an allocation-form
8629 -- parameter must be passed indicating that the caller is allocating
8630 -- the result object. This is needed because such a function can be
8631 -- called as a dispatching operation and must be treated similarly to
8632 -- functions with indefinite result subtypes.
8634 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8635 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8637 -- The allocation for indefinite library-level objects occurs on the
8638 -- heap as opposed to the secondary stack. This accommodates DLLs where
8639 -- the secondary stack is destroyed after each library unload. This is a
8640 -- hybrid mechanism where a stack-allocated object lives on the heap.
8642 elsif Is_Library_Level_Entity (Obj_Def_Id)
8643 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8644 then
8645 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8646 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8647 Caller_Object := Empty;
8649 -- Create a finalization master for the access result type to ensure
8650 -- that the heap allocation can properly chain the object and later
8651 -- finalize it when the library unit goes out of scope.
8653 if Needs_Finalization (Etype (Func_Call)) then
8654 Build_Finalization_Master
8655 (Typ => Ptr_Typ,
8656 For_Lib_Level => True,
8657 Insertion_Node => Ptr_Typ_Decl);
8659 Fmaster_Actual :=
8660 Make_Attribute_Reference (Loc,
8661 Prefix =>
8662 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8663 Attribute_Name => Name_Unrestricted_Access);
8664 end if;
8666 -- In other indefinite cases, pass an indication to do the allocation
8667 -- on the secondary stack and set Caller_Object to Empty so that a null
8668 -- value will be passed for the caller's object address. A transient
8669 -- scope is established to ensure eventual cleanup of the result.
8671 else
8672 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8673 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8674 Caller_Object := Empty;
8676 Establish_Transient_Scope (Obj_Decl, Manage_Sec_Stack => True);
8677 end if;
8679 -- Pass along any finalization master actual, which is needed in the
8680 -- case where the called function initializes a return object of an
8681 -- enclosing build-in-place function.
8683 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8684 (Func_Call => Func_Call,
8685 Func_Id => Function_Id,
8686 Master_Exp => Fmaster_Actual);
8688 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8689 and then Has_Task (Result_Subt)
8690 then
8691 -- Here we're passing along the master that was passed in to this
8692 -- function.
8694 Add_Task_Actuals_To_Build_In_Place_Call
8695 (Func_Call, Function_Id,
8696 Master_Actual =>
8697 New_Occurrence_Of
8698 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8700 else
8701 Add_Task_Actuals_To_Build_In_Place_Call
8702 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8703 end if;
8705 Add_Access_Actual_To_Build_In_Place_Call
8706 (Func_Call,
8707 Function_Id,
8708 Caller_Object,
8709 Is_Access => Pass_Caller_Acc);
8711 -- Finally, create an access object initialized to a reference to the
8712 -- function call. We know this access value cannot be null, so mark the
8713 -- entity accordingly to suppress the access check.
8715 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8716 Set_Etype (Def_Id, Ptr_Typ);
8717 Set_Is_Known_Non_Null (Def_Id);
8719 if Nkind_In (Function_Call, N_Type_Conversion,
8720 N_Unchecked_Type_Conversion)
8721 then
8722 Res_Decl :=
8723 Make_Object_Declaration (Loc,
8724 Defining_Identifier => Def_Id,
8725 Constant_Present => True,
8726 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8727 Expression =>
8728 Make_Unchecked_Type_Conversion (Loc,
8729 New_Occurrence_Of (Ptr_Typ, Loc),
8730 Make_Reference (Loc, Relocate_Node (Func_Call))));
8731 else
8732 Res_Decl :=
8733 Make_Object_Declaration (Loc,
8734 Defining_Identifier => Def_Id,
8735 Constant_Present => True,
8736 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8737 Expression =>
8738 Make_Reference (Loc, Relocate_Node (Func_Call)));
8739 end if;
8741 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8743 -- If the result subtype of the called function is definite and is not
8744 -- itself the return expression of an enclosing BIP function, then mark
8745 -- the object as having no initialization.
8747 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8749 -- The related object declaration is encased in a transient block
8750 -- because the build-in-place function call contains at least one
8751 -- nested function call that produces a controlled transient
8752 -- temporary:
8754 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8756 -- Since the build-in-place expansion decouples the call from the
8757 -- object declaration, the finalization machinery lacks the context
8758 -- which prompted the generation of the transient block. To resolve
8759 -- this scenario, store the build-in-place call.
8761 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8762 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8763 end if;
8765 Set_Expression (Obj_Decl, Empty);
8766 Set_No_Initialization (Obj_Decl);
8768 -- In case of an indefinite result subtype, or if the call is the
8769 -- return expression of an enclosing BIP function, rewrite the object
8770 -- declaration as an object renaming where the renamed object is a
8771 -- dereference of <function_Call>'reference:
8773 -- Obj : Subt renames <function_call>'Ref.all;
8775 else
8776 Call_Deref :=
8777 Make_Explicit_Dereference (Obj_Loc,
8778 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8780 Rewrite (Obj_Decl,
8781 Make_Object_Renaming_Declaration (Obj_Loc,
8782 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8783 Subtype_Mark =>
8784 New_Occurrence_Of (Designated_Type, Obj_Loc),
8785 Name => Call_Deref));
8787 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
8788 -- to Obj_Def_Id.
8790 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8792 -- If the original entity comes from source, then mark the new
8793 -- entity as needing debug information, even though it's defined
8794 -- by a generated renaming that does not come from source, so that
8795 -- the Materialize_Entity flag will be set on the entity when
8796 -- Debug_Renaming_Declaration is called during analysis.
8798 if Comes_From_Source (Obj_Def_Id) then
8799 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8800 end if;
8802 Analyze (Obj_Decl);
8803 Replace_Renaming_Declaration_Id
8804 (Obj_Decl, Original_Node (Obj_Decl));
8805 end if;
8806 end Make_Build_In_Place_Call_In_Object_Declaration;
8808 -------------------------------------------------
8809 -- Make_Build_In_Place_Iface_Call_In_Allocator --
8810 -------------------------------------------------
8812 procedure Make_Build_In_Place_Iface_Call_In_Allocator
8813 (Allocator : Node_Id;
8814 Function_Call : Node_Id)
8816 BIP_Func_Call : constant Node_Id :=
8817 Unqual_BIP_Iface_Function_Call (Function_Call);
8818 Loc : constant Source_Ptr := Sloc (Function_Call);
8820 Anon_Type : Entity_Id;
8821 Tmp_Decl : Node_Id;
8822 Tmp_Id : Entity_Id;
8824 begin
8825 -- No action of the call has already been processed
8827 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8828 return;
8829 end if;
8831 Tmp_Id := Make_Temporary (Loc, 'D');
8833 -- Insert a temporary before N initialized with the BIP function call
8834 -- without its enclosing type conversions and analyze it without its
8835 -- expansion. This temporary facilitates us reusing the BIP machinery,
8836 -- which takes care of adding the extra build-in-place actuals and
8837 -- transforms this object declaration into an object renaming
8838 -- declaration.
8840 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
8841 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
8842 Set_Etype (Anon_Type, Anon_Type);
8844 Tmp_Decl :=
8845 Make_Object_Declaration (Loc,
8846 Defining_Identifier => Tmp_Id,
8847 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
8848 Expression =>
8849 Make_Allocator (Loc,
8850 Expression =>
8851 Make_Qualified_Expression (Loc,
8852 Subtype_Mark =>
8853 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8854 Expression => New_Copy_Tree (BIP_Func_Call))));
8856 Expander_Mode_Save_And_Set (False);
8857 Insert_Action (Allocator, Tmp_Decl);
8858 Expander_Mode_Restore;
8860 Make_Build_In_Place_Call_In_Allocator
8861 (Allocator => Expression (Tmp_Decl),
8862 Function_Call => Expression (Expression (Tmp_Decl)));
8864 Rewrite (Allocator, New_Occurrence_Of (Tmp_Id, Loc));
8865 end Make_Build_In_Place_Iface_Call_In_Allocator;
8867 ---------------------------------------------------------
8868 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
8869 ---------------------------------------------------------
8871 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
8872 (Function_Call : Node_Id)
8874 BIP_Func_Call : constant Node_Id :=
8875 Unqual_BIP_Iface_Function_Call (Function_Call);
8876 Loc : constant Source_Ptr := Sloc (Function_Call);
8878 Tmp_Decl : Node_Id;
8879 Tmp_Id : Entity_Id;
8881 begin
8882 -- No action of the call has already been processed
8884 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8885 return;
8886 end if;
8888 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
8890 -- Insert a temporary before the call initialized with function call to
8891 -- reuse the BIP machinery which takes care of adding the extra build-in
8892 -- place actuals and transforms this object declaration into an object
8893 -- renaming declaration.
8895 Tmp_Id := Make_Temporary (Loc, 'D');
8897 Tmp_Decl :=
8898 Make_Object_Declaration (Loc,
8899 Defining_Identifier => Tmp_Id,
8900 Object_Definition =>
8901 New_Occurrence_Of (Etype (Function_Call), Loc),
8902 Expression => Relocate_Node (Function_Call));
8904 Expander_Mode_Save_And_Set (False);
8905 Insert_Action (Function_Call, Tmp_Decl);
8906 Expander_Mode_Restore;
8908 Make_Build_In_Place_Iface_Call_In_Object_Declaration
8909 (Obj_Decl => Tmp_Decl,
8910 Function_Call => Expression (Tmp_Decl));
8911 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
8913 ----------------------------------------------------------
8914 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
8915 ----------------------------------------------------------
8917 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
8918 (Obj_Decl : Node_Id;
8919 Function_Call : Node_Id)
8921 BIP_Func_Call : constant Node_Id :=
8922 Unqual_BIP_Iface_Function_Call (Function_Call);
8923 Loc : constant Source_Ptr := Sloc (Function_Call);
8924 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8926 Tmp_Decl : Node_Id;
8927 Tmp_Id : Entity_Id;
8929 begin
8930 -- No action of the call has already been processed
8932 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8933 return;
8934 end if;
8936 Tmp_Id := Make_Temporary (Loc, 'D');
8938 -- Insert a temporary before N initialized with the BIP function call
8939 -- without its enclosing type conversions and analyze it without its
8940 -- expansion. This temporary facilitates us reusing the BIP machinery,
8941 -- which takes care of adding the extra build-in-place actuals and
8942 -- transforms this object declaration into an object renaming
8943 -- declaration.
8945 Tmp_Decl :=
8946 Make_Object_Declaration (Loc,
8947 Defining_Identifier => Tmp_Id,
8948 Object_Definition =>
8949 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8950 Expression => New_Copy_Tree (BIP_Func_Call));
8952 Expander_Mode_Save_And_Set (False);
8953 Insert_Action (Obj_Decl, Tmp_Decl);
8954 Expander_Mode_Restore;
8956 Make_Build_In_Place_Call_In_Object_Declaration
8957 (Obj_Decl => Tmp_Decl,
8958 Function_Call => Expression (Tmp_Decl));
8960 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
8962 -- Replace the original build-in-place function call by a reference to
8963 -- the resulting temporary object renaming declaration. In this way,
8964 -- all the interface conversions performed in the original Function_Call
8965 -- on the build-in-place object are preserved.
8967 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
8969 -- Replace the original object declaration by an internal object
8970 -- renaming declaration. This leaves the generated code more clean (the
8971 -- build-in-place function call in an object renaming declaration and
8972 -- displacements of the pointer to the build-in-place object in another
8973 -- renaming declaration) and allows us to invoke the routine that takes
8974 -- care of replacing the identifier of the renaming declaration (routine
8975 -- originally developed for the regular build-in-place management).
8977 Rewrite (Obj_Decl,
8978 Make_Object_Renaming_Declaration (Loc,
8979 Defining_Identifier => Make_Temporary (Loc, 'D'),
8980 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
8981 Name => Function_Call));
8982 Analyze (Obj_Decl);
8984 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
8985 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
8987 --------------------------------------------
8988 -- Make_CPP_Constructor_Call_In_Allocator --
8989 --------------------------------------------
8991 procedure Make_CPP_Constructor_Call_In_Allocator
8992 (Allocator : Node_Id;
8993 Function_Call : Node_Id)
8995 Loc : constant Source_Ptr := Sloc (Function_Call);
8996 Acc_Type : constant Entity_Id := Etype (Allocator);
8997 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8998 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9000 New_Allocator : Node_Id;
9001 Return_Obj_Access : Entity_Id;
9002 Tmp_Obj : Node_Id;
9004 begin
9005 pragma Assert (Nkind (Allocator) = N_Allocator
9006 and then Nkind (Function_Call) = N_Function_Call);
9007 pragma Assert (Convention (Function_Id) = Convention_CPP
9008 and then Is_Constructor (Function_Id));
9009 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9011 -- Replace the initialized allocator of form "new T'(Func (...))" with
9012 -- an uninitialized allocator of form "new T", where T is the result
9013 -- subtype of the called function. The call to the function is handled
9014 -- separately further below.
9016 New_Allocator :=
9017 Make_Allocator (Loc,
9018 Expression => New_Occurrence_Of (Result_Subt, Loc));
9019 Set_No_Initialization (New_Allocator);
9021 -- Copy attributes to new allocator. Note that the new allocator
9022 -- logically comes from source if the original one did, so copy the
9023 -- relevant flag. This ensures proper treatment of the restriction
9024 -- No_Implicit_Heap_Allocations in this case.
9026 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9027 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9028 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9030 Rewrite (Allocator, New_Allocator);
9032 -- Create a new access object and initialize it to the result of the
9033 -- new uninitialized allocator. Note: we do not use Allocator as the
9034 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9035 -- as this would create a sort of infinite "recursion".
9037 Return_Obj_Access := Make_Temporary (Loc, 'R');
9038 Set_Etype (Return_Obj_Access, Acc_Type);
9040 -- Generate:
9041 -- Rnnn : constant ptr_T := new (T);
9042 -- Init (Rnn.all,...);
9044 Tmp_Obj :=
9045 Make_Object_Declaration (Loc,
9046 Defining_Identifier => Return_Obj_Access,
9047 Constant_Present => True,
9048 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9049 Expression => Relocate_Node (Allocator));
9050 Insert_Action (Allocator, Tmp_Obj);
9052 Insert_List_After_And_Analyze (Tmp_Obj,
9053 Build_Initialization_Call (Loc,
9054 Id_Ref =>
9055 Make_Explicit_Dereference (Loc,
9056 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9057 Typ => Etype (Function_Id),
9058 Constructor_Ref => Function_Call));
9060 -- Finally, replace the allocator node with a reference to the result of
9061 -- the function call itself (which will effectively be an access to the
9062 -- object created by the allocator).
9064 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9066 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9067 -- generate an implicit conversion to force displacement of the "this"
9068 -- pointer.
9070 if Is_Interface (Designated_Type (Acc_Type)) then
9071 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9072 end if;
9074 Analyze_And_Resolve (Allocator, Acc_Type);
9075 end Make_CPP_Constructor_Call_In_Allocator;
9077 -----------------------------------
9078 -- Needs_BIP_Finalization_Master --
9079 -----------------------------------
9081 function Needs_BIP_Finalization_Master
9082 (Func_Id : Entity_Id) return Boolean
9084 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9085 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9086 begin
9087 -- A formal giving the finalization master is needed for build-in-place
9088 -- functions whose result type needs finalization or is a tagged type.
9089 -- Tagged primitive build-in-place functions need such a formal because
9090 -- they can be called by a dispatching call, and extensions may require
9091 -- finalization even if the root type doesn't. This means they're also
9092 -- needed for tagged nonprimitive build-in-place functions with tagged
9093 -- results, since such functions can be called via access-to-function
9094 -- types, and those can be used to call primitives, so masters have to
9095 -- be passed to all such build-in-place functions, primitive or not.
9097 return
9098 not Restriction_Active (No_Finalization)
9099 and then (Needs_Finalization (Func_Typ)
9100 or else Is_Tagged_Type (Func_Typ));
9101 end Needs_BIP_Finalization_Master;
9103 --------------------------
9104 -- Needs_BIP_Alloc_Form --
9105 --------------------------
9107 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9108 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9109 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9111 begin
9112 -- A build-in-place function needs to know which allocation form to
9113 -- use when:
9115 -- 1) The result subtype is unconstrained. In this case, depending on
9116 -- the context of the call, the object may need to be created in the
9117 -- secondary stack, the heap, or a user-defined storage pool.
9119 -- 2) The result subtype is tagged. In this case the function call may
9120 -- dispatch on result and thus needs to be treated in the same way as
9121 -- calls to functions with class-wide results, because a callee that
9122 -- can be dispatched to may have any of various result subtypes, so
9123 -- if any of the possible callees would require an allocation form to
9124 -- be passed then they all do.
9126 -- 3) The result subtype needs finalization actions. In this case, based
9127 -- on the context of the call, the object may need to be created at
9128 -- the caller site, in the heap, or in a user-defined storage pool.
9130 return
9131 not Is_Constrained (Func_Typ)
9132 or else Is_Tagged_Type (Func_Typ)
9133 or else Needs_Finalization (Func_Typ);
9134 end Needs_BIP_Alloc_Form;
9136 --------------------------------------
9137 -- Needs_Result_Accessibility_Level --
9138 --------------------------------------
9140 function Needs_Result_Accessibility_Level
9141 (Func_Id : Entity_Id) return Boolean
9143 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9145 function Has_Unconstrained_Access_Discriminant_Component
9146 (Comp_Typ : Entity_Id) return Boolean;
9147 -- Returns True if any component of the type has an unconstrained access
9148 -- discriminant.
9150 -----------------------------------------------------
9151 -- Has_Unconstrained_Access_Discriminant_Component --
9152 -----------------------------------------------------
9154 function Has_Unconstrained_Access_Discriminant_Component
9155 (Comp_Typ : Entity_Id) return Boolean
9157 begin
9158 if not Is_Limited_Type (Comp_Typ) then
9159 return False;
9161 -- Only limited types can have access discriminants with
9162 -- defaults.
9164 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9165 return True;
9167 elsif Is_Array_Type (Comp_Typ) then
9168 return Has_Unconstrained_Access_Discriminant_Component
9169 (Underlying_Type (Component_Type (Comp_Typ)));
9171 elsif Is_Record_Type (Comp_Typ) then
9172 declare
9173 Comp : Entity_Id;
9175 begin
9176 Comp := First_Component (Comp_Typ);
9177 while Present (Comp) loop
9178 if Has_Unconstrained_Access_Discriminant_Component
9179 (Underlying_Type (Etype (Comp)))
9180 then
9181 return True;
9182 end if;
9184 Next_Component (Comp);
9185 end loop;
9186 end;
9187 end if;
9189 return False;
9190 end Has_Unconstrained_Access_Discriminant_Component;
9192 Feature_Disabled : constant Boolean := True;
9193 -- Temporary
9195 -- Start of processing for Needs_Result_Accessibility_Level
9197 begin
9198 -- False if completion unavailable (how does this happen???)
9200 if not Present (Func_Typ) then
9201 return False;
9203 elsif Feature_Disabled then
9204 return False;
9206 -- False if not a function, also handle enum-lit renames case
9208 elsif Func_Typ = Standard_Void_Type
9209 or else Is_Scalar_Type (Func_Typ)
9210 then
9211 return False;
9213 -- Handle a corner case, a cross-dialect subp renaming. For example,
9214 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9215 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9217 elsif Present (Alias (Func_Id)) then
9219 -- Unimplemented: a cross-dialect subp renaming which does not set
9220 -- the Alias attribute (e.g., a rename of a dereference of an access
9221 -- to subprogram value). ???
9223 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9225 -- Remaining cases require Ada 2012 mode
9227 elsif Ada_Version < Ada_2012 then
9228 return False;
9230 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9231 or else Is_Tagged_Type (Func_Typ)
9232 then
9233 -- In the case of, say, a null tagged record result type, the need
9234 -- for this extra parameter might not be obvious. This function
9235 -- returns True for all tagged types for compatibility reasons.
9236 -- A function with, say, a tagged null controlling result type might
9237 -- be overridden by a primitive of an extension having an access
9238 -- discriminant and the overrider and overridden must have compatible
9239 -- calling conventions (including implicitly declared parameters).
9240 -- Similarly, values of one access-to-subprogram type might designate
9241 -- both a primitive subprogram of a given type and a function
9242 -- which is, for example, not a primitive subprogram of any type.
9243 -- Again, this requires calling convention compatibility.
9244 -- It might be possible to solve these issues by introducing
9245 -- wrappers, but that is not the approach that was chosen.
9247 return True;
9249 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9250 return True;
9252 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9253 return True;
9255 -- False for all other cases
9257 else
9258 return False;
9259 end if;
9260 end Needs_Result_Accessibility_Level;
9262 -------------------------------------
9263 -- Replace_Renaming_Declaration_Id --
9264 -------------------------------------
9266 procedure Replace_Renaming_Declaration_Id
9267 (New_Decl : Node_Id;
9268 Orig_Decl : Node_Id)
9270 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9271 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9273 begin
9274 Set_Chars (New_Id, Chars (Orig_Id));
9276 -- Swap next entity links in preparation for exchanging entities
9278 declare
9279 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9280 begin
9281 Link_Entities (New_Id, Next_Entity (Orig_Id));
9282 Link_Entities (Orig_Id, Next_Id);
9283 end;
9285 Set_Homonym (New_Id, Homonym (Orig_Id));
9286 Exchange_Entities (New_Id, Orig_Id);
9288 -- Preserve source indication of original declaration, so that xref
9289 -- information is properly generated for the right entity.
9291 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9292 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9294 Set_Comes_From_Source (New_Id, False);
9295 end Replace_Renaming_Declaration_Id;
9297 ---------------------------------
9298 -- Rewrite_Function_Call_For_C --
9299 ---------------------------------
9301 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9302 Orig_Func : constant Entity_Id := Entity (Name (N));
9303 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9304 Par : constant Node_Id := Parent (N);
9305 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9306 Loc : constant Source_Ptr := Sloc (Par);
9307 Actuals : List_Id;
9308 Last_Actual : Node_Id;
9309 Last_Formal : Entity_Id;
9311 -- Start of processing for Rewrite_Function_Call_For_C
9313 begin
9314 -- The actuals may be given by named associations, so the added actual
9315 -- that is the target of the return value of the call must be a named
9316 -- association as well, so we retrieve the name of the generated
9317 -- out_formal.
9319 Last_Formal := First_Formal (Proc_Id);
9320 while Present (Next_Formal (Last_Formal)) loop
9321 Last_Formal := Next_Formal (Last_Formal);
9322 end loop;
9324 Actuals := Parameter_Associations (N);
9326 -- The original function may lack parameters
9328 if No (Actuals) then
9329 Actuals := New_List;
9330 end if;
9332 -- If the function call is the expression of an assignment statement,
9333 -- transform the assignment into a procedure call. Generate:
9335 -- LHS := Func_Call (...);
9337 -- Proc_Call (..., LHS);
9339 -- If function is inherited, a conversion may be necessary.
9341 if Nkind (Par) = N_Assignment_Statement then
9342 Last_Actual := Name (Par);
9344 if not Comes_From_Source (Orig_Func)
9345 and then Etype (Orig_Func) /= Etype (Func_Id)
9346 then
9347 Last_Actual :=
9348 Make_Type_Conversion (Loc,
9349 New_Occurrence_Of (Etype (Func_Id), Loc),
9350 Last_Actual);
9351 end if;
9353 Append_To (Actuals,
9354 Make_Parameter_Association (Loc,
9355 Selector_Name =>
9356 Make_Identifier (Loc, Chars (Last_Formal)),
9357 Explicit_Actual_Parameter => Last_Actual));
9359 Rewrite (Par,
9360 Make_Procedure_Call_Statement (Loc,
9361 Name => New_Occurrence_Of (Proc_Id, Loc),
9362 Parameter_Associations => Actuals));
9363 Analyze (Par);
9365 -- Otherwise the context is an expression. Generate a temporary and a
9366 -- procedure call to obtain the function result. Generate:
9368 -- ... Func_Call (...) ...
9370 -- Temp : ...;
9371 -- Proc_Call (..., Temp);
9372 -- ... Temp ...
9374 else
9375 declare
9376 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9377 Call : Node_Id;
9378 Decl : Node_Id;
9380 begin
9381 -- Generate:
9382 -- Temp : ...;
9384 Decl :=
9385 Make_Object_Declaration (Loc,
9386 Defining_Identifier => Temp_Id,
9387 Object_Definition =>
9388 New_Occurrence_Of (Etype (Func_Id), Loc));
9390 -- Generate:
9391 -- Proc_Call (..., Temp);
9393 Append_To (Actuals,
9394 Make_Parameter_Association (Loc,
9395 Selector_Name =>
9396 Make_Identifier (Loc, Chars (Last_Formal)),
9397 Explicit_Actual_Parameter =>
9398 New_Occurrence_Of (Temp_Id, Loc)));
9400 Call :=
9401 Make_Procedure_Call_Statement (Loc,
9402 Name => New_Occurrence_Of (Proc_Id, Loc),
9403 Parameter_Associations => Actuals);
9405 Insert_Actions (Par, New_List (Decl, Call));
9406 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9407 end;
9408 end if;
9409 end Rewrite_Function_Call_For_C;
9411 ------------------------------------
9412 -- Set_Enclosing_Sec_Stack_Return --
9413 ------------------------------------
9415 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9416 P : Node_Id := N;
9418 begin
9419 -- Due to a possible mix of internally generated blocks, source blocks
9420 -- and loops, the scope stack may not be contiguous as all labels are
9421 -- inserted at the top level within the related function. Instead,
9422 -- perform a parent-based traversal and mark all appropriate constructs.
9424 while Present (P) loop
9426 -- Mark the label of a source or internally generated block or
9427 -- loop.
9429 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
9430 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9432 -- Mark the enclosing function
9434 elsif Nkind (P) = N_Subprogram_Body then
9435 if Present (Corresponding_Spec (P)) then
9436 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9437 else
9438 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9439 end if;
9441 -- Do not go beyond the enclosing function
9443 exit;
9444 end if;
9446 P := Parent (P);
9447 end loop;
9448 end Set_Enclosing_Sec_Stack_Return;
9450 ------------------------------------
9451 -- Unqual_BIP_Iface_Function_Call --
9452 ------------------------------------
9454 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9455 Has_Pointer_Displacement : Boolean := False;
9456 On_Object_Declaration : Boolean := False;
9457 -- Remember if processing the renaming expressions on recursion we have
9458 -- traversed an object declaration, since we can traverse many object
9459 -- declaration renamings but just one regular object declaration.
9461 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9462 -- Search for a build-in-place function call skipping any qualification
9463 -- including qualified expressions, type conversions, references, calls
9464 -- to displace the pointer to the object, and renamings. Return Empty if
9465 -- no build-in-place function call is found.
9467 ------------------------------
9468 -- Unqual_BIP_Function_Call --
9469 ------------------------------
9471 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9472 begin
9473 -- Recurse to handle case of multiple levels of qualification and/or
9474 -- conversion.
9476 if Nkind_In (Expr, N_Qualified_Expression,
9477 N_Type_Conversion,
9478 N_Unchecked_Type_Conversion)
9479 then
9480 return Unqual_BIP_Function_Call (Expression (Expr));
9482 -- Recurse to handle case of multiple levels of references and
9483 -- explicit dereferences.
9485 elsif Nkind_In (Expr, N_Attribute_Reference,
9486 N_Explicit_Dereference,
9487 N_Reference)
9488 then
9489 return Unqual_BIP_Function_Call (Prefix (Expr));
9491 -- Recurse on object renamings
9493 elsif Nkind (Expr) = N_Identifier
9494 and then Present (Entity (Expr))
9495 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9496 and then Nkind (Parent (Entity (Expr))) =
9497 N_Object_Renaming_Declaration
9498 and then Present (Renamed_Object (Entity (Expr)))
9499 then
9500 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9502 -- Recurse on the initializing expression of the first reference of
9503 -- an object declaration.
9505 elsif not On_Object_Declaration
9506 and then Nkind (Expr) = N_Identifier
9507 and then Present (Entity (Expr))
9508 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9509 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9510 and then Present (Expression (Parent (Entity (Expr))))
9511 then
9512 On_Object_Declaration := True;
9513 return
9514 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9516 -- Recurse to handle calls to displace the pointer to the object to
9517 -- reference a secondary dispatch table.
9519 elsif Nkind (Expr) = N_Function_Call
9520 and then Nkind (Name (Expr)) in N_Has_Entity
9521 and then Present (Entity (Name (Expr)))
9522 and then RTU_Loaded (Ada_Tags)
9523 and then RTE_Available (RE_Displace)
9524 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9525 then
9526 Has_Pointer_Displacement := True;
9527 return
9528 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9530 -- Normal case: check if the inner expression is a BIP function call
9531 -- and the pointer to the object is displaced.
9533 elsif Has_Pointer_Displacement
9534 and then Is_Build_In_Place_Function_Call (Expr)
9535 then
9536 return Expr;
9538 else
9539 return Empty;
9540 end if;
9541 end Unqual_BIP_Function_Call;
9543 -- Start of processing for Unqual_BIP_Iface_Function_Call
9545 begin
9546 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9548 -- Can happen for X'Elab_Spec in the binder-generated file
9550 return Empty;
9551 end if;
9553 return Unqual_BIP_Function_Call (Expr);
9554 end Unqual_BIP_Iface_Function_Call;
9556 end Exp_Ch6;