]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/exp_ch6.adb
[Ada] Variable-sized node types
[thirdparty/gcc.git] / gcc / ada / exp_ch6.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
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_2020
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 (Original_Node (Parent (Parent (Ren_Root)))) then
3593 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
3594
3595 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
3596 Rewrite (Call_Node,
3597 Make_Entry_Call_Statement (Loc,
3598 Name =>
3599 New_Copy_Tree (Name (Ren_Decl)),
3600 Parameter_Associations =>
3601 New_Copy_List_Tree
3602 (Parameter_Associations (Call_Node))));
3603
3604 return;
3605 end if;
3606 end if;
3607 end;
3608 end if;
3609
3610 -- If this is a call to a predicate function, try to constant fold it
3611
3612 if Nkind (Call_Node) = N_Function_Call
3613 and then Is_Entity_Name (Name (Call_Node))
3614 and then Is_Predicate_Function (Subp)
3615 and then Can_Fold_Predicate_Call (Subp)
3616 then
3617 return;
3618 end if;
3619
3620 if Transform_Function_Array
3621 and then Nkind (Call_Node) = N_Function_Call
3622 and then Is_Entity_Name (Name (Call_Node))
3623 then
3624 declare
3625 Func_Id : constant Entity_Id :=
3626 Ultimate_Alias (Entity (Name (Call_Node)));
3627 begin
3628 -- When generating C code, transform a function call that returns
3629 -- a constrained array type into procedure form.
3630
3631 if Rewritten_For_C (Func_Id) then
3632
3633 -- For internally generated calls ensure that they reference
3634 -- the entity of the spec of the called function (needed since
3635 -- the expander may generate calls using the entity of their
3636 -- body).
3637
3638 if not Comes_From_Source (Call_Node)
3639 and then Nkind (Unit_Declaration_Node (Func_Id)) =
3640 N_Subprogram_Body
3641 then
3642 Set_Entity (Name (Call_Node),
3643 Corresponding_Function
3644 (Corresponding_Procedure (Func_Id)));
3645 end if;
3646
3647 Rewrite_Function_Call_For_C (Call_Node);
3648 return;
3649
3650 -- Also introduce a temporary for functions that return a record
3651 -- called within another procedure or function call, since records
3652 -- are passed by pointer in the generated C code, and we cannot
3653 -- take a pointer from a subprogram call.
3654
3655 elsif Modify_Tree_For_C
3656 and then Nkind (Parent (Call_Node)) in N_Subprogram_Call
3657 and then Is_Record_Type (Etype (Func_Id))
3658 then
3659 declare
3660 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
3661 Decl : Node_Id;
3662
3663 begin
3664 -- Generate:
3665 -- Temp : ... := Func_Call (...);
3666
3667 Decl :=
3668 Make_Object_Declaration (Loc,
3669 Defining_Identifier => Temp_Id,
3670 Object_Definition =>
3671 New_Occurrence_Of (Etype (Func_Id), Loc),
3672 Expression =>
3673 Make_Function_Call (Loc,
3674 Name =>
3675 New_Occurrence_Of (Func_Id, Loc),
3676 Parameter_Associations =>
3677 Parameter_Associations (Call_Node)));
3678
3679 Insert_Action (Parent (Call_Node), Decl);
3680 Rewrite (Call_Node, New_Occurrence_Of (Temp_Id, Loc));
3681 return;
3682 end;
3683 end if;
3684 end;
3685 end if;
3686
3687 -- First step, compute extra actuals, corresponding to any Extra_Formals
3688 -- present. Note that we do not access Extra_Formals directly, instead
3689 -- we simply note the presence of the extra formals as we process the
3690 -- regular formals collecting corresponding actuals in Extra_Actuals.
3691
3692 -- We also generate any required range checks for actuals for in formals
3693 -- as we go through the loop, since this is a convenient place to do it.
3694 -- (Though it seems that this would be better done in Expand_Actuals???)
3695
3696 -- Special case: Thunks must not compute the extra actuals; they must
3697 -- just propagate to the target primitive their extra actuals.
3698
3699 if Is_Thunk (Current_Scope)
3700 and then Thunk_Entity (Current_Scope) = Subp
3701 and then Present (Extra_Formals (Subp))
3702 then
3703 pragma Assert (Present (Extra_Formals (Current_Scope)));
3704
3705 declare
3706 Target_Formal : Entity_Id;
3707 Thunk_Formal : Entity_Id;
3708
3709 begin
3710 Target_Formal := Extra_Formals (Subp);
3711 Thunk_Formal := Extra_Formals (Current_Scope);
3712 while Present (Target_Formal) loop
3713 Add_Extra_Actual
3714 (Expr => New_Occurrence_Of (Thunk_Formal, Loc),
3715 EF => Thunk_Formal);
3716
3717 Target_Formal := Extra_Formal (Target_Formal);
3718 Thunk_Formal := Extra_Formal (Thunk_Formal);
3719 end loop;
3720
3721 while Is_Non_Empty_List (Extra_Actuals) loop
3722 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3723 end loop;
3724
3725 Expand_Actuals (Call_Node, Subp, Post_Call);
3726 pragma Assert (Is_Empty_List (Post_Call));
3727 pragma Assert (Check_Number_Of_Actuals (Call_Node, Subp));
3728 pragma Assert (Check_BIP_Actuals (Call_Node, Subp));
3729 return;
3730 end;
3731 end if;
3732
3733 Formal := First_Formal (Subp);
3734 Actual := First_Actual (Call_Node);
3735 Param_Count := 1;
3736 while Present (Formal) loop
3737 -- Prepare to examine current entry
3738
3739 Prev := Actual;
3740
3741 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
3742 -- to expand it in a further round.
3743
3744 CW_Interface_Formals_Present :=
3745 CW_Interface_Formals_Present
3746 or else Is_Class_Wide_Interface_Type (Etype (Formal));
3747
3748 -- Create possible extra actual for constrained case. Usually, the
3749 -- extra actual is of the form actual'constrained, but since this
3750 -- attribute is only available for unconstrained records, TRUE is
3751 -- expanded if the type of the formal happens to be constrained (for
3752 -- instance when this procedure is inherited from an unconstrained
3753 -- record to a constrained one) or if the actual has no discriminant
3754 -- (its type is constrained). An exception to this is the case of a
3755 -- private type without discriminants. In this case we pass FALSE
3756 -- because the object has underlying discriminants with defaults.
3757
3758 if Present (Extra_Constrained (Formal)) then
3759 if Ekind (Etype (Prev)) in Private_Kind
3760 and then not Has_Discriminants (Base_Type (Etype (Prev)))
3761 then
3762 Add_Extra_Actual
3763 (Expr => New_Occurrence_Of (Standard_False, Loc),
3764 EF => Extra_Constrained (Formal));
3765
3766 elsif Is_Constrained (Etype (Formal))
3767 or else not Has_Discriminants (Etype (Prev))
3768 then
3769 Add_Extra_Actual
3770 (Expr => New_Occurrence_Of (Standard_True, Loc),
3771 EF => Extra_Constrained (Formal));
3772
3773 -- Do not produce extra actuals for Unchecked_Union parameters.
3774 -- Jump directly to the end of the loop.
3775
3776 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
3777 goto Skip_Extra_Actual_Generation;
3778
3779 else
3780 -- If the actual is a type conversion, then the constrained
3781 -- test applies to the actual, not the target type.
3782
3783 declare
3784 Act_Prev : Node_Id;
3785
3786 begin
3787 -- Test for unchecked conversions as well, which can occur
3788 -- as out parameter actuals on calls to stream procedures.
3789
3790 Act_Prev := Prev;
3791 while Nkind (Act_Prev) in N_Type_Conversion
3792 | N_Unchecked_Type_Conversion
3793 loop
3794 Act_Prev := Expression (Act_Prev);
3795 end loop;
3796
3797 -- If the expression is a conversion of a dereference, this
3798 -- is internally generated code that manipulates addresses,
3799 -- e.g. when building interface tables. No check should
3800 -- occur in this case, and the discriminated object is not
3801 -- directly at hand.
3802
3803 if not Comes_From_Source (Actual)
3804 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3805 and then Nkind (Act_Prev) = N_Explicit_Dereference
3806 then
3807 Add_Extra_Actual
3808 (Expr => New_Occurrence_Of (Standard_False, Loc),
3809 EF => Extra_Constrained (Formal));
3810
3811 else
3812 Add_Extra_Actual
3813 (Expr =>
3814 Make_Attribute_Reference (Sloc (Prev),
3815 Prefix =>
3816 Duplicate_Subexpr_No_Checks
3817 (Act_Prev, Name_Req => True),
3818 Attribute_Name => Name_Constrained),
3819 EF => Extra_Constrained (Formal));
3820 end if;
3821 end;
3822 end if;
3823 end if;
3824
3825 -- Create possible extra actual for accessibility level
3826
3827 if Present (Extra_Accessibility (Formal)) then
3828 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
3829 -- accessibility levels.
3830
3831 if Is_Thunk (Current_Scope) then
3832 declare
3833 Parm_Ent : Entity_Id;
3834
3835 begin
3836 if Is_Controlling_Actual (Actual) then
3837
3838 -- Find the corresponding actual of the thunk
3839
3840 Parm_Ent := First_Entity (Current_Scope);
3841 for J in 2 .. Param_Count loop
3842 Next_Entity (Parm_Ent);
3843 end loop;
3844
3845 -- Handle unchecked conversion of access types generated
3846 -- in thunks (cf. Expand_Interface_Thunk).
3847
3848 elsif Is_Access_Type (Etype (Actual))
3849 and then Nkind (Actual) = N_Unchecked_Type_Conversion
3850 then
3851 Parm_Ent := Entity (Expression (Actual));
3852
3853 else pragma Assert (Is_Entity_Name (Actual));
3854 Parm_Ent := Entity (Actual);
3855 end if;
3856
3857 Add_Extra_Actual
3858 (Expr =>
3859 New_Occurrence_Of
3860 (Get_Dynamic_Accessibility (Parm_Ent), Loc),
3861 EF => Extra_Accessibility (Formal));
3862 end;
3863
3864 -- Conditional expressions
3865
3866 elsif Nkind (Prev) = N_Expression_With_Actions
3867 and then Nkind (Original_Node (Prev)) in
3868 N_If_Expression | N_Case_Expression
3869 then
3870 Add_Cond_Expression_Extra_Actual (Formal);
3871
3872 -- Internal constant generated to remove side effects (normally
3873 -- from the expansion of dispatching calls).
3874
3875 -- First verify the actual is internal
3876
3877 elsif not Comes_From_Source (Prev)
3878 and then Original_Node (Prev) = Prev
3879
3880 -- Next check that the actual is a constant
3881
3882 and then Nkind (Prev) = N_Identifier
3883 and then Ekind (Entity (Prev)) = E_Constant
3884 and then Nkind (Parent (Entity (Prev))) = N_Object_Declaration
3885 then
3886 -- Generate the accessibility level based on the expression in
3887 -- the constant's declaration.
3888
3889 Add_Extra_Actual
3890 (Expr => Accessibility_Level
3891 (Expr => Expression (Parent (Entity (Prev))),
3892 Level => Dynamic_Level),
3893 EF => Extra_Accessibility (Formal));
3894
3895 -- Normal case
3896
3897 else
3898 Add_Extra_Actual
3899 (Expr => Accessibility_Level (Prev, Dynamic_Level),
3900 EF => Extra_Accessibility (Formal));
3901 end if;
3902 end if;
3903
3904 -- Perform the check of 4.6(49) that prevents a null value from being
3905 -- passed as an actual to an access parameter. Note that the check
3906 -- is elided in the common cases of passing an access attribute or
3907 -- access parameter as an actual. Also, we currently don't enforce
3908 -- this check for expander-generated actuals and when -gnatdj is set.
3909
3910 if Ada_Version >= Ada_2005 then
3911
3912 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
3913 -- the intent of 6.4.1(13) is that null-exclusion checks should
3914 -- not be done for 'out' parameters, even though it refers only
3915 -- to constraint checks, and a null_exclusion is not a constraint.
3916 -- Note that AI05-0196-1 corrects this mistake in the RM.
3917
3918 if Is_Access_Type (Etype (Formal))
3919 and then Can_Never_Be_Null (Etype (Formal))
3920 and then Ekind (Formal) /= E_Out_Parameter
3921 and then Nkind (Prev) /= N_Raise_Constraint_Error
3922 and then (Known_Null (Prev)
3923 or else not Can_Never_Be_Null (Etype (Prev)))
3924 then
3925 Install_Null_Excluding_Check (Prev);
3926 end if;
3927
3928 -- Ada_Version < Ada_2005
3929
3930 else
3931 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
3932 or else Access_Checks_Suppressed (Subp)
3933 then
3934 null;
3935
3936 elsif Debug_Flag_J then
3937 null;
3938
3939 elsif not Comes_From_Source (Prev) then
3940 null;
3941
3942 elsif Is_Entity_Name (Prev)
3943 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3944 then
3945 null;
3946
3947 elsif Nkind (Prev) in N_Allocator | N_Attribute_Reference then
3948 null;
3949
3950 else
3951 Install_Null_Excluding_Check (Prev);
3952 end if;
3953 end if;
3954
3955 -- Perform appropriate validity checks on parameters that
3956 -- are entities.
3957
3958 if Validity_Checks_On then
3959 if (Ekind (Formal) = E_In_Parameter
3960 and then Validity_Check_In_Params)
3961 or else
3962 (Ekind (Formal) = E_In_Out_Parameter
3963 and then Validity_Check_In_Out_Params)
3964 then
3965 -- If the actual is an indexed component of a packed type (or
3966 -- is an indexed or selected component whose prefix recursively
3967 -- meets this condition), it has not been expanded yet. It will
3968 -- be copied in the validity code that follows, and has to be
3969 -- expanded appropriately, so reanalyze it.
3970
3971 -- What we do is just to unset analyzed bits on prefixes till
3972 -- we reach something that does not have a prefix.
3973
3974 declare
3975 Nod : Node_Id;
3976
3977 begin
3978 Nod := Actual;
3979 while Nkind (Nod) in
3980 N_Indexed_Component | N_Selected_Component
3981 loop
3982 Set_Analyzed (Nod, False);
3983 Nod := Prefix (Nod);
3984 end loop;
3985 end;
3986
3987 Ensure_Valid (Actual);
3988 end if;
3989 end if;
3990
3991 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3992 -- since this is a left side reference. We only do this for calls
3993 -- from the source program since we assume that compiler generated
3994 -- calls explicitly generate any required checks. We also need it
3995 -- only if we are doing standard validity checks, since clearly it is
3996 -- not needed if validity checks are off, and in subscript validity
3997 -- checking mode, all indexed components are checked with a call
3998 -- directly from Expand_N_Indexed_Component.
3999
4000 if Comes_From_Source (Call_Node)
4001 and then Ekind (Formal) /= E_In_Parameter
4002 and then Validity_Checks_On
4003 and then Validity_Check_Default
4004 and then not Validity_Check_Subscripts
4005 then
4006 Check_Valid_Lvalue_Subscripts (Actual);
4007 end if;
4008
4009 -- Mark any scalar OUT parameter that is a simple variable as no
4010 -- longer known to be valid (unless the type is always valid). This
4011 -- reflects the fact that if an OUT parameter is never set in a
4012 -- procedure, then it can become invalid on the procedure return.
4013
4014 if Ekind (Formal) = E_Out_Parameter
4015 and then Is_Entity_Name (Actual)
4016 and then Ekind (Entity (Actual)) = E_Variable
4017 and then not Is_Known_Valid (Etype (Actual))
4018 then
4019 Set_Is_Known_Valid (Entity (Actual), False);
4020 end if;
4021
4022 -- For an OUT or IN OUT parameter, if the actual is an entity, then
4023 -- clear current values, since they can be clobbered. We are probably
4024 -- doing this in more places than we need to, but better safe than
4025 -- sorry when it comes to retaining bad current values.
4026
4027 if Ekind (Formal) /= E_In_Parameter
4028 and then Is_Entity_Name (Actual)
4029 and then Present (Entity (Actual))
4030 then
4031 declare
4032 Ent : constant Entity_Id := Entity (Actual);
4033 Sav : Node_Id;
4034
4035 begin
4036 -- For an OUT or IN OUT parameter that is an assignable entity,
4037 -- we do not want to clobber the Last_Assignment field, since
4038 -- if it is set, it was precisely because it is indeed an OUT
4039 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
4040 -- since the subprogram could have returned in invalid value.
4041
4042 if Is_Assignable (Ent) then
4043 Sav := Last_Assignment (Ent);
4044 Kill_Current_Values (Ent);
4045 Set_Last_Assignment (Ent, Sav);
4046 Set_Is_Known_Valid (Ent, False);
4047 Set_Is_True_Constant (Ent, False);
4048
4049 -- For all other cases, just kill the current values
4050
4051 else
4052 Kill_Current_Values (Ent);
4053 end if;
4054 end;
4055 end if;
4056
4057 -- If the formal is class wide and the actual is an aggregate, force
4058 -- evaluation so that the back end who does not know about class-wide
4059 -- type, does not generate a temporary of the wrong size.
4060
4061 if not Is_Class_Wide_Type (Etype (Formal)) then
4062 null;
4063
4064 elsif Nkind (Actual) = N_Aggregate
4065 or else (Nkind (Actual) = N_Qualified_Expression
4066 and then Nkind (Expression (Actual)) = N_Aggregate)
4067 then
4068 Force_Evaluation (Actual);
4069 end if;
4070
4071 -- In a remote call, if the formal is of a class-wide type, check
4072 -- that the actual meets the requirements described in E.4(18).
4073
4074 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
4075 Insert_Action (Actual,
4076 Make_Transportable_Check (Loc,
4077 Duplicate_Subexpr_Move_Checks (Actual)));
4078 end if;
4079
4080 -- Perform invariant checks for all intermediate types in a view
4081 -- conversion after successful return from a call that passes the
4082 -- view conversion as an IN OUT or OUT parameter (RM 7.3.2 (12/3,
4083 -- 13/3, 14/3)). Consider only source conversion in order to avoid
4084 -- generating spurious checks on complex expansion such as object
4085 -- initialization through an extension aggregate.
4086
4087 if Comes_From_Source (Call_Node)
4088 and then Ekind (Formal) /= E_In_Parameter
4089 and then Nkind (Actual) = N_Type_Conversion
4090 then
4091 Add_View_Conversion_Invariants (Formal, Actual);
4092 end if;
4093
4094 -- Generating C the initialization of an allocator is performed by
4095 -- means of individual statements, and hence it must be done before
4096 -- the call.
4097
4098 if Modify_Tree_For_C
4099 and then Nkind (Actual) = N_Allocator
4100 and then Nkind (Expression (Actual)) = N_Qualified_Expression
4101 then
4102 Remove_Side_Effects (Actual);
4103 end if;
4104
4105 -- This label is required when skipping extra actual generation for
4106 -- Unchecked_Union parameters.
4107
4108 <<Skip_Extra_Actual_Generation>>
4109
4110 Param_Count := Param_Count + 1;
4111 Next_Actual (Actual);
4112 Next_Formal (Formal);
4113 end loop;
4114
4115 -- If we are calling an Ada 2012 function which needs to have the
4116 -- "accessibility level determined by the point of call" (AI05-0234)
4117 -- passed in to it, then pass it in.
4118
4119 if Ekind (Subp) in E_Function | E_Operator | E_Subprogram_Type
4120 and then
4121 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
4122 then
4123 declare
4124 Extra_Form : Node_Id := Empty;
4125 Level : Node_Id := Empty;
4126
4127 begin
4128 -- Detect cases where the function call has been internally
4129 -- generated by examining the original node and return library
4130 -- level - taking care to avoid ignoring function calls expanded
4131 -- in prefix notation.
4132
4133 if Nkind (Original_Node (Call_Node)) not in N_Function_Call
4134 | N_Selected_Component
4135 | N_Indexed_Component
4136 then
4137 Level := Make_Integer_Literal
4138 (Loc, Scope_Depth (Standard_Standard));
4139
4140 -- Otherwise get the level normally based on the call node
4141
4142 else
4143 Level := Accessibility_Level (Call_Node, Dynamic_Level);
4144
4145 end if;
4146
4147 -- It may be possible that we are re-expanding an already
4148 -- expanded call when are are dealing with dispatching ???
4149
4150 if not Present (Parameter_Associations (Call_Node))
4151 or else Nkind (Last (Parameter_Associations (Call_Node)))
4152 /= N_Parameter_Association
4153 or else not Is_Accessibility_Actual
4154 (Last (Parameter_Associations (Call_Node)))
4155 then
4156 Extra_Form := Extra_Accessibility_Of_Result
4157 (Ultimate_Alias (Subp));
4158
4159 Add_Extra_Actual
4160 (Expr => Level,
4161 EF => Extra_Form);
4162 end if;
4163 end;
4164 end if;
4165
4166 -- If we are expanding the RHS of an assignment we need to check if tag
4167 -- propagation is needed. You might expect this processing to be in
4168 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
4169 -- assignment might be transformed to a declaration for an unconstrained
4170 -- value if the expression is classwide.
4171
4172 if Nkind (Call_Node) = N_Function_Call
4173 and then Is_Tag_Indeterminate (Call_Node)
4174 and then Is_Entity_Name (Name (Call_Node))
4175 then
4176 declare
4177 Ass : Node_Id := Empty;
4178
4179 begin
4180 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
4181 Ass := Parent (Call_Node);
4182
4183 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
4184 and then Nkind (Parent (Parent (Call_Node))) =
4185 N_Assignment_Statement
4186 then
4187 Ass := Parent (Parent (Call_Node));
4188
4189 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
4190 and then Nkind (Parent (Parent (Call_Node))) =
4191 N_Assignment_Statement
4192 then
4193 Ass := Parent (Parent (Call_Node));
4194 end if;
4195
4196 if Present (Ass)
4197 and then Is_Class_Wide_Type (Etype (Name (Ass)))
4198 then
4199 -- Move the error messages below to sem???
4200
4201 if Is_Access_Type (Etype (Call_Node)) then
4202 if Designated_Type (Etype (Call_Node)) /=
4203 Root_Type (Etype (Name (Ass)))
4204 then
4205 Error_Msg_NE
4206 ("tag-indeterminate expression must have designated "
4207 & "type& (RM 5.2 (6))",
4208 Call_Node, Root_Type (Etype (Name (Ass))));
4209 else
4210 Propagate_Tag (Name (Ass), Call_Node);
4211 end if;
4212
4213 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
4214 Error_Msg_NE
4215 ("tag-indeterminate expression must have type & "
4216 & "(RM 5.2 (6))",
4217 Call_Node, Root_Type (Etype (Name (Ass))));
4218
4219 else
4220 Propagate_Tag (Name (Ass), Call_Node);
4221 end if;
4222
4223 -- The call will be rewritten as a dispatching call, and
4224 -- expanded as such.
4225
4226 return;
4227 end if;
4228 end;
4229 end if;
4230
4231 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
4232 -- it to point to the correct secondary virtual table.
4233
4234 if Nkind (Call_Node) in N_Subprogram_Call
4235 and then CW_Interface_Formals_Present
4236 then
4237 Expand_Interface_Actuals (Call_Node);
4238 end if;
4239
4240 -- Deals with Dispatch_Call if we still have a call, before expanding
4241 -- extra actuals since this will be done on the re-analysis of the
4242 -- dispatching call. Note that we do not try to shorten the actual list
4243 -- for a dispatching call, it would not make sense to do so. Expansion
4244 -- of dispatching calls is suppressed for VM targets, because the VM
4245 -- back-ends directly handle the generation of dispatching calls and
4246 -- would have to undo any expansion to an indirect call.
4247
4248 if Nkind (Call_Node) in N_Subprogram_Call
4249 and then Present (Controlling_Argument (Call_Node))
4250 then
4251 declare
4252 Call_Typ : constant Entity_Id := Etype (Call_Node);
4253 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
4254 Eq_Prim_Op : Entity_Id := Empty;
4255 New_Call : Node_Id;
4256 Param : Node_Id;
4257 Prev_Call : Node_Id;
4258
4259 begin
4260 if not Is_Limited_Type (Typ) then
4261 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
4262 end if;
4263
4264 if Tagged_Type_Expansion then
4265 Expand_Dispatching_Call (Call_Node);
4266
4267 -- The following return is worrisome. Is it really OK to skip
4268 -- all remaining processing in this procedure ???
4269
4270 return;
4271
4272 -- VM targets
4273
4274 else
4275 Apply_Tag_Checks (Call_Node);
4276
4277 -- If this is a dispatching "=", we must first compare the
4278 -- tags so we generate: x.tag = y.tag and then x = y
4279
4280 if Subp = Eq_Prim_Op then
4281
4282 -- Mark the node as analyzed to avoid reanalyzing this
4283 -- dispatching call (which would cause a never-ending loop)
4284
4285 Prev_Call := Relocate_Node (Call_Node);
4286 Set_Analyzed (Prev_Call);
4287
4288 Param := First_Actual (Call_Node);
4289 New_Call :=
4290 Make_And_Then (Loc,
4291 Left_Opnd =>
4292 Make_Op_Eq (Loc,
4293 Left_Opnd =>
4294 Make_Selected_Component (Loc,
4295 Prefix => New_Value (Param),
4296 Selector_Name =>
4297 New_Occurrence_Of
4298 (First_Tag_Component (Typ), Loc)),
4299
4300 Right_Opnd =>
4301 Make_Selected_Component (Loc,
4302 Prefix =>
4303 Unchecked_Convert_To (Typ,
4304 New_Value (Next_Actual (Param))),
4305 Selector_Name =>
4306 New_Occurrence_Of
4307 (First_Tag_Component (Typ), Loc))),
4308 Right_Opnd => Prev_Call);
4309
4310 Rewrite (Call_Node, New_Call);
4311
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 end if;
4323
4324 -- If this is a dispatching "=" then we must update the reference
4325 -- to the call node because we generated:
4326 -- x.tag = y.tag and then x = y
4327
4328 if Subp = Eq_Prim_Op then
4329 Call_Node := Right_Opnd (Call_Node);
4330 end if;
4331 end;
4332 end if;
4333
4334 -- Similarly, expand calls to RCI subprograms on which pragma
4335 -- All_Calls_Remote applies. The rewriting will be reanalyzed
4336 -- later. Do this only when the call comes from source since we
4337 -- do not want such a rewriting to occur in expanded code.
4338
4339 if Is_All_Remote_Call (Call_Node) then
4340 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
4341
4342 -- Similarly, do not add extra actuals for an entry call whose entity
4343 -- is a protected procedure, or for an internal protected subprogram
4344 -- call, because it will be rewritten as a protected subprogram call
4345 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
4346
4347 elsif Is_Protected_Type (Scope (Subp))
4348 and then Ekind (Subp) in E_Procedure | E_Function
4349 then
4350 null;
4351
4352 -- During that loop we gathered the extra actuals (the ones that
4353 -- correspond to Extra_Formals), so now they can be appended.
4354
4355 else
4356 while Is_Non_Empty_List (Extra_Actuals) loop
4357 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
4358 end loop;
4359 end if;
4360
4361 -- At this point we have all the actuals, so this is the point at which
4362 -- the various expansion activities for actuals is carried out.
4363
4364 Expand_Actuals (Call_Node, Subp, Post_Call);
4365
4366 -- If it is a recursive call then call the internal procedure that
4367 -- verifies Subprogram_Variant contract (if present and enabled).
4368 -- Detecting calls to subprogram aliases is necessary for recursive
4369 -- calls in instances of generic subprograms, where the renaming of
4370 -- the current subprogram is called.
4371
4372 if Is_Subprogram (Subp)
4373 and then Same_Or_Aliased_Subprograms (Subp, Current_Scope)
4374 then
4375 Check_Subprogram_Variant;
4376 end if;
4377
4378 -- Verify that the actuals do not share storage. This check must be done
4379 -- on the caller side rather that inside the subprogram to avoid issues
4380 -- of parameter passing.
4381
4382 if Check_Aliasing_Of_Parameters then
4383 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
4384 end if;
4385
4386 -- If the subprogram is a renaming, or if it is inherited, replace it in
4387 -- the call with the name of the actual subprogram being called. If this
4388 -- is a dispatching call, the run-time decides what to call. The Alias
4389 -- attribute does not apply to entries.
4390
4391 if Nkind (Call_Node) /= N_Entry_Call_Statement
4392 and then No (Controlling_Argument (Call_Node))
4393 and then Present (Parent_Subp)
4394 and then not Is_Direct_Deep_Call (Subp)
4395 then
4396 if Present (Inherited_From_Formal (Subp)) then
4397 Parent_Subp := Inherited_From_Formal (Subp);
4398 else
4399 Parent_Subp := Ultimate_Alias (Parent_Subp);
4400 end if;
4401
4402 -- The below setting of Entity is suspect, see F109-018 discussion???
4403
4404 Set_Entity (Name (Call_Node), Parent_Subp);
4405
4406 -- Move this check to sem???
4407
4408 if Is_Abstract_Subprogram (Parent_Subp)
4409 and then not In_Instance
4410 then
4411 Error_Msg_NE
4412 ("cannot call abstract subprogram &!",
4413 Name (Call_Node), Parent_Subp);
4414 end if;
4415
4416 -- Inspect all formals of derived subprogram Subp. Compare parameter
4417 -- types with the parent subprogram and check whether an actual may
4418 -- need a type conversion to the corresponding formal of the parent
4419 -- subprogram.
4420
4421 -- Not clear whether intrinsic subprograms need such conversions. ???
4422
4423 if not Is_Intrinsic_Subprogram (Parent_Subp)
4424 or else Is_Generic_Instance (Parent_Subp)
4425 then
4426 declare
4427 procedure Convert (Act : Node_Id; Typ : Entity_Id);
4428 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
4429 -- and resolve the newly generated construct.
4430
4431 -------------
4432 -- Convert --
4433 -------------
4434
4435 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
4436 begin
4437 Rewrite (Act, OK_Convert_To (Typ, Act));
4438 Analyze_And_Resolve (Act, Typ);
4439 end Convert;
4440
4441 -- Local variables
4442
4443 Actual_Typ : Entity_Id;
4444 Formal_Typ : Entity_Id;
4445 Parent_Typ : Entity_Id;
4446
4447 begin
4448 Actual := First_Actual (Call_Node);
4449 Formal := First_Formal (Subp);
4450 Parent_Formal := First_Formal (Parent_Subp);
4451 while Present (Formal) loop
4452 Actual_Typ := Etype (Actual);
4453 Formal_Typ := Etype (Formal);
4454 Parent_Typ := Etype (Parent_Formal);
4455
4456 -- For an IN parameter of a scalar type, the derived formal
4457 -- type and parent formal type differ, and the parent formal
4458 -- type and actual type do not match statically.
4459
4460 if Is_Scalar_Type (Formal_Typ)
4461 and then Ekind (Formal) = E_In_Parameter
4462 and then Formal_Typ /= Parent_Typ
4463 and then
4464 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
4465 and then not Raises_Constraint_Error (Actual)
4466 then
4467 Convert (Actual, Parent_Typ);
4468
4469 -- For access types, the parent formal type and actual type
4470 -- differ.
4471
4472 elsif Is_Access_Type (Formal_Typ)
4473 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
4474 then
4475 if Ekind (Formal) /= E_In_Parameter then
4476 Convert (Actual, Parent_Typ);
4477
4478 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
4479 and then Designated_Type (Parent_Typ) /=
4480 Designated_Type (Actual_Typ)
4481 and then not Is_Controlling_Formal (Formal)
4482 then
4483 -- This unchecked conversion is not necessary unless
4484 -- inlining is enabled, because in that case the type
4485 -- mismatch may become visible in the body about to be
4486 -- inlined.
4487
4488 Rewrite (Actual,
4489 Unchecked_Convert_To (Parent_Typ, Actual));
4490 Analyze_And_Resolve (Actual, Parent_Typ);
4491 end if;
4492
4493 -- If there is a change of representation, then generate a
4494 -- warning, and do the change of representation.
4495
4496 elsif not Has_Compatible_Representation
4497 (Target_Type => Formal_Typ,
4498 Operand_Type => Parent_Typ)
4499 then
4500 Error_Msg_N
4501 ("??change of representation required", Actual);
4502 Convert (Actual, Parent_Typ);
4503
4504 -- For array and record types, the parent formal type and
4505 -- derived formal type have different sizes or pragma Pack
4506 -- status.
4507
4508 elsif ((Is_Array_Type (Formal_Typ)
4509 and then Is_Array_Type (Parent_Typ))
4510 or else
4511 (Is_Record_Type (Formal_Typ)
4512 and then Is_Record_Type (Parent_Typ)))
4513 and then
4514 (Esize (Formal_Typ) /= Esize (Parent_Typ)
4515 or else Has_Pragma_Pack (Formal_Typ) /=
4516 Has_Pragma_Pack (Parent_Typ))
4517 then
4518 Convert (Actual, Parent_Typ);
4519 end if;
4520
4521 Next_Actual (Actual);
4522 Next_Formal (Formal);
4523 Next_Formal (Parent_Formal);
4524 end loop;
4525 end;
4526 end if;
4527
4528 Orig_Subp := Subp;
4529 Subp := Parent_Subp;
4530 end if;
4531
4532 -- Deal with case where call is an explicit dereference
4533
4534 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
4535
4536 -- Handle case of access to protected subprogram type
4537
4538 if Is_Access_Protected_Subprogram_Type
4539 (Base_Type (Etype (Prefix (Name (Call_Node)))))
4540 then
4541 -- If this is a call through an access to protected operation, the
4542 -- prefix has the form (object'address, operation'access). Rewrite
4543 -- as a for other protected calls: the object is the 1st parameter
4544 -- of the list of actuals.
4545
4546 declare
4547 Call : Node_Id;
4548 Parm : List_Id;
4549 Nam : Node_Id;
4550 Obj : Node_Id;
4551 Ptr : constant Node_Id := Prefix (Name (Call_Node));
4552
4553 T : constant Entity_Id :=
4554 Equivalent_Type (Base_Type (Etype (Ptr)));
4555
4556 D_T : constant Entity_Id :=
4557 Designated_Type (Base_Type (Etype (Ptr)));
4558
4559 begin
4560 Obj :=
4561 Make_Selected_Component (Loc,
4562 Prefix => Unchecked_Convert_To (T, Ptr),
4563 Selector_Name =>
4564 New_Occurrence_Of (First_Entity (T), Loc));
4565
4566 Nam :=
4567 Make_Selected_Component (Loc,
4568 Prefix => Unchecked_Convert_To (T, Ptr),
4569 Selector_Name =>
4570 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
4571
4572 Nam :=
4573 Make_Explicit_Dereference (Loc,
4574 Prefix => Nam);
4575
4576 if Present (Parameter_Associations (Call_Node)) then
4577 Parm := Parameter_Associations (Call_Node);
4578 else
4579 Parm := New_List;
4580 end if;
4581
4582 Prepend (Obj, Parm);
4583
4584 if Etype (D_T) = Standard_Void_Type then
4585 Call :=
4586 Make_Procedure_Call_Statement (Loc,
4587 Name => Nam,
4588 Parameter_Associations => Parm);
4589 else
4590 Call :=
4591 Make_Function_Call (Loc,
4592 Name => Nam,
4593 Parameter_Associations => Parm);
4594 end if;
4595
4596 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
4597 Set_Etype (Call, Etype (D_T));
4598
4599 -- We do not re-analyze the call to avoid infinite recursion.
4600 -- We analyze separately the prefix and the object, and set
4601 -- the checks on the prefix that would otherwise be emitted
4602 -- when resolving a call.
4603
4604 Rewrite (Call_Node, Call);
4605 Analyze (Nam);
4606 Apply_Access_Check (Nam);
4607 Analyze (Obj);
4608 return;
4609 end;
4610 end if;
4611 end if;
4612
4613 -- If this is a call to an intrinsic subprogram, then perform the
4614 -- appropriate expansion to the corresponding tree node and we
4615 -- are all done (since after that the call is gone).
4616
4617 -- In the case where the intrinsic is to be processed by the back end,
4618 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
4619 -- since the idea in this case is to pass the call unchanged. If the
4620 -- intrinsic is an inherited unchecked conversion, and the derived type
4621 -- is the target type of the conversion, we must retain it as the return
4622 -- type of the expression. Otherwise the expansion below, which uses the
4623 -- parent operation, will yield the wrong type.
4624
4625 if Is_Intrinsic_Subprogram (Subp) then
4626 Expand_Intrinsic_Call (Call_Node, Subp);
4627
4628 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
4629 and then Parent_Subp /= Orig_Subp
4630 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
4631 then
4632 Set_Etype (Call_Node, Etype (Orig_Subp));
4633 end if;
4634
4635 return;
4636 end if;
4637
4638 if Ekind (Subp) in E_Function | E_Procedure then
4639
4640 -- We perform a simple optimization on calls for To_Address by
4641 -- replacing them with an unchecked conversion. Not only is this
4642 -- efficient, but it also avoids order of elaboration problems when
4643 -- address clauses are inlined (address expression elaborated at the
4644 -- wrong point).
4645
4646 -- We perform this optimization regardless of whether we are in the
4647 -- main unit or in a unit in the context of the main unit, to ensure
4648 -- that the generated tree is the same in both cases, for CodePeer
4649 -- use.
4650
4651 if Is_RTE (Subp, RE_To_Address) then
4652 Rewrite (Call_Node,
4653 Unchecked_Convert_To
4654 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
4655 return;
4656
4657 -- A call to a null procedure is replaced by a null statement, but we
4658 -- are not allowed to ignore possible side effects of the call, so we
4659 -- make sure that actuals are evaluated.
4660 -- We also suppress this optimization for GNATcoverage.
4661
4662 elsif Is_Null_Procedure (Subp)
4663 and then not Opt.Suppress_Control_Flow_Optimizations
4664 then
4665 Actual := First_Actual (Call_Node);
4666 while Present (Actual) loop
4667 Remove_Side_Effects (Actual);
4668 Next_Actual (Actual);
4669 end loop;
4670
4671 Rewrite (Call_Node, Make_Null_Statement (Loc));
4672 return;
4673 end if;
4674
4675 -- Handle inlining. No action needed if the subprogram is not inlined
4676
4677 if not Is_Inlined (Subp) then
4678 null;
4679
4680 -- Front-end inlining of expression functions (performed also when
4681 -- back-end inlining is enabled).
4682
4683 elsif Is_Inlinable_Expression_Function (Subp) then
4684 Rewrite
4685 (Call_Node, New_Copy (Expression_Of_Expression_Function (Subp)));
4686 Analyze (Call_Node);
4687 return;
4688
4689 -- Handle front-end inlining
4690
4691 elsif not Back_End_Inlining then
4692 Inlined_Subprogram : declare
4693 Bod : Node_Id;
4694 Must_Inline : Boolean := False;
4695 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
4696
4697 begin
4698 -- Verify that the body to inline has already been seen, and
4699 -- that if the body is in the current unit the inlining does
4700 -- not occur earlier. This avoids order-of-elaboration problems
4701 -- in the back end.
4702
4703 -- This should be documented in sinfo/einfo ???
4704
4705 if No (Spec)
4706 or else Nkind (Spec) /= N_Subprogram_Declaration
4707 or else No (Body_To_Inline (Spec))
4708 then
4709 Must_Inline := False;
4710
4711 -- If this an inherited function that returns a private type,
4712 -- do not inline if the full view is an unconstrained array,
4713 -- because such calls cannot be inlined.
4714
4715 elsif Present (Orig_Subp)
4716 and then Is_Array_Type (Etype (Orig_Subp))
4717 and then not Is_Constrained (Etype (Orig_Subp))
4718 then
4719 Must_Inline := False;
4720
4721 elsif In_Unfrozen_Instance (Scope (Subp)) then
4722 Must_Inline := False;
4723
4724 else
4725 Bod := Body_To_Inline (Spec);
4726
4727 if (In_Extended_Main_Code_Unit (Call_Node)
4728 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
4729 or else Has_Pragma_Inline_Always (Subp))
4730 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
4731 or else
4732 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
4733 then
4734 Must_Inline := True;
4735
4736 -- If we are compiling a package body that is not the main
4737 -- unit, it must be for inlining/instantiation purposes,
4738 -- in which case we inline the call to insure that the same
4739 -- temporaries are generated when compiling the body by
4740 -- itself. Otherwise link errors can occur.
4741
4742 -- If the function being called is itself in the main unit,
4743 -- we cannot inline, because there is a risk of double
4744 -- elaboration and/or circularity: the inlining can make
4745 -- visible a private entity in the body of the main unit,
4746 -- that gigi will see before its sees its proper definition.
4747
4748 elsif not In_Extended_Main_Code_Unit (Call_Node)
4749 and then In_Package_Body
4750 then
4751 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
4752
4753 -- Inline calls to _postconditions when generating C code
4754
4755 elsif Modify_Tree_For_C
4756 and then In_Same_Extended_Unit (Sloc (Bod), Loc)
4757 and then Chars (Name (Call_Node)) = Name_uPostconditions
4758 then
4759 Must_Inline := True;
4760 end if;
4761 end if;
4762
4763 if Must_Inline then
4764 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4765
4766 else
4767 -- Let the back end handle it
4768
4769 Add_Inlined_Body (Subp, Call_Node);
4770
4771 if Front_End_Inlining
4772 and then Nkind (Spec) = N_Subprogram_Declaration
4773 and then In_Extended_Main_Code_Unit (Call_Node)
4774 and then No (Body_To_Inline (Spec))
4775 and then not Has_Completion (Subp)
4776 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
4777 then
4778 Cannot_Inline
4779 ("cannot inline& (body not seen yet)?",
4780 Call_Node, Subp);
4781 end if;
4782 end if;
4783 end Inlined_Subprogram;
4784
4785 -- Front-end expansion of simple functions returning unconstrained
4786 -- types (see Check_And_Split_Unconstrained_Function). Note that the
4787 -- case of a simple renaming (Body_To_Inline in N_Entity below, see
4788 -- also Build_Renamed_Body) cannot be expanded here because this may
4789 -- give rise to order-of-elaboration issues for the types of the
4790 -- parameters of the subprogram, if any.
4791
4792 elsif Present (Unit_Declaration_Node (Subp))
4793 and then Nkind (Unit_Declaration_Node (Subp)) =
4794 N_Subprogram_Declaration
4795 and then Present (Body_To_Inline (Unit_Declaration_Node (Subp)))
4796 and then
4797 Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) not in
4798 N_Entity
4799 then
4800 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
4801
4802 -- Back-end inlining either if optimization is enabled or the call is
4803 -- required to be inlined.
4804
4805 elsif Optimization_Level > 0
4806 or else Has_Pragma_Inline_Always (Subp)
4807 then
4808 Add_Inlined_Body (Subp, Call_Node);
4809 end if;
4810 end if;
4811
4812 -- Check for protected subprogram. This is either an intra-object call,
4813 -- or a protected function call. Protected procedure calls are rewritten
4814 -- as entry calls and handled accordingly.
4815
4816 -- In Ada 2005, this may be an indirect call to an access parameter that
4817 -- is an access_to_subprogram. In that case the anonymous type has a
4818 -- scope that is a protected operation, but the call is a regular one.
4819 -- In either case do not expand call if subprogram is eliminated.
4820
4821 Scop := Scope (Subp);
4822
4823 if Nkind (Call_Node) /= N_Entry_Call_Statement
4824 and then Is_Protected_Type (Scop)
4825 and then Ekind (Subp) /= E_Subprogram_Type
4826 and then not Is_Eliminated (Subp)
4827 then
4828 -- If the call is an internal one, it is rewritten as a call to the
4829 -- corresponding unprotected subprogram.
4830
4831 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
4832 end if;
4833
4834 -- Functions returning controlled objects need special attention. If
4835 -- the return type is limited, then the context is initialization and
4836 -- different processing applies. If the call is to a protected function,
4837 -- the expansion above will call Expand_Call recursively. Otherwise the
4838 -- function call is transformed into a temporary which obtains the
4839 -- result from the secondary stack.
4840
4841 if Needs_Finalization (Etype (Subp)) then
4842 if not Is_Build_In_Place_Function_Call (Call_Node)
4843 and then
4844 (No (First_Formal (Subp))
4845 or else
4846 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
4847 then
4848 Expand_Ctrl_Function_Call (Call_Node);
4849
4850 -- Build-in-place function calls which appear in anonymous contexts
4851 -- need a transient scope to ensure the proper finalization of the
4852 -- intermediate result after its use.
4853
4854 elsif Is_Build_In_Place_Function_Call (Call_Node)
4855 and then Nkind (Parent (Unqual_Conv (Call_Node))) in
4856 N_Attribute_Reference
4857 | N_Function_Call
4858 | N_Indexed_Component
4859 | N_Object_Renaming_Declaration
4860 | N_Procedure_Call_Statement
4861 | N_Selected_Component
4862 | N_Slice
4863 and then
4864 (Ekind (Current_Scope) /= E_Loop
4865 or else Nkind (Parent (Call_Node)) /= N_Function_Call
4866 or else not Is_Build_In_Place_Function_Call
4867 (Parent (Call_Node)))
4868 then
4869 Establish_Transient_Scope (Call_Node, Manage_Sec_Stack => True);
4870 end if;
4871 end if;
4872 end Expand_Call_Helper;
4873
4874 -------------------------------
4875 -- Expand_Ctrl_Function_Call --
4876 -------------------------------
4877
4878 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4879 function Is_Element_Reference (N : Node_Id) return Boolean;
4880 -- Determine whether node N denotes a reference to an Ada 2012 container
4881 -- element.
4882
4883 --------------------------
4884 -- Is_Element_Reference --
4885 --------------------------
4886
4887 function Is_Element_Reference (N : Node_Id) return Boolean is
4888 Ref : constant Node_Id := Original_Node (N);
4889
4890 begin
4891 -- Analysis marks an element reference by setting the generalized
4892 -- indexing attribute of an indexed component before the component
4893 -- is rewritten into a function call.
4894
4895 return
4896 Nkind (Ref) = N_Indexed_Component
4897 and then Present (Generalized_Indexing (Ref));
4898 end Is_Element_Reference;
4899
4900 -- Start of processing for Expand_Ctrl_Function_Call
4901
4902 begin
4903 -- Optimization, if the returned value (which is on the sec-stack) is
4904 -- returned again, no need to copy/readjust/finalize, we can just pass
4905 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4906 -- attachment is needed
4907
4908 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4909 return;
4910 end if;
4911
4912 -- Resolution is now finished, make sure we don't start analysis again
4913 -- because of the duplication.
4914
4915 Set_Analyzed (N);
4916
4917 -- A function which returns a controlled object uses the secondary
4918 -- stack. Rewrite the call into a temporary which obtains the result of
4919 -- the function using 'reference.
4920
4921 Remove_Side_Effects (N);
4922
4923 -- The side effect removal of the function call produced a temporary.
4924 -- When the context is a case expression, if expression, or expression
4925 -- with actions, the lifetime of the temporary must be extended to match
4926 -- that of the context. Otherwise the function result will be finalized
4927 -- too early and affect the result of the expression. To prevent this
4928 -- unwanted effect, the temporary should not be considered for clean up
4929 -- actions by the general finalization machinery.
4930
4931 -- Exception to this rule are references to Ada 2012 container elements.
4932 -- Such references must be finalized at the end of each iteration of the
4933 -- related quantified expression, otherwise the container will remain
4934 -- busy.
4935
4936 if Nkind (N) = N_Explicit_Dereference
4937 and then Within_Case_Or_If_Expression (N)
4938 and then not Is_Element_Reference (N)
4939 then
4940 Set_Is_Ignored_Transient (Entity (Prefix (N)));
4941 end if;
4942 end Expand_Ctrl_Function_Call;
4943
4944 ----------------------------------------
4945 -- Expand_N_Extended_Return_Statement --
4946 ----------------------------------------
4947
4948 -- If there is a Handled_Statement_Sequence, we rewrite this:
4949
4950 -- return Result : T := <expression> do
4951 -- <handled_seq_of_stms>
4952 -- end return;
4953
4954 -- to be:
4955
4956 -- declare
4957 -- Result : T := <expression>;
4958 -- begin
4959 -- <handled_seq_of_stms>
4960 -- return Result;
4961 -- end;
4962
4963 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
4964
4965 -- return Result : T := <expression>;
4966
4967 -- to be:
4968
4969 -- return <expression>;
4970
4971 -- unless it's build-in-place or there's no <expression>, in which case
4972 -- we generate:
4973
4974 -- declare
4975 -- Result : T := <expression>;
4976 -- begin
4977 -- return Result;
4978 -- end;
4979
4980 -- Note that this case could have been written by the user as an extended
4981 -- return statement, or could have been transformed to this from a simple
4982 -- return statement.
4983
4984 -- That is, we need to have a reified return object if there are statements
4985 -- (which might refer to it) or if we're doing build-in-place (so we can
4986 -- set its address to the final resting place or if there is no expression
4987 -- (in which case default initial values might need to be set)).
4988
4989 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
4990 Loc : constant Source_Ptr := Sloc (N);
4991
4992 function Build_Heap_Or_Pool_Allocator
4993 (Temp_Id : Entity_Id;
4994 Temp_Typ : Entity_Id;
4995 Func_Id : Entity_Id;
4996 Ret_Typ : Entity_Id;
4997 Alloc_Expr : Node_Id) return Node_Id;
4998 -- Create the statements necessary to allocate a return object on the
4999 -- heap or user-defined storage pool. The object may need finalization
5000 -- actions depending on the return type.
5001 --
5002 -- * Controlled case
5003 --
5004 -- if BIPfinalizationmaster = null then
5005 -- Temp_Id := <Alloc_Expr>;
5006 -- else
5007 -- declare
5008 -- type Ptr_Typ is access Ret_Typ;
5009 -- for Ptr_Typ'Storage_Pool use
5010 -- Base_Pool (BIPfinalizationmaster.all).all;
5011 -- Local : Ptr_Typ;
5012 --
5013 -- begin
5014 -- procedure Allocate (...) is
5015 -- begin
5016 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5017 -- end Allocate;
5018 --
5019 -- Local := <Alloc_Expr>;
5020 -- Temp_Id := Temp_Typ (Local);
5021 -- end;
5022 -- end if;
5023 --
5024 -- * Non-controlled case
5025 --
5026 -- Temp_Id := <Alloc_Expr>;
5027 --
5028 -- Temp_Id is the temporary which is used to reference the internally
5029 -- created object in all allocation forms. Temp_Typ is the type of the
5030 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5031 -- type of Func_Id. Alloc_Expr is the actual allocator.
5032
5033 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id;
5034 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5035 -- with parameters:
5036 -- From current activation chain
5037 -- To activation chain passed in by the caller
5038 -- New_Master master passed in by the caller
5039 --
5040 -- Func_Id is the entity of the function where the extended return
5041 -- statement appears.
5042
5043 ----------------------------------
5044 -- Build_Heap_Or_Pool_Allocator --
5045 ----------------------------------
5046
5047 function Build_Heap_Or_Pool_Allocator
5048 (Temp_Id : Entity_Id;
5049 Temp_Typ : Entity_Id;
5050 Func_Id : Entity_Id;
5051 Ret_Typ : Entity_Id;
5052 Alloc_Expr : Node_Id) return Node_Id
5053 is
5054 begin
5055 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5056
5057 -- Processing for objects that require finalization actions
5058
5059 if Needs_Finalization (Ret_Typ) then
5060 declare
5061 Decls : constant List_Id := New_List;
5062 Fin_Mas_Id : constant Entity_Id :=
5063 Build_In_Place_Formal
5064 (Func_Id, BIP_Finalization_Master);
5065 Orig_Expr : constant Node_Id :=
5066 New_Copy_Tree
5067 (Source => Alloc_Expr,
5068 Scopes_In_EWA_OK => True);
5069 Stmts : constant List_Id := New_List;
5070 Desig_Typ : Entity_Id;
5071 Local_Id : Entity_Id;
5072 Pool_Id : Entity_Id;
5073 Ptr_Typ : Entity_Id;
5074
5075 begin
5076 -- Generate:
5077 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5078
5079 Pool_Id := Make_Temporary (Loc, 'P');
5080
5081 Append_To (Decls,
5082 Make_Object_Renaming_Declaration (Loc,
5083 Defining_Identifier => Pool_Id,
5084 Subtype_Mark =>
5085 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
5086 Name =>
5087 Make_Explicit_Dereference (Loc,
5088 Prefix =>
5089 Make_Function_Call (Loc,
5090 Name =>
5091 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
5092 Parameter_Associations => New_List (
5093 Make_Explicit_Dereference (Loc,
5094 Prefix =>
5095 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
5096
5097 -- Create an access type which uses the storage pool of the
5098 -- caller's master. This additional type is necessary because
5099 -- the finalization master cannot be associated with the type
5100 -- of the temporary. Otherwise the secondary stack allocation
5101 -- will fail.
5102
5103 Desig_Typ := Ret_Typ;
5104
5105 -- Ensure that the build-in-place machinery uses a fat pointer
5106 -- when allocating an unconstrained array on the heap. In this
5107 -- case the result object type is a constrained array type even
5108 -- though the function type is unconstrained.
5109
5110 if Ekind (Desig_Typ) = E_Array_Subtype then
5111 Desig_Typ := Base_Type (Desig_Typ);
5112 end if;
5113
5114 -- Generate:
5115 -- type Ptr_Typ is access Desig_Typ;
5116
5117 Ptr_Typ := Make_Temporary (Loc, 'P');
5118
5119 Append_To (Decls,
5120 Make_Full_Type_Declaration (Loc,
5121 Defining_Identifier => Ptr_Typ,
5122 Type_Definition =>
5123 Make_Access_To_Object_Definition (Loc,
5124 Subtype_Indication =>
5125 New_Occurrence_Of (Desig_Typ, Loc))));
5126
5127 -- Perform minor decoration in order to set the master and the
5128 -- storage pool attributes.
5129
5130 Set_Ekind (Ptr_Typ, E_Access_Type);
5131 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5132 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5133
5134 -- Create the temporary, generate:
5135 -- Local_Id : Ptr_Typ;
5136
5137 Local_Id := Make_Temporary (Loc, 'T');
5138
5139 Append_To (Decls,
5140 Make_Object_Declaration (Loc,
5141 Defining_Identifier => Local_Id,
5142 Object_Definition =>
5143 New_Occurrence_Of (Ptr_Typ, Loc)));
5144
5145 -- Allocate the object, generate:
5146 -- Local_Id := <Alloc_Expr>;
5147
5148 Append_To (Stmts,
5149 Make_Assignment_Statement (Loc,
5150 Name => New_Occurrence_Of (Local_Id, Loc),
5151 Expression => Alloc_Expr));
5152
5153 -- Generate:
5154 -- Temp_Id := Temp_Typ (Local_Id);
5155
5156 Append_To (Stmts,
5157 Make_Assignment_Statement (Loc,
5158 Name => New_Occurrence_Of (Temp_Id, Loc),
5159 Expression =>
5160 Unchecked_Convert_To (Temp_Typ,
5161 New_Occurrence_Of (Local_Id, Loc))));
5162
5163 -- Wrap the allocation in a block. This is further conditioned
5164 -- by checking the caller finalization master at runtime. A
5165 -- null value indicates a non-existent master, most likely due
5166 -- to a Finalize_Storage_Only allocation.
5167
5168 -- Generate:
5169 -- if BIPfinalizationmaster = null then
5170 -- Temp_Id := <Orig_Expr>;
5171 -- else
5172 -- declare
5173 -- <Decls>
5174 -- begin
5175 -- <Stmts>
5176 -- end;
5177 -- end if;
5178
5179 return
5180 Make_If_Statement (Loc,
5181 Condition =>
5182 Make_Op_Eq (Loc,
5183 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
5184 Right_Opnd => Make_Null (Loc)),
5185
5186 Then_Statements => New_List (
5187 Make_Assignment_Statement (Loc,
5188 Name => New_Occurrence_Of (Temp_Id, Loc),
5189 Expression => Orig_Expr)),
5190
5191 Else_Statements => New_List (
5192 Make_Block_Statement (Loc,
5193 Declarations => Decls,
5194 Handled_Statement_Sequence =>
5195 Make_Handled_Sequence_Of_Statements (Loc,
5196 Statements => Stmts))));
5197 end;
5198
5199 -- For all other cases, generate:
5200 -- Temp_Id := <Alloc_Expr>;
5201
5202 else
5203 return
5204 Make_Assignment_Statement (Loc,
5205 Name => New_Occurrence_Of (Temp_Id, Loc),
5206 Expression => Alloc_Expr);
5207 end if;
5208 end Build_Heap_Or_Pool_Allocator;
5209
5210 ---------------------------
5211 -- Move_Activation_Chain --
5212 ---------------------------
5213
5214 function Move_Activation_Chain (Func_Id : Entity_Id) return Node_Id is
5215 begin
5216 return
5217 Make_Procedure_Call_Statement (Loc,
5218 Name =>
5219 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5220
5221 Parameter_Associations => New_List (
5222
5223 -- Source chain
5224
5225 Make_Attribute_Reference (Loc,
5226 Prefix => Make_Identifier (Loc, Name_uChain),
5227 Attribute_Name => Name_Unrestricted_Access),
5228
5229 -- Destination chain
5230
5231 New_Occurrence_Of
5232 (Build_In_Place_Formal (Func_Id, BIP_Activation_Chain), Loc),
5233
5234 -- New master
5235
5236 New_Occurrence_Of
5237 (Build_In_Place_Formal (Func_Id, BIP_Task_Master), Loc)));
5238 end Move_Activation_Chain;
5239
5240 -- Local variables
5241
5242 Func_Id : constant Entity_Id :=
5243 Return_Applies_To (Return_Statement_Entity (N));
5244 Is_BIP_Func : constant Boolean :=
5245 Is_Build_In_Place_Function (Func_Id);
5246 Ret_Obj_Id : constant Entity_Id :=
5247 First_Entity (Return_Statement_Entity (N));
5248 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5249 Ret_Typ : constant Entity_Id := Etype (Func_Id);
5250
5251 Exp : Node_Id;
5252 HSS : Node_Id;
5253 Result : Node_Id;
5254 Stmts : List_Id;
5255
5256 Return_Stmt : Node_Id := Empty;
5257 -- Force initialization to facilitate static analysis
5258
5259 -- Start of processing for Expand_N_Extended_Return_Statement
5260
5261 begin
5262 -- Given that functionality of interface thunks is simple (just displace
5263 -- the pointer to the object) they are always handled by means of
5264 -- simple return statements.
5265
5266 pragma Assert (not Is_Thunk (Current_Subprogram));
5267
5268 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5269 Exp := Expression (Ret_Obj_Decl);
5270
5271 -- Assert that if F says "return R : T := G(...) do..."
5272 -- then F and G are both b-i-p, or neither b-i-p.
5273
5274 if Nkind (Exp) = N_Function_Call then
5275 pragma Assert (Ekind (Current_Subprogram) = E_Function);
5276 pragma Assert
5277 (Is_Build_In_Place_Function (Current_Subprogram) =
5278 Is_Build_In_Place_Function_Call (Exp));
5279 null;
5280 end if;
5281
5282 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
5283 -- a check that the level of the return expression's underlying type
5284 -- is not deeper than the level of the master enclosing the function.
5285
5286 -- AI12-043: The check is made immediately after the return object
5287 -- is created.
5288
5289 if Present (Exp) and then Is_Class_Wide_Type (Ret_Typ) then
5290 Apply_CW_Accessibility_Check (Exp, Func_Id);
5291 end if;
5292 else
5293 Exp := Empty;
5294 end if;
5295
5296 HSS := Handled_Statement_Sequence (N);
5297
5298 -- If the returned object needs finalization actions, the function must
5299 -- perform the appropriate cleanup should it fail to return. The state
5300 -- of the function itself is tracked through a flag which is coupled
5301 -- with the scope finalizer. There is one flag per each return object
5302 -- in case of multiple returns.
5303
5304 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5305 declare
5306 Flag_Decl : Node_Id;
5307 Flag_Id : Entity_Id;
5308 Func_Bod : Node_Id;
5309
5310 begin
5311 -- Recover the function body
5312
5313 Func_Bod := Unit_Declaration_Node (Func_Id);
5314
5315 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5316 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5317 end if;
5318
5319 if Nkind (Func_Bod) = N_Function_Specification then
5320 Func_Bod := Parent (Func_Bod); -- one more level for child units
5321 end if;
5322
5323 pragma Assert (Nkind (Func_Bod) = N_Subprogram_Body);
5324
5325 -- Create a flag to track the function state
5326
5327 Flag_Id := Make_Temporary (Loc, 'F');
5328 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5329
5330 -- Insert the flag at the beginning of the function declarations,
5331 -- generate:
5332 -- Fnn : Boolean := False;
5333
5334 Flag_Decl :=
5335 Make_Object_Declaration (Loc,
5336 Defining_Identifier => Flag_Id,
5337 Object_Definition =>
5338 New_Occurrence_Of (Standard_Boolean, Loc),
5339 Expression =>
5340 New_Occurrence_Of (Standard_False, Loc));
5341
5342 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5343 Analyze (Flag_Decl);
5344 end;
5345 end if;
5346
5347 -- Build a simple_return_statement that returns the return object when
5348 -- there is a statement sequence, or no expression, or the analysis of
5349 -- the return object declaration generated extra actions, or the result
5350 -- will be built in place. Note however that we currently do this for
5351 -- all composite cases, even though they are not built in place.
5352
5353 if Present (HSS)
5354 or else No (Exp)
5355 or else List_Length (Return_Object_Declarations (N)) > 1
5356 or else Is_Composite_Type (Ret_Typ)
5357 then
5358 if No (HSS) then
5359 Stmts := New_List;
5360
5361 -- If the extended return has a handled statement sequence, then wrap
5362 -- it in a block and use the block as the first statement.
5363
5364 else
5365 Stmts := New_List (
5366 Make_Block_Statement (Loc,
5367 Declarations => New_List,
5368 Handled_Statement_Sequence => HSS));
5369 end if;
5370
5371 -- If the result type contains tasks, we call Move_Activation_Chain.
5372 -- Later, the cleanup code will call Complete_Master, which will
5373 -- terminate any unactivated tasks belonging to the return statement
5374 -- master. But Move_Activation_Chain updates their master to be that
5375 -- of the caller, so they will not be terminated unless the return
5376 -- statement completes unsuccessfully due to exception, abort, goto,
5377 -- or exit. As a formality, we test whether the function requires the
5378 -- result to be built in place, though that's necessarily true for
5379 -- the case of result types with task parts.
5380
5381 if Is_BIP_Func and then Has_Task (Ret_Typ) then
5382
5383 -- The return expression is an aggregate for a complex type which
5384 -- contains tasks. This particular case is left unexpanded since
5385 -- the regular expansion would insert all temporaries and
5386 -- initialization code in the wrong block.
5387
5388 if Nkind (Exp) = N_Aggregate then
5389 Expand_N_Aggregate (Exp);
5390 end if;
5391
5392 -- Do not move the activation chain if the return object does not
5393 -- contain tasks.
5394
5395 if Has_Task (Etype (Ret_Obj_Id)) then
5396 Append_To (Stmts, Move_Activation_Chain (Func_Id));
5397 end if;
5398 end if;
5399
5400 -- Update the state of the function right before the object is
5401 -- returned.
5402
5403 if Is_BIP_Func and then Needs_Finalization (Etype (Ret_Obj_Id)) then
5404 declare
5405 Flag_Id : constant Entity_Id :=
5406 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5407
5408 begin
5409 -- Generate:
5410 -- Fnn := True;
5411
5412 Append_To (Stmts,
5413 Make_Assignment_Statement (Loc,
5414 Name => New_Occurrence_Of (Flag_Id, Loc),
5415 Expression => New_Occurrence_Of (Standard_True, Loc)));
5416 end;
5417 end if;
5418
5419 -- Build a simple_return_statement that returns the return object
5420
5421 Return_Stmt :=
5422 Make_Simple_Return_Statement (Loc,
5423 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5424 Append_To (Stmts, Return_Stmt);
5425
5426 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5427 end if;
5428
5429 -- Case where we build a return statement block
5430
5431 if Present (HSS) then
5432 Result :=
5433 Make_Block_Statement (Loc,
5434 Declarations => Return_Object_Declarations (N),
5435 Handled_Statement_Sequence => HSS);
5436
5437 -- We set the entity of the new block statement to be that of the
5438 -- return statement. This is necessary so that various fields, such
5439 -- as Finalization_Chain_Entity carry over from the return statement
5440 -- to the block. Note that this block is unusual, in that its entity
5441 -- is an E_Return_Statement rather than an E_Block.
5442
5443 Set_Identifier
5444 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5445
5446 -- If the object decl was already rewritten as a renaming, then we
5447 -- don't want to do the object allocation and transformation of
5448 -- the return object declaration to a renaming. This case occurs
5449 -- when the return object is initialized by a call to another
5450 -- build-in-place function, and that function is responsible for
5451 -- the allocation of the return object.
5452
5453 if Is_BIP_Func
5454 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5455 then
5456 pragma Assert
5457 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5458 and then
5459
5460 -- It is a regular BIP object declaration
5461
5462 (Is_Build_In_Place_Function_Call
5463 (Expression (Original_Node (Ret_Obj_Decl)))
5464
5465 -- It is a BIP object declaration that displaces the pointer
5466 -- to the object to reference a converted interface type.
5467
5468 or else
5469 Present (Unqual_BIP_Iface_Function_Call
5470 (Expression (Original_Node (Ret_Obj_Decl))))));
5471
5472 -- Return the build-in-place result by reference
5473
5474 Set_By_Ref (Return_Stmt);
5475
5476 elsif Is_BIP_Func then
5477
5478 -- Locate the implicit access parameter associated with the
5479 -- caller-supplied return object and convert the return
5480 -- statement's return object declaration to a renaming of a
5481 -- dereference of the access parameter. If the return object's
5482 -- declaration includes an expression that has not already been
5483 -- expanded as separate assignments, then add an assignment
5484 -- statement to ensure the return object gets initialized.
5485
5486 -- declare
5487 -- Result : T [:= <expression>];
5488 -- begin
5489 -- ...
5490
5491 -- is converted to
5492
5493 -- declare
5494 -- Result : T renames FuncRA.all;
5495 -- [Result := <expression;]
5496 -- begin
5497 -- ...
5498
5499 declare
5500 Ret_Obj_Expr : constant Node_Id := Expression (Ret_Obj_Decl);
5501 Ret_Obj_Typ : constant Entity_Id := Etype (Ret_Obj_Id);
5502
5503 Init_Assignment : Node_Id := Empty;
5504 Obj_Acc_Formal : Entity_Id;
5505 Obj_Acc_Deref : Node_Id;
5506 Obj_Alloc_Formal : Entity_Id;
5507
5508 begin
5509 -- Build-in-place results must be returned by reference
5510
5511 Set_By_Ref (Return_Stmt);
5512
5513 -- Retrieve the implicit access parameter passed by the caller
5514
5515 Obj_Acc_Formal :=
5516 Build_In_Place_Formal (Func_Id, BIP_Object_Access);
5517
5518 -- If the return object's declaration includes an expression
5519 -- and the declaration isn't marked as No_Initialization, then
5520 -- we need to generate an assignment to the object and insert
5521 -- it after the declaration before rewriting it as a renaming
5522 -- (otherwise we'll lose the initialization). The case where
5523 -- the result type is an interface (or class-wide interface)
5524 -- is also excluded because the context of the function call
5525 -- must be unconstrained, so the initialization will always
5526 -- be done as part of an allocator evaluation (storage pool
5527 -- or secondary stack), never to a constrained target object
5528 -- passed in by the caller. Besides the assignment being
5529 -- unneeded in this case, it avoids problems with trying to
5530 -- generate a dispatching assignment when the return expression
5531 -- is a nonlimited descendant of a limited interface (the
5532 -- interface has no assignment operation).
5533
5534 if Present (Ret_Obj_Expr)
5535 and then not No_Initialization (Ret_Obj_Decl)
5536 and then not Is_Interface (Ret_Obj_Typ)
5537 then
5538 Init_Assignment :=
5539 Make_Assignment_Statement (Loc,
5540 Name => New_Occurrence_Of (Ret_Obj_Id, Loc),
5541 Expression =>
5542 New_Copy_Tree
5543 (Source => Ret_Obj_Expr,
5544 Scopes_In_EWA_OK => True));
5545
5546 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5547 Set_Assignment_OK (Name (Init_Assignment));
5548 Set_No_Ctrl_Actions (Init_Assignment);
5549
5550 Set_Parent (Name (Init_Assignment), Init_Assignment);
5551 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5552
5553 Set_Expression (Ret_Obj_Decl, Empty);
5554
5555 if Is_Class_Wide_Type (Etype (Ret_Obj_Id))
5556 and then not Is_Class_Wide_Type
5557 (Etype (Expression (Init_Assignment)))
5558 then
5559 Rewrite (Expression (Init_Assignment),
5560 Make_Type_Conversion (Loc,
5561 Subtype_Mark =>
5562 New_Occurrence_Of (Etype (Ret_Obj_Id), Loc),
5563 Expression =>
5564 Relocate_Node (Expression (Init_Assignment))));
5565 end if;
5566
5567 -- In the case of functions where the calling context can
5568 -- determine the form of allocation needed, initialization
5569 -- is done with each part of the if statement that handles
5570 -- the different forms of allocation (this is true for
5571 -- unconstrained, tagged, and controlled result subtypes).
5572
5573 if not Needs_BIP_Alloc_Form (Func_Id) then
5574 Insert_After (Ret_Obj_Decl, Init_Assignment);
5575 end if;
5576 end if;
5577
5578 -- When the function's subtype is unconstrained, a run-time
5579 -- test may be needed to decide the form of allocation to use
5580 -- for the return object. The function has an implicit formal
5581 -- parameter indicating this. If the BIP_Alloc_Form formal has
5582 -- the value one, then the caller has passed access to an
5583 -- existing object for use as the return object. If the value
5584 -- is two, then the return object must be allocated on the
5585 -- secondary stack. Otherwise, the object must be allocated in
5586 -- a storage pool. We generate an if statement to test the
5587 -- implicit allocation formal and initialize a local access
5588 -- value appropriately, creating allocators in the secondary
5589 -- stack and global heap cases. The special formal also exists
5590 -- and must be tested when the function has a tagged result,
5591 -- even when the result subtype is constrained, because in
5592 -- general such functions can be called in dispatching contexts
5593 -- and must be handled similarly to functions with a class-wide
5594 -- result.
5595
5596 if Needs_BIP_Alloc_Form (Func_Id) then
5597 Obj_Alloc_Formal :=
5598 Build_In_Place_Formal (Func_Id, BIP_Alloc_Form);
5599
5600 declare
5601 Pool_Id : constant Entity_Id :=
5602 Make_Temporary (Loc, 'P');
5603 Alloc_Obj_Id : Entity_Id;
5604 Alloc_Obj_Decl : Node_Id;
5605 Alloc_If_Stmt : Node_Id;
5606 Guard_Except : Node_Id;
5607 Heap_Allocator : Node_Id;
5608 Pool_Decl : Node_Id;
5609 Pool_Allocator : Node_Id;
5610 Ptr_Type_Decl : Node_Id;
5611 Ref_Type : Entity_Id;
5612 SS_Allocator : Node_Id;
5613
5614 begin
5615 -- Create an access type designating the function's
5616 -- result subtype.
5617
5618 Ref_Type := Make_Temporary (Loc, 'A');
5619
5620 Ptr_Type_Decl :=
5621 Make_Full_Type_Declaration (Loc,
5622 Defining_Identifier => Ref_Type,
5623 Type_Definition =>
5624 Make_Access_To_Object_Definition (Loc,
5625 All_Present => True,
5626 Subtype_Indication =>
5627 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5628
5629 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5630
5631 -- Create an access object that will be initialized to an
5632 -- access value denoting the return object, either coming
5633 -- from an implicit access value passed in by the caller
5634 -- or from the result of an allocator.
5635
5636 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5637 Set_Etype (Alloc_Obj_Id, Ref_Type);
5638
5639 Alloc_Obj_Decl :=
5640 Make_Object_Declaration (Loc,
5641 Defining_Identifier => Alloc_Obj_Id,
5642 Object_Definition =>
5643 New_Occurrence_Of (Ref_Type, Loc));
5644
5645 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5646
5647 -- Create allocators for both the secondary stack and
5648 -- global heap. If there's an initialization expression,
5649 -- then create these as initialized allocators.
5650
5651 if Present (Ret_Obj_Expr)
5652 and then not No_Initialization (Ret_Obj_Decl)
5653 then
5654 -- Always use the type of the expression for the
5655 -- qualified expression, rather than the result type.
5656 -- In general we cannot always use the result type
5657 -- for the allocator, because the expression might be
5658 -- of a specific type, such as in the case of an
5659 -- aggregate or even a nonlimited object when the
5660 -- result type is a limited class-wide interface type.
5661
5662 Heap_Allocator :=
5663 Make_Allocator (Loc,
5664 Expression =>
5665 Make_Qualified_Expression (Loc,
5666 Subtype_Mark =>
5667 New_Occurrence_Of
5668 (Etype (Ret_Obj_Expr), Loc),
5669 Expression =>
5670 New_Copy_Tree
5671 (Source => Ret_Obj_Expr,
5672 Scopes_In_EWA_OK => True)));
5673
5674 else
5675 -- If the function returns a class-wide type we cannot
5676 -- use the return type for the allocator. Instead we
5677 -- use the type of the expression, which must be an
5678 -- aggregate of a definite type.
5679
5680 if Is_Class_Wide_Type (Ret_Obj_Typ) then
5681 Heap_Allocator :=
5682 Make_Allocator (Loc,
5683 Expression =>
5684 New_Occurrence_Of
5685 (Etype (Ret_Obj_Expr), Loc));
5686 else
5687 Heap_Allocator :=
5688 Make_Allocator (Loc,
5689 Expression =>
5690 New_Occurrence_Of (Ret_Obj_Typ, Loc));
5691 end if;
5692
5693 -- If the object requires default initialization then
5694 -- that will happen later following the elaboration of
5695 -- the object renaming. If we don't turn it off here
5696 -- then the object will be default initialized twice.
5697
5698 Set_No_Initialization (Heap_Allocator);
5699 end if;
5700
5701 -- Set the flag indicating that the allocator came from
5702 -- a build-in-place return statement, so we can avoid
5703 -- adjusting the allocated object. Note that this flag
5704 -- will be inherited by the copies made below.
5705
5706 Set_Alloc_For_BIP_Return (Heap_Allocator);
5707
5708 -- The Pool_Allocator is just like the Heap_Allocator,
5709 -- except we set Storage_Pool and Procedure_To_Call so
5710 -- it will use the user-defined storage pool.
5711
5712 Pool_Allocator :=
5713 New_Copy_Tree
5714 (Source => Heap_Allocator,
5715 Scopes_In_EWA_OK => True);
5716
5717 pragma Assert (Alloc_For_BIP_Return (Pool_Allocator));
5718
5719 -- Do not generate the renaming of the build-in-place
5720 -- pool parameter on ZFP because the parameter is not
5721 -- created in the first place.
5722
5723 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
5724 Pool_Decl :=
5725 Make_Object_Renaming_Declaration (Loc,
5726 Defining_Identifier => Pool_Id,
5727 Subtype_Mark =>
5728 New_Occurrence_Of
5729 (RTE (RE_Root_Storage_Pool), Loc),
5730 Name =>
5731 Make_Explicit_Dereference (Loc,
5732 New_Occurrence_Of
5733 (Build_In_Place_Formal
5734 (Func_Id, BIP_Storage_Pool), Loc)));
5735 Set_Storage_Pool (Pool_Allocator, Pool_Id);
5736 Set_Procedure_To_Call
5737 (Pool_Allocator, RTE (RE_Allocate_Any));
5738 else
5739 Pool_Decl := Make_Null_Statement (Loc);
5740 end if;
5741
5742 -- If the No_Allocators restriction is active, then only
5743 -- an allocator for secondary stack allocation is needed.
5744 -- It's OK for such allocators to have Comes_From_Source
5745 -- set to False, because gigi knows not to flag them as
5746 -- being a violation of No_Implicit_Heap_Allocations.
5747
5748 if Restriction_Active (No_Allocators) then
5749 SS_Allocator := Heap_Allocator;
5750 Heap_Allocator := Make_Null (Loc);
5751 Pool_Allocator := Make_Null (Loc);
5752
5753 -- Otherwise the heap and pool allocators may be needed,
5754 -- so we make another allocator for secondary stack
5755 -- allocation.
5756
5757 else
5758 SS_Allocator :=
5759 New_Copy_Tree
5760 (Source => Heap_Allocator,
5761 Scopes_In_EWA_OK => True);
5762
5763 pragma Assert (Alloc_For_BIP_Return (SS_Allocator));
5764
5765 -- The heap and pool allocators are marked as
5766 -- Comes_From_Source since they correspond to an
5767 -- explicit user-written allocator (that is, it will
5768 -- only be executed on behalf of callers that call the
5769 -- function as initialization for such an allocator).
5770 -- Prevents errors when No_Implicit_Heap_Allocations
5771 -- is in force.
5772
5773 Set_Comes_From_Source (Heap_Allocator, True);
5774 Set_Comes_From_Source (Pool_Allocator, True);
5775 end if;
5776
5777 -- The allocator is returned on the secondary stack
5778
5779 Check_Restriction (No_Secondary_Stack, N);
5780 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
5781 Set_Procedure_To_Call
5782 (SS_Allocator, RTE (RE_SS_Allocate));
5783
5784 -- The allocator is returned on the secondary stack,
5785 -- so indicate that the function return, as well as
5786 -- all blocks that encloses the allocator, must not
5787 -- release it. The flags must be set now because
5788 -- the decision to use the secondary stack is done
5789 -- very late in the course of expanding the return
5790 -- statement, past the point where these flags are
5791 -- normally set.
5792
5793 Set_Uses_Sec_Stack (Func_Id);
5794 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
5795 Set_Sec_Stack_Needed_For_Return
5796 (Return_Statement_Entity (N));
5797 Set_Enclosing_Sec_Stack_Return (N);
5798
5799 -- Guard against poor expansion on the caller side by
5800 -- using a raise statement to catch out-of-range values
5801 -- of formal parameter BIP_Alloc_Form.
5802
5803 if Exceptions_OK then
5804 Guard_Except :=
5805 Make_Raise_Program_Error (Loc,
5806 Reason => PE_Build_In_Place_Mismatch);
5807 else
5808 Guard_Except := Make_Null_Statement (Loc);
5809 end if;
5810
5811 -- Create an if statement to test the BIP_Alloc_Form
5812 -- formal and initialize the access object to either the
5813 -- BIP_Object_Access formal (BIP_Alloc_Form =
5814 -- Caller_Allocation), the result of allocating the
5815 -- object in the secondary stack (BIP_Alloc_Form =
5816 -- Secondary_Stack), or else an allocator to create the
5817 -- return object in the heap or user-defined pool
5818 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
5819
5820 -- ??? An unchecked type conversion must be made in the
5821 -- case of assigning the access object formal to the
5822 -- local access object, because a normal conversion would
5823 -- be illegal in some cases (such as converting access-
5824 -- to-unconstrained to access-to-constrained), but the
5825 -- the unchecked conversion will presumably fail to work
5826 -- right in just such cases. It's not clear at all how to
5827 -- handle this. ???
5828
5829 Alloc_If_Stmt :=
5830 Make_If_Statement (Loc,
5831 Condition =>
5832 Make_Op_Eq (Loc,
5833 Left_Opnd =>
5834 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5835 Right_Opnd =>
5836 Make_Integer_Literal (Loc,
5837 UI_From_Int (BIP_Allocation_Form'Pos
5838 (Caller_Allocation)))),
5839
5840 Then_Statements => New_List (
5841 Make_Assignment_Statement (Loc,
5842 Name =>
5843 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5844 Expression =>
5845 Make_Unchecked_Type_Conversion (Loc,
5846 Subtype_Mark =>
5847 New_Occurrence_Of (Ref_Type, Loc),
5848 Expression =>
5849 New_Occurrence_Of (Obj_Acc_Formal, Loc)))),
5850
5851 Elsif_Parts => New_List (
5852 Make_Elsif_Part (Loc,
5853 Condition =>
5854 Make_Op_Eq (Loc,
5855 Left_Opnd =>
5856 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5857 Right_Opnd =>
5858 Make_Integer_Literal (Loc,
5859 UI_From_Int (BIP_Allocation_Form'Pos
5860 (Secondary_Stack)))),
5861
5862 Then_Statements => New_List (
5863 Make_Assignment_Statement (Loc,
5864 Name =>
5865 New_Occurrence_Of (Alloc_Obj_Id, Loc),
5866 Expression => SS_Allocator))),
5867
5868 Make_Elsif_Part (Loc,
5869 Condition =>
5870 Make_Op_Eq (Loc,
5871 Left_Opnd =>
5872 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5873 Right_Opnd =>
5874 Make_Integer_Literal (Loc,
5875 UI_From_Int (BIP_Allocation_Form'Pos
5876 (Global_Heap)))),
5877
5878 Then_Statements => New_List (
5879 Build_Heap_Or_Pool_Allocator
5880 (Temp_Id => Alloc_Obj_Id,
5881 Temp_Typ => Ref_Type,
5882 Func_Id => Func_Id,
5883 Ret_Typ => Ret_Obj_Typ,
5884 Alloc_Expr => Heap_Allocator))),
5885
5886 -- ???If all is well, we can put the following
5887 -- 'elsif' in the 'else', but this is a useful
5888 -- self-check in case caller and callee don't agree
5889 -- on whether BIPAlloc and so on should be passed.
5890
5891 Make_Elsif_Part (Loc,
5892 Condition =>
5893 Make_Op_Eq (Loc,
5894 Left_Opnd =>
5895 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
5896 Right_Opnd =>
5897 Make_Integer_Literal (Loc,
5898 UI_From_Int (BIP_Allocation_Form'Pos
5899 (User_Storage_Pool)))),
5900
5901 Then_Statements => New_List (
5902 Pool_Decl,
5903 Build_Heap_Or_Pool_Allocator
5904 (Temp_Id => Alloc_Obj_Id,
5905 Temp_Typ => Ref_Type,
5906 Func_Id => Func_Id,
5907 Ret_Typ => Ret_Obj_Typ,
5908 Alloc_Expr => Pool_Allocator)))),
5909
5910 -- Raise Program_Error if it's none of the above;
5911 -- this is a compiler bug.
5912
5913 Else_Statements => New_List (Guard_Except));
5914
5915 -- If a separate initialization assignment was created
5916 -- earlier, append that following the assignment of the
5917 -- implicit access formal to the access object, to ensure
5918 -- that the return object is initialized in that case. In
5919 -- this situation, the target of the assignment must be
5920 -- rewritten to denote a dereference of the access to the
5921 -- return object passed in by the caller.
5922
5923 if Present (Init_Assignment) then
5924 Rewrite (Name (Init_Assignment),
5925 Make_Explicit_Dereference (Loc,
5926 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
5927 pragma Assert
5928 (Assignment_OK
5929 (Original_Node (Name (Init_Assignment))));
5930 Set_Assignment_OK (Name (Init_Assignment));
5931
5932 Set_Etype (Name (Init_Assignment), Etype (Ret_Obj_Id));
5933
5934 Append_To
5935 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
5936 end if;
5937
5938 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
5939
5940 -- Remember the local access object for use in the
5941 -- dereference of the renaming created below.
5942
5943 Obj_Acc_Formal := Alloc_Obj_Id;
5944 end;
5945
5946 -- When the function's subtype is unconstrained and a run-time
5947 -- test is not needed, we nevertheless need to build the return
5948 -- using the function's result subtype.
5949
5950 elsif not Is_Constrained (Underlying_Type (Etype (Func_Id)))
5951 then
5952 declare
5953 Alloc_Obj_Id : Entity_Id;
5954 Alloc_Obj_Decl : Node_Id;
5955 Ptr_Type_Decl : Node_Id;
5956 Ref_Type : Entity_Id;
5957
5958 begin
5959 -- Create an access type designating the function's
5960 -- result subtype.
5961
5962 Ref_Type := Make_Temporary (Loc, 'A');
5963
5964 Ptr_Type_Decl :=
5965 Make_Full_Type_Declaration (Loc,
5966 Defining_Identifier => Ref_Type,
5967 Type_Definition =>
5968 Make_Access_To_Object_Definition (Loc,
5969 All_Present => True,
5970 Subtype_Indication =>
5971 New_Occurrence_Of (Ret_Obj_Typ, Loc)));
5972
5973 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5974
5975 -- Create an access object initialized to the conversion
5976 -- of the implicit access value passed in by the caller.
5977
5978 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5979 Set_Etype (Alloc_Obj_Id, Ref_Type);
5980
5981 -- See the ??? comment a few lines above about the use of
5982 -- an unchecked conversion here.
5983
5984 Alloc_Obj_Decl :=
5985 Make_Object_Declaration (Loc,
5986 Defining_Identifier => Alloc_Obj_Id,
5987 Object_Definition =>
5988 New_Occurrence_Of (Ref_Type, Loc),
5989 Expression =>
5990 Make_Unchecked_Type_Conversion (Loc,
5991 Subtype_Mark =>
5992 New_Occurrence_Of (Ref_Type, Loc),
5993 Expression =>
5994 New_Occurrence_Of (Obj_Acc_Formal, Loc)));
5995
5996 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5997
5998 -- Remember the local access object for use in the
5999 -- dereference of the renaming created below.
6000
6001 Obj_Acc_Formal := Alloc_Obj_Id;
6002 end;
6003 end if;
6004
6005 -- Replace the return object declaration with a renaming of a
6006 -- dereference of the access value designating the return
6007 -- object.
6008
6009 Obj_Acc_Deref :=
6010 Make_Explicit_Dereference (Loc,
6011 Prefix => New_Occurrence_Of (Obj_Acc_Formal, Loc));
6012
6013 Rewrite (Ret_Obj_Decl,
6014 Make_Object_Renaming_Declaration (Loc,
6015 Defining_Identifier => Ret_Obj_Id,
6016 Access_Definition => Empty,
6017 Subtype_Mark => New_Occurrence_Of (Ret_Obj_Typ, Loc),
6018 Name => Obj_Acc_Deref));
6019
6020 Set_Renamed_Object (Ret_Obj_Id, Obj_Acc_Deref);
6021 end;
6022 end if;
6023
6024 -- Case where we do not need to build a block. But we're about to drop
6025 -- Return_Object_Declarations on the floor, so assert that it contains
6026 -- only the return object declaration.
6027
6028 else pragma Assert (List_Length (Return_Object_Declarations (N)) = 1);
6029
6030 -- Build simple_return_statement that returns the expression directly
6031
6032 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6033 Result := Return_Stmt;
6034 end if;
6035
6036 -- Set the flag to prevent infinite recursion
6037
6038 Set_Comes_From_Extended_Return_Statement (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_Simple_Return_Statement --
6071 --------------------------------------
6072
6073 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6074 begin
6075 -- Defend against previous errors (i.e. the return statement calls a
6076 -- function that is not available in configurable runtime).
6077
6078 if Present (Expression (N))
6079 and then Nkind (Expression (N)) = N_Empty
6080 then
6081 Check_Error_Detected;
6082 return;
6083 end if;
6084
6085 -- Distinguish the function and non-function cases:
6086
6087 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6088 when E_Function
6089 | E_Generic_Function
6090 =>
6091 Expand_Simple_Function_Return (N);
6092
6093 when E_Entry
6094 | E_Entry_Family
6095 | E_Generic_Procedure
6096 | E_Procedure
6097 | E_Return_Statement
6098 =>
6099 Expand_Non_Function_Return (N);
6100
6101 when others =>
6102 raise Program_Error;
6103 end case;
6104
6105 exception
6106 when RE_Not_Available =>
6107 return;
6108 end Expand_N_Simple_Return_Statement;
6109
6110 ------------------------------
6111 -- Expand_N_Subprogram_Body --
6112 ------------------------------
6113
6114 -- Add dummy push/pop label nodes at start and end to clear any local
6115 -- exception indications if local-exception-to-goto optimization is active.
6116
6117 -- Add return statement if last statement in body is not a return statement
6118 -- (this makes things easier on Gigi which does not want to have to handle
6119 -- a missing return).
6120
6121 -- Add call to Activate_Tasks if body is a task activator
6122
6123 -- Deal with possible detection of infinite recursion
6124
6125 -- Eliminate body completely if convention stubbed
6126
6127 -- Encode entity names within body, since we will not need to reference
6128 -- these entities any longer in the front end.
6129
6130 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6131
6132 -- Reset Pure indication if any parameter has root type System.Address
6133 -- or has any parameters of limited types, where limited means that the
6134 -- run-time view is limited (i.e. the full type is limited).
6135
6136 -- Wrap thread body
6137
6138 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6139 Body_Id : constant Entity_Id := Defining_Entity (N);
6140 HSS : constant Node_Id := Handled_Statement_Sequence (N);
6141 Loc : constant Source_Ptr := Sloc (N);
6142
6143 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id);
6144 -- Append a return statement to the statement sequence Stmts if the last
6145 -- statement is not already a return or a goto statement. Note that the
6146 -- latter test is not critical, it does not matter if we add a few extra
6147 -- returns, since they get eliminated anyway later on. Spec_Id denotes
6148 -- the corresponding spec of the subprogram body.
6149
6150 ----------------
6151 -- Add_Return --
6152 ----------------
6153
6154 procedure Add_Return (Spec_Id : Entity_Id; Stmts : List_Id) is
6155 Last_Stmt : Node_Id;
6156 Loc : Source_Ptr;
6157 Stmt : Node_Id;
6158
6159 begin
6160 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6161 -- not relevant in this context since they are not executable.
6162
6163 Last_Stmt := Last (Stmts);
6164 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
6165 Prev (Last_Stmt);
6166 end loop;
6167
6168 -- Now insert return unless last statement is a transfer
6169
6170 if not Is_Transfer (Last_Stmt) then
6171
6172 -- The source location for the return is the end label of the
6173 -- procedure if present. Otherwise use the sloc of the last
6174 -- statement in the list. If the list comes from a generated
6175 -- exception handler and we are not debugging generated code,
6176 -- all the statements within the handler are made invisible
6177 -- to the debugger.
6178
6179 if Nkind (Parent (Stmts)) = N_Exception_Handler
6180 and then not Comes_From_Source (Parent (Stmts))
6181 then
6182 Loc := Sloc (Last_Stmt);
6183 elsif Present (End_Label (HSS)) then
6184 Loc := Sloc (End_Label (HSS));
6185 else
6186 Loc := Sloc (Last_Stmt);
6187 end if;
6188
6189 -- Append return statement, and set analyzed manually. We can't
6190 -- call Analyze on this return since the scope is wrong.
6191
6192 -- Note: it almost works to push the scope and then do the Analyze
6193 -- call, but something goes wrong in some weird cases and it is
6194 -- not worth worrying about ???
6195
6196 Stmt := Make_Simple_Return_Statement (Loc);
6197
6198 -- The return statement is handled properly, and the call to the
6199 -- postcondition, inserted below, does not require information
6200 -- from the body either. However, that call is analyzed in the
6201 -- enclosing scope, and an elaboration check might improperly be
6202 -- added to it. A guard in Sem_Elab is needed to prevent that
6203 -- spurious check, see Check_Elab_Call.
6204
6205 Append_To (Stmts, Stmt);
6206 Set_Analyzed (Stmt);
6207
6208 -- Call the _Postconditions procedure if the related subprogram
6209 -- has contract assertions that need to be verified on exit.
6210
6211 -- Also, mark the successful return to signal that postconditions
6212 -- need to be evaluated when finalization occurs by setting
6213 -- Return_Success_For_Postcond to be True.
6214
6215 if Ekind (Spec_Id) = E_Procedure
6216 and then Present (Postconditions_Proc (Spec_Id))
6217 then
6218 -- Generate:
6219 --
6220 -- Return_Success_For_Postcond := True;
6221 -- if Postcond_Enabled then
6222 -- _postconditions;
6223 -- end if;
6224
6225 Insert_Action (Stmt,
6226 Make_Assignment_Statement (Loc,
6227 Name =>
6228 New_Occurrence_Of
6229 (Get_Return_Success_For_Postcond (Spec_Id), Loc),
6230 Expression => New_Occurrence_Of (Standard_True, Loc)));
6231
6232 -- Wrap the call to _postconditions within a test of the
6233 -- Postcond_Enabled flag to delay postcondition evaluation
6234 -- until after finalization when required.
6235
6236 Insert_Action (Stmt,
6237 Make_If_Statement (Loc,
6238 Condition =>
6239 New_Occurrence_Of (Get_Postcond_Enabled (Spec_Id), Loc),
6240 Then_Statements => New_List (
6241 Make_Procedure_Call_Statement (Loc,
6242 Name =>
6243 New_Occurrence_Of
6244 (Postconditions_Proc (Spec_Id), Loc)))));
6245 end if;
6246
6247 -- Ada 2020 (AI12-0279): append the call to 'Yield unless this is
6248 -- a generic subprogram (since in such case it will be added to
6249 -- the instantiations).
6250
6251 if Has_Yield_Aspect (Spec_Id)
6252 and then Ekind (Spec_Id) /= E_Generic_Procedure
6253 and then RTE_Available (RE_Yield)
6254 then
6255 Insert_Action (Stmt,
6256 Make_Procedure_Call_Statement (Loc,
6257 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6258 end if;
6259 end if;
6260 end Add_Return;
6261
6262 -- Local variables
6263
6264 Except_H : Node_Id;
6265 L : List_Id;
6266 Spec_Id : Entity_Id;
6267
6268 -- Start of processing for Expand_N_Subprogram_Body
6269
6270 begin
6271 if Present (Corresponding_Spec (N)) then
6272 Spec_Id := Corresponding_Spec (N);
6273 else
6274 Spec_Id := Body_Id;
6275 end if;
6276
6277 -- If this is a Pure function which has any parameters whose root type
6278 -- is System.Address, reset the Pure indication.
6279 -- This check is also performed when the subprogram is frozen, but we
6280 -- repeat it on the body so that the indication is consistent, and so
6281 -- it applies as well to bodies without separate specifications.
6282
6283 if Is_Pure (Spec_Id)
6284 and then Is_Subprogram (Spec_Id)
6285 and then not Has_Pragma_Pure_Function (Spec_Id)
6286 then
6287 Check_Function_With_Address_Parameter (Spec_Id);
6288
6289 if Spec_Id /= Body_Id then
6290 Set_Is_Pure (Body_Id, Is_Pure (Spec_Id));
6291 end if;
6292 end if;
6293
6294 -- Set L to either the list of declarations if present, or to the list
6295 -- of statements if no declarations are present. This is used to insert
6296 -- new stuff at the start.
6297
6298 if Is_Non_Empty_List (Declarations (N)) then
6299 L := Declarations (N);
6300 else
6301 L := Statements (HSS);
6302 end if;
6303
6304 -- If local-exception-to-goto optimization active, insert dummy push
6305 -- statements at start, and dummy pop statements at end, but inhibit
6306 -- this if we have No_Exception_Handlers, since they are useless and
6307 -- interfere with analysis, e.g. by CodePeer. We also don't need these
6308 -- if we're unnesting subprograms because the only purpose of these
6309 -- nodes is to ensure we don't set a label in one subprogram and branch
6310 -- to it in another.
6311
6312 if (Debug_Flag_Dot_G
6313 or else Restriction_Active (No_Exception_Propagation))
6314 and then not Restriction_Active (No_Exception_Handlers)
6315 and then not CodePeer_Mode
6316 and then not Unnest_Subprogram_Mode
6317 and then Is_Non_Empty_List (L)
6318 then
6319 declare
6320 FS : constant Node_Id := First (L);
6321 FL : constant Source_Ptr := Sloc (FS);
6322 LS : Node_Id;
6323 LL : Source_Ptr;
6324
6325 begin
6326 -- LS points to either last statement, if statements are present
6327 -- or to the last declaration if there are no statements present.
6328 -- It is the node after which the pop's are generated.
6329
6330 if Is_Non_Empty_List (Statements (HSS)) then
6331 LS := Last (Statements (HSS));
6332 else
6333 LS := Last (L);
6334 end if;
6335
6336 LL := Sloc (LS);
6337
6338 Insert_List_Before_And_Analyze (FS, New_List (
6339 Make_Push_Constraint_Error_Label (FL),
6340 Make_Push_Program_Error_Label (FL),
6341 Make_Push_Storage_Error_Label (FL)));
6342
6343 Insert_List_After_And_Analyze (LS, New_List (
6344 Make_Pop_Constraint_Error_Label (LL),
6345 Make_Pop_Program_Error_Label (LL),
6346 Make_Pop_Storage_Error_Label (LL)));
6347 end;
6348 end if;
6349
6350 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6351
6352 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
6353 declare
6354 F : Entity_Id;
6355 A : Node_Id;
6356
6357 begin
6358 -- Loop through formals
6359
6360 F := First_Formal (Spec_Id);
6361 while Present (F) loop
6362 if Is_Scalar_Type (Etype (F))
6363 and then Ekind (F) = E_Out_Parameter
6364 then
6365 Check_Restriction (No_Default_Initialization, F);
6366
6367 -- Insert the initialization. We turn off validity checks
6368 -- for this assignment, since we do not want any check on
6369 -- the initial value itself (which may well be invalid).
6370 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6371
6372 A :=
6373 Make_Assignment_Statement (Loc,
6374 Name => New_Occurrence_Of (F, Loc),
6375 Expression => Get_Simple_Init_Val (Etype (F), N));
6376 Set_Suppress_Assignment_Checks (A);
6377
6378 Insert_Before_And_Analyze (First (L),
6379 A, Suppress => Validity_Check);
6380 end if;
6381
6382 Next_Formal (F);
6383 end loop;
6384 end;
6385 end if;
6386
6387 -- Clear out statement list for stubbed procedure
6388
6389 if Present (Corresponding_Spec (N)) then
6390 Set_Elaboration_Flag (N, Spec_Id);
6391
6392 if Convention (Spec_Id) = Convention_Stubbed
6393 or else Is_Eliminated (Spec_Id)
6394 then
6395 Set_Declarations (N, Empty_List);
6396 Set_Handled_Statement_Sequence (N,
6397 Make_Handled_Sequence_Of_Statements (Loc,
6398 Statements => New_List (Make_Null_Statement (Loc))));
6399
6400 return;
6401 end if;
6402 end if;
6403
6404 -- Create a set of discriminals for the next protected subprogram body
6405
6406 if Is_List_Member (N)
6407 and then Present (Parent (List_Containing (N)))
6408 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6409 and then Present (Next_Protected_Operation (N))
6410 then
6411 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
6412 end if;
6413
6414 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6415 -- subprograms with no specs are not frozen.
6416
6417 declare
6418 Typ : constant Entity_Id := Etype (Spec_Id);
6419 Utyp : constant Entity_Id := Underlying_Type (Typ);
6420
6421 begin
6422 if Is_Limited_View (Typ) then
6423 Set_Returns_By_Ref (Spec_Id);
6424
6425 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6426 Set_Returns_By_Ref (Spec_Id);
6427 end if;
6428 end;
6429
6430 -- For a procedure, we add a return for all possible syntactic ends of
6431 -- the subprogram.
6432
6433 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure then
6434 Add_Return (Spec_Id, Statements (HSS));
6435
6436 if Present (Exception_Handlers (HSS)) then
6437 Except_H := First_Non_Pragma (Exception_Handlers (HSS));
6438 while Present (Except_H) loop
6439 Add_Return (Spec_Id, Statements (Except_H));
6440 Next_Non_Pragma (Except_H);
6441 end loop;
6442 end if;
6443
6444 -- For a function, we must deal with the case where there is at least
6445 -- one missing return. What we do is to wrap the entire body of the
6446 -- function in a block:
6447
6448 -- begin
6449 -- ...
6450 -- end;
6451
6452 -- becomes
6453
6454 -- begin
6455 -- begin
6456 -- ...
6457 -- end;
6458
6459 -- raise Program_Error;
6460 -- end;
6461
6462 -- This approach is necessary because the raise must be signalled to the
6463 -- caller, not handled by any local handler (RM 6.4(11)).
6464
6465 -- Note: we do not need to analyze the constructed sequence here, since
6466 -- it has no handler, and an attempt to analyze the handled statement
6467 -- sequence twice is risky in various ways (e.g. the issue of expanding
6468 -- cleanup actions twice).
6469
6470 elsif Has_Missing_Return (Spec_Id) then
6471 declare
6472 Hloc : constant Source_Ptr := Sloc (HSS);
6473 Blok : constant Node_Id :=
6474 Make_Block_Statement (Hloc,
6475 Handled_Statement_Sequence => HSS);
6476 Rais : constant Node_Id :=
6477 Make_Raise_Program_Error (Hloc,
6478 Reason => PE_Missing_Return);
6479
6480 begin
6481 Set_Handled_Statement_Sequence (N,
6482 Make_Handled_Sequence_Of_Statements (Hloc,
6483 Statements => New_List (Blok, Rais)));
6484
6485 Push_Scope (Spec_Id);
6486 Analyze (Blok);
6487 Analyze (Rais);
6488 Pop_Scope;
6489 end;
6490 end if;
6491
6492 -- If subprogram contains a parameterless recursive call, then we may
6493 -- have an infinite recursion, so see if we can generate code to check
6494 -- for this possibility if storage checks are not suppressed.
6495
6496 if Ekind (Spec_Id) = E_Procedure
6497 and then Has_Recursive_Call (Spec_Id)
6498 and then not Storage_Checks_Suppressed (Spec_Id)
6499 then
6500 Detect_Infinite_Recursion (N, Spec_Id);
6501 end if;
6502
6503 -- Set to encode entity names in package body before gigi is called
6504
6505 Qualify_Entity_Names (N);
6506
6507 -- If the body belongs to a nonabstract library-level source primitive
6508 -- of a tagged type, install an elaboration check which ensures that a
6509 -- dispatching call targeting the primitive will not execute the body
6510 -- without it being previously elaborated.
6511
6512 Install_Primitive_Elaboration_Check (N);
6513 end Expand_N_Subprogram_Body;
6514
6515 -----------------------------------
6516 -- Expand_N_Subprogram_Body_Stub --
6517 -----------------------------------
6518
6519 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6520 Bod : Node_Id;
6521
6522 begin
6523 if Present (Corresponding_Body (N)) then
6524 Bod := Unit_Declaration_Node (Corresponding_Body (N));
6525
6526 -- The body may have been expanded already when it is analyzed
6527 -- through the subunit node. Do no expand again: it interferes
6528 -- with the construction of unnesting tables when generating C.
6529
6530 if not Analyzed (Bod) then
6531 Expand_N_Subprogram_Body (Bod);
6532 end if;
6533
6534 -- Add full qualification to entities that may be created late
6535 -- during unnesting.
6536
6537 Qualify_Entity_Names (N);
6538 end if;
6539 end Expand_N_Subprogram_Body_Stub;
6540
6541 -------------------------------------
6542 -- Expand_N_Subprogram_Declaration --
6543 -------------------------------------
6544
6545 -- If the declaration appears within a protected body, it is a private
6546 -- operation of the protected type. We must create the corresponding
6547 -- protected subprogram an associated formals. For a normal protected
6548 -- operation, this is done when expanding the protected type declaration.
6549
6550 -- If the declaration is for a null procedure, emit null body
6551
6552 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6553 Loc : constant Source_Ptr := Sloc (N);
6554 Subp : constant Entity_Id := Defining_Entity (N);
6555
6556 -- Local variables
6557
6558 Scop : constant Entity_Id := Scope (Subp);
6559 Prot_Bod : Node_Id;
6560 Prot_Decl : Node_Id;
6561 Prot_Id : Entity_Id;
6562 Typ : Entity_Id;
6563
6564 begin
6565 -- Deal with case of protected subprogram. Do not generate protected
6566 -- operation if operation is flagged as eliminated.
6567
6568 if Is_List_Member (N)
6569 and then Present (Parent (List_Containing (N)))
6570 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6571 and then Is_Protected_Type (Scop)
6572 then
6573 if No (Protected_Body_Subprogram (Subp))
6574 and then not Is_Eliminated (Subp)
6575 then
6576 Prot_Decl :=
6577 Make_Subprogram_Declaration (Loc,
6578 Specification =>
6579 Build_Protected_Sub_Specification
6580 (N, Scop, Unprotected_Mode));
6581
6582 -- The protected subprogram is declared outside of the protected
6583 -- body. Given that the body has frozen all entities so far, we
6584 -- analyze the subprogram and perform freezing actions explicitly.
6585 -- including the generation of an explicit freeze node, to ensure
6586 -- that gigi has the proper order of elaboration.
6587 -- If the body is a subunit, the insertion point is before the
6588 -- stub in the parent.
6589
6590 Prot_Bod := Parent (List_Containing (N));
6591
6592 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6593 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6594 end if;
6595
6596 Insert_Before (Prot_Bod, Prot_Decl);
6597 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6598 Set_Has_Delayed_Freeze (Prot_Id);
6599
6600 Push_Scope (Scope (Scop));
6601 Analyze (Prot_Decl);
6602 Freeze_Before (N, Prot_Id);
6603 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6604 Pop_Scope;
6605 end if;
6606
6607 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6608 -- cases this is superfluous because calls to it will be automatically
6609 -- inlined, but we definitely need the body if preconditions for the
6610 -- procedure are present, or if performing coverage analysis.
6611
6612 elsif Nkind (Specification (N)) = N_Procedure_Specification
6613 and then Null_Present (Specification (N))
6614 then
6615 declare
6616 Bod : constant Node_Id := Body_To_Inline (N);
6617
6618 begin
6619 Set_Has_Completion (Subp, False);
6620 Append_Freeze_Action (Subp, Bod);
6621
6622 -- The body now contains raise statements, so calls to it will
6623 -- not be inlined.
6624
6625 Set_Is_Inlined (Subp, False);
6626 end;
6627 end if;
6628
6629 -- When generating C code, transform a function that returns a
6630 -- constrained array type into a procedure with an out parameter
6631 -- that carries the return value.
6632
6633 -- We skip this transformation for unchecked conversions, since they
6634 -- are not needed by the C generator (and this also produces cleaner
6635 -- output).
6636
6637 Typ := Get_Fullest_View (Etype (Subp));
6638
6639 if Transform_Function_Array
6640 and then Nkind (Specification (N)) = N_Function_Specification
6641 and then Is_Array_Type (Typ)
6642 and then Is_Constrained (Typ)
6643 and then not Is_Unchecked_Conversion_Instance (Subp)
6644 then
6645 Build_Procedure_Form (N);
6646 end if;
6647 end Expand_N_Subprogram_Declaration;
6648
6649 --------------------------------
6650 -- Expand_Non_Function_Return --
6651 --------------------------------
6652
6653 procedure Expand_Non_Function_Return (N : Node_Id) is
6654 pragma Assert (No (Expression (N)));
6655
6656 Loc : constant Source_Ptr := Sloc (N);
6657 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
6658 Kind : constant Entity_Kind := Ekind (Scope_Id);
6659 Call : Node_Id;
6660 Acc_Stat : Node_Id;
6661 Goto_Stat : Node_Id;
6662 Lab_Node : Node_Id;
6663
6664 begin
6665 -- Call the _Postconditions procedure if the related subprogram has
6666 -- contract assertions that need to be verified on exit.
6667
6668 -- Also, mark the successful return to signal that postconditions need
6669 -- to be evaluated when finalization occurs.
6670
6671 if Ekind (Scope_Id) in E_Entry | E_Entry_Family | E_Procedure
6672 and then Present (Postconditions_Proc (Scope_Id))
6673 then
6674 -- Generate:
6675 --
6676 -- Return_Success_For_Postcond := True;
6677 -- if Postcond_Enabled then
6678 -- _postconditions;
6679 -- end if;
6680
6681 Insert_Action (N,
6682 Make_Assignment_Statement (Loc,
6683 Name =>
6684 New_Occurrence_Of
6685 (Get_Return_Success_For_Postcond (Scope_Id), Loc),
6686 Expression => New_Occurrence_Of (Standard_True, Loc)));
6687
6688 -- Wrap the call to _postconditions within a test of the
6689 -- Postcond_Enabled flag to delay postcondition evaluation until
6690 -- after finalization when required.
6691
6692 Insert_Action (N,
6693 Make_If_Statement (Loc,
6694 Condition =>
6695 New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc),
6696 Then_Statements => New_List (
6697 Make_Procedure_Call_Statement (Loc,
6698 Name =>
6699 New_Occurrence_Of
6700 (Postconditions_Proc (Scope_Id), Loc)))));
6701 end if;
6702
6703 -- Ada 2020 (AI12-0279)
6704
6705 if Has_Yield_Aspect (Scope_Id)
6706 and then RTE_Available (RE_Yield)
6707 then
6708 Insert_Action (N,
6709 Make_Procedure_Call_Statement (Loc,
6710 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6711 end if;
6712
6713 -- If it is a return from a procedure do no extra steps
6714
6715 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6716 return;
6717
6718 -- If it is a nested return within an extended one, replace it with a
6719 -- return of the previously declared return object.
6720
6721 elsif Kind = E_Return_Statement then
6722 Rewrite (N,
6723 Make_Simple_Return_Statement (Loc,
6724 Expression =>
6725 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6726 Set_Comes_From_Extended_Return_Statement (N);
6727 Set_Return_Statement_Entity (N, Scope_Id);
6728 Expand_Simple_Function_Return (N);
6729 return;
6730 end if;
6731
6732 pragma Assert (Is_Entry (Scope_Id));
6733
6734 -- Look at the enclosing block to see whether the return is from an
6735 -- accept statement or an entry body.
6736
6737 for J in reverse 0 .. Scope_Stack.Last loop
6738 Scope_Id := Scope_Stack.Table (J).Entity;
6739 exit when Is_Concurrent_Type (Scope_Id);
6740 end loop;
6741
6742 -- If it is a return from accept statement it is expanded as call to
6743 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6744
6745 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6746 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6747
6748 if Is_Task_Type (Scope_Id) then
6749
6750 Call :=
6751 Make_Procedure_Call_Statement (Loc,
6752 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6753 Insert_Before (N, Call);
6754 -- why not insert actions here???
6755 Analyze (Call);
6756
6757 Acc_Stat := Parent (N);
6758 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6759 Acc_Stat := Parent (Acc_Stat);
6760 end loop;
6761
6762 Lab_Node := Last (Statements
6763 (Handled_Statement_Sequence (Acc_Stat)));
6764
6765 Goto_Stat := Make_Goto_Statement (Loc,
6766 Name => New_Occurrence_Of
6767 (Entity (Identifier (Lab_Node)), Loc));
6768
6769 Set_Analyzed (Goto_Stat);
6770
6771 Rewrite (N, Goto_Stat);
6772 Analyze (N);
6773
6774 -- If it is a return from an entry body, put a Complete_Entry_Body call
6775 -- in front of the return.
6776
6777 elsif Is_Protected_Type (Scope_Id) then
6778 Call :=
6779 Make_Procedure_Call_Statement (Loc,
6780 Name =>
6781 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6782 Parameter_Associations => New_List (
6783 Make_Attribute_Reference (Loc,
6784 Prefix =>
6785 New_Occurrence_Of
6786 (Find_Protection_Object (Current_Scope), Loc),
6787 Attribute_Name => Name_Unchecked_Access)));
6788
6789 Insert_Before (N, Call);
6790 Analyze (Call);
6791 end if;
6792 end Expand_Non_Function_Return;
6793
6794 ---------------------------------------
6795 -- Expand_Protected_Object_Reference --
6796 ---------------------------------------
6797
6798 function Expand_Protected_Object_Reference
6799 (N : Node_Id;
6800 Scop : Entity_Id) return Node_Id
6801 is
6802 Loc : constant Source_Ptr := Sloc (N);
6803 Corr : Entity_Id;
6804 Rec : Node_Id;
6805 Param : Entity_Id;
6806 Proc : Entity_Id;
6807
6808 begin
6809 Rec := Make_Identifier (Loc, Name_uObject);
6810 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6811
6812 -- Find enclosing protected operation, and retrieve its first parameter,
6813 -- which denotes the enclosing protected object. If the enclosing
6814 -- operation is an entry, we are immediately within the protected body,
6815 -- and we can retrieve the object from the service entries procedure. A
6816 -- barrier function has the same signature as an entry. A barrier
6817 -- function is compiled within the protected object, but unlike
6818 -- protected operations its never needs locks, so that its protected
6819 -- body subprogram points to itself.
6820
6821 Proc := Current_Scope;
6822 while Present (Proc)
6823 and then Scope (Proc) /= Scop
6824 loop
6825 Proc := Scope (Proc);
6826 end loop;
6827
6828 Corr := Protected_Body_Subprogram (Proc);
6829
6830 if No (Corr) then
6831
6832 -- Previous error left expansion incomplete.
6833 -- Nothing to do on this call.
6834
6835 return Empty;
6836 end if;
6837
6838 Param :=
6839 Defining_Identifier
6840 (First (Parameter_Specifications (Parent (Corr))));
6841
6842 if Is_Subprogram (Proc) and then Proc /= Corr then
6843
6844 -- Protected function or procedure
6845
6846 Set_Entity (Rec, Param);
6847
6848 -- Rec is a reference to an entity which will not be in scope when
6849 -- the call is reanalyzed, and needs no further analysis.
6850
6851 Set_Analyzed (Rec);
6852
6853 else
6854 -- Entry or barrier function for entry body. The first parameter of
6855 -- the entry body procedure is pointer to the object. We create a
6856 -- local variable of the proper type, duplicating what is done to
6857 -- define _object later on.
6858
6859 declare
6860 Decls : List_Id;
6861 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
6862
6863 begin
6864 Decls := New_List (
6865 Make_Full_Type_Declaration (Loc,
6866 Defining_Identifier => Obj_Ptr,
6867 Type_Definition =>
6868 Make_Access_To_Object_Definition (Loc,
6869 Subtype_Indication =>
6870 New_Occurrence_Of
6871 (Corresponding_Record_Type (Scop), Loc))));
6872
6873 Insert_Actions (N, Decls);
6874 Freeze_Before (N, Obj_Ptr);
6875
6876 Rec :=
6877 Make_Explicit_Dereference (Loc,
6878 Prefix =>
6879 Unchecked_Convert_To (Obj_Ptr,
6880 New_Occurrence_Of (Param, Loc)));
6881
6882 -- Analyze new actual. Other actuals in calls are already analyzed
6883 -- and the list of actuals is not reanalyzed after rewriting.
6884
6885 Set_Parent (Rec, N);
6886 Analyze (Rec);
6887 end;
6888 end if;
6889
6890 return Rec;
6891 end Expand_Protected_Object_Reference;
6892
6893 --------------------------------------
6894 -- Expand_Protected_Subprogram_Call --
6895 --------------------------------------
6896
6897 procedure Expand_Protected_Subprogram_Call
6898 (N : Node_Id;
6899 Subp : Entity_Id;
6900 Scop : Entity_Id)
6901 is
6902 Rec : Node_Id;
6903
6904 procedure Expand_Internal_Init_Call;
6905 -- A call to an operation of the type may occur in the initialization
6906 -- of a private component. In that case the prefix of the call is an
6907 -- entity name and the call is treated as internal even though it
6908 -- appears in code outside of the protected type.
6909
6910 procedure Freeze_Called_Function;
6911 -- If it is a function call it can appear in elaboration code and
6912 -- the called entity must be frozen before the call. This must be
6913 -- done before the call is expanded, as the expansion may rewrite it
6914 -- to something other than a call (e.g. a temporary initialized in a
6915 -- transient block).
6916
6917 -------------------------------
6918 -- Expand_Internal_Init_Call --
6919 -------------------------------
6920
6921 procedure Expand_Internal_Init_Call is
6922 begin
6923 -- If the context is a protected object (rather than a protected
6924 -- type) the call itself is bound to raise program_error because
6925 -- the protected body will not have been elaborated yet. This is
6926 -- diagnosed subsequently in Sem_Elab.
6927
6928 Freeze_Called_Function;
6929
6930 -- The target of the internal call is the first formal of the
6931 -- enclosing initialization procedure.
6932
6933 Rec := New_Occurrence_Of (First_Formal (Current_Scope), Sloc (N));
6934 Build_Protected_Subprogram_Call (N,
6935 Name => Name (N),
6936 Rec => Rec,
6937 External => False);
6938 Analyze (N);
6939 Resolve (N, Etype (Subp));
6940 end Expand_Internal_Init_Call;
6941
6942 ----------------------------
6943 -- Freeze_Called_Function --
6944 ----------------------------
6945
6946 procedure Freeze_Called_Function is
6947 begin
6948 if Ekind (Subp) = E_Function then
6949 Freeze_Expression (Name (N));
6950 end if;
6951 end Freeze_Called_Function;
6952
6953 -- Start of processing for Expand_Protected_Subprogram_Call
6954
6955 begin
6956 -- If the protected object is not an enclosing scope, this is an inter-
6957 -- object function call. Inter-object procedure calls are expanded by
6958 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
6959 -- subprogram being called is in the protected body being compiled, and
6960 -- if the protected object in the call is statically the enclosing type.
6961 -- The object may be a component of some other data structure, in which
6962 -- case this must be handled as an inter-object call.
6963
6964 if not In_Open_Scopes (Scop)
6965 or else Is_Entry_Wrapper (Current_Scope)
6966 or else not Is_Entity_Name (Name (N))
6967 then
6968 if Nkind (Name (N)) = N_Selected_Component then
6969 Rec := Prefix (Name (N));
6970
6971 elsif Nkind (Name (N)) = N_Indexed_Component then
6972 Rec := Prefix (Prefix (Name (N)));
6973
6974 -- If this is a call within an entry wrapper, it appears within a
6975 -- precondition that calls another primitive of the synchronized
6976 -- type. The target object of the call is the first actual on the
6977 -- wrapper. Note that this is an external call, because the wrapper
6978 -- is called outside of the synchronized object. This means that
6979 -- an entry call to an entry with preconditions involves two
6980 -- synchronized operations.
6981
6982 elsif Ekind (Current_Scope) = E_Procedure
6983 and then Is_Entry_Wrapper (Current_Scope)
6984 then
6985 Rec := New_Occurrence_Of (First_Entity (Current_Scope), Sloc (N));
6986
6987 -- A default parameter of a protected operation may be a call to
6988 -- a protected function of the type. This appears as an internal
6989 -- call in the profile of the operation, but if the context is an
6990 -- external call we must convert the call into an external one,
6991 -- using the protected object that is the target, so that:
6992
6993 -- Prot.P (F)
6994 -- is transformed into
6995 -- Prot.P (Prot.F)
6996
6997 elsif Nkind (Parent (N)) = N_Procedure_Call_Statement
6998 and then Nkind (Name (Parent (N))) = N_Selected_Component
6999 and then Is_Protected_Type (Etype (Prefix (Name (Parent (N)))))
7000 and then Is_Entity_Name (Name (N))
7001 and then Scope (Entity (Name (N))) =
7002 Etype (Prefix (Name (Parent (N))))
7003 then
7004 Rewrite (Name (N),
7005 Make_Selected_Component (Sloc (N),
7006 Prefix => New_Copy_Tree (Prefix (Name (Parent (N)))),
7007 Selector_Name => Relocate_Node (Name (N))));
7008
7009 Analyze_And_Resolve (N);
7010 return;
7011
7012 else
7013 -- If the context is the initialization procedure for a protected
7014 -- type, the call is legal because the called entity must be a
7015 -- function of that enclosing type, and this is treated as an
7016 -- internal call.
7017
7018 pragma Assert
7019 (Is_Entity_Name (Name (N)) and then Inside_Init_Proc);
7020
7021 Expand_Internal_Init_Call;
7022 return;
7023 end if;
7024
7025 Freeze_Called_Function;
7026 Build_Protected_Subprogram_Call (N,
7027 Name => New_Occurrence_Of (Subp, Sloc (N)),
7028 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7029 External => True);
7030
7031 else
7032 Rec := Expand_Protected_Object_Reference (N, Scop);
7033
7034 if No (Rec) then
7035 return;
7036 end if;
7037
7038 Freeze_Called_Function;
7039 Build_Protected_Subprogram_Call (N,
7040 Name => Name (N),
7041 Rec => Rec,
7042 External => False);
7043 end if;
7044
7045 -- Analyze and resolve the new call. The actuals have already been
7046 -- resolved, but expansion of a function call will add extra actuals
7047 -- if needed. Analysis of a procedure call already includes resolution.
7048
7049 Analyze (N);
7050
7051 if Ekind (Subp) = E_Function then
7052 Resolve (N, Etype (Subp));
7053 end if;
7054 end Expand_Protected_Subprogram_Call;
7055
7056 -----------------------------------
7057 -- Expand_Simple_Function_Return --
7058 -----------------------------------
7059
7060 -- The "simple" comes from the syntax rule simple_return_statement. The
7061 -- semantics are not at all simple.
7062
7063 procedure Expand_Simple_Function_Return (N : Node_Id) is
7064 Loc : constant Source_Ptr := Sloc (N);
7065
7066 Scope_Id : constant Entity_Id :=
7067 Return_Applies_To (Return_Statement_Entity (N));
7068 -- The function we are returning from
7069
7070 R_Type : constant Entity_Id := Etype (Scope_Id);
7071 -- The result type of the function
7072
7073 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7074
7075 Exp : Node_Id := Expression (N);
7076 pragma Assert (Present (Exp));
7077
7078 Exp_Is_Function_Call : constant Boolean :=
7079 Nkind (Exp) = N_Function_Call
7080 or else (Nkind (Exp) = N_Explicit_Dereference
7081 and then Is_Entity_Name (Prefix (Exp))
7082 and then Ekind (Entity (Prefix (Exp))) = E_Constant
7083 and then Is_Related_To_Func_Return (Entity (Prefix (Exp))));
7084
7085 Exp_Typ : constant Entity_Id := Etype (Exp);
7086 -- The type of the expression (not necessarily the same as R_Type)
7087
7088 Subtype_Ind : Node_Id;
7089 -- If the result type of the function is class-wide and the expression
7090 -- has a specific type, then we use the expression's type as the type of
7091 -- the return object. In cases where the expression is an aggregate that
7092 -- is built in place, this avoids the need for an expensive conversion
7093 -- of the return object to the specific type on assignments to the
7094 -- individual components.
7095
7096 -- Start of processing for Expand_Simple_Function_Return
7097
7098 begin
7099 if Is_Class_Wide_Type (R_Type)
7100 and then not Is_Class_Wide_Type (Exp_Typ)
7101 and then Nkind (Exp) /= N_Type_Conversion
7102 then
7103 Subtype_Ind := New_Occurrence_Of (Exp_Typ, Loc);
7104 else
7105 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7106
7107 -- If the result type is class-wide and the expression is a view
7108 -- conversion, the conversion plays no role in the expansion because
7109 -- it does not modify the tag of the object. Remove the conversion
7110 -- altogether to prevent tag overwriting.
7111
7112 if Is_Class_Wide_Type (R_Type)
7113 and then not Is_Class_Wide_Type (Exp_Typ)
7114 and then Nkind (Exp) = N_Type_Conversion
7115 then
7116 Exp := Expression (Exp);
7117 end if;
7118 end if;
7119
7120 -- Assert that if F says "return G(...);"
7121 -- then F and G are both b-i-p, or neither b-i-p.
7122
7123 if Nkind (Exp) = N_Function_Call then
7124 pragma Assert (Ekind (Scope_Id) = E_Function);
7125 pragma Assert
7126 (Is_Build_In_Place_Function (Scope_Id) =
7127 Is_Build_In_Place_Function_Call (Exp));
7128 null;
7129 end if;
7130
7131 -- For the case of a simple return that does not come from an
7132 -- extended return, in the case of build-in-place, we rewrite
7133 -- "return <expression>;" to be:
7134
7135 -- return _anon_ : <return_subtype> := <expression>
7136
7137 -- The expansion produced by Expand_N_Extended_Return_Statement will
7138 -- contain simple return statements (for example, a block containing
7139 -- simple return of the return object), which brings us back here with
7140 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7141 -- checking for a simple return that does not come from an extended
7142 -- return is to avoid this infinite recursion.
7143
7144 -- The reason for this design is that for Ada 2005 limited returns, we
7145 -- need to reify the return object, so we can build it "in place", and
7146 -- we need a block statement to hang finalization and tasking stuff.
7147
7148 -- ??? In order to avoid disruption, we avoid translating to extended
7149 -- return except in the cases where we really need to (Ada 2005 for
7150 -- inherently limited). We might prefer to do this translation in all
7151 -- cases (except perhaps for the case of Ada 95 inherently limited),
7152 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7153 -- code. This would also allow us to do the build-in-place optimization
7154 -- for efficiency even in cases where it is semantically not required.
7155
7156 -- As before, we check the type of the return expression rather than the
7157 -- return type of the function, because the latter may be a limited
7158 -- class-wide interface type, which is not a limited type, even though
7159 -- the type of the expression may be.
7160
7161 pragma Assert
7162 (Comes_From_Extended_Return_Statement (N)
7163 or else not Is_Build_In_Place_Function_Call (Exp)
7164 or else Is_Build_In_Place_Function (Scope_Id));
7165
7166 if not Comes_From_Extended_Return_Statement (N)
7167 and then Is_Build_In_Place_Function (Scope_Id)
7168 and then not Debug_Flag_Dot_L
7169
7170 -- The functionality of interface thunks is simple and it is always
7171 -- handled by means of simple return statements. This leaves their
7172 -- expansion simple and clean.
7173
7174 and then not Is_Thunk (Scope_Id)
7175 then
7176 declare
7177 Return_Object_Entity : constant Entity_Id :=
7178 Make_Temporary (Loc, 'R', Exp);
7179
7180 Obj_Decl : constant Node_Id :=
7181 Make_Object_Declaration (Loc,
7182 Defining_Identifier => Return_Object_Entity,
7183 Object_Definition => Subtype_Ind,
7184 Expression => Exp);
7185
7186 Ext : constant Node_Id :=
7187 Make_Extended_Return_Statement (Loc,
7188 Return_Object_Declarations => New_List (Obj_Decl));
7189 -- Do not perform this high-level optimization if the result type
7190 -- is an interface because the "this" pointer must be displaced.
7191
7192 begin
7193 Rewrite (N, Ext);
7194 Analyze (N);
7195 return;
7196 end;
7197 end if;
7198
7199 -- Here we have a simple return statement that is part of the expansion
7200 -- of an extended return statement (either written by the user, or
7201 -- generated by the above code).
7202
7203 -- Always normalize C/Fortran boolean result. This is not always needed,
7204 -- but it seems a good idea to minimize the passing around of non-
7205 -- normalized values, and in any case this handles the processing of
7206 -- barrier functions for protected types, which turn the condition into
7207 -- a return statement.
7208
7209 if Is_Boolean_Type (Exp_Typ) and then Nonzero_Is_True (Exp_Typ) then
7210 Adjust_Condition (Exp);
7211 Adjust_Result_Type (Exp, Exp_Typ);
7212 end if;
7213
7214 -- Do validity check if enabled for returns
7215
7216 if Validity_Checks_On and then Validity_Check_Returns then
7217 Ensure_Valid (Exp);
7218 end if;
7219
7220 -- Check the result expression of a scalar function against the subtype
7221 -- of the function by inserting a conversion. This conversion must
7222 -- eventually be performed for other classes of types, but for now it's
7223 -- only done for scalars ???
7224
7225 if Is_Scalar_Type (Exp_Typ) and then Exp_Typ /= R_Type then
7226 Rewrite (Exp, Convert_To (R_Type, Exp));
7227
7228 -- The expression is resolved to ensure that the conversion gets
7229 -- expanded to generate a possible constraint check.
7230
7231 Analyze_And_Resolve (Exp, R_Type);
7232 end if;
7233
7234 -- Deal with returning variable length objects and controlled types
7235
7236 -- Nothing to do if we are returning by reference, or this is not a
7237 -- type that requires special processing (indicated by the fact that
7238 -- it requires a cleanup scope for the secondary stack case).
7239
7240 if Is_Build_In_Place_Function (Scope_Id)
7241 or else Is_Limited_Interface (Exp_Typ)
7242 then
7243 null;
7244
7245 -- No copy needed for thunks returning interface type objects since
7246 -- the object is returned by reference and the maximum functionality
7247 -- required is just to displace the pointer.
7248
7249 elsif Is_Thunk (Scope_Id) and then Is_Interface (Exp_Typ) then
7250 null;
7251
7252 -- If the call is within a thunk and the type is a limited view, the
7253 -- backend will eventually see the non-limited view of the type.
7254
7255 elsif Is_Thunk (Scope_Id) and then Is_Incomplete_Type (Exp_Typ) then
7256 return;
7257
7258 -- A return statement from an ignored Ghost function does not use the
7259 -- secondary stack (or any other one).
7260
7261 elsif not Requires_Transient_Scope (R_Type)
7262 or else Is_Ignored_Ghost_Entity (Scope_Id)
7263 then
7264
7265 -- Mutable records with variable-length components are not returned
7266 -- on the sec-stack, so we need to make sure that the back end will
7267 -- only copy back the size of the actual value, and not the maximum
7268 -- size. We create an actual subtype for this purpose. However we
7269 -- need not do it if the expression is a function call since this
7270 -- will be done in the called function and doing it here too would
7271 -- cause a temporary with maximum size to be created.
7272
7273 declare
7274 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ));
7275 Decl : Node_Id;
7276 Ent : Entity_Id;
7277 begin
7278 if not Exp_Is_Function_Call
7279 and then Has_Discriminants (Ubt)
7280 and then not Is_Constrained (Ubt)
7281 and then not Has_Unchecked_Union (Ubt)
7282 then
7283 Decl := Build_Actual_Subtype (Ubt, Exp);
7284 Ent := Defining_Identifier (Decl);
7285 Insert_Action (Exp, Decl);
7286 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7287 Analyze_And_Resolve (Exp);
7288 end if;
7289 end;
7290
7291 -- Here if secondary stack is used
7292
7293 else
7294 -- Prevent the reclamation of the secondary stack by all enclosing
7295 -- blocks and loops as well as the related function; otherwise the
7296 -- result would be reclaimed too early.
7297
7298 Set_Enclosing_Sec_Stack_Return (N);
7299
7300 -- Optimize the case where the result is a function call. In this
7301 -- case the result is already on the secondary stack and no further
7302 -- processing is required except to set the By_Ref flag to ensure
7303 -- that gigi does not attempt an extra unnecessary copy. (Actually
7304 -- not just unnecessary but wrong in the case of a controlled type,
7305 -- where gigi does not know how to do a copy.)
7306
7307 if Requires_Transient_Scope (Exp_Typ)
7308 and then Exp_Is_Function_Call
7309 then
7310 Set_By_Ref (N);
7311
7312 -- Remove side effects from the expression now so that other parts
7313 -- of the expander do not have to reanalyze this node without this
7314 -- optimization
7315
7316 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7317
7318 -- Ada 2005 (AI-251): If the type of the returned object is
7319 -- an interface then add an implicit type conversion to force
7320 -- displacement of the "this" pointer.
7321
7322 if Is_Interface (R_Type) then
7323 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7324 end if;
7325
7326 Analyze_And_Resolve (Exp, R_Type);
7327
7328 -- For controlled types, do the allocation on the secondary stack
7329 -- manually in order to call adjust at the right time:
7330
7331 -- type Anon1 is access R_Type;
7332 -- for Anon1'Storage_pool use ss_pool;
7333 -- Anon2 : anon1 := new R_Type'(expr);
7334 -- return Anon2.all;
7335
7336 -- We do the same for classwide types that are not potentially
7337 -- controlled (by the virtue of restriction No_Finalization) because
7338 -- gigi is not able to properly allocate class-wide types.
7339
7340 elsif CW_Or_Has_Controlled_Part (Utyp) then
7341 declare
7342 Loc : constant Source_Ptr := Sloc (N);
7343 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7344 Alloc_Node : Node_Id;
7345 Temp : Entity_Id;
7346
7347 begin
7348 Set_Ekind (Acc_Typ, E_Access_Type);
7349
7350 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7351
7352 -- This is an allocator for the secondary stack, and it's fine
7353 -- to have Comes_From_Source set False on it, as gigi knows not
7354 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7355
7356 Alloc_Node :=
7357 Make_Allocator (Loc,
7358 Expression =>
7359 Make_Qualified_Expression (Loc,
7360 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
7361 Expression => Relocate_Node (Exp)));
7362
7363 -- We do not want discriminant checks on the declaration,
7364 -- given that it gets its value from the allocator.
7365
7366 Set_No_Initialization (Alloc_Node);
7367
7368 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7369
7370 Insert_List_Before_And_Analyze (N, New_List (
7371 Make_Full_Type_Declaration (Loc,
7372 Defining_Identifier => Acc_Typ,
7373 Type_Definition =>
7374 Make_Access_To_Object_Definition (Loc,
7375 Subtype_Indication => Subtype_Ind)),
7376
7377 Make_Object_Declaration (Loc,
7378 Defining_Identifier => Temp,
7379 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
7380 Expression => Alloc_Node)));
7381
7382 Rewrite (Exp,
7383 Make_Explicit_Dereference (Loc,
7384 Prefix => New_Occurrence_Of (Temp, Loc)));
7385
7386 -- Ada 2005 (AI-251): If the type of the returned object is
7387 -- an interface then add an implicit type conversion to force
7388 -- displacement of the "this" pointer.
7389
7390 if Is_Interface (R_Type) then
7391 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7392 end if;
7393
7394 Analyze_And_Resolve (Exp, R_Type);
7395 end;
7396
7397 -- Otherwise use the gigi mechanism to allocate result on the
7398 -- secondary stack.
7399
7400 else
7401 Check_Restriction (No_Secondary_Stack, N);
7402 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7403 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7404 end if;
7405 end if;
7406
7407 -- Implement the rules of 6.5(8-10), which require a tag check in
7408 -- the case of a limited tagged return type, and tag reassignment for
7409 -- nonlimited tagged results. These actions are needed when the return
7410 -- type is a specific tagged type and the result expression is a
7411 -- conversion or a formal parameter, because in that case the tag of
7412 -- the expression might differ from the tag of the specific result type.
7413
7414 -- We must also verify an underlying type exists for the return type in
7415 -- case it is incomplete - in which case is not necessary to generate a
7416 -- check anyway since an incomplete limited tagged return type would
7417 -- qualify as a premature usage.
7418
7419 if Present (Utyp)
7420 and then Is_Tagged_Type (Utyp)
7421 and then not Is_Class_Wide_Type (Utyp)
7422 and then (Nkind (Exp) in
7423 N_Type_Conversion | N_Unchecked_Type_Conversion
7424 or else (Is_Entity_Name (Exp)
7425 and then Is_Formal (Entity (Exp))))
7426 then
7427 -- When the return type is limited, perform a check that the tag of
7428 -- the result is the same as the tag of the return type.
7429
7430 if Is_Limited_Type (R_Type) then
7431 Insert_Action (Exp,
7432 Make_Raise_Constraint_Error (Loc,
7433 Condition =>
7434 Make_Op_Ne (Loc,
7435 Left_Opnd =>
7436 Make_Selected_Component (Loc,
7437 Prefix => Duplicate_Subexpr (Exp),
7438 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7439 Right_Opnd =>
7440 Make_Attribute_Reference (Loc,
7441 Prefix =>
7442 New_Occurrence_Of (Base_Type (Utyp), Loc),
7443 Attribute_Name => Name_Tag)),
7444 Reason => CE_Tag_Check_Failed));
7445
7446 -- If the result type is a specific nonlimited tagged type, then we
7447 -- have to ensure that the tag of the result is that of the result
7448 -- type. This is handled by making a copy of the expression in
7449 -- the case where it might have a different tag, namely when the
7450 -- expression is a conversion or a formal parameter. We create a new
7451 -- object of the result type and initialize it from the expression,
7452 -- which will implicitly force the tag to be set appropriately.
7453
7454 else
7455 declare
7456 ExpR : constant Node_Id := Relocate_Node (Exp);
7457 Result_Id : constant Entity_Id :=
7458 Make_Temporary (Loc, 'R', ExpR);
7459 Result_Exp : constant Node_Id :=
7460 New_Occurrence_Of (Result_Id, Loc);
7461 Result_Obj : constant Node_Id :=
7462 Make_Object_Declaration (Loc,
7463 Defining_Identifier => Result_Id,
7464 Object_Definition =>
7465 New_Occurrence_Of (R_Type, Loc),
7466 Constant_Present => True,
7467 Expression => ExpR);
7468
7469 begin
7470 Set_Assignment_OK (Result_Obj);
7471 Insert_Action (Exp, Result_Obj);
7472
7473 Rewrite (Exp, Result_Exp);
7474 Analyze_And_Resolve (Exp, R_Type);
7475 end;
7476 end if;
7477
7478 -- Ada 2005 (AI95-344): If the result type is class-wide, then insert
7479 -- a check that the level of the return expression's underlying type
7480 -- is not deeper than the level of the master enclosing the function.
7481
7482 -- AI12-043: The check is made immediately after the return object is
7483 -- created. This means that we do not apply it to the simple return
7484 -- generated by the expansion of an extended return statement.
7485
7486 -- No runtime check needed in interface thunks since it is performed
7487 -- by the target primitive associated with the thunk.
7488
7489 elsif Is_Class_Wide_Type (R_Type)
7490 and then not Comes_From_Extended_Return_Statement (N)
7491 and then not Is_Thunk (Scope_Id)
7492 then
7493 Apply_CW_Accessibility_Check (Exp, Scope_Id);
7494
7495 -- Ada 2012 (AI05-0073): If the result subtype of the function is
7496 -- defined by an access_definition designating a specific tagged
7497 -- type T, a check is made that the result value is null or the tag
7498 -- of the object designated by the result value identifies T.
7499
7500 -- The return expression is referenced twice in the code below, so it
7501 -- must be made free of side effects. Given that different compilers
7502 -- may evaluate these parameters in different order, both occurrences
7503 -- perform a copy.
7504
7505 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7506 and then Is_Tagged_Type (Designated_Type (R_Type))
7507 and then not Is_Class_Wide_Type (Designated_Type (R_Type))
7508 and then Nkind (Original_Node (Exp)) /= N_Null
7509 and then not Tag_Checks_Suppressed (Designated_Type (R_Type))
7510 then
7511 -- Generate:
7512 -- [Constraint_Error
7513 -- when Exp /= null
7514 -- and then Exp.all not in Designated_Type]
7515
7516 Insert_Action (N,
7517 Make_Raise_Constraint_Error (Loc,
7518 Condition =>
7519 Make_And_Then (Loc,
7520 Left_Opnd =>
7521 Make_Op_Ne (Loc,
7522 Left_Opnd => Duplicate_Subexpr (Exp),
7523 Right_Opnd => Make_Null (Loc)),
7524
7525 Right_Opnd =>
7526 Make_Not_In (Loc,
7527 Left_Opnd =>
7528 Make_Explicit_Dereference (Loc,
7529 Prefix => Duplicate_Subexpr (Exp)),
7530 Right_Opnd =>
7531 New_Occurrence_Of (Designated_Type (R_Type), Loc))),
7532
7533 Reason => CE_Tag_Check_Failed),
7534 Suppress => All_Checks);
7535 end if;
7536
7537 -- If we are returning a nonscalar object that is possibly unaligned,
7538 -- then copy the value into a temporary first. This copy may need to
7539 -- expand to a loop of component operations.
7540
7541 if Is_Possibly_Unaligned_Slice (Exp)
7542 or else (Is_Possibly_Unaligned_Object (Exp)
7543 and then not Represented_As_Scalar (Etype (Exp)))
7544 then
7545 declare
7546 ExpR : constant Node_Id := Relocate_Node (Exp);
7547 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7548 begin
7549 Insert_Action (Exp,
7550 Make_Object_Declaration (Loc,
7551 Defining_Identifier => Tnn,
7552 Constant_Present => True,
7553 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7554 Expression => ExpR),
7555 Suppress => All_Checks);
7556 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7557 end;
7558 end if;
7559
7560 -- Call the _Postconditions procedure if the related function has
7561 -- contract assertions that need to be verified on exit.
7562
7563 if Ekind (Scope_Id) = E_Function
7564 and then Present (Postconditions_Proc (Scope_Id))
7565 then
7566 -- In the case of discriminated objects, we have created a
7567 -- constrained subtype above, and used the underlying type. This
7568 -- transformation is post-analysis and harmless, except that now the
7569 -- call to the post-condition will be analyzed and the type kinds
7570 -- have to match.
7571
7572 if Nkind (Exp) = N_Unchecked_Type_Conversion
7573 and then Is_Private_Type (R_Type) /= Is_Private_Type (Etype (Exp))
7574 then
7575 Rewrite (Exp, Expression (Relocate_Node (Exp)));
7576 end if;
7577
7578 -- We are going to reference the returned value twice in this case,
7579 -- once in the call to _Postconditions, and once in the actual return
7580 -- statement, but we can't have side effects happening twice.
7581
7582 Force_Evaluation (Exp, Mode => Strict);
7583
7584 -- Save the return value or a pointer to the return value since we
7585 -- may need to call postconditions after finalization when cleanup
7586 -- actions are present.
7587
7588 -- Generate:
7589 --
7590 -- Result_Object_For_Postcond := [Exp]'Unrestricted_Access;
7591
7592 Insert_Action (Exp,
7593 Make_Assignment_Statement (Loc,
7594 Name =>
7595 New_Occurrence_Of
7596 (Get_Result_Object_For_Postcond (Scope_Id), Loc),
7597 Expression =>
7598 (if Is_Elementary_Type (Etype (R_Type)) then
7599 New_Copy_Tree (Exp)
7600 else
7601 Make_Attribute_Reference (Loc,
7602 Attribute_Name => Name_Unrestricted_Access,
7603 Prefix => New_Copy_Tree (Exp)))));
7604
7605 -- Mark the successful return to signal that postconditions need to
7606 -- be evaluated when finalization occurs.
7607
7608 -- Generate:
7609 --
7610 -- Return_Success_For_Postcond := True;
7611 -- if Postcond_Enabled then
7612 -- _Postconditions ([exp]);
7613 -- end if;
7614
7615 Insert_Action (Exp,
7616 Make_Assignment_Statement (Loc,
7617 Name =>
7618 New_Occurrence_Of
7619 (Get_Return_Success_For_Postcond (Scope_Id), Loc),
7620 Expression => New_Occurrence_Of (Standard_True, Loc)));
7621
7622 -- Wrap the call to _postconditions within a test of the
7623 -- Postcond_Enabled flag to delay postcondition evaluation until
7624 -- after finalization when required.
7625
7626 Insert_Action (Exp,
7627 Make_If_Statement (Loc,
7628 Condition =>
7629 New_Occurrence_Of (Get_Postcond_Enabled (Scope_Id), Loc),
7630 Then_Statements => New_List (
7631 Make_Procedure_Call_Statement (Loc,
7632 Name =>
7633 New_Occurrence_Of
7634 (Postconditions_Proc (Scope_Id), Loc),
7635 Parameter_Associations => New_List (New_Copy_Tree (Exp))))));
7636 end if;
7637
7638 -- Ada 2005 (AI-251): If this return statement corresponds with an
7639 -- simple return statement associated with an extended return statement
7640 -- and the type of the returned object is an interface then generate an
7641 -- implicit conversion to force displacement of the "this" pointer.
7642
7643 if Ada_Version >= Ada_2005
7644 and then Comes_From_Extended_Return_Statement (N)
7645 and then Nkind (Expression (N)) = N_Identifier
7646 and then Is_Interface (Utyp)
7647 and then Utyp /= Underlying_Type (Exp_Typ)
7648 then
7649 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
7650 Analyze_And_Resolve (Exp);
7651 end if;
7652
7653 -- Ada 2020 (AI12-0279)
7654
7655 if Has_Yield_Aspect (Scope_Id)
7656 and then RTE_Available (RE_Yield)
7657 then
7658 Insert_Action (N,
7659 Make_Procedure_Call_Statement (Loc,
7660 New_Occurrence_Of (RTE (RE_Yield), Loc)));
7661 end if;
7662 end Expand_Simple_Function_Return;
7663
7664 -----------------------
7665 -- Freeze_Subprogram --
7666 -----------------------
7667
7668 procedure Freeze_Subprogram (N : Node_Id) is
7669 Loc : constant Source_Ptr := Sloc (N);
7670
7671 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
7672 -- (Ada 2005): Register a predefined primitive in all the secondary
7673 -- dispatch tables of its primitive type.
7674
7675 ----------------------------------
7676 -- Register_Predefined_DT_Entry --
7677 ----------------------------------
7678
7679 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
7680 Iface_DT_Ptr : Elmt_Id;
7681 Tagged_Typ : Entity_Id;
7682 Thunk_Id : Entity_Id;
7683 Thunk_Code : Node_Id;
7684
7685 begin
7686 Tagged_Typ := Find_Dispatching_Type (Prim);
7687
7688 if No (Access_Disp_Table (Tagged_Typ))
7689 or else not Has_Interfaces (Tagged_Typ)
7690 or else not RTE_Available (RE_Interface_Tag)
7691 or else Restriction_Active (No_Dispatching_Calls)
7692 then
7693 return;
7694 end if;
7695
7696 -- Skip the first two access-to-dispatch-table pointers since they
7697 -- leads to the primary dispatch table (predefined DT and user
7698 -- defined DT). We are only concerned with the secondary dispatch
7699 -- table pointers. Note that the access-to- dispatch-table pointer
7700 -- corresponds to the first implemented interface retrieved below.
7701
7702 Iface_DT_Ptr :=
7703 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
7704
7705 while Present (Iface_DT_Ptr)
7706 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
7707 loop
7708 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7709 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code,
7710 Iface => Related_Type (Node (Iface_DT_Ptr)));
7711
7712 if Present (Thunk_Code) then
7713 Insert_Actions_After (N, New_List (
7714 Thunk_Code,
7715
7716 Build_Set_Predefined_Prim_Op_Address (Loc,
7717 Tag_Node =>
7718 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
7719 Position => DT_Position (Prim),
7720 Address_Node =>
7721 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7722 Make_Attribute_Reference (Loc,
7723 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
7724 Attribute_Name => Name_Unrestricted_Access))),
7725
7726 Build_Set_Predefined_Prim_Op_Address (Loc,
7727 Tag_Node =>
7728 New_Occurrence_Of
7729 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
7730 Loc),
7731 Position => DT_Position (Prim),
7732 Address_Node =>
7733 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
7734 Make_Attribute_Reference (Loc,
7735 Prefix => New_Occurrence_Of (Prim, Loc),
7736 Attribute_Name => Name_Unrestricted_Access)))));
7737 end if;
7738
7739 -- Skip the tag of the predefined primitives dispatch table
7740
7741 Next_Elmt (Iface_DT_Ptr);
7742 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
7743
7744 -- Skip tag of the no-thunks dispatch table
7745
7746 Next_Elmt (Iface_DT_Ptr);
7747 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7748
7749 -- Skip tag of predefined primitives no-thunks dispatch table
7750
7751 Next_Elmt (Iface_DT_Ptr);
7752 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
7753
7754 Next_Elmt (Iface_DT_Ptr);
7755 end loop;
7756 end Register_Predefined_DT_Entry;
7757
7758 -- Local variables
7759
7760 Subp : constant Entity_Id := Entity (N);
7761
7762 -- Start of processing for Freeze_Subprogram
7763
7764 begin
7765 -- We suppress the initialization of the dispatch table entry when
7766 -- not Tagged_Type_Expansion because the dispatching mechanism is
7767 -- handled internally by the target.
7768
7769 if Is_Dispatching_Operation (Subp)
7770 and then not Is_Abstract_Subprogram (Subp)
7771 and then Present (DTC_Entity (Subp))
7772 and then Present (Scope (DTC_Entity (Subp)))
7773 and then Tagged_Type_Expansion
7774 and then not Restriction_Active (No_Dispatching_Calls)
7775 and then RTE_Available (RE_Tag)
7776 then
7777 declare
7778 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
7779
7780 begin
7781 -- Handle private overridden primitives
7782
7783 if not Is_CPP_Class (Typ) then
7784 Check_Overriding_Operation (Subp);
7785 end if;
7786
7787 -- We assume that imported CPP primitives correspond with objects
7788 -- whose constructor is in the CPP side; therefore we don't need
7789 -- to generate code to register them in the dispatch table.
7790
7791 if Is_CPP_Class (Typ) then
7792 null;
7793
7794 -- Handle CPP primitives found in derivations of CPP_Class types.
7795 -- These primitives must have been inherited from some parent, and
7796 -- there is no need to register them in the dispatch table because
7797 -- Build_Inherit_Prims takes care of initializing these slots.
7798
7799 elsif Is_Imported (Subp)
7800 and then Convention (Subp) in Convention_C_Family
7801 then
7802 null;
7803
7804 -- Generate code to register the primitive in non statically
7805 -- allocated dispatch tables
7806
7807 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
7808
7809 -- When a primitive is frozen, enter its name in its dispatch
7810 -- table slot.
7811
7812 if not Is_Interface (Typ)
7813 or else Present (Interface_Alias (Subp))
7814 then
7815 if Is_Predefined_Dispatching_Operation (Subp) then
7816 Register_Predefined_DT_Entry (Subp);
7817 end if;
7818
7819 Insert_Actions_After (N,
7820 Register_Primitive (Loc, Prim => Subp));
7821 end if;
7822 end if;
7823 end;
7824 end if;
7825
7826 -- Mark functions that return by reference. Note that it cannot be part
7827 -- of the normal semantic analysis of the spec since the underlying
7828 -- returned type may not be known yet (for private types).
7829
7830 declare
7831 Typ : constant Entity_Id := Etype (Subp);
7832 Utyp : constant Entity_Id := Underlying_Type (Typ);
7833
7834 begin
7835 if Is_Limited_View (Typ) then
7836 Set_Returns_By_Ref (Subp);
7837
7838 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7839 Set_Returns_By_Ref (Subp);
7840 end if;
7841 end;
7842
7843 -- Wnen freezing a null procedure, analyze its delayed aspects now
7844 -- because we may not have reached the end of the declarative list when
7845 -- delayed aspects are normally analyzed. This ensures that dispatching
7846 -- calls are properly rewritten when the generated _Postcondition
7847 -- procedure is analyzed in the null procedure body.
7848
7849 if Nkind (Parent (Subp)) = N_Procedure_Specification
7850 and then Null_Present (Parent (Subp))
7851 then
7852 Analyze_Entry_Or_Subprogram_Contract (Subp);
7853 end if;
7854 end Freeze_Subprogram;
7855
7856 --------------------------
7857 -- Has_BIP_Extra_Formal --
7858 --------------------------
7859
7860 function Has_BIP_Extra_Formal
7861 (E : Entity_Id;
7862 Kind : BIP_Formal_Kind) return Boolean
7863 is
7864 Extra_Formal : Entity_Id := Extra_Formals (E);
7865
7866 begin
7867 -- We can only rely on the availability of the extra formals in frozen
7868 -- entities or in subprogram types of dispatching calls (since their
7869 -- extra formals are added when the target subprogram is frozen; see
7870 -- Expand_Dispatching_Call).
7871
7872 pragma Assert (Is_Frozen (E)
7873 or else (Ekind (E) = E_Subprogram_Type
7874 and then Is_Dispatch_Table_Entity (E))
7875 or else (Is_Dispatching_Operation (E)
7876 and then Is_Frozen (Find_Dispatching_Type (E))));
7877
7878 while Present (Extra_Formal) loop
7879 if Is_Build_In_Place_Entity (Extra_Formal)
7880 and then BIP_Suffix_Kind (Extra_Formal) = Kind
7881 then
7882 return True;
7883 end if;
7884
7885 Next_Formal_With_Extras (Extra_Formal);
7886 end loop;
7887
7888 return False;
7889 end Has_BIP_Extra_Formal;
7890
7891 ------------------------------
7892 -- Insert_Post_Call_Actions --
7893 ------------------------------
7894
7895 procedure Insert_Post_Call_Actions (N : Node_Id; Post_Call : List_Id) is
7896 Context : constant Node_Id := Parent (N);
7897
7898 begin
7899 if Is_Empty_List (Post_Call) then
7900 return;
7901 end if;
7902
7903 -- Cases where the call is not a member of a statement list. This also
7904 -- includes the cases where the call is an actual in another function
7905 -- call, or is an index, or is an operand of an if-expression, i.e. is
7906 -- in an expression context.
7907
7908 if not Is_List_Member (N)
7909 or else Nkind (Context) in N_Function_Call
7910 | N_If_Expression
7911 | N_Indexed_Component
7912 then
7913 -- In Ada 2012 the call may be a function call in an expression
7914 -- (since OUT and IN OUT parameters are now allowed for such calls).
7915 -- The write-back of (in)-out parameters is handled by the back-end,
7916 -- but the constraint checks generated when subtypes of formal and
7917 -- actual don't match must be inserted in the form of assignments.
7918 -- Also do this in the case of explicit dereferences, which can occur
7919 -- due to rewritings of function calls with controlled results.
7920
7921 if Nkind (N) = N_Function_Call
7922 or else Nkind (Original_Node (N)) = N_Function_Call
7923 or else Nkind (N) = N_Explicit_Dereference
7924 then
7925 pragma Assert (Ada_Version >= Ada_2012);
7926 -- Functions with '[in] out' parameters are only allowed in Ada
7927 -- 2012.
7928
7929 -- We used to handle this by climbing up parents to a
7930 -- non-statement/declaration and then simply making a call to
7931 -- Insert_Actions_After (P, Post_Call), but that doesn't work
7932 -- for Ada 2012. If we are in the middle of an expression, e.g.
7933 -- the condition of an IF, this call would insert after the IF
7934 -- statement, which is much too late to be doing the write back.
7935 -- For example:
7936
7937 -- if Clobber (X) then
7938 -- Put_Line (X'Img);
7939 -- else
7940 -- goto Junk
7941 -- end if;
7942
7943 -- Now assume Clobber changes X, if we put the write back after
7944 -- the IF, the Put_Line gets the wrong value and the goto causes
7945 -- the write back to be skipped completely.
7946
7947 -- To deal with this, we replace the call by
7948 --
7949 -- do
7950 -- Tnnn : constant function-result-type := function-call;
7951 -- Post_Call actions
7952 -- in
7953 -- Tnnn;
7954 -- end;
7955 --
7956 -- However, that doesn't work if function-result-type requires
7957 -- finalization (because function-call's result never gets
7958 -- finalized). So in that case, we instead replace the call by
7959 --
7960 -- do
7961 -- type Ref is access all function-result-type;
7962 -- Ptr : constant Ref := function-call'Reference;
7963 -- Tnnn : constant function-result-type := Ptr.all;
7964 -- Finalize (Ptr.all);
7965 -- Post_Call actions
7966 -- in
7967 -- Tnnn;
7968 -- end;
7969 --
7970
7971 declare
7972 Loc : constant Source_Ptr := Sloc (N);
7973 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
7974 FRTyp : constant Entity_Id := Etype (N);
7975 Name : constant Node_Id := Relocate_Node (N);
7976
7977 begin
7978 if Needs_Finalization (FRTyp) then
7979 declare
7980 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7981
7982 Ptr_Typ_Decl : constant Node_Id :=
7983 Make_Full_Type_Declaration (Loc,
7984 Defining_Identifier => Ptr_Typ,
7985 Type_Definition =>
7986 Make_Access_To_Object_Definition (Loc,
7987 All_Present => True,
7988 Subtype_Indication =>
7989 New_Occurrence_Of (FRTyp, Loc)));
7990
7991 Ptr_Obj : constant Entity_Id :=
7992 Make_Temporary (Loc, 'P');
7993
7994 Ptr_Obj_Decl : constant Node_Id :=
7995 Make_Object_Declaration (Loc,
7996 Defining_Identifier => Ptr_Obj,
7997 Object_Definition =>
7998 New_Occurrence_Of (Ptr_Typ, Loc),
7999 Constant_Present => True,
8000 Expression =>
8001 Make_Attribute_Reference (Loc,
8002 Prefix => Name,
8003 Attribute_Name => Name_Unrestricted_Access));
8004
8005 function Ptr_Dereference return Node_Id is
8006 (Make_Explicit_Dereference (Loc,
8007 Prefix => New_Occurrence_Of (Ptr_Obj, Loc)));
8008
8009 Tnn_Decl : constant Node_Id :=
8010 Make_Object_Declaration (Loc,
8011 Defining_Identifier => Tnnn,
8012 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
8013 Constant_Present => True,
8014 Expression => Ptr_Dereference);
8015
8016 Finalize_Call : constant Node_Id :=
8017 Make_Final_Call
8018 (Obj_Ref => Ptr_Dereference, Typ => FRTyp);
8019 begin
8020 -- Prepend in reverse order
8021
8022 Prepend_To (Post_Call, Finalize_Call);
8023 Prepend_To (Post_Call, Tnn_Decl);
8024 Prepend_To (Post_Call, Ptr_Obj_Decl);
8025 Prepend_To (Post_Call, Ptr_Typ_Decl);
8026 end;
8027 else
8028 Prepend_To (Post_Call,
8029 Make_Object_Declaration (Loc,
8030 Defining_Identifier => Tnnn,
8031 Object_Definition => New_Occurrence_Of (FRTyp, Loc),
8032 Constant_Present => True,
8033 Expression => Name));
8034 end if;
8035
8036 Rewrite (N,
8037 Make_Expression_With_Actions (Loc,
8038 Actions => Post_Call,
8039 Expression => New_Occurrence_Of (Tnnn, Loc)));
8040
8041 -- We don't want to just blindly call Analyze_And_Resolve
8042 -- because that would cause unwanted recursion on the call.
8043 -- So for a moment set the call as analyzed to prevent that
8044 -- recursion, and get the rest analyzed properly, then reset
8045 -- the analyzed flag, so our caller can continue.
8046
8047 Set_Analyzed (Name, True);
8048 Analyze_And_Resolve (N, FRTyp);
8049 Set_Analyzed (Name, False);
8050 end;
8051
8052 -- If not the special Ada 2012 case of a function call, then we must
8053 -- have the triggering statement of a triggering alternative or an
8054 -- entry call alternative, and we can add the post call stuff to the
8055 -- corresponding statement list.
8056
8057 else
8058 pragma Assert (Nkind (Context) in N_Entry_Call_Alternative
8059 | N_Triggering_Alternative);
8060
8061 if Is_Non_Empty_List (Statements (Context)) then
8062 Insert_List_Before_And_Analyze
8063 (First (Statements (Context)), Post_Call);
8064 else
8065 Set_Statements (Context, Post_Call);
8066 end if;
8067 end if;
8068
8069 -- A procedure call is always part of a declarative or statement list,
8070 -- however a function call may appear nested within a construct. Most
8071 -- cases of function call nesting are handled in the special case above.
8072 -- The only exception is when the function call acts as an actual in a
8073 -- procedure call. In this case the function call is in a list, but the
8074 -- post-call actions must be inserted after the procedure call.
8075 -- What if the function call is an aggregate component ???
8076
8077 elsif Nkind (Context) = N_Procedure_Call_Statement then
8078 Insert_Actions_After (Context, Post_Call);
8079
8080 -- Otherwise, normal case where N is in a statement sequence, just put
8081 -- the post-call stuff after the call statement.
8082
8083 else
8084 Insert_Actions_After (N, Post_Call);
8085 end if;
8086 end Insert_Post_Call_Actions;
8087
8088 -----------------------------------
8089 -- Is_Build_In_Place_Result_Type --
8090 -----------------------------------
8091
8092 function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean is
8093 begin
8094 if not Expander_Active then
8095 return False;
8096 end if;
8097
8098 -- In Ada 2005 all functions with an inherently limited return type
8099 -- must be handled using a build-in-place profile, including the case
8100 -- of a function with a limited interface result, where the function
8101 -- may return objects of nonlimited descendants.
8102
8103 if Is_Limited_View (Typ) then
8104 return Ada_Version >= Ada_2005 and then not Debug_Flag_Dot_L;
8105
8106 else
8107 if Debug_Flag_Dot_9 then
8108 return False;
8109 end if;
8110
8111 if Has_Interfaces (Typ) then
8112 return False;
8113 end if;
8114
8115 declare
8116 T : Entity_Id := Typ;
8117 begin
8118 -- For T'Class, return True if it's True for T. This is necessary
8119 -- because a class-wide function might say "return F (...)", where
8120 -- F returns the corresponding specific type. We need a loop in
8121 -- case T is a subtype of a class-wide type.
8122
8123 while Is_Class_Wide_Type (T) loop
8124 T := Etype (T);
8125 end loop;
8126
8127 -- If this is a generic formal type in an instance, return True if
8128 -- it's True for the generic actual type.
8129
8130 if Nkind (Parent (T)) = N_Subtype_Declaration
8131 and then Present (Generic_Parent_Type (Parent (T)))
8132 then
8133 T := Entity (Subtype_Indication (Parent (T)));
8134
8135 if Present (Full_View (T)) then
8136 T := Full_View (T);
8137 end if;
8138 end if;
8139
8140 if Present (Underlying_Type (T)) then
8141 T := Underlying_Type (T);
8142 end if;
8143
8144 declare
8145 Result : Boolean;
8146 -- So we can stop here in the debugger
8147 begin
8148 -- ???For now, enable build-in-place for a very narrow set of
8149 -- controlled types. Change "if True" to "if False" to
8150 -- experiment with more controlled types. Eventually, we might
8151 -- like to enable build-in-place for all tagged types, all
8152 -- types that need finalization, and all caller-unknown-size
8153 -- types.
8154
8155 if True then
8156 Result := Is_Controlled (T)
8157 and then not Is_Generic_Actual_Type (T)
8158 and then Present (Enclosing_Subprogram (T))
8159 and then not Is_Compilation_Unit (Enclosing_Subprogram (T))
8160 and then Ekind (Enclosing_Subprogram (T)) = E_Procedure;
8161 else
8162 Result := Is_Controlled (T);
8163 end if;
8164
8165 return Result;
8166 end;
8167 end;
8168 end if;
8169 end Is_Build_In_Place_Result_Type;
8170
8171 ------------------------------
8172 -- Is_Build_In_Place_Entity --
8173 ------------------------------
8174
8175 function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean is
8176 Nam : constant String := Get_Name_String (Chars (E));
8177
8178 function Has_Suffix (Suffix : String) return Boolean;
8179 -- Return True if Nam has suffix Suffix
8180
8181 function Has_Suffix (Suffix : String) return Boolean is
8182 Len : constant Natural := Suffix'Length;
8183 begin
8184 return Nam'Length > Len
8185 and then Nam (Nam'Last - Len + 1 .. Nam'Last) = Suffix;
8186 end Has_Suffix;
8187
8188 -- Start of processing for Is_Build_In_Place_Entity
8189
8190 begin
8191 return Has_Suffix (BIP_Alloc_Suffix)
8192 or else Has_Suffix (BIP_Storage_Pool_Suffix)
8193 or else Has_Suffix (BIP_Finalization_Master_Suffix)
8194 or else Has_Suffix (BIP_Task_Master_Suffix)
8195 or else Has_Suffix (BIP_Activation_Chain_Suffix)
8196 or else Has_Suffix (BIP_Object_Access_Suffix);
8197 end Is_Build_In_Place_Entity;
8198
8199 --------------------------------
8200 -- Is_Build_In_Place_Function --
8201 --------------------------------
8202
8203 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
8204 begin
8205 -- This function is called from Expand_Subtype_From_Expr during
8206 -- semantic analysis, even when expansion is off. In those cases
8207 -- the build_in_place expansion will not take place.
8208
8209 if not Expander_Active then
8210 return False;
8211 end if;
8212
8213 -- For now we test whether E denotes a function or access-to-function
8214 -- type whose result subtype is inherently limited. Later this test
8215 -- may be revised to allow composite nonlimited types.
8216
8217 if Ekind (E) in E_Function | E_Generic_Function
8218 or else (Ekind (E) = E_Subprogram_Type
8219 and then Etype (E) /= Standard_Void_Type)
8220 then
8221 -- If the function is imported from a foreign language, we don't do
8222 -- build-in-place. Note that Import (Ada) functions can do
8223 -- build-in-place. Note that it is OK for a build-in-place function
8224 -- to return a type with a foreign convention; the build-in-place
8225 -- machinery will ensure there is no copying.
8226
8227 return Is_Build_In_Place_Result_Type (Etype (E))
8228 and then not (Has_Foreign_Convention (E) and then Is_Imported (E))
8229 and then not Debug_Flag_Dot_L;
8230 else
8231 return False;
8232 end if;
8233 end Is_Build_In_Place_Function;
8234
8235 -------------------------------------
8236 -- Is_Build_In_Place_Function_Call --
8237 -------------------------------------
8238
8239 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
8240 Exp_Node : constant Node_Id := Unqual_Conv (N);
8241 Function_Id : Entity_Id;
8242
8243 begin
8244 -- Return False if the expander is currently inactive, since awareness
8245 -- of build-in-place treatment is only relevant during expansion. Note
8246 -- that Is_Build_In_Place_Function, which is called as part of this
8247 -- function, is also conditioned this way, but we need to check here as
8248 -- well to avoid blowing up on processing protected calls when expansion
8249 -- is disabled (such as with -gnatc) since those would trip over the
8250 -- raise of Program_Error below.
8251
8252 -- In SPARK mode, build-in-place calls are not expanded, so that we
8253 -- may end up with a call that is neither resolved to an entity, nor
8254 -- an indirect call.
8255
8256 if not Expander_Active or else Nkind (Exp_Node) /= N_Function_Call then
8257 return False;
8258 end if;
8259
8260 if Is_Entity_Name (Name (Exp_Node)) then
8261 Function_Id := Entity (Name (Exp_Node));
8262
8263 -- In the case of an explicitly dereferenced call, use the subprogram
8264 -- type generated for the dereference.
8265
8266 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8267 Function_Id := Etype (Name (Exp_Node));
8268
8269 -- This may be a call to a protected function.
8270
8271 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
8272 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
8273
8274 else
8275 raise Program_Error;
8276 end if;
8277
8278 declare
8279 Result : constant Boolean := Is_Build_In_Place_Function (Function_Id);
8280 -- So we can stop here in the debugger
8281 begin
8282 return Result;
8283 end;
8284 end Is_Build_In_Place_Function_Call;
8285
8286 -----------------------
8287 -- Is_Null_Procedure --
8288 -----------------------
8289
8290 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8291 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8292
8293 begin
8294 if Ekind (Subp) /= E_Procedure then
8295 return False;
8296
8297 -- Check if this is a declared null procedure
8298
8299 elsif Nkind (Decl) = N_Subprogram_Declaration then
8300 if not Null_Present (Specification (Decl)) then
8301 return False;
8302
8303 elsif No (Body_To_Inline (Decl)) then
8304 return False;
8305
8306 -- Check if the body contains only a null statement, followed by
8307 -- the return statement added during expansion.
8308
8309 else
8310 declare
8311 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8312
8313 Stat : Node_Id;
8314 Stat2 : Node_Id;
8315
8316 begin
8317 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8318 return False;
8319 else
8320 -- We must skip SCIL nodes because they are currently
8321 -- implemented as special N_Null_Statement nodes.
8322
8323 Stat :=
8324 First_Non_SCIL_Node
8325 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8326 Stat2 := Next_Non_SCIL_Node (Stat);
8327
8328 return
8329 Is_Empty_List (Declarations (Orig_Bod))
8330 and then Nkind (Stat) = N_Null_Statement
8331 and then
8332 (No (Stat2)
8333 or else
8334 (Nkind (Stat2) = N_Simple_Return_Statement
8335 and then No (Next (Stat2))));
8336 end if;
8337 end;
8338 end if;
8339
8340 else
8341 return False;
8342 end if;
8343 end Is_Null_Procedure;
8344
8345 -------------------------------------------
8346 -- Make_Build_In_Place_Call_In_Allocator --
8347 -------------------------------------------
8348
8349 procedure Make_Build_In_Place_Call_In_Allocator
8350 (Allocator : Node_Id;
8351 Function_Call : Node_Id)
8352 is
8353 Acc_Type : constant Entity_Id := Etype (Allocator);
8354 Loc : constant Source_Ptr := Sloc (Function_Call);
8355 Func_Call : Node_Id := Function_Call;
8356 Ref_Func_Call : Node_Id;
8357 Function_Id : Entity_Id;
8358 Result_Subt : Entity_Id;
8359 New_Allocator : Node_Id;
8360 Return_Obj_Access : Entity_Id; -- temp for function result
8361 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8362 Alloc_Form : BIP_Allocation_Form;
8363 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8364 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8365 Chain : Entity_Id; -- activation chain, in case of tasks
8366
8367 begin
8368 -- Step past qualification or unchecked conversion (the latter can occur
8369 -- in cases of calls to 'Input).
8370
8371 if Nkind (Func_Call) in N_Qualified_Expression
8372 | N_Type_Conversion
8373 | N_Unchecked_Type_Conversion
8374 then
8375 Func_Call := Expression (Func_Call);
8376 end if;
8377
8378 -- Mark the call as processed as a build-in-place call
8379
8380 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8381 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8382
8383 if Is_Entity_Name (Name (Func_Call)) then
8384 Function_Id := Entity (Name (Func_Call));
8385
8386 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8387 Function_Id := Etype (Name (Func_Call));
8388
8389 else
8390 raise Program_Error;
8391 end if;
8392
8393 Warn_BIP (Func_Call);
8394
8395 Result_Subt := Available_View (Etype (Function_Id));
8396
8397 -- Create a temp for the function result. In the caller-allocates case,
8398 -- this will be initialized to the result of a new uninitialized
8399 -- allocator. Note: we do not use Allocator as the Related_Node of
8400 -- Return_Obj_Access in call to Make_Temporary below as this would
8401 -- create a sort of infinite "recursion".
8402
8403 Return_Obj_Access := Make_Temporary (Loc, 'R');
8404 Set_Etype (Return_Obj_Access, Acc_Type);
8405 Set_Can_Never_Be_Null (Acc_Type, False);
8406 -- It gets initialized to null, so we can't have that
8407
8408 -- When the result subtype is constrained, the return object is created
8409 -- on the caller side, and access to it is passed to the function. This
8410 -- optimization is disabled when the result subtype needs finalization
8411 -- actions because the caller side allocation may result in undesirable
8412 -- finalization. Consider the following example:
8413 --
8414 -- function Make_Lim_Ctrl return Lim_Ctrl is
8415 -- begin
8416 -- return Result : Lim_Ctrl := raise Program_Error do
8417 -- null;
8418 -- end return;
8419 -- end Make_Lim_Ctrl;
8420 --
8421 -- Obj : Lim_Ctrl_Ptr := new Lim_Ctrl'(Make_Lim_Ctrl);
8422 --
8423 -- Even though the size of limited controlled type Lim_Ctrl is known,
8424 -- allocating Obj at the caller side will chain Obj on Lim_Ctrl_Ptr's
8425 -- finalization master. The subsequent call to Make_Lim_Ctrl will fail
8426 -- during the initialization actions for Result, which implies that
8427 -- Result (and Obj by extension) should not be finalized. However Obj
8428 -- will be finalized when access type Lim_Ctrl_Ptr goes out of scope
8429 -- since it is already attached on the related finalization master.
8430
8431 -- Here and in related routines, we must examine the full view of the
8432 -- type, because the view at the point of call may differ from the
8433 -- one in the function body, and the expansion mechanism depends on
8434 -- the characteristics of the full view.
8435
8436 if Needs_BIP_Alloc_Form (Function_Id) then
8437 Temp_Init := Empty;
8438
8439 -- Case of a user-defined storage pool. Pass an allocation parameter
8440 -- indicating that the function should allocate its result in the
8441 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8442 -- pool may not be aliased.
8443
8444 if Present (Associated_Storage_Pool (Acc_Type)) then
8445 Alloc_Form := User_Storage_Pool;
8446 Pool :=
8447 Make_Attribute_Reference (Loc,
8448 Prefix =>
8449 New_Occurrence_Of
8450 (Associated_Storage_Pool (Acc_Type), Loc),
8451 Attribute_Name => Name_Unrestricted_Access);
8452
8453 -- No user-defined pool; pass an allocation parameter indicating that
8454 -- the function should allocate its result on the heap.
8455
8456 else
8457 Alloc_Form := Global_Heap;
8458 Pool := Make_Null (No_Location);
8459 end if;
8460
8461 -- The caller does not provide the return object in this case, so we
8462 -- have to pass null for the object access actual.
8463
8464 Return_Obj_Actual := Empty;
8465
8466 else
8467 -- Replace the initialized allocator of form "new T'(Func (...))"
8468 -- with an uninitialized allocator of form "new T", where T is the
8469 -- result subtype of the called function. The call to the function
8470 -- is handled separately further below.
8471
8472 New_Allocator :=
8473 Make_Allocator (Loc,
8474 Expression => New_Occurrence_Of (Result_Subt, Loc));
8475 Set_No_Initialization (New_Allocator);
8476
8477 -- Copy attributes to new allocator. Note that the new allocator
8478 -- logically comes from source if the original one did, so copy the
8479 -- relevant flag. This ensures proper treatment of the restriction
8480 -- No_Implicit_Heap_Allocations in this case.
8481
8482 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8483 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8484 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8485
8486 Rewrite (Allocator, New_Allocator);
8487
8488 -- Initial value of the temp is the result of the uninitialized
8489 -- allocator. Unchecked_Convert is needed for T'Input where T is
8490 -- derived from a controlled type.
8491
8492 Temp_Init := Relocate_Node (Allocator);
8493
8494 if Nkind (Function_Call) in
8495 N_Type_Conversion | N_Unchecked_Type_Conversion
8496 then
8497 Temp_Init := Unchecked_Convert_To (Acc_Type, Temp_Init);
8498 end if;
8499
8500 -- Indicate that caller allocates, and pass in the return object
8501
8502 Alloc_Form := Caller_Allocation;
8503 Pool := Make_Null (No_Location);
8504 Return_Obj_Actual :=
8505 Make_Unchecked_Type_Conversion (Loc,
8506 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8507 Expression =>
8508 Make_Explicit_Dereference (Loc,
8509 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8510
8511 -- When the result subtype is unconstrained, the function itself must
8512 -- perform the allocation of the return object, so we pass parameters
8513 -- indicating that.
8514
8515 end if;
8516
8517 -- Declare the temp object
8518
8519 Insert_Action (Allocator,
8520 Make_Object_Declaration (Loc,
8521 Defining_Identifier => Return_Obj_Access,
8522 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8523 Expression => Temp_Init));
8524
8525 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8526
8527 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8528 -- then generate an implicit conversion to force displacement of the
8529 -- "this" pointer.
8530
8531 if Is_Interface (Designated_Type (Acc_Type)) then
8532 Rewrite
8533 (Ref_Func_Call,
8534 OK_Convert_To (Acc_Type, Ref_Func_Call));
8535
8536 -- If the types are incompatible, we need an unchecked conversion. Note
8537 -- that the full types will be compatible, but the types not visibly
8538 -- compatible.
8539
8540 elsif Nkind (Function_Call)
8541 in N_Type_Conversion | N_Unchecked_Type_Conversion
8542 then
8543 Ref_Func_Call := Unchecked_Convert_To (Acc_Type, Ref_Func_Call);
8544 end if;
8545
8546 declare
8547 Assign : constant Node_Id :=
8548 Make_Assignment_Statement (Loc,
8549 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8550 Expression => Ref_Func_Call);
8551 -- Assign the result of the function call into the temp. In the
8552 -- caller-allocates case, this is overwriting the temp with its
8553 -- initial value, which has no effect. In the callee-allocates case,
8554 -- this is setting the temp to point to the object allocated by the
8555 -- callee. Unchecked_Convert is needed for T'Input where T is derived
8556 -- from a controlled type.
8557
8558 Actions : List_Id;
8559 -- Actions to be inserted. If there are no tasks, this is just the
8560 -- assignment statement. If the allocated object has tasks, we need
8561 -- to wrap the assignment in a block that activates them. The
8562 -- activation chain of that block must be passed to the function,
8563 -- rather than some outer chain.
8564
8565 begin
8566 if Might_Have_Tasks (Result_Subt) then
8567 Actions := New_List;
8568 Build_Task_Allocate_Block_With_Init_Stmts
8569 (Actions, Allocator, Init_Stmts => New_List (Assign));
8570 Chain := Activation_Chain_Entity (Last (Actions));
8571 else
8572 Actions := New_List (Assign);
8573 Chain := Empty;
8574 end if;
8575
8576 Insert_Actions (Allocator, Actions);
8577 end;
8578
8579 -- When the function has a controlling result, an allocation-form
8580 -- parameter must be passed indicating that the caller is allocating
8581 -- the result object. This is needed because such a function can be
8582 -- called as a dispatching operation and must be treated similarly
8583 -- to functions with unconstrained result subtypes.
8584
8585 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8586 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8587
8588 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8589 (Func_Call, Function_Id, Acc_Type);
8590
8591 Add_Task_Actuals_To_Build_In_Place_Call
8592 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8593 Chain => Chain);
8594
8595 -- Add an implicit actual to the function call that provides access
8596 -- to the allocated object. An unchecked conversion to the (specific)
8597 -- result subtype of the function is inserted to handle cases where
8598 -- the access type of the allocator has a class-wide designated type.
8599
8600 Add_Access_Actual_To_Build_In_Place_Call
8601 (Func_Call, Function_Id, Return_Obj_Actual);
8602
8603 -- Finally, replace the allocator node with a reference to the temp
8604
8605 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8606
8607 Analyze_And_Resolve (Allocator, Acc_Type);
8608 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8609 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8610 end Make_Build_In_Place_Call_In_Allocator;
8611
8612 ---------------------------------------------------
8613 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8614 ---------------------------------------------------
8615
8616 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8617 (Function_Call : Node_Id)
8618 is
8619 Loc : constant Source_Ptr := Sloc (Function_Call);
8620 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8621 Function_Id : Entity_Id;
8622 Result_Subt : Entity_Id;
8623 Return_Obj_Id : Entity_Id;
8624 Return_Obj_Decl : Entity_Id;
8625
8626 begin
8627 -- If the call has already been processed to add build-in-place actuals
8628 -- then return. One place this can occur is for calls to build-in-place
8629 -- functions that occur within a call to a protected operation, where
8630 -- due to rewriting and expansion of the protected call there can be
8631 -- more than one call to Expand_Actuals for the same set of actuals.
8632
8633 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8634 return;
8635 end if;
8636
8637 -- Mark the call as processed as a build-in-place call
8638
8639 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8640
8641 if Is_Entity_Name (Name (Func_Call)) then
8642 Function_Id := Entity (Name (Func_Call));
8643
8644 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8645 Function_Id := Etype (Name (Func_Call));
8646
8647 else
8648 raise Program_Error;
8649 end if;
8650
8651 Warn_BIP (Func_Call);
8652
8653 Result_Subt := Etype (Function_Id);
8654
8655 -- If the build-in-place function returns a controlled object, then the
8656 -- object needs to be finalized immediately after the context. Since
8657 -- this case produces a transient scope, the servicing finalizer needs
8658 -- to name the returned object. Create a temporary which is initialized
8659 -- with the function call:
8660 --
8661 -- Temp_Id : Func_Type := BIP_Func_Call;
8662 --
8663 -- The initialization expression of the temporary will be rewritten by
8664 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8665 -- Call_In_Object_Declaration.
8666
8667 if Needs_Finalization (Result_Subt) then
8668 declare
8669 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8670 Temp_Decl : Node_Id;
8671
8672 begin
8673 -- Reset the guard on the function call since the following does
8674 -- not perform actual call expansion.
8675
8676 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8677
8678 Temp_Decl :=
8679 Make_Object_Declaration (Loc,
8680 Defining_Identifier => Temp_Id,
8681 Object_Definition =>
8682 New_Occurrence_Of (Result_Subt, Loc),
8683 Expression =>
8684 New_Copy_Tree (Function_Call));
8685
8686 Insert_Action (Function_Call, Temp_Decl);
8687
8688 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8689 Analyze (Function_Call);
8690 end;
8691
8692 -- When the result subtype is definite, an object of the subtype is
8693 -- declared and an access value designating it is passed as an actual.
8694
8695 elsif Caller_Known_Size (Func_Call, Result_Subt) then
8696
8697 -- Create a temporary object to hold the function result
8698
8699 Return_Obj_Id := Make_Temporary (Loc, 'R');
8700 Set_Etype (Return_Obj_Id, Result_Subt);
8701
8702 Return_Obj_Decl :=
8703 Make_Object_Declaration (Loc,
8704 Defining_Identifier => Return_Obj_Id,
8705 Aliased_Present => True,
8706 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8707
8708 Set_No_Initialization (Return_Obj_Decl);
8709
8710 Insert_Action (Func_Call, Return_Obj_Decl);
8711
8712 -- When the function has a controlling result, an allocation-form
8713 -- parameter must be passed indicating that the caller is allocating
8714 -- the result object. This is needed because such a function can be
8715 -- called as a dispatching operation and must be treated similarly
8716 -- to functions with unconstrained result subtypes.
8717
8718 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8719 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8720
8721 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8722 (Func_Call, Function_Id);
8723
8724 Add_Task_Actuals_To_Build_In_Place_Call
8725 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8726
8727 -- Add an implicit actual to the function call that provides access
8728 -- to the caller's return object.
8729
8730 Add_Access_Actual_To_Build_In_Place_Call
8731 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8732
8733 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8734 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8735
8736 -- When the result subtype is unconstrained, the function must allocate
8737 -- the return object in the secondary stack, so appropriate implicit
8738 -- parameters are added to the call to indicate that. A transient
8739 -- scope is established to ensure eventual cleanup of the result.
8740
8741 else
8742 -- Pass an allocation parameter indicating that the function should
8743 -- allocate its result on the secondary stack.
8744
8745 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8746 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8747
8748 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8749 (Func_Call, Function_Id);
8750
8751 Add_Task_Actuals_To_Build_In_Place_Call
8752 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8753
8754 -- Pass a null value to the function since no return object is
8755 -- available on the caller side.
8756
8757 Add_Access_Actual_To_Build_In_Place_Call
8758 (Func_Call, Function_Id, Empty);
8759
8760 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
8761 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
8762 end if;
8763 end Make_Build_In_Place_Call_In_Anonymous_Context;
8764
8765 --------------------------------------------
8766 -- Make_Build_In_Place_Call_In_Assignment --
8767 --------------------------------------------
8768
8769 procedure Make_Build_In_Place_Call_In_Assignment
8770 (Assign : Node_Id;
8771 Function_Call : Node_Id)
8772 is
8773 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8774 Lhs : constant Node_Id := Name (Assign);
8775 Loc : constant Source_Ptr := Sloc (Function_Call);
8776 Func_Id : Entity_Id;
8777 Obj_Decl : Node_Id;
8778 Obj_Id : Entity_Id;
8779 Ptr_Typ : Entity_Id;
8780 Ptr_Typ_Decl : Node_Id;
8781 New_Expr : Node_Id;
8782 Result_Subt : Entity_Id;
8783
8784 begin
8785 -- Mark the call as processed as a build-in-place call
8786
8787 pragma Assert (not Is_Expanded_Build_In_Place_Call (Func_Call));
8788 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8789
8790 if Is_Entity_Name (Name (Func_Call)) then
8791 Func_Id := Entity (Name (Func_Call));
8792
8793 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8794 Func_Id := Etype (Name (Func_Call));
8795
8796 else
8797 raise Program_Error;
8798 end if;
8799
8800 Warn_BIP (Func_Call);
8801
8802 Result_Subt := Etype (Func_Id);
8803
8804 -- When the result subtype is unconstrained, an additional actual must
8805 -- be passed to indicate that the caller is providing the return object.
8806 -- This parameter must also be passed when the called function has a
8807 -- controlling result, because dispatching calls to the function needs
8808 -- to be treated effectively the same as calls to class-wide functions.
8809
8810 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8811 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8812
8813 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8814 (Func_Call, Func_Id);
8815
8816 Add_Task_Actuals_To_Build_In_Place_Call
8817 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8818
8819 -- Add an implicit actual to the function call that provides access to
8820 -- the caller's return object.
8821
8822 Add_Access_Actual_To_Build_In_Place_Call
8823 (Func_Call,
8824 Func_Id,
8825 Make_Unchecked_Type_Conversion (Loc,
8826 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8827 Expression => Relocate_Node (Lhs)));
8828
8829 -- Create an access type designating the function's result subtype
8830
8831 Ptr_Typ := Make_Temporary (Loc, 'A');
8832
8833 Ptr_Typ_Decl :=
8834 Make_Full_Type_Declaration (Loc,
8835 Defining_Identifier => Ptr_Typ,
8836 Type_Definition =>
8837 Make_Access_To_Object_Definition (Loc,
8838 All_Present => True,
8839 Subtype_Indication =>
8840 New_Occurrence_Of (Result_Subt, Loc)));
8841 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8842
8843 -- Finally, create an access object initialized to a reference to the
8844 -- function call. We know this access value is non-null, so mark the
8845 -- entity accordingly to suppress junk access checks.
8846
8847 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8848
8849 -- Add a conversion if it's the wrong type
8850
8851 if Etype (New_Expr) /= Ptr_Typ then
8852 New_Expr :=
8853 Make_Unchecked_Type_Conversion (Loc,
8854 New_Occurrence_Of (Ptr_Typ, Loc), New_Expr);
8855 end if;
8856
8857 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8858 Set_Etype (Obj_Id, Ptr_Typ);
8859 Set_Is_Known_Non_Null (Obj_Id);
8860
8861 Obj_Decl :=
8862 Make_Object_Declaration (Loc,
8863 Defining_Identifier => Obj_Id,
8864 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8865 Expression => New_Expr);
8866 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8867
8868 Rewrite (Assign, Make_Null_Statement (Loc));
8869 pragma Assert (Check_Number_Of_Actuals (Func_Call, Func_Id));
8870 pragma Assert (Check_BIP_Actuals (Func_Call, Func_Id));
8871 end Make_Build_In_Place_Call_In_Assignment;
8872
8873 ----------------------------------------------------
8874 -- Make_Build_In_Place_Call_In_Object_Declaration --
8875 ----------------------------------------------------
8876
8877 procedure Make_Build_In_Place_Call_In_Object_Declaration
8878 (Obj_Decl : Node_Id;
8879 Function_Call : Node_Id)
8880 is
8881 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id;
8882 -- Get the value of Function_Id, below
8883
8884 ---------------------
8885 -- Get_Function_Id --
8886 ---------------------
8887
8888 function Get_Function_Id (Func_Call : Node_Id) return Entity_Id is
8889 begin
8890 if Is_Entity_Name (Name (Func_Call)) then
8891 return Entity (Name (Func_Call));
8892
8893 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8894 return Etype (Name (Func_Call));
8895
8896 else
8897 raise Program_Error;
8898 end if;
8899 end Get_Function_Id;
8900
8901 -- Local variables
8902
8903 Func_Call : constant Node_Id := Unqual_Conv (Function_Call);
8904 Function_Id : constant Entity_Id := Get_Function_Id (Func_Call);
8905 Loc : constant Source_Ptr := Sloc (Function_Call);
8906 Obj_Loc : constant Source_Ptr := Sloc (Obj_Decl);
8907 Obj_Def_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
8908 Obj_Typ : constant Entity_Id := Etype (Obj_Def_Id);
8909 Encl_Func : constant Entity_Id := Enclosing_Subprogram (Obj_Def_Id);
8910 Result_Subt : constant Entity_Id := Etype (Function_Id);
8911
8912 Call_Deref : Node_Id;
8913 Caller_Object : Node_Id;
8914 Def_Id : Entity_Id;
8915 Designated_Type : Entity_Id;
8916 Fmaster_Actual : Node_Id := Empty;
8917 Pool_Actual : Node_Id;
8918 Ptr_Typ : Entity_Id;
8919 Ptr_Typ_Decl : Node_Id;
8920 Pass_Caller_Acc : Boolean := False;
8921 Res_Decl : Node_Id;
8922
8923 Definite : constant Boolean :=
8924 Caller_Known_Size (Func_Call, Result_Subt)
8925 and then not Is_Class_Wide_Type (Obj_Typ);
8926 -- In the case of "X : T'Class := F(...);", where F returns a
8927 -- Caller_Known_Size (specific) tagged type, we treat it as
8928 -- indefinite, because the code for the Definite case below sets the
8929 -- initialization expression of the object to Empty, which would be
8930 -- illegal Ada, and would cause gigi to misallocate X.
8931
8932 -- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
8933
8934 begin
8935 -- If the call has already been processed to add build-in-place actuals
8936 -- then return.
8937
8938 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8939 return;
8940 end if;
8941
8942 -- Mark the call as processed as a build-in-place call
8943
8944 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8945
8946 Warn_BIP (Func_Call);
8947
8948 -- Create an access type designating the function's result subtype.
8949 -- We use the type of the original call because it may be a call to an
8950 -- inherited operation, which the expansion has replaced with the parent
8951 -- operation that yields the parent type. Note that this access type
8952 -- must be declared before we establish a transient scope, so that it
8953 -- receives the proper accessibility level.
8954
8955 if Is_Class_Wide_Type (Obj_Typ)
8956 and then not Is_Interface (Obj_Typ)
8957 and then not Is_Class_Wide_Type (Etype (Function_Call))
8958 then
8959 Designated_Type := Obj_Typ;
8960 else
8961 Designated_Type := Etype (Function_Call);
8962 end if;
8963
8964 Ptr_Typ := Make_Temporary (Loc, 'A');
8965 Ptr_Typ_Decl :=
8966 Make_Full_Type_Declaration (Loc,
8967 Defining_Identifier => Ptr_Typ,
8968 Type_Definition =>
8969 Make_Access_To_Object_Definition (Loc,
8970 All_Present => True,
8971 Subtype_Indication =>
8972 New_Occurrence_Of (Designated_Type, Loc)));
8973
8974 -- The access type and its accompanying object must be inserted after
8975 -- the object declaration in the constrained case, so that the function
8976 -- call can be passed access to the object. In the indefinite case, or
8977 -- if the object declaration is for a return object, the access type and
8978 -- object must be inserted before the object, since the object
8979 -- declaration is rewritten to be a renaming of a dereference of the
8980 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8981 -- the result object is in a different (transient) scope, so won't cause
8982 -- freezing.
8983
8984 if Definite and then not Is_Return_Object (Obj_Def_Id) then
8985
8986 -- The presence of an address clause complicates the build-in-place
8987 -- expansion because the indicated address must be processed before
8988 -- the indirect call is generated (including the definition of a
8989 -- local pointer to the object). The address clause may come from
8990 -- an aspect specification or from an explicit attribute
8991 -- specification appearing after the object declaration. These two
8992 -- cases require different processing.
8993
8994 if Has_Aspect (Obj_Def_Id, Aspect_Address) then
8995
8996 -- Skip non-delayed pragmas that correspond to other aspects, if
8997 -- any, to find proper insertion point for freeze node of object.
8998
8999 declare
9000 D : Node_Id := Obj_Decl;
9001 N : Node_Id := Next (D);
9002
9003 begin
9004 while Present (N)
9005 and then Nkind (N) in N_Attribute_Reference | N_Pragma
9006 loop
9007 Analyze (N);
9008 D := N;
9009 Next (N);
9010 end loop;
9011
9012 Insert_After (D, Ptr_Typ_Decl);
9013
9014 -- Freeze object before pointer declaration, to ensure that
9015 -- generated attribute for address is inserted at the proper
9016 -- place.
9017
9018 Freeze_Before (Ptr_Typ_Decl, Obj_Def_Id);
9019 end;
9020
9021 Analyze (Ptr_Typ_Decl);
9022
9023 elsif Present (Following_Address_Clause (Obj_Decl)) then
9024
9025 -- Locate explicit address clause, which may also follow pragmas
9026 -- generated by other aspect specifications.
9027
9028 declare
9029 Addr : constant Node_Id := Following_Address_Clause (Obj_Decl);
9030 D : Node_Id := Next (Obj_Decl);
9031
9032 begin
9033 while Present (D) loop
9034 Analyze (D);
9035 exit when D = Addr;
9036 Next (D);
9037 end loop;
9038
9039 Insert_After_And_Analyze (Addr, Ptr_Typ_Decl);
9040 end;
9041
9042 else
9043 Insert_After_And_Analyze (Obj_Decl, Ptr_Typ_Decl);
9044 end if;
9045 else
9046 Insert_Action (Obj_Decl, Ptr_Typ_Decl);
9047 end if;
9048
9049 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
9050 -- elaborated in an inner (transient) scope and thus won't cause
9051 -- freezing by itself. It's not an itype, but it needs to be frozen
9052 -- inside the current subprogram (see Freeze_Outside in freeze.adb).
9053
9054 Freeze_Itype (Ptr_Typ, Ptr_Typ_Decl);
9055
9056 -- If the object is a return object of an enclosing build-in-place
9057 -- function, then the implicit build-in-place parameters of the
9058 -- enclosing function are simply passed along to the called function.
9059 -- (Unfortunately, this won't cover the case of extension aggregates
9060 -- where the ancestor part is a build-in-place indefinite function
9061 -- call that should be passed along the caller's parameters.
9062 -- Currently those get mishandled by reassigning the result of the
9063 -- call to the aggregate return object, when the call result should
9064 -- really be directly built in place in the aggregate and not in a
9065 -- temporary. ???)
9066
9067 if Is_Return_Object (Obj_Def_Id) then
9068 Pass_Caller_Acc := True;
9069
9070 -- When the enclosing function has a BIP_Alloc_Form formal then we
9071 -- pass it along to the callee (such as when the enclosing function
9072 -- has an unconstrained or tagged result type).
9073
9074 if Needs_BIP_Alloc_Form (Encl_Func) then
9075 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
9076 Pool_Actual :=
9077 New_Occurrence_Of
9078 (Build_In_Place_Formal
9079 (Encl_Func, BIP_Storage_Pool), Loc);
9080
9081 -- The build-in-place pool formal is not built on e.g. ZFP
9082
9083 else
9084 Pool_Actual := Empty;
9085 end if;
9086
9087 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9088 (Function_Call => Func_Call,
9089 Function_Id => Function_Id,
9090 Alloc_Form_Exp =>
9091 New_Occurrence_Of
9092 (Build_In_Place_Formal (Encl_Func, BIP_Alloc_Form), Loc),
9093 Pool_Actual => Pool_Actual);
9094
9095 -- Otherwise, if enclosing function has a definite result subtype,
9096 -- then caller allocation will be used.
9097
9098 else
9099 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9100 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9101 end if;
9102
9103 if Needs_BIP_Finalization_Master (Encl_Func) then
9104 Fmaster_Actual :=
9105 New_Occurrence_Of
9106 (Build_In_Place_Formal
9107 (Encl_Func, BIP_Finalization_Master), Loc);
9108 end if;
9109
9110 -- Retrieve the BIPacc formal from the enclosing function and convert
9111 -- it to the access type of the callee's BIP_Object_Access formal.
9112
9113 Caller_Object :=
9114 Make_Unchecked_Type_Conversion (Loc,
9115 Subtype_Mark =>
9116 New_Occurrence_Of
9117 (Etype (Build_In_Place_Formal
9118 (Function_Id, BIP_Object_Access)),
9119 Loc),
9120 Expression =>
9121 New_Occurrence_Of
9122 (Build_In_Place_Formal (Encl_Func, BIP_Object_Access),
9123 Loc));
9124
9125 -- In the definite case, add an implicit actual to the function call
9126 -- that provides access to the declared object. An unchecked conversion
9127 -- to the (specific) result type of the function is inserted to handle
9128 -- the case where the object is declared with a class-wide type.
9129
9130 elsif Definite then
9131 Caller_Object :=
9132 Make_Unchecked_Type_Conversion (Loc,
9133 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9134 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
9135
9136 -- When the function has a controlling result, an allocation-form
9137 -- parameter must be passed indicating that the caller is allocating
9138 -- the result object. This is needed because such a function can be
9139 -- called as a dispatching operation and must be treated similarly to
9140 -- functions with indefinite result subtypes.
9141
9142 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9143 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9144
9145 -- The allocation for indefinite library-level objects occurs on the
9146 -- heap as opposed to the secondary stack. This accommodates DLLs where
9147 -- the secondary stack is destroyed after each library unload. This is a
9148 -- hybrid mechanism where a stack-allocated object lives on the heap.
9149
9150 elsif Is_Library_Level_Entity (Obj_Def_Id)
9151 and then not Restriction_Active (No_Implicit_Heap_Allocations)
9152 then
9153 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9154 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
9155 Caller_Object := Empty;
9156
9157 -- Create a finalization master for the access result type to ensure
9158 -- that the heap allocation can properly chain the object and later
9159 -- finalize it when the library unit goes out of scope.
9160
9161 if Needs_Finalization (Etype (Func_Call)) then
9162 Build_Finalization_Master
9163 (Typ => Ptr_Typ,
9164 For_Lib_Level => True,
9165 Insertion_Node => Ptr_Typ_Decl);
9166
9167 Fmaster_Actual :=
9168 Make_Attribute_Reference (Loc,
9169 Prefix =>
9170 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
9171 Attribute_Name => Name_Unrestricted_Access);
9172 end if;
9173
9174 -- In other indefinite cases, pass an indication to do the allocation
9175 -- on the secondary stack and set Caller_Object to Empty so that a null
9176 -- value will be passed for the caller's object address. A transient
9177 -- scope is established to ensure eventual cleanup of the result.
9178
9179 else
9180 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9181 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9182 Caller_Object := Empty;
9183
9184 Establish_Transient_Scope (Obj_Decl, Manage_Sec_Stack => True);
9185 end if;
9186
9187 -- Pass along any finalization master actual, which is needed in the
9188 -- case where the called function initializes a return object of an
9189 -- enclosing build-in-place function.
9190
9191 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9192 (Func_Call => Func_Call,
9193 Func_Id => Function_Id,
9194 Master_Exp => Fmaster_Actual);
9195
9196 if Nkind (Parent (Obj_Decl)) = N_Extended_Return_Statement
9197 and then Needs_BIP_Task_Actuals (Function_Id)
9198 then
9199 -- Here we're passing along the master that was passed in to this
9200 -- function.
9201
9202 Add_Task_Actuals_To_Build_In_Place_Call
9203 (Func_Call, Function_Id,
9204 Master_Actual =>
9205 New_Occurrence_Of
9206 (Build_In_Place_Formal (Encl_Func, BIP_Task_Master), Loc));
9207
9208 else
9209 Add_Task_Actuals_To_Build_In_Place_Call
9210 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9211 end if;
9212
9213 Add_Access_Actual_To_Build_In_Place_Call
9214 (Func_Call,
9215 Function_Id,
9216 Caller_Object,
9217 Is_Access => Pass_Caller_Acc);
9218
9219 -- Finally, create an access object initialized to a reference to the
9220 -- function call. We know this access value cannot be null, so mark the
9221 -- entity accordingly to suppress the access check. We need to suppress
9222 -- warnings, because this can be part of the expansion of "for ... of"
9223 -- and similar constructs that generate finalization actions. Such
9224 -- finalization actions are safe, because they check a count that
9225 -- indicates which objects should be finalized, but the back end
9226 -- nonetheless warns about uninitialized objects.
9227
9228 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9229 Set_Warnings_Off (Def_Id);
9230 Set_Etype (Def_Id, Ptr_Typ);
9231 Set_Is_Known_Non_Null (Def_Id);
9232
9233 if Nkind (Function_Call) in N_Type_Conversion
9234 | N_Unchecked_Type_Conversion
9235 then
9236 Res_Decl :=
9237 Make_Object_Declaration (Loc,
9238 Defining_Identifier => Def_Id,
9239 Constant_Present => True,
9240 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9241 Expression =>
9242 Make_Unchecked_Type_Conversion (Loc,
9243 New_Occurrence_Of (Ptr_Typ, Loc),
9244 Make_Reference (Loc, Relocate_Node (Func_Call))));
9245 else
9246 Res_Decl :=
9247 Make_Object_Declaration (Loc,
9248 Defining_Identifier => Def_Id,
9249 Constant_Present => True,
9250 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9251 Expression =>
9252 Make_Reference (Loc, Relocate_Node (Func_Call)));
9253 end if;
9254
9255 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9256
9257 -- If the result subtype of the called function is definite and is not
9258 -- itself the return expression of an enclosing BIP function, then mark
9259 -- the object as having no initialization.
9260
9261 if Definite and then not Is_Return_Object (Obj_Def_Id) then
9262
9263 -- The related object declaration is encased in a transient block
9264 -- because the build-in-place function call contains at least one
9265 -- nested function call that produces a controlled transient
9266 -- temporary:
9267
9268 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9269
9270 -- Since the build-in-place expansion decouples the call from the
9271 -- object declaration, the finalization machinery lacks the context
9272 -- which prompted the generation of the transient block. To resolve
9273 -- this scenario, store the build-in-place call.
9274
9275 if Scope_Is_Transient then
9276 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9277 end if;
9278
9279 Set_Expression (Obj_Decl, Empty);
9280 Set_No_Initialization (Obj_Decl);
9281
9282 -- In case of an indefinite result subtype, or if the call is the
9283 -- return expression of an enclosing BIP function, rewrite the object
9284 -- declaration as an object renaming where the renamed object is a
9285 -- dereference of <function_Call>'reference:
9286 --
9287 -- Obj : Subt renames <function_call>'Ref.all;
9288
9289 else
9290 Call_Deref :=
9291 Make_Explicit_Dereference (Obj_Loc,
9292 Prefix => New_Occurrence_Of (Def_Id, Obj_Loc));
9293
9294 Rewrite (Obj_Decl,
9295 Make_Object_Renaming_Declaration (Obj_Loc,
9296 Defining_Identifier => Make_Temporary (Obj_Loc, 'D'),
9297 Subtype_Mark =>
9298 New_Occurrence_Of (Designated_Type, Obj_Loc),
9299 Name => Call_Deref));
9300
9301 -- At this point, Defining_Identifier (Obj_Decl) is no longer equal
9302 -- to Obj_Def_Id.
9303
9304 Set_Renamed_Object (Defining_Identifier (Obj_Decl), Call_Deref);
9305
9306 -- If the original entity comes from source, then mark the new
9307 -- entity as needing debug information, even though it's defined
9308 -- by a generated renaming that does not come from source, so that
9309 -- the Materialize_Entity flag will be set on the entity when
9310 -- Debug_Renaming_Declaration is called during analysis.
9311
9312 if Comes_From_Source (Obj_Def_Id) then
9313 Set_Debug_Info_Needed (Defining_Identifier (Obj_Decl));
9314 end if;
9315
9316 Analyze (Obj_Decl);
9317 Replace_Renaming_Declaration_Id
9318 (Obj_Decl, Original_Node (Obj_Decl));
9319 end if;
9320
9321 pragma Assert (Check_Number_Of_Actuals (Func_Call, Function_Id));
9322 pragma Assert (Check_BIP_Actuals (Func_Call, Function_Id));
9323 end Make_Build_In_Place_Call_In_Object_Declaration;
9324
9325 -------------------------------------------------
9326 -- Make_Build_In_Place_Iface_Call_In_Allocator --
9327 -------------------------------------------------
9328
9329 procedure Make_Build_In_Place_Iface_Call_In_Allocator
9330 (Allocator : Node_Id;
9331 Function_Call : Node_Id)
9332 is
9333 BIP_Func_Call : constant Node_Id :=
9334 Unqual_BIP_Iface_Function_Call (Function_Call);
9335 Loc : constant Source_Ptr := Sloc (Function_Call);
9336
9337 Anon_Type : Entity_Id;
9338 Tmp_Decl : Node_Id;
9339 Tmp_Id : Entity_Id;
9340
9341 begin
9342 -- No action if the call has already been processed
9343
9344 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9345 return;
9346 end if;
9347
9348 Tmp_Id := Make_Temporary (Loc, 'D');
9349
9350 -- Insert a temporary before N initialized with the BIP function call
9351 -- without its enclosing type conversions and analyze it without its
9352 -- expansion. This temporary facilitates us reusing the BIP machinery,
9353 -- which takes care of adding the extra build-in-place actuals and
9354 -- transforms this object declaration into an object renaming
9355 -- declaration.
9356
9357 Anon_Type := Create_Itype (E_Anonymous_Access_Type, Function_Call);
9358 Set_Directly_Designated_Type (Anon_Type, Etype (BIP_Func_Call));
9359 Set_Etype (Anon_Type, Anon_Type);
9360 Build_Class_Wide_Master (Anon_Type);
9361
9362 Tmp_Decl :=
9363 Make_Object_Declaration (Loc,
9364 Defining_Identifier => Tmp_Id,
9365 Object_Definition => New_Occurrence_Of (Anon_Type, Loc),
9366 Expression =>
9367 Make_Allocator (Loc,
9368 Expression =>
9369 Make_Qualified_Expression (Loc,
9370 Subtype_Mark =>
9371 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9372 Expression => New_Copy_Tree (BIP_Func_Call))));
9373
9374 -- Manually set the associated node for the anonymous access type to
9375 -- be its local declaration, to avoid confusing and complicating
9376 -- the accessibility machinery.
9377
9378 Set_Associated_Node_For_Itype (Anon_Type, Tmp_Decl);
9379
9380 Expander_Mode_Save_And_Set (False);
9381 Insert_Action (Allocator, Tmp_Decl);
9382 Expander_Mode_Restore;
9383
9384 Make_Build_In_Place_Call_In_Allocator
9385 (Allocator => Expression (Tmp_Decl),
9386 Function_Call => Expression (Expression (Tmp_Decl)));
9387
9388 -- Add a conversion to displace the pointer to the allocated object
9389 -- to reference the corresponding dispatch table.
9390
9391 Rewrite (Allocator,
9392 Convert_To (Etype (Allocator),
9393 New_Occurrence_Of (Tmp_Id, Loc)));
9394 end Make_Build_In_Place_Iface_Call_In_Allocator;
9395
9396 ---------------------------------------------------------
9397 -- Make_Build_In_Place_Iface_Call_In_Anonymous_Context --
9398 ---------------------------------------------------------
9399
9400 procedure Make_Build_In_Place_Iface_Call_In_Anonymous_Context
9401 (Function_Call : Node_Id)
9402 is
9403 BIP_Func_Call : constant Node_Id :=
9404 Unqual_BIP_Iface_Function_Call (Function_Call);
9405 Loc : constant Source_Ptr := Sloc (Function_Call);
9406
9407 Tmp_Decl : Node_Id;
9408 Tmp_Id : Entity_Id;
9409
9410 begin
9411 -- No action of the call has already been processed
9412
9413 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9414 return;
9415 end if;
9416
9417 pragma Assert (Needs_Finalization (Etype (BIP_Func_Call)));
9418
9419 -- Insert a temporary before the call initialized with function call to
9420 -- reuse the BIP machinery which takes care of adding the extra build-in
9421 -- place actuals and transforms this object declaration into an object
9422 -- renaming declaration.
9423
9424 Tmp_Id := Make_Temporary (Loc, 'D');
9425
9426 Tmp_Decl :=
9427 Make_Object_Declaration (Loc,
9428 Defining_Identifier => Tmp_Id,
9429 Object_Definition =>
9430 New_Occurrence_Of (Etype (Function_Call), Loc),
9431 Expression => Relocate_Node (Function_Call));
9432
9433 Expander_Mode_Save_And_Set (False);
9434 Insert_Action (Function_Call, Tmp_Decl);
9435 Expander_Mode_Restore;
9436
9437 Make_Build_In_Place_Iface_Call_In_Object_Declaration
9438 (Obj_Decl => Tmp_Decl,
9439 Function_Call => Expression (Tmp_Decl));
9440 end Make_Build_In_Place_Iface_Call_In_Anonymous_Context;
9441
9442 ----------------------------------------------------------
9443 -- Make_Build_In_Place_Iface_Call_In_Object_Declaration --
9444 ----------------------------------------------------------
9445
9446 procedure Make_Build_In_Place_Iface_Call_In_Object_Declaration
9447 (Obj_Decl : Node_Id;
9448 Function_Call : Node_Id)
9449 is
9450 BIP_Func_Call : constant Node_Id :=
9451 Unqual_BIP_Iface_Function_Call (Function_Call);
9452 Loc : constant Source_Ptr := Sloc (Function_Call);
9453 Obj_Id : constant Entity_Id := Defining_Entity (Obj_Decl);
9454
9455 Tmp_Decl : Node_Id;
9456 Tmp_Id : Entity_Id;
9457
9458 begin
9459 -- No action of the call has already been processed
9460
9461 if Is_Expanded_Build_In_Place_Call (BIP_Func_Call) then
9462 return;
9463 end if;
9464
9465 Tmp_Id := Make_Temporary (Loc, 'D');
9466
9467 -- Insert a temporary before N initialized with the BIP function call
9468 -- without its enclosing type conversions and analyze it without its
9469 -- expansion. This temporary facilitates us reusing the BIP machinery,
9470 -- which takes care of adding the extra build-in-place actuals and
9471 -- transforms this object declaration into an object renaming
9472 -- declaration.
9473
9474 Tmp_Decl :=
9475 Make_Object_Declaration (Loc,
9476 Defining_Identifier => Tmp_Id,
9477 Object_Definition =>
9478 New_Occurrence_Of (Etype (BIP_Func_Call), Loc),
9479 Expression => New_Copy_Tree (BIP_Func_Call));
9480
9481 Expander_Mode_Save_And_Set (False);
9482 Insert_Action (Obj_Decl, Tmp_Decl);
9483 Expander_Mode_Restore;
9484
9485 Make_Build_In_Place_Call_In_Object_Declaration
9486 (Obj_Decl => Tmp_Decl,
9487 Function_Call => Expression (Tmp_Decl));
9488
9489 pragma Assert (Nkind (Tmp_Decl) = N_Object_Renaming_Declaration);
9490
9491 -- Replace the original build-in-place function call by a reference to
9492 -- the resulting temporary object renaming declaration. In this way,
9493 -- all the interface conversions performed in the original Function_Call
9494 -- on the build-in-place object are preserved.
9495
9496 Rewrite (BIP_Func_Call, New_Occurrence_Of (Tmp_Id, Loc));
9497
9498 -- Replace the original object declaration by an internal object
9499 -- renaming declaration. This leaves the generated code more clean (the
9500 -- build-in-place function call in an object renaming declaration and
9501 -- displacements of the pointer to the build-in-place object in another
9502 -- renaming declaration) and allows us to invoke the routine that takes
9503 -- care of replacing the identifier of the renaming declaration (routine
9504 -- originally developed for the regular build-in-place management).
9505
9506 Rewrite (Obj_Decl,
9507 Make_Object_Renaming_Declaration (Loc,
9508 Defining_Identifier => Make_Temporary (Loc, 'D'),
9509 Subtype_Mark => New_Occurrence_Of (Etype (Obj_Id), Loc),
9510 Name => Function_Call));
9511 Analyze (Obj_Decl);
9512
9513 Replace_Renaming_Declaration_Id (Obj_Decl, Original_Node (Obj_Decl));
9514 end Make_Build_In_Place_Iface_Call_In_Object_Declaration;
9515
9516 --------------------------------------------
9517 -- Make_CPP_Constructor_Call_In_Allocator --
9518 --------------------------------------------
9519
9520 procedure Make_CPP_Constructor_Call_In_Allocator
9521 (Allocator : Node_Id;
9522 Function_Call : Node_Id)
9523 is
9524 Loc : constant Source_Ptr := Sloc (Function_Call);
9525 Acc_Type : constant Entity_Id := Etype (Allocator);
9526 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9527 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9528
9529 New_Allocator : Node_Id;
9530 Return_Obj_Access : Entity_Id;
9531 Tmp_Obj : Node_Id;
9532
9533 begin
9534 pragma Assert (Nkind (Allocator) = N_Allocator
9535 and then Nkind (Function_Call) = N_Function_Call);
9536 pragma Assert (Convention (Function_Id) = Convention_CPP
9537 and then Is_Constructor (Function_Id));
9538 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9539
9540 -- Replace the initialized allocator of form "new T'(Func (...))" with
9541 -- an uninitialized allocator of form "new T", where T is the result
9542 -- subtype of the called function. The call to the function is handled
9543 -- separately further below.
9544
9545 New_Allocator :=
9546 Make_Allocator (Loc,
9547 Expression => New_Occurrence_Of (Result_Subt, Loc));
9548 Set_No_Initialization (New_Allocator);
9549
9550 -- Copy attributes to new allocator. Note that the new allocator
9551 -- logically comes from source if the original one did, so copy the
9552 -- relevant flag. This ensures proper treatment of the restriction
9553 -- No_Implicit_Heap_Allocations in this case.
9554
9555 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9556 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9557 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9558
9559 Rewrite (Allocator, New_Allocator);
9560
9561 -- Create a new access object and initialize it to the result of the
9562 -- new uninitialized allocator. Note: we do not use Allocator as the
9563 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9564 -- as this would create a sort of infinite "recursion".
9565
9566 Return_Obj_Access := Make_Temporary (Loc, 'R');
9567 Set_Etype (Return_Obj_Access, Acc_Type);
9568
9569 -- Generate:
9570 -- Rnnn : constant ptr_T := new (T);
9571 -- Init (Rnn.all,...);
9572
9573 Tmp_Obj :=
9574 Make_Object_Declaration (Loc,
9575 Defining_Identifier => Return_Obj_Access,
9576 Constant_Present => True,
9577 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9578 Expression => Relocate_Node (Allocator));
9579 Insert_Action (Allocator, Tmp_Obj);
9580
9581 Insert_List_After_And_Analyze (Tmp_Obj,
9582 Build_Initialization_Call (Loc,
9583 Id_Ref =>
9584 Make_Explicit_Dereference (Loc,
9585 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9586 Typ => Etype (Function_Id),
9587 Constructor_Ref => Function_Call));
9588
9589 -- Finally, replace the allocator node with a reference to the result of
9590 -- the function call itself (which will effectively be an access to the
9591 -- object created by the allocator).
9592
9593 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9594
9595 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9596 -- generate an implicit conversion to force displacement of the "this"
9597 -- pointer.
9598
9599 if Is_Interface (Designated_Type (Acc_Type)) then
9600 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9601 end if;
9602
9603 Analyze_And_Resolve (Allocator, Acc_Type);
9604 end Make_CPP_Constructor_Call_In_Allocator;
9605
9606 ----------------------
9607 -- Might_Have_Tasks --
9608 ----------------------
9609
9610 function Might_Have_Tasks (Typ : Entity_Id) return Boolean is
9611 begin
9612 return not Global_No_Tasking
9613 and then not No_Run_Time_Mode
9614 and then (Has_Task (Typ)
9615 or else (Is_Class_Wide_Type (Typ)
9616 and then Is_Limited_Record (Typ)));
9617 end Might_Have_Tasks;
9618
9619 ----------------------------
9620 -- Needs_BIP_Task_Actuals --
9621 ----------------------------
9622
9623 function Needs_BIP_Task_Actuals (Func_Id : Entity_Id) return Boolean is
9624 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9625 Subp_Id : Entity_Id;
9626 Func_Typ : Entity_Id;
9627
9628 begin
9629 if Global_No_Tasking or else No_Run_Time_Mode then
9630 return False;
9631 end if;
9632
9633 -- For thunks we must rely on their target entity; otherwise, given that
9634 -- the profile of thunks for functions returning a limited interface
9635 -- type returns a class-wide type, we would erroneously add these extra
9636 -- formals.
9637
9638 if Is_Thunk (Func_Id) then
9639 Subp_Id := Thunk_Entity (Func_Id);
9640
9641 -- Common case
9642
9643 else
9644 Subp_Id := Func_Id;
9645 end if;
9646
9647 Func_Typ := Underlying_Type (Etype (Subp_Id));
9648
9649 -- At first sight, for all the following cases, we could add assertions
9650 -- to ensure that if Func_Id is frozen then the computed result matches
9651 -- with the availability of the task master extra formal; unfortunately
9652 -- this is not feasible because we may be precisely freezing this entity
9653 -- (that is, Is_Frozen has been set by Freeze_Entity but it has not
9654 -- completed its work).
9655
9656 if Has_Task (Func_Typ) then
9657 return True;
9658
9659 elsif Ekind (Func_Id) = E_Function then
9660 return Might_Have_Tasks (Func_Typ);
9661
9662 -- Handle subprogram type internally generated for dispatching call. We
9663 -- cannot rely on the return type of the subprogram type of dispatching
9664 -- calls since it is always a class-wide type (cf. Expand_Dispatching_
9665 -- Call).
9666
9667 elsif Ekind (Func_Id) = E_Subprogram_Type then
9668 if Is_Dispatch_Table_Entity (Func_Id) then
9669 return Has_BIP_Extra_Formal (Func_Id, BIP_Task_Master);
9670 else
9671 return Might_Have_Tasks (Func_Typ);
9672 end if;
9673
9674 else
9675 raise Program_Error;
9676 end if;
9677 end Needs_BIP_Task_Actuals;
9678
9679 -----------------------------------
9680 -- Needs_BIP_Finalization_Master --
9681 -----------------------------------
9682
9683 function Needs_BIP_Finalization_Master
9684 (Func_Id : Entity_Id) return Boolean
9685 is
9686 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9687 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9688 begin
9689 -- A formal giving the finalization master is needed for build-in-place
9690 -- functions whose result type needs finalization or is a tagged type.
9691 -- Tagged primitive build-in-place functions need such a formal because
9692 -- they can be called by a dispatching call, and extensions may require
9693 -- finalization even if the root type doesn't. This means they're also
9694 -- needed for tagged nonprimitive build-in-place functions with tagged
9695 -- results, since such functions can be called via access-to-function
9696 -- types, and those can be used to call primitives, so masters have to
9697 -- be passed to all such build-in-place functions, primitive or not.
9698
9699 return
9700 not Restriction_Active (No_Finalization)
9701 and then (Needs_Finalization (Func_Typ)
9702 or else Is_Tagged_Type (Func_Typ));
9703 end Needs_BIP_Finalization_Master;
9704
9705 --------------------------
9706 -- Needs_BIP_Alloc_Form --
9707 --------------------------
9708
9709 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9710 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9711 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9712 begin
9713 return Requires_Transient_Scope (Func_Typ);
9714 end Needs_BIP_Alloc_Form;
9715
9716 -------------------------------------
9717 -- Replace_Renaming_Declaration_Id --
9718 -------------------------------------
9719
9720 procedure Replace_Renaming_Declaration_Id
9721 (New_Decl : Node_Id;
9722 Orig_Decl : Node_Id)
9723 is
9724 New_Id : constant Entity_Id := Defining_Entity (New_Decl);
9725 Orig_Id : constant Entity_Id := Defining_Entity (Orig_Decl);
9726
9727 begin
9728 Set_Chars (New_Id, Chars (Orig_Id));
9729
9730 -- Swap next entity links in preparation for exchanging entities
9731
9732 declare
9733 Next_Id : constant Entity_Id := Next_Entity (New_Id);
9734 begin
9735 Link_Entities (New_Id, Next_Entity (Orig_Id));
9736 Link_Entities (Orig_Id, Next_Id);
9737 end;
9738
9739 Set_Homonym (New_Id, Homonym (Orig_Id));
9740 Exchange_Entities (New_Id, Orig_Id);
9741
9742 -- Preserve source indication of original declaration, so that xref
9743 -- information is properly generated for the right entity.
9744
9745 Preserve_Comes_From_Source (New_Decl, Orig_Decl);
9746 Preserve_Comes_From_Source (Orig_Id, Orig_Decl);
9747
9748 Set_Comes_From_Source (New_Id, False);
9749 end Replace_Renaming_Declaration_Id;
9750
9751 ---------------------------------
9752 -- Rewrite_Function_Call_For_C --
9753 ---------------------------------
9754
9755 procedure Rewrite_Function_Call_For_C (N : Node_Id) is
9756 Orig_Func : constant Entity_Id := Entity (Name (N));
9757 Func_Id : constant Entity_Id := Ultimate_Alias (Orig_Func);
9758 Par : constant Node_Id := Parent (N);
9759 Proc_Id : constant Entity_Id := Corresponding_Procedure (Func_Id);
9760 Loc : constant Source_Ptr := Sloc (Par);
9761 Actuals : List_Id;
9762 Last_Actual : Node_Id;
9763 Last_Formal : Entity_Id;
9764
9765 -- Start of processing for Rewrite_Function_Call_For_C
9766
9767 begin
9768 -- The actuals may be given by named associations, so the added actual
9769 -- that is the target of the return value of the call must be a named
9770 -- association as well, so we retrieve the name of the generated
9771 -- out_formal.
9772
9773 Last_Formal := First_Formal (Proc_Id);
9774 while Present (Next_Formal (Last_Formal)) loop
9775 Next_Formal (Last_Formal);
9776 end loop;
9777
9778 Actuals := Parameter_Associations (N);
9779
9780 -- The original function may lack parameters
9781
9782 if No (Actuals) then
9783 Actuals := New_List;
9784 end if;
9785
9786 -- If the function call is the expression of an assignment statement,
9787 -- transform the assignment into a procedure call. Generate:
9788
9789 -- LHS := Func_Call (...);
9790
9791 -- Proc_Call (..., LHS);
9792
9793 -- If function is inherited, a conversion may be necessary.
9794
9795 if Nkind (Par) = N_Assignment_Statement then
9796 Last_Actual := Name (Par);
9797
9798 if not Comes_From_Source (Orig_Func)
9799 and then Etype (Orig_Func) /= Etype (Func_Id)
9800 then
9801 Last_Actual :=
9802 Make_Type_Conversion (Loc,
9803 New_Occurrence_Of (Etype (Func_Id), Loc),
9804 Last_Actual);
9805 end if;
9806
9807 Append_To (Actuals,
9808 Make_Parameter_Association (Loc,
9809 Selector_Name =>
9810 Make_Identifier (Loc, Chars (Last_Formal)),
9811 Explicit_Actual_Parameter => Last_Actual));
9812
9813 Rewrite (Par,
9814 Make_Procedure_Call_Statement (Loc,
9815 Name => New_Occurrence_Of (Proc_Id, Loc),
9816 Parameter_Associations => Actuals));
9817 Analyze (Par);
9818
9819 -- Otherwise the context is an expression. Generate a temporary and a
9820 -- procedure call to obtain the function result. Generate:
9821
9822 -- ... Func_Call (...) ...
9823
9824 -- Temp : ...;
9825 -- Proc_Call (..., Temp);
9826 -- ... Temp ...
9827
9828 else
9829 declare
9830 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
9831 Call : Node_Id;
9832 Decl : Node_Id;
9833
9834 begin
9835 -- Generate:
9836 -- Temp : ...;
9837
9838 Decl :=
9839 Make_Object_Declaration (Loc,
9840 Defining_Identifier => Temp_Id,
9841 Object_Definition =>
9842 New_Occurrence_Of (Etype (Func_Id), Loc));
9843
9844 -- Generate:
9845 -- Proc_Call (..., Temp);
9846
9847 Append_To (Actuals,
9848 Make_Parameter_Association (Loc,
9849 Selector_Name =>
9850 Make_Identifier (Loc, Chars (Last_Formal)),
9851 Explicit_Actual_Parameter =>
9852 New_Occurrence_Of (Temp_Id, Loc)));
9853
9854 Call :=
9855 Make_Procedure_Call_Statement (Loc,
9856 Name => New_Occurrence_Of (Proc_Id, Loc),
9857 Parameter_Associations => Actuals);
9858
9859 Insert_Actions (Par, New_List (Decl, Call));
9860 Rewrite (N, New_Occurrence_Of (Temp_Id, Loc));
9861 end;
9862 end if;
9863 end Rewrite_Function_Call_For_C;
9864
9865 ------------------------------------
9866 -- Set_Enclosing_Sec_Stack_Return --
9867 ------------------------------------
9868
9869 procedure Set_Enclosing_Sec_Stack_Return (N : Node_Id) is
9870 P : Node_Id := N;
9871
9872 begin
9873 -- Due to a possible mix of internally generated blocks, source blocks
9874 -- and loops, the scope stack may not be contiguous as all labels are
9875 -- inserted at the top level within the related function. Instead,
9876 -- perform a parent-based traversal and mark all appropriate constructs.
9877
9878 while Present (P) loop
9879
9880 -- Mark the label of a source or internally generated block or
9881 -- loop.
9882
9883 if Nkind (P) in N_Block_Statement | N_Loop_Statement then
9884 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
9885
9886 -- Mark the enclosing function
9887
9888 elsif Nkind (P) = N_Subprogram_Body then
9889 if Present (Corresponding_Spec (P)) then
9890 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
9891 else
9892 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
9893 end if;
9894
9895 -- Do not go beyond the enclosing function
9896
9897 exit;
9898 end if;
9899
9900 P := Parent (P);
9901 end loop;
9902 end Set_Enclosing_Sec_Stack_Return;
9903
9904 ------------------------------------
9905 -- Unqual_BIP_Iface_Function_Call --
9906 ------------------------------------
9907
9908 function Unqual_BIP_Iface_Function_Call (Expr : Node_Id) return Node_Id is
9909 Has_Pointer_Displacement : Boolean := False;
9910 On_Object_Declaration : Boolean := False;
9911 -- Remember if processing the renaming expressions on recursion we have
9912 -- traversed an object declaration, since we can traverse many object
9913 -- declaration renamings but just one regular object declaration.
9914
9915 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id;
9916 -- Search for a build-in-place function call skipping any qualification
9917 -- including qualified expressions, type conversions, references, calls
9918 -- to displace the pointer to the object, and renamings. Return Empty if
9919 -- no build-in-place function call is found.
9920
9921 ------------------------------
9922 -- Unqual_BIP_Function_Call --
9923 ------------------------------
9924
9925 function Unqual_BIP_Function_Call (Expr : Node_Id) return Node_Id is
9926 begin
9927 -- Recurse to handle case of multiple levels of qualification and/or
9928 -- conversion.
9929
9930 if Nkind (Expr) in N_Qualified_Expression
9931 | N_Type_Conversion
9932 | N_Unchecked_Type_Conversion
9933 then
9934 return Unqual_BIP_Function_Call (Expression (Expr));
9935
9936 -- Recurse to handle case of multiple levels of references and
9937 -- explicit dereferences.
9938
9939 elsif Nkind (Expr) in N_Attribute_Reference
9940 | N_Explicit_Dereference
9941 | N_Reference
9942 then
9943 return Unqual_BIP_Function_Call (Prefix (Expr));
9944
9945 -- Recurse on object renamings
9946
9947 elsif Nkind (Expr) = N_Identifier
9948 and then Present (Entity (Expr))
9949 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9950 and then Nkind (Parent (Entity (Expr))) =
9951 N_Object_Renaming_Declaration
9952 and then Present (Renamed_Object (Entity (Expr)))
9953 then
9954 return Unqual_BIP_Function_Call (Renamed_Object (Entity (Expr)));
9955
9956 -- Recurse on the initializing expression of the first reference of
9957 -- an object declaration.
9958
9959 elsif not On_Object_Declaration
9960 and then Nkind (Expr) = N_Identifier
9961 and then Present (Entity (Expr))
9962 and then Ekind (Entity (Expr)) in E_Constant | E_Variable
9963 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
9964 and then Present (Expression (Parent (Entity (Expr))))
9965 then
9966 On_Object_Declaration := True;
9967 return
9968 Unqual_BIP_Function_Call (Expression (Parent (Entity (Expr))));
9969
9970 -- Recurse to handle calls to displace the pointer to the object to
9971 -- reference a secondary dispatch table.
9972
9973 elsif Nkind (Expr) = N_Function_Call
9974 and then Nkind (Name (Expr)) in N_Has_Entity
9975 and then Present (Entity (Name (Expr)))
9976 and then Is_RTE (Entity (Name (Expr)), RE_Displace)
9977 then
9978 Has_Pointer_Displacement := True;
9979 return
9980 Unqual_BIP_Function_Call (First (Parameter_Associations (Expr)));
9981
9982 -- Normal case: check if the inner expression is a BIP function call
9983 -- and the pointer to the object is displaced.
9984
9985 elsif Has_Pointer_Displacement
9986 and then Is_Build_In_Place_Function_Call (Expr)
9987 then
9988 return Expr;
9989
9990 else
9991 return Empty;
9992 end if;
9993 end Unqual_BIP_Function_Call;
9994
9995 -- Start of processing for Unqual_BIP_Iface_Function_Call
9996
9997 begin
9998 if Nkind (Expr) = N_Identifier and then No (Entity (Expr)) then
9999
10000 -- Can happen for X'Elab_Spec in the binder-generated file
10001
10002 return Empty;
10003 end if;
10004
10005 return Unqual_BIP_Function_Call (Expr);
10006 end Unqual_BIP_Iface_Function_Call;
10007
10008 --------------
10009 -- Warn_BIP --
10010 --------------
10011
10012 procedure Warn_BIP (Func_Call : Node_Id) is
10013 begin
10014 if Debug_Flag_Underscore_BB then
10015 Error_Msg_N ("build-in-place function call??", Func_Call);
10016 end if;
10017 end Warn_BIP;
10018
10019 end Exp_Ch6;