4a928965bf627e2065df3f4849a61328c4486873
[gcc.git] / gcc / ada / exp_ch6.adb
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-2014, 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 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Atag; use Exp_Atag;
35 with Exp_Ch2; use Exp_Ch2;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Dbug; use Exp_Dbug;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Intr; use Exp_Intr;
43 with Exp_Pakd; use Exp_Pakd;
44 with Exp_Prag; use Exp_Prag;
45 with Exp_Tss; use Exp_Tss;
46 with Exp_Util; use Exp_Util;
47 with Exp_VFpt; use Exp_VFpt;
48 with Fname; use Fname;
49 with Freeze; use Freeze;
50 with Inline; use Inline;
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 Output; use Output;
57 with Restrict; use Restrict;
58 with Rident; use Rident;
59 with Rtsfind; use Rtsfind;
60 with Sem; use Sem;
61 with Sem_Aux; use Sem_Aux;
62 with Sem_Ch6; use Sem_Ch6;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch12; use Sem_Ch12;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_SCIL; use Sem_SCIL;
73 with Sem_Util; use Sem_Util;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Stand; use Stand;
78 with Stringt; use Stringt;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Validsw; use Validsw;
83
84 package body Exp_Ch6 is
85
86 Inlined_Calls : Elist_Id := No_Elist;
87 Backend_Calls : Elist_Id := No_Elist;
88 -- List of frontend inlined calls and inline calls passed to the backend
89
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
93
94 procedure Add_Access_Actual_To_Build_In_Place_Call
95 (Function_Call : Node_Id;
96 Function_Id : Entity_Id;
97 Return_Object : Node_Id;
98 Is_Access : Boolean := False);
99 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
100 -- object name given by Return_Object and add the attribute to the end of
101 -- the actual parameter list associated with the build-in-place function
102 -- call denoted by Function_Call. However, if Is_Access is True, then
103 -- Return_Object is already an access expression, in which case it's passed
104 -- along directly to the build-in-place function. Finally, if Return_Object
105 -- is empty, then pass a null literal as the actual.
106
107 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
108 (Function_Call : Node_Id;
109 Function_Id : Entity_Id;
110 Alloc_Form : BIP_Allocation_Form := Unspecified;
111 Alloc_Form_Exp : Node_Id := Empty;
112 Pool_Actual : Node_Id := Make_Null (No_Location));
113 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
114 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
115 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
116 -- otherwise pass a literal corresponding to the Alloc_Form parameter
117 -- (which must not be Unspecified in that case). Pool_Actual is the
118 -- parameter to pass to BIP_Storage_Pool.
119
120 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
121 (Func_Call : Node_Id;
122 Func_Id : Entity_Id;
123 Ptr_Typ : Entity_Id := Empty;
124 Master_Exp : Node_Id := Empty);
125 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
126 -- finalization actions, add an actual parameter which is a pointer to the
127 -- finalization master of the caller. If Master_Exp is not Empty, then that
128 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
129 -- will result in an automatic "null" value for the actual.
130
131 procedure Add_Task_Actuals_To_Build_In_Place_Call
132 (Function_Call : Node_Id;
133 Function_Id : Entity_Id;
134 Master_Actual : Node_Id);
135 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
136 -- contains tasks, add two actual parameters: the master, and a pointer to
137 -- the caller's activation chain. Master_Actual is the actual parameter
138 -- expression to pass for the master. In most cases, this is the current
139 -- master (_master). The two exceptions are: If the function call is the
140 -- initialization expression for an allocator, we pass the master of the
141 -- access type. If the function call is the initialization expression for a
142 -- return object, we pass along the master passed in by the caller. The
143 -- activation chain to pass is always the local one. Note: Master_Actual
144 -- can be Empty, but only if there are no tasks.
145
146 procedure Check_Overriding_Operation (Subp : Entity_Id);
147 -- Subp is a dispatching operation. Check whether it may override an
148 -- inherited private operation, in which case its DT entry is that of
149 -- the hidden operation, not the one it may have received earlier.
150 -- This must be done before emitting the code to set the corresponding
151 -- DT to the address of the subprogram. The actual placement of Subp in
152 -- the proper place in the list of primitive operations is done in
153 -- Declare_Inherited_Private_Subprograms, which also has to deal with
154 -- implicit operations. This duplication is unavoidable for now???
155
156 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
157 -- This procedure is called only if the subprogram body N, whose spec
158 -- has the given entity Spec, contains a parameterless recursive call.
159 -- It attempts to generate runtime code to detect if this a case of
160 -- infinite recursion.
161 --
162 -- The body is scanned to determine dependencies. If the only external
163 -- dependencies are on a small set of scalar variables, then the values
164 -- of these variables are captured on entry to the subprogram, and if
165 -- the values are not changed for the call, we know immediately that
166 -- we have an infinite recursion.
167
168 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id);
169 -- For each actual of an in-out or out parameter which is a numeric
170 -- (view) conversion of the form T (A), where A denotes a variable,
171 -- we insert the declaration:
172 --
173 -- Temp : T[ := T (A)];
174 --
175 -- prior to the call. Then we replace the actual with a reference to Temp,
176 -- and append the assignment:
177 --
178 -- A := TypeA (Temp);
179 --
180 -- after the call. Here TypeA is the actual type of variable A. For out
181 -- parameters, the initial declaration has no expression. If A is not an
182 -- entity name, we generate instead:
183 --
184 -- Var : TypeA renames A;
185 -- Temp : T := Var; -- omitting expression for out parameter.
186 -- ...
187 -- Var := TypeA (Temp);
188 --
189 -- For other in-out parameters, we emit the required constraint checks
190 -- before and/or after the call.
191 --
192 -- For all parameter modes, actuals that denote components and slices of
193 -- packed arrays are expanded into suitable temporaries.
194 --
195 -- For non-scalar objects that are possibly unaligned, add call by copy
196 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
197 --
198 -- The parameter N is IN OUT because in some cases, the expansion code
199 -- rewrites the call as an expression actions with the call inside. In
200 -- this case N is reset to point to the inside call so that the caller
201 -- can continue processing of this call.
202
203 procedure Expand_Ctrl_Function_Call (N : Node_Id);
204 -- N is a function call which returns a controlled object. Transform the
205 -- call into a temporary which retrieves the returned object from the
206 -- secondary stack using 'reference.
207
208 procedure Expand_Inlined_Call
209 (N : Node_Id;
210 Subp : Entity_Id;
211 Orig_Subp : Entity_Id);
212 -- If called subprogram can be inlined by the front-end, retrieve the
213 -- analyzed body, replace formals with actuals and expand call in place.
214 -- Generate thunks for actuals that are expressions, and insert the
215 -- corresponding constant declarations before the call. If the original
216 -- call is to a derived operation, the return type is the one of the
217 -- derived operation, but the body is that of the original, so return
218 -- expressions in the body must be converted to the desired type (which
219 -- is simply not noted in the tree without inline expansion).
220
221 procedure Expand_Non_Function_Return (N : Node_Id);
222 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
223 -- a procedure body, entry body, accept statement, or extended return
224 -- statement. Note that all non-function returns are simple return
225 -- statements.
226
227 function Expand_Protected_Object_Reference
228 (N : Node_Id;
229 Scop : Entity_Id) return Node_Id;
230
231 procedure Expand_Protected_Subprogram_Call
232 (N : Node_Id;
233 Subp : Entity_Id;
234 Scop : Entity_Id);
235 -- A call to a protected subprogram within the protected object may appear
236 -- as a regular call. The list of actuals must be expanded to contain a
237 -- reference to the object itself, and the call becomes a call to the
238 -- corresponding protected subprogram.
239
240 function Has_Unconstrained_Access_Discriminants
241 (Subtyp : Entity_Id) return Boolean;
242 -- Returns True if the given subtype is unconstrained and has one
243 -- or more access discriminants.
244
245 procedure Expand_Simple_Function_Return (N : Node_Id);
246 -- Expand simple return from function. In the case where we are returning
247 -- from a function body this is called by Expand_N_Simple_Return_Statement.
248
249 ----------------------------------------------
250 -- Add_Access_Actual_To_Build_In_Place_Call --
251 ----------------------------------------------
252
253 procedure Add_Access_Actual_To_Build_In_Place_Call
254 (Function_Call : Node_Id;
255 Function_Id : Entity_Id;
256 Return_Object : Node_Id;
257 Is_Access : Boolean := False)
258 is
259 Loc : constant Source_Ptr := Sloc (Function_Call);
260 Obj_Address : Node_Id;
261 Obj_Acc_Formal : Entity_Id;
262
263 begin
264 -- Locate the implicit access parameter in the called function
265
266 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
267
268 -- If no return object is provided, then pass null
269
270 if not Present (Return_Object) then
271 Obj_Address := Make_Null (Loc);
272 Set_Parent (Obj_Address, Function_Call);
273
274 -- If Return_Object is already an expression of an access type, then use
275 -- it directly, since it must be an access value denoting the return
276 -- object, and couldn't possibly be the return object itself.
277
278 elsif Is_Access then
279 Obj_Address := Return_Object;
280 Set_Parent (Obj_Address, Function_Call);
281
282 -- Apply Unrestricted_Access to caller's return object
283
284 else
285 Obj_Address :=
286 Make_Attribute_Reference (Loc,
287 Prefix => Return_Object,
288 Attribute_Name => Name_Unrestricted_Access);
289
290 Set_Parent (Return_Object, Obj_Address);
291 Set_Parent (Obj_Address, Function_Call);
292 end if;
293
294 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
295
296 -- Build the parameter association for the new actual and add it to the
297 -- end of the function's actuals.
298
299 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
300 end Add_Access_Actual_To_Build_In_Place_Call;
301
302 ------------------------------------------------------
303 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
304 ------------------------------------------------------
305
306 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
307 (Function_Call : Node_Id;
308 Function_Id : Entity_Id;
309 Alloc_Form : BIP_Allocation_Form := Unspecified;
310 Alloc_Form_Exp : Node_Id := Empty;
311 Pool_Actual : Node_Id := Make_Null (No_Location))
312 is
313 Loc : constant Source_Ptr := Sloc (Function_Call);
314 Alloc_Form_Actual : Node_Id;
315 Alloc_Form_Formal : Node_Id;
316 Pool_Formal : Node_Id;
317
318 begin
319 -- The allocation form generally doesn't need to be passed in the case
320 -- of a constrained result subtype, since normally the caller performs
321 -- the allocation in that case. However this formal is still needed in
322 -- the case where the function has a tagged result, because generally
323 -- such functions can be called in a dispatching context and such calls
324 -- must be handled like calls to class-wide functions.
325
326 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
327 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
328 then
329 return;
330 end if;
331
332 -- Locate the implicit allocation form parameter in the called function.
333 -- Maybe it would be better for each implicit formal of a build-in-place
334 -- function to have a flag or a Uint attribute to identify it. ???
335
336 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
337
338 if Present (Alloc_Form_Exp) then
339 pragma Assert (Alloc_Form = Unspecified);
340
341 Alloc_Form_Actual := Alloc_Form_Exp;
342
343 else
344 pragma Assert (Alloc_Form /= Unspecified);
345
346 Alloc_Form_Actual :=
347 Make_Integer_Literal (Loc,
348 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
349 end if;
350
351 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
352
353 -- Build the parameter association for the new actual and add it to the
354 -- end of the function's actuals.
355
356 Add_Extra_Actual_To_Call
357 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
358
359 -- Pass the Storage_Pool parameter. This parameter is omitted on
360 -- .NET/JVM/ZFP as those targets do not support pools.
361
362 if VM_Target = No_VM
363 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
364 then
365 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
366 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
367 Add_Extra_Actual_To_Call
368 (Function_Call, Pool_Formal, Pool_Actual);
369 end if;
370 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
371
372 -----------------------------------------------------------
373 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
374 -----------------------------------------------------------
375
376 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
377 (Func_Call : Node_Id;
378 Func_Id : Entity_Id;
379 Ptr_Typ : Entity_Id := Empty;
380 Master_Exp : Node_Id := Empty)
381 is
382 begin
383 if not Needs_BIP_Finalization_Master (Func_Id) then
384 return;
385 end if;
386
387 declare
388 Formal : constant Entity_Id :=
389 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
390 Loc : constant Source_Ptr := Sloc (Func_Call);
391
392 Actual : Node_Id;
393 Desig_Typ : Entity_Id;
394
395 begin
396 -- If there is a finalization master actual, such as the implicit
397 -- finalization master of an enclosing build-in-place function,
398 -- then this must be added as an extra actual of the call.
399
400 if Present (Master_Exp) then
401 Actual := Master_Exp;
402
403 -- Case where the context does not require an actual master
404
405 elsif No (Ptr_Typ) then
406 Actual := Make_Null (Loc);
407
408 else
409 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
410
411 -- Check for a library-level access type whose designated type has
412 -- supressed finalization. Such an access types lack a master.
413 -- Pass a null actual to the callee in order to signal a missing
414 -- master.
415
416 if Is_Library_Level_Entity (Ptr_Typ)
417 and then Finalize_Storage_Only (Desig_Typ)
418 then
419 Actual := Make_Null (Loc);
420
421 -- Types in need of finalization actions
422
423 elsif Needs_Finalization (Desig_Typ) then
424
425 -- The general mechanism of creating finalization masters for
426 -- anonymous access types is disabled by default, otherwise
427 -- finalization masters will pop all over the place. Such types
428 -- use context-specific masters.
429
430 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
431 and then No (Finalization_Master (Ptr_Typ))
432 then
433 Build_Finalization_Master
434 (Typ => Ptr_Typ,
435 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
436 Encl_Scope => Scope (Ptr_Typ));
437 end if;
438
439 -- Access-to-controlled types should always have a master
440
441 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
442
443 Actual :=
444 Make_Attribute_Reference (Loc,
445 Prefix =>
446 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
447 Attribute_Name => Name_Unrestricted_Access);
448
449 -- Tagged types
450
451 else
452 Actual := Make_Null (Loc);
453 end if;
454 end if;
455
456 Analyze_And_Resolve (Actual, Etype (Formal));
457
458 -- Build the parameter association for the new actual and add it to
459 -- the end of the function's actuals.
460
461 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
462 end;
463 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
464
465 ------------------------------
466 -- Add_Extra_Actual_To_Call --
467 ------------------------------
468
469 procedure Add_Extra_Actual_To_Call
470 (Subprogram_Call : Node_Id;
471 Extra_Formal : Entity_Id;
472 Extra_Actual : Node_Id)
473 is
474 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
475 Param_Assoc : Node_Id;
476
477 begin
478 Param_Assoc :=
479 Make_Parameter_Association (Loc,
480 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
481 Explicit_Actual_Parameter => Extra_Actual);
482
483 Set_Parent (Param_Assoc, Subprogram_Call);
484 Set_Parent (Extra_Actual, Param_Assoc);
485
486 if Present (Parameter_Associations (Subprogram_Call)) then
487 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
488 N_Parameter_Association
489 then
490
491 -- Find last named actual, and append
492
493 declare
494 L : Node_Id;
495 begin
496 L := First_Actual (Subprogram_Call);
497 while Present (L) loop
498 if No (Next_Actual (L)) then
499 Set_Next_Named_Actual (Parent (L), Extra_Actual);
500 exit;
501 end if;
502 Next_Actual (L);
503 end loop;
504 end;
505
506 else
507 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
508 end if;
509
510 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
511
512 else
513 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
514 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
515 end if;
516 end Add_Extra_Actual_To_Call;
517
518 ---------------------------------------------
519 -- Add_Task_Actuals_To_Build_In_Place_Call --
520 ---------------------------------------------
521
522 procedure Add_Task_Actuals_To_Build_In_Place_Call
523 (Function_Call : Node_Id;
524 Function_Id : Entity_Id;
525 Master_Actual : Node_Id)
526 is
527 Loc : constant Source_Ptr := Sloc (Function_Call);
528 Result_Subt : constant Entity_Id :=
529 Available_View (Etype (Function_Id));
530 Actual : Node_Id;
531 Chain_Actual : Node_Id;
532 Chain_Formal : Node_Id;
533 Master_Formal : Node_Id;
534
535 begin
536 -- No such extra parameters are needed if there are no tasks
537
538 if not Has_Task (Result_Subt) then
539 return;
540 end if;
541
542 Actual := Master_Actual;
543
544 -- Use a dummy _master actual in case of No_Task_Hierarchy
545
546 if Restriction_Active (No_Task_Hierarchy) then
547 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
548
549 -- In the case where we use the master associated with an access type,
550 -- the actual is an entity and requires an explicit reference.
551
552 elsif Nkind (Actual) = N_Defining_Identifier then
553 Actual := New_Occurrence_Of (Actual, Loc);
554 end if;
555
556 -- Locate the implicit master parameter in the called function
557
558 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
559 Analyze_And_Resolve (Actual, Etype (Master_Formal));
560
561 -- Build the parameter association for the new actual and add it to the
562 -- end of the function's actuals.
563
564 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
565
566 -- Locate the implicit activation chain parameter in the called function
567
568 Chain_Formal :=
569 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
570
571 -- Create the actual which is a pointer to the current activation chain
572
573 Chain_Actual :=
574 Make_Attribute_Reference (Loc,
575 Prefix => Make_Identifier (Loc, Name_uChain),
576 Attribute_Name => Name_Unrestricted_Access);
577
578 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
579
580 -- Build the parameter association for the new actual and add it to the
581 -- end of the function's actuals.
582
583 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
584 end Add_Task_Actuals_To_Build_In_Place_Call;
585
586 -----------------------
587 -- BIP_Formal_Suffix --
588 -----------------------
589
590 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
591 begin
592 case Kind is
593 when BIP_Alloc_Form =>
594 return "BIPalloc";
595 when BIP_Storage_Pool =>
596 return "BIPstoragepool";
597 when BIP_Finalization_Master =>
598 return "BIPfinalizationmaster";
599 when BIP_Task_Master =>
600 return "BIPtaskmaster";
601 when BIP_Activation_Chain =>
602 return "BIPactivationchain";
603 when BIP_Object_Access =>
604 return "BIPaccess";
605 end case;
606 end BIP_Formal_Suffix;
607
608 ---------------------------
609 -- Build_In_Place_Formal --
610 ---------------------------
611
612 function Build_In_Place_Formal
613 (Func : Entity_Id;
614 Kind : BIP_Formal_Kind) return Entity_Id
615 is
616 Formal_Name : constant Name_Id :=
617 New_External_Name
618 (Chars (Func), BIP_Formal_Suffix (Kind));
619 Extra_Formal : Entity_Id := Extra_Formals (Func);
620
621 begin
622 -- Maybe it would be better for each implicit formal of a build-in-place
623 -- function to have a flag or a Uint attribute to identify it. ???
624
625 -- The return type in the function declaration may have been a limited
626 -- view, and the extra formals for the function were not generated at
627 -- that point. At the point of call the full view must be available and
628 -- the extra formals can be created.
629
630 if No (Extra_Formal) then
631 Create_Extra_Formals (Func);
632 Extra_Formal := Extra_Formals (Func);
633 end if;
634
635 loop
636 pragma Assert (Present (Extra_Formal));
637 exit when Chars (Extra_Formal) = Formal_Name;
638
639 Next_Formal_With_Extras (Extra_Formal);
640 end loop;
641
642 return Extra_Formal;
643 end Build_In_Place_Formal;
644
645 --------------------------------
646 -- Check_Overriding_Operation --
647 --------------------------------
648
649 procedure Check_Overriding_Operation (Subp : Entity_Id) is
650 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
651 Op_List : constant Elist_Id := Primitive_Operations (Typ);
652 Op_Elmt : Elmt_Id;
653 Prim_Op : Entity_Id;
654 Par_Op : Entity_Id;
655
656 begin
657 if Is_Derived_Type (Typ)
658 and then not Is_Private_Type (Typ)
659 and then In_Open_Scopes (Scope (Etype (Typ)))
660 and then Is_Base_Type (Typ)
661 then
662 -- Subp overrides an inherited private operation if there is an
663 -- inherited operation with a different name than Subp (see
664 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
665 -- same name as Subp.
666
667 Op_Elmt := First_Elmt (Op_List);
668 while Present (Op_Elmt) loop
669 Prim_Op := Node (Op_Elmt);
670 Par_Op := Alias (Prim_Op);
671
672 if Present (Par_Op)
673 and then not Comes_From_Source (Prim_Op)
674 and then Chars (Prim_Op) /= Chars (Par_Op)
675 and then Chars (Par_Op) = Chars (Subp)
676 and then Is_Hidden (Par_Op)
677 and then Type_Conformant (Prim_Op, Subp)
678 then
679 Set_DT_Position (Subp, DT_Position (Prim_Op));
680 end if;
681
682 Next_Elmt (Op_Elmt);
683 end loop;
684 end if;
685 end Check_Overriding_Operation;
686
687 -------------------------------
688 -- Detect_Infinite_Recursion --
689 -------------------------------
690
691 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
692 Loc : constant Source_Ptr := Sloc (N);
693
694 Var_List : constant Elist_Id := New_Elmt_List;
695 -- List of globals referenced by body of procedure
696
697 Call_List : constant Elist_Id := New_Elmt_List;
698 -- List of recursive calls in body of procedure
699
700 Shad_List : constant Elist_Id := New_Elmt_List;
701 -- List of entity id's for entities created to capture the value of
702 -- referenced globals on entry to the procedure.
703
704 Scop : constant Uint := Scope_Depth (Spec);
705 -- This is used to record the scope depth of the current procedure, so
706 -- that we can identify global references.
707
708 Max_Vars : constant := 4;
709 -- Do not test more than four global variables
710
711 Count_Vars : Natural := 0;
712 -- Count variables found so far
713
714 Var : Entity_Id;
715 Elm : Elmt_Id;
716 Ent : Entity_Id;
717 Call : Elmt_Id;
718 Decl : Node_Id;
719 Test : Node_Id;
720 Elm1 : Elmt_Id;
721 Elm2 : Elmt_Id;
722 Last : Node_Id;
723
724 function Process (Nod : Node_Id) return Traverse_Result;
725 -- Function to traverse the subprogram body (using Traverse_Func)
726
727 -------------
728 -- Process --
729 -------------
730
731 function Process (Nod : Node_Id) return Traverse_Result is
732 begin
733 -- Procedure call
734
735 if Nkind (Nod) = N_Procedure_Call_Statement then
736
737 -- Case of one of the detected recursive calls
738
739 if Is_Entity_Name (Name (Nod))
740 and then Has_Recursive_Call (Entity (Name (Nod)))
741 and then Entity (Name (Nod)) = Spec
742 then
743 Append_Elmt (Nod, Call_List);
744 return Skip;
745
746 -- Any other procedure call may have side effects
747
748 else
749 return Abandon;
750 end if;
751
752 -- A call to a pure function can always be ignored
753
754 elsif Nkind (Nod) = N_Function_Call
755 and then Is_Entity_Name (Name (Nod))
756 and then Is_Pure (Entity (Name (Nod)))
757 then
758 return Skip;
759
760 -- Case of an identifier reference
761
762 elsif Nkind (Nod) = N_Identifier then
763 Ent := Entity (Nod);
764
765 -- If no entity, then ignore the reference
766
767 -- Not clear why this can happen. To investigate, remove this
768 -- test and look at the crash that occurs here in 3401-004 ???
769
770 if No (Ent) then
771 return Skip;
772
773 -- Ignore entities with no Scope, again not clear how this
774 -- can happen, to investigate, look at 4108-008 ???
775
776 elsif No (Scope (Ent)) then
777 return Skip;
778
779 -- Ignore the reference if not to a more global object
780
781 elsif Scope_Depth (Scope (Ent)) >= Scop then
782 return Skip;
783
784 -- References to types, exceptions and constants are always OK
785
786 elsif Is_Type (Ent)
787 or else Ekind (Ent) = E_Exception
788 or else Ekind (Ent) = E_Constant
789 then
790 return Skip;
791
792 -- If other than a non-volatile scalar variable, we have some
793 -- kind of global reference (e.g. to a function) that we cannot
794 -- deal with so we forget the attempt.
795
796 elsif Ekind (Ent) /= E_Variable
797 or else not Is_Scalar_Type (Etype (Ent))
798 or else Treat_As_Volatile (Ent)
799 then
800 return Abandon;
801
802 -- Otherwise we have a reference to a global scalar
803
804 else
805 -- Loop through global entities already detected
806
807 Elm := First_Elmt (Var_List);
808 loop
809 -- If not detected before, record this new global reference
810
811 if No (Elm) then
812 Count_Vars := Count_Vars + 1;
813
814 if Count_Vars <= Max_Vars then
815 Append_Elmt (Entity (Nod), Var_List);
816 else
817 return Abandon;
818 end if;
819
820 exit;
821
822 -- If recorded before, ignore
823
824 elsif Node (Elm) = Entity (Nod) then
825 return Skip;
826
827 -- Otherwise keep looking
828
829 else
830 Next_Elmt (Elm);
831 end if;
832 end loop;
833
834 return Skip;
835 end if;
836
837 -- For all other node kinds, recursively visit syntactic children
838
839 else
840 return OK;
841 end if;
842 end Process;
843
844 function Traverse_Body is new Traverse_Func (Process);
845
846 -- Start of processing for Detect_Infinite_Recursion
847
848 begin
849 -- Do not attempt detection in No_Implicit_Conditional mode, since we
850 -- won't be able to generate the code to handle the recursion in any
851 -- case.
852
853 if Restriction_Active (No_Implicit_Conditionals) then
854 return;
855 end if;
856
857 -- Otherwise do traversal and quit if we get abandon signal
858
859 if Traverse_Body (N) = Abandon then
860 return;
861
862 -- We must have a call, since Has_Recursive_Call was set. If not just
863 -- ignore (this is only an error check, so if we have a funny situation,
864 -- due to bugs or errors, we do not want to bomb).
865
866 elsif Is_Empty_Elmt_List (Call_List) then
867 return;
868 end if;
869
870 -- Here is the case where we detect recursion at compile time
871
872 -- Push our current scope for analyzing the declarations and code that
873 -- we will insert for the checking.
874
875 Push_Scope (Spec);
876
877 -- This loop builds temporary variables for each of the referenced
878 -- globals, so that at the end of the loop the list Shad_List contains
879 -- these temporaries in one-to-one correspondence with the elements in
880 -- Var_List.
881
882 Last := Empty;
883 Elm := First_Elmt (Var_List);
884 while Present (Elm) loop
885 Var := Node (Elm);
886 Ent := Make_Temporary (Loc, 'S');
887 Append_Elmt (Ent, Shad_List);
888
889 -- Insert a declaration for this temporary at the start of the
890 -- declarations for the procedure. The temporaries are declared as
891 -- constant objects initialized to the current values of the
892 -- corresponding temporaries.
893
894 Decl :=
895 Make_Object_Declaration (Loc,
896 Defining_Identifier => Ent,
897 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
898 Constant_Present => True,
899 Expression => New_Occurrence_Of (Var, Loc));
900
901 if No (Last) then
902 Prepend (Decl, Declarations (N));
903 else
904 Insert_After (Last, Decl);
905 end if;
906
907 Last := Decl;
908 Analyze (Decl);
909 Next_Elmt (Elm);
910 end loop;
911
912 -- Loop through calls
913
914 Call := First_Elmt (Call_List);
915 while Present (Call) loop
916
917 -- Build a predicate expression of the form
918
919 -- True
920 -- and then global1 = temp1
921 -- and then global2 = temp2
922 -- ...
923
924 -- This predicate determines if any of the global values
925 -- referenced by the procedure have changed since the
926 -- current call, if not an infinite recursion is assured.
927
928 Test := New_Occurrence_Of (Standard_True, Loc);
929
930 Elm1 := First_Elmt (Var_List);
931 Elm2 := First_Elmt (Shad_List);
932 while Present (Elm1) loop
933 Test :=
934 Make_And_Then (Loc,
935 Left_Opnd => Test,
936 Right_Opnd =>
937 Make_Op_Eq (Loc,
938 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
939 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
940
941 Next_Elmt (Elm1);
942 Next_Elmt (Elm2);
943 end loop;
944
945 -- Now we replace the call with the sequence
946
947 -- if no-changes (see above) then
948 -- raise Storage_Error;
949 -- else
950 -- original-call
951 -- end if;
952
953 Rewrite (Node (Call),
954 Make_If_Statement (Loc,
955 Condition => Test,
956 Then_Statements => New_List (
957 Make_Raise_Storage_Error (Loc,
958 Reason => SE_Infinite_Recursion)),
959
960 Else_Statements => New_List (
961 Relocate_Node (Node (Call)))));
962
963 Analyze (Node (Call));
964
965 Next_Elmt (Call);
966 end loop;
967
968 -- Remove temporary scope stack entry used for analysis
969
970 Pop_Scope;
971 end Detect_Infinite_Recursion;
972
973 --------------------
974 -- Expand_Actuals --
975 --------------------
976
977 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
978 Loc : constant Source_Ptr := Sloc (N);
979 Actual : Node_Id;
980 Formal : Entity_Id;
981 N_Node : Node_Id;
982 Post_Call : List_Id;
983 E_Actual : Entity_Id;
984 E_Formal : Entity_Id;
985
986 procedure Add_Call_By_Copy_Code;
987 -- For cases where the parameter must be passed by copy, this routine
988 -- generates a temporary variable into which the actual is copied and
989 -- then passes this as the parameter. For an OUT or IN OUT parameter,
990 -- an assignment is also generated to copy the result back. The call
991 -- also takes care of any constraint checks required for the type
992 -- conversion case (on both the way in and the way out).
993
994 procedure Add_Simple_Call_By_Copy_Code;
995 -- This is similar to the above, but is used in cases where we know
996 -- that all that is needed is to simply create a temporary and copy
997 -- the value in and out of the temporary.
998
999 procedure Check_Fortran_Logical;
1000 -- A value of type Logical that is passed through a formal parameter
1001 -- must be normalized because .TRUE. usually does not have the same
1002 -- representation as True. We assume that .FALSE. = False = 0.
1003 -- What about functions that return a logical type ???
1004
1005 function Is_Legal_Copy return Boolean;
1006 -- Check that an actual can be copied before generating the temporary
1007 -- to be used in the call. If the actual is of a by_reference type then
1008 -- the program is illegal (this can only happen in the presence of
1009 -- rep. clauses that force an incorrect alignment). If the formal is
1010 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1011 -- the effect that this might lead to unaligned arguments.
1012
1013 function Make_Var (Actual : Node_Id) return Entity_Id;
1014 -- Returns an entity that refers to the given actual parameter, Actual
1015 -- (not including any type conversion). If Actual is an entity name,
1016 -- then this entity is returned unchanged, otherwise a renaming is
1017 -- created to provide an entity for the actual.
1018
1019 procedure Reset_Packed_Prefix;
1020 -- The expansion of a packed array component reference is delayed in
1021 -- the context of a call. Now we need to complete the expansion, so we
1022 -- unmark the analyzed bits in all prefixes.
1023
1024 ---------------------------
1025 -- Add_Call_By_Copy_Code --
1026 ---------------------------
1027
1028 procedure Add_Call_By_Copy_Code is
1029 Expr : Node_Id;
1030 Init : Node_Id;
1031 Temp : Entity_Id;
1032 Indic : Node_Id;
1033 Var : Entity_Id;
1034 F_Typ : constant Entity_Id := Etype (Formal);
1035 V_Typ : Entity_Id;
1036 Crep : Boolean;
1037
1038 begin
1039 if not Is_Legal_Copy then
1040 return;
1041 end if;
1042
1043 Temp := Make_Temporary (Loc, 'T', Actual);
1044
1045 -- Use formal type for temp, unless formal type is an unconstrained
1046 -- array, in which case we don't have to worry about bounds checks,
1047 -- and we use the actual type, since that has appropriate bounds.
1048
1049 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1050 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1051 else
1052 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1053 end if;
1054
1055 if Nkind (Actual) = N_Type_Conversion then
1056 V_Typ := Etype (Expression (Actual));
1057
1058 -- If the formal is an (in-)out parameter, capture the name
1059 -- of the variable in order to build the post-call assignment.
1060
1061 Var := Make_Var (Expression (Actual));
1062
1063 Crep := not Same_Representation
1064 (F_Typ, Etype (Expression (Actual)));
1065
1066 else
1067 V_Typ := Etype (Actual);
1068 Var := Make_Var (Actual);
1069 Crep := False;
1070 end if;
1071
1072 -- Setup initialization for case of in out parameter, or an out
1073 -- parameter where the formal is an unconstrained array (in the
1074 -- latter case, we have to pass in an object with bounds).
1075
1076 -- If this is an out parameter, the initial copy is wasteful, so as
1077 -- an optimization for the one-dimensional case we extract the
1078 -- bounds of the actual and build an uninitialized temporary of the
1079 -- right size.
1080
1081 if Ekind (Formal) = E_In_Out_Parameter
1082 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1083 then
1084 if Nkind (Actual) = N_Type_Conversion then
1085 if Conversion_OK (Actual) then
1086 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1087 else
1088 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1089 end if;
1090
1091 elsif Ekind (Formal) = E_Out_Parameter
1092 and then Is_Array_Type (F_Typ)
1093 and then Number_Dimensions (F_Typ) = 1
1094 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1095 then
1096 -- Actual is a one-dimensional array or slice, and the type
1097 -- requires no initialization. Create a temporary of the
1098 -- right size, but do not copy actual into it (optimization).
1099
1100 Init := Empty;
1101 Indic :=
1102 Make_Subtype_Indication (Loc,
1103 Subtype_Mark =>
1104 New_Occurrence_Of (F_Typ, Loc),
1105 Constraint =>
1106 Make_Index_Or_Discriminant_Constraint (Loc,
1107 Constraints => New_List (
1108 Make_Range (Loc,
1109 Low_Bound =>
1110 Make_Attribute_Reference (Loc,
1111 Prefix => New_Occurrence_Of (Var, Loc),
1112 Attribute_Name => Name_First),
1113 High_Bound =>
1114 Make_Attribute_Reference (Loc,
1115 Prefix => New_Occurrence_Of (Var, Loc),
1116 Attribute_Name => Name_Last)))));
1117
1118 else
1119 Init := New_Occurrence_Of (Var, Loc);
1120 end if;
1121
1122 -- An initialization is created for packed conversions as
1123 -- actuals for out parameters to enable Make_Object_Declaration
1124 -- to determine the proper subtype for N_Node. Note that this
1125 -- is wasteful because the extra copying on the call side is
1126 -- not required for such out parameters. ???
1127
1128 elsif Ekind (Formal) = E_Out_Parameter
1129 and then Nkind (Actual) = N_Type_Conversion
1130 and then (Is_Bit_Packed_Array (F_Typ)
1131 or else
1132 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1133 then
1134 if Conversion_OK (Actual) then
1135 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1136 else
1137 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1138 end if;
1139
1140 elsif Ekind (Formal) = E_In_Parameter then
1141
1142 -- Handle the case in which the actual is a type conversion
1143
1144 if Nkind (Actual) = N_Type_Conversion then
1145 if Conversion_OK (Actual) then
1146 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1147 else
1148 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1149 end if;
1150 else
1151 Init := New_Occurrence_Of (Var, Loc);
1152 end if;
1153
1154 else
1155 Init := Empty;
1156 end if;
1157
1158 N_Node :=
1159 Make_Object_Declaration (Loc,
1160 Defining_Identifier => Temp,
1161 Object_Definition => Indic,
1162 Expression => Init);
1163 Set_Assignment_OK (N_Node);
1164 Insert_Action (N, N_Node);
1165
1166 -- Now, normally the deal here is that we use the defining
1167 -- identifier created by that object declaration. There is
1168 -- one exception to this. In the change of representation case
1169 -- the above declaration will end up looking like:
1170
1171 -- temp : type := identifier;
1172
1173 -- And in this case we might as well use the identifier directly
1174 -- and eliminate the temporary. Note that the analysis of the
1175 -- declaration was not a waste of time in that case, since it is
1176 -- what generated the necessary change of representation code. If
1177 -- the change of representation introduced additional code, as in
1178 -- a fixed-integer conversion, the expression is not an identifier
1179 -- and must be kept.
1180
1181 if Crep
1182 and then Present (Expression (N_Node))
1183 and then Is_Entity_Name (Expression (N_Node))
1184 then
1185 Temp := Entity (Expression (N_Node));
1186 Rewrite (N_Node, Make_Null_Statement (Loc));
1187 end if;
1188
1189 -- For IN parameter, all we do is to replace the actual
1190
1191 if Ekind (Formal) = E_In_Parameter then
1192 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1193 Analyze (Actual);
1194
1195 -- Processing for OUT or IN OUT parameter
1196
1197 else
1198 -- Kill current value indications for the temporary variable we
1199 -- created, since we just passed it as an OUT parameter.
1200
1201 Kill_Current_Values (Temp);
1202 Set_Is_Known_Valid (Temp, False);
1203
1204 -- If type conversion, use reverse conversion on exit
1205
1206 if Nkind (Actual) = N_Type_Conversion then
1207 if Conversion_OK (Actual) then
1208 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1209 else
1210 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1211 end if;
1212 else
1213 Expr := New_Occurrence_Of (Temp, Loc);
1214 end if;
1215
1216 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1217 Analyze (Actual);
1218
1219 -- If the actual is a conversion of a packed reference, it may
1220 -- already have been expanded by Remove_Side_Effects, and the
1221 -- resulting variable is a temporary which does not designate
1222 -- the proper out-parameter, which may not be addressable. In
1223 -- that case, generate an assignment to the original expression
1224 -- (before expansion of the packed reference) so that the proper
1225 -- expansion of assignment to a packed component can take place.
1226
1227 declare
1228 Obj : Node_Id;
1229 Lhs : Node_Id;
1230
1231 begin
1232 if Is_Renaming_Of_Object (Var)
1233 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1234 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1235 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1236 = N_Indexed_Component
1237 and then
1238 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1239 then
1240 Obj := Renamed_Object (Var);
1241 Lhs :=
1242 Make_Selected_Component (Loc,
1243 Prefix =>
1244 New_Copy_Tree (Original_Node (Prefix (Obj))),
1245 Selector_Name => New_Copy (Selector_Name (Obj)));
1246 Reset_Analyzed_Flags (Lhs);
1247
1248 else
1249 Lhs := New_Occurrence_Of (Var, Loc);
1250 end if;
1251
1252 Set_Assignment_OK (Lhs);
1253
1254 if Is_Access_Type (E_Formal)
1255 and then Is_Entity_Name (Lhs)
1256 and then
1257 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1258 then
1259 -- Copyback target is an Ada 2012 stand-alone object of an
1260 -- anonymous access type.
1261
1262 pragma Assert (Ada_Version >= Ada_2012);
1263
1264 if Type_Access_Level (E_Formal) >
1265 Object_Access_Level (Lhs)
1266 then
1267 Append_To (Post_Call,
1268 Make_Raise_Program_Error (Loc,
1269 Reason => PE_Accessibility_Check_Failed));
1270 end if;
1271
1272 Append_To (Post_Call,
1273 Make_Assignment_Statement (Loc,
1274 Name => Lhs,
1275 Expression => Expr));
1276
1277 -- We would like to somehow suppress generation of the
1278 -- extra_accessibility assignment generated by the expansion
1279 -- of the above assignment statement. It's not a correctness
1280 -- issue because the following assignment renders it dead,
1281 -- but generating back-to-back assignments to the same
1282 -- target is undesirable. ???
1283
1284 Append_To (Post_Call,
1285 Make_Assignment_Statement (Loc,
1286 Name => New_Occurrence_Of (
1287 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1288 Expression => Make_Integer_Literal (Loc,
1289 Type_Access_Level (E_Formal))));
1290
1291 else
1292 Append_To (Post_Call,
1293 Make_Assignment_Statement (Loc,
1294 Name => Lhs,
1295 Expression => Expr));
1296 end if;
1297 end;
1298 end if;
1299 end Add_Call_By_Copy_Code;
1300
1301 ----------------------------------
1302 -- Add_Simple_Call_By_Copy_Code --
1303 ----------------------------------
1304
1305 procedure Add_Simple_Call_By_Copy_Code is
1306 Temp : Entity_Id;
1307 Decl : Node_Id;
1308 Incod : Node_Id;
1309 Outcod : Node_Id;
1310 Lhs : Node_Id;
1311 Rhs : Node_Id;
1312 Indic : Node_Id;
1313 F_Typ : constant Entity_Id := Etype (Formal);
1314
1315 begin
1316 if not Is_Legal_Copy then
1317 return;
1318 end if;
1319
1320 -- Use formal type for temp, unless formal type is an unconstrained
1321 -- array, in which case we don't have to worry about bounds checks,
1322 -- and we use the actual type, since that has appropriate bounds.
1323
1324 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1325 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1326 else
1327 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1328 end if;
1329
1330 -- Prepare to generate code
1331
1332 Reset_Packed_Prefix;
1333
1334 Temp := Make_Temporary (Loc, 'T', Actual);
1335 Incod := Relocate_Node (Actual);
1336 Outcod := New_Copy_Tree (Incod);
1337
1338 -- Generate declaration of temporary variable, initializing it
1339 -- with the input parameter unless we have an OUT formal or
1340 -- this is an initialization call.
1341
1342 -- If the formal is an out parameter with discriminants, the
1343 -- discriminants must be captured even if the rest of the object
1344 -- is in principle uninitialized, because the discriminants may
1345 -- be read by the called subprogram.
1346
1347 if Ekind (Formal) = E_Out_Parameter then
1348 Incod := Empty;
1349
1350 if Has_Discriminants (Etype (Formal)) then
1351 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1352 end if;
1353
1354 elsif Inside_Init_Proc then
1355
1356 -- Could use a comment here to match comment below ???
1357
1358 if Nkind (Actual) /= N_Selected_Component
1359 or else
1360 not Has_Discriminant_Dependent_Constraint
1361 (Entity (Selector_Name (Actual)))
1362 then
1363 Incod := Empty;
1364
1365 -- Otherwise, keep the component in order to generate the proper
1366 -- actual subtype, that depends on enclosing discriminants.
1367
1368 else
1369 null;
1370 end if;
1371 end if;
1372
1373 Decl :=
1374 Make_Object_Declaration (Loc,
1375 Defining_Identifier => Temp,
1376 Object_Definition => Indic,
1377 Expression => Incod);
1378
1379 if Inside_Init_Proc
1380 and then No (Incod)
1381 then
1382 -- If the call is to initialize a component of a composite type,
1383 -- and the component does not depend on discriminants, use the
1384 -- actual type of the component. This is required in case the
1385 -- component is constrained, because in general the formal of the
1386 -- initialization procedure will be unconstrained. Note that if
1387 -- the component being initialized is constrained by an enclosing
1388 -- discriminant, the presence of the initialization in the
1389 -- declaration will generate an expression for the actual subtype.
1390
1391 Set_No_Initialization (Decl);
1392 Set_Object_Definition (Decl,
1393 New_Occurrence_Of (Etype (Actual), Loc));
1394 end if;
1395
1396 Insert_Action (N, Decl);
1397
1398 -- The actual is simply a reference to the temporary
1399
1400 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1401
1402 -- Generate copy out if OUT or IN OUT parameter
1403
1404 if Ekind (Formal) /= E_In_Parameter then
1405 Lhs := Outcod;
1406 Rhs := New_Occurrence_Of (Temp, Loc);
1407
1408 -- Deal with conversion
1409
1410 if Nkind (Lhs) = N_Type_Conversion then
1411 Lhs := Expression (Lhs);
1412 Rhs := Convert_To (Etype (Actual), Rhs);
1413 end if;
1414
1415 Append_To (Post_Call,
1416 Make_Assignment_Statement (Loc,
1417 Name => Lhs,
1418 Expression => Rhs));
1419 Set_Assignment_OK (Name (Last (Post_Call)));
1420 end if;
1421 end Add_Simple_Call_By_Copy_Code;
1422
1423 ---------------------------
1424 -- Check_Fortran_Logical --
1425 ---------------------------
1426
1427 procedure Check_Fortran_Logical is
1428 Logical : constant Entity_Id := Etype (Formal);
1429 Var : Entity_Id;
1430
1431 -- Note: this is very incomplete, e.g. it does not handle arrays
1432 -- of logical values. This is really not the right approach at all???)
1433
1434 begin
1435 if Convention (Subp) = Convention_Fortran
1436 and then Root_Type (Etype (Formal)) = Standard_Boolean
1437 and then Ekind (Formal) /= E_In_Parameter
1438 then
1439 Var := Make_Var (Actual);
1440 Append_To (Post_Call,
1441 Make_Assignment_Statement (Loc,
1442 Name => New_Occurrence_Of (Var, Loc),
1443 Expression =>
1444 Unchecked_Convert_To (
1445 Logical,
1446 Make_Op_Ne (Loc,
1447 Left_Opnd => New_Occurrence_Of (Var, Loc),
1448 Right_Opnd =>
1449 Unchecked_Convert_To (
1450 Logical,
1451 New_Occurrence_Of (Standard_False, Loc))))));
1452 end if;
1453 end Check_Fortran_Logical;
1454
1455 -------------------
1456 -- Is_Legal_Copy --
1457 -------------------
1458
1459 function Is_Legal_Copy return Boolean is
1460 begin
1461 -- An attempt to copy a value of such a type can only occur if
1462 -- representation clauses give the actual a misaligned address.
1463
1464 if Is_By_Reference_Type (Etype (Formal)) then
1465
1466 -- If the front-end does not perform full type layout, the actual
1467 -- may in fact be properly aligned but there is not enough front-
1468 -- end information to determine this. In that case gigi will emit
1469 -- an error if a copy is not legal, or generate the proper code.
1470 -- For other backends we report the error now.
1471
1472 -- Seems wrong to be issuing an error in the expander, since it
1473 -- will be missed in -gnatc mode ???
1474
1475 if Frontend_Layout_On_Target then
1476 Error_Msg_N
1477 ("misaligned actual cannot be passed by reference", Actual);
1478 end if;
1479
1480 return False;
1481
1482 -- For users of Starlet, we assume that the specification of by-
1483 -- reference mechanism is mandatory. This may lead to unaligned
1484 -- objects but at least for DEC legacy code it is known to work.
1485 -- The warning will alert users of this code that a problem may
1486 -- be lurking.
1487
1488 elsif Mechanism (Formal) = By_Reference
1489 and then Is_Valued_Procedure (Scope (Formal))
1490 then
1491 Error_Msg_N
1492 ("by_reference actual may be misaligned??", Actual);
1493 return False;
1494
1495 else
1496 return True;
1497 end if;
1498 end Is_Legal_Copy;
1499
1500 --------------
1501 -- Make_Var --
1502 --------------
1503
1504 function Make_Var (Actual : Node_Id) return Entity_Id is
1505 Var : Entity_Id;
1506
1507 begin
1508 if Is_Entity_Name (Actual) then
1509 return Entity (Actual);
1510
1511 else
1512 Var := Make_Temporary (Loc, 'T', Actual);
1513
1514 N_Node :=
1515 Make_Object_Renaming_Declaration (Loc,
1516 Defining_Identifier => Var,
1517 Subtype_Mark =>
1518 New_Occurrence_Of (Etype (Actual), Loc),
1519 Name => Relocate_Node (Actual));
1520
1521 Insert_Action (N, N_Node);
1522 return Var;
1523 end if;
1524 end Make_Var;
1525
1526 -------------------------
1527 -- Reset_Packed_Prefix --
1528 -------------------------
1529
1530 procedure Reset_Packed_Prefix is
1531 Pfx : Node_Id := Actual;
1532 begin
1533 loop
1534 Set_Analyzed (Pfx, False);
1535 exit when
1536 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1537 Pfx := Prefix (Pfx);
1538 end loop;
1539 end Reset_Packed_Prefix;
1540
1541 -- Start of processing for Expand_Actuals
1542
1543 begin
1544 Post_Call := New_List;
1545
1546 Formal := First_Formal (Subp);
1547 Actual := First_Actual (N);
1548 while Present (Formal) loop
1549 E_Formal := Etype (Formal);
1550 E_Actual := Etype (Actual);
1551
1552 if Is_Scalar_Type (E_Formal)
1553 or else Nkind (Actual) = N_Slice
1554 then
1555 Check_Fortran_Logical;
1556
1557 -- RM 6.4.1 (11)
1558
1559 elsif Ekind (Formal) /= E_Out_Parameter then
1560
1561 -- The unusual case of the current instance of a protected type
1562 -- requires special handling. This can only occur in the context
1563 -- of a call within the body of a protected operation.
1564
1565 if Is_Entity_Name (Actual)
1566 and then Ekind (Entity (Actual)) = E_Protected_Type
1567 and then In_Open_Scopes (Entity (Actual))
1568 then
1569 if Scope (Subp) /= Entity (Actual) then
1570 Error_Msg_N
1571 ("operation outside protected type may not "
1572 & "call back its protected operations??", Actual);
1573 end if;
1574
1575 Rewrite (Actual,
1576 Expand_Protected_Object_Reference (N, Entity (Actual)));
1577 end if;
1578
1579 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1580 -- build-in-place function, then a temporary return object needs
1581 -- to be created and access to it must be passed to the function.
1582 -- Currently we limit such functions to those with inherently
1583 -- limited result subtypes, but eventually we plan to expand the
1584 -- functions that are treated as build-in-place to include other
1585 -- composite result types.
1586
1587 if Is_Build_In_Place_Function_Call (Actual) then
1588 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1589 end if;
1590
1591 Apply_Constraint_Check (Actual, E_Formal);
1592
1593 -- Out parameter case. No constraint checks on access type
1594 -- RM 6.4.1 (13)
1595
1596 elsif Is_Access_Type (E_Formal) then
1597 null;
1598
1599 -- RM 6.4.1 (14)
1600
1601 elsif Has_Discriminants (Base_Type (E_Formal))
1602 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1603 then
1604 Apply_Constraint_Check (Actual, E_Formal);
1605
1606 -- RM 6.4.1 (15)
1607
1608 else
1609 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1610 end if;
1611
1612 -- Processing for IN-OUT and OUT parameters
1613
1614 if Ekind (Formal) /= E_In_Parameter then
1615
1616 -- For type conversions of arrays, apply length/range checks
1617
1618 if Is_Array_Type (E_Formal)
1619 and then Nkind (Actual) = N_Type_Conversion
1620 then
1621 if Is_Constrained (E_Formal) then
1622 Apply_Length_Check (Expression (Actual), E_Formal);
1623 else
1624 Apply_Range_Check (Expression (Actual), E_Formal);
1625 end if;
1626 end if;
1627
1628 -- If argument is a type conversion for a type that is passed
1629 -- by copy, then we must pass the parameter by copy.
1630
1631 if Nkind (Actual) = N_Type_Conversion
1632 and then
1633 (Is_Numeric_Type (E_Formal)
1634 or else Is_Access_Type (E_Formal)
1635 or else Is_Enumeration_Type (E_Formal)
1636 or else Is_Bit_Packed_Array (Etype (Formal))
1637 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1638
1639 -- Also pass by copy if change of representation
1640
1641 or else not Same_Representation
1642 (Etype (Formal),
1643 Etype (Expression (Actual))))
1644 then
1645 Add_Call_By_Copy_Code;
1646
1647 -- References to components of bit packed arrays are expanded
1648 -- at this point, rather than at the point of analysis of the
1649 -- actuals, to handle the expansion of the assignment to
1650 -- [in] out parameters.
1651
1652 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1653 Add_Simple_Call_By_Copy_Code;
1654
1655 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1656 -- because the back-end cannot cope with such objects. In other
1657 -- cases where alignment forces a copy, the back-end generates
1658 -- it properly. It should not be generated unconditionally in the
1659 -- front-end because it does not know precisely the alignment
1660 -- requirements of the target, and makes too conservative an
1661 -- estimate, leading to superfluous copies or spurious errors
1662 -- on by-reference parameters.
1663
1664 elsif Nkind (Actual) = N_Selected_Component
1665 and then
1666 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1667 and then not Represented_As_Scalar (Etype (Formal))
1668 then
1669 Add_Simple_Call_By_Copy_Code;
1670
1671 -- References to slices of bit packed arrays are expanded
1672
1673 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1674 Add_Call_By_Copy_Code;
1675
1676 -- References to possibly unaligned slices of arrays are expanded
1677
1678 elsif Is_Possibly_Unaligned_Slice (Actual) then
1679 Add_Call_By_Copy_Code;
1680
1681 -- Deal with access types where the actual subtype and the
1682 -- formal subtype are not the same, requiring a check.
1683
1684 -- It is necessary to exclude tagged types because of "downward
1685 -- conversion" errors.
1686
1687 elsif Is_Access_Type (E_Formal)
1688 and then not Same_Type (E_Formal, E_Actual)
1689 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1690 then
1691 Add_Call_By_Copy_Code;
1692
1693 -- If the actual is not a scalar and is marked for volatile
1694 -- treatment, whereas the formal is not volatile, then pass
1695 -- by copy unless it is a by-reference type.
1696
1697 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1698 -- because this is the enforcement of a language rule that applies
1699 -- only to "real" volatile variables, not e.g. to the address
1700 -- clause overlay case.
1701
1702 elsif Is_Entity_Name (Actual)
1703 and then Is_Volatile (Entity (Actual))
1704 and then not Is_By_Reference_Type (E_Actual)
1705 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1706 and then not Is_Volatile (E_Formal)
1707 then
1708 Add_Call_By_Copy_Code;
1709
1710 elsif Nkind (Actual) = N_Indexed_Component
1711 and then Is_Entity_Name (Prefix (Actual))
1712 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1713 then
1714 Add_Call_By_Copy_Code;
1715
1716 -- Add call-by-copy code for the case of scalar out parameters
1717 -- when it is not known at compile time that the subtype of the
1718 -- formal is a subrange of the subtype of the actual (or vice
1719 -- versa for in out parameters), in order to get range checks
1720 -- on such actuals. (Maybe this case should be handled earlier
1721 -- in the if statement???)
1722
1723 elsif Is_Scalar_Type (E_Formal)
1724 and then
1725 (not In_Subrange_Of (E_Formal, E_Actual)
1726 or else
1727 (Ekind (Formal) = E_In_Out_Parameter
1728 and then not In_Subrange_Of (E_Actual, E_Formal)))
1729 then
1730 -- Perhaps the setting back to False should be done within
1731 -- Add_Call_By_Copy_Code, since it could get set on other
1732 -- cases occurring above???
1733
1734 if Do_Range_Check (Actual) then
1735 Set_Do_Range_Check (Actual, False);
1736 end if;
1737
1738 Add_Call_By_Copy_Code;
1739 end if;
1740
1741 -- RM 3.2.4 (23/3) : A predicate is checked on in-out and out
1742 -- by-reference parameters on exit from the call. If the actual
1743 -- is a derived type and the operation is inherited, the body
1744 -- of the operation will not contain a call to the predicate
1745 -- function, so it must be done explicitly after the call. Ditto
1746 -- if the actual is an entity of a predicated subtype.
1747
1748 -- The rule refers to by-reference types, but a check is needed
1749 -- for by-copy types as well. That check is subsumed by the rule
1750 -- for subtype conversion on assignment, but we can generate the
1751 -- required check now.
1752
1753 -- Note that this is needed only if the subtype of the actual has
1754 -- an explicit predicate aspect, not if it inherits them from a
1755 -- base type or ancestor. The check is also superfluous if the
1756 -- subtype is elaborated before the body of the subprogram, but
1757 -- this is harder to verify, and there may be a redundant check.
1758
1759 -- Note also that Subp may be either a subprogram entity for
1760 -- direct calls, or a type entity for indirect calls, which must
1761 -- be handled separately because the name does not denote an
1762 -- overloadable entity.
1763
1764 -- If the formal is class-wide the corresponding postcondition
1765 -- procedure does not include a predicate call, so it has to be
1766 -- generated explicitly.
1767
1768 if not Is_Init_Proc (Subp)
1769 and then (Has_Aspect (E_Actual, Aspect_Predicate)
1770 or else
1771 Has_Aspect (E_Actual, Aspect_Dynamic_Predicate)
1772 or else
1773 Has_Aspect (E_Actual, Aspect_Static_Predicate))
1774 and then Present (Predicate_Function (E_Actual))
1775 then
1776 if Is_Entity_Name (Actual)
1777 or else
1778 (Is_Derived_Type (E_Actual)
1779 and then Is_Overloadable (Subp)
1780 and then Is_Inherited_Operation_For_Type (Subp, E_Actual))
1781 then
1782 Append_To (Post_Call,
1783 Make_Predicate_Check (E_Actual, Actual));
1784
1785 elsif Is_Class_Wide_Type (E_Formal)
1786 and then not Is_Class_Wide_Type (E_Actual)
1787 then
1788 Append_To (Post_Call,
1789 Make_Predicate_Check (E_Actual, Actual));
1790 end if;
1791 end if;
1792
1793 -- Processing for IN parameters
1794
1795 else
1796 -- For IN parameters is in the packed array case, we expand an
1797 -- indexed component (the circuit in Exp_Ch4 deliberately left
1798 -- indexed components appearing as actuals untouched, so that
1799 -- the special processing above for the OUT and IN OUT cases
1800 -- could be performed. We could make the test in Exp_Ch4 more
1801 -- complex and have it detect the parameter mode, but it is
1802 -- easier simply to handle all cases here.)
1803
1804 if Nkind (Actual) = N_Indexed_Component
1805 and then Is_Packed (Etype (Prefix (Actual)))
1806 then
1807 Reset_Packed_Prefix;
1808 Expand_Packed_Element_Reference (Actual);
1809
1810 -- If we have a reference to a bit packed array, we copy it, since
1811 -- the actual must be byte aligned.
1812
1813 -- Is this really necessary in all cases???
1814
1815 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1816 Add_Simple_Call_By_Copy_Code;
1817
1818 -- If a non-scalar actual is possibly unaligned, we need a copy
1819
1820 elsif Is_Possibly_Unaligned_Object (Actual)
1821 and then not Represented_As_Scalar (Etype (Formal))
1822 then
1823 Add_Simple_Call_By_Copy_Code;
1824
1825 -- Similarly, we have to expand slices of packed arrays here
1826 -- because the result must be byte aligned.
1827
1828 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1829 Add_Call_By_Copy_Code;
1830
1831 -- Only processing remaining is to pass by copy if this is a
1832 -- reference to a possibly unaligned slice, since the caller
1833 -- expects an appropriately aligned argument.
1834
1835 elsif Is_Possibly_Unaligned_Slice (Actual) then
1836 Add_Call_By_Copy_Code;
1837
1838 -- An unusual case: a current instance of an enclosing task can be
1839 -- an actual, and must be replaced by a reference to self.
1840
1841 elsif Is_Entity_Name (Actual)
1842 and then Is_Task_Type (Entity (Actual))
1843 then
1844 if In_Open_Scopes (Entity (Actual)) then
1845 Rewrite (Actual,
1846 (Make_Function_Call (Loc,
1847 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1848 Analyze (Actual);
1849
1850 -- A task type cannot otherwise appear as an actual
1851
1852 else
1853 raise Program_Error;
1854 end if;
1855 end if;
1856 end if;
1857
1858 Next_Formal (Formal);
1859 Next_Actual (Actual);
1860 end loop;
1861
1862 -- Find right place to put post call stuff if it is present
1863
1864 if not Is_Empty_List (Post_Call) then
1865
1866 -- Cases where the call is not a member of a statement list
1867
1868 if not Is_List_Member (N) then
1869
1870 -- In Ada 2012 the call may be a function call in an expression
1871 -- (since OUT and IN OUT parameters are now allowed for such
1872 -- calls). The write-back of (in)-out parameters is handled
1873 -- by the back-end, but the constraint checks generated when
1874 -- subtypes of formal and actual don't match must be inserted
1875 -- in the form of assignments.
1876
1877 if Ada_Version >= Ada_2012
1878 and then Nkind (N) = N_Function_Call
1879 then
1880 -- We used to just do handle this by climbing up parents to
1881 -- a non-statement/declaration and then simply making a call
1882 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1883 -- work. If we are in the middle of an expression, e.g. the
1884 -- condition of an IF, this call would insert after the IF
1885 -- statement, which is much too late to be doing the write
1886 -- back. For example:
1887
1888 -- if Clobber (X) then
1889 -- Put_Line (X'Img);
1890 -- else
1891 -- goto Junk
1892 -- end if;
1893
1894 -- Now assume Clobber changes X, if we put the write back
1895 -- after the IF, the Put_Line gets the wrong value and the
1896 -- goto causes the write back to be skipped completely.
1897
1898 -- To deal with this, we replace the call by
1899
1900 -- do
1901 -- Tnnn : function-result-type renames function-call;
1902 -- Post_Call actions
1903 -- in
1904 -- Tnnn;
1905 -- end;
1906
1907 -- Note: this won't do in Modify_Tree_For_C mode, but we
1908 -- will deal with that later (it will require creating a
1909 -- declaration for Temp, using Insert_Declaration) ???
1910
1911 declare
1912 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
1913 FRTyp : constant Entity_Id := Etype (N);
1914 Name : constant Node_Id := Relocate_Node (N);
1915
1916 begin
1917 Prepend_To (Post_Call,
1918 Make_Object_Renaming_Declaration (Loc,
1919 Defining_Identifier => Tnnn,
1920 Subtype_Mark => New_Occurrence_Of (FRTyp, Loc),
1921 Name => Name));
1922
1923 Rewrite (N,
1924 Make_Expression_With_Actions (Loc,
1925 Actions => Post_Call,
1926 Expression => New_Occurrence_Of (Tnnn, Loc)));
1927
1928 -- We don't want to just blindly call Analyze_And_Resolve
1929 -- because that would cause unwanted recursion on the call.
1930 -- So for a moment set the call as analyzed to prevent that
1931 -- recursion, and get the rest analyzed properly, then reset
1932 -- the analyzed flag, so our caller can continue.
1933
1934 Set_Analyzed (Name, True);
1935 Analyze_And_Resolve (N, FRTyp);
1936 Set_Analyzed (Name, False);
1937
1938 -- Reset calling argument to point to function call inside
1939 -- the expression with actions so the caller can continue
1940 -- to process the call.
1941
1942 N := Name;
1943 end;
1944
1945 -- If not the special Ada 2012 case of a function call, then
1946 -- we must have the triggering statement of a triggering
1947 -- alternative or an entry call alternative, and we can add
1948 -- the post call stuff to the corresponding statement list.
1949
1950 else
1951 declare
1952 P : Node_Id;
1953
1954 begin
1955 P := Parent (N);
1956 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1957 N_Entry_Call_Alternative));
1958
1959 if Is_Non_Empty_List (Statements (P)) then
1960 Insert_List_Before_And_Analyze
1961 (First (Statements (P)), Post_Call);
1962 else
1963 Set_Statements (P, Post_Call);
1964 end if;
1965
1966 return;
1967 end;
1968 end if;
1969
1970 -- Otherwise, normal case where N is in a statement sequence,
1971 -- just put the post-call stuff after the call statement.
1972
1973 else
1974 Insert_Actions_After (N, Post_Call);
1975 return;
1976 end if;
1977 end if;
1978
1979 -- The call node itself is re-analyzed in Expand_Call
1980
1981 end Expand_Actuals;
1982
1983 -----------------
1984 -- Expand_Call --
1985 -----------------
1986
1987 -- This procedure handles expansion of function calls and procedure call
1988 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1989 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1990
1991 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1992 -- Provide values of actuals for all formals in Extra_Formals list
1993 -- Replace "call" to enumeration literal function by literal itself
1994 -- Rewrite call to predefined operator as operator
1995 -- Replace actuals to in-out parameters that are numeric conversions,
1996 -- with explicit assignment to temporaries before and after the call.
1997 -- Remove optional actuals if First_Optional_Parameter specified.
1998
1999 -- Note that the list of actuals has been filled with default expressions
2000 -- during semantic analysis of the call. Only the extra actuals required
2001 -- for the 'Constrained attribute and for accessibility checks are added
2002 -- at this point.
2003
2004 procedure Expand_Call (N : Node_Id) is
2005 Loc : constant Source_Ptr := Sloc (N);
2006 Call_Node : Node_Id := N;
2007 Extra_Actuals : List_Id := No_List;
2008 Prev : Node_Id := Empty;
2009
2010 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2011 -- Adds one entry to the end of the actual parameter list. Used for
2012 -- default parameters and for extra actuals (for Extra_Formals). The
2013 -- argument is an N_Parameter_Association node.
2014
2015 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2016 -- Adds an extra actual to the list of extra actuals. Expr is the
2017 -- expression for the value of the actual, EF is the entity for the
2018 -- extra formal.
2019
2020 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id);
2021 -- Check and inline the body of Subp. Invoked when compiling with
2022 -- optimizations enabled and Subp has pragma inline or inline always.
2023 -- If the subprogram is a renaming, or if it is inherited, then Subp
2024 -- references the renamed entity and Orig_Subp is the entity of the
2025 -- call node N.
2026
2027 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id);
2028 -- Check and inline the body of Subp. Invoked when compiling without
2029 -- optimizations and Subp has pragma inline always. If the subprogram is
2030 -- a renaming, or if it is inherited, then Subp references the renamed
2031 -- entity and Orig_Subp is the entity of the call node N.
2032
2033 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2034 -- Within an instance, a type derived from a non-tagged formal derived
2035 -- type inherits from the original parent, not from the actual. The
2036 -- current derivation mechanism has the derived type inherit from the
2037 -- actual, which is only correct outside of the instance. If the
2038 -- subprogram is inherited, we test for this particular case through a
2039 -- convoluted tree traversal before setting the proper subprogram to be
2040 -- called.
2041
2042 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2043 -- Return true if E comes from an instance that is not yet frozen
2044
2045 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2046 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2047
2048 function New_Value (From : Node_Id) return Node_Id;
2049 -- From is the original Expression. New_Value is equivalent to a call
2050 -- to Duplicate_Subexpr with an explicit dereference when From is an
2051 -- access parameter.
2052
2053 --------------------------
2054 -- Add_Actual_Parameter --
2055 --------------------------
2056
2057 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2058 Actual_Expr : constant Node_Id :=
2059 Explicit_Actual_Parameter (Insert_Param);
2060
2061 begin
2062 -- Case of insertion is first named actual
2063
2064 if No (Prev) or else
2065 Nkind (Parent (Prev)) /= N_Parameter_Association
2066 then
2067 Set_Next_Named_Actual
2068 (Insert_Param, First_Named_Actual (Call_Node));
2069 Set_First_Named_Actual (Call_Node, Actual_Expr);
2070
2071 if No (Prev) then
2072 if No (Parameter_Associations (Call_Node)) then
2073 Set_Parameter_Associations (Call_Node, New_List);
2074 end if;
2075
2076 Append (Insert_Param, Parameter_Associations (Call_Node));
2077
2078 else
2079 Insert_After (Prev, Insert_Param);
2080 end if;
2081
2082 -- Case of insertion is not first named actual
2083
2084 else
2085 Set_Next_Named_Actual
2086 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2087 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2088 Append (Insert_Param, Parameter_Associations (Call_Node));
2089 end if;
2090
2091 Prev := Actual_Expr;
2092 end Add_Actual_Parameter;
2093
2094 ----------------------
2095 -- Add_Extra_Actual --
2096 ----------------------
2097
2098 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2099 Loc : constant Source_Ptr := Sloc (Expr);
2100
2101 begin
2102 if Extra_Actuals = No_List then
2103 Extra_Actuals := New_List;
2104 Set_Parent (Extra_Actuals, Call_Node);
2105 end if;
2106
2107 Append_To (Extra_Actuals,
2108 Make_Parameter_Association (Loc,
2109 Selector_Name => Make_Identifier (Loc, Chars (EF)),
2110 Explicit_Actual_Parameter => Expr));
2111
2112 Analyze_And_Resolve (Expr, Etype (EF));
2113
2114 if Nkind (Call_Node) = N_Function_Call then
2115 Set_Is_Accessibility_Actual (Parent (Expr));
2116 end if;
2117 end Add_Extra_Actual;
2118
2119 ----------------
2120 -- Do_Inline --
2121 ----------------
2122
2123 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2124 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2125
2126 procedure Do_Backend_Inline;
2127 -- Check that the call can be safely passed to the backend. If true
2128 -- then register the enclosing unit of Subp to Inlined_Bodies so that
2129 -- the body of Subp can be retrieved and analyzed by the backend.
2130
2131 procedure Register_Backend_Call (N : Node_Id);
2132 -- Append N to the list Backend_Calls
2133
2134 -----------------------
2135 -- Do_Backend_Inline --
2136 -----------------------
2137
2138 procedure Do_Backend_Inline is
2139 begin
2140 -- No extra test needed for init subprograms since we know they
2141 -- are available to the backend.
2142
2143 if Is_Init_Proc (Subp) then
2144 Add_Inlined_Body (Subp);
2145 Register_Backend_Call (Call_Node);
2146
2147 -- Verify that if the body to inline is located in the current
2148 -- unit the inlining does not occur earlier. This avoids
2149 -- order-of-elaboration problems in the back end.
2150
2151 elsif In_Same_Extended_Unit (Call_Node, Subp)
2152 and then Nkind (Spec) = N_Subprogram_Declaration
2153 and then Earlier_In_Extended_Unit
2154 (Loc, Sloc (Body_To_Inline (Spec)))
2155 then
2156 Error_Msg_NE
2157 ("cannot inline& (body not seen yet)??", Call_Node, Subp);
2158
2159 else
2160 declare
2161 Backend_Inline : Boolean := True;
2162
2163 begin
2164 -- If we are compiling a package body that is not the
2165 -- main unit, it must be for inlining/instantiation
2166 -- purposes, in which case we inline the call to insure
2167 -- that the same temporaries are generated when compiling
2168 -- the body by itself. Otherwise link errors can occur.
2169
2170 -- If the function being called is itself in the main
2171 -- unit, we cannot inline, because there is a risk of
2172 -- double elaboration and/or circularity: the inlining
2173 -- can make visible a private entity in the body of the
2174 -- main unit, that gigi will see before its sees its
2175 -- proper definition.
2176
2177 if not (In_Extended_Main_Code_Unit (Call_Node))
2178 and then In_Package_Body
2179 then
2180 Backend_Inline :=
2181 not In_Extended_Main_Source_Unit (Subp);
2182 end if;
2183
2184 if Backend_Inline then
2185 Add_Inlined_Body (Subp);
2186 Register_Backend_Call (Call_Node);
2187 end if;
2188 end;
2189 end if;
2190 end Do_Backend_Inline;
2191
2192 ---------------------------
2193 -- Register_Backend_Call --
2194 ---------------------------
2195
2196 procedure Register_Backend_Call (N : Node_Id) is
2197 begin
2198 if Backend_Calls = No_Elist then
2199 Backend_Calls := New_Elmt_List;
2200 end if;
2201
2202 Append_Elmt (N, To => Backend_Calls);
2203 end Register_Backend_Call;
2204
2205 -- Start of processing for Do_Inline
2206
2207 begin
2208 -- Verify that the body to inline has already been seen
2209
2210 if No (Spec)
2211 or else Nkind (Spec) /= N_Subprogram_Declaration
2212 or else No (Body_To_Inline (Spec))
2213 then
2214 if Comes_From_Source (Subp)
2215 and then Must_Inline (Subp)
2216 then
2217 Cannot_Inline
2218 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2219
2220 -- Let the back end handle it
2221
2222 else
2223 Do_Backend_Inline;
2224 return;
2225 end if;
2226
2227 -- If this an inherited function that returns a private type, do not
2228 -- inline if the full view is an unconstrained array, because such
2229 -- calls cannot be inlined.
2230
2231 elsif Present (Orig_Subp)
2232 and then Is_Array_Type (Etype (Orig_Subp))
2233 and then not Is_Constrained (Etype (Orig_Subp))
2234 then
2235 Cannot_Inline
2236 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2237
2238 else
2239 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2240 end if;
2241 end Do_Inline;
2242
2243 ----------------------
2244 -- Do_Inline_Always --
2245 ----------------------
2246
2247 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2248 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2249 Body_Id : Entity_Id;
2250
2251 begin
2252 if No (Spec)
2253 or else Nkind (Spec) /= N_Subprogram_Declaration
2254 or else No (Body_To_Inline (Spec))
2255 or else Serious_Errors_Detected /= 0
2256 then
2257 return;
2258 end if;
2259
2260 Body_Id := Corresponding_Body (Spec);
2261
2262 -- Verify that the body to inline has already been seen
2263
2264 if No (Body_Id)
2265 or else not Analyzed (Body_Id)
2266 then
2267 Set_Is_Inlined (Subp, False);
2268
2269 if Comes_From_Source (Subp) then
2270
2271 -- Report a warning only if the call is located in the unit of
2272 -- the called subprogram; otherwise it is an error.
2273
2274 if not In_Same_Extended_Unit (Call_Node, Subp) then
2275 Cannot_Inline
2276 ("cannot inline& (body not seen yet)?", Call_Node, Subp,
2277 Is_Serious => True);
2278
2279 elsif In_Open_Scopes (Subp) then
2280
2281 -- For backward compatibility we generate the same error
2282 -- or warning of the previous implementation. This will
2283 -- be changed when we definitely incorporate the new
2284 -- support ???
2285
2286 if Front_End_Inlining
2287 and then Optimization_Level = 0
2288 then
2289 Error_Msg_N
2290 ("call to recursive subprogram cannot be inlined?p?",
2291 N);
2292
2293 -- Do not emit error compiling runtime packages
2294
2295 elsif Is_Predefined_File_Name
2296 (Unit_File_Name (Get_Source_Unit (Subp)))
2297 then
2298 Error_Msg_N
2299 ("call to recursive subprogram cannot be inlined??",
2300 N);
2301
2302 else
2303 Error_Msg_N
2304 ("call to recursive subprogram cannot be inlined",
2305 N);
2306 end if;
2307
2308 else
2309 Cannot_Inline
2310 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2311 end if;
2312 end if;
2313
2314 return;
2315
2316 -- If this an inherited function that returns a private type, do not
2317 -- inline if the full view is an unconstrained array, because such
2318 -- calls cannot be inlined.
2319
2320 elsif Present (Orig_Subp)
2321 and then Is_Array_Type (Etype (Orig_Subp))
2322 and then not Is_Constrained (Etype (Orig_Subp))
2323 then
2324 Cannot_Inline
2325 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2326
2327 -- If the called subprogram comes from an instance in the same
2328 -- unit, and the instance is not yet frozen, inlining might
2329 -- trigger order-of-elaboration problems.
2330
2331 elsif In_Unfrozen_Instance (Scope (Subp)) then
2332 Cannot_Inline
2333 ("cannot inline& (unfrozen instance)?", Call_Node, Subp);
2334
2335 else
2336 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2337 end if;
2338 end Do_Inline_Always;
2339
2340 ---------------------------
2341 -- Inherited_From_Formal --
2342 ---------------------------
2343
2344 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2345 Par : Entity_Id;
2346 Gen_Par : Entity_Id;
2347 Gen_Prim : Elist_Id;
2348 Elmt : Elmt_Id;
2349 Indic : Node_Id;
2350
2351 begin
2352 -- If the operation is inherited, it is attached to the corresponding
2353 -- type derivation. If the parent in the derivation is a generic
2354 -- actual, it is a subtype of the actual, and we have to recover the
2355 -- original derived type declaration to find the proper parent.
2356
2357 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2358 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2359 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2360 N_Derived_Type_Definition
2361 or else not In_Instance
2362 then
2363 return Empty;
2364
2365 else
2366 Indic :=
2367 Subtype_Indication
2368 (Type_Definition (Original_Node (Parent (S))));
2369
2370 if Nkind (Indic) = N_Subtype_Indication then
2371 Par := Entity (Subtype_Mark (Indic));
2372 else
2373 Par := Entity (Indic);
2374 end if;
2375 end if;
2376
2377 if not Is_Generic_Actual_Type (Par)
2378 or else Is_Tagged_Type (Par)
2379 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2380 or else not In_Open_Scopes (Scope (Par))
2381 then
2382 return Empty;
2383 else
2384 Gen_Par := Generic_Parent_Type (Parent (Par));
2385 end if;
2386
2387 -- If the actual has no generic parent type, the formal is not
2388 -- a formal derived type, so nothing to inherit.
2389
2390 if No (Gen_Par) then
2391 return Empty;
2392 end if;
2393
2394 -- If the generic parent type is still the generic type, this is a
2395 -- private formal, not a derived formal, and there are no operations
2396 -- inherited from the formal.
2397
2398 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2399 return Empty;
2400 end if;
2401
2402 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2403
2404 Elmt := First_Elmt (Gen_Prim);
2405 while Present (Elmt) loop
2406 if Chars (Node (Elmt)) = Chars (S) then
2407 declare
2408 F1 : Entity_Id;
2409 F2 : Entity_Id;
2410
2411 begin
2412 F1 := First_Formal (S);
2413 F2 := First_Formal (Node (Elmt));
2414 while Present (F1)
2415 and then Present (F2)
2416 loop
2417 if Etype (F1) = Etype (F2)
2418 or else Etype (F2) = Gen_Par
2419 then
2420 Next_Formal (F1);
2421 Next_Formal (F2);
2422 else
2423 Next_Elmt (Elmt);
2424 exit; -- not the right subprogram
2425 end if;
2426
2427 return Node (Elmt);
2428 end loop;
2429 end;
2430
2431 else
2432 Next_Elmt (Elmt);
2433 end if;
2434 end loop;
2435
2436 raise Program_Error;
2437 end Inherited_From_Formal;
2438
2439 --------------------------
2440 -- In_Unfrozen_Instance --
2441 --------------------------
2442
2443 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2444 S : Entity_Id;
2445
2446 begin
2447 S := E;
2448 while Present (S) and then S /= Standard_Standard loop
2449 if Is_Generic_Instance (S)
2450 and then Present (Freeze_Node (S))
2451 and then not Analyzed (Freeze_Node (S))
2452 then
2453 return True;
2454 end if;
2455
2456 S := Scope (S);
2457 end loop;
2458
2459 return False;
2460 end In_Unfrozen_Instance;
2461
2462 -------------------------
2463 -- Is_Direct_Deep_Call --
2464 -------------------------
2465
2466 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2467 begin
2468 if Is_TSS (Subp, TSS_Deep_Adjust)
2469 or else Is_TSS (Subp, TSS_Deep_Finalize)
2470 or else Is_TSS (Subp, TSS_Deep_Initialize)
2471 then
2472 declare
2473 Actual : Node_Id;
2474 Formal : Node_Id;
2475
2476 begin
2477 Actual := First (Parameter_Associations (N));
2478 Formal := First_Formal (Subp);
2479 while Present (Actual)
2480 and then Present (Formal)
2481 loop
2482 if Nkind (Actual) = N_Identifier
2483 and then Is_Controlling_Actual (Actual)
2484 and then Etype (Actual) = Etype (Formal)
2485 then
2486 return True;
2487 end if;
2488
2489 Next (Actual);
2490 Next_Formal (Formal);
2491 end loop;
2492 end;
2493 end if;
2494
2495 return False;
2496 end Is_Direct_Deep_Call;
2497
2498 ---------------
2499 -- New_Value --
2500 ---------------
2501
2502 function New_Value (From : Node_Id) return Node_Id is
2503 Res : constant Node_Id := Duplicate_Subexpr (From);
2504 begin
2505 if Is_Access_Type (Etype (From)) then
2506 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2507 else
2508 return Res;
2509 end if;
2510 end New_Value;
2511
2512 -- Local variables
2513
2514 Curr_S : constant Entity_Id := Current_Scope;
2515 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2516 Actual : Node_Id;
2517 Formal : Entity_Id;
2518 Orig_Subp : Entity_Id := Empty;
2519 Param_Count : Natural := 0;
2520 Parent_Formal : Entity_Id;
2521 Parent_Subp : Entity_Id;
2522 Scop : Entity_Id;
2523 Subp : Entity_Id;
2524
2525 Prev_Orig : Node_Id;
2526 -- Original node for an actual, which may have been rewritten. If the
2527 -- actual is a function call that has been transformed from a selected
2528 -- component, the original node is unanalyzed. Otherwise, it carries
2529 -- semantic information used to generate additional actuals.
2530
2531 CW_Interface_Formals_Present : Boolean := False;
2532
2533 -- Start of processing for Expand_Call
2534
2535 begin
2536 -- Expand the procedure call if the first actual has a dimension and if
2537 -- the procedure is Put (Ada 2012).
2538
2539 if Ada_Version >= Ada_2012
2540 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2541 and then Present (Parameter_Associations (Call_Node))
2542 then
2543 Expand_Put_Call_With_Symbol (Call_Node);
2544 end if;
2545
2546 -- Ignore if previous error
2547
2548 if Nkind (Call_Node) in N_Has_Etype
2549 and then Etype (Call_Node) = Any_Type
2550 then
2551 return;
2552 end if;
2553
2554 -- Call using access to subprogram with explicit dereference
2555
2556 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2557 Subp := Etype (Name (Call_Node));
2558 Parent_Subp := Empty;
2559
2560 -- Case of call to simple entry, where the Name is a selected component
2561 -- whose prefix is the task, and whose selector name is the entry name
2562
2563 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2564 Subp := Entity (Selector_Name (Name (Call_Node)));
2565 Parent_Subp := Empty;
2566
2567 -- Case of call to member of entry family, where Name is an indexed
2568 -- component, with the prefix being a selected component giving the
2569 -- task and entry family name, and the index being the entry index.
2570
2571 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2572 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2573 Parent_Subp := Empty;
2574
2575 -- Normal case
2576
2577 else
2578 Subp := Entity (Name (Call_Node));
2579 Parent_Subp := Alias (Subp);
2580
2581 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2582 -- if we can tell that the first parameter cannot possibly be null.
2583 -- This improves efficiency by avoiding a run-time test.
2584
2585 -- We do not do this if Raise_Exception_Always does not exist, which
2586 -- can happen in configurable run time profiles which provide only a
2587 -- Raise_Exception.
2588
2589 if Is_RTE (Subp, RE_Raise_Exception)
2590 and then RTE_Available (RE_Raise_Exception_Always)
2591 then
2592 declare
2593 FA : constant Node_Id :=
2594 Original_Node (First_Actual (Call_Node));
2595
2596 begin
2597 -- The case we catch is where the first argument is obtained
2598 -- using the Identity attribute (which must always be
2599 -- non-null).
2600
2601 if Nkind (FA) = N_Attribute_Reference
2602 and then Attribute_Name (FA) = Name_Identity
2603 then
2604 Subp := RTE (RE_Raise_Exception_Always);
2605 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2606 end if;
2607 end;
2608 end if;
2609
2610 if Ekind (Subp) = E_Entry then
2611 Parent_Subp := Empty;
2612 end if;
2613 end if;
2614
2615 -- Detect the following code in System.Finalization_Masters only on
2616 -- .NET/JVM targets:
2617 --
2618 -- procedure Finalize (Master : in out Finalization_Master) is
2619 -- begin
2620 -- . . .
2621 -- begin
2622 -- Finalize (Curr_Ptr.all);
2623 --
2624 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2625 -- cannot be named in library or user code, the compiler has to install
2626 -- a kludge and transform the call to Finalize into Deep_Finalize.
2627
2628 if VM_Target /= No_VM
2629 and then Chars (Subp) = Name_Finalize
2630 and then Ekind (Curr_S) = E_Block
2631 and then Ekind (Scope (Curr_S)) = E_Procedure
2632 and then Chars (Scope (Curr_S)) = Name_Finalize
2633 and then Etype (First_Formal (Scope (Curr_S))) =
2634 RTE (RE_Finalization_Master)
2635 then
2636 declare
2637 Deep_Fin : constant Entity_Id :=
2638 Find_Prim_Op (RTE (RE_Root_Controlled),
2639 TSS_Deep_Finalize);
2640 begin
2641 -- Since Root_Controlled is a tagged type, the compiler should
2642 -- always generate Deep_Finalize for it.
2643
2644 pragma Assert (Present (Deep_Fin));
2645
2646 -- Generate:
2647 -- Deep_Finalize (Curr_Ptr.all);
2648
2649 Rewrite (N,
2650 Make_Procedure_Call_Statement (Loc,
2651 Name =>
2652 New_Occurrence_Of (Deep_Fin, Loc),
2653 Parameter_Associations =>
2654 New_Copy_List_Tree (Parameter_Associations (N))));
2655
2656 Analyze (N);
2657 return;
2658 end;
2659 end if;
2660
2661 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2662 -- alternative in an asynchronous select or as an entry call in
2663 -- a conditional or timed select. Check whether the procedure call
2664 -- is a renaming of an entry and rewrite it as an entry call.
2665
2666 if Ada_Version >= Ada_2005
2667 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2668 and then
2669 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2670 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2671 or else
2672 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2673 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2674 then
2675 declare
2676 Ren_Decl : Node_Id;
2677 Ren_Root : Entity_Id := Subp;
2678
2679 begin
2680 -- This may be a chain of renamings, find the root
2681
2682 if Present (Alias (Ren_Root)) then
2683 Ren_Root := Alias (Ren_Root);
2684 end if;
2685
2686 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2687 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2688
2689 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2690 Rewrite (Call_Node,
2691 Make_Entry_Call_Statement (Loc,
2692 Name =>
2693 New_Copy_Tree (Name (Ren_Decl)),
2694 Parameter_Associations =>
2695 New_Copy_List_Tree
2696 (Parameter_Associations (Call_Node))));
2697
2698 return;
2699 end if;
2700 end if;
2701 end;
2702 end if;
2703
2704 -- First step, compute extra actuals, corresponding to any Extra_Formals
2705 -- present. Note that we do not access Extra_Formals directly, instead
2706 -- we simply note the presence of the extra formals as we process the
2707 -- regular formals collecting corresponding actuals in Extra_Actuals.
2708
2709 -- We also generate any required range checks for actuals for in formals
2710 -- as we go through the loop, since this is a convenient place to do it.
2711 -- (Though it seems that this would be better done in Expand_Actuals???)
2712
2713 -- Special case: Thunks must not compute the extra actuals; they must
2714 -- just propagate to the target primitive their extra actuals.
2715
2716 if Is_Thunk (Current_Scope)
2717 and then Thunk_Entity (Current_Scope) = Subp
2718 and then Present (Extra_Formals (Subp))
2719 then
2720 pragma Assert (Present (Extra_Formals (Current_Scope)));
2721
2722 declare
2723 Target_Formal : Entity_Id;
2724 Thunk_Formal : Entity_Id;
2725
2726 begin
2727 Target_Formal := Extra_Formals (Subp);
2728 Thunk_Formal := Extra_Formals (Current_Scope);
2729 while Present (Target_Formal) loop
2730 Add_Extra_Actual
2731 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2732
2733 Target_Formal := Extra_Formal (Target_Formal);
2734 Thunk_Formal := Extra_Formal (Thunk_Formal);
2735 end loop;
2736
2737 while Is_Non_Empty_List (Extra_Actuals) loop
2738 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2739 end loop;
2740
2741 Expand_Actuals (Call_Node, Subp);
2742 return;
2743 end;
2744 end if;
2745
2746 Formal := First_Formal (Subp);
2747 Actual := First_Actual (Call_Node);
2748 Param_Count := 1;
2749 while Present (Formal) loop
2750
2751 -- Generate range check if required
2752
2753 if Do_Range_Check (Actual)
2754 and then Ekind (Formal) = E_In_Parameter
2755 then
2756 Set_Do_Range_Check (Actual, False);
2757 Generate_Range_Check
2758 (Actual, Etype (Formal), CE_Range_Check_Failed);
2759 end if;
2760
2761 -- Prepare to examine current entry
2762
2763 Prev := Actual;
2764 Prev_Orig := Original_Node (Prev);
2765
2766 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2767 -- to expand it in a further round.
2768
2769 CW_Interface_Formals_Present :=
2770 CW_Interface_Formals_Present
2771 or else
2772 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2773 and then Is_Interface (Etype (Etype (Formal))))
2774 or else
2775 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2776 and then Is_Interface (Directly_Designated_Type
2777 (Etype (Etype (Formal)))));
2778
2779 -- Create possible extra actual for constrained case. Usually, the
2780 -- extra actual is of the form actual'constrained, but since this
2781 -- attribute is only available for unconstrained records, TRUE is
2782 -- expanded if the type of the formal happens to be constrained (for
2783 -- instance when this procedure is inherited from an unconstrained
2784 -- record to a constrained one) or if the actual has no discriminant
2785 -- (its type is constrained). An exception to this is the case of a
2786 -- private type without discriminants. In this case we pass FALSE
2787 -- because the object has underlying discriminants with defaults.
2788
2789 if Present (Extra_Constrained (Formal)) then
2790 if Ekind (Etype (Prev)) in Private_Kind
2791 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2792 then
2793 Add_Extra_Actual
2794 (New_Occurrence_Of (Standard_False, Loc),
2795 Extra_Constrained (Formal));
2796
2797 elsif Is_Constrained (Etype (Formal))
2798 or else not Has_Discriminants (Etype (Prev))
2799 then
2800 Add_Extra_Actual
2801 (New_Occurrence_Of (Standard_True, Loc),
2802 Extra_Constrained (Formal));
2803
2804 -- Do not produce extra actuals for Unchecked_Union parameters.
2805 -- Jump directly to the end of the loop.
2806
2807 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2808 goto Skip_Extra_Actual_Generation;
2809
2810 else
2811 -- If the actual is a type conversion, then the constrained
2812 -- test applies to the actual, not the target type.
2813
2814 declare
2815 Act_Prev : Node_Id;
2816
2817 begin
2818 -- Test for unchecked conversions as well, which can occur
2819 -- as out parameter actuals on calls to stream procedures.
2820
2821 Act_Prev := Prev;
2822 while Nkind_In (Act_Prev, N_Type_Conversion,
2823 N_Unchecked_Type_Conversion)
2824 loop
2825 Act_Prev := Expression (Act_Prev);
2826 end loop;
2827
2828 -- If the expression is a conversion of a dereference, this
2829 -- is internally generated code that manipulates addresses,
2830 -- e.g. when building interface tables. No check should
2831 -- occur in this case, and the discriminated object is not
2832 -- directly a hand.
2833
2834 if not Comes_From_Source (Actual)
2835 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2836 and then Nkind (Act_Prev) = N_Explicit_Dereference
2837 then
2838 Add_Extra_Actual
2839 (New_Occurrence_Of (Standard_False, Loc),
2840 Extra_Constrained (Formal));
2841
2842 else
2843 Add_Extra_Actual
2844 (Make_Attribute_Reference (Sloc (Prev),
2845 Prefix =>
2846 Duplicate_Subexpr_No_Checks
2847 (Act_Prev, Name_Req => True),
2848 Attribute_Name => Name_Constrained),
2849 Extra_Constrained (Formal));
2850 end if;
2851 end;
2852 end if;
2853 end if;
2854
2855 -- Create possible extra actual for accessibility level
2856
2857 if Present (Extra_Accessibility (Formal)) then
2858
2859 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2860 -- attribute, then the original actual may be an aliased object
2861 -- occurring as the prefix in a call using "Object.Operation"
2862 -- notation. In that case we must pass the level of the object,
2863 -- so Prev_Orig is reset to Prev and the attribute will be
2864 -- processed by the code for Access attributes further below.
2865
2866 if Prev_Orig /= Prev
2867 and then Nkind (Prev) = N_Attribute_Reference
2868 and then
2869 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2870 and then Is_Aliased_View (Prev_Orig)
2871 then
2872 Prev_Orig := Prev;
2873 end if;
2874
2875 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2876 -- accessibility levels.
2877
2878 if Is_Thunk (Current_Scope) then
2879 declare
2880 Parm_Ent : Entity_Id;
2881
2882 begin
2883 if Is_Controlling_Actual (Actual) then
2884
2885 -- Find the corresponding actual of the thunk
2886
2887 Parm_Ent := First_Entity (Current_Scope);
2888 for J in 2 .. Param_Count loop
2889 Next_Entity (Parm_Ent);
2890 end loop;
2891
2892 -- Handle unchecked conversion of access types generated
2893 -- in thunks (cf. Expand_Interface_Thunk).
2894
2895 elsif Is_Access_Type (Etype (Actual))
2896 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2897 then
2898 Parm_Ent := Entity (Expression (Actual));
2899
2900 else pragma Assert (Is_Entity_Name (Actual));
2901 Parm_Ent := Entity (Actual);
2902 end if;
2903
2904 Add_Extra_Actual
2905 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2906 Extra_Accessibility (Formal));
2907 end;
2908
2909 elsif Is_Entity_Name (Prev_Orig) then
2910
2911 -- When passing an access parameter, or a renaming of an access
2912 -- parameter, as the actual to another access parameter we need
2913 -- to pass along the actual's own access level parameter. This
2914 -- is done if we are within the scope of the formal access
2915 -- parameter (if this is an inlined body the extra formal is
2916 -- irrelevant).
2917
2918 if (Is_Formal (Entity (Prev_Orig))
2919 or else
2920 (Present (Renamed_Object (Entity (Prev_Orig)))
2921 and then
2922 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2923 and then
2924 Is_Formal
2925 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2926 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2927 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2928 then
2929 declare
2930 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2931
2932 begin
2933 pragma Assert (Present (Parm_Ent));
2934
2935 if Present (Extra_Accessibility (Parm_Ent)) then
2936 Add_Extra_Actual
2937 (New_Occurrence_Of
2938 (Extra_Accessibility (Parm_Ent), Loc),
2939 Extra_Accessibility (Formal));
2940
2941 -- If the actual access parameter does not have an
2942 -- associated extra formal providing its scope level,
2943 -- then treat the actual as having library-level
2944 -- accessibility.
2945
2946 else
2947 Add_Extra_Actual
2948 (Make_Integer_Literal (Loc,
2949 Intval => Scope_Depth (Standard_Standard)),
2950 Extra_Accessibility (Formal));
2951 end if;
2952 end;
2953
2954 -- The actual is a normal access value, so just pass the level
2955 -- of the actual's access type.
2956
2957 else
2958 Add_Extra_Actual
2959 (Dynamic_Accessibility_Level (Prev_Orig),
2960 Extra_Accessibility (Formal));
2961 end if;
2962
2963 -- If the actual is an access discriminant, then pass the level
2964 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2965
2966 elsif Nkind (Prev_Orig) = N_Selected_Component
2967 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2968 E_Discriminant
2969 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2970 E_Anonymous_Access_Type
2971 then
2972 Add_Extra_Actual
2973 (Make_Integer_Literal (Loc,
2974 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2975 Extra_Accessibility (Formal));
2976
2977 -- All other cases
2978
2979 else
2980 case Nkind (Prev_Orig) is
2981
2982 when N_Attribute_Reference =>
2983 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2984
2985 -- For X'Access, pass on the level of the prefix X
2986
2987 when Attribute_Access =>
2988
2989 -- If this is an Access attribute applied to the
2990 -- the current instance object passed to a type
2991 -- initialization procedure, then use the level
2992 -- of the type itself. This is not really correct,
2993 -- as there should be an extra level parameter
2994 -- passed in with _init formals (only in the case
2995 -- where the type is immutably limited), but we
2996 -- don't have an easy way currently to create such
2997 -- an extra formal (init procs aren't ever frozen).
2998 -- For now we just use the level of the type,
2999 -- which may be too shallow, but that works better
3000 -- than passing Object_Access_Level of the type,
3001 -- which can be one level too deep in some cases.
3002 -- ???
3003
3004 if Is_Entity_Name (Prefix (Prev_Orig))
3005 and then Is_Type (Entity (Prefix (Prev_Orig)))
3006 then
3007 Add_Extra_Actual
3008 (Make_Integer_Literal (Loc,
3009 Intval =>
3010 Type_Access_Level
3011 (Entity (Prefix (Prev_Orig)))),
3012 Extra_Accessibility (Formal));
3013
3014 else
3015 Add_Extra_Actual
3016 (Make_Integer_Literal (Loc,
3017 Intval =>
3018 Object_Access_Level
3019 (Prefix (Prev_Orig))),
3020 Extra_Accessibility (Formal));
3021 end if;
3022
3023 -- Treat the unchecked attributes as library-level
3024
3025 when Attribute_Unchecked_Access |
3026 Attribute_Unrestricted_Access =>
3027 Add_Extra_Actual
3028 (Make_Integer_Literal (Loc,
3029 Intval => Scope_Depth (Standard_Standard)),
3030 Extra_Accessibility (Formal));
3031
3032 -- No other cases of attributes returning access
3033 -- values that can be passed to access parameters.
3034
3035 when others =>
3036 raise Program_Error;
3037
3038 end case;
3039
3040 -- For allocators we pass the level of the execution of the
3041 -- called subprogram, which is one greater than the current
3042 -- scope level.
3043
3044 when N_Allocator =>
3045 Add_Extra_Actual
3046 (Make_Integer_Literal (Loc,
3047 Intval => Scope_Depth (Current_Scope) + 1),
3048 Extra_Accessibility (Formal));
3049
3050 -- For most other cases we simply pass the level of the
3051 -- actual's access type. The type is retrieved from
3052 -- Prev rather than Prev_Orig, because in some cases
3053 -- Prev_Orig denotes an original expression that has
3054 -- not been analyzed.
3055
3056 when others =>
3057 Add_Extra_Actual
3058 (Dynamic_Accessibility_Level (Prev),
3059 Extra_Accessibility (Formal));
3060 end case;
3061 end if;
3062 end if;
3063
3064 -- Perform the check of 4.6(49) that prevents a null value from being
3065 -- passed as an actual to an access parameter. Note that the check
3066 -- is elided in the common cases of passing an access attribute or
3067 -- access parameter as an actual. Also, we currently don't enforce
3068 -- this check for expander-generated actuals and when -gnatdj is set.
3069
3070 if Ada_Version >= Ada_2005 then
3071
3072 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3073 -- the intent of 6.4.1(13) is that null-exclusion checks should
3074 -- not be done for 'out' parameters, even though it refers only
3075 -- to constraint checks, and a null_exclusion is not a constraint.
3076 -- Note that AI05-0196-1 corrects this mistake in the RM.
3077
3078 if Is_Access_Type (Etype (Formal))
3079 and then Can_Never_Be_Null (Etype (Formal))
3080 and then Ekind (Formal) /= E_Out_Parameter
3081 and then Nkind (Prev) /= N_Raise_Constraint_Error
3082 and then (Known_Null (Prev)
3083 or else not Can_Never_Be_Null (Etype (Prev)))
3084 then
3085 Install_Null_Excluding_Check (Prev);
3086 end if;
3087
3088 -- Ada_Version < Ada_2005
3089
3090 else
3091 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3092 or else Access_Checks_Suppressed (Subp)
3093 then
3094 null;
3095
3096 elsif Debug_Flag_J then
3097 null;
3098
3099 elsif not Comes_From_Source (Prev) then
3100 null;
3101
3102 elsif Is_Entity_Name (Prev)
3103 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3104 then
3105 null;
3106
3107 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3108 null;
3109
3110 -- Suppress null checks when passing to access parameters of Java
3111 -- and CIL subprograms. (Should this be done for other foreign
3112 -- conventions as well ???)
3113
3114 elsif Convention (Subp) = Convention_Java
3115 or else Convention (Subp) = Convention_CIL
3116 then
3117 null;
3118
3119 else
3120 Install_Null_Excluding_Check (Prev);
3121 end if;
3122 end if;
3123
3124 -- Perform appropriate validity checks on parameters that
3125 -- are entities.
3126
3127 if Validity_Checks_On then
3128 if (Ekind (Formal) = E_In_Parameter
3129 and then Validity_Check_In_Params)
3130 or else
3131 (Ekind (Formal) = E_In_Out_Parameter
3132 and then Validity_Check_In_Out_Params)
3133 then
3134 -- If the actual is an indexed component of a packed type (or
3135 -- is an indexed or selected component whose prefix recursively
3136 -- meets this condition), it has not been expanded yet. It will
3137 -- be copied in the validity code that follows, and has to be
3138 -- expanded appropriately, so reanalyze it.
3139
3140 -- What we do is just to unset analyzed bits on prefixes till
3141 -- we reach something that does not have a prefix.
3142
3143 declare
3144 Nod : Node_Id;
3145
3146 begin
3147 Nod := Actual;
3148 while Nkind_In (Nod, N_Indexed_Component,
3149 N_Selected_Component)
3150 loop
3151 Set_Analyzed (Nod, False);
3152 Nod := Prefix (Nod);
3153 end loop;
3154 end;
3155
3156 Ensure_Valid (Actual);
3157 end if;
3158 end if;
3159
3160 -- For Ada 2012, if a parameter is aliased, the actual must be a
3161 -- tagged type or an aliased view of an object.
3162
3163 if Is_Aliased (Formal)
3164 and then not Is_Aliased_View (Actual)
3165 and then not Is_Tagged_Type (Etype (Formal))
3166 then
3167 Error_Msg_NE
3168 ("actual for aliased formal& must be aliased object",
3169 Actual, Formal);
3170 end if;
3171
3172 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3173 -- since this is a left side reference. We only do this for calls
3174 -- from the source program since we assume that compiler generated
3175 -- calls explicitly generate any required checks. We also need it
3176 -- only if we are doing standard validity checks, since clearly it is
3177 -- not needed if validity checks are off, and in subscript validity
3178 -- checking mode, all indexed components are checked with a call
3179 -- directly from Expand_N_Indexed_Component.
3180
3181 if Comes_From_Source (Call_Node)
3182 and then Ekind (Formal) /= E_In_Parameter
3183 and then Validity_Checks_On
3184 and then Validity_Check_Default
3185 and then not Validity_Check_Subscripts
3186 then
3187 Check_Valid_Lvalue_Subscripts (Actual);
3188 end if;
3189
3190 -- Mark any scalar OUT parameter that is a simple variable as no
3191 -- longer known to be valid (unless the type is always valid). This
3192 -- reflects the fact that if an OUT parameter is never set in a
3193 -- procedure, then it can become invalid on the procedure return.
3194
3195 if Ekind (Formal) = E_Out_Parameter
3196 and then Is_Entity_Name (Actual)
3197 and then Ekind (Entity (Actual)) = E_Variable
3198 and then not Is_Known_Valid (Etype (Actual))
3199 then
3200 Set_Is_Known_Valid (Entity (Actual), False);
3201 end if;
3202
3203 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3204 -- clear current values, since they can be clobbered. We are probably
3205 -- doing this in more places than we need to, but better safe than
3206 -- sorry when it comes to retaining bad current values.
3207
3208 if Ekind (Formal) /= E_In_Parameter
3209 and then Is_Entity_Name (Actual)
3210 and then Present (Entity (Actual))
3211 then
3212 declare
3213 Ent : constant Entity_Id := Entity (Actual);
3214 Sav : Node_Id;
3215
3216 begin
3217 -- For an OUT or IN OUT parameter that is an assignable entity,
3218 -- we do not want to clobber the Last_Assignment field, since
3219 -- if it is set, it was precisely because it is indeed an OUT
3220 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3221 -- since the subprogram could have returned in invalid value.
3222
3223 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3224 and then Is_Assignable (Ent)
3225 then
3226 Sav := Last_Assignment (Ent);
3227 Kill_Current_Values (Ent);
3228 Set_Last_Assignment (Ent, Sav);
3229 Set_Is_Known_Valid (Ent, False);
3230
3231 -- For all other cases, just kill the current values
3232
3233 else
3234 Kill_Current_Values (Ent);
3235 end if;
3236 end;
3237 end if;
3238
3239 -- If the formal is class wide and the actual is an aggregate, force
3240 -- evaluation so that the back end who does not know about class-wide
3241 -- type, does not generate a temporary of the wrong size.
3242
3243 if not Is_Class_Wide_Type (Etype (Formal)) then
3244 null;
3245
3246 elsif Nkind (Actual) = N_Aggregate
3247 or else (Nkind (Actual) = N_Qualified_Expression
3248 and then Nkind (Expression (Actual)) = N_Aggregate)
3249 then
3250 Force_Evaluation (Actual);
3251 end if;
3252
3253 -- In a remote call, if the formal is of a class-wide type, check
3254 -- that the actual meets the requirements described in E.4(18).
3255
3256 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3257 Insert_Action (Actual,
3258 Make_Transportable_Check (Loc,
3259 Duplicate_Subexpr_Move_Checks (Actual)));
3260 end if;
3261
3262 -- This label is required when skipping extra actual generation for
3263 -- Unchecked_Union parameters.
3264
3265 <<Skip_Extra_Actual_Generation>>
3266
3267 Param_Count := Param_Count + 1;
3268 Next_Actual (Actual);
3269 Next_Formal (Formal);
3270 end loop;
3271
3272 -- If we are calling an Ada 2012 function which needs to have the
3273 -- "accessibility level determined by the point of call" (AI05-0234)
3274 -- passed in to it, then pass it in.
3275
3276 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3277 and then
3278 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3279 then
3280 declare
3281 Ancestor : Node_Id := Parent (Call_Node);
3282 Level : Node_Id := Empty;
3283 Defer : Boolean := False;
3284
3285 begin
3286 -- Unimplemented: if Subp returns an anonymous access type, then
3287
3288 -- a) if the call is the operand of an explict conversion, then
3289 -- the target type of the conversion (a named access type)
3290 -- determines the accessibility level pass in;
3291
3292 -- b) if the call defines an access discriminant of an object
3293 -- (e.g., the discriminant of an object being created by an
3294 -- allocator, or the discriminant of a function result),
3295 -- then the accessibility level to pass in is that of the
3296 -- discriminated object being initialized).
3297
3298 -- ???
3299
3300 while Nkind (Ancestor) = N_Qualified_Expression
3301 loop
3302 Ancestor := Parent (Ancestor);
3303 end loop;
3304
3305 case Nkind (Ancestor) is
3306 when N_Allocator =>
3307
3308 -- At this point, we'd like to assign
3309
3310 -- Level := Dynamic_Accessibility_Level (Ancestor);
3311
3312 -- but Etype of Ancestor may not have been set yet,
3313 -- so that doesn't work.
3314
3315 -- Handle this later in Expand_Allocator_Expression.
3316
3317 Defer := True;
3318
3319 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3320 declare
3321 Def_Id : constant Entity_Id :=
3322 Defining_Identifier (Ancestor);
3323
3324 begin
3325 if Is_Return_Object (Def_Id) then
3326 if Present (Extra_Accessibility_Of_Result
3327 (Return_Applies_To (Scope (Def_Id))))
3328 then
3329 -- Pass along value that was passed in if the
3330 -- routine we are returning from also has an
3331 -- Accessibility_Of_Result formal.
3332
3333 Level :=
3334 New_Occurrence_Of
3335 (Extra_Accessibility_Of_Result
3336 (Return_Applies_To (Scope (Def_Id))), Loc);
3337 end if;
3338 else
3339 Level :=
3340 Make_Integer_Literal (Loc,
3341 Intval => Object_Access_Level (Def_Id));
3342 end if;
3343 end;
3344
3345 when N_Simple_Return_Statement =>
3346 if Present (Extra_Accessibility_Of_Result
3347 (Return_Applies_To
3348 (Return_Statement_Entity (Ancestor))))
3349 then
3350 -- Pass along value that was passed in if the returned
3351 -- routine also has an Accessibility_Of_Result formal.
3352
3353 Level :=
3354 New_Occurrence_Of
3355 (Extra_Accessibility_Of_Result
3356 (Return_Applies_To
3357 (Return_Statement_Entity (Ancestor))), Loc);
3358 end if;
3359
3360 when others =>
3361 null;
3362 end case;
3363
3364 if not Defer then
3365 if not Present (Level) then
3366
3367 -- The "innermost master that evaluates the function call".
3368
3369 -- ??? - Should we use Integer'Last here instead in order
3370 -- to deal with (some of) the problems associated with
3371 -- calls to subps whose enclosing scope is unknown (e.g.,
3372 -- Anon_Access_To_Subp_Param.all)?
3373
3374 Level := Make_Integer_Literal (Loc,
3375 Scope_Depth (Current_Scope) + 1);
3376 end if;
3377
3378 Add_Extra_Actual
3379 (Level,
3380 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3381 end if;
3382 end;
3383 end if;
3384
3385 -- If we are expanding the RHS of an assignment we need to check if tag
3386 -- propagation is needed. You might expect this processing to be in
3387 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3388 -- assignment might be transformed to a declaration for an unconstrained
3389 -- value if the expression is classwide.
3390
3391 if Nkind (Call_Node) = N_Function_Call
3392 and then Is_Tag_Indeterminate (Call_Node)
3393 and then Is_Entity_Name (Name (Call_Node))
3394 then
3395 declare
3396 Ass : Node_Id := Empty;
3397
3398 begin
3399 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3400 Ass := Parent (Call_Node);
3401
3402 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3403 and then Nkind (Parent (Parent (Call_Node))) =
3404 N_Assignment_Statement
3405 then
3406 Ass := Parent (Parent (Call_Node));
3407
3408 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3409 and then Nkind (Parent (Parent (Call_Node))) =
3410 N_Assignment_Statement
3411 then
3412 Ass := Parent (Parent (Call_Node));
3413 end if;
3414
3415 if Present (Ass)
3416 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3417 then
3418 if Is_Access_Type (Etype (Call_Node)) then
3419 if Designated_Type (Etype (Call_Node)) /=
3420 Root_Type (Etype (Name (Ass)))
3421 then
3422 Error_Msg_NE
3423 ("tag-indeterminate expression "
3424 & " must have designated type& (RM 5.2 (6))",
3425 Call_Node, Root_Type (Etype (Name (Ass))));
3426 else
3427 Propagate_Tag (Name (Ass), Call_Node);
3428 end if;
3429
3430 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3431 Error_Msg_NE
3432 ("tag-indeterminate expression must have type&"
3433 & "(RM 5.2 (6))",
3434 Call_Node, Root_Type (Etype (Name (Ass))));
3435
3436 else
3437 Propagate_Tag (Name (Ass), Call_Node);
3438 end if;
3439
3440 -- The call will be rewritten as a dispatching call, and
3441 -- expanded as such.
3442
3443 return;
3444 end if;
3445 end;
3446 end if;
3447
3448 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3449 -- it to point to the correct secondary virtual table
3450
3451 if Nkind (Call_Node) in N_Subprogram_Call
3452 and then CW_Interface_Formals_Present
3453 then
3454 Expand_Interface_Actuals (Call_Node);
3455 end if;
3456
3457 -- Deals with Dispatch_Call if we still have a call, before expanding
3458 -- extra actuals since this will be done on the re-analysis of the
3459 -- dispatching call. Note that we do not try to shorten the actual list
3460 -- for a dispatching call, it would not make sense to do so. Expansion
3461 -- of dispatching calls is suppressed when VM_Target, because the VM
3462 -- back-ends directly handle the generation of dispatching calls and
3463 -- would have to undo any expansion to an indirect call.
3464
3465 if Nkind (Call_Node) in N_Subprogram_Call
3466 and then Present (Controlling_Argument (Call_Node))
3467 then
3468 declare
3469 Call_Typ : constant Entity_Id := Etype (Call_Node);
3470 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3471 Eq_Prim_Op : Entity_Id := Empty;
3472 New_Call : Node_Id;
3473 Param : Node_Id;
3474 Prev_Call : Node_Id;
3475
3476 begin
3477 if not Is_Limited_Type (Typ) then
3478 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3479 end if;
3480
3481 if Tagged_Type_Expansion then
3482 Expand_Dispatching_Call (Call_Node);
3483
3484 -- The following return is worrisome. Is it really OK to skip
3485 -- all remaining processing in this procedure ???
3486
3487 return;
3488
3489 -- VM targets
3490
3491 else
3492 Apply_Tag_Checks (Call_Node);
3493
3494 -- If this is a dispatching "=", we must first compare the
3495 -- tags so we generate: x.tag = y.tag and then x = y
3496
3497 if Subp = Eq_Prim_Op then
3498
3499 -- Mark the node as analyzed to avoid reanalizing this
3500 -- dispatching call (which would cause a never-ending loop)
3501
3502 Prev_Call := Relocate_Node (Call_Node);
3503 Set_Analyzed (Prev_Call);
3504
3505 Param := First_Actual (Call_Node);
3506 New_Call :=
3507 Make_And_Then (Loc,
3508 Left_Opnd =>
3509 Make_Op_Eq (Loc,
3510 Left_Opnd =>
3511 Make_Selected_Component (Loc,
3512 Prefix => New_Value (Param),
3513 Selector_Name =>
3514 New_Occurrence_Of
3515 (First_Tag_Component (Typ), Loc)),
3516
3517 Right_Opnd =>
3518 Make_Selected_Component (Loc,
3519 Prefix =>
3520 Unchecked_Convert_To (Typ,
3521 New_Value (Next_Actual (Param))),
3522 Selector_Name =>
3523 New_Occurrence_Of
3524 (First_Tag_Component (Typ), Loc))),
3525 Right_Opnd => Prev_Call);
3526
3527 Rewrite (Call_Node, New_Call);
3528
3529 Analyze_And_Resolve
3530 (Call_Node, Call_Typ, Suppress => All_Checks);
3531 end if;
3532
3533 -- Expansion of a dispatching call results in an indirect call,
3534 -- which in turn causes current values to be killed (see
3535 -- Resolve_Call), so on VM targets we do the call here to
3536 -- ensure consistent warnings between VM and non-VM targets.
3537
3538 Kill_Current_Values;
3539 end if;
3540
3541 -- If this is a dispatching "=" then we must update the reference
3542 -- to the call node because we generated:
3543 -- x.tag = y.tag and then x = y
3544
3545 if Subp = Eq_Prim_Op then
3546 Call_Node := Right_Opnd (Call_Node);
3547 end if;
3548 end;
3549 end if;
3550
3551 -- Similarly, expand calls to RCI subprograms on which pragma
3552 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3553 -- later. Do this only when the call comes from source since we
3554 -- do not want such a rewriting to occur in expanded code.
3555
3556 if Is_All_Remote_Call (Call_Node) then
3557 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3558
3559 -- Similarly, do not add extra actuals for an entry call whose entity
3560 -- is a protected procedure, or for an internal protected subprogram
3561 -- call, because it will be rewritten as a protected subprogram call
3562 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3563
3564 elsif Is_Protected_Type (Scope (Subp))
3565 and then (Ekind (Subp) = E_Procedure
3566 or else Ekind (Subp) = E_Function)
3567 then
3568 null;
3569
3570 -- During that loop we gathered the extra actuals (the ones that
3571 -- correspond to Extra_Formals), so now they can be appended.
3572
3573 else
3574 while Is_Non_Empty_List (Extra_Actuals) loop
3575 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3576 end loop;
3577 end if;
3578
3579 -- At this point we have all the actuals, so this is the point at which
3580 -- the various expansion activities for actuals is carried out.
3581
3582 Expand_Actuals (Call_Node, Subp);
3583
3584 -- Verify that the actuals do not share storage. This check must be done
3585 -- on the caller side rather that inside the subprogram to avoid issues
3586 -- of parameter passing.
3587
3588 if Check_Aliasing_Of_Parameters then
3589 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3590 end if;
3591
3592 -- If the subprogram is a renaming, or if it is inherited, replace it in
3593 -- the call with the name of the actual subprogram being called. If this
3594 -- is a dispatching call, the run-time decides what to call. The Alias
3595 -- attribute does not apply to entries.
3596
3597 if Nkind (Call_Node) /= N_Entry_Call_Statement
3598 and then No (Controlling_Argument (Call_Node))
3599 and then Present (Parent_Subp)
3600 and then not Is_Direct_Deep_Call (Subp)
3601 then
3602 if Present (Inherited_From_Formal (Subp)) then
3603 Parent_Subp := Inherited_From_Formal (Subp);
3604 else
3605 Parent_Subp := Ultimate_Alias (Parent_Subp);
3606 end if;
3607
3608 -- The below setting of Entity is suspect, see F109-018 discussion???
3609
3610 Set_Entity (Name (Call_Node), Parent_Subp);
3611
3612 if Is_Abstract_Subprogram (Parent_Subp)
3613 and then not In_Instance
3614 then
3615 Error_Msg_NE
3616 ("cannot call abstract subprogram &!",
3617 Name (Call_Node), Parent_Subp);
3618 end if;
3619
3620 -- Inspect all formals of derived subprogram Subp. Compare parameter
3621 -- types with the parent subprogram and check whether an actual may
3622 -- need a type conversion to the corresponding formal of the parent
3623 -- subprogram.
3624
3625 -- Not clear whether intrinsic subprograms need such conversions. ???
3626
3627 if not Is_Intrinsic_Subprogram (Parent_Subp)
3628 or else Is_Generic_Instance (Parent_Subp)
3629 then
3630 declare
3631 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3632 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3633 -- and resolve the newly generated construct.
3634
3635 -------------
3636 -- Convert --
3637 -------------
3638
3639 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3640 begin
3641 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3642 Analyze (Act);
3643 Resolve (Act, Typ);
3644 end Convert;
3645
3646 -- Local variables
3647
3648 Actual_Typ : Entity_Id;
3649 Formal_Typ : Entity_Id;
3650 Parent_Typ : Entity_Id;
3651
3652 begin
3653 Actual := First_Actual (Call_Node);
3654 Formal := First_Formal (Subp);
3655 Parent_Formal := First_Formal (Parent_Subp);
3656 while Present (Formal) loop
3657 Actual_Typ := Etype (Actual);
3658 Formal_Typ := Etype (Formal);
3659 Parent_Typ := Etype (Parent_Formal);
3660
3661 -- For an IN parameter of a scalar type, the parent formal
3662 -- type and derived formal type differ or the parent formal
3663 -- type and actual type do not match statically.
3664
3665 if Is_Scalar_Type (Formal_Typ)
3666 and then Ekind (Formal) = E_In_Parameter
3667 and then Formal_Typ /= Parent_Typ
3668 and then
3669 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3670 and then not Raises_Constraint_Error (Actual)
3671 then
3672 Convert (Actual, Parent_Typ);
3673 Enable_Range_Check (Actual);
3674
3675 -- If the actual has been marked as requiring a range
3676 -- check, then generate it here.
3677
3678 if Do_Range_Check (Actual) then
3679 Set_Do_Range_Check (Actual, False);
3680 Generate_Range_Check
3681 (Actual, Etype (Formal), CE_Range_Check_Failed);
3682 end if;
3683
3684 -- For access types, the parent formal type and actual type
3685 -- differ.
3686
3687 elsif Is_Access_Type (Formal_Typ)
3688 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3689 then
3690 if Ekind (Formal) /= E_In_Parameter then
3691 Convert (Actual, Parent_Typ);
3692
3693 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3694 and then Designated_Type (Parent_Typ) /=
3695 Designated_Type (Actual_Typ)
3696 and then not Is_Controlling_Formal (Formal)
3697 then
3698 -- This unchecked conversion is not necessary unless
3699 -- inlining is enabled, because in that case the type
3700 -- mismatch may become visible in the body about to be
3701 -- inlined.
3702
3703 Rewrite (Actual,
3704 Unchecked_Convert_To (Parent_Typ,
3705 Relocate_Node (Actual)));
3706 Analyze (Actual);
3707 Resolve (Actual, Parent_Typ);
3708 end if;
3709
3710 -- For array and record types, the parent formal type and
3711 -- derived formal type have different sizes or pragma Pack
3712 -- status.
3713
3714 elsif ((Is_Array_Type (Formal_Typ)
3715 and then Is_Array_Type (Parent_Typ))
3716 or else
3717 (Is_Record_Type (Formal_Typ)
3718 and then Is_Record_Type (Parent_Typ)))
3719 and then
3720 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3721 or else Has_Pragma_Pack (Formal_Typ) /=
3722 Has_Pragma_Pack (Parent_Typ))
3723 then
3724 Convert (Actual, Parent_Typ);
3725 end if;
3726
3727 Next_Actual (Actual);
3728 Next_Formal (Formal);
3729 Next_Formal (Parent_Formal);
3730 end loop;
3731 end;
3732 end if;
3733
3734 Orig_Subp := Subp;
3735 Subp := Parent_Subp;
3736 end if;
3737
3738 -- Deal with case where call is an explicit dereference
3739
3740 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3741
3742 -- Handle case of access to protected subprogram type
3743
3744 if Is_Access_Protected_Subprogram_Type
3745 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3746 then
3747 -- If this is a call through an access to protected operation, the
3748 -- prefix has the form (object'address, operation'access). Rewrite
3749 -- as a for other protected calls: the object is the 1st parameter
3750 -- of the list of actuals.
3751
3752 declare
3753 Call : Node_Id;
3754 Parm : List_Id;
3755 Nam : Node_Id;
3756 Obj : Node_Id;
3757 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3758
3759 T : constant Entity_Id :=
3760 Equivalent_Type (Base_Type (Etype (Ptr)));
3761
3762 D_T : constant Entity_Id :=
3763 Designated_Type (Base_Type (Etype (Ptr)));
3764
3765 begin
3766 Obj :=
3767 Make_Selected_Component (Loc,
3768 Prefix => Unchecked_Convert_To (T, Ptr),
3769 Selector_Name =>
3770 New_Occurrence_Of (First_Entity (T), Loc));
3771
3772 Nam :=
3773 Make_Selected_Component (Loc,
3774 Prefix => Unchecked_Convert_To (T, Ptr),
3775 Selector_Name =>
3776 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3777
3778 Nam :=
3779 Make_Explicit_Dereference (Loc,
3780 Prefix => Nam);
3781
3782 if Present (Parameter_Associations (Call_Node)) then
3783 Parm := Parameter_Associations (Call_Node);
3784 else
3785 Parm := New_List;
3786 end if;
3787
3788 Prepend (Obj, Parm);
3789
3790 if Etype (D_T) = Standard_Void_Type then
3791 Call :=
3792 Make_Procedure_Call_Statement (Loc,
3793 Name => Nam,
3794 Parameter_Associations => Parm);
3795 else
3796 Call :=
3797 Make_Function_Call (Loc,
3798 Name => Nam,
3799 Parameter_Associations => Parm);
3800 end if;
3801
3802 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3803 Set_Etype (Call, Etype (D_T));
3804
3805 -- We do not re-analyze the call to avoid infinite recursion.
3806 -- We analyze separately the prefix and the object, and set
3807 -- the checks on the prefix that would otherwise be emitted
3808 -- when resolving a call.
3809
3810 Rewrite (Call_Node, Call);
3811 Analyze (Nam);
3812 Apply_Access_Check (Nam);
3813 Analyze (Obj);
3814 return;
3815 end;
3816 end if;
3817 end if;
3818
3819 -- If this is a call to an intrinsic subprogram, then perform the
3820 -- appropriate expansion to the corresponding tree node and we
3821 -- are all done (since after that the call is gone).
3822
3823 -- In the case where the intrinsic is to be processed by the back end,
3824 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3825 -- since the idea in this case is to pass the call unchanged. If the
3826 -- intrinsic is an inherited unchecked conversion, and the derived type
3827 -- is the target type of the conversion, we must retain it as the return
3828 -- type of the expression. Otherwise the expansion below, which uses the
3829 -- parent operation, will yield the wrong type.
3830
3831 if Is_Intrinsic_Subprogram (Subp) then
3832 Expand_Intrinsic_Call (Call_Node, Subp);
3833
3834 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3835 and then Parent_Subp /= Orig_Subp
3836 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3837 then
3838 Set_Etype (Call_Node, Etype (Orig_Subp));
3839 end if;
3840
3841 return;
3842 end if;
3843
3844 if Ekind_In (Subp, E_Function, E_Procedure) then
3845
3846 -- We perform two simple optimization on calls:
3847
3848 -- a) replace calls to null procedures unconditionally;
3849
3850 -- b) for To_Address, just do an unchecked conversion. Not only is
3851 -- this efficient, but it also avoids order of elaboration problems
3852 -- when address clauses are inlined (address expression elaborated
3853 -- at the wrong point).
3854
3855 -- We perform these optimization regardless of whether we are in the
3856 -- main unit or in a unit in the context of the main unit, to ensure
3857 -- that tree generated is the same in both cases, for CodePeer use.
3858
3859 if Is_RTE (Subp, RE_To_Address) then
3860 Rewrite (Call_Node,
3861 Unchecked_Convert_To
3862 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3863 return;
3864
3865 elsif Is_Null_Procedure (Subp) then
3866 Rewrite (Call_Node, Make_Null_Statement (Loc));
3867 return;
3868 end if;
3869
3870 -- Handle inlining (old semantics)
3871
3872 if Is_Inlined (Subp) and then not Debug_Flag_Dot_K then
3873 Inlined_Subprogram : declare
3874 Bod : Node_Id;
3875 Must_Inline : Boolean := False;
3876 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3877
3878 begin
3879 -- Verify that the body to inline has already been seen, and
3880 -- that if the body is in the current unit the inlining does
3881 -- not occur earlier. This avoids order-of-elaboration problems
3882 -- in the back end.
3883
3884 -- This should be documented in sinfo/einfo ???
3885
3886 if No (Spec)
3887 or else Nkind (Spec) /= N_Subprogram_Declaration
3888 or else No (Body_To_Inline (Spec))
3889 then
3890 Must_Inline := False;
3891
3892 -- If this an inherited function that returns a private type,
3893 -- do not inline if the full view is an unconstrained array,
3894 -- because such calls cannot be inlined.
3895
3896 elsif Present (Orig_Subp)
3897 and then Is_Array_Type (Etype (Orig_Subp))
3898 and then not Is_Constrained (Etype (Orig_Subp))
3899 then
3900 Must_Inline := False;
3901
3902 elsif In_Unfrozen_Instance (Scope (Subp)) then
3903 Must_Inline := False;
3904
3905 else
3906 Bod := Body_To_Inline (Spec);
3907
3908 if (In_Extended_Main_Code_Unit (Call_Node)
3909 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3910 or else Has_Pragma_Inline_Always (Subp))
3911 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3912 or else
3913 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3914 then
3915 Must_Inline := True;
3916
3917 -- If we are compiling a package body that is not the main
3918 -- unit, it must be for inlining/instantiation purposes,
3919 -- in which case we inline the call to insure that the same
3920 -- temporaries are generated when compiling the body by
3921 -- itself. Otherwise link errors can occur.
3922
3923 -- If the function being called is itself in the main unit,
3924 -- we cannot inline, because there is a risk of double
3925 -- elaboration and/or circularity: the inlining can make
3926 -- visible a private entity in the body of the main unit,
3927 -- that gigi will see before its sees its proper definition.
3928
3929 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3930 and then In_Package_Body
3931 then
3932 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3933 end if;
3934 end if;
3935
3936 if Must_Inline then
3937 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3938
3939 else
3940 -- Let the back end handle it
3941
3942 Add_Inlined_Body (Subp);
3943
3944 if Front_End_Inlining
3945 and then Nkind (Spec) = N_Subprogram_Declaration
3946 and then (In_Extended_Main_Code_Unit (Call_Node))
3947 and then No (Body_To_Inline (Spec))
3948 and then not Has_Completion (Subp)
3949 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3950 then
3951 Cannot_Inline
3952 ("cannot inline& (body not seen yet)?",
3953 Call_Node, Subp);
3954 end if;
3955 end if;
3956 end Inlined_Subprogram;
3957
3958 -- Handle inlining (new semantics)
3959
3960 elsif Is_Inlined (Subp) then
3961 declare
3962 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3963
3964 begin
3965 if Must_Inline (Subp) then
3966 if In_Extended_Main_Code_Unit (Call_Node)
3967 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3968 and then not Has_Completion (Subp)
3969 then
3970 Cannot_Inline
3971 ("cannot inline& (body not seen yet)?",
3972 Call_Node, Subp);
3973
3974 else
3975 Do_Inline_Always (Subp, Orig_Subp);
3976 end if;
3977
3978 elsif Optimization_Level > 0 then
3979 Do_Inline (Subp, Orig_Subp);
3980 end if;
3981
3982 -- The call may have been inlined or may have been passed to
3983 -- the backend. No further action needed if it was inlined.
3984
3985 if Nkind (N) /= N_Function_Call then
3986 return;
3987 end if;
3988 end;
3989 end if;
3990 end if;
3991
3992 -- Check for protected subprogram. This is either an intra-object call,
3993 -- or a protected function call. Protected procedure calls are rewritten
3994 -- as entry calls and handled accordingly.
3995
3996 -- In Ada 2005, this may be an indirect call to an access parameter that
3997 -- is an access_to_subprogram. In that case the anonymous type has a
3998 -- scope that is a protected operation, but the call is a regular one.
3999 -- In either case do not expand call if subprogram is eliminated.
4000
4001 Scop := Scope (Subp);
4002
4003 if Nkind (Call_Node) /= N_Entry_Call_Statement
4004 and then Is_Protected_Type (Scop)
4005 and then Ekind (Subp) /= E_Subprogram_Type
4006 and then not Is_Eliminated (Subp)
4007 then
4008 -- If the call is an internal one, it is rewritten as a call to the
4009 -- corresponding unprotected subprogram.
4010
4011 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4012 end if;
4013
4014 -- Functions returning controlled objects need special attention. If
4015 -- the return type is limited, then the context is initialization and
4016 -- different processing applies. If the call is to a protected function,
4017 -- the expansion above will call Expand_Call recursively. Otherwise the
4018 -- function call is transformed into a temporary which obtains the
4019 -- result from the secondary stack.
4020
4021 if Needs_Finalization (Etype (Subp)) then
4022 if not Is_Limited_View (Etype (Subp))
4023 and then
4024 (No (First_Formal (Subp))
4025 or else
4026 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4027 then
4028 Expand_Ctrl_Function_Call (Call_Node);
4029
4030 -- Build-in-place function calls which appear in anonymous contexts
4031 -- need a transient scope to ensure the proper finalization of the
4032 -- intermediate result after its use.
4033
4034 elsif Is_Build_In_Place_Function_Call (Call_Node)
4035 and then
4036 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
4037 N_Function_Call,
4038 N_Indexed_Component,
4039 N_Object_Renaming_Declaration,
4040 N_Procedure_Call_Statement,
4041 N_Selected_Component,
4042 N_Slice)
4043 then
4044 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4045 end if;
4046 end if;
4047
4048 -- Test for First_Optional_Parameter, and if so, truncate parameter list
4049 -- if there are optional parameters at the trailing end.
4050 -- Note: we never delete procedures for call via a pointer.
4051
4052 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
4053 and then Present (First_Optional_Parameter (Subp))
4054 then
4055 declare
4056 Last_Keep_Arg : Node_Id;
4057
4058 begin
4059 -- Last_Keep_Arg will hold the last actual that should be kept.
4060 -- If it remains empty at the end, it means that all parameters
4061 -- are optional.
4062
4063 Last_Keep_Arg := Empty;
4064
4065 -- Find first optional parameter, must be present since we checked
4066 -- the validity of the parameter before setting it.
4067
4068 Formal := First_Formal (Subp);
4069 Actual := First_Actual (Call_Node);
4070 while Formal /= First_Optional_Parameter (Subp) loop
4071 Last_Keep_Arg := Actual;
4072 Next_Formal (Formal);
4073 Next_Actual (Actual);
4074 end loop;
4075
4076 -- We have Formal and Actual pointing to the first potentially
4077 -- droppable argument. We can drop all the trailing arguments
4078 -- whose actual matches the default. Note that we know that all
4079 -- remaining formals have defaults, because we checked that this
4080 -- requirement was met before setting First_Optional_Parameter.
4081
4082 -- We use Fully_Conformant_Expressions to check for identity
4083 -- between formals and actuals, which may miss some cases, but
4084 -- on the other hand, this is only an optimization (if we fail
4085 -- to truncate a parameter it does not affect functionality).
4086 -- So if the default is 3 and the actual is 1+2, we consider
4087 -- them unequal, which hardly seems worrisome.
4088
4089 while Present (Formal) loop
4090 if not Fully_Conformant_Expressions
4091 (Actual, Default_Value (Formal))
4092 then
4093 Last_Keep_Arg := Actual;
4094 end if;
4095
4096 Next_Formal (Formal);
4097 Next_Actual (Actual);
4098 end loop;
4099
4100 -- If no arguments, delete entire list, this is the easy case
4101
4102 if No (Last_Keep_Arg) then
4103 Set_Parameter_Associations (Call_Node, No_List);
4104 Set_First_Named_Actual (Call_Node, Empty);
4105
4106 -- Case where at the last retained argument is positional. This
4107 -- is also an easy case, since the retained arguments are already
4108 -- in the right form, and we don't need to worry about the order
4109 -- of arguments that get eliminated.
4110
4111 elsif Is_List_Member (Last_Keep_Arg) then
4112 while Present (Next (Last_Keep_Arg)) loop
4113 Discard_Node (Remove_Next (Last_Keep_Arg));
4114 end loop;
4115
4116 Set_First_Named_Actual (Call_Node, Empty);
4117
4118 -- This is the annoying case where the last retained argument
4119 -- is a named parameter. Since the original arguments are not
4120 -- in declaration order, we may have to delete some fairly
4121 -- random collection of arguments.
4122
4123 else
4124 declare
4125 Temp : Node_Id;
4126 Passoc : Node_Id;
4127
4128 begin
4129 -- First step, remove all the named parameters from the
4130 -- list (they are still chained using First_Named_Actual
4131 -- and Next_Named_Actual, so we have not lost them).
4132
4133 Temp := First (Parameter_Associations (Call_Node));
4134
4135 -- Case of all parameters named, remove them all
4136
4137 if Nkind (Temp) = N_Parameter_Association then
4138 -- Suppress warnings to avoid warning on possible
4139 -- infinite loop (because Call_Node is not modified).
4140
4141 pragma Warnings (Off);
4142 while Is_Non_Empty_List
4143 (Parameter_Associations (Call_Node))
4144 loop
4145 Temp :=
4146 Remove_Head (Parameter_Associations (Call_Node));
4147 end loop;
4148 pragma Warnings (On);
4149
4150 -- Case of mixed positional/named, remove named parameters
4151
4152 else
4153 while Nkind (Next (Temp)) /= N_Parameter_Association loop
4154 Next (Temp);
4155 end loop;
4156
4157 while Present (Next (Temp)) loop
4158 Remove (Next (Temp));
4159 end loop;
4160 end if;
4161
4162 -- Now we loop through the named parameters, till we get
4163 -- to the last one to be retained, adding them to the list.
4164 -- Note that the Next_Named_Actual list does not need to be
4165 -- touched since we are only reordering them on the actual
4166 -- parameter association list.
4167
4168 Passoc := Parent (First_Named_Actual (Call_Node));
4169 loop
4170 Temp := Relocate_Node (Passoc);
4171 Append_To
4172 (Parameter_Associations (Call_Node), Temp);
4173 exit when
4174 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
4175 Passoc := Parent (Next_Named_Actual (Passoc));
4176 end loop;
4177
4178 Set_Next_Named_Actual (Temp, Empty);
4179
4180 loop
4181 Temp := Next_Named_Actual (Passoc);
4182 exit when No (Temp);
4183 Set_Next_Named_Actual
4184 (Passoc, Next_Named_Actual (Parent (Temp)));
4185 end loop;
4186 end;
4187
4188 end if;
4189 end;
4190 end if;
4191 end Expand_Call;
4192
4193 -------------------------------
4194 -- Expand_Ctrl_Function_Call --
4195 -------------------------------
4196
4197 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4198 begin
4199 -- Optimization, if the returned value (which is on the sec-stack) is
4200 -- returned again, no need to copy/readjust/finalize, we can just pass
4201 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4202 -- attachment is needed
4203
4204 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4205 return;
4206 end if;
4207
4208 -- Resolution is now finished, make sure we don't start analysis again
4209 -- because of the duplication.
4210
4211 Set_Analyzed (N);
4212
4213 -- A function which returns a controlled object uses the secondary
4214 -- stack. Rewrite the call into a temporary which obtains the result of
4215 -- the function using 'reference.
4216
4217 Remove_Side_Effects (N);
4218
4219 -- When the temporary function result appears inside a case or an if
4220 -- expression, its lifetime must be extended to match that of the
4221 -- context. If not, the function result would be finalized prematurely
4222 -- and the evaluation of the expression could yield the wrong result.
4223
4224 if Within_Case_Or_If_Expression (N)
4225 and then Nkind (N) = N_Explicit_Dereference
4226 then
4227 Set_Is_Processed_Transient (Entity (Prefix (N)));
4228 end if;
4229 end Expand_Ctrl_Function_Call;
4230
4231 -------------------------
4232 -- Expand_Inlined_Call --
4233 -------------------------
4234
4235 procedure Expand_Inlined_Call
4236 (N : Node_Id;
4237 Subp : Entity_Id;
4238 Orig_Subp : Entity_Id)
4239 is
4240 Loc : constant Source_Ptr := Sloc (N);
4241 Is_Predef : constant Boolean :=
4242 Is_Predefined_File_Name
4243 (Unit_File_Name (Get_Source_Unit (Subp)));
4244 Orig_Bod : constant Node_Id :=
4245 Body_To_Inline (Unit_Declaration_Node (Subp));
4246
4247 Blk : Node_Id;
4248 Decl : Node_Id;
4249 Decls : constant List_Id := New_List;
4250 Exit_Lab : Entity_Id := Empty;
4251 F : Entity_Id;
4252 A : Node_Id;
4253 Lab_Decl : Node_Id;
4254 Lab_Id : Node_Id;
4255 New_A : Node_Id;
4256 Num_Ret : Int := 0;
4257 Ret_Type : Entity_Id;
4258
4259 Targ : Node_Id;
4260 -- The target of the call. If context is an assignment statement then
4261 -- this is the left-hand side of the assignment, else it is a temporary
4262 -- to which the return value is assigned prior to rewriting the call.
4263
4264 Targ1 : Node_Id;
4265 -- A separate target used when the return type is unconstrained
4266
4267 Temp : Entity_Id;
4268 Temp_Typ : Entity_Id;
4269
4270 Return_Object : Entity_Id := Empty;
4271 -- Entity in declaration in an extended_return_statement
4272
4273 Is_Unc : Boolean;
4274 Is_Unc_Decl : Boolean;
4275 -- If the type returned by the function is unconstrained and the call
4276 -- can be inlined, special processing is required.
4277
4278 procedure Make_Exit_Label;
4279 -- Build declaration for exit label to be used in Return statements,
4280 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4281 -- declaration). Does nothing if Exit_Lab already set.
4282
4283 function Process_Formals (N : Node_Id) return Traverse_Result;
4284 -- Replace occurrence of a formal with the corresponding actual, or the
4285 -- thunk generated for it. Replace a return statement with an assignment
4286 -- to the target of the call, with appropriate conversions if needed.
4287
4288 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
4289 -- If the call being expanded is that of an internal subprogram, set the
4290 -- sloc of the generated block to that of the call itself, so that the
4291 -- expansion is skipped by the "next" command in gdb.
4292 -- Same processing for a subprogram in a predefined file, e.g.
4293 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4294 -- simplify our own development.
4295
4296 procedure Reset_Dispatching_Calls (N : Node_Id);
4297 -- In subtree N search for occurrences of dispatching calls that use the
4298 -- Ada 2005 Object.Operation notation and the object is a formal of the
4299 -- inlined subprogram. Reset the entity associated with Operation in all
4300 -- the found occurrences.
4301
4302 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
4303 -- If the function body is a single expression, replace call with
4304 -- expression, else insert block appropriately.
4305
4306 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
4307 -- If procedure body has no local variables, inline body without
4308 -- creating block, otherwise rewrite call with block.
4309
4310 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
4311 -- Determine whether a formal parameter is used only once in Orig_Bod
4312
4313 ---------------------
4314 -- Make_Exit_Label --
4315 ---------------------
4316
4317 procedure Make_Exit_Label is
4318 Lab_Ent : Entity_Id;
4319 begin
4320 if No (Exit_Lab) then
4321 Lab_Ent := Make_Temporary (Loc, 'L');
4322 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
4323 Exit_Lab := Make_Label (Loc, Lab_Id);
4324 Lab_Decl :=
4325 Make_Implicit_Label_Declaration (Loc,
4326 Defining_Identifier => Lab_Ent,
4327 Label_Construct => Exit_Lab);
4328 end if;
4329 end Make_Exit_Label;
4330
4331 ---------------------
4332 -- Process_Formals --
4333 ---------------------
4334
4335 function Process_Formals (N : Node_Id) return Traverse_Result is
4336 A : Entity_Id;
4337 E : Entity_Id;
4338 Ret : Node_Id;
4339
4340 begin
4341 if Is_Entity_Name (N) and then Present (Entity (N)) then
4342 E := Entity (N);
4343
4344 if Is_Formal (E) and then Scope (E) = Subp then
4345 A := Renamed_Object (E);
4346
4347 -- Rewrite the occurrence of the formal into an occurrence of
4348 -- the actual. Also establish visibility on the proper view of
4349 -- the actual's subtype for the body's context (if the actual's
4350 -- subtype is private at the call point but its full view is
4351 -- visible to the body, then the inlined tree here must be
4352 -- analyzed with the full view).
4353
4354 if Is_Entity_Name (A) then
4355 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
4356 Check_Private_View (N);
4357
4358 elsif Nkind (A) = N_Defining_Identifier then
4359 Rewrite (N, New_Occurrence_Of (A, Loc));
4360 Check_Private_View (N);
4361
4362 -- Numeric literal
4363
4364 else
4365 Rewrite (N, New_Copy (A));
4366 end if;
4367 end if;
4368
4369 return Skip;
4370
4371 elsif Is_Entity_Name (N)
4372 and then Present (Return_Object)
4373 and then Chars (N) = Chars (Return_Object)
4374 then
4375 -- Occurrence within an extended return statement. The return
4376 -- object is local to the body been inlined, and thus the generic
4377 -- copy is not analyzed yet, so we match by name, and replace it
4378 -- with target of call.
4379
4380 if Nkind (Targ) = N_Defining_Identifier then
4381 Rewrite (N, New_Occurrence_Of (Targ, Loc));
4382 else
4383 Rewrite (N, New_Copy_Tree (Targ));
4384 end if;
4385
4386 return Skip;
4387
4388 elsif Nkind (N) = N_Simple_Return_Statement then
4389 if No (Expression (N)) then
4390 Make_Exit_Label;
4391 Rewrite (N,
4392 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4393
4394 else
4395 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
4396 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
4397 then
4398 -- Function body is a single expression. No need for
4399 -- exit label.
4400
4401 null;
4402
4403 else
4404 Num_Ret := Num_Ret + 1;
4405 Make_Exit_Label;
4406 end if;
4407
4408 -- Because of the presence of private types, the views of the
4409 -- expression and the context may be different, so place an
4410 -- unchecked conversion to the context type to avoid spurious
4411 -- errors, e.g. when the expression is a numeric literal and
4412 -- the context is private. If the expression is an aggregate,
4413 -- use a qualified expression, because an aggregate is not a
4414 -- legal argument of a conversion. Ditto for numeric literals,
4415 -- which must be resolved to a specific type.
4416
4417 if Nkind_In (Expression (N), N_Aggregate,
4418 N_Null,
4419 N_Real_Literal,
4420 N_Integer_Literal)
4421 then
4422 Ret :=
4423 Make_Qualified_Expression (Sloc (N),
4424 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
4425 Expression => Relocate_Node (Expression (N)));
4426 else
4427 Ret :=
4428 Unchecked_Convert_To
4429 (Ret_Type, Relocate_Node (Expression (N)));
4430 end if;
4431
4432 if Nkind (Targ) = N_Defining_Identifier then
4433 Rewrite (N,
4434 Make_Assignment_Statement (Loc,
4435 Name => New_Occurrence_Of (Targ, Loc),
4436 Expression => Ret));
4437 else
4438 Rewrite (N,
4439 Make_Assignment_Statement (Loc,
4440 Name => New_Copy (Targ),
4441 Expression => Ret));
4442 end if;
4443
4444 Set_Assignment_OK (Name (N));
4445
4446 if Present (Exit_Lab) then
4447 Insert_After (N,
4448 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4449 end if;
4450 end if;
4451
4452 return OK;
4453
4454 -- An extended return becomes a block whose first statement is the
4455 -- assignment of the initial expression of the return object to the
4456 -- target of the call itself.
4457
4458 elsif Nkind (N) = N_Extended_Return_Statement then
4459 declare
4460 Return_Decl : constant Entity_Id :=
4461 First (Return_Object_Declarations (N));
4462 Assign : Node_Id;
4463
4464 begin
4465 Return_Object := Defining_Identifier (Return_Decl);
4466
4467 if Present (Expression (Return_Decl)) then
4468 if Nkind (Targ) = N_Defining_Identifier then
4469 Assign :=
4470 Make_Assignment_Statement (Loc,
4471 Name => New_Occurrence_Of (Targ, Loc),
4472 Expression => Expression (Return_Decl));
4473 else
4474 Assign :=
4475 Make_Assignment_Statement (Loc,
4476 Name => New_Copy (Targ),
4477 Expression => Expression (Return_Decl));
4478 end if;
4479
4480 Set_Assignment_OK (Name (Assign));
4481
4482 if No (Handled_Statement_Sequence (N)) then
4483 Set_Handled_Statement_Sequence (N,
4484 Make_Handled_Sequence_Of_Statements (Loc,
4485 Statements => New_List));
4486 end if;
4487
4488 Prepend (Assign,
4489 Statements (Handled_Statement_Sequence (N)));
4490 end if;
4491
4492 Rewrite (N,
4493 Make_Block_Statement (Loc,
4494 Handled_Statement_Sequence =>
4495 Handled_Statement_Sequence (N)));
4496
4497 return OK;
4498 end;
4499
4500 -- Remove pragma Unreferenced since it may refer to formals that
4501 -- are not visible in the inlined body, and in any case we will
4502 -- not be posting warnings on the inlined body so it is unneeded.
4503
4504 elsif Nkind (N) = N_Pragma
4505 and then Pragma_Name (N) = Name_Unreferenced
4506 then
4507 Rewrite (N, Make_Null_Statement (Sloc (N)));
4508 return OK;
4509
4510 else
4511 return OK;
4512 end if;
4513 end Process_Formals;
4514
4515 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
4516
4517 ------------------
4518 -- Process_Sloc --
4519 ------------------
4520
4521 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
4522 begin
4523 if not Debug_Generated_Code then
4524 Set_Sloc (Nod, Sloc (N));
4525 Set_Comes_From_Source (Nod, False);
4526 end if;
4527
4528 return OK;
4529 end Process_Sloc;
4530
4531 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
4532
4533 ------------------------------
4534 -- Reset_Dispatching_Calls --
4535 ------------------------------
4536
4537 procedure Reset_Dispatching_Calls (N : Node_Id) is
4538
4539 function Do_Reset (N : Node_Id) return Traverse_Result;
4540 -- Comment required ???
4541
4542 --------------
4543 -- Do_Reset --
4544 --------------
4545
4546 function Do_Reset (N : Node_Id) return Traverse_Result is
4547 begin
4548 if Nkind (N) = N_Procedure_Call_Statement
4549 and then Nkind (Name (N)) = N_Selected_Component
4550 and then Nkind (Prefix (Name (N))) = N_Identifier
4551 and then Is_Formal (Entity (Prefix (Name (N))))
4552 and then Is_Dispatching_Operation
4553 (Entity (Selector_Name (Name (N))))
4554 then
4555 Set_Entity (Selector_Name (Name (N)), Empty);
4556 end if;
4557
4558 return OK;
4559 end Do_Reset;
4560
4561 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
4562
4563 -- Local variables
4564
4565 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
4566 pragma Unreferenced (Dummy);
4567
4568 -- Start of processing for Reset_Dispatching_Calls
4569
4570 begin
4571 null;
4572 end Reset_Dispatching_Calls;
4573
4574 ---------------------------
4575 -- Rewrite_Function_Call --
4576 ---------------------------
4577
4578 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
4579 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4580 Fst : constant Node_Id := First (Statements (HSS));
4581
4582 begin
4583 -- Optimize simple case: function body is a single return statement,
4584 -- which has been expanded into an assignment.
4585
4586 if Is_Empty_List (Declarations (Blk))
4587 and then Nkind (Fst) = N_Assignment_Statement
4588 and then No (Next (Fst))
4589 then
4590 -- The function call may have been rewritten as the temporary
4591 -- that holds the result of the call, in which case remove the
4592 -- now useless declaration.
4593
4594 if Nkind (N) = N_Identifier
4595 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4596 then
4597 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
4598 end if;
4599
4600 Rewrite (N, Expression (Fst));
4601
4602 elsif Nkind (N) = N_Identifier
4603 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4604 then
4605 -- The block assigns the result of the call to the temporary
4606
4607 Insert_After (Parent (Entity (N)), Blk);
4608
4609 -- If the context is an assignment, and the left-hand side is free of
4610 -- side-effects, the replacement is also safe.
4611 -- Can this be generalized further???
4612
4613 elsif Nkind (Parent (N)) = N_Assignment_Statement
4614 and then
4615 (Is_Entity_Name (Name (Parent (N)))
4616 or else
4617 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
4618 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
4619
4620 or else
4621 (Nkind (Name (Parent (N))) = N_Selected_Component
4622 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
4623 then
4624 -- Replace assignment with the block
4625
4626 declare
4627 Original_Assignment : constant Node_Id := Parent (N);
4628
4629 begin
4630 -- Preserve the original assignment node to keep the complete
4631 -- assignment subtree consistent enough for Analyze_Assignment
4632 -- to proceed (specifically, the original Lhs node must still
4633 -- have an assignment statement as its parent).
4634
4635 -- We cannot rely on Original_Node to go back from the block
4636 -- node to the assignment node, because the assignment might
4637 -- already be a rewrite substitution.
4638
4639 Discard_Node (Relocate_Node (Original_Assignment));
4640 Rewrite (Original_Assignment, Blk);
4641 end;
4642
4643 elsif Nkind (Parent (N)) = N_Object_Declaration then
4644
4645 -- A call to a function which returns an unconstrained type
4646 -- found in the expression initializing an object-declaration is
4647 -- expanded into a procedure call which must be added after the
4648 -- object declaration.
4649
4650 if Is_Unc_Decl and then Debug_Flag_Dot_K then
4651 Insert_Action_After (Parent (N), Blk);
4652 else
4653 Set_Expression (Parent (N), Empty);
4654 Insert_After (Parent (N), Blk);
4655 end if;
4656
4657 elsif Is_Unc and then not Debug_Flag_Dot_K then
4658 Insert_Before (Parent (N), Blk);
4659 end if;
4660 end Rewrite_Function_Call;
4661
4662 ----------------------------
4663 -- Rewrite_Procedure_Call --
4664 ----------------------------
4665
4666 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
4667 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4668
4669 begin
4670 -- If there is a transient scope for N, this will be the scope of the
4671 -- actions for N, and the statements in Blk need to be within this
4672 -- scope. For example, they need to have visibility on the constant
4673 -- declarations created for the formals.
4674
4675 -- If N needs no transient scope, and if there are no declarations in
4676 -- the inlined body, we can do a little optimization and insert the
4677 -- statements for the body directly after N, and rewrite N to a
4678 -- null statement, instead of rewriting N into a full-blown block
4679 -- statement.
4680
4681 if not Scope_Is_Transient
4682 and then Is_Empty_List (Declarations (Blk))
4683 then
4684 Insert_List_After (N, Statements (HSS));
4685 Rewrite (N, Make_Null_Statement (Loc));
4686 else
4687 Rewrite (N, Blk);
4688 end if;
4689 end Rewrite_Procedure_Call;
4690
4691 -------------------------
4692 -- Formal_Is_Used_Once --
4693 -------------------------
4694
4695 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
4696 Use_Counter : Int := 0;
4697
4698 function Count_Uses (N : Node_Id) return Traverse_Result;
4699 -- Traverse the tree and count the uses of the formal parameter.
4700 -- In this case, for optimization purposes, we do not need to
4701 -- continue the traversal once more than one use is encountered.
4702
4703 ----------------
4704 -- Count_Uses --
4705 ----------------
4706
4707 function Count_Uses (N : Node_Id) return Traverse_Result is
4708 begin
4709 -- The original node is an identifier
4710
4711 if Nkind (N) = N_Identifier
4712 and then Present (Entity (N))
4713
4714 -- Original node's entity points to the one in the copied body
4715
4716 and then Nkind (Entity (N)) = N_Identifier
4717 and then Present (Entity (Entity (N)))
4718
4719 -- The entity of the copied node is the formal parameter
4720
4721 and then Entity (Entity (N)) = Formal
4722 then
4723 Use_Counter := Use_Counter + 1;
4724
4725 if Use_Counter > 1 then
4726
4727 -- Denote more than one use and abandon the traversal
4728
4729 Use_Counter := 2;
4730 return Abandon;
4731
4732 end if;
4733 end if;
4734
4735 return OK;
4736 end Count_Uses;
4737
4738 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
4739
4740 -- Start of processing for Formal_Is_Used_Once
4741
4742 begin
4743 Count_Formal_Uses (Orig_Bod);
4744 return Use_Counter = 1;
4745 end Formal_Is_Used_Once;
4746
4747 -- Start of processing for Expand_Inlined_Call
4748
4749 begin
4750 -- Initializations for old/new semantics
4751
4752 if not Debug_Flag_Dot_K then
4753 Is_Unc := Is_Array_Type (Etype (Subp))
4754 and then not Is_Constrained (Etype (Subp));
4755 Is_Unc_Decl := False;
4756 else
4757 Is_Unc := Returns_Unconstrained_Type (Subp)
4758 and then Optimization_Level > 0;
4759 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
4760 and then Is_Unc;
4761 end if;
4762
4763 -- Check for an illegal attempt to inline a recursive procedure. If the
4764 -- subprogram has parameters this is detected when trying to supply a
4765 -- binding for parameters that already have one. For parameterless
4766 -- subprograms this must be done explicitly.
4767
4768 if In_Open_Scopes (Subp) then
4769 Error_Msg_N ("call to recursive subprogram cannot be inlined??", N);
4770 Set_Is_Inlined (Subp, False);
4771 return;
4772
4773 -- Skip inlining if this is not a true inlining since the attribute
4774 -- Body_To_Inline is also set for renamings (see sinfo.ads)
4775
4776 elsif Nkind (Orig_Bod) in N_Entity then
4777 return;
4778
4779 -- Skip inlining if the function returns an unconstrained type using
4780 -- an extended return statement since this part of the new inlining
4781 -- model which is not yet supported by the current implementation. ???
4782
4783 elsif Is_Unc
4784 and then
4785 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod))))
4786 = N_Extended_Return_Statement
4787 and then not Debug_Flag_Dot_K
4788 then
4789 return;
4790 end if;
4791
4792 if Nkind (Orig_Bod) = N_Defining_Identifier
4793 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
4794 then
4795 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4796 -- can be replaced with calls to the renamed entity directly, because
4797 -- the subprograms are subtype conformant. If the renamed subprogram
4798 -- is an inherited operation, we must redo the expansion because
4799 -- implicit conversions may be needed. Similarly, if the renamed
4800 -- entity is inlined, expand the call for further optimizations.
4801
4802 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
4803
4804 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
4805 Expand_Call (N);
4806 end if;
4807
4808 return;
4809 end if;
4810
4811 -- Register the call in the list of inlined calls
4812
4813 if Inlined_Calls = No_Elist then
4814 Inlined_Calls := New_Elmt_List;
4815 end if;
4816
4817 Append_Elmt (N, To => Inlined_Calls);
4818
4819 -- Use generic machinery to copy body of inlined subprogram, as if it
4820 -- were an instantiation, resetting source locations appropriately, so
4821 -- that nested inlined calls appear in the main unit.
4822
4823 Save_Env (Subp, Empty);
4824 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
4825
4826 -- Old semantics
4827
4828 if not Debug_Flag_Dot_K then
4829 declare
4830 Bod : Node_Id;
4831
4832 begin
4833 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4834 Blk :=
4835 Make_Block_Statement (Loc,
4836 Declarations => Declarations (Bod),
4837 Handled_Statement_Sequence =>
4838 Handled_Statement_Sequence (Bod));
4839
4840 if No (Declarations (Bod)) then
4841 Set_Declarations (Blk, New_List);
4842 end if;
4843
4844 -- For the unconstrained case, capture the name of the local
4845 -- variable that holds the result. This must be the first
4846 -- declaration in the block, because its bounds cannot depend
4847 -- on local variables. Otherwise there is no way to declare the
4848 -- result outside of the block. Needless to say, in general the
4849 -- bounds will depend on the actuals in the call.
4850
4851 -- If the context is an assignment statement, as is the case
4852 -- for the expansion of an extended return, the left-hand side
4853 -- provides bounds even if the return type is unconstrained.
4854
4855 if Is_Unc then
4856 declare
4857 First_Decl : Node_Id;
4858
4859 begin
4860 First_Decl := First (Declarations (Blk));
4861
4862 if Nkind (First_Decl) /= N_Object_Declaration then
4863 return;
4864 end if;
4865
4866 if Nkind (Parent (N)) /= N_Assignment_Statement then
4867 Targ1 := Defining_Identifier (First_Decl);
4868 else
4869 Targ1 := Name (Parent (N));
4870 end if;
4871 end;
4872 end if;
4873 end;
4874
4875 -- New semantics
4876
4877 else
4878 declare
4879 Bod : Node_Id;
4880
4881 begin
4882 -- General case
4883
4884 if not Is_Unc then
4885 Bod :=
4886 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4887 Blk :=
4888 Make_Block_Statement (Loc,
4889 Declarations => Declarations (Bod),
4890 Handled_Statement_Sequence =>
4891 Handled_Statement_Sequence (Bod));
4892
4893 -- Inline a call to a function that returns an unconstrained type.
4894 -- The semantic analyzer checked that frontend-inlined functions
4895 -- returning unconstrained types have no declarations and have
4896 -- a single extended return statement. As part of its processing
4897 -- the function was split in two subprograms: a procedure P and
4898 -- a function F that has a block with a call to procedure P (see
4899 -- Split_Unconstrained_Function).
4900
4901 else
4902 pragma Assert
4903 (Nkind
4904 (First
4905 (Statements (Handled_Statement_Sequence (Orig_Bod))))
4906 = N_Block_Statement);
4907
4908 declare
4909 Blk_Stmt : constant Node_Id :=
4910 First
4911 (Statements
4912 (Handled_Statement_Sequence (Orig_Bod)));
4913 First_Stmt : constant Node_Id :=
4914 First
4915 (Statements
4916 (Handled_Statement_Sequence (Blk_Stmt)));
4917 Second_Stmt : constant Node_Id := Next (First_Stmt);
4918
4919 begin
4920 pragma Assert
4921 (Nkind (First_Stmt) = N_Procedure_Call_Statement
4922 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
4923 and then No (Next (Second_Stmt)));
4924
4925 Bod :=
4926 Copy_Generic_Node
4927 (First
4928 (Statements (Handled_Statement_Sequence (Orig_Bod))),
4929 Empty, Instantiating => True);
4930 Blk := Bod;
4931
4932 -- Capture the name of the local variable that holds the
4933 -- result. This must be the first declaration in the block,
4934 -- because its bounds cannot depend on local variables.
4935 -- Otherwise there is no way to declare the result outside
4936 -- of the block. Needless to say, in general the bounds will
4937 -- depend on the actuals in the call.
4938
4939 if Nkind (Parent (N)) /= N_Assignment_Statement then
4940 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4941
4942 -- If the context is an assignment statement, as is the case
4943 -- for the expansion of an extended return, the left-hand
4944 -- side provides bounds even if the return type is
4945 -- unconstrained.
4946
4947 else
4948 Targ1 := Name (Parent (N));
4949 end if;
4950 end;
4951 end if;
4952
4953 if No (Declarations (Bod)) then
4954 Set_Declarations (Blk, New_List);
4955 end if;
4956 end;
4957 end if;
4958
4959 -- If this is a derived function, establish the proper return type
4960
4961 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4962 Ret_Type := Etype (Orig_Subp);
4963 else
4964 Ret_Type := Etype (Subp);
4965 end if;
4966
4967 -- Create temporaries for the actuals that are expressions, or that are
4968 -- scalars and require copying to preserve semantics.
4969
4970 F := First_Formal (Subp);
4971 A := First_Actual (N);
4972 while Present (F) loop
4973 if Present (Renamed_Object (F)) then
4974 Error_Msg_N ("cannot inline call to recursive subprogram", N);
4975 return;
4976 end if;
4977
4978 -- Reset Last_Assignment for any parameters of mode out or in out, to
4979 -- prevent spurious warnings about overwriting for assignments to the
4980 -- formal in the inlined code.
4981
4982 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
4983 Set_Last_Assignment (Entity (A), Empty);
4984 end if;
4985
4986 -- If the argument may be a controlling argument in a call within
4987 -- the inlined body, we must preserve its classwide nature to insure
4988 -- that dynamic dispatching take place subsequently. If the formal
4989 -- has a constraint it must be preserved to retain the semantics of
4990 -- the body.
4991
4992 if Is_Class_Wide_Type (Etype (F))
4993 or else (Is_Access_Type (Etype (F))
4994 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
4995 then
4996 Temp_Typ := Etype (F);
4997
4998 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
4999 and then Etype (F) /= Base_Type (Etype (F))
5000 then
5001 Temp_Typ := Etype (F);
5002 else
5003 Temp_Typ := Etype (A);
5004 end if;
5005
5006 -- If the actual is a simple name or a literal, no need to
5007 -- create a temporary, object can be used directly.
5008
5009 -- If the actual is a literal and the formal has its address taken,
5010 -- we cannot pass the literal itself as an argument, so its value
5011 -- must be captured in a temporary.
5012
5013 if (Is_Entity_Name (A)
5014 and then
5015 (not Is_Scalar_Type (Etype (A))
5016 or else Ekind (Entity (A)) = E_Enumeration_Literal))
5017
5018 -- When the actual is an identifier and the corresponding formal is
5019 -- used only once in the original body, the formal can be substituted
5020 -- directly with the actual parameter.
5021
5022 or else (Nkind (A) = N_Identifier
5023 and then Formal_Is_Used_Once (F))
5024
5025 or else
5026 (Nkind_In (A, N_Real_Literal,
5027 N_Integer_Literal,
5028 N_Character_Literal)
5029 and then not Address_Taken (F))
5030 then
5031 if Etype (F) /= Etype (A) then
5032 Set_Renamed_Object
5033 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
5034 else
5035 Set_Renamed_Object (F, A);
5036 end if;
5037
5038 else
5039 Temp := Make_Temporary (Loc, 'C');
5040
5041 -- If the actual for an in/in-out parameter is a view conversion,
5042 -- make it into an unchecked conversion, given that an untagged
5043 -- type conversion is not a proper object for a renaming.
5044
5045 -- In-out conversions that involve real conversions have already
5046 -- been transformed in Expand_Actuals.
5047
5048 if Nkind (A) = N_Type_Conversion
5049 and then Ekind (F) /= E_In_Parameter
5050 then
5051 New_A :=
5052 Make_Unchecked_Type_Conversion (Loc,
5053 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
5054 Expression => Relocate_Node (Expression (A)));
5055
5056 elsif Etype (F) /= Etype (A) then
5057 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
5058 Temp_Typ := Etype (F);
5059
5060 else
5061 New_A := Relocate_Node (A);
5062 end if;
5063
5064 Set_Sloc (New_A, Sloc (N));
5065
5066 -- If the actual has a by-reference type, it cannot be copied,
5067 -- so its value is captured in a renaming declaration. Otherwise
5068 -- declare a local constant initialized with the actual.
5069
5070 -- We also use a renaming declaration for expressions of an array
5071 -- type that is not bit-packed, both for efficiency reasons and to
5072 -- respect the semantics of the call: in most cases the original
5073 -- call will pass the parameter by reference, and thus the inlined
5074 -- code will have the same semantics.
5075
5076 if Ekind (F) = E_In_Parameter
5077 and then not Is_By_Reference_Type (Etype (A))
5078 and then
5079 (not Is_Array_Type (Etype (A))
5080 or else not Is_Object_Reference (A)
5081 or else Is_Bit_Packed_Array (Etype (A)))
5082 then
5083 Decl :=
5084 Make_Object_Declaration (Loc,
5085 Defining_Identifier => Temp,
5086 Constant_Present => True,
5087 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
5088 Expression => New_A);
5089 else
5090 Decl :=
5091 Make_Object_Renaming_Declaration (Loc,
5092 Defining_Identifier => Temp,
5093 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
5094 Name => New_A);
5095 end if;
5096
5097 Append (Decl, Decls);
5098 Set_Renamed_Object (F, Temp);
5099 end if;
5100
5101 Next_Formal (F);
5102 Next_Actual (A);
5103 end loop;
5104
5105 -- Establish target of function call. If context is not assignment or
5106 -- declaration, create a temporary as a target. The declaration for the
5107 -- temporary may be subsequently optimized away if the body is a single
5108 -- expression, or if the left-hand side of the assignment is simple
5109 -- enough, i.e. an entity or an explicit dereference of one.
5110
5111 if Ekind (Subp) = E_Function then
5112 if Nkind (Parent (N)) = N_Assignment_Statement
5113 and then Is_Entity_Name (Name (Parent (N)))
5114 then
5115 Targ := Name (Parent (N));
5116
5117 elsif Nkind (Parent (N)) = N_Assignment_Statement
5118 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5119 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5120 then
5121 Targ := Name (Parent (N));
5122
5123 elsif Nkind (Parent (N)) = N_Assignment_Statement
5124 and then Nkind (Name (Parent (N))) = N_Selected_Component
5125 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5126 then
5127 Targ := New_Copy_Tree (Name (Parent (N)));
5128
5129 elsif Nkind (Parent (N)) = N_Object_Declaration
5130 and then Is_Limited_Type (Etype (Subp))
5131 then
5132 Targ := Defining_Identifier (Parent (N));
5133
5134 -- New semantics: In an object declaration avoid an extra copy
5135 -- of the result of a call to an inlined function that returns
5136 -- an unconstrained type
5137
5138 elsif Debug_Flag_Dot_K
5139 and then Nkind (Parent (N)) = N_Object_Declaration
5140 and then Is_Unc
5141 then
5142 Targ := Defining_Identifier (Parent (N));
5143
5144 else
5145 -- Replace call with temporary and create its declaration
5146
5147 Temp := Make_Temporary (Loc, 'C');
5148 Set_Is_Internal (Temp);
5149
5150 -- For the unconstrained case, the generated temporary has the
5151 -- same constrained declaration as the result variable. It may
5152 -- eventually be possible to remove that temporary and use the
5153 -- result variable directly.
5154
5155 if Is_Unc
5156 and then Nkind (Parent (N)) /= N_Assignment_Statement
5157 then
5158 Decl :=
5159 Make_Object_Declaration (Loc,
5160 Defining_Identifier => Temp,
5161 Object_Definition =>
5162 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5163
5164 Replace_Formals (Decl);
5165
5166 else
5167 Decl :=
5168 Make_Object_Declaration (Loc,
5169 Defining_Identifier => Temp,
5170 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
5171
5172 Set_Etype (Temp, Ret_Type);
5173 end if;
5174
5175 Set_No_Initialization (Decl);
5176 Append (Decl, Decls);
5177 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5178 Targ := Temp;
5179 end if;
5180 end if;
5181
5182 Insert_Actions (N, Decls);
5183
5184 if Is_Unc_Decl then
5185
5186 -- Special management for inlining a call to a function that returns
5187 -- an unconstrained type and initializes an object declaration: we
5188 -- avoid generating undesired extra calls and goto statements.
5189
5190 -- Given:
5191 -- function Func (...) return ...
5192 -- begin
5193 -- declare
5194 -- Result : String (1 .. 4);
5195 -- begin
5196 -- Proc (Result, ...);
5197 -- return Result;
5198 -- end;
5199 -- end F;
5200
5201 -- Result : String := Func (...);
5202
5203 -- Replace this object declaration by:
5204
5205 -- Result : String (1 .. 4);
5206 -- Proc (Result, ...);
5207
5208 Remove_Homonym (Targ);
5209
5210 Decl :=
5211 Make_Object_Declaration
5212 (Loc,
5213 Defining_Identifier => Targ,
5214 Object_Definition =>
5215 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5216 Replace_Formals (Decl);
5217 Rewrite (Parent (N), Decl);
5218 Analyze (Parent (N));
5219
5220 -- Avoid spurious warnings since we know that this declaration is
5221 -- referenced by the procedure call.
5222
5223 Set_Never_Set_In_Source (Targ, False);
5224
5225 -- Remove the local declaration of the extended return stmt from the
5226 -- inlined code
5227
5228 Remove (Parent (Targ1));
5229
5230 -- Update the reference to the result (since we have rewriten the
5231 -- object declaration)
5232
5233 declare
5234 Blk_Call_Stmt : Node_Id;
5235
5236 begin
5237 -- Capture the call to the procedure
5238
5239 Blk_Call_Stmt :=
5240 First (Statements (Handled_Statement_Sequence (Blk)));
5241 pragma Assert
5242 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
5243
5244 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
5245 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
5246 New_Occurrence_Of (Targ, Loc));
5247 end;
5248
5249 -- Remove the return statement
5250
5251 pragma Assert
5252 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5253 N_Simple_Return_Statement);
5254
5255 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5256 end if;
5257
5258 -- Traverse the tree and replace formals with actuals or their thunks.
5259 -- Attach block to tree before analysis and rewriting.
5260
5261 Replace_Formals (Blk);
5262 Set_Parent (Blk, N);
5263
5264 if not Comes_From_Source (Subp) or else Is_Predef then
5265 Reset_Slocs (Blk);
5266 end if;
5267
5268 if Is_Unc_Decl then
5269
5270 -- No action needed since return statement has been already removed
5271
5272 null;
5273
5274 elsif Present (Exit_Lab) then
5275
5276 -- If the body was a single expression, the single return statement
5277 -- and the corresponding label are useless.
5278
5279 if Num_Ret = 1
5280 and then
5281 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5282 N_Goto_Statement
5283 then
5284 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5285 else
5286 Append (Lab_Decl, (Declarations (Blk)));
5287 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
5288 end if;
5289 end if;
5290
5291 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5292 -- on conflicting private views that Gigi would ignore. If this is a
5293 -- predefined unit, analyze with checks off, as is done in the non-
5294 -- inlined run-time units.
5295
5296 declare
5297 I_Flag : constant Boolean := In_Inlined_Body;
5298
5299 begin
5300 In_Inlined_Body := True;
5301
5302 if Is_Predef then
5303 declare
5304 Style : constant Boolean := Style_Check;
5305
5306 begin
5307 Style_Check := False;
5308
5309 -- Search for dispatching calls that use the Object.Operation
5310 -- notation using an Object that is a parameter of the inlined
5311 -- function. We reset the decoration of Operation to force
5312 -- the reanalysis of the inlined dispatching call because
5313 -- the actual object has been inlined.
5314
5315 Reset_Dispatching_Calls (Blk);
5316
5317 Analyze (Blk, Suppress => All_Checks);
5318 Style_Check := Style;
5319 end;
5320
5321 else
5322 Analyze (Blk);
5323 end if;
5324
5325 In_Inlined_Body := I_Flag;
5326 end;
5327
5328 if Ekind (Subp) = E_Procedure then
5329 Rewrite_Procedure_Call (N, Blk);
5330
5331 else
5332 Rewrite_Function_Call (N, Blk);
5333
5334 if Is_Unc_Decl then
5335 null;
5336
5337 -- For the unconstrained case, the replacement of the call has been
5338 -- made prior to the complete analysis of the generated declarations.
5339 -- Propagate the proper type now.
5340
5341 elsif Is_Unc then
5342 if Nkind (N) = N_Identifier then
5343 Set_Etype (N, Etype (Entity (N)));
5344 else
5345 Set_Etype (N, Etype (Targ1));
5346 end if;
5347 end if;
5348 end if;
5349
5350 Restore_Env;
5351
5352 -- Cleanup mapping between formals and actuals for other expansions
5353
5354 F := First_Formal (Subp);
5355 while Present (F) loop
5356 Set_Renamed_Object (F, Empty);
5357 Next_Formal (F);
5358 end loop;
5359 end Expand_Inlined_Call;
5360
5361 ----------------------------------------
5362 -- Expand_N_Extended_Return_Statement --
5363 ----------------------------------------
5364
5365 -- If there is a Handled_Statement_Sequence, we rewrite this:
5366
5367 -- return Result : T := <expression> do
5368 -- <handled_seq_of_stms>
5369 -- end return;
5370
5371 -- to be:
5372
5373 -- declare
5374 -- Result : T := <expression>;
5375 -- begin
5376 -- <handled_seq_of_stms>
5377 -- return Result;
5378 -- end;
5379
5380 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5381
5382 -- return Result : T := <expression>;
5383
5384 -- to be:
5385
5386 -- return <expression>;
5387
5388 -- unless it's build-in-place or there's no <expression>, in which case
5389 -- we generate:
5390
5391 -- declare
5392 -- Result : T := <expression>;
5393 -- begin
5394 -- return Result;
5395 -- end;
5396
5397 -- Note that this case could have been written by the user as an extended
5398 -- return statement, or could have been transformed to this from a simple
5399 -- return statement.
5400
5401 -- That is, we need to have a reified return object if there are statements
5402 -- (which might refer to it) or if we're doing build-in-place (so we can
5403 -- set its address to the final resting place or if there is no expression
5404 -- (in which case default initial values might need to be set).
5405
5406 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5407 Loc : constant Source_Ptr := Sloc (N);
5408
5409 Par_Func : constant Entity_Id :=
5410 Return_Applies_To (Return_Statement_Entity (N));
5411 Result_Subt : constant Entity_Id := Etype (Par_Func);
5412 Ret_Obj_Id : constant Entity_Id :=
5413 First_Entity (Return_Statement_Entity (N));
5414 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5415
5416 Is_Build_In_Place : constant Boolean :=
5417 Is_Build_In_Place_Function (Par_Func);
5418
5419 Exp : Node_Id;
5420 HSS : Node_Id;
5421 Result : Node_Id;
5422 Return_Stmt : Node_Id;
5423 Stmts : List_Id;
5424
5425 function Build_Heap_Allocator
5426 (Temp_Id : Entity_Id;
5427 Temp_Typ : Entity_Id;
5428 Func_Id : Entity_Id;
5429 Ret_Typ : Entity_Id;
5430 Alloc_Expr : Node_Id) return Node_Id;
5431 -- Create the statements necessary to allocate a return object on the
5432 -- caller's master. The master is available through implicit parameter
5433 -- BIPfinalizationmaster.
5434 --
5435 -- if BIPfinalizationmaster /= null then
5436 -- declare
5437 -- type Ptr_Typ is access Ret_Typ;
5438 -- for Ptr_Typ'Storage_Pool use
5439 -- Base_Pool (BIPfinalizationmaster.all).all;
5440 -- Local : Ptr_Typ;
5441 --
5442 -- begin
5443 -- procedure Allocate (...) is
5444 -- begin
5445 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5446 -- end Allocate;
5447 --
5448 -- Local := <Alloc_Expr>;
5449 -- Temp_Id := Temp_Typ (Local);
5450 -- end;
5451 -- end if;
5452 --
5453 -- Temp_Id is the temporary which is used to reference the internally
5454 -- created object in all allocation forms. Temp_Typ is the type of the
5455 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5456 -- type of Func_Id. Alloc_Expr is the actual allocator.
5457
5458 function Move_Activation_Chain return Node_Id;
5459 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5460 -- with parameters:
5461 -- From current activation chain
5462 -- To activation chain passed in by the caller
5463 -- New_Master master passed in by the caller
5464
5465 --------------------------
5466 -- Build_Heap_Allocator --
5467 --------------------------
5468
5469 function Build_Heap_Allocator
5470 (Temp_Id : Entity_Id;
5471 Temp_Typ : Entity_Id;
5472 Func_Id : Entity_Id;
5473 Ret_Typ : Entity_Id;
5474 Alloc_Expr : Node_Id) return Node_Id
5475 is
5476 begin
5477 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5478
5479 -- Processing for build-in-place object allocation. This is disabled
5480 -- on .NET/JVM because the targets do not support pools.
5481
5482 if VM_Target = No_VM
5483 and then Needs_Finalization (Ret_Typ)
5484 then
5485 declare
5486 Decls : constant List_Id := New_List;
5487 Fin_Mas_Id : constant Entity_Id :=
5488 Build_In_Place_Formal
5489 (Func_Id, BIP_Finalization_Master);
5490 Stmts : constant List_Id := New_List;
5491 Desig_Typ : Entity_Id;
5492 Local_Id : Entity_Id;
5493 Pool_Id : Entity_Id;
5494 Ptr_Typ : Entity_Id;
5495
5496 begin
5497 -- Generate:
5498 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5499
5500 Pool_Id := Make_Temporary (Loc, 'P');
5501
5502 Append_To (Decls,
5503 Make_Object_Renaming_Declaration (Loc,
5504 Defining_Identifier => Pool_Id,
5505 Subtype_Mark =>
5506 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
5507 Name =>
5508 Make_Explicit_Dereference (Loc,
5509 Prefix =>
5510 Make_Function_Call (Loc,
5511 Name =>
5512 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
5513 Parameter_Associations => New_List (
5514 Make_Explicit_Dereference (Loc,
5515 Prefix =>
5516 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
5517
5518 -- Create an access type which uses the storage pool of the
5519 -- caller's master. This additional type is necessary because
5520 -- the finalization master cannot be associated with the type
5521 -- of the temporary. Otherwise the secondary stack allocation
5522 -- will fail.
5523
5524 Desig_Typ := Ret_Typ;
5525
5526 -- Ensure that the build-in-place machinery uses a fat pointer
5527 -- when allocating an unconstrained array on the heap. In this
5528 -- case the result object type is a constrained array type even
5529 -- though the function type is unconstrained.
5530
5531 if Ekind (Desig_Typ) = E_Array_Subtype then
5532 Desig_Typ := Base_Type (Desig_Typ);
5533 end if;
5534
5535 -- Generate:
5536 -- type Ptr_Typ is access Desig_Typ;
5537
5538 Ptr_Typ := Make_Temporary (Loc, 'P');
5539
5540 Append_To (Decls,
5541 Make_Full_Type_Declaration (Loc,
5542 Defining_Identifier => Ptr_Typ,
5543 Type_Definition =>
5544 Make_Access_To_Object_Definition (Loc,
5545 Subtype_Indication =>
5546 New_Occurrence_Of (Desig_Typ, Loc))));
5547
5548 -- Perform minor decoration in order to set the master and the
5549 -- storage pool attributes.
5550
5551 Set_Ekind (Ptr_Typ, E_Access_Type);
5552 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5553 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5554
5555 -- Create the temporary, generate:
5556 -- Local_Id : Ptr_Typ;
5557
5558 Local_Id := Make_Temporary (Loc, 'T');
5559
5560 Append_To (Decls,
5561 Make_Object_Declaration (Loc,
5562 Defining_Identifier => Local_Id,
5563 Object_Definition =>
5564 New_Occurrence_Of (Ptr_Typ, Loc)));
5565
5566 -- Allocate the object, generate:
5567 -- Local_Id := <Alloc_Expr>;
5568
5569 Append_To (Stmts,
5570 Make_Assignment_Statement (Loc,
5571 Name => New_Occurrence_Of (Local_Id, Loc),
5572 Expression => Alloc_Expr));
5573
5574 -- Generate:
5575 -- Temp_Id := Temp_Typ (Local_Id);
5576
5577 Append_To (Stmts,
5578 Make_Assignment_Statement (Loc,
5579 Name => New_Occurrence_Of (Temp_Id, Loc),
5580 Expression =>
5581 Unchecked_Convert_To (Temp_Typ,
5582 New_Occurrence_Of (Local_Id, Loc))));
5583
5584 -- Wrap the allocation in a block. This is further conditioned
5585 -- by checking the caller finalization master at runtime. A
5586 -- null value indicates a non-existent master, most likely due
5587 -- to a Finalize_Storage_Only allocation.
5588
5589 -- Generate:
5590 -- if BIPfinalizationmaster /= null then
5591 -- declare
5592 -- <Decls>
5593 -- begin
5594 -- <Stmts>
5595 -- end;
5596 -- end if;
5597
5598 return
5599 Make_If_Statement (Loc,
5600 Condition =>
5601 Make_Op_Ne (Loc,
5602 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
5603 Right_Opnd => Make_Null (Loc)),
5604
5605 Then_Statements => New_List (
5606 Make_Block_Statement (Loc,
5607 Declarations => Decls,
5608 Handled_Statement_Sequence =>
5609 Make_Handled_Sequence_Of_Statements (Loc,
5610 Statements => Stmts))));
5611 end;
5612
5613 -- For all other cases, generate:
5614 -- Temp_Id := <Alloc_Expr>;
5615
5616 else
5617 return
5618 Make_Assignment_Statement (Loc,
5619 Name => New_Occurrence_Of (Temp_Id, Loc),
5620 Expression => Alloc_Expr);
5621 end if;
5622 end Build_Heap_Allocator;
5623
5624 ---------------------------
5625 -- Move_Activation_Chain --
5626 ---------------------------
5627
5628 function Move_Activation_Chain return Node_Id is
5629 begin
5630 return
5631 Make_Procedure_Call_Statement (Loc,
5632 Name =>
5633 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5634
5635 Parameter_Associations => New_List (
5636
5637 -- Source chain
5638
5639 Make_Attribute_Reference (Loc,
5640 Prefix => Make_Identifier (Loc, Name_uChain),
5641 Attribute_Name => Name_Unrestricted_Access),
5642
5643 -- Destination chain
5644
5645 New_Occurrence_Of
5646 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
5647
5648 -- New master
5649
5650 New_Occurrence_Of
5651 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
5652 end Move_Activation_Chain;
5653
5654 -- Start of processing for Expand_N_Extended_Return_Statement
5655
5656 begin
5657 -- Given that functionality of interface thunks is simple (just displace
5658 -- the pointer to the object) they are always handled by means of
5659 -- simple return statements.
5660
5661 pragma Assert (not Is_Thunk (Current_Scope));
5662
5663 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5664 Exp := Expression (Ret_Obj_Decl);
5665 else
5666 Exp := Empty;
5667 end if;
5668
5669 HSS := Handled_Statement_Sequence (N);
5670
5671 -- If the returned object needs finalization actions, the function must
5672 -- perform the appropriate cleanup should it fail to return. The state
5673 -- of the function itself is tracked through a flag which is coupled
5674 -- with the scope finalizer. There is one flag per each return object
5675 -- in case of multiple returns.
5676
5677 if Is_Build_In_Place
5678 and then Needs_Finalization (Etype (Ret_Obj_Id))
5679 then
5680 declare
5681 Flag_Decl : Node_Id;
5682 Flag_Id : Entity_Id;
5683 Func_Bod : Node_Id;
5684
5685 begin
5686 -- Recover the function body
5687
5688 Func_Bod := Unit_Declaration_Node (Par_Func);
5689
5690 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5691 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5692 end if;
5693
5694 -- Create a flag to track the function state
5695
5696 Flag_Id := Make_Temporary (Loc, 'F');
5697 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5698
5699 -- Insert the flag at the beginning of the function declarations,
5700 -- generate:
5701 -- Fnn : Boolean := False;
5702
5703 Flag_Decl :=
5704 Make_Object_Declaration (Loc,
5705 Defining_Identifier => Flag_Id,
5706 Object_Definition =>
5707 New_Occurrence_Of (Standard_Boolean, Loc),
5708 Expression =>
5709 New_Occurrence_Of (Standard_False, Loc));
5710
5711 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5712 Analyze (Flag_Decl);
5713 end;
5714 end if;
5715
5716 -- Build a simple_return_statement that returns the return object when
5717 -- there is a statement sequence, or no expression, or the result will
5718 -- be built in place. Note however that we currently do this for all
5719 -- composite cases, even though nonlimited composite results are not yet
5720 -- built in place (though we plan to do so eventually).
5721
5722 if Present (HSS)
5723 or else Is_Composite_Type (Result_Subt)
5724 or else No (Exp)
5725 then
5726 if No (HSS) then
5727 Stmts := New_List;
5728
5729 -- If the extended return has a handled statement sequence, then wrap
5730 -- it in a block and use the block as the first statement.
5731
5732 else
5733 Stmts := New_List (
5734 Make_Block_Statement (Loc,
5735 Declarations => New_List,
5736 Handled_Statement_Sequence => HSS));
5737 end if;
5738
5739 -- If the result type contains tasks, we call Move_Activation_Chain.
5740 -- Later, the cleanup code will call Complete_Master, which will
5741 -- terminate any unactivated tasks belonging to the return statement
5742 -- master. But Move_Activation_Chain updates their master to be that
5743 -- of the caller, so they will not be terminated unless the return
5744 -- statement completes unsuccessfully due to exception, abort, goto,
5745 -- or exit. As a formality, we test whether the function requires the
5746 -- result to be built in place, though that's necessarily true for
5747 -- the case of result types with task parts.
5748
5749 if Is_Build_In_Place
5750 and then Has_Task (Result_Subt)
5751 then
5752 -- The return expression is an aggregate for a complex type which
5753 -- contains tasks. This particular case is left unexpanded since
5754 -- the regular expansion would insert all temporaries and
5755 -- initialization code in the wrong block.
5756
5757 if Nkind (Exp) = N_Aggregate then
5758 Expand_N_Aggregate (Exp);
5759 end if;
5760
5761 -- Do not move the activation chain if the return object does not
5762 -- contain tasks.
5763
5764 if Has_Task (Etype (Ret_Obj_Id)) then
5765 Append_To (Stmts, Move_Activation_Chain);
5766 end if;
5767 end if;
5768
5769 -- Update the state of the function right before the object is
5770 -- returned.
5771
5772 if Is_Build_In_Place
5773 and then Needs_Finalization (Etype (Ret_Obj_Id))
5774 then
5775 declare
5776 Flag_Id : constant Entity_Id :=
5777 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5778
5779 begin
5780 -- Generate:
5781 -- Fnn := True;
5782
5783 Append_To (Stmts,
5784 Make_Assignment_Statement (Loc,
5785 Name => New_Occurrence_Of (Flag_Id, Loc),
5786 Expression => New_Occurrence_Of (Standard_True, Loc)));
5787 end;
5788 end if;
5789
5790 -- Build a simple_return_statement that returns the return object
5791
5792 Return_Stmt :=
5793 Make_Simple_Return_Statement (Loc,
5794 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5795 Append_To (Stmts, Return_Stmt);
5796
5797 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5798 end if;
5799
5800 -- Case where we build a return statement block
5801
5802 if Present (HSS) then
5803 Result :=
5804 Make_Block_Statement (Loc,
5805 Declarations => Return_Object_Declarations (N),
5806 Handled_Statement_Sequence => HSS);
5807
5808 -- We set the entity of the new block statement to be that of the
5809 -- return statement. This is necessary so that various fields, such
5810 -- as Finalization_Chain_Entity carry over from the return statement
5811 -- to the block. Note that this block is unusual, in that its entity
5812 -- is an E_Return_Statement rather than an E_Block.
5813
5814 Set_Identifier
5815 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5816
5817 -- If the object decl was already rewritten as a renaming, then we
5818 -- don't want to do the object allocation and transformation of of
5819 -- the return object declaration to a renaming. This case occurs
5820 -- when the return object is initialized by a call to another
5821 -- build-in-place function, and that function is responsible for
5822 -- the allocation of the return object.
5823
5824 if Is_Build_In_Place
5825 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5826 then
5827 pragma Assert
5828 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5829 and then Is_Build_In_Place_Function_Call
5830 (Expression (Original_Node (Ret_Obj_Decl))));
5831
5832 -- Return the build-in-place result by reference
5833
5834 Set_By_Ref (Return_Stmt);
5835
5836 elsif Is_Build_In_Place then
5837
5838 -- Locate the implicit access parameter associated with the
5839 -- caller-supplied return object and convert the return
5840 -- statement's return object declaration to a renaming of a
5841 -- dereference of the access parameter. If the return object's
5842 -- declaration includes an expression that has not already been
5843 -- expanded as separate assignments, then add an assignment
5844 -- statement to ensure the return object gets initialized.
5845
5846 -- declare
5847 -- Result : T [:= <expression>];
5848 -- begin
5849 -- ...
5850
5851 -- is converted to
5852
5853 -- declare
5854 -- Result : T renames FuncRA.all;
5855 -- [Result := <expression;]
5856 -- begin
5857 -- ...
5858
5859 declare
5860 Return_Obj_Id : constant Entity_Id :=
5861 Defining_Identifier (Ret_Obj_Decl);
5862 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
5863 Return_Obj_Expr : constant Node_Id :=
5864 Expression (Ret_Obj_Decl);
5865 Constr_Result : constant Boolean :=
5866 Is_Constrained (Result_Subt);
5867 Obj_Alloc_Formal : Entity_Id;
5868 Object_Access : Entity_Id;
5869 Obj_Acc_Deref : Node_Id;
5870 Init_Assignment : Node_Id := Empty;
5871
5872 begin
5873 -- Build-in-place results must be returned by reference
5874
5875 Set_By_Ref (Return_Stmt);
5876
5877 -- Retrieve the implicit access parameter passed by the caller
5878
5879 Object_Access :=
5880 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
5881
5882 -- If the return object's declaration includes an expression
5883 -- and the declaration isn't marked as No_Initialization, then
5884 -- we need to generate an assignment to the object and insert
5885 -- it after the declaration before rewriting it as a renaming
5886 -- (otherwise we'll lose the initialization). The case where
5887 -- the result type is an interface (or class-wide interface)
5888 -- is also excluded because the context of the function call
5889 -- must be unconstrained, so the initialization will always
5890 -- be done as part of an allocator evaluation (storage pool
5891 -- or secondary stack), never to a constrained target object
5892 -- passed in by the caller. Besides the assignment being
5893 -- unneeded in this case, it avoids problems with trying to
5894 -- generate a dispatching assignment when the return expression
5895 -- is a nonlimited descendant of a limited interface (the
5896 -- interface has no assignment operation).
5897
5898 if Present (Return_Obj_Expr)
5899 and then not No_Initialization (Ret_Obj_Decl)
5900 and then not Is_Interface (Return_Obj_Typ)
5901 then
5902 Init_Assignment :=
5903 Make_Assignment_Statement (Loc,
5904 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
5905 Expression => Relocate_Node (Return_Obj_Expr));
5906
5907 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
5908 Set_Assignment_OK (Name (Init_Assignment));
5909 Set_No_Ctrl_Actions (Init_Assignment);
5910
5911 Set_Parent (Name (Init_Assignment), Init_Assignment);
5912 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5913
5914 Set_Expression (Ret_Obj_Decl, Empty);
5915
5916 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
5917 and then not Is_Class_Wide_Type
5918 (Etype (Expression (Init_Assignment)))
5919 then
5920 Rewrite (Expression (Init_Assignment),
5921 Make_Type_Conversion (Loc,
5922 Subtype_Mark =>
5923 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
5924 Expression =>
5925 Relocate_Node (Expression (Init_Assignment))));
5926 end if;
5927
5928 -- In the case of functions where the calling context can
5929 -- determine the form of allocation needed, initialization
5930 -- is done with each part of the if statement that handles
5931 -- the different forms of allocation (this is true for
5932 -- unconstrained and tagged result subtypes).
5933
5934 if Constr_Result
5935 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
5936 then
5937 Insert_After (Ret_Obj_Decl, Init_Assignment);
5938 end if;
5939 end if;
5940
5941 -- When the function's subtype is unconstrained, a run-time
5942 -- test is needed to determine the form of allocation to use
5943 -- for the return object. The function has an implicit formal
5944 -- parameter indicating this. If the BIP_Alloc_Form formal has
5945 -- the value one, then the caller has passed access to an
5946 -- existing object for use as the return object. If the value
5947 -- is two, then the return object must be allocated on the
5948 -- secondary stack. Otherwise, the object must be allocated in
5949 -- a storage pool (currently only supported for the global
5950 -- heap, user-defined storage pools TBD ???). We generate an
5951 -- if statement to test the implicit allocation formal and
5952 -- initialize a local access value appropriately, creating
5953 -- allocators in the secondary stack and global heap cases.
5954 -- The special formal also exists and must be tested when the
5955 -- function has a tagged result, even when the result subtype
5956 -- is constrained, because in general such functions can be
5957 -- called in dispatching contexts and must be handled similarly
5958 -- to functions with a class-wide result.
5959
5960 if not Constr_Result
5961 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5962 then
5963 Obj_Alloc_Formal :=
5964 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
5965
5966 declare
5967 Pool_Id : constant Entity_Id :=
5968 Make_Temporary (Loc, 'P');
5969 Alloc_Obj_Id : Entity_Id;
5970 Alloc_Obj_Decl : Node_Id;
5971 Alloc_If_Stmt : Node_Id;
5972 Heap_Allocator : Node_Id;
5973 Pool_Decl : Node_Id;
5974 Pool_Allocator : Node_Id;
5975 Ptr_Type_Decl : Node_Id;
5976 Ref_Type : Entity_Id;
5977 SS_Allocator : Node_Id;
5978
5979 begin
5980 -- Reuse the itype created for the function's implicit
5981 -- access formal. This avoids the need to create a new
5982 -- access type here, plus it allows assigning the access
5983 -- formal directly without applying a conversion.
5984
5985 -- Ref_Type := Etype (Object_Access);
5986
5987 -- Create an access type designating the function's
5988 -- result subtype.
5989
5990 Ref_Type := Make_Temporary (Loc, 'A');
5991
5992 Ptr_Type_Decl :=
5993 Make_Full_Type_Declaration (Loc,
5994 Defining_Identifier => Ref_Type,
5995 Type_Definition =>
5996 Make_Access_To_Object_Definition (Loc,
5997 All_Present => True,
5998 Subtype_Indication =>
5999 New_Occurrence_Of (Return_Obj_Typ, Loc)));
6000
6001 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
6002
6003 -- Create an access object that will be initialized to an
6004 -- access value denoting the return object, either coming
6005 -- from an implicit access value passed in by the caller
6006 -- or from the result of an allocator.
6007
6008 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
6009 Set_Etype (Alloc_Obj_Id, Ref_Type);
6010
6011 Alloc_Obj_Decl :=
6012 Make_Object_Declaration (Loc,
6013 Defining_Identifier => Alloc_Obj_Id,
6014 Object_Definition =>
6015 New_Occurrence_Of (Ref_Type, Loc));
6016
6017 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
6018
6019 -- Create allocators for both the secondary stack and
6020 -- global heap. If there's an initialization expression,
6021 -- then create these as initialized allocators.
6022
6023 if Present (Return_Obj_Expr)
6024 and then not No_Initialization (Ret_Obj_Decl)
6025 then
6026 -- Always use the type of the expression for the
6027 -- qualified expression, rather than the result type.
6028 -- In general we cannot always use the result type
6029 -- for the allocator, because the expression might be
6030 -- of a specific type, such as in the case of an
6031 -- aggregate or even a nonlimited object when the
6032 -- result type is a limited class-wide interface type.
6033
6034 Heap_Allocator :=
6035 Make_Allocator (Loc,
6036 Expression =>
6037 Make_Qualified_Expression (Loc,
6038 Subtype_Mark =>
6039 New_Occurrence_Of
6040 (Etype (Return_Obj_Expr), Loc),
6041 Expression =>
6042 New_Copy_Tree (Return_Obj_Expr)));
6043
6044 else
6045 -- If the function returns a class-wide type we cannot
6046 -- use the return type for the allocator. Instead we
6047 -- use the type of the expression, which must be an
6048 -- aggregate of a definite type.
6049
6050 if Is_Class_Wide_Type (Return_Obj_Typ) then
6051 Heap_Allocator :=
6052 Make_Allocator (Loc,
6053 Expression =>
6054 New_Occurrence_Of
6055 (Etype (Return_Obj_Expr), Loc));
6056 else
6057 Heap_Allocator :=
6058 Make_Allocator (Loc,
6059 Expression =>
6060 New_Occurrence_Of (Return_Obj_Typ, Loc));
6061 end if;
6062
6063 -- If the object requires default initialization then
6064 -- that will happen later following the elaboration of
6065 -- the object renaming. If we don't turn it off here
6066 -- then the object will be default initialized twice.
6067
6068 Set_No_Initialization (Heap_Allocator);
6069 end if;
6070
6071 -- The Pool_Allocator is just like the Heap_Allocator,
6072 -- except we set Storage_Pool and Procedure_To_Call so
6073 -- it will use the user-defined storage pool.
6074
6075 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
6076
6077 -- Do not generate the renaming of the build-in-place
6078 -- pool parameter on .NET/JVM/ZFP because the parameter
6079 -- is not created in the first place.
6080
6081 if VM_Target = No_VM
6082 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6083 then
6084 Pool_Decl :=
6085 Make_Object_Renaming_Declaration (Loc,
6086 Defining_Identifier => Pool_Id,
6087 Subtype_Mark =>
6088 New_Occurrence_Of
6089 (RTE (RE_Root_Storage_Pool), Loc),
6090 Name =>
6091 Make_Explicit_Dereference (Loc,
6092 New_Occurrence_Of
6093 (Build_In_Place_Formal
6094 (Par_Func, BIP_Storage_Pool), Loc)));
6095 Set_Storage_Pool (Pool_Allocator, Pool_Id);
6096 Set_Procedure_To_Call
6097 (Pool_Allocator, RTE (RE_Allocate_Any));
6098 else
6099 Pool_Decl := Make_Null_Statement (Loc);
6100 end if;
6101
6102 -- If the No_Allocators restriction is active, then only
6103 -- an allocator for secondary stack allocation is needed.
6104 -- It's OK for such allocators to have Comes_From_Source
6105 -- set to False, because gigi knows not to flag them as
6106 -- being a violation of No_Implicit_Heap_Allocations.
6107
6108 if Restriction_Active (No_Allocators) then
6109 SS_Allocator := Heap_Allocator;
6110 Heap_Allocator := Make_Null (Loc);
6111 Pool_Allocator := Make_Null (Loc);
6112
6113 -- Otherwise the heap and pool allocators may be needed,
6114 -- so we make another allocator for secondary stack
6115 -- allocation.
6116
6117 else
6118 SS_Allocator := New_Copy_Tree (Heap_Allocator);
6119
6120 -- The heap and pool allocators are marked as
6121 -- Comes_From_Source since they correspond to an
6122 -- explicit user-written allocator (that is, it will
6123 -- only be executed on behalf of callers that call the
6124 -- function as initialization for such an allocator).
6125 -- Prevents errors when No_Implicit_Heap_Allocations
6126 -- is in force.
6127
6128 Set_Comes_From_Source (Heap_Allocator, True);
6129 Set_Comes_From_Source (Pool_Allocator, True);
6130 end if;
6131
6132 -- The allocator is returned on the secondary stack. We
6133 -- don't do this on VM targets, since the SS is not used.
6134
6135 if VM_Target = No_VM then
6136 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
6137 Set_Procedure_To_Call
6138 (SS_Allocator, RTE (RE_SS_Allocate));
6139
6140 -- The allocator is returned on the secondary stack,
6141 -- so indicate that the function return, as well as
6142 -- the block that encloses the allocator, must not
6143 -- release it. The flags must be set now because
6144 -- the decision to use the secondary stack is done
6145 -- very late in the course of expanding the return
6146 -- statement, past the point where these flags are
6147 -- normally set.
6148
6149 Set_Sec_Stack_Needed_For_Return (Par_Func);
6150 Set_Sec_Stack_Needed_For_Return
6151 (Return_Statement_Entity (N));
6152 Set_Uses_Sec_Stack (Par_Func);
6153 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
6154 end if;
6155
6156 -- Create an if statement to test the BIP_Alloc_Form
6157 -- formal and initialize the access object to either the
6158 -- BIP_Object_Access formal (BIP_Alloc_Form =
6159 -- Caller_Allocation), the result of allocating the
6160 -- object in the secondary stack (BIP_Alloc_Form =
6161 -- Secondary_Stack), or else an allocator to create the
6162 -- return object in the heap or user-defined pool
6163 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6164
6165 -- ??? An unchecked type conversion must be made in the
6166 -- case of assigning the access object formal to the
6167 -- local access object, because a normal conversion would
6168 -- be illegal in some cases (such as converting access-
6169 -- to-unconstrained to access-to-constrained), but the
6170 -- the unchecked conversion will presumably fail to work
6171 -- right in just such cases. It's not clear at all how to
6172 -- handle this. ???
6173
6174 Alloc_If_Stmt :=
6175 Make_If_Statement (Loc,
6176 Condition =>
6177 Make_Op_Eq (Loc,
6178 Left_Opnd =>
6179 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6180 Right_Opnd =>
6181 Make_Integer_Literal (Loc,
6182 UI_From_Int (BIP_Allocation_Form'Pos
6183 (Caller_Allocation)))),
6184
6185 Then_Statements => New_List (
6186 Make_Assignment_Statement (Loc,
6187 Name =>
6188 New_Occurrence_Of (Alloc_Obj_Id, Loc),
6189 Expression =>
6190 Make_Unchecked_Type_Conversion (Loc,
6191 Subtype_Mark =>
6192 New_Occurrence_Of (Ref_Type, Loc),
6193 Expression =>
6194 New_Occurrence_Of (Object_Access, Loc)))),
6195
6196 Elsif_Parts => New_List (
6197 Make_Elsif_Part (Loc,
6198 Condition =>
6199 Make_Op_Eq (Loc,
6200 Left_Opnd =>
6201 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6202 Right_Opnd =>
6203 Make_Integer_Literal (Loc,
6204 UI_From_Int (BIP_Allocation_Form'Pos
6205 (Secondary_Stack)))),
6206
6207 Then_Statements => New_List (
6208 Make_Assignment_Statement (Loc,
6209 Name =>
6210 New_Occurrence_Of (Alloc_Obj_Id, Loc),
6211 Expression => SS_Allocator))),
6212
6213 Make_Elsif_Part (Loc,
6214 Condition =>
6215 Make_Op_Eq (Loc,
6216 Left_Opnd =>
6217 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6218 Right_Opnd =>
6219 Make_Integer_Literal (Loc,
6220 UI_From_Int (BIP_Allocation_Form'Pos
6221 (Global_Heap)))),
6222
6223 Then_Statements => New_List (
6224 Build_Heap_Allocator
6225 (Temp_Id => Alloc_Obj_Id,
6226 Temp_Typ => Ref_Type,
6227 Func_Id => Par_Func,
6228 Ret_Typ => Return_Obj_Typ,
6229 Alloc_Expr => Heap_Allocator)))),
6230
6231 Else_Statements => New_List (
6232 Pool_Decl,
6233 Build_Heap_Allocator
6234 (Temp_Id => Alloc_Obj_Id,
6235 Temp_Typ => Ref_Type,
6236 Func_Id => Par_Func,
6237 Ret_Typ => Return_Obj_Typ,
6238 Alloc_Expr => Pool_Allocator)));
6239
6240 -- If a separate initialization assignment was created
6241 -- earlier, append that following the assignment of the
6242 -- implicit access formal to the access object, to ensure
6243 -- that the return object is initialized in that case. In
6244 -- this situation, the target of the assignment must be
6245 -- rewritten to denote a dereference of the access to the
6246 -- return object passed in by the caller.
6247
6248 if Present (Init_Assignment) then
6249 Rewrite (Name (Init_Assignment),
6250 Make_Explicit_Dereference (Loc,
6251 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
6252
6253 Set_Etype
6254 (Name (Init_Assignment), Etype (Return_Obj_Id));
6255
6256 Append_To
6257 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
6258 end if;
6259
6260 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
6261
6262 -- Remember the local access object for use in the
6263 -- dereference of the renaming created below.
6264
6265 Object_Access := Alloc_Obj_Id;
6266 end;
6267 end if;
6268
6269 -- Replace the return object declaration with a renaming of a
6270 -- dereference of the access value designating the return
6271 -- object.
6272
6273 Obj_Acc_Deref :=
6274 Make_Explicit_Dereference (Loc,
6275 Prefix => New_Occurrence_Of (Object_Access, Loc));
6276
6277 Rewrite (Ret_Obj_Decl,
6278 Make_Object_Renaming_Declaration (Loc,
6279 Defining_Identifier => Return_Obj_Id,
6280 Access_Definition => Empty,
6281 Subtype_Mark =>
6282 New_Occurrence_Of (Return_Obj_Typ, Loc),
6283 Name => Obj_Acc_Deref));
6284
6285 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
6286 end;
6287 end if;
6288
6289 -- Case where we do not build a block
6290
6291 else
6292 -- We're about to drop Return_Object_Declarations on the floor, so
6293 -- we need to insert it, in case it got expanded into useful code.
6294 -- Remove side effects from expression, which may be duplicated in
6295 -- subsequent checks (see Expand_Simple_Function_Return).
6296
6297 Insert_List_Before (N, Return_Object_Declarations (N));
6298 Remove_Side_Effects (Exp);
6299
6300 -- Build simple_return_statement that returns the expression directly
6301
6302 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6303 Result := Return_Stmt;
6304 end if;
6305
6306 -- Set the flag to prevent infinite recursion
6307
6308 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
6309
6310 Rewrite (N, Result);
6311 Analyze (N);
6312 end Expand_N_Extended_Return_Statement;
6313
6314 ----------------------------
6315 -- Expand_N_Function_Call --
6316 ----------------------------
6317
6318 procedure Expand_N_Function_Call (N : Node_Id) is
6319 begin
6320 Expand_Call (N);
6321
6322 -- If the return value of a foreign compiled function is VAX Float, then
6323 -- expand the return (adjusts the location of the return value on
6324 -- Alpha/VMS, no-op everywhere else).
6325 -- Comes_From_Source intercepts recursive expansion.
6326
6327 if Nkind (N) = N_Function_Call
6328 and then Vax_Float (Etype (N))
6329 and then Present (Name (N))
6330 and then Present (Entity (Name (N)))
6331 and then Has_Foreign_Convention (Entity (Name (N)))
6332 and then Comes_From_Source (Parent (N))
6333 then
6334 Expand_Vax_Foreign_Return (N);
6335 end if;
6336 end Expand_N_Function_Call;
6337
6338 ---------------------------------------
6339 -- Expand_N_Procedure_Call_Statement --
6340 ---------------------------------------
6341
6342 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
6343 begin
6344 Expand_Call (N);
6345 end Expand_N_Procedure_Call_Statement;
6346
6347 --------------------------------------
6348 -- Expand_N_Simple_Return_Statement --
6349 --------------------------------------
6350
6351 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6352 begin
6353 -- Defend against previous errors (i.e. the return statement calls a
6354 -- function that is not available in configurable runtime).
6355
6356 if Present (Expression (N))
6357 and then Nkind (Expression (N)) = N_Empty
6358 then
6359 Check_Error_Detected;
6360 return;
6361 end if;
6362
6363 -- Distinguish the function and non-function cases:
6364
6365 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6366
6367 when E_Function |
6368 E_Generic_Function =>
6369 Expand_Simple_Function_Return (N);
6370
6371 when E_Procedure |
6372 E_Generic_Procedure |
6373 E_Entry |
6374 E_Entry_Family |
6375 E_Return_Statement =>
6376 Expand_Non_Function_Return (N);
6377
6378 when others =>
6379 raise Program_Error;
6380 end case;
6381
6382 exception
6383 when RE_Not_Available =>
6384 return;
6385 end Expand_N_Simple_Return_Statement;
6386
6387 ------------------------------
6388 -- Expand_N_Subprogram_Body --
6389 ------------------------------
6390
6391 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6392 -- by the back-end.
6393
6394 -- Add dummy push/pop label nodes at start and end to clear any local
6395 -- exception indications if local-exception-to-goto optimization is active.
6396
6397 -- Add return statement if last statement in body is not a return statement
6398 -- (this makes things easier on Gigi which does not want to have to handle
6399 -- a missing return).
6400
6401 -- Add call to Activate_Tasks if body is a task activator
6402
6403 -- Deal with possible detection of infinite recursion
6404
6405 -- Eliminate body completely if convention stubbed
6406
6407 -- Encode entity names within body, since we will not need to reference
6408 -- these entities any longer in the front end.
6409
6410 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6411
6412 -- Reset Pure indication if any parameter has root type System.Address
6413 -- or has any parameters of limited types, where limited means that the
6414 -- run-time view is limited (i.e. the full type is limited).
6415
6416 -- Wrap thread body
6417
6418 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6419 Loc : constant Source_Ptr := Sloc (N);
6420 H : constant Node_Id := Handled_Statement_Sequence (N);
6421 Body_Id : Entity_Id;
6422 Except_H : Node_Id;
6423 L : List_Id;
6424 Spec_Id : Entity_Id;
6425
6426 procedure Add_Return (S : List_Id);
6427 -- Append a return statement to the statement sequence S if the last
6428 -- statement is not already a return or a goto statement. Note that
6429 -- the latter test is not critical, it does not matter if we add a few
6430 -- extra returns, since they get eliminated anyway later on.
6431
6432 ----------------
6433 -- Add_Return --
6434 ----------------
6435
6436 procedure Add_Return (S : List_Id) is
6437 Last_Stm : Node_Id;
6438 Loc : Source_Ptr;
6439
6440 begin
6441 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6442 -- not relevant in this context since they are not executable.
6443
6444 Last_Stm := Last (S);
6445 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
6446 Prev (Last_Stm);
6447 end loop;
6448
6449 -- Now insert return unless last statement is a transfer
6450
6451 if not Is_Transfer (Last_Stm) then
6452
6453 -- The source location for the return is the end label of the
6454 -- procedure if present. Otherwise use the sloc of the last
6455 -- statement in the list. If the list comes from a generated
6456 -- exception handler and we are not debugging generated code,
6457 -- all the statements within the handler are made invisible
6458 -- to the debugger.
6459
6460 if Nkind (Parent (S)) = N_Exception_Handler
6461 and then not Comes_From_Source (Parent (S))
6462 then
6463 Loc := Sloc (Last_Stm);
6464 elsif Present (End_Label (H)) then
6465 Loc := Sloc (End_Label (H));
6466 else
6467 Loc := Sloc (Last_Stm);
6468 end if;
6469
6470 declare
6471 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
6472
6473 begin
6474 -- Append return statement, and set analyzed manually. We can't
6475 -- call Analyze on this return since the scope is wrong.
6476
6477 -- Note: it almost works to push the scope and then do the
6478 -- Analyze call, but something goes wrong in some weird cases
6479 -- and it is not worth worrying about ???
6480
6481 Append_To (S, Rtn);
6482 Set_Analyzed (Rtn);
6483
6484 -- Call _Postconditions procedure if appropriate. We need to
6485 -- do this explicitly because we did not analyze the generated
6486 -- return statement above, so the call did not get inserted.
6487
6488 if Ekind (Spec_Id) = E_Procedure
6489 and then Has_Postconditions (Spec_Id)
6490 then
6491 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
6492 Insert_Action (Rtn,
6493 Make_Procedure_Call_Statement (Loc,
6494 Name =>
6495 New_Occurrence_Of
6496 (Postcondition_Proc (Spec_Id), Loc)));
6497 end if;
6498 end;
6499 end if;
6500 end Add_Return;
6501
6502 -- Start of processing for Expand_N_Subprogram_Body
6503
6504 begin
6505 -- Set L to either the list of declarations if present, or to the list
6506 -- of statements if no declarations are present. This is used to insert
6507 -- new stuff at the start.
6508
6509 if Is_Non_Empty_List (Declarations (N)) then
6510 L := Declarations (N);
6511 else
6512 L := Statements (H);
6513 end if;
6514
6515 -- If local-exception-to-goto optimization active, insert dummy push
6516 -- statements at start, and dummy pop statements at end, but inhibit
6517 -- this if we have No_Exception_Handlers, since they are useless and
6518 -- intefere with analysis, e.g. by codepeer.
6519
6520 if (Debug_Flag_Dot_G
6521 or else Restriction_Active (No_Exception_Propagation))
6522 and then not Restriction_Active (No_Exception_Handlers)
6523 and then not CodePeer_Mode
6524 and then Is_Non_Empty_List (L)
6525 then
6526 declare
6527 FS : constant Node_Id := First (L);
6528 FL : constant Source_Ptr := Sloc (FS);
6529 LS : Node_Id;
6530 LL : Source_Ptr;
6531
6532 begin
6533 -- LS points to either last statement, if statements are present
6534 -- or to the last declaration if there are no statements present.
6535 -- It is the node after which the pop's are generated.
6536
6537 if Is_Non_Empty_List (Statements (H)) then
6538 LS := Last (Statements (H));
6539 else
6540 LS := Last (L);
6541 end if;
6542
6543 LL := Sloc (LS);
6544
6545 Insert_List_Before_And_Analyze (FS, New_List (
6546 Make_Push_Constraint_Error_Label (FL),
6547 Make_Push_Program_Error_Label (FL),
6548 Make_Push_Storage_Error_Label (FL)));
6549
6550 Insert_List_After_And_Analyze (LS, New_List (
6551 Make_Pop_Constraint_Error_Label (LL),
6552 Make_Pop_Program_Error_Label (LL),
6553 Make_Pop_Storage_Error_Label (LL)));
6554 end;
6555 end if;
6556
6557 -- Find entity for subprogram
6558
6559 Body_Id := Defining_Entity (N);
6560
6561 if Present (Corresponding_Spec (N)) then
6562 Spec_Id := Corresponding_Spec (N);
6563 else
6564 Spec_Id := Body_Id;
6565 end if;
6566
6567 -- Need poll on entry to subprogram if polling enabled. We only do this
6568 -- for non-empty subprograms, since it does not seem necessary to poll
6569 -- for a dummy null subprogram.
6570
6571 if Is_Non_Empty_List (L) then
6572
6573 -- Do not add a polling call if the subprogram is to be inlined by
6574 -- the back-end, to avoid repeated calls with multiple inlinings.
6575
6576 if Is_Inlined (Spec_Id)
6577 and then Front_End_Inlining
6578 and then Optimization_Level > 1
6579 then
6580 null;
6581 else
6582 Generate_Poll_Call (First (L));
6583 end if;
6584 end if;
6585
6586 -- If this is a Pure function which has any parameters whose root type
6587 -- is System.Address, reset the Pure indication, since it will likely
6588 -- cause incorrect code to be generated as the parameter is probably
6589 -- a pointer, and the fact that the same pointer is passed does not mean
6590 -- that the same value is being referenced.
6591
6592 -- Note that if the programmer gave an explicit Pure_Function pragma,
6593 -- then we believe the programmer, and leave the subprogram Pure.
6594
6595 -- This code should probably be at the freeze point, so that it happens
6596 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6597 -- semantic tree has Is_Pure set properly ???
6598
6599 if Is_Pure (Spec_Id)
6600 and then Is_Subprogram (Spec_Id)
6601 and then not Has_Pragma_Pure_Function (Spec_Id)
6602 then
6603 declare
6604 F : Entity_Id;
6605
6606 begin
6607 F := First_Formal (Spec_Id);
6608 while Present (F) loop
6609 if Is_Descendent_Of_Address (Etype (F))
6610
6611 -- Note that this test is being made in the body of the
6612 -- subprogram, not the spec, so we are testing the full
6613 -- type for being limited here, as required.
6614
6615 or else Is_Limited_Type (Etype (F))
6616 then
6617 Set_Is_Pure (Spec_Id, False);
6618
6619 if Spec_Id /= Body_Id then
6620 Set_Is_Pure (Body_Id, False);
6621 end if;
6622
6623 exit;
6624 end if;
6625
6626 Next_Formal (F);
6627 end loop;
6628 end;
6629 end if;
6630
6631 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6632
6633 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
6634 declare
6635 F : Entity_Id;
6636 A : Node_Id;
6637
6638 begin
6639 -- Loop through formals
6640
6641 F := First_Formal (Spec_Id);
6642 while Present (F) loop
6643 if Is_Scalar_Type (Etype (F))
6644 and then Ekind (F) = E_Out_Parameter
6645 then
6646 Check_Restriction (No_Default_Initialization, F);
6647
6648 -- Insert the initialization. We turn off validity checks
6649 -- for this assignment, since we do not want any check on
6650 -- the initial value itself (which may well be invalid).
6651 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6652
6653 A := Make_Assignment_Statement (Loc,
6654 Name => New_Occurrence_Of (F, Loc),
6655 Expression => Get_Simple_Init_Val (Etype (F), N));
6656 Set_Suppress_Assignment_Checks (A);
6657
6658 Insert_Before_And_Analyze (First (L),
6659 A, Suppress => Validity_Check);
6660 end if;
6661
6662 Next_Formal (F);
6663 end loop;
6664 end;
6665 end if;
6666
6667 -- Clear out statement list for stubbed procedure
6668
6669 if Present (Corresponding_Spec (N)) then
6670 Set_Elaboration_Flag (N, Spec_Id);
6671
6672 if Convention (Spec_Id) = Convention_Stubbed
6673 or else Is_Eliminated (Spec_Id)
6674 then
6675 Set_Declarations (N, Empty_List);
6676 Set_Handled_Statement_Sequence (N,
6677 Make_Handled_Sequence_Of_Statements (Loc,
6678 Statements => New_List (Make_Null_Statement (Loc))));
6679 return;
6680 end if;
6681 end if;
6682
6683 -- Create a set of discriminals for the next protected subprogram body
6684
6685 if Is_List_Member (N)
6686 and then Present (Parent (List_Containing (N)))
6687 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6688 and then Present (Next_Protected_Operation (N))
6689 then
6690 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
6691 end if;
6692
6693 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6694 -- subprograms with no specs are not frozen.
6695
6696 declare
6697 Typ : constant Entity_Id := Etype (Spec_Id);
6698 Utyp : constant Entity_Id := Underlying_Type (Typ);
6699
6700 begin
6701 if not Acts_As_Spec (N)
6702 and then Nkind (Parent (Parent (Spec_Id))) /=
6703 N_Subprogram_Body_Stub
6704 then
6705 null;
6706
6707 elsif Is_Limited_View (Typ) then
6708 Set_Returns_By_Ref (Spec_Id);
6709
6710 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6711 Set_Returns_By_Ref (Spec_Id);
6712 end if;
6713 end;
6714
6715 -- For a procedure, we add a return for all possible syntactic ends of
6716 -- the subprogram.
6717
6718 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
6719 Add_Return (Statements (H));
6720
6721 if Present (Exception_Handlers (H)) then
6722 Except_H := First_Non_Pragma (Exception_Handlers (H));
6723 while Present (Except_H) loop
6724 Add_Return (Statements (Except_H));
6725 Next_Non_Pragma (Except_H);
6726 end loop;
6727 end if;
6728
6729 -- For a function, we must deal with the case where there is at least
6730 -- one missing return. What we do is to wrap the entire body of the
6731 -- function in a block:
6732
6733 -- begin
6734 -- ...
6735 -- end;
6736
6737 -- becomes
6738
6739 -- begin
6740 -- begin
6741 -- ...
6742 -- end;
6743
6744 -- raise Program_Error;
6745 -- end;
6746
6747 -- This approach is necessary because the raise must be signalled to the
6748 -- caller, not handled by any local handler (RM 6.4(11)).
6749
6750 -- Note: we do not need to analyze the constructed sequence here, since
6751 -- it has no handler, and an attempt to analyze the handled statement
6752 -- sequence twice is risky in various ways (e.g. the issue of expanding
6753 -- cleanup actions twice).
6754
6755 elsif Has_Missing_Return (Spec_Id) then
6756 declare
6757 Hloc : constant Source_Ptr := Sloc (H);
6758 Blok : constant Node_Id :=
6759 Make_Block_Statement (Hloc,
6760 Handled_Statement_Sequence => H);
6761 Rais : constant Node_Id :=
6762 Make_Raise_Program_Error (Hloc,
6763 Reason => PE_Missing_Return);
6764
6765 begin
6766 Set_Handled_Statement_Sequence (N,
6767 Make_Handled_Sequence_Of_Statements (Hloc,
6768 Statements => New_List (Blok, Rais)));
6769
6770 Push_Scope (Spec_Id);
6771 Analyze (Blok);
6772 Analyze (Rais);
6773 Pop_Scope;
6774 end;
6775 end if;
6776
6777 -- If subprogram contains a parameterless recursive call, then we may
6778 -- have an infinite recursion, so see if we can generate code to check
6779 -- for this possibility if storage checks are not suppressed.
6780
6781 if Ekind (Spec_Id) = E_Procedure
6782 and then Has_Recursive_Call (Spec_Id)
6783 and then not Storage_Checks_Suppressed (Spec_Id)
6784 then
6785 Detect_Infinite_Recursion (N, Spec_Id);
6786 end if;
6787
6788 -- Set to encode entity names in package body before gigi is called
6789
6790 Qualify_Entity_Names (N);
6791 end Expand_N_Subprogram_Body;
6792
6793 -----------------------------------
6794 -- Expand_N_Subprogram_Body_Stub --
6795 -----------------------------------
6796
6797 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6798 begin
6799 if Present (Corresponding_Body (N)) then
6800 Expand_N_Subprogram_Body (
6801 Unit_Declaration_Node (Corresponding_Body (N)));
6802 end if;
6803 end Expand_N_Subprogram_Body_Stub;
6804
6805 -------------------------------------
6806 -- Expand_N_Subprogram_Declaration --
6807 -------------------------------------
6808
6809 -- If the declaration appears within a protected body, it is a private
6810 -- operation of the protected type. We must create the corresponding
6811 -- protected subprogram an associated formals. For a normal protected
6812 -- operation, this is done when expanding the protected type declaration.
6813
6814 -- If the declaration is for a null procedure, emit null body
6815
6816 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6817 Loc : constant Source_Ptr := Sloc (N);
6818 Subp : constant Entity_Id := Defining_Entity (N);
6819 Scop : constant Entity_Id := Scope (Subp);
6820 Prot_Decl : Node_Id;
6821 Prot_Bod : Node_Id;
6822 Prot_Id : Entity_Id;
6823
6824 begin
6825 -- In SPARK, subprogram declarations are only allowed in package
6826 -- specifications.
6827
6828 if Nkind (Parent (N)) /= N_Package_Specification then
6829 if Nkind (Parent (N)) = N_Compilation_Unit then
6830 Check_SPARK_Restriction
6831 ("subprogram declaration is not a library item", N);
6832
6833 elsif Present (Next (N))
6834 and then Nkind (Next (N)) = N_Pragma
6835 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
6836 then
6837 -- In SPARK, subprogram declarations are also permitted in
6838 -- declarative parts when immediately followed by a corresponding
6839 -- pragma Import. We only check here that there is some pragma
6840 -- Import.
6841
6842 null;
6843 else
6844 Check_SPARK_Restriction
6845 ("subprogram declaration is not allowed here", N);
6846 end if;
6847 end if;
6848
6849 -- Deal with case of protected subprogram. Do not generate protected
6850 -- operation if operation is flagged as eliminated.
6851
6852 if Is_List_Member (N)
6853 and then Present (Parent (List_Containing (N)))
6854 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6855 and then Is_Protected_Type (Scop)
6856 then
6857 if No (Protected_Body_Subprogram (Subp))
6858 and then not Is_Eliminated (Subp)
6859 then
6860 Prot_Decl :=
6861 Make_Subprogram_Declaration (Loc,
6862 Specification =>
6863 Build_Protected_Sub_Specification
6864 (N, Scop, Unprotected_Mode));
6865
6866 -- The protected subprogram is declared outside of the protected
6867 -- body. Given that the body has frozen all entities so far, we
6868 -- analyze the subprogram and perform freezing actions explicitly.
6869 -- including the generation of an explicit freeze node, to ensure
6870 -- that gigi has the proper order of elaboration.
6871 -- If the body is a subunit, the insertion point is before the
6872 -- stub in the parent.
6873
6874 Prot_Bod := Parent (List_Containing (N));
6875
6876 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6877 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6878 end if;
6879
6880 Insert_Before (Prot_Bod, Prot_Decl);
6881 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6882 Set_Has_Delayed_Freeze (Prot_Id);
6883
6884 Push_Scope (Scope (Scop));
6885 Analyze (Prot_Decl);
6886 Freeze_Before (N, Prot_Id);
6887 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6888
6889 -- Create protected operation as well. Even though the operation
6890 -- is only accessible within the body, it is possible to make it
6891 -- available outside of the protected object by using 'Access to
6892 -- provide a callback, so build protected version in all cases.
6893
6894 Prot_Decl :=
6895 Make_Subprogram_Declaration (Loc,
6896 Specification =>
6897 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
6898 Insert_Before (Prot_Bod, Prot_Decl);
6899 Analyze (Prot_Decl);
6900
6901 Pop_Scope;
6902 end if;
6903
6904 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6905 -- cases this is superfluous because calls to it will be automatically
6906 -- inlined, but we definitely need the body if preconditions for the
6907 -- procedure are present.
6908
6909 elsif Nkind (Specification (N)) = N_Procedure_Specification
6910 and then Null_Present (Specification (N))
6911 then
6912 declare
6913 Bod : constant Node_Id := Body_To_Inline (N);
6914
6915 begin
6916 Set_Has_Completion (Subp, False);
6917 Append_Freeze_Action (Subp, Bod);
6918
6919 -- The body now contains raise statements, so calls to it will
6920 -- not be inlined.
6921
6922 Set_Is_Inlined (Subp, False);
6923 end;
6924 end if;
6925 end Expand_N_Subprogram_Declaration;
6926
6927 --------------------------------
6928 -- Expand_Non_Function_Return --
6929 --------------------------------
6930
6931 procedure Expand_Non_Function_Return (N : Node_Id) is
6932 pragma Assert (No (Expression (N)));
6933
6934 Loc : constant Source_Ptr := Sloc (N);
6935 Scope_Id : Entity_Id :=
6936 Return_Applies_To (Return_Statement_Entity (N));
6937 Kind : constant Entity_Kind := Ekind (Scope_Id);
6938 Call : Node_Id;
6939 Acc_Stat : Node_Id;
6940 Goto_Stat : Node_Id;
6941 Lab_Node : Node_Id;
6942
6943 begin
6944 -- Call _Postconditions procedure if procedure with active
6945 -- postconditions. Here, we use the Postcondition_Proc attribute,
6946 -- which is needed for implicitly-generated returns. Functions
6947 -- never have implicitly-generated returns, and there's no
6948 -- room for Postcondition_Proc in E_Function, so we look up the
6949 -- identifier Name_uPostconditions for function returns (see
6950 -- Expand_Simple_Function_Return).
6951
6952 if Ekind (Scope_Id) = E_Procedure
6953 and then Has_Postconditions (Scope_Id)
6954 then
6955 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
6956 Insert_Action (N,
6957 Make_Procedure_Call_Statement (Loc,
6958 Name => New_Occurrence_Of (Postcondition_Proc (Scope_Id), Loc)));
6959 end if;
6960
6961 -- If it is a return from a procedure do no extra steps
6962
6963 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6964 return;
6965
6966 -- If it is a nested return within an extended one, replace it with a
6967 -- return of the previously declared return object.
6968
6969 elsif Kind = E_Return_Statement then
6970 Rewrite (N,
6971 Make_Simple_Return_Statement (Loc,
6972 Expression =>
6973 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6974 Set_Comes_From_Extended_Return_Statement (N);
6975 Set_Return_Statement_Entity (N, Scope_Id);
6976 Expand_Simple_Function_Return (N);
6977 return;
6978 end if;
6979
6980 pragma Assert (Is_Entry (Scope_Id));
6981
6982 -- Look at the enclosing block to see whether the return is from an
6983 -- accept statement or an entry body.
6984
6985 for J in reverse 0 .. Scope_Stack.Last loop
6986 Scope_Id := Scope_Stack.Table (J).Entity;
6987 exit when Is_Concurrent_Type (Scope_Id);
6988 end loop;
6989
6990 -- If it is a return from accept statement it is expanded as call to
6991 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6992
6993 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6994 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6995
6996 if Is_Task_Type (Scope_Id) then
6997
6998 Call :=
6999 Make_Procedure_Call_Statement (Loc,
7000 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
7001 Insert_Before (N, Call);
7002 -- why not insert actions here???
7003 Analyze (Call);
7004
7005 Acc_Stat := Parent (N);
7006 while Nkind (Acc_Stat) /= N_Accept_Statement loop
7007 Acc_Stat := Parent (Acc_Stat);
7008 end loop;
7009
7010 Lab_Node := Last (Statements
7011 (Handled_Statement_Sequence (Acc_Stat)));
7012
7013 Goto_Stat := Make_Goto_Statement (Loc,
7014 Name => New_Occurrence_Of
7015 (Entity (Identifier (Lab_Node)), Loc));
7016
7017 Set_Analyzed (Goto_Stat);
7018
7019 Rewrite (N, Goto_Stat);
7020 Analyze (N);
7021
7022 -- If it is a return from an entry body, put a Complete_Entry_Body call
7023 -- in front of the return.
7024
7025 elsif Is_Protected_Type (Scope_Id) then
7026 Call :=
7027 Make_Procedure_Call_Statement (Loc,
7028 Name =>
7029 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
7030 Parameter_Associations => New_List (
7031 Make_Attribute_Reference (Loc,
7032 Prefix =>
7033 New_Occurrence_Of
7034 (Find_Protection_Object (Current_Scope), Loc),
7035 Attribute_Name => Name_Unchecked_Access)));
7036
7037 Insert_Before (N, Call);
7038 Analyze (Call);
7039 end if;
7040 end Expand_Non_Function_Return;
7041
7042 ---------------------------------------
7043 -- Expand_Protected_Object_Reference --
7044 ---------------------------------------
7045
7046 function Expand_Protected_Object_Reference
7047 (N : Node_Id;
7048 Scop : Entity_Id) return Node_Id
7049 is
7050 Loc : constant Source_Ptr := Sloc (N);
7051 Corr : Entity_Id;
7052 Rec : Node_Id;
7053 Param : Entity_Id;
7054 Proc : Entity_Id;
7055
7056 begin
7057 Rec := Make_Identifier (Loc, Name_uObject);
7058 Set_Etype (Rec, Corresponding_Record_Type (Scop));
7059
7060 -- Find enclosing protected operation, and retrieve its first parameter,
7061 -- which denotes the enclosing protected object. If the enclosing
7062 -- operation is an entry, we are immediately within the protected body,
7063 -- and we can retrieve the object from the service entries procedure. A
7064 -- barrier function has the same signature as an entry. A barrier
7065 -- function is compiled within the protected object, but unlike
7066 -- protected operations its never needs locks, so that its protected
7067 -- body subprogram points to itself.
7068
7069 Proc := Current_Scope;
7070 while Present (Proc)
7071 and then Scope (Proc) /= Scop
7072 loop
7073 Proc := Scope (Proc);
7074 end loop;
7075
7076 Corr := Protected_Body_Subprogram (Proc);
7077
7078 if No (Corr) then
7079
7080 -- Previous error left expansion incomplete.
7081 -- Nothing to do on this call.
7082
7083 return Empty;
7084 end if;
7085
7086 Param :=
7087 Defining_Identifier
7088 (First (Parameter_Specifications (Parent (Corr))));
7089
7090 if Is_Subprogram (Proc)
7091 and then Proc /= Corr
7092 then
7093 -- Protected function or procedure
7094
7095 Set_Entity (Rec, Param);
7096
7097 -- Rec is a reference to an entity which will not be in scope when
7098 -- the call is reanalyzed, and needs no further analysis.
7099
7100 Set_Analyzed (Rec);
7101
7102 else
7103 -- Entry or barrier function for entry body. The first parameter of
7104 -- the entry body procedure is pointer to the object. We create a
7105 -- local variable of the proper type, duplicating what is done to
7106 -- define _object later on.
7107
7108 declare
7109 Decls : List_Id;
7110 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
7111
7112 begin
7113 Decls := New_List (
7114 Make_Full_Type_Declaration (Loc,
7115 Defining_Identifier => Obj_Ptr,
7116 Type_Definition =>
7117 Make_Access_To_Object_Definition (Loc,
7118 Subtype_Indication =>
7119 New_Occurrence_Of
7120 (Corresponding_Record_Type (Scop), Loc))));
7121
7122 Insert_Actions (N, Decls);
7123 Freeze_Before (N, Obj_Ptr);
7124
7125 Rec :=
7126 Make_Explicit_Dereference (Loc,
7127 Prefix =>
7128 Unchecked_Convert_To (Obj_Ptr,
7129 New_Occurrence_Of (Param, Loc)));
7130
7131 -- Analyze new actual. Other actuals in calls are already analyzed
7132 -- and the list of actuals is not reanalyzed after rewriting.
7133
7134 Set_Parent (Rec, N);
7135 Analyze (Rec);
7136 end;
7137 end if;
7138
7139 return Rec;
7140 end Expand_Protected_Object_Reference;
7141
7142 --------------------------------------
7143 -- Expand_Protected_Subprogram_Call --
7144 --------------------------------------
7145
7146 procedure Expand_Protected_Subprogram_Call
7147 (N : Node_Id;
7148 Subp : Entity_Id;
7149 Scop : Entity_Id)
7150 is
7151 Rec : Node_Id;
7152
7153 procedure Freeze_Called_Function;
7154 -- If it is a function call it can appear in elaboration code and
7155 -- the called entity must be frozen before the call. This must be
7156 -- done before the call is expanded, as the expansion may rewrite it
7157 -- to something other than a call (e.g. a temporary initialized in a
7158 -- transient block).
7159
7160 ----------------------------
7161 -- Freeze_Called_Function --
7162 ----------------------------
7163
7164 procedure Freeze_Called_Function is
7165 begin
7166 if Ekind (Subp) = E_Function then
7167 Freeze_Expression (Name (N));
7168 end if;
7169 end Freeze_Called_Function;
7170
7171 -- Start of processing for Expand_Protected_Subprogram_Call
7172
7173 begin
7174 -- If the protected object is not an enclosing scope, this is an inter-
7175 -- object function call. Inter-object procedure calls are expanded by
7176 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7177 -- subprogram being called is in the protected body being compiled, and
7178 -- if the protected object in the call is statically the enclosing type.
7179 -- The object may be an component of some other data structure, in which
7180 -- case this must be handled as an inter-object call.
7181
7182 if not In_Open_Scopes (Scop)
7183 or else not Is_Entity_Name (Name (N))
7184 then
7185 if Nkind (Name (N)) = N_Selected_Component then
7186 Rec := Prefix (Name (N));
7187
7188 else
7189 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
7190 Rec := Prefix (Prefix (Name (N)));
7191 end if;
7192
7193 Freeze_Called_Function;
7194 Build_Protected_Subprogram_Call (N,
7195 Name => New_Occurrence_Of (Subp, Sloc (N)),
7196 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7197 External => True);
7198
7199 else
7200 Rec := Expand_Protected_Object_Reference (N, Scop);
7201
7202 if No (Rec) then
7203 return;
7204 end if;
7205
7206 Freeze_Called_Function;
7207 Build_Protected_Subprogram_Call (N,
7208 Name => Name (N),
7209 Rec => Rec,
7210 External => False);
7211
7212 end if;
7213
7214 -- Analyze and resolve the new call. The actuals have already been
7215 -- resolved, but expansion of a function call will add extra actuals
7216 -- if needed. Analysis of a procedure call already includes resolution.
7217
7218 Analyze (N);
7219
7220 if Ekind (Subp) = E_Function then
7221 Resolve (N, Etype (Subp));
7222 end if;
7223 end Expand_Protected_Subprogram_Call;
7224
7225 --------------------------------------------
7226 -- Has_Unconstrained_Access_Discriminants --
7227 --------------------------------------------
7228
7229 function Has_Unconstrained_Access_Discriminants
7230 (Subtyp : Entity_Id) return Boolean
7231 is
7232 Discr : Entity_Id;
7233
7234 begin
7235 if Has_Discriminants (Subtyp)
7236 and then not Is_Constrained (Subtyp)
7237 then
7238 Discr := First_Discriminant (Subtyp);
7239 while Present (Discr) loop
7240 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7241 return True;
7242 end if;
7243
7244 Next_Discriminant (Discr);
7245 end loop;
7246 end if;
7247
7248 return False;
7249 end Has_Unconstrained_Access_Discriminants;
7250
7251 -----------------------------------
7252 -- Expand_Simple_Function_Return --
7253 -----------------------------------
7254
7255 -- The "simple" comes from the syntax rule simple_return_statement. The
7256 -- semantics are not at all simple.
7257
7258 procedure Expand_Simple_Function_Return (N : Node_Id) is
7259 Loc : constant Source_Ptr := Sloc (N);
7260
7261 Scope_Id : constant Entity_Id :=
7262 Return_Applies_To (Return_Statement_Entity (N));
7263 -- The function we are returning from
7264
7265 R_Type : constant Entity_Id := Etype (Scope_Id);
7266 -- The result type of the function
7267
7268 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7269
7270 Exp : constant Node_Id := Expression (N);
7271 pragma Assert (Present (Exp));
7272
7273 Exptyp : constant Entity_Id := Etype (Exp);
7274 -- The type of the expression (not necessarily the same as R_Type)
7275
7276 Subtype_Ind : Node_Id;
7277 -- If the result type of the function is class-wide and the expression
7278 -- has a specific type, then we use the expression's type as the type of
7279 -- the return object. In cases where the expression is an aggregate that
7280 -- is built in place, this avoids the need for an expensive conversion
7281 -- of the return object to the specific type on assignments to the
7282 -- individual components.
7283
7284 begin
7285 if Is_Class_Wide_Type (R_Type)
7286 and then not Is_Class_Wide_Type (Etype (Exp))
7287 then
7288 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
7289 else
7290 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7291 end if;
7292
7293 -- For the case of a simple return that does not come from an extended
7294 -- return, in the case of Ada 2005 where we are returning a limited
7295 -- type, we rewrite "return <expression>;" to be:
7296
7297 -- return _anon_ : <return_subtype> := <expression>
7298
7299 -- The expansion produced by Expand_N_Extended_Return_Statement will
7300 -- contain simple return statements (for example, a block containing
7301 -- simple return of the return object), which brings us back here with
7302 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7303 -- checking for a simple return that does not come from an extended
7304 -- return is to avoid this infinite recursion.
7305
7306 -- The reason for this design is that for Ada 2005 limited returns, we
7307 -- need to reify the return object, so we can build it "in place", and
7308 -- we need a block statement to hang finalization and tasking stuff.
7309
7310 -- ??? In order to avoid disruption, we avoid translating to extended
7311 -- return except in the cases where we really need to (Ada 2005 for
7312 -- inherently limited). We might prefer to do this translation in all
7313 -- cases (except perhaps for the case of Ada 95 inherently limited),
7314 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7315 -- code. This would also allow us to do the build-in-place optimization
7316 -- for efficiency even in cases where it is semantically not required.
7317
7318 -- As before, we check the type of the return expression rather than the
7319 -- return type of the function, because the latter may be a limited
7320 -- class-wide interface type, which is not a limited type, even though
7321 -- the type of the expression may be.
7322
7323 if not Comes_From_Extended_Return_Statement (N)
7324 and then Is_Limited_View (Etype (Expression (N)))
7325 and then Ada_Version >= Ada_2005
7326 and then not Debug_Flag_Dot_L
7327
7328 -- The functionality of interface thunks is simple and it is always
7329 -- handled by means of simple return statements. This leaves their
7330 -- expansion simple and clean.
7331
7332 and then not Is_Thunk (Current_Scope)
7333 then
7334 declare
7335 Return_Object_Entity : constant Entity_Id :=
7336 Make_Temporary (Loc, 'R', Exp);
7337
7338 Obj_Decl : constant Node_Id :=
7339 Make_Object_Declaration (Loc,
7340 Defining_Identifier => Return_Object_Entity,
7341 Object_Definition => Subtype_Ind,
7342 Expression => Exp);
7343
7344 Ext : constant Node_Id :=
7345 Make_Extended_Return_Statement (Loc,
7346 Return_Object_Declarations => New_List (Obj_Decl));
7347 -- Do not perform this high-level optimization if the result type
7348 -- is an interface because the "this" pointer must be displaced.
7349
7350 begin
7351 Rewrite (N, Ext);
7352 Analyze (N);
7353 return;
7354 end;
7355 end if;
7356
7357 -- Here we have a simple return statement that is part of the expansion
7358 -- of an extended return statement (either written by the user, or
7359 -- generated by the above code).
7360
7361 -- Always normalize C/Fortran boolean result. This is not always needed,
7362 -- but it seems a good idea to minimize the passing around of non-
7363 -- normalized values, and in any case this handles the processing of
7364 -- barrier functions for protected types, which turn the condition into
7365 -- a return statement.
7366
7367 if Is_Boolean_Type (Exptyp)
7368 and then Nonzero_Is_True (Exptyp)
7369 then
7370 Adjust_Condition (Exp);
7371 Adjust_Result_Type (Exp, Exptyp);
7372 end if;
7373
7374 -- Do validity check if enabled for returns
7375
7376 if Validity_Checks_On
7377 and then Validity_Check_Returns
7378 then
7379 Ensure_Valid (Exp);
7380 end if;
7381
7382 -- Check the result expression of a scalar function against the subtype
7383 -- of the function by inserting a conversion. This conversion must
7384 -- eventually be performed for other classes of types, but for now it's
7385 -- only done for scalars.
7386 -- ???
7387
7388 if Is_Scalar_Type (Exptyp) then
7389 Rewrite (Exp, Convert_To (R_Type, Exp));
7390
7391 -- The expression is resolved to ensure that the conversion gets
7392 -- expanded to generate a possible constraint check.
7393
7394 Analyze_And_Resolve (Exp, R_Type);
7395 end if;
7396
7397 -- Deal with returning variable length objects and controlled types
7398
7399 -- Nothing to do if we are returning by reference, or this is not a
7400 -- type that requires special processing (indicated by the fact that
7401 -- it requires a cleanup scope for the secondary stack case).
7402
7403 if Is_Limited_View (Exptyp)
7404 or else Is_Limited_Interface (Exptyp)
7405 then
7406 null;
7407
7408 -- No copy needed for thunks returning interface type objects since
7409 -- the object is returned by reference and the maximum functionality
7410 -- required is just to displace the pointer.
7411
7412 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
7413 null;
7414
7415 elsif not Requires_Transient_Scope (R_Type) then
7416
7417 -- Mutable records with no variable length components are not
7418 -- returned on the sec-stack, so we need to make sure that the
7419 -- backend will only copy back the size of the actual value, and not
7420 -- the maximum size. We create an actual subtype for this purpose.
7421
7422 declare
7423 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
7424 Decl : Node_Id;
7425 Ent : Entity_Id;
7426 begin
7427 if Has_Discriminants (Ubt)
7428 and then not Is_Constrained (Ubt)
7429 and then not Has_Unchecked_Union (Ubt)
7430 then
7431 Decl := Build_Actual_Subtype (Ubt, Exp);
7432 Ent := Defining_Identifier (Decl);
7433 Insert_Action (Exp, Decl);
7434 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7435 Analyze_And_Resolve (Exp);
7436 end if;
7437 end;
7438
7439 -- Here if secondary stack is used
7440
7441 else
7442 -- Make sure that no surrounding block will reclaim the secondary
7443 -- stack on which we are going to put the result. Not only may this
7444 -- introduce secondary stack leaks but worse, if the reclamation is
7445 -- done too early, then the result we are returning may get
7446 -- clobbered.
7447
7448 declare
7449 S : Entity_Id;
7450 begin
7451 S := Current_Scope;
7452 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
7453 Set_Sec_Stack_Needed_For_Return (S, True);
7454 S := Enclosing_Dynamic_Scope (S);
7455 end loop;
7456
7457 -- The enclosing function itself must be marked as well, to
7458 -- prevent premature secondary stack cleanup.
7459
7460 if Ekind (S) = E_Function then
7461 Set_Sec_Stack_Needed_For_Return (Scope_Id);
7462 end if;
7463 end;
7464
7465 -- Optimize the case where the result is a function call. In this
7466 -- case either the result is already on the secondary stack, or is
7467 -- already being returned with the stack pointer depressed and no
7468 -- further processing is required except to set the By_Ref flag
7469 -- to ensure that gigi does not attempt an extra unnecessary copy.
7470 -- (actually not just unnecessary but harmfully wrong in the case
7471 -- of a controlled type, where gigi does not know how to do a copy).
7472 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7473 -- for array types if the constrained status of the target type is
7474 -- different from that of the expression.
7475
7476 if Requires_Transient_Scope (Exptyp)
7477 and then
7478 (not Is_Array_Type (Exptyp)
7479 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
7480 or else CW_Or_Has_Controlled_Part (Utyp))
7481 and then Nkind (Exp) = N_Function_Call
7482 then
7483 Set_By_Ref (N);
7484
7485 -- Remove side effects from the expression now so that other parts
7486 -- of the expander do not have to reanalyze this node without this
7487 -- optimization
7488
7489 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7490
7491 -- For controlled types, do the allocation on the secondary stack
7492 -- manually in order to call adjust at the right time:
7493
7494 -- type Anon1 is access R_Type;
7495 -- for Anon1'Storage_pool use ss_pool;
7496 -- Anon2 : anon1 := new R_Type'(expr);
7497 -- return Anon2.all;
7498
7499 -- We do the same for classwide types that are not potentially
7500 -- controlled (by the virtue of restriction No_Finalization) because
7501 -- gigi is not able to properly allocate class-wide types.
7502
7503 elsif CW_Or_Has_Controlled_Part (Utyp) then
7504 declare
7505 Loc : constant Source_Ptr := Sloc (N);
7506 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7507 Alloc_Node : Node_Id;
7508 Temp : Entity_Id;
7509
7510 begin
7511 Set_Ekind (Acc_Typ, E_Access_Type);
7512
7513 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7514
7515 -- This is an allocator for the secondary stack, and it's fine
7516 -- to have Comes_From_Source set False on it, as gigi knows not
7517 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7518
7519 Alloc_Node :=
7520 Make_Allocator (Loc,
7521 Expression =>
7522 Make_Qualified_Expression (Loc,
7523 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
7524 Expression => Relocate_Node (Exp)));
7525
7526 -- We do not want discriminant checks on the declaration,
7527 -- given that it gets its value from the allocator.
7528
7529 Set_No_Initialization (Alloc_Node);
7530
7531 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7532
7533 Insert_List_Before_And_Analyze (N, New_List (
7534 Make_Full_Type_Declaration (Loc,
7535 Defining_Identifier => Acc_Typ,
7536 Type_Definition =>
7537 Make_Access_To_Object_Definition (Loc,
7538 Subtype_Indication => Subtype_Ind)),
7539
7540 Make_Object_Declaration (Loc,
7541 Defining_Identifier => Temp,
7542 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
7543 Expression => Alloc_Node)));
7544
7545 Rewrite (Exp,
7546 Make_Explicit_Dereference (Loc,
7547 Prefix => New_Occurrence_Of (Temp, Loc)));
7548
7549 -- Ada 2005 (AI-251): If the type of the returned object is
7550 -- an interface then add an implicit type conversion to force
7551 -- displacement of the "this" pointer.
7552
7553 if Is_Interface (R_Type) then
7554 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7555 end if;
7556
7557 Analyze_And_Resolve (Exp, R_Type);
7558 end;
7559
7560 -- Otherwise use the gigi mechanism to allocate result on the
7561 -- secondary stack.
7562
7563 else
7564 Check_Restriction (No_Secondary_Stack, N);
7565 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7566
7567 -- If we are generating code for the VM do not use
7568 -- SS_Allocate since everything is heap-allocated anyway.
7569
7570 if VM_Target = No_VM then
7571 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7572 end if;
7573 end if;
7574 end if;
7575
7576 -- Implement the rules of 6.5(8-10), which require a tag check in
7577 -- the case of a limited tagged return type, and tag reassignment for
7578 -- nonlimited tagged results. These actions are needed when the return
7579 -- type is a specific tagged type and the result expression is a
7580 -- conversion or a formal parameter, because in that case the tag of
7581 -- the expression might differ from the tag of the specific result type.
7582
7583 if Is_Tagged_Type (Utyp)
7584 and then not Is_Class_Wide_Type (Utyp)
7585 and then (Nkind_In (Exp, N_Type_Conversion,
7586 N_Unchecked_Type_Conversion)
7587 or else (Is_Entity_Name (Exp)
7588 and then Ekind (Entity (Exp)) in Formal_Kind))
7589 then
7590 -- When the return type is limited, perform a check that the tag of
7591 -- the result is the same as the tag of the return type.
7592
7593 if Is_Limited_Type (R_Type) then
7594 Insert_Action (Exp,
7595 Make_Raise_Constraint_Error (Loc,
7596 Condition =>
7597 Make_Op_Ne (Loc,
7598 Left_Opnd =>
7599 Make_Selected_Component (Loc,
7600 Prefix => Duplicate_Subexpr (Exp),
7601 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7602 Right_Opnd =>
7603 Make_Attribute_Reference (Loc,
7604 Prefix =>
7605 New_Occurrence_Of (Base_Type (Utyp), Loc),
7606 Attribute_Name => Name_Tag)),
7607 Reason => CE_Tag_Check_Failed));
7608
7609 -- If the result type is a specific nonlimited tagged type, then we
7610 -- have to ensure that the tag of the result is that of the result
7611 -- type. This is handled by making a copy of the expression in
7612 -- the case where it might have a different tag, namely when the
7613 -- expression is a conversion or a formal parameter. We create a new
7614 -- object of the result type and initialize it from the expression,
7615 -- which will implicitly force the tag to be set appropriately.
7616
7617 else
7618 declare
7619 ExpR : constant Node_Id := Relocate_Node (Exp);
7620 Result_Id : constant Entity_Id :=
7621 Make_Temporary (Loc, 'R', ExpR);
7622 Result_Exp : constant Node_Id :=
7623 New_Occurrence_Of (Result_Id, Loc);
7624 Result_Obj : constant Node_Id :=
7625 Make_Object_Declaration (Loc,
7626 Defining_Identifier => Result_Id,
7627 Object_Definition =>
7628 New_Occurrence_Of (R_Type, Loc),
7629 Constant_Present => True,
7630 Expression => ExpR);
7631
7632 begin
7633 Set_Assignment_OK (Result_Obj);
7634 Insert_Action (Exp, Result_Obj);
7635
7636 Rewrite (Exp, Result_Exp);
7637 Analyze_And_Resolve (Exp, R_Type);
7638 end;
7639 end if;
7640
7641 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
7642 -- a check that the level of the return expression's underlying type
7643 -- is not deeper than the level of the master enclosing the function.
7644 -- Always generate the check when the type of the return expression
7645 -- is class-wide, when it's a type conversion, or when it's a formal
7646 -- parameter. Otherwise, suppress the check in the case where the
7647 -- return expression has a specific type whose level is known not to
7648 -- be statically deeper than the function's result type.
7649
7650 -- No runtime check needed in interface thunks since it is performed
7651 -- by the target primitive associated with the thunk.
7652
7653 -- Note: accessibility check is skipped in the VM case, since there
7654 -- does not seem to be any practical way to implement this check.
7655
7656 elsif Ada_Version >= Ada_2005
7657 and then Tagged_Type_Expansion
7658 and then Is_Class_Wide_Type (R_Type)
7659 and then not Is_Thunk (Current_Scope)
7660 and then not Scope_Suppress.Suppress (Accessibility_Check)
7661 and then
7662 (Is_Class_Wide_Type (Etype (Exp))
7663 or else Nkind_In (Exp, N_Type_Conversion,
7664 N_Unchecked_Type_Conversion)
7665 or else (Is_Entity_Name (Exp)
7666 and then Ekind (Entity (Exp)) in Formal_Kind)
7667 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
7668 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
7669 then
7670 declare
7671 Tag_Node : Node_Id;
7672
7673 begin
7674 -- Ada 2005 (AI-251): In class-wide interface objects we displace
7675 -- "this" to reference the base of the object. This is required to
7676 -- get access to the TSD of the object.
7677
7678 if Is_Class_Wide_Type (Etype (Exp))
7679 and then Is_Interface (Etype (Exp))
7680 and then Nkind (Exp) = N_Explicit_Dereference
7681 then
7682 Tag_Node :=
7683 Make_Explicit_Dereference (Loc,
7684 Prefix =>
7685 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7686 Make_Function_Call (Loc,
7687 Name =>
7688 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
7689 Parameter_Associations => New_List (
7690 Unchecked_Convert_To (RTE (RE_Address),
7691 Duplicate_Subexpr (Prefix (Exp)))))));
7692 else
7693 Tag_Node :=
7694 Make_Attribute_Reference (Loc,
7695 Prefix => Duplicate_Subexpr (Exp),
7696 Attribute_Name => Name_Tag);
7697 end if;
7698
7699 Insert_Action (Exp,
7700 Make_Raise_Program_Error (Loc,
7701 Condition =>
7702 Make_Op_Gt (Loc,
7703 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
7704 Right_Opnd =>
7705 Make_Integer_Literal (Loc,
7706 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
7707 Reason => PE_Accessibility_Check_Failed));
7708 end;
7709
7710 -- AI05-0073: If function has a controlling access result, check that
7711 -- the tag of the return value, if it is not null, matches designated
7712 -- type of return type.
7713
7714 -- The return expression is referenced twice in the code below, so it
7715 -- must be made free of side effects. Given that different compilers
7716 -- may evaluate these parameters in different order, both occurrences
7717 -- perform a copy.
7718
7719 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7720 and then Has_Controlling_Result (Scope_Id)
7721 then
7722 Insert_Action (N,
7723 Make_Raise_Constraint_Error (Loc,
7724 Condition =>
7725 Make_And_Then (Loc,
7726 Left_Opnd =>
7727 Make_Op_Ne (Loc,
7728 Left_Opnd => Duplicate_Subexpr (Exp),
7729 Right_Opnd => Make_Null (Loc)),
7730
7731 Right_Opnd => Make_Op_Ne (Loc,
7732 Left_Opnd =>
7733 Make_Selected_Component (Loc,
7734 Prefix => Duplicate_Subexpr (Exp),
7735 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7736
7737 Right_Opnd =>
7738 Make_Attribute_Reference (Loc,
7739 Prefix =>
7740 New_Occurrence_Of (Designated_Type (R_Type), Loc),
7741 Attribute_Name => Name_Tag))),
7742
7743 Reason => CE_Tag_Check_Failed),
7744 Suppress => All_Checks);
7745 end if;
7746
7747 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
7748 -- ensure that the function result does not outlive an
7749 -- object designated by one of it discriminants.
7750
7751 if Present (Extra_Accessibility_Of_Result (Scope_Id))
7752 and then Has_Unconstrained_Access_Discriminants (R_Type)
7753 then
7754 declare
7755 Discrim_Source : Node_Id;
7756
7757 procedure Check_Against_Result_Level (Level : Node_Id);
7758 -- Check the given accessibility level against the level
7759 -- determined by the point of call. (AI05-0234).
7760
7761 --------------------------------
7762 -- Check_Against_Result_Level --
7763 --------------------------------
7764
7765 procedure Check_Against_Result_Level (Level : Node_Id) is
7766 begin
7767 Insert_Action (N,
7768 Make_Raise_Program_Error (Loc,
7769 Condition =>
7770 Make_Op_Gt (Loc,
7771 Left_Opnd => Level,
7772 Right_Opnd =>
7773 New_Occurrence_Of
7774 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
7775 Reason => PE_Accessibility_Check_Failed));
7776 end Check_Against_Result_Level;
7777
7778 begin
7779 Discrim_Source := Exp;
7780 while Nkind (Discrim_Source) = N_Qualified_Expression loop
7781 Discrim_Source := Expression (Discrim_Source);
7782 end loop;
7783
7784 if Nkind (Discrim_Source) = N_Identifier
7785 and then Is_Return_Object (Entity (Discrim_Source))
7786 then
7787 Discrim_Source := Entity (Discrim_Source);
7788
7789 if Is_Constrained (Etype (Discrim_Source)) then
7790 Discrim_Source := Etype (Discrim_Source);
7791 else
7792 Discrim_Source := Expression (Parent (Discrim_Source));
7793 end if;
7794
7795 elsif Nkind (Discrim_Source) = N_Identifier
7796 and then Nkind_In (Original_Node (Discrim_Source),
7797 N_Aggregate, N_Extension_Aggregate)
7798 then
7799 Discrim_Source := Original_Node (Discrim_Source);
7800
7801 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
7802 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
7803 then
7804 Discrim_Source := Original_Node (Discrim_Source);
7805 end if;
7806
7807 while Nkind_In (Discrim_Source, N_Qualified_Expression,
7808 N_Type_Conversion,
7809 N_Unchecked_Type_Conversion)
7810 loop
7811 Discrim_Source := Expression (Discrim_Source);
7812 end loop;
7813
7814 case Nkind (Discrim_Source) is
7815 when N_Defining_Identifier =>
7816
7817 pragma Assert (Is_Composite_Type (Discrim_Source)
7818 and then Has_Discriminants (Discrim_Source)
7819 and then Is_Constrained (Discrim_Source));
7820
7821 declare
7822 Discrim : Entity_Id :=
7823 First_Discriminant (Base_Type (R_Type));
7824 Disc_Elmt : Elmt_Id :=
7825 First_Elmt (Discriminant_Constraint
7826 (Discrim_Source));
7827 begin
7828 loop
7829 if Ekind (Etype (Discrim)) =
7830 E_Anonymous_Access_Type
7831 then
7832 Check_Against_Result_Level
7833 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7834 end if;
7835
7836 Next_Elmt (Disc_Elmt);
7837 Next_Discriminant (Discrim);
7838 exit when not Present (Discrim);
7839 end loop;
7840 end;
7841
7842 when N_Aggregate | N_Extension_Aggregate =>
7843
7844 -- Unimplemented: extension aggregate case where discrims
7845 -- come from ancestor part, not extension part.
7846
7847 declare
7848 Discrim : Entity_Id :=
7849 First_Discriminant (Base_Type (R_Type));
7850
7851 Disc_Exp : Node_Id := Empty;
7852
7853 Positionals_Exhausted
7854 : Boolean := not Present (Expressions
7855 (Discrim_Source));
7856
7857 function Associated_Expr
7858 (Comp_Id : Entity_Id;
7859 Associations : List_Id) return Node_Id;
7860
7861 -- Given a component and a component associations list,
7862 -- locate the expression for that component; returns
7863 -- Empty if no such expression is found.
7864
7865 ---------------------
7866 -- Associated_Expr --
7867 ---------------------
7868
7869 function Associated_Expr
7870 (Comp_Id : Entity_Id;
7871 Associations : List_Id) return Node_Id
7872 is
7873 Assoc : Node_Id;
7874 Choice : Node_Id;
7875
7876 begin
7877 -- Simple linear search seems ok here
7878
7879 Assoc := First (Associations);
7880 while Present (Assoc) loop
7881 Choice := First (Choices (Assoc));
7882 while Present (Choice) loop
7883 if (Nkind (Choice) = N_Identifier
7884 and then Chars (Choice) = Chars (Comp_Id))
7885 or else (Nkind (Choice) = N_Others_Choice)
7886 then
7887 return Expression (Assoc);
7888 end if;
7889
7890 Next (Choice);
7891 end loop;
7892
7893 Next (Assoc);
7894 end loop;
7895
7896 return Empty;
7897 end Associated_Expr;
7898
7899 -- Start of processing for Expand_Simple_Function_Return
7900
7901 begin
7902 if not Positionals_Exhausted then
7903 Disc_Exp := First (Expressions (Discrim_Source));
7904 end if;
7905
7906 loop
7907 if Positionals_Exhausted then
7908 Disc_Exp :=
7909 Associated_Expr
7910 (Discrim,
7911 Component_Associations (Discrim_Source));
7912 end if;
7913
7914 if Ekind (Etype (Discrim)) =
7915 E_Anonymous_Access_Type
7916 then
7917 Check_Against_Result_Level
7918 (Dynamic_Accessibility_Level (Disc_Exp));
7919 end if;
7920
7921 Next_Discriminant (Discrim);
7922 exit when not Present (Discrim);
7923
7924 if not Positionals_Exhausted then
7925 Next (Disc_Exp);
7926 Positionals_Exhausted := not Present (Disc_Exp);
7927 end if;
7928 end loop;
7929 end;
7930
7931 when N_Function_Call =>
7932
7933 -- No check needed (check performed by callee)
7934
7935 null;
7936
7937 when others =>
7938
7939 declare
7940 Level : constant Node_Id :=
7941 Make_Integer_Literal (Loc,
7942 Object_Access_Level (Discrim_Source));
7943
7944 begin
7945 -- Unimplemented: check for name prefix that includes
7946 -- a dereference of an access value with a dynamic
7947 -- accessibility level (e.g., an access param or a
7948 -- saooaaat) and use dynamic level in that case. For
7949 -- example:
7950 -- return Access_Param.all(Some_Index).Some_Component;
7951 -- ???
7952
7953 Set_Etype (Level, Standard_Natural);
7954 Check_Against_Result_Level (Level);
7955 end;
7956
7957 end case;
7958 end;
7959 end if;
7960
7961 -- If we are returning an object that may not be bit-aligned, then copy
7962 -- the value into a temporary first. This copy may need to expand to a
7963 -- loop of component operations.
7964
7965 if Is_Possibly_Unaligned_Slice (Exp)
7966 or else Is_Possibly_Unaligned_Object (Exp)
7967 then
7968 declare
7969 ExpR : constant Node_Id := Relocate_Node (Exp);
7970 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7971 begin
7972 Insert_Action (Exp,
7973 Make_Object_Declaration (Loc,
7974 Defining_Identifier => Tnn,
7975 Constant_Present => True,
7976 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7977 Expression => ExpR),
7978 Suppress => All_Checks);
7979 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7980 end;
7981 end if;
7982
7983 -- Generate call to postcondition checks if they are present
7984
7985 if Ekind (Scope_Id) = E_Function
7986 and then Has_Postconditions (Scope_Id)
7987 then
7988 -- We are going to reference the returned value twice in this case,
7989 -- once in the call to _Postconditions, and once in the actual return
7990 -- statement, but we can't have side effects happening twice, and in
7991 -- any case for efficiency we don't want to do the computation twice.
7992
7993 -- If the returned expression is an entity name, we don't need to
7994 -- worry since it is efficient and safe to reference it twice, that's
7995 -- also true for literals other than string literals, and for the
7996 -- case of X.all where X is an entity name.
7997
7998 if Is_Entity_Name (Exp)
7999 or else Nkind_In (Exp, N_Character_Literal,
8000 N_Integer_Literal,
8001 N_Real_Literal)
8002 or else (Nkind (Exp) = N_Explicit_Dereference
8003 and then Is_Entity_Name (Prefix (Exp)))
8004 then
8005 null;
8006
8007 -- Otherwise we are going to need a temporary to capture the value
8008
8009 else
8010 declare
8011 ExpR : Node_Id := Relocate_Node (Exp);
8012 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
8013
8014 begin
8015 -- In the case of discriminated objects, we have created a
8016 -- constrained subtype above, and used the underlying type.
8017 -- This transformation is post-analysis and harmless, except
8018 -- that now the call to the post-condition will be analyzed and
8019 -- type kinds have to match.
8020
8021 if Nkind (ExpR) = N_Unchecked_Type_Conversion
8022 and then
8023 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
8024 then
8025 ExpR := Expression (ExpR);
8026 end if;
8027
8028 -- For a complex expression of an elementary type, capture
8029 -- value in the temporary and use it as the reference.
8030
8031 if Is_Elementary_Type (R_Type) then
8032 Insert_Action (Exp,
8033 Make_Object_Declaration (Loc,
8034 Defining_Identifier => Tnn,
8035 Constant_Present => True,
8036 Object_Definition => New_Occurrence_Of (R_Type, Loc),
8037 Expression => ExpR),
8038 Suppress => All_Checks);
8039
8040 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8041
8042 -- If we have something we can rename, generate a renaming of
8043 -- the object and replace the expression with a reference
8044
8045 elsif Is_Object_Reference (Exp) then
8046 Insert_Action (Exp,
8047 Make_Object_Renaming_Declaration (Loc,
8048 Defining_Identifier => Tnn,
8049 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
8050 Name => ExpR),
8051 Suppress => All_Checks);
8052
8053 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8054
8055 -- Otherwise we have something like a string literal or an
8056 -- aggregate. We could copy the value, but that would be
8057 -- inefficient. Instead we make a reference to the value and
8058 -- capture this reference with a renaming, the expression is
8059 -- then replaced by a dereference of this renaming.
8060
8061 else
8062 -- For now, copy the value, since the code below does not
8063 -- seem to work correctly ???
8064
8065 Insert_Action (Exp,
8066 Make_Object_Declaration (Loc,
8067 Defining_Identifier => Tnn,
8068 Constant_Present => True,
8069 Object_Definition => New_Occurrence_Of (R_Type, Loc),
8070 Expression => Relocate_Node (Exp)),
8071 Suppress => All_Checks);
8072
8073 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8074
8075 -- Insert_Action (Exp,
8076 -- Make_Object_Renaming_Declaration (Loc,
8077 -- Defining_Identifier => Tnn,
8078 -- Access_Definition =>
8079 -- Make_Access_Definition (Loc,
8080 -- All_Present => True,
8081 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
8082 -- Name =>
8083 -- Make_Reference (Loc,
8084 -- Prefix => Relocate_Node (Exp))),
8085 -- Suppress => All_Checks);
8086
8087 -- Rewrite (Exp,
8088 -- Make_Explicit_Dereference (Loc,
8089 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
8090 end if;
8091 end;
8092 end if;
8093
8094 -- Generate call to _postconditions
8095
8096 Insert_Action (Exp,
8097 Make_Procedure_Call_Statement (Loc,
8098 Name => Make_Identifier (Loc, Name_uPostconditions),
8099 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
8100 end if;
8101
8102 -- Ada 2005 (AI-251): If this return statement corresponds with an
8103 -- simple return statement associated with an extended return statement
8104 -- and the type of the returned object is an interface then generate an
8105 -- implicit conversion to force displacement of the "this" pointer.
8106
8107 if Ada_Version >= Ada_2005
8108 and then Comes_From_Extended_Return_Statement (N)
8109 and then Nkind (Expression (N)) = N_Identifier
8110 and then Is_Interface (Utyp)
8111 and then Utyp /= Underlying_Type (Exptyp)
8112 then
8113 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
8114 Analyze_And_Resolve (Exp);
8115 end if;
8116 end Expand_Simple_Function_Return;
8117
8118 --------------------------------
8119 -- Expand_Subprogram_Contract --
8120 --------------------------------
8121
8122 procedure Expand_Subprogram_Contract
8123 (N : Node_Id;
8124 Spec_Id : Entity_Id;
8125 Body_Id : Entity_Id)
8126 is
8127 procedure Add_Invariant_And_Predicate_Checks
8128 (Subp_Id : Entity_Id;
8129 Stmts : in out List_Id;
8130 Result : out Node_Id);
8131 -- Process the result of function Subp_Id (if applicable) and all its
8132 -- formals. Add invariant and predicate checks where applicable. The
8133 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
8134 -- function, Result contains the entity of parameter _Result, to be
8135 -- used in the creation of procedure _Postconditions.
8136
8137 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
8138 -- Append a node to a list. If there is no list, create a new one. When
8139 -- the item denotes a pragma, it is added to the list only when it is
8140 -- enabled.
8141
8142 procedure Build_Postconditions_Procedure
8143 (Subp_Id : Entity_Id;
8144 Stmts : List_Id;
8145 Result : Entity_Id);
8146 -- Create the body of procedure _Postconditions which handles various
8147 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
8148 -- of statements to be checked on exit. Parameter Result is the entity
8149 -- of parameter _Result when Subp_Id denotes a function.
8150
8151 function Build_Pragma_Check_Equivalent
8152 (Prag : Node_Id;
8153 Subp_Id : Entity_Id := Empty;
8154 Inher_Id : Entity_Id := Empty) return Node_Id;
8155 -- Transform a [refined] pre- or postcondition denoted by Prag into an
8156 -- equivalent pragma Check. When the pre- or postcondition is inherited,
8157 -- the routine corrects the references of all formals of Inher_Id to
8158 -- point to the formals of Subp_Id.
8159
8160 procedure Collect_Body_Postconditions (Stmts : in out List_Id);
8161 -- Process all postconditions found in the declarations of the body. The
8162 -- routine appends the pragma Check equivalents to list Stmts.
8163
8164 procedure Collect_Spec_Postconditions
8165 (Subp_Id : Entity_Id;
8166 Stmts : in out List_Id);
8167 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
8168 -- The routine appends the pragma Check equivalents to list Stmts.
8169
8170 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id);
8171 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
8172 -- routine prepends the pragma Check equivalents to the declarations of
8173 -- the body.
8174
8175 procedure Prepend_To_Declarations (Item : Node_Id);
8176 -- Prepend a single item to the declarations of the subprogram body
8177
8178 procedure Process_Contract_Cases
8179 (Subp_Id : Entity_Id;
8180 Stmts : in out List_Id);
8181 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
8182 -- appends the expanded code to list Stmts.
8183
8184 ----------------------------------------
8185 -- Add_Invariant_And_Predicate_Checks --
8186 ----------------------------------------
8187
8188 procedure Add_Invariant_And_Predicate_Checks
8189 (Subp_Id : Entity_Id;
8190 Stmts : in out List_Id;
8191 Result : out Node_Id)
8192 is
8193 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
8194 -- Id denotes the return value of a function or a formal parameter.
8195 -- Add an invariant check if the type of Id is access to a type with
8196 -- invariants. The routine appends the generated code to Stmts.
8197
8198 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
8199 -- Determine whether type Typ can benefit from invariant checks. To
8200 -- qualify, the type must have a non-null invariant procedure and
8201 -- subprogram Subp_Id must appear visible from the point of view of
8202 -- the type.
8203
8204 function Predicate_Checks_OK (Typ : Entity_Id) return Boolean;
8205 -- Determine whether type Typ can benefit from predicate checks. To
8206 -- qualify, the type must have at least one checked predicate.
8207
8208 ---------------------------------
8209 -- Add_Invariant_Access_Checks --
8210 ---------------------------------
8211
8212 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
8213 Loc : constant Source_Ptr := Sloc (N);
8214 Ref : Node_Id;
8215 Typ : Entity_Id;
8216
8217 begin
8218 Typ := Etype (Id);
8219
8220 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
8221 Typ := Designated_Type (Typ);
8222
8223 if Invariant_Checks_OK (Typ) then
8224 Ref :=
8225 Make_Explicit_Dereference (Loc,
8226 Prefix => New_Occurrence_Of (Id, Loc));
8227 Set_Etype (Ref, Typ);
8228
8229 -- Generate:
8230 -- if <Id> /= null then
8231 -- <invariant_call (<Ref>)>
8232 -- end if;
8233
8234 Append_Enabled_Item
8235 (Item =>
8236 Make_If_Statement (Loc,
8237 Condition =>
8238 Make_Op_Ne (Loc,
8239 Left_Opnd => New_Occurrence_Of (Id, Loc),
8240 Right_Opnd => Make_Null (Loc)),
8241 Then_Statements => New_List (
8242 Make_Invariant_Call (Ref))),
8243 List => Stmts);
8244 end if;
8245 end if;
8246 end Add_Invariant_Access_Checks;
8247
8248 -------------------------
8249 -- Invariant_Checks_OK --
8250 -------------------------
8251
8252 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
8253 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
8254 -- Determine whether the body of procedure Proc_Id contains a sole
8255 -- null statement, possibly followed by an optional return.
8256
8257 function Has_Public_Visibility_Of_Subprogram return Boolean;
8258 -- Determine whether type Typ has public visibility of subprogram
8259 -- Subp_Id.
8260
8261 -------------------
8262 -- Has_Null_Body --
8263 -------------------
8264
8265 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
8266 Body_Id : Entity_Id;
8267 Decl : Node_Id;
8268 Spec : Node_Id;
8269 Stmt1 : Node_Id;
8270 Stmt2 : Node_Id;
8271
8272 begin
8273 Spec := Parent (Proc_Id);
8274 Decl := Parent (Spec);
8275
8276 -- Retrieve the entity of the invariant procedure body
8277
8278 if Nkind (Spec) = N_Procedure_Specification
8279 and then Nkind (Decl) = N_Subprogram_Declaration
8280 then
8281 Body_Id := Corresponding_Body (Decl);
8282
8283 -- The body acts as a spec
8284
8285 else
8286 Body_Id := Proc_Id;
8287 end if;
8288
8289 -- The body will be generated later
8290
8291 if No (Body_Id) then
8292 return False;
8293 end if;
8294
8295 Spec := Parent (Body_Id);
8296 Decl := Parent (Spec);
8297
8298 pragma Assert
8299 (Nkind (Spec) = N_Procedure_Specification
8300 and then Nkind (Decl) = N_Subprogram_Body);
8301
8302 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
8303
8304 -- Look for a null statement followed by an optional return
8305 -- statement.
8306
8307 if Nkind (Stmt1) = N_Null_Statement then
8308 Stmt2 := Next (Stmt1);
8309
8310 if Present (Stmt2) then
8311 return Nkind (Stmt2) = N_Simple_Return_Statement;
8312 else
8313 return True;
8314 end if;
8315 end if;
8316
8317 return False;
8318 end Has_Null_Body;
8319
8320 -----------------------------------------
8321 -- Has_Public_Visibility_Of_Subprogram --
8322 -----------------------------------------
8323
8324 function Has_Public_Visibility_Of_Subprogram return Boolean is
8325 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
8326
8327 begin
8328 -- An Initialization procedure must be considered visible even
8329 -- though it is internally generated.
8330
8331 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
8332 return True;
8333
8334 elsif Ekind (Scope (Typ)) /= E_Package then
8335 return False;
8336
8337 -- Internally generated code is never publicly visible except
8338 -- for a subprogram that is the implementation of an expression
8339 -- function. In that case the visibility is determined by the
8340 -- last check.
8341
8342 elsif not Comes_From_Source (Subp_Decl)
8343 and then
8344 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
8345 or else not
8346 Comes_From_Source (Defining_Entity (Subp_Decl)))
8347 then
8348 return False;
8349
8350 -- Determine whether the subprogram is declared in the visible
8351 -- declarations of the package containing the type.
8352
8353 else
8354 return List_Containing (Subp_Decl) =
8355 Visible_Declarations
8356 (Specification (Unit_Declaration_Node (Scope (Typ))));
8357 end if;
8358 end Has_Public_Visibility_Of_Subprogram;
8359
8360 -- Start of processing for Invariant_Checks_OK
8361
8362 begin
8363 return
8364 Has_Invariants (Typ)
8365 and then Present (Invariant_Procedure (Typ))
8366 and then not Has_Null_Body (Invariant_Procedure (Typ))
8367 and then Has_Public_Visibility_Of_Subprogram;
8368 end Invariant_Checks_OK;
8369
8370 -------------------------
8371 -- Predicate_Checks_OK --
8372 -------------------------
8373
8374 function Predicate_Checks_OK (Typ : Entity_Id) return Boolean is
8375 function Has_Checked_Predicate return Boolean;
8376 -- Determine whether type Typ has or inherits at least one
8377 -- predicate aspect or pragma, for which the applicable policy is
8378 -- Checked.
8379
8380 ---------------------------
8381 -- Has_Checked_Predicate --
8382 ---------------------------
8383
8384 function Has_Checked_Predicate return Boolean is
8385 Anc : Entity_Id;
8386 Pred : Node_Id;
8387
8388 begin
8389 -- Climb the ancestor type chain staring from the input. This
8390 -- is done because the input type may lack aspect/pragma
8391 -- predicate and simply inherit those from its ancestor.
8392
8393 -- Note that predicate pragmas correspond to all three cases
8394 -- of predicate aspects (Predicate, Dynamic_Predicate, and
8395 -- Static_Predicate), so this routine checks for all three
8396 -- cases.
8397
8398 Anc := Typ;
8399 while Present (Anc) loop
8400 Pred := Get_Pragma (Anc, Pragma_Predicate);
8401
8402 if Present (Pred) and then not Is_Ignored (Pred) then
8403 return True;
8404 end if;
8405
8406 Anc := Nearest_Ancestor (Anc);
8407 end loop;
8408
8409 return False;
8410 end Has_Checked_Predicate;
8411
8412 -- Start of processing for Predicate_Checks_OK
8413
8414 begin
8415 return
8416 Has_Predicates (Typ)
8417 and then Present (Predicate_Function (Typ))
8418 and then Has_Checked_Predicate;
8419 end Predicate_Checks_OK;
8420
8421 -- Local variables
8422
8423 Loc : constant Source_Ptr := Sloc (N);
8424 -- Source location of subprogram contract
8425
8426 Formal : Entity_Id;
8427 Typ : Entity_Id;
8428
8429 -- Start of processing for Add_Invariant_And_Predicate_Checks
8430
8431 begin
8432 Result := Empty;
8433
8434 -- Do not generate any checks if no code is being generated
8435
8436 if not Expander_Active then
8437 return;
8438 end if;
8439
8440 -- Process the result of a function
8441
8442 if Ekind_In (Subp_Id, E_Function, E_Generic_Function) then
8443 Typ := Etype (Subp_Id);
8444
8445 -- Generate _Result which is used in procedure _Postconditions to
8446 -- verify the return value.
8447
8448 Result := Make_Defining_Identifier (Loc, Name_uResult);
8449 Set_Etype (Result, Typ);
8450
8451 -- Add an invariant check when the return type has invariants and
8452 -- the related function is visible to the outside.
8453
8454 if Invariant_Checks_OK (Typ) then
8455 Append_Enabled_Item
8456 (Item =>
8457 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
8458 List => Stmts);
8459 end if;
8460
8461 -- Add an invariant check when the return type is an access to a
8462 -- type with invariants.
8463
8464 Add_Invariant_Access_Checks (Result);
8465 end if;
8466
8467 -- Add invariant and predicates for all formals that qualify
8468
8469 Formal := First_Formal (Subp_Id);
8470 while Present (Formal) loop
8471 Typ := Etype (Formal);
8472
8473 if Ekind (Formal) /= E_In_Parameter
8474 or else Is_Access_Type (Typ)
8475 then
8476 if Invariant_Checks_OK (Typ) then
8477 Append_Enabled_Item
8478 (Item =>
8479 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
8480 List => Stmts);
8481 end if;
8482
8483 Add_Invariant_Access_Checks (Formal);
8484
8485 if Predicate_Checks_OK (Typ) then
8486 Append_Enabled_Item
8487 (Item => Make_Predicate_Check
8488 (Typ, New_Occurrence_Of (Formal, Loc)),
8489 List => Stmts);
8490 end if;
8491 end if;
8492
8493 Next_Formal (Formal);
8494 end loop;
8495 end Add_Invariant_And_Predicate_Checks;
8496
8497 -------------------------
8498 -- Append_Enabled_Item --
8499 -------------------------
8500
8501 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
8502 begin
8503 -- Do not chain ignored or disabled pragmas
8504
8505 if Nkind (Item) = N_Pragma
8506 and then (Is_Ignored (Item) or else Is_Disabled (Item))
8507 then
8508 null;
8509
8510 -- Otherwise, add the item
8511
8512 else
8513 if No (List) then
8514 List := New_List;
8515 end if;
8516
8517 -- If the pragma is a conjunct in a composite postcondition, it
8518 -- has been processed in reverse order. In the postcondition body
8519 -- if must appear before the others.
8520
8521 if Nkind (Item) = N_Pragma
8522 and then From_Aspect_Specification (Item)
8523 and then Split_PPC (Item)
8524 then
8525 Prepend (Item, List);
8526 else
8527 Append (Item, List);
8528 end if;
8529 end if;
8530 end Append_Enabled_Item;
8531
8532 ------------------------------------
8533 -- Build_Postconditions_Procedure --
8534 ------------------------------------
8535
8536 procedure Build_Postconditions_Procedure
8537 (Subp_Id : Entity_Id;
8538 Stmts : List_Id;
8539 Result : Entity_Id)
8540 is
8541 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
8542 -- Insert node Stmt before the first source declaration of the
8543 -- related subprogram's body. If no such declaration exists, Stmt
8544 -- becomes the last declaration.
8545
8546 --------------------------------------------
8547 -- Insert_Before_First_Source_Declaration --
8548 --------------------------------------------
8549
8550 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
8551 Decls : constant List_Id := Declarations (N);
8552 Decl : Node_Id;
8553
8554 begin
8555 -- Inspect the declarations of the related subprogram body looking
8556 -- for the first source declaration.
8557
8558 if Present (Decls) then
8559 Decl := First (Decls);
8560 while Present (Decl) loop
8561 if Comes_From_Source (Decl) then
8562 Insert_Before (Decl, Stmt);
8563 return;
8564 end if;
8565
8566 Next (Decl);
8567 end loop;
8568
8569 -- If we get there, then the subprogram body lacks any source
8570 -- declarations. The body of _Postconditions now acts as the
8571 -- last declaration.
8572
8573 Append (Stmt, Decls);
8574
8575 -- Ensure that the body has a declaration list
8576
8577 else
8578 Set_Declarations (N, New_List (Stmt));
8579 end if;
8580 end Insert_Before_First_Source_Declaration;
8581
8582 -- Local variables
8583
8584 Loc : constant Source_Ptr := Sloc (N);
8585 Params : List_Id := No_List;
8586 Proc_Id : Entity_Id;
8587
8588 -- Start of processing for Build_Postconditions_Procedure
8589
8590 begin
8591 -- Do not create the routine if no code is being generated
8592
8593 if not Expander_Active then
8594 return;
8595
8596 -- Nothing to do if there are no actions to check on exit
8597
8598 elsif No (Stmts) then
8599 return;
8600 end if;
8601
8602 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
8603
8604 -- The related subprogram is a function, create the specification of
8605 -- parameter _Result.
8606
8607 if Present (Result) then
8608 Params := New_List (
8609 Make_Parameter_Specification (Loc,
8610 Defining_Identifier => Result,
8611 Parameter_Type =>
8612 New_Occurrence_Of (Etype (Result), Loc)));
8613 end if;
8614
8615 -- Insert _Postconditions before the first source declaration of the
8616 -- body. This ensures that the body will not cause any premature
8617 -- freezing as it may mention types:
8618
8619 -- procedure Proc (Obj : Array_Typ) is
8620 -- procedure _postconditions is
8621 -- begin
8622 -- ... Obj ...
8623 -- end _postconditions;
8624
8625 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
8626 -- begin
8627
8628 -- In the example above, Obj is of type T but the incorrect placement
8629 -- of _Postconditions will cause a crash in gigi due to an out of
8630 -- order reference. The body of _Postconditions must be placed after
8631 -- the declaration of Temp to preserve correct visibility.
8632
8633 -- Note that we set an explicit End_Label in order to override the
8634 -- sloc of the implicit RETURN statement, and prevent it from
8635 -- inheriting the sloc of one of the postconditions: this would cause
8636 -- confusing debug info to be produced, interfering with coverage
8637 -- analysis tools.
8638
8639 Insert_Before_First_Source_Declaration (
8640 Make_Subprogram_Body (Loc,
8641 Specification =>
8642 Make_Procedure_Specification (Loc,
8643 Defining_Unit_Name => Proc_Id,
8644 Parameter_Specifications => Params),
8645
8646 Declarations => Empty_List,
8647 Handled_Statement_Sequence =>
8648 Make_Handled_Sequence_Of_Statements (Loc,
8649 Statements => Stmts,
8650 End_Label => Make_Identifier (Loc, Chars (Proc_Id)))));
8651
8652 -- Set the attributes of the related subprogram to capture the
8653 -- generated procedure.
8654
8655 if Ekind_In (Subp_Id, E_Generic_Procedure, E_Procedure) then
8656 Set_Postcondition_Proc (Subp_Id, Proc_Id);
8657 end if;
8658
8659 Set_Has_Postconditions (Subp_Id);
8660 end Build_Postconditions_Procedure;
8661
8662 -----------------------------------
8663 -- Build_Pragma_Check_Equivalent --
8664 -----------------------------------
8665
8666 function Build_Pragma_Check_Equivalent
8667 (Prag : Node_Id;
8668 Subp_Id : Entity_Id := Empty;
8669 Inher_Id : Entity_Id := Empty) return Node_Id
8670 is
8671 Loc : constant Source_Ptr := Sloc (Prag);
8672 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
8673 Check_Prag : Node_Id;
8674 Formals_Map : Elist_Id;
8675 Inher_Formal : Entity_Id;
8676 Msg_Arg : Node_Id;
8677 Nam : Name_Id;
8678 Subp_Formal : Entity_Id;
8679
8680 begin
8681 Formals_Map := No_Elist;
8682
8683 -- When the pre- or postcondition is inherited, map the formals of
8684 -- the inherited subprogram to those of the current subprogram.
8685
8686 if Present (Inher_Id) then
8687 pragma Assert (Present (Subp_Id));
8688
8689 Formals_Map := New_Elmt_List;
8690
8691 -- Create a relation <inherited formal> => <subprogram formal>
8692
8693 Inher_Formal := First_Formal (Inher_Id);
8694 Subp_Formal := First_Formal (Subp_Id);
8695 while Present (Inher_Formal) and then Present (Subp_Formal) loop
8696 Append_Elmt (Inher_Formal, Formals_Map);
8697 Append_Elmt (Subp_Formal, Formals_Map);
8698
8699 Next_Formal (Inher_Formal);
8700 Next_Formal (Subp_Formal);
8701 end loop;
8702 end if;
8703
8704 -- Copy the original pragma while performing substitutions (if
8705 -- applicable).
8706
8707 Check_Prag :=
8708 New_Copy_Tree
8709 (Source => Prag,
8710 Map => Formals_Map,
8711 New_Scope => Current_Scope);
8712
8713 -- Mark the pragma as being internally generated and reset the
8714 -- Analyzed flag.
8715
8716 Set_Comes_From_Source (Check_Prag, False);
8717 Set_Analyzed (Check_Prag, False);
8718
8719 -- For a postcondition pragma within a generic, preserve the pragma
8720 -- for later expansion. This is also used when an error was detected,
8721 -- thus setting Expander_Active to False.
8722
8723 if Prag_Nam = Name_Postcondition and then not Expander_Active then
8724 return Check_Prag;
8725 end if;
8726
8727 if Present (Corresponding_Aspect (Prag)) then
8728 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
8729 else
8730 Nam := Prag_Nam;
8731 end if;
8732
8733 -- Convert the copy into pragma Check by correcting the name and
8734 -- adding a check_kind argument.
8735
8736 Set_Pragma_Identifier
8737 (Check_Prag, Make_Identifier (Loc, Name_Check));
8738
8739 Prepend_To (Pragma_Argument_Associations (Check_Prag),
8740 Make_Pragma_Argument_Association (Loc,
8741 Expression => Make_Identifier (Loc, Nam)));
8742
8743 -- Update the error message when the pragma is inherited
8744
8745 if Present (Inher_Id) then
8746 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
8747
8748 if Chars (Msg_Arg) = Name_Message then
8749 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
8750
8751 -- Insert "inherited" to improve the error message
8752
8753 if Name_Buffer (1 .. 8) = "failed p" then
8754 Insert_Str_In_Name_Buffer ("inherited ", 8);
8755 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
8756 end if;
8757 end if;
8758 end if;
8759
8760 return Check_Prag;
8761 end Build_Pragma_Check_Equivalent;
8762
8763 ---------------------------------
8764 -- Collect_Body_Postconditions --
8765 ---------------------------------
8766
8767 procedure Collect_Body_Postconditions (Stmts : in out List_Id) is
8768 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id);
8769 -- Process all postconditions of the kind denoted by Post_Nam
8770
8771 -----------------------------------------
8772 -- Collect_Body_Postconditions_Of_Kind --
8773 -----------------------------------------
8774
8775 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id) is
8776 procedure Collect_Body_Postconditions_In_Decls
8777 (First_Decl : Node_Id);
8778 -- Process all postconditions found in a declarative list starting
8779 -- with declaration First_Decl.
8780
8781 ------------------------------------------
8782 -- Collect_Body_Postconditions_In_Decls --
8783 ------------------------------------------
8784
8785 procedure Collect_Body_Postconditions_In_Decls
8786 (First_Decl : Node_Id)
8787 is
8788 Check_Prag : Node_Id;
8789 Decl : Node_Id;
8790
8791 begin
8792 -- Inspect the declarative list looking for a pragma that
8793 -- matches the desired name.
8794
8795 Decl := First_Decl;
8796 while Present (Decl) loop
8797
8798 -- Note that non-matching pragmas are skipped
8799
8800 if Nkind (Decl) = N_Pragma then
8801 if Pragma_Name (Decl) = Post_Nam then
8802 if not Analyzed (Decl) then
8803 Analyze (Decl);
8804 end if;
8805
8806 Check_Prag := Build_Pragma_Check_Equivalent (Decl);
8807
8808 if Expander_Active then
8809 Append_Enabled_Item
8810 (Item => Check_Prag,
8811 List => Stmts);
8812
8813 -- If analyzing a generic unit, save pragma for later
8814
8815 else
8816 Prepend_To_Declarations (Check_Prag);
8817 end if;
8818 end if;
8819
8820 -- Skip internally generated code
8821
8822 elsif not Comes_From_Source (Decl) then
8823 null;
8824
8825 -- Postcondition pragmas are usually grouped together. There
8826 -- is no need to inspect the whole declarative list.
8827
8828 else
8829 exit;
8830 end if;
8831
8832 Next (Decl);
8833 end loop;
8834 end Collect_Body_Postconditions_In_Decls;
8835
8836 -- Local variables
8837
8838 Unit_Decl : constant Node_Id := Parent (N);
8839
8840 -- Start of processing for Collect_Body_Postconditions_Of_Kind
8841
8842 begin
8843 pragma Assert (Nam_In (Post_Nam, Name_Postcondition,
8844 Name_Refined_Post));
8845
8846 -- Inspect the declarations of the subprogram body looking for a
8847 -- pragma that matches the desired name.
8848
8849 Collect_Body_Postconditions_In_Decls
8850 (First_Decl => First (Declarations (N)));
8851
8852 -- The subprogram body being processed is actually the proper body
8853 -- of a stub with a corresponding spec. The subprogram stub may
8854 -- carry a postcondition pragma in which case it must be taken
8855 -- into account. The pragma appears after the stub.
8856
8857 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
8858 Collect_Body_Postconditions_In_Decls
8859 (First_Decl => Next (Corresponding_Stub (Unit_Decl)));
8860 end if;
8861 end Collect_Body_Postconditions_Of_Kind;
8862
8863 -- Start of processing for Collect_Body_Postconditions
8864
8865 begin
8866 Collect_Body_Postconditions_Of_Kind (Name_Refined_Post);
8867 Collect_Body_Postconditions_Of_Kind (Name_Postcondition);
8868 end Collect_Body_Postconditions;
8869
8870 ---------------------------------
8871 -- Collect_Spec_Postconditions --
8872 ---------------------------------
8873
8874 procedure Collect_Spec_Postconditions
8875 (Subp_Id : Entity_Id;
8876 Stmts : in out List_Id)
8877 is
8878 Inher_Subps : constant Subprogram_List :=
8879 Inherited_Subprograms (Subp_Id);
8880 Check_Prag : Node_Id;
8881 Prag : Node_Id;
8882 Inher_Subp_Id : Entity_Id;
8883
8884 begin
8885 -- Process the contract of the spec
8886
8887 Prag := Pre_Post_Conditions (Contract (Subp_Id));
8888 while Present (Prag) loop
8889 if Pragma_Name (Prag) = Name_Postcondition then
8890 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
8891
8892 if Expander_Active then
8893 Append_Enabled_Item
8894 (Item => Check_Prag,
8895 List => Stmts);
8896
8897 -- When analyzing a generic unit, save the pragma for later
8898
8899 else
8900 Prepend_To_Declarations (Check_Prag);
8901 end if;
8902 end if;
8903
8904 Prag := Next_Pragma (Prag);
8905 end loop;
8906
8907 -- Process the contracts of all inherited subprograms, looking for
8908 -- class-wide postconditions.
8909
8910 for Index in Inher_Subps'Range loop
8911 Inher_Subp_Id := Inher_Subps (Index);
8912
8913 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
8914 while Present (Prag) loop
8915 if Pragma_Name (Prag) = Name_Postcondition
8916 and then Class_Present (Prag)
8917 then
8918 Check_Prag :=
8919 Build_Pragma_Check_Equivalent
8920 (Prag => Prag,
8921 Subp_Id => Subp_Id,
8922 Inher_Id => Inher_Subp_Id);
8923
8924 if Expander_Active then
8925 Append_Enabled_Item
8926 (Item => Check_Prag,
8927 List => Stmts);
8928
8929 -- When analyzing a generic unit, save the pragma for later
8930
8931 else
8932 Prepend_To_Declarations (Check_Prag);
8933 end if;
8934 end if;
8935
8936 Prag := Next_Pragma (Prag);
8937 end loop;
8938 end loop;
8939 end Collect_Spec_Postconditions;
8940
8941 --------------------------------
8942 -- Collect_Spec_Preconditions --
8943 --------------------------------
8944
8945 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id) is
8946 Class_Pre : Node_Id := Empty;
8947 -- The sole class-wide precondition pragma that applies to the
8948 -- subprogram.
8949
8950 procedure Add_Or_Save_Precondition (Prag : Node_Id);
8951 -- Save a class-wide precondition or add a regulat precondition to
8952 -- the declarative list of the body.
8953
8954 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
8955 -- Merge two class-wide preconditions by "or else"-ing them. The
8956 -- changes are accumulated in parameter Into. Update the error
8957 -- message of Into.
8958
8959 ------------------------------
8960 -- Add_Or_Save_Precondition --
8961 ------------------------------
8962
8963 procedure Add_Or_Save_Precondition (Prag : Node_Id) is
8964 Check_Prag : Node_Id;
8965
8966 begin
8967 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
8968
8969 -- Save the sole class-wide precondition (if any) for the next
8970 -- step where it will be merged with inherited preconditions.
8971
8972 if Class_Present (Prag) then
8973 pragma Assert (No (Class_Pre));
8974 Class_Pre := Check_Prag;
8975
8976 -- Accumulate the corresponding Check pragmas to the top of the
8977 -- declarations. Prepending the items ensures that they will be
8978 -- evaluated in their original order.
8979
8980 else
8981 Prepend_To_Declarations (Check_Prag);
8982 end if;
8983 end Add_Or_Save_Precondition;
8984
8985 -------------------------
8986 -- Merge_Preconditions --
8987 -------------------------
8988
8989 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
8990 function Expression_Arg (Prag : Node_Id) return Node_Id;
8991 -- Return the boolean expression argument of a precondition while
8992 -- updating its parenteses count for the subsequent merge.
8993
8994 function Message_Arg (Prag : Node_Id) return Node_Id;
8995 -- Return the message argument of a precondition
8996
8997 --------------------
8998 -- Expression_Arg --
8999 --------------------
9000
9001 function Expression_Arg (Prag : Node_Id) return Node_Id is
9002 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9003 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
9004
9005 begin
9006 if Paren_Count (Arg) = 0 then
9007 Set_Paren_Count (Arg, 1);
9008 end if;
9009
9010 return Arg;
9011 end Expression_Arg;
9012
9013 -----------------
9014 -- Message_Arg --
9015 -----------------
9016
9017 function Message_Arg (Prag : Node_Id) return Node_Id is
9018 Args : constant List_Id := Pragma_Argument_Associations (Prag);
9019 begin
9020 return Get_Pragma_Arg (Last (Args));
9021 end Message_Arg;
9022
9023 -- Local variables
9024
9025 From_Expr : constant Node_Id := Expression_Arg (From);
9026 From_Msg : constant Node_Id := Message_Arg (From);
9027 Into_Expr : constant Node_Id := Expression_Arg (Into);
9028 Into_Msg : constant Node_Id := Message_Arg (Into);
9029 Loc : constant Source_Ptr := Sloc (Into);
9030
9031 -- Start of processing for Merge_Preconditions
9032
9033 begin
9034 -- Merge the two preconditions by "or else"-ing them
9035
9036 Rewrite (Into_Expr,
9037 Make_Or_Else (Loc,
9038 Right_Opnd => Relocate_Node (Into_Expr),
9039 Left_Opnd => From_Expr));
9040
9041 -- Merge the two error messages to produce a single message of the
9042 -- form:
9043
9044 -- failed precondition from ...
9045 -- also failed inherited precondition from ...
9046
9047 if not Exception_Locations_Suppressed then
9048 Start_String (Strval (Into_Msg));
9049 Store_String_Char (ASCII.LF);
9050 Store_String_Chars (" also ");
9051 Store_String_Chars (Strval (From_Msg));
9052
9053 Set_Strval (Into_Msg, End_String);
9054 end if;
9055 end Merge_Preconditions;
9056
9057 -- Local variables
9058
9059 Inher_Subps : constant Subprogram_List :=
9060 Inherited_Subprograms (Subp_Id);
9061 Subp_Decl : constant Node_Id := Parent (Parent (Subp_Id));
9062 Check_Prag : Node_Id;
9063 Decl : Node_Id;
9064 Inher_Subp_Id : Entity_Id;
9065 Prag : Node_Id;
9066
9067 -- Start of processing for Collect_Spec_Preconditions
9068
9069 begin
9070 -- Process the contract of the spec
9071
9072 Prag := Pre_Post_Conditions (Contract (Subp_Id));
9073 while Present (Prag) loop
9074 if Pragma_Name (Prag) = Name_Precondition then
9075 Add_Or_Save_Precondition (Prag);
9076 end if;
9077
9078 Prag := Next_Pragma (Prag);
9079 end loop;
9080
9081 -- The subprogram declaration being processed is actually a body
9082 -- stub. The stub may carry a precondition pragma in which case it
9083 -- must be taken into account. The pragma appears after the stub.
9084
9085 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
9086
9087 -- Inspect the declarations following the body stub
9088
9089 Decl := Next (Subp_Decl);
9090 while Present (Decl) loop
9091
9092 -- Note that non-matching pragmas are skipped
9093
9094 if Nkind (Decl) = N_Pragma then
9095 if Pragma_Name (Decl) = Name_Precondition then
9096 if not Analyzed (Decl) then
9097 Analyze (Decl);
9098 end if;
9099
9100 Add_Or_Save_Precondition (Decl);
9101 end if;
9102
9103 -- Skip internally generated code
9104
9105 elsif not Comes_From_Source (Decl) then
9106 null;
9107
9108 -- Preconditions are usually grouped together. There is no need
9109 -- to inspect the whole declarative list.
9110
9111 else
9112 exit;
9113 end if;
9114
9115 Next (Decl);
9116 end loop;
9117 end if;
9118
9119 -- Process the contracts of all inherited subprograms, looking for
9120 -- class-wide preconditions.
9121
9122 for Index in Inher_Subps'Range loop
9123 Inher_Subp_Id := Inher_Subps (Index);
9124
9125 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
9126 while Present (Prag) loop
9127 if Pragma_Name (Prag) = Name_Precondition
9128 and then Class_Present (Prag)
9129 then
9130 Check_Prag :=
9131 Build_Pragma_Check_Equivalent
9132 (Prag => Prag,
9133 Subp_Id => Subp_Id,
9134 Inher_Id => Inher_Subp_Id);
9135
9136 -- The spec or an inherited subprogram already yielded a
9137 -- class-wide precondition. Merge the existing precondition
9138 -- with the current one using "or else".
9139
9140 if Present (Class_Pre) then
9141 Merge_Preconditions (Check_Prag, Class_Pre);
9142 else
9143 Class_Pre := Check_Prag;
9144 end if;
9145 end if;
9146
9147 Prag := Next_Pragma (Prag);
9148 end loop;
9149 end loop;
9150
9151 -- Add the merged class-wide preconditions (if any)
9152
9153 if Present (Class_Pre) then
9154 Prepend_To_Declarations (Class_Pre);
9155 end if;
9156 end Collect_Spec_Preconditions;
9157
9158 -----------------------------
9159 -- Prepend_To_Declarations --
9160 -----------------------------
9161
9162 procedure Prepend_To_Declarations (Item : Node_Id) is
9163 Decls : List_Id := Declarations (N);
9164
9165 begin
9166 -- Ensure that the body has a declarative list
9167
9168 if No (Decls) then
9169 Decls := New_List;
9170 Set_Declarations (N, Decls);
9171 end if;
9172
9173 Prepend_To (Decls, Item);
9174 end Prepend_To_Declarations;
9175
9176 ----------------------------
9177 -- Process_Contract_Cases --
9178 ----------------------------
9179
9180 procedure Process_Contract_Cases
9181 (Subp_Id : Entity_Id;
9182 Stmts : in out List_Id)
9183 is
9184 Prag : Node_Id;
9185
9186 begin
9187 -- Do not build the Contract_Cases circuitry if no code is being
9188 -- generated.
9189
9190 if not Expander_Active then
9191 return;
9192 end if;
9193
9194 Prag := Contract_Test_Cases (Contract (Subp_Id));
9195 while Present (Prag) loop
9196 if Pragma_Name (Prag) = Name_Contract_Cases then
9197 Expand_Contract_Cases
9198 (CCs => Prag,
9199 Subp_Id => Subp_Id,
9200 Decls => Declarations (N),
9201 Stmts => Stmts);
9202 end if;
9203
9204 Prag := Next_Pragma (Prag);
9205 end loop;
9206 end Process_Contract_Cases;
9207
9208 -- Local variables
9209
9210 Post_Stmts : List_Id := No_List;
9211 Result : Entity_Id;
9212 Subp_Id : Entity_Id;
9213
9214 -- Start of processing for Expand_Subprogram_Contract
9215
9216 begin
9217 if Present (Spec_Id) then
9218 Subp_Id := Spec_Id;
9219 else
9220 Subp_Id := Body_Id;
9221 end if;
9222
9223 -- Do not process a predicate function as its body will end up with a
9224 -- recursive call to itself and blow up the stack.
9225
9226 if Ekind (Subp_Id) = E_Function
9227 and then Is_Predicate_Function (Subp_Id)
9228 then
9229 return;
9230
9231 -- Do not process TSS subprograms
9232
9233 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
9234 return;
9235 end if;
9236
9237 -- The expansion of a subprogram contract involves the relocation of
9238 -- various contract assertions to the declarations of the body in a
9239 -- particular order. The order is as follows:
9240
9241 -- function Example (...) return ... is
9242 -- procedure _Postconditions (...) is
9243 -- begin
9244 -- <refined postconditions from body>
9245 -- <postconditions from body>
9246 -- <postconditions from spec>
9247 -- <inherited postconditions>
9248 -- <contract case consequences>
9249 -- <invariant check of function result (if applicable)>
9250 -- <invariant and predicate checks of parameters>
9251 -- end _Postconditions;
9252
9253 -- <inherited preconditions>
9254 -- <preconditions from spec>
9255 -- <preconditions from body>
9256 -- <refined preconditions from body>
9257 -- <contract case conditions>
9258
9259 -- <source declarations>
9260 -- begin
9261 -- <source statements>
9262
9263 -- _Preconditions (Result);
9264 -- return Result;
9265 -- end Example;
9266
9267 -- Routine _Postconditions holds all contract assertions that must be
9268 -- verified on exit from the related routine.
9269
9270 -- Collect all [inherited] preconditions from the spec, transform them
9271 -- into Check pragmas and add them to the declarations of the body in
9272 -- the order outlined above.
9273
9274 if Present (Spec_Id) then
9275 Collect_Spec_Preconditions (Spec_Id);
9276 end if;
9277
9278 -- Transform all [refined] postconditions of the body into Check
9279 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9280
9281 Collect_Body_Postconditions (Post_Stmts);
9282
9283 -- Transform all [inherited] postconditions from the spec into Check
9284 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9285
9286 if Present (Spec_Id) then
9287 Collect_Spec_Postconditions (Spec_Id, Post_Stmts);
9288
9289 -- Transform pragma Contract_Cases from the spec into its circuitry
9290
9291 Process_Contract_Cases (Spec_Id, Post_Stmts);
9292 end if;
9293
9294 -- Apply invariant and predicate checks on the result of a function (if
9295 -- applicable) and all formals. The resulting checks are accumulated in
9296 -- list Post_Stmts.
9297
9298 Add_Invariant_And_Predicate_Checks (Subp_Id, Post_Stmts, Result);
9299
9300 -- Construct procedure _Postconditions
9301
9302 Build_Postconditions_Procedure (Subp_Id, Post_Stmts, Result);
9303 end Expand_Subprogram_Contract;
9304
9305 --------------------------------
9306 -- Is_Build_In_Place_Function --
9307 --------------------------------
9308
9309 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
9310 begin
9311 -- This function is called from Expand_Subtype_From_Expr during
9312 -- semantic analysis, even when expansion is off. In those cases
9313 -- the build_in_place expansion will not take place.
9314
9315 if not Expander_Active then
9316 return False;
9317 end if;
9318
9319 -- For now we test whether E denotes a function or access-to-function
9320 -- type whose result subtype is inherently limited. Later this test
9321 -- may be revised to allow composite nonlimited types. Functions with
9322 -- a foreign convention or whose result type has a foreign convention
9323 -- never qualify.
9324
9325 if Ekind_In (E, E_Function, E_Generic_Function)
9326 or else (Ekind (E) = E_Subprogram_Type
9327 and then Etype (E) /= Standard_Void_Type)
9328 then
9329 -- Note: If the function has a foreign convention, it cannot build
9330 -- its result in place, so you're on your own. On the other hand,
9331 -- if only the return type has a foreign convention, its layout is
9332 -- intended to be compatible with the other language, but the build-
9333 -- in place machinery can ensure that the object is not copied.
9334
9335 if Has_Foreign_Convention (E) then
9336 return False;
9337
9338 -- In Ada 2005 all functions with an inherently limited return type
9339 -- must be handled using a build-in-place profile, including the case
9340 -- of a function with a limited interface result, where the function
9341 -- may return objects of nonlimited descendants.
9342
9343 else
9344 return Is_Limited_View (Etype (E))
9345 and then Ada_Version >= Ada_2005
9346 and then not Debug_Flag_Dot_L;
9347 end if;
9348
9349 else
9350 return False;
9351 end if;
9352 end Is_Build_In_Place_Function;
9353
9354 -------------------------------------
9355 -- Is_Build_In_Place_Function_Call --
9356 -------------------------------------
9357
9358 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
9359 Exp_Node : Node_Id := N;
9360 Function_Id : Entity_Id;
9361
9362 begin
9363 -- Return False if the expander is currently inactive, since awareness
9364 -- of build-in-place treatment is only relevant during expansion. Note
9365 -- that Is_Build_In_Place_Function, which is called as part of this
9366 -- function, is also conditioned this way, but we need to check here as
9367 -- well to avoid blowing up on processing protected calls when expansion
9368 -- is disabled (such as with -gnatc) since those would trip over the
9369 -- raise of Program_Error below.
9370
9371 -- In SPARK mode, build-in-place calls are not expanded, so that we
9372 -- may end up with a call that is neither resolved to an entity, nor
9373 -- an indirect call.
9374
9375 if not Expander_Active then
9376 return False;
9377 end if;
9378
9379 -- Step past qualification or unchecked conversion (the latter can occur
9380 -- in cases of calls to 'Input).
9381
9382 if Nkind_In (Exp_Node, N_Qualified_Expression,
9383 N_Unchecked_Type_Conversion)
9384 then
9385 Exp_Node := Expression (N);
9386 end if;
9387
9388 if Nkind (Exp_Node) /= N_Function_Call then
9389 return False;
9390
9391 else
9392 if Is_Entity_Name (Name (Exp_Node)) then
9393 Function_Id := Entity (Name (Exp_Node));
9394
9395 -- In the case of an explicitly dereferenced call, use the subprogram
9396 -- type generated for the dereference.
9397
9398 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
9399 Function_Id := Etype (Name (Exp_Node));
9400
9401 -- This may be a call to a protected function.
9402
9403 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
9404 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
9405
9406 else
9407 raise Program_Error;
9408 end if;
9409
9410 return Is_Build_In_Place_Function (Function_Id);
9411 end if;
9412 end Is_Build_In_Place_Function_Call;
9413
9414 -----------------------
9415 -- Freeze_Subprogram --
9416 -----------------------
9417
9418 procedure Freeze_Subprogram (N : Node_Id) is
9419 Loc : constant Source_Ptr := Sloc (N);
9420
9421 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
9422 -- (Ada 2005): Register a predefined primitive in all the secondary
9423 -- dispatch tables of its primitive type.
9424
9425 ----------------------------------
9426 -- Register_Predefined_DT_Entry --
9427 ----------------------------------
9428
9429 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
9430 Iface_DT_Ptr : Elmt_Id;
9431 Tagged_Typ : Entity_Id;
9432 Thunk_Id : Entity_Id;
9433 Thunk_Code : Node_Id;
9434
9435 begin
9436 Tagged_Typ := Find_Dispatching_Type (Prim);
9437
9438 if No (Access_Disp_Table (Tagged_Typ))
9439 or else not Has_Interfaces (Tagged_Typ)
9440 or else not RTE_Available (RE_Interface_Tag)
9441 or else Restriction_Active (No_Dispatching_Calls)
9442 then
9443 return;
9444 end if;
9445
9446 -- Skip the first two access-to-dispatch-table pointers since they
9447 -- leads to the primary dispatch table (predefined DT and user
9448 -- defined DT). We are only concerned with the secondary dispatch
9449 -- table pointers. Note that the access-to- dispatch-table pointer
9450 -- corresponds to the first implemented interface retrieved below.
9451
9452 Iface_DT_Ptr :=
9453 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
9454
9455 while Present (Iface_DT_Ptr)
9456 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
9457 loop
9458 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
9459 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
9460
9461 if Present (Thunk_Code) then
9462 Insert_Actions_After (N, New_List (
9463 Thunk_Code,
9464
9465 Build_Set_Predefined_Prim_Op_Address (Loc,
9466 Tag_Node =>
9467 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
9468 Position => DT_Position (Prim),
9469 Address_Node =>
9470 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
9471 Make_Attribute_Reference (Loc,
9472 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
9473 Attribute_Name => Name_Unrestricted_Access))),
9474
9475 Build_Set_Predefined_Prim_Op_Address (Loc,
9476 Tag_Node =>
9477 New_Occurrence_Of
9478 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
9479 Loc),
9480 Position => DT_Position (Prim),
9481 Address_Node =>
9482 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
9483 Make_Attribute_Reference (Loc,
9484 Prefix => New_Occurrence_Of (Prim, Loc),
9485 Attribute_Name => Name_Unrestricted_Access)))));
9486 end if;
9487
9488 -- Skip the tag of the predefined primitives dispatch table
9489
9490 Next_Elmt (Iface_DT_Ptr);
9491 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
9492
9493 -- Skip tag of the no-thunks dispatch table
9494
9495 Next_Elmt (Iface_DT_Ptr);
9496 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
9497
9498 -- Skip tag of predefined primitives no-thunks dispatch table
9499
9500 Next_Elmt (Iface_DT_Ptr);
9501 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
9502
9503 Next_Elmt (Iface_DT_Ptr);
9504 end loop;
9505 end Register_Predefined_DT_Entry;
9506
9507 -- Local variables
9508
9509 Subp : constant Entity_Id := Entity (N);
9510
9511 -- Start of processing for Freeze_Subprogram
9512
9513 begin
9514 -- We suppress the initialization of the dispatch table entry when
9515 -- VM_Target because the dispatching mechanism is handled internally
9516 -- by the VM.
9517
9518 if Is_Dispatching_Operation (Subp)
9519 and then not Is_Abstract_Subprogram (Subp)
9520 and then Present (DTC_Entity (Subp))
9521 and then Present (Scope (DTC_Entity (Subp)))
9522 and then Tagged_Type_Expansion
9523 and then not Restriction_Active (No_Dispatching_Calls)
9524 and then RTE_Available (RE_Tag)
9525 then
9526 declare
9527 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
9528
9529 begin
9530 -- Handle private overridden primitives
9531
9532 if not Is_CPP_Class (Typ) then
9533 Check_Overriding_Operation (Subp);
9534 end if;
9535
9536 -- We assume that imported CPP primitives correspond with objects
9537 -- whose constructor is in the CPP side; therefore we don't need
9538 -- to generate code to register them in the dispatch table.
9539
9540 if Is_CPP_Class (Typ) then
9541 null;
9542
9543 -- Handle CPP primitives found in derivations of CPP_Class types.
9544 -- These primitives must have been inherited from some parent, and
9545 -- there is no need to register them in the dispatch table because
9546 -- Build_Inherit_Prims takes care of initializing these slots.
9547
9548 elsif Is_Imported (Subp)
9549 and then (Convention (Subp) = Convention_CPP
9550 or else Convention (Subp) = Convention_C)
9551 then
9552 null;
9553
9554 -- Generate code to register the primitive in non statically
9555 -- allocated dispatch tables
9556
9557 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
9558
9559 -- When a primitive is frozen, enter its name in its dispatch
9560 -- table slot.
9561
9562 if not Is_Interface (Typ)
9563 or else Present (Interface_Alias (Subp))
9564 then
9565 if Is_Predefined_Dispatching_Operation (Subp) then
9566 Register_Predefined_DT_Entry (Subp);
9567 end if;
9568
9569 Insert_Actions_After (N,
9570 Register_Primitive (Loc, Prim => Subp));
9571 end if;
9572 end if;
9573 end;
9574 end if;
9575
9576 -- Mark functions that return by reference. Note that it cannot be part
9577 -- of the normal semantic analysis of the spec since the underlying
9578 -- returned type may not be known yet (for private types).
9579
9580 declare
9581 Typ : constant Entity_Id := Etype (Subp);
9582 Utyp : constant Entity_Id := Underlying_Type (Typ);
9583 begin
9584 if Is_Limited_View (Typ) then
9585 Set_Returns_By_Ref (Subp);
9586 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
9587 Set_Returns_By_Ref (Subp);
9588 end if;
9589 end;
9590
9591 -- Wnen freezing a null procedure, analyze its delayed aspects now
9592 -- because we may not have reached the end of the declarative list when
9593 -- delayed aspects are normally analyzed. This ensures that dispatching
9594 -- calls are properly rewritten when the generated _Postcondition
9595 -- procedure is analyzed in the null procedure body.
9596
9597 if Nkind (Parent (Subp)) = N_Procedure_Specification
9598 and then Null_Present (Parent (Subp))
9599 then
9600 Analyze_Subprogram_Contract (Subp);
9601 end if;
9602 end Freeze_Subprogram;
9603
9604 -----------------------
9605 -- Is_Null_Procedure --
9606 -----------------------
9607
9608 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
9609 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
9610
9611 begin
9612 if Ekind (Subp) /= E_Procedure then
9613 return False;
9614
9615 -- Check if this is a declared null procedure
9616
9617 elsif Nkind (Decl) = N_Subprogram_Declaration then
9618 if not Null_Present (Specification (Decl)) then
9619 return False;
9620
9621 elsif No (Body_To_Inline (Decl)) then
9622 return False;
9623
9624 -- Check if the body contains only a null statement, followed by
9625 -- the return statement added during expansion.
9626
9627 else
9628 declare
9629 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
9630
9631 Stat : Node_Id;
9632 Stat2 : Node_Id;
9633
9634 begin
9635 if Nkind (Orig_Bod) /= N_Subprogram_Body then
9636 return False;
9637 else
9638 -- We must skip SCIL nodes because they are currently
9639 -- implemented as special N_Null_Statement nodes.
9640
9641 Stat :=
9642 First_Non_SCIL_Node
9643 (Statements (Handled_Statement_Sequence (Orig_Bod)));
9644 Stat2 := Next_Non_SCIL_Node (Stat);
9645
9646 return
9647 Is_Empty_List (Declarations (Orig_Bod))
9648 and then Nkind (Stat) = N_Null_Statement
9649 and then
9650 (No (Stat2)
9651 or else
9652 (Nkind (Stat2) = N_Simple_Return_Statement
9653 and then No (Next (Stat2))));
9654 end if;
9655 end;
9656 end if;
9657
9658 else
9659 return False;
9660 end if;
9661 end Is_Null_Procedure;
9662
9663 -------------------------------------------
9664 -- Make_Build_In_Place_Call_In_Allocator --
9665 -------------------------------------------
9666
9667 procedure Make_Build_In_Place_Call_In_Allocator
9668 (Allocator : Node_Id;
9669 Function_Call : Node_Id)
9670 is
9671 Acc_Type : constant Entity_Id := Etype (Allocator);
9672 Loc : Source_Ptr;
9673 Func_Call : Node_Id := Function_Call;
9674 Function_Id : Entity_Id;
9675 Result_Subt : Entity_Id;
9676 New_Allocator : Node_Id;
9677 Return_Obj_Access : Entity_Id;
9678
9679 begin
9680 -- Step past qualification or unchecked conversion (the latter can occur
9681 -- in cases of calls to 'Input).
9682
9683 if Nkind_In (Func_Call,
9684 N_Qualified_Expression,
9685 N_Unchecked_Type_Conversion)
9686 then
9687 Func_Call := Expression (Func_Call);
9688 end if;
9689
9690 -- If the call has already been processed to add build-in-place actuals
9691 -- then return. This should not normally occur in an allocator context,
9692 -- but we add the protection as a defensive measure.
9693
9694 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9695 return;
9696 end if;
9697
9698 -- Mark the call as processed as a build-in-place call
9699
9700 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9701
9702 Loc := Sloc (Function_Call);
9703
9704 if Is_Entity_Name (Name (Func_Call)) then
9705 Function_Id := Entity (Name (Func_Call));
9706
9707 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9708 Function_Id := Etype (Name (Func_Call));
9709
9710 else
9711 raise Program_Error;
9712 end if;
9713
9714 Result_Subt := Available_View (Etype (Function_Id));
9715
9716 -- Check whether return type includes tasks. This may not have been done
9717 -- previously, if the type was a limited view.
9718
9719 if Has_Task (Result_Subt) then
9720 Build_Activation_Chain_Entity (Allocator);
9721 end if;
9722
9723 -- When the result subtype is constrained, the return object must be
9724 -- allocated on the caller side, and access to it is passed to the
9725 -- function.
9726
9727 -- Here and in related routines, we must examine the full view of the
9728 -- type, because the view at the point of call may differ from that
9729 -- that in the function body, and the expansion mechanism depends on
9730 -- the characteristics of the full view.
9731
9732 if Is_Constrained (Underlying_Type (Result_Subt)) then
9733
9734 -- Replace the initialized allocator of form "new T'(Func (...))"
9735 -- with an uninitialized allocator of form "new T", where T is the
9736 -- result subtype of the called function. The call to the function
9737 -- is handled separately further below.
9738
9739 New_Allocator :=
9740 Make_Allocator (Loc,
9741 Expression => New_Occurrence_Of (Result_Subt, Loc));
9742 Set_No_Initialization (New_Allocator);
9743
9744 -- Copy attributes to new allocator. Note that the new allocator
9745 -- logically comes from source if the original one did, so copy the
9746 -- relevant flag. This ensures proper treatment of the restriction
9747 -- No_Implicit_Heap_Allocations in this case.
9748
9749 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9750 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9751 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9752
9753 Rewrite (Allocator, New_Allocator);
9754
9755 -- Create a new access object and initialize it to the result of the
9756 -- new uninitialized allocator. Note: we do not use Allocator as the
9757 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9758 -- as this would create a sort of infinite "recursion".
9759
9760 Return_Obj_Access := Make_Temporary (Loc, 'R');
9761 Set_Etype (Return_Obj_Access, Acc_Type);
9762
9763 Insert_Action (Allocator,
9764 Make_Object_Declaration (Loc,
9765 Defining_Identifier => Return_Obj_Access,
9766 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9767 Expression => Relocate_Node (Allocator)));
9768
9769 -- When the function has a controlling result, an allocation-form
9770 -- parameter must be passed indicating that the caller is allocating
9771 -- the result object. This is needed because such a function can be
9772 -- called as a dispatching operation and must be treated similarly
9773 -- to functions with unconstrained result subtypes.
9774
9775 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9776 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9777
9778 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9779 (Func_Call, Function_Id, Acc_Type);
9780
9781 Add_Task_Actuals_To_Build_In_Place_Call
9782 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
9783
9784 -- Add an implicit actual to the function call that provides access
9785 -- to the allocated object. An unchecked conversion to the (specific)
9786 -- result subtype of the function is inserted to handle cases where
9787 -- the access type of the allocator has a class-wide designated type.
9788
9789 Add_Access_Actual_To_Build_In_Place_Call
9790 (Func_Call,
9791 Function_Id,
9792 Make_Unchecked_Type_Conversion (Loc,
9793 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9794 Expression =>
9795 Make_Explicit_Dereference (Loc,
9796 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc))));
9797
9798 -- When the result subtype is unconstrained, the function itself must
9799 -- perform the allocation of the return object, so we pass parameters
9800 -- indicating that. We don't yet handle the case where the allocation
9801 -- must be done in a user-defined storage pool, which will require
9802 -- passing another actual or two to provide allocation/deallocation
9803 -- operations. ???
9804
9805 else
9806 -- Case of a user-defined storage pool. Pass an allocation parameter
9807 -- indicating that the function should allocate its result in the
9808 -- pool, and pass the pool. Use 'Unrestricted_Access because the
9809 -- pool may not be aliased.
9810
9811 if VM_Target = No_VM
9812 and then Present (Associated_Storage_Pool (Acc_Type))
9813 then
9814 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9815 (Func_Call, Function_Id, Alloc_Form => User_Storage_Pool,
9816 Pool_Actual =>
9817 Make_Attribute_Reference (Loc,
9818 Prefix =>
9819 New_Occurrence_Of
9820 (Associated_Storage_Pool (Acc_Type), Loc),
9821 Attribute_Name => Name_Unrestricted_Access));
9822
9823 -- No user-defined pool; pass an allocation parameter indicating that
9824 -- the function should allocate its result on the heap.
9825
9826 else
9827 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9828 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
9829 end if;
9830
9831 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9832 (Func_Call, Function_Id, Acc_Type);
9833
9834 Add_Task_Actuals_To_Build_In_Place_Call
9835 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
9836
9837 -- The caller does not provide the return object in this case, so we
9838 -- have to pass null for the object access actual.
9839
9840 Add_Access_Actual_To_Build_In_Place_Call
9841 (Func_Call, Function_Id, Return_Object => Empty);
9842 end if;
9843
9844 -- If the build-in-place function call returns a controlled object,
9845 -- the finalization master will require a reference to routine
9846 -- Finalize_Address of the designated type. Setting this attribute
9847 -- is done in the same manner to expansion of allocators.
9848
9849 if Needs_Finalization (Result_Subt) then
9850
9851 -- Controlled types with supressed finalization do not need to
9852 -- associate the address of their Finalize_Address primitives with
9853 -- a master since they do not need a master to begin with.
9854
9855 if Is_Library_Level_Entity (Acc_Type)
9856 and then Finalize_Storage_Only (Result_Subt)
9857 then
9858 null;
9859
9860 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
9861 -- because Finalize_Address is never built.
9862
9863 elsif not CodePeer_Mode then
9864 Insert_Action (Allocator,
9865 Make_Set_Finalize_Address_Call (Loc,
9866 Typ => Etype (Function_Id),
9867 Ptr_Typ => Acc_Type));
9868 end if;
9869 end if;
9870
9871 -- Finally, replace the allocator node with a reference to the result
9872 -- of the function call itself (which will effectively be an access
9873 -- to the object created by the allocator).
9874
9875 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
9876
9877 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9878 -- generate an implicit conversion to force displacement of the "this"
9879 -- pointer.
9880
9881 if Is_Interface (Designated_Type (Acc_Type)) then
9882 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9883 end if;
9884
9885 Analyze_And_Resolve (Allocator, Acc_Type);
9886 end Make_Build_In_Place_Call_In_Allocator;
9887
9888 ---------------------------------------------------
9889 -- Make_Build_In_Place_Call_In_Anonymous_Context --
9890 ---------------------------------------------------
9891
9892 procedure Make_Build_In_Place_Call_In_Anonymous_Context
9893 (Function_Call : Node_Id)
9894 is
9895 Loc : Source_Ptr;
9896 Func_Call : Node_Id := Function_Call;
9897 Function_Id : Entity_Id;
9898 Result_Subt : Entity_Id;
9899 Return_Obj_Id : Entity_Id;
9900 Return_Obj_Decl : Entity_Id;
9901
9902 begin
9903 -- Step past qualification or unchecked conversion (the latter can occur
9904 -- in cases of calls to 'Input).
9905
9906 if Nkind_In (Func_Call, N_Qualified_Expression,
9907 N_Unchecked_Type_Conversion)
9908 then
9909 Func_Call := Expression (Func_Call);
9910 end if;
9911
9912 -- If the call has already been processed to add build-in-place actuals
9913 -- then return. One place this can occur is for calls to build-in-place
9914 -- functions that occur within a call to a protected operation, where
9915 -- due to rewriting and expansion of the protected call there can be
9916 -- more than one call to Expand_Actuals for the same set of actuals.
9917
9918 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9919 return;
9920 end if;
9921
9922 -- Mark the call as processed as a build-in-place call
9923
9924 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9925
9926 Loc := Sloc (Function_Call);
9927
9928 if Is_Entity_Name (Name (Func_Call)) then
9929 Function_Id := Entity (Name (Func_Call));
9930
9931 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9932 Function_Id := Etype (Name (Func_Call));
9933
9934 else
9935 raise Program_Error;
9936 end if;
9937
9938 Result_Subt := Etype (Function_Id);
9939
9940 -- If the build-in-place function returns a controlled object, then the
9941 -- object needs to be finalized immediately after the context. Since
9942 -- this case produces a transient scope, the servicing finalizer needs
9943 -- to name the returned object. Create a temporary which is initialized
9944 -- with the function call:
9945 --
9946 -- Temp_Id : Func_Type := BIP_Func_Call;
9947 --
9948 -- The initialization expression of the temporary will be rewritten by
9949 -- the expander using the appropriate mechanism in Make_Build_In_Place_
9950 -- Call_In_Object_Declaration.
9951
9952 if Needs_Finalization (Result_Subt) then
9953 declare
9954 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
9955 Temp_Decl : Node_Id;
9956
9957 begin
9958 -- Reset the guard on the function call since the following does
9959 -- not perform actual call expansion.
9960
9961 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
9962
9963 Temp_Decl :=
9964 Make_Object_Declaration (Loc,
9965 Defining_Identifier => Temp_Id,
9966 Object_Definition =>
9967 New_Occurrence_Of (Result_Subt, Loc),
9968 Expression =>
9969 New_Copy_Tree (Function_Call));
9970
9971 Insert_Action (Function_Call, Temp_Decl);
9972
9973 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
9974 Analyze (Function_Call);
9975 end;
9976
9977 -- When the result subtype is constrained, an object of the subtype is
9978 -- declared and an access value designating it is passed as an actual.
9979
9980 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
9981
9982 -- Create a temporary object to hold the function result
9983
9984 Return_Obj_Id := Make_Temporary (Loc, 'R');
9985 Set_Etype (Return_Obj_Id, Result_Subt);
9986
9987 Return_Obj_Decl :=
9988 Make_Object_Declaration (Loc,
9989 Defining_Identifier => Return_Obj_Id,
9990 Aliased_Present => True,
9991 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
9992
9993 Set_No_Initialization (Return_Obj_Decl);
9994
9995 Insert_Action (Func_Call, Return_Obj_Decl);
9996
9997 -- When the function has a controlling result, an allocation-form
9998 -- parameter must be passed indicating that the caller is allocating
9999 -- the result object. This is needed because such a function can be
10000 -- called as a dispatching operation and must be treated similarly
10001 -- to functions with unconstrained result subtypes.
10002
10003 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10004 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
10005
10006 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10007 (Func_Call, Function_Id);
10008
10009 Add_Task_Actuals_To_Build_In_Place_Call
10010 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
10011
10012 -- Add an implicit actual to the function call that provides access
10013 -- to the caller's return object.
10014
10015 Add_Access_Actual_To_Build_In_Place_Call
10016 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
10017
10018 -- When the result subtype is unconstrained, the function must allocate
10019 -- the return object in the secondary stack, so appropriate implicit
10020 -- parameters are added to the call to indicate that. A transient
10021 -- scope is established to ensure eventual cleanup of the result.
10022
10023 else
10024 -- Pass an allocation parameter indicating that the function should
10025 -- allocate its result on the secondary stack.
10026
10027 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10028 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
10029
10030 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10031 (Func_Call, Function_Id);
10032
10033 Add_Task_Actuals_To_Build_In_Place_Call
10034 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
10035
10036 -- Pass a null value to the function since no return object is
10037 -- available on the caller side.
10038
10039 Add_Access_Actual_To_Build_In_Place_Call
10040 (Func_Call, Function_Id, Empty);
10041 end if;
10042 end Make_Build_In_Place_Call_In_Anonymous_Context;
10043
10044 --------------------------------------------
10045 -- Make_Build_In_Place_Call_In_Assignment --
10046 --------------------------------------------
10047
10048 procedure Make_Build_In_Place_Call_In_Assignment
10049 (Assign : Node_Id;
10050 Function_Call : Node_Id)
10051 is
10052 Lhs : constant Node_Id := Name (Assign);
10053 Func_Call : Node_Id := Function_Call;
10054 Func_Id : Entity_Id;
10055 Loc : Source_Ptr;
10056 Obj_Decl : Node_Id;
10057 Obj_Id : Entity_Id;
10058 Ptr_Typ : Entity_Id;
10059 Ptr_Typ_Decl : Node_Id;
10060 New_Expr : Node_Id;
10061 Result_Subt : Entity_Id;
10062 Target : Node_Id;
10063
10064 begin
10065 -- Step past qualification or unchecked conversion (the latter can occur
10066 -- in cases of calls to 'Input).
10067
10068 if Nkind_In (Func_Call, N_Qualified_Expression,
10069 N_Unchecked_Type_Conversion)
10070 then
10071 Func_Call := Expression (Func_Call);
10072 end if;
10073
10074 -- If the call has already been processed to add build-in-place actuals
10075 -- then return. This should not normally occur in an assignment context,
10076 -- but we add the protection as a defensive measure.
10077
10078 if Is_Expanded_Build_In_Place_Call (Func_Call) then
10079 return;
10080 end if;
10081
10082 -- Mark the call as processed as a build-in-place call
10083
10084 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
10085
10086 Loc := Sloc (Function_Call);
10087
10088 if Is_Entity_Name (Name (Func_Call)) then
10089 Func_Id := Entity (Name (Func_Call));
10090
10091 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
10092 Func_Id := Etype (Name (Func_Call));
10093
10094 else
10095 raise Program_Error;
10096 end if;
10097
10098 Result_Subt := Etype (Func_Id);
10099
10100 -- When the result subtype is unconstrained, an additional actual must
10101 -- be passed to indicate that the caller is providing the return object.
10102 -- This parameter must also be passed when the called function has a
10103 -- controlling result, because dispatching calls to the function needs
10104 -- to be treated effectively the same as calls to class-wide functions.
10105
10106 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10107 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
10108
10109 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10110 (Func_Call, Func_Id);
10111
10112 Add_Task_Actuals_To_Build_In_Place_Call
10113 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
10114
10115 -- Add an implicit actual to the function call that provides access to
10116 -- the caller's return object.
10117
10118 Add_Access_Actual_To_Build_In_Place_Call
10119 (Func_Call,
10120 Func_Id,
10121 Make_Unchecked_Type_Conversion (Loc,
10122 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10123 Expression => Relocate_Node (Lhs)));
10124
10125 -- Create an access type designating the function's result subtype
10126
10127 Ptr_Typ := Make_Temporary (Loc, 'A');
10128
10129 Ptr_Typ_Decl :=
10130 Make_Full_Type_Declaration (Loc,
10131 Defining_Identifier => Ptr_Typ,
10132 Type_Definition =>
10133 Make_Access_To_Object_Definition (Loc,
10134 All_Present => True,
10135 Subtype_Indication =>
10136 New_Occurrence_Of (Result_Subt, Loc)));
10137 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
10138
10139 -- Finally, create an access object initialized to a reference to the
10140 -- function call. We know this access value is non-null, so mark the
10141 -- entity accordingly to suppress junk access checks.
10142
10143 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
10144
10145 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
10146 Set_Etype (Obj_Id, Ptr_Typ);
10147 Set_Is_Known_Non_Null (Obj_Id);
10148
10149 Obj_Decl :=
10150 Make_Object_Declaration (Loc,
10151 Defining_Identifier => Obj_Id,
10152 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
10153 Expression => New_Expr);
10154 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
10155
10156 Rewrite (Assign, Make_Null_Statement (Loc));
10157
10158 -- Retrieve the target of the assignment
10159
10160 if Nkind (Lhs) = N_Selected_Component then
10161 Target := Selector_Name (Lhs);
10162 elsif Nkind (Lhs) = N_Type_Conversion then
10163 Target := Expression (Lhs);
10164 else
10165 Target := Lhs;
10166 end if;
10167
10168 -- If we are assigning to a return object or this is an expression of
10169 -- an extension aggregate, the target should either be an identifier
10170 -- or a simple expression. All other cases imply a different scenario.
10171
10172 if Nkind (Target) in N_Has_Entity then
10173 Target := Entity (Target);
10174 else
10175 return;
10176 end if;
10177 end Make_Build_In_Place_Call_In_Assignment;
10178
10179 ----------------------------------------------------
10180 -- Make_Build_In_Place_Call_In_Object_Declaration --
10181 ----------------------------------------------------
10182
10183 procedure Make_Build_In_Place_Call_In_Object_Declaration
10184 (Object_Decl : Node_Id;
10185 Function_Call : Node_Id)
10186 is
10187 Loc : Source_Ptr;
10188 Obj_Def_Id : constant Entity_Id :=
10189 Defining_Identifier (Object_Decl);
10190 Enclosing_Func : constant Entity_Id :=
10191 Enclosing_Subprogram (Obj_Def_Id);
10192 Call_Deref : Node_Id;
10193 Caller_Object : Node_Id;
10194 Def_Id : Entity_Id;
10195 Fmaster_Actual : Node_Id := Empty;
10196 Func_Call : Node_Id := Function_Call;
10197 Function_Id : Entity_Id;
10198 Pool_Actual : Node_Id;
10199 Ptr_Typ : Entity_Id;
10200 Ptr_Typ_Decl : Node_Id;
10201 Pass_Caller_Acc : Boolean := False;
10202 Res_Decl : Node_Id;
10203 Result_Subt : Entity_Id;
10204
10205 begin
10206 -- Step past qualification or unchecked conversion (the latter can occur
10207 -- in cases of calls to 'Input).
10208
10209 if Nkind_In (Func_Call, N_Qualified_Expression,
10210 N_Unchecked_Type_Conversion)
10211 then
10212 Func_Call := Expression (Func_Call);
10213 end if;
10214
10215 -- If the call has already been processed to add build-in-place actuals
10216 -- then return. This should not normally occur in an object declaration,
10217 -- but we add the protection as a defensive measure.
10218
10219 if Is_Expanded_Build_In_Place_Call (Func_Call) then
10220 return;
10221 end if;
10222
10223 -- Mark the call as processed as a build-in-place call
10224
10225 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
10226
10227 Loc := Sloc (Function_Call);
10228
10229 if Is_Entity_Name (Name (Func_Call)) then
10230 Function_Id := Entity (Name (Func_Call));
10231
10232 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
10233 Function_Id := Etype (Name (Func_Call));
10234
10235 else
10236 raise Program_Error;
10237 end if;
10238
10239 Result_Subt := Etype (Function_Id);
10240
10241 -- Create an access type designating the function's result subtype. We
10242 -- use the type of the original call because it may be a call to an
10243 -- inherited operation, which the expansion has replaced with the parent
10244 -- operation that yields the parent type. Note that this access type
10245 -- must be declared before we establish a transient scope, so that it
10246 -- receives the proper accessibility level.
10247
10248 Ptr_Typ := Make_Temporary (Loc, 'A');
10249 Ptr_Typ_Decl :=
10250 Make_Full_Type_Declaration (Loc,
10251 Defining_Identifier => Ptr_Typ,
10252 Type_Definition =>
10253 Make_Access_To_Object_Definition (Loc,
10254 All_Present => True,
10255 Subtype_Indication =>
10256 New_Occurrence_Of (Etype (Function_Call), Loc)));
10257
10258 -- The access type and its accompanying object must be inserted after
10259 -- the object declaration in the constrained case, so that the function
10260 -- call can be passed access to the object. In the unconstrained case,
10261 -- or if the object declaration is for a return object, the access type
10262 -- and object must be inserted before the object, since the object
10263 -- declaration is rewritten to be a renaming of a dereference of the
10264 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
10265 -- the result object is in a different (transient) scope, so won't
10266 -- cause freezing.
10267
10268 if Is_Constrained (Underlying_Type (Result_Subt))
10269 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
10270 then
10271 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
10272 else
10273 Insert_Action (Object_Decl, Ptr_Typ_Decl);
10274 end if;
10275
10276 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
10277 -- elaborated in an inner (transient) scope and thus won't cause
10278 -- freezing by itself.
10279
10280 declare
10281 Ptr_Typ_Freeze_Ref : constant Node_Id :=
10282 New_Occurrence_Of (Ptr_Typ, Loc);
10283 begin
10284 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
10285 Freeze_Expression (Ptr_Typ_Freeze_Ref);
10286 end;
10287
10288 -- If the the object is a return object of an enclosing build-in-place
10289 -- function, then the implicit build-in-place parameters of the
10290 -- enclosing function are simply passed along to the called function.
10291 -- (Unfortunately, this won't cover the case of extension aggregates
10292 -- where the ancestor part is a build-in-place unconstrained function
10293 -- call that should be passed along the caller's parameters. Currently
10294 -- those get mishandled by reassigning the result of the call to the
10295 -- aggregate return object, when the call result should really be
10296 -- directly built in place in the aggregate and not in a temporary. ???)
10297
10298 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
10299 Pass_Caller_Acc := True;
10300
10301 -- When the enclosing function has a BIP_Alloc_Form formal then we
10302 -- pass it along to the callee (such as when the enclosing function
10303 -- has an unconstrained or tagged result type).
10304
10305 if Needs_BIP_Alloc_Form (Enclosing_Func) then
10306 if VM_Target = No_VM and then
10307 RTE_Available (RE_Root_Storage_Pool_Ptr)
10308 then
10309 Pool_Actual :=
10310 New_Occurrence_Of (Build_In_Place_Formal
10311 (Enclosing_Func, BIP_Storage_Pool), Loc);
10312
10313 -- The build-in-place pool formal is not built on .NET/JVM
10314
10315 else
10316 Pool_Actual := Empty;
10317 end if;
10318
10319 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10320 (Func_Call,
10321 Function_Id,
10322 Alloc_Form_Exp =>
10323 New_Occurrence_Of
10324 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
10325 Loc),
10326 Pool_Actual => Pool_Actual);
10327
10328 -- Otherwise, if enclosing function has a constrained result subtype,
10329 -- then caller allocation will be used.
10330
10331 else
10332 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10333 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
10334 end if;
10335
10336 if Needs_BIP_Finalization_Master (Enclosing_Func) then
10337 Fmaster_Actual :=
10338 New_Occurrence_Of
10339 (Build_In_Place_Formal
10340 (Enclosing_Func, BIP_Finalization_Master), Loc);
10341 end if;
10342
10343 -- Retrieve the BIPacc formal from the enclosing function and convert
10344 -- it to the access type of the callee's BIP_Object_Access formal.
10345
10346 Caller_Object :=
10347 Make_Unchecked_Type_Conversion (Loc,
10348 Subtype_Mark =>
10349 New_Occurrence_Of
10350 (Etype
10351 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
10352 Loc),
10353 Expression =>
10354 New_Occurrence_Of
10355 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
10356 Loc));
10357
10358 -- In the constrained case, add an implicit actual to the function call
10359 -- that provides access to the declared object. An unchecked conversion
10360 -- to the (specific) result type of the function is inserted to handle
10361 -- the case where the object is declared with a class-wide type.
10362
10363 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
10364 Caller_Object :=
10365 Make_Unchecked_Type_Conversion (Loc,
10366 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10367 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
10368
10369 -- When the function has a controlling result, an allocation-form
10370 -- parameter must be passed indicating that the caller is allocating
10371 -- the result object. This is needed because such a function can be
10372 -- called as a dispatching operation and must be treated similarly
10373 -- to functions with unconstrained result subtypes.
10374
10375 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10376 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
10377
10378 -- In other unconstrained cases, pass an indication to do the allocation
10379 -- on the secondary stack and set Caller_Object to Empty so that a null
10380 -- value will be passed for the caller's object address. A transient
10381 -- scope is established to ensure eventual cleanup of the result.
10382
10383 else
10384 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10385 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
10386 Caller_Object := Empty;
10387
10388 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
10389 end if;
10390
10391 -- Pass along any finalization master actual, which is needed in the
10392 -- case where the called function initializes a return object of an
10393 -- enclosing build-in-place function.
10394
10395 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10396 (Func_Call => Func_Call,
10397 Func_Id => Function_Id,
10398 Master_Exp => Fmaster_Actual);
10399
10400 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
10401 and then Has_Task (Result_Subt)
10402 then
10403 -- Here we're passing along the master that was passed in to this
10404 -- function.
10405
10406 Add_Task_Actuals_To_Build_In_Place_Call
10407 (Func_Call, Function_Id,
10408 Master_Actual =>
10409 New_Occurrence_Of (Build_In_Place_Formal
10410 (Enclosing_Func, BIP_Task_Master), Loc));
10411
10412 else
10413 Add_Task_Actuals_To_Build_In_Place_Call
10414 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
10415 end if;
10416
10417 Add_Access_Actual_To_Build_In_Place_Call
10418 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
10419
10420 -- Finally, create an access object initialized to a reference to the
10421 -- function call. We know this access value cannot be null, so mark the
10422 -- entity accordingly to suppress the access check.
10423
10424 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
10425 Set_Etype (Def_Id, Ptr_Typ);
10426 Set_Is_Known_Non_Null (Def_Id);
10427
10428 Res_Decl :=
10429 Make_Object_Declaration (Loc,
10430 Defining_Identifier => Def_Id,
10431 Constant_Present => True,
10432 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
10433 Expression =>
10434 Make_Reference (Loc, Relocate_Node (Func_Call)));
10435
10436 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
10437
10438 -- If the result subtype of the called function is constrained and
10439 -- is not itself the return expression of an enclosing BIP function,
10440 -- then mark the object as having no initialization.
10441
10442 if Is_Constrained (Underlying_Type (Result_Subt))
10443 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
10444 then
10445 -- The related object declaration is encased in a transient block
10446 -- because the build-in-place function call contains at least one
10447 -- nested function call that produces a controlled transient
10448 -- temporary:
10449
10450 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
10451
10452 -- Since the build-in-place expansion decouples the call from the
10453 -- object declaration, the finalization machinery lacks the context
10454 -- which prompted the generation of the transient block. To resolve
10455 -- this scenario, store the build-in-place call.
10456
10457 if Scope_Is_Transient
10458 and then Node_To_Be_Wrapped = Object_Decl
10459 then
10460 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
10461 end if;
10462
10463 Set_Expression (Object_Decl, Empty);
10464 Set_No_Initialization (Object_Decl);
10465
10466 -- In case of an unconstrained result subtype, or if the call is the
10467 -- return expression of an enclosing BIP function, rewrite the object
10468 -- declaration as an object renaming where the renamed object is a
10469 -- dereference of <function_Call>'reference:
10470 --
10471 -- Obj : Subt renames <function_call>'Ref.all;
10472
10473 else
10474 Call_Deref :=
10475 Make_Explicit_Dereference (Loc,
10476 Prefix => New_Occurrence_Of (Def_Id, Loc));
10477
10478 Loc := Sloc (Object_Decl);
10479 Rewrite (Object_Decl,
10480 Make_Object_Renaming_Declaration (Loc,
10481 Defining_Identifier => Make_Temporary (Loc, 'D'),
10482 Access_Definition => Empty,
10483 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10484 Name => Call_Deref));
10485
10486 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
10487
10488 Analyze (Object_Decl);
10489
10490 -- Replace the internal identifier of the renaming declaration's
10491 -- entity with identifier of the original object entity. We also have
10492 -- to exchange the entities containing their defining identifiers to
10493 -- ensure the correct replacement of the object declaration by the
10494 -- object renaming declaration to avoid homograph conflicts (since
10495 -- the object declaration's defining identifier was already entered
10496 -- in current scope). The Next_Entity links of the two entities also
10497 -- have to be swapped since the entities are part of the return
10498 -- scope's entity list and the list structure would otherwise be
10499 -- corrupted. Finally, the homonym chain must be preserved as well.
10500
10501 declare
10502 Renaming_Def_Id : constant Entity_Id :=
10503 Defining_Identifier (Object_Decl);
10504 Next_Entity_Temp : constant Entity_Id :=
10505 Next_Entity (Renaming_Def_Id);
10506 begin
10507 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
10508
10509 -- Swap next entity links in preparation for exchanging entities
10510
10511 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
10512 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
10513 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
10514
10515 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
10516
10517 -- Preserve source indication of original declaration, so that
10518 -- xref information is properly generated for the right entity.
10519
10520 Preserve_Comes_From_Source
10521 (Object_Decl, Original_Node (Object_Decl));
10522
10523 Preserve_Comes_From_Source
10524 (Obj_Def_Id, Original_Node (Object_Decl));
10525
10526 Set_Comes_From_Source (Renaming_Def_Id, False);
10527 end;
10528 end if;
10529
10530 -- If the object entity has a class-wide Etype, then we need to change
10531 -- it to the result subtype of the function call, because otherwise the
10532 -- object will be class-wide without an explicit initialization and
10533 -- won't be allocated properly by the back end. It seems unclean to make
10534 -- such a revision to the type at this point, and we should try to
10535 -- improve this treatment when build-in-place functions with class-wide
10536 -- results are implemented. ???
10537
10538 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
10539 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
10540 end if;
10541 end Make_Build_In_Place_Call_In_Object_Declaration;
10542
10543 --------------------------------------------
10544 -- Make_CPP_Constructor_Call_In_Allocator --
10545 --------------------------------------------
10546
10547 procedure Make_CPP_Constructor_Call_In_Allocator
10548 (Allocator : Node_Id;
10549 Function_Call : Node_Id)
10550 is
10551 Loc : constant Source_Ptr := Sloc (Function_Call);
10552 Acc_Type : constant Entity_Id := Etype (Allocator);
10553 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
10554 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
10555
10556 New_Allocator : Node_Id;
10557 Return_Obj_Access : Entity_Id;
10558 Tmp_Obj : Node_Id;
10559
10560 begin
10561 pragma Assert (Nkind (Allocator) = N_Allocator
10562 and then Nkind (Function_Call) = N_Function_Call);
10563 pragma Assert (Convention (Function_Id) = Convention_CPP
10564 and then Is_Constructor (Function_Id));
10565 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
10566
10567 -- Replace the initialized allocator of form "new T'(Func (...))" with
10568 -- an uninitialized allocator of form "new T", where T is the result
10569 -- subtype of the called function. The call to the function is handled
10570 -- separately further below.
10571
10572 New_Allocator :=
10573 Make_Allocator (Loc,
10574 Expression => New_Occurrence_Of (Result_Subt, Loc));
10575 Set_No_Initialization (New_Allocator);
10576
10577 -- Copy attributes to new allocator. Note that the new allocator
10578 -- logically comes from source if the original one did, so copy the
10579 -- relevant flag. This ensures proper treatment of the restriction
10580 -- No_Implicit_Heap_Allocations in this case.
10581
10582 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
10583 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
10584 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
10585
10586 Rewrite (Allocator, New_Allocator);
10587
10588 -- Create a new access object and initialize it to the result of the
10589 -- new uninitialized allocator. Note: we do not use Allocator as the
10590 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
10591 -- as this would create a sort of infinite "recursion".
10592
10593 Return_Obj_Access := Make_Temporary (Loc, 'R');
10594 Set_Etype (Return_Obj_Access, Acc_Type);
10595
10596 -- Generate:
10597 -- Rnnn : constant ptr_T := new (T);
10598 -- Init (Rnn.all,...);
10599
10600 Tmp_Obj :=
10601 Make_Object_Declaration (Loc,
10602 Defining_Identifier => Return_Obj_Access,
10603 Constant_Present => True,
10604 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
10605 Expression => Relocate_Node (Allocator));
10606 Insert_Action (Allocator, Tmp_Obj);
10607
10608 Insert_List_After_And_Analyze (Tmp_Obj,
10609 Build_Initialization_Call (Loc,
10610 Id_Ref =>
10611 Make_Explicit_Dereference (Loc,
10612 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
10613 Typ => Etype (Function_Id),
10614 Constructor_Ref => Function_Call));
10615
10616 -- Finally, replace the allocator node with a reference to the result of
10617 -- the function call itself (which will effectively be an access to the
10618 -- object created by the allocator).
10619
10620 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
10621
10622 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
10623 -- generate an implicit conversion to force displacement of the "this"
10624 -- pointer.
10625
10626 if Is_Interface (Designated_Type (Acc_Type)) then
10627 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
10628 end if;
10629
10630 Analyze_And_Resolve (Allocator, Acc_Type);
10631 end Make_CPP_Constructor_Call_In_Allocator;
10632
10633 -----------------------------------
10634 -- Needs_BIP_Finalization_Master --
10635 -----------------------------------
10636
10637 function Needs_BIP_Finalization_Master
10638 (Func_Id : Entity_Id) return Boolean
10639 is
10640 pragma Assert (Is_Build_In_Place_Function (Func_Id));
10641 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10642 begin
10643 return
10644 not Restriction_Active (No_Finalization)
10645 and then Needs_Finalization (Func_Typ);
10646 end Needs_BIP_Finalization_Master;
10647
10648 --------------------------
10649 -- Needs_BIP_Alloc_Form --
10650 --------------------------
10651
10652 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
10653 pragma Assert (Is_Build_In_Place_Function (Func_Id));
10654 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10655 begin
10656 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
10657 end Needs_BIP_Alloc_Form;
10658
10659 --------------------------------------
10660 -- Needs_Result_Accessibility_Level --
10661 --------------------------------------
10662
10663 function Needs_Result_Accessibility_Level
10664 (Func_Id : Entity_Id) return Boolean
10665 is
10666 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10667
10668 function Has_Unconstrained_Access_Discriminant_Component
10669 (Comp_Typ : Entity_Id) return Boolean;
10670 -- Returns True if any component of the type has an unconstrained access
10671 -- discriminant.
10672
10673 -----------------------------------------------------
10674 -- Has_Unconstrained_Access_Discriminant_Component --
10675 -----------------------------------------------------
10676
10677 function Has_Unconstrained_Access_Discriminant_Component
10678 (Comp_Typ : Entity_Id) return Boolean
10679 is
10680 begin
10681 if not Is_Limited_Type (Comp_Typ) then
10682 return False;
10683
10684 -- Only limited types can have access discriminants with
10685 -- defaults.
10686
10687 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
10688 return True;
10689
10690 elsif Is_Array_Type (Comp_Typ) then
10691 return Has_Unconstrained_Access_Discriminant_Component
10692 (Underlying_Type (Component_Type (Comp_Typ)));
10693
10694 elsif Is_Record_Type (Comp_Typ) then
10695 declare
10696 Comp : Entity_Id;
10697
10698 begin
10699 Comp := First_Component (Comp_Typ);
10700 while Present (Comp) loop
10701 if Has_Unconstrained_Access_Discriminant_Component
10702 (Underlying_Type (Etype (Comp)))
10703 then
10704 return True;
10705 end if;
10706
10707 Next_Component (Comp);
10708 end loop;
10709 end;
10710 end if;
10711
10712 return False;
10713 end Has_Unconstrained_Access_Discriminant_Component;
10714
10715 Feature_Disabled : constant Boolean := True;
10716 -- Temporary
10717
10718 -- Start of processing for Needs_Result_Accessibility_Level
10719
10720 begin
10721 -- False if completion unavailable (how does this happen???)
10722
10723 if not Present (Func_Typ) then
10724 return False;
10725
10726 elsif Feature_Disabled then
10727 return False;
10728
10729 -- False if not a function, also handle enum-lit renames case
10730
10731 elsif Func_Typ = Standard_Void_Type
10732 or else Is_Scalar_Type (Func_Typ)
10733 then
10734 return False;
10735
10736 -- Handle a corner case, a cross-dialect subp renaming. For example,
10737 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
10738 -- an Ada 2005 (or earlier) unit references predefined run-time units.
10739
10740 elsif Present (Alias (Func_Id)) then
10741
10742 -- Unimplemented: a cross-dialect subp renaming which does not set
10743 -- the Alias attribute (e.g., a rename of a dereference of an access
10744 -- to subprogram value). ???
10745
10746 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
10747
10748 -- Remaining cases require Ada 2012 mode
10749
10750 elsif Ada_Version < Ada_2012 then
10751 return False;
10752
10753 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
10754 or else Is_Tagged_Type (Func_Typ)
10755 then
10756 -- In the case of, say, a null tagged record result type, the need
10757 -- for this extra parameter might not be obvious. This function
10758 -- returns True for all tagged types for compatibility reasons.
10759 -- A function with, say, a tagged null controlling result type might
10760 -- be overridden by a primitive of an extension having an access
10761 -- discriminant and the overrider and overridden must have compatible
10762 -- calling conventions (including implicitly declared parameters).
10763 -- Similarly, values of one access-to-subprogram type might designate
10764 -- both a primitive subprogram of a given type and a function
10765 -- which is, for example, not a primitive subprogram of any type.
10766 -- Again, this requires calling convention compatibility.
10767 -- It might be possible to solve these issues by introducing
10768 -- wrappers, but that is not the approach that was chosen.
10769
10770 return True;
10771
10772 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
10773 return True;
10774
10775 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
10776 return True;
10777
10778 -- False for all other cases
10779
10780 else
10781 return False;
10782 end if;
10783 end Needs_Result_Accessibility_Level;
10784
10785 ------------------------
10786 -- List_Inlining_Info --
10787 ------------------------
10788
10789 procedure List_Inlining_Info is
10790 Elmt : Elmt_Id;
10791 Nod : Node_Id;
10792 Count : Nat;
10793
10794 begin
10795 if not Debug_Flag_Dot_J then
10796 return;
10797 end if;
10798
10799 -- Generate listing of calls inlined by the frontend
10800
10801 if Present (Inlined_Calls) then
10802 Count := 0;
10803 Elmt := First_Elmt (Inlined_Calls);
10804 while Present (Elmt) loop
10805 Nod := Node (Elmt);
10806
10807 if In_Extended_Main_Code_Unit (Nod) then
10808 Count := Count + 1;
10809
10810 if Count = 1 then
10811 Write_Str ("Listing of frontend inlined calls");
10812 Write_Eol;
10813 end if;
10814
10815 Write_Str (" ");
10816 Write_Int (Count);
10817 Write_Str (":");
10818 Write_Location (Sloc (Nod));
10819 Write_Str (":");
10820 Output.Write_Eol;
10821 end if;
10822
10823 Next_Elmt (Elmt);
10824 end loop;
10825 end if;
10826
10827 -- Generate listing of calls passed to the backend
10828
10829 if Present (Backend_Calls) then
10830 Count := 0;
10831
10832 Elmt := First_Elmt (Backend_Calls);
10833 while Present (Elmt) loop
10834 Nod := Node (Elmt);
10835
10836 if In_Extended_Main_Code_Unit (Nod) then
10837 Count := Count + 1;
10838
10839 if Count = 1 then
10840 Write_Str ("Listing of inlined calls passed to the backend");
10841 Write_Eol;
10842 end if;
10843
10844 Write_Str (" ");
10845 Write_Int (Count);
10846 Write_Str (":");
10847 Write_Location (Sloc (Nod));
10848 Output.Write_Eol;
10849 end if;
10850
10851 Next_Elmt (Elmt);
10852 end loop;
10853 end if;
10854 end List_Inlining_Info;
10855
10856 end Exp_Ch6;