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