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