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