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