Debian Codegraph @

gcc/ada/exp_ch6.adb

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