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