Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobd48544fdadae6d03e1e87bad571d3bbd69e40dc9
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-2013, 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 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Exp_Aggr; use Exp_Aggr;
34 with Exp_Atag; use Exp_Atag;
35 with Exp_Ch2; use Exp_Ch2;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Dbug; use Exp_Dbug;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Intr; use Exp_Intr;
43 with Exp_Pakd; use Exp_Pakd;
44 with Exp_Tss; use Exp_Tss;
45 with Exp_Util; use Exp_Util;
46 with Exp_VFpt; use Exp_VFpt;
47 with Fname; use Fname;
48 with Freeze; use Freeze;
49 with Inline; use Inline;
50 with Lib; use Lib;
51 with Namet; use Namet;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Output; use Output;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aux; use Sem_Aux;
61 with Sem_Ch6; use Sem_Ch6;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch12; use Sem_Ch12;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Mech; use Sem_Mech;
70 with Sem_Res; use Sem_Res;
71 with Sem_SCIL; use Sem_SCIL;
72 with Sem_Util; use Sem_Util;
73 with Sinfo; use Sinfo;
74 with Sinput; use Sinput;
75 with Snames; use Snames;
76 with Stand; use Stand;
77 with Stringt; use Stringt;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Uintp; use Uintp;
81 with Validsw; use Validsw;
83 package body Exp_Ch6 is
85 Inlined_Calls : Elist_Id := No_Elist;
86 Backend_Calls : Elist_Id := No_Elist;
87 -- List of frontend inlined calls and inline calls passed to the backend
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
93 procedure Add_Access_Actual_To_Build_In_Place_Call
94 (Function_Call : Node_Id;
95 Function_Id : Entity_Id;
96 Return_Object : Node_Id;
97 Is_Access : Boolean := False);
98 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
99 -- object name given by Return_Object and add the attribute to the end of
100 -- the actual parameter list associated with the build-in-place function
101 -- call denoted by Function_Call. However, if Is_Access is True, then
102 -- Return_Object is already an access expression, in which case it's passed
103 -- along directly to the build-in-place function. Finally, if Return_Object
104 -- is empty, then pass a null literal as the actual.
106 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
107 (Function_Call : Node_Id;
108 Function_Id : Entity_Id;
109 Alloc_Form : BIP_Allocation_Form := Unspecified;
110 Alloc_Form_Exp : Node_Id := Empty;
111 Pool_Actual : Node_Id := Make_Null (No_Location));
112 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
113 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
114 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
115 -- otherwise pass a literal corresponding to the Alloc_Form parameter
116 -- (which must not be Unspecified in that case). Pool_Actual is the
117 -- parameter to pass to BIP_Storage_Pool.
119 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
120 (Func_Call : Node_Id;
121 Func_Id : Entity_Id;
122 Ptr_Typ : Entity_Id := Empty;
123 Master_Exp : Node_Id := Empty);
124 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
125 -- finalization actions, add an actual parameter which is a pointer to the
126 -- finalization master of the caller. If Master_Exp is not Empty, then that
127 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
128 -- will result in an automatic "null" value for the actual.
130 procedure Add_Task_Actuals_To_Build_In_Place_Call
131 (Function_Call : Node_Id;
132 Function_Id : Entity_Id;
133 Master_Actual : Node_Id);
134 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
135 -- contains tasks, add two actual parameters: the master, and a pointer to
136 -- the caller's activation chain. Master_Actual is the actual parameter
137 -- expression to pass for the master. In most cases, this is the current
138 -- master (_master). The two exceptions are: If the function call is the
139 -- initialization expression for an allocator, we pass the master of the
140 -- access type. If the function call is the initialization expression for a
141 -- return object, we pass along the master passed in by the caller. The
142 -- activation chain to pass is always the local one. Note: Master_Actual
143 -- can be Empty, but only if there are no tasks.
145 procedure Check_Overriding_Operation (Subp : Entity_Id);
146 -- Subp is a dispatching operation. Check whether it may override an
147 -- inherited private operation, in which case its DT entry is that of
148 -- the hidden operation, not the one it may have received earlier.
149 -- This must be done before emitting the code to set the corresponding
150 -- DT to the address of the subprogram. The actual placement of Subp in
151 -- the proper place in the list of primitive operations is done in
152 -- Declare_Inherited_Private_Subprograms, which also has to deal with
153 -- implicit operations. This duplication is unavoidable for now???
155 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
156 -- This procedure is called only if the subprogram body N, whose spec
157 -- has the given entity Spec, contains a parameterless recursive call.
158 -- It attempts to generate runtime code to detect if this a case of
159 -- infinite recursion.
161 -- The body is scanned to determine dependencies. If the only external
162 -- dependencies are on a small set of scalar variables, then the values
163 -- of these variables are captured on entry to the subprogram, and if
164 -- the values are not changed for the call, we know immediately that
165 -- we have an infinite recursion.
167 procedure Expand_Ctrl_Function_Call (N : Node_Id);
168 -- N is a function call which returns a controlled object. Transform the
169 -- call into a temporary which retrieves the returned object from the
170 -- secondary stack using 'reference.
172 procedure Expand_Inlined_Call
173 (N : Node_Id;
174 Subp : Entity_Id;
175 Orig_Subp : Entity_Id);
176 -- If called subprogram can be inlined by the front-end, retrieve the
177 -- analyzed body, replace formals with actuals and expand call in place.
178 -- Generate thunks for actuals that are expressions, and insert the
179 -- corresponding constant declarations before the call. If the original
180 -- call is to a derived operation, the return type is the one of the
181 -- derived operation, but the body is that of the original, so return
182 -- expressions in the body must be converted to the desired type (which
183 -- is simply not noted in the tree without inline expansion).
185 procedure Expand_Non_Function_Return (N : Node_Id);
186 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
187 -- a procedure body, entry body, accept statement, or extended return
188 -- statement. Note that all non-function returns are simple return
189 -- statements.
191 function Expand_Protected_Object_Reference
192 (N : Node_Id;
193 Scop : Entity_Id) return Node_Id;
195 procedure Expand_Protected_Subprogram_Call
196 (N : Node_Id;
197 Subp : Entity_Id;
198 Scop : Entity_Id);
199 -- A call to a protected subprogram within the protected object may appear
200 -- as a regular call. The list of actuals must be expanded to contain a
201 -- reference to the object itself, and the call becomes a call to the
202 -- corresponding protected subprogram.
204 function Has_Unconstrained_Access_Discriminants
205 (Subtyp : Entity_Id) return Boolean;
206 -- Returns True if the given subtype is unconstrained and has one
207 -- or more access discriminants.
209 procedure Expand_Simple_Function_Return (N : Node_Id);
210 -- Expand simple return from function. In the case where we are returning
211 -- from a function body this is called by Expand_N_Simple_Return_Statement.
213 ----------------------------------------------
214 -- Add_Access_Actual_To_Build_In_Place_Call --
215 ----------------------------------------------
217 procedure Add_Access_Actual_To_Build_In_Place_Call
218 (Function_Call : Node_Id;
219 Function_Id : Entity_Id;
220 Return_Object : Node_Id;
221 Is_Access : Boolean := False)
223 Loc : constant Source_Ptr := Sloc (Function_Call);
224 Obj_Address : Node_Id;
225 Obj_Acc_Formal : Entity_Id;
227 begin
228 -- Locate the implicit access parameter in the called function
230 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
232 -- If no return object is provided, then pass null
234 if not Present (Return_Object) then
235 Obj_Address := Make_Null (Loc);
236 Set_Parent (Obj_Address, Function_Call);
238 -- If Return_Object is already an expression of an access type, then use
239 -- it directly, since it must be an access value denoting the return
240 -- object, and couldn't possibly be the return object itself.
242 elsif Is_Access then
243 Obj_Address := Return_Object;
244 Set_Parent (Obj_Address, Function_Call);
246 -- Apply Unrestricted_Access to caller's return object
248 else
249 Obj_Address :=
250 Make_Attribute_Reference (Loc,
251 Prefix => Return_Object,
252 Attribute_Name => Name_Unrestricted_Access);
254 Set_Parent (Return_Object, Obj_Address);
255 Set_Parent (Obj_Address, Function_Call);
256 end if;
258 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
260 -- Build the parameter association for the new actual and add it to the
261 -- end of the function's actuals.
263 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
264 end Add_Access_Actual_To_Build_In_Place_Call;
266 ------------------------------------------------------
267 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
268 ------------------------------------------------------
270 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
271 (Function_Call : Node_Id;
272 Function_Id : Entity_Id;
273 Alloc_Form : BIP_Allocation_Form := Unspecified;
274 Alloc_Form_Exp : Node_Id := Empty;
275 Pool_Actual : Node_Id := Make_Null (No_Location))
277 Loc : constant Source_Ptr := Sloc (Function_Call);
278 Alloc_Form_Actual : Node_Id;
279 Alloc_Form_Formal : Node_Id;
280 Pool_Formal : Node_Id;
282 begin
283 -- The allocation form generally doesn't need to be passed in the case
284 -- of a constrained result subtype, since normally the caller performs
285 -- the allocation in that case. However this formal is still needed in
286 -- the case where the function has a tagged result, because generally
287 -- such functions can be called in a dispatching context and such calls
288 -- must be handled like calls to class-wide functions.
290 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
291 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
292 then
293 return;
294 end if;
296 -- Locate the implicit allocation form parameter in the called function.
297 -- Maybe it would be better for each implicit formal of a build-in-place
298 -- function to have a flag or a Uint attribute to identify it. ???
300 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
302 if Present (Alloc_Form_Exp) then
303 pragma Assert (Alloc_Form = Unspecified);
305 Alloc_Form_Actual := Alloc_Form_Exp;
307 else
308 pragma Assert (Alloc_Form /= Unspecified);
310 Alloc_Form_Actual :=
311 Make_Integer_Literal (Loc,
312 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
313 end if;
315 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
317 -- Build the parameter association for the new actual and add it to the
318 -- end of the function's actuals.
320 Add_Extra_Actual_To_Call
321 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
323 -- Pass the Storage_Pool parameter. This parameter is omitted on
324 -- .NET/JVM/ZFP as those targets do not support pools.
326 if VM_Target = No_VM
327 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
328 then
329 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
330 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
331 Add_Extra_Actual_To_Call
332 (Function_Call, Pool_Formal, Pool_Actual);
333 end if;
334 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
336 -----------------------------------------------------------
337 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
338 -----------------------------------------------------------
340 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
341 (Func_Call : Node_Id;
342 Func_Id : Entity_Id;
343 Ptr_Typ : Entity_Id := Empty;
344 Master_Exp : Node_Id := Empty)
346 begin
347 if not Needs_BIP_Finalization_Master (Func_Id) then
348 return;
349 end if;
351 declare
352 Formal : constant Entity_Id :=
353 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
354 Loc : constant Source_Ptr := Sloc (Func_Call);
356 Actual : Node_Id;
357 Desig_Typ : Entity_Id;
359 begin
360 -- If there is a finalization master actual, such as the implicit
361 -- finalization master of an enclosing build-in-place function,
362 -- then this must be added as an extra actual of the call.
364 if Present (Master_Exp) then
365 Actual := Master_Exp;
367 -- Case where the context does not require an actual master
369 elsif No (Ptr_Typ) then
370 Actual := Make_Null (Loc);
372 else
373 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
375 -- Check for a library-level access type whose designated type has
376 -- supressed finalization. Such an access types lack a master.
377 -- Pass a null actual to the callee in order to signal a missing
378 -- master.
380 if Is_Library_Level_Entity (Ptr_Typ)
381 and then Finalize_Storage_Only (Desig_Typ)
382 then
383 Actual := Make_Null (Loc);
385 -- Types in need of finalization actions
387 elsif Needs_Finalization (Desig_Typ) then
389 -- The general mechanism of creating finalization masters for
390 -- anonymous access types is disabled by default, otherwise
391 -- finalization masters will pop all over the place. Such types
392 -- use context-specific masters.
394 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
395 and then No (Finalization_Master (Ptr_Typ))
396 then
397 Build_Finalization_Master
398 (Typ => Ptr_Typ,
399 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
400 Encl_Scope => Scope (Ptr_Typ));
401 end if;
403 -- Access-to-controlled types should always have a master
405 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
407 Actual :=
408 Make_Attribute_Reference (Loc,
409 Prefix =>
410 New_Reference_To (Finalization_Master (Ptr_Typ), Loc),
411 Attribute_Name => Name_Unrestricted_Access);
413 -- Tagged types
415 else
416 Actual := Make_Null (Loc);
417 end if;
418 end if;
420 Analyze_And_Resolve (Actual, Etype (Formal));
422 -- Build the parameter association for the new actual and add it to
423 -- the end of the function's actuals.
425 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
426 end;
427 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
429 ------------------------------
430 -- Add_Extra_Actual_To_Call --
431 ------------------------------
433 procedure Add_Extra_Actual_To_Call
434 (Subprogram_Call : Node_Id;
435 Extra_Formal : Entity_Id;
436 Extra_Actual : Node_Id)
438 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
439 Param_Assoc : Node_Id;
441 begin
442 Param_Assoc :=
443 Make_Parameter_Association (Loc,
444 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
445 Explicit_Actual_Parameter => Extra_Actual);
447 Set_Parent (Param_Assoc, Subprogram_Call);
448 Set_Parent (Extra_Actual, Param_Assoc);
450 if Present (Parameter_Associations (Subprogram_Call)) then
451 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
452 N_Parameter_Association
453 then
455 -- Find last named actual, and append
457 declare
458 L : Node_Id;
459 begin
460 L := First_Actual (Subprogram_Call);
461 while Present (L) loop
462 if No (Next_Actual (L)) then
463 Set_Next_Named_Actual (Parent (L), Extra_Actual);
464 exit;
465 end if;
466 Next_Actual (L);
467 end loop;
468 end;
470 else
471 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
472 end if;
474 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
476 else
477 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
478 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
479 end if;
480 end Add_Extra_Actual_To_Call;
482 ---------------------------------------------
483 -- Add_Task_Actuals_To_Build_In_Place_Call --
484 ---------------------------------------------
486 procedure Add_Task_Actuals_To_Build_In_Place_Call
487 (Function_Call : Node_Id;
488 Function_Id : Entity_Id;
489 Master_Actual : Node_Id)
491 Loc : constant Source_Ptr := Sloc (Function_Call);
492 Result_Subt : constant Entity_Id :=
493 Available_View (Etype (Function_Id));
494 Actual : Node_Id;
495 Chain_Actual : Node_Id;
496 Chain_Formal : Node_Id;
497 Master_Formal : Node_Id;
499 begin
500 -- No such extra parameters are needed if there are no tasks
502 if not Has_Task (Result_Subt) then
503 return;
504 end if;
506 Actual := Master_Actual;
508 -- Use a dummy _master actual in case of No_Task_Hierarchy
510 if Restriction_Active (No_Task_Hierarchy) then
511 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
513 -- In the case where we use the master associated with an access type,
514 -- the actual is an entity and requires an explicit reference.
516 elsif Nkind (Actual) = N_Defining_Identifier then
517 Actual := New_Reference_To (Actual, Loc);
518 end if;
520 -- Locate the implicit master parameter in the called function
522 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
523 Analyze_And_Resolve (Actual, Etype (Master_Formal));
525 -- Build the parameter association for the new actual and add it to the
526 -- end of the function's actuals.
528 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
530 -- Locate the implicit activation chain parameter in the called function
532 Chain_Formal :=
533 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
535 -- Create the actual which is a pointer to the current activation chain
537 Chain_Actual :=
538 Make_Attribute_Reference (Loc,
539 Prefix => Make_Identifier (Loc, Name_uChain),
540 Attribute_Name => Name_Unrestricted_Access);
542 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
544 -- Build the parameter association for the new actual and add it to the
545 -- end of the function's actuals.
547 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
548 end Add_Task_Actuals_To_Build_In_Place_Call;
550 -----------------------
551 -- BIP_Formal_Suffix --
552 -----------------------
554 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
555 begin
556 case Kind is
557 when BIP_Alloc_Form =>
558 return "BIPalloc";
559 when BIP_Storage_Pool =>
560 return "BIPstoragepool";
561 when BIP_Finalization_Master =>
562 return "BIPfinalizationmaster";
563 when BIP_Task_Master =>
564 return "BIPtaskmaster";
565 when BIP_Activation_Chain =>
566 return "BIPactivationchain";
567 when BIP_Object_Access =>
568 return "BIPaccess";
569 end case;
570 end BIP_Formal_Suffix;
572 ---------------------------
573 -- Build_In_Place_Formal --
574 ---------------------------
576 function Build_In_Place_Formal
577 (Func : Entity_Id;
578 Kind : BIP_Formal_Kind) return Entity_Id
580 Formal_Name : constant Name_Id :=
581 New_External_Name
582 (Chars (Func), BIP_Formal_Suffix (Kind));
583 Extra_Formal : Entity_Id := Extra_Formals (Func);
585 begin
586 -- Maybe it would be better for each implicit formal of a build-in-place
587 -- function to have a flag or a Uint attribute to identify it. ???
589 -- The return type in the function declaration may have been a limited
590 -- view, and the extra formals for the function were not generated at
591 -- that point. At the point of call the full view must be available and
592 -- the extra formals can be created.
594 if No (Extra_Formal) then
595 Create_Extra_Formals (Func);
596 Extra_Formal := Extra_Formals (Func);
597 end if;
599 loop
600 pragma Assert (Present (Extra_Formal));
601 exit when Chars (Extra_Formal) = Formal_Name;
603 Next_Formal_With_Extras (Extra_Formal);
604 end loop;
606 return Extra_Formal;
607 end Build_In_Place_Formal;
609 --------------------------------
610 -- Check_Overriding_Operation --
611 --------------------------------
613 procedure Check_Overriding_Operation (Subp : Entity_Id) is
614 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
615 Op_List : constant Elist_Id := Primitive_Operations (Typ);
616 Op_Elmt : Elmt_Id;
617 Prim_Op : Entity_Id;
618 Par_Op : Entity_Id;
620 begin
621 if Is_Derived_Type (Typ)
622 and then not Is_Private_Type (Typ)
623 and then In_Open_Scopes (Scope (Etype (Typ)))
624 and then Is_Base_Type (Typ)
625 then
626 -- Subp overrides an inherited private operation if there is an
627 -- inherited operation with a different name than Subp (see
628 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
629 -- same name as Subp.
631 Op_Elmt := First_Elmt (Op_List);
632 while Present (Op_Elmt) loop
633 Prim_Op := Node (Op_Elmt);
634 Par_Op := Alias (Prim_Op);
636 if Present (Par_Op)
637 and then not Comes_From_Source (Prim_Op)
638 and then Chars (Prim_Op) /= Chars (Par_Op)
639 and then Chars (Par_Op) = Chars (Subp)
640 and then Is_Hidden (Par_Op)
641 and then Type_Conformant (Prim_Op, Subp)
642 then
643 Set_DT_Position (Subp, DT_Position (Prim_Op));
644 end if;
646 Next_Elmt (Op_Elmt);
647 end loop;
648 end if;
649 end Check_Overriding_Operation;
651 -------------------------------
652 -- Detect_Infinite_Recursion --
653 -------------------------------
655 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
656 Loc : constant Source_Ptr := Sloc (N);
658 Var_List : constant Elist_Id := New_Elmt_List;
659 -- List of globals referenced by body of procedure
661 Call_List : constant Elist_Id := New_Elmt_List;
662 -- List of recursive calls in body of procedure
664 Shad_List : constant Elist_Id := New_Elmt_List;
665 -- List of entity id's for entities created to capture the value of
666 -- referenced globals on entry to the procedure.
668 Scop : constant Uint := Scope_Depth (Spec);
669 -- This is used to record the scope depth of the current procedure, so
670 -- that we can identify global references.
672 Max_Vars : constant := 4;
673 -- Do not test more than four global variables
675 Count_Vars : Natural := 0;
676 -- Count variables found so far
678 Var : Entity_Id;
679 Elm : Elmt_Id;
680 Ent : Entity_Id;
681 Call : Elmt_Id;
682 Decl : Node_Id;
683 Test : Node_Id;
684 Elm1 : Elmt_Id;
685 Elm2 : Elmt_Id;
686 Last : Node_Id;
688 function Process (Nod : Node_Id) return Traverse_Result;
689 -- Function to traverse the subprogram body (using Traverse_Func)
691 -------------
692 -- Process --
693 -------------
695 function Process (Nod : Node_Id) return Traverse_Result is
696 begin
697 -- Procedure call
699 if Nkind (Nod) = N_Procedure_Call_Statement then
701 -- Case of one of the detected recursive calls
703 if Is_Entity_Name (Name (Nod))
704 and then Has_Recursive_Call (Entity (Name (Nod)))
705 and then Entity (Name (Nod)) = Spec
706 then
707 Append_Elmt (Nod, Call_List);
708 return Skip;
710 -- Any other procedure call may have side effects
712 else
713 return Abandon;
714 end if;
716 -- A call to a pure function can always be ignored
718 elsif Nkind (Nod) = N_Function_Call
719 and then Is_Entity_Name (Name (Nod))
720 and then Is_Pure (Entity (Name (Nod)))
721 then
722 return Skip;
724 -- Case of an identifier reference
726 elsif Nkind (Nod) = N_Identifier then
727 Ent := Entity (Nod);
729 -- If no entity, then ignore the reference
731 -- Not clear why this can happen. To investigate, remove this
732 -- test and look at the crash that occurs here in 3401-004 ???
734 if No (Ent) then
735 return Skip;
737 -- Ignore entities with no Scope, again not clear how this
738 -- can happen, to investigate, look at 4108-008 ???
740 elsif No (Scope (Ent)) then
741 return Skip;
743 -- Ignore the reference if not to a more global object
745 elsif Scope_Depth (Scope (Ent)) >= Scop then
746 return Skip;
748 -- References to types, exceptions and constants are always OK
750 elsif Is_Type (Ent)
751 or else Ekind (Ent) = E_Exception
752 or else Ekind (Ent) = E_Constant
753 then
754 return Skip;
756 -- If other than a non-volatile scalar variable, we have some
757 -- kind of global reference (e.g. to a function) that we cannot
758 -- deal with so we forget the attempt.
760 elsif Ekind (Ent) /= E_Variable
761 or else not Is_Scalar_Type (Etype (Ent))
762 or else Treat_As_Volatile (Ent)
763 then
764 return Abandon;
766 -- Otherwise we have a reference to a global scalar
768 else
769 -- Loop through global entities already detected
771 Elm := First_Elmt (Var_List);
772 loop
773 -- If not detected before, record this new global reference
775 if No (Elm) then
776 Count_Vars := Count_Vars + 1;
778 if Count_Vars <= Max_Vars then
779 Append_Elmt (Entity (Nod), Var_List);
780 else
781 return Abandon;
782 end if;
784 exit;
786 -- If recorded before, ignore
788 elsif Node (Elm) = Entity (Nod) then
789 return Skip;
791 -- Otherwise keep looking
793 else
794 Next_Elmt (Elm);
795 end if;
796 end loop;
798 return Skip;
799 end if;
801 -- For all other node kinds, recursively visit syntactic children
803 else
804 return OK;
805 end if;
806 end Process;
808 function Traverse_Body is new Traverse_Func (Process);
810 -- Start of processing for Detect_Infinite_Recursion
812 begin
813 -- Do not attempt detection in No_Implicit_Conditional mode, since we
814 -- won't be able to generate the code to handle the recursion in any
815 -- case.
817 if Restriction_Active (No_Implicit_Conditionals) then
818 return;
819 end if;
821 -- Otherwise do traversal and quit if we get abandon signal
823 if Traverse_Body (N) = Abandon then
824 return;
826 -- We must have a call, since Has_Recursive_Call was set. If not just
827 -- ignore (this is only an error check, so if we have a funny situation,
828 -- due to bugs or errors, we do not want to bomb!)
830 elsif Is_Empty_Elmt_List (Call_List) then
831 return;
832 end if;
834 -- Here is the case where we detect recursion at compile time
836 -- Push our current scope for analyzing the declarations and code that
837 -- we will insert for the checking.
839 Push_Scope (Spec);
841 -- This loop builds temporary variables for each of the referenced
842 -- globals, so that at the end of the loop the list Shad_List contains
843 -- these temporaries in one-to-one correspondence with the elements in
844 -- Var_List.
846 Last := Empty;
847 Elm := First_Elmt (Var_List);
848 while Present (Elm) loop
849 Var := Node (Elm);
850 Ent := Make_Temporary (Loc, 'S');
851 Append_Elmt (Ent, Shad_List);
853 -- Insert a declaration for this temporary at the start of the
854 -- declarations for the procedure. The temporaries are declared as
855 -- constant objects initialized to the current values of the
856 -- corresponding temporaries.
858 Decl :=
859 Make_Object_Declaration (Loc,
860 Defining_Identifier => Ent,
861 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
862 Constant_Present => True,
863 Expression => New_Occurrence_Of (Var, Loc));
865 if No (Last) then
866 Prepend (Decl, Declarations (N));
867 else
868 Insert_After (Last, Decl);
869 end if;
871 Last := Decl;
872 Analyze (Decl);
873 Next_Elmt (Elm);
874 end loop;
876 -- Loop through calls
878 Call := First_Elmt (Call_List);
879 while Present (Call) loop
881 -- Build a predicate expression of the form
883 -- True
884 -- and then global1 = temp1
885 -- and then global2 = temp2
886 -- ...
888 -- This predicate determines if any of the global values
889 -- referenced by the procedure have changed since the
890 -- current call, if not an infinite recursion is assured.
892 Test := New_Occurrence_Of (Standard_True, Loc);
894 Elm1 := First_Elmt (Var_List);
895 Elm2 := First_Elmt (Shad_List);
896 while Present (Elm1) loop
897 Test :=
898 Make_And_Then (Loc,
899 Left_Opnd => Test,
900 Right_Opnd =>
901 Make_Op_Eq (Loc,
902 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
903 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
905 Next_Elmt (Elm1);
906 Next_Elmt (Elm2);
907 end loop;
909 -- Now we replace the call with the sequence
911 -- if no-changes (see above) then
912 -- raise Storage_Error;
913 -- else
914 -- original-call
915 -- end if;
917 Rewrite (Node (Call),
918 Make_If_Statement (Loc,
919 Condition => Test,
920 Then_Statements => New_List (
921 Make_Raise_Storage_Error (Loc,
922 Reason => SE_Infinite_Recursion)),
924 Else_Statements => New_List (
925 Relocate_Node (Node (Call)))));
927 Analyze (Node (Call));
929 Next_Elmt (Call);
930 end loop;
932 -- Remove temporary scope stack entry used for analysis
934 Pop_Scope;
935 end Detect_Infinite_Recursion;
937 --------------------
938 -- Expand_Actuals --
939 --------------------
941 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
942 Loc : constant Source_Ptr := Sloc (N);
943 Actual : Node_Id;
944 Formal : Entity_Id;
945 N_Node : Node_Id;
946 Post_Call : List_Id;
947 E_Actual : Entity_Id;
948 E_Formal : Entity_Id;
950 procedure Add_Call_By_Copy_Code;
951 -- For cases where the parameter must be passed by copy, this routine
952 -- generates a temporary variable into which the actual is copied and
953 -- then passes this as the parameter. For an OUT or IN OUT parameter,
954 -- an assignment is also generated to copy the result back. The call
955 -- also takes care of any constraint checks required for the type
956 -- conversion case (on both the way in and the way out).
958 procedure Add_Simple_Call_By_Copy_Code;
959 -- This is similar to the above, but is used in cases where we know
960 -- that all that is needed is to simply create a temporary and copy
961 -- the value in and out of the temporary.
963 procedure Check_Fortran_Logical;
964 -- A value of type Logical that is passed through a formal parameter
965 -- must be normalized because .TRUE. usually does not have the same
966 -- representation as True. We assume that .FALSE. = False = 0.
967 -- What about functions that return a logical type ???
969 function Is_Legal_Copy return Boolean;
970 -- Check that an actual can be copied before generating the temporary
971 -- to be used in the call. If the actual is of a by_reference type then
972 -- the program is illegal (this can only happen in the presence of
973 -- rep. clauses that force an incorrect alignment). If the formal is
974 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
975 -- the effect that this might lead to unaligned arguments.
977 function Make_Var (Actual : Node_Id) return Entity_Id;
978 -- Returns an entity that refers to the given actual parameter,
979 -- Actual (not including any type conversion). If Actual is an
980 -- entity name, then this entity is returned unchanged, otherwise
981 -- a renaming is created to provide an entity for the actual.
983 procedure Reset_Packed_Prefix;
984 -- The expansion of a packed array component reference is delayed in
985 -- the context of a call. Now we need to complete the expansion, so we
986 -- unmark the analyzed bits in all prefixes.
988 ---------------------------
989 -- Add_Call_By_Copy_Code --
990 ---------------------------
992 procedure Add_Call_By_Copy_Code is
993 Expr : Node_Id;
994 Init : Node_Id;
995 Temp : Entity_Id;
996 Indic : Node_Id;
997 Var : Entity_Id;
998 F_Typ : constant Entity_Id := Etype (Formal);
999 V_Typ : Entity_Id;
1000 Crep : Boolean;
1002 begin
1003 if not Is_Legal_Copy then
1004 return;
1005 end if;
1007 Temp := Make_Temporary (Loc, 'T', Actual);
1009 -- Use formal type for temp, unless formal type is an unconstrained
1010 -- array, in which case we don't have to worry about bounds checks,
1011 -- and we use the actual type, since that has appropriate bounds.
1013 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1014 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1015 else
1016 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1017 end if;
1019 if Nkind (Actual) = N_Type_Conversion then
1020 V_Typ := Etype (Expression (Actual));
1022 -- If the formal is an (in-)out parameter, capture the name
1023 -- of the variable in order to build the post-call assignment.
1025 Var := Make_Var (Expression (Actual));
1027 Crep := not Same_Representation
1028 (F_Typ, Etype (Expression (Actual)));
1030 else
1031 V_Typ := Etype (Actual);
1032 Var := Make_Var (Actual);
1033 Crep := False;
1034 end if;
1036 -- Setup initialization for case of in out parameter, or an out
1037 -- parameter where the formal is an unconstrained array (in the
1038 -- latter case, we have to pass in an object with bounds).
1040 -- If this is an out parameter, the initial copy is wasteful, so as
1041 -- an optimization for the one-dimensional case we extract the
1042 -- bounds of the actual and build an uninitialized temporary of the
1043 -- right size.
1045 if Ekind (Formal) = E_In_Out_Parameter
1046 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1047 then
1048 if Nkind (Actual) = N_Type_Conversion then
1049 if Conversion_OK (Actual) then
1050 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1051 else
1052 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1053 end if;
1055 elsif Ekind (Formal) = E_Out_Parameter
1056 and then Is_Array_Type (F_Typ)
1057 and then Number_Dimensions (F_Typ) = 1
1058 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1059 then
1060 -- Actual is a one-dimensional array or slice, and the type
1061 -- requires no initialization. Create a temporary of the
1062 -- right size, but do not copy actual into it (optimization).
1064 Init := Empty;
1065 Indic :=
1066 Make_Subtype_Indication (Loc,
1067 Subtype_Mark =>
1068 New_Occurrence_Of (F_Typ, Loc),
1069 Constraint =>
1070 Make_Index_Or_Discriminant_Constraint (Loc,
1071 Constraints => New_List (
1072 Make_Range (Loc,
1073 Low_Bound =>
1074 Make_Attribute_Reference (Loc,
1075 Prefix => New_Occurrence_Of (Var, Loc),
1076 Attribute_Name => Name_First),
1077 High_Bound =>
1078 Make_Attribute_Reference (Loc,
1079 Prefix => New_Occurrence_Of (Var, Loc),
1080 Attribute_Name => Name_Last)))));
1082 else
1083 Init := New_Occurrence_Of (Var, Loc);
1084 end if;
1086 -- An initialization is created for packed conversions as
1087 -- actuals for out parameters to enable Make_Object_Declaration
1088 -- to determine the proper subtype for N_Node. Note that this
1089 -- is wasteful because the extra copying on the call side is
1090 -- not required for such out parameters. ???
1092 elsif Ekind (Formal) = E_Out_Parameter
1093 and then Nkind (Actual) = N_Type_Conversion
1094 and then (Is_Bit_Packed_Array (F_Typ)
1095 or else
1096 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1097 then
1098 if Conversion_OK (Actual) then
1099 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1100 else
1101 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1102 end if;
1104 elsif Ekind (Formal) = E_In_Parameter then
1106 -- Handle the case in which the actual is a type conversion
1108 if Nkind (Actual) = N_Type_Conversion then
1109 if Conversion_OK (Actual) then
1110 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1111 else
1112 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1113 end if;
1114 else
1115 Init := New_Occurrence_Of (Var, Loc);
1116 end if;
1118 else
1119 Init := Empty;
1120 end if;
1122 N_Node :=
1123 Make_Object_Declaration (Loc,
1124 Defining_Identifier => Temp,
1125 Object_Definition => Indic,
1126 Expression => Init);
1127 Set_Assignment_OK (N_Node);
1128 Insert_Action (N, N_Node);
1130 -- Now, normally the deal here is that we use the defining
1131 -- identifier created by that object declaration. There is
1132 -- one exception to this. In the change of representation case
1133 -- the above declaration will end up looking like:
1135 -- temp : type := identifier;
1137 -- And in this case we might as well use the identifier directly
1138 -- and eliminate the temporary. Note that the analysis of the
1139 -- declaration was not a waste of time in that case, since it is
1140 -- what generated the necessary change of representation code. If
1141 -- the change of representation introduced additional code, as in
1142 -- a fixed-integer conversion, the expression is not an identifier
1143 -- and must be kept.
1145 if Crep
1146 and then Present (Expression (N_Node))
1147 and then Is_Entity_Name (Expression (N_Node))
1148 then
1149 Temp := Entity (Expression (N_Node));
1150 Rewrite (N_Node, Make_Null_Statement (Loc));
1151 end if;
1153 -- For IN parameter, all we do is to replace the actual
1155 if Ekind (Formal) = E_In_Parameter then
1156 Rewrite (Actual, New_Reference_To (Temp, Loc));
1157 Analyze (Actual);
1159 -- Processing for OUT or IN OUT parameter
1161 else
1162 -- Kill current value indications for the temporary variable we
1163 -- created, since we just passed it as an OUT parameter.
1165 Kill_Current_Values (Temp);
1166 Set_Is_Known_Valid (Temp, False);
1168 -- If type conversion, use reverse conversion on exit
1170 if Nkind (Actual) = N_Type_Conversion then
1171 if Conversion_OK (Actual) then
1172 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1173 else
1174 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1175 end if;
1176 else
1177 Expr := New_Occurrence_Of (Temp, Loc);
1178 end if;
1180 Rewrite (Actual, New_Reference_To (Temp, Loc));
1181 Analyze (Actual);
1183 -- If the actual is a conversion of a packed reference, it may
1184 -- already have been expanded by Remove_Side_Effects, and the
1185 -- resulting variable is a temporary which does not designate
1186 -- the proper out-parameter, which may not be addressable. In
1187 -- that case, generate an assignment to the original expression
1188 -- (before expansion of the packed reference) so that the proper
1189 -- expansion of assignment to a packed component can take place.
1191 declare
1192 Obj : Node_Id;
1193 Lhs : Node_Id;
1195 begin
1196 if Is_Renaming_Of_Object (Var)
1197 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1198 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1199 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1200 = N_Indexed_Component
1201 and then
1202 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1203 then
1204 Obj := Renamed_Object (Var);
1205 Lhs :=
1206 Make_Selected_Component (Loc,
1207 Prefix =>
1208 New_Copy_Tree (Original_Node (Prefix (Obj))),
1209 Selector_Name => New_Copy (Selector_Name (Obj)));
1210 Reset_Analyzed_Flags (Lhs);
1212 else
1213 Lhs := New_Occurrence_Of (Var, Loc);
1214 end if;
1216 Set_Assignment_OK (Lhs);
1218 if Is_Access_Type (E_Formal)
1219 and then Is_Entity_Name (Lhs)
1220 and then
1221 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1222 then
1223 -- Copyback target is an Ada 2012 stand-alone object of an
1224 -- anonymous access type.
1226 pragma Assert (Ada_Version >= Ada_2012);
1228 if Type_Access_Level (E_Formal) >
1229 Object_Access_Level (Lhs)
1230 then
1231 Append_To (Post_Call,
1232 Make_Raise_Program_Error (Loc,
1233 Reason => PE_Accessibility_Check_Failed));
1234 end if;
1236 Append_To (Post_Call,
1237 Make_Assignment_Statement (Loc,
1238 Name => Lhs,
1239 Expression => Expr));
1241 -- We would like to somehow suppress generation of the
1242 -- extra_accessibility assignment generated by the expansion
1243 -- of the above assignment statement. It's not a correctness
1244 -- issue because the following assignment renders it dead,
1245 -- but generating back-to-back assignments to the same
1246 -- target is undesirable. ???
1248 Append_To (Post_Call,
1249 Make_Assignment_Statement (Loc,
1250 Name => New_Occurrence_Of (
1251 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1252 Expression => Make_Integer_Literal (Loc,
1253 Type_Access_Level (E_Formal))));
1255 else
1256 Append_To (Post_Call,
1257 Make_Assignment_Statement (Loc,
1258 Name => Lhs,
1259 Expression => Expr));
1260 end if;
1261 end;
1262 end if;
1263 end Add_Call_By_Copy_Code;
1265 ----------------------------------
1266 -- Add_Simple_Call_By_Copy_Code --
1267 ----------------------------------
1269 procedure Add_Simple_Call_By_Copy_Code is
1270 Temp : Entity_Id;
1271 Decl : Node_Id;
1272 Incod : Node_Id;
1273 Outcod : Node_Id;
1274 Lhs : Node_Id;
1275 Rhs : Node_Id;
1276 Indic : Node_Id;
1277 F_Typ : constant Entity_Id := Etype (Formal);
1279 begin
1280 if not Is_Legal_Copy then
1281 return;
1282 end if;
1284 -- Use formal type for temp, unless formal type is an unconstrained
1285 -- array, in which case we don't have to worry about bounds checks,
1286 -- and we use the actual type, since that has appropriate bounds.
1288 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1289 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1290 else
1291 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1292 end if;
1294 -- Prepare to generate code
1296 Reset_Packed_Prefix;
1298 Temp := Make_Temporary (Loc, 'T', Actual);
1299 Incod := Relocate_Node (Actual);
1300 Outcod := New_Copy_Tree (Incod);
1302 -- Generate declaration of temporary variable, initializing it
1303 -- with the input parameter unless we have an OUT formal or
1304 -- this is an initialization call.
1306 -- If the formal is an out parameter with discriminants, the
1307 -- discriminants must be captured even if the rest of the object
1308 -- is in principle uninitialized, because the discriminants may
1309 -- be read by the called subprogram.
1311 if Ekind (Formal) = E_Out_Parameter then
1312 Incod := Empty;
1314 if Has_Discriminants (Etype (Formal)) then
1315 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1316 end if;
1318 elsif Inside_Init_Proc then
1320 -- Could use a comment here to match comment below ???
1322 if Nkind (Actual) /= N_Selected_Component
1323 or else
1324 not Has_Discriminant_Dependent_Constraint
1325 (Entity (Selector_Name (Actual)))
1326 then
1327 Incod := Empty;
1329 -- Otherwise, keep the component in order to generate the proper
1330 -- actual subtype, that depends on enclosing discriminants.
1332 else
1333 null;
1334 end if;
1335 end if;
1337 Decl :=
1338 Make_Object_Declaration (Loc,
1339 Defining_Identifier => Temp,
1340 Object_Definition => Indic,
1341 Expression => Incod);
1343 if Inside_Init_Proc
1344 and then No (Incod)
1345 then
1346 -- If the call is to initialize a component of a composite type,
1347 -- and the component does not depend on discriminants, use the
1348 -- actual type of the component. This is required in case the
1349 -- component is constrained, because in general the formal of the
1350 -- initialization procedure will be unconstrained. Note that if
1351 -- the component being initialized is constrained by an enclosing
1352 -- discriminant, the presence of the initialization in the
1353 -- declaration will generate an expression for the actual subtype.
1355 Set_No_Initialization (Decl);
1356 Set_Object_Definition (Decl,
1357 New_Occurrence_Of (Etype (Actual), Loc));
1358 end if;
1360 Insert_Action (N, Decl);
1362 -- The actual is simply a reference to the temporary
1364 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1366 -- Generate copy out if OUT or IN OUT parameter
1368 if Ekind (Formal) /= E_In_Parameter then
1369 Lhs := Outcod;
1370 Rhs := New_Occurrence_Of (Temp, Loc);
1372 -- Deal with conversion
1374 if Nkind (Lhs) = N_Type_Conversion then
1375 Lhs := Expression (Lhs);
1376 Rhs := Convert_To (Etype (Actual), Rhs);
1377 end if;
1379 Append_To (Post_Call,
1380 Make_Assignment_Statement (Loc,
1381 Name => Lhs,
1382 Expression => Rhs));
1383 Set_Assignment_OK (Name (Last (Post_Call)));
1384 end if;
1385 end Add_Simple_Call_By_Copy_Code;
1387 ---------------------------
1388 -- Check_Fortran_Logical --
1389 ---------------------------
1391 procedure Check_Fortran_Logical is
1392 Logical : constant Entity_Id := Etype (Formal);
1393 Var : Entity_Id;
1395 -- Note: this is very incomplete, e.g. it does not handle arrays
1396 -- of logical values. This is really not the right approach at all???)
1398 begin
1399 if Convention (Subp) = Convention_Fortran
1400 and then Root_Type (Etype (Formal)) = Standard_Boolean
1401 and then Ekind (Formal) /= E_In_Parameter
1402 then
1403 Var := Make_Var (Actual);
1404 Append_To (Post_Call,
1405 Make_Assignment_Statement (Loc,
1406 Name => New_Occurrence_Of (Var, Loc),
1407 Expression =>
1408 Unchecked_Convert_To (
1409 Logical,
1410 Make_Op_Ne (Loc,
1411 Left_Opnd => New_Occurrence_Of (Var, Loc),
1412 Right_Opnd =>
1413 Unchecked_Convert_To (
1414 Logical,
1415 New_Occurrence_Of (Standard_False, Loc))))));
1416 end if;
1417 end Check_Fortran_Logical;
1419 -------------------
1420 -- Is_Legal_Copy --
1421 -------------------
1423 function Is_Legal_Copy return Boolean is
1424 begin
1425 -- An attempt to copy a value of such a type can only occur if
1426 -- representation clauses give the actual a misaligned address.
1428 if Is_By_Reference_Type (Etype (Formal)) then
1430 -- If the front-end does not perform full type layout, the actual
1431 -- may in fact be properly aligned but there is not enough front-
1432 -- end information to determine this. In that case gigi will emit
1433 -- an error if a copy is not legal, or generate the proper code.
1434 -- For other backends we report the error now.
1436 -- Seems wrong to be issuing an error in the expander, since it
1437 -- will be missed in -gnatc mode ???
1439 if Frontend_Layout_On_Target then
1440 Error_Msg_N
1441 ("misaligned actual cannot be passed by reference", Actual);
1442 end if;
1444 return False;
1446 -- For users of Starlet, we assume that the specification of by-
1447 -- reference mechanism is mandatory. This may lead to unaligned
1448 -- objects but at least for DEC legacy code it is known to work.
1449 -- The warning will alert users of this code that a problem may
1450 -- be lurking.
1452 elsif Mechanism (Formal) = By_Reference
1453 and then Is_Valued_Procedure (Scope (Formal))
1454 then
1455 Error_Msg_N
1456 ("by_reference actual may be misaligned??", Actual);
1457 return False;
1459 else
1460 return True;
1461 end if;
1462 end Is_Legal_Copy;
1464 --------------
1465 -- Make_Var --
1466 --------------
1468 function Make_Var (Actual : Node_Id) return Entity_Id is
1469 Var : Entity_Id;
1471 begin
1472 if Is_Entity_Name (Actual) then
1473 return Entity (Actual);
1475 else
1476 Var := Make_Temporary (Loc, 'T', Actual);
1478 N_Node :=
1479 Make_Object_Renaming_Declaration (Loc,
1480 Defining_Identifier => Var,
1481 Subtype_Mark =>
1482 New_Occurrence_Of (Etype (Actual), Loc),
1483 Name => Relocate_Node (Actual));
1485 Insert_Action (N, N_Node);
1486 return Var;
1487 end if;
1488 end Make_Var;
1490 -------------------------
1491 -- Reset_Packed_Prefix --
1492 -------------------------
1494 procedure Reset_Packed_Prefix is
1495 Pfx : Node_Id := Actual;
1496 begin
1497 loop
1498 Set_Analyzed (Pfx, False);
1499 exit when
1500 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1501 Pfx := Prefix (Pfx);
1502 end loop;
1503 end Reset_Packed_Prefix;
1505 -- Start of processing for Expand_Actuals
1507 begin
1508 Post_Call := New_List;
1510 Formal := First_Formal (Subp);
1511 Actual := First_Actual (N);
1512 while Present (Formal) loop
1513 E_Formal := Etype (Formal);
1514 E_Actual := Etype (Actual);
1516 if Is_Scalar_Type (E_Formal)
1517 or else Nkind (Actual) = N_Slice
1518 then
1519 Check_Fortran_Logical;
1521 -- RM 6.4.1 (11)
1523 elsif Ekind (Formal) /= E_Out_Parameter then
1525 -- The unusual case of the current instance of a protected type
1526 -- requires special handling. This can only occur in the context
1527 -- of a call within the body of a protected operation.
1529 if Is_Entity_Name (Actual)
1530 and then Ekind (Entity (Actual)) = E_Protected_Type
1531 and then In_Open_Scopes (Entity (Actual))
1532 then
1533 if Scope (Subp) /= Entity (Actual) then
1534 Error_Msg_N
1535 ("operation outside protected type may not "
1536 & "call back its protected operations??", Actual);
1537 end if;
1539 Rewrite (Actual,
1540 Expand_Protected_Object_Reference (N, Entity (Actual)));
1541 end if;
1543 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1544 -- build-in-place function, then a temporary return object needs
1545 -- to be created and access to it must be passed to the function.
1546 -- Currently we limit such functions to those with inherently
1547 -- limited result subtypes, but eventually we plan to expand the
1548 -- functions that are treated as build-in-place to include other
1549 -- composite result types.
1551 if Is_Build_In_Place_Function_Call (Actual) then
1552 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1553 end if;
1555 Apply_Constraint_Check (Actual, E_Formal);
1557 -- Out parameter case. No constraint checks on access type
1558 -- RM 6.4.1 (13)
1560 elsif Is_Access_Type (E_Formal) then
1561 null;
1563 -- RM 6.4.1 (14)
1565 elsif Has_Discriminants (Base_Type (E_Formal))
1566 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1567 then
1568 Apply_Constraint_Check (Actual, E_Formal);
1570 -- RM 6.4.1 (15)
1572 else
1573 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1574 end if;
1576 -- Processing for IN-OUT and OUT parameters
1578 if Ekind (Formal) /= E_In_Parameter then
1580 -- For type conversions of arrays, apply length/range checks
1582 if Is_Array_Type (E_Formal)
1583 and then Nkind (Actual) = N_Type_Conversion
1584 then
1585 if Is_Constrained (E_Formal) then
1586 Apply_Length_Check (Expression (Actual), E_Formal);
1587 else
1588 Apply_Range_Check (Expression (Actual), E_Formal);
1589 end if;
1590 end if;
1592 -- If argument is a type conversion for a type that is passed
1593 -- by copy, then we must pass the parameter by copy.
1595 if Nkind (Actual) = N_Type_Conversion
1596 and then
1597 (Is_Numeric_Type (E_Formal)
1598 or else Is_Access_Type (E_Formal)
1599 or else Is_Enumeration_Type (E_Formal)
1600 or else Is_Bit_Packed_Array (Etype (Formal))
1601 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1603 -- Also pass by copy if change of representation
1605 or else not Same_Representation
1606 (Etype (Formal),
1607 Etype (Expression (Actual))))
1608 then
1609 Add_Call_By_Copy_Code;
1611 -- References to components of bit packed arrays are expanded
1612 -- at this point, rather than at the point of analysis of the
1613 -- actuals, to handle the expansion of the assignment to
1614 -- [in] out parameters.
1616 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1617 Add_Simple_Call_By_Copy_Code;
1619 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1620 -- because the back-end cannot cope with such objects. In other
1621 -- cases where alignment forces a copy, the back-end generates
1622 -- it properly. It should not be generated unconditionally in the
1623 -- front-end because it does not know precisely the alignment
1624 -- requirements of the target, and makes too conservative an
1625 -- estimate, leading to superfluous copies or spurious errors
1626 -- on by-reference parameters.
1628 elsif Nkind (Actual) = N_Selected_Component
1629 and then
1630 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1631 and then not Represented_As_Scalar (Etype (Formal))
1632 then
1633 Add_Simple_Call_By_Copy_Code;
1635 -- References to slices of bit packed arrays are expanded
1637 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1638 Add_Call_By_Copy_Code;
1640 -- References to possibly unaligned slices of arrays are expanded
1642 elsif Is_Possibly_Unaligned_Slice (Actual) then
1643 Add_Call_By_Copy_Code;
1645 -- Deal with access types where the actual subtype and the
1646 -- formal subtype are not the same, requiring a check.
1648 -- It is necessary to exclude tagged types because of "downward
1649 -- conversion" errors.
1651 elsif Is_Access_Type (E_Formal)
1652 and then not Same_Type (E_Formal, E_Actual)
1653 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1654 then
1655 Add_Call_By_Copy_Code;
1657 -- If the actual is not a scalar and is marked for volatile
1658 -- treatment, whereas the formal is not volatile, then pass
1659 -- by copy unless it is a by-reference type.
1661 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1662 -- because this is the enforcement of a language rule that applies
1663 -- only to "real" volatile variables, not e.g. to the address
1664 -- clause overlay case.
1666 elsif Is_Entity_Name (Actual)
1667 and then Is_Volatile (Entity (Actual))
1668 and then not Is_By_Reference_Type (E_Actual)
1669 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1670 and then not Is_Volatile (E_Formal)
1671 then
1672 Add_Call_By_Copy_Code;
1674 elsif Nkind (Actual) = N_Indexed_Component
1675 and then Is_Entity_Name (Prefix (Actual))
1676 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1677 then
1678 Add_Call_By_Copy_Code;
1680 -- Add call-by-copy code for the case of scalar out parameters
1681 -- when it is not known at compile time that the subtype of the
1682 -- formal is a subrange of the subtype of the actual (or vice
1683 -- versa for in out parameters), in order to get range checks
1684 -- on such actuals. (Maybe this case should be handled earlier
1685 -- in the if statement???)
1687 elsif Is_Scalar_Type (E_Formal)
1688 and then
1689 (not In_Subrange_Of (E_Formal, E_Actual)
1690 or else
1691 (Ekind (Formal) = E_In_Out_Parameter
1692 and then not In_Subrange_Of (E_Actual, E_Formal)))
1693 then
1694 -- Perhaps the setting back to False should be done within
1695 -- Add_Call_By_Copy_Code, since it could get set on other
1696 -- cases occurring above???
1698 if Do_Range_Check (Actual) then
1699 Set_Do_Range_Check (Actual, False);
1700 end if;
1702 Add_Call_By_Copy_Code;
1703 end if;
1705 -- RM 3.2.4 (23/3) : A predicate is checked on in-out and out
1706 -- by-reference parameters on exit from the call. If the actual
1707 -- is a derived type and the operation is inherited, the body
1708 -- of the operation will not contain a call to the predicate
1709 -- function, so it must be done explicitly after the call. Ditto
1710 -- if the actual is an entity of a predicated subtype.
1712 -- The rule refers to by-reference types, but a check is needed
1713 -- for by-copy types as well. That check is subsumed by the rule
1714 -- for subtype conversion on assignment, but we can generate the
1715 -- required check now.
1717 -- Note that this is needed only if the subtype of the actual has
1718 -- an explicit predicate aspect, not if it inherits them from a
1719 -- base type or ancestor. The check is also superfluous if the
1720 -- subtype is elaborated before the body of the subprogram, but
1721 -- this is harder to verify, and there may be a redundant check.
1723 -- Note also that Subp may be either a subprogram entity for
1724 -- direct calls, or a type entity for indirect calls, which must
1725 -- be handled separately because the name does not denote an
1726 -- overloadable entity.
1728 -- If the formal is class-wide the corresponding postcondition
1729 -- procedure does not include a predicate call, so it has to be
1730 -- generated explicitly.
1732 if not Is_Init_Proc (Subp)
1733 and then (Has_Aspect (E_Actual, Aspect_Predicate)
1734 or else
1735 Has_Aspect (E_Actual, Aspect_Dynamic_Predicate)
1736 or else
1737 Has_Aspect (E_Actual, Aspect_Static_Predicate))
1738 and then Present (Predicate_Function (E_Actual))
1739 then
1740 if Is_Entity_Name (Actual)
1741 or else
1742 (Is_Derived_Type (E_Actual)
1743 and then Is_Overloadable (Subp)
1744 and then Is_Inherited_Operation_For_Type (Subp, E_Actual))
1745 then
1746 Append_To (Post_Call,
1747 Make_Predicate_Check (E_Actual, Actual));
1749 elsif Is_Class_Wide_Type (E_Formal)
1750 and then not Is_Class_Wide_Type (E_Actual)
1751 then
1752 Append_To (Post_Call,
1753 Make_Predicate_Check (E_Actual, Actual));
1754 end if;
1755 end if;
1757 -- Processing for IN parameters
1759 else
1760 -- For IN parameters is in the packed array case, we expand an
1761 -- indexed component (the circuit in Exp_Ch4 deliberately left
1762 -- indexed components appearing as actuals untouched, so that
1763 -- the special processing above for the OUT and IN OUT cases
1764 -- could be performed. We could make the test in Exp_Ch4 more
1765 -- complex and have it detect the parameter mode, but it is
1766 -- easier simply to handle all cases here.)
1768 if Nkind (Actual) = N_Indexed_Component
1769 and then Is_Packed (Etype (Prefix (Actual)))
1770 then
1771 Reset_Packed_Prefix;
1772 Expand_Packed_Element_Reference (Actual);
1774 -- If we have a reference to a bit packed array, we copy it, since
1775 -- the actual must be byte aligned.
1777 -- Is this really necessary in all cases???
1779 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1780 Add_Simple_Call_By_Copy_Code;
1782 -- If a non-scalar actual is possibly unaligned, we need a copy
1784 elsif Is_Possibly_Unaligned_Object (Actual)
1785 and then not Represented_As_Scalar (Etype (Formal))
1786 then
1787 Add_Simple_Call_By_Copy_Code;
1789 -- Similarly, we have to expand slices of packed arrays here
1790 -- because the result must be byte aligned.
1792 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1793 Add_Call_By_Copy_Code;
1795 -- Only processing remaining is to pass by copy if this is a
1796 -- reference to a possibly unaligned slice, since the caller
1797 -- expects an appropriately aligned argument.
1799 elsif Is_Possibly_Unaligned_Slice (Actual) then
1800 Add_Call_By_Copy_Code;
1802 -- An unusual case: a current instance of an enclosing task can be
1803 -- an actual, and must be replaced by a reference to self.
1805 elsif Is_Entity_Name (Actual)
1806 and then Is_Task_Type (Entity (Actual))
1807 then
1808 if In_Open_Scopes (Entity (Actual)) then
1809 Rewrite (Actual,
1810 (Make_Function_Call (Loc,
1811 Name => New_Reference_To (RTE (RE_Self), Loc))));
1812 Analyze (Actual);
1814 -- A task type cannot otherwise appear as an actual
1816 else
1817 raise Program_Error;
1818 end if;
1819 end if;
1820 end if;
1822 Next_Formal (Formal);
1823 Next_Actual (Actual);
1824 end loop;
1826 -- Find right place to put post call stuff if it is present
1828 if not Is_Empty_List (Post_Call) then
1830 -- Cases where the call is not a member of a statement list
1832 if not Is_List_Member (N) then
1833 declare
1834 P : Node_Id := Parent (N);
1836 begin
1837 -- In Ada 2012 the call may be a function call in an expression
1838 -- (since OUT and IN OUT parameters are now allowed for such
1839 -- calls. The write-back of (in)-out parameters is handled
1840 -- by the back-end, but the constraint checks generated when
1841 -- subtypes of formal and actual don't match must be inserted
1842 -- in the form of assignments, at the nearest point after the
1843 -- declaration or statement that contains the call.
1845 if Ada_Version >= Ada_2012
1846 and then Nkind (N) = N_Function_Call
1847 then
1848 while Nkind (P) not in N_Declaration
1849 and then
1850 Nkind (P) not in N_Statement_Other_Than_Procedure_Call
1851 loop
1852 P := Parent (P);
1853 end loop;
1855 Insert_Actions_After (P, Post_Call);
1857 -- If not the special Ada 2012 case of a function call, then
1858 -- we must have the triggering statement of a triggering
1859 -- alternative or an entry call alternative, and we can add
1860 -- the post call stuff to the corresponding statement list.
1862 else
1863 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1864 N_Entry_Call_Alternative));
1866 if Is_Non_Empty_List (Statements (P)) then
1867 Insert_List_Before_And_Analyze
1868 (First (Statements (P)), Post_Call);
1869 else
1870 Set_Statements (P, Post_Call);
1871 end if;
1872 end if;
1874 end;
1876 -- Otherwise, normal case where N is in a statement sequence,
1877 -- just put the post-call stuff after the call statement.
1879 else
1880 Insert_Actions_After (N, Post_Call);
1881 end if;
1882 end if;
1884 -- The call node itself is re-analyzed in Expand_Call
1886 end Expand_Actuals;
1888 -----------------
1889 -- Expand_Call --
1890 -----------------
1892 -- This procedure handles expansion of function calls and procedure call
1893 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1894 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1896 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1897 -- Provide values of actuals for all formals in Extra_Formals list
1898 -- Replace "call" to enumeration literal function by literal itself
1899 -- Rewrite call to predefined operator as operator
1900 -- Replace actuals to in-out parameters that are numeric conversions,
1901 -- with explicit assignment to temporaries before and after the call.
1902 -- Remove optional actuals if First_Optional_Parameter specified.
1904 -- Note that the list of actuals has been filled with default expressions
1905 -- during semantic analysis of the call. Only the extra actuals required
1906 -- for the 'Constrained attribute and for accessibility checks are added
1907 -- at this point.
1909 procedure Expand_Call (N : Node_Id) is
1910 Loc : constant Source_Ptr := Sloc (N);
1911 Call_Node : Node_Id := N;
1912 Extra_Actuals : List_Id := No_List;
1913 Prev : Node_Id := Empty;
1915 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1916 -- Adds one entry to the end of the actual parameter list. Used for
1917 -- default parameters and for extra actuals (for Extra_Formals). The
1918 -- argument is an N_Parameter_Association node.
1920 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1921 -- Adds an extra actual to the list of extra actuals. Expr is the
1922 -- expression for the value of the actual, EF is the entity for the
1923 -- extra formal.
1925 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id);
1926 -- Check and inline the body of Subp. Invoked when compiling with
1927 -- optimizations enabled and Subp has pragma inline or inline always.
1928 -- If the subprogram is a renaming, or if it is inherited, then Subp
1929 -- references the renamed entity and Orig_Subp is the entity of the
1930 -- call node N.
1932 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id);
1933 -- Check and inline the body of Subp. Invoked when compiling without
1934 -- optimizations and Subp has pragma inline always. If the subprogram is
1935 -- a renaming, or if it is inherited, then Subp references the renamed
1936 -- entity and Orig_Subp is the entity of the call node N.
1938 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1939 -- Within an instance, a type derived from a non-tagged formal derived
1940 -- type inherits from the original parent, not from the actual. The
1941 -- current derivation mechanism has the derived type inherit from the
1942 -- actual, which is only correct outside of the instance. If the
1943 -- subprogram is inherited, we test for this particular case through a
1944 -- convoluted tree traversal before setting the proper subprogram to be
1945 -- called.
1947 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
1948 -- Return true if E comes from an instance that is not yet frozen
1950 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
1951 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1953 function New_Value (From : Node_Id) return Node_Id;
1954 -- From is the original Expression. New_Value is equivalent to a call
1955 -- to Duplicate_Subexpr with an explicit dereference when From is an
1956 -- access parameter.
1958 --------------------------
1959 -- Add_Actual_Parameter --
1960 --------------------------
1962 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1963 Actual_Expr : constant Node_Id :=
1964 Explicit_Actual_Parameter (Insert_Param);
1966 begin
1967 -- Case of insertion is first named actual
1969 if No (Prev) or else
1970 Nkind (Parent (Prev)) /= N_Parameter_Association
1971 then
1972 Set_Next_Named_Actual
1973 (Insert_Param, First_Named_Actual (Call_Node));
1974 Set_First_Named_Actual (Call_Node, Actual_Expr);
1976 if No (Prev) then
1977 if No (Parameter_Associations (Call_Node)) then
1978 Set_Parameter_Associations (Call_Node, New_List);
1979 end if;
1981 Append (Insert_Param, Parameter_Associations (Call_Node));
1983 else
1984 Insert_After (Prev, Insert_Param);
1985 end if;
1987 -- Case of insertion is not first named actual
1989 else
1990 Set_Next_Named_Actual
1991 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1992 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1993 Append (Insert_Param, Parameter_Associations (Call_Node));
1994 end if;
1996 Prev := Actual_Expr;
1997 end Add_Actual_Parameter;
1999 ----------------------
2000 -- Add_Extra_Actual --
2001 ----------------------
2003 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2004 Loc : constant Source_Ptr := Sloc (Expr);
2006 begin
2007 if Extra_Actuals = No_List then
2008 Extra_Actuals := New_List;
2009 Set_Parent (Extra_Actuals, Call_Node);
2010 end if;
2012 Append_To (Extra_Actuals,
2013 Make_Parameter_Association (Loc,
2014 Selector_Name => Make_Identifier (Loc, Chars (EF)),
2015 Explicit_Actual_Parameter => Expr));
2017 Analyze_And_Resolve (Expr, Etype (EF));
2019 if Nkind (Call_Node) = N_Function_Call then
2020 Set_Is_Accessibility_Actual (Parent (Expr));
2021 end if;
2022 end Add_Extra_Actual;
2024 ----------------
2025 -- Do_Inline --
2026 ----------------
2028 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2029 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2031 procedure Do_Backend_Inline;
2032 -- Check that the call can be safely passed to the backend. If true
2033 -- then register the enclosing unit of Subp to Inlined_Bodies so that
2034 -- the body of Subp can be retrieved and analyzed by the backend.
2036 procedure Register_Backend_Call (N : Node_Id);
2037 -- Append N to the list Backend_Calls
2039 -----------------------
2040 -- Do_Backend_Inline --
2041 -----------------------
2043 procedure Do_Backend_Inline is
2044 begin
2045 -- No extra test needed for init subprograms since we know they
2046 -- are available to the backend!
2048 if Is_Init_Proc (Subp) then
2049 Add_Inlined_Body (Subp);
2050 Register_Backend_Call (Call_Node);
2052 -- Verify that if the body to inline is located in the current
2053 -- unit the inlining does not occur earlier. This avoids
2054 -- order-of-elaboration problems in the back end.
2056 elsif In_Same_Extended_Unit (Call_Node, Subp)
2057 and then Nkind (Spec) = N_Subprogram_Declaration
2058 and then Earlier_In_Extended_Unit
2059 (Loc, Sloc (Body_To_Inline (Spec)))
2060 then
2061 Error_Msg_NE
2062 ("cannot inline& (body not seen yet)??", Call_Node, Subp);
2064 else
2065 declare
2066 Backend_Inline : Boolean := True;
2068 begin
2069 -- If we are compiling a package body that is not the
2070 -- main unit, it must be for inlining/instantiation
2071 -- purposes, in which case we inline the call to insure
2072 -- that the same temporaries are generated when compiling
2073 -- the body by itself. Otherwise link errors can occur.
2075 -- If the function being called is itself in the main
2076 -- unit, we cannot inline, because there is a risk of
2077 -- double elaboration and/or circularity: the inlining
2078 -- can make visible a private entity in the body of the
2079 -- main unit, that gigi will see before its sees its
2080 -- proper definition.
2082 if not (In_Extended_Main_Code_Unit (Call_Node))
2083 and then In_Package_Body
2084 then
2085 Backend_Inline :=
2086 not In_Extended_Main_Source_Unit (Subp);
2087 end if;
2089 if Backend_Inline then
2090 Add_Inlined_Body (Subp);
2091 Register_Backend_Call (Call_Node);
2092 end if;
2093 end;
2094 end if;
2095 end Do_Backend_Inline;
2097 ---------------------------
2098 -- Register_Backend_Call --
2099 ---------------------------
2101 procedure Register_Backend_Call (N : Node_Id) is
2102 begin
2103 if Backend_Calls = No_Elist then
2104 Backend_Calls := New_Elmt_List;
2105 end if;
2107 Append_Elmt (N, To => Backend_Calls);
2108 end Register_Backend_Call;
2110 -- Start of processing for Do_Inline
2112 begin
2113 -- Verify that the body to inline has already been seen
2115 if No (Spec)
2116 or else Nkind (Spec) /= N_Subprogram_Declaration
2117 or else No (Body_To_Inline (Spec))
2118 then
2119 if Comes_From_Source (Subp)
2120 and then Must_Inline (Subp)
2121 then
2122 Cannot_Inline
2123 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2125 -- Let the back end handle it
2127 else
2128 Do_Backend_Inline;
2129 return;
2130 end if;
2132 -- If this an inherited function that returns a private type, do not
2133 -- inline if the full view is an unconstrained array, because such
2134 -- calls cannot be inlined.
2136 elsif Present (Orig_Subp)
2137 and then Is_Array_Type (Etype (Orig_Subp))
2138 and then not Is_Constrained (Etype (Orig_Subp))
2139 then
2140 Cannot_Inline
2141 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2143 else
2144 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2145 end if;
2146 end Do_Inline;
2148 ----------------------
2149 -- Do_Inline_Always --
2150 ----------------------
2152 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2153 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2154 Body_Id : Entity_Id;
2156 begin
2157 if No (Spec)
2158 or else Nkind (Spec) /= N_Subprogram_Declaration
2159 or else No (Body_To_Inline (Spec))
2160 or else Serious_Errors_Detected /= 0
2161 then
2162 return;
2163 end if;
2165 Body_Id := Corresponding_Body (Spec);
2167 -- Verify that the body to inline has already been seen
2169 if No (Body_Id)
2170 or else not Analyzed (Body_Id)
2171 then
2172 Set_Is_Inlined (Subp, False);
2174 if Comes_From_Source (Subp) then
2176 -- Report a warning only if the call is located in the unit of
2177 -- the called subprogram; otherwise it is an error.
2179 if not In_Same_Extended_Unit (Call_Node, Subp) then
2180 Cannot_Inline
2181 ("cannot inline& (body not seen yet)?", Call_Node, Subp,
2182 Is_Serious => True);
2184 elsif In_Open_Scopes (Subp) then
2186 -- For backward compatibility we generate the same error
2187 -- or warning of the previous implementation. This will
2188 -- be changed when we definitely incorporate the new
2189 -- support ???
2191 if Front_End_Inlining
2192 and then Optimization_Level = 0
2193 then
2194 Error_Msg_N
2195 ("call to recursive subprogram cannot be inlined?p?",
2198 -- Do not emit error compiling runtime packages
2200 elsif Is_Predefined_File_Name
2201 (Unit_File_Name (Get_Source_Unit (Subp)))
2202 then
2203 Error_Msg_N
2204 ("call to recursive subprogram cannot be inlined??",
2207 else
2208 Error_Msg_N
2209 ("call to recursive subprogram cannot be inlined",
2211 end if;
2213 else
2214 Cannot_Inline
2215 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2216 end if;
2217 end if;
2219 return;
2221 -- If this an inherited function that returns a private type, do not
2222 -- inline if the full view is an unconstrained array, because such
2223 -- calls cannot be inlined.
2225 elsif Present (Orig_Subp)
2226 and then Is_Array_Type (Etype (Orig_Subp))
2227 and then not Is_Constrained (Etype (Orig_Subp))
2228 then
2229 Cannot_Inline
2230 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2232 -- If the called subprogram comes from an instance in the same
2233 -- unit, and the instance is not yet frozen, inlining might
2234 -- trigger order-of-elaboration problems.
2236 elsif In_Unfrozen_Instance (Scope (Subp)) then
2237 Cannot_Inline
2238 ("cannot inline& (unfrozen instance)?", Call_Node, Subp);
2240 else
2241 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2242 end if;
2243 end Do_Inline_Always;
2245 ---------------------------
2246 -- Inherited_From_Formal --
2247 ---------------------------
2249 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2250 Par : Entity_Id;
2251 Gen_Par : Entity_Id;
2252 Gen_Prim : Elist_Id;
2253 Elmt : Elmt_Id;
2254 Indic : Node_Id;
2256 begin
2257 -- If the operation is inherited, it is attached to the corresponding
2258 -- type derivation. If the parent in the derivation is a generic
2259 -- actual, it is a subtype of the actual, and we have to recover the
2260 -- original derived type declaration to find the proper parent.
2262 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2263 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2264 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2265 N_Derived_Type_Definition
2266 or else not In_Instance
2267 then
2268 return Empty;
2270 else
2271 Indic :=
2272 Subtype_Indication
2273 (Type_Definition (Original_Node (Parent (S))));
2275 if Nkind (Indic) = N_Subtype_Indication then
2276 Par := Entity (Subtype_Mark (Indic));
2277 else
2278 Par := Entity (Indic);
2279 end if;
2280 end if;
2282 if not Is_Generic_Actual_Type (Par)
2283 or else Is_Tagged_Type (Par)
2284 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2285 or else not In_Open_Scopes (Scope (Par))
2286 then
2287 return Empty;
2288 else
2289 Gen_Par := Generic_Parent_Type (Parent (Par));
2290 end if;
2292 -- If the actual has no generic parent type, the formal is not
2293 -- a formal derived type, so nothing to inherit.
2295 if No (Gen_Par) then
2296 return Empty;
2297 end if;
2299 -- If the generic parent type is still the generic type, this is a
2300 -- private formal, not a derived formal, and there are no operations
2301 -- inherited from the formal.
2303 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2304 return Empty;
2305 end if;
2307 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2309 Elmt := First_Elmt (Gen_Prim);
2310 while Present (Elmt) loop
2311 if Chars (Node (Elmt)) = Chars (S) then
2312 declare
2313 F1 : Entity_Id;
2314 F2 : Entity_Id;
2316 begin
2317 F1 := First_Formal (S);
2318 F2 := First_Formal (Node (Elmt));
2319 while Present (F1)
2320 and then Present (F2)
2321 loop
2322 if Etype (F1) = Etype (F2)
2323 or else Etype (F2) = Gen_Par
2324 then
2325 Next_Formal (F1);
2326 Next_Formal (F2);
2327 else
2328 Next_Elmt (Elmt);
2329 exit; -- not the right subprogram
2330 end if;
2332 return Node (Elmt);
2333 end loop;
2334 end;
2336 else
2337 Next_Elmt (Elmt);
2338 end if;
2339 end loop;
2341 raise Program_Error;
2342 end Inherited_From_Formal;
2344 --------------------------
2345 -- In_Unfrozen_Instance --
2346 --------------------------
2348 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2349 S : Entity_Id;
2351 begin
2352 S := E;
2353 while Present (S) and then S /= Standard_Standard loop
2354 if Is_Generic_Instance (S)
2355 and then Present (Freeze_Node (S))
2356 and then not Analyzed (Freeze_Node (S))
2357 then
2358 return True;
2359 end if;
2361 S := Scope (S);
2362 end loop;
2364 return False;
2365 end In_Unfrozen_Instance;
2367 -------------------------
2368 -- Is_Direct_Deep_Call --
2369 -------------------------
2371 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2372 begin
2373 if Is_TSS (Subp, TSS_Deep_Adjust)
2374 or else Is_TSS (Subp, TSS_Deep_Finalize)
2375 or else Is_TSS (Subp, TSS_Deep_Initialize)
2376 then
2377 declare
2378 Actual : Node_Id;
2379 Formal : Node_Id;
2381 begin
2382 Actual := First (Parameter_Associations (N));
2383 Formal := First_Formal (Subp);
2384 while Present (Actual)
2385 and then Present (Formal)
2386 loop
2387 if Nkind (Actual) = N_Identifier
2388 and then Is_Controlling_Actual (Actual)
2389 and then Etype (Actual) = Etype (Formal)
2390 then
2391 return True;
2392 end if;
2394 Next (Actual);
2395 Next_Formal (Formal);
2396 end loop;
2397 end;
2398 end if;
2400 return False;
2401 end Is_Direct_Deep_Call;
2403 ---------------
2404 -- New_Value --
2405 ---------------
2407 function New_Value (From : Node_Id) return Node_Id is
2408 Res : constant Node_Id := Duplicate_Subexpr (From);
2409 begin
2410 if Is_Access_Type (Etype (From)) then
2411 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2412 else
2413 return Res;
2414 end if;
2415 end New_Value;
2417 -- Local variables
2419 Curr_S : constant Entity_Id := Current_Scope;
2420 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2421 Actual : Node_Id;
2422 Formal : Entity_Id;
2423 Orig_Subp : Entity_Id := Empty;
2424 Param_Count : Natural := 0;
2425 Parent_Formal : Entity_Id;
2426 Parent_Subp : Entity_Id;
2427 Scop : Entity_Id;
2428 Subp : Entity_Id;
2430 Prev_Orig : Node_Id;
2431 -- Original node for an actual, which may have been rewritten. If the
2432 -- actual is a function call that has been transformed from a selected
2433 -- component, the original node is unanalyzed. Otherwise, it carries
2434 -- semantic information used to generate additional actuals.
2436 CW_Interface_Formals_Present : Boolean := False;
2438 -- Start of processing for Expand_Call
2440 begin
2441 -- Expand the procedure call if the first actual has a dimension and if
2442 -- the procedure is Put (Ada 2012).
2444 if Ada_Version >= Ada_2012
2445 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2446 and then Present (Parameter_Associations (Call_Node))
2447 then
2448 Expand_Put_Call_With_Symbol (Call_Node);
2449 end if;
2451 -- Ignore if previous error
2453 if Nkind (Call_Node) in N_Has_Etype
2454 and then Etype (Call_Node) = Any_Type
2455 then
2456 return;
2457 end if;
2459 -- Call using access to subprogram with explicit dereference
2461 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2462 Subp := Etype (Name (Call_Node));
2463 Parent_Subp := Empty;
2465 -- Case of call to simple entry, where the Name is a selected component
2466 -- whose prefix is the task, and whose selector name is the entry name
2468 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2469 Subp := Entity (Selector_Name (Name (Call_Node)));
2470 Parent_Subp := Empty;
2472 -- Case of call to member of entry family, where Name is an indexed
2473 -- component, with the prefix being a selected component giving the
2474 -- task and entry family name, and the index being the entry index.
2476 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2477 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2478 Parent_Subp := Empty;
2480 -- Normal case
2482 else
2483 Subp := Entity (Name (Call_Node));
2484 Parent_Subp := Alias (Subp);
2486 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2487 -- if we can tell that the first parameter cannot possibly be null.
2488 -- This improves efficiency by avoiding a run-time test.
2490 -- We do not do this if Raise_Exception_Always does not exist, which
2491 -- can happen in configurable run time profiles which provide only a
2492 -- Raise_Exception.
2494 if Is_RTE (Subp, RE_Raise_Exception)
2495 and then RTE_Available (RE_Raise_Exception_Always)
2496 then
2497 declare
2498 FA : constant Node_Id :=
2499 Original_Node (First_Actual (Call_Node));
2501 begin
2502 -- The case we catch is where the first argument is obtained
2503 -- using the Identity attribute (which must always be
2504 -- non-null).
2506 if Nkind (FA) = N_Attribute_Reference
2507 and then Attribute_Name (FA) = Name_Identity
2508 then
2509 Subp := RTE (RE_Raise_Exception_Always);
2510 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2511 end if;
2512 end;
2513 end if;
2515 if Ekind (Subp) = E_Entry then
2516 Parent_Subp := Empty;
2517 end if;
2518 end if;
2520 -- Detect the following code in System.Finalization_Masters only on
2521 -- .NET/JVM targets:
2523 -- procedure Finalize (Master : in out Finalization_Master) is
2524 -- begin
2525 -- . . .
2526 -- begin
2527 -- Finalize (Curr_Ptr.all);
2529 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2530 -- cannot be named in library or user code, the compiler has to install
2531 -- a kludge and transform the call to Finalize into Deep_Finalize.
2533 if VM_Target /= No_VM
2534 and then Chars (Subp) = Name_Finalize
2535 and then Ekind (Curr_S) = E_Block
2536 and then Ekind (Scope (Curr_S)) = E_Procedure
2537 and then Chars (Scope (Curr_S)) = Name_Finalize
2538 and then Etype (First_Formal (Scope (Curr_S))) =
2539 RTE (RE_Finalization_Master)
2540 then
2541 declare
2542 Deep_Fin : constant Entity_Id :=
2543 Find_Prim_Op (RTE (RE_Root_Controlled),
2544 TSS_Deep_Finalize);
2545 begin
2546 -- Since Root_Controlled is a tagged type, the compiler should
2547 -- always generate Deep_Finalize for it.
2549 pragma Assert (Present (Deep_Fin));
2551 -- Generate:
2552 -- Deep_Finalize (Curr_Ptr.all);
2554 Rewrite (N,
2555 Make_Procedure_Call_Statement (Loc,
2556 Name =>
2557 New_Reference_To (Deep_Fin, Loc),
2558 Parameter_Associations =>
2559 New_Copy_List_Tree (Parameter_Associations (N))));
2561 Analyze (N);
2562 return;
2563 end;
2564 end if;
2566 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2567 -- alternative in an asynchronous select or as an entry call in
2568 -- a conditional or timed select. Check whether the procedure call
2569 -- is a renaming of an entry and rewrite it as an entry call.
2571 if Ada_Version >= Ada_2005
2572 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2573 and then
2574 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2575 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2576 or else
2577 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2578 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2579 then
2580 declare
2581 Ren_Decl : Node_Id;
2582 Ren_Root : Entity_Id := Subp;
2584 begin
2585 -- This may be a chain of renamings, find the root
2587 if Present (Alias (Ren_Root)) then
2588 Ren_Root := Alias (Ren_Root);
2589 end if;
2591 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2592 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2594 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2595 Rewrite (Call_Node,
2596 Make_Entry_Call_Statement (Loc,
2597 Name =>
2598 New_Copy_Tree (Name (Ren_Decl)),
2599 Parameter_Associations =>
2600 New_Copy_List_Tree
2601 (Parameter_Associations (Call_Node))));
2603 return;
2604 end if;
2605 end if;
2606 end;
2607 end if;
2609 -- First step, compute extra actuals, corresponding to any Extra_Formals
2610 -- present. Note that we do not access Extra_Formals directly, instead
2611 -- we simply note the presence of the extra formals as we process the
2612 -- regular formals collecting corresponding actuals in Extra_Actuals.
2614 -- We also generate any required range checks for actuals for in formals
2615 -- as we go through the loop, since this is a convenient place to do it.
2616 -- (Though it seems that this would be better done in Expand_Actuals???)
2618 -- Special case: Thunks must not compute the extra actuals; they must
2619 -- just propagate to the target primitive their extra actuals.
2621 if Is_Thunk (Current_Scope)
2622 and then Thunk_Entity (Current_Scope) = Subp
2623 and then Present (Extra_Formals (Subp))
2624 then
2625 pragma Assert (Present (Extra_Formals (Current_Scope)));
2627 declare
2628 Target_Formal : Entity_Id;
2629 Thunk_Formal : Entity_Id;
2631 begin
2632 Target_Formal := Extra_Formals (Subp);
2633 Thunk_Formal := Extra_Formals (Current_Scope);
2634 while Present (Target_Formal) loop
2635 Add_Extra_Actual
2636 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2638 Target_Formal := Extra_Formal (Target_Formal);
2639 Thunk_Formal := Extra_Formal (Thunk_Formal);
2640 end loop;
2642 while Is_Non_Empty_List (Extra_Actuals) loop
2643 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2644 end loop;
2646 Expand_Actuals (Call_Node, Subp);
2647 return;
2648 end;
2649 end if;
2651 Formal := First_Formal (Subp);
2652 Actual := First_Actual (Call_Node);
2653 Param_Count := 1;
2654 while Present (Formal) loop
2656 -- Generate range check if required
2658 if Do_Range_Check (Actual)
2659 and then Ekind (Formal) = E_In_Parameter
2660 then
2661 Set_Do_Range_Check (Actual, False);
2662 Generate_Range_Check
2663 (Actual, Etype (Formal), CE_Range_Check_Failed);
2664 end if;
2666 -- Prepare to examine current entry
2668 Prev := Actual;
2669 Prev_Orig := Original_Node (Prev);
2671 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2672 -- to expand it in a further round.
2674 CW_Interface_Formals_Present :=
2675 CW_Interface_Formals_Present
2676 or else
2677 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2678 and then Is_Interface (Etype (Etype (Formal))))
2679 or else
2680 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2681 and then Is_Interface (Directly_Designated_Type
2682 (Etype (Etype (Formal)))));
2684 -- Create possible extra actual for constrained case. Usually, the
2685 -- extra actual is of the form actual'constrained, but since this
2686 -- attribute is only available for unconstrained records, TRUE is
2687 -- expanded if the type of the formal happens to be constrained (for
2688 -- instance when this procedure is inherited from an unconstrained
2689 -- record to a constrained one) or if the actual has no discriminant
2690 -- (its type is constrained). An exception to this is the case of a
2691 -- private type without discriminants. In this case we pass FALSE
2692 -- because the object has underlying discriminants with defaults.
2694 if Present (Extra_Constrained (Formal)) then
2695 if Ekind (Etype (Prev)) in Private_Kind
2696 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2697 then
2698 Add_Extra_Actual
2699 (New_Occurrence_Of (Standard_False, Loc),
2700 Extra_Constrained (Formal));
2702 elsif Is_Constrained (Etype (Formal))
2703 or else not Has_Discriminants (Etype (Prev))
2704 then
2705 Add_Extra_Actual
2706 (New_Occurrence_Of (Standard_True, Loc),
2707 Extra_Constrained (Formal));
2709 -- Do not produce extra actuals for Unchecked_Union parameters.
2710 -- Jump directly to the end of the loop.
2712 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2713 goto Skip_Extra_Actual_Generation;
2715 else
2716 -- If the actual is a type conversion, then the constrained
2717 -- test applies to the actual, not the target type.
2719 declare
2720 Act_Prev : Node_Id;
2722 begin
2723 -- Test for unchecked conversions as well, which can occur
2724 -- as out parameter actuals on calls to stream procedures.
2726 Act_Prev := Prev;
2727 while Nkind_In (Act_Prev, N_Type_Conversion,
2728 N_Unchecked_Type_Conversion)
2729 loop
2730 Act_Prev := Expression (Act_Prev);
2731 end loop;
2733 -- If the expression is a conversion of a dereference, this
2734 -- is internally generated code that manipulates addresses,
2735 -- e.g. when building interface tables. No check should
2736 -- occur in this case, and the discriminated object is not
2737 -- directly a hand.
2739 if not Comes_From_Source (Actual)
2740 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2741 and then Nkind (Act_Prev) = N_Explicit_Dereference
2742 then
2743 Add_Extra_Actual
2744 (New_Occurrence_Of (Standard_False, Loc),
2745 Extra_Constrained (Formal));
2747 else
2748 Add_Extra_Actual
2749 (Make_Attribute_Reference (Sloc (Prev),
2750 Prefix =>
2751 Duplicate_Subexpr_No_Checks
2752 (Act_Prev, Name_Req => True),
2753 Attribute_Name => Name_Constrained),
2754 Extra_Constrained (Formal));
2755 end if;
2756 end;
2757 end if;
2758 end if;
2760 -- Create possible extra actual for accessibility level
2762 if Present (Extra_Accessibility (Formal)) then
2764 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2765 -- attribute, then the original actual may be an aliased object
2766 -- occurring as the prefix in a call using "Object.Operation"
2767 -- notation. In that case we must pass the level of the object,
2768 -- so Prev_Orig is reset to Prev and the attribute will be
2769 -- processed by the code for Access attributes further below.
2771 if Prev_Orig /= Prev
2772 and then Nkind (Prev) = N_Attribute_Reference
2773 and then
2774 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2775 and then Is_Aliased_View (Prev_Orig)
2776 then
2777 Prev_Orig := Prev;
2778 end if;
2780 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2781 -- accessibility levels.
2783 if Is_Thunk (Current_Scope) then
2784 declare
2785 Parm_Ent : Entity_Id;
2787 begin
2788 if Is_Controlling_Actual (Actual) then
2790 -- Find the corresponding actual of the thunk
2792 Parm_Ent := First_Entity (Current_Scope);
2793 for J in 2 .. Param_Count loop
2794 Next_Entity (Parm_Ent);
2795 end loop;
2797 -- Handle unchecked conversion of access types generated
2798 -- in thunks (cf. Expand_Interface_Thunk).
2800 elsif Is_Access_Type (Etype (Actual))
2801 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2802 then
2803 Parm_Ent := Entity (Expression (Actual));
2805 else pragma Assert (Is_Entity_Name (Actual));
2806 Parm_Ent := Entity (Actual);
2807 end if;
2809 Add_Extra_Actual
2810 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2811 Extra_Accessibility (Formal));
2812 end;
2814 elsif Is_Entity_Name (Prev_Orig) then
2816 -- When passing an access parameter, or a renaming of an access
2817 -- parameter, as the actual to another access parameter we need
2818 -- to pass along the actual's own access level parameter. This
2819 -- is done if we are within the scope of the formal access
2820 -- parameter (if this is an inlined body the extra formal is
2821 -- irrelevant).
2823 if (Is_Formal (Entity (Prev_Orig))
2824 or else
2825 (Present (Renamed_Object (Entity (Prev_Orig)))
2826 and then
2827 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2828 and then
2829 Is_Formal
2830 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2831 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2832 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2833 then
2834 declare
2835 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2837 begin
2838 pragma Assert (Present (Parm_Ent));
2840 if Present (Extra_Accessibility (Parm_Ent)) then
2841 Add_Extra_Actual
2842 (New_Occurrence_Of
2843 (Extra_Accessibility (Parm_Ent), Loc),
2844 Extra_Accessibility (Formal));
2846 -- If the actual access parameter does not have an
2847 -- associated extra formal providing its scope level,
2848 -- then treat the actual as having library-level
2849 -- accessibility.
2851 else
2852 Add_Extra_Actual
2853 (Make_Integer_Literal (Loc,
2854 Intval => Scope_Depth (Standard_Standard)),
2855 Extra_Accessibility (Formal));
2856 end if;
2857 end;
2859 -- The actual is a normal access value, so just pass the level
2860 -- of the actual's access type.
2862 else
2863 Add_Extra_Actual
2864 (Dynamic_Accessibility_Level (Prev_Orig),
2865 Extra_Accessibility (Formal));
2866 end if;
2868 -- If the actual is an access discriminant, then pass the level
2869 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2871 elsif Nkind (Prev_Orig) = N_Selected_Component
2872 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2873 E_Discriminant
2874 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2875 E_Anonymous_Access_Type
2876 then
2877 Add_Extra_Actual
2878 (Make_Integer_Literal (Loc,
2879 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2880 Extra_Accessibility (Formal));
2882 -- All other cases
2884 else
2885 case Nkind (Prev_Orig) is
2887 when N_Attribute_Reference =>
2888 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2890 -- For X'Access, pass on the level of the prefix X
2892 when Attribute_Access =>
2894 -- If this is an Access attribute applied to the
2895 -- the current instance object passed to a type
2896 -- initialization procedure, then use the level
2897 -- of the type itself. This is not really correct,
2898 -- as there should be an extra level parameter
2899 -- passed in with _init formals (only in the case
2900 -- where the type is immutably limited), but we
2901 -- don't have an easy way currently to create such
2902 -- an extra formal (init procs aren't ever frozen).
2903 -- For now we just use the level of the type,
2904 -- which may be too shallow, but that works better
2905 -- than passing Object_Access_Level of the type,
2906 -- which can be one level too deep in some cases.
2907 -- ???
2909 if Is_Entity_Name (Prefix (Prev_Orig))
2910 and then Is_Type (Entity (Prefix (Prev_Orig)))
2911 then
2912 Add_Extra_Actual
2913 (Make_Integer_Literal (Loc,
2914 Intval =>
2915 Type_Access_Level
2916 (Entity (Prefix (Prev_Orig)))),
2917 Extra_Accessibility (Formal));
2919 else
2920 Add_Extra_Actual
2921 (Make_Integer_Literal (Loc,
2922 Intval =>
2923 Object_Access_Level
2924 (Prefix (Prev_Orig))),
2925 Extra_Accessibility (Formal));
2926 end if;
2928 -- Treat the unchecked attributes as library-level
2930 when Attribute_Unchecked_Access |
2931 Attribute_Unrestricted_Access =>
2932 Add_Extra_Actual
2933 (Make_Integer_Literal (Loc,
2934 Intval => Scope_Depth (Standard_Standard)),
2935 Extra_Accessibility (Formal));
2937 -- No other cases of attributes returning access
2938 -- values that can be passed to access parameters.
2940 when others =>
2941 raise Program_Error;
2943 end case;
2945 -- For allocators we pass the level of the execution of the
2946 -- called subprogram, which is one greater than the current
2947 -- scope level.
2949 when N_Allocator =>
2950 Add_Extra_Actual
2951 (Make_Integer_Literal (Loc,
2952 Intval => Scope_Depth (Current_Scope) + 1),
2953 Extra_Accessibility (Formal));
2955 -- For most other cases we simply pass the level of the
2956 -- actual's access type. The type is retrieved from
2957 -- Prev rather than Prev_Orig, because in some cases
2958 -- Prev_Orig denotes an original expression that has
2959 -- not been analyzed.
2961 when others =>
2962 Add_Extra_Actual
2963 (Dynamic_Accessibility_Level (Prev),
2964 Extra_Accessibility (Formal));
2965 end case;
2966 end if;
2967 end if;
2969 -- Perform the check of 4.6(49) that prevents a null value from being
2970 -- passed as an actual to an access parameter. Note that the check
2971 -- is elided in the common cases of passing an access attribute or
2972 -- access parameter as an actual. Also, we currently don't enforce
2973 -- this check for expander-generated actuals and when -gnatdj is set.
2975 if Ada_Version >= Ada_2005 then
2977 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2978 -- the intent of 6.4.1(13) is that null-exclusion checks should
2979 -- not be done for 'out' parameters, even though it refers only
2980 -- to constraint checks, and a null_exclusion is not a constraint.
2981 -- Note that AI05-0196-1 corrects this mistake in the RM.
2983 if Is_Access_Type (Etype (Formal))
2984 and then Can_Never_Be_Null (Etype (Formal))
2985 and then Ekind (Formal) /= E_Out_Parameter
2986 and then Nkind (Prev) /= N_Raise_Constraint_Error
2987 and then (Known_Null (Prev)
2988 or else not Can_Never_Be_Null (Etype (Prev)))
2989 then
2990 Install_Null_Excluding_Check (Prev);
2991 end if;
2993 -- Ada_Version < Ada_2005
2995 else
2996 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2997 or else Access_Checks_Suppressed (Subp)
2998 then
2999 null;
3001 elsif Debug_Flag_J then
3002 null;
3004 elsif not Comes_From_Source (Prev) then
3005 null;
3007 elsif Is_Entity_Name (Prev)
3008 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3009 then
3010 null;
3012 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3013 null;
3015 -- Suppress null checks when passing to access parameters of Java
3016 -- and CIL subprograms. (Should this be done for other foreign
3017 -- conventions as well ???)
3019 elsif Convention (Subp) = Convention_Java
3020 or else Convention (Subp) = Convention_CIL
3021 then
3022 null;
3024 else
3025 Install_Null_Excluding_Check (Prev);
3026 end if;
3027 end if;
3029 -- Perform appropriate validity checks on parameters that
3030 -- are entities.
3032 if Validity_Checks_On then
3033 if (Ekind (Formal) = E_In_Parameter
3034 and then Validity_Check_In_Params)
3035 or else
3036 (Ekind (Formal) = E_In_Out_Parameter
3037 and then Validity_Check_In_Out_Params)
3038 then
3039 -- If the actual is an indexed component of a packed type (or
3040 -- is an indexed or selected component whose prefix recursively
3041 -- meets this condition), it has not been expanded yet. It will
3042 -- be copied in the validity code that follows, and has to be
3043 -- expanded appropriately, so reanalyze it.
3045 -- What we do is just to unset analyzed bits on prefixes till
3046 -- we reach something that does not have a prefix.
3048 declare
3049 Nod : Node_Id;
3051 begin
3052 Nod := Actual;
3053 while Nkind_In (Nod, N_Indexed_Component,
3054 N_Selected_Component)
3055 loop
3056 Set_Analyzed (Nod, False);
3057 Nod := Prefix (Nod);
3058 end loop;
3059 end;
3061 Ensure_Valid (Actual);
3062 end if;
3063 end if;
3065 -- For Ada 2012, if a parameter is aliased, the actual must be a
3066 -- tagged type or an aliased view of an object.
3068 if Is_Aliased (Formal)
3069 and then not Is_Aliased_View (Actual)
3070 and then not Is_Tagged_Type (Etype (Formal))
3071 then
3072 Error_Msg_NE
3073 ("actual for aliased formal& must be aliased object",
3074 Actual, Formal);
3075 end if;
3077 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3078 -- since this is a left side reference. We only do this for calls
3079 -- from the source program since we assume that compiler generated
3080 -- calls explicitly generate any required checks. We also need it
3081 -- only if we are doing standard validity checks, since clearly it is
3082 -- not needed if validity checks are off, and in subscript validity
3083 -- checking mode, all indexed components are checked with a call
3084 -- directly from Expand_N_Indexed_Component.
3086 if Comes_From_Source (Call_Node)
3087 and then Ekind (Formal) /= E_In_Parameter
3088 and then Validity_Checks_On
3089 and then Validity_Check_Default
3090 and then not Validity_Check_Subscripts
3091 then
3092 Check_Valid_Lvalue_Subscripts (Actual);
3093 end if;
3095 -- Mark any scalar OUT parameter that is a simple variable as no
3096 -- longer known to be valid (unless the type is always valid). This
3097 -- reflects the fact that if an OUT parameter is never set in a
3098 -- procedure, then it can become invalid on the procedure return.
3100 if Ekind (Formal) = E_Out_Parameter
3101 and then Is_Entity_Name (Actual)
3102 and then Ekind (Entity (Actual)) = E_Variable
3103 and then not Is_Known_Valid (Etype (Actual))
3104 then
3105 Set_Is_Known_Valid (Entity (Actual), False);
3106 end if;
3108 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3109 -- clear current values, since they can be clobbered. We are probably
3110 -- doing this in more places than we need to, but better safe than
3111 -- sorry when it comes to retaining bad current values!
3113 if Ekind (Formal) /= E_In_Parameter
3114 and then Is_Entity_Name (Actual)
3115 and then Present (Entity (Actual))
3116 then
3117 declare
3118 Ent : constant Entity_Id := Entity (Actual);
3119 Sav : Node_Id;
3121 begin
3122 -- For an OUT or IN OUT parameter that is an assignable entity,
3123 -- we do not want to clobber the Last_Assignment field, since
3124 -- if it is set, it was precisely because it is indeed an OUT
3125 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
3126 -- since the subprogram could have returned in invalid value.
3128 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3129 and then Is_Assignable (Ent)
3130 then
3131 Sav := Last_Assignment (Ent);
3132 Kill_Current_Values (Ent);
3133 Set_Last_Assignment (Ent, Sav);
3134 Set_Is_Known_Valid (Ent, False);
3136 -- For all other cases, just kill the current values
3138 else
3139 Kill_Current_Values (Ent);
3140 end if;
3141 end;
3142 end if;
3144 -- If the formal is class wide and the actual is an aggregate, force
3145 -- evaluation so that the back end who does not know about class-wide
3146 -- type, does not generate a temporary of the wrong size.
3148 if not Is_Class_Wide_Type (Etype (Formal)) then
3149 null;
3151 elsif Nkind (Actual) = N_Aggregate
3152 or else (Nkind (Actual) = N_Qualified_Expression
3153 and then Nkind (Expression (Actual)) = N_Aggregate)
3154 then
3155 Force_Evaluation (Actual);
3156 end if;
3158 -- In a remote call, if the formal is of a class-wide type, check
3159 -- that the actual meets the requirements described in E.4(18).
3161 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3162 Insert_Action (Actual,
3163 Make_Transportable_Check (Loc,
3164 Duplicate_Subexpr_Move_Checks (Actual)));
3165 end if;
3167 -- This label is required when skipping extra actual generation for
3168 -- Unchecked_Union parameters.
3170 <<Skip_Extra_Actual_Generation>>
3172 Param_Count := Param_Count + 1;
3173 Next_Actual (Actual);
3174 Next_Formal (Formal);
3175 end loop;
3177 -- If we are calling an Ada 2012 function which needs to have the
3178 -- "accessibility level determined by the point of call" (AI05-0234)
3179 -- passed in to it, then pass it in.
3181 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3182 and then
3183 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3184 then
3185 declare
3186 Ancestor : Node_Id := Parent (Call_Node);
3187 Level : Node_Id := Empty;
3188 Defer : Boolean := False;
3190 begin
3191 -- Unimplemented: if Subp returns an anonymous access type, then
3193 -- a) if the call is the operand of an explict conversion, then
3194 -- the target type of the conversion (a named access type)
3195 -- determines the accessibility level pass in;
3197 -- b) if the call defines an access discriminant of an object
3198 -- (e.g., the discriminant of an object being created by an
3199 -- allocator, or the discriminant of a function result),
3200 -- then the accessibility level to pass in is that of the
3201 -- discriminated object being initialized).
3203 -- ???
3205 while Nkind (Ancestor) = N_Qualified_Expression
3206 loop
3207 Ancestor := Parent (Ancestor);
3208 end loop;
3210 case Nkind (Ancestor) is
3211 when N_Allocator =>
3213 -- At this point, we'd like to assign
3215 -- Level := Dynamic_Accessibility_Level (Ancestor);
3217 -- but Etype of Ancestor may not have been set yet,
3218 -- so that doesn't work.
3220 -- Handle this later in Expand_Allocator_Expression.
3222 Defer := True;
3224 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3225 declare
3226 Def_Id : constant Entity_Id :=
3227 Defining_Identifier (Ancestor);
3229 begin
3230 if Is_Return_Object (Def_Id) then
3231 if Present (Extra_Accessibility_Of_Result
3232 (Return_Applies_To (Scope (Def_Id))))
3233 then
3234 -- Pass along value that was passed in if the
3235 -- routine we are returning from also has an
3236 -- Accessibility_Of_Result formal.
3238 Level :=
3239 New_Occurrence_Of
3240 (Extra_Accessibility_Of_Result
3241 (Return_Applies_To (Scope (Def_Id))), Loc);
3242 end if;
3243 else
3244 Level :=
3245 Make_Integer_Literal (Loc,
3246 Intval => Object_Access_Level (Def_Id));
3247 end if;
3248 end;
3250 when N_Simple_Return_Statement =>
3251 if Present (Extra_Accessibility_Of_Result
3252 (Return_Applies_To
3253 (Return_Statement_Entity (Ancestor))))
3254 then
3255 -- Pass along value that was passed in if the routine
3256 -- we are returning from also has an
3257 -- Accessibility_Of_Result formal.
3259 Level :=
3260 New_Occurrence_Of
3261 (Extra_Accessibility_Of_Result
3262 (Return_Applies_To
3263 (Return_Statement_Entity (Ancestor))), Loc);
3264 end if;
3266 when others =>
3267 null;
3268 end case;
3270 if not Defer then
3271 if not Present (Level) then
3273 -- The "innermost master that evaluates the function call".
3275 -- ??? - Should we use Integer'Last here instead in order
3276 -- to deal with (some of) the problems associated with
3277 -- calls to subps whose enclosing scope is unknown (e.g.,
3278 -- Anon_Access_To_Subp_Param.all)?
3280 Level := Make_Integer_Literal (Loc,
3281 Scope_Depth (Current_Scope) + 1);
3282 end if;
3284 Add_Extra_Actual
3285 (Level,
3286 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3287 end if;
3288 end;
3289 end if;
3291 -- If we are expanding the RHS of an assignment we need to check if tag
3292 -- propagation is needed. You might expect this processing to be in
3293 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3294 -- assignment might be transformed to a declaration for an unconstrained
3295 -- value if the expression is classwide.
3297 if Nkind (Call_Node) = N_Function_Call
3298 and then Is_Tag_Indeterminate (Call_Node)
3299 and then Is_Entity_Name (Name (Call_Node))
3300 then
3301 declare
3302 Ass : Node_Id := Empty;
3304 begin
3305 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3306 Ass := Parent (Call_Node);
3308 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3309 and then Nkind (Parent (Parent (Call_Node))) =
3310 N_Assignment_Statement
3311 then
3312 Ass := Parent (Parent (Call_Node));
3314 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3315 and then Nkind (Parent (Parent (Call_Node))) =
3316 N_Assignment_Statement
3317 then
3318 Ass := Parent (Parent (Call_Node));
3319 end if;
3321 if Present (Ass)
3322 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3323 then
3324 if Is_Access_Type (Etype (Call_Node)) then
3325 if Designated_Type (Etype (Call_Node)) /=
3326 Root_Type (Etype (Name (Ass)))
3327 then
3328 Error_Msg_NE
3329 ("tag-indeterminate expression "
3330 & " must have designated type& (RM 5.2 (6))",
3331 Call_Node, Root_Type (Etype (Name (Ass))));
3332 else
3333 Propagate_Tag (Name (Ass), Call_Node);
3334 end if;
3336 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3337 Error_Msg_NE
3338 ("tag-indeterminate expression must have type&"
3339 & "(RM 5.2 (6))",
3340 Call_Node, Root_Type (Etype (Name (Ass))));
3342 else
3343 Propagate_Tag (Name (Ass), Call_Node);
3344 end if;
3346 -- The call will be rewritten as a dispatching call, and
3347 -- expanded as such.
3349 return;
3350 end if;
3351 end;
3352 end if;
3354 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3355 -- it to point to the correct secondary virtual table
3357 if Nkind (Call_Node) in N_Subprogram_Call
3358 and then CW_Interface_Formals_Present
3359 then
3360 Expand_Interface_Actuals (Call_Node);
3361 end if;
3363 -- Deals with Dispatch_Call if we still have a call, before expanding
3364 -- extra actuals since this will be done on the re-analysis of the
3365 -- dispatching call. Note that we do not try to shorten the actual list
3366 -- for a dispatching call, it would not make sense to do so. Expansion
3367 -- of dispatching calls is suppressed when VM_Target, because the VM
3368 -- back-ends directly handle the generation of dispatching calls and
3369 -- would have to undo any expansion to an indirect call.
3371 if Nkind (Call_Node) in N_Subprogram_Call
3372 and then Present (Controlling_Argument (Call_Node))
3373 then
3374 declare
3375 Call_Typ : constant Entity_Id := Etype (Call_Node);
3376 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3377 Eq_Prim_Op : Entity_Id := Empty;
3378 New_Call : Node_Id;
3379 Param : Node_Id;
3380 Prev_Call : Node_Id;
3382 begin
3383 if not Is_Limited_Type (Typ) then
3384 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3385 end if;
3387 if Tagged_Type_Expansion then
3388 Expand_Dispatching_Call (Call_Node);
3390 -- The following return is worrisome. Is it really OK to skip
3391 -- all remaining processing in this procedure ???
3393 return;
3395 -- VM targets
3397 else
3398 Apply_Tag_Checks (Call_Node);
3400 -- If this is a dispatching "=", we must first compare the
3401 -- tags so we generate: x.tag = y.tag and then x = y
3403 if Subp = Eq_Prim_Op then
3405 -- Mark the node as analyzed to avoid reanalizing this
3406 -- dispatching call (which would cause a never-ending loop)
3408 Prev_Call := Relocate_Node (Call_Node);
3409 Set_Analyzed (Prev_Call);
3411 Param := First_Actual (Call_Node);
3412 New_Call :=
3413 Make_And_Then (Loc,
3414 Left_Opnd =>
3415 Make_Op_Eq (Loc,
3416 Left_Opnd =>
3417 Make_Selected_Component (Loc,
3418 Prefix => New_Value (Param),
3419 Selector_Name =>
3420 New_Reference_To (First_Tag_Component (Typ),
3421 Loc)),
3423 Right_Opnd =>
3424 Make_Selected_Component (Loc,
3425 Prefix =>
3426 Unchecked_Convert_To (Typ,
3427 New_Value (Next_Actual (Param))),
3428 Selector_Name =>
3429 New_Reference_To
3430 (First_Tag_Component (Typ), Loc))),
3431 Right_Opnd => Prev_Call);
3433 Rewrite (Call_Node, New_Call);
3435 Analyze_And_Resolve
3436 (Call_Node, Call_Typ, Suppress => All_Checks);
3437 end if;
3439 -- Expansion of a dispatching call results in an indirect call,
3440 -- which in turn causes current values to be killed (see
3441 -- Resolve_Call), so on VM targets we do the call here to
3442 -- ensure consistent warnings between VM and non-VM targets.
3444 Kill_Current_Values;
3445 end if;
3447 -- If this is a dispatching "=" then we must update the reference
3448 -- to the call node because we generated:
3449 -- x.tag = y.tag and then x = y
3451 if Subp = Eq_Prim_Op then
3452 Call_Node := Right_Opnd (Call_Node);
3453 end if;
3454 end;
3455 end if;
3457 -- Similarly, expand calls to RCI subprograms on which pragma
3458 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3459 -- later. Do this only when the call comes from source since we
3460 -- do not want such a rewriting to occur in expanded code.
3462 if Is_All_Remote_Call (Call_Node) then
3463 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3465 -- Similarly, do not add extra actuals for an entry call whose entity
3466 -- is a protected procedure, or for an internal protected subprogram
3467 -- call, because it will be rewritten as a protected subprogram call
3468 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3470 elsif Is_Protected_Type (Scope (Subp))
3471 and then (Ekind (Subp) = E_Procedure
3472 or else Ekind (Subp) = E_Function)
3473 then
3474 null;
3476 -- During that loop we gathered the extra actuals (the ones that
3477 -- correspond to Extra_Formals), so now they can be appended.
3479 else
3480 while Is_Non_Empty_List (Extra_Actuals) loop
3481 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3482 end loop;
3483 end if;
3485 -- At this point we have all the actuals, so this is the point at which
3486 -- the various expansion activities for actuals is carried out.
3488 Expand_Actuals (Call_Node, Subp);
3490 -- Verify that the actuals do not share storage. This check must be done
3491 -- on the caller side rather that inside the subprogram to avoid issues
3492 -- of parameter passing.
3494 if Check_Aliasing_Of_Parameters then
3495 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3496 end if;
3498 -- If the subprogram is a renaming, or if it is inherited, replace it in
3499 -- the call with the name of the actual subprogram being called. If this
3500 -- is a dispatching call, the run-time decides what to call. The Alias
3501 -- attribute does not apply to entries.
3503 if Nkind (Call_Node) /= N_Entry_Call_Statement
3504 and then No (Controlling_Argument (Call_Node))
3505 and then Present (Parent_Subp)
3506 and then not Is_Direct_Deep_Call (Subp)
3507 then
3508 if Present (Inherited_From_Formal (Subp)) then
3509 Parent_Subp := Inherited_From_Formal (Subp);
3510 else
3511 Parent_Subp := Ultimate_Alias (Parent_Subp);
3512 end if;
3514 -- The below setting of Entity is suspect, see F109-018 discussion???
3516 Set_Entity (Name (Call_Node), Parent_Subp);
3518 if Is_Abstract_Subprogram (Parent_Subp)
3519 and then not In_Instance
3520 then
3521 Error_Msg_NE
3522 ("cannot call abstract subprogram &!",
3523 Name (Call_Node), Parent_Subp);
3524 end if;
3526 -- Inspect all formals of derived subprogram Subp. Compare parameter
3527 -- types with the parent subprogram and check whether an actual may
3528 -- need a type conversion to the corresponding formal of the parent
3529 -- subprogram.
3531 -- Not clear whether intrinsic subprograms need such conversions. ???
3533 if not Is_Intrinsic_Subprogram (Parent_Subp)
3534 or else Is_Generic_Instance (Parent_Subp)
3535 then
3536 declare
3537 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3538 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3539 -- and resolve the newly generated construct.
3541 -------------
3542 -- Convert --
3543 -------------
3545 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3546 begin
3547 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3548 Analyze (Act);
3549 Resolve (Act, Typ);
3550 end Convert;
3552 -- Local variables
3554 Actual_Typ : Entity_Id;
3555 Formal_Typ : Entity_Id;
3556 Parent_Typ : Entity_Id;
3558 begin
3559 Actual := First_Actual (Call_Node);
3560 Formal := First_Formal (Subp);
3561 Parent_Formal := First_Formal (Parent_Subp);
3562 while Present (Formal) loop
3563 Actual_Typ := Etype (Actual);
3564 Formal_Typ := Etype (Formal);
3565 Parent_Typ := Etype (Parent_Formal);
3567 -- For an IN parameter of a scalar type, the parent formal
3568 -- type and derived formal type differ or the parent formal
3569 -- type and actual type do not match statically.
3571 if Is_Scalar_Type (Formal_Typ)
3572 and then Ekind (Formal) = E_In_Parameter
3573 and then Formal_Typ /= Parent_Typ
3574 and then
3575 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3576 and then not Raises_Constraint_Error (Actual)
3577 then
3578 Convert (Actual, Parent_Typ);
3579 Enable_Range_Check (Actual);
3581 -- If the actual has been marked as requiring a range
3582 -- check, then generate it here.
3584 if Do_Range_Check (Actual) then
3585 Set_Do_Range_Check (Actual, False);
3586 Generate_Range_Check
3587 (Actual, Etype (Formal), CE_Range_Check_Failed);
3588 end if;
3590 -- For access types, the parent formal type and actual type
3591 -- differ.
3593 elsif Is_Access_Type (Formal_Typ)
3594 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3595 then
3596 if Ekind (Formal) /= E_In_Parameter then
3597 Convert (Actual, Parent_Typ);
3599 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3600 and then Designated_Type (Parent_Typ) /=
3601 Designated_Type (Actual_Typ)
3602 and then not Is_Controlling_Formal (Formal)
3603 then
3604 -- This unchecked conversion is not necessary unless
3605 -- inlining is enabled, because in that case the type
3606 -- mismatch may become visible in the body about to be
3607 -- inlined.
3609 Rewrite (Actual,
3610 Unchecked_Convert_To (Parent_Typ,
3611 Relocate_Node (Actual)));
3612 Analyze (Actual);
3613 Resolve (Actual, Parent_Typ);
3614 end if;
3616 -- For array and record types, the parent formal type and
3617 -- derived formal type have different sizes or pragma Pack
3618 -- status.
3620 elsif ((Is_Array_Type (Formal_Typ)
3621 and then Is_Array_Type (Parent_Typ))
3622 or else
3623 (Is_Record_Type (Formal_Typ)
3624 and then Is_Record_Type (Parent_Typ)))
3625 and then
3626 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3627 or else Has_Pragma_Pack (Formal_Typ) /=
3628 Has_Pragma_Pack (Parent_Typ))
3629 then
3630 Convert (Actual, Parent_Typ);
3631 end if;
3633 Next_Actual (Actual);
3634 Next_Formal (Formal);
3635 Next_Formal (Parent_Formal);
3636 end loop;
3637 end;
3638 end if;
3640 Orig_Subp := Subp;
3641 Subp := Parent_Subp;
3642 end if;
3644 -- Check for violation of No_Abort_Statements
3646 if Restriction_Check_Required (No_Abort_Statements)
3647 and then Is_RTE (Subp, RE_Abort_Task)
3648 then
3649 Check_Restriction (No_Abort_Statements, Call_Node);
3651 -- Check for violation of No_Dynamic_Attachment
3653 elsif Restriction_Check_Required (No_Dynamic_Attachment)
3654 and then RTU_Loaded (Ada_Interrupts)
3655 and then (Is_RTE (Subp, RE_Is_Reserved) or else
3656 Is_RTE (Subp, RE_Is_Attached) or else
3657 Is_RTE (Subp, RE_Current_Handler) or else
3658 Is_RTE (Subp, RE_Attach_Handler) or else
3659 Is_RTE (Subp, RE_Exchange_Handler) or else
3660 Is_RTE (Subp, RE_Detach_Handler) or else
3661 Is_RTE (Subp, RE_Reference))
3662 then
3663 Check_Restriction (No_Dynamic_Attachment, Call_Node);
3664 end if;
3666 -- Deal with case where call is an explicit dereference
3668 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3670 -- Handle case of access to protected subprogram type
3672 if Is_Access_Protected_Subprogram_Type
3673 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3674 then
3675 -- If this is a call through an access to protected operation, the
3676 -- prefix has the form (object'address, operation'access). Rewrite
3677 -- as a for other protected calls: the object is the 1st parameter
3678 -- of the list of actuals.
3680 declare
3681 Call : Node_Id;
3682 Parm : List_Id;
3683 Nam : Node_Id;
3684 Obj : Node_Id;
3685 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3687 T : constant Entity_Id :=
3688 Equivalent_Type (Base_Type (Etype (Ptr)));
3690 D_T : constant Entity_Id :=
3691 Designated_Type (Base_Type (Etype (Ptr)));
3693 begin
3694 Obj :=
3695 Make_Selected_Component (Loc,
3696 Prefix => Unchecked_Convert_To (T, Ptr),
3697 Selector_Name =>
3698 New_Occurrence_Of (First_Entity (T), Loc));
3700 Nam :=
3701 Make_Selected_Component (Loc,
3702 Prefix => Unchecked_Convert_To (T, Ptr),
3703 Selector_Name =>
3704 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3706 Nam :=
3707 Make_Explicit_Dereference (Loc,
3708 Prefix => Nam);
3710 if Present (Parameter_Associations (Call_Node)) then
3711 Parm := Parameter_Associations (Call_Node);
3712 else
3713 Parm := New_List;
3714 end if;
3716 Prepend (Obj, Parm);
3718 if Etype (D_T) = Standard_Void_Type then
3719 Call :=
3720 Make_Procedure_Call_Statement (Loc,
3721 Name => Nam,
3722 Parameter_Associations => Parm);
3723 else
3724 Call :=
3725 Make_Function_Call (Loc,
3726 Name => Nam,
3727 Parameter_Associations => Parm);
3728 end if;
3730 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3731 Set_Etype (Call, Etype (D_T));
3733 -- We do not re-analyze the call to avoid infinite recursion.
3734 -- We analyze separately the prefix and the object, and set
3735 -- the checks on the prefix that would otherwise be emitted
3736 -- when resolving a call.
3738 Rewrite (Call_Node, Call);
3739 Analyze (Nam);
3740 Apply_Access_Check (Nam);
3741 Analyze (Obj);
3742 return;
3743 end;
3744 end if;
3745 end if;
3747 -- If this is a call to an intrinsic subprogram, then perform the
3748 -- appropriate expansion to the corresponding tree node and we
3749 -- are all done (since after that the call is gone!)
3751 -- In the case where the intrinsic is to be processed by the back end,
3752 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3753 -- since the idea in this case is to pass the call unchanged. If the
3754 -- intrinsic is an inherited unchecked conversion, and the derived type
3755 -- is the target type of the conversion, we must retain it as the return
3756 -- type of the expression. Otherwise the expansion below, which uses the
3757 -- parent operation, will yield the wrong type.
3759 if Is_Intrinsic_Subprogram (Subp) then
3760 Expand_Intrinsic_Call (Call_Node, Subp);
3762 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3763 and then Parent_Subp /= Orig_Subp
3764 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3765 then
3766 Set_Etype (Call_Node, Etype (Orig_Subp));
3767 end if;
3769 return;
3770 end if;
3772 if Ekind_In (Subp, E_Function, E_Procedure) then
3774 -- We perform two simple optimization on calls:
3776 -- a) replace calls to null procedures unconditionally;
3778 -- b) for To_Address, just do an unchecked conversion. Not only is
3779 -- this efficient, but it also avoids order of elaboration problems
3780 -- when address clauses are inlined (address expression elaborated
3781 -- at the wrong point).
3783 -- We perform these optimization regardless of whether we are in the
3784 -- main unit or in a unit in the context of the main unit, to ensure
3785 -- that tree generated is the same in both cases, for CodePeer use.
3787 if Is_RTE (Subp, RE_To_Address) then
3788 Rewrite (Call_Node,
3789 Unchecked_Convert_To
3790 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3791 return;
3793 elsif Is_Null_Procedure (Subp) then
3794 Rewrite (Call_Node, Make_Null_Statement (Loc));
3795 return;
3796 end if;
3798 -- Handle inlining (old semantics)
3800 if Is_Inlined (Subp) and then not Debug_Flag_Dot_K then
3801 Inlined_Subprogram : declare
3802 Bod : Node_Id;
3803 Must_Inline : Boolean := False;
3804 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3806 begin
3807 -- Verify that the body to inline has already been seen, and
3808 -- that if the body is in the current unit the inlining does
3809 -- not occur earlier. This avoids order-of-elaboration problems
3810 -- in the back end.
3812 -- This should be documented in sinfo/einfo ???
3814 if No (Spec)
3815 or else Nkind (Spec) /= N_Subprogram_Declaration
3816 or else No (Body_To_Inline (Spec))
3817 then
3818 Must_Inline := False;
3820 -- If this an inherited function that returns a private type,
3821 -- do not inline if the full view is an unconstrained array,
3822 -- because such calls cannot be inlined.
3824 elsif Present (Orig_Subp)
3825 and then Is_Array_Type (Etype (Orig_Subp))
3826 and then not Is_Constrained (Etype (Orig_Subp))
3827 then
3828 Must_Inline := False;
3830 elsif In_Unfrozen_Instance (Scope (Subp)) then
3831 Must_Inline := False;
3833 else
3834 Bod := Body_To_Inline (Spec);
3836 if (In_Extended_Main_Code_Unit (Call_Node)
3837 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3838 or else Has_Pragma_Inline_Always (Subp))
3839 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3840 or else
3841 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3842 then
3843 Must_Inline := True;
3845 -- If we are compiling a package body that is not the main
3846 -- unit, it must be for inlining/instantiation purposes,
3847 -- in which case we inline the call to insure that the same
3848 -- temporaries are generated when compiling the body by
3849 -- itself. Otherwise link errors can occur.
3851 -- If the function being called is itself in the main unit,
3852 -- we cannot inline, because there is a risk of double
3853 -- elaboration and/or circularity: the inlining can make
3854 -- visible a private entity in the body of the main unit,
3855 -- that gigi will see before its sees its proper definition.
3857 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3858 and then In_Package_Body
3859 then
3860 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3861 end if;
3862 end if;
3864 if Must_Inline then
3865 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3867 else
3868 -- Let the back end handle it
3870 Add_Inlined_Body (Subp);
3872 if Front_End_Inlining
3873 and then Nkind (Spec) = N_Subprogram_Declaration
3874 and then (In_Extended_Main_Code_Unit (Call_Node))
3875 and then No (Body_To_Inline (Spec))
3876 and then not Has_Completion (Subp)
3877 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3878 then
3879 Cannot_Inline
3880 ("cannot inline& (body not seen yet)?",
3881 Call_Node, Subp);
3882 end if;
3883 end if;
3884 end Inlined_Subprogram;
3886 -- Handle inlining (new semantics)
3888 elsif Is_Inlined (Subp) then
3889 declare
3890 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3892 begin
3893 if Must_Inline (Subp) then
3894 if In_Extended_Main_Code_Unit (Call_Node)
3895 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3896 and then not Has_Completion (Subp)
3897 then
3898 Cannot_Inline
3899 ("cannot inline& (body not seen yet)?",
3900 Call_Node, Subp);
3902 else
3903 Do_Inline_Always (Subp, Orig_Subp);
3904 end if;
3906 elsif Optimization_Level > 0 then
3907 Do_Inline (Subp, Orig_Subp);
3908 end if;
3910 -- The call may have been inlined or may have been passed to
3911 -- the backend. No further action needed if it was inlined.
3913 if Nkind (N) /= N_Function_Call then
3914 return;
3915 end if;
3916 end;
3917 end if;
3918 end if;
3920 -- Check for protected subprogram. This is either an intra-object call,
3921 -- or a protected function call. Protected procedure calls are rewritten
3922 -- as entry calls and handled accordingly.
3924 -- In Ada 2005, this may be an indirect call to an access parameter that
3925 -- is an access_to_subprogram. In that case the anonymous type has a
3926 -- scope that is a protected operation, but the call is a regular one.
3927 -- In either case do not expand call if subprogram is eliminated.
3929 Scop := Scope (Subp);
3931 if Nkind (Call_Node) /= N_Entry_Call_Statement
3932 and then Is_Protected_Type (Scop)
3933 and then Ekind (Subp) /= E_Subprogram_Type
3934 and then not Is_Eliminated (Subp)
3935 then
3936 -- If the call is an internal one, it is rewritten as a call to the
3937 -- corresponding unprotected subprogram.
3939 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3940 end if;
3942 -- Functions returning controlled objects need special attention. If
3943 -- the return type is limited, then the context is initialization and
3944 -- different processing applies. If the call is to a protected function,
3945 -- the expansion above will call Expand_Call recursively. Otherwise the
3946 -- function call is transformed into a temporary which obtains the
3947 -- result from the secondary stack.
3949 if Needs_Finalization (Etype (Subp)) then
3950 if not Is_Immutably_Limited_Type (Etype (Subp))
3951 and then
3952 (No (First_Formal (Subp))
3953 or else
3954 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3955 then
3956 Expand_Ctrl_Function_Call (Call_Node);
3958 -- Build-in-place function calls which appear in anonymous contexts
3959 -- need a transient scope to ensure the proper finalization of the
3960 -- intermediate result after its use.
3962 elsif Is_Build_In_Place_Function_Call (Call_Node)
3963 and then
3964 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3965 N_Function_Call,
3966 N_Indexed_Component,
3967 N_Object_Renaming_Declaration,
3968 N_Procedure_Call_Statement,
3969 N_Selected_Component,
3970 N_Slice)
3971 then
3972 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3973 end if;
3974 end if;
3976 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3977 -- if there are optional parameters at the trailing end.
3978 -- Note: we never delete procedures for call via a pointer.
3980 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3981 and then Present (First_Optional_Parameter (Subp))
3982 then
3983 declare
3984 Last_Keep_Arg : Node_Id;
3986 begin
3987 -- Last_Keep_Arg will hold the last actual that should be kept.
3988 -- If it remains empty at the end, it means that all parameters
3989 -- are optional.
3991 Last_Keep_Arg := Empty;
3993 -- Find first optional parameter, must be present since we checked
3994 -- the validity of the parameter before setting it.
3996 Formal := First_Formal (Subp);
3997 Actual := First_Actual (Call_Node);
3998 while Formal /= First_Optional_Parameter (Subp) loop
3999 Last_Keep_Arg := Actual;
4000 Next_Formal (Formal);
4001 Next_Actual (Actual);
4002 end loop;
4004 -- We have Formal and Actual pointing to the first potentially
4005 -- droppable argument. We can drop all the trailing arguments
4006 -- whose actual matches the default. Note that we know that all
4007 -- remaining formals have defaults, because we checked that this
4008 -- requirement was met before setting First_Optional_Parameter.
4010 -- We use Fully_Conformant_Expressions to check for identity
4011 -- between formals and actuals, which may miss some cases, but
4012 -- on the other hand, this is only an optimization (if we fail
4013 -- to truncate a parameter it does not affect functionality).
4014 -- So if the default is 3 and the actual is 1+2, we consider
4015 -- them unequal, which hardly seems worrisome.
4017 while Present (Formal) loop
4018 if not Fully_Conformant_Expressions
4019 (Actual, Default_Value (Formal))
4020 then
4021 Last_Keep_Arg := Actual;
4022 end if;
4024 Next_Formal (Formal);
4025 Next_Actual (Actual);
4026 end loop;
4028 -- If no arguments, delete entire list, this is the easy case
4030 if No (Last_Keep_Arg) then
4031 Set_Parameter_Associations (Call_Node, No_List);
4032 Set_First_Named_Actual (Call_Node, Empty);
4034 -- Case where at the last retained argument is positional. This
4035 -- is also an easy case, since the retained arguments are already
4036 -- in the right form, and we don't need to worry about the order
4037 -- of arguments that get eliminated.
4039 elsif Is_List_Member (Last_Keep_Arg) then
4040 while Present (Next (Last_Keep_Arg)) loop
4041 Discard_Node (Remove_Next (Last_Keep_Arg));
4042 end loop;
4044 Set_First_Named_Actual (Call_Node, Empty);
4046 -- This is the annoying case where the last retained argument
4047 -- is a named parameter. Since the original arguments are not
4048 -- in declaration order, we may have to delete some fairly
4049 -- random collection of arguments.
4051 else
4052 declare
4053 Temp : Node_Id;
4054 Passoc : Node_Id;
4056 begin
4057 -- First step, remove all the named parameters from the
4058 -- list (they are still chained using First_Named_Actual
4059 -- and Next_Named_Actual, so we have not lost them!)
4061 Temp := First (Parameter_Associations (Call_Node));
4063 -- Case of all parameters named, remove them all
4065 if Nkind (Temp) = N_Parameter_Association then
4066 -- Suppress warnings to avoid warning on possible
4067 -- infinite loop (because Call_Node is not modified).
4069 pragma Warnings (Off);
4070 while Is_Non_Empty_List
4071 (Parameter_Associations (Call_Node))
4072 loop
4073 Temp :=
4074 Remove_Head (Parameter_Associations (Call_Node));
4075 end loop;
4076 pragma Warnings (On);
4078 -- Case of mixed positional/named, remove named parameters
4080 else
4081 while Nkind (Next (Temp)) /= N_Parameter_Association loop
4082 Next (Temp);
4083 end loop;
4085 while Present (Next (Temp)) loop
4086 Remove (Next (Temp));
4087 end loop;
4088 end if;
4090 -- Now we loop through the named parameters, till we get
4091 -- to the last one to be retained, adding them to the list.
4092 -- Note that the Next_Named_Actual list does not need to be
4093 -- touched since we are only reordering them on the actual
4094 -- parameter association list.
4096 Passoc := Parent (First_Named_Actual (Call_Node));
4097 loop
4098 Temp := Relocate_Node (Passoc);
4099 Append_To
4100 (Parameter_Associations (Call_Node), Temp);
4101 exit when
4102 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
4103 Passoc := Parent (Next_Named_Actual (Passoc));
4104 end loop;
4106 Set_Next_Named_Actual (Temp, Empty);
4108 loop
4109 Temp := Next_Named_Actual (Passoc);
4110 exit when No (Temp);
4111 Set_Next_Named_Actual
4112 (Passoc, Next_Named_Actual (Parent (Temp)));
4113 end loop;
4114 end;
4116 end if;
4117 end;
4118 end if;
4119 end Expand_Call;
4121 ---------------------------
4122 -- Expand_Contract_Cases --
4123 ---------------------------
4125 -- Pragma Contract_Cases is expanded in the following manner:
4127 -- subprogram S is
4128 -- Flag_1 : Boolean := False;
4129 -- . . .
4130 -- Flag_N : Boolean := False;
4131 -- Flag_N+1 : Boolean := False; -- when "others" present
4132 -- Count : Natural := 0;
4134 -- <preconditions (if any)>
4136 -- if Case_Guard_1 then
4137 -- Flag_1 := True;
4138 -- Count := Count + 1;
4139 -- end if;
4140 -- . . .
4141 -- if Case_Guard_N then
4142 -- Flag_N := True;
4143 -- Count := Count + 1;
4144 -- end if;
4146 -- if Count = 0 then
4147 -- raise Assertion_Error with "xxx contract cases incomplete";
4148 -- <or>
4149 -- Flag_N+1 := True; -- when "others" present
4151 -- elsif Count > 1 then
4152 -- declare
4153 -- Str0 : constant String :=
4154 -- "contract cases overlap for subprogram ABC";
4155 -- Str1 : constant String :=
4156 -- (if Flag_1 then
4157 -- Str0 & "case guard at xxx evaluates to True"
4158 -- else Str0);
4159 -- StrN : constant String :=
4160 -- (if Flag_N then
4161 -- StrN-1 & "case guard at xxx evaluates to True"
4162 -- else StrN-1);
4163 -- begin
4164 -- raise Assertion_Error with StrN;
4165 -- end;
4166 -- end if;
4168 -- procedure _Postconditions is
4169 -- begin
4170 -- <postconditions (if any)>
4172 -- if Flag_1 and then not Consequence_1 then
4173 -- raise Assertion_Error with "failed contract case at xxx";
4174 -- end if;
4175 -- . . .
4176 -- if Flag_N[+1] and then not Consequence_N[+1] then
4177 -- raise Assertion_Error with "failed contract case at xxx";
4178 -- end if;
4179 -- end _Postconditions;
4180 -- begin
4181 -- . . .
4182 -- end S;
4184 procedure Expand_Contract_Cases
4185 (CCs : Node_Id;
4186 Subp_Id : Entity_Id;
4187 Decls : List_Id;
4188 Stmts : in out List_Id)
4190 Loc : constant Source_Ptr := Sloc (CCs);
4192 procedure Case_Guard_Error
4193 (Decls : List_Id;
4194 Flag : Entity_Id;
4195 Error_Loc : Source_Ptr;
4196 Msg : in out Entity_Id);
4197 -- Given a declarative list Decls, status flag Flag, the location of the
4198 -- error and a string Msg, construct the following check:
4199 -- Msg : constant String :=
4200 -- (if Flag then
4201 -- Msg & "case guard at Error_Loc evaluates to True"
4202 -- else Msg);
4203 -- The resulting code is added to Decls
4205 procedure Consequence_Error
4206 (Checks : in out Node_Id;
4207 Flag : Entity_Id;
4208 Conseq : Node_Id);
4209 -- Given an if statement Checks, status flag Flag and a consequence
4210 -- Conseq, construct the following check:
4211 -- [els]if Flag and then not Conseq then
4212 -- raise Assertion_Error
4213 -- with "failed contract case at Sloc (Conseq)";
4214 -- [end if;]
4215 -- The resulting code is added to Checks
4217 function Declaration_Of (Id : Entity_Id) return Node_Id;
4218 -- Given the entity Id of a boolean flag, generate:
4219 -- Id : Boolean := False;
4221 function Increment (Id : Entity_Id) return Node_Id;
4222 -- Given the entity Id of a numerical variable, generate:
4223 -- Id := Id + 1;
4225 function Set (Id : Entity_Id) return Node_Id;
4226 -- Given the entity Id of a boolean variable, generate:
4227 -- Id := True;
4229 ----------------------
4230 -- Case_Guard_Error --
4231 ----------------------
4233 procedure Case_Guard_Error
4234 (Decls : List_Id;
4235 Flag : Entity_Id;
4236 Error_Loc : Source_Ptr;
4237 Msg : in out Entity_Id)
4239 New_Line : constant Character := Character'Val (10);
4240 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
4242 begin
4243 Start_String;
4244 Store_String_Char (New_Line);
4245 Store_String_Chars (" case guard at ");
4246 Store_String_Chars (Build_Location_String (Error_Loc));
4247 Store_String_Chars (" evaluates to True");
4249 -- Generate:
4250 -- New_Msg : constant String :=
4251 -- (if Flag then
4252 -- Msg & "case guard at Error_Loc evaluates to True"
4253 -- else Msg);
4255 Append_To (Decls,
4256 Make_Object_Declaration (Loc,
4257 Defining_Identifier => New_Msg,
4258 Constant_Present => True,
4259 Object_Definition => New_Reference_To (Standard_String, Loc),
4260 Expression =>
4261 Make_If_Expression (Loc,
4262 Expressions => New_List (
4263 New_Reference_To (Flag, Loc),
4265 Make_Op_Concat (Loc,
4266 Left_Opnd => New_Reference_To (Msg, Loc),
4267 Right_Opnd => Make_String_Literal (Loc, End_String)),
4269 New_Reference_To (Msg, Loc)))));
4271 Msg := New_Msg;
4272 end Case_Guard_Error;
4274 -----------------------
4275 -- Consequence_Error --
4276 -----------------------
4278 procedure Consequence_Error
4279 (Checks : in out Node_Id;
4280 Flag : Entity_Id;
4281 Conseq : Node_Id)
4283 Cond : Node_Id;
4284 Error : Node_Id;
4286 begin
4287 -- Generate:
4288 -- Flag and then not Conseq
4290 Cond :=
4291 Make_And_Then (Loc,
4292 Left_Opnd => New_Reference_To (Flag, Loc),
4293 Right_Opnd =>
4294 Make_Op_Not (Loc,
4295 Right_Opnd => Relocate_Node (Conseq)));
4297 -- Generate:
4298 -- raise Assertion_Error
4299 -- with "failed contract case at Sloc (Conseq)";
4301 Start_String;
4302 Store_String_Chars ("failed contract case at ");
4303 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
4305 Error :=
4306 Make_Procedure_Call_Statement (Loc,
4307 Name =>
4308 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
4309 Parameter_Associations => New_List (
4310 Make_String_Literal (Loc, End_String)));
4312 if No (Checks) then
4313 Checks :=
4314 Make_If_Statement (Loc,
4315 Condition => Cond,
4316 Then_Statements => New_List (Error));
4318 else
4319 if No (Elsif_Parts (Checks)) then
4320 Set_Elsif_Parts (Checks, New_List);
4321 end if;
4323 Append_To (Elsif_Parts (Checks),
4324 Make_Elsif_Part (Loc,
4325 Condition => Cond,
4326 Then_Statements => New_List (Error)));
4327 end if;
4328 end Consequence_Error;
4330 --------------------
4331 -- Declaration_Of --
4332 --------------------
4334 function Declaration_Of (Id : Entity_Id) return Node_Id is
4335 begin
4336 return
4337 Make_Object_Declaration (Loc,
4338 Defining_Identifier => Id,
4339 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
4340 Expression => New_Reference_To (Standard_False, Loc));
4341 end Declaration_Of;
4343 ---------------
4344 -- Increment --
4345 ---------------
4347 function Increment (Id : Entity_Id) return Node_Id is
4348 begin
4349 return
4350 Make_Assignment_Statement (Loc,
4351 Name => New_Reference_To (Id, Loc),
4352 Expression =>
4353 Make_Op_Add (Loc,
4354 Left_Opnd => New_Reference_To (Id, Loc),
4355 Right_Opnd => Make_Integer_Literal (Loc, 1)));
4356 end Increment;
4358 ---------
4359 -- Set --
4360 ---------
4362 function Set (Id : Entity_Id) return Node_Id is
4363 begin
4364 return
4365 Make_Assignment_Statement (Loc,
4366 Name => New_Reference_To (Id, Loc),
4367 Expression => New_Reference_To (Standard_True, Loc));
4368 end Set;
4370 -- Local variables
4372 Aggr : constant Node_Id :=
4373 Expression (First
4374 (Pragma_Argument_Associations (CCs)));
4375 Case_Guard : Node_Id;
4376 CG_Checks : Node_Id;
4377 CG_Stmts : List_Id;
4378 Conseq : Node_Id;
4379 Conseq_Checks : Node_Id := Empty;
4380 Count : Entity_Id;
4381 Error_Decls : List_Id;
4382 Flag : Entity_Id;
4383 Msg_Str : Entity_Id;
4384 Multiple_PCs : Boolean;
4385 Others_Flag : Entity_Id := Empty;
4386 Post_Case : Node_Id;
4388 -- Start of processing for Expand_Contract_Cases
4390 begin
4391 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
4392 -- already been rewritten as a Null statement.
4394 if Is_Ignored (CCs) then
4395 return;
4397 -- Guard against malformed contract cases
4399 elsif Nkind (Aggr) /= N_Aggregate then
4400 return;
4401 end if;
4403 Multiple_PCs := List_Length (Component_Associations (Aggr)) > 1;
4405 -- Create the counter which tracks the number of case guards that
4406 -- evaluate to True.
4408 -- Count : Natural := 0;
4410 Count := Make_Temporary (Loc, 'C');
4412 Prepend_To (Decls,
4413 Make_Object_Declaration (Loc,
4414 Defining_Identifier => Count,
4415 Object_Definition => New_Reference_To (Standard_Natural, Loc),
4416 Expression => Make_Integer_Literal (Loc, 0)));
4418 -- Create the base error message for multiple overlapping case guards
4420 -- Msg_Str : constant String :=
4421 -- "contract cases overlap for subprogram Subp_Id";
4423 if Multiple_PCs then
4424 Msg_Str := Make_Temporary (Loc, 'S');
4426 Start_String;
4427 Store_String_Chars ("contract cases overlap for subprogram ");
4428 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
4430 Error_Decls := New_List (
4431 Make_Object_Declaration (Loc,
4432 Defining_Identifier => Msg_Str,
4433 Constant_Present => True,
4434 Object_Definition => New_Reference_To (Standard_String, Loc),
4435 Expression => Make_String_Literal (Loc, End_String)));
4436 end if;
4438 -- Process individual post cases
4440 Post_Case := First (Component_Associations (Aggr));
4441 while Present (Post_Case) loop
4442 Case_Guard := First (Choices (Post_Case));
4443 Conseq := Expression (Post_Case);
4445 -- The "others" choice requires special processing
4447 if Nkind (Case_Guard) = N_Others_Choice then
4448 Others_Flag := Make_Temporary (Loc, 'F');
4449 Prepend_To (Decls, Declaration_Of (Others_Flag));
4451 -- Check possible overlap between a case guard and "others"
4453 if Multiple_PCs and Exception_Extra_Info then
4454 Case_Guard_Error
4455 (Decls => Error_Decls,
4456 Flag => Others_Flag,
4457 Error_Loc => Sloc (Case_Guard),
4458 Msg => Msg_Str);
4459 end if;
4461 -- Check the corresponding consequence of "others"
4463 Consequence_Error
4464 (Checks => Conseq_Checks,
4465 Flag => Others_Flag,
4466 Conseq => Conseq);
4468 -- Regular post case
4470 else
4471 -- Create the flag which tracks the state of its associated case
4472 -- guard.
4474 Flag := Make_Temporary (Loc, 'F');
4475 Prepend_To (Decls, Declaration_Of (Flag));
4477 -- The flag is set when the case guard is evaluated to True
4478 -- if Case_Guard then
4479 -- Flag := True;
4480 -- Count := Count + 1;
4481 -- end if;
4483 Append_To (Decls,
4484 Make_If_Statement (Loc,
4485 Condition => Relocate_Node (Case_Guard),
4486 Then_Statements => New_List (
4487 Set (Flag),
4488 Increment (Count))));
4490 -- Check whether this case guard overlaps with another one
4492 if Multiple_PCs and Exception_Extra_Info then
4493 Case_Guard_Error
4494 (Decls => Error_Decls,
4495 Flag => Flag,
4496 Error_Loc => Sloc (Case_Guard),
4497 Msg => Msg_Str);
4498 end if;
4500 -- The corresponding consequence of the case guard which evaluated
4501 -- to True must hold on exit from the subprogram.
4503 Consequence_Error
4504 (Checks => Conseq_Checks,
4505 Flag => Flag,
4506 Conseq => Conseq);
4507 end if;
4509 Next (Post_Case);
4510 end loop;
4512 -- Raise Assertion_Error when none of the case guards evaluate to True.
4513 -- The only exception is when we have "others", in which case there is
4514 -- no error because "others" acts as a default True.
4516 -- Generate:
4517 -- Flag := True;
4519 if Present (Others_Flag) then
4520 CG_Stmts := New_List (Set (Others_Flag));
4522 -- Generate:
4523 -- raise Assertion_Error with "xxx contract cases incomplete";
4525 else
4526 Start_String;
4527 Store_String_Chars (Build_Location_String (Loc));
4528 Store_String_Chars (" contract cases incomplete");
4530 CG_Stmts := New_List (
4531 Make_Procedure_Call_Statement (Loc,
4532 Name =>
4533 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
4534 Parameter_Associations => New_List (
4535 Make_String_Literal (Loc, End_String))));
4536 end if;
4538 CG_Checks :=
4539 Make_If_Statement (Loc,
4540 Condition =>
4541 Make_Op_Eq (Loc,
4542 Left_Opnd => New_Reference_To (Count, Loc),
4543 Right_Opnd => Make_Integer_Literal (Loc, 0)),
4544 Then_Statements => CG_Stmts);
4546 -- Detect a possible failure due to several case guards evaluating to
4547 -- True.
4549 -- Generate:
4550 -- elsif Count > 0 then
4551 -- declare
4552 -- <Error_Decls>
4553 -- begin
4554 -- raise Assertion_Error with <Msg_Str>;
4555 -- end if;
4557 if Multiple_PCs then
4558 Set_Elsif_Parts (CG_Checks, New_List (
4559 Make_Elsif_Part (Loc,
4560 Condition =>
4561 Make_Op_Gt (Loc,
4562 Left_Opnd => New_Reference_To (Count, Loc),
4563 Right_Opnd => Make_Integer_Literal (Loc, 1)),
4565 Then_Statements => New_List (
4566 Make_Block_Statement (Loc,
4567 Declarations => Error_Decls,
4568 Handled_Statement_Sequence =>
4569 Make_Handled_Sequence_Of_Statements (Loc,
4570 Statements => New_List (
4571 Make_Procedure_Call_Statement (Loc,
4572 Name =>
4573 New_Reference_To
4574 (RTE (RE_Raise_Assert_Failure), Loc),
4575 Parameter_Associations => New_List (
4576 New_Reference_To (Msg_Str, Loc))))))))));
4577 end if;
4579 Append_To (Decls, CG_Checks);
4581 -- Raise Assertion_Error when the corresponding consequence of a case
4582 -- guard that evaluated to True fails.
4584 if No (Stmts) then
4585 Stmts := New_List;
4586 end if;
4588 Append_To (Stmts, Conseq_Checks);
4589 end Expand_Contract_Cases;
4591 -------------------------------
4592 -- Expand_Ctrl_Function_Call --
4593 -------------------------------
4595 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4596 begin
4597 -- Optimization, if the returned value (which is on the sec-stack) is
4598 -- returned again, no need to copy/readjust/finalize, we can just pass
4599 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4600 -- attachment is needed
4602 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4603 return;
4604 end if;
4606 -- Resolution is now finished, make sure we don't start analysis again
4607 -- because of the duplication.
4609 Set_Analyzed (N);
4611 -- A function which returns a controlled object uses the secondary
4612 -- stack. Rewrite the call into a temporary which obtains the result of
4613 -- the function using 'reference.
4615 Remove_Side_Effects (N);
4617 -- When the temporary function result appears inside a case or an if
4618 -- expression, its lifetime must be extended to match that of the
4619 -- context. If not, the function result would be finalized prematurely
4620 -- and the evaluation of the expression could yield the wrong result.
4622 if Within_Case_Or_If_Expression (N)
4623 and then Nkind (N) = N_Explicit_Dereference
4624 then
4625 Set_Is_Processed_Transient (Entity (Prefix (N)));
4626 end if;
4627 end Expand_Ctrl_Function_Call;
4629 -------------------------
4630 -- Expand_Inlined_Call --
4631 -------------------------
4633 procedure Expand_Inlined_Call
4634 (N : Node_Id;
4635 Subp : Entity_Id;
4636 Orig_Subp : Entity_Id)
4638 Loc : constant Source_Ptr := Sloc (N);
4639 Is_Predef : constant Boolean :=
4640 Is_Predefined_File_Name
4641 (Unit_File_Name (Get_Source_Unit (Subp)));
4642 Orig_Bod : constant Node_Id :=
4643 Body_To_Inline (Unit_Declaration_Node (Subp));
4645 Blk : Node_Id;
4646 Decl : Node_Id;
4647 Decls : constant List_Id := New_List;
4648 Exit_Lab : Entity_Id := Empty;
4649 F : Entity_Id;
4650 A : Node_Id;
4651 Lab_Decl : Node_Id;
4652 Lab_Id : Node_Id;
4653 New_A : Node_Id;
4654 Num_Ret : Int := 0;
4655 Ret_Type : Entity_Id;
4657 Targ : Node_Id;
4658 -- The target of the call. If context is an assignment statement then
4659 -- this is the left-hand side of the assignment, else it is a temporary
4660 -- to which the return value is assigned prior to rewriting the call.
4662 Targ1 : Node_Id;
4663 -- A separate target used when the return type is unconstrained
4665 Temp : Entity_Id;
4666 Temp_Typ : Entity_Id;
4668 Return_Object : Entity_Id := Empty;
4669 -- Entity in declaration in an extended_return_statement
4671 Is_Unc : Boolean;
4672 Is_Unc_Decl : Boolean;
4673 -- If the type returned by the function is unconstrained and the call
4674 -- can be inlined, special processing is required.
4676 procedure Make_Exit_Label;
4677 -- Build declaration for exit label to be used in Return statements,
4678 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4679 -- declaration). Does nothing if Exit_Lab already set.
4681 function Process_Formals (N : Node_Id) return Traverse_Result;
4682 -- Replace occurrence of a formal with the corresponding actual, or the
4683 -- thunk generated for it. Replace a return statement with an assignment
4684 -- to the target of the call, with appropriate conversions if needed.
4686 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
4687 -- If the call being expanded is that of an internal subprogram, set the
4688 -- sloc of the generated block to that of the call itself, so that the
4689 -- expansion is skipped by the "next" command in gdb.
4690 -- Same processing for a subprogram in a predefined file, e.g.
4691 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4692 -- simplify our own development.
4694 procedure Reset_Dispatching_Calls (N : Node_Id);
4695 -- In subtree N search for occurrences of dispatching calls that use the
4696 -- Ada 2005 Object.Operation notation and the object is a formal of the
4697 -- inlined subprogram. Reset the entity associated with Operation in all
4698 -- the found occurrences.
4700 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
4701 -- If the function body is a single expression, replace call with
4702 -- expression, else insert block appropriately.
4704 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
4705 -- If procedure body has no local variables, inline body without
4706 -- creating block, otherwise rewrite call with block.
4708 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
4709 -- Determine whether a formal parameter is used only once in Orig_Bod
4711 ---------------------
4712 -- Make_Exit_Label --
4713 ---------------------
4715 procedure Make_Exit_Label is
4716 Lab_Ent : Entity_Id;
4717 begin
4718 if No (Exit_Lab) then
4719 Lab_Ent := Make_Temporary (Loc, 'L');
4720 Lab_Id := New_Reference_To (Lab_Ent, Loc);
4721 Exit_Lab := Make_Label (Loc, Lab_Id);
4722 Lab_Decl :=
4723 Make_Implicit_Label_Declaration (Loc,
4724 Defining_Identifier => Lab_Ent,
4725 Label_Construct => Exit_Lab);
4726 end if;
4727 end Make_Exit_Label;
4729 ---------------------
4730 -- Process_Formals --
4731 ---------------------
4733 function Process_Formals (N : Node_Id) return Traverse_Result is
4734 A : Entity_Id;
4735 E : Entity_Id;
4736 Ret : Node_Id;
4738 begin
4739 if Is_Entity_Name (N) and then Present (Entity (N)) then
4740 E := Entity (N);
4742 if Is_Formal (E) and then Scope (E) = Subp then
4743 A := Renamed_Object (E);
4745 -- Rewrite the occurrence of the formal into an occurrence of
4746 -- the actual. Also establish visibility on the proper view of
4747 -- the actual's subtype for the body's context (if the actual's
4748 -- subtype is private at the call point but its full view is
4749 -- visible to the body, then the inlined tree here must be
4750 -- analyzed with the full view).
4752 if Is_Entity_Name (A) then
4753 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
4754 Check_Private_View (N);
4756 elsif Nkind (A) = N_Defining_Identifier then
4757 Rewrite (N, New_Occurrence_Of (A, Loc));
4758 Check_Private_View (N);
4760 -- Numeric literal
4762 else
4763 Rewrite (N, New_Copy (A));
4764 end if;
4765 end if;
4767 return Skip;
4769 elsif Is_Entity_Name (N)
4770 and then Present (Return_Object)
4771 and then Chars (N) = Chars (Return_Object)
4772 then
4773 -- Occurrence within an extended return statement. The return
4774 -- object is local to the body been inlined, and thus the generic
4775 -- copy is not analyzed yet, so we match by name, and replace it
4776 -- with target of call.
4778 if Nkind (Targ) = N_Defining_Identifier then
4779 Rewrite (N, New_Occurrence_Of (Targ, Loc));
4780 else
4781 Rewrite (N, New_Copy_Tree (Targ));
4782 end if;
4784 return Skip;
4786 elsif Nkind (N) = N_Simple_Return_Statement then
4787 if No (Expression (N)) then
4788 Make_Exit_Label;
4789 Rewrite (N,
4790 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4792 else
4793 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
4794 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
4795 then
4796 -- Function body is a single expression. No need for
4797 -- exit label.
4799 null;
4801 else
4802 Num_Ret := Num_Ret + 1;
4803 Make_Exit_Label;
4804 end if;
4806 -- Because of the presence of private types, the views of the
4807 -- expression and the context may be different, so place an
4808 -- unchecked conversion to the context type to avoid spurious
4809 -- errors, e.g. when the expression is a numeric literal and
4810 -- the context is private. If the expression is an aggregate,
4811 -- use a qualified expression, because an aggregate is not a
4812 -- legal argument of a conversion. Ditto for numeric literals,
4813 -- which must be resolved to a specific type.
4815 if Nkind_In (Expression (N), N_Aggregate,
4816 N_Null,
4817 N_Real_Literal,
4818 N_Integer_Literal)
4819 then
4820 Ret :=
4821 Make_Qualified_Expression (Sloc (N),
4822 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
4823 Expression => Relocate_Node (Expression (N)));
4824 else
4825 Ret :=
4826 Unchecked_Convert_To
4827 (Ret_Type, Relocate_Node (Expression (N)));
4828 end if;
4830 if Nkind (Targ) = N_Defining_Identifier then
4831 Rewrite (N,
4832 Make_Assignment_Statement (Loc,
4833 Name => New_Occurrence_Of (Targ, Loc),
4834 Expression => Ret));
4835 else
4836 Rewrite (N,
4837 Make_Assignment_Statement (Loc,
4838 Name => New_Copy (Targ),
4839 Expression => Ret));
4840 end if;
4842 Set_Assignment_OK (Name (N));
4844 if Present (Exit_Lab) then
4845 Insert_After (N,
4846 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4847 end if;
4848 end if;
4850 return OK;
4852 -- An extended return becomes a block whose first statement is the
4853 -- assignment of the initial expression of the return object to the
4854 -- target of the call itself.
4856 elsif Nkind (N) = N_Extended_Return_Statement then
4857 declare
4858 Return_Decl : constant Entity_Id :=
4859 First (Return_Object_Declarations (N));
4860 Assign : Node_Id;
4862 begin
4863 Return_Object := Defining_Identifier (Return_Decl);
4865 if Present (Expression (Return_Decl)) then
4866 if Nkind (Targ) = N_Defining_Identifier then
4867 Assign :=
4868 Make_Assignment_Statement (Loc,
4869 Name => New_Occurrence_Of (Targ, Loc),
4870 Expression => Expression (Return_Decl));
4871 else
4872 Assign :=
4873 Make_Assignment_Statement (Loc,
4874 Name => New_Copy (Targ),
4875 Expression => Expression (Return_Decl));
4876 end if;
4878 Set_Assignment_OK (Name (Assign));
4880 if No (Handled_Statement_Sequence (N)) then
4881 Set_Handled_Statement_Sequence (N,
4882 Make_Handled_Sequence_Of_Statements (Loc,
4883 Statements => New_List));
4884 end if;
4886 Prepend (Assign,
4887 Statements (Handled_Statement_Sequence (N)));
4888 end if;
4890 Rewrite (N,
4891 Make_Block_Statement (Loc,
4892 Handled_Statement_Sequence =>
4893 Handled_Statement_Sequence (N)));
4895 return OK;
4896 end;
4898 -- Remove pragma Unreferenced since it may refer to formals that
4899 -- are not visible in the inlined body, and in any case we will
4900 -- not be posting warnings on the inlined body so it is unneeded.
4902 elsif Nkind (N) = N_Pragma
4903 and then Pragma_Name (N) = Name_Unreferenced
4904 then
4905 Rewrite (N, Make_Null_Statement (Sloc (N)));
4906 return OK;
4908 else
4909 return OK;
4910 end if;
4911 end Process_Formals;
4913 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
4915 ------------------
4916 -- Process_Sloc --
4917 ------------------
4919 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
4920 begin
4921 if not Debug_Generated_Code then
4922 Set_Sloc (Nod, Sloc (N));
4923 Set_Comes_From_Source (Nod, False);
4924 end if;
4926 return OK;
4927 end Process_Sloc;
4929 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
4931 ------------------------------
4932 -- Reset_Dispatching_Calls --
4933 ------------------------------
4935 procedure Reset_Dispatching_Calls (N : Node_Id) is
4937 function Do_Reset (N : Node_Id) return Traverse_Result;
4938 -- Comment required ???
4940 --------------
4941 -- Do_Reset --
4942 --------------
4944 function Do_Reset (N : Node_Id) return Traverse_Result is
4945 begin
4946 if Nkind (N) = N_Procedure_Call_Statement
4947 and then Nkind (Name (N)) = N_Selected_Component
4948 and then Nkind (Prefix (Name (N))) = N_Identifier
4949 and then Is_Formal (Entity (Prefix (Name (N))))
4950 and then Is_Dispatching_Operation
4951 (Entity (Selector_Name (Name (N))))
4952 then
4953 Set_Entity (Selector_Name (Name (N)), Empty);
4954 end if;
4956 return OK;
4957 end Do_Reset;
4959 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
4961 -- Local variables
4963 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
4964 pragma Unreferenced (Dummy);
4966 -- Start of processing for Reset_Dispatching_Calls
4968 begin
4969 null;
4970 end Reset_Dispatching_Calls;
4972 ---------------------------
4973 -- Rewrite_Function_Call --
4974 ---------------------------
4976 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
4977 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4978 Fst : constant Node_Id := First (Statements (HSS));
4980 begin
4981 -- Optimize simple case: function body is a single return statement,
4982 -- which has been expanded into an assignment.
4984 if Is_Empty_List (Declarations (Blk))
4985 and then Nkind (Fst) = N_Assignment_Statement
4986 and then No (Next (Fst))
4987 then
4988 -- The function call may have been rewritten as the temporary
4989 -- that holds the result of the call, in which case remove the
4990 -- now useless declaration.
4992 if Nkind (N) = N_Identifier
4993 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4994 then
4995 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
4996 end if;
4998 Rewrite (N, Expression (Fst));
5000 elsif Nkind (N) = N_Identifier
5001 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
5002 then
5003 -- The block assigns the result of the call to the temporary
5005 Insert_After (Parent (Entity (N)), Blk);
5007 -- If the context is an assignment, and the left-hand side is free of
5008 -- side-effects, the replacement is also safe.
5009 -- Can this be generalized further???
5011 elsif Nkind (Parent (N)) = N_Assignment_Statement
5012 and then
5013 (Is_Entity_Name (Name (Parent (N)))
5014 or else
5015 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
5016 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
5018 or else
5019 (Nkind (Name (Parent (N))) = N_Selected_Component
5020 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
5021 then
5022 -- Replace assignment with the block
5024 declare
5025 Original_Assignment : constant Node_Id := Parent (N);
5027 begin
5028 -- Preserve the original assignment node to keep the complete
5029 -- assignment subtree consistent enough for Analyze_Assignment
5030 -- to proceed (specifically, the original Lhs node must still
5031 -- have an assignment statement as its parent).
5033 -- We cannot rely on Original_Node to go back from the block
5034 -- node to the assignment node, because the assignment might
5035 -- already be a rewrite substitution.
5037 Discard_Node (Relocate_Node (Original_Assignment));
5038 Rewrite (Original_Assignment, Blk);
5039 end;
5041 elsif Nkind (Parent (N)) = N_Object_Declaration then
5043 -- A call to a function which returns an unconstrained type
5044 -- found in the expression initializing an object-declaration is
5045 -- expanded into a procedure call which must be added after the
5046 -- object declaration.
5048 if Is_Unc_Decl and then Debug_Flag_Dot_K then
5049 Insert_Action_After (Parent (N), Blk);
5050 else
5051 Set_Expression (Parent (N), Empty);
5052 Insert_After (Parent (N), Blk);
5053 end if;
5055 elsif Is_Unc and then not Debug_Flag_Dot_K then
5056 Insert_Before (Parent (N), Blk);
5057 end if;
5058 end Rewrite_Function_Call;
5060 ----------------------------
5061 -- Rewrite_Procedure_Call --
5062 ----------------------------
5064 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
5065 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
5067 begin
5068 -- If there is a transient scope for N, this will be the scope of the
5069 -- actions for N, and the statements in Blk need to be within this
5070 -- scope. For example, they need to have visibility on the constant
5071 -- declarations created for the formals.
5073 -- If N needs no transient scope, and if there are no declarations in
5074 -- the inlined body, we can do a little optimization and insert the
5075 -- statements for the body directly after N, and rewrite N to a
5076 -- null statement, instead of rewriting N into a full-blown block
5077 -- statement.
5079 if not Scope_Is_Transient
5080 and then Is_Empty_List (Declarations (Blk))
5081 then
5082 Insert_List_After (N, Statements (HSS));
5083 Rewrite (N, Make_Null_Statement (Loc));
5084 else
5085 Rewrite (N, Blk);
5086 end if;
5087 end Rewrite_Procedure_Call;
5089 -------------------------
5090 -- Formal_Is_Used_Once --
5091 -------------------------
5093 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
5094 Use_Counter : Int := 0;
5096 function Count_Uses (N : Node_Id) return Traverse_Result;
5097 -- Traverse the tree and count the uses of the formal parameter.
5098 -- In this case, for optimization purposes, we do not need to
5099 -- continue the traversal once more than one use is encountered.
5101 ----------------
5102 -- Count_Uses --
5103 ----------------
5105 function Count_Uses (N : Node_Id) return Traverse_Result is
5106 begin
5107 -- The original node is an identifier
5109 if Nkind (N) = N_Identifier
5110 and then Present (Entity (N))
5112 -- Original node's entity points to the one in the copied body
5114 and then Nkind (Entity (N)) = N_Identifier
5115 and then Present (Entity (Entity (N)))
5117 -- The entity of the copied node is the formal parameter
5119 and then Entity (Entity (N)) = Formal
5120 then
5121 Use_Counter := Use_Counter + 1;
5123 if Use_Counter > 1 then
5125 -- Denote more than one use and abandon the traversal
5127 Use_Counter := 2;
5128 return Abandon;
5130 end if;
5131 end if;
5133 return OK;
5134 end Count_Uses;
5136 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
5138 -- Start of processing for Formal_Is_Used_Once
5140 begin
5141 Count_Formal_Uses (Orig_Bod);
5142 return Use_Counter = 1;
5143 end Formal_Is_Used_Once;
5145 -- Start of processing for Expand_Inlined_Call
5147 begin
5148 -- Initializations for old/new semantics
5150 if not Debug_Flag_Dot_K then
5151 Is_Unc := Is_Array_Type (Etype (Subp))
5152 and then not Is_Constrained (Etype (Subp));
5153 Is_Unc_Decl := False;
5154 else
5155 Is_Unc := Returns_Unconstrained_Type (Subp)
5156 and then Optimization_Level > 0;
5157 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
5158 and then Is_Unc;
5159 end if;
5161 -- Check for an illegal attempt to inline a recursive procedure. If the
5162 -- subprogram has parameters this is detected when trying to supply a
5163 -- binding for parameters that already have one. For parameterless
5164 -- subprograms this must be done explicitly.
5166 if In_Open_Scopes (Subp) then
5167 Error_Msg_N ("call to recursive subprogram cannot be inlined??", N);
5168 Set_Is_Inlined (Subp, False);
5169 return;
5171 -- Skip inlining if this is not a true inlining since the attribute
5172 -- Body_To_Inline is also set for renamings (see sinfo.ads)
5174 elsif Nkind (Orig_Bod) in N_Entity then
5175 return;
5177 -- Skip inlining if the function returns an unconstrained type using
5178 -- an extended return statement since this part of the new inlining
5179 -- model which is not yet supported by the current implementation. ???
5181 elsif Is_Unc
5182 and then
5183 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod))))
5184 = N_Extended_Return_Statement
5185 and then not Debug_Flag_Dot_K
5186 then
5187 return;
5188 end if;
5190 if Nkind (Orig_Bod) = N_Defining_Identifier
5191 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
5192 then
5193 -- Subprogram is renaming_as_body. Calls occurring after the renaming
5194 -- can be replaced with calls to the renamed entity directly, because
5195 -- the subprograms are subtype conformant. If the renamed subprogram
5196 -- is an inherited operation, we must redo the expansion because
5197 -- implicit conversions may be needed. Similarly, if the renamed
5198 -- entity is inlined, expand the call for further optimizations.
5200 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
5202 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
5203 Expand_Call (N);
5204 end if;
5206 return;
5207 end if;
5209 -- Register the call in the list of inlined calls
5211 if Inlined_Calls = No_Elist then
5212 Inlined_Calls := New_Elmt_List;
5213 end if;
5215 Append_Elmt (N, To => Inlined_Calls);
5217 -- Use generic machinery to copy body of inlined subprogram, as if it
5218 -- were an instantiation, resetting source locations appropriately, so
5219 -- that nested inlined calls appear in the main unit.
5221 Save_Env (Subp, Empty);
5222 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
5224 -- Old semantics
5226 if not Debug_Flag_Dot_K then
5227 declare
5228 Bod : Node_Id;
5230 begin
5231 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
5232 Blk :=
5233 Make_Block_Statement (Loc,
5234 Declarations => Declarations (Bod),
5235 Handled_Statement_Sequence =>
5236 Handled_Statement_Sequence (Bod));
5238 if No (Declarations (Bod)) then
5239 Set_Declarations (Blk, New_List);
5240 end if;
5242 -- For the unconstrained case, capture the name of the local
5243 -- variable that holds the result. This must be the first
5244 -- declaration in the block, because its bounds cannot depend
5245 -- on local variables. Otherwise there is no way to declare the
5246 -- result outside of the block. Needless to say, in general the
5247 -- bounds will depend on the actuals in the call.
5249 -- If the context is an assignment statement, as is the case
5250 -- for the expansion of an extended return, the left-hand side
5251 -- provides bounds even if the return type is unconstrained.
5253 if Is_Unc then
5254 declare
5255 First_Decl : Node_Id;
5257 begin
5258 First_Decl := First (Declarations (Blk));
5260 if Nkind (First_Decl) /= N_Object_Declaration then
5261 return;
5262 end if;
5264 if Nkind (Parent (N)) /= N_Assignment_Statement then
5265 Targ1 := Defining_Identifier (First_Decl);
5266 else
5267 Targ1 := Name (Parent (N));
5268 end if;
5269 end;
5270 end if;
5271 end;
5273 -- New semantics
5275 else
5276 declare
5277 Bod : Node_Id;
5279 begin
5280 -- General case
5282 if not Is_Unc then
5283 Bod :=
5284 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
5285 Blk :=
5286 Make_Block_Statement (Loc,
5287 Declarations => Declarations (Bod),
5288 Handled_Statement_Sequence =>
5289 Handled_Statement_Sequence (Bod));
5291 -- Inline a call to a function that returns an unconstrained type.
5292 -- The semantic analyzer checked that frontend-inlined functions
5293 -- returning unconstrained types have no declarations and have
5294 -- a single extended return statement. As part of its processing
5295 -- the function was split in two subprograms: a procedure P and
5296 -- a function F that has a block with a call to procedure P (see
5297 -- Split_Unconstrained_Function).
5299 else
5300 pragma Assert
5301 (Nkind
5302 (First
5303 (Statements (Handled_Statement_Sequence (Orig_Bod))))
5304 = N_Block_Statement);
5306 declare
5307 Blk_Stmt : constant Node_Id :=
5308 First
5309 (Statements
5310 (Handled_Statement_Sequence (Orig_Bod)));
5311 First_Stmt : constant Node_Id :=
5312 First
5313 (Statements
5314 (Handled_Statement_Sequence (Blk_Stmt)));
5315 Second_Stmt : constant Node_Id := Next (First_Stmt);
5317 begin
5318 pragma Assert
5319 (Nkind (First_Stmt) = N_Procedure_Call_Statement
5320 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
5321 and then No (Next (Second_Stmt)));
5323 Bod :=
5324 Copy_Generic_Node
5325 (First
5326 (Statements (Handled_Statement_Sequence (Orig_Bod))),
5327 Empty, Instantiating => True);
5328 Blk := Bod;
5330 -- Capture the name of the local variable that holds the
5331 -- result. This must be the first declaration in the block,
5332 -- because its bounds cannot depend on local variables.
5333 -- Otherwise there is no way to declare the result outside
5334 -- of the block. Needless to say, in general the bounds will
5335 -- depend on the actuals in the call.
5337 if Nkind (Parent (N)) /= N_Assignment_Statement then
5338 Targ1 := Defining_Identifier (First (Declarations (Blk)));
5340 -- If the context is an assignment statement, as is the case
5341 -- for the expansion of an extended return, the left-hand
5342 -- side provides bounds even if the return type is
5343 -- unconstrained.
5345 else
5346 Targ1 := Name (Parent (N));
5347 end if;
5348 end;
5349 end if;
5351 if No (Declarations (Bod)) then
5352 Set_Declarations (Blk, New_List);
5353 end if;
5354 end;
5355 end if;
5357 -- If this is a derived function, establish the proper return type
5359 if Present (Orig_Subp) and then Orig_Subp /= Subp then
5360 Ret_Type := Etype (Orig_Subp);
5361 else
5362 Ret_Type := Etype (Subp);
5363 end if;
5365 -- Create temporaries for the actuals that are expressions, or that are
5366 -- scalars and require copying to preserve semantics.
5368 F := First_Formal (Subp);
5369 A := First_Actual (N);
5370 while Present (F) loop
5371 if Present (Renamed_Object (F)) then
5372 Error_Msg_N ("cannot inline call to recursive subprogram", N);
5373 return;
5374 end if;
5376 -- Reset Last_Assignment for any parameters of mode out or in out, to
5377 -- prevent spurious warnings about overwriting for assignments to the
5378 -- formal in the inlined code.
5380 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
5381 Set_Last_Assignment (Entity (A), Empty);
5382 end if;
5384 -- If the argument may be a controlling argument in a call within
5385 -- the inlined body, we must preserve its classwide nature to insure
5386 -- that dynamic dispatching take place subsequently. If the formal
5387 -- has a constraint it must be preserved to retain the semantics of
5388 -- the body.
5390 if Is_Class_Wide_Type (Etype (F))
5391 or else (Is_Access_Type (Etype (F))
5392 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
5393 then
5394 Temp_Typ := Etype (F);
5396 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
5397 and then Etype (F) /= Base_Type (Etype (F))
5398 then
5399 Temp_Typ := Etype (F);
5400 else
5401 Temp_Typ := Etype (A);
5402 end if;
5404 -- If the actual is a simple name or a literal, no need to
5405 -- create a temporary, object can be used directly.
5407 -- If the actual is a literal and the formal has its address taken,
5408 -- we cannot pass the literal itself as an argument, so its value
5409 -- must be captured in a temporary.
5411 if (Is_Entity_Name (A)
5412 and then
5413 (not Is_Scalar_Type (Etype (A))
5414 or else Ekind (Entity (A)) = E_Enumeration_Literal))
5416 -- When the actual is an identifier and the corresponding formal is
5417 -- used only once in the original body, the formal can be substituted
5418 -- directly with the actual parameter.
5420 or else (Nkind (A) = N_Identifier
5421 and then Formal_Is_Used_Once (F))
5423 or else
5424 (Nkind_In (A, N_Real_Literal,
5425 N_Integer_Literal,
5426 N_Character_Literal)
5427 and then not Address_Taken (F))
5428 then
5429 if Etype (F) /= Etype (A) then
5430 Set_Renamed_Object
5431 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
5432 else
5433 Set_Renamed_Object (F, A);
5434 end if;
5436 else
5437 Temp := Make_Temporary (Loc, 'C');
5439 -- If the actual for an in/in-out parameter is a view conversion,
5440 -- make it into an unchecked conversion, given that an untagged
5441 -- type conversion is not a proper object for a renaming.
5443 -- In-out conversions that involve real conversions have already
5444 -- been transformed in Expand_Actuals.
5446 if Nkind (A) = N_Type_Conversion
5447 and then Ekind (F) /= E_In_Parameter
5448 then
5449 New_A :=
5450 Make_Unchecked_Type_Conversion (Loc,
5451 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
5452 Expression => Relocate_Node (Expression (A)));
5454 elsif Etype (F) /= Etype (A) then
5455 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
5456 Temp_Typ := Etype (F);
5458 else
5459 New_A := Relocate_Node (A);
5460 end if;
5462 Set_Sloc (New_A, Sloc (N));
5464 -- If the actual has a by-reference type, it cannot be copied,
5465 -- so its value is captured in a renaming declaration. Otherwise
5466 -- declare a local constant initialized with the actual.
5468 -- We also use a renaming declaration for expressions of an array
5469 -- type that is not bit-packed, both for efficiency reasons and to
5470 -- respect the semantics of the call: in most cases the original
5471 -- call will pass the parameter by reference, and thus the inlined
5472 -- code will have the same semantics.
5474 if Ekind (F) = E_In_Parameter
5475 and then not Is_By_Reference_Type (Etype (A))
5476 and then
5477 (not Is_Array_Type (Etype (A))
5478 or else not Is_Object_Reference (A)
5479 or else Is_Bit_Packed_Array (Etype (A)))
5480 then
5481 Decl :=
5482 Make_Object_Declaration (Loc,
5483 Defining_Identifier => Temp,
5484 Constant_Present => True,
5485 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
5486 Expression => New_A);
5487 else
5488 Decl :=
5489 Make_Object_Renaming_Declaration (Loc,
5490 Defining_Identifier => Temp,
5491 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
5492 Name => New_A);
5493 end if;
5495 Append (Decl, Decls);
5496 Set_Renamed_Object (F, Temp);
5497 end if;
5499 Next_Formal (F);
5500 Next_Actual (A);
5501 end loop;
5503 -- Establish target of function call. If context is not assignment or
5504 -- declaration, create a temporary as a target. The declaration for the
5505 -- temporary may be subsequently optimized away if the body is a single
5506 -- expression, or if the left-hand side of the assignment is simple
5507 -- enough, i.e. an entity or an explicit dereference of one.
5509 if Ekind (Subp) = E_Function then
5510 if Nkind (Parent (N)) = N_Assignment_Statement
5511 and then Is_Entity_Name (Name (Parent (N)))
5512 then
5513 Targ := Name (Parent (N));
5515 elsif Nkind (Parent (N)) = N_Assignment_Statement
5516 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5517 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5518 then
5519 Targ := Name (Parent (N));
5521 elsif Nkind (Parent (N)) = N_Assignment_Statement
5522 and then Nkind (Name (Parent (N))) = N_Selected_Component
5523 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5524 then
5525 Targ := New_Copy_Tree (Name (Parent (N)));
5527 elsif Nkind (Parent (N)) = N_Object_Declaration
5528 and then Is_Limited_Type (Etype (Subp))
5529 then
5530 Targ := Defining_Identifier (Parent (N));
5532 -- New semantics: In an object declaration avoid an extra copy
5533 -- of the result of a call to an inlined function that returns
5534 -- an unconstrained type
5536 elsif Debug_Flag_Dot_K
5537 and then Nkind (Parent (N)) = N_Object_Declaration
5538 and then Is_Unc
5539 then
5540 Targ := Defining_Identifier (Parent (N));
5542 else
5543 -- Replace call with temporary and create its declaration
5545 Temp := Make_Temporary (Loc, 'C');
5546 Set_Is_Internal (Temp);
5548 -- For the unconstrained case, the generated temporary has the
5549 -- same constrained declaration as the result variable. It may
5550 -- eventually be possible to remove that temporary and use the
5551 -- result variable directly.
5553 if Is_Unc
5554 and then Nkind (Parent (N)) /= N_Assignment_Statement
5555 then
5556 Decl :=
5557 Make_Object_Declaration (Loc,
5558 Defining_Identifier => Temp,
5559 Object_Definition =>
5560 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5562 Replace_Formals (Decl);
5564 else
5565 Decl :=
5566 Make_Object_Declaration (Loc,
5567 Defining_Identifier => Temp,
5568 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
5570 Set_Etype (Temp, Ret_Type);
5571 end if;
5573 Set_No_Initialization (Decl);
5574 Append (Decl, Decls);
5575 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5576 Targ := Temp;
5577 end if;
5578 end if;
5580 Insert_Actions (N, Decls);
5582 if Is_Unc_Decl then
5584 -- Special management for inlining a call to a function that returns
5585 -- an unconstrained type and initializes an object declaration: we
5586 -- avoid generating undesired extra calls and goto statements.
5588 -- Given:
5589 -- function Func (...) return ...
5590 -- begin
5591 -- declare
5592 -- Result : String (1 .. 4);
5593 -- begin
5594 -- Proc (Result, ...);
5595 -- return Result;
5596 -- end;
5597 -- end F;
5599 -- Result : String := Func (...);
5601 -- Replace this object declaration by:
5603 -- Result : String (1 .. 4);
5604 -- Proc (Result, ...);
5606 Remove_Homonym (Targ);
5608 Decl :=
5609 Make_Object_Declaration
5610 (Loc,
5611 Defining_Identifier => Targ,
5612 Object_Definition =>
5613 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5614 Replace_Formals (Decl);
5615 Rewrite (Parent (N), Decl);
5616 Analyze (Parent (N));
5618 -- Avoid spurious warnings since we know that this declaration is
5619 -- referenced by the procedure call.
5621 Set_Never_Set_In_Source (Targ, False);
5623 -- Remove the local declaration of the extended return stmt from the
5624 -- inlined code
5626 Remove (Parent (Targ1));
5628 -- Update the reference to the result (since we have rewriten the
5629 -- object declaration)
5631 declare
5632 Blk_Call_Stmt : Node_Id;
5634 begin
5635 -- Capture the call to the procedure
5637 Blk_Call_Stmt :=
5638 First (Statements (Handled_Statement_Sequence (Blk)));
5639 pragma Assert
5640 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
5642 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
5643 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
5644 New_Reference_To (Targ, Loc));
5645 end;
5647 -- Remove the return statement
5649 pragma Assert
5650 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5651 N_Simple_Return_Statement);
5653 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5654 end if;
5656 -- Traverse the tree and replace formals with actuals or their thunks.
5657 -- Attach block to tree before analysis and rewriting.
5659 Replace_Formals (Blk);
5660 Set_Parent (Blk, N);
5662 if not Comes_From_Source (Subp) or else Is_Predef then
5663 Reset_Slocs (Blk);
5664 end if;
5666 if Is_Unc_Decl then
5668 -- No action needed since return statement has been already removed!
5670 null;
5672 elsif Present (Exit_Lab) then
5674 -- If the body was a single expression, the single return statement
5675 -- and the corresponding label are useless.
5677 if Num_Ret = 1
5678 and then
5679 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5680 N_Goto_Statement
5681 then
5682 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5683 else
5684 Append (Lab_Decl, (Declarations (Blk)));
5685 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
5686 end if;
5687 end if;
5689 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5690 -- on conflicting private views that Gigi would ignore. If this is a
5691 -- predefined unit, analyze with checks off, as is done in the non-
5692 -- inlined run-time units.
5694 declare
5695 I_Flag : constant Boolean := In_Inlined_Body;
5697 begin
5698 In_Inlined_Body := True;
5700 if Is_Predef then
5701 declare
5702 Style : constant Boolean := Style_Check;
5704 begin
5705 Style_Check := False;
5707 -- Search for dispatching calls that use the Object.Operation
5708 -- notation using an Object that is a parameter of the inlined
5709 -- function. We reset the decoration of Operation to force
5710 -- the reanalysis of the inlined dispatching call because
5711 -- the actual object has been inlined.
5713 Reset_Dispatching_Calls (Blk);
5715 Analyze (Blk, Suppress => All_Checks);
5716 Style_Check := Style;
5717 end;
5719 else
5720 Analyze (Blk);
5721 end if;
5723 In_Inlined_Body := I_Flag;
5724 end;
5726 if Ekind (Subp) = E_Procedure then
5727 Rewrite_Procedure_Call (N, Blk);
5729 else
5730 Rewrite_Function_Call (N, Blk);
5732 if Is_Unc_Decl then
5733 null;
5735 -- For the unconstrained case, the replacement of the call has been
5736 -- made prior to the complete analysis of the generated declarations.
5737 -- Propagate the proper type now.
5739 elsif Is_Unc then
5740 if Nkind (N) = N_Identifier then
5741 Set_Etype (N, Etype (Entity (N)));
5742 else
5743 Set_Etype (N, Etype (Targ1));
5744 end if;
5745 end if;
5746 end if;
5748 Restore_Env;
5750 -- Cleanup mapping between formals and actuals for other expansions
5752 F := First_Formal (Subp);
5753 while Present (F) loop
5754 Set_Renamed_Object (F, Empty);
5755 Next_Formal (F);
5756 end loop;
5757 end Expand_Inlined_Call;
5759 ----------------------------------------
5760 -- Expand_N_Extended_Return_Statement --
5761 ----------------------------------------
5763 -- If there is a Handled_Statement_Sequence, we rewrite this:
5765 -- return Result : T := <expression> do
5766 -- <handled_seq_of_stms>
5767 -- end return;
5769 -- to be:
5771 -- declare
5772 -- Result : T := <expression>;
5773 -- begin
5774 -- <handled_seq_of_stms>
5775 -- return Result;
5776 -- end;
5778 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5780 -- return Result : T := <expression>;
5782 -- to be:
5784 -- return <expression>;
5786 -- unless it's build-in-place or there's no <expression>, in which case
5787 -- we generate:
5789 -- declare
5790 -- Result : T := <expression>;
5791 -- begin
5792 -- return Result;
5793 -- end;
5795 -- Note that this case could have been written by the user as an extended
5796 -- return statement, or could have been transformed to this from a simple
5797 -- return statement.
5799 -- That is, we need to have a reified return object if there are statements
5800 -- (which might refer to it) or if we're doing build-in-place (so we can
5801 -- set its address to the final resting place or if there is no expression
5802 -- (in which case default initial values might need to be set).
5804 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5805 Loc : constant Source_Ptr := Sloc (N);
5807 Par_Func : constant Entity_Id :=
5808 Return_Applies_To (Return_Statement_Entity (N));
5809 Result_Subt : constant Entity_Id := Etype (Par_Func);
5810 Ret_Obj_Id : constant Entity_Id :=
5811 First_Entity (Return_Statement_Entity (N));
5812 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5814 Is_Build_In_Place : constant Boolean :=
5815 Is_Build_In_Place_Function (Par_Func);
5817 Exp : Node_Id;
5818 HSS : Node_Id;
5819 Result : Node_Id;
5820 Return_Stmt : Node_Id;
5821 Stmts : List_Id;
5823 function Build_Heap_Allocator
5824 (Temp_Id : Entity_Id;
5825 Temp_Typ : Entity_Id;
5826 Func_Id : Entity_Id;
5827 Ret_Typ : Entity_Id;
5828 Alloc_Expr : Node_Id) return Node_Id;
5829 -- Create the statements necessary to allocate a return object on the
5830 -- caller's master. The master is available through implicit parameter
5831 -- BIPfinalizationmaster.
5833 -- if BIPfinalizationmaster /= null then
5834 -- declare
5835 -- type Ptr_Typ is access Ret_Typ;
5836 -- for Ptr_Typ'Storage_Pool use
5837 -- Base_Pool (BIPfinalizationmaster.all).all;
5838 -- Local : Ptr_Typ;
5840 -- begin
5841 -- procedure Allocate (...) is
5842 -- begin
5843 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5844 -- end Allocate;
5846 -- Local := <Alloc_Expr>;
5847 -- Temp_Id := Temp_Typ (Local);
5848 -- end;
5849 -- end if;
5851 -- Temp_Id is the temporary which is used to reference the internally
5852 -- created object in all allocation forms. Temp_Typ is the type of the
5853 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5854 -- type of Func_Id. Alloc_Expr is the actual allocator.
5856 function Move_Activation_Chain return Node_Id;
5857 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5858 -- with parameters:
5859 -- From current activation chain
5860 -- To activation chain passed in by the caller
5861 -- New_Master master passed in by the caller
5863 --------------------------
5864 -- Build_Heap_Allocator --
5865 --------------------------
5867 function Build_Heap_Allocator
5868 (Temp_Id : Entity_Id;
5869 Temp_Typ : Entity_Id;
5870 Func_Id : Entity_Id;
5871 Ret_Typ : Entity_Id;
5872 Alloc_Expr : Node_Id) return Node_Id
5874 begin
5875 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5877 -- Processing for build-in-place object allocation. This is disabled
5878 -- on .NET/JVM because the targets do not support pools.
5880 if VM_Target = No_VM
5881 and then Needs_Finalization (Ret_Typ)
5882 then
5883 declare
5884 Decls : constant List_Id := New_List;
5885 Fin_Mas_Id : constant Entity_Id :=
5886 Build_In_Place_Formal
5887 (Func_Id, BIP_Finalization_Master);
5888 Stmts : constant List_Id := New_List;
5889 Desig_Typ : Entity_Id;
5890 Local_Id : Entity_Id;
5891 Pool_Id : Entity_Id;
5892 Ptr_Typ : Entity_Id;
5894 begin
5895 -- Generate:
5896 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5898 Pool_Id := Make_Temporary (Loc, 'P');
5900 Append_To (Decls,
5901 Make_Object_Renaming_Declaration (Loc,
5902 Defining_Identifier => Pool_Id,
5903 Subtype_Mark =>
5904 New_Reference_To (RTE (RE_Root_Storage_Pool), Loc),
5905 Name =>
5906 Make_Explicit_Dereference (Loc,
5907 Prefix =>
5908 Make_Function_Call (Loc,
5909 Name =>
5910 New_Reference_To (RTE (RE_Base_Pool), Loc),
5911 Parameter_Associations => New_List (
5912 Make_Explicit_Dereference (Loc,
5913 Prefix =>
5914 New_Reference_To (Fin_Mas_Id, Loc)))))));
5916 -- Create an access type which uses the storage pool of the
5917 -- caller's master. This additional type is necessary because
5918 -- the finalization master cannot be associated with the type
5919 -- of the temporary. Otherwise the secondary stack allocation
5920 -- will fail.
5922 Desig_Typ := Ret_Typ;
5924 -- Ensure that the build-in-place machinery uses a fat pointer
5925 -- when allocating an unconstrained array on the heap. In this
5926 -- case the result object type is a constrained array type even
5927 -- though the function type is unconstrained.
5929 if Ekind (Desig_Typ) = E_Array_Subtype then
5930 Desig_Typ := Base_Type (Desig_Typ);
5931 end if;
5933 -- Generate:
5934 -- type Ptr_Typ is access Desig_Typ;
5936 Ptr_Typ := Make_Temporary (Loc, 'P');
5938 Append_To (Decls,
5939 Make_Full_Type_Declaration (Loc,
5940 Defining_Identifier => Ptr_Typ,
5941 Type_Definition =>
5942 Make_Access_To_Object_Definition (Loc,
5943 Subtype_Indication =>
5944 New_Reference_To (Desig_Typ, Loc))));
5946 -- Perform minor decoration in order to set the master and the
5947 -- storage pool attributes.
5949 Set_Ekind (Ptr_Typ, E_Access_Type);
5950 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5951 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5953 -- Create the temporary, generate:
5954 -- Local_Id : Ptr_Typ;
5956 Local_Id := Make_Temporary (Loc, 'T');
5958 Append_To (Decls,
5959 Make_Object_Declaration (Loc,
5960 Defining_Identifier => Local_Id,
5961 Object_Definition =>
5962 New_Reference_To (Ptr_Typ, Loc)));
5964 -- Allocate the object, generate:
5965 -- Local_Id := <Alloc_Expr>;
5967 Append_To (Stmts,
5968 Make_Assignment_Statement (Loc,
5969 Name => New_Reference_To (Local_Id, Loc),
5970 Expression => Alloc_Expr));
5972 -- Generate:
5973 -- Temp_Id := Temp_Typ (Local_Id);
5975 Append_To (Stmts,
5976 Make_Assignment_Statement (Loc,
5977 Name => New_Reference_To (Temp_Id, Loc),
5978 Expression =>
5979 Unchecked_Convert_To (Temp_Typ,
5980 New_Reference_To (Local_Id, Loc))));
5982 -- Wrap the allocation in a block. This is further conditioned
5983 -- by checking the caller finalization master at runtime. A
5984 -- null value indicates a non-existent master, most likely due
5985 -- to a Finalize_Storage_Only allocation.
5987 -- Generate:
5988 -- if BIPfinalizationmaster /= null then
5989 -- declare
5990 -- <Decls>
5991 -- begin
5992 -- <Stmts>
5993 -- end;
5994 -- end if;
5996 return
5997 Make_If_Statement (Loc,
5998 Condition =>
5999 Make_Op_Ne (Loc,
6000 Left_Opnd => New_Reference_To (Fin_Mas_Id, Loc),
6001 Right_Opnd => Make_Null (Loc)),
6003 Then_Statements => New_List (
6004 Make_Block_Statement (Loc,
6005 Declarations => Decls,
6006 Handled_Statement_Sequence =>
6007 Make_Handled_Sequence_Of_Statements (Loc,
6008 Statements => Stmts))));
6009 end;
6011 -- For all other cases, generate:
6012 -- Temp_Id := <Alloc_Expr>;
6014 else
6015 return
6016 Make_Assignment_Statement (Loc,
6017 Name => New_Reference_To (Temp_Id, Loc),
6018 Expression => Alloc_Expr);
6019 end if;
6020 end Build_Heap_Allocator;
6022 ---------------------------
6023 -- Move_Activation_Chain --
6024 ---------------------------
6026 function Move_Activation_Chain return Node_Id is
6027 begin
6028 return
6029 Make_Procedure_Call_Statement (Loc,
6030 Name =>
6031 New_Reference_To (RTE (RE_Move_Activation_Chain), Loc),
6033 Parameter_Associations => New_List (
6035 -- Source chain
6037 Make_Attribute_Reference (Loc,
6038 Prefix => Make_Identifier (Loc, Name_uChain),
6039 Attribute_Name => Name_Unrestricted_Access),
6041 -- Destination chain
6043 New_Reference_To
6044 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
6046 -- New master
6048 New_Reference_To
6049 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
6050 end Move_Activation_Chain;
6052 -- Start of processing for Expand_N_Extended_Return_Statement
6054 begin
6055 -- Given that functionality of interface thunks is simple (just displace
6056 -- the pointer to the object) they are always handled by means of
6057 -- simple return statements.
6059 pragma Assert (not Is_Thunk (Current_Scope));
6061 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
6062 Exp := Expression (Ret_Obj_Decl);
6063 else
6064 Exp := Empty;
6065 end if;
6067 HSS := Handled_Statement_Sequence (N);
6069 -- If the returned object needs finalization actions, the function must
6070 -- perform the appropriate cleanup should it fail to return. The state
6071 -- of the function itself is tracked through a flag which is coupled
6072 -- with the scope finalizer. There is one flag per each return object
6073 -- in case of multiple returns.
6075 if Is_Build_In_Place
6076 and then Needs_Finalization (Etype (Ret_Obj_Id))
6077 then
6078 declare
6079 Flag_Decl : Node_Id;
6080 Flag_Id : Entity_Id;
6081 Func_Bod : Node_Id;
6083 begin
6084 -- Recover the function body
6086 Func_Bod := Unit_Declaration_Node (Par_Func);
6088 if Nkind (Func_Bod) = N_Subprogram_Declaration then
6089 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
6090 end if;
6092 -- Create a flag to track the function state
6094 Flag_Id := Make_Temporary (Loc, 'F');
6095 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
6097 -- Insert the flag at the beginning of the function declarations,
6098 -- generate:
6099 -- Fnn : Boolean := False;
6101 Flag_Decl :=
6102 Make_Object_Declaration (Loc,
6103 Defining_Identifier => Flag_Id,
6104 Object_Definition =>
6105 New_Reference_To (Standard_Boolean, Loc),
6106 Expression => New_Reference_To (Standard_False, Loc));
6108 Prepend_To (Declarations (Func_Bod), Flag_Decl);
6109 Analyze (Flag_Decl);
6110 end;
6111 end if;
6113 -- Build a simple_return_statement that returns the return object when
6114 -- there is a statement sequence, or no expression, or the result will
6115 -- be built in place. Note however that we currently do this for all
6116 -- composite cases, even though nonlimited composite results are not yet
6117 -- built in place (though we plan to do so eventually).
6119 if Present (HSS)
6120 or else Is_Composite_Type (Result_Subt)
6121 or else No (Exp)
6122 then
6123 if No (HSS) then
6124 Stmts := New_List;
6126 -- If the extended return has a handled statement sequence, then wrap
6127 -- it in a block and use the block as the first statement.
6129 else
6130 Stmts := New_List (
6131 Make_Block_Statement (Loc,
6132 Declarations => New_List,
6133 Handled_Statement_Sequence => HSS));
6134 end if;
6136 -- If the result type contains tasks, we call Move_Activation_Chain.
6137 -- Later, the cleanup code will call Complete_Master, which will
6138 -- terminate any unactivated tasks belonging to the return statement
6139 -- master. But Move_Activation_Chain updates their master to be that
6140 -- of the caller, so they will not be terminated unless the return
6141 -- statement completes unsuccessfully due to exception, abort, goto,
6142 -- or exit. As a formality, we test whether the function requires the
6143 -- result to be built in place, though that's necessarily true for
6144 -- the case of result types with task parts.
6146 if Is_Build_In_Place
6147 and then Has_Task (Result_Subt)
6148 then
6149 -- The return expression is an aggregate for a complex type which
6150 -- contains tasks. This particular case is left unexpanded since
6151 -- the regular expansion would insert all temporaries and
6152 -- initialization code in the wrong block.
6154 if Nkind (Exp) = N_Aggregate then
6155 Expand_N_Aggregate (Exp);
6156 end if;
6158 -- Do not move the activation chain if the return object does not
6159 -- contain tasks.
6161 if Has_Task (Etype (Ret_Obj_Id)) then
6162 Append_To (Stmts, Move_Activation_Chain);
6163 end if;
6164 end if;
6166 -- Update the state of the function right before the object is
6167 -- returned.
6169 if Is_Build_In_Place
6170 and then Needs_Finalization (Etype (Ret_Obj_Id))
6171 then
6172 declare
6173 Flag_Id : constant Entity_Id :=
6174 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
6176 begin
6177 -- Generate:
6178 -- Fnn := True;
6180 Append_To (Stmts,
6181 Make_Assignment_Statement (Loc,
6182 Name => New_Reference_To (Flag_Id, Loc),
6183 Expression => New_Reference_To (Standard_True, Loc)));
6184 end;
6185 end if;
6187 -- Build a simple_return_statement that returns the return object
6189 Return_Stmt :=
6190 Make_Simple_Return_Statement (Loc,
6191 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
6192 Append_To (Stmts, Return_Stmt);
6194 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
6195 end if;
6197 -- Case where we build a return statement block
6199 if Present (HSS) then
6200 Result :=
6201 Make_Block_Statement (Loc,
6202 Declarations => Return_Object_Declarations (N),
6203 Handled_Statement_Sequence => HSS);
6205 -- We set the entity of the new block statement to be that of the
6206 -- return statement. This is necessary so that various fields, such
6207 -- as Finalization_Chain_Entity carry over from the return statement
6208 -- to the block. Note that this block is unusual, in that its entity
6209 -- is an E_Return_Statement rather than an E_Block.
6211 Set_Identifier
6212 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
6214 -- If the object decl was already rewritten as a renaming, then we
6215 -- don't want to do the object allocation and transformation of of
6216 -- the return object declaration to a renaming. This case occurs
6217 -- when the return object is initialized by a call to another
6218 -- build-in-place function, and that function is responsible for
6219 -- the allocation of the return object.
6221 if Is_Build_In_Place
6222 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
6223 then
6224 pragma Assert
6225 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
6226 and then Is_Build_In_Place_Function_Call
6227 (Expression (Original_Node (Ret_Obj_Decl))));
6229 -- Return the build-in-place result by reference
6231 Set_By_Ref (Return_Stmt);
6233 elsif Is_Build_In_Place then
6235 -- Locate the implicit access parameter associated with the
6236 -- caller-supplied return object and convert the return
6237 -- statement's return object declaration to a renaming of a
6238 -- dereference of the access parameter. If the return object's
6239 -- declaration includes an expression that has not already been
6240 -- expanded as separate assignments, then add an assignment
6241 -- statement to ensure the return object gets initialized.
6243 -- declare
6244 -- Result : T [:= <expression>];
6245 -- begin
6246 -- ...
6248 -- is converted to
6250 -- declare
6251 -- Result : T renames FuncRA.all;
6252 -- [Result := <expression;]
6253 -- begin
6254 -- ...
6256 declare
6257 Return_Obj_Id : constant Entity_Id :=
6258 Defining_Identifier (Ret_Obj_Decl);
6259 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
6260 Return_Obj_Expr : constant Node_Id :=
6261 Expression (Ret_Obj_Decl);
6262 Constr_Result : constant Boolean :=
6263 Is_Constrained (Result_Subt);
6264 Obj_Alloc_Formal : Entity_Id;
6265 Object_Access : Entity_Id;
6266 Obj_Acc_Deref : Node_Id;
6267 Init_Assignment : Node_Id := Empty;
6269 begin
6270 -- Build-in-place results must be returned by reference
6272 Set_By_Ref (Return_Stmt);
6274 -- Retrieve the implicit access parameter passed by the caller
6276 Object_Access :=
6277 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
6279 -- If the return object's declaration includes an expression
6280 -- and the declaration isn't marked as No_Initialization, then
6281 -- we need to generate an assignment to the object and insert
6282 -- it after the declaration before rewriting it as a renaming
6283 -- (otherwise we'll lose the initialization). The case where
6284 -- the result type is an interface (or class-wide interface)
6285 -- is also excluded because the context of the function call
6286 -- must be unconstrained, so the initialization will always
6287 -- be done as part of an allocator evaluation (storage pool
6288 -- or secondary stack), never to a constrained target object
6289 -- passed in by the caller. Besides the assignment being
6290 -- unneeded in this case, it avoids problems with trying to
6291 -- generate a dispatching assignment when the return expression
6292 -- is a nonlimited descendant of a limited interface (the
6293 -- interface has no assignment operation).
6295 if Present (Return_Obj_Expr)
6296 and then not No_Initialization (Ret_Obj_Decl)
6297 and then not Is_Interface (Return_Obj_Typ)
6298 then
6299 Init_Assignment :=
6300 Make_Assignment_Statement (Loc,
6301 Name => New_Reference_To (Return_Obj_Id, Loc),
6302 Expression => Relocate_Node (Return_Obj_Expr));
6304 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
6305 Set_Assignment_OK (Name (Init_Assignment));
6306 Set_No_Ctrl_Actions (Init_Assignment);
6308 Set_Parent (Name (Init_Assignment), Init_Assignment);
6309 Set_Parent (Expression (Init_Assignment), Init_Assignment);
6311 Set_Expression (Ret_Obj_Decl, Empty);
6313 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
6314 and then not Is_Class_Wide_Type
6315 (Etype (Expression (Init_Assignment)))
6316 then
6317 Rewrite (Expression (Init_Assignment),
6318 Make_Type_Conversion (Loc,
6319 Subtype_Mark =>
6320 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
6321 Expression =>
6322 Relocate_Node (Expression (Init_Assignment))));
6323 end if;
6325 -- In the case of functions where the calling context can
6326 -- determine the form of allocation needed, initialization
6327 -- is done with each part of the if statement that handles
6328 -- the different forms of allocation (this is true for
6329 -- unconstrained and tagged result subtypes).
6331 if Constr_Result
6332 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
6333 then
6334 Insert_After (Ret_Obj_Decl, Init_Assignment);
6335 end if;
6336 end if;
6338 -- When the function's subtype is unconstrained, a run-time
6339 -- test is needed to determine the form of allocation to use
6340 -- for the return object. The function has an implicit formal
6341 -- parameter indicating this. If the BIP_Alloc_Form formal has
6342 -- the value one, then the caller has passed access to an
6343 -- existing object for use as the return object. If the value
6344 -- is two, then the return object must be allocated on the
6345 -- secondary stack. Otherwise, the object must be allocated in
6346 -- a storage pool (currently only supported for the global
6347 -- heap, user-defined storage pools TBD ???). We generate an
6348 -- if statement to test the implicit allocation formal and
6349 -- initialize a local access value appropriately, creating
6350 -- allocators in the secondary stack and global heap cases.
6351 -- The special formal also exists and must be tested when the
6352 -- function has a tagged result, even when the result subtype
6353 -- is constrained, because in general such functions can be
6354 -- called in dispatching contexts and must be handled similarly
6355 -- to functions with a class-wide result.
6357 if not Constr_Result
6358 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
6359 then
6360 Obj_Alloc_Formal :=
6361 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
6363 declare
6364 Pool_Id : constant Entity_Id :=
6365 Make_Temporary (Loc, 'P');
6366 Alloc_Obj_Id : Entity_Id;
6367 Alloc_Obj_Decl : Node_Id;
6368 Alloc_If_Stmt : Node_Id;
6369 Heap_Allocator : Node_Id;
6370 Pool_Decl : Node_Id;
6371 Pool_Allocator : Node_Id;
6372 Ptr_Type_Decl : Node_Id;
6373 Ref_Type : Entity_Id;
6374 SS_Allocator : Node_Id;
6376 begin
6377 -- Reuse the itype created for the function's implicit
6378 -- access formal. This avoids the need to create a new
6379 -- access type here, plus it allows assigning the access
6380 -- formal directly without applying a conversion.
6382 -- Ref_Type := Etype (Object_Access);
6384 -- Create an access type designating the function's
6385 -- result subtype.
6387 Ref_Type := Make_Temporary (Loc, 'A');
6389 Ptr_Type_Decl :=
6390 Make_Full_Type_Declaration (Loc,
6391 Defining_Identifier => Ref_Type,
6392 Type_Definition =>
6393 Make_Access_To_Object_Definition (Loc,
6394 All_Present => True,
6395 Subtype_Indication =>
6396 New_Reference_To (Return_Obj_Typ, Loc)));
6398 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
6400 -- Create an access object that will be initialized to an
6401 -- access value denoting the return object, either coming
6402 -- from an implicit access value passed in by the caller
6403 -- or from the result of an allocator.
6405 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
6406 Set_Etype (Alloc_Obj_Id, Ref_Type);
6408 Alloc_Obj_Decl :=
6409 Make_Object_Declaration (Loc,
6410 Defining_Identifier => Alloc_Obj_Id,
6411 Object_Definition =>
6412 New_Reference_To (Ref_Type, Loc));
6414 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
6416 -- Create allocators for both the secondary stack and
6417 -- global heap. If there's an initialization expression,
6418 -- then create these as initialized allocators.
6420 if Present (Return_Obj_Expr)
6421 and then not No_Initialization (Ret_Obj_Decl)
6422 then
6423 -- Always use the type of the expression for the
6424 -- qualified expression, rather than the result type.
6425 -- In general we cannot always use the result type
6426 -- for the allocator, because the expression might be
6427 -- of a specific type, such as in the case of an
6428 -- aggregate or even a nonlimited object when the
6429 -- result type is a limited class-wide interface type.
6431 Heap_Allocator :=
6432 Make_Allocator (Loc,
6433 Expression =>
6434 Make_Qualified_Expression (Loc,
6435 Subtype_Mark =>
6436 New_Reference_To
6437 (Etype (Return_Obj_Expr), Loc),
6438 Expression =>
6439 New_Copy_Tree (Return_Obj_Expr)));
6441 else
6442 -- If the function returns a class-wide type we cannot
6443 -- use the return type for the allocator. Instead we
6444 -- use the type of the expression, which must be an
6445 -- aggregate of a definite type.
6447 if Is_Class_Wide_Type (Return_Obj_Typ) then
6448 Heap_Allocator :=
6449 Make_Allocator (Loc,
6450 Expression =>
6451 New_Reference_To
6452 (Etype (Return_Obj_Expr), Loc));
6453 else
6454 Heap_Allocator :=
6455 Make_Allocator (Loc,
6456 Expression =>
6457 New_Reference_To (Return_Obj_Typ, Loc));
6458 end if;
6460 -- If the object requires default initialization then
6461 -- that will happen later following the elaboration of
6462 -- the object renaming. If we don't turn it off here
6463 -- then the object will be default initialized twice.
6465 Set_No_Initialization (Heap_Allocator);
6466 end if;
6468 -- The Pool_Allocator is just like the Heap_Allocator,
6469 -- except we set Storage_Pool and Procedure_To_Call so
6470 -- it will use the user-defined storage pool.
6472 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
6474 -- Do not generate the renaming of the build-in-place
6475 -- pool parameter on .NET/JVM/ZFP because the parameter
6476 -- is not created in the first place.
6478 if VM_Target = No_VM
6479 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6480 then
6481 Pool_Decl :=
6482 Make_Object_Renaming_Declaration (Loc,
6483 Defining_Identifier => Pool_Id,
6484 Subtype_Mark =>
6485 New_Reference_To
6486 (RTE (RE_Root_Storage_Pool), Loc),
6487 Name =>
6488 Make_Explicit_Dereference (Loc,
6489 New_Reference_To
6490 (Build_In_Place_Formal
6491 (Par_Func, BIP_Storage_Pool), Loc)));
6492 Set_Storage_Pool (Pool_Allocator, Pool_Id);
6493 Set_Procedure_To_Call
6494 (Pool_Allocator, RTE (RE_Allocate_Any));
6495 else
6496 Pool_Decl := Make_Null_Statement (Loc);
6497 end if;
6499 -- If the No_Allocators restriction is active, then only
6500 -- an allocator for secondary stack allocation is needed.
6501 -- It's OK for such allocators to have Comes_From_Source
6502 -- set to False, because gigi knows not to flag them as
6503 -- being a violation of No_Implicit_Heap_Allocations.
6505 if Restriction_Active (No_Allocators) then
6506 SS_Allocator := Heap_Allocator;
6507 Heap_Allocator := Make_Null (Loc);
6508 Pool_Allocator := Make_Null (Loc);
6510 -- Otherwise the heap and pool allocators may be needed,
6511 -- so we make another allocator for secondary stack
6512 -- allocation.
6514 else
6515 SS_Allocator := New_Copy_Tree (Heap_Allocator);
6517 -- The heap and pool allocators are marked as
6518 -- Comes_From_Source since they correspond to an
6519 -- explicit user-written allocator (that is, it will
6520 -- only be executed on behalf of callers that call the
6521 -- function as initialization for such an allocator).
6522 -- Prevents errors when No_Implicit_Heap_Allocations
6523 -- is in force.
6525 Set_Comes_From_Source (Heap_Allocator, True);
6526 Set_Comes_From_Source (Pool_Allocator, True);
6527 end if;
6529 -- The allocator is returned on the secondary stack. We
6530 -- don't do this on VM targets, since the SS is not used.
6532 if VM_Target = No_VM then
6533 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
6534 Set_Procedure_To_Call
6535 (SS_Allocator, RTE (RE_SS_Allocate));
6537 -- The allocator is returned on the secondary stack,
6538 -- so indicate that the function return, as well as
6539 -- the block that encloses the allocator, must not
6540 -- release it. The flags must be set now because
6541 -- the decision to use the secondary stack is done
6542 -- very late in the course of expanding the return
6543 -- statement, past the point where these flags are
6544 -- normally set.
6546 Set_Sec_Stack_Needed_For_Return (Par_Func);
6547 Set_Sec_Stack_Needed_For_Return
6548 (Return_Statement_Entity (N));
6549 Set_Uses_Sec_Stack (Par_Func);
6550 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
6551 end if;
6553 -- Create an if statement to test the BIP_Alloc_Form
6554 -- formal and initialize the access object to either the
6555 -- BIP_Object_Access formal (BIP_Alloc_Form =
6556 -- Caller_Allocation), the result of allocating the
6557 -- object in the secondary stack (BIP_Alloc_Form =
6558 -- Secondary_Stack), or else an allocator to create the
6559 -- return object in the heap or user-defined pool
6560 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6562 -- ??? An unchecked type conversion must be made in the
6563 -- case of assigning the access object formal to the
6564 -- local access object, because a normal conversion would
6565 -- be illegal in some cases (such as converting access-
6566 -- to-unconstrained to access-to-constrained), but the
6567 -- the unchecked conversion will presumably fail to work
6568 -- right in just such cases. It's not clear at all how to
6569 -- handle this. ???
6571 Alloc_If_Stmt :=
6572 Make_If_Statement (Loc,
6573 Condition =>
6574 Make_Op_Eq (Loc,
6575 Left_Opnd =>
6576 New_Reference_To (Obj_Alloc_Formal, Loc),
6577 Right_Opnd =>
6578 Make_Integer_Literal (Loc,
6579 UI_From_Int (BIP_Allocation_Form'Pos
6580 (Caller_Allocation)))),
6582 Then_Statements => New_List (
6583 Make_Assignment_Statement (Loc,
6584 Name =>
6585 New_Reference_To (Alloc_Obj_Id, Loc),
6586 Expression =>
6587 Make_Unchecked_Type_Conversion (Loc,
6588 Subtype_Mark =>
6589 New_Reference_To (Ref_Type, Loc),
6590 Expression =>
6591 New_Reference_To (Object_Access, Loc)))),
6593 Elsif_Parts => New_List (
6594 Make_Elsif_Part (Loc,
6595 Condition =>
6596 Make_Op_Eq (Loc,
6597 Left_Opnd =>
6598 New_Reference_To (Obj_Alloc_Formal, Loc),
6599 Right_Opnd =>
6600 Make_Integer_Literal (Loc,
6601 UI_From_Int (BIP_Allocation_Form'Pos
6602 (Secondary_Stack)))),
6604 Then_Statements => New_List (
6605 Make_Assignment_Statement (Loc,
6606 Name =>
6607 New_Reference_To (Alloc_Obj_Id, Loc),
6608 Expression => SS_Allocator))),
6610 Make_Elsif_Part (Loc,
6611 Condition =>
6612 Make_Op_Eq (Loc,
6613 Left_Opnd =>
6614 New_Reference_To (Obj_Alloc_Formal, Loc),
6615 Right_Opnd =>
6616 Make_Integer_Literal (Loc,
6617 UI_From_Int (BIP_Allocation_Form'Pos
6618 (Global_Heap)))),
6620 Then_Statements => New_List (
6621 Build_Heap_Allocator
6622 (Temp_Id => Alloc_Obj_Id,
6623 Temp_Typ => Ref_Type,
6624 Func_Id => Par_Func,
6625 Ret_Typ => Return_Obj_Typ,
6626 Alloc_Expr => Heap_Allocator)))),
6628 Else_Statements => New_List (
6629 Pool_Decl,
6630 Build_Heap_Allocator
6631 (Temp_Id => Alloc_Obj_Id,
6632 Temp_Typ => Ref_Type,
6633 Func_Id => Par_Func,
6634 Ret_Typ => Return_Obj_Typ,
6635 Alloc_Expr => Pool_Allocator)));
6637 -- If a separate initialization assignment was created
6638 -- earlier, append that following the assignment of the
6639 -- implicit access formal to the access object, to ensure
6640 -- that the return object is initialized in that case. In
6641 -- this situation, the target of the assignment must be
6642 -- rewritten to denote a dereference of the access to the
6643 -- return object passed in by the caller.
6645 if Present (Init_Assignment) then
6646 Rewrite (Name (Init_Assignment),
6647 Make_Explicit_Dereference (Loc,
6648 Prefix => New_Reference_To (Alloc_Obj_Id, Loc)));
6650 Set_Etype
6651 (Name (Init_Assignment), Etype (Return_Obj_Id));
6653 Append_To
6654 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
6655 end if;
6657 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
6659 -- Remember the local access object for use in the
6660 -- dereference of the renaming created below.
6662 Object_Access := Alloc_Obj_Id;
6663 end;
6664 end if;
6666 -- Replace the return object declaration with a renaming of a
6667 -- dereference of the access value designating the return
6668 -- object.
6670 Obj_Acc_Deref :=
6671 Make_Explicit_Dereference (Loc,
6672 Prefix => New_Reference_To (Object_Access, Loc));
6674 Rewrite (Ret_Obj_Decl,
6675 Make_Object_Renaming_Declaration (Loc,
6676 Defining_Identifier => Return_Obj_Id,
6677 Access_Definition => Empty,
6678 Subtype_Mark =>
6679 New_Occurrence_Of (Return_Obj_Typ, Loc),
6680 Name => Obj_Acc_Deref));
6682 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
6683 end;
6684 end if;
6686 -- Case where we do not build a block
6688 else
6689 -- We're about to drop Return_Object_Declarations on the floor, so
6690 -- we need to insert it, in case it got expanded into useful code.
6691 -- Remove side effects from expression, which may be duplicated in
6692 -- subsequent checks (see Expand_Simple_Function_Return).
6694 Insert_List_Before (N, Return_Object_Declarations (N));
6695 Remove_Side_Effects (Exp);
6697 -- Build simple_return_statement that returns the expression directly
6699 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6700 Result := Return_Stmt;
6701 end if;
6703 -- Set the flag to prevent infinite recursion
6705 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
6707 Rewrite (N, Result);
6708 Analyze (N);
6709 end Expand_N_Extended_Return_Statement;
6711 ----------------------------
6712 -- Expand_N_Function_Call --
6713 ----------------------------
6715 procedure Expand_N_Function_Call (N : Node_Id) is
6716 begin
6717 Expand_Call (N);
6719 -- If the return value of a foreign compiled function is VAX Float, then
6720 -- expand the return (adjusts the location of the return value on
6721 -- Alpha/VMS, no-op everywhere else).
6722 -- Comes_From_Source intercepts recursive expansion.
6724 if Nkind (N) = N_Function_Call
6725 and then Vax_Float (Etype (N))
6726 and then Present (Name (N))
6727 and then Present (Entity (Name (N)))
6728 and then Has_Foreign_Convention (Entity (Name (N)))
6729 and then Comes_From_Source (Parent (N))
6730 then
6731 Expand_Vax_Foreign_Return (N);
6732 end if;
6733 end Expand_N_Function_Call;
6735 ---------------------------------------
6736 -- Expand_N_Procedure_Call_Statement --
6737 ---------------------------------------
6739 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
6740 begin
6741 Expand_Call (N);
6742 end Expand_N_Procedure_Call_Statement;
6744 --------------------------------------
6745 -- Expand_N_Simple_Return_Statement --
6746 --------------------------------------
6748 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6749 begin
6750 -- Defend against previous errors (i.e. the return statement calls a
6751 -- function that is not available in configurable runtime).
6753 if Present (Expression (N))
6754 and then Nkind (Expression (N)) = N_Empty
6755 then
6756 Check_Error_Detected;
6757 return;
6758 end if;
6760 -- Distinguish the function and non-function cases:
6762 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6764 when E_Function |
6765 E_Generic_Function =>
6766 Expand_Simple_Function_Return (N);
6768 when E_Procedure |
6769 E_Generic_Procedure |
6770 E_Entry |
6771 E_Entry_Family |
6772 E_Return_Statement =>
6773 Expand_Non_Function_Return (N);
6775 when others =>
6776 raise Program_Error;
6777 end case;
6779 exception
6780 when RE_Not_Available =>
6781 return;
6782 end Expand_N_Simple_Return_Statement;
6784 ------------------------------
6785 -- Expand_N_Subprogram_Body --
6786 ------------------------------
6788 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6789 -- by the back-end.
6791 -- Add dummy push/pop label nodes at start and end to clear any local
6792 -- exception indications if local-exception-to-goto optimization is active.
6794 -- Add return statement if last statement in body is not a return statement
6795 -- (this makes things easier on Gigi which does not want to have to handle
6796 -- a missing return).
6798 -- Add call to Activate_Tasks if body is a task activator
6800 -- Deal with possible detection of infinite recursion
6802 -- Eliminate body completely if convention stubbed
6804 -- Encode entity names within body, since we will not need to reference
6805 -- these entities any longer in the front end.
6807 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6809 -- Reset Pure indication if any parameter has root type System.Address
6810 -- or has any parameters of limited types, where limited means that the
6811 -- run-time view is limited (i.e. the full type is limited).
6813 -- Wrap thread body
6815 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6816 Loc : constant Source_Ptr := Sloc (N);
6817 H : constant Node_Id := Handled_Statement_Sequence (N);
6818 Body_Id : Entity_Id;
6819 Except_H : Node_Id;
6820 L : List_Id;
6821 Spec_Id : Entity_Id;
6823 procedure Add_Return (S : List_Id);
6824 -- Append a return statement to the statement sequence S if the last
6825 -- statement is not already a return or a goto statement. Note that
6826 -- the latter test is not critical, it does not matter if we add a few
6827 -- extra returns, since they get eliminated anyway later on.
6829 ----------------
6830 -- Add_Return --
6831 ----------------
6833 procedure Add_Return (S : List_Id) is
6834 Last_Stm : Node_Id;
6835 Loc : Source_Ptr;
6837 begin
6838 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6839 -- not relevant in this context since they are not executable.
6841 Last_Stm := Last (S);
6842 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
6843 Prev (Last_Stm);
6844 end loop;
6846 -- Now insert return unless last statement is a transfer
6848 if not Is_Transfer (Last_Stm) then
6850 -- The source location for the return is the end label of the
6851 -- procedure if present. Otherwise use the sloc of the last
6852 -- statement in the list. If the list comes from a generated
6853 -- exception handler and we are not debugging generated code,
6854 -- all the statements within the handler are made invisible
6855 -- to the debugger.
6857 if Nkind (Parent (S)) = N_Exception_Handler
6858 and then not Comes_From_Source (Parent (S))
6859 then
6860 Loc := Sloc (Last_Stm);
6861 elsif Present (End_Label (H)) then
6862 Loc := Sloc (End_Label (H));
6863 else
6864 Loc := Sloc (Last_Stm);
6865 end if;
6867 declare
6868 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
6870 begin
6871 -- Append return statement, and set analyzed manually. We can't
6872 -- call Analyze on this return since the scope is wrong.
6874 -- Note: it almost works to push the scope and then do the
6875 -- Analyze call, but something goes wrong in some weird cases
6876 -- and it is not worth worrying about ???
6878 Append_To (S, Rtn);
6879 Set_Analyzed (Rtn);
6881 -- Call _Postconditions procedure if appropriate. We need to
6882 -- do this explicitly because we did not analyze the generated
6883 -- return statement above, so the call did not get inserted.
6885 if Ekind (Spec_Id) = E_Procedure
6886 and then Has_Postconditions (Spec_Id)
6887 then
6888 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
6889 Insert_Action (Rtn,
6890 Make_Procedure_Call_Statement (Loc,
6891 Name =>
6892 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
6893 end if;
6894 end;
6895 end if;
6896 end Add_Return;
6898 -- Start of processing for Expand_N_Subprogram_Body
6900 begin
6901 -- Set L to either the list of declarations if present, or to the list
6902 -- of statements if no declarations are present. This is used to insert
6903 -- new stuff at the start.
6905 if Is_Non_Empty_List (Declarations (N)) then
6906 L := Declarations (N);
6907 else
6908 L := Statements (H);
6909 end if;
6911 -- If local-exception-to-goto optimization active, insert dummy push
6912 -- statements at start, and dummy pop statements at end, but inhibit
6913 -- this if we have No_Exception_Handlers, since they are useless and
6914 -- intefere with analysis, e.g. by codepeer.
6916 if (Debug_Flag_Dot_G
6917 or else Restriction_Active (No_Exception_Propagation))
6918 and then not Restriction_Active (No_Exception_Handlers)
6919 and then not CodePeer_Mode
6920 and then Is_Non_Empty_List (L)
6921 then
6922 declare
6923 FS : constant Node_Id := First (L);
6924 FL : constant Source_Ptr := Sloc (FS);
6925 LS : Node_Id;
6926 LL : Source_Ptr;
6928 begin
6929 -- LS points to either last statement, if statements are present
6930 -- or to the last declaration if there are no statements present.
6931 -- It is the node after which the pop's are generated.
6933 if Is_Non_Empty_List (Statements (H)) then
6934 LS := Last (Statements (H));
6935 else
6936 LS := Last (L);
6937 end if;
6939 LL := Sloc (LS);
6941 Insert_List_Before_And_Analyze (FS, New_List (
6942 Make_Push_Constraint_Error_Label (FL),
6943 Make_Push_Program_Error_Label (FL),
6944 Make_Push_Storage_Error_Label (FL)));
6946 Insert_List_After_And_Analyze (LS, New_List (
6947 Make_Pop_Constraint_Error_Label (LL),
6948 Make_Pop_Program_Error_Label (LL),
6949 Make_Pop_Storage_Error_Label (LL)));
6950 end;
6951 end if;
6953 -- Find entity for subprogram
6955 Body_Id := Defining_Entity (N);
6957 if Present (Corresponding_Spec (N)) then
6958 Spec_Id := Corresponding_Spec (N);
6959 else
6960 Spec_Id := Body_Id;
6961 end if;
6963 -- Need poll on entry to subprogram if polling enabled. We only do this
6964 -- for non-empty subprograms, since it does not seem necessary to poll
6965 -- for a dummy null subprogram.
6967 if Is_Non_Empty_List (L) then
6969 -- Do not add a polling call if the subprogram is to be inlined by
6970 -- the back-end, to avoid repeated calls with multiple inlinings.
6972 if Is_Inlined (Spec_Id)
6973 and then Front_End_Inlining
6974 and then Optimization_Level > 1
6975 then
6976 null;
6977 else
6978 Generate_Poll_Call (First (L));
6979 end if;
6980 end if;
6982 -- If this is a Pure function which has any parameters whose root type
6983 -- is System.Address, reset the Pure indication, since it will likely
6984 -- cause incorrect code to be generated as the parameter is probably
6985 -- a pointer, and the fact that the same pointer is passed does not mean
6986 -- that the same value is being referenced.
6988 -- Note that if the programmer gave an explicit Pure_Function pragma,
6989 -- then we believe the programmer, and leave the subprogram Pure.
6991 -- This code should probably be at the freeze point, so that it happens
6992 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6993 -- semantic tree has Is_Pure set properly ???
6995 if Is_Pure (Spec_Id)
6996 and then Is_Subprogram (Spec_Id)
6997 and then not Has_Pragma_Pure_Function (Spec_Id)
6998 then
6999 declare
7000 F : Entity_Id;
7002 begin
7003 F := First_Formal (Spec_Id);
7004 while Present (F) loop
7005 if Is_Descendent_Of_Address (Etype (F))
7007 -- Note that this test is being made in the body of the
7008 -- subprogram, not the spec, so we are testing the full
7009 -- type for being limited here, as required.
7011 or else Is_Limited_Type (Etype (F))
7012 then
7013 Set_Is_Pure (Spec_Id, False);
7015 if Spec_Id /= Body_Id then
7016 Set_Is_Pure (Body_Id, False);
7017 end if;
7019 exit;
7020 end if;
7022 Next_Formal (F);
7023 end loop;
7024 end;
7025 end if;
7027 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
7029 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
7030 declare
7031 F : Entity_Id;
7032 A : Node_Id;
7034 begin
7035 -- Loop through formals
7037 F := First_Formal (Spec_Id);
7038 while Present (F) loop
7039 if Is_Scalar_Type (Etype (F))
7040 and then Ekind (F) = E_Out_Parameter
7041 then
7042 Check_Restriction (No_Default_Initialization, F);
7044 -- Insert the initialization. We turn off validity checks
7045 -- for this assignment, since we do not want any check on
7046 -- the initial value itself (which may well be invalid).
7047 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
7049 A := Make_Assignment_Statement (Loc,
7050 Name => New_Occurrence_Of (F, Loc),
7051 Expression => Get_Simple_Init_Val (Etype (F), N));
7052 Set_Suppress_Assignment_Checks (A);
7054 Insert_Before_And_Analyze (First (L),
7055 A, Suppress => Validity_Check);
7056 end if;
7058 Next_Formal (F);
7059 end loop;
7060 end;
7061 end if;
7063 -- Clear out statement list for stubbed procedure
7065 if Present (Corresponding_Spec (N)) then
7066 Set_Elaboration_Flag (N, Spec_Id);
7068 if Convention (Spec_Id) = Convention_Stubbed
7069 or else Is_Eliminated (Spec_Id)
7070 then
7071 Set_Declarations (N, Empty_List);
7072 Set_Handled_Statement_Sequence (N,
7073 Make_Handled_Sequence_Of_Statements (Loc,
7074 Statements => New_List (Make_Null_Statement (Loc))));
7075 return;
7076 end if;
7077 end if;
7079 -- Create a set of discriminals for the next protected subprogram body
7081 if Is_List_Member (N)
7082 and then Present (Parent (List_Containing (N)))
7083 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
7084 and then Present (Next_Protected_Operation (N))
7085 then
7086 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
7087 end if;
7089 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
7090 -- subprograms with no specs are not frozen.
7092 declare
7093 Typ : constant Entity_Id := Etype (Spec_Id);
7094 Utyp : constant Entity_Id := Underlying_Type (Typ);
7096 begin
7097 if not Acts_As_Spec (N)
7098 and then Nkind (Parent (Parent (Spec_Id))) /=
7099 N_Subprogram_Body_Stub
7100 then
7101 null;
7103 elsif Is_Immutably_Limited_Type (Typ) then
7104 Set_Returns_By_Ref (Spec_Id);
7106 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
7107 Set_Returns_By_Ref (Spec_Id);
7108 end if;
7109 end;
7111 -- For a procedure, we add a return for all possible syntactic ends of
7112 -- the subprogram.
7114 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
7115 Add_Return (Statements (H));
7117 if Present (Exception_Handlers (H)) then
7118 Except_H := First_Non_Pragma (Exception_Handlers (H));
7119 while Present (Except_H) loop
7120 Add_Return (Statements (Except_H));
7121 Next_Non_Pragma (Except_H);
7122 end loop;
7123 end if;
7125 -- For a function, we must deal with the case where there is at least
7126 -- one missing return. What we do is to wrap the entire body of the
7127 -- function in a block:
7129 -- begin
7130 -- ...
7131 -- end;
7133 -- becomes
7135 -- begin
7136 -- begin
7137 -- ...
7138 -- end;
7140 -- raise Program_Error;
7141 -- end;
7143 -- This approach is necessary because the raise must be signalled to the
7144 -- caller, not handled by any local handler (RM 6.4(11)).
7146 -- Note: we do not need to analyze the constructed sequence here, since
7147 -- it has no handler, and an attempt to analyze the handled statement
7148 -- sequence twice is risky in various ways (e.g. the issue of expanding
7149 -- cleanup actions twice).
7151 elsif Has_Missing_Return (Spec_Id) then
7152 declare
7153 Hloc : constant Source_Ptr := Sloc (H);
7154 Blok : constant Node_Id :=
7155 Make_Block_Statement (Hloc,
7156 Handled_Statement_Sequence => H);
7157 Rais : constant Node_Id :=
7158 Make_Raise_Program_Error (Hloc,
7159 Reason => PE_Missing_Return);
7161 begin
7162 Set_Handled_Statement_Sequence (N,
7163 Make_Handled_Sequence_Of_Statements (Hloc,
7164 Statements => New_List (Blok, Rais)));
7166 Push_Scope (Spec_Id);
7167 Analyze (Blok);
7168 Analyze (Rais);
7169 Pop_Scope;
7170 end;
7171 end if;
7173 -- If subprogram contains a parameterless recursive call, then we may
7174 -- have an infinite recursion, so see if we can generate code to check
7175 -- for this possibility if storage checks are not suppressed.
7177 if Ekind (Spec_Id) = E_Procedure
7178 and then Has_Recursive_Call (Spec_Id)
7179 and then not Storage_Checks_Suppressed (Spec_Id)
7180 then
7181 Detect_Infinite_Recursion (N, Spec_Id);
7182 end if;
7184 -- Set to encode entity names in package body before gigi is called
7186 Qualify_Entity_Names (N);
7187 end Expand_N_Subprogram_Body;
7189 -----------------------------------
7190 -- Expand_N_Subprogram_Body_Stub --
7191 -----------------------------------
7193 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
7194 begin
7195 if Present (Corresponding_Body (N)) then
7196 Expand_N_Subprogram_Body (
7197 Unit_Declaration_Node (Corresponding_Body (N)));
7198 end if;
7199 end Expand_N_Subprogram_Body_Stub;
7201 -------------------------------------
7202 -- Expand_N_Subprogram_Declaration --
7203 -------------------------------------
7205 -- If the declaration appears within a protected body, it is a private
7206 -- operation of the protected type. We must create the corresponding
7207 -- protected subprogram an associated formals. For a normal protected
7208 -- operation, this is done when expanding the protected type declaration.
7210 -- If the declaration is for a null procedure, emit null body
7212 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
7213 Loc : constant Source_Ptr := Sloc (N);
7214 Subp : constant Entity_Id := Defining_Entity (N);
7215 Scop : constant Entity_Id := Scope (Subp);
7216 Prot_Decl : Node_Id;
7217 Prot_Bod : Node_Id;
7218 Prot_Id : Entity_Id;
7220 begin
7221 -- In SPARK, subprogram declarations are only allowed in package
7222 -- specifications.
7224 if Nkind (Parent (N)) /= N_Package_Specification then
7225 if Nkind (Parent (N)) = N_Compilation_Unit then
7226 Check_SPARK_Restriction
7227 ("subprogram declaration is not a library item", N);
7229 elsif Present (Next (N))
7230 and then Nkind (Next (N)) = N_Pragma
7231 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
7232 then
7233 -- In SPARK, subprogram declarations are also permitted in
7234 -- declarative parts when immediately followed by a corresponding
7235 -- pragma Import. We only check here that there is some pragma
7236 -- Import.
7238 null;
7239 else
7240 Check_SPARK_Restriction
7241 ("subprogram declaration is not allowed here", N);
7242 end if;
7243 end if;
7245 -- Deal with case of protected subprogram. Do not generate protected
7246 -- operation if operation is flagged as eliminated.
7248 if Is_List_Member (N)
7249 and then Present (Parent (List_Containing (N)))
7250 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
7251 and then Is_Protected_Type (Scop)
7252 then
7253 if No (Protected_Body_Subprogram (Subp))
7254 and then not Is_Eliminated (Subp)
7255 then
7256 Prot_Decl :=
7257 Make_Subprogram_Declaration (Loc,
7258 Specification =>
7259 Build_Protected_Sub_Specification
7260 (N, Scop, Unprotected_Mode));
7262 -- The protected subprogram is declared outside of the protected
7263 -- body. Given that the body has frozen all entities so far, we
7264 -- analyze the subprogram and perform freezing actions explicitly.
7265 -- including the generation of an explicit freeze node, to ensure
7266 -- that gigi has the proper order of elaboration.
7267 -- If the body is a subunit, the insertion point is before the
7268 -- stub in the parent.
7270 Prot_Bod := Parent (List_Containing (N));
7272 if Nkind (Parent (Prot_Bod)) = N_Subunit then
7273 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
7274 end if;
7276 Insert_Before (Prot_Bod, Prot_Decl);
7277 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
7278 Set_Has_Delayed_Freeze (Prot_Id);
7280 Push_Scope (Scope (Scop));
7281 Analyze (Prot_Decl);
7282 Freeze_Before (N, Prot_Id);
7283 Set_Protected_Body_Subprogram (Subp, Prot_Id);
7285 -- Create protected operation as well. Even though the operation
7286 -- is only accessible within the body, it is possible to make it
7287 -- available outside of the protected object by using 'Access to
7288 -- provide a callback, so build protected version in all cases.
7290 Prot_Decl :=
7291 Make_Subprogram_Declaration (Loc,
7292 Specification =>
7293 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
7294 Insert_Before (Prot_Bod, Prot_Decl);
7295 Analyze (Prot_Decl);
7297 Pop_Scope;
7298 end if;
7300 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
7301 -- cases this is superfluous because calls to it will be automatically
7302 -- inlined, but we definitely need the body if preconditions for the
7303 -- procedure are present.
7305 elsif Nkind (Specification (N)) = N_Procedure_Specification
7306 and then Null_Present (Specification (N))
7307 then
7308 declare
7309 Bod : constant Node_Id := Body_To_Inline (N);
7311 begin
7312 Set_Has_Completion (Subp, False);
7313 Append_Freeze_Action (Subp, Bod);
7315 -- The body now contains raise statements, so calls to it will
7316 -- not be inlined.
7318 Set_Is_Inlined (Subp, False);
7319 end;
7320 end if;
7321 end Expand_N_Subprogram_Declaration;
7323 --------------------------------
7324 -- Expand_Non_Function_Return --
7325 --------------------------------
7327 procedure Expand_Non_Function_Return (N : Node_Id) is
7328 pragma Assert (No (Expression (N)));
7330 Loc : constant Source_Ptr := Sloc (N);
7331 Scope_Id : Entity_Id :=
7332 Return_Applies_To (Return_Statement_Entity (N));
7333 Kind : constant Entity_Kind := Ekind (Scope_Id);
7334 Call : Node_Id;
7335 Acc_Stat : Node_Id;
7336 Goto_Stat : Node_Id;
7337 Lab_Node : Node_Id;
7339 begin
7340 -- Call _Postconditions procedure if procedure with active
7341 -- postconditions. Here, we use the Postcondition_Proc attribute,
7342 -- which is needed for implicitly-generated returns. Functions
7343 -- never have implicitly-generated returns, and there's no
7344 -- room for Postcondition_Proc in E_Function, so we look up the
7345 -- identifier Name_uPostconditions for function returns (see
7346 -- Expand_Simple_Function_Return).
7348 if Ekind (Scope_Id) = E_Procedure
7349 and then Has_Postconditions (Scope_Id)
7350 then
7351 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
7352 Insert_Action (N,
7353 Make_Procedure_Call_Statement (Loc,
7354 Name => New_Reference_To (Postcondition_Proc (Scope_Id), Loc)));
7355 end if;
7357 -- If it is a return from a procedure do no extra steps
7359 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
7360 return;
7362 -- If it is a nested return within an extended one, replace it with a
7363 -- return of the previously declared return object.
7365 elsif Kind = E_Return_Statement then
7366 Rewrite (N,
7367 Make_Simple_Return_Statement (Loc,
7368 Expression =>
7369 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
7370 Set_Comes_From_Extended_Return_Statement (N);
7371 Set_Return_Statement_Entity (N, Scope_Id);
7372 Expand_Simple_Function_Return (N);
7373 return;
7374 end if;
7376 pragma Assert (Is_Entry (Scope_Id));
7378 -- Look at the enclosing block to see whether the return is from an
7379 -- accept statement or an entry body.
7381 for J in reverse 0 .. Scope_Stack.Last loop
7382 Scope_Id := Scope_Stack.Table (J).Entity;
7383 exit when Is_Concurrent_Type (Scope_Id);
7384 end loop;
7386 -- If it is a return from accept statement it is expanded as call to
7387 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
7389 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
7390 -- Expand_N_Accept_Alternative in exp_ch9.adb)
7392 if Is_Task_Type (Scope_Id) then
7394 Call :=
7395 Make_Procedure_Call_Statement (Loc,
7396 Name => New_Reference_To (RTE (RE_Complete_Rendezvous), Loc));
7397 Insert_Before (N, Call);
7398 -- why not insert actions here???
7399 Analyze (Call);
7401 Acc_Stat := Parent (N);
7402 while Nkind (Acc_Stat) /= N_Accept_Statement loop
7403 Acc_Stat := Parent (Acc_Stat);
7404 end loop;
7406 Lab_Node := Last (Statements
7407 (Handled_Statement_Sequence (Acc_Stat)));
7409 Goto_Stat := Make_Goto_Statement (Loc,
7410 Name => New_Occurrence_Of
7411 (Entity (Identifier (Lab_Node)), Loc));
7413 Set_Analyzed (Goto_Stat);
7415 Rewrite (N, Goto_Stat);
7416 Analyze (N);
7418 -- If it is a return from an entry body, put a Complete_Entry_Body call
7419 -- in front of the return.
7421 elsif Is_Protected_Type (Scope_Id) then
7422 Call :=
7423 Make_Procedure_Call_Statement (Loc,
7424 Name =>
7425 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc),
7426 Parameter_Associations => New_List (
7427 Make_Attribute_Reference (Loc,
7428 Prefix =>
7429 New_Reference_To
7430 (Find_Protection_Object (Current_Scope), Loc),
7431 Attribute_Name => Name_Unchecked_Access)));
7433 Insert_Before (N, Call);
7434 Analyze (Call);
7435 end if;
7436 end Expand_Non_Function_Return;
7438 ---------------------------------------
7439 -- Expand_Protected_Object_Reference --
7440 ---------------------------------------
7442 function Expand_Protected_Object_Reference
7443 (N : Node_Id;
7444 Scop : Entity_Id) return Node_Id
7446 Loc : constant Source_Ptr := Sloc (N);
7447 Corr : Entity_Id;
7448 Rec : Node_Id;
7449 Param : Entity_Id;
7450 Proc : Entity_Id;
7452 begin
7453 Rec := Make_Identifier (Loc, Name_uObject);
7454 Set_Etype (Rec, Corresponding_Record_Type (Scop));
7456 -- Find enclosing protected operation, and retrieve its first parameter,
7457 -- which denotes the enclosing protected object. If the enclosing
7458 -- operation is an entry, we are immediately within the protected body,
7459 -- and we can retrieve the object from the service entries procedure. A
7460 -- barrier function has the same signature as an entry. A barrier
7461 -- function is compiled within the protected object, but unlike
7462 -- protected operations its never needs locks, so that its protected
7463 -- body subprogram points to itself.
7465 Proc := Current_Scope;
7466 while Present (Proc)
7467 and then Scope (Proc) /= Scop
7468 loop
7469 Proc := Scope (Proc);
7470 end loop;
7472 Corr := Protected_Body_Subprogram (Proc);
7474 if No (Corr) then
7476 -- Previous error left expansion incomplete.
7477 -- Nothing to do on this call.
7479 return Empty;
7480 end if;
7482 Param :=
7483 Defining_Identifier
7484 (First (Parameter_Specifications (Parent (Corr))));
7486 if Is_Subprogram (Proc)
7487 and then Proc /= Corr
7488 then
7489 -- Protected function or procedure
7491 Set_Entity (Rec, Param);
7493 -- Rec is a reference to an entity which will not be in scope when
7494 -- the call is reanalyzed, and needs no further analysis.
7496 Set_Analyzed (Rec);
7498 else
7499 -- Entry or barrier function for entry body. The first parameter of
7500 -- the entry body procedure is pointer to the object. We create a
7501 -- local variable of the proper type, duplicating what is done to
7502 -- define _object later on.
7504 declare
7505 Decls : List_Id;
7506 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
7508 begin
7509 Decls := New_List (
7510 Make_Full_Type_Declaration (Loc,
7511 Defining_Identifier => Obj_Ptr,
7512 Type_Definition =>
7513 Make_Access_To_Object_Definition (Loc,
7514 Subtype_Indication =>
7515 New_Reference_To
7516 (Corresponding_Record_Type (Scop), Loc))));
7518 Insert_Actions (N, Decls);
7519 Freeze_Before (N, Obj_Ptr);
7521 Rec :=
7522 Make_Explicit_Dereference (Loc,
7523 Prefix =>
7524 Unchecked_Convert_To (Obj_Ptr,
7525 New_Occurrence_Of (Param, Loc)));
7527 -- Analyze new actual. Other actuals in calls are already analyzed
7528 -- and the list of actuals is not reanalyzed after rewriting.
7530 Set_Parent (Rec, N);
7531 Analyze (Rec);
7532 end;
7533 end if;
7535 return Rec;
7536 end Expand_Protected_Object_Reference;
7538 --------------------------------------
7539 -- Expand_Protected_Subprogram_Call --
7540 --------------------------------------
7542 procedure Expand_Protected_Subprogram_Call
7543 (N : Node_Id;
7544 Subp : Entity_Id;
7545 Scop : Entity_Id)
7547 Rec : Node_Id;
7549 begin
7550 -- If the protected object is not an enclosing scope, this is an inter-
7551 -- object function call. Inter-object procedure calls are expanded by
7552 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7553 -- subprogram being called is in the protected body being compiled, and
7554 -- if the protected object in the call is statically the enclosing type.
7555 -- The object may be an component of some other data structure, in which
7556 -- case this must be handled as an inter-object call.
7558 if not In_Open_Scopes (Scop)
7559 or else not Is_Entity_Name (Name (N))
7560 then
7561 if Nkind (Name (N)) = N_Selected_Component then
7562 Rec := Prefix (Name (N));
7564 else
7565 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
7566 Rec := Prefix (Prefix (Name (N)));
7567 end if;
7569 Build_Protected_Subprogram_Call (N,
7570 Name => New_Occurrence_Of (Subp, Sloc (N)),
7571 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7572 External => True);
7574 else
7575 Rec := Expand_Protected_Object_Reference (N, Scop);
7577 if No (Rec) then
7578 return;
7579 end if;
7581 Build_Protected_Subprogram_Call (N,
7582 Name => Name (N),
7583 Rec => Rec,
7584 External => False);
7586 end if;
7588 -- If it is a function call it can appear in elaboration code and
7589 -- the called entity must be frozen here.
7591 if Ekind (Subp) = E_Function then
7592 Freeze_Expression (Name (N));
7593 end if;
7595 -- Analyze and resolve the new call. The actuals have already been
7596 -- resolved, but expansion of a function call will add extra actuals
7597 -- if needed. Analysis of a procedure call already includes resolution.
7599 Analyze (N);
7601 if Ekind (Subp) = E_Function then
7602 Resolve (N, Etype (Subp));
7603 end if;
7604 end Expand_Protected_Subprogram_Call;
7606 --------------------------------------------
7607 -- Has_Unconstrained_Access_Discriminants --
7608 --------------------------------------------
7610 function Has_Unconstrained_Access_Discriminants
7611 (Subtyp : Entity_Id) return Boolean
7613 Discr : Entity_Id;
7615 begin
7616 if Has_Discriminants (Subtyp)
7617 and then not Is_Constrained (Subtyp)
7618 then
7619 Discr := First_Discriminant (Subtyp);
7620 while Present (Discr) loop
7621 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7622 return True;
7623 end if;
7625 Next_Discriminant (Discr);
7626 end loop;
7627 end if;
7629 return False;
7630 end Has_Unconstrained_Access_Discriminants;
7632 -----------------------------------
7633 -- Expand_Simple_Function_Return --
7634 -----------------------------------
7636 -- The "simple" comes from the syntax rule simple_return_statement. The
7637 -- semantics are not at all simple!
7639 procedure Expand_Simple_Function_Return (N : Node_Id) is
7640 Loc : constant Source_Ptr := Sloc (N);
7642 Scope_Id : constant Entity_Id :=
7643 Return_Applies_To (Return_Statement_Entity (N));
7644 -- The function we are returning from
7646 R_Type : constant Entity_Id := Etype (Scope_Id);
7647 -- The result type of the function
7649 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7651 Exp : constant Node_Id := Expression (N);
7652 pragma Assert (Present (Exp));
7654 Exptyp : constant Entity_Id := Etype (Exp);
7655 -- The type of the expression (not necessarily the same as R_Type)
7657 Subtype_Ind : Node_Id;
7658 -- If the result type of the function is class-wide and the expression
7659 -- has a specific type, then we use the expression's type as the type of
7660 -- the return object. In cases where the expression is an aggregate that
7661 -- is built in place, this avoids the need for an expensive conversion
7662 -- of the return object to the specific type on assignments to the
7663 -- individual components.
7665 begin
7666 if Is_Class_Wide_Type (R_Type)
7667 and then not Is_Class_Wide_Type (Etype (Exp))
7668 then
7669 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
7670 else
7671 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7672 end if;
7674 -- For the case of a simple return that does not come from an extended
7675 -- return, in the case of Ada 2005 where we are returning a limited
7676 -- type, we rewrite "return <expression>;" to be:
7678 -- return _anon_ : <return_subtype> := <expression>
7680 -- The expansion produced by Expand_N_Extended_Return_Statement will
7681 -- contain simple return statements (for example, a block containing
7682 -- simple return of the return object), which brings us back here with
7683 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7684 -- checking for a simple return that does not come from an extended
7685 -- return is to avoid this infinite recursion.
7687 -- The reason for this design is that for Ada 2005 limited returns, we
7688 -- need to reify the return object, so we can build it "in place", and
7689 -- we need a block statement to hang finalization and tasking stuff.
7691 -- ??? In order to avoid disruption, we avoid translating to extended
7692 -- return except in the cases where we really need to (Ada 2005 for
7693 -- inherently limited). We might prefer to do this translation in all
7694 -- cases (except perhaps for the case of Ada 95 inherently limited),
7695 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7696 -- code. This would also allow us to do the build-in-place optimization
7697 -- for efficiency even in cases where it is semantically not required.
7699 -- As before, we check the type of the return expression rather than the
7700 -- return type of the function, because the latter may be a limited
7701 -- class-wide interface type, which is not a limited type, even though
7702 -- the type of the expression may be.
7704 if not Comes_From_Extended_Return_Statement (N)
7705 and then Is_Immutably_Limited_Type (Etype (Expression (N)))
7706 and then Ada_Version >= Ada_2005
7707 and then not Debug_Flag_Dot_L
7709 -- The functionality of interface thunks is simple and it is always
7710 -- handled by means of simple return statements. This leaves their
7711 -- expansion simple and clean.
7713 and then not Is_Thunk (Current_Scope)
7714 then
7715 declare
7716 Return_Object_Entity : constant Entity_Id :=
7717 Make_Temporary (Loc, 'R', Exp);
7719 Obj_Decl : constant Node_Id :=
7720 Make_Object_Declaration (Loc,
7721 Defining_Identifier => Return_Object_Entity,
7722 Object_Definition => Subtype_Ind,
7723 Expression => Exp);
7725 Ext : constant Node_Id :=
7726 Make_Extended_Return_Statement (Loc,
7727 Return_Object_Declarations => New_List (Obj_Decl));
7728 -- Do not perform this high-level optimization if the result type
7729 -- is an interface because the "this" pointer must be displaced.
7731 begin
7732 Rewrite (N, Ext);
7733 Analyze (N);
7734 return;
7735 end;
7736 end if;
7738 -- Here we have a simple return statement that is part of the expansion
7739 -- of an extended return statement (either written by the user, or
7740 -- generated by the above code).
7742 -- Always normalize C/Fortran boolean result. This is not always needed,
7743 -- but it seems a good idea to minimize the passing around of non-
7744 -- normalized values, and in any case this handles the processing of
7745 -- barrier functions for protected types, which turn the condition into
7746 -- a return statement.
7748 if Is_Boolean_Type (Exptyp)
7749 and then Nonzero_Is_True (Exptyp)
7750 then
7751 Adjust_Condition (Exp);
7752 Adjust_Result_Type (Exp, Exptyp);
7753 end if;
7755 -- Do validity check if enabled for returns
7757 if Validity_Checks_On
7758 and then Validity_Check_Returns
7759 then
7760 Ensure_Valid (Exp);
7761 end if;
7763 -- Check the result expression of a scalar function against the subtype
7764 -- of the function by inserting a conversion. This conversion must
7765 -- eventually be performed for other classes of types, but for now it's
7766 -- only done for scalars.
7767 -- ???
7769 if Is_Scalar_Type (Exptyp) then
7770 Rewrite (Exp, Convert_To (R_Type, Exp));
7772 -- The expression is resolved to ensure that the conversion gets
7773 -- expanded to generate a possible constraint check.
7775 Analyze_And_Resolve (Exp, R_Type);
7776 end if;
7778 -- Deal with returning variable length objects and controlled types
7780 -- Nothing to do if we are returning by reference, or this is not a
7781 -- type that requires special processing (indicated by the fact that
7782 -- it requires a cleanup scope for the secondary stack case).
7784 if Is_Immutably_Limited_Type (Exptyp)
7785 or else Is_Limited_Interface (Exptyp)
7786 then
7787 null;
7789 -- No copy needed for thunks returning interface type objects since
7790 -- the object is returned by reference and the maximum functionality
7791 -- required is just to displace the pointer.
7793 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
7794 null;
7796 elsif not Requires_Transient_Scope (R_Type) then
7798 -- Mutable records with no variable length components are not
7799 -- returned on the sec-stack, so we need to make sure that the
7800 -- backend will only copy back the size of the actual value, and not
7801 -- the maximum size. We create an actual subtype for this purpose.
7803 declare
7804 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
7805 Decl : Node_Id;
7806 Ent : Entity_Id;
7807 begin
7808 if Has_Discriminants (Ubt)
7809 and then not Is_Constrained (Ubt)
7810 and then not Has_Unchecked_Union (Ubt)
7811 then
7812 Decl := Build_Actual_Subtype (Ubt, Exp);
7813 Ent := Defining_Identifier (Decl);
7814 Insert_Action (Exp, Decl);
7815 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7816 Analyze_And_Resolve (Exp);
7817 end if;
7818 end;
7820 -- Here if secondary stack is used
7822 else
7823 -- Make sure that no surrounding block will reclaim the secondary
7824 -- stack on which we are going to put the result. Not only may this
7825 -- introduce secondary stack leaks but worse, if the reclamation is
7826 -- done too early, then the result we are returning may get
7827 -- clobbered.
7829 declare
7830 S : Entity_Id;
7831 begin
7832 S := Current_Scope;
7833 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
7834 Set_Sec_Stack_Needed_For_Return (S, True);
7835 S := Enclosing_Dynamic_Scope (S);
7836 end loop;
7837 end;
7839 -- Optimize the case where the result is a function call. In this
7840 -- case either the result is already on the secondary stack, or is
7841 -- already being returned with the stack pointer depressed and no
7842 -- further processing is required except to set the By_Ref flag
7843 -- to ensure that gigi does not attempt an extra unnecessary copy.
7844 -- (actually not just unnecessary but harmfully wrong in the case
7845 -- of a controlled type, where gigi does not know how to do a copy).
7846 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7847 -- for array types if the constrained status of the target type is
7848 -- different from that of the expression.
7850 if Requires_Transient_Scope (Exptyp)
7851 and then
7852 (not Is_Array_Type (Exptyp)
7853 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
7854 or else CW_Or_Has_Controlled_Part (Utyp))
7855 and then Nkind (Exp) = N_Function_Call
7856 then
7857 Set_By_Ref (N);
7859 -- Remove side effects from the expression now so that other parts
7860 -- of the expander do not have to reanalyze this node without this
7861 -- optimization
7863 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7865 -- For controlled types, do the allocation on the secondary stack
7866 -- manually in order to call adjust at the right time:
7868 -- type Anon1 is access R_Type;
7869 -- for Anon1'Storage_pool use ss_pool;
7870 -- Anon2 : anon1 := new R_Type'(expr);
7871 -- return Anon2.all;
7873 -- We do the same for classwide types that are not potentially
7874 -- controlled (by the virtue of restriction No_Finalization) because
7875 -- gigi is not able to properly allocate class-wide types.
7877 elsif CW_Or_Has_Controlled_Part (Utyp) then
7878 declare
7879 Loc : constant Source_Ptr := Sloc (N);
7880 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7881 Alloc_Node : Node_Id;
7882 Temp : Entity_Id;
7884 begin
7885 Set_Ekind (Acc_Typ, E_Access_Type);
7887 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7889 -- This is an allocator for the secondary stack, and it's fine
7890 -- to have Comes_From_Source set False on it, as gigi knows not
7891 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7893 Alloc_Node :=
7894 Make_Allocator (Loc,
7895 Expression =>
7896 Make_Qualified_Expression (Loc,
7897 Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
7898 Expression => Relocate_Node (Exp)));
7900 -- We do not want discriminant checks on the declaration,
7901 -- given that it gets its value from the allocator.
7903 Set_No_Initialization (Alloc_Node);
7905 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7907 Insert_List_Before_And_Analyze (N, New_List (
7908 Make_Full_Type_Declaration (Loc,
7909 Defining_Identifier => Acc_Typ,
7910 Type_Definition =>
7911 Make_Access_To_Object_Definition (Loc,
7912 Subtype_Indication => Subtype_Ind)),
7914 Make_Object_Declaration (Loc,
7915 Defining_Identifier => Temp,
7916 Object_Definition => New_Reference_To (Acc_Typ, Loc),
7917 Expression => Alloc_Node)));
7919 Rewrite (Exp,
7920 Make_Explicit_Dereference (Loc,
7921 Prefix => New_Reference_To (Temp, Loc)));
7923 -- Ada 2005 (AI-251): If the type of the returned object is
7924 -- an interface then add an implicit type conversion to force
7925 -- displacement of the "this" pointer.
7927 if Is_Interface (R_Type) then
7928 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7929 end if;
7931 Analyze_And_Resolve (Exp, R_Type);
7932 end;
7934 -- Otherwise use the gigi mechanism to allocate result on the
7935 -- secondary stack.
7937 else
7938 Check_Restriction (No_Secondary_Stack, N);
7939 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7941 -- If we are generating code for the VM do not use
7942 -- SS_Allocate since everything is heap-allocated anyway.
7944 if VM_Target = No_VM then
7945 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7946 end if;
7947 end if;
7948 end if;
7950 -- Implement the rules of 6.5(8-10), which require a tag check in
7951 -- the case of a limited tagged return type, and tag reassignment for
7952 -- nonlimited tagged results. These actions are needed when the return
7953 -- type is a specific tagged type and the result expression is a
7954 -- conversion or a formal parameter, because in that case the tag of
7955 -- the expression might differ from the tag of the specific result type.
7957 if Is_Tagged_Type (Utyp)
7958 and then not Is_Class_Wide_Type (Utyp)
7959 and then (Nkind_In (Exp, N_Type_Conversion,
7960 N_Unchecked_Type_Conversion)
7961 or else (Is_Entity_Name (Exp)
7962 and then Ekind (Entity (Exp)) in Formal_Kind))
7963 then
7964 -- When the return type is limited, perform a check that the tag of
7965 -- the result is the same as the tag of the return type.
7967 if Is_Limited_Type (R_Type) then
7968 Insert_Action (Exp,
7969 Make_Raise_Constraint_Error (Loc,
7970 Condition =>
7971 Make_Op_Ne (Loc,
7972 Left_Opnd =>
7973 Make_Selected_Component (Loc,
7974 Prefix => Duplicate_Subexpr (Exp),
7975 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7976 Right_Opnd =>
7977 Make_Attribute_Reference (Loc,
7978 Prefix =>
7979 New_Occurrence_Of (Base_Type (Utyp), Loc),
7980 Attribute_Name => Name_Tag)),
7981 Reason => CE_Tag_Check_Failed));
7983 -- If the result type is a specific nonlimited tagged type, then we
7984 -- have to ensure that the tag of the result is that of the result
7985 -- type. This is handled by making a copy of the expression in
7986 -- the case where it might have a different tag, namely when the
7987 -- expression is a conversion or a formal parameter. We create a new
7988 -- object of the result type and initialize it from the expression,
7989 -- which will implicitly force the tag to be set appropriately.
7991 else
7992 declare
7993 ExpR : constant Node_Id := Relocate_Node (Exp);
7994 Result_Id : constant Entity_Id :=
7995 Make_Temporary (Loc, 'R', ExpR);
7996 Result_Exp : constant Node_Id :=
7997 New_Reference_To (Result_Id, Loc);
7998 Result_Obj : constant Node_Id :=
7999 Make_Object_Declaration (Loc,
8000 Defining_Identifier => Result_Id,
8001 Object_Definition =>
8002 New_Reference_To (R_Type, Loc),
8003 Constant_Present => True,
8004 Expression => ExpR);
8006 begin
8007 Set_Assignment_OK (Result_Obj);
8008 Insert_Action (Exp, Result_Obj);
8010 Rewrite (Exp, Result_Exp);
8011 Analyze_And_Resolve (Exp, R_Type);
8012 end;
8013 end if;
8015 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
8016 -- a check that the level of the return expression's underlying type
8017 -- is not deeper than the level of the master enclosing the function.
8018 -- Always generate the check when the type of the return expression
8019 -- is class-wide, when it's a type conversion, or when it's a formal
8020 -- parameter. Otherwise, suppress the check in the case where the
8021 -- return expression has a specific type whose level is known not to
8022 -- be statically deeper than the function's result type.
8024 -- No runtime check needed in interface thunks since it is performed
8025 -- by the target primitive associated with the thunk.
8027 -- Note: accessibility check is skipped in the VM case, since there
8028 -- does not seem to be any practical way to implement this check.
8030 elsif Ada_Version >= Ada_2005
8031 and then Tagged_Type_Expansion
8032 and then Is_Class_Wide_Type (R_Type)
8033 and then not Is_Thunk (Current_Scope)
8034 and then not Scope_Suppress.Suppress (Accessibility_Check)
8035 and then
8036 (Is_Class_Wide_Type (Etype (Exp))
8037 or else Nkind_In (Exp, N_Type_Conversion,
8038 N_Unchecked_Type_Conversion)
8039 or else (Is_Entity_Name (Exp)
8040 and then Ekind (Entity (Exp)) in Formal_Kind)
8041 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
8042 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
8043 then
8044 declare
8045 Tag_Node : Node_Id;
8047 begin
8048 -- Ada 2005 (AI-251): In class-wide interface objects we displace
8049 -- "this" to reference the base of the object. This is required to
8050 -- get access to the TSD of the object.
8052 if Is_Class_Wide_Type (Etype (Exp))
8053 and then Is_Interface (Etype (Exp))
8054 and then Nkind (Exp) = N_Explicit_Dereference
8055 then
8056 Tag_Node :=
8057 Make_Explicit_Dereference (Loc,
8058 Prefix =>
8059 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
8060 Make_Function_Call (Loc,
8061 Name =>
8062 New_Reference_To (RTE (RE_Base_Address), Loc),
8063 Parameter_Associations => New_List (
8064 Unchecked_Convert_To (RTE (RE_Address),
8065 Duplicate_Subexpr (Prefix (Exp)))))));
8066 else
8067 Tag_Node :=
8068 Make_Attribute_Reference (Loc,
8069 Prefix => Duplicate_Subexpr (Exp),
8070 Attribute_Name => Name_Tag);
8071 end if;
8073 Insert_Action (Exp,
8074 Make_Raise_Program_Error (Loc,
8075 Condition =>
8076 Make_Op_Gt (Loc,
8077 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
8078 Right_Opnd =>
8079 Make_Integer_Literal (Loc,
8080 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
8081 Reason => PE_Accessibility_Check_Failed));
8082 end;
8084 -- AI05-0073: If function has a controlling access result, check that
8085 -- the tag of the return value, if it is not null, matches designated
8086 -- type of return type.
8087 -- The return expression is referenced twice in the code below, so
8088 -- it must be made free of side effects. Given that different compilers
8089 -- may evaluate these parameters in different order, both occurrences
8090 -- perform a copy.
8092 elsif Ekind (R_Type) = E_Anonymous_Access_Type
8093 and then Has_Controlling_Result (Scope_Id)
8094 then
8095 Insert_Action (N,
8096 Make_Raise_Constraint_Error (Loc,
8097 Condition =>
8098 Make_And_Then (Loc,
8099 Left_Opnd =>
8100 Make_Op_Ne (Loc,
8101 Left_Opnd => Duplicate_Subexpr (Exp),
8102 Right_Opnd => Make_Null (Loc)),
8104 Right_Opnd => Make_Op_Ne (Loc,
8105 Left_Opnd =>
8106 Make_Selected_Component (Loc,
8107 Prefix => Duplicate_Subexpr (Exp),
8108 Selector_Name => Make_Identifier (Loc, Name_uTag)),
8110 Right_Opnd =>
8111 Make_Attribute_Reference (Loc,
8112 Prefix =>
8113 New_Occurrence_Of (Designated_Type (R_Type), Loc),
8114 Attribute_Name => Name_Tag))),
8116 Reason => CE_Tag_Check_Failed),
8117 Suppress => All_Checks);
8118 end if;
8120 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
8121 -- ensure that the function result does not outlive an
8122 -- object designated by one of it discriminants.
8124 if Present (Extra_Accessibility_Of_Result (Scope_Id))
8125 and then Has_Unconstrained_Access_Discriminants (R_Type)
8126 then
8127 declare
8128 Discrim_Source : Node_Id;
8130 procedure Check_Against_Result_Level (Level : Node_Id);
8131 -- Check the given accessibility level against the level
8132 -- determined by the point of call. (AI05-0234).
8134 --------------------------------
8135 -- Check_Against_Result_Level --
8136 --------------------------------
8138 procedure Check_Against_Result_Level (Level : Node_Id) is
8139 begin
8140 Insert_Action (N,
8141 Make_Raise_Program_Error (Loc,
8142 Condition =>
8143 Make_Op_Gt (Loc,
8144 Left_Opnd => Level,
8145 Right_Opnd =>
8146 New_Occurrence_Of
8147 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
8148 Reason => PE_Accessibility_Check_Failed));
8149 end Check_Against_Result_Level;
8151 begin
8152 Discrim_Source := Exp;
8153 while Nkind (Discrim_Source) = N_Qualified_Expression loop
8154 Discrim_Source := Expression (Discrim_Source);
8155 end loop;
8157 if Nkind (Discrim_Source) = N_Identifier
8158 and then Is_Return_Object (Entity (Discrim_Source))
8159 then
8160 Discrim_Source := Entity (Discrim_Source);
8162 if Is_Constrained (Etype (Discrim_Source)) then
8163 Discrim_Source := Etype (Discrim_Source);
8164 else
8165 Discrim_Source := Expression (Parent (Discrim_Source));
8166 end if;
8168 elsif Nkind (Discrim_Source) = N_Identifier
8169 and then Nkind_In (Original_Node (Discrim_Source),
8170 N_Aggregate, N_Extension_Aggregate)
8171 then
8172 Discrim_Source := Original_Node (Discrim_Source);
8174 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
8175 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
8176 then
8177 Discrim_Source := Original_Node (Discrim_Source);
8178 end if;
8180 while Nkind_In (Discrim_Source, N_Qualified_Expression,
8181 N_Type_Conversion,
8182 N_Unchecked_Type_Conversion)
8183 loop
8184 Discrim_Source := Expression (Discrim_Source);
8185 end loop;
8187 case Nkind (Discrim_Source) is
8188 when N_Defining_Identifier =>
8190 pragma Assert (Is_Composite_Type (Discrim_Source)
8191 and then Has_Discriminants (Discrim_Source)
8192 and then Is_Constrained (Discrim_Source));
8194 declare
8195 Discrim : Entity_Id :=
8196 First_Discriminant (Base_Type (R_Type));
8197 Disc_Elmt : Elmt_Id :=
8198 First_Elmt (Discriminant_Constraint
8199 (Discrim_Source));
8200 begin
8201 loop
8202 if Ekind (Etype (Discrim)) =
8203 E_Anonymous_Access_Type
8204 then
8205 Check_Against_Result_Level
8206 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
8207 end if;
8209 Next_Elmt (Disc_Elmt);
8210 Next_Discriminant (Discrim);
8211 exit when not Present (Discrim);
8212 end loop;
8213 end;
8215 when N_Aggregate | N_Extension_Aggregate =>
8217 -- Unimplemented: extension aggregate case where discrims
8218 -- come from ancestor part, not extension part.
8220 declare
8221 Discrim : Entity_Id :=
8222 First_Discriminant (Base_Type (R_Type));
8224 Disc_Exp : Node_Id := Empty;
8226 Positionals_Exhausted
8227 : Boolean := not Present (Expressions
8228 (Discrim_Source));
8230 function Associated_Expr
8231 (Comp_Id : Entity_Id;
8232 Associations : List_Id) return Node_Id;
8234 -- Given a component and a component associations list,
8235 -- locate the expression for that component; returns
8236 -- Empty if no such expression is found.
8238 ---------------------
8239 -- Associated_Expr --
8240 ---------------------
8242 function Associated_Expr
8243 (Comp_Id : Entity_Id;
8244 Associations : List_Id) return Node_Id
8246 Assoc : Node_Id;
8247 Choice : Node_Id;
8249 begin
8250 -- Simple linear search seems ok here
8252 Assoc := First (Associations);
8253 while Present (Assoc) loop
8254 Choice := First (Choices (Assoc));
8255 while Present (Choice) loop
8256 if (Nkind (Choice) = N_Identifier
8257 and then Chars (Choice) = Chars (Comp_Id))
8258 or else (Nkind (Choice) = N_Others_Choice)
8259 then
8260 return Expression (Assoc);
8261 end if;
8263 Next (Choice);
8264 end loop;
8266 Next (Assoc);
8267 end loop;
8269 return Empty;
8270 end Associated_Expr;
8272 -- Start of processing for Expand_Simple_Function_Return
8274 begin
8275 if not Positionals_Exhausted then
8276 Disc_Exp := First (Expressions (Discrim_Source));
8277 end if;
8279 loop
8280 if Positionals_Exhausted then
8281 Disc_Exp :=
8282 Associated_Expr
8283 (Discrim,
8284 Component_Associations (Discrim_Source));
8285 end if;
8287 if Ekind (Etype (Discrim)) =
8288 E_Anonymous_Access_Type
8289 then
8290 Check_Against_Result_Level
8291 (Dynamic_Accessibility_Level (Disc_Exp));
8292 end if;
8294 Next_Discriminant (Discrim);
8295 exit when not Present (Discrim);
8297 if not Positionals_Exhausted then
8298 Next (Disc_Exp);
8299 Positionals_Exhausted := not Present (Disc_Exp);
8300 end if;
8301 end loop;
8302 end;
8304 when N_Function_Call =>
8306 -- No check needed (check performed by callee)
8308 null;
8310 when others =>
8312 declare
8313 Level : constant Node_Id :=
8314 Make_Integer_Literal (Loc,
8315 Object_Access_Level (Discrim_Source));
8317 begin
8318 -- Unimplemented: check for name prefix that includes
8319 -- a dereference of an access value with a dynamic
8320 -- accessibility level (e.g., an access param or a
8321 -- saooaaat) and use dynamic level in that case. For
8322 -- example:
8323 -- return Access_Param.all(Some_Index).Some_Component;
8324 -- ???
8326 Set_Etype (Level, Standard_Natural);
8327 Check_Against_Result_Level (Level);
8328 end;
8330 end case;
8331 end;
8332 end if;
8334 -- If we are returning an object that may not be bit-aligned, then copy
8335 -- the value into a temporary first. This copy may need to expand to a
8336 -- loop of component operations.
8338 if Is_Possibly_Unaligned_Slice (Exp)
8339 or else Is_Possibly_Unaligned_Object (Exp)
8340 then
8341 declare
8342 ExpR : constant Node_Id := Relocate_Node (Exp);
8343 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
8344 begin
8345 Insert_Action (Exp,
8346 Make_Object_Declaration (Loc,
8347 Defining_Identifier => Tnn,
8348 Constant_Present => True,
8349 Object_Definition => New_Occurrence_Of (R_Type, Loc),
8350 Expression => ExpR),
8351 Suppress => All_Checks);
8352 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8353 end;
8354 end if;
8356 -- Generate call to postcondition checks if they are present
8358 if Ekind (Scope_Id) = E_Function
8359 and then Has_Postconditions (Scope_Id)
8360 then
8361 -- We are going to reference the returned value twice in this case,
8362 -- once in the call to _Postconditions, and once in the actual return
8363 -- statement, but we can't have side effects happening twice, and in
8364 -- any case for efficiency we don't want to do the computation twice.
8366 -- If the returned expression is an entity name, we don't need to
8367 -- worry since it is efficient and safe to reference it twice, that's
8368 -- also true for literals other than string literals, and for the
8369 -- case of X.all where X is an entity name.
8371 if Is_Entity_Name (Exp)
8372 or else Nkind_In (Exp, N_Character_Literal,
8373 N_Integer_Literal,
8374 N_Real_Literal)
8375 or else (Nkind (Exp) = N_Explicit_Dereference
8376 and then Is_Entity_Name (Prefix (Exp)))
8377 then
8378 null;
8380 -- Otherwise we are going to need a temporary to capture the value
8382 else
8383 declare
8384 ExpR : Node_Id := Relocate_Node (Exp);
8385 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
8387 begin
8388 -- In the case of discriminated objects, we have created a
8389 -- constrained subtype above, and used the underlying type.
8390 -- This transformation is post-analysis and harmless, except
8391 -- that now the call to the post-condition will be analyzed and
8392 -- type kinds have to match.
8394 if Nkind (ExpR) = N_Unchecked_Type_Conversion
8395 and then
8396 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
8397 then
8398 ExpR := Expression (ExpR);
8399 end if;
8401 -- For a complex expression of an elementary type, capture
8402 -- value in the temporary and use it as the reference.
8404 if Is_Elementary_Type (R_Type) then
8405 Insert_Action (Exp,
8406 Make_Object_Declaration (Loc,
8407 Defining_Identifier => Tnn,
8408 Constant_Present => True,
8409 Object_Definition => New_Occurrence_Of (R_Type, Loc),
8410 Expression => ExpR),
8411 Suppress => All_Checks);
8413 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8415 -- If we have something we can rename, generate a renaming of
8416 -- the object and replace the expression with a reference
8418 elsif Is_Object_Reference (Exp) then
8419 Insert_Action (Exp,
8420 Make_Object_Renaming_Declaration (Loc,
8421 Defining_Identifier => Tnn,
8422 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
8423 Name => ExpR),
8424 Suppress => All_Checks);
8426 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8428 -- Otherwise we have something like a string literal or an
8429 -- aggregate. We could copy the value, but that would be
8430 -- inefficient. Instead we make a reference to the value and
8431 -- capture this reference with a renaming, the expression is
8432 -- then replaced by a dereference of this renaming.
8434 else
8435 -- For now, copy the value, since the code below does not
8436 -- seem to work correctly ???
8438 Insert_Action (Exp,
8439 Make_Object_Declaration (Loc,
8440 Defining_Identifier => Tnn,
8441 Constant_Present => True,
8442 Object_Definition => New_Occurrence_Of (R_Type, Loc),
8443 Expression => Relocate_Node (Exp)),
8444 Suppress => All_Checks);
8446 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
8448 -- Insert_Action (Exp,
8449 -- Make_Object_Renaming_Declaration (Loc,
8450 -- Defining_Identifier => Tnn,
8451 -- Access_Definition =>
8452 -- Make_Access_Definition (Loc,
8453 -- All_Present => True,
8454 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
8455 -- Name =>
8456 -- Make_Reference (Loc,
8457 -- Prefix => Relocate_Node (Exp))),
8458 -- Suppress => All_Checks);
8460 -- Rewrite (Exp,
8461 -- Make_Explicit_Dereference (Loc,
8462 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
8463 end if;
8464 end;
8465 end if;
8467 -- Generate call to _postconditions
8469 Insert_Action (Exp,
8470 Make_Procedure_Call_Statement (Loc,
8471 Name => Make_Identifier (Loc, Name_uPostconditions),
8472 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
8473 end if;
8475 -- Ada 2005 (AI-251): If this return statement corresponds with an
8476 -- simple return statement associated with an extended return statement
8477 -- and the type of the returned object is an interface then generate an
8478 -- implicit conversion to force displacement of the "this" pointer.
8480 if Ada_Version >= Ada_2005
8481 and then Comes_From_Extended_Return_Statement (N)
8482 and then Nkind (Expression (N)) = N_Identifier
8483 and then Is_Interface (Utyp)
8484 and then Utyp /= Underlying_Type (Exptyp)
8485 then
8486 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
8487 Analyze_And_Resolve (Exp);
8488 end if;
8489 end Expand_Simple_Function_Return;
8491 --------------------------------
8492 -- Is_Build_In_Place_Function --
8493 --------------------------------
8495 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
8496 begin
8497 -- This function is called from Expand_Subtype_From_Expr during
8498 -- semantic analysis, even when expansion is off. In those cases
8499 -- the build_in_place expansion will not take place.
8501 if not Expander_Active then
8502 return False;
8503 end if;
8505 -- For now we test whether E denotes a function or access-to-function
8506 -- type whose result subtype is inherently limited. Later this test may
8507 -- be revised to allow composite nonlimited types. Functions with a
8508 -- foreign convention or whose result type has a foreign convention
8509 -- never qualify.
8511 if Ekind_In (E, E_Function, E_Generic_Function)
8512 or else (Ekind (E) = E_Subprogram_Type
8513 and then Etype (E) /= Standard_Void_Type)
8514 then
8515 -- Note: If you have Convention (C) on an inherently limited type,
8516 -- you're on your own. That is, the C code will have to be carefully
8517 -- written to know about the Ada conventions.
8519 if Has_Foreign_Convention (E)
8520 or else Has_Foreign_Convention (Etype (E))
8521 then
8522 return False;
8524 -- In Ada 2005 all functions with an inherently limited return type
8525 -- must be handled using a build-in-place profile, including the case
8526 -- of a function with a limited interface result, where the function
8527 -- may return objects of nonlimited descendants.
8529 else
8530 return Is_Immutably_Limited_Type (Etype (E))
8531 and then Ada_Version >= Ada_2005
8532 and then not Debug_Flag_Dot_L;
8533 end if;
8535 else
8536 return False;
8537 end if;
8538 end Is_Build_In_Place_Function;
8540 -------------------------------------
8541 -- Is_Build_In_Place_Function_Call --
8542 -------------------------------------
8544 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
8545 Exp_Node : Node_Id := N;
8546 Function_Id : Entity_Id;
8548 begin
8549 -- Return False when the expander is inactive, since awareness of
8550 -- build-in-place treatment is only relevant during expansion. Note that
8551 -- Is_Build_In_Place_Function, which is called as part of this function,
8552 -- is also conditioned this way, but we need to check here as well to
8553 -- avoid blowing up on processing protected calls when expansion is
8554 -- disabled (such as with -gnatc) since those would trip over the raise
8555 -- of Program_Error below.
8557 if not Expander_Active then
8558 return False;
8559 end if;
8561 -- Step past qualification or unchecked conversion (the latter can occur
8562 -- in cases of calls to 'Input).
8564 if Nkind_In (Exp_Node, N_Qualified_Expression,
8565 N_Unchecked_Type_Conversion)
8566 then
8567 Exp_Node := Expression (N);
8568 end if;
8570 if Nkind (Exp_Node) /= N_Function_Call then
8571 return False;
8573 else
8574 -- In SPARK mode, build-in-place calls are not expanded, so that we
8575 -- may end up with a call that is neither resolved to an entity, nor
8576 -- an indirect call.
8578 if SPARK_Mode then
8579 return False;
8581 elsif Is_Entity_Name (Name (Exp_Node)) then
8582 Function_Id := Entity (Name (Exp_Node));
8584 -- In the case of an explicitly dereferenced call, use the subprogram
8585 -- type generated for the dereference.
8587 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8588 Function_Id := Etype (Name (Exp_Node));
8590 -- This may be a call to a protected function.
8592 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
8593 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
8595 else
8596 raise Program_Error;
8597 end if;
8599 return Is_Build_In_Place_Function (Function_Id);
8600 end if;
8601 end Is_Build_In_Place_Function_Call;
8603 -----------------------
8604 -- Freeze_Subprogram --
8605 -----------------------
8607 procedure Freeze_Subprogram (N : Node_Id) is
8608 Loc : constant Source_Ptr := Sloc (N);
8610 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
8611 -- (Ada 2005): Register a predefined primitive in all the secondary
8612 -- dispatch tables of its primitive type.
8614 ----------------------------------
8615 -- Register_Predefined_DT_Entry --
8616 ----------------------------------
8618 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
8619 Iface_DT_Ptr : Elmt_Id;
8620 Tagged_Typ : Entity_Id;
8621 Thunk_Id : Entity_Id;
8622 Thunk_Code : Node_Id;
8624 begin
8625 Tagged_Typ := Find_Dispatching_Type (Prim);
8627 if No (Access_Disp_Table (Tagged_Typ))
8628 or else not Has_Interfaces (Tagged_Typ)
8629 or else not RTE_Available (RE_Interface_Tag)
8630 or else Restriction_Active (No_Dispatching_Calls)
8631 then
8632 return;
8633 end if;
8635 -- Skip the first two access-to-dispatch-table pointers since they
8636 -- leads to the primary dispatch table (predefined DT and user
8637 -- defined DT). We are only concerned with the secondary dispatch
8638 -- table pointers. Note that the access-to- dispatch-table pointer
8639 -- corresponds to the first implemented interface retrieved below.
8641 Iface_DT_Ptr :=
8642 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
8644 while Present (Iface_DT_Ptr)
8645 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
8646 loop
8647 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8648 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
8650 if Present (Thunk_Code) then
8651 Insert_Actions_After (N, New_List (
8652 Thunk_Code,
8654 Build_Set_Predefined_Prim_Op_Address (Loc,
8655 Tag_Node =>
8656 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
8657 Position => DT_Position (Prim),
8658 Address_Node =>
8659 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8660 Make_Attribute_Reference (Loc,
8661 Prefix => New_Reference_To (Thunk_Id, Loc),
8662 Attribute_Name => Name_Unrestricted_Access))),
8664 Build_Set_Predefined_Prim_Op_Address (Loc,
8665 Tag_Node =>
8666 New_Reference_To
8667 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
8668 Loc),
8669 Position => DT_Position (Prim),
8670 Address_Node =>
8671 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8672 Make_Attribute_Reference (Loc,
8673 Prefix => New_Reference_To (Prim, Loc),
8674 Attribute_Name => Name_Unrestricted_Access)))));
8675 end if;
8677 -- Skip the tag of the predefined primitives dispatch table
8679 Next_Elmt (Iface_DT_Ptr);
8680 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8682 -- Skip tag of the no-thunks dispatch table
8684 Next_Elmt (Iface_DT_Ptr);
8685 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8687 -- Skip tag of predefined primitives no-thunks dispatch table
8689 Next_Elmt (Iface_DT_Ptr);
8690 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8692 Next_Elmt (Iface_DT_Ptr);
8693 end loop;
8694 end Register_Predefined_DT_Entry;
8696 -- Local variables
8698 Subp : constant Entity_Id := Entity (N);
8700 -- Start of processing for Freeze_Subprogram
8702 begin
8703 -- We suppress the initialization of the dispatch table entry when
8704 -- VM_Target because the dispatching mechanism is handled internally
8705 -- by the VM.
8707 if Is_Dispatching_Operation (Subp)
8708 and then not Is_Abstract_Subprogram (Subp)
8709 and then Present (DTC_Entity (Subp))
8710 and then Present (Scope (DTC_Entity (Subp)))
8711 and then Tagged_Type_Expansion
8712 and then not Restriction_Active (No_Dispatching_Calls)
8713 and then RTE_Available (RE_Tag)
8714 then
8715 declare
8716 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
8718 begin
8719 -- Handle private overridden primitives
8721 if not Is_CPP_Class (Typ) then
8722 Check_Overriding_Operation (Subp);
8723 end if;
8725 -- We assume that imported CPP primitives correspond with objects
8726 -- whose constructor is in the CPP side; therefore we don't need
8727 -- to generate code to register them in the dispatch table.
8729 if Is_CPP_Class (Typ) then
8730 null;
8732 -- Handle CPP primitives found in derivations of CPP_Class types.
8733 -- These primitives must have been inherited from some parent, and
8734 -- there is no need to register them in the dispatch table because
8735 -- Build_Inherit_Prims takes care of the initialization of these
8736 -- slots.
8738 elsif Is_Imported (Subp)
8739 and then (Convention (Subp) = Convention_CPP
8740 or else Convention (Subp) = Convention_C)
8741 then
8742 null;
8744 -- Generate code to register the primitive in non statically
8745 -- allocated dispatch tables
8747 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8749 -- When a primitive is frozen, enter its name in its dispatch
8750 -- table slot.
8752 if not Is_Interface (Typ)
8753 or else Present (Interface_Alias (Subp))
8754 then
8755 if Is_Predefined_Dispatching_Operation (Subp) then
8756 Register_Predefined_DT_Entry (Subp);
8757 end if;
8759 Insert_Actions_After (N,
8760 Register_Primitive (Loc, Prim => Subp));
8761 end if;
8762 end if;
8763 end;
8764 end if;
8766 -- Mark functions that return by reference. Note that it cannot be part
8767 -- of the normal semantic analysis of the spec since the underlying
8768 -- returned type may not be known yet (for private types).
8770 declare
8771 Typ : constant Entity_Id := Etype (Subp);
8772 Utyp : constant Entity_Id := Underlying_Type (Typ);
8773 begin
8774 if Is_Immutably_Limited_Type (Typ) then
8775 Set_Returns_By_Ref (Subp);
8776 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8777 Set_Returns_By_Ref (Subp);
8778 end if;
8779 end;
8781 -- Wnen freezing a null procedure, analyze its delayed aspects now
8782 -- because we may not have reached the end of the declarative list when
8783 -- delayed aspects are normally analyzed. This ensures that dispatching
8784 -- calls are properly rewritten when the generated _Postcondition
8785 -- procedure is analyzed in the null procedure body.
8787 if Nkind (Parent (Subp)) = N_Procedure_Specification
8788 and then Null_Present (Parent (Subp))
8789 then
8790 Analyze_Subprogram_Contract (Subp);
8791 end if;
8792 end Freeze_Subprogram;
8794 -----------------------
8795 -- Is_Null_Procedure --
8796 -----------------------
8798 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8799 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8801 begin
8802 if Ekind (Subp) /= E_Procedure then
8803 return False;
8805 -- Check if this is a declared null procedure
8807 elsif Nkind (Decl) = N_Subprogram_Declaration then
8808 if not Null_Present (Specification (Decl)) then
8809 return False;
8811 elsif No (Body_To_Inline (Decl)) then
8812 return False;
8814 -- Check if the body contains only a null statement, followed by
8815 -- the return statement added during expansion.
8817 else
8818 declare
8819 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8821 Stat : Node_Id;
8822 Stat2 : Node_Id;
8824 begin
8825 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8826 return False;
8827 else
8828 -- We must skip SCIL nodes because they are currently
8829 -- implemented as special N_Null_Statement nodes.
8831 Stat :=
8832 First_Non_SCIL_Node
8833 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8834 Stat2 := Next_Non_SCIL_Node (Stat);
8836 return
8837 Is_Empty_List (Declarations (Orig_Bod))
8838 and then Nkind (Stat) = N_Null_Statement
8839 and then
8840 (No (Stat2)
8841 or else
8842 (Nkind (Stat2) = N_Simple_Return_Statement
8843 and then No (Next (Stat2))));
8844 end if;
8845 end;
8846 end if;
8848 else
8849 return False;
8850 end if;
8851 end Is_Null_Procedure;
8853 -------------------------------------------
8854 -- Make_Build_In_Place_Call_In_Allocator --
8855 -------------------------------------------
8857 procedure Make_Build_In_Place_Call_In_Allocator
8858 (Allocator : Node_Id;
8859 Function_Call : Node_Id)
8861 Acc_Type : constant Entity_Id := Etype (Allocator);
8862 Loc : Source_Ptr;
8863 Func_Call : Node_Id := Function_Call;
8864 Function_Id : Entity_Id;
8865 Result_Subt : Entity_Id;
8866 New_Allocator : Node_Id;
8867 Return_Obj_Access : Entity_Id;
8869 begin
8870 -- Step past qualification or unchecked conversion (the latter can occur
8871 -- in cases of calls to 'Input).
8873 if Nkind_In (Func_Call,
8874 N_Qualified_Expression,
8875 N_Unchecked_Type_Conversion)
8876 then
8877 Func_Call := Expression (Func_Call);
8878 end if;
8880 -- If the call has already been processed to add build-in-place actuals
8881 -- then return. This should not normally occur in an allocator context,
8882 -- but we add the protection as a defensive measure.
8884 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8885 return;
8886 end if;
8888 -- Mark the call as processed as a build-in-place call
8890 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8892 Loc := Sloc (Function_Call);
8894 if Is_Entity_Name (Name (Func_Call)) then
8895 Function_Id := Entity (Name (Func_Call));
8897 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8898 Function_Id := Etype (Name (Func_Call));
8900 else
8901 raise Program_Error;
8902 end if;
8904 Result_Subt := Available_View (Etype (Function_Id));
8906 -- Check whether return type includes tasks. This may not have been done
8907 -- previously, if the type was a limited view.
8909 if Has_Task (Result_Subt) then
8910 Build_Activation_Chain_Entity (Allocator);
8911 end if;
8913 -- When the result subtype is constrained, the return object must be
8914 -- allocated on the caller side, and access to it is passed to the
8915 -- function.
8917 -- Here and in related routines, we must examine the full view of the
8918 -- type, because the view at the point of call may differ from that
8919 -- that in the function body, and the expansion mechanism depends on
8920 -- the characteristics of the full view.
8922 if Is_Constrained (Underlying_Type (Result_Subt)) then
8924 -- Replace the initialized allocator of form "new T'(Func (...))"
8925 -- with an uninitialized allocator of form "new T", where T is the
8926 -- result subtype of the called function. The call to the function
8927 -- is handled separately further below.
8929 New_Allocator :=
8930 Make_Allocator (Loc,
8931 Expression => New_Reference_To (Result_Subt, Loc));
8932 Set_No_Initialization (New_Allocator);
8934 -- Copy attributes to new allocator. Note that the new allocator
8935 -- logically comes from source if the original one did, so copy the
8936 -- relevant flag. This ensures proper treatment of the restriction
8937 -- No_Implicit_Heap_Allocations in this case.
8939 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8940 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8941 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8943 Rewrite (Allocator, New_Allocator);
8945 -- Create a new access object and initialize it to the result of the
8946 -- new uninitialized allocator. Note: we do not use Allocator as the
8947 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
8948 -- as this would create a sort of infinite "recursion".
8950 Return_Obj_Access := Make_Temporary (Loc, 'R');
8951 Set_Etype (Return_Obj_Access, Acc_Type);
8953 Insert_Action (Allocator,
8954 Make_Object_Declaration (Loc,
8955 Defining_Identifier => Return_Obj_Access,
8956 Object_Definition => New_Reference_To (Acc_Type, Loc),
8957 Expression => Relocate_Node (Allocator)));
8959 -- When the function has a controlling result, an allocation-form
8960 -- parameter must be passed indicating that the caller is allocating
8961 -- the result object. This is needed because such a function can be
8962 -- called as a dispatching operation and must be treated similarly
8963 -- to functions with unconstrained result subtypes.
8965 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8966 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8968 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8969 (Func_Call, Function_Id, Acc_Type);
8971 Add_Task_Actuals_To_Build_In_Place_Call
8972 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
8974 -- Add an implicit actual to the function call that provides access
8975 -- to the allocated object. An unchecked conversion to the (specific)
8976 -- result subtype of the function is inserted to handle cases where
8977 -- the access type of the allocator has a class-wide designated type.
8979 Add_Access_Actual_To_Build_In_Place_Call
8980 (Func_Call,
8981 Function_Id,
8982 Make_Unchecked_Type_Conversion (Loc,
8983 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
8984 Expression =>
8985 Make_Explicit_Dereference (Loc,
8986 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
8988 -- When the result subtype is unconstrained, the function itself must
8989 -- perform the allocation of the return object, so we pass parameters
8990 -- indicating that. We don't yet handle the case where the allocation
8991 -- must be done in a user-defined storage pool, which will require
8992 -- passing another actual or two to provide allocation/deallocation
8993 -- operations. ???
8995 else
8996 -- Case of a user-defined storage pool. Pass an allocation parameter
8997 -- indicating that the function should allocate its result in the
8998 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8999 -- pool may not be aliased.
9001 if VM_Target = No_VM
9002 and then Present (Associated_Storage_Pool (Acc_Type))
9003 then
9004 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9005 (Func_Call, Function_Id, Alloc_Form => User_Storage_Pool,
9006 Pool_Actual =>
9007 Make_Attribute_Reference (Loc,
9008 Prefix =>
9009 New_Reference_To
9010 (Associated_Storage_Pool (Acc_Type), Loc),
9011 Attribute_Name => Name_Unrestricted_Access));
9013 -- No user-defined pool; pass an allocation parameter indicating that
9014 -- the function should allocate its result on the heap.
9016 else
9017 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9018 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
9019 end if;
9021 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9022 (Func_Call, Function_Id, Acc_Type);
9024 Add_Task_Actuals_To_Build_In_Place_Call
9025 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
9027 -- The caller does not provide the return object in this case, so we
9028 -- have to pass null for the object access actual.
9030 Add_Access_Actual_To_Build_In_Place_Call
9031 (Func_Call, Function_Id, Return_Object => Empty);
9032 end if;
9034 -- If the build-in-place function call returns a controlled object,
9035 -- the finalization master will require a reference to routine
9036 -- Finalize_Address of the designated type. Setting this attribute
9037 -- is done in the same manner to expansion of allocators.
9039 if Needs_Finalization (Result_Subt) then
9041 -- Controlled types with supressed finalization do not need to
9042 -- associate the address of their Finalize_Address primitives with
9043 -- a master since they do not need a master to begin with.
9045 if Is_Library_Level_Entity (Acc_Type)
9046 and then Finalize_Storage_Only (Result_Subt)
9047 then
9048 null;
9050 -- Do not generate the call to Set_Finalize_Address in SPARK mode
9051 -- because it is not necessary and results in unwanted expansion.
9052 -- This expansion is also not carried out in CodePeer mode because
9053 -- Finalize_Address is never built.
9055 elsif not SPARK_Mode
9056 and then not CodePeer_Mode
9057 then
9058 Insert_Action (Allocator,
9059 Make_Set_Finalize_Address_Call (Loc,
9060 Typ => Etype (Function_Id),
9061 Ptr_Typ => Acc_Type));
9062 end if;
9063 end if;
9065 -- Finally, replace the allocator node with a reference to the result
9066 -- of the function call itself (which will effectively be an access
9067 -- to the object created by the allocator).
9069 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
9071 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9072 -- generate an implicit conversion to force displacement of the "this"
9073 -- pointer.
9075 if Is_Interface (Designated_Type (Acc_Type)) then
9076 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9077 end if;
9079 Analyze_And_Resolve (Allocator, Acc_Type);
9080 end Make_Build_In_Place_Call_In_Allocator;
9082 ---------------------------------------------------
9083 -- Make_Build_In_Place_Call_In_Anonymous_Context --
9084 ---------------------------------------------------
9086 procedure Make_Build_In_Place_Call_In_Anonymous_Context
9087 (Function_Call : Node_Id)
9089 Loc : Source_Ptr;
9090 Func_Call : Node_Id := Function_Call;
9091 Function_Id : Entity_Id;
9092 Result_Subt : Entity_Id;
9093 Return_Obj_Id : Entity_Id;
9094 Return_Obj_Decl : Entity_Id;
9096 begin
9097 -- Step past qualification or unchecked conversion (the latter can occur
9098 -- in cases of calls to 'Input).
9100 if Nkind_In (Func_Call, N_Qualified_Expression,
9101 N_Unchecked_Type_Conversion)
9102 then
9103 Func_Call := Expression (Func_Call);
9104 end if;
9106 -- If the call has already been processed to add build-in-place actuals
9107 -- then return. One place this can occur is for calls to build-in-place
9108 -- functions that occur within a call to a protected operation, where
9109 -- due to rewriting and expansion of the protected call there can be
9110 -- more than one call to Expand_Actuals for the same set of actuals.
9112 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9113 return;
9114 end if;
9116 -- Mark the call as processed as a build-in-place call
9118 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9120 Loc := Sloc (Function_Call);
9122 if Is_Entity_Name (Name (Func_Call)) then
9123 Function_Id := Entity (Name (Func_Call));
9125 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9126 Function_Id := Etype (Name (Func_Call));
9128 else
9129 raise Program_Error;
9130 end if;
9132 Result_Subt := Etype (Function_Id);
9134 -- If the build-in-place function returns a controlled object, then the
9135 -- object needs to be finalized immediately after the context. Since
9136 -- this case produces a transient scope, the servicing finalizer needs
9137 -- to name the returned object. Create a temporary which is initialized
9138 -- with the function call:
9140 -- Temp_Id : Func_Type := BIP_Func_Call;
9142 -- The initialization expression of the temporary will be rewritten by
9143 -- the expander using the appropriate mechanism in Make_Build_In_Place_
9144 -- Call_In_Object_Declaration.
9146 if Needs_Finalization (Result_Subt) then
9147 declare
9148 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
9149 Temp_Decl : Node_Id;
9151 begin
9152 -- Reset the guard on the function call since the following does
9153 -- not perform actual call expansion.
9155 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
9157 Temp_Decl :=
9158 Make_Object_Declaration (Loc,
9159 Defining_Identifier => Temp_Id,
9160 Object_Definition =>
9161 New_Reference_To (Result_Subt, Loc),
9162 Expression =>
9163 New_Copy_Tree (Function_Call));
9165 Insert_Action (Function_Call, Temp_Decl);
9167 Rewrite (Function_Call, New_Reference_To (Temp_Id, Loc));
9168 Analyze (Function_Call);
9169 end;
9171 -- When the result subtype is constrained, an object of the subtype is
9172 -- declared and an access value designating it is passed as an actual.
9174 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
9176 -- Create a temporary object to hold the function result
9178 Return_Obj_Id := Make_Temporary (Loc, 'R');
9179 Set_Etype (Return_Obj_Id, Result_Subt);
9181 Return_Obj_Decl :=
9182 Make_Object_Declaration (Loc,
9183 Defining_Identifier => Return_Obj_Id,
9184 Aliased_Present => True,
9185 Object_Definition => New_Reference_To (Result_Subt, Loc));
9187 Set_No_Initialization (Return_Obj_Decl);
9189 Insert_Action (Func_Call, Return_Obj_Decl);
9191 -- When the function has a controlling result, an allocation-form
9192 -- parameter must be passed indicating that the caller is allocating
9193 -- the result object. This is needed because such a function can be
9194 -- called as a dispatching operation and must be treated similarly
9195 -- to functions with unconstrained result subtypes.
9197 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9198 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9200 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9201 (Func_Call, Function_Id);
9203 Add_Task_Actuals_To_Build_In_Place_Call
9204 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9206 -- Add an implicit actual to the function call that provides access
9207 -- to the caller's return object.
9209 Add_Access_Actual_To_Build_In_Place_Call
9210 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
9212 -- When the result subtype is unconstrained, the function must allocate
9213 -- the return object in the secondary stack, so appropriate implicit
9214 -- parameters are added to the call to indicate that. A transient
9215 -- scope is established to ensure eventual cleanup of the result.
9217 else
9218 -- Pass an allocation parameter indicating that the function should
9219 -- allocate its result on the secondary stack.
9221 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9222 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9224 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9225 (Func_Call, Function_Id);
9227 Add_Task_Actuals_To_Build_In_Place_Call
9228 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9230 -- Pass a null value to the function since no return object is
9231 -- available on the caller side.
9233 Add_Access_Actual_To_Build_In_Place_Call
9234 (Func_Call, Function_Id, Empty);
9235 end if;
9236 end Make_Build_In_Place_Call_In_Anonymous_Context;
9238 --------------------------------------------
9239 -- Make_Build_In_Place_Call_In_Assignment --
9240 --------------------------------------------
9242 procedure Make_Build_In_Place_Call_In_Assignment
9243 (Assign : Node_Id;
9244 Function_Call : Node_Id)
9246 Lhs : constant Node_Id := Name (Assign);
9247 Func_Call : Node_Id := Function_Call;
9248 Func_Id : Entity_Id;
9249 Loc : Source_Ptr;
9250 Obj_Decl : Node_Id;
9251 Obj_Id : Entity_Id;
9252 Ptr_Typ : Entity_Id;
9253 Ptr_Typ_Decl : Node_Id;
9254 New_Expr : Node_Id;
9255 Result_Subt : Entity_Id;
9256 Target : Node_Id;
9258 begin
9259 -- Step past qualification or unchecked conversion (the latter can occur
9260 -- in cases of calls to 'Input).
9262 if Nkind_In (Func_Call, N_Qualified_Expression,
9263 N_Unchecked_Type_Conversion)
9264 then
9265 Func_Call := Expression (Func_Call);
9266 end if;
9268 -- If the call has already been processed to add build-in-place actuals
9269 -- then return. This should not normally occur in an assignment context,
9270 -- but we add the protection as a defensive measure.
9272 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9273 return;
9274 end if;
9276 -- Mark the call as processed as a build-in-place call
9278 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9280 Loc := Sloc (Function_Call);
9282 if Is_Entity_Name (Name (Func_Call)) then
9283 Func_Id := Entity (Name (Func_Call));
9285 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9286 Func_Id := Etype (Name (Func_Call));
9288 else
9289 raise Program_Error;
9290 end if;
9292 Result_Subt := Etype (Func_Id);
9294 -- When the result subtype is unconstrained, an additional actual must
9295 -- be passed to indicate that the caller is providing the return object.
9296 -- This parameter must also be passed when the called function has a
9297 -- controlling result, because dispatching calls to the function needs
9298 -- to be treated effectively the same as calls to class-wide functions.
9300 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9301 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
9303 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9304 (Func_Call, Func_Id);
9306 Add_Task_Actuals_To_Build_In_Place_Call
9307 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
9309 -- Add an implicit actual to the function call that provides access to
9310 -- the caller's return object.
9312 Add_Access_Actual_To_Build_In_Place_Call
9313 (Func_Call,
9314 Func_Id,
9315 Make_Unchecked_Type_Conversion (Loc,
9316 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
9317 Expression => Relocate_Node (Lhs)));
9319 -- Create an access type designating the function's result subtype
9321 Ptr_Typ := Make_Temporary (Loc, 'A');
9323 Ptr_Typ_Decl :=
9324 Make_Full_Type_Declaration (Loc,
9325 Defining_Identifier => Ptr_Typ,
9326 Type_Definition =>
9327 Make_Access_To_Object_Definition (Loc,
9328 All_Present => True,
9329 Subtype_Indication =>
9330 New_Reference_To (Result_Subt, Loc)));
9331 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
9333 -- Finally, create an access object initialized to a reference to the
9334 -- function call. We know this access value is non-null, so mark the
9335 -- entity accordingly to suppress junk access checks.
9337 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
9339 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
9340 Set_Etype (Obj_Id, Ptr_Typ);
9341 Set_Is_Known_Non_Null (Obj_Id);
9343 Obj_Decl :=
9344 Make_Object_Declaration (Loc,
9345 Defining_Identifier => Obj_Id,
9346 Object_Definition => New_Reference_To (Ptr_Typ, Loc),
9347 Expression => New_Expr);
9348 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
9350 Rewrite (Assign, Make_Null_Statement (Loc));
9352 -- Retrieve the target of the assignment
9354 if Nkind (Lhs) = N_Selected_Component then
9355 Target := Selector_Name (Lhs);
9356 elsif Nkind (Lhs) = N_Type_Conversion then
9357 Target := Expression (Lhs);
9358 else
9359 Target := Lhs;
9360 end if;
9362 -- If we are assigning to a return object or this is an expression of
9363 -- an extension aggregate, the target should either be an identifier
9364 -- or a simple expression. All other cases imply a different scenario.
9366 if Nkind (Target) in N_Has_Entity then
9367 Target := Entity (Target);
9368 else
9369 return;
9370 end if;
9371 end Make_Build_In_Place_Call_In_Assignment;
9373 ----------------------------------------------------
9374 -- Make_Build_In_Place_Call_In_Object_Declaration --
9375 ----------------------------------------------------
9377 procedure Make_Build_In_Place_Call_In_Object_Declaration
9378 (Object_Decl : Node_Id;
9379 Function_Call : Node_Id)
9381 Loc : Source_Ptr;
9382 Obj_Def_Id : constant Entity_Id :=
9383 Defining_Identifier (Object_Decl);
9384 Enclosing_Func : constant Entity_Id :=
9385 Enclosing_Subprogram (Obj_Def_Id);
9386 Call_Deref : Node_Id;
9387 Caller_Object : Node_Id;
9388 Def_Id : Entity_Id;
9389 Fmaster_Actual : Node_Id := Empty;
9390 Func_Call : Node_Id := Function_Call;
9391 Function_Id : Entity_Id;
9392 Pool_Actual : Node_Id;
9393 Ptr_Typ_Decl : Node_Id;
9394 Pass_Caller_Acc : Boolean := False;
9395 New_Expr : Node_Id;
9396 Ref_Type : Entity_Id;
9397 Result_Subt : Entity_Id;
9399 begin
9400 -- Step past qualification or unchecked conversion (the latter can occur
9401 -- in cases of calls to 'Input).
9403 if Nkind_In (Func_Call, N_Qualified_Expression,
9404 N_Unchecked_Type_Conversion)
9405 then
9406 Func_Call := Expression (Func_Call);
9407 end if;
9409 -- If the call has already been processed to add build-in-place actuals
9410 -- then return. This should not normally occur in an object declaration,
9411 -- but we add the protection as a defensive measure.
9413 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9414 return;
9415 end if;
9417 -- Mark the call as processed as a build-in-place call
9419 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9421 Loc := Sloc (Function_Call);
9423 if Is_Entity_Name (Name (Func_Call)) then
9424 Function_Id := Entity (Name (Func_Call));
9426 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9427 Function_Id := Etype (Name (Func_Call));
9429 else
9430 raise Program_Error;
9431 end if;
9433 Result_Subt := Etype (Function_Id);
9435 -- If the the object is a return object of an enclosing build-in-place
9436 -- function, then the implicit build-in-place parameters of the
9437 -- enclosing function are simply passed along to the called function.
9438 -- (Unfortunately, this won't cover the case of extension aggregates
9439 -- where the ancestor part is a build-in-place unconstrained function
9440 -- call that should be passed along the caller's parameters. Currently
9441 -- those get mishandled by reassigning the result of the call to the
9442 -- aggregate return object, when the call result should really be
9443 -- directly built in place in the aggregate and not in a temporary. ???)
9445 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
9446 Pass_Caller_Acc := True;
9448 -- When the enclosing function has a BIP_Alloc_Form formal then we
9449 -- pass it along to the callee (such as when the enclosing function
9450 -- has an unconstrained or tagged result type).
9452 if Needs_BIP_Alloc_Form (Enclosing_Func) then
9453 if VM_Target = No_VM and then
9454 RTE_Available (RE_Root_Storage_Pool_Ptr)
9455 then
9456 Pool_Actual :=
9457 New_Reference_To (Build_In_Place_Formal
9458 (Enclosing_Func, BIP_Storage_Pool), Loc);
9460 -- The build-in-place pool formal is not built on .NET/JVM
9462 else
9463 Pool_Actual := Empty;
9464 end if;
9466 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9467 (Func_Call,
9468 Function_Id,
9469 Alloc_Form_Exp =>
9470 New_Reference_To
9471 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
9472 Loc),
9473 Pool_Actual => Pool_Actual);
9475 -- Otherwise, if enclosing function has a constrained result subtype,
9476 -- then caller allocation will be used.
9478 else
9479 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9480 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9481 end if;
9483 if Needs_BIP_Finalization_Master (Enclosing_Func) then
9484 Fmaster_Actual :=
9485 New_Reference_To
9486 (Build_In_Place_Formal
9487 (Enclosing_Func, BIP_Finalization_Master), Loc);
9488 end if;
9490 -- Retrieve the BIPacc formal from the enclosing function and convert
9491 -- it to the access type of the callee's BIP_Object_Access formal.
9493 Caller_Object :=
9494 Make_Unchecked_Type_Conversion (Loc,
9495 Subtype_Mark =>
9496 New_Reference_To
9497 (Etype
9498 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
9499 Loc),
9500 Expression =>
9501 New_Reference_To
9502 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
9503 Loc));
9505 -- In the constrained case, add an implicit actual to the function call
9506 -- that provides access to the declared object. An unchecked conversion
9507 -- to the (specific) result type of the function is inserted to handle
9508 -- the case where the object is declared with a class-wide type.
9510 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
9511 Caller_Object :=
9512 Make_Unchecked_Type_Conversion (Loc,
9513 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
9514 Expression => New_Reference_To (Obj_Def_Id, Loc));
9516 -- When the function has a controlling result, an allocation-form
9517 -- parameter must be passed indicating that the caller is allocating
9518 -- the result object. This is needed because such a function can be
9519 -- called as a dispatching operation and must be treated similarly
9520 -- to functions with unconstrained result subtypes.
9522 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9523 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9525 -- In other unconstrained cases, pass an indication to do the allocation
9526 -- on the secondary stack and set Caller_Object to Empty so that a null
9527 -- value will be passed for the caller's object address. A transient
9528 -- scope is established to ensure eventual cleanup of the result.
9530 else
9531 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9532 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9533 Caller_Object := Empty;
9535 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
9536 end if;
9538 -- Pass along any finalization master actual, which is needed in the
9539 -- case where the called function initializes a return object of an
9540 -- enclosing build-in-place function.
9542 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9543 (Func_Call => Func_Call,
9544 Func_Id => Function_Id,
9545 Master_Exp => Fmaster_Actual);
9547 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
9548 and then Has_Task (Result_Subt)
9549 then
9550 -- Here we're passing along the master that was passed in to this
9551 -- function.
9553 Add_Task_Actuals_To_Build_In_Place_Call
9554 (Func_Call, Function_Id,
9555 Master_Actual =>
9556 New_Reference_To (Build_In_Place_Formal
9557 (Enclosing_Func, BIP_Task_Master), Loc));
9559 else
9560 Add_Task_Actuals_To_Build_In_Place_Call
9561 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9562 end if;
9564 Add_Access_Actual_To_Build_In_Place_Call
9565 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
9567 -- Create an access type designating the function's result subtype. We
9568 -- use the type of the original expression because it may be a call to
9569 -- an inherited operation, which the expansion has replaced with the
9570 -- parent operation that yields the parent type.
9572 Ref_Type := Make_Temporary (Loc, 'A');
9574 Ptr_Typ_Decl :=
9575 Make_Full_Type_Declaration (Loc,
9576 Defining_Identifier => Ref_Type,
9577 Type_Definition =>
9578 Make_Access_To_Object_Definition (Loc,
9579 All_Present => True,
9580 Subtype_Indication =>
9581 New_Reference_To (Etype (Function_Call), Loc)));
9583 -- The access type and its accompanying object must be inserted after
9584 -- the object declaration in the constrained case, so that the function
9585 -- call can be passed access to the object. In the unconstrained case,
9586 -- or if the object declaration is for a return object, the access type
9587 -- and object must be inserted before the object, since the object
9588 -- declaration is rewritten to be a renaming of a dereference of the
9589 -- access object.
9591 if Is_Constrained (Underlying_Type (Result_Subt))
9592 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9593 then
9594 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
9595 else
9596 Insert_Action (Object_Decl, Ptr_Typ_Decl);
9597 end if;
9599 -- Finally, create an access object initialized to a reference to the
9600 -- function call. We know this access value cannot be null, so mark the
9601 -- entity accordingly to suppress the access check.
9603 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
9605 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
9606 Set_Etype (Def_Id, Ref_Type);
9607 Set_Is_Known_Non_Null (Def_Id);
9609 Insert_After_And_Analyze (Ptr_Typ_Decl,
9610 Make_Object_Declaration (Loc,
9611 Defining_Identifier => Def_Id,
9612 Object_Definition => New_Reference_To (Ref_Type, Loc),
9613 Expression => New_Expr));
9615 -- If the result subtype of the called function is constrained and
9616 -- is not itself the return expression of an enclosing BIP function,
9617 -- then mark the object as having no initialization.
9619 if Is_Constrained (Underlying_Type (Result_Subt))
9620 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9621 then
9622 Set_Expression (Object_Decl, Empty);
9623 Set_No_Initialization (Object_Decl);
9625 -- In case of an unconstrained result subtype, or if the call is the
9626 -- return expression of an enclosing BIP function, rewrite the object
9627 -- declaration as an object renaming where the renamed object is a
9628 -- dereference of <function_Call>'reference:
9630 -- Obj : Subt renames <function_call>'Ref.all;
9632 else
9633 Call_Deref :=
9634 Make_Explicit_Dereference (Loc,
9635 Prefix => New_Reference_To (Def_Id, Loc));
9637 Loc := Sloc (Object_Decl);
9638 Rewrite (Object_Decl,
9639 Make_Object_Renaming_Declaration (Loc,
9640 Defining_Identifier => Make_Temporary (Loc, 'D'),
9641 Access_Definition => Empty,
9642 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9643 Name => Call_Deref));
9645 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
9647 Analyze (Object_Decl);
9649 -- Replace the internal identifier of the renaming declaration's
9650 -- entity with identifier of the original object entity. We also have
9651 -- to exchange the entities containing their defining identifiers to
9652 -- ensure the correct replacement of the object declaration by the
9653 -- object renaming declaration to avoid homograph conflicts (since
9654 -- the object declaration's defining identifier was already entered
9655 -- in current scope). The Next_Entity links of the two entities also
9656 -- have to be swapped since the entities are part of the return
9657 -- scope's entity list and the list structure would otherwise be
9658 -- corrupted. Finally, the homonym chain must be preserved as well.
9660 declare
9661 Renaming_Def_Id : constant Entity_Id :=
9662 Defining_Identifier (Object_Decl);
9663 Next_Entity_Temp : constant Entity_Id :=
9664 Next_Entity (Renaming_Def_Id);
9665 begin
9666 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
9668 -- Swap next entity links in preparation for exchanging entities
9670 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9671 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9672 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9674 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9676 -- Preserve source indication of original declaration, so that
9677 -- xref information is properly generated for the right entity.
9679 Preserve_Comes_From_Source
9680 (Object_Decl, Original_Node (Object_Decl));
9682 Preserve_Comes_From_Source
9683 (Obj_Def_Id, Original_Node (Object_Decl));
9685 Set_Comes_From_Source (Renaming_Def_Id, False);
9686 end;
9687 end if;
9689 -- If the object entity has a class-wide Etype, then we need to change
9690 -- it to the result subtype of the function call, because otherwise the
9691 -- object will be class-wide without an explicit initialization and
9692 -- won't be allocated properly by the back end. It seems unclean to make
9693 -- such a revision to the type at this point, and we should try to
9694 -- improve this treatment when build-in-place functions with class-wide
9695 -- results are implemented. ???
9697 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9698 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9699 end if;
9700 end Make_Build_In_Place_Call_In_Object_Declaration;
9702 --------------------------------------------
9703 -- Make_CPP_Constructor_Call_In_Allocator --
9704 --------------------------------------------
9706 procedure Make_CPP_Constructor_Call_In_Allocator
9707 (Allocator : Node_Id;
9708 Function_Call : Node_Id)
9710 Loc : constant Source_Ptr := Sloc (Function_Call);
9711 Acc_Type : constant Entity_Id := Etype (Allocator);
9712 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9713 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9715 New_Allocator : Node_Id;
9716 Return_Obj_Access : Entity_Id;
9717 Tmp_Obj : Node_Id;
9719 begin
9720 pragma Assert (Nkind (Allocator) = N_Allocator
9721 and then Nkind (Function_Call) = N_Function_Call);
9722 pragma Assert (Convention (Function_Id) = Convention_CPP
9723 and then Is_Constructor (Function_Id));
9724 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9726 -- Replace the initialized allocator of form "new T'(Func (...))" with
9727 -- an uninitialized allocator of form "new T", where T is the result
9728 -- subtype of the called function. The call to the function is handled
9729 -- separately further below.
9731 New_Allocator :=
9732 Make_Allocator (Loc,
9733 Expression => New_Reference_To (Result_Subt, Loc));
9734 Set_No_Initialization (New_Allocator);
9736 -- Copy attributes to new allocator. Note that the new allocator
9737 -- logically comes from source if the original one did, so copy the
9738 -- relevant flag. This ensures proper treatment of the restriction
9739 -- No_Implicit_Heap_Allocations in this case.
9741 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9742 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9743 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9745 Rewrite (Allocator, New_Allocator);
9747 -- Create a new access object and initialize it to the result of the
9748 -- new uninitialized allocator. Note: we do not use Allocator as the
9749 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9750 -- as this would create a sort of infinite "recursion".
9752 Return_Obj_Access := Make_Temporary (Loc, 'R');
9753 Set_Etype (Return_Obj_Access, Acc_Type);
9755 -- Generate:
9756 -- Rnnn : constant ptr_T := new (T);
9757 -- Init (Rnn.all,...);
9759 Tmp_Obj :=
9760 Make_Object_Declaration (Loc,
9761 Defining_Identifier => Return_Obj_Access,
9762 Constant_Present => True,
9763 Object_Definition => New_Reference_To (Acc_Type, Loc),
9764 Expression => Relocate_Node (Allocator));
9765 Insert_Action (Allocator, Tmp_Obj);
9767 Insert_List_After_And_Analyze (Tmp_Obj,
9768 Build_Initialization_Call (Loc,
9769 Id_Ref =>
9770 Make_Explicit_Dereference (Loc,
9771 Prefix => New_Reference_To (Return_Obj_Access, Loc)),
9772 Typ => Etype (Function_Id),
9773 Constructor_Ref => Function_Call));
9775 -- Finally, replace the allocator node with a reference to the result of
9776 -- the function call itself (which will effectively be an access to the
9777 -- object created by the allocator).
9779 Rewrite (Allocator, New_Reference_To (Return_Obj_Access, Loc));
9781 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9782 -- generate an implicit conversion to force displacement of the "this"
9783 -- pointer.
9785 if Is_Interface (Designated_Type (Acc_Type)) then
9786 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9787 end if;
9789 Analyze_And_Resolve (Allocator, Acc_Type);
9790 end Make_CPP_Constructor_Call_In_Allocator;
9792 -----------------------------------
9793 -- Needs_BIP_Finalization_Master --
9794 -----------------------------------
9796 function Needs_BIP_Finalization_Master
9797 (Func_Id : Entity_Id) return Boolean
9799 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9800 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9801 begin
9802 return
9803 not Restriction_Active (No_Finalization)
9804 and then Needs_Finalization (Func_Typ);
9805 end Needs_BIP_Finalization_Master;
9807 --------------------------
9808 -- Needs_BIP_Alloc_Form --
9809 --------------------------
9811 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9812 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9813 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9814 begin
9815 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9816 end Needs_BIP_Alloc_Form;
9818 --------------------------------------
9819 -- Needs_Result_Accessibility_Level --
9820 --------------------------------------
9822 function Needs_Result_Accessibility_Level
9823 (Func_Id : Entity_Id) return Boolean
9825 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9827 function Has_Unconstrained_Access_Discriminant_Component
9828 (Comp_Typ : Entity_Id) return Boolean;
9829 -- Returns True if any component of the type has an unconstrained access
9830 -- discriminant.
9832 -----------------------------------------------------
9833 -- Has_Unconstrained_Access_Discriminant_Component --
9834 -----------------------------------------------------
9836 function Has_Unconstrained_Access_Discriminant_Component
9837 (Comp_Typ : Entity_Id) return Boolean
9839 begin
9840 if not Is_Limited_Type (Comp_Typ) then
9841 return False;
9843 -- Only limited types can have access discriminants with
9844 -- defaults.
9846 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9847 return True;
9849 elsif Is_Array_Type (Comp_Typ) then
9850 return Has_Unconstrained_Access_Discriminant_Component
9851 (Underlying_Type (Component_Type (Comp_Typ)));
9853 elsif Is_Record_Type (Comp_Typ) then
9854 declare
9855 Comp : Entity_Id;
9857 begin
9858 Comp := First_Component (Comp_Typ);
9859 while Present (Comp) loop
9860 if Has_Unconstrained_Access_Discriminant_Component
9861 (Underlying_Type (Etype (Comp)))
9862 then
9863 return True;
9864 end if;
9866 Next_Component (Comp);
9867 end loop;
9868 end;
9869 end if;
9871 return False;
9872 end Has_Unconstrained_Access_Discriminant_Component;
9874 Feature_Disabled : constant Boolean := True;
9875 -- Temporary
9877 -- Start of processing for Needs_Result_Accessibility_Level
9879 begin
9880 -- False if completion unavailable (how does this happen???)
9882 if not Present (Func_Typ) then
9883 return False;
9885 elsif Feature_Disabled then
9886 return False;
9888 -- False if not a function, also handle enum-lit renames case
9890 elsif Func_Typ = Standard_Void_Type
9891 or else Is_Scalar_Type (Func_Typ)
9892 then
9893 return False;
9895 -- Handle a corner case, a cross-dialect subp renaming. For example,
9896 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9897 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9899 elsif Present (Alias (Func_Id)) then
9901 -- Unimplemented: a cross-dialect subp renaming which does not set
9902 -- the Alias attribute (e.g., a rename of a dereference of an access
9903 -- to subprogram value). ???
9905 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9907 -- Remaining cases require Ada 2012 mode
9909 elsif Ada_Version < Ada_2012 then
9910 return False;
9912 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9913 or else Is_Tagged_Type (Func_Typ)
9914 then
9915 -- In the case of, say, a null tagged record result type, the need
9916 -- for this extra parameter might not be obvious. This function
9917 -- returns True for all tagged types for compatibility reasons.
9918 -- A function with, say, a tagged null controlling result type might
9919 -- be overridden by a primitive of an extension having an access
9920 -- discriminant and the overrider and overridden must have compatible
9921 -- calling conventions (including implicitly declared parameters).
9922 -- Similarly, values of one access-to-subprogram type might designate
9923 -- both a primitive subprogram of a given type and a function
9924 -- which is, for example, not a primitive subprogram of any type.
9925 -- Again, this requires calling convention compatibility.
9926 -- It might be possible to solve these issues by introducing
9927 -- wrappers, but that is not the approach that was chosen.
9929 return True;
9931 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9932 return True;
9934 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9935 return True;
9937 -- False for all other cases
9939 else
9940 return False;
9941 end if;
9942 end Needs_Result_Accessibility_Level;
9944 ------------------------
9945 -- List_Inlining_Info --
9946 ------------------------
9948 procedure List_Inlining_Info is
9949 Elmt : Elmt_Id;
9950 Nod : Node_Id;
9951 Count : Nat;
9953 begin
9954 if not Debug_Flag_Dot_J then
9955 return;
9956 end if;
9958 -- Generate listing of calls inlined by the frontend
9960 if Present (Inlined_Calls) then
9961 Count := 0;
9962 Elmt := First_Elmt (Inlined_Calls);
9963 while Present (Elmt) loop
9964 Nod := Node (Elmt);
9966 if In_Extended_Main_Code_Unit (Nod) then
9967 Count := Count + 1;
9969 if Count = 1 then
9970 Write_Str ("Listing of frontend inlined calls");
9971 Write_Eol;
9972 end if;
9974 Write_Str (" ");
9975 Write_Int (Count);
9976 Write_Str (":");
9977 Write_Location (Sloc (Nod));
9978 Write_Str (":");
9979 Output.Write_Eol;
9980 end if;
9982 Next_Elmt (Elmt);
9983 end loop;
9984 end if;
9986 -- Generate listing of calls passed to the backend
9988 if Present (Backend_Calls) then
9989 Count := 0;
9991 Elmt := First_Elmt (Backend_Calls);
9992 while Present (Elmt) loop
9993 Nod := Node (Elmt);
9995 if In_Extended_Main_Code_Unit (Nod) then
9996 Count := Count + 1;
9998 if Count = 1 then
9999 Write_Str ("Listing of inlined calls passed to the backend");
10000 Write_Eol;
10001 end if;
10003 Write_Str (" ");
10004 Write_Int (Count);
10005 Write_Str (":");
10006 Write_Location (Sloc (Nod));
10007 Output.Write_Eol;
10008 end if;
10010 Next_Elmt (Elmt);
10011 end loop;
10012 end if;
10013 end List_Inlining_Info;
10015 end Exp_Ch6;