]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch6.adb
2017-10-19 Bob Duff <duff@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
2255 begin
2256 pragma Assert (Nkind_In (N, N_Entry_Call_Statement,
2257 N_Function_Call,
2258 N_Procedure_Call_Statement));
2259
2260 Expand_Call_Helper (N, Post_Call);
2261 Insert_Post_Call_Actions (N, Post_Call);
2262 end Expand_Call;
2263
2264 ------------------------
2265 -- Expand_Call_Helper --
2266 ------------------------
2267
2268 -- This procedure handles expansion of function calls and procedure call
2269 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2270 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2271
2272 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2273 -- Provide values of actuals for all formals in Extra_Formals list
2274 -- Replace "call" to enumeration literal function by literal itself
2275 -- Rewrite call to predefined operator as operator
2276 -- Replace actuals to in-out parameters that are numeric conversions,
2277 -- with explicit assignment to temporaries before and after the call.
2278
2279 -- Note that the list of actuals has been filled with default expressions
2280 -- during semantic analysis of the call. Only the extra actuals required
2281 -- for the 'Constrained attribute and for accessibility checks are added
2282 -- at this point.
2283
2284 procedure Expand_Call_Helper (N : Node_Id; Post_Call : out List_Id) is
2285 Loc : constant Source_Ptr := Sloc (N);
2286 Call_Node : Node_Id := N;
2287 Extra_Actuals : List_Id := No_List;
2288 Prev : Node_Id := Empty;
2289
2290 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2291 -- Adds one entry to the end of the actual parameter list. Used for
2292 -- default parameters and for extra actuals (for Extra_Formals). The
2293 -- argument is an N_Parameter_Association node.
2294
2295 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2296 -- Adds an extra actual to the list of extra actuals. Expr is the
2297 -- expression for the value of the actual, EF is the entity for the
2298 -- extra formal.
2299
2300 procedure Add_View_Conversion_Invariants
2301 (Formal : Entity_Id;
2302 Actual : Node_Id);
2303 -- Adds invariant checks for every intermediate type between the range
2304 -- of a view converted argument to its ancestor (from parent to child).
2305
2306 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2307 -- Within an instance, a type derived from an untagged formal derived
2308 -- type inherits from the original parent, not from the actual. The
2309 -- current derivation mechanism has the derived type inherit from the
2310 -- actual, which is only correct outside of the instance. If the
2311 -- subprogram is inherited, we test for this particular case through a
2312 -- convoluted tree traversal before setting the proper subprogram to be
2313 -- called.
2314
2315 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2316 -- Return true if E comes from an instance that is not yet frozen
2317
2318 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2319 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2320
2321 function New_Value (From : Node_Id) return Node_Id;
2322 -- From is the original Expression. New_Value is equivalent to a call
2323 -- to Duplicate_Subexpr with an explicit dereference when From is an
2324 -- access parameter.
2325
2326 --------------------------
2327 -- Add_Actual_Parameter --
2328 --------------------------
2329
2330 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2331 Actual_Expr : constant Node_Id :=
2332 Explicit_Actual_Parameter (Insert_Param);
2333
2334 begin
2335 -- Case of insertion is first named actual
2336
2337 if No (Prev) or else
2338 Nkind (Parent (Prev)) /= N_Parameter_Association
2339 then
2340 Set_Next_Named_Actual
2341 (Insert_Param, First_Named_Actual (Call_Node));
2342 Set_First_Named_Actual (Call_Node, Actual_Expr);
2343
2344 if No (Prev) then
2345 if No (Parameter_Associations (Call_Node)) then
2346 Set_Parameter_Associations (Call_Node, New_List);
2347 end if;
2348
2349 Append (Insert_Param, Parameter_Associations (Call_Node));
2350
2351 else
2352 Insert_After (Prev, Insert_Param);
2353 end if;
2354
2355 -- Case of insertion is not first named actual
2356
2357 else
2358 Set_Next_Named_Actual
2359 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2360 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2361 Append (Insert_Param, Parameter_Associations (Call_Node));
2362 end if;
2363
2364 Prev := Actual_Expr;
2365 end Add_Actual_Parameter;
2366
2367 ----------------------
2368 -- Add_Extra_Actual --
2369 ----------------------
2370
2371 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2372 Loc : constant Source_Ptr := Sloc (Expr);
2373
2374 begin
2375 if Extra_Actuals = No_List then
2376 Extra_Actuals := New_List;
2377 Set_Parent (Extra_Actuals, Call_Node);
2378 end if;
2379
2380 Append_To (Extra_Actuals,
2381 Make_Parameter_Association (Loc,
2382 Selector_Name => New_Occurrence_Of (EF, Loc),
2383 Explicit_Actual_Parameter => Expr));
2384
2385 Analyze_And_Resolve (Expr, Etype (EF));
2386
2387 if Nkind (Call_Node) = N_Function_Call then
2388 Set_Is_Accessibility_Actual (Parent (Expr));
2389 end if;
2390 end Add_Extra_Actual;
2391
2392 ------------------------------------
2393 -- Add_View_Conversion_Invariants --
2394 ------------------------------------
2395
2396 procedure Add_View_Conversion_Invariants
2397 (Formal : Entity_Id;
2398 Actual : Node_Id)
2399 is
2400 Arg : Entity_Id;
2401 Curr_Typ : Entity_Id;
2402 Inv_Checks : List_Id;
2403 Par_Typ : Entity_Id;
2404
2405 begin
2406 Inv_Checks := No_List;
2407
2408 -- Extract the argument from a potentially nested set of view
2409 -- conversions.
2410
2411 Arg := Actual;
2412 while Nkind (Arg) = N_Type_Conversion loop
2413 Arg := Expression (Arg);
2414 end loop;
2415
2416 -- Move up the derivation chain starting with the type of the formal
2417 -- parameter down to the type of the actual object.
2418
2419 Curr_Typ := Empty;
2420 Par_Typ := Etype (Arg);
2421 while Par_Typ /= Etype (Formal) and Par_Typ /= Curr_Typ loop
2422 Curr_Typ := Par_Typ;
2423
2424 if Has_Invariants (Curr_Typ)
2425 and then Present (Invariant_Procedure (Curr_Typ))
2426 then
2427 -- Verify the invariate of the current type. Generate:
2428
2429 -- <Curr_Typ>Invariant (Curr_Typ (Arg));
2430
2431 Prepend_New_To (Inv_Checks,
2432 Make_Procedure_Call_Statement (Loc,
2433 Name =>
2434 New_Occurrence_Of
2435 (Invariant_Procedure (Curr_Typ), Loc),
2436 Parameter_Associations => New_List (
2437 Make_Type_Conversion (Loc,
2438 Subtype_Mark => New_Occurrence_Of (Curr_Typ, Loc),
2439 Expression => New_Copy_Tree (Arg)))));
2440 end if;
2441
2442 Par_Typ := Base_Type (Etype (Curr_Typ));
2443 end loop;
2444
2445 if not Is_Empty_List (Inv_Checks) then
2446 Insert_Actions_After (N, Inv_Checks);
2447 end if;
2448 end Add_View_Conversion_Invariants;
2449
2450 ---------------------------
2451 -- Inherited_From_Formal --
2452 ---------------------------
2453
2454 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2455 Par : Entity_Id;
2456 Gen_Par : Entity_Id;
2457 Gen_Prim : Elist_Id;
2458 Elmt : Elmt_Id;
2459 Indic : Node_Id;
2460
2461 begin
2462 -- If the operation is inherited, it is attached to the corresponding
2463 -- type derivation. If the parent in the derivation is a generic
2464 -- actual, it is a subtype of the actual, and we have to recover the
2465 -- original derived type declaration to find the proper parent.
2466
2467 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2468 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2469 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2470 N_Derived_Type_Definition
2471 or else not In_Instance
2472 then
2473 return Empty;
2474
2475 else
2476 Indic :=
2477 Subtype_Indication
2478 (Type_Definition (Original_Node (Parent (S))));
2479
2480 if Nkind (Indic) = N_Subtype_Indication then
2481 Par := Entity (Subtype_Mark (Indic));
2482 else
2483 Par := Entity (Indic);
2484 end if;
2485 end if;
2486
2487 if not Is_Generic_Actual_Type (Par)
2488 or else Is_Tagged_Type (Par)
2489 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2490 or else not In_Open_Scopes (Scope (Par))
2491 then
2492 return Empty;
2493 else
2494 Gen_Par := Generic_Parent_Type (Parent (Par));
2495 end if;
2496
2497 -- If the actual has no generic parent type, the formal is not
2498 -- a formal derived type, so nothing to inherit.
2499
2500 if No (Gen_Par) then
2501 return Empty;
2502 end if;
2503
2504 -- If the generic parent type is still the generic type, this is a
2505 -- private formal, not a derived formal, and there are no operations
2506 -- inherited from the formal.
2507
2508 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2509 return Empty;
2510 end if;
2511
2512 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2513
2514 Elmt := First_Elmt (Gen_Prim);
2515 while Present (Elmt) loop
2516 if Chars (Node (Elmt)) = Chars (S) then
2517 declare
2518 F1 : Entity_Id;
2519 F2 : Entity_Id;
2520
2521 begin
2522 F1 := First_Formal (S);
2523 F2 := First_Formal (Node (Elmt));
2524 while Present (F1)
2525 and then Present (F2)
2526 loop
2527 if Etype (F1) = Etype (F2)
2528 or else Etype (F2) = Gen_Par
2529 then
2530 Next_Formal (F1);
2531 Next_Formal (F2);
2532 else
2533 Next_Elmt (Elmt);
2534 exit; -- not the right subprogram
2535 end if;
2536
2537 return Node (Elmt);
2538 end loop;
2539 end;
2540
2541 else
2542 Next_Elmt (Elmt);
2543 end if;
2544 end loop;
2545
2546 raise Program_Error;
2547 end Inherited_From_Formal;
2548
2549 --------------------------
2550 -- In_Unfrozen_Instance --
2551 --------------------------
2552
2553 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2554 S : Entity_Id;
2555
2556 begin
2557 S := E;
2558 while Present (S) and then S /= Standard_Standard loop
2559 if Is_Generic_Instance (S)
2560 and then Present (Freeze_Node (S))
2561 and then not Analyzed (Freeze_Node (S))
2562 then
2563 return True;
2564 end if;
2565
2566 S := Scope (S);
2567 end loop;
2568
2569 return False;
2570 end In_Unfrozen_Instance;
2571
2572 -------------------------
2573 -- Is_Direct_Deep_Call --
2574 -------------------------
2575
2576 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2577 begin
2578 if Is_TSS (Subp, TSS_Deep_Adjust)
2579 or else Is_TSS (Subp, TSS_Deep_Finalize)
2580 or else Is_TSS (Subp, TSS_Deep_Initialize)
2581 then
2582 declare
2583 Actual : Node_Id;
2584 Formal : Node_Id;
2585
2586 begin
2587 Actual := First (Parameter_Associations (N));
2588 Formal := First_Formal (Subp);
2589 while Present (Actual)
2590 and then Present (Formal)
2591 loop
2592 if Nkind (Actual) = N_Identifier
2593 and then Is_Controlling_Actual (Actual)
2594 and then Etype (Actual) = Etype (Formal)
2595 then
2596 return True;
2597 end if;
2598
2599 Next (Actual);
2600 Next_Formal (Formal);
2601 end loop;
2602 end;
2603 end if;
2604
2605 return False;
2606 end Is_Direct_Deep_Call;
2607
2608 ---------------
2609 -- New_Value --
2610 ---------------
2611
2612 function New_Value (From : Node_Id) return Node_Id is
2613 Res : constant Node_Id := Duplicate_Subexpr (From);
2614 begin
2615 if Is_Access_Type (Etype (From)) then
2616 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2617 else
2618 return Res;
2619 end if;
2620 end New_Value;
2621
2622 -- Local variables
2623
2624 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2625 Actual : Node_Id;
2626 Formal : Entity_Id;
2627 Orig_Subp : Entity_Id := Empty;
2628 Param_Count : Natural := 0;
2629 Parent_Formal : Entity_Id;
2630 Parent_Subp : Entity_Id;
2631 Pref_Entity : Entity_Id;
2632 Scop : Entity_Id;
2633 Subp : Entity_Id;
2634
2635 Prev_Orig : Node_Id;
2636 -- Original node for an actual, which may have been rewritten. If the
2637 -- actual is a function call that has been transformed from a selected
2638 -- component, the original node is unanalyzed. Otherwise, it carries
2639 -- semantic information used to generate additional actuals.
2640
2641 CW_Interface_Formals_Present : Boolean := False;
2642
2643 -- Start of processing for Expand_Call_Helper
2644
2645 begin
2646 Post_Call := New_List;
2647
2648 -- Expand the function or procedure call if the first actual has a
2649 -- declared dimension aspect, and the subprogram is declared in one
2650 -- of the dimension I/O packages.
2651
2652 if Ada_Version >= Ada_2012
2653 and then
2654 Nkind_In (Call_Node, N_Procedure_Call_Statement, N_Function_Call)
2655 and then Present (Parameter_Associations (Call_Node))
2656 then
2657 Expand_Put_Call_With_Symbol (Call_Node);
2658 end if;
2659
2660 -- Ignore if previous error
2661
2662 if Nkind (Call_Node) in N_Has_Etype
2663 and then Etype (Call_Node) = Any_Type
2664 then
2665 return;
2666 end if;
2667
2668 -- Call using access to subprogram with explicit dereference
2669
2670 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2671 Subp := Etype (Name (Call_Node));
2672 Parent_Subp := Empty;
2673
2674 -- Case of call to simple entry, where the Name is a selected component
2675 -- whose prefix is the task, and whose selector name is the entry name
2676
2677 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2678 Subp := Entity (Selector_Name (Name (Call_Node)));
2679 Parent_Subp := Empty;
2680
2681 -- Case of call to member of entry family, where Name is an indexed
2682 -- component, with the prefix being a selected component giving the
2683 -- task and entry family name, and the index being the entry index.
2684
2685 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2686 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2687 Parent_Subp := Empty;
2688
2689 -- Normal case
2690
2691 else
2692 Subp := Entity (Name (Call_Node));
2693 Parent_Subp := Alias (Subp);
2694
2695 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2696 -- if we can tell that the first parameter cannot possibly be null.
2697 -- This improves efficiency by avoiding a run-time test.
2698
2699 -- We do not do this if Raise_Exception_Always does not exist, which
2700 -- can happen in configurable run time profiles which provide only a
2701 -- Raise_Exception.
2702
2703 if Is_RTE (Subp, RE_Raise_Exception)
2704 and then RTE_Available (RE_Raise_Exception_Always)
2705 then
2706 declare
2707 FA : constant Node_Id :=
2708 Original_Node (First_Actual (Call_Node));
2709
2710 begin
2711 -- The case we catch is where the first argument is obtained
2712 -- using the Identity attribute (which must always be
2713 -- non-null).
2714
2715 if Nkind (FA) = N_Attribute_Reference
2716 and then Attribute_Name (FA) = Name_Identity
2717 then
2718 Subp := RTE (RE_Raise_Exception_Always);
2719 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2720 end if;
2721 end;
2722 end if;
2723
2724 if Ekind (Subp) = E_Entry then
2725 Parent_Subp := Empty;
2726 end if;
2727 end if;
2728
2729 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2730 -- alternative in an asynchronous select or as an entry call in
2731 -- a conditional or timed select. Check whether the procedure call
2732 -- is a renaming of an entry and rewrite it as an entry call.
2733
2734 if Ada_Version >= Ada_2005
2735 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2736 and then
2737 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2738 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2739 or else
2740 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2741 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2742 then
2743 declare
2744 Ren_Decl : Node_Id;
2745 Ren_Root : Entity_Id := Subp;
2746
2747 begin
2748 -- This may be a chain of renamings, find the root
2749
2750 if Present (Alias (Ren_Root)) then
2751 Ren_Root := Alias (Ren_Root);
2752 end if;
2753
2754 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2755 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2756
2757 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2758 Rewrite (Call_Node,
2759 Make_Entry_Call_Statement (Loc,
2760 Name =>
2761 New_Copy_Tree (Name (Ren_Decl)),
2762 Parameter_Associations =>
2763 New_Copy_List_Tree
2764 (Parameter_Associations (Call_Node))));
2765
2766 return;
2767 end if;
2768 end if;
2769 end;
2770 end if;
2771
2772 if Modify_Tree_For_C
2773 and then Nkind (Call_Node) = N_Function_Call
2774 and then Is_Entity_Name (Name (Call_Node))
2775 then
2776 declare
2777 Func_Id : constant Entity_Id :=
2778 Ultimate_Alias (Entity (Name (Call_Node)));
2779 begin
2780 -- When generating C code, transform a function call that returns
2781 -- a constrained array type into procedure form.
2782
2783 if Rewritten_For_C (Func_Id) then
2784
2785 -- For internally generated calls ensure that they reference
2786 -- the entity of the spec of the called function (needed since
2787 -- the expander may generate calls using the entity of their
2788 -- body). See for example Expand_Boolean_Operator().
2789
2790 if not (Comes_From_Source (Call_Node))
2791 and then Nkind (Unit_Declaration_Node (Func_Id)) =
2792 N_Subprogram_Body
2793 then
2794 Set_Entity (Name (Call_Node),
2795 Corresponding_Function
2796 (Corresponding_Procedure (Func_Id)));
2797 end if;
2798
2799 Rewrite_Function_Call_For_C (Call_Node);
2800 return;
2801
2802 -- Also introduce a temporary for functions that return a record
2803 -- called within another procedure or function call, since records
2804 -- are passed by pointer in the generated C code, and we cannot
2805 -- take a pointer from a subprogram call.
2806
2807 elsif Nkind (Parent (Call_Node)) in N_Subprogram_Call
2808 and then Is_Record_Type (Etype (Func_Id))
2809 then
2810 declare
2811 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
2812 Decl : Node_Id;
2813
2814 begin
2815 -- Generate:
2816 -- Temp : ... := Func_Call (...);
2817
2818 Decl :=
2819 Make_Object_Declaration (Loc,
2820 Defining_Identifier => Temp_Id,
2821 Object_Definition =>
2822 New_Occurrence_Of (Etype (Func_Id), Loc),
2823 Expression =>
2824 Make_Function_Call (Loc,
2825 Name =>
2826 New_Occurrence_Of (Func_Id, Loc),
2827 Parameter_Associations =>
2828 Parameter_Associations (Call_Node)));
2829
2830 Insert_Action (Parent (Call_Node), Decl);
2831 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
2832 return;
2833 end;
2834 end if;
2835 end;
2836 end if;
2837
2838 -- First step, compute extra actuals, corresponding to any Extra_Formals
2839 -- present. Note that we do not access Extra_Formals directly, instead
2840 -- we simply note the presence of the extra formals as we process the
2841 -- regular formals collecting corresponding actuals in Extra_Actuals.
2842
2843 -- We also generate any required range checks for actuals for in formals
2844 -- as we go through the loop, since this is a convenient place to do it.
2845 -- (Though it seems that this would be better done in Expand_Actuals???)
2846
2847 -- Special case: Thunks must not compute the extra actuals; they must
2848 -- just propagate to the target primitive their extra actuals.
2849
2850 if Is_Thunk (Current_Scope)
2851 and then Thunk_Entity (Current_Scope) = Subp
2852 and then Present (Extra_Formals (Subp))
2853 then
2854 pragma Assert (Present (Extra_Formals (Current_Scope)));
2855
2856 declare
2857 Target_Formal : Entity_Id;
2858 Thunk_Formal : Entity_Id;
2859
2860 begin
2861 Target_Formal := Extra_Formals (Subp);
2862 Thunk_Formal := Extra_Formals (Current_Scope);
2863 while Present (Target_Formal) loop
2864 Add_Extra_Actual
2865 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
2866 EF => Thunk_Formal);
2867
2868 Target_Formal := Extra_Formal (Target_Formal);
2869 Thunk_Formal := Extra_Formal (Thunk_Formal);
2870 end loop;
2871
2872 while Is_Non_Empty_List (Extra_Actuals) loop
2873 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2874 end loop;
2875
2876 Expand_Actuals (Call_Node, Subp, Post_Call);
2877 pragma Assert (Is_Empty_List (Post_Call));
2878 return;
2879 end;
2880 end if;
2881
2882 Formal := First_Formal (Subp);
2883 Actual := First_Actual (Call_Node);
2884 Param_Count := 1;
2885 while Present (Formal) loop
2886
2887 -- Generate range check if required
2888
2889 if Do_Range_Check (Actual)
2890 and then Ekind (Formal) = E_In_Parameter
2891 then
2892 Generate_Range_Check
2893 (Actual, Etype (Formal), CE_Range_Check_Failed);
2894 end if;
2895
2896 -- Prepare to examine current entry
2897
2898 Prev := Actual;
2899 Prev_Orig := Original_Node (Prev);
2900
2901 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2902 -- to expand it in a further round.
2903
2904 CW_Interface_Formals_Present :=
2905 CW_Interface_Formals_Present
2906 or else
2907 (Is_Class_Wide_Type (Etype (Formal))
2908 and then Is_Interface (Etype (Etype (Formal))))
2909 or else
2910 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2911 and then Is_Class_Wide_Type (Directly_Designated_Type
2912 (Etype (Etype (Formal))))
2913 and then Is_Interface (Directly_Designated_Type
2914 (Etype (Etype (Formal)))));
2915
2916 -- Create possible extra actual for constrained case. Usually, the
2917 -- extra actual is of the form actual'constrained, but since this
2918 -- attribute is only available for unconstrained records, TRUE is
2919 -- expanded if the type of the formal happens to be constrained (for
2920 -- instance when this procedure is inherited from an unconstrained
2921 -- record to a constrained one) or if the actual has no discriminant
2922 -- (its type is constrained). An exception to this is the case of a
2923 -- private type without discriminants. In this case we pass FALSE
2924 -- because the object has underlying discriminants with defaults.
2925
2926 if Present (Extra_Constrained (Formal)) then
2927 if Ekind (Etype (Prev)) in Private_Kind
2928 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2929 then
2930 Add_Extra_Actual
2931 (Expr => New_Occurrence_Of (Standard_False, Loc),
2932 EF => Extra_Constrained (Formal));
2933
2934 elsif Is_Constrained (Etype (Formal))
2935 or else not Has_Discriminants (Etype (Prev))
2936 then
2937 Add_Extra_Actual
2938 (Expr => New_Occurrence_Of (Standard_True, Loc),
2939 EF => Extra_Constrained (Formal));
2940
2941 -- Do not produce extra actuals for Unchecked_Union parameters.
2942 -- Jump directly to the end of the loop.
2943
2944 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2945 goto Skip_Extra_Actual_Generation;
2946
2947 else
2948 -- If the actual is a type conversion, then the constrained
2949 -- test applies to the actual, not the target type.
2950
2951 declare
2952 Act_Prev : Node_Id;
2953
2954 begin
2955 -- Test for unchecked conversions as well, which can occur
2956 -- as out parameter actuals on calls to stream procedures.
2957
2958 Act_Prev := Prev;
2959 while Nkind_In (Act_Prev, N_Type_Conversion,
2960 N_Unchecked_Type_Conversion)
2961 loop
2962 Act_Prev := Expression (Act_Prev);
2963 end loop;
2964
2965 -- If the expression is a conversion of a dereference, this
2966 -- is internally generated code that manipulates addresses,
2967 -- e.g. when building interface tables. No check should
2968 -- occur in this case, and the discriminated object is not
2969 -- directly a hand.
2970
2971 if not Comes_From_Source (Actual)
2972 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2973 and then Nkind (Act_Prev) = N_Explicit_Dereference
2974 then
2975 Add_Extra_Actual
2976 (Expr => New_Occurrence_Of (Standard_False, Loc),
2977 EF => Extra_Constrained (Formal));
2978
2979 else
2980 Add_Extra_Actual
2981 (Expr =>
2982 Make_Attribute_Reference (Sloc (Prev),
2983 Prefix =>
2984 Duplicate_Subexpr_No_Checks
2985 (Act_Prev, Name_Req => True),
2986 Attribute_Name => Name_Constrained),
2987 EF => Extra_Constrained (Formal));
2988 end if;
2989 end;
2990 end if;
2991 end if;
2992
2993 -- Create possible extra actual for accessibility level
2994
2995 if Present (Extra_Accessibility (Formal)) then
2996
2997 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2998 -- attribute, then the original actual may be an aliased object
2999 -- occurring as the prefix in a call using "Object.Operation"
3000 -- notation. In that case we must pass the level of the object,
3001 -- so Prev_Orig is reset to Prev and the attribute will be
3002 -- processed by the code for Access attributes further below.
3003
3004 if Prev_Orig /= Prev
3005 and then Nkind (Prev) = N_Attribute_Reference
3006 and then Get_Attribute_Id (Attribute_Name (Prev)) =
3007 Attribute_Access
3008 and then Is_Aliased_View (Prev_Orig)
3009 then
3010 Prev_Orig := Prev;
3011
3012 -- A class-wide precondition generates a test in which formals of
3013 -- the subprogram are replaced by actuals that came from source.
3014 -- In that case as well, the accessiblity comes from the actual.
3015 -- This is the one case in which there are references to formals
3016 -- outside of their subprogram.
3017
3018 elsif Prev_Orig /= Prev
3019 and then Is_Entity_Name (Prev_Orig)
3020 and then Present (Entity (Prev_Orig))
3021 and then Is_Formal (Entity (Prev_Orig))
3022 and then not In_Open_Scopes (Scope (Entity (Prev_Orig)))
3023 then
3024 Prev_Orig := Prev;
3025
3026 -- If the actual is a formal of an enclosing subprogram it is
3027 -- the right entity, even if it is a rewriting. This happens
3028 -- when the call is within an inherited condition or predicate.
3029
3030 elsif Is_Entity_Name (Actual)
3031 and then Is_Formal (Entity (Actual))
3032 and then In_Open_Scopes (Scope (Entity (Actual)))
3033 then
3034 Prev_Orig := Prev;
3035
3036 elsif Nkind (Prev_Orig) = N_Type_Conversion then
3037 Prev_Orig := Expression (Prev_Orig);
3038 end if;
3039
3040 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3041 -- accessibility levels.
3042
3043 if Is_Thunk (Current_Scope) then
3044 declare
3045 Parm_Ent : Entity_Id;
3046
3047 begin
3048 if Is_Controlling_Actual (Actual) then
3049
3050 -- Find the corresponding actual of the thunk
3051
3052 Parm_Ent := First_Entity (Current_Scope);
3053 for J in 2 .. Param_Count loop
3054 Next_Entity (Parm_Ent);
3055 end loop;
3056
3057 -- Handle unchecked conversion of access types generated
3058 -- in thunks (cf. Expand_Interface_Thunk).
3059
3060 elsif Is_Access_Type (Etype (Actual))
3061 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3062 then
3063 Parm_Ent := Entity (Expression (Actual));
3064
3065 else pragma Assert (Is_Entity_Name (Actual));
3066 Parm_Ent := Entity (Actual);
3067 end if;
3068
3069 Add_Extra_Actual
3070 (Expr =>
3071 New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
3072 EF => Extra_Accessibility (Formal));
3073 end;
3074
3075 elsif Is_Entity_Name (Prev_Orig) then
3076
3077 -- When passing an access parameter, or a renaming of an access
3078 -- parameter, as the actual to another access parameter we need
3079 -- to pass along the actual's own access level parameter. This
3080 -- is done if we are within the scope of the formal access
3081 -- parameter (if this is an inlined body the extra formal is
3082 -- irrelevant).
3083
3084 if (Is_Formal (Entity (Prev_Orig))
3085 or else
3086 (Present (Renamed_Object (Entity (Prev_Orig)))
3087 and then
3088 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
3089 and then
3090 Is_Formal
3091 (Entity (Renamed_Object (Entity (Prev_Orig))))))
3092 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
3093 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
3094 then
3095 declare
3096 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
3097
3098 begin
3099 pragma Assert (Present (Parm_Ent));
3100
3101 if Present (Extra_Accessibility (Parm_Ent)) then
3102 Add_Extra_Actual
3103 (Expr =>
3104 New_Occurrence_Of
3105 (Extra_Accessibility (Parm_Ent), Loc),
3106 EF => Extra_Accessibility (Formal));
3107
3108 -- If the actual access parameter does not have an
3109 -- associated extra formal providing its scope level,
3110 -- then treat the actual as having library-level
3111 -- accessibility.
3112
3113 else
3114 Add_Extra_Actual
3115 (Expr =>
3116 Make_Integer_Literal (Loc,
3117 Intval => Scope_Depth (Standard_Standard)),
3118 EF => Extra_Accessibility (Formal));
3119 end if;
3120 end;
3121
3122 -- The actual is a normal access value, so just pass the level
3123 -- of the actual's access type.
3124
3125 else
3126 Add_Extra_Actual
3127 (Expr => Dynamic_Accessibility_Level (Prev_Orig),
3128 EF => Extra_Accessibility (Formal));
3129 end if;
3130
3131 -- If the actual is an access discriminant, then pass the level
3132 -- of the enclosing object (RM05-3.10.2(12.4/2)).
3133
3134 elsif Nkind (Prev_Orig) = N_Selected_Component
3135 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
3136 E_Discriminant
3137 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
3138 E_Anonymous_Access_Type
3139 then
3140 Add_Extra_Actual
3141 (Expr =>
3142 Make_Integer_Literal (Loc,
3143 Intval => Object_Access_Level (Prefix (Prev_Orig))),
3144 EF => Extra_Accessibility (Formal));
3145
3146 -- All other cases
3147
3148 else
3149 case Nkind (Prev_Orig) is
3150 when N_Attribute_Reference =>
3151 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
3152
3153 -- For X'Access, pass on the level of the prefix X
3154
3155 when Attribute_Access =>
3156
3157 -- Accessibility level of S'Access is that of A
3158
3159 Prev_Orig := Prefix (Prev_Orig);
3160
3161 -- If the expression is a view conversion, the
3162 -- accessibility level is that of the expression.
3163
3164 if Nkind (Original_Node (Prev_Orig)) =
3165 N_Type_Conversion
3166 and then
3167 Nkind (Expression (Original_Node (Prev_Orig))) =
3168 N_Explicit_Dereference
3169 then
3170 Prev_Orig :=
3171 Expression (Original_Node (Prev_Orig));
3172 end if;
3173
3174 -- If this is an Access attribute applied to the
3175 -- the current instance object passed to a type
3176 -- initialization procedure, then use the level
3177 -- of the type itself. This is not really correct,
3178 -- as there should be an extra level parameter
3179 -- passed in with _init formals (only in the case
3180 -- where the type is immutably limited), but we
3181 -- don't have an easy way currently to create such
3182 -- an extra formal (init procs aren't ever frozen).
3183 -- For now we just use the level of the type,
3184 -- which may be too shallow, but that works better
3185 -- than passing Object_Access_Level of the type,
3186 -- which can be one level too deep in some cases.
3187 -- ???
3188
3189 -- A further case that requires special handling
3190 -- is the common idiom E.all'access. If E is a
3191 -- formal of the enclosing subprogram, the
3192 -- accessibility of the expression is that of E.
3193
3194 if Is_Entity_Name (Prev_Orig) then
3195 Pref_Entity := Entity (Prev_Orig);
3196
3197 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3198 and then Is_Entity_Name (Prefix (Prev_Orig))
3199 then
3200 Pref_Entity := Entity (Prefix ((Prev_Orig)));
3201
3202 else
3203 Pref_Entity := Empty;
3204 end if;
3205
3206 if Is_Entity_Name (Prev_Orig)
3207 and then Is_Type (Entity (Prev_Orig))
3208 then
3209 Add_Extra_Actual
3210 (Expr =>
3211 Make_Integer_Literal (Loc,
3212 Intval =>
3213 Type_Access_Level (Pref_Entity)),
3214 EF => Extra_Accessibility (Formal));
3215
3216 elsif Nkind (Prev_Orig) = N_Explicit_Dereference
3217 and then Present (Pref_Entity)
3218 and then Is_Formal (Pref_Entity)
3219 and then Present
3220 (Extra_Accessibility (Pref_Entity))
3221 then
3222 Add_Extra_Actual
3223 (Expr =>
3224 New_Occurrence_Of
3225 (Extra_Accessibility (Pref_Entity), Loc),
3226 EF => Extra_Accessibility (Formal));
3227
3228 else
3229 Add_Extra_Actual
3230 (Expr =>
3231 Make_Integer_Literal (Loc,
3232 Intval =>
3233 Object_Access_Level (Prev_Orig)),
3234 EF => Extra_Accessibility (Formal));
3235 end if;
3236
3237 -- Treat the unchecked attributes as library-level
3238
3239 when Attribute_Unchecked_Access
3240 | Attribute_Unrestricted_Access
3241 =>
3242 Add_Extra_Actual
3243 (Expr =>
3244 Make_Integer_Literal (Loc,
3245 Intval => Scope_Depth (Standard_Standard)),
3246 EF => Extra_Accessibility (Formal));
3247
3248 -- No other cases of attributes returning access
3249 -- values that can be passed to access parameters.
3250
3251 when others =>
3252 raise Program_Error;
3253
3254 end case;
3255
3256 -- For allocators we pass the level of the execution of the
3257 -- called subprogram, which is one greater than the current
3258 -- scope level.
3259
3260 when N_Allocator =>
3261 Add_Extra_Actual
3262 (Expr =>
3263 Make_Integer_Literal (Loc,
3264 Intval => Scope_Depth (Current_Scope) + 1),
3265 EF => Extra_Accessibility (Formal));
3266
3267 -- For most other cases we simply pass the level of the
3268 -- actual's access type. The type is retrieved from
3269 -- Prev rather than Prev_Orig, because in some cases
3270 -- Prev_Orig denotes an original expression that has
3271 -- not been analyzed.
3272
3273 when others =>
3274 Add_Extra_Actual
3275 (Expr => Dynamic_Accessibility_Level (Prev),
3276 EF => Extra_Accessibility (Formal));
3277 end case;
3278 end if;
3279 end if;
3280
3281 -- Perform the check of 4.6(49) that prevents a null value from being
3282 -- passed as an actual to an access parameter. Note that the check
3283 -- is elided in the common cases of passing an access attribute or
3284 -- access parameter as an actual. Also, we currently don't enforce
3285 -- this check for expander-generated actuals and when -gnatdj is set.
3286
3287 if Ada_Version >= Ada_2005 then
3288
3289 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3290 -- the intent of 6.4.1(13) is that null-exclusion checks should
3291 -- not be done for 'out' parameters, even though it refers only
3292 -- to constraint checks, and a null_exclusion is not a constraint.
3293 -- Note that AI05-0196-1 corrects this mistake in the RM.
3294
3295 if Is_Access_Type (Etype (Formal))
3296 and then Can_Never_Be_Null (Etype (Formal))
3297 and then Ekind (Formal) /= E_Out_Parameter
3298 and then Nkind (Prev) /= N_Raise_Constraint_Error
3299 and then (Known_Null (Prev)
3300 or else not Can_Never_Be_Null (Etype (Prev)))
3301 then
3302 Install_Null_Excluding_Check (Prev);
3303 end if;
3304
3305 -- Ada_Version < Ada_2005
3306
3307 else
3308 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3309 or else Access_Checks_Suppressed (Subp)
3310 then
3311 null;
3312
3313 elsif Debug_Flag_J then
3314 null;
3315
3316 elsif not Comes_From_Source (Prev) then
3317 null;
3318
3319 elsif Is_Entity_Name (Prev)
3320 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3321 then
3322 null;
3323
3324 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3325 null;
3326
3327 else
3328 Install_Null_Excluding_Check (Prev);
3329 end if;
3330 end if;
3331
3332 -- Perform appropriate validity checks on parameters that
3333 -- are entities.
3334
3335 if Validity_Checks_On then
3336 if (Ekind (Formal) = E_In_Parameter
3337 and then Validity_Check_In_Params)
3338 or else
3339 (Ekind (Formal) = E_In_Out_Parameter
3340 and then Validity_Check_In_Out_Params)
3341 then
3342 -- If the actual is an indexed component of a packed type (or
3343 -- is an indexed or selected component whose prefix recursively
3344 -- meets this condition), it has not been expanded yet. It will
3345 -- be copied in the validity code that follows, and has to be
3346 -- expanded appropriately, so reanalyze it.
3347
3348 -- What we do is just to unset analyzed bits on prefixes till
3349 -- we reach something that does not have a prefix.
3350
3351 declare
3352 Nod : Node_Id;
3353
3354 begin
3355 Nod := Actual;
3356 while Nkind_In (Nod, N_Indexed_Component,
3357 N_Selected_Component)
3358 loop
3359 Set_Analyzed (Nod, False);
3360 Nod := Prefix (Nod);
3361 end loop;
3362 end;
3363
3364 Ensure_Valid (Actual);
3365 end if;
3366 end if;
3367
3368 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3369 -- since this is a left side reference. We only do this for calls
3370 -- from the source program since we assume that compiler generated
3371 -- calls explicitly generate any required checks. We also need it
3372 -- only if we are doing standard validity checks, since clearly it is
3373 -- not needed if validity checks are off, and in subscript validity
3374 -- checking mode, all indexed components are checked with a call
3375 -- directly from Expand_N_Indexed_Component.
3376
3377 if Comes_From_Source (Call_Node)
3378 and then Ekind (Formal) /= E_In_Parameter
3379 and then Validity_Checks_On
3380 and then Validity_Check_Default
3381 and then not Validity_Check_Subscripts
3382 then
3383 Check_Valid_Lvalue_Subscripts (Actual);
3384 end if;
3385
3386 -- Mark any scalar OUT parameter that is a simple variable as no
3387 -- longer known to be valid (unless the type is always valid). This
3388 -- reflects the fact that if an OUT parameter is never set in a
3389 -- procedure, then it can become invalid on the procedure return.
3390
3391 if Ekind (Formal) = E_Out_Parameter
3392 and then Is_Entity_Name (Actual)
3393 and then Ekind (Entity (Actual)) = E_Variable
3394 and then not Is_Known_Valid (Etype (Actual))
3395 then
3396 Set_Is_Known_Valid (Entity (Actual), False);
3397 end if;
3398
3399 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3400 -- clear current values, since they can be clobbered. We are probably
3401 -- doing this in more places than we need to, but better safe than
3402 -- sorry when it comes to retaining bad current values.
3403
3404 if Ekind (Formal) /= E_In_Parameter
3405 and then Is_Entity_Name (Actual)
3406 and then Present (Entity (Actual))
3407 then
3408 declare
3409 Ent : constant Entity_Id := Entity (Actual);
3410 Sav : Node_Id;
3411
3412 begin
3413 -- For an OUT or IN OUT parameter that is an assignable entity,
3414 -- we do not want to clobber the Last_Assignment field, since
3415 -- if it is set, it was precisely because it is indeed an OUT
3416 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3417 -- since the subprogram could have returned in invalid value.
3418
3419 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3420 and then Is_Assignable (Ent)
3421 then
3422 Sav := Last_Assignment (Ent);
3423 Kill_Current_Values (Ent);
3424 Set_Last_Assignment (Ent, Sav);
3425 Set_Is_Known_Valid (Ent, False);
3426
3427 -- For all other cases, just kill the current values
3428
3429 else
3430 Kill_Current_Values (Ent);
3431 end if;
3432 end;
3433 end if;
3434
3435 -- If the formal is class wide and the actual is an aggregate, force
3436 -- evaluation so that the back end who does not know about class-wide
3437 -- type, does not generate a temporary of the wrong size.
3438
3439 if not Is_Class_Wide_Type (Etype (Formal)) then
3440 null;
3441
3442 elsif Nkind (Actual) = N_Aggregate
3443 or else (Nkind (Actual) = N_Qualified_Expression
3444 and then Nkind (Expression (Actual)) = N_Aggregate)
3445 then
3446 Force_Evaluation (Actual);
3447 end if;
3448
3449 -- In a remote call, if the formal is of a class-wide type, check
3450 -- that the actual meets the requirements described in E.4(18).
3451
3452 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3453 Insert_Action (Actual,
3454 Make_Transportable_Check (Loc,
3455 Duplicate_Subexpr_Move_Checks (Actual)));
3456 end if;
3457
3458 -- Perform invariant checks for all intermediate types in a view
3459 -- conversion after successful return from a call that passes the
3460 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
3461 -- 13/3, 14/3)). Consider only source conversion in order to avoid
3462 -- generating spurious checks on complex expansion such as object
3463 -- initialization through an extension aggregate.
3464
3465 if Comes_From_Source (N)
3466 and then Ekind (Formal) /= E_In_Parameter
3467 and then Nkind (Actual) = N_Type_Conversion
3468 then
3469 Add_View_Conversion_Invariants (Formal, Actual);
3470 end if;
3471
3472 -- Generating C the initialization of an allocator is performed by
3473 -- means of individual statements, and hence it must be done before
3474 -- the call.
3475
3476 if Modify_Tree_For_C
3477 and then Nkind (Actual) = N_Allocator
3478 and then Nkind (Expression (Actual)) = N_Qualified_Expression
3479 then
3480 Remove_Side_Effects (Actual);
3481 end if;
3482
3483 -- This label is required when skipping extra actual generation for
3484 -- Unchecked_Union parameters.
3485
3486 <<Skip_Extra_Actual_Generation>>
3487
3488 Param_Count := Param_Count + 1;
3489 Next_Actual (Actual);
3490 Next_Formal (Formal);
3491 end loop;
3492
3493 -- If we are calling an Ada 2012 function which needs to have the
3494 -- "accessibility level determined by the point of call" (AI05-0234)
3495 -- passed in to it, then pass it in.
3496
3497 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3498 and then
3499 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3500 then
3501 declare
3502 Ancestor : Node_Id := Parent (Call_Node);
3503 Level : Node_Id := Empty;
3504 Defer : Boolean := False;
3505
3506 begin
3507 -- Unimplemented: if Subp returns an anonymous access type, then
3508
3509 -- a) if the call is the operand of an explict conversion, then
3510 -- the target type of the conversion (a named access type)
3511 -- determines the accessibility level pass in;
3512
3513 -- b) if the call defines an access discriminant of an object
3514 -- (e.g., the discriminant of an object being created by an
3515 -- allocator, or the discriminant of a function result),
3516 -- then the accessibility level to pass in is that of the
3517 -- discriminated object being initialized).
3518
3519 -- ???
3520
3521 while Nkind (Ancestor) = N_Qualified_Expression
3522 loop
3523 Ancestor := Parent (Ancestor);
3524 end loop;
3525
3526 case Nkind (Ancestor) is
3527 when N_Allocator =>
3528
3529 -- At this point, we'd like to assign
3530
3531 -- Level := Dynamic_Accessibility_Level (Ancestor);
3532
3533 -- but Etype of Ancestor may not have been set yet,
3534 -- so that doesn't work.
3535
3536 -- Handle this later in Expand_Allocator_Expression.
3537
3538 Defer := True;
3539
3540 when N_Object_Declaration
3541 | N_Object_Renaming_Declaration
3542 =>
3543 declare
3544 Def_Id : constant Entity_Id :=
3545 Defining_Identifier (Ancestor);
3546
3547 begin
3548 if Is_Return_Object (Def_Id) then
3549 if Present (Extra_Accessibility_Of_Result
3550 (Return_Applies_To (Scope (Def_Id))))
3551 then
3552 -- Pass along value that was passed in if the
3553 -- routine we are returning from also has an
3554 -- Accessibility_Of_Result formal.
3555
3556 Level :=
3557 New_Occurrence_Of
3558 (Extra_Accessibility_Of_Result
3559 (Return_Applies_To (Scope (Def_Id))), Loc);
3560 end if;
3561 else
3562 Level :=
3563 Make_Integer_Literal (Loc,
3564 Intval => Object_Access_Level (Def_Id));
3565 end if;
3566 end;
3567
3568 when N_Simple_Return_Statement =>
3569 if Present (Extra_Accessibility_Of_Result
3570 (Return_Applies_To
3571 (Return_Statement_Entity (Ancestor))))
3572 then
3573 -- Pass along value that was passed in if the returned
3574 -- routine also has an Accessibility_Of_Result formal.
3575
3576 Level :=
3577 New_Occurrence_Of
3578 (Extra_Accessibility_Of_Result
3579 (Return_Applies_To
3580 (Return_Statement_Entity (Ancestor))), Loc);
3581 end if;
3582
3583 when others =>
3584 null;
3585 end case;
3586
3587 if not Defer then
3588 if not Present (Level) then
3589
3590 -- The "innermost master that evaluates the function call".
3591
3592 -- ??? - Should we use Integer'Last here instead in order
3593 -- to deal with (some of) the problems associated with
3594 -- calls to subps whose enclosing scope is unknown (e.g.,
3595 -- Anon_Access_To_Subp_Param.all)?
3596
3597 Level :=
3598 Make_Integer_Literal (Loc,
3599 Intval => Scope_Depth (Current_Scope) + 1);
3600 end if;
3601
3602 Add_Extra_Actual
3603 (Expr => Level,
3604 EF =>
3605 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3606 end if;
3607 end;
3608 end if;
3609
3610 -- If we are expanding the RHS of an assignment we need to check if tag
3611 -- propagation is needed. You might expect this processing to be in
3612 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3613 -- assignment might be transformed to a declaration for an unconstrained
3614 -- value if the expression is classwide.
3615
3616 if Nkind (Call_Node) = N_Function_Call
3617 and then Is_Tag_Indeterminate (Call_Node)
3618 and then Is_Entity_Name (Name (Call_Node))
3619 then
3620 declare
3621 Ass : Node_Id := Empty;
3622
3623 begin
3624 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3625 Ass := Parent (Call_Node);
3626
3627 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3628 and then Nkind (Parent (Parent (Call_Node))) =
3629 N_Assignment_Statement
3630 then
3631 Ass := Parent (Parent (Call_Node));
3632
3633 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3634 and then Nkind (Parent (Parent (Call_Node))) =
3635 N_Assignment_Statement
3636 then
3637 Ass := Parent (Parent (Call_Node));
3638 end if;
3639
3640 if Present (Ass)
3641 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3642 then
3643 if Is_Access_Type (Etype (Call_Node)) then
3644 if Designated_Type (Etype (Call_Node)) /=
3645 Root_Type (Etype (Name (Ass)))
3646 then
3647 Error_Msg_NE
3648 ("tag-indeterminate expression must have designated "
3649 & "type& (RM 5.2 (6))",
3650 Call_Node, Root_Type (Etype (Name (Ass))));
3651 else
3652 Propagate_Tag (Name (Ass), Call_Node);
3653 end if;
3654
3655 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3656 Error_Msg_NE
3657 ("tag-indeterminate expression must have type & "
3658 & "(RM 5.2 (6))",
3659 Call_Node, Root_Type (Etype (Name (Ass))));
3660
3661 else
3662 Propagate_Tag (Name (Ass), Call_Node);
3663 end if;
3664
3665 -- The call will be rewritten as a dispatching call, and
3666 -- expanded as such.
3667
3668 return;
3669 end if;
3670 end;
3671 end if;
3672
3673 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3674 -- it to point to the correct secondary virtual table
3675
3676 if Nkind (Call_Node) in N_Subprogram_Call
3677 and then CW_Interface_Formals_Present
3678 then
3679 Expand_Interface_Actuals (Call_Node);
3680 end if;
3681
3682 -- Deals with Dispatch_Call if we still have a call, before expanding
3683 -- extra actuals since this will be done on the re-analysis of the
3684 -- dispatching call. Note that we do not try to shorten the actual list
3685 -- for a dispatching call, it would not make sense to do so. Expansion
3686 -- of dispatching calls is suppressed for VM targets, because the VM
3687 -- back-ends directly handle the generation of dispatching calls and
3688 -- would have to undo any expansion to an indirect call.
3689
3690 if Nkind (Call_Node) in N_Subprogram_Call
3691 and then Present (Controlling_Argument (Call_Node))
3692 then
3693 declare
3694 Call_Typ : constant Entity_Id := Etype (Call_Node);
3695 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3696 Eq_Prim_Op : Entity_Id := Empty;
3697 New_Call : Node_Id;
3698 Param : Node_Id;
3699 Prev_Call : Node_Id;
3700
3701 begin
3702 if not Is_Limited_Type (Typ) then
3703 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3704 end if;
3705
3706 if Tagged_Type_Expansion then
3707 Expand_Dispatching_Call (Call_Node);
3708
3709 -- The following return is worrisome. Is it really OK to skip
3710 -- all remaining processing in this procedure ???
3711
3712 return;
3713
3714 -- VM targets
3715
3716 else
3717 Apply_Tag_Checks (Call_Node);
3718
3719 -- If this is a dispatching "=", we must first compare the
3720 -- tags so we generate: x.tag = y.tag and then x = y
3721
3722 if Subp = Eq_Prim_Op then
3723
3724 -- Mark the node as analyzed to avoid reanalyzing this
3725 -- dispatching call (which would cause a never-ending loop)
3726
3727 Prev_Call := Relocate_Node (Call_Node);
3728 Set_Analyzed (Prev_Call);
3729
3730 Param := First_Actual (Call_Node);
3731 New_Call :=
3732 Make_And_Then (Loc,
3733 Left_Opnd =>
3734 Make_Op_Eq (Loc,
3735 Left_Opnd =>
3736 Make_Selected_Component (Loc,
3737 Prefix => New_Value (Param),
3738 Selector_Name =>
3739 New_Occurrence_Of
3740 (First_Tag_Component (Typ), Loc)),
3741
3742 Right_Opnd =>
3743 Make_Selected_Component (Loc,
3744 Prefix =>
3745 Unchecked_Convert_To (Typ,
3746 New_Value (Next_Actual (Param))),
3747 Selector_Name =>
3748 New_Occurrence_Of
3749 (First_Tag_Component (Typ), Loc))),
3750 Right_Opnd => Prev_Call);
3751
3752 Rewrite (Call_Node, New_Call);
3753
3754 Analyze_And_Resolve
3755 (Call_Node, Call_Typ, Suppress => All_Checks);
3756 end if;
3757
3758 -- Expansion of a dispatching call results in an indirect call,
3759 -- which in turn causes current values to be killed (see
3760 -- Resolve_Call), so on VM targets we do the call here to
3761 -- ensure consistent warnings between VM and non-VM targets.
3762
3763 Kill_Current_Values;
3764 end if;
3765
3766 -- If this is a dispatching "=" then we must update the reference
3767 -- to the call node because we generated:
3768 -- x.tag = y.tag and then x = y
3769
3770 if Subp = Eq_Prim_Op then
3771 Call_Node := Right_Opnd (Call_Node);
3772 end if;
3773 end;
3774 end if;
3775
3776 -- Similarly, expand calls to RCI subprograms on which pragma
3777 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3778 -- later. Do this only when the call comes from source since we
3779 -- do not want such a rewriting to occur in expanded code.
3780
3781 if Is_All_Remote_Call (Call_Node) then
3782 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3783
3784 -- Similarly, do not add extra actuals for an entry call whose entity
3785 -- is a protected procedure, or for an internal protected subprogram
3786 -- call, because it will be rewritten as a protected subprogram call
3787 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3788
3789 elsif Is_Protected_Type (Scope (Subp))
3790 and then (Ekind (Subp) = E_Procedure
3791 or else Ekind (Subp) = E_Function)
3792 then
3793 null;
3794
3795 -- During that loop we gathered the extra actuals (the ones that
3796 -- correspond to Extra_Formals), so now they can be appended.
3797
3798 else
3799 while Is_Non_Empty_List (Extra_Actuals) loop
3800 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3801 end loop;
3802 end if;
3803
3804 -- At this point we have all the actuals, so this is the point at which
3805 -- the various expansion activities for actuals is carried out.
3806
3807 Expand_Actuals (Call_Node, Subp, Post_Call);
3808
3809 -- Verify that the actuals do not share storage. This check must be done
3810 -- on the caller side rather that inside the subprogram to avoid issues
3811 -- of parameter passing.
3812
3813 if Check_Aliasing_Of_Parameters then
3814 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3815 end if;
3816
3817 -- If the subprogram is a renaming, or if it is inherited, replace it in
3818 -- the call with the name of the actual subprogram being called. If this
3819 -- is a dispatching call, the run-time decides what to call. The Alias
3820 -- attribute does not apply to entries.
3821
3822 if Nkind (Call_Node) /= N_Entry_Call_Statement
3823 and then No (Controlling_Argument (Call_Node))
3824 and then Present (Parent_Subp)
3825 and then not Is_Direct_Deep_Call (Subp)
3826 then
3827 if Present (Inherited_From_Formal (Subp)) then
3828 Parent_Subp := Inherited_From_Formal (Subp);
3829 else
3830 Parent_Subp := Ultimate_Alias (Parent_Subp);
3831 end if;
3832
3833 -- The below setting of Entity is suspect, see F109-018 discussion???
3834
3835 Set_Entity (Name (Call_Node), Parent_Subp);
3836
3837 if Is_Abstract_Subprogram (Parent_Subp)
3838 and then not In_Instance
3839 then
3840 Error_Msg_NE
3841 ("cannot call abstract subprogram &!",
3842 Name (Call_Node), Parent_Subp);
3843 end if;
3844
3845 -- Inspect all formals of derived subprogram Subp. Compare parameter
3846 -- types with the parent subprogram and check whether an actual may
3847 -- need a type conversion to the corresponding formal of the parent
3848 -- subprogram.
3849
3850 -- Not clear whether intrinsic subprograms need such conversions. ???
3851
3852 if not Is_Intrinsic_Subprogram (Parent_Subp)
3853 or else Is_Generic_Instance (Parent_Subp)
3854 then
3855 declare
3856 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3857 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3858 -- and resolve the newly generated construct.
3859
3860 -------------
3861 -- Convert --
3862 -------------
3863
3864 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3865 begin
3866 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3867 Analyze (Act);
3868 Resolve (Act, Typ);
3869 end Convert;
3870
3871 -- Local variables
3872
3873 Actual_Typ : Entity_Id;
3874 Formal_Typ : Entity_Id;
3875 Parent_Typ : Entity_Id;
3876
3877 begin
3878 Actual := First_Actual (Call_Node);
3879 Formal := First_Formal (Subp);
3880 Parent_Formal := First_Formal (Parent_Subp);
3881 while Present (Formal) loop
3882 Actual_Typ := Etype (Actual);
3883 Formal_Typ := Etype (Formal);
3884 Parent_Typ := Etype (Parent_Formal);
3885
3886 -- For an IN parameter of a scalar type, the parent formal
3887 -- type and derived formal type differ or the parent formal
3888 -- type and actual type do not match statically.
3889
3890 if Is_Scalar_Type (Formal_Typ)
3891 and then Ekind (Formal) = E_In_Parameter
3892 and then Formal_Typ /= Parent_Typ
3893 and then
3894 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3895 and then not Raises_Constraint_Error (Actual)
3896 then
3897 Convert (Actual, Parent_Typ);
3898 Enable_Range_Check (Actual);
3899
3900 -- If the actual has been marked as requiring a range
3901 -- check, then generate it here.
3902
3903 if Do_Range_Check (Actual) then
3904 Generate_Range_Check
3905 (Actual, Etype (Formal), CE_Range_Check_Failed);
3906 end if;
3907
3908 -- For access types, the parent formal type and actual type
3909 -- differ.
3910
3911 elsif Is_Access_Type (Formal_Typ)
3912 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3913 then
3914 if Ekind (Formal) /= E_In_Parameter then
3915 Convert (Actual, Parent_Typ);
3916
3917 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3918 and then Designated_Type (Parent_Typ) /=
3919 Designated_Type (Actual_Typ)
3920 and then not Is_Controlling_Formal (Formal)
3921 then
3922 -- This unchecked conversion is not necessary unless
3923 -- inlining is enabled, because in that case the type
3924 -- mismatch may become visible in the body about to be
3925 -- inlined.
3926
3927 Rewrite (Actual,
3928 Unchecked_Convert_To (Parent_Typ,
3929 Relocate_Node (Actual)));
3930 Analyze (Actual);
3931 Resolve (Actual, Parent_Typ);
3932 end if;
3933
3934 -- If there is a change of representation, then generate a
3935 -- warning, and do the change of representation.
3936
3937 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3938 Error_Msg_N
3939 ("??change of representation required", Actual);
3940 Convert (Actual, Parent_Typ);
3941
3942 -- For array and record types, the parent formal type and
3943 -- derived formal type have different sizes or pragma Pack
3944 -- status.
3945
3946 elsif ((Is_Array_Type (Formal_Typ)
3947 and then Is_Array_Type (Parent_Typ))
3948 or else
3949 (Is_Record_Type (Formal_Typ)
3950 and then Is_Record_Type (Parent_Typ)))
3951 and then
3952 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3953 or else Has_Pragma_Pack (Formal_Typ) /=
3954 Has_Pragma_Pack (Parent_Typ))
3955 then
3956 Convert (Actual, Parent_Typ);
3957 end if;
3958
3959 Next_Actual (Actual);
3960 Next_Formal (Formal);
3961 Next_Formal (Parent_Formal);
3962 end loop;
3963 end;
3964 end if;
3965
3966 Orig_Subp := Subp;
3967 Subp := Parent_Subp;
3968 end if;
3969
3970 -- Deal with case where call is an explicit dereference
3971
3972 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3973
3974 -- Handle case of access to protected subprogram type
3975
3976 if Is_Access_Protected_Subprogram_Type
3977 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3978 then
3979 -- If this is a call through an access to protected operation, the
3980 -- prefix has the form (object'address, operation'access). Rewrite
3981 -- as a for other protected calls: the object is the 1st parameter
3982 -- of the list of actuals.
3983
3984 declare
3985 Call : Node_Id;
3986 Parm : List_Id;
3987 Nam : Node_Id;
3988 Obj : Node_Id;
3989 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3990
3991 T : constant Entity_Id :=
3992 Equivalent_Type (Base_Type (Etype (Ptr)));
3993
3994 D_T : constant Entity_Id :=
3995 Designated_Type (Base_Type (Etype (Ptr)));
3996
3997 begin
3998 Obj :=
3999 Make_Selected_Component (Loc,
4000 Prefix => Unchecked_Convert_To (T, Ptr),
4001 Selector_Name =>
4002 New_Occurrence_Of (First_Entity (T), Loc));
4003
4004 Nam :=
4005 Make_Selected_Component (Loc,
4006 Prefix => Unchecked_Convert_To (T, Ptr),
4007 Selector_Name =>
4008 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4009
4010 Nam :=
4011 Make_Explicit_Dereference (Loc,
4012 Prefix => Nam);
4013
4014 if Present (Parameter_Associations (Call_Node)) then
4015 Parm := Parameter_Associations (Call_Node);
4016 else
4017 Parm := New_List;
4018 end if;
4019
4020 Prepend (Obj, Parm);
4021
4022 if Etype (D_T) = Standard_Void_Type then
4023 Call :=
4024 Make_Procedure_Call_Statement (Loc,
4025 Name => Nam,
4026 Parameter_Associations => Parm);
4027 else
4028 Call :=
4029 Make_Function_Call (Loc,
4030 Name => Nam,
4031 Parameter_Associations => Parm);
4032 end if;
4033
4034 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4035 Set_Etype (Call, Etype (D_T));
4036
4037 -- We do not re-analyze the call to avoid infinite recursion.
4038 -- We analyze separately the prefix and the object, and set
4039 -- the checks on the prefix that would otherwise be emitted
4040 -- when resolving a call.
4041
4042 Rewrite (Call_Node, Call);
4043 Analyze (Nam);
4044 Apply_Access_Check (Nam);
4045 Analyze (Obj);
4046 return;
4047 end;
4048 end if;
4049 end if;
4050
4051 -- If this is a call to an intrinsic subprogram, then perform the
4052 -- appropriate expansion to the corresponding tree node and we
4053 -- are all done (since after that the call is gone).
4054
4055 -- In the case where the intrinsic is to be processed by the back end,
4056 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4057 -- since the idea in this case is to pass the call unchanged. If the
4058 -- intrinsic is an inherited unchecked conversion, and the derived type
4059 -- is the target type of the conversion, we must retain it as the return
4060 -- type of the expression. Otherwise the expansion below, which uses the
4061 -- parent operation, will yield the wrong type.
4062
4063 if Is_Intrinsic_Subprogram (Subp) then
4064 Expand_Intrinsic_Call (Call_Node, Subp);
4065
4066 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4067 and then Parent_Subp /= Orig_Subp
4068 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4069 then
4070 Set_Etype (Call_Node, Etype (Orig_Subp));
4071 end if;
4072
4073 return;
4074 end if;
4075
4076 if Ekind_In (Subp, E_Function, E_Procedure) then
4077
4078 -- We perform a simple optimization on calls for To_Address by
4079 -- replacing them with an unchecked conversion. Not only is this
4080 -- efficient, but it also avoids order of elaboration problems when
4081 -- address clauses are inlined (address expression elaborated at the
4082 -- wrong point).
4083
4084 -- We perform this optimization regardless of whether we are in the
4085 -- main unit or in a unit in the context of the main unit, to ensure
4086 -- that the generated tree is the same in both cases, for CodePeer
4087 -- use.
4088
4089 if Is_RTE (Subp, RE_To_Address) then
4090 Rewrite (Call_Node,
4091 Unchecked_Convert_To
4092 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4093 return;
4094
4095 -- A call to a null procedure is replaced by a null statement, but we
4096 -- are not allowed to ignore possible side effects of the call, so we
4097 -- make sure that actuals are evaluated.
4098 -- We also suppress this optimization for GNATCoverage.
4099
4100 elsif Is_Null_Procedure (Subp)
4101 and then not Opt.Suppress_Control_Flow_Optimizations
4102 then
4103 Actual := First_Actual (Call_Node);
4104 while Present (Actual) loop
4105 Remove_Side_Effects (Actual);
4106 Next_Actual (Actual);
4107 end loop;
4108
4109 Rewrite (Call_Node, Make_Null_Statement (Loc));
4110 return;
4111 end if;
4112
4113 -- Handle inlining. No action needed if the subprogram is not inlined
4114
4115 if not Is_Inlined (Subp) then
4116 null;
4117
4118 -- Frontend inlining of expression functions (performed also when
4119 -- backend inlining is enabled).
4120
4121 elsif Is_Inlinable_Expression_Function (Subp) then
4122 Rewrite (N, New_Copy (Expression_Of_Expression_Function (Subp)));
4123 Analyze (N);
4124 return;
4125
4126 -- Handle frontend inlining
4127
4128 elsif not Back_End_Inlining then
4129 Inlined_Subprogram : declare
4130 Bod : Node_Id;
4131 Must_Inline : Boolean := False;
4132 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4133
4134 begin
4135 -- Verify that the body to inline has already been seen, and
4136 -- that if the body is in the current unit the inlining does
4137 -- not occur earlier. This avoids order-of-elaboration problems
4138 -- in the back end.
4139
4140 -- This should be documented in sinfo/einfo ???
4141
4142 if No (Spec)
4143 or else Nkind (Spec) /= N_Subprogram_Declaration
4144 or else No (Body_To_Inline (Spec))
4145 then
4146 Must_Inline := False;
4147
4148 -- If this an inherited function that returns a private type,
4149 -- do not inline if the full view is an unconstrained array,
4150 -- because such calls cannot be inlined.
4151
4152 elsif Present (Orig_Subp)
4153 and then Is_Array_Type (Etype (Orig_Subp))
4154 and then not Is_Constrained (Etype (Orig_Subp))
4155 then
4156 Must_Inline := False;
4157
4158 elsif In_Unfrozen_Instance (Scope (Subp)) then
4159 Must_Inline := False;
4160
4161 else
4162 Bod := Body_To_Inline (Spec);
4163
4164 if (In_Extended_Main_Code_Unit (Call_Node)
4165 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4166 or else Has_Pragma_Inline_Always (Subp))
4167 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4168 or else
4169 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4170 then
4171 Must_Inline := True;
4172
4173 -- If we are compiling a package body that is not the main
4174 -- unit, it must be for inlining/instantiation purposes,
4175 -- in which case we inline the call to insure that the same
4176 -- temporaries are generated when compiling the body by
4177 -- itself. Otherwise link errors can occur.
4178
4179 -- If the function being called is itself in the main unit,
4180 -- we cannot inline, because there is a risk of double
4181 -- elaboration and/or circularity: the inlining can make
4182 -- visible a private entity in the body of the main unit,
4183 -- that gigi will see before its sees its proper definition.
4184
4185 elsif not (In_Extended_Main_Code_Unit (Call_Node))
4186 and then In_Package_Body
4187 then
4188 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4189
4190 -- Inline calls to _postconditions when generating C code
4191
4192 elsif Modify_Tree_For_C
4193 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4194 and then Chars (Name (N)) = Name_uPostconditions
4195 then
4196 Must_Inline := True;
4197 end if;
4198 end if;
4199
4200 if Must_Inline then
4201 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4202
4203 else
4204 -- Let the back end handle it
4205
4206 Add_Inlined_Body (Subp, Call_Node);
4207
4208 if Front_End_Inlining
4209 and then Nkind (Spec) = N_Subprogram_Declaration
4210 and then (In_Extended_Main_Code_Unit (Call_Node))
4211 and then No (Body_To_Inline (Spec))
4212 and then not Has_Completion (Subp)
4213 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4214 then
4215 Cannot_Inline
4216 ("cannot inline& (body not seen yet)?",
4217 Call_Node, Subp);
4218 end if;
4219 end if;
4220 end Inlined_Subprogram;
4221
4222 -- Back end inlining: let the back end handle it
4223
4224 elsif No (Unit_Declaration_Node (Subp))
4225 or else Nkind (Unit_Declaration_Node (Subp)) /=
4226 N_Subprogram_Declaration
4227 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
4228 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
4229 N_Entity
4230 then
4231 Add_Inlined_Body (Subp, Call_Node);
4232
4233 -- If the inlined call appears within an instantiation and some
4234 -- level of optimization is required, ensure that the enclosing
4235 -- instance body is available so that the back-end can actually
4236 -- perform the inlining.
4237
4238 if In_Instance
4239 and then Comes_From_Source (Subp)
4240 and then Optimization_Level > 0
4241 then
4242 declare
4243 Decl : Node_Id;
4244 Inst : Entity_Id;
4245 Inst_Node : Node_Id;
4246
4247 begin
4248 Inst := Scope (Subp);
4249
4250 -- Find enclosing instance
4251
4252 while Present (Inst) and then Inst /= Standard_Standard loop
4253 exit when Is_Generic_Instance (Inst);
4254 Inst := Scope (Inst);
4255 end loop;
4256
4257 if Present (Inst)
4258 and then Is_Generic_Instance (Inst)
4259 and then not Is_Inlined (Inst)
4260 then
4261 Set_Is_Inlined (Inst);
4262 Decl := Unit_Declaration_Node (Inst);
4263
4264 -- Do not add a pending instantiation if the body exits
4265 -- already, or if the instance is a compilation unit, or
4266 -- the instance node is missing.
4267
4268 if Present (Corresponding_Body (Decl))
4269 or else Nkind (Parent (Decl)) = N_Compilation_Unit
4270 or else No (Next (Decl))
4271 then
4272 null;
4273
4274 else
4275 -- The instantiation node usually follows the package
4276 -- declaration for the instance. If the generic unit
4277 -- has aspect specifications, they are transformed
4278 -- into pragmas in the instance, and the instance node
4279 -- appears after them.
4280
4281 Inst_Node := Next (Decl);
4282
4283 while Nkind (Inst_Node) /= N_Package_Instantiation loop
4284 Inst_Node := Next (Inst_Node);
4285 end loop;
4286
4287 Add_Pending_Instantiation (Inst_Node, Decl);
4288 end if;
4289 end if;
4290 end;
4291 end if;
4292
4293 -- Front end expansion of simple functions returning unconstrained
4294 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4295 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
4296 -- also Build_Renamed_Body) cannot be expanded here because this may
4297 -- give rise to order-of-elaboration issues for the types of the
4298 -- parameters of the subprogram, if any.
4299
4300 else
4301 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4302 end if;
4303 end if;
4304
4305 -- Check for protected subprogram. This is either an intra-object call,
4306 -- or a protected function call. Protected procedure calls are rewritten
4307 -- as entry calls and handled accordingly.
4308
4309 -- In Ada 2005, this may be an indirect call to an access parameter that
4310 -- is an access_to_subprogram. In that case the anonymous type has a
4311 -- scope that is a protected operation, but the call is a regular one.
4312 -- In either case do not expand call if subprogram is eliminated.
4313
4314 Scop := Scope (Subp);
4315
4316 if Nkind (Call_Node) /= N_Entry_Call_Statement
4317 and then Is_Protected_Type (Scop)
4318 and then Ekind (Subp) /= E_Subprogram_Type
4319 and then not Is_Eliminated (Subp)
4320 then
4321 -- If the call is an internal one, it is rewritten as a call to the
4322 -- corresponding unprotected subprogram.
4323
4324 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4325 end if;
4326
4327 -- Functions returning controlled objects need special attention. If
4328 -- the return type is limited, then the context is initialization and
4329 -- different processing applies. If the call is to a protected function,
4330 -- the expansion above will call Expand_Call recursively. Otherwise the
4331 -- function call is transformed into a temporary which obtains the
4332 -- result from the secondary stack.
4333
4334 if Needs_Finalization (Etype (Subp)) then
4335 if not Is_Build_In_Place_Function_Call (Call_Node)
4336 and then
4337 (No (First_Formal (Subp))
4338 or else
4339 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4340 then
4341 Expand_Ctrl_Function_Call (Call_Node);
4342
4343 -- Build-in-place function calls which appear in anonymous contexts
4344 -- need a transient scope to ensure the proper finalization of the
4345 -- intermediate result after its use.
4346
4347 elsif Is_Build_In_Place_Function_Call (Call_Node)
4348 and then Nkind_In (Parent (Unqual_Conv (Call_Node)),
4349 N_Attribute_Reference,
4350 N_Function_Call,
4351 N_Indexed_Component,
4352 N_Object_Renaming_Declaration,
4353 N_Procedure_Call_Statement,
4354 N_Selected_Component,
4355 N_Slice)
4356 then
4357 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
4358 end if;
4359 end if;
4360 end Expand_Call_Helper;
4361
4362 -------------------------------
4363 -- Expand_Ctrl_Function_Call --
4364 -------------------------------
4365
4366 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4367 function Is_Element_Reference (N : Node_Id) return Boolean;
4368 -- Determine whether node N denotes a reference to an Ada 2012 container
4369 -- element.
4370
4371 --------------------------
4372 -- Is_Element_Reference --
4373 --------------------------
4374
4375 function Is_Element_Reference (N : Node_Id) return Boolean is
4376 Ref : constant Node_Id := Original_Node (N);
4377
4378 begin
4379 -- Analysis marks an element reference by setting the generalized
4380 -- indexing attribute of an indexed component before the component
4381 -- is rewritten into a function call.
4382
4383 return
4384 Nkind (Ref) = N_Indexed_Component
4385 and then Present (Generalized_Indexing (Ref));
4386 end Is_Element_Reference;
4387
4388 -- Start of processing for Expand_Ctrl_Function_Call
4389
4390 begin
4391 -- Optimization, if the returned value (which is on the sec-stack) is
4392 -- returned again, no need to copy/readjust/finalize, we can just pass
4393 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4394 -- attachment is needed
4395
4396 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4397 return;
4398 end if;
4399
4400 -- Resolution is now finished, make sure we don't start analysis again
4401 -- because of the duplication.
4402
4403 Set_Analyzed (N);
4404
4405 -- A function which returns a controlled object uses the secondary
4406 -- stack. Rewrite the call into a temporary which obtains the result of
4407 -- the function using 'reference.
4408
4409 Remove_Side_Effects (N);
4410
4411 -- The side effect removal of the function call produced a temporary.
4412 -- When the context is a case expression, if expression, or expression
4413 -- with actions, the lifetime of the temporary must be extended to match
4414 -- that of the context. Otherwise the function result will be finalized
4415 -- too early and affect the result of the expression. To prevent this
4416 -- unwanted effect, the temporary should not be considered for clean up
4417 -- actions by the general finalization machinery.
4418
4419 -- Exception to this rule are references to Ada 2012 container elements.
4420 -- Such references must be finalized at the end of each iteration of the
4421 -- related quantified expression, otherwise the container will remain
4422 -- busy.
4423
4424 if Nkind (N) = N_Explicit_Dereference
4425 and then Within_Case_Or_If_Expression (N)
4426 and then not Is_Element_Reference (N)
4427 then
4428 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4429 end if;
4430 end Expand_Ctrl_Function_Call;
4431
4432 ----------------------------------------
4433 -- Expand_N_Extended_Return_Statement --
4434 ----------------------------------------
4435
4436 -- If there is a Handled_Statement_Sequence, we rewrite this:
4437
4438 -- return Result : T := <expression> do
4439 -- <handled_seq_of_stms>
4440 -- end return;
4441
4442 -- to be:
4443
4444 -- declare
4445 -- Result : T := <expression>;
4446 -- begin
4447 -- <handled_seq_of_stms>
4448 -- return Result;
4449 -- end;
4450
4451 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4452
4453 -- return Result : T := <expression>;
4454
4455 -- to be:
4456
4457 -- return <expression>;
4458
4459 -- unless it's build-in-place or there's no <expression>, in which case
4460 -- we generate:
4461
4462 -- declare
4463 -- Result : T := <expression>;
4464 -- begin
4465 -- return Result;
4466 -- end;
4467
4468 -- Note that this case could have been written by the user as an extended
4469 -- return statement, or could have been transformed to this from a simple
4470 -- return statement.
4471
4472 -- That is, we need to have a reified return object if there are statements
4473 -- (which might refer to it) or if we're doing build-in-place (so we can
4474 -- set its address to the final resting place or if there is no expression
4475 -- (in which case default initial values might need to be set).
4476
4477 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4478 Loc : constant Source_Ptr := Sloc (N);
4479
4480 function Build_Heap_Allocator
4481 (Temp_Id : Entity_Id;
4482 Temp_Typ : Entity_Id;
4483 Func_Id : Entity_Id;
4484 Ret_Typ : Entity_Id;
4485 Alloc_Expr : Node_Id) return Node_Id;
4486 -- Create the statements necessary to allocate a return object on the
4487 -- caller's master. The master is available through implicit parameter
4488 -- BIPfinalizationmaster.
4489 --
4490 -- if BIPfinalizationmaster /= null then
4491 -- declare
4492 -- type Ptr_Typ is access Ret_Typ;
4493 -- for Ptr_Typ'Storage_Pool use
4494 -- Base_Pool (BIPfinalizationmaster.all).all;
4495 -- Local : Ptr_Typ;
4496 --
4497 -- begin
4498 -- procedure Allocate (...) is
4499 -- begin
4500 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4501 -- end Allocate;
4502 --
4503 -- Local := <Alloc_Expr>;
4504 -- Temp_Id := Temp_Typ (Local);
4505 -- end;
4506 -- end if;
4507 --
4508 -- Temp_Id is the temporary which is used to reference the internally
4509 -- created object in all allocation forms. Temp_Typ is the type of the
4510 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4511 -- type of Func_Id. Alloc_Expr is the actual allocator.
4512
4513 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
4514 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4515 -- with parameters:
4516 -- From current activation chain
4517 -- To activation chain passed in by the caller
4518 -- New_Master master passed in by the caller
4519 --
4520 -- Func_Id is the entity of the function where the extended return
4521 -- statement appears.
4522
4523 --------------------------
4524 -- Build_Heap_Allocator --
4525 --------------------------
4526
4527 function Build_Heap_Allocator
4528 (Temp_Id : Entity_Id;
4529 Temp_Typ : Entity_Id;
4530 Func_Id : Entity_Id;
4531 Ret_Typ : Entity_Id;
4532 Alloc_Expr : Node_Id) return Node_Id
4533 is
4534 begin
4535 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4536
4537 -- Processing for build-in-place object allocation.
4538
4539 if Needs_Finalization (Ret_Typ) then
4540 declare
4541 Decls : constant List_Id := New_List;
4542 Fin_Mas_Id : constant Entity_Id :=
4543 Build_In_Place_Formal
4544 (Func_Id, BIP_Finalization_Master);
4545 Stmts : constant List_Id := New_List;
4546 Desig_Typ : Entity_Id;
4547 Local_Id : Entity_Id;
4548 Pool_Id : Entity_Id;
4549 Ptr_Typ : Entity_Id;
4550
4551 begin
4552 -- Generate:
4553 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4554
4555 Pool_Id := Make_Temporary (Loc, 'P');
4556
4557 Append_To (Decls,
4558 Make_Object_Renaming_Declaration (Loc,
4559 Defining_Identifier => Pool_Id,
4560 Subtype_Mark =>
4561 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4562 Name =>
4563 Make_Explicit_Dereference (Loc,
4564 Prefix =>
4565 Make_Function_Call (Loc,
4566 Name =>
4567 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4568 Parameter_Associations => New_List (
4569 Make_Explicit_Dereference (Loc,
4570 Prefix =>
4571 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4572
4573 -- Create an access type which uses the storage pool of the
4574 -- caller's master. This additional type is necessary because
4575 -- the finalization master cannot be associated with the type
4576 -- of the temporary. Otherwise the secondary stack allocation
4577 -- will fail.
4578
4579 Desig_Typ := Ret_Typ;
4580
4581 -- Ensure that the build-in-place machinery uses a fat pointer
4582 -- when allocating an unconstrained array on the heap. In this
4583 -- case the result object type is a constrained array type even
4584 -- though the function type is unconstrained.
4585
4586 if Ekind (Desig_Typ) = E_Array_Subtype then
4587 Desig_Typ := Base_Type (Desig_Typ);
4588 end if;
4589
4590 -- Generate:
4591 -- type Ptr_Typ is access Desig_Typ;
4592
4593 Ptr_Typ := Make_Temporary (Loc, 'P');
4594
4595 Append_To (Decls,
4596 Make_Full_Type_Declaration (Loc,
4597 Defining_Identifier => Ptr_Typ,
4598 Type_Definition =>
4599 Make_Access_To_Object_Definition (Loc,
4600 Subtype_Indication =>
4601 New_Occurrence_Of (Desig_Typ, Loc))));
4602
4603 -- Perform minor decoration in order to set the master and the
4604 -- storage pool attributes.
4605
4606 Set_Ekind (Ptr_Typ, E_Access_Type);
4607 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4608 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4609
4610 -- Create the temporary, generate:
4611 -- Local_Id : Ptr_Typ;
4612
4613 Local_Id := Make_Temporary (Loc, 'T');
4614
4615 Append_To (Decls,
4616 Make_Object_Declaration (Loc,
4617 Defining_Identifier => Local_Id,
4618 Object_Definition =>
4619 New_Occurrence_Of (Ptr_Typ, Loc)));
4620
4621 -- Allocate the object, generate:
4622 -- Local_Id := <Alloc_Expr>;
4623
4624 Append_To (Stmts,
4625 Make_Assignment_Statement (Loc,
4626 Name => New_Occurrence_Of (Local_Id, Loc),
4627 Expression => Alloc_Expr));
4628
4629 -- Generate:
4630 -- Temp_Id := Temp_Typ (Local_Id);
4631
4632 Append_To (Stmts,
4633 Make_Assignment_Statement (Loc,
4634 Name => New_Occurrence_Of (Temp_Id, Loc),
4635 Expression =>
4636 Unchecked_Convert_To (Temp_Typ,
4637 New_Occurrence_Of (Local_Id, Loc))));
4638
4639 -- Wrap the allocation in a block. This is further conditioned
4640 -- by checking the caller finalization master at runtime. A
4641 -- null value indicates a non-existent master, most likely due
4642 -- to a Finalize_Storage_Only allocation.
4643
4644 -- Generate:
4645 -- if BIPfinalizationmaster /= null then
4646 -- declare
4647 -- <Decls>
4648 -- begin
4649 -- <Stmts>
4650 -- end;
4651 -- end if;
4652
4653 return
4654 Make_If_Statement (Loc,
4655 Condition =>
4656 Make_Op_Ne (Loc,
4657 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4658 Right_Opnd => Make_Null (Loc)),
4659
4660 Then_Statements => New_List (
4661 Make_Block_Statement (Loc,
4662 Declarations => Decls,
4663 Handled_Statement_Sequence =>
4664 Make_Handled_Sequence_Of_Statements (Loc,
4665 Statements => Stmts))));
4666 end;
4667
4668 -- For all other cases, generate:
4669 -- Temp_Id := <Alloc_Expr>;
4670
4671 else
4672 return
4673 Make_Assignment_Statement (Loc,
4674 Name => New_Occurrence_Of (Temp_Id, Loc),
4675 Expression => Alloc_Expr);
4676 end if;
4677 end Build_Heap_Allocator;
4678
4679 ---------------------------
4680 -- Move_Activation_Chain --
4681 ---------------------------
4682
4683 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
4684 begin
4685 return
4686 Make_Procedure_Call_Statement (Loc,
4687 Name =>
4688 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4689
4690 Parameter_Associations => New_List (
4691
4692 -- Source chain
4693
4694 Make_Attribute_Reference (Loc,
4695 Prefix => Make_Identifier (Loc, Name_uChain),
4696 Attribute_Name => Name_Unrestricted_Access),
4697
4698 -- Destination chain
4699
4700 New_Occurrence_Of
4701 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
4702
4703 -- New master
4704
4705 New_Occurrence_Of
4706 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
4707 end Move_Activation_Chain;
4708
4709 -- Local variables
4710
4711 Func_Id : constant Entity_Id :=
4712 Return_Applies_To (Return_Statement_Entity (N));
4713 Is_BIP_Func : constant Boolean :=
4714 Is_Build_In_Place_Function (Func_Id);
4715 Ret_Obj_Id : constant Entity_Id :=
4716 First_Entity (Return_Statement_Entity (N));
4717 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4718 Ret_Typ : constant Entity_Id := Etype (Func_Id);
4719
4720 Exp : Node_Id;
4721 HSS : Node_Id;
4722 Result : Node_Id;
4723 Return_Stmt : Node_Id;
4724 Stmts : List_Id;
4725
4726 -- Start of processing for Expand_N_Extended_Return_Statement
4727
4728 begin
4729 -- Given that functionality of interface thunks is simple (just displace
4730 -- the pointer to the object) they are always handled by means of
4731 -- simple return statements.
4732
4733 pragma Assert (not Is_Thunk (Current_Scope));
4734
4735 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4736 Exp := Expression (Ret_Obj_Decl);
4737 else
4738 Exp := Empty;
4739 end if;
4740
4741 HSS := Handled_Statement_Sequence (N);
4742
4743 -- If the returned object needs finalization actions, the function must
4744 -- perform the appropriate cleanup should it fail to return. The state
4745 -- of the function itself is tracked through a flag which is coupled
4746 -- with the scope finalizer. There is one flag per each return object
4747 -- in case of multiple returns.
4748
4749 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4750 declare
4751 Flag_Decl : Node_Id;
4752 Flag_Id : Entity_Id;
4753 Func_Bod : Node_Id;
4754
4755 begin
4756 -- Recover the function body
4757
4758 Func_Bod := Unit_Declaration_Node (Func_Id);
4759
4760 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4761 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4762 end if;
4763
4764 if Nkind (Func_Bod) = N_Function_Specification then
4765 Func_Bod := Parent (Func_Bod); -- one more level for child units
4766 end if;
4767
4768 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
4769
4770 -- Create a flag to track the function state
4771
4772 Flag_Id := Make_Temporary (Loc, 'F');
4773 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4774
4775 -- Insert the flag at the beginning of the function declarations,
4776 -- generate:
4777 -- Fnn : Boolean := False;
4778
4779 Flag_Decl :=
4780 Make_Object_Declaration (Loc,
4781 Defining_Identifier => Flag_Id,
4782 Object_Definition =>
4783 New_Occurrence_Of (Standard_Boolean, Loc),
4784 Expression =>
4785 New_Occurrence_Of (Standard_False, Loc));
4786
4787 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4788 Analyze (Flag_Decl);
4789 end;
4790 end if;
4791
4792 -- Build a simple_return_statement that returns the return object when
4793 -- there is a statement sequence, or no expression, or the result will
4794 -- be built in place. Note however that we currently do this for all
4795 -- composite cases, even though not all are built in place.
4796
4797 if Present (HSS)
4798 or else Is_Composite_Type (Ret_Typ)
4799 or else No (Exp)
4800 then
4801 if No (HSS) then
4802 Stmts := New_List;
4803
4804 -- If the extended return has a handled statement sequence, then wrap
4805 -- it in a block and use the block as the first statement.
4806
4807 else
4808 Stmts := New_List (
4809 Make_Block_Statement (Loc,
4810 Declarations => New_List,
4811 Handled_Statement_Sequence => HSS));
4812 end if;
4813
4814 -- If the result type contains tasks, we call Move_Activation_Chain.
4815 -- Later, the cleanup code will call Complete_Master, which will
4816 -- terminate any unactivated tasks belonging to the return statement
4817 -- master. But Move_Activation_Chain updates their master to be that
4818 -- of the caller, so they will not be terminated unless the return
4819 -- statement completes unsuccessfully due to exception, abort, goto,
4820 -- or exit. As a formality, we test whether the function requires the
4821 -- result to be built in place, though that's necessarily true for
4822 -- the case of result types with task parts.
4823
4824 if Is_BIP_Func and then Has_Task (Ret_Typ) then
4825
4826 -- The return expression is an aggregate for a complex type which
4827 -- contains tasks. This particular case is left unexpanded since
4828 -- the regular expansion would insert all temporaries and
4829 -- initialization code in the wrong block.
4830
4831 if Nkind (Exp) = N_Aggregate then
4832 Expand_N_Aggregate (Exp);
4833 end if;
4834
4835 -- Do not move the activation chain if the return object does not
4836 -- contain tasks.
4837
4838 if Has_Task (Etype (Ret_Obj_Id)) then
4839 Append_To (Stmts, Move_Activation_Chain (Func_Id));
4840 end if;
4841 end if;
4842
4843 -- Update the state of the function right before the object is
4844 -- returned.
4845
4846 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
4847 declare
4848 Flag_Id : constant Entity_Id :=
4849 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4850
4851 begin
4852 -- Generate:
4853 -- Fnn := True;
4854
4855 Append_To (Stmts,
4856 Make_Assignment_Statement (Loc,
4857 Name => New_Occurrence_Of (Flag_Id, Loc),
4858 Expression => New_Occurrence_Of (Standard_True, Loc)));
4859 end;
4860 end if;
4861
4862 -- Build a simple_return_statement that returns the return object
4863
4864 Return_Stmt :=
4865 Make_Simple_Return_Statement (Loc,
4866 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4867 Append_To (Stmts, Return_Stmt);
4868
4869 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4870 end if;
4871
4872 -- Case where we build a return statement block
4873
4874 if Present (HSS) then
4875 Result :=
4876 Make_Block_Statement (Loc,
4877 Declarations => Return_Object_Declarations (N),
4878 Handled_Statement_Sequence => HSS);
4879
4880 -- We set the entity of the new block statement to be that of the
4881 -- return statement. This is necessary so that various fields, such
4882 -- as Finalization_Chain_Entity carry over from the return statement
4883 -- to the block. Note that this block is unusual, in that its entity
4884 -- is an E_Return_Statement rather than an E_Block.
4885
4886 Set_Identifier
4887 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4888
4889 -- If the object decl was already rewritten as a renaming, then we
4890 -- don't want to do the object allocation and transformation of
4891 -- the return object declaration to a renaming. This case occurs
4892 -- when the return object is initialized by a call to another
4893 -- build-in-place function, and that function is responsible for
4894 -- the allocation of the return object.
4895
4896 if Is_BIP_Func
4897 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4898 then
4899 pragma Assert
4900 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4901 and then
4902
4903 -- It is a regular BIP object declaration
4904
4905 (Is_Build_In_Place_Function_Call
4906 (Expression (Original_Node (Ret_Obj_Decl)))
4907
4908 -- It is a BIP object declaration that displaces the pointer
4909 -- to the object to reference a convered interface type.
4910
4911 or else
4912 Present (Unqual_BIP_Iface_Function_Call
4913 (Expression (Original_Node (Ret_Obj_Decl))))));
4914
4915 -- Return the build-in-place result by reference
4916
4917 Set_By_Ref (Return_Stmt);
4918
4919 elsif Is_BIP_Func then
4920
4921 -- Locate the implicit access parameter associated with the
4922 -- caller-supplied return object and convert the return
4923 -- statement's return object declaration to a renaming of a
4924 -- dereference of the access parameter. If the return object's
4925 -- declaration includes an expression that has not already been
4926 -- expanded as separate assignments, then add an assignment
4927 -- statement to ensure the return object gets initialized.
4928
4929 -- declare
4930 -- Result : T [:= <expression>];
4931 -- begin
4932 -- ...
4933
4934 -- is converted to
4935
4936 -- declare
4937 -- Result : T renames FuncRA.all;
4938 -- [Result := <expression;]
4939 -- begin
4940 -- ...
4941
4942 declare
4943 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
4944 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
4945
4946 Init_Assignment : Node_Id := Empty;
4947 Obj_Acc_Formal : Entity_Id;
4948 Obj_Acc_Deref : Node_Id;
4949 Obj_Alloc_Formal : Entity_Id;
4950
4951 begin
4952 -- Build-in-place results must be returned by reference
4953
4954 Set_By_Ref (Return_Stmt);
4955
4956 -- Retrieve the implicit access parameter passed by the caller
4957
4958 Obj_Acc_Formal :=
4959 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
4960
4961 -- If the return object's declaration includes an expression
4962 -- and the declaration isn't marked as No_Initialization, then
4963 -- we need to generate an assignment to the object and insert
4964 -- it after the declaration before rewriting it as a renaming
4965 -- (otherwise we'll lose the initialization). The case where
4966 -- the result type is an interface (or class-wide interface)
4967 -- is also excluded because the context of the function call
4968 -- must be unconstrained, so the initialization will always
4969 -- be done as part of an allocator evaluation (storage pool
4970 -- or secondary stack), never to a constrained target object
4971 -- passed in by the caller. Besides the assignment being
4972 -- unneeded in this case, it avoids problems with trying to
4973 -- generate a dispatching assignment when the return expression
4974 -- is a nonlimited descendant of a limited interface (the
4975 -- interface has no assignment operation).
4976
4977 if Present (Ret_Obj_Expr)
4978 and then not No_Initialization (Ret_Obj_Decl)
4979 and then not Is_Interface (Ret_Obj_Typ)
4980 then
4981 Init_Assignment :=
4982 Make_Assignment_Statement (Loc,
4983 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
4984 Expression => New_Copy_Tree (Ret_Obj_Expr));
4985
4986 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
4987 Set_Assignment_OK (Name (Init_Assignment));
4988 Set_No_Ctrl_Actions (Init_Assignment);
4989
4990 Set_Parent (Name (Init_Assignment), Init_Assignment);
4991 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4992
4993 Set_Expression (Ret_Obj_Decl, Empty);
4994
4995 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
4996 and then not Is_Class_Wide_Type
4997 (Etype (Expression (Init_Assignment)))
4998 then
4999 Rewrite (Expression (Init_Assignment),
5000 Make_Type_Conversion (Loc,
5001 Subtype_Mark =>
5002 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
5003 Expression =>
5004 Relocate_Node (Expression (Init_Assignment))));
5005 end if;
5006
5007 -- In the case of functions where the calling context can
5008 -- determine the form of allocation needed, initialization
5009 -- is done with each part of the if statement that handles
5010 -- the different forms of allocation (this is true for
5011 -- unconstrained and tagged result subtypes).
5012
5013 if Is_Constrained (Ret_Typ)
5014 and then not Is_Tagged_Type (Underlying_Type (Ret_Typ))
5015 then
5016 Insert_After (Ret_Obj_Decl, Init_Assignment);
5017 end if;
5018 end if;
5019
5020 -- When the function's subtype is unconstrained, a run-time
5021 -- test is needed to determine the form of allocation to use
5022 -- for the return object. The function has an implicit formal
5023 -- parameter indicating this. If the BIP_Alloc_Form formal has
5024 -- the value one, then the caller has passed access to an
5025 -- existing object for use as the return object. If the value
5026 -- is two, then the return object must be allocated on the
5027 -- secondary stack. Otherwise, the object must be allocated in
5028 -- a storage pool. We generate an if statement to test the
5029 -- implicit allocation formal and initialize a local access
5030 -- value appropriately, creating allocators in the secondary
5031 -- stack and global heap cases. The special formal also exists
5032 -- and must be tested when the function has a tagged result,
5033 -- even when the result subtype is constrained, because in
5034 -- general such functions can be called in dispatching contexts
5035 -- and must be handled similarly to functions with a class-wide
5036 -- result.
5037
5038 if not Is_Constrained (Ret_Typ)
5039 or else Is_Tagged_Type (Underlying_Type (Ret_Typ))
5040 then
5041 Obj_Alloc_Formal :=
5042 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5043
5044 declare
5045 Pool_Id : constant Entity_Id :=
5046 Make_Temporary (Loc, 'P');
5047 Alloc_Obj_Id : Entity_Id;
5048 Alloc_Obj_Decl : Node_Id;
5049 Alloc_If_Stmt : Node_Id;
5050 Heap_Allocator : Node_Id;
5051 Pool_Decl : Node_Id;
5052 Pool_Allocator : Node_Id;
5053 Ptr_Type_Decl : Node_Id;
5054 Ref_Type : Entity_Id;
5055 SS_Allocator : Node_Id;
5056
5057 begin
5058 -- Reuse the itype created for the function's implicit
5059 -- access formal. This avoids the need to create a new
5060 -- access type here, plus it allows assigning the access
5061 -- formal directly without applying a conversion.
5062
5063 -- Ref_Type := Etype (Object_Access);
5064
5065 -- Create an access type designating the function's
5066 -- result subtype.
5067
5068 Ref_Type := Make_Temporary (Loc, 'A');
5069
5070 Ptr_Type_Decl :=
5071 Make_Full_Type_Declaration (Loc,
5072 Defining_Identifier => Ref_Type,
5073 Type_Definition =>
5074 Make_Access_To_Object_Definition (Loc,
5075 All_Present => True,
5076 Subtype_Indication =>
5077 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5078
5079 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5080
5081 -- Create an access object that will be initialized to an
5082 -- access value denoting the return object, either coming
5083 -- from an implicit access value passed in by the caller
5084 -- or from the result of an allocator.
5085
5086 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5087 Set_Etype (Alloc_Obj_Id, Ref_Type);
5088
5089 Alloc_Obj_Decl :=
5090 Make_Object_Declaration (Loc,
5091 Defining_Identifier => Alloc_Obj_Id,
5092 Object_Definition =>
5093 New_Occurrence_Of (Ref_Type, Loc));
5094
5095 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5096
5097 -- Create allocators for both the secondary stack and
5098 -- global heap. If there's an initialization expression,
5099 -- then create these as initialized allocators.
5100
5101 if Present (Ret_Obj_Expr)
5102 and then not No_Initialization (Ret_Obj_Decl)
5103 then
5104 -- Always use the type of the expression for the
5105 -- qualified expression, rather than the result type.
5106 -- In general we cannot always use the result type
5107 -- for the allocator, because the expression might be
5108 -- of a specific type, such as in the case of an
5109 -- aggregate or even a nonlimited object when the
5110 -- result type is a limited class-wide interface type.
5111
5112 Heap_Allocator :=
5113 Make_Allocator (Loc,
5114 Expression =>
5115 Make_Qualified_Expression (Loc,
5116 Subtype_Mark =>
5117 New_Occurrence_Of
5118 (Etype (Ret_Obj_Expr), Loc),
5119 Expression => New_Copy_Tree (Ret_Obj_Expr)));
5120
5121 else
5122 -- If the function returns a class-wide type we cannot
5123 -- use the return type for the allocator. Instead we
5124 -- use the type of the expression, which must be an
5125 -- aggregate of a definite type.
5126
5127 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5128 Heap_Allocator :=
5129 Make_Allocator (Loc,
5130 Expression =>
5131 New_Occurrence_Of
5132 (Etype (Ret_Obj_Expr), Loc));
5133 else
5134 Heap_Allocator :=
5135 Make_Allocator (Loc,
5136 Expression =>
5137 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5138 end if;
5139
5140 -- If the object requires default initialization then
5141 -- that will happen later following the elaboration of
5142 -- the object renaming. If we don't turn it off here
5143 -- then the object will be default initialized twice.
5144
5145 Set_No_Initialization (Heap_Allocator);
5146 end if;
5147
5148 -- The Pool_Allocator is just like the Heap_Allocator,
5149 -- except we set Storage_Pool and Procedure_To_Call so
5150 -- it will use the user-defined storage pool.
5151
5152 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5153
5154 -- Do not generate the renaming of the build-in-place
5155 -- pool parameter on ZFP because the parameter is not
5156 -- created in the first place.
5157
5158 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5159 Pool_Decl :=
5160 Make_Object_Renaming_Declaration (Loc,
5161 Defining_Identifier => Pool_Id,
5162 Subtype_Mark =>
5163 New_Occurrence_Of
5164 (RTE (RE_Root_Storage_Pool), Loc),
5165 Name =>
5166 Make_Explicit_Dereference (Loc,
5167 New_Occurrence_Of
5168 (Build_In_Place_Formal
5169 (Func_Id, BIP_Storage_Pool), Loc)));
5170 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5171 Set_Procedure_To_Call
5172 (Pool_Allocator, RTE (RE_Allocate_Any));
5173 else
5174 Pool_Decl := Make_Null_Statement (Loc);
5175 end if;
5176
5177 -- If the No_Allocators restriction is active, then only
5178 -- an allocator for secondary stack allocation is needed.
5179 -- It's OK for such allocators to have Comes_From_Source
5180 -- set to False, because gigi knows not to flag them as
5181 -- being a violation of No_Implicit_Heap_Allocations.
5182
5183 if Restriction_Active (No_Allocators) then
5184 SS_Allocator := Heap_Allocator;
5185 Heap_Allocator := Make_Null (Loc);
5186 Pool_Allocator := Make_Null (Loc);
5187
5188 -- Otherwise the heap and pool allocators may be needed,
5189 -- so we make another allocator for secondary stack
5190 -- allocation.
5191
5192 else
5193 SS_Allocator := New_Copy_Tree (Heap_Allocator);
5194
5195 -- The heap and pool allocators are marked as
5196 -- Comes_From_Source since they correspond to an
5197 -- explicit user-written allocator (that is, it will
5198 -- only be executed on behalf of callers that call the
5199 -- function as initialization for such an allocator).
5200 -- Prevents errors when No_Implicit_Heap_Allocations
5201 -- is in force.
5202
5203 Set_Comes_From_Source (Heap_Allocator, True);
5204 Set_Comes_From_Source (Pool_Allocator, True);
5205 end if;
5206
5207 -- The allocator is returned on the secondary stack.
5208
5209 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5210 Set_Procedure_To_Call
5211 (SS_Allocator, RTE (RE_SS_Allocate));
5212
5213 -- The allocator is returned on the secondary stack,
5214 -- so indicate that the function return, as well as
5215 -- all blocks that encloses the allocator, must not
5216 -- release it. The flags must be set now because
5217 -- the decision to use the secondary stack is done
5218 -- very late in the course of expanding the return
5219 -- statement, past the point where these flags are
5220 -- normally set.
5221
5222 Set_Uses_Sec_Stack (Func_Id);
5223 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5224 Set_Sec_Stack_Needed_For_Return
5225 (Return_Statement_Entity (N));
5226 Set_Enclosing_Sec_Stack_Return (N);
5227
5228 -- Create an if statement to test the BIP_Alloc_Form
5229 -- formal and initialize the access object to either the
5230 -- BIP_Object_Access formal (BIP_Alloc_Form =
5231 -- Caller_Allocation), the result of allocating the
5232 -- object in the secondary stack (BIP_Alloc_Form =
5233 -- Secondary_Stack), or else an allocator to create the
5234 -- return object in the heap or user-defined pool
5235 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5236
5237 -- ??? An unchecked type conversion must be made in the
5238 -- case of assigning the access object formal to the
5239 -- local access object, because a normal conversion would
5240 -- be illegal in some cases (such as converting access-
5241 -- to-unconstrained to access-to-constrained), but the
5242 -- the unchecked conversion will presumably fail to work
5243 -- right in just such cases. It's not clear at all how to
5244 -- handle this. ???
5245
5246 Alloc_If_Stmt :=
5247 Make_If_Statement (Loc,
5248 Condition =>
5249 Make_Op_Eq (Loc,
5250 Left_Opnd =>
5251 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5252 Right_Opnd =>
5253 Make_Integer_Literal (Loc,
5254 UI_From_Int (BIP_Allocation_Form'Pos
5255 (Caller_Allocation)))),
5256
5257 Then_Statements => New_List (
5258 Make_Assignment_Statement (Loc,
5259 Name =>
5260 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5261 Expression =>
5262 Make_Unchecked_Type_Conversion (Loc,
5263 Subtype_Mark =>
5264 New_Occurrence_Of (Ref_Type, Loc),
5265 Expression =>
5266 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5267
5268 Elsif_Parts => New_List (
5269 Make_Elsif_Part (Loc,
5270 Condition =>
5271 Make_Op_Eq (Loc,
5272 Left_Opnd =>
5273 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5274 Right_Opnd =>
5275 Make_Integer_Literal (Loc,
5276 UI_From_Int (BIP_Allocation_Form'Pos
5277 (Secondary_Stack)))),
5278
5279 Then_Statements => New_List (
5280 Make_Assignment_Statement (Loc,
5281 Name =>
5282 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5283 Expression => SS_Allocator))),
5284
5285 Make_Elsif_Part (Loc,
5286 Condition =>
5287 Make_Op_Eq (Loc,
5288 Left_Opnd =>
5289 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5290 Right_Opnd =>
5291 Make_Integer_Literal (Loc,
5292 UI_From_Int (BIP_Allocation_Form'Pos
5293 (Global_Heap)))),
5294
5295 Then_Statements => New_List (
5296 Build_Heap_Allocator
5297 (Temp_Id => Alloc_Obj_Id,
5298 Temp_Typ => Ref_Type,
5299 Func_Id => Func_Id,
5300 Ret_Typ => Ret_Obj_Typ,
5301 Alloc_Expr => Heap_Allocator))),
5302
5303 -- ???If all is well, we can put the following
5304 -- 'elsif' in the 'else', but this is a useful
5305 -- self-check in case caller and callee don't agree
5306 -- on whether BIPAlloc and so on should be passed.
5307
5308 Make_Elsif_Part (Loc,
5309 Condition =>
5310 Make_Op_Eq (Loc,
5311 Left_Opnd =>
5312 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5313 Right_Opnd =>
5314 Make_Integer_Literal (Loc,
5315 UI_From_Int (BIP_Allocation_Form'Pos
5316 (User_Storage_Pool)))),
5317
5318 Then_Statements => New_List (
5319 Pool_Decl,
5320 Build_Heap_Allocator
5321 (Temp_Id => Alloc_Obj_Id,
5322 Temp_Typ => Ref_Type,
5323 Func_Id => Func_Id,
5324 Ret_Typ => Ret_Obj_Typ,
5325 Alloc_Expr => Pool_Allocator)))),
5326
5327 -- Raise Program_Error if it's none of the above;
5328 -- this is a compiler bug. ???PE_All_Guards_Closed
5329 -- is bogus; we should have a new code.
5330
5331 Else_Statements => New_List (
5332 Make_Raise_Program_Error (Loc,
5333 Reason => PE_All_Guards_Closed)));
5334
5335 -- If a separate initialization assignment was created
5336 -- earlier, append that following the assignment of the
5337 -- implicit access formal to the access object, to ensure
5338 -- that the return object is initialized in that case. In
5339 -- this situation, the target of the assignment must be
5340 -- rewritten to denote a dereference of the access to the
5341 -- return object passed in by the caller.
5342
5343 if Present (Init_Assignment) then
5344 Rewrite (Name (Init_Assignment),
5345 Make_Explicit_Dereference (Loc,
5346 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5347
5348 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5349
5350 Append_To
5351 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5352 end if;
5353
5354 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5355
5356 -- Remember the local access object for use in the
5357 -- dereference of the renaming created below.
5358
5359 Obj_Acc_Formal := Alloc_Obj_Id;
5360 end;
5361 end if;
5362
5363 -- Replace the return object declaration with a renaming of a
5364 -- dereference of the access value designating the return
5365 -- object.
5366
5367 Obj_Acc_Deref :=
5368 Make_Explicit_Dereference (Loc,
5369 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
5370
5371 Rewrite (Ret_Obj_Decl,
5372 Make_Object_Renaming_Declaration (Loc,
5373 Defining_Identifier => Ret_Obj_Id,
5374 Access_Definition => Empty,
5375 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
5376 Name => Obj_Acc_Deref));
5377
5378 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
5379 end;
5380 end if;
5381
5382 -- Case where we do not build a block
5383
5384 else
5385 -- We're about to drop Return_Object_Declarations on the floor, so
5386 -- we need to insert it, in case it got expanded into useful code.
5387 -- Remove side effects from expression, which may be duplicated in
5388 -- subsequent checks (see Expand_Simple_Function_Return).
5389
5390 Insert_List_Before (N, Return_Object_Declarations (N));
5391 Remove_Side_Effects (Exp);
5392
5393 -- Build simple_return_statement that returns the expression directly
5394
5395 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
5396 Result := Return_Stmt;
5397 end if;
5398
5399 -- Set the flag to prevent infinite recursion
5400
5401 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
5402
5403 Rewrite (N, Result);
5404 Analyze (N);
5405 end Expand_N_Extended_Return_Statement;
5406
5407 ----------------------------
5408 -- Expand_N_Function_Call --
5409 ----------------------------
5410
5411 procedure Expand_N_Function_Call (N : Node_Id) is
5412 begin
5413 Expand_Call (N);
5414 end Expand_N_Function_Call;
5415
5416 ---------------------------------------
5417 -- Expand_N_Procedure_Call_Statement --
5418 ---------------------------------------
5419
5420 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
5421 begin
5422 Expand_Call (N);
5423 end Expand_N_Procedure_Call_Statement;
5424
5425 --------------------------------------
5426 -- Expand_N_Simple_Return_Statement --
5427 --------------------------------------
5428
5429 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
5430 begin
5431 -- Defend against previous errors (i.e. the return statement calls a
5432 -- function that is not available in configurable runtime).
5433
5434 if Present (Expression (N))
5435 and then Nkind (Expression (N)) = N_Empty
5436 then
5437 Check_Error_Detected;
5438 return;
5439 end if;
5440
5441 -- Distinguish the function and non-function cases:
5442
5443 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
5444 when E_Function
5445 | E_Generic_Function
5446 =>
5447 Expand_Simple_Function_Return (N);
5448
5449 when E_Entry
5450 | E_Entry_Family
5451 | E_Generic_Procedure
5452 | E_Procedure
5453 | E_Return_Statement
5454 =>
5455 Expand_Non_Function_Return (N);
5456
5457 when others =>
5458 raise Program_Error;
5459 end case;
5460
5461 exception
5462 when RE_Not_Available =>
5463 return;
5464 end Expand_N_Simple_Return_Statement;
5465
5466 ------------------------------
5467 -- Expand_N_Subprogram_Body --
5468 ------------------------------
5469
5470 -- Add poll call if ATC polling is enabled, unless the body will be inlined
5471 -- by the back-end.
5472
5473 -- Add dummy push/pop label nodes at start and end to clear any local
5474 -- exception indications if local-exception-to-goto optimization is active.
5475
5476 -- Add return statement if last statement in body is not a return statement
5477 -- (this makes things easier on Gigi which does not want to have to handle
5478 -- a missing return).
5479
5480 -- Add call to Activate_Tasks if body is a task activator
5481
5482 -- Deal with possible detection of infinite recursion
5483
5484 -- Eliminate body completely if convention stubbed
5485
5486 -- Encode entity names within body, since we will not need to reference
5487 -- these entities any longer in the front end.
5488
5489 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
5490
5491 -- Reset Pure indication if any parameter has root type System.Address
5492 -- or has any parameters of limited types, where limited means that the
5493 -- run-time view is limited (i.e. the full type is limited).
5494
5495 -- Wrap thread body
5496
5497 procedure Expand_N_Subprogram_Body (N : Node_Id) is
5498 Body_Id : constant Entity_Id := Defining_Entity (N);
5499 HSS : constant Node_Id := Handled_Statement_Sequence (N);
5500 Loc : constant Source_Ptr := Sloc (N);
5501
5502 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
5503 -- Append a return statement to the statement sequence Stmts if the last
5504 -- statement is not already a return or a goto statement. Note that the
5505 -- latter test is not critical, it does not matter if we add a few extra
5506 -- returns, since they get eliminated anyway later on. Spec_Id denotes
5507 -- the corresponding spec of the subprogram body.
5508
5509 ----------------
5510 -- Add_Return --
5511 ----------------
5512
5513 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
5514 Last_Stmt : Node_Id;
5515 Loc : Source_Ptr;
5516 Stmt : Node_Id;
5517
5518 begin
5519 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5520 -- not relevant in this context since they are not executable.
5521
5522 Last_Stmt := Last (Stmts);
5523 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5524 Prev (Last_Stmt);
5525 end loop;
5526
5527 -- Now insert return unless last statement is a transfer
5528
5529 if not Is_Transfer (Last_Stmt) then
5530
5531 -- The source location for the return is the end label of the
5532 -- procedure if present. Otherwise use the sloc of the last
5533 -- statement in the list. If the list comes from a generated
5534 -- exception handler and we are not debugging generated code,
5535 -- all the statements within the handler are made invisible
5536 -- to the debugger.
5537
5538 if Nkind (Parent (Stmts)) = N_Exception_Handler
5539 and then not Comes_From_Source (Parent (Stmts))
5540 then
5541 Loc := Sloc (Last_Stmt);
5542 elsif Present (End_Label (HSS)) then
5543 Loc := Sloc (End_Label (HSS));
5544 else
5545 Loc := Sloc (Last_Stmt);
5546 end if;
5547
5548 -- Append return statement, and set analyzed manually. We can't
5549 -- call Analyze on this return since the scope is wrong.
5550
5551 -- Note: it almost works to push the scope and then do the Analyze
5552 -- call, but something goes wrong in some weird cases and it is
5553 -- not worth worrying about ???
5554
5555 Stmt := Make_Simple_Return_Statement (Loc);
5556
5557 -- The return statement is handled properly, and the call to the
5558 -- postcondition, inserted below, does not require information
5559 -- from the body either. However, that call is analyzed in the
5560 -- enclosing scope, and an elaboration check might improperly be
5561 -- added to it. A guard in Sem_Elab is needed to prevent that
5562 -- spurious check, see Check_Elab_Call.
5563
5564 Append_To (Stmts, Stmt);
5565 Set_Analyzed (Stmt);
5566
5567 -- Call the _Postconditions procedure if the related subprogram
5568 -- has contract assertions that need to be verified on exit.
5569
5570 if Ekind (Spec_Id) = E_Procedure
5571 and then Present (Postconditions_Proc (Spec_Id))
5572 then
5573 Insert_Action (Stmt,
5574 Make_Procedure_Call_Statement (Loc,
5575 Name =>
5576 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5577 end if;
5578 end if;
5579 end Add_Return;
5580
5581 -- Local variables
5582
5583 Except_H : Node_Id;
5584 L : List_Id;
5585 Spec_Id : Entity_Id;
5586
5587 -- Start of processing for Expand_N_Subprogram_Body
5588
5589 begin
5590 if Present (Corresponding_Spec (N)) then
5591 Spec_Id := Corresponding_Spec (N);
5592 else
5593 Spec_Id := Body_Id;
5594 end if;
5595
5596 -- If this is a Pure function which has any parameters whose root type
5597 -- is System.Address, reset the Pure indication.
5598 -- This check is also performed when the subprogram is frozen, but we
5599 -- repeat it on the body so that the indication is consistent, and so
5600 -- it applies as well to bodies without separate specifications.
5601
5602 if Is_Pure (Spec_Id)
5603 and then Is_Subprogram (Spec_Id)
5604 and then not Has_Pragma_Pure_Function (Spec_Id)
5605 then
5606 Check_Function_With_Address_Parameter (Spec_Id);
5607
5608 if Spec_Id /= Body_Id then
5609 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
5610 end if;
5611 end if;
5612
5613 -- Set L to either the list of declarations if present, or to the list
5614 -- of statements if no declarations are present. This is used to insert
5615 -- new stuff at the start.
5616
5617 if Is_Non_Empty_List (Declarations (N)) then
5618 L := Declarations (N);
5619 else
5620 L := Statements (HSS);
5621 end if;
5622
5623 -- If local-exception-to-goto optimization active, insert dummy push
5624 -- statements at start, and dummy pop statements at end, but inhibit
5625 -- this if we have No_Exception_Handlers, since they are useless and
5626 -- intefere with analysis, e.g. by codepeer.
5627
5628 if (Debug_Flag_Dot_G
5629 or else Restriction_Active (No_Exception_Propagation))
5630 and then not Restriction_Active (No_Exception_Handlers)
5631 and then not CodePeer_Mode
5632 and then Is_Non_Empty_List (L)
5633 then
5634 declare
5635 FS : constant Node_Id := First (L);
5636 FL : constant Source_Ptr := Sloc (FS);
5637 LS : Node_Id;
5638 LL : Source_Ptr;
5639
5640 begin
5641 -- LS points to either last statement, if statements are present
5642 -- or to the last declaration if there are no statements present.
5643 -- It is the node after which the pop's are generated.
5644
5645 if Is_Non_Empty_List (Statements (HSS)) then
5646 LS := Last (Statements (HSS));
5647 else
5648 LS := Last (L);
5649 end if;
5650
5651 LL := Sloc (LS);
5652
5653 Insert_List_Before_And_Analyze (FS, New_List (
5654 Make_Push_Constraint_Error_Label (FL),
5655 Make_Push_Program_Error_Label (FL),
5656 Make_Push_Storage_Error_Label (FL)));
5657
5658 Insert_List_After_And_Analyze (LS, New_List (
5659 Make_Pop_Constraint_Error_Label (LL),
5660 Make_Pop_Program_Error_Label (LL),
5661 Make_Pop_Storage_Error_Label (LL)));
5662 end;
5663 end if;
5664
5665 -- Need poll on entry to subprogram if polling enabled. We only do this
5666 -- for non-empty subprograms, since it does not seem necessary to poll
5667 -- for a dummy null subprogram.
5668
5669 if Is_Non_Empty_List (L) then
5670
5671 -- Do not add a polling call if the subprogram is to be inlined by
5672 -- the back-end, to avoid repeated calls with multiple inlinings.
5673
5674 if Is_Inlined (Spec_Id)
5675 and then Front_End_Inlining
5676 and then Optimization_Level > 1
5677 then
5678 null;
5679 else
5680 Generate_Poll_Call (First (L));
5681 end if;
5682 end if;
5683
5684 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5685
5686 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5687 declare
5688 F : Entity_Id;
5689 A : Node_Id;
5690
5691 begin
5692 -- Loop through formals
5693
5694 F := First_Formal (Spec_Id);
5695 while Present (F) loop
5696 if Is_Scalar_Type (Etype (F))
5697 and then Ekind (F) = E_Out_Parameter
5698 then
5699 Check_Restriction (No_Default_Initialization, F);
5700
5701 -- Insert the initialization. We turn off validity checks
5702 -- for this assignment, since we do not want any check on
5703 -- the initial value itself (which may well be invalid).
5704 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5705
5706 A :=
5707 Make_Assignment_Statement (Loc,
5708 Name => New_Occurrence_Of (F, Loc),
5709 Expression => Get_Simple_Init_Val (Etype (F), N));
5710 Set_Suppress_Assignment_Checks (A);
5711
5712 Insert_Before_And_Analyze (First (L),
5713 A, Suppress => Validity_Check);
5714 end if;
5715
5716 Next_Formal (F);
5717 end loop;
5718 end;
5719 end if;
5720
5721 -- Clear out statement list for stubbed procedure
5722
5723 if Present (Corresponding_Spec (N)) then
5724 Set_Elaboration_Flag (N, Spec_Id);
5725
5726 if Convention (Spec_Id) = Convention_Stubbed
5727 or else Is_Eliminated (Spec_Id)
5728 then
5729 Set_Declarations (N, Empty_List);
5730 Set_Handled_Statement_Sequence (N,
5731 Make_Handled_Sequence_Of_Statements (Loc,
5732 Statements => New_List (Make_Null_Statement (Loc))));
5733
5734 return;
5735 end if;
5736 end if;
5737
5738 -- Create a set of discriminals for the next protected subprogram body
5739
5740 if Is_List_Member (N)
5741 and then Present (Parent (List_Containing (N)))
5742 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5743 and then Present (Next_Protected_Operation (N))
5744 then
5745 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5746 end if;
5747
5748 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5749 -- subprograms with no specs are not frozen.
5750
5751 declare
5752 Typ : constant Entity_Id := Etype (Spec_Id);
5753 Utyp : constant Entity_Id := Underlying_Type (Typ);
5754
5755 begin
5756 if Is_Limited_View (Typ) then
5757 Set_Returns_By_Ref (Spec_Id);
5758
5759 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5760 Set_Returns_By_Ref (Spec_Id);
5761 end if;
5762 end;
5763
5764 -- For a procedure, we add a return for all possible syntactic ends of
5765 -- the subprogram.
5766
5767 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5768 Add_Return (Spec_Id, Statements (HSS));
5769
5770 if Present (Exception_Handlers (HSS)) then
5771 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
5772 while Present (Except_H) loop
5773 Add_Return (Spec_Id, Statements (Except_H));
5774 Next_Non_Pragma (Except_H);
5775 end loop;
5776 end if;
5777
5778 -- For a function, we must deal with the case where there is at least
5779 -- one missing return. What we do is to wrap the entire body of the
5780 -- function in a block:
5781
5782 -- begin
5783 -- ...
5784 -- end;
5785
5786 -- becomes
5787
5788 -- begin
5789 -- begin
5790 -- ...
5791 -- end;
5792
5793 -- raise Program_Error;
5794 -- end;
5795
5796 -- This approach is necessary because the raise must be signalled to the
5797 -- caller, not handled by any local handler (RM 6.4(11)).
5798
5799 -- Note: we do not need to analyze the constructed sequence here, since
5800 -- it has no handler, and an attempt to analyze the handled statement
5801 -- sequence twice is risky in various ways (e.g. the issue of expanding
5802 -- cleanup actions twice).
5803
5804 elsif Has_Missing_Return (Spec_Id) then
5805 declare
5806 Hloc : constant Source_Ptr := Sloc (HSS);
5807 Blok : constant Node_Id :=
5808 Make_Block_Statement (Hloc,
5809 Handled_Statement_Sequence => HSS);
5810 Rais : constant Node_Id :=
5811 Make_Raise_Program_Error (Hloc,
5812 Reason => PE_Missing_Return);
5813
5814 begin
5815 Set_Handled_Statement_Sequence (N,
5816 Make_Handled_Sequence_Of_Statements (Hloc,
5817 Statements => New_List (Blok, Rais)));
5818
5819 Push_Scope (Spec_Id);
5820 Analyze (Blok);
5821 Analyze (Rais);
5822 Pop_Scope;
5823 end;
5824 end if;
5825
5826 -- If subprogram contains a parameterless recursive call, then we may
5827 -- have an infinite recursion, so see if we can generate code to check
5828 -- for this possibility if storage checks are not suppressed.
5829
5830 if Ekind (Spec_Id) = E_Procedure
5831 and then Has_Recursive_Call (Spec_Id)
5832 and then not Storage_Checks_Suppressed (Spec_Id)
5833 then
5834 Detect_Infinite_Recursion (N, Spec_Id);
5835 end if;
5836
5837 -- Set to encode entity names in package body before gigi is called
5838
5839 Qualify_Entity_Names (N);
5840
5841 -- If the body belongs to a nonabstract library-level source primitive
5842 -- of a tagged type, install an elaboration check which ensures that a
5843 -- dispatching call targeting the primitive will not execute the body
5844 -- without it being previously elaborated.
5845
5846 Install_Primitive_Elaboration_Check (N);
5847 end Expand_N_Subprogram_Body;
5848
5849 -----------------------------------
5850 -- Expand_N_Subprogram_Body_Stub --
5851 -----------------------------------
5852
5853 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5854 Bod : Node_Id;
5855
5856 begin
5857 if Present (Corresponding_Body (N)) then
5858 Bod := Unit_Declaration_Node (Corresponding_Body (N));
5859
5860 -- The body may have been expanded already when it is analyzed
5861 -- through the subunit node. Do no expand again: it interferes
5862 -- with the construction of unnesting tables when generating C.
5863
5864 if not Analyzed (Bod) then
5865 Expand_N_Subprogram_Body (Bod);
5866 end if;
5867
5868 -- Add full qualification to entities that may be created late
5869 -- during unnesting.
5870
5871 Qualify_Entity_Names (N);
5872 end if;
5873 end Expand_N_Subprogram_Body_Stub;
5874
5875 -------------------------------------
5876 -- Expand_N_Subprogram_Declaration --
5877 -------------------------------------
5878
5879 -- If the declaration appears within a protected body, it is a private
5880 -- operation of the protected type. We must create the corresponding
5881 -- protected subprogram an associated formals. For a normal protected
5882 -- operation, this is done when expanding the protected type declaration.
5883
5884 -- If the declaration is for a null procedure, emit null body
5885
5886 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5887 Loc : constant Source_Ptr := Sloc (N);
5888 Subp : constant Entity_Id := Defining_Entity (N);
5889
5890 -- Local variables
5891
5892 Scop : constant Entity_Id := Scope (Subp);
5893 Prot_Bod : Node_Id;
5894 Prot_Decl : Node_Id;
5895 Prot_Id : Entity_Id;
5896
5897 -- Start of processing for Expand_N_Subprogram_Declaration
5898
5899 begin
5900 -- In SPARK, subprogram declarations are only allowed in package
5901 -- specifications.
5902
5903 if Nkind (Parent (N)) /= N_Package_Specification then
5904 if Nkind (Parent (N)) = N_Compilation_Unit then
5905 Check_SPARK_05_Restriction
5906 ("subprogram declaration is not a library item", N);
5907
5908 elsif Present (Next (N))
5909 and then Nkind (Next (N)) = N_Pragma
5910 and then Get_Pragma_Id (Next (N)) = Pragma_Import
5911 then
5912 -- In SPARK, subprogram declarations are also permitted in
5913 -- declarative parts when immediately followed by a corresponding
5914 -- pragma Import. We only check here that there is some pragma
5915 -- Import.
5916
5917 null;
5918 else
5919 Check_SPARK_05_Restriction
5920 ("subprogram declaration is not allowed here", N);
5921 end if;
5922 end if;
5923
5924 -- Deal with case of protected subprogram. Do not generate protected
5925 -- operation if operation is flagged as eliminated.
5926
5927 if Is_List_Member (N)
5928 and then Present (Parent (List_Containing (N)))
5929 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5930 and then Is_Protected_Type (Scop)
5931 then
5932 if No (Protected_Body_Subprogram (Subp))
5933 and then not Is_Eliminated (Subp)
5934 then
5935 Prot_Decl :=
5936 Make_Subprogram_Declaration (Loc,
5937 Specification =>
5938 Build_Protected_Sub_Specification
5939 (N, Scop, Unprotected_Mode));
5940
5941 -- The protected subprogram is declared outside of the protected
5942 -- body. Given that the body has frozen all entities so far, we
5943 -- analyze the subprogram and perform freezing actions explicitly.
5944 -- including the generation of an explicit freeze node, to ensure
5945 -- that gigi has the proper order of elaboration.
5946 -- If the body is a subunit, the insertion point is before the
5947 -- stub in the parent.
5948
5949 Prot_Bod := Parent (List_Containing (N));
5950
5951 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5952 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5953 end if;
5954
5955 Insert_Before (Prot_Bod, Prot_Decl);
5956 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5957 Set_Has_Delayed_Freeze (Prot_Id);
5958
5959 Push_Scope (Scope (Scop));
5960 Analyze (Prot_Decl);
5961 Freeze_Before (N, Prot_Id);
5962 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5963
5964 -- Create protected operation as well. Even though the operation
5965 -- is only accessible within the body, it is possible to make it
5966 -- available outside of the protected object by using 'Access to
5967 -- provide a callback, so build protected version in all cases.
5968
5969 Prot_Decl :=
5970 Make_Subprogram_Declaration (Loc,
5971 Specification =>
5972 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5973 Insert_Before (Prot_Bod, Prot_Decl);
5974 Analyze (Prot_Decl);
5975
5976 Pop_Scope;
5977 end if;
5978
5979 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5980 -- cases this is superfluous because calls to it will be automatically
5981 -- inlined, but we definitely need the body if preconditions for the
5982 -- procedure are present, or if performing coverage analysis.
5983
5984 elsif Nkind (Specification (N)) = N_Procedure_Specification
5985 and then Null_Present (Specification (N))
5986 then
5987 declare
5988 Bod : constant Node_Id := Body_To_Inline (N);
5989
5990 begin
5991 Set_Has_Completion (Subp, False);
5992 Append_Freeze_Action (Subp, Bod);
5993
5994 -- The body now contains raise statements, so calls to it will
5995 -- not be inlined.
5996
5997 Set_Is_Inlined (Subp, False);
5998 end;
5999 end if;
6000
6001 -- When generating C code, transform a function that returns a
6002 -- constrained array type into a procedure with an out parameter
6003 -- that carries the return value.
6004
6005 -- We skip this transformation for unchecked conversions, since they
6006 -- are not needed by the C generator (and this also produces cleaner
6007 -- output).
6008
6009 if Modify_Tree_For_C
6010 and then Nkind (Specification (N)) = N_Function_Specification
6011 and then Is_Array_Type (Etype (Subp))
6012 and then Is_Constrained (Etype (Subp))
6013 and then not Is_Unchecked_Conversion_Instance (Subp)
6014 then
6015 Build_Procedure_Form (N);
6016 end if;
6017 end Expand_N_Subprogram_Declaration;
6018
6019 --------------------------------
6020 -- Expand_Non_Function_Return --
6021 --------------------------------
6022
6023 procedure Expand_Non_Function_Return (N : Node_Id) is
6024 pragma Assert (No (Expression (N)));
6025
6026 Loc : constant Source_Ptr := Sloc (N);
6027 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6028 Kind : constant Entity_Kind := Ekind (Scope_Id);
6029 Call : Node_Id;
6030 Acc_Stat : Node_Id;
6031 Goto_Stat : Node_Id;
6032 Lab_Node : Node_Id;
6033
6034 begin
6035 -- Call the _Postconditions procedure if the related subprogram has
6036 -- contract assertions that need to be verified on exit.
6037
6038 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
6039 and then Present (Postconditions_Proc (Scope_Id))
6040 then
6041 Insert_Action (N,
6042 Make_Procedure_Call_Statement (Loc,
6043 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
6044 end if;
6045
6046 -- If it is a return from a procedure do no extra steps
6047
6048 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6049 return;
6050
6051 -- If it is a nested return within an extended one, replace it with a
6052 -- return of the previously declared return object.
6053
6054 elsif Kind = E_Return_Statement then
6055 Rewrite (N,
6056 Make_Simple_Return_Statement (Loc,
6057 Expression =>
6058 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6059 Set_Comes_From_Extended_Return_Statement (N);
6060 Set_Return_Statement_Entity (N, Scope_Id);
6061 Expand_Simple_Function_Return (N);
6062 return;
6063 end if;
6064
6065 pragma Assert (Is_Entry (Scope_Id));
6066
6067 -- Look at the enclosing block to see whether the return is from an
6068 -- accept statement or an entry body.
6069
6070 for J in reverse 0 .. Scope_Stack.Last loop
6071 Scope_Id := Scope_Stack.Table (J).Entity;
6072 exit when Is_Concurrent_Type (Scope_Id);
6073 end loop;
6074
6075 -- If it is a return from accept statement it is expanded as call to
6076 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6077
6078 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6079 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6080
6081 if Is_Task_Type (Scope_Id) then
6082
6083 Call :=
6084 Make_Procedure_Call_Statement (Loc,
6085 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6086 Insert_Before (N, Call);
6087 -- why not insert actions here???
6088 Analyze (Call);
6089
6090 Acc_Stat := Parent (N);
6091 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6092 Acc_Stat := Parent (Acc_Stat);
6093 end loop;
6094
6095 Lab_Node := Last (Statements
6096 (Handled_Statement_Sequence (Acc_Stat)));
6097
6098 Goto_Stat := Make_Goto_Statement (Loc,
6099 Name => New_Occurrence_Of
6100 (Entity (Identifier (Lab_Node)), Loc));
6101
6102 Set_Analyzed (Goto_Stat);
6103
6104 Rewrite (N, Goto_Stat);
6105 Analyze (N);
6106
6107 -- If it is a return from an entry body, put a Complete_Entry_Body call
6108 -- in front of the return.
6109
6110 elsif Is_Protected_Type (Scope_Id) then
6111 Call :=
6112 Make_Procedure_Call_Statement (Loc,
6113 Name =>
6114 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6115 Parameter_Associations => New_List (
6116 Make_Attribute_Reference (Loc,
6117 Prefix =>
6118 New_Occurrence_Of
6119 (Find_Protection_Object (Current_Scope), Loc),
6120 Attribute_Name => Name_Unchecked_Access)));
6121
6122 Insert_Before (N, Call);
6123 Analyze (Call);
6124 end if;
6125 end Expand_Non_Function_Return;
6126
6127 ---------------------------------------
6128 -- Expand_Protected_Object_Reference --
6129 ---------------------------------------
6130
6131 function Expand_Protected_Object_Reference
6132 (N : Node_Id;
6133 Scop : Entity_Id) return Node_Id
6134 is
6135 Loc : constant Source_Ptr := Sloc (N);
6136 Corr : Entity_Id;
6137 Rec : Node_Id;
6138 Param : Entity_Id;
6139 Proc : Entity_Id;
6140
6141 begin
6142 Rec := Make_Identifier (Loc, Name_uObject);
6143 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6144
6145 -- Find enclosing protected operation, and retrieve its first parameter,
6146 -- which denotes the enclosing protected object. If the enclosing
6147 -- operation is an entry, we are immediately within the protected body,
6148 -- and we can retrieve the object from the service entries procedure. A
6149 -- barrier function has the same signature as an entry. A barrier
6150 -- function is compiled within the protected object, but unlike
6151 -- protected operations its never needs locks, so that its protected
6152 -- body subprogram points to itself.
6153
6154 Proc := Current_Scope;
6155 while Present (Proc)
6156 and then Scope (Proc) /= Scop
6157 loop
6158 Proc := Scope (Proc);
6159 end loop;
6160
6161 Corr := Protected_Body_Subprogram (Proc);
6162
6163 if No (Corr) then
6164
6165 -- Previous error left expansion incomplete.
6166 -- Nothing to do on this call.
6167
6168 return Empty;
6169 end if;
6170
6171 Param :=
6172 Defining_Identifier
6173 (First (Parameter_Specifications (Parent (Corr))));
6174
6175 if Is_Subprogram (Proc) and then Proc /= Corr then
6176
6177 -- Protected function or procedure
6178
6179 Set_Entity (Rec, Param);
6180
6181 -- Rec is a reference to an entity which will not be in scope when
6182 -- the call is reanalyzed, and needs no further analysis.
6183
6184 Set_Analyzed (Rec);
6185
6186 else
6187 -- Entry or barrier function for entry body. The first parameter of
6188 -- the entry body procedure is pointer to the object. We create a
6189 -- local variable of the proper type, duplicating what is done to
6190 -- define _object later on.
6191
6192 declare
6193 Decls : List_Id;
6194 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6195
6196 begin
6197 Decls := New_List (
6198 Make_Full_Type_Declaration (Loc,
6199 Defining_Identifier => Obj_Ptr,
6200 Type_Definition =>
6201 Make_Access_To_Object_Definition (Loc,
6202 Subtype_Indication =>
6203 New_Occurrence_Of
6204 (Corresponding_Record_Type (Scop), Loc))));
6205
6206 Insert_Actions (N, Decls);
6207 Freeze_Before (N, Obj_Ptr);
6208
6209 Rec :=
6210 Make_Explicit_Dereference (Loc,
6211 Prefix =>
6212 Unchecked_Convert_To (Obj_Ptr,
6213 New_Occurrence_Of (Param, Loc)));
6214
6215 -- Analyze new actual. Other actuals in calls are already analyzed
6216 -- and the list of actuals is not reanalyzed after rewriting.
6217
6218 Set_Parent (Rec, N);
6219 Analyze (Rec);
6220 end;
6221 end if;
6222
6223 return Rec;
6224 end Expand_Protected_Object_Reference;
6225
6226 --------------------------------------
6227 -- Expand_Protected_Subprogram_Call --
6228 --------------------------------------
6229
6230 procedure Expand_Protected_Subprogram_Call
6231 (N : Node_Id;
6232 Subp : Entity_Id;
6233 Scop : Entity_Id)
6234 is
6235 Rec : Node_Id;
6236
6237 procedure Expand_Internal_Init_Call;
6238 -- A call to an operation of the type may occur in the initialization
6239 -- of a private component. In that case the prefix of the call is an
6240 -- entity name and the call is treated as internal even though it
6241 -- appears in code outside of the protected type.
6242
6243 procedure Freeze_Called_Function;
6244 -- If it is a function call it can appear in elaboration code and
6245 -- the called entity must be frozen before the call. This must be
6246 -- done before the call is expanded, as the expansion may rewrite it
6247 -- to something other than a call (e.g. a temporary initialized in a
6248 -- transient block).
6249
6250 -------------------------------
6251 -- Expand_Internal_Init_Call --
6252 -------------------------------
6253
6254 procedure Expand_Internal_Init_Call is
6255 begin
6256 -- If the context is a protected object (rather than a protected
6257 -- type) the call itself is bound to raise program_error because
6258 -- the protected body will not have been elaborated yet. This is
6259 -- diagnosed subsequently in Sem_Elab.
6260
6261 Freeze_Called_Function;
6262
6263 -- The target of the internal call is the first formal of the
6264 -- enclosing initialization procedure.
6265
6266 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6267 Build_Protected_Subprogram_Call (N,
6268 Name => Name (N),
6269 Rec => Rec,
6270 External => False);
6271 Analyze (N);
6272 Resolve (N, Etype (Subp));
6273 end Expand_Internal_Init_Call;
6274
6275 ----------------------------
6276 -- Freeze_Called_Function --
6277 ----------------------------
6278
6279 procedure Freeze_Called_Function is
6280 begin
6281 if Ekind (Subp) = E_Function then
6282 Freeze_Expression (Name (N));
6283 end if;
6284 end Freeze_Called_Function;
6285
6286 -- Start of processing for Expand_Protected_Subprogram_Call
6287
6288 begin
6289 -- If the protected object is not an enclosing scope, this is an inter-
6290 -- object function call. Inter-object procedure calls are expanded by
6291 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6292 -- subprogram being called is in the protected body being compiled, and
6293 -- if the protected object in the call is statically the enclosing type.
6294 -- The object may be a component of some other data structure, in which
6295 -- case this must be handled as an inter-object call.
6296
6297 if not In_Open_Scopes (Scop)
6298 or else Is_Entry_Wrapper (Current_Scope)
6299 or else not Is_Entity_Name (Name (N))
6300 then
6301 if Nkind (Name (N)) = N_Selected_Component then
6302 Rec := Prefix (Name (N));
6303
6304 elsif Nkind (Name (N)) = N_Indexed_Component then
6305 Rec := Prefix (Prefix (Name (N)));
6306
6307 -- If this is a call within an entry wrapper, it appears within a
6308 -- precondition that calls another primitive of the synchronized
6309 -- type. The target object of the call is the first actual on the
6310 -- wrapper. Note that this is an external call, because the wrapper
6311 -- is called outside of the synchronized object. This means that
6312 -- an entry call to an entry with preconditions involves two
6313 -- synchronized operations.
6314
6315 elsif Ekind (Current_Scope) = E_Procedure
6316 and then Is_Entry_Wrapper (Current_Scope)
6317 then
6318 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6319
6320 else
6321 -- If the context is the initialization procedure for a protected
6322 -- type, the call is legal because the called entity must be a
6323 -- function of that enclosing type, and this is treated as an
6324 -- internal call.
6325
6326 pragma Assert
6327 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
6328
6329 Expand_Internal_Init_Call;
6330 return;
6331 end if;
6332
6333 Freeze_Called_Function;
6334 Build_Protected_Subprogram_Call (N,
6335 Name => New_Occurrence_Of (Subp, Sloc (N)),
6336 Rec => Convert_Concurrent (Rec, Etype (Rec)),
6337 External => True);
6338
6339 else
6340 Rec := Expand_Protected_Object_Reference (N, Scop);
6341
6342 if No (Rec) then
6343 return;
6344 end if;
6345
6346 Freeze_Called_Function;
6347 Build_Protected_Subprogram_Call (N,
6348 Name => Name (N),
6349 Rec => Rec,
6350 External => False);
6351 end if;
6352
6353 -- Analyze and resolve the new call. The actuals have already been
6354 -- resolved, but expansion of a function call will add extra actuals
6355 -- if needed. Analysis of a procedure call already includes resolution.
6356
6357 Analyze (N);
6358
6359 if Ekind (Subp) = E_Function then
6360 Resolve (N, Etype (Subp));
6361 end if;
6362 end Expand_Protected_Subprogram_Call;
6363
6364 -----------------------------------
6365 -- Expand_Simple_Function_Return --
6366 -----------------------------------
6367
6368 -- The "simple" comes from the syntax rule simple_return_statement. The
6369 -- semantics are not at all simple.
6370
6371 procedure Expand_Simple_Function_Return (N : Node_Id) is
6372 Loc : constant Source_Ptr := Sloc (N);
6373
6374 Scope_Id : constant Entity_Id :=
6375 Return_Applies_To (Return_Statement_Entity (N));
6376 -- The function we are returning from
6377
6378 R_Type : constant Entity_Id := Etype (Scope_Id);
6379 -- The result type of the function
6380
6381 Utyp : constant Entity_Id := Underlying_Type (R_Type);
6382
6383 Exp : Node_Id := Expression (N);
6384 pragma Assert (Present (Exp));
6385
6386 Exptyp : constant Entity_Id := Etype (Exp);
6387 -- The type of the expression (not necessarily the same as R_Type)
6388
6389 Subtype_Ind : Node_Id;
6390 -- If the result type of the function is class-wide and the expression
6391 -- has a specific type, then we use the expression's type as the type of
6392 -- the return object. In cases where the expression is an aggregate that
6393 -- is built in place, this avoids the need for an expensive conversion
6394 -- of the return object to the specific type on assignments to the
6395 -- individual components.
6396
6397 begin
6398 if Is_Class_Wide_Type (R_Type)
6399 and then not Is_Class_Wide_Type (Exptyp)
6400 and then Nkind (Exp) /= N_Type_Conversion
6401 then
6402 Subtype_Ind := New_Occurrence_Of (Exptyp, Loc);
6403 else
6404 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
6405
6406 -- If the result type is class-wide and the expression is a view
6407 -- conversion, the conversion plays no role in the expansion because
6408 -- it does not modify the tag of the object. Remove the conversion
6409 -- altogether to prevent tag overwriting.
6410
6411 if Is_Class_Wide_Type (R_Type)
6412 and then not Is_Class_Wide_Type (Exptyp)
6413 and then Nkind (Exp) = N_Type_Conversion
6414 then
6415 Exp := Expression (Exp);
6416 end if;
6417 end if;
6418
6419 -- For the case of a simple return that does not come from an
6420 -- extended return, in the case of build-in-place, we rewrite
6421 -- "return <expression>;" to be:
6422
6423 -- return _anon_ : <return_subtype> := <expression>
6424
6425 -- The expansion produced by Expand_N_Extended_Return_Statement will
6426 -- contain simple return statements (for example, a block containing
6427 -- simple return of the return object), which brings us back here with
6428 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
6429 -- checking for a simple return that does not come from an extended
6430 -- return is to avoid this infinite recursion.
6431
6432 -- The reason for this design is that for Ada 2005 limited returns, we
6433 -- need to reify the return object, so we can build it "in place", and
6434 -- we need a block statement to hang finalization and tasking stuff.
6435
6436 -- ??? In order to avoid disruption, we avoid translating to extended
6437 -- return except in the cases where we really need to (Ada 2005 for
6438 -- inherently limited). We might prefer to do this translation in all
6439 -- cases (except perhaps for the case of Ada 95 inherently limited),
6440 -- in order to fully exercise the Expand_N_Extended_Return_Statement
6441 -- code. This would also allow us to do the build-in-place optimization
6442 -- for efficiency even in cases where it is semantically not required.
6443
6444 -- As before, we check the type of the return expression rather than the
6445 -- return type of the function, because the latter may be a limited
6446 -- class-wide interface type, which is not a limited type, even though
6447 -- the type of the expression may be.
6448
6449 pragma Assert
6450 (Comes_From_Extended_Return_Statement (N)
6451 or else not Is_Build_In_Place_Function_Call (Exp)
6452 or else Is_Build_In_Place_Function (Scope_Id));
6453
6454 if not Comes_From_Extended_Return_Statement (N)
6455 and then Is_Build_In_Place_Function (Scope_Id)
6456 and then not Debug_Flag_Dot_L
6457
6458 -- The functionality of interface thunks is simple and it is always
6459 -- handled by means of simple return statements. This leaves their
6460 -- expansion simple and clean.
6461
6462 and then not Is_Thunk (Current_Scope)
6463 then
6464 declare
6465 Return_Object_Entity : constant Entity_Id :=
6466 Make_Temporary (Loc, 'R', Exp);
6467
6468 Obj_Decl : constant Node_Id :=
6469 Make_Object_Declaration (Loc,
6470 Defining_Identifier => Return_Object_Entity,
6471 Object_Definition => Subtype_Ind,
6472 Expression => Exp);
6473
6474 Ext : constant Node_Id :=
6475 Make_Extended_Return_Statement (Loc,
6476 Return_Object_Declarations => New_List (Obj_Decl));
6477 -- Do not perform this high-level optimization if the result type
6478 -- is an interface because the "this" pointer must be displaced.
6479
6480 begin
6481 Rewrite (N, Ext);
6482 Analyze (N);
6483 return;
6484 end;
6485 end if;
6486
6487 -- Here we have a simple return statement that is part of the expansion
6488 -- of an extended return statement (either written by the user, or
6489 -- generated by the above code).
6490
6491 -- Always normalize C/Fortran boolean result. This is not always needed,
6492 -- but it seems a good idea to minimize the passing around of non-
6493 -- normalized values, and in any case this handles the processing of
6494 -- barrier functions for protected types, which turn the condition into
6495 -- a return statement.
6496
6497 if Is_Boolean_Type (Exptyp)
6498 and then Nonzero_Is_True (Exptyp)
6499 then
6500 Adjust_Condition (Exp);
6501 Adjust_Result_Type (Exp, Exptyp);
6502 end if;
6503
6504 -- Do validity check if enabled for returns
6505
6506 if Validity_Checks_On
6507 and then Validity_Check_Returns
6508 then
6509 Ensure_Valid (Exp);
6510 end if;
6511
6512 -- Check the result expression of a scalar function against the subtype
6513 -- of the function by inserting a conversion. This conversion must
6514 -- eventually be performed for other classes of types, but for now it's
6515 -- only done for scalars.
6516 -- ???
6517
6518 if Is_Scalar_Type (Exptyp) then
6519 Rewrite (Exp, Convert_To (R_Type, Exp));
6520
6521 -- The expression is resolved to ensure that the conversion gets
6522 -- expanded to generate a possible constraint check.
6523
6524 Analyze_And_Resolve (Exp, R_Type);
6525 end if;
6526
6527 -- Deal with returning variable length objects and controlled types
6528
6529 -- Nothing to do if we are returning by reference, or this is not a
6530 -- type that requires special processing (indicated by the fact that
6531 -- it requires a cleanup scope for the secondary stack case).
6532
6533 if Is_Build_In_Place_Function (Scope_Id)
6534 or else Is_Limited_Interface (Exptyp)
6535 then
6536 null;
6537
6538 -- No copy needed for thunks returning interface type objects since
6539 -- the object is returned by reference and the maximum functionality
6540 -- required is just to displace the pointer.
6541
6542 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
6543 null;
6544
6545 -- If the call is within a thunk and the type is a limited view, the
6546 -- backend will eventually see the non-limited view of the type.
6547
6548 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
6549 return;
6550
6551 elsif not Requires_Transient_Scope (R_Type) then
6552
6553 -- Mutable records with variable-length components are not returned
6554 -- on the sec-stack, so we need to make sure that the back end will
6555 -- only copy back the size of the actual value, and not the maximum
6556 -- size. We create an actual subtype for this purpose. However we
6557 -- need not do it if the expression is a function call since this
6558 -- will be done in the called function and doing it here too would
6559 -- cause a temporary with maximum size to be created.
6560
6561 declare
6562 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
6563 Decl : Node_Id;
6564 Ent : Entity_Id;
6565 begin
6566 if Nkind (Exp) /= N_Function_Call
6567 and then Has_Discriminants (Ubt)
6568 and then not Is_Constrained (Ubt)
6569 and then not Has_Unchecked_Union (Ubt)
6570 then
6571 Decl := Build_Actual_Subtype (Ubt, Exp);
6572 Ent := Defining_Identifier (Decl);
6573 Insert_Action (Exp, Decl);
6574 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6575 Analyze_And_Resolve (Exp);
6576 end if;
6577 end;
6578
6579 -- Here if secondary stack is used
6580
6581 else
6582 -- Prevent the reclamation of the secondary stack by all enclosing
6583 -- blocks and loops as well as the related function; otherwise the
6584 -- result would be reclaimed too early.
6585
6586 Set_Enclosing_Sec_Stack_Return (N);
6587
6588 -- Optimize the case where the result is a function call. In this
6589 -- case either the result is already on the secondary stack, or is
6590 -- already being returned with the stack pointer depressed and no
6591 -- further processing is required except to set the By_Ref flag
6592 -- to ensure that gigi does not attempt an extra unnecessary copy.
6593 -- (actually not just unnecessary but harmfully wrong in the case
6594 -- of a controlled type, where gigi does not know how to do a copy).
6595 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6596 -- for array types if the constrained status of the target type is
6597 -- different from that of the expression.
6598
6599 if Requires_Transient_Scope (Exptyp)
6600 and then
6601 (not Is_Array_Type (Exptyp)
6602 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6603 or else CW_Or_Has_Controlled_Part (Utyp))
6604 and then Nkind (Exp) = N_Function_Call
6605 then
6606 Set_By_Ref (N);
6607
6608 -- Remove side effects from the expression now so that other parts
6609 -- of the expander do not have to reanalyze this node without this
6610 -- optimization
6611
6612 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6613
6614 -- Ada 2005 (AI-251): If the type of the returned object is
6615 -- an interface then add an implicit type conversion to force
6616 -- displacement of the "this" pointer.
6617
6618 if Is_Interface (R_Type) then
6619 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6620 end if;
6621
6622 Analyze_And_Resolve (Exp, R_Type);
6623
6624 -- For controlled types, do the allocation on the secondary stack
6625 -- manually in order to call adjust at the right time:
6626
6627 -- type Anon1 is access R_Type;
6628 -- for Anon1'Storage_pool use ss_pool;
6629 -- Anon2 : anon1 := new R_Type'(expr);
6630 -- return Anon2.all;
6631
6632 -- We do the same for classwide types that are not potentially
6633 -- controlled (by the virtue of restriction No_Finalization) because
6634 -- gigi is not able to properly allocate class-wide types.
6635
6636 elsif CW_Or_Has_Controlled_Part (Utyp) then
6637 declare
6638 Loc : constant Source_Ptr := Sloc (N);
6639 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6640 Alloc_Node : Node_Id;
6641 Temp : Entity_Id;
6642
6643 begin
6644 Set_Ekind (Acc_Typ, E_Access_Type);
6645
6646 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6647
6648 -- This is an allocator for the secondary stack, and it's fine
6649 -- to have Comes_From_Source set False on it, as gigi knows not
6650 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6651
6652 Alloc_Node :=
6653 Make_Allocator (Loc,
6654 Expression =>
6655 Make_Qualified_Expression (Loc,
6656 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6657 Expression => Relocate_Node (Exp)));
6658
6659 -- We do not want discriminant checks on the declaration,
6660 -- given that it gets its value from the allocator.
6661
6662 Set_No_Initialization (Alloc_Node);
6663
6664 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6665
6666 Insert_List_Before_And_Analyze (N, New_List (
6667 Make_Full_Type_Declaration (Loc,
6668 Defining_Identifier => Acc_Typ,
6669 Type_Definition =>
6670 Make_Access_To_Object_Definition (Loc,
6671 Subtype_Indication => Subtype_Ind)),
6672
6673 Make_Object_Declaration (Loc,
6674 Defining_Identifier => Temp,
6675 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6676 Expression => Alloc_Node)));
6677
6678 Rewrite (Exp,
6679 Make_Explicit_Dereference (Loc,
6680 Prefix => New_Occurrence_Of (Temp, Loc)));
6681
6682 -- Ada 2005 (AI-251): If the type of the returned object is
6683 -- an interface then add an implicit type conversion to force
6684 -- displacement of the "this" pointer.
6685
6686 if Is_Interface (R_Type) then
6687 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6688 end if;
6689
6690 Analyze_And_Resolve (Exp, R_Type);
6691 end;
6692
6693 -- Otherwise use the gigi mechanism to allocate result on the
6694 -- secondary stack.
6695
6696 else
6697 Check_Restriction (No_Secondary_Stack, N);
6698 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6699 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6700 end if;
6701 end if;
6702
6703 -- Implement the rules of 6.5(8-10), which require a tag check in
6704 -- the case of a limited tagged return type, and tag reassignment for
6705 -- nonlimited tagged results. These actions are needed when the return
6706 -- type is a specific tagged type and the result expression is a
6707 -- conversion or a formal parameter, because in that case the tag of
6708 -- the expression might differ from the tag of the specific result type.
6709
6710 if Is_Tagged_Type (Utyp)
6711 and then not Is_Class_Wide_Type (Utyp)
6712 and then (Nkind_In (Exp, N_Type_Conversion,
6713 N_Unchecked_Type_Conversion)
6714 or else (Is_Entity_Name (Exp)
6715 and then Ekind (Entity (Exp)) in Formal_Kind))
6716 then
6717 -- When the return type is limited, perform a check that the tag of
6718 -- the result is the same as the tag of the return type.
6719
6720 if Is_Limited_Type (R_Type) then
6721 Insert_Action (Exp,
6722 Make_Raise_Constraint_Error (Loc,
6723 Condition =>
6724 Make_Op_Ne (Loc,
6725 Left_Opnd =>
6726 Make_Selected_Component (Loc,
6727 Prefix => Duplicate_Subexpr (Exp),
6728 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6729 Right_Opnd =>
6730 Make_Attribute_Reference (Loc,
6731 Prefix =>
6732 New_Occurrence_Of (Base_Type (Utyp), Loc),
6733 Attribute_Name => Name_Tag)),
6734 Reason => CE_Tag_Check_Failed));
6735
6736 -- If the result type is a specific nonlimited tagged type, then we
6737 -- have to ensure that the tag of the result is that of the result
6738 -- type. This is handled by making a copy of the expression in
6739 -- the case where it might have a different tag, namely when the
6740 -- expression is a conversion or a formal parameter. We create a new
6741 -- object of the result type and initialize it from the expression,
6742 -- which will implicitly force the tag to be set appropriately.
6743
6744 else
6745 declare
6746 ExpR : constant Node_Id := Relocate_Node (Exp);
6747 Result_Id : constant Entity_Id :=
6748 Make_Temporary (Loc, 'R', ExpR);
6749 Result_Exp : constant Node_Id :=
6750 New_Occurrence_Of (Result_Id, Loc);
6751 Result_Obj : constant Node_Id :=
6752 Make_Object_Declaration (Loc,
6753 Defining_Identifier => Result_Id,
6754 Object_Definition =>
6755 New_Occurrence_Of (R_Type, Loc),
6756 Constant_Present => True,
6757 Expression => ExpR);
6758
6759 begin
6760 Set_Assignment_OK (Result_Obj);
6761 Insert_Action (Exp, Result_Obj);
6762
6763 Rewrite (Exp, Result_Exp);
6764 Analyze_And_Resolve (Exp, R_Type);
6765 end;
6766 end if;
6767
6768 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6769 -- a check that the level of the return expression's underlying type
6770 -- is not deeper than the level of the master enclosing the function.
6771 -- Always generate the check when the type of the return expression
6772 -- is class-wide, when it's a type conversion, or when it's a formal
6773 -- parameter. Otherwise, suppress the check in the case where the
6774 -- return expression has a specific type whose level is known not to
6775 -- be statically deeper than the function's result type.
6776
6777 -- No runtime check needed in interface thunks since it is performed
6778 -- by the target primitive associated with the thunk.
6779
6780 -- Note: accessibility check is skipped in the VM case, since there
6781 -- does not seem to be any practical way to implement this check.
6782
6783 elsif Ada_Version >= Ada_2005
6784 and then Tagged_Type_Expansion
6785 and then Is_Class_Wide_Type (R_Type)
6786 and then not Is_Thunk (Current_Scope)
6787 and then not Scope_Suppress.Suppress (Accessibility_Check)
6788 and then
6789 (Is_Class_Wide_Type (Etype (Exp))
6790 or else Nkind_In (Exp, N_Type_Conversion,
6791 N_Unchecked_Type_Conversion)
6792 or else (Is_Entity_Name (Exp)
6793 and then Ekind (Entity (Exp)) in Formal_Kind)
6794 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6795 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6796 then
6797 declare
6798 Tag_Node : Node_Id;
6799
6800 begin
6801 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6802 -- "this" to reference the base of the object. This is required to
6803 -- get access to the TSD of the object.
6804
6805 if Is_Class_Wide_Type (Etype (Exp))
6806 and then Is_Interface (Etype (Exp))
6807 then
6808 -- If the expression is an explicit dereference then we can
6809 -- directly displace the pointer to reference the base of
6810 -- the object.
6811
6812 if Nkind (Exp) = N_Explicit_Dereference then
6813 Tag_Node :=
6814 Make_Explicit_Dereference (Loc,
6815 Prefix =>
6816 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6817 Make_Function_Call (Loc,
6818 Name =>
6819 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6820 Parameter_Associations => New_List (
6821 Unchecked_Convert_To (RTE (RE_Address),
6822 Duplicate_Subexpr (Prefix (Exp)))))));
6823
6824 -- Similar case to the previous one but the expression is a
6825 -- renaming of an explicit dereference.
6826
6827 elsif Nkind (Exp) = N_Identifier
6828 and then Present (Renamed_Object (Entity (Exp)))
6829 and then Nkind (Renamed_Object (Entity (Exp)))
6830 = N_Explicit_Dereference
6831 then
6832 Tag_Node :=
6833 Make_Explicit_Dereference (Loc,
6834 Prefix =>
6835 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6836 Make_Function_Call (Loc,
6837 Name =>
6838 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6839 Parameter_Associations => New_List (
6840 Unchecked_Convert_To (RTE (RE_Address),
6841 Duplicate_Subexpr
6842 (Prefix
6843 (Renamed_Object (Entity (Exp)))))))));
6844
6845 -- Common case: obtain the address of the actual object and
6846 -- displace the pointer to reference the base of the object.
6847
6848 else
6849 Tag_Node :=
6850 Make_Explicit_Dereference (Loc,
6851 Prefix =>
6852 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6853 Make_Function_Call (Loc,
6854 Name =>
6855 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6856 Parameter_Associations => New_List (
6857 Make_Attribute_Reference (Loc,
6858 Prefix => Duplicate_Subexpr (Exp),
6859 Attribute_Name => Name_Address)))));
6860 end if;
6861 else
6862 Tag_Node :=
6863 Make_Attribute_Reference (Loc,
6864 Prefix => Duplicate_Subexpr (Exp),
6865 Attribute_Name => Name_Tag);
6866 end if;
6867
6868 -- CodePeer does not do anything useful with
6869 -- Ada.Tags.Type_Specific_Data components.
6870
6871 if not CodePeer_Mode then
6872 Insert_Action (Exp,
6873 Make_Raise_Program_Error (Loc,
6874 Condition =>
6875 Make_Op_Gt (Loc,
6876 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6877 Right_Opnd =>
6878 Make_Integer_Literal (Loc,
6879 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6880 Reason => PE_Accessibility_Check_Failed));
6881 end if;
6882 end;
6883
6884 -- AI05-0073: If function has a controlling access result, check that
6885 -- the tag of the return value, if it is not null, matches designated
6886 -- type of return type.
6887
6888 -- The return expression is referenced twice in the code below, so it
6889 -- must be made free of side effects. Given that different compilers
6890 -- may evaluate these parameters in different order, both occurrences
6891 -- perform a copy.
6892
6893 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6894 and then Has_Controlling_Result (Scope_Id)
6895 then
6896 Insert_Action (N,
6897 Make_Raise_Constraint_Error (Loc,
6898 Condition =>
6899 Make_And_Then (Loc,
6900 Left_Opnd =>
6901 Make_Op_Ne (Loc,
6902 Left_Opnd => Duplicate_Subexpr (Exp),
6903 Right_Opnd => Make_Null (Loc)),
6904
6905 Right_Opnd => Make_Op_Ne (Loc,
6906 Left_Opnd =>
6907 Make_Selected_Component (Loc,
6908 Prefix => Duplicate_Subexpr (Exp),
6909 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6910
6911 Right_Opnd =>
6912 Make_Attribute_Reference (Loc,
6913 Prefix =>
6914 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6915 Attribute_Name => Name_Tag))),
6916
6917 Reason => CE_Tag_Check_Failed),
6918 Suppress => All_Checks);
6919 end if;
6920
6921 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6922 -- ensure that the function result does not outlive an
6923 -- object designated by one of it discriminants.
6924
6925 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6926 and then Has_Unconstrained_Access_Discriminants (R_Type)
6927 then
6928 declare
6929 Discrim_Source : Node_Id;
6930
6931 procedure Check_Against_Result_Level (Level : Node_Id);
6932 -- Check the given accessibility level against the level
6933 -- determined by the point of call. (AI05-0234).
6934
6935 --------------------------------
6936 -- Check_Against_Result_Level --
6937 --------------------------------
6938
6939 procedure Check_Against_Result_Level (Level : Node_Id) is
6940 begin
6941 Insert_Action (N,
6942 Make_Raise_Program_Error (Loc,
6943 Condition =>
6944 Make_Op_Gt (Loc,
6945 Left_Opnd => Level,
6946 Right_Opnd =>
6947 New_Occurrence_Of
6948 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6949 Reason => PE_Accessibility_Check_Failed));
6950 end Check_Against_Result_Level;
6951
6952 begin
6953 Discrim_Source := Exp;
6954 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6955 Discrim_Source := Expression (Discrim_Source);
6956 end loop;
6957
6958 if Nkind (Discrim_Source) = N_Identifier
6959 and then Is_Return_Object (Entity (Discrim_Source))
6960 then
6961 Discrim_Source := Entity (Discrim_Source);
6962
6963 if Is_Constrained (Etype (Discrim_Source)) then
6964 Discrim_Source := Etype (Discrim_Source);
6965 else
6966 Discrim_Source := Expression (Parent (Discrim_Source));
6967 end if;
6968
6969 elsif Nkind (Discrim_Source) = N_Identifier
6970 and then Nkind_In (Original_Node (Discrim_Source),
6971 N_Aggregate, N_Extension_Aggregate)
6972 then
6973 Discrim_Source := Original_Node (Discrim_Source);
6974
6975 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6976 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6977 then
6978 Discrim_Source := Original_Node (Discrim_Source);
6979 end if;
6980
6981 Discrim_Source := Unqual_Conv (Discrim_Source);
6982
6983 case Nkind (Discrim_Source) is
6984 when N_Defining_Identifier =>
6985 pragma Assert (Is_Composite_Type (Discrim_Source)
6986 and then Has_Discriminants (Discrim_Source)
6987 and then Is_Constrained (Discrim_Source));
6988
6989 declare
6990 Discrim : Entity_Id :=
6991 First_Discriminant (Base_Type (R_Type));
6992 Disc_Elmt : Elmt_Id :=
6993 First_Elmt (Discriminant_Constraint
6994 (Discrim_Source));
6995 begin
6996 loop
6997 if Ekind (Etype (Discrim)) =
6998 E_Anonymous_Access_Type
6999 then
7000 Check_Against_Result_Level
7001 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7002 end if;
7003
7004 Next_Elmt (Disc_Elmt);
7005 Next_Discriminant (Discrim);
7006 exit when not Present (Discrim);
7007 end loop;
7008 end;
7009
7010 when N_Aggregate
7011 | N_Extension_Aggregate
7012 =>
7013 -- Unimplemented: extension aggregate case where discrims
7014 -- come from ancestor part, not extension part.
7015
7016 declare
7017 Discrim : Entity_Id :=
7018 First_Discriminant (Base_Type (R_Type));
7019
7020 Disc_Exp : Node_Id := Empty;
7021
7022 Positionals_Exhausted
7023 : Boolean := not Present (Expressions
7024 (Discrim_Source));
7025
7026 function Associated_Expr
7027 (Comp_Id : Entity_Id;
7028 Associations : List_Id) return Node_Id;
7029
7030 -- Given a component and a component associations list,
7031 -- locate the expression for that component; returns
7032 -- Empty if no such expression is found.
7033
7034 ---------------------
7035 -- Associated_Expr --
7036 ---------------------
7037
7038 function Associated_Expr
7039 (Comp_Id : Entity_Id;
7040 Associations : List_Id) return Node_Id
7041 is
7042 Assoc : Node_Id;
7043 Choice : Node_Id;
7044
7045 begin
7046 -- Simple linear search seems ok here
7047
7048 Assoc := First (Associations);
7049 while Present (Assoc) loop
7050 Choice := First (Choices (Assoc));
7051 while Present (Choice) loop
7052 if (Nkind (Choice) = N_Identifier
7053 and then Chars (Choice) = Chars (Comp_Id))
7054 or else (Nkind (Choice) = N_Others_Choice)
7055 then
7056 return Expression (Assoc);
7057 end if;
7058
7059 Next (Choice);
7060 end loop;
7061
7062 Next (Assoc);
7063 end loop;
7064
7065 return Empty;
7066 end Associated_Expr;
7067
7068 -- Start of processing for Expand_Simple_Function_Return
7069
7070 begin
7071 if not Positionals_Exhausted then
7072 Disc_Exp := First (Expressions (Discrim_Source));
7073 end if;
7074
7075 loop
7076 if Positionals_Exhausted then
7077 Disc_Exp :=
7078 Associated_Expr
7079 (Discrim,
7080 Component_Associations (Discrim_Source));
7081 end if;
7082
7083 if Ekind (Etype (Discrim)) =
7084 E_Anonymous_Access_Type
7085 then
7086 Check_Against_Result_Level
7087 (Dynamic_Accessibility_Level (Disc_Exp));
7088 end if;
7089
7090 Next_Discriminant (Discrim);
7091 exit when not Present (Discrim);
7092
7093 if not Positionals_Exhausted then
7094 Next (Disc_Exp);
7095 Positionals_Exhausted := not Present (Disc_Exp);
7096 end if;
7097 end loop;
7098 end;
7099
7100 when N_Function_Call =>
7101
7102 -- No check needed (check performed by callee)
7103
7104 null;
7105
7106 when others =>
7107 declare
7108 Level : constant Node_Id :=
7109 Make_Integer_Literal (Loc,
7110 Object_Access_Level (Discrim_Source));
7111
7112 begin
7113 -- Unimplemented: check for name prefix that includes
7114 -- a dereference of an access value with a dynamic
7115 -- accessibility level (e.g., an access param or a
7116 -- saooaaat) and use dynamic level in that case. For
7117 -- example:
7118 -- return Access_Param.all(Some_Index).Some_Component;
7119 -- ???
7120
7121 Set_Etype (Level, Standard_Natural);
7122 Check_Against_Result_Level (Level);
7123 end;
7124 end case;
7125 end;
7126 end if;
7127
7128 -- If we are returning an object that may not be bit-aligned, then copy
7129 -- the value into a temporary first. This copy may need to expand to a
7130 -- loop of component operations.
7131
7132 if Is_Possibly_Unaligned_Slice (Exp)
7133 or else Is_Possibly_Unaligned_Object (Exp)
7134 then
7135 declare
7136 ExpR : constant Node_Id := Relocate_Node (Exp);
7137 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7138 begin
7139 Insert_Action (Exp,
7140 Make_Object_Declaration (Loc,
7141 Defining_Identifier => Tnn,
7142 Constant_Present => True,
7143 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7144 Expression => ExpR),
7145 Suppress => All_Checks);
7146 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7147 end;
7148 end if;
7149
7150 -- Call the _Postconditions procedure if the related function has
7151 -- contract assertions that need to be verified on exit.
7152
7153 if Ekind (Scope_Id) = E_Function
7154 and then Present (Postconditions_Proc (Scope_Id))
7155 then
7156 -- In the case of discriminated objects, we have created a
7157 -- constrained subtype above, and used the underlying type. This
7158 -- transformation is post-analysis and harmless, except that now the
7159 -- call to the post-condition will be analyzed and the type kinds
7160 -- have to match.
7161
7162 if Nkind (Exp) = N_Unchecked_Type_Conversion
7163 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7164 then
7165 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7166 end if;
7167
7168 -- We are going to reference the returned value twice in this case,
7169 -- once in the call to _Postconditions, and once in the actual return
7170 -- statement, but we can't have side effects happening twice.
7171
7172 Force_Evaluation (Exp, Mode => Strict);
7173
7174 -- Generate call to _Postconditions
7175
7176 Insert_Action (Exp,
7177 Make_Procedure_Call_Statement (Loc,
7178 Name =>
7179 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
7180 Parameter_Associations => New_List (New_Copy_Tree (Exp))));
7181 end if;
7182
7183 -- Ada 2005 (AI-251): If this return statement corresponds with an
7184 -- simple return statement associated with an extended return statement
7185 -- and the type of the returned object is an interface then generate an
7186 -- implicit conversion to force displacement of the "this" pointer.
7187
7188 if Ada_Version >= Ada_2005
7189 and then Comes_From_Extended_Return_Statement (N)
7190 and then Nkind (Expression (N)) = N_Identifier
7191 and then Is_Interface (Utyp)
7192 and then Utyp /= Underlying_Type (Exptyp)
7193 then
7194 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7195 Analyze_And_Resolve (Exp);
7196 end if;
7197 end Expand_Simple_Function_Return;
7198
7199 --------------------------------------------
7200 -- Has_Unconstrained_Access_Discriminants --
7201 --------------------------------------------
7202
7203 function Has_Unconstrained_Access_Discriminants
7204 (Subtyp : Entity_Id) return Boolean
7205 is
7206 Discr : Entity_Id;
7207
7208 begin
7209 if Has_Discriminants (Subtyp)
7210 and then not Is_Constrained (Subtyp)
7211 then
7212 Discr := First_Discriminant (Subtyp);
7213 while Present (Discr) loop
7214 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7215 return True;
7216 end if;
7217
7218 Next_Discriminant (Discr);
7219 end loop;
7220 end if;
7221
7222 return False;
7223 end Has_Unconstrained_Access_Discriminants;
7224
7225 -----------------------------------
7226 -- Is_Build_In_Place_Result_Type --
7227 -----------------------------------
7228
7229 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
7230 begin
7231 if not Expander_Active then
7232 return False;
7233 end if;
7234
7235 -- In Ada 2005 all functions with an inherently limited return type
7236 -- must be handled using a build-in-place profile, including the case
7237 -- of a function with a limited interface result, where the function
7238 -- may return objects of nonlimited descendants.
7239
7240 if Is_Limited_View (Typ) then
7241 return Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L;
7242 else
7243 if Has_Interfaces (Typ) then
7244 return False;
7245 end if;
7246
7247 declare
7248 T : Entity_Id := Typ;
7249 begin
7250 -- For T'Class, return True if it's True for T. This is necessary
7251 -- because a class-wide function might say "return F (...)", where
7252 -- F returns the corresponding specific type.
7253
7254 if Is_Class_Wide_Type (Typ) then
7255 T := Etype (Typ);
7256 end if;
7257
7258 -- If this is a generic formal type in an instance, return True if
7259 -- it's True for the generic actual type.
7260
7261 if Nkind (Parent (Typ)) = N_Subtype_Declaration
7262 and then Present (Generic_Parent_Type (Parent (Typ)))
7263 then
7264 T := Entity (Subtype_Indication (Parent (Typ)));
7265
7266 if Present (Full_View (T)) then
7267 T := Full_View (T);
7268 end if;
7269
7270 elsif Present (Underlying_Type (Typ)) then
7271 T := Underlying_Type (Typ);
7272 end if;
7273
7274 declare
7275 Result : Boolean;
7276 begin
7277 -- ???For now, enable build-in-place for a very narrow set of
7278 -- controlled types. Change "if True" to "if False" to
7279 -- experiment more controlled types. Eventually, we would
7280 -- like to enable build-in-place for all tagged types, all
7281 -- types that need finalization, and all caller-unknown-size
7282 -- types. We will eventually use Debug_Flag_Dot_9 to disable
7283 -- build-in-place for nonlimited types.
7284
7285 -- if Debug_Flag_Dot_9 then
7286 if True then
7287 Result := Is_Controlled (T)
7288 and then Present (Enclosing_Subprogram (T))
7289 and then not Is_Compilation_Unit (Enclosing_Subprogram (T))
7290 and then Ekind (Enclosing_Subprogram (T)) = E_Procedure;
7291 else
7292 Result := Is_Controlled (T);
7293 end if;
7294
7295 return Result;
7296 end;
7297 end;
7298 end if;
7299 end Is_Build_In_Place_Result_Type;
7300
7301 --------------------------------
7302 -- Is_Build_In_Place_Function --
7303 --------------------------------
7304
7305 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7306 begin
7307 -- This function is called from Expand_Subtype_From_Expr during
7308 -- semantic analysis, even when expansion is off. In those cases
7309 -- the build_in_place expansion will not take place.
7310
7311 if not Expander_Active then
7312 return False;
7313 end if;
7314
7315 -- For now we test whether E denotes a function or access-to-function
7316 -- type whose result subtype is inherently limited. Later this test
7317 -- may be revised to allow composite nonlimited types. Functions with
7318 -- a foreign convention or whose result type has a foreign convention
7319 -- never qualify.
7320
7321 if Ekind_In (E, E_Function, E_Generic_Function)
7322 or else (Ekind (E) = E_Subprogram_Type
7323 and then Etype (E) /= Standard_Void_Type)
7324 then
7325 -- Note: If the function has a foreign convention, it cannot build
7326 -- its result in place, so you're on your own. On the other hand,
7327 -- if only the return type has a foreign convention, its layout is
7328 -- intended to be compatible with the other language, but the build-
7329 -- in place machinery can ensure that the object is not copied.
7330
7331 return Is_Build_In_Place_Result_Type (Etype (E))
7332 and then not Has_Foreign_Convention (E)
7333 and then not Debug_Flag_Dot_L;
7334
7335 else
7336 return False;
7337 end if;
7338 end Is_Build_In_Place_Function;
7339
7340 -------------------------------------
7341 -- Is_Build_In_Place_Function_Call --
7342 -------------------------------------
7343
7344 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7345 Exp_Node : constant Node_Id := Unqual_Conv (N);
7346 Function_Id : Entity_Id;
7347
7348 begin
7349 -- Return False if the expander is currently inactive, since awareness
7350 -- of build-in-place treatment is only relevant during expansion. Note
7351 -- that Is_Build_In_Place_Function, which is called as part of this
7352 -- function, is also conditioned this way, but we need to check here as
7353 -- well to avoid blowing up on processing protected calls when expansion
7354 -- is disabled (such as with -gnatc) since those would trip over the
7355 -- raise of Program_Error below.
7356
7357 -- In SPARK mode, build-in-place calls are not expanded, so that we
7358 -- may end up with a call that is neither resolved to an entity, nor
7359 -- an indirect call.
7360
7361 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
7362 return False;
7363 end if;
7364
7365 if Is_Entity_Name (Name (Exp_Node)) then
7366 Function_Id := Entity (Name (Exp_Node));
7367
7368 -- In the case of an explicitly dereferenced call, use the subprogram
7369 -- type generated for the dereference.
7370
7371 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7372 Function_Id := Etype (Name (Exp_Node));
7373
7374 -- This may be a call to a protected function.
7375
7376 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7377 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7378
7379 else
7380 raise Program_Error;
7381 end if;
7382
7383 declare
7384 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
7385 -- So we can stop here in the debugger
7386 begin
7387 return Result;
7388 end;
7389 end Is_Build_In_Place_Function_Call;
7390
7391 -----------------------
7392 -- Freeze_Subprogram --
7393 -----------------------
7394
7395 procedure Freeze_Subprogram (N : Node_Id) is
7396 Loc : constant Source_Ptr := Sloc (N);
7397
7398 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7399 -- (Ada 2005): Register a predefined primitive in all the secondary
7400 -- dispatch tables of its primitive type.
7401
7402 ----------------------------------
7403 -- Register_Predefined_DT_Entry --
7404 ----------------------------------
7405
7406 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7407 Iface_DT_Ptr : Elmt_Id;
7408 Tagged_Typ : Entity_Id;
7409 Thunk_Id : Entity_Id;
7410 Thunk_Code : Node_Id;
7411
7412 begin
7413 Tagged_Typ := Find_Dispatching_Type (Prim);
7414
7415 if No (Access_Disp_Table (Tagged_Typ))
7416 or else not Has_Interfaces (Tagged_Typ)
7417 or else not RTE_Available (RE_Interface_Tag)
7418 or else Restriction_Active (No_Dispatching_Calls)
7419 then
7420 return;
7421 end if;
7422
7423 -- Skip the first two access-to-dispatch-table pointers since they
7424 -- leads to the primary dispatch table (predefined DT and user
7425 -- defined DT). We are only concerned with the secondary dispatch
7426 -- table pointers. Note that the access-to- dispatch-table pointer
7427 -- corresponds to the first implemented interface retrieved below.
7428
7429 Iface_DT_Ptr :=
7430 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7431
7432 while Present (Iface_DT_Ptr)
7433 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7434 loop
7435 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7436 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
7437
7438 if Present (Thunk_Code) then
7439 Insert_Actions_After (N, New_List (
7440 Thunk_Code,
7441
7442 Build_Set_Predefined_Prim_Op_Address (Loc,
7443 Tag_Node =>
7444 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7445 Position => DT_Position (Prim),
7446 Address_Node =>
7447 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7448 Make_Attribute_Reference (Loc,
7449 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7450 Attribute_Name => Name_Unrestricted_Access))),
7451
7452 Build_Set_Predefined_Prim_Op_Address (Loc,
7453 Tag_Node =>
7454 New_Occurrence_Of
7455 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7456 Loc),
7457 Position => DT_Position (Prim),
7458 Address_Node =>
7459 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7460 Make_Attribute_Reference (Loc,
7461 Prefix => New_Occurrence_Of (Prim, Loc),
7462 Attribute_Name => Name_Unrestricted_Access)))));
7463 end if;
7464
7465 -- Skip the tag of the predefined primitives dispatch table
7466
7467 Next_Elmt (Iface_DT_Ptr);
7468 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7469
7470 -- Skip tag of the no-thunks dispatch table
7471
7472 Next_Elmt (Iface_DT_Ptr);
7473 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7474
7475 -- Skip tag of predefined primitives no-thunks dispatch table
7476
7477 Next_Elmt (Iface_DT_Ptr);
7478 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7479
7480 Next_Elmt (Iface_DT_Ptr);
7481 end loop;
7482 end Register_Predefined_DT_Entry;
7483
7484 -- Local variables
7485
7486 Subp : constant Entity_Id := Entity (N);
7487
7488 -- Start of processing for Freeze_Subprogram
7489
7490 begin
7491 -- We suppress the initialization of the dispatch table entry when
7492 -- not Tagged_Type_Expansion because the dispatching mechanism is
7493 -- handled internally by the target.
7494
7495 if Is_Dispatching_Operation (Subp)
7496 and then not Is_Abstract_Subprogram (Subp)
7497 and then Present (DTC_Entity (Subp))
7498 and then Present (Scope (DTC_Entity (Subp)))
7499 and then Tagged_Type_Expansion
7500 and then not Restriction_Active (No_Dispatching_Calls)
7501 and then RTE_Available (RE_Tag)
7502 then
7503 declare
7504 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7505
7506 begin
7507 -- Handle private overridden primitives
7508
7509 if not Is_CPP_Class (Typ) then
7510 Check_Overriding_Operation (Subp);
7511 end if;
7512
7513 -- We assume that imported CPP primitives correspond with objects
7514 -- whose constructor is in the CPP side; therefore we don't need
7515 -- to generate code to register them in the dispatch table.
7516
7517 if Is_CPP_Class (Typ) then
7518 null;
7519
7520 -- Handle CPP primitives found in derivations of CPP_Class types.
7521 -- These primitives must have been inherited from some parent, and
7522 -- there is no need to register them in the dispatch table because
7523 -- Build_Inherit_Prims takes care of initializing these slots.
7524
7525 elsif Is_Imported (Subp)
7526 and then (Convention (Subp) = Convention_CPP
7527 or else Convention (Subp) = Convention_C)
7528 then
7529 null;
7530
7531 -- Generate code to register the primitive in non statically
7532 -- allocated dispatch tables
7533
7534 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7535
7536 -- When a primitive is frozen, enter its name in its dispatch
7537 -- table slot.
7538
7539 if not Is_Interface (Typ)
7540 or else Present (Interface_Alias (Subp))
7541 then
7542 if Is_Predefined_Dispatching_Operation (Subp) then
7543 Register_Predefined_DT_Entry (Subp);
7544 end if;
7545
7546 Insert_Actions_After (N,
7547 Register_Primitive (Loc, Prim => Subp));
7548 end if;
7549 end if;
7550 end;
7551 end if;
7552
7553 -- Mark functions that return by reference. Note that it cannot be part
7554 -- of the normal semantic analysis of the spec since the underlying
7555 -- returned type may not be known yet (for private types).
7556
7557 declare
7558 Typ : constant Entity_Id := Etype (Subp);
7559 Utyp : constant Entity_Id := Underlying_Type (Typ);
7560
7561 begin
7562 if Is_Limited_View (Typ) then
7563 Set_Returns_By_Ref (Subp);
7564
7565 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7566 Set_Returns_By_Ref (Subp);
7567 end if;
7568 end;
7569
7570 -- Wnen freezing a null procedure, analyze its delayed aspects now
7571 -- because we may not have reached the end of the declarative list when
7572 -- delayed aspects are normally analyzed. This ensures that dispatching
7573 -- calls are properly rewritten when the generated _Postcondition
7574 -- procedure is analyzed in the null procedure body.
7575
7576 if Nkind (Parent (Subp)) = N_Procedure_Specification
7577 and then Null_Present (Parent (Subp))
7578 then
7579 Analyze_Entry_Or_Subprogram_Contract (Subp);
7580 end if;
7581 end Freeze_Subprogram;
7582
7583 ------------------------------
7584 -- Insert_Post_Call_Actions --
7585 ------------------------------
7586
7587 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7588 Context : constant Node_Id := Parent (N);
7589
7590 begin
7591 if Is_Empty_List (Post_Call) then
7592 return;
7593 end if;
7594
7595 -- Cases where the call is not a member of a statement list. This
7596 -- includes the case where the call is an actual in another function
7597 -- call or indexing, i.e. an expression context as well.
7598
7599 if not Is_List_Member (N)
7600 or else Nkind_In (Context, N_Function_Call, N_Indexed_Component)
7601 then
7602 -- In Ada 2012 the call may be a function call in an expression
7603 -- (since OUT and IN OUT parameters are now allowed for such calls).
7604 -- The write-back of (in)-out parameters is handled by the back-end,
7605 -- but the constraint checks generated when subtypes of formal and
7606 -- actual don't match must be inserted in the form of assignments.
7607
7608 if Nkind (Original_Node (N)) = N_Function_Call then
7609 pragma Assert (Ada_Version >= Ada_2012);
7610 -- Functions with '[in] out' parameters are only allowed in Ada
7611 -- 2012.
7612
7613 -- We used to handle this by climbing up parents to a
7614 -- non-statement/declaration and then simply making a call to
7615 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7616 -- for Ada 2012. If we are in the middle of an expression, e.g.
7617 -- the condition of an IF, this call would insert after the IF
7618 -- statement, which is much too late to be doing the write back.
7619 -- For example:
7620
7621 -- if Clobber (X) then
7622 -- Put_Line (X'Img);
7623 -- else
7624 -- goto Junk
7625 -- end if;
7626
7627 -- Now assume Clobber changes X, if we put the write back after
7628 -- the IF, the Put_Line gets the wrong value and the goto causes
7629 -- the write back to be skipped completely.
7630
7631 -- To deal with this, we replace the call by
7632
7633 -- do
7634 -- Tnnn : constant function-result-type := function-call;
7635 -- Post_Call actions
7636 -- in
7637 -- Tnnn;
7638 -- end;
7639
7640 declare
7641 Loc : constant Source_Ptr := Sloc (N);
7642 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7643 FRTyp : constant Entity_Id := Etype (N);
7644 Name : constant Node_Id := Relocate_Node (N);
7645
7646 begin
7647 Prepend_To (Post_Call,
7648 Make_Object_Declaration (Loc,
7649 Defining_Identifier => Tnnn,
7650 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
7651 Constant_Present => True,
7652 Expression => Name));
7653
7654 Rewrite (N,
7655 Make_Expression_With_Actions (Loc,
7656 Actions => Post_Call,
7657 Expression => New_Occurrence_Of (Tnnn, Loc)));
7658
7659 -- We don't want to just blindly call Analyze_And_Resolve
7660 -- because that would cause unwanted recursion on the call.
7661 -- So for a moment set the call as analyzed to prevent that
7662 -- recursion, and get the rest analyzed properly, then reset
7663 -- the analyzed flag, so our caller can continue.
7664
7665 Set_Analyzed (Name, True);
7666 Analyze_And_Resolve (N, FRTyp);
7667 Set_Analyzed (Name, False);
7668 end;
7669
7670 -- If not the special Ada 2012 case of a function call, then we must
7671 -- have the triggering statement of a triggering alternative or an
7672 -- entry call alternative, and we can add the post call stuff to the
7673 -- corresponding statement list.
7674
7675 else
7676 pragma Assert (Nkind_In (Context, N_Entry_Call_Alternative,
7677 N_Triggering_Alternative));
7678
7679 if Is_Non_Empty_List (Statements (Context)) then
7680 Insert_List_Before_And_Analyze
7681 (First (Statements (Context)), Post_Call);
7682 else
7683 Set_Statements (Context, Post_Call);
7684 end if;
7685 end if;
7686
7687 -- A procedure call is always part of a declarative or statement list,
7688 -- however a function call may appear nested within a construct. Most
7689 -- cases of function call nesting are handled in the special case above.
7690 -- The only exception is when the function call acts as an actual in a
7691 -- procedure call. In this case the function call is in a list, but the
7692 -- post-call actions must be inserted after the procedure call.
7693
7694 elsif Nkind (Context) = N_Procedure_Call_Statement then
7695 Insert_Actions_After (Context, Post_Call);
7696
7697 -- Otherwise, normal case where N is in a statement sequence, just put
7698 -- the post-call stuff after the call statement.
7699
7700 else
7701 Insert_Actions_After (N, Post_Call);
7702 end if;
7703 end Insert_Post_Call_Actions;
7704
7705 -----------------------
7706 -- Is_Null_Procedure --
7707 -----------------------
7708
7709 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
7710 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
7711
7712 begin
7713 if Ekind (Subp) /= E_Procedure then
7714 return False;
7715
7716 -- Check if this is a declared null procedure
7717
7718 elsif Nkind (Decl) = N_Subprogram_Declaration then
7719 if not Null_Present (Specification (Decl)) then
7720 return False;
7721
7722 elsif No (Body_To_Inline (Decl)) then
7723 return False;
7724
7725 -- Check if the body contains only a null statement, followed by
7726 -- the return statement added during expansion.
7727
7728 else
7729 declare
7730 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
7731
7732 Stat : Node_Id;
7733 Stat2 : Node_Id;
7734
7735 begin
7736 if Nkind (Orig_Bod) /= N_Subprogram_Body then
7737 return False;
7738 else
7739 -- We must skip SCIL nodes because they are currently
7740 -- implemented as special N_Null_Statement nodes.
7741
7742 Stat :=
7743 First_Non_SCIL_Node
7744 (Statements (Handled_Statement_Sequence (Orig_Bod)));
7745 Stat2 := Next_Non_SCIL_Node (Stat);
7746
7747 return
7748 Is_Empty_List (Declarations (Orig_Bod))
7749 and then Nkind (Stat) = N_Null_Statement
7750 and then
7751 (No (Stat2)
7752 or else
7753 (Nkind (Stat2) = N_Simple_Return_Statement
7754 and then No (Next (Stat2))));
7755 end if;
7756 end;
7757 end if;
7758
7759 else
7760 return False;
7761 end if;
7762 end Is_Null_Procedure;
7763
7764 -------------------------------------------
7765 -- Make_Build_In_Place_Call_In_Allocator --
7766 -------------------------------------------
7767
7768 procedure Make_Build_In_Place_Call_In_Allocator
7769 (Allocator : Node_Id;
7770 Function_Call : Node_Id)
7771 is
7772 Acc_Type : constant Entity_Id := Etype (Allocator);
7773 Loc : constant Source_Ptr := Sloc (Function_Call);
7774 Func_Call : Node_Id := Function_Call;
7775 Ref_Func_Call : Node_Id;
7776 Function_Id : Entity_Id;
7777 Result_Subt : Entity_Id;
7778 New_Allocator : Node_Id;
7779 Return_Obj_Access : Entity_Id; -- temp for function result
7780 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
7781 Alloc_Form : BIP_Allocation_Form;
7782 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
7783 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
7784 Chain : Entity_Id; -- activation chain, in case of tasks
7785
7786 begin
7787 -- Step past qualification or unchecked conversion (the latter can occur
7788 -- in cases of calls to 'Input).
7789
7790 if Nkind_In (Func_Call,
7791 N_Qualified_Expression,
7792 N_Type_Conversion,
7793 N_Unchecked_Type_Conversion)
7794 then
7795 Func_Call := Expression (Func_Call);
7796 end if;
7797
7798 -- Mark the call as processed as a build-in-place call
7799
7800 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
7801 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
7802
7803 if Is_Entity_Name (Name (Func_Call)) then
7804 Function_Id := Entity (Name (Func_Call));
7805
7806 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
7807 Function_Id := Etype (Name (Func_Call));
7808
7809 else
7810 raise Program_Error;
7811 end if;
7812
7813 Result_Subt := Available_View (Etype (Function_Id));
7814
7815 -- Create a temp for the function result. In the caller-allocates case,
7816 -- this will be initialized to the result of a new uninitialized
7817 -- allocator. Note: we do not use Allocator as the Related_Node of
7818 -- Return_Obj_Access in call to Make_Temporary below as this would
7819 -- create a sort of infinite "recursion".
7820
7821 Return_Obj_Access := Make_Temporary (Loc, 'R');
7822 Set_Etype (Return_Obj_Access, Acc_Type);
7823 Set_Can_Never_Be_Null (Acc_Type, False);
7824 -- It gets initialized to null, so we can't have that
7825
7826 -- When the result subtype is constrained, the return object is
7827 -- allocated on the caller side, and access to it is passed to the
7828 -- function.
7829
7830 -- Here and in related routines, we must examine the full view of the
7831 -- type, because the view at the point of call may differ from that
7832 -- that in the function body, and the expansion mechanism depends on
7833 -- the characteristics of the full view.
7834
7835 if Is_Constrained (Underlying_Type (Result_Subt)) then
7836 -- Replace the initialized allocator of form "new T'(Func (...))"
7837 -- with an uninitialized allocator of form "new T", where T is the
7838 -- result subtype of the called function. The call to the function
7839 -- is handled separately further below.
7840
7841 New_Allocator :=
7842 Make_Allocator (Loc,
7843 Expression => New_Occurrence_Of (Result_Subt, Loc));
7844 Set_No_Initialization (New_Allocator);
7845
7846 -- Copy attributes to new allocator. Note that the new allocator
7847 -- logically comes from source if the original one did, so copy the
7848 -- relevant flag. This ensures proper treatment of the restriction
7849 -- No_Implicit_Heap_Allocations in this case.
7850
7851 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
7852 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
7853 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
7854
7855 Rewrite (Allocator, New_Allocator);
7856
7857 -- Initial value of the temp is the result of the uninitialized
7858 -- allocator. Unchecked_Convert is needed for T'Input where T is
7859 -- derived from a controlled type.
7860
7861 Temp_Init := Relocate_Node (Allocator);
7862
7863 if Nkind_In
7864 (Function_Call, N_Type_Conversion, N_Unchecked_Type_Conversion)
7865 then
7866 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
7867 end if;
7868
7869 -- Indicate that caller allocates, and pass in the return object
7870
7871 Alloc_Form := Caller_Allocation;
7872 Pool := Make_Null (No_Location);
7873 Return_Obj_Actual :=
7874 Make_Unchecked_Type_Conversion (Loc,
7875 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
7876 Expression =>
7877 Make_Explicit_Dereference (Loc,
7878 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
7879
7880 -- When the result subtype is unconstrained, the function itself must
7881 -- perform the allocation of the return object, so we pass parameters
7882 -- indicating that.
7883
7884 else
7885 Temp_Init := Empty;
7886
7887 -- Case of a user-defined storage pool. Pass an allocation parameter
7888 -- indicating that the function should allocate its result in the
7889 -- pool, and pass the pool. Use 'Unrestricted_Access because the
7890 -- pool may not be aliased.
7891
7892 if Present (Associated_Storage_Pool (Acc_Type)) then
7893 Alloc_Form := User_Storage_Pool;
7894 Pool :=
7895 Make_Attribute_Reference (Loc,
7896 Prefix =>
7897 New_Occurrence_Of
7898 (Associated_Storage_Pool (Acc_Type), Loc),
7899 Attribute_Name => Name_Unrestricted_Access);
7900
7901 -- No user-defined pool; pass an allocation parameter indicating that
7902 -- the function should allocate its result on the heap.
7903
7904 else
7905 Alloc_Form := Global_Heap;
7906 Pool := Make_Null (No_Location);
7907 end if;
7908
7909 -- The caller does not provide the return object in this case, so we
7910 -- have to pass null for the object access actual.
7911
7912 Return_Obj_Actual := Empty;
7913 end if;
7914
7915 -- Declare the temp object
7916
7917 Insert_Action (Allocator,
7918 Make_Object_Declaration (Loc,
7919 Defining_Identifier => Return_Obj_Access,
7920 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
7921 Expression => Temp_Init));
7922
7923 Ref_Func_Call := Make_Reference (Loc, Func_Call);
7924
7925 -- Ada 2005 (AI-251): If the type of the allocator is an interface
7926 -- then generate an implicit conversion to force displacement of the
7927 -- "this" pointer.
7928
7929 if Is_Interface (Designated_Type (Acc_Type)) then
7930 Rewrite
7931 (Ref_Func_Call,
7932 OK_Convert_To (Acc_Type, Ref_Func_Call));
7933
7934 -- If the types are incompatible, we need an unchecked conversion. Note
7935 -- that the full types will be compatible, but the types not visibly
7936 -- compatible.
7937
7938 elsif Nkind_In
7939 (Function_Call, N_Type_Conversion, N_Unchecked_Type_Conversion)
7940 then
7941 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
7942 end if;
7943
7944 declare
7945 Assign : constant Node_Id :=
7946 Make_Assignment_Statement (Loc,
7947 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
7948 Expression => Ref_Func_Call);
7949 -- Assign the result of the function call into the temp. In the
7950 -- caller-allocates case, this is overwriting the temp with its
7951 -- initial value, which has no effect. In the callee-allocates case,
7952 -- this is setting the temp to point to the object allocated by the
7953 -- callee. Unchecked_Convert is needed for T'Input where T is derived
7954 -- from a controlled type.
7955
7956 Actions : List_Id;
7957 -- Actions to be inserted. If there are no tasks, this is just the
7958 -- assignment statement. If the allocated object has tasks, we need
7959 -- to wrap the assignment in a block that activates them. The
7960 -- activation chain of that block must be passed to the function,
7961 -- rather than some outer chain.
7962 begin
7963 if Has_Task (Result_Subt) then
7964 Actions := New_List;
7965 Build_Task_Allocate_Block_With_Init_Stmts
7966 (Actions, Allocator, Init_Stmts => New_List (Assign));
7967 Chain := Activation_Chain_Entity (Last (Actions));
7968 else
7969 Actions := New_List (Assign);
7970 Chain := Empty;
7971 end if;
7972
7973 Insert_Actions (Allocator, Actions);
7974 end;
7975
7976 -- When the function has a controlling result, an allocation-form
7977 -- parameter must be passed indicating that the caller is allocating
7978 -- the result object. This is needed because such a function can be
7979 -- called as a dispatching operation and must be treated similarly
7980 -- to functions with unconstrained result subtypes.
7981
7982 Add_Unconstrained_Actuals_To_Build_In_Place_Call
7983 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
7984
7985 Add_Finalization_Master_Actual_To_Build_In_Place_Call
7986 (Func_Call, Function_Id, Acc_Type);
7987
7988 Add_Task_Actuals_To_Build_In_Place_Call
7989 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
7990 Chain => Chain);
7991
7992 -- Add an implicit actual to the function call that provides access
7993 -- to the allocated object. An unchecked conversion to the (specific)
7994 -- result subtype of the function is inserted to handle cases where
7995 -- the access type of the allocator has a class-wide designated type.
7996
7997 Add_Access_Actual_To_Build_In_Place_Call
7998 (Func_Call, Function_Id, Return_Obj_Actual);
7999
8000 -- Finally, replace the allocator node with a reference to the temp
8001
8002 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8003
8004 Analyze_And_Resolve (Allocator, Acc_Type);
8005 end Make_Build_In_Place_Call_In_Allocator;
8006
8007 ---------------------------------------------------
8008 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8009 ---------------------------------------------------
8010
8011 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8012 (Function_Call : Node_Id)
8013 is
8014 Loc : constant Source_Ptr := Sloc (Function_Call);
8015 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8016 Function_Id : Entity_Id;
8017 Result_Subt : Entity_Id;
8018 Return_Obj_Id : Entity_Id;
8019 Return_Obj_Decl : Entity_Id;
8020
8021 begin
8022 -- If the call has already been processed to add build-in-place actuals
8023 -- then return. One place this can occur is for calls to build-in-place
8024 -- functions that occur within a call to a protected operation, where
8025 -- due to rewriting and expansion of the protected call there can be
8026 -- more than one call to Expand_Actuals for the same set of actuals.
8027
8028 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8029 return;
8030 end if;
8031
8032 -- Mark the call as processed as a build-in-place call
8033
8034 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8035
8036 if Is_Entity_Name (Name (Func_Call)) then
8037 Function_Id := Entity (Name (Func_Call));
8038
8039 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8040 Function_Id := Etype (Name (Func_Call));
8041
8042 else
8043 raise Program_Error;
8044 end if;
8045
8046 Result_Subt := Etype (Function_Id);
8047
8048 -- If the build-in-place function returns a controlled object, then the
8049 -- object needs to be finalized immediately after the context. Since
8050 -- this case produces a transient scope, the servicing finalizer needs
8051 -- to name the returned object. Create a temporary which is initialized
8052 -- with the function call:
8053 --
8054 -- Temp_Id : Func_Type := BIP_Func_Call;
8055 --
8056 -- The initialization expression of the temporary will be rewritten by
8057 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8058 -- Call_In_Object_Declaration.
8059
8060 if Needs_Finalization (Result_Subt) then
8061 declare
8062 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8063 Temp_Decl : Node_Id;
8064
8065 begin
8066 -- Reset the guard on the function call since the following does
8067 -- not perform actual call expansion.
8068
8069 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8070
8071 Temp_Decl :=
8072 Make_Object_Declaration (Loc,
8073 Defining_Identifier => Temp_Id,
8074 Object_Definition =>
8075 New_Occurrence_Of (Result_Subt, Loc),
8076 Expression =>
8077 New_Copy_Tree (Function_Call));
8078
8079 Insert_Action (Function_Call, Temp_Decl);
8080
8081 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8082 Analyze (Function_Call);
8083 end;
8084
8085 -- When the result subtype is definite, an object of the subtype is
8086 -- declared and an access value designating it is passed as an actual.
8087
8088 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8089
8090 -- Create a temporary object to hold the function result
8091
8092 Return_Obj_Id := Make_Temporary (Loc, 'R');
8093 Set_Etype (Return_Obj_Id, Result_Subt);
8094
8095 Return_Obj_Decl :=
8096 Make_Object_Declaration (Loc,
8097 Defining_Identifier => Return_Obj_Id,
8098 Aliased_Present => True,
8099 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8100
8101 Set_No_Initialization (Return_Obj_Decl);
8102
8103 Insert_Action (Func_Call, Return_Obj_Decl);
8104
8105 -- When the function has a controlling result, an allocation-form
8106 -- parameter must be passed indicating that the caller is allocating
8107 -- the result object. This is needed because such a function can be
8108 -- called as a dispatching operation and must be treated similarly
8109 -- to functions with unconstrained result subtypes.
8110
8111 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8112 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8113
8114 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8115 (Func_Call, Function_Id);
8116
8117 Add_Task_Actuals_To_Build_In_Place_Call
8118 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8119
8120 -- Add an implicit actual to the function call that provides access
8121 -- to the caller's return object.
8122
8123 Add_Access_Actual_To_Build_In_Place_Call
8124 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8125
8126 -- When the result subtype is unconstrained, the function must allocate
8127 -- the return object in the secondary stack, so appropriate implicit
8128 -- parameters are added to the call to indicate that. A transient
8129 -- scope is established to ensure eventual cleanup of the result.
8130
8131 else
8132 -- Pass an allocation parameter indicating that the function should
8133 -- allocate its result on the secondary stack.
8134
8135 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8136 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8137
8138 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8139 (Func_Call, Function_Id);
8140
8141 Add_Task_Actuals_To_Build_In_Place_Call
8142 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8143
8144 -- Pass a null value to the function since no return object is
8145 -- available on the caller side.
8146
8147 Add_Access_Actual_To_Build_In_Place_Call
8148 (Func_Call, Function_Id, Empty);
8149 end if;
8150 end Make_Build_In_Place_Call_In_Anonymous_Context;
8151
8152 --------------------------------------------
8153 -- Make_Build_In_Place_Call_In_Assignment --
8154 --------------------------------------------
8155
8156 procedure Make_Build_In_Place_Call_In_Assignment
8157 (Assign : Node_Id;
8158 Function_Call : Node_Id)
8159 is
8160 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8161 Lhs : constant Node_Id := Name (Assign);
8162 Loc : constant Source_Ptr := Sloc (Function_Call);
8163 Func_Id : Entity_Id;
8164 Obj_Decl : Node_Id;
8165 Obj_Id : Entity_Id;
8166 Ptr_Typ : Entity_Id;
8167 Ptr_Typ_Decl : Node_Id;
8168 New_Expr : Node_Id;
8169 Result_Subt : Entity_Id;
8170
8171 begin
8172 -- Mark the call as processed as a build-in-place call
8173
8174 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8175 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8176
8177 if Is_Entity_Name (Name (Func_Call)) then
8178 Func_Id := Entity (Name (Func_Call));
8179
8180 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8181 Func_Id := Etype (Name (Func_Call));
8182
8183 else
8184 raise Program_Error;
8185 end if;
8186
8187 Result_Subt := Etype (Func_Id);
8188
8189 -- When the result subtype is unconstrained, an additional actual must
8190 -- be passed to indicate that the caller is providing the return object.
8191 -- This parameter must also be passed when the called function has a
8192 -- controlling result, because dispatching calls to the function needs
8193 -- to be treated effectively the same as calls to class-wide functions.
8194
8195 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8196 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8197
8198 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8199 (Func_Call, Func_Id);
8200
8201 Add_Task_Actuals_To_Build_In_Place_Call
8202 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8203
8204 -- Add an implicit actual to the function call that provides access to
8205 -- the caller's return object.
8206
8207 Add_Access_Actual_To_Build_In_Place_Call
8208 (Func_Call,
8209 Func_Id,
8210 Make_Unchecked_Type_Conversion (Loc,
8211 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8212 Expression => Relocate_Node (Lhs)));
8213
8214 -- Create an access type designating the function's result subtype
8215
8216 Ptr_Typ := Make_Temporary (Loc, 'A');
8217
8218 Ptr_Typ_Decl :=
8219 Make_Full_Type_Declaration (Loc,
8220 Defining_Identifier => Ptr_Typ,
8221 Type_Definition =>
8222 Make_Access_To_Object_Definition (Loc,
8223 All_Present => True,
8224 Subtype_Indication =>
8225 New_Occurrence_Of (Result_Subt, Loc)));
8226 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8227
8228 -- Finally, create an access object initialized to a reference to the
8229 -- function call. We know this access value is non-null, so mark the
8230 -- entity accordingly to suppress junk access checks.
8231
8232 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8233
8234 -- Add a conversion if it's the wrong type
8235
8236 if Etype (New_Expr) /= Ptr_Typ then
8237 New_Expr :=
8238 Make_Unchecked_Type_Conversion (Loc,
8239 New_Occurrence_Of (Ptr_Typ, Loc), New_Expr);
8240 end if;
8241
8242 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8243 Set_Etype (Obj_Id, Ptr_Typ);
8244 Set_Is_Known_Non_Null (Obj_Id);
8245
8246 Obj_Decl :=
8247 Make_Object_Declaration (Loc,
8248 Defining_Identifier => Obj_Id,
8249 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8250 Expression => New_Expr);
8251 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8252
8253 Rewrite (Assign, Make_Null_Statement (Loc));
8254 end Make_Build_In_Place_Call_In_Assignment;
8255
8256 ----------------------------------------------------
8257 -- Make_Build_In_Place_Call_In_Object_Declaration --
8258 ----------------------------------------------------
8259
8260 procedure Make_Build_In_Place_Call_In_Object_Declaration
8261 (Obj_Decl : Node_Id;
8262 Function_Call : Node_Id)
8263 is
8264 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8265 -- Get the value of Function_Id, below
8266
8267 ---------------------
8268 -- Get_Function_Id --
8269 ---------------------
8270
8271 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8272 begin
8273 if Is_Entity_Name (Name (Func_Call)) then
8274 return Entity (Name (Func_Call));
8275
8276 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8277 return Etype (Name (Func_Call));
8278
8279 else
8280 raise Program_Error;
8281 end if;
8282 end Get_Function_Id;
8283
8284 -- Local variables
8285
8286 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8287 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8288 Loc : constant Source_Ptr := Sloc (Function_Call);
8289 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8290 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8291 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8292 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8293 Result_Subt : constant Entity_Id := Etype (Function_Id);
8294
8295 Call_Deref : Node_Id;
8296 Caller_Object : Node_Id;
8297 Def_Id : Entity_Id;
8298 Designated_Type : Entity_Id;
8299 Fmaster_Actual : Node_Id := Empty;
8300 Pool_Actual : Node_Id;
8301 Ptr_Typ : Entity_Id;
8302 Ptr_Typ_Decl : Node_Id;
8303 Pass_Caller_Acc : Boolean := False;
8304 Res_Decl : Node_Id;
8305
8306 Definite : constant Boolean :=
8307 Caller_Known_Size (Func_Call, Result_Subt)
8308 and then not Is_Class_Wide_Type (Obj_Typ);
8309 -- In the case of "X : T'Class := F(...);", where F returns a
8310 -- Caller_Known_Size (specific) tagged type, we treat it as
8311 -- indefinite, because the code for the Definite case below sets the
8312 -- initialization expression of the object to Empty, which would be
8313 -- illegal Ada, and would cause gigi to misallocate X.
8314
8315 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8316
8317 begin
8318 -- If the call has already been processed to add build-in-place actuals
8319 -- then return.
8320
8321 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8322 return;
8323 end if;
8324
8325 -- Mark the call as processed as a build-in-place call
8326
8327 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8328
8329 -- Create an access type designating the function's result subtype.
8330 -- We use the type of the original call because it may be a call to an
8331 -- inherited operation, which the expansion has replaced with the parent
8332 -- operation that yields the parent type. Note that this access type
8333 -- must be declared before we establish a transient scope, so that it
8334 -- receives the proper accessibility level.
8335
8336 if Is_Class_Wide_Type (Obj_Typ)
8337 and then not Is_Interface (Obj_Typ)
8338 and then not Is_Class_Wide_Type (Etype (Function_Call))
8339 then
8340 Designated_Type := Obj_Typ;
8341 else
8342 Designated_Type := Etype (Function_Call);
8343 end if;
8344
8345 Ptr_Typ := Make_Temporary (Loc, 'A');
8346 Ptr_Typ_Decl :=
8347 Make_Full_Type_Declaration (Loc,
8348 Defining_Identifier => Ptr_Typ,
8349 Type_Definition =>
8350 Make_Access_To_Object_Definition (Loc,
8351 All_Present => True,
8352 Subtype_Indication =>
8353 New_Occurrence_Of (Designated_Type, Loc)));
8354
8355 -- The access type and its accompanying object must be inserted after
8356 -- the object declaration in the constrained case, so that the function
8357 -- call can be passed access to the object. In the indefinite case, or
8358 -- if the object declaration is for a return object, the access type and
8359 -- object must be inserted before the object, since the object
8360 -- declaration is rewritten to be a renaming of a dereference of the
8361 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8362 -- the result object is in a different (transient) scope, so won't cause
8363 -- freezing.
8364
8365 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8366 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
8367 else
8368 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
8369 end if;
8370
8371 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8372 -- elaborated in an inner (transient) scope and thus won't cause
8373 -- freezing by itself. It's not an itype, but it needs to be frozen
8374 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
8375
8376 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
8377
8378 -- If the object is a return object of an enclosing build-in-place
8379 -- function, then the implicit build-in-place parameters of the
8380 -- enclosing function are simply passed along to the called function.
8381 -- (Unfortunately, this won't cover the case of extension aggregates
8382 -- where the ancestor part is a build-in-place indefinite function
8383 -- call that should be passed along the caller's parameters.
8384 -- Currently those get mishandled by reassigning the result of the
8385 -- call to the aggregate return object, when the call result should
8386 -- really be directly built in place in the aggregate and not in a
8387 -- temporary. ???)
8388
8389 if Is_Return_Object (Obj_Def_Id) then
8390 Pass_Caller_Acc := True;
8391
8392 -- When the enclosing function has a BIP_Alloc_Form formal then we
8393 -- pass it along to the callee (such as when the enclosing function
8394 -- has an unconstrained or tagged result type).
8395
8396 if Needs_BIP_Alloc_Form (Encl_Func) then
8397 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8398 Pool_Actual :=
8399 New_Occurrence_Of
8400 (Build_In_Place_Formal
8401 (Encl_Func, BIP_Storage_Pool), Loc);
8402
8403 -- The build-in-place pool formal is not built on e.g. ZFP
8404
8405 else
8406 Pool_Actual := Empty;
8407 end if;
8408
8409 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8410 (Function_Call => Func_Call,
8411 Function_Id => Function_Id,
8412 Alloc_Form_Exp =>
8413 New_Occurrence_Of
8414 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
8415 Pool_Actual => Pool_Actual);
8416
8417 -- Otherwise, if enclosing function has a definite result subtype,
8418 -- then caller allocation will be used.
8419
8420 else
8421 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8422 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8423 end if;
8424
8425 if Needs_BIP_Finalization_Master (Encl_Func) then
8426 Fmaster_Actual :=
8427 New_Occurrence_Of
8428 (Build_In_Place_Formal
8429 (Encl_Func, BIP_Finalization_Master), Loc);
8430 end if;
8431
8432 -- Retrieve the BIPacc formal from the enclosing function and convert
8433 -- it to the access type of the callee's BIP_Object_Access formal.
8434
8435 Caller_Object :=
8436 Make_Unchecked_Type_Conversion (Loc,
8437 Subtype_Mark =>
8438 New_Occurrence_Of
8439 (Etype (Build_In_Place_Formal
8440 (Function_Id, BIP_Object_Access)),
8441 Loc),
8442 Expression =>
8443 New_Occurrence_Of
8444 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
8445 Loc));
8446
8447 -- In the definite case, add an implicit actual to the function call
8448 -- that provides access to the declared object. An unchecked conversion
8449 -- to the (specific) result type of the function is inserted to handle
8450 -- the case where the object is declared with a class-wide type.
8451
8452 elsif Definite then
8453 Caller_Object :=
8454 Make_Unchecked_Type_Conversion (Loc,
8455 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8456 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8457
8458 -- When the function has a controlling result, an allocation-form
8459 -- parameter must be passed indicating that the caller is allocating
8460 -- the result object. This is needed because such a function can be
8461 -- called as a dispatching operation and must be treated similarly to
8462 -- functions with indefinite result subtypes.
8463
8464 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8465 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8466
8467 -- The allocation for indefinite library-level objects occurs on the
8468 -- heap as opposed to the secondary stack. This accommodates DLLs where
8469 -- the secondary stack is destroyed after each library unload. This is a
8470 -- hybrid mechanism where a stack-allocated object lives on the heap.
8471
8472 elsif Is_Library_Level_Entity (Obj_Def_Id)
8473 and then not Restriction_Active (No_Implicit_Heap_Allocations)
8474 then
8475 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8476 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
8477 Caller_Object := Empty;
8478
8479 -- Create a finalization master for the access result type to ensure
8480 -- that the heap allocation can properly chain the object and later
8481 -- finalize it when the library unit goes out of scope.
8482
8483 if Needs_Finalization (Etype (Func_Call)) then
8484 Build_Finalization_Master
8485 (Typ => Ptr_Typ,
8486 For_Lib_Level => True,
8487 Insertion_Node => Ptr_Typ_Decl);
8488
8489 Fmaster_Actual :=
8490 Make_Attribute_Reference (Loc,
8491 Prefix =>
8492 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
8493 Attribute_Name => Name_Unrestricted_Access);
8494 end if;
8495
8496 -- In other indefinite cases, pass an indication to do the allocation on
8497 -- the secondary stack and set Caller_Object to Empty so that a null
8498 -- value will be passed for the caller's object address. A transient
8499 -- scope is established to ensure eventual cleanup of the result.
8500
8501 else
8502 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8503 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8504 Caller_Object := Empty;
8505
8506 Establish_Transient_Scope (Obj_Decl, Sec_Stack => True);
8507 end if;
8508
8509 -- Pass along any finalization master actual, which is needed in the
8510 -- case where the called function initializes a return object of an
8511 -- enclosing build-in-place function.
8512
8513 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8514 (Func_Call => Func_Call,
8515 Func_Id => Function_Id,
8516 Master_Exp => Fmaster_Actual);
8517
8518 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
8519 and then Has_Task (Result_Subt)
8520 then
8521 -- Here we're passing along the master that was passed in to this
8522 -- function.
8523
8524 Add_Task_Actuals_To_Build_In_Place_Call
8525 (Func_Call, Function_Id,
8526 Master_Actual =>
8527 New_Occurrence_Of
8528 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
8529
8530 else
8531 Add_Task_Actuals_To_Build_In_Place_Call
8532 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8533 end if;
8534
8535 Add_Access_Actual_To_Build_In_Place_Call
8536 (Func_Call,
8537 Function_Id,
8538 Caller_Object,
8539 Is_Access => Pass_Caller_Acc);
8540
8541 -- Finally, create an access object initialized to a reference to the
8542 -- function call. We know this access value cannot be null, so mark the
8543 -- entity accordingly to suppress the access check.
8544
8545 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
8546 Set_Etype (Def_Id, Ptr_Typ);
8547 Set_Is_Known_Non_Null (Def_Id);
8548
8549 if Nkind_In (Function_Call, N_Type_Conversion,
8550 N_Unchecked_Type_Conversion)
8551 then
8552 Res_Decl :=
8553 Make_Object_Declaration (Loc,
8554 Defining_Identifier => Def_Id,
8555 Constant_Present => True,
8556 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8557 Expression =>
8558 Make_Unchecked_Type_Conversion (Loc,
8559 New_Occurrence_Of (Ptr_Typ, Loc),
8560 Make_Reference (Loc, Relocate_Node (Func_Call))));
8561 else
8562 Res_Decl :=
8563 Make_Object_Declaration (Loc,
8564 Defining_Identifier => Def_Id,
8565 Constant_Present => True,
8566 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8567 Expression =>
8568 Make_Reference (Loc, Relocate_Node (Func_Call)));
8569 end if;
8570
8571 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
8572
8573 -- If the result subtype of the called function is definite and is not
8574 -- itself the return expression of an enclosing BIP function, then mark
8575 -- the object as having no initialization.
8576
8577 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8578
8579 -- The related object declaration is encased in a transient block
8580 -- because the build-in-place function call contains at least one
8581 -- nested function call that produces a controlled transient
8582 -- temporary:
8583
8584 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
8585
8586 -- Since the build-in-place expansion decouples the call from the
8587 -- object declaration, the finalization machinery lacks the context
8588 -- which prompted the generation of the transient block. To resolve
8589 -- this scenario, store the build-in-place call.
8590
8591 if Scope_Is_Transient and then Node_To_Be_Wrapped = Obj_Decl then
8592 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
8593 end if;
8594
8595 Set_Expression (Obj_Decl, Empty);
8596 Set_No_Initialization (Obj_Decl);
8597
8598 -- In case of an indefinite result subtype, or if the call is the
8599 -- return expression of an enclosing BIP function, rewrite the object
8600 -- declaration as an object renaming where the renamed object is a
8601 -- dereference of <function_Call>'reference:
8602 --
8603 -- Obj : Subt renames <function_call>'Ref.all;
8604
8605 else
8606 Call_Deref :=
8607 Make_Explicit_Dereference (Obj_Loc,
8608 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
8609
8610 Rewrite (Obj_Decl,
8611 Make_Object_Renaming_Declaration (Obj_Loc,
8612 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
8613 Subtype_Mark =>
8614 New_Occurrence_Of (Designated_Type, Obj_Loc),
8615 Name => Call_Deref));
8616
8617 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
8618 -- to Obj_Def_Id.
8619
8620 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
8621
8622 -- If the original entity comes from source, then mark the new
8623 -- entity as needing debug information, even though it's defined
8624 -- by a generated renaming that does not come from source, so that
8625 -- the Materialize_Entity flag will be set on the entity when
8626 -- Debug_Renaming_Declaration is called during analysis.
8627
8628 if Comes_From_Source (Obj_Def_Id) then
8629 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
8630 end if;
8631
8632 Analyze (Obj_Decl);
8633 Replace_Renaming_Declaration_Id
8634 (Obj_Decl, Original_Node (Obj_Decl));
8635 end if;
8636 end Make_Build_In_Place_Call_In_Object_Declaration;
8637
8638 -------------------------------------------------
8639 -- Make_Build_In_Place_Iface_Call_In_Allocator --
8640 -------------------------------------------------
8641
8642 procedure Make_Build_In_Place_Iface_Call_In_Allocator
8643 (Allocator : Node_Id;
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 Anon_Type : Entity_Id;
8651 Tmp_Decl : Node_Id;
8652 Tmp_Id : Entity_Id;
8653
8654 begin
8655 -- No action of the call has already been processed
8656
8657 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8658 return;
8659 end if;
8660
8661 Tmp_Id := Make_Temporary (Loc, 'D');
8662
8663 -- Insert a temporary before N initialized with the BIP function call
8664 -- without its enclosing type conversions and analyze it without its
8665 -- expansion. This temporary facilitates us reusing the BIP machinery,
8666 -- which takes care of adding the extra build-in-place actuals and
8667 -- transforms this object declaration into an object renaming
8668 -- declaration.
8669
8670 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
8671 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
8672 Set_Etype (Anon_Type, Anon_Type);
8673
8674 Tmp_Decl :=
8675 Make_Object_Declaration (Loc,
8676 Defining_Identifier => Tmp_Id,
8677 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
8678 Expression =>
8679 Make_Allocator (Loc,
8680 Expression =>
8681 Make_Qualified_Expression (Loc,
8682 Subtype_Mark =>
8683 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8684 Expression => New_Copy_Tree (BIP_Func_Call))));
8685
8686 Expander_Mode_Save_And_Set (False);
8687 Insert_Action (Allocator, Tmp_Decl);
8688 Expander_Mode_Restore;
8689
8690 Make_Build_In_Place_Call_In_Allocator
8691 (Allocator => Expression (Tmp_Decl),
8692 Function_Call => Expression (Expression (Tmp_Decl)));
8693
8694 Rewrite (Allocator, New_Occurrence_Of (Tmp_Id, Loc));
8695 end Make_Build_In_Place_Iface_Call_In_Allocator;
8696
8697 ---------------------------------------------------------
8698 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
8699 ---------------------------------------------------------
8700
8701 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
8702 (Function_Call : Node_Id)
8703 is
8704 BIP_Func_Call : constant Node_Id :=
8705 Unqual_BIP_Iface_Function_Call (Function_Call);
8706 Loc : constant Source_Ptr := Sloc (Function_Call);
8707
8708 Tmp_Decl : Node_Id;
8709 Tmp_Id : Entity_Id;
8710
8711 begin
8712 -- No action of the call has already been processed
8713
8714 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8715 return;
8716 end if;
8717
8718 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
8719
8720 -- Insert a temporary before the call initialized with function call to
8721 -- reuse the BIP machinery which takes care of adding the extra build-in
8722 -- place actuals and transforms this object declaration into an object
8723 -- renaming declaration.
8724
8725 Tmp_Id := Make_Temporary (Loc, 'D');
8726
8727 Tmp_Decl :=
8728 Make_Object_Declaration (Loc,
8729 Defining_Identifier => Tmp_Id,
8730 Object_Definition =>
8731 New_Occurrence_Of (Etype (Function_Call), Loc),
8732 Expression => Relocate_Node (Function_Call));
8733
8734 Expander_Mode_Save_And_Set (False);
8735 Insert_Action (Function_Call, Tmp_Decl);
8736 Expander_Mode_Restore;
8737
8738 Make_Build_In_Place_Iface_Call_In_Object_Declaration
8739 (Obj_Decl => Tmp_Decl,
8740 Function_Call => Expression (Tmp_Decl));
8741 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
8742
8743 ----------------------------------------------------------
8744 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
8745 ----------------------------------------------------------
8746
8747 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
8748 (Obj_Decl : Node_Id;
8749 Function_Call : Node_Id)
8750 is
8751 BIP_Func_Call : constant Node_Id :=
8752 Unqual_BIP_Iface_Function_Call (Function_Call);
8753 Loc : constant Source_Ptr := Sloc (Function_Call);
8754 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
8755
8756 Tmp_Decl : Node_Id;
8757 Tmp_Id : Entity_Id;
8758
8759 begin
8760 -- No action of the call has already been processed
8761
8762 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
8763 return;
8764 end if;
8765
8766 Tmp_Id := Make_Temporary (Loc, 'D');
8767
8768 -- Insert a temporary before N initialized with the BIP function call
8769 -- without its enclosing type conversions and analyze it without its
8770 -- expansion. This temporary facilitates us reusing the BIP machinery,
8771 -- which takes care of adding the extra build-in-place actuals and
8772 -- transforms this object declaration into an object renaming
8773 -- declaration.
8774
8775 Tmp_Decl :=
8776 Make_Object_Declaration (Loc,
8777 Defining_Identifier => Tmp_Id,
8778 Object_Definition =>
8779 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
8780 Expression => New_Copy_Tree (BIP_Func_Call));
8781
8782 Expander_Mode_Save_And_Set (False);
8783 Insert_Action (Obj_Decl, Tmp_Decl);
8784 Expander_Mode_Restore;
8785
8786 Make_Build_In_Place_Call_In_Object_Declaration
8787 (Obj_Decl => Tmp_Decl,
8788 Function_Call => Expression (Tmp_Decl));
8789
8790 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
8791
8792 -- Replace the original build-in-place function call by a reference to
8793 -- the resulting temporary object renaming declaration. In this way,
8794 -- all the interface conversions performed in the original Function_Call
8795 -- on the build-in-place object are preserved.
8796
8797 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
8798
8799 -- Replace the original object declaration by an internal object
8800 -- renaming declaration. This leaves the generated code more clean (the
8801 -- build-in-place function call in an object renaming declaration and
8802 -- displacements of the pointer to the build-in-place object in another
8803 -- renaming declaration) and allows us to invoke the routine that takes
8804 -- care of replacing the identifier of the renaming declaration (routine
8805 -- originally developed for the regular build-in-place management).
8806
8807 Rewrite (Obj_Decl,
8808 Make_Object_Renaming_Declaration (Loc,
8809 Defining_Identifier => Make_Temporary (Loc, 'D'),
8810 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
8811 Name => Function_Call));
8812 Analyze (Obj_Decl);
8813
8814 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
8815 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
8816
8817 --------------------------------------------
8818 -- Make_CPP_Constructor_Call_In_Allocator --
8819 --------------------------------------------
8820
8821 procedure Make_CPP_Constructor_Call_In_Allocator
8822 (Allocator : Node_Id;
8823 Function_Call : Node_Id)
8824 is
8825 Loc : constant Source_Ptr := Sloc (Function_Call);
8826 Acc_Type : constant Entity_Id := Etype (Allocator);
8827 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
8828 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
8829
8830 New_Allocator : Node_Id;
8831 Return_Obj_Access : Entity_Id;
8832 Tmp_Obj : Node_Id;
8833
8834 begin
8835 pragma Assert (Nkind (Allocator) = N_Allocator
8836 and then Nkind (Function_Call) = N_Function_Call);
8837 pragma Assert (Convention (Function_Id) = Convention_CPP
8838 and then Is_Constructor (Function_Id));
8839 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
8840
8841 -- Replace the initialized allocator of form "new T'(Func (...))" with
8842 -- an uninitialized allocator of form "new T", where T is the result
8843 -- subtype of the called function. The call to the function is handled
8844 -- separately further below.
8845
8846 New_Allocator :=
8847 Make_Allocator (Loc,
8848 Expression => New_Occurrence_Of (Result_Subt, Loc));
8849 Set_No_Initialization (New_Allocator);
8850
8851 -- Copy attributes to new allocator. Note that the new allocator
8852 -- logically comes from source if the original one did, so copy the
8853 -- relevant flag. This ensures proper treatment of the restriction
8854 -- No_Implicit_Heap_Allocations in this case.
8855
8856 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8857 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8858 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8859
8860 Rewrite (Allocator, New_Allocator);
8861
8862 -- Create a new access object and initialize it to the result of the
8863 -- new uninitialized allocator. Note: we do not use Allocator as the
8864 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8865 -- as this would create a sort of infinite "recursion".
8866
8867 Return_Obj_Access := Make_Temporary (Loc, 'R');
8868 Set_Etype (Return_Obj_Access, Acc_Type);
8869
8870 -- Generate:
8871 -- Rnnn : constant ptr_T := new (T);
8872 -- Init (Rnn.all,...);
8873
8874 Tmp_Obj :=
8875 Make_Object_Declaration (Loc,
8876 Defining_Identifier => Return_Obj_Access,
8877 Constant_Present => True,
8878 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8879 Expression => Relocate_Node (Allocator));
8880 Insert_Action (Allocator, Tmp_Obj);
8881
8882 Insert_List_After_And_Analyze (Tmp_Obj,
8883 Build_Initialization_Call (Loc,
8884 Id_Ref =>
8885 Make_Explicit_Dereference (Loc,
8886 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
8887 Typ => Etype (Function_Id),
8888 Constructor_Ref => Function_Call));
8889
8890 -- Finally, replace the allocator node with a reference to the result of
8891 -- the function call itself (which will effectively be an access to the
8892 -- object created by the allocator).
8893
8894 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8895
8896 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
8897 -- generate an implicit conversion to force displacement of the "this"
8898 -- pointer.
8899
8900 if Is_Interface (Designated_Type (Acc_Type)) then
8901 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
8902 end if;
8903
8904 Analyze_And_Resolve (Allocator, Acc_Type);
8905 end Make_CPP_Constructor_Call_In_Allocator;
8906
8907 -----------------------------------
8908 -- Needs_BIP_Finalization_Master --
8909 -----------------------------------
8910
8911 function Needs_BIP_Finalization_Master
8912 (Func_Id : Entity_Id) return Boolean
8913 is
8914 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8915 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8916 begin
8917 -- A formal giving the finalization master is needed for build-in-place
8918 -- functions whose result type needs finalization or is a tagged type.
8919 -- Tagged primitive build-in-place functions need such a formal because
8920 -- they can be called by a dispatching call, and extensions may require
8921 -- finalization even if the root type doesn't. This means they're also
8922 -- needed for tagged nonprimitive build-in-place functions with tagged
8923 -- results, since such functions can be called via access-to-function
8924 -- types, and those can be used to call primitives, so masters have to
8925 -- be passed to all such build-in-place functions, primitive or not.
8926
8927 return
8928 not Restriction_Active (No_Finalization)
8929 and then (Needs_Finalization (Func_Typ)
8930 or else Is_Tagged_Type (Func_Typ));
8931 end Needs_BIP_Finalization_Master;
8932
8933 --------------------------
8934 -- Needs_BIP_Alloc_Form --
8935 --------------------------
8936
8937 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
8938 pragma Assert (Is_Build_In_Place_Function (Func_Id));
8939 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8940 begin
8941 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
8942 end Needs_BIP_Alloc_Form;
8943
8944 --------------------------------------
8945 -- Needs_Result_Accessibility_Level --
8946 --------------------------------------
8947
8948 function Needs_Result_Accessibility_Level
8949 (Func_Id : Entity_Id) return Boolean
8950 is
8951 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
8952
8953 function Has_Unconstrained_Access_Discriminant_Component
8954 (Comp_Typ : Entity_Id) return Boolean;
8955 -- Returns True if any component of the type has an unconstrained access
8956 -- discriminant.
8957
8958 -----------------------------------------------------
8959 -- Has_Unconstrained_Access_Discriminant_Component --
8960 -----------------------------------------------------
8961
8962 function Has_Unconstrained_Access_Discriminant_Component
8963 (Comp_Typ : Entity_Id) return Boolean
8964 is
8965 begin
8966 if not Is_Limited_Type (Comp_Typ) then
8967 return False;
8968
8969 -- Only limited types can have access discriminants with
8970 -- defaults.
8971
8972 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
8973 return True;
8974
8975 elsif Is_Array_Type (Comp_Typ) then
8976 return Has_Unconstrained_Access_Discriminant_Component
8977 (Underlying_Type (Component_Type (Comp_Typ)));
8978
8979 elsif Is_Record_Type (Comp_Typ) then
8980 declare
8981 Comp : Entity_Id;
8982
8983 begin
8984 Comp := First_Component (Comp_Typ);
8985 while Present (Comp) loop
8986 if Has_Unconstrained_Access_Discriminant_Component
8987 (Underlying_Type (Etype (Comp)))
8988 then
8989 return True;
8990 end if;
8991
8992 Next_Component (Comp);
8993 end loop;
8994 end;
8995 end if;
8996
8997 return False;
8998 end Has_Unconstrained_Access_Discriminant_Component;
8999
9000 Feature_Disabled : constant Boolean := True;
9001 -- Temporary
9002
9003 -- Start of processing for Needs_Result_Accessibility_Level
9004
9005 begin
9006 -- False if completion unavailable (how does this happen???)
9007
9008 if not Present (Func_Typ) then
9009 return False;
9010
9011 elsif Feature_Disabled then
9012 return False;
9013
9014 -- False if not a function, also handle enum-lit renames case
9015
9016 elsif Func_Typ = Standard_Void_Type
9017 or else Is_Scalar_Type (Func_Typ)
9018 then
9019 return False;
9020
9021 -- Handle a corner case, a cross-dialect subp renaming. For example,
9022 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9023 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9024
9025 elsif Present (Alias (Func_Id)) then
9026
9027 -- Unimplemented: a cross-dialect subp renaming which does not set
9028 -- the Alias attribute (e.g., a rename of a dereference of an access
9029 -- to subprogram value). ???
9030
9031 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9032
9033 -- Remaining cases require Ada 2012 mode
9034
9035 elsif Ada_Version < Ada_2012 then
9036 return False;
9037
9038 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9039 or else Is_Tagged_Type (Func_Typ)
9040 then
9041 -- In the case of, say, a null tagged record result type, the need
9042 -- for this extra parameter might not be obvious. This function
9043 -- returns True for all tagged types for compatibility reasons.
9044 -- A function with, say, a tagged null controlling result type might
9045 -- be overridden by a primitive of an extension having an access
9046 -- discriminant and the overrider and overridden must have compatible
9047 -- calling conventions (including implicitly declared parameters).
9048 -- Similarly, values of one access-to-subprogram type might designate
9049 -- both a primitive subprogram of a given type and a function
9050 -- which is, for example, not a primitive subprogram of any type.
9051 -- Again, this requires calling convention compatibility.
9052 -- It might be possible to solve these issues by introducing
9053 -- wrappers, but that is not the approach that was chosen.
9054
9055 return True;
9056
9057 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9058 return True;
9059
9060 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9061 return True;
9062
9063 -- False for all other cases
9064
9065 else
9066 return False;
9067 end if;
9068 end Needs_Result_Accessibility_Level;
9069
9070 -------------------------------------
9071 -- Replace_Renaming_Declaration_Id --
9072 -------------------------------------
9073
9074 procedure Replace_Renaming_Declaration_Id
9075 (New_Decl : Node_Id;
9076 Orig_Decl : Node_Id)
9077 is
9078 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9079 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9080
9081 begin
9082 Set_Chars (New_Id, Chars (Orig_Id));
9083
9084 -- Swap next entity links in preparation for exchanging entities
9085
9086 declare
9087 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9088 begin
9089 Set_Next_Entity (New_Id, Next_Entity (Orig_Id));
9090 Set_Next_Entity (Orig_Id, Next_Id);
9091 end;
9092
9093 Set_Homonym (New_Id, Homonym (Orig_Id));
9094 Exchange_Entities (New_Id, Orig_Id);
9095
9096 -- Preserve source indication of original declaration, so that xref
9097 -- information is properly generated for the right entity.
9098
9099 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9100 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9101
9102 Set_Comes_From_Source (New_Id, False);
9103 end Replace_Renaming_Declaration_Id;
9104
9105 ---------------------------------
9106 -- Rewrite_Function_Call_For_C --
9107 ---------------------------------
9108
9109 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9110 Orig_Func : constant Entity_Id := Entity (Name (N));
9111 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9112 Par : constant Node_Id := Parent (N);
9113 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9114 Loc : constant Source_Ptr := Sloc (Par);
9115 Actuals : List_Id;
9116 Last_Actual : Node_Id;
9117 Last_Formal : Entity_Id;
9118
9119 -- Start of processing for Rewrite_Function_Call_For_C
9120
9121 begin
9122 -- The actuals may be given by named associations, so the added actual
9123 -- that is the target of the return value of the call must be a named
9124 -- association as well, so we retrieve the name of the generated
9125 -- out_formal.
9126
9127 Last_Formal := First_Formal (Proc_Id);
9128 while Present (Next_Formal (Last_Formal)) loop
9129 Last_Formal := Next_Formal (Last_Formal);
9130 end loop;
9131
9132 Actuals := Parameter_Associations (N);
9133
9134 -- The original function may lack parameters
9135
9136 if No (Actuals) then
9137 Actuals := New_List;
9138 end if;
9139
9140 -- If the function call is the expression of an assignment statement,
9141 -- transform the assignment into a procedure call. Generate:
9142
9143 -- LHS := Func_Call (...);
9144
9145 -- Proc_Call (..., LHS);
9146
9147 -- If function is inherited, a conversion may be necessary.
9148
9149 if Nkind (Par) = N_Assignment_Statement then
9150 Last_Actual := Name (Par);
9151
9152 if not Comes_From_Source (Orig_Func)
9153 and then Etype (Orig_Func) /= Etype (Func_Id)
9154 then
9155 Last_Actual :=
9156 Make_Type_Conversion (Loc,
9157 New_Occurrence_Of (Etype (Func_Id), Loc),
9158 Last_Actual);
9159 end if;
9160
9161 Append_To (Actuals,
9162 Make_Parameter_Association (Loc,
9163 Selector_Name =>
9164 Make_Identifier (Loc, Chars (Last_Formal)),
9165 Explicit_Actual_Parameter => Last_Actual));
9166
9167 Rewrite (Par,
9168 Make_Procedure_Call_Statement (Loc,
9169 Name => New_Occurrence_Of (Proc_Id, Loc),
9170 Parameter_Associations => Actuals));
9171 Analyze (Par);
9172
9173 -- Otherwise the context is an expression. Generate a temporary and a
9174 -- procedure call to obtain the function result. Generate:
9175
9176 -- ... Func_Call (...) ...
9177
9178 -- Temp : ...;
9179 -- Proc_Call (..., Temp);
9180 -- ... Temp ...
9181
9182 else
9183 declare
9184 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9185 Call : Node_Id;
9186 Decl : Node_Id;
9187
9188 begin
9189 -- Generate:
9190 -- Temp : ...;
9191
9192 Decl :=
9193 Make_Object_Declaration (Loc,
9194 Defining_Identifier => Temp_Id,
9195 Object_Definition =>
9196 New_Occurrence_Of (Etype (Func_Id), Loc));
9197
9198 -- Generate:
9199 -- Proc_Call (..., Temp);
9200
9201 Append_To (Actuals,
9202 Make_Parameter_Association (Loc,
9203 Selector_Name =>
9204 Make_Identifier (Loc, Chars (Last_Formal)),
9205 Explicit_Actual_Parameter =>
9206 New_Occurrence_Of (Temp_Id, Loc)));
9207
9208 Call :=
9209 Make_Procedure_Call_Statement (Loc,
9210 Name => New_Occurrence_Of (Proc_Id, Loc),
9211 Parameter_Associations => Actuals);
9212
9213 Insert_Actions (Par, New_List (Decl, Call));
9214 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9215 end;
9216 end if;
9217 end Rewrite_Function_Call_For_C;
9218
9219 ------------------------------------
9220 -- Set_Enclosing_Sec_Stack_Return --
9221 ------------------------------------
9222
9223 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9224 P : Node_Id := N;
9225
9226 begin
9227 -- Due to a possible mix of internally generated blocks, source blocks
9228 -- and loops, the scope stack may not be contiguous as all labels are
9229 -- inserted at the top level within the related function. Instead,
9230 -- perform a parent-based traversal and mark all appropriate constructs.
9231
9232 while Present (P) loop
9233
9234 -- Mark the label of a source or internally generated block or
9235 -- loop.
9236
9237 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
9238 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9239
9240 -- Mark the enclosing function
9241
9242 elsif Nkind (P) = N_Subprogram_Body then
9243 if Present (Corresponding_Spec (P)) then
9244 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9245 else
9246 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9247 end if;
9248
9249 -- Do not go beyond the enclosing function
9250
9251 exit;
9252 end if;
9253
9254 P := Parent (P);
9255 end loop;
9256 end Set_Enclosing_Sec_Stack_Return;
9257
9258 ------------------------------------
9259 -- Unqual_BIP_Iface_Function_Call --
9260 ------------------------------------
9261
9262 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9263 Has_Pointer_Displacement : Boolean := False;
9264 On_Object_Declaration : Boolean := False;
9265 -- Remember if processing the renaming expressions on recursion we have
9266 -- traversed an object declaration, since we can traverse many object
9267 -- declaration renamings but just one regular object declaration.
9268
9269 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9270 -- Search for a build-in-place function call skipping any qualification
9271 -- including qualified expressions, type conversions, references, calls
9272 -- to displace the pointer to the object, and renamings. Return Empty if
9273 -- no build-in-place function call is found.
9274
9275 ------------------------------
9276 -- Unqual_BIP_Function_Call --
9277 ------------------------------
9278
9279 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9280 begin
9281 -- Recurse to handle case of multiple levels of qualification and/or
9282 -- conversion.
9283
9284 if Nkind_In (Expr, N_Qualified_Expression,
9285 N_Type_Conversion,
9286 N_Unchecked_Type_Conversion)
9287 then
9288 return Unqual_BIP_Function_Call (Expression (Expr));
9289
9290 -- Recurse to handle case of multiple levels of references and
9291 -- explicit dereferences.
9292
9293 elsif Nkind_In (Expr, N_Attribute_Reference,
9294 N_Explicit_Dereference,
9295 N_Reference)
9296 then
9297 return Unqual_BIP_Function_Call (Prefix (Expr));
9298
9299 -- Recurse on object renamings
9300
9301 elsif Nkind (Expr) = N_Identifier
9302 and then Present (Entity (Expr))
9303 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9304 and then Nkind (Parent (Entity (Expr))) =
9305 N_Object_Renaming_Declaration
9306 and then Present (Renamed_Object (Entity (Expr)))
9307 then
9308 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9309
9310 -- Recurse on the initializing expression of the first reference of
9311 -- an object declaration.
9312
9313 elsif not On_Object_Declaration
9314 and then Nkind (Expr) = N_Identifier
9315 and then Present (Entity (Expr))
9316 and then Ekind_In (Entity (Expr), E_Constant, E_Variable)
9317 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9318 and then Present (Expression (Parent (Entity (Expr))))
9319 then
9320 On_Object_Declaration := True;
9321 return
9322 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9323
9324 -- Recurse to handle calls to displace the pointer to the object to
9325 -- reference a secondary dispatch table.
9326
9327 elsif Nkind (Expr) = N_Function_Call
9328 and then Nkind (Name (Expr)) in N_Has_Entity
9329 and then Present (Entity (Name (Expr)))
9330 and then RTU_Loaded (Ada_Tags)
9331 and then RTE_Available (RE_Displace)
9332 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9333 then
9334 Has_Pointer_Displacement := True;
9335 return
9336 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9337
9338 -- Normal case: check if the inner expression is a BIP function call
9339 -- and the pointer to the object is displaced.
9340
9341 elsif Has_Pointer_Displacement
9342 and then Is_Build_In_Place_Function_Call (Expr)
9343 then
9344 return Expr;
9345
9346 else
9347 return Empty;
9348 end if;
9349 end Unqual_BIP_Function_Call;
9350
9351 -- Start of processing for Unqual_BIP_Iface_Function_Call
9352
9353 begin
9354 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9355
9356 -- Can happen for X'Elab_Spec in the binder-generated file
9357
9358 return Empty;
9359 end if;
9360
9361 return Unqual_BIP_Function_Call (Expr);
9362 end Unqual_BIP_Iface_Function_Call;
9363
9364 end Exp_Ch6;