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