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