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