Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / ada / exp_ch6.adb
blob46cc9ca4d103d0f3f3adc541a595644adb271a24
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-2014, 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_Prag; use Exp_Prag;
45 with Exp_Tss; use Exp_Tss;
46 with Exp_Util; use Exp_Util;
47 with Exp_VFpt; use Exp_VFpt;
48 with Fname; use Fname;
49 with Freeze; use Freeze;
50 with Inline; use Inline;
51 with Lib; use Lib;
52 with Namet; use Namet;
53 with Nlists; use Nlists;
54 with Nmake; use Nmake;
55 with Opt; use Opt;
56 with Output; use Output;
57 with Restrict; use Restrict;
58 with Rident; use Rident;
59 with Rtsfind; use Rtsfind;
60 with Sem; use Sem;
61 with Sem_Aux; use Sem_Aux;
62 with Sem_Ch6; use Sem_Ch6;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch12; use Sem_Ch12;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_SCIL; use Sem_SCIL;
73 with Sem_Util; use Sem_Util;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Stand; use Stand;
78 with Stringt; use Stringt;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Validsw; use Validsw;
84 package body Exp_Ch6 is
86 Inlined_Calls : Elist_Id := No_Elist;
87 Backend_Calls : Elist_Id := No_Elist;
88 -- List of frontend inlined calls and inline calls passed to the backend
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Add_Access_Actual_To_Build_In_Place_Call
95 (Function_Call : Node_Id;
96 Function_Id : Entity_Id;
97 Return_Object : Node_Id;
98 Is_Access : Boolean := False);
99 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
100 -- object name given by Return_Object and add the attribute to the end of
101 -- the actual parameter list associated with the build-in-place function
102 -- call denoted by Function_Call. However, if Is_Access is True, then
103 -- Return_Object is already an access expression, in which case it's passed
104 -- along directly to the build-in-place function. Finally, if Return_Object
105 -- is empty, then pass a null literal as the actual.
107 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
108 (Function_Call : Node_Id;
109 Function_Id : Entity_Id;
110 Alloc_Form : BIP_Allocation_Form := Unspecified;
111 Alloc_Form_Exp : Node_Id := Empty;
112 Pool_Actual : Node_Id := Make_Null (No_Location));
113 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
114 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
115 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
116 -- otherwise pass a literal corresponding to the Alloc_Form parameter
117 -- (which must not be Unspecified in that case). Pool_Actual is the
118 -- parameter to pass to BIP_Storage_Pool.
120 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
121 (Func_Call : Node_Id;
122 Func_Id : Entity_Id;
123 Ptr_Typ : Entity_Id := Empty;
124 Master_Exp : Node_Id := Empty);
125 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
126 -- finalization actions, add an actual parameter which is a pointer to the
127 -- finalization master of the caller. If Master_Exp is not Empty, then that
128 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
129 -- will result in an automatic "null" value for the actual.
131 procedure Add_Task_Actuals_To_Build_In_Place_Call
132 (Function_Call : Node_Id;
133 Function_Id : Entity_Id;
134 Master_Actual : Node_Id);
135 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
136 -- contains tasks, add two actual parameters: the master, and a pointer to
137 -- the caller's activation chain. Master_Actual is the actual parameter
138 -- expression to pass for the master. In most cases, this is the current
139 -- master (_master). The two exceptions are: If the function call is the
140 -- initialization expression for an allocator, we pass the master of the
141 -- access type. If the function call is the initialization expression for a
142 -- return object, we pass along the master passed in by the caller. The
143 -- activation chain to pass is always the local one. Note: Master_Actual
144 -- can be Empty, but only if there are no tasks.
146 procedure Check_Overriding_Operation (Subp : Entity_Id);
147 -- Subp is a dispatching operation. Check whether it may override an
148 -- inherited private operation, in which case its DT entry is that of
149 -- the hidden operation, not the one it may have received earlier.
150 -- This must be done before emitting the code to set the corresponding
151 -- DT to the address of the subprogram. The actual placement of Subp in
152 -- the proper place in the list of primitive operations is done in
153 -- Declare_Inherited_Private_Subprograms, which also has to deal with
154 -- implicit operations. This duplication is unavoidable for now???
156 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
157 -- This procedure is called only if the subprogram body N, whose spec
158 -- has the given entity Spec, contains a parameterless recursive call.
159 -- It attempts to generate runtime code to detect if this a case of
160 -- infinite recursion.
162 -- The body is scanned to determine dependencies. If the only external
163 -- dependencies are on a small set of scalar variables, then the values
164 -- of these variables are captured on entry to the subprogram, and if
165 -- the values are not changed for the call, we know immediately that
166 -- we have an infinite recursion.
168 procedure Expand_Ctrl_Function_Call (N : Node_Id);
169 -- N is a function call which returns a controlled object. Transform the
170 -- call into a temporary which retrieves the returned object from the
171 -- secondary stack using 'reference.
173 procedure Expand_Inlined_Call
174 (N : Node_Id;
175 Subp : Entity_Id;
176 Orig_Subp : Entity_Id);
177 -- If called subprogram can be inlined by the front-end, retrieve the
178 -- analyzed body, replace formals with actuals and expand call in place.
179 -- Generate thunks for actuals that are expressions, and insert the
180 -- corresponding constant declarations before the call. If the original
181 -- call is to a derived operation, the return type is the one of the
182 -- derived operation, but the body is that of the original, so return
183 -- expressions in the body must be converted to the desired type (which
184 -- is simply not noted in the tree without inline expansion).
186 procedure Expand_Non_Function_Return (N : Node_Id);
187 -- Called by Expand_N_Simple_Return_Statement in case we're returning from
188 -- a procedure body, entry body, accept statement, or extended return
189 -- statement. Note that all non-function returns are simple return
190 -- statements.
192 function Expand_Protected_Object_Reference
193 (N : Node_Id;
194 Scop : Entity_Id) return Node_Id;
196 procedure Expand_Protected_Subprogram_Call
197 (N : Node_Id;
198 Subp : Entity_Id;
199 Scop : Entity_Id);
200 -- A call to a protected subprogram within the protected object may appear
201 -- as a regular call. The list of actuals must be expanded to contain a
202 -- reference to the object itself, and the call becomes a call to the
203 -- corresponding protected subprogram.
205 function Has_Unconstrained_Access_Discriminants
206 (Subtyp : Entity_Id) return Boolean;
207 -- Returns True if the given subtype is unconstrained and has one
208 -- or more access discriminants.
210 procedure Expand_Simple_Function_Return (N : Node_Id);
211 -- Expand simple return from function. In the case where we are returning
212 -- from a function body this is called by Expand_N_Simple_Return_Statement.
214 ----------------------------------------------
215 -- Add_Access_Actual_To_Build_In_Place_Call --
216 ----------------------------------------------
218 procedure Add_Access_Actual_To_Build_In_Place_Call
219 (Function_Call : Node_Id;
220 Function_Id : Entity_Id;
221 Return_Object : Node_Id;
222 Is_Access : Boolean := False)
224 Loc : constant Source_Ptr := Sloc (Function_Call);
225 Obj_Address : Node_Id;
226 Obj_Acc_Formal : Entity_Id;
228 begin
229 -- Locate the implicit access parameter in the called function
231 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
233 -- If no return object is provided, then pass null
235 if not Present (Return_Object) then
236 Obj_Address := Make_Null (Loc);
237 Set_Parent (Obj_Address, Function_Call);
239 -- If Return_Object is already an expression of an access type, then use
240 -- it directly, since it must be an access value denoting the return
241 -- object, and couldn't possibly be the return object itself.
243 elsif Is_Access then
244 Obj_Address := Return_Object;
245 Set_Parent (Obj_Address, Function_Call);
247 -- Apply Unrestricted_Access to caller's return object
249 else
250 Obj_Address :=
251 Make_Attribute_Reference (Loc,
252 Prefix => Return_Object,
253 Attribute_Name => Name_Unrestricted_Access);
255 Set_Parent (Return_Object, Obj_Address);
256 Set_Parent (Obj_Address, Function_Call);
257 end if;
259 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
261 -- Build the parameter association for the new actual and add it to the
262 -- end of the function's actuals.
264 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
265 end Add_Access_Actual_To_Build_In_Place_Call;
267 ------------------------------------------------------
268 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
269 ------------------------------------------------------
271 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
272 (Function_Call : Node_Id;
273 Function_Id : Entity_Id;
274 Alloc_Form : BIP_Allocation_Form := Unspecified;
275 Alloc_Form_Exp : Node_Id := Empty;
276 Pool_Actual : Node_Id := Make_Null (No_Location))
278 Loc : constant Source_Ptr := Sloc (Function_Call);
279 Alloc_Form_Actual : Node_Id;
280 Alloc_Form_Formal : Node_Id;
281 Pool_Formal : Node_Id;
283 begin
284 -- The allocation form generally doesn't need to be passed in the case
285 -- of a constrained result subtype, since normally the caller performs
286 -- the allocation in that case. However this formal is still needed in
287 -- the case where the function has a tagged result, because generally
288 -- such functions can be called in a dispatching context and such calls
289 -- must be handled like calls to class-wide functions.
291 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
292 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
293 then
294 return;
295 end if;
297 -- Locate the implicit allocation form parameter in the called function.
298 -- Maybe it would be better for each implicit formal of a build-in-place
299 -- function to have a flag or a Uint attribute to identify it. ???
301 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
303 if Present (Alloc_Form_Exp) then
304 pragma Assert (Alloc_Form = Unspecified);
306 Alloc_Form_Actual := Alloc_Form_Exp;
308 else
309 pragma Assert (Alloc_Form /= Unspecified);
311 Alloc_Form_Actual :=
312 Make_Integer_Literal (Loc,
313 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
314 end if;
316 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
318 -- Build the parameter association for the new actual and add it to the
319 -- end of the function's actuals.
321 Add_Extra_Actual_To_Call
322 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
324 -- Pass the Storage_Pool parameter. This parameter is omitted on
325 -- .NET/JVM/ZFP as those targets do not support pools.
327 if VM_Target = No_VM
328 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
329 then
330 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
331 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
332 Add_Extra_Actual_To_Call
333 (Function_Call, Pool_Formal, Pool_Actual);
334 end if;
335 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
337 -----------------------------------------------------------
338 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
339 -----------------------------------------------------------
341 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
342 (Func_Call : Node_Id;
343 Func_Id : Entity_Id;
344 Ptr_Typ : Entity_Id := Empty;
345 Master_Exp : Node_Id := Empty)
347 begin
348 if not Needs_BIP_Finalization_Master (Func_Id) then
349 return;
350 end if;
352 declare
353 Formal : constant Entity_Id :=
354 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
355 Loc : constant Source_Ptr := Sloc (Func_Call);
357 Actual : Node_Id;
358 Desig_Typ : Entity_Id;
360 begin
361 -- If there is a finalization master actual, such as the implicit
362 -- finalization master of an enclosing build-in-place function,
363 -- then this must be added as an extra actual of the call.
365 if Present (Master_Exp) then
366 Actual := Master_Exp;
368 -- Case where the context does not require an actual master
370 elsif No (Ptr_Typ) then
371 Actual := Make_Null (Loc);
373 else
374 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
376 -- Check for a library-level access type whose designated type has
377 -- supressed finalization. Such an access types lack a master.
378 -- Pass a null actual to the callee in order to signal a missing
379 -- master.
381 if Is_Library_Level_Entity (Ptr_Typ)
382 and then Finalize_Storage_Only (Desig_Typ)
383 then
384 Actual := Make_Null (Loc);
386 -- Types in need of finalization actions
388 elsif Needs_Finalization (Desig_Typ) then
390 -- The general mechanism of creating finalization masters for
391 -- anonymous access types is disabled by default, otherwise
392 -- finalization masters will pop all over the place. Such types
393 -- use context-specific masters.
395 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
396 and then No (Finalization_Master (Ptr_Typ))
397 then
398 Build_Finalization_Master
399 (Typ => Ptr_Typ,
400 Ins_Node => Associated_Node_For_Itype (Ptr_Typ),
401 Encl_Scope => Scope (Ptr_Typ));
402 end if;
404 -- Access-to-controlled types should always have a master
406 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
408 Actual :=
409 Make_Attribute_Reference (Loc,
410 Prefix =>
411 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
412 Attribute_Name => Name_Unrestricted_Access);
414 -- Tagged types
416 else
417 Actual := Make_Null (Loc);
418 end if;
419 end if;
421 Analyze_And_Resolve (Actual, Etype (Formal));
423 -- Build the parameter association for the new actual and add it to
424 -- the end of the function's actuals.
426 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
427 end;
428 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
430 ------------------------------
431 -- Add_Extra_Actual_To_Call --
432 ------------------------------
434 procedure Add_Extra_Actual_To_Call
435 (Subprogram_Call : Node_Id;
436 Extra_Formal : Entity_Id;
437 Extra_Actual : Node_Id)
439 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
440 Param_Assoc : Node_Id;
442 begin
443 Param_Assoc :=
444 Make_Parameter_Association (Loc,
445 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
446 Explicit_Actual_Parameter => Extra_Actual);
448 Set_Parent (Param_Assoc, Subprogram_Call);
449 Set_Parent (Extra_Actual, Param_Assoc);
451 if Present (Parameter_Associations (Subprogram_Call)) then
452 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
453 N_Parameter_Association
454 then
456 -- Find last named actual, and append
458 declare
459 L : Node_Id;
460 begin
461 L := First_Actual (Subprogram_Call);
462 while Present (L) loop
463 if No (Next_Actual (L)) then
464 Set_Next_Named_Actual (Parent (L), Extra_Actual);
465 exit;
466 end if;
467 Next_Actual (L);
468 end loop;
469 end;
471 else
472 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
473 end if;
475 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
477 else
478 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
479 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
480 end if;
481 end Add_Extra_Actual_To_Call;
483 ---------------------------------------------
484 -- Add_Task_Actuals_To_Build_In_Place_Call --
485 ---------------------------------------------
487 procedure Add_Task_Actuals_To_Build_In_Place_Call
488 (Function_Call : Node_Id;
489 Function_Id : Entity_Id;
490 Master_Actual : Node_Id)
492 Loc : constant Source_Ptr := Sloc (Function_Call);
493 Result_Subt : constant Entity_Id :=
494 Available_View (Etype (Function_Id));
495 Actual : Node_Id;
496 Chain_Actual : Node_Id;
497 Chain_Formal : Node_Id;
498 Master_Formal : Node_Id;
500 begin
501 -- No such extra parameters are needed if there are no tasks
503 if not Has_Task (Result_Subt) then
504 return;
505 end if;
507 Actual := Master_Actual;
509 -- Use a dummy _master actual in case of No_Task_Hierarchy
511 if Restriction_Active (No_Task_Hierarchy) then
512 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
514 -- In the case where we use the master associated with an access type,
515 -- the actual is an entity and requires an explicit reference.
517 elsif Nkind (Actual) = N_Defining_Identifier then
518 Actual := New_Occurrence_Of (Actual, Loc);
519 end if;
521 -- Locate the implicit master parameter in the called function
523 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
524 Analyze_And_Resolve (Actual, Etype (Master_Formal));
526 -- Build the parameter association for the new actual and add it to the
527 -- end of the function's actuals.
529 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
531 -- Locate the implicit activation chain parameter in the called function
533 Chain_Formal :=
534 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
536 -- Create the actual which is a pointer to the current activation chain
538 Chain_Actual :=
539 Make_Attribute_Reference (Loc,
540 Prefix => Make_Identifier (Loc, Name_uChain),
541 Attribute_Name => Name_Unrestricted_Access);
543 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
545 -- Build the parameter association for the new actual and add it to the
546 -- end of the function's actuals.
548 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
549 end Add_Task_Actuals_To_Build_In_Place_Call;
551 -----------------------
552 -- BIP_Formal_Suffix --
553 -----------------------
555 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
556 begin
557 case Kind is
558 when BIP_Alloc_Form =>
559 return "BIPalloc";
560 when BIP_Storage_Pool =>
561 return "BIPstoragepool";
562 when BIP_Finalization_Master =>
563 return "BIPfinalizationmaster";
564 when BIP_Task_Master =>
565 return "BIPtaskmaster";
566 when BIP_Activation_Chain =>
567 return "BIPactivationchain";
568 when BIP_Object_Access =>
569 return "BIPaccess";
570 end case;
571 end BIP_Formal_Suffix;
573 ---------------------------
574 -- Build_In_Place_Formal --
575 ---------------------------
577 function Build_In_Place_Formal
578 (Func : Entity_Id;
579 Kind : BIP_Formal_Kind) return Entity_Id
581 Formal_Name : constant Name_Id :=
582 New_External_Name
583 (Chars (Func), BIP_Formal_Suffix (Kind));
584 Extra_Formal : Entity_Id := Extra_Formals (Func);
586 begin
587 -- Maybe it would be better for each implicit formal of a build-in-place
588 -- function to have a flag or a Uint attribute to identify it. ???
590 -- The return type in the function declaration may have been a limited
591 -- view, and the extra formals for the function were not generated at
592 -- that point. At the point of call the full view must be available and
593 -- the extra formals can be created.
595 if No (Extra_Formal) then
596 Create_Extra_Formals (Func);
597 Extra_Formal := Extra_Formals (Func);
598 end if;
600 loop
601 pragma Assert (Present (Extra_Formal));
602 exit when Chars (Extra_Formal) = Formal_Name;
604 Next_Formal_With_Extras (Extra_Formal);
605 end loop;
607 return Extra_Formal;
608 end Build_In_Place_Formal;
610 --------------------------------
611 -- Check_Overriding_Operation --
612 --------------------------------
614 procedure Check_Overriding_Operation (Subp : Entity_Id) is
615 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
616 Op_List : constant Elist_Id := Primitive_Operations (Typ);
617 Op_Elmt : Elmt_Id;
618 Prim_Op : Entity_Id;
619 Par_Op : Entity_Id;
621 begin
622 if Is_Derived_Type (Typ)
623 and then not Is_Private_Type (Typ)
624 and then In_Open_Scopes (Scope (Etype (Typ)))
625 and then Is_Base_Type (Typ)
626 then
627 -- Subp overrides an inherited private operation if there is an
628 -- inherited operation with a different name than Subp (see
629 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
630 -- same name as Subp.
632 Op_Elmt := First_Elmt (Op_List);
633 while Present (Op_Elmt) loop
634 Prim_Op := Node (Op_Elmt);
635 Par_Op := Alias (Prim_Op);
637 if Present (Par_Op)
638 and then not Comes_From_Source (Prim_Op)
639 and then Chars (Prim_Op) /= Chars (Par_Op)
640 and then Chars (Par_Op) = Chars (Subp)
641 and then Is_Hidden (Par_Op)
642 and then Type_Conformant (Prim_Op, Subp)
643 then
644 Set_DT_Position (Subp, DT_Position (Prim_Op));
645 end if;
647 Next_Elmt (Op_Elmt);
648 end loop;
649 end if;
650 end Check_Overriding_Operation;
652 -------------------------------
653 -- Detect_Infinite_Recursion --
654 -------------------------------
656 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
657 Loc : constant Source_Ptr := Sloc (N);
659 Var_List : constant Elist_Id := New_Elmt_List;
660 -- List of globals referenced by body of procedure
662 Call_List : constant Elist_Id := New_Elmt_List;
663 -- List of recursive calls in body of procedure
665 Shad_List : constant Elist_Id := New_Elmt_List;
666 -- List of entity id's for entities created to capture the value of
667 -- referenced globals on entry to the procedure.
669 Scop : constant Uint := Scope_Depth (Spec);
670 -- This is used to record the scope depth of the current procedure, so
671 -- that we can identify global references.
673 Max_Vars : constant := 4;
674 -- Do not test more than four global variables
676 Count_Vars : Natural := 0;
677 -- Count variables found so far
679 Var : Entity_Id;
680 Elm : Elmt_Id;
681 Ent : Entity_Id;
682 Call : Elmt_Id;
683 Decl : Node_Id;
684 Test : Node_Id;
685 Elm1 : Elmt_Id;
686 Elm2 : Elmt_Id;
687 Last : Node_Id;
689 function Process (Nod : Node_Id) return Traverse_Result;
690 -- Function to traverse the subprogram body (using Traverse_Func)
692 -------------
693 -- Process --
694 -------------
696 function Process (Nod : Node_Id) return Traverse_Result is
697 begin
698 -- Procedure call
700 if Nkind (Nod) = N_Procedure_Call_Statement then
702 -- Case of one of the detected recursive calls
704 if Is_Entity_Name (Name (Nod))
705 and then Has_Recursive_Call (Entity (Name (Nod)))
706 and then Entity (Name (Nod)) = Spec
707 then
708 Append_Elmt (Nod, Call_List);
709 return Skip;
711 -- Any other procedure call may have side effects
713 else
714 return Abandon;
715 end if;
717 -- A call to a pure function can always be ignored
719 elsif Nkind (Nod) = N_Function_Call
720 and then Is_Entity_Name (Name (Nod))
721 and then Is_Pure (Entity (Name (Nod)))
722 then
723 return Skip;
725 -- Case of an identifier reference
727 elsif Nkind (Nod) = N_Identifier then
728 Ent := Entity (Nod);
730 -- If no entity, then ignore the reference
732 -- Not clear why this can happen. To investigate, remove this
733 -- test and look at the crash that occurs here in 3401-004 ???
735 if No (Ent) then
736 return Skip;
738 -- Ignore entities with no Scope, again not clear how this
739 -- can happen, to investigate, look at 4108-008 ???
741 elsif No (Scope (Ent)) then
742 return Skip;
744 -- Ignore the reference if not to a more global object
746 elsif Scope_Depth (Scope (Ent)) >= Scop then
747 return Skip;
749 -- References to types, exceptions and constants are always OK
751 elsif Is_Type (Ent)
752 or else Ekind (Ent) = E_Exception
753 or else Ekind (Ent) = E_Constant
754 then
755 return Skip;
757 -- If other than a non-volatile scalar variable, we have some
758 -- kind of global reference (e.g. to a function) that we cannot
759 -- deal with so we forget the attempt.
761 elsif Ekind (Ent) /= E_Variable
762 or else not Is_Scalar_Type (Etype (Ent))
763 or else Treat_As_Volatile (Ent)
764 then
765 return Abandon;
767 -- Otherwise we have a reference to a global scalar
769 else
770 -- Loop through global entities already detected
772 Elm := First_Elmt (Var_List);
773 loop
774 -- If not detected before, record this new global reference
776 if No (Elm) then
777 Count_Vars := Count_Vars + 1;
779 if Count_Vars <= Max_Vars then
780 Append_Elmt (Entity (Nod), Var_List);
781 else
782 return Abandon;
783 end if;
785 exit;
787 -- If recorded before, ignore
789 elsif Node (Elm) = Entity (Nod) then
790 return Skip;
792 -- Otherwise keep looking
794 else
795 Next_Elmt (Elm);
796 end if;
797 end loop;
799 return Skip;
800 end if;
802 -- For all other node kinds, recursively visit syntactic children
804 else
805 return OK;
806 end if;
807 end Process;
809 function Traverse_Body is new Traverse_Func (Process);
811 -- Start of processing for Detect_Infinite_Recursion
813 begin
814 -- Do not attempt detection in No_Implicit_Conditional mode, since we
815 -- won't be able to generate the code to handle the recursion in any
816 -- case.
818 if Restriction_Active (No_Implicit_Conditionals) then
819 return;
820 end if;
822 -- Otherwise do traversal and quit if we get abandon signal
824 if Traverse_Body (N) = Abandon then
825 return;
827 -- We must have a call, since Has_Recursive_Call was set. If not just
828 -- ignore (this is only an error check, so if we have a funny situation,
829 -- due to bugs or errors, we do not want to bomb).
831 elsif Is_Empty_Elmt_List (Call_List) then
832 return;
833 end if;
835 -- Here is the case where we detect recursion at compile time
837 -- Push our current scope for analyzing the declarations and code that
838 -- we will insert for the checking.
840 Push_Scope (Spec);
842 -- This loop builds temporary variables for each of the referenced
843 -- globals, so that at the end of the loop the list Shad_List contains
844 -- these temporaries in one-to-one correspondence with the elements in
845 -- Var_List.
847 Last := Empty;
848 Elm := First_Elmt (Var_List);
849 while Present (Elm) loop
850 Var := Node (Elm);
851 Ent := Make_Temporary (Loc, 'S');
852 Append_Elmt (Ent, Shad_List);
854 -- Insert a declaration for this temporary at the start of the
855 -- declarations for the procedure. The temporaries are declared as
856 -- constant objects initialized to the current values of the
857 -- corresponding temporaries.
859 Decl :=
860 Make_Object_Declaration (Loc,
861 Defining_Identifier => Ent,
862 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
863 Constant_Present => True,
864 Expression => New_Occurrence_Of (Var, Loc));
866 if No (Last) then
867 Prepend (Decl, Declarations (N));
868 else
869 Insert_After (Last, Decl);
870 end if;
872 Last := Decl;
873 Analyze (Decl);
874 Next_Elmt (Elm);
875 end loop;
877 -- Loop through calls
879 Call := First_Elmt (Call_List);
880 while Present (Call) loop
882 -- Build a predicate expression of the form
884 -- True
885 -- and then global1 = temp1
886 -- and then global2 = temp2
887 -- ...
889 -- This predicate determines if any of the global values
890 -- referenced by the procedure have changed since the
891 -- current call, if not an infinite recursion is assured.
893 Test := New_Occurrence_Of (Standard_True, Loc);
895 Elm1 := First_Elmt (Var_List);
896 Elm2 := First_Elmt (Shad_List);
897 while Present (Elm1) loop
898 Test :=
899 Make_And_Then (Loc,
900 Left_Opnd => Test,
901 Right_Opnd =>
902 Make_Op_Eq (Loc,
903 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
904 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
906 Next_Elmt (Elm1);
907 Next_Elmt (Elm2);
908 end loop;
910 -- Now we replace the call with the sequence
912 -- if no-changes (see above) then
913 -- raise Storage_Error;
914 -- else
915 -- original-call
916 -- end if;
918 Rewrite (Node (Call),
919 Make_If_Statement (Loc,
920 Condition => Test,
921 Then_Statements => New_List (
922 Make_Raise_Storage_Error (Loc,
923 Reason => SE_Infinite_Recursion)),
925 Else_Statements => New_List (
926 Relocate_Node (Node (Call)))));
928 Analyze (Node (Call));
930 Next_Elmt (Call);
931 end loop;
933 -- Remove temporary scope stack entry used for analysis
935 Pop_Scope;
936 end Detect_Infinite_Recursion;
938 --------------------
939 -- Expand_Actuals --
940 --------------------
942 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
943 Loc : constant Source_Ptr := Sloc (N);
944 Actual : Node_Id;
945 Formal : Entity_Id;
946 N_Node : Node_Id;
947 Post_Call : List_Id;
948 E_Actual : Entity_Id;
949 E_Formal : Entity_Id;
951 procedure Add_Call_By_Copy_Code;
952 -- For cases where the parameter must be passed by copy, this routine
953 -- generates a temporary variable into which the actual is copied and
954 -- then passes this as the parameter. For an OUT or IN OUT parameter,
955 -- an assignment is also generated to copy the result back. The call
956 -- also takes care of any constraint checks required for the type
957 -- conversion case (on both the way in and the way out).
959 procedure Add_Simple_Call_By_Copy_Code;
960 -- This is similar to the above, but is used in cases where we know
961 -- that all that is needed is to simply create a temporary and copy
962 -- the value in and out of the temporary.
964 procedure Check_Fortran_Logical;
965 -- A value of type Logical that is passed through a formal parameter
966 -- must be normalized because .TRUE. usually does not have the same
967 -- representation as True. We assume that .FALSE. = False = 0.
968 -- What about functions that return a logical type ???
970 function Is_Legal_Copy return Boolean;
971 -- Check that an actual can be copied before generating the temporary
972 -- to be used in the call. If the actual is of a by_reference type then
973 -- the program is illegal (this can only happen in the presence of
974 -- rep. clauses that force an incorrect alignment). If the formal is
975 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
976 -- the effect that this might lead to unaligned arguments.
978 function Make_Var (Actual : Node_Id) return Entity_Id;
979 -- Returns an entity that refers to the given actual parameter,
980 -- Actual (not including any type conversion). If Actual is an
981 -- entity name, then this entity is returned unchanged, otherwise
982 -- a renaming is created to provide an entity for the actual.
984 procedure Reset_Packed_Prefix;
985 -- The expansion of a packed array component reference is delayed in
986 -- the context of a call. Now we need to complete the expansion, so we
987 -- unmark the analyzed bits in all prefixes.
989 ---------------------------
990 -- Add_Call_By_Copy_Code --
991 ---------------------------
993 procedure Add_Call_By_Copy_Code is
994 Expr : Node_Id;
995 Init : Node_Id;
996 Temp : Entity_Id;
997 Indic : Node_Id;
998 Var : Entity_Id;
999 F_Typ : constant Entity_Id := Etype (Formal);
1000 V_Typ : Entity_Id;
1001 Crep : Boolean;
1003 begin
1004 if not Is_Legal_Copy then
1005 return;
1006 end if;
1008 Temp := Make_Temporary (Loc, 'T', Actual);
1010 -- Use formal type for temp, unless formal type is an unconstrained
1011 -- array, in which case we don't have to worry about bounds checks,
1012 -- and we use the actual type, since that has appropriate bounds.
1014 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1015 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1016 else
1017 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1018 end if;
1020 if Nkind (Actual) = N_Type_Conversion then
1021 V_Typ := Etype (Expression (Actual));
1023 -- If the formal is an (in-)out parameter, capture the name
1024 -- of the variable in order to build the post-call assignment.
1026 Var := Make_Var (Expression (Actual));
1028 Crep := not Same_Representation
1029 (F_Typ, Etype (Expression (Actual)));
1031 else
1032 V_Typ := Etype (Actual);
1033 Var := Make_Var (Actual);
1034 Crep := False;
1035 end if;
1037 -- Setup initialization for case of in out parameter, or an out
1038 -- parameter where the formal is an unconstrained array (in the
1039 -- latter case, we have to pass in an object with bounds).
1041 -- If this is an out parameter, the initial copy is wasteful, so as
1042 -- an optimization for the one-dimensional case we extract the
1043 -- bounds of the actual and build an uninitialized temporary of the
1044 -- right size.
1046 if Ekind (Formal) = E_In_Out_Parameter
1047 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1048 then
1049 if Nkind (Actual) = N_Type_Conversion then
1050 if Conversion_OK (Actual) then
1051 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1052 else
1053 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1054 end if;
1056 elsif Ekind (Formal) = E_Out_Parameter
1057 and then Is_Array_Type (F_Typ)
1058 and then Number_Dimensions (F_Typ) = 1
1059 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1060 then
1061 -- Actual is a one-dimensional array or slice, and the type
1062 -- requires no initialization. Create a temporary of the
1063 -- right size, but do not copy actual into it (optimization).
1065 Init := Empty;
1066 Indic :=
1067 Make_Subtype_Indication (Loc,
1068 Subtype_Mark =>
1069 New_Occurrence_Of (F_Typ, Loc),
1070 Constraint =>
1071 Make_Index_Or_Discriminant_Constraint (Loc,
1072 Constraints => New_List (
1073 Make_Range (Loc,
1074 Low_Bound =>
1075 Make_Attribute_Reference (Loc,
1076 Prefix => New_Occurrence_Of (Var, Loc),
1077 Attribute_Name => Name_First),
1078 High_Bound =>
1079 Make_Attribute_Reference (Loc,
1080 Prefix => New_Occurrence_Of (Var, Loc),
1081 Attribute_Name => Name_Last)))));
1083 else
1084 Init := New_Occurrence_Of (Var, Loc);
1085 end if;
1087 -- An initialization is created for packed conversions as
1088 -- actuals for out parameters to enable Make_Object_Declaration
1089 -- to determine the proper subtype for N_Node. Note that this
1090 -- is wasteful because the extra copying on the call side is
1091 -- not required for such out parameters. ???
1093 elsif Ekind (Formal) = E_Out_Parameter
1094 and then Nkind (Actual) = N_Type_Conversion
1095 and then (Is_Bit_Packed_Array (F_Typ)
1096 or else
1097 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1098 then
1099 if Conversion_OK (Actual) then
1100 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1101 else
1102 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1103 end if;
1105 elsif Ekind (Formal) = E_In_Parameter then
1107 -- Handle the case in which the actual is a type conversion
1109 if Nkind (Actual) = N_Type_Conversion then
1110 if Conversion_OK (Actual) then
1111 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1112 else
1113 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1114 end if;
1115 else
1116 Init := New_Occurrence_Of (Var, Loc);
1117 end if;
1119 else
1120 Init := Empty;
1121 end if;
1123 N_Node :=
1124 Make_Object_Declaration (Loc,
1125 Defining_Identifier => Temp,
1126 Object_Definition => Indic,
1127 Expression => Init);
1128 Set_Assignment_OK (N_Node);
1129 Insert_Action (N, N_Node);
1131 -- Now, normally the deal here is that we use the defining
1132 -- identifier created by that object declaration. There is
1133 -- one exception to this. In the change of representation case
1134 -- the above declaration will end up looking like:
1136 -- temp : type := identifier;
1138 -- And in this case we might as well use the identifier directly
1139 -- and eliminate the temporary. Note that the analysis of the
1140 -- declaration was not a waste of time in that case, since it is
1141 -- what generated the necessary change of representation code. If
1142 -- the change of representation introduced additional code, as in
1143 -- a fixed-integer conversion, the expression is not an identifier
1144 -- and must be kept.
1146 if Crep
1147 and then Present (Expression (N_Node))
1148 and then Is_Entity_Name (Expression (N_Node))
1149 then
1150 Temp := Entity (Expression (N_Node));
1151 Rewrite (N_Node, Make_Null_Statement (Loc));
1152 end if;
1154 -- For IN parameter, all we do is to replace the actual
1156 if Ekind (Formal) = E_In_Parameter then
1157 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1158 Analyze (Actual);
1160 -- Processing for OUT or IN OUT parameter
1162 else
1163 -- Kill current value indications for the temporary variable we
1164 -- created, since we just passed it as an OUT parameter.
1166 Kill_Current_Values (Temp);
1167 Set_Is_Known_Valid (Temp, False);
1169 -- If type conversion, use reverse conversion on exit
1171 if Nkind (Actual) = N_Type_Conversion then
1172 if Conversion_OK (Actual) then
1173 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1174 else
1175 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1176 end if;
1177 else
1178 Expr := New_Occurrence_Of (Temp, Loc);
1179 end if;
1181 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1182 Analyze (Actual);
1184 -- If the actual is a conversion of a packed reference, it may
1185 -- already have been expanded by Remove_Side_Effects, and the
1186 -- resulting variable is a temporary which does not designate
1187 -- the proper out-parameter, which may not be addressable. In
1188 -- that case, generate an assignment to the original expression
1189 -- (before expansion of the packed reference) so that the proper
1190 -- expansion of assignment to a packed component can take place.
1192 declare
1193 Obj : Node_Id;
1194 Lhs : Node_Id;
1196 begin
1197 if Is_Renaming_Of_Object (Var)
1198 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1199 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1200 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1201 = N_Indexed_Component
1202 and then
1203 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1204 then
1205 Obj := Renamed_Object (Var);
1206 Lhs :=
1207 Make_Selected_Component (Loc,
1208 Prefix =>
1209 New_Copy_Tree (Original_Node (Prefix (Obj))),
1210 Selector_Name => New_Copy (Selector_Name (Obj)));
1211 Reset_Analyzed_Flags (Lhs);
1213 else
1214 Lhs := New_Occurrence_Of (Var, Loc);
1215 end if;
1217 Set_Assignment_OK (Lhs);
1219 if Is_Access_Type (E_Formal)
1220 and then Is_Entity_Name (Lhs)
1221 and then
1222 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1223 then
1224 -- Copyback target is an Ada 2012 stand-alone object of an
1225 -- anonymous access type.
1227 pragma Assert (Ada_Version >= Ada_2012);
1229 if Type_Access_Level (E_Formal) >
1230 Object_Access_Level (Lhs)
1231 then
1232 Append_To (Post_Call,
1233 Make_Raise_Program_Error (Loc,
1234 Reason => PE_Accessibility_Check_Failed));
1235 end if;
1237 Append_To (Post_Call,
1238 Make_Assignment_Statement (Loc,
1239 Name => Lhs,
1240 Expression => Expr));
1242 -- We would like to somehow suppress generation of the
1243 -- extra_accessibility assignment generated by the expansion
1244 -- of the above assignment statement. It's not a correctness
1245 -- issue because the following assignment renders it dead,
1246 -- but generating back-to-back assignments to the same
1247 -- target is undesirable. ???
1249 Append_To (Post_Call,
1250 Make_Assignment_Statement (Loc,
1251 Name => New_Occurrence_Of (
1252 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1253 Expression => Make_Integer_Literal (Loc,
1254 Type_Access_Level (E_Formal))));
1256 else
1257 Append_To (Post_Call,
1258 Make_Assignment_Statement (Loc,
1259 Name => Lhs,
1260 Expression => Expr));
1261 end if;
1262 end;
1263 end if;
1264 end Add_Call_By_Copy_Code;
1266 ----------------------------------
1267 -- Add_Simple_Call_By_Copy_Code --
1268 ----------------------------------
1270 procedure Add_Simple_Call_By_Copy_Code is
1271 Temp : Entity_Id;
1272 Decl : Node_Id;
1273 Incod : Node_Id;
1274 Outcod : Node_Id;
1275 Lhs : Node_Id;
1276 Rhs : Node_Id;
1277 Indic : Node_Id;
1278 F_Typ : constant Entity_Id := Etype (Formal);
1280 begin
1281 if not Is_Legal_Copy then
1282 return;
1283 end if;
1285 -- Use formal type for temp, unless formal type is an unconstrained
1286 -- array, in which case we don't have to worry about bounds checks,
1287 -- and we use the actual type, since that has appropriate bounds.
1289 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1290 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1291 else
1292 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1293 end if;
1295 -- Prepare to generate code
1297 Reset_Packed_Prefix;
1299 Temp := Make_Temporary (Loc, 'T', Actual);
1300 Incod := Relocate_Node (Actual);
1301 Outcod := New_Copy_Tree (Incod);
1303 -- Generate declaration of temporary variable, initializing it
1304 -- with the input parameter unless we have an OUT formal or
1305 -- this is an initialization call.
1307 -- If the formal is an out parameter with discriminants, the
1308 -- discriminants must be captured even if the rest of the object
1309 -- is in principle uninitialized, because the discriminants may
1310 -- be read by the called subprogram.
1312 if Ekind (Formal) = E_Out_Parameter then
1313 Incod := Empty;
1315 if Has_Discriminants (Etype (Formal)) then
1316 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1317 end if;
1319 elsif Inside_Init_Proc then
1321 -- Could use a comment here to match comment below ???
1323 if Nkind (Actual) /= N_Selected_Component
1324 or else
1325 not Has_Discriminant_Dependent_Constraint
1326 (Entity (Selector_Name (Actual)))
1327 then
1328 Incod := Empty;
1330 -- Otherwise, keep the component in order to generate the proper
1331 -- actual subtype, that depends on enclosing discriminants.
1333 else
1334 null;
1335 end if;
1336 end if;
1338 Decl :=
1339 Make_Object_Declaration (Loc,
1340 Defining_Identifier => Temp,
1341 Object_Definition => Indic,
1342 Expression => Incod);
1344 if Inside_Init_Proc
1345 and then No (Incod)
1346 then
1347 -- If the call is to initialize a component of a composite type,
1348 -- and the component does not depend on discriminants, use the
1349 -- actual type of the component. This is required in case the
1350 -- component is constrained, because in general the formal of the
1351 -- initialization procedure will be unconstrained. Note that if
1352 -- the component being initialized is constrained by an enclosing
1353 -- discriminant, the presence of the initialization in the
1354 -- declaration will generate an expression for the actual subtype.
1356 Set_No_Initialization (Decl);
1357 Set_Object_Definition (Decl,
1358 New_Occurrence_Of (Etype (Actual), Loc));
1359 end if;
1361 Insert_Action (N, Decl);
1363 -- The actual is simply a reference to the temporary
1365 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1367 -- Generate copy out if OUT or IN OUT parameter
1369 if Ekind (Formal) /= E_In_Parameter then
1370 Lhs := Outcod;
1371 Rhs := New_Occurrence_Of (Temp, Loc);
1373 -- Deal with conversion
1375 if Nkind (Lhs) = N_Type_Conversion then
1376 Lhs := Expression (Lhs);
1377 Rhs := Convert_To (Etype (Actual), Rhs);
1378 end if;
1380 Append_To (Post_Call,
1381 Make_Assignment_Statement (Loc,
1382 Name => Lhs,
1383 Expression => Rhs));
1384 Set_Assignment_OK (Name (Last (Post_Call)));
1385 end if;
1386 end Add_Simple_Call_By_Copy_Code;
1388 ---------------------------
1389 -- Check_Fortran_Logical --
1390 ---------------------------
1392 procedure Check_Fortran_Logical is
1393 Logical : constant Entity_Id := Etype (Formal);
1394 Var : Entity_Id;
1396 -- Note: this is very incomplete, e.g. it does not handle arrays
1397 -- of logical values. This is really not the right approach at all???)
1399 begin
1400 if Convention (Subp) = Convention_Fortran
1401 and then Root_Type (Etype (Formal)) = Standard_Boolean
1402 and then Ekind (Formal) /= E_In_Parameter
1403 then
1404 Var := Make_Var (Actual);
1405 Append_To (Post_Call,
1406 Make_Assignment_Statement (Loc,
1407 Name => New_Occurrence_Of (Var, Loc),
1408 Expression =>
1409 Unchecked_Convert_To (
1410 Logical,
1411 Make_Op_Ne (Loc,
1412 Left_Opnd => New_Occurrence_Of (Var, Loc),
1413 Right_Opnd =>
1414 Unchecked_Convert_To (
1415 Logical,
1416 New_Occurrence_Of (Standard_False, Loc))))));
1417 end if;
1418 end Check_Fortran_Logical;
1420 -------------------
1421 -- Is_Legal_Copy --
1422 -------------------
1424 function Is_Legal_Copy return Boolean is
1425 begin
1426 -- An attempt to copy a value of such a type can only occur if
1427 -- representation clauses give the actual a misaligned address.
1429 if Is_By_Reference_Type (Etype (Formal)) then
1431 -- If the front-end does not perform full type layout, the actual
1432 -- may in fact be properly aligned but there is not enough front-
1433 -- end information to determine this. In that case gigi will emit
1434 -- an error if a copy is not legal, or generate the proper code.
1435 -- For other backends we report the error now.
1437 -- Seems wrong to be issuing an error in the expander, since it
1438 -- will be missed in -gnatc mode ???
1440 if Frontend_Layout_On_Target then
1441 Error_Msg_N
1442 ("misaligned actual cannot be passed by reference", Actual);
1443 end if;
1445 return False;
1447 -- For users of Starlet, we assume that the specification of by-
1448 -- reference mechanism is mandatory. This may lead to unaligned
1449 -- objects but at least for DEC legacy code it is known to work.
1450 -- The warning will alert users of this code that a problem may
1451 -- be lurking.
1453 elsif Mechanism (Formal) = By_Reference
1454 and then Is_Valued_Procedure (Scope (Formal))
1455 then
1456 Error_Msg_N
1457 ("by_reference actual may be misaligned??", Actual);
1458 return False;
1460 else
1461 return True;
1462 end if;
1463 end Is_Legal_Copy;
1465 --------------
1466 -- Make_Var --
1467 --------------
1469 function Make_Var (Actual : Node_Id) return Entity_Id is
1470 Var : Entity_Id;
1472 begin
1473 if Is_Entity_Name (Actual) then
1474 return Entity (Actual);
1476 else
1477 Var := Make_Temporary (Loc, 'T', Actual);
1479 N_Node :=
1480 Make_Object_Renaming_Declaration (Loc,
1481 Defining_Identifier => Var,
1482 Subtype_Mark =>
1483 New_Occurrence_Of (Etype (Actual), Loc),
1484 Name => Relocate_Node (Actual));
1486 Insert_Action (N, N_Node);
1487 return Var;
1488 end if;
1489 end Make_Var;
1491 -------------------------
1492 -- Reset_Packed_Prefix --
1493 -------------------------
1495 procedure Reset_Packed_Prefix is
1496 Pfx : Node_Id := Actual;
1497 begin
1498 loop
1499 Set_Analyzed (Pfx, False);
1500 exit when
1501 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1502 Pfx := Prefix (Pfx);
1503 end loop;
1504 end Reset_Packed_Prefix;
1506 -- Start of processing for Expand_Actuals
1508 begin
1509 Post_Call := New_List;
1511 Formal := First_Formal (Subp);
1512 Actual := First_Actual (N);
1513 while Present (Formal) loop
1514 E_Formal := Etype (Formal);
1515 E_Actual := Etype (Actual);
1517 if Is_Scalar_Type (E_Formal)
1518 or else Nkind (Actual) = N_Slice
1519 then
1520 Check_Fortran_Logical;
1522 -- RM 6.4.1 (11)
1524 elsif Ekind (Formal) /= E_Out_Parameter then
1526 -- The unusual case of the current instance of a protected type
1527 -- requires special handling. This can only occur in the context
1528 -- of a call within the body of a protected operation.
1530 if Is_Entity_Name (Actual)
1531 and then Ekind (Entity (Actual)) = E_Protected_Type
1532 and then In_Open_Scopes (Entity (Actual))
1533 then
1534 if Scope (Subp) /= Entity (Actual) then
1535 Error_Msg_N
1536 ("operation outside protected type may not "
1537 & "call back its protected operations??", Actual);
1538 end if;
1540 Rewrite (Actual,
1541 Expand_Protected_Object_Reference (N, Entity (Actual)));
1542 end if;
1544 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1545 -- build-in-place function, then a temporary return object needs
1546 -- to be created and access to it must be passed to the function.
1547 -- Currently we limit such functions to those with inherently
1548 -- limited result subtypes, but eventually we plan to expand the
1549 -- functions that are treated as build-in-place to include other
1550 -- composite result types.
1552 if Is_Build_In_Place_Function_Call (Actual) then
1553 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1554 end if;
1556 Apply_Constraint_Check (Actual, E_Formal);
1558 -- Out parameter case. No constraint checks on access type
1559 -- RM 6.4.1 (13)
1561 elsif Is_Access_Type (E_Formal) then
1562 null;
1564 -- RM 6.4.1 (14)
1566 elsif Has_Discriminants (Base_Type (E_Formal))
1567 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1568 then
1569 Apply_Constraint_Check (Actual, E_Formal);
1571 -- RM 6.4.1 (15)
1573 else
1574 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1575 end if;
1577 -- Processing for IN-OUT and OUT parameters
1579 if Ekind (Formal) /= E_In_Parameter then
1581 -- For type conversions of arrays, apply length/range checks
1583 if Is_Array_Type (E_Formal)
1584 and then Nkind (Actual) = N_Type_Conversion
1585 then
1586 if Is_Constrained (E_Formal) then
1587 Apply_Length_Check (Expression (Actual), E_Formal);
1588 else
1589 Apply_Range_Check (Expression (Actual), E_Formal);
1590 end if;
1591 end if;
1593 -- If argument is a type conversion for a type that is passed
1594 -- by copy, then we must pass the parameter by copy.
1596 if Nkind (Actual) = N_Type_Conversion
1597 and then
1598 (Is_Numeric_Type (E_Formal)
1599 or else Is_Access_Type (E_Formal)
1600 or else Is_Enumeration_Type (E_Formal)
1601 or else Is_Bit_Packed_Array (Etype (Formal))
1602 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1604 -- Also pass by copy if change of representation
1606 or else not Same_Representation
1607 (Etype (Formal),
1608 Etype (Expression (Actual))))
1609 then
1610 Add_Call_By_Copy_Code;
1612 -- References to components of bit packed arrays are expanded
1613 -- at this point, rather than at the point of analysis of the
1614 -- actuals, to handle the expansion of the assignment to
1615 -- [in] out parameters.
1617 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1618 Add_Simple_Call_By_Copy_Code;
1620 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1621 -- because the back-end cannot cope with such objects. In other
1622 -- cases where alignment forces a copy, the back-end generates
1623 -- it properly. It should not be generated unconditionally in the
1624 -- front-end because it does not know precisely the alignment
1625 -- requirements of the target, and makes too conservative an
1626 -- estimate, leading to superfluous copies or spurious errors
1627 -- on by-reference parameters.
1629 elsif Nkind (Actual) = N_Selected_Component
1630 and then
1631 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1632 and then not Represented_As_Scalar (Etype (Formal))
1633 then
1634 Add_Simple_Call_By_Copy_Code;
1636 -- References to slices of bit packed arrays are expanded
1638 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1639 Add_Call_By_Copy_Code;
1641 -- References to possibly unaligned slices of arrays are expanded
1643 elsif Is_Possibly_Unaligned_Slice (Actual) then
1644 Add_Call_By_Copy_Code;
1646 -- Deal with access types where the actual subtype and the
1647 -- formal subtype are not the same, requiring a check.
1649 -- It is necessary to exclude tagged types because of "downward
1650 -- conversion" errors.
1652 elsif Is_Access_Type (E_Formal)
1653 and then not Same_Type (E_Formal, E_Actual)
1654 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1655 then
1656 Add_Call_By_Copy_Code;
1658 -- If the actual is not a scalar and is marked for volatile
1659 -- treatment, whereas the formal is not volatile, then pass
1660 -- by copy unless it is a by-reference type.
1662 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1663 -- because this is the enforcement of a language rule that applies
1664 -- only to "real" volatile variables, not e.g. to the address
1665 -- clause overlay case.
1667 elsif Is_Entity_Name (Actual)
1668 and then Is_Volatile (Entity (Actual))
1669 and then not Is_By_Reference_Type (E_Actual)
1670 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1671 and then not Is_Volatile (E_Formal)
1672 then
1673 Add_Call_By_Copy_Code;
1675 elsif Nkind (Actual) = N_Indexed_Component
1676 and then Is_Entity_Name (Prefix (Actual))
1677 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1678 then
1679 Add_Call_By_Copy_Code;
1681 -- Add call-by-copy code for the case of scalar out parameters
1682 -- when it is not known at compile time that the subtype of the
1683 -- formal is a subrange of the subtype of the actual (or vice
1684 -- versa for in out parameters), in order to get range checks
1685 -- on such actuals. (Maybe this case should be handled earlier
1686 -- in the if statement???)
1688 elsif Is_Scalar_Type (E_Formal)
1689 and then
1690 (not In_Subrange_Of (E_Formal, E_Actual)
1691 or else
1692 (Ekind (Formal) = E_In_Out_Parameter
1693 and then not In_Subrange_Of (E_Actual, E_Formal)))
1694 then
1695 -- Perhaps the setting back to False should be done within
1696 -- Add_Call_By_Copy_Code, since it could get set on other
1697 -- cases occurring above???
1699 if Do_Range_Check (Actual) then
1700 Set_Do_Range_Check (Actual, False);
1701 end if;
1703 Add_Call_By_Copy_Code;
1704 end if;
1706 -- RM 3.2.4 (23/3) : A predicate is checked on in-out and out
1707 -- by-reference parameters on exit from the call. If the actual
1708 -- is a derived type and the operation is inherited, the body
1709 -- of the operation will not contain a call to the predicate
1710 -- function, so it must be done explicitly after the call. Ditto
1711 -- if the actual is an entity of a predicated subtype.
1713 -- The rule refers to by-reference types, but a check is needed
1714 -- for by-copy types as well. That check is subsumed by the rule
1715 -- for subtype conversion on assignment, but we can generate the
1716 -- required check now.
1718 -- Note that this is needed only if the subtype of the actual has
1719 -- an explicit predicate aspect, not if it inherits them from a
1720 -- base type or ancestor. The check is also superfluous if the
1721 -- subtype is elaborated before the body of the subprogram, but
1722 -- this is harder to verify, and there may be a redundant check.
1724 -- Note also that Subp may be either a subprogram entity for
1725 -- direct calls, or a type entity for indirect calls, which must
1726 -- be handled separately because the name does not denote an
1727 -- overloadable entity.
1729 -- If the formal is class-wide the corresponding postcondition
1730 -- procedure does not include a predicate call, so it has to be
1731 -- generated explicitly.
1733 if not Is_Init_Proc (Subp)
1734 and then (Has_Aspect (E_Actual, Aspect_Predicate)
1735 or else
1736 Has_Aspect (E_Actual, Aspect_Dynamic_Predicate)
1737 or else
1738 Has_Aspect (E_Actual, Aspect_Static_Predicate))
1739 and then Present (Predicate_Function (E_Actual))
1740 then
1741 if Is_Entity_Name (Actual)
1742 or else
1743 (Is_Derived_Type (E_Actual)
1744 and then Is_Overloadable (Subp)
1745 and then Is_Inherited_Operation_For_Type (Subp, E_Actual))
1746 then
1747 Append_To (Post_Call,
1748 Make_Predicate_Check (E_Actual, Actual));
1750 elsif Is_Class_Wide_Type (E_Formal)
1751 and then not Is_Class_Wide_Type (E_Actual)
1752 then
1753 Append_To (Post_Call,
1754 Make_Predicate_Check (E_Actual, Actual));
1755 end if;
1756 end if;
1758 -- Processing for IN parameters
1760 else
1761 -- For IN parameters is in the packed array case, we expand an
1762 -- indexed component (the circuit in Exp_Ch4 deliberately left
1763 -- indexed components appearing as actuals untouched, so that
1764 -- the special processing above for the OUT and IN OUT cases
1765 -- could be performed. We could make the test in Exp_Ch4 more
1766 -- complex and have it detect the parameter mode, but it is
1767 -- easier simply to handle all cases here.)
1769 if Nkind (Actual) = N_Indexed_Component
1770 and then Is_Packed (Etype (Prefix (Actual)))
1771 then
1772 Reset_Packed_Prefix;
1773 Expand_Packed_Element_Reference (Actual);
1775 -- If we have a reference to a bit packed array, we copy it, since
1776 -- the actual must be byte aligned.
1778 -- Is this really necessary in all cases???
1780 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1781 Add_Simple_Call_By_Copy_Code;
1783 -- If a non-scalar actual is possibly unaligned, we need a copy
1785 elsif Is_Possibly_Unaligned_Object (Actual)
1786 and then not Represented_As_Scalar (Etype (Formal))
1787 then
1788 Add_Simple_Call_By_Copy_Code;
1790 -- Similarly, we have to expand slices of packed arrays here
1791 -- because the result must be byte aligned.
1793 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1794 Add_Call_By_Copy_Code;
1796 -- Only processing remaining is to pass by copy if this is a
1797 -- reference to a possibly unaligned slice, since the caller
1798 -- expects an appropriately aligned argument.
1800 elsif Is_Possibly_Unaligned_Slice (Actual) then
1801 Add_Call_By_Copy_Code;
1803 -- An unusual case: a current instance of an enclosing task can be
1804 -- an actual, and must be replaced by a reference to self.
1806 elsif Is_Entity_Name (Actual)
1807 and then Is_Task_Type (Entity (Actual))
1808 then
1809 if In_Open_Scopes (Entity (Actual)) then
1810 Rewrite (Actual,
1811 (Make_Function_Call (Loc,
1812 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1813 Analyze (Actual);
1815 -- A task type cannot otherwise appear as an actual
1817 else
1818 raise Program_Error;
1819 end if;
1820 end if;
1821 end if;
1823 Next_Formal (Formal);
1824 Next_Actual (Actual);
1825 end loop;
1827 -- Find right place to put post call stuff if it is present
1829 if not Is_Empty_List (Post_Call) then
1831 -- Cases where the call is not a member of a statement list
1833 if not Is_List_Member (N) then
1834 declare
1835 P : Node_Id := Parent (N);
1837 begin
1838 -- In Ada 2012 the call may be a function call in an expression
1839 -- (since OUT and IN OUT parameters are now allowed for such
1840 -- calls. The write-back of (in)-out parameters is handled
1841 -- by the back-end, but the constraint checks generated when
1842 -- subtypes of formal and actual don't match must be inserted
1843 -- in the form of assignments, at the nearest point after the
1844 -- declaration or statement that contains the call.
1846 if Ada_Version >= Ada_2012
1847 and then Nkind (N) = N_Function_Call
1848 then
1849 while Nkind (P) not in N_Declaration
1850 and then
1851 Nkind (P) not in N_Statement_Other_Than_Procedure_Call
1852 loop
1853 P := Parent (P);
1854 end loop;
1856 Insert_Actions_After (P, Post_Call);
1858 -- If not the special Ada 2012 case of a function call, then
1859 -- we must have the triggering statement of a triggering
1860 -- alternative or an entry call alternative, and we can add
1861 -- the post call stuff to the corresponding statement list.
1863 else
1864 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1865 N_Entry_Call_Alternative));
1867 if Is_Non_Empty_List (Statements (P)) then
1868 Insert_List_Before_And_Analyze
1869 (First (Statements (P)), Post_Call);
1870 else
1871 Set_Statements (P, Post_Call);
1872 end if;
1873 end if;
1875 end;
1877 -- Otherwise, normal case where N is in a statement sequence,
1878 -- just put the post-call stuff after the call statement.
1880 else
1881 Insert_Actions_After (N, Post_Call);
1882 end if;
1883 end if;
1885 -- The call node itself is re-analyzed in Expand_Call
1887 end Expand_Actuals;
1889 -----------------
1890 -- Expand_Call --
1891 -----------------
1893 -- This procedure handles expansion of function calls and procedure call
1894 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1895 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1897 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1898 -- Provide values of actuals for all formals in Extra_Formals list
1899 -- Replace "call" to enumeration literal function by literal itself
1900 -- Rewrite call to predefined operator as operator
1901 -- Replace actuals to in-out parameters that are numeric conversions,
1902 -- with explicit assignment to temporaries before and after the call.
1903 -- Remove optional actuals if First_Optional_Parameter specified.
1905 -- Note that the list of actuals has been filled with default expressions
1906 -- during semantic analysis of the call. Only the extra actuals required
1907 -- for the 'Constrained attribute and for accessibility checks are added
1908 -- at this point.
1910 procedure Expand_Call (N : Node_Id) is
1911 Loc : constant Source_Ptr := Sloc (N);
1912 Call_Node : Node_Id := N;
1913 Extra_Actuals : List_Id := No_List;
1914 Prev : Node_Id := Empty;
1916 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1917 -- Adds one entry to the end of the actual parameter list. Used for
1918 -- default parameters and for extra actuals (for Extra_Formals). The
1919 -- argument is an N_Parameter_Association node.
1921 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1922 -- Adds an extra actual to the list of extra actuals. Expr is the
1923 -- expression for the value of the actual, EF is the entity for the
1924 -- extra formal.
1926 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id);
1927 -- Check and inline the body of Subp. Invoked when compiling with
1928 -- optimizations enabled and Subp has pragma inline or inline always.
1929 -- If the subprogram is a renaming, or if it is inherited, then Subp
1930 -- references the renamed entity and Orig_Subp is the entity of the
1931 -- call node N.
1933 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id);
1934 -- Check and inline the body of Subp. Invoked when compiling without
1935 -- optimizations and Subp has pragma inline always. If the subprogram is
1936 -- a renaming, or if it is inherited, then Subp references the renamed
1937 -- entity and Orig_Subp is the entity of the call node N.
1939 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1940 -- Within an instance, a type derived from a non-tagged formal derived
1941 -- type inherits from the original parent, not from the actual. The
1942 -- current derivation mechanism has the derived type inherit from the
1943 -- actual, which is only correct outside of the instance. If the
1944 -- subprogram is inherited, we test for this particular case through a
1945 -- convoluted tree traversal before setting the proper subprogram to be
1946 -- called.
1948 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
1949 -- Return true if E comes from an instance that is not yet frozen
1951 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
1952 -- Determine if Subp denotes a non-dispatching call to a Deep routine
1954 function New_Value (From : Node_Id) return Node_Id;
1955 -- From is the original Expression. New_Value is equivalent to a call
1956 -- to Duplicate_Subexpr with an explicit dereference when From is an
1957 -- access parameter.
1959 --------------------------
1960 -- Add_Actual_Parameter --
1961 --------------------------
1963 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1964 Actual_Expr : constant Node_Id :=
1965 Explicit_Actual_Parameter (Insert_Param);
1967 begin
1968 -- Case of insertion is first named actual
1970 if No (Prev) or else
1971 Nkind (Parent (Prev)) /= N_Parameter_Association
1972 then
1973 Set_Next_Named_Actual
1974 (Insert_Param, First_Named_Actual (Call_Node));
1975 Set_First_Named_Actual (Call_Node, Actual_Expr);
1977 if No (Prev) then
1978 if No (Parameter_Associations (Call_Node)) then
1979 Set_Parameter_Associations (Call_Node, New_List);
1980 end if;
1982 Append (Insert_Param, Parameter_Associations (Call_Node));
1984 else
1985 Insert_After (Prev, Insert_Param);
1986 end if;
1988 -- Case of insertion is not first named actual
1990 else
1991 Set_Next_Named_Actual
1992 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1993 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1994 Append (Insert_Param, Parameter_Associations (Call_Node));
1995 end if;
1997 Prev := Actual_Expr;
1998 end Add_Actual_Parameter;
2000 ----------------------
2001 -- Add_Extra_Actual --
2002 ----------------------
2004 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2005 Loc : constant Source_Ptr := Sloc (Expr);
2007 begin
2008 if Extra_Actuals = No_List then
2009 Extra_Actuals := New_List;
2010 Set_Parent (Extra_Actuals, Call_Node);
2011 end if;
2013 Append_To (Extra_Actuals,
2014 Make_Parameter_Association (Loc,
2015 Selector_Name => Make_Identifier (Loc, Chars (EF)),
2016 Explicit_Actual_Parameter => Expr));
2018 Analyze_And_Resolve (Expr, Etype (EF));
2020 if Nkind (Call_Node) = N_Function_Call then
2021 Set_Is_Accessibility_Actual (Parent (Expr));
2022 end if;
2023 end Add_Extra_Actual;
2025 ----------------
2026 -- Do_Inline --
2027 ----------------
2029 procedure Do_Inline (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2030 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2032 procedure Do_Backend_Inline;
2033 -- Check that the call can be safely passed to the backend. If true
2034 -- then register the enclosing unit of Subp to Inlined_Bodies so that
2035 -- the body of Subp can be retrieved and analyzed by the backend.
2037 procedure Register_Backend_Call (N : Node_Id);
2038 -- Append N to the list Backend_Calls
2040 -----------------------
2041 -- Do_Backend_Inline --
2042 -----------------------
2044 procedure Do_Backend_Inline is
2045 begin
2046 -- No extra test needed for init subprograms since we know they
2047 -- are available to the backend.
2049 if Is_Init_Proc (Subp) then
2050 Add_Inlined_Body (Subp);
2051 Register_Backend_Call (Call_Node);
2053 -- Verify that if the body to inline is located in the current
2054 -- unit the inlining does not occur earlier. This avoids
2055 -- order-of-elaboration problems in the back end.
2057 elsif In_Same_Extended_Unit (Call_Node, Subp)
2058 and then Nkind (Spec) = N_Subprogram_Declaration
2059 and then Earlier_In_Extended_Unit
2060 (Loc, Sloc (Body_To_Inline (Spec)))
2061 then
2062 Error_Msg_NE
2063 ("cannot inline& (body not seen yet)??", Call_Node, Subp);
2065 else
2066 declare
2067 Backend_Inline : Boolean := True;
2069 begin
2070 -- If we are compiling a package body that is not the
2071 -- main unit, it must be for inlining/instantiation
2072 -- purposes, in which case we inline the call to insure
2073 -- that the same temporaries are generated when compiling
2074 -- the body by itself. Otherwise link errors can occur.
2076 -- If the function being called is itself in the main
2077 -- unit, we cannot inline, because there is a risk of
2078 -- double elaboration and/or circularity: the inlining
2079 -- can make visible a private entity in the body of the
2080 -- main unit, that gigi will see before its sees its
2081 -- proper definition.
2083 if not (In_Extended_Main_Code_Unit (Call_Node))
2084 and then In_Package_Body
2085 then
2086 Backend_Inline :=
2087 not In_Extended_Main_Source_Unit (Subp);
2088 end if;
2090 if Backend_Inline then
2091 Add_Inlined_Body (Subp);
2092 Register_Backend_Call (Call_Node);
2093 end if;
2094 end;
2095 end if;
2096 end Do_Backend_Inline;
2098 ---------------------------
2099 -- Register_Backend_Call --
2100 ---------------------------
2102 procedure Register_Backend_Call (N : Node_Id) is
2103 begin
2104 if Backend_Calls = No_Elist then
2105 Backend_Calls := New_Elmt_List;
2106 end if;
2108 Append_Elmt (N, To => Backend_Calls);
2109 end Register_Backend_Call;
2111 -- Start of processing for Do_Inline
2113 begin
2114 -- Verify that the body to inline has already been seen
2116 if No (Spec)
2117 or else Nkind (Spec) /= N_Subprogram_Declaration
2118 or else No (Body_To_Inline (Spec))
2119 then
2120 if Comes_From_Source (Subp)
2121 and then Must_Inline (Subp)
2122 then
2123 Cannot_Inline
2124 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2126 -- Let the back end handle it
2128 else
2129 Do_Backend_Inline;
2130 return;
2131 end if;
2133 -- If this an inherited function that returns a private type, do not
2134 -- inline if the full view is an unconstrained array, because such
2135 -- calls cannot be inlined.
2137 elsif Present (Orig_Subp)
2138 and then Is_Array_Type (Etype (Orig_Subp))
2139 and then not Is_Constrained (Etype (Orig_Subp))
2140 then
2141 Cannot_Inline
2142 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2144 else
2145 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2146 end if;
2147 end Do_Inline;
2149 ----------------------
2150 -- Do_Inline_Always --
2151 ----------------------
2153 procedure Do_Inline_Always (Subp : Entity_Id; Orig_Subp : Entity_Id) is
2154 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2155 Body_Id : Entity_Id;
2157 begin
2158 if No (Spec)
2159 or else Nkind (Spec) /= N_Subprogram_Declaration
2160 or else No (Body_To_Inline (Spec))
2161 or else Serious_Errors_Detected /= 0
2162 then
2163 return;
2164 end if;
2166 Body_Id := Corresponding_Body (Spec);
2168 -- Verify that the body to inline has already been seen
2170 if No (Body_Id)
2171 or else not Analyzed (Body_Id)
2172 then
2173 Set_Is_Inlined (Subp, False);
2175 if Comes_From_Source (Subp) then
2177 -- Report a warning only if the call is located in the unit of
2178 -- the called subprogram; otherwise it is an error.
2180 if not In_Same_Extended_Unit (Call_Node, Subp) then
2181 Cannot_Inline
2182 ("cannot inline& (body not seen yet)?", Call_Node, Subp,
2183 Is_Serious => True);
2185 elsif In_Open_Scopes (Subp) then
2187 -- For backward compatibility we generate the same error
2188 -- or warning of the previous implementation. This will
2189 -- be changed when we definitely incorporate the new
2190 -- support ???
2192 if Front_End_Inlining
2193 and then Optimization_Level = 0
2194 then
2195 Error_Msg_N
2196 ("call to recursive subprogram cannot be inlined?p?",
2199 -- Do not emit error compiling runtime packages
2201 elsif Is_Predefined_File_Name
2202 (Unit_File_Name (Get_Source_Unit (Subp)))
2203 then
2204 Error_Msg_N
2205 ("call to recursive subprogram cannot be inlined??",
2208 else
2209 Error_Msg_N
2210 ("call to recursive subprogram cannot be inlined",
2212 end if;
2214 else
2215 Cannot_Inline
2216 ("cannot inline& (body not seen yet)?", Call_Node, Subp);
2217 end if;
2218 end if;
2220 return;
2222 -- If this an inherited function that returns a private type, do not
2223 -- inline if the full view is an unconstrained array, because such
2224 -- calls cannot be inlined.
2226 elsif Present (Orig_Subp)
2227 and then Is_Array_Type (Etype (Orig_Subp))
2228 and then not Is_Constrained (Etype (Orig_Subp))
2229 then
2230 Cannot_Inline
2231 ("cannot inline& (unconstrained array)?", Call_Node, Subp);
2233 -- If the called subprogram comes from an instance in the same
2234 -- unit, and the instance is not yet frozen, inlining might
2235 -- trigger order-of-elaboration problems.
2237 elsif In_Unfrozen_Instance (Scope (Subp)) then
2238 Cannot_Inline
2239 ("cannot inline& (unfrozen instance)?", Call_Node, Subp);
2241 else
2242 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
2243 end if;
2244 end Do_Inline_Always;
2246 ---------------------------
2247 -- Inherited_From_Formal --
2248 ---------------------------
2250 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2251 Par : Entity_Id;
2252 Gen_Par : Entity_Id;
2253 Gen_Prim : Elist_Id;
2254 Elmt : Elmt_Id;
2255 Indic : Node_Id;
2257 begin
2258 -- If the operation is inherited, it is attached to the corresponding
2259 -- type derivation. If the parent in the derivation is a generic
2260 -- actual, it is a subtype of the actual, and we have to recover the
2261 -- original derived type declaration to find the proper parent.
2263 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2264 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2265 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2266 N_Derived_Type_Definition
2267 or else not In_Instance
2268 then
2269 return Empty;
2271 else
2272 Indic :=
2273 Subtype_Indication
2274 (Type_Definition (Original_Node (Parent (S))));
2276 if Nkind (Indic) = N_Subtype_Indication then
2277 Par := Entity (Subtype_Mark (Indic));
2278 else
2279 Par := Entity (Indic);
2280 end if;
2281 end if;
2283 if not Is_Generic_Actual_Type (Par)
2284 or else Is_Tagged_Type (Par)
2285 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2286 or else not In_Open_Scopes (Scope (Par))
2287 then
2288 return Empty;
2289 else
2290 Gen_Par := Generic_Parent_Type (Parent (Par));
2291 end if;
2293 -- If the actual has no generic parent type, the formal is not
2294 -- a formal derived type, so nothing to inherit.
2296 if No (Gen_Par) then
2297 return Empty;
2298 end if;
2300 -- If the generic parent type is still the generic type, this is a
2301 -- private formal, not a derived formal, and there are no operations
2302 -- inherited from the formal.
2304 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2305 return Empty;
2306 end if;
2308 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2310 Elmt := First_Elmt (Gen_Prim);
2311 while Present (Elmt) loop
2312 if Chars (Node (Elmt)) = Chars (S) then
2313 declare
2314 F1 : Entity_Id;
2315 F2 : Entity_Id;
2317 begin
2318 F1 := First_Formal (S);
2319 F2 := First_Formal (Node (Elmt));
2320 while Present (F1)
2321 and then Present (F2)
2322 loop
2323 if Etype (F1) = Etype (F2)
2324 or else Etype (F2) = Gen_Par
2325 then
2326 Next_Formal (F1);
2327 Next_Formal (F2);
2328 else
2329 Next_Elmt (Elmt);
2330 exit; -- not the right subprogram
2331 end if;
2333 return Node (Elmt);
2334 end loop;
2335 end;
2337 else
2338 Next_Elmt (Elmt);
2339 end if;
2340 end loop;
2342 raise Program_Error;
2343 end Inherited_From_Formal;
2345 --------------------------
2346 -- In_Unfrozen_Instance --
2347 --------------------------
2349 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2350 S : Entity_Id;
2352 begin
2353 S := E;
2354 while Present (S) and then S /= Standard_Standard loop
2355 if Is_Generic_Instance (S)
2356 and then Present (Freeze_Node (S))
2357 and then not Analyzed (Freeze_Node (S))
2358 then
2359 return True;
2360 end if;
2362 S := Scope (S);
2363 end loop;
2365 return False;
2366 end In_Unfrozen_Instance;
2368 -------------------------
2369 -- Is_Direct_Deep_Call --
2370 -------------------------
2372 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2373 begin
2374 if Is_TSS (Subp, TSS_Deep_Adjust)
2375 or else Is_TSS (Subp, TSS_Deep_Finalize)
2376 or else Is_TSS (Subp, TSS_Deep_Initialize)
2377 then
2378 declare
2379 Actual : Node_Id;
2380 Formal : Node_Id;
2382 begin
2383 Actual := First (Parameter_Associations (N));
2384 Formal := First_Formal (Subp);
2385 while Present (Actual)
2386 and then Present (Formal)
2387 loop
2388 if Nkind (Actual) = N_Identifier
2389 and then Is_Controlling_Actual (Actual)
2390 and then Etype (Actual) = Etype (Formal)
2391 then
2392 return True;
2393 end if;
2395 Next (Actual);
2396 Next_Formal (Formal);
2397 end loop;
2398 end;
2399 end if;
2401 return False;
2402 end Is_Direct_Deep_Call;
2404 ---------------
2405 -- New_Value --
2406 ---------------
2408 function New_Value (From : Node_Id) return Node_Id is
2409 Res : constant Node_Id := Duplicate_Subexpr (From);
2410 begin
2411 if Is_Access_Type (Etype (From)) then
2412 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2413 else
2414 return Res;
2415 end if;
2416 end New_Value;
2418 -- Local variables
2420 Curr_S : constant Entity_Id := Current_Scope;
2421 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2422 Actual : Node_Id;
2423 Formal : Entity_Id;
2424 Orig_Subp : Entity_Id := Empty;
2425 Param_Count : Natural := 0;
2426 Parent_Formal : Entity_Id;
2427 Parent_Subp : Entity_Id;
2428 Scop : Entity_Id;
2429 Subp : Entity_Id;
2431 Prev_Orig : Node_Id;
2432 -- Original node for an actual, which may have been rewritten. If the
2433 -- actual is a function call that has been transformed from a selected
2434 -- component, the original node is unanalyzed. Otherwise, it carries
2435 -- semantic information used to generate additional actuals.
2437 CW_Interface_Formals_Present : Boolean := False;
2439 -- Start of processing for Expand_Call
2441 begin
2442 -- Expand the procedure call if the first actual has a dimension and if
2443 -- the procedure is Put (Ada 2012).
2445 if Ada_Version >= Ada_2012
2446 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2447 and then Present (Parameter_Associations (Call_Node))
2448 then
2449 Expand_Put_Call_With_Symbol (Call_Node);
2450 end if;
2452 -- Ignore if previous error
2454 if Nkind (Call_Node) in N_Has_Etype
2455 and then Etype (Call_Node) = Any_Type
2456 then
2457 return;
2458 end if;
2460 -- Call using access to subprogram with explicit dereference
2462 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2463 Subp := Etype (Name (Call_Node));
2464 Parent_Subp := Empty;
2466 -- Case of call to simple entry, where the Name is a selected component
2467 -- whose prefix is the task, and whose selector name is the entry name
2469 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2470 Subp := Entity (Selector_Name (Name (Call_Node)));
2471 Parent_Subp := Empty;
2473 -- Case of call to member of entry family, where Name is an indexed
2474 -- component, with the prefix being a selected component giving the
2475 -- task and entry family name, and the index being the entry index.
2477 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2478 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2479 Parent_Subp := Empty;
2481 -- Normal case
2483 else
2484 Subp := Entity (Name (Call_Node));
2485 Parent_Subp := Alias (Subp);
2487 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2488 -- if we can tell that the first parameter cannot possibly be null.
2489 -- This improves efficiency by avoiding a run-time test.
2491 -- We do not do this if Raise_Exception_Always does not exist, which
2492 -- can happen in configurable run time profiles which provide only a
2493 -- Raise_Exception.
2495 if Is_RTE (Subp, RE_Raise_Exception)
2496 and then RTE_Available (RE_Raise_Exception_Always)
2497 then
2498 declare
2499 FA : constant Node_Id :=
2500 Original_Node (First_Actual (Call_Node));
2502 begin
2503 -- The case we catch is where the first argument is obtained
2504 -- using the Identity attribute (which must always be
2505 -- non-null).
2507 if Nkind (FA) = N_Attribute_Reference
2508 and then Attribute_Name (FA) = Name_Identity
2509 then
2510 Subp := RTE (RE_Raise_Exception_Always);
2511 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2512 end if;
2513 end;
2514 end if;
2516 if Ekind (Subp) = E_Entry then
2517 Parent_Subp := Empty;
2518 end if;
2519 end if;
2521 -- Detect the following code in System.Finalization_Masters only on
2522 -- .NET/JVM targets:
2524 -- procedure Finalize (Master : in out Finalization_Master) is
2525 -- begin
2526 -- . . .
2527 -- begin
2528 -- Finalize (Curr_Ptr.all);
2530 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2531 -- cannot be named in library or user code, the compiler has to install
2532 -- a kludge and transform the call to Finalize into Deep_Finalize.
2534 if VM_Target /= No_VM
2535 and then Chars (Subp) = Name_Finalize
2536 and then Ekind (Curr_S) = E_Block
2537 and then Ekind (Scope (Curr_S)) = E_Procedure
2538 and then Chars (Scope (Curr_S)) = Name_Finalize
2539 and then Etype (First_Formal (Scope (Curr_S))) =
2540 RTE (RE_Finalization_Master)
2541 then
2542 declare
2543 Deep_Fin : constant Entity_Id :=
2544 Find_Prim_Op (RTE (RE_Root_Controlled),
2545 TSS_Deep_Finalize);
2546 begin
2547 -- Since Root_Controlled is a tagged type, the compiler should
2548 -- always generate Deep_Finalize for it.
2550 pragma Assert (Present (Deep_Fin));
2552 -- Generate:
2553 -- Deep_Finalize (Curr_Ptr.all);
2555 Rewrite (N,
2556 Make_Procedure_Call_Statement (Loc,
2557 Name =>
2558 New_Occurrence_Of (Deep_Fin, Loc),
2559 Parameter_Associations =>
2560 New_Copy_List_Tree (Parameter_Associations (N))));
2562 Analyze (N);
2563 return;
2564 end;
2565 end if;
2567 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2568 -- alternative in an asynchronous select or as an entry call in
2569 -- a conditional or timed select. Check whether the procedure call
2570 -- is a renaming of an entry and rewrite it as an entry call.
2572 if Ada_Version >= Ada_2005
2573 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2574 and then
2575 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2576 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2577 or else
2578 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2579 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2580 then
2581 declare
2582 Ren_Decl : Node_Id;
2583 Ren_Root : Entity_Id := Subp;
2585 begin
2586 -- This may be a chain of renamings, find the root
2588 if Present (Alias (Ren_Root)) then
2589 Ren_Root := Alias (Ren_Root);
2590 end if;
2592 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2593 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2595 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2596 Rewrite (Call_Node,
2597 Make_Entry_Call_Statement (Loc,
2598 Name =>
2599 New_Copy_Tree (Name (Ren_Decl)),
2600 Parameter_Associations =>
2601 New_Copy_List_Tree
2602 (Parameter_Associations (Call_Node))));
2604 return;
2605 end if;
2606 end if;
2607 end;
2608 end if;
2610 -- First step, compute extra actuals, corresponding to any Extra_Formals
2611 -- present. Note that we do not access Extra_Formals directly, instead
2612 -- we simply note the presence of the extra formals as we process the
2613 -- regular formals collecting corresponding actuals in Extra_Actuals.
2615 -- We also generate any required range checks for actuals for in formals
2616 -- as we go through the loop, since this is a convenient place to do it.
2617 -- (Though it seems that this would be better done in Expand_Actuals???)
2619 -- Special case: Thunks must not compute the extra actuals; they must
2620 -- just propagate to the target primitive their extra actuals.
2622 if Is_Thunk (Current_Scope)
2623 and then Thunk_Entity (Current_Scope) = Subp
2624 and then Present (Extra_Formals (Subp))
2625 then
2626 pragma Assert (Present (Extra_Formals (Current_Scope)));
2628 declare
2629 Target_Formal : Entity_Id;
2630 Thunk_Formal : Entity_Id;
2632 begin
2633 Target_Formal := Extra_Formals (Subp);
2634 Thunk_Formal := Extra_Formals (Current_Scope);
2635 while Present (Target_Formal) loop
2636 Add_Extra_Actual
2637 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2639 Target_Formal := Extra_Formal (Target_Formal);
2640 Thunk_Formal := Extra_Formal (Thunk_Formal);
2641 end loop;
2643 while Is_Non_Empty_List (Extra_Actuals) loop
2644 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2645 end loop;
2647 Expand_Actuals (Call_Node, Subp);
2648 return;
2649 end;
2650 end if;
2652 Formal := First_Formal (Subp);
2653 Actual := First_Actual (Call_Node);
2654 Param_Count := 1;
2655 while Present (Formal) loop
2657 -- Generate range check if required
2659 if Do_Range_Check (Actual)
2660 and then Ekind (Formal) = E_In_Parameter
2661 then
2662 Set_Do_Range_Check (Actual, False);
2663 Generate_Range_Check
2664 (Actual, Etype (Formal), CE_Range_Check_Failed);
2665 end if;
2667 -- Prepare to examine current entry
2669 Prev := Actual;
2670 Prev_Orig := Original_Node (Prev);
2672 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2673 -- to expand it in a further round.
2675 CW_Interface_Formals_Present :=
2676 CW_Interface_Formals_Present
2677 or else
2678 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2679 and then Is_Interface (Etype (Etype (Formal))))
2680 or else
2681 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2682 and then Is_Interface (Directly_Designated_Type
2683 (Etype (Etype (Formal)))));
2685 -- Create possible extra actual for constrained case. Usually, the
2686 -- extra actual is of the form actual'constrained, but since this
2687 -- attribute is only available for unconstrained records, TRUE is
2688 -- expanded if the type of the formal happens to be constrained (for
2689 -- instance when this procedure is inherited from an unconstrained
2690 -- record to a constrained one) or if the actual has no discriminant
2691 -- (its type is constrained). An exception to this is the case of a
2692 -- private type without discriminants. In this case we pass FALSE
2693 -- because the object has underlying discriminants with defaults.
2695 if Present (Extra_Constrained (Formal)) then
2696 if Ekind (Etype (Prev)) in Private_Kind
2697 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2698 then
2699 Add_Extra_Actual
2700 (New_Occurrence_Of (Standard_False, Loc),
2701 Extra_Constrained (Formal));
2703 elsif Is_Constrained (Etype (Formal))
2704 or else not Has_Discriminants (Etype (Prev))
2705 then
2706 Add_Extra_Actual
2707 (New_Occurrence_Of (Standard_True, Loc),
2708 Extra_Constrained (Formal));
2710 -- Do not produce extra actuals for Unchecked_Union parameters.
2711 -- Jump directly to the end of the loop.
2713 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2714 goto Skip_Extra_Actual_Generation;
2716 else
2717 -- If the actual is a type conversion, then the constrained
2718 -- test applies to the actual, not the target type.
2720 declare
2721 Act_Prev : Node_Id;
2723 begin
2724 -- Test for unchecked conversions as well, which can occur
2725 -- as out parameter actuals on calls to stream procedures.
2727 Act_Prev := Prev;
2728 while Nkind_In (Act_Prev, N_Type_Conversion,
2729 N_Unchecked_Type_Conversion)
2730 loop
2731 Act_Prev := Expression (Act_Prev);
2732 end loop;
2734 -- If the expression is a conversion of a dereference, this
2735 -- is internally generated code that manipulates addresses,
2736 -- e.g. when building interface tables. No check should
2737 -- occur in this case, and the discriminated object is not
2738 -- directly a hand.
2740 if not Comes_From_Source (Actual)
2741 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2742 and then Nkind (Act_Prev) = N_Explicit_Dereference
2743 then
2744 Add_Extra_Actual
2745 (New_Occurrence_Of (Standard_False, Loc),
2746 Extra_Constrained (Formal));
2748 else
2749 Add_Extra_Actual
2750 (Make_Attribute_Reference (Sloc (Prev),
2751 Prefix =>
2752 Duplicate_Subexpr_No_Checks
2753 (Act_Prev, Name_Req => True),
2754 Attribute_Name => Name_Constrained),
2755 Extra_Constrained (Formal));
2756 end if;
2757 end;
2758 end if;
2759 end if;
2761 -- Create possible extra actual for accessibility level
2763 if Present (Extra_Accessibility (Formal)) then
2765 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2766 -- attribute, then the original actual may be an aliased object
2767 -- occurring as the prefix in a call using "Object.Operation"
2768 -- notation. In that case we must pass the level of the object,
2769 -- so Prev_Orig is reset to Prev and the attribute will be
2770 -- processed by the code for Access attributes further below.
2772 if Prev_Orig /= Prev
2773 and then Nkind (Prev) = N_Attribute_Reference
2774 and then
2775 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2776 and then Is_Aliased_View (Prev_Orig)
2777 then
2778 Prev_Orig := Prev;
2779 end if;
2781 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2782 -- accessibility levels.
2784 if Is_Thunk (Current_Scope) then
2785 declare
2786 Parm_Ent : Entity_Id;
2788 begin
2789 if Is_Controlling_Actual (Actual) then
2791 -- Find the corresponding actual of the thunk
2793 Parm_Ent := First_Entity (Current_Scope);
2794 for J in 2 .. Param_Count loop
2795 Next_Entity (Parm_Ent);
2796 end loop;
2798 -- Handle unchecked conversion of access types generated
2799 -- in thunks (cf. Expand_Interface_Thunk).
2801 elsif Is_Access_Type (Etype (Actual))
2802 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2803 then
2804 Parm_Ent := Entity (Expression (Actual));
2806 else pragma Assert (Is_Entity_Name (Actual));
2807 Parm_Ent := Entity (Actual);
2808 end if;
2810 Add_Extra_Actual
2811 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2812 Extra_Accessibility (Formal));
2813 end;
2815 elsif Is_Entity_Name (Prev_Orig) then
2817 -- When passing an access parameter, or a renaming of an access
2818 -- parameter, as the actual to another access parameter we need
2819 -- to pass along the actual's own access level parameter. This
2820 -- is done if we are within the scope of the formal access
2821 -- parameter (if this is an inlined body the extra formal is
2822 -- irrelevant).
2824 if (Is_Formal (Entity (Prev_Orig))
2825 or else
2826 (Present (Renamed_Object (Entity (Prev_Orig)))
2827 and then
2828 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2829 and then
2830 Is_Formal
2831 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2832 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2833 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2834 then
2835 declare
2836 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2838 begin
2839 pragma Assert (Present (Parm_Ent));
2841 if Present (Extra_Accessibility (Parm_Ent)) then
2842 Add_Extra_Actual
2843 (New_Occurrence_Of
2844 (Extra_Accessibility (Parm_Ent), Loc),
2845 Extra_Accessibility (Formal));
2847 -- If the actual access parameter does not have an
2848 -- associated extra formal providing its scope level,
2849 -- then treat the actual as having library-level
2850 -- accessibility.
2852 else
2853 Add_Extra_Actual
2854 (Make_Integer_Literal (Loc,
2855 Intval => Scope_Depth (Standard_Standard)),
2856 Extra_Accessibility (Formal));
2857 end if;
2858 end;
2860 -- The actual is a normal access value, so just pass the level
2861 -- of the actual's access type.
2863 else
2864 Add_Extra_Actual
2865 (Dynamic_Accessibility_Level (Prev_Orig),
2866 Extra_Accessibility (Formal));
2867 end if;
2869 -- If the actual is an access discriminant, then pass the level
2870 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2872 elsif Nkind (Prev_Orig) = N_Selected_Component
2873 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2874 E_Discriminant
2875 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2876 E_Anonymous_Access_Type
2877 then
2878 Add_Extra_Actual
2879 (Make_Integer_Literal (Loc,
2880 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2881 Extra_Accessibility (Formal));
2883 -- All other cases
2885 else
2886 case Nkind (Prev_Orig) is
2888 when N_Attribute_Reference =>
2889 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2891 -- For X'Access, pass on the level of the prefix X
2893 when Attribute_Access =>
2895 -- If this is an Access attribute applied to the
2896 -- the current instance object passed to a type
2897 -- initialization procedure, then use the level
2898 -- of the type itself. This is not really correct,
2899 -- as there should be an extra level parameter
2900 -- passed in with _init formals (only in the case
2901 -- where the type is immutably limited), but we
2902 -- don't have an easy way currently to create such
2903 -- an extra formal (init procs aren't ever frozen).
2904 -- For now we just use the level of the type,
2905 -- which may be too shallow, but that works better
2906 -- than passing Object_Access_Level of the type,
2907 -- which can be one level too deep in some cases.
2908 -- ???
2910 if Is_Entity_Name (Prefix (Prev_Orig))
2911 and then Is_Type (Entity (Prefix (Prev_Orig)))
2912 then
2913 Add_Extra_Actual
2914 (Make_Integer_Literal (Loc,
2915 Intval =>
2916 Type_Access_Level
2917 (Entity (Prefix (Prev_Orig)))),
2918 Extra_Accessibility (Formal));
2920 else
2921 Add_Extra_Actual
2922 (Make_Integer_Literal (Loc,
2923 Intval =>
2924 Object_Access_Level
2925 (Prefix (Prev_Orig))),
2926 Extra_Accessibility (Formal));
2927 end if;
2929 -- Treat the unchecked attributes as library-level
2931 when Attribute_Unchecked_Access |
2932 Attribute_Unrestricted_Access =>
2933 Add_Extra_Actual
2934 (Make_Integer_Literal (Loc,
2935 Intval => Scope_Depth (Standard_Standard)),
2936 Extra_Accessibility (Formal));
2938 -- No other cases of attributes returning access
2939 -- values that can be passed to access parameters.
2941 when others =>
2942 raise Program_Error;
2944 end case;
2946 -- For allocators we pass the level of the execution of the
2947 -- called subprogram, which is one greater than the current
2948 -- scope level.
2950 when N_Allocator =>
2951 Add_Extra_Actual
2952 (Make_Integer_Literal (Loc,
2953 Intval => Scope_Depth (Current_Scope) + 1),
2954 Extra_Accessibility (Formal));
2956 -- For most other cases we simply pass the level of the
2957 -- actual's access type. The type is retrieved from
2958 -- Prev rather than Prev_Orig, because in some cases
2959 -- Prev_Orig denotes an original expression that has
2960 -- not been analyzed.
2962 when others =>
2963 Add_Extra_Actual
2964 (Dynamic_Accessibility_Level (Prev),
2965 Extra_Accessibility (Formal));
2966 end case;
2967 end if;
2968 end if;
2970 -- Perform the check of 4.6(49) that prevents a null value from being
2971 -- passed as an actual to an access parameter. Note that the check
2972 -- is elided in the common cases of passing an access attribute or
2973 -- access parameter as an actual. Also, we currently don't enforce
2974 -- this check for expander-generated actuals and when -gnatdj is set.
2976 if Ada_Version >= Ada_2005 then
2978 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2979 -- the intent of 6.4.1(13) is that null-exclusion checks should
2980 -- not be done for 'out' parameters, even though it refers only
2981 -- to constraint checks, and a null_exclusion is not a constraint.
2982 -- Note that AI05-0196-1 corrects this mistake in the RM.
2984 if Is_Access_Type (Etype (Formal))
2985 and then Can_Never_Be_Null (Etype (Formal))
2986 and then Ekind (Formal) /= E_Out_Parameter
2987 and then Nkind (Prev) /= N_Raise_Constraint_Error
2988 and then (Known_Null (Prev)
2989 or else not Can_Never_Be_Null (Etype (Prev)))
2990 then
2991 Install_Null_Excluding_Check (Prev);
2992 end if;
2994 -- Ada_Version < Ada_2005
2996 else
2997 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2998 or else Access_Checks_Suppressed (Subp)
2999 then
3000 null;
3002 elsif Debug_Flag_J then
3003 null;
3005 elsif not Comes_From_Source (Prev) then
3006 null;
3008 elsif Is_Entity_Name (Prev)
3009 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
3010 then
3011 null;
3013 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
3014 null;
3016 -- Suppress null checks when passing to access parameters of Java
3017 -- and CIL subprograms. (Should this be done for other foreign
3018 -- conventions as well ???)
3020 elsif Convention (Subp) = Convention_Java
3021 or else Convention (Subp) = Convention_CIL
3022 then
3023 null;
3025 else
3026 Install_Null_Excluding_Check (Prev);
3027 end if;
3028 end if;
3030 -- Perform appropriate validity checks on parameters that
3031 -- are entities.
3033 if Validity_Checks_On then
3034 if (Ekind (Formal) = E_In_Parameter
3035 and then Validity_Check_In_Params)
3036 or else
3037 (Ekind (Formal) = E_In_Out_Parameter
3038 and then Validity_Check_In_Out_Params)
3039 then
3040 -- If the actual is an indexed component of a packed type (or
3041 -- is an indexed or selected component whose prefix recursively
3042 -- meets this condition), it has not been expanded yet. It will
3043 -- be copied in the validity code that follows, and has to be
3044 -- expanded appropriately, so reanalyze it.
3046 -- What we do is just to unset analyzed bits on prefixes till
3047 -- we reach something that does not have a prefix.
3049 declare
3050 Nod : Node_Id;
3052 begin
3053 Nod := Actual;
3054 while Nkind_In (Nod, N_Indexed_Component,
3055 N_Selected_Component)
3056 loop
3057 Set_Analyzed (Nod, False);
3058 Nod := Prefix (Nod);
3059 end loop;
3060 end;
3062 Ensure_Valid (Actual);
3063 end if;
3064 end if;
3066 -- For Ada 2012, if a parameter is aliased, the actual must be a
3067 -- tagged type or an aliased view of an object.
3069 if Is_Aliased (Formal)
3070 and then not Is_Aliased_View (Actual)
3071 and then not Is_Tagged_Type (Etype (Formal))
3072 then
3073 Error_Msg_NE
3074 ("actual for aliased formal& must be aliased object",
3075 Actual, Formal);
3076 end if;
3078 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3079 -- since this is a left side reference. We only do this for calls
3080 -- from the source program since we assume that compiler generated
3081 -- calls explicitly generate any required checks. We also need it
3082 -- only if we are doing standard validity checks, since clearly it is
3083 -- not needed if validity checks are off, and in subscript validity
3084 -- checking mode, all indexed components are checked with a call
3085 -- directly from Expand_N_Indexed_Component.
3087 if Comes_From_Source (Call_Node)
3088 and then Ekind (Formal) /= E_In_Parameter
3089 and then Validity_Checks_On
3090 and then Validity_Check_Default
3091 and then not Validity_Check_Subscripts
3092 then
3093 Check_Valid_Lvalue_Subscripts (Actual);
3094 end if;
3096 -- Mark any scalar OUT parameter that is a simple variable as no
3097 -- longer known to be valid (unless the type is always valid). This
3098 -- reflects the fact that if an OUT parameter is never set in a
3099 -- procedure, then it can become invalid on the procedure return.
3101 if Ekind (Formal) = E_Out_Parameter
3102 and then Is_Entity_Name (Actual)
3103 and then Ekind (Entity (Actual)) = E_Variable
3104 and then not Is_Known_Valid (Etype (Actual))
3105 then
3106 Set_Is_Known_Valid (Entity (Actual), False);
3107 end if;
3109 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3110 -- clear current values, since they can be clobbered. We are probably
3111 -- doing this in more places than we need to, but better safe than
3112 -- sorry when it comes to retaining bad current values.
3114 if Ekind (Formal) /= E_In_Parameter
3115 and then Is_Entity_Name (Actual)
3116 and then Present (Entity (Actual))
3117 then
3118 declare
3119 Ent : constant Entity_Id := Entity (Actual);
3120 Sav : Node_Id;
3122 begin
3123 -- For an OUT or IN OUT parameter that is an assignable entity,
3124 -- we do not want to clobber the Last_Assignment field, since
3125 -- if it is set, it was precisely because it is indeed an OUT
3126 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3127 -- since the subprogram could have returned in invalid value.
3129 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3130 and then Is_Assignable (Ent)
3131 then
3132 Sav := Last_Assignment (Ent);
3133 Kill_Current_Values (Ent);
3134 Set_Last_Assignment (Ent, Sav);
3135 Set_Is_Known_Valid (Ent, False);
3137 -- For all other cases, just kill the current values
3139 else
3140 Kill_Current_Values (Ent);
3141 end if;
3142 end;
3143 end if;
3145 -- If the formal is class wide and the actual is an aggregate, force
3146 -- evaluation so that the back end who does not know about class-wide
3147 -- type, does not generate a temporary of the wrong size.
3149 if not Is_Class_Wide_Type (Etype (Formal)) then
3150 null;
3152 elsif Nkind (Actual) = N_Aggregate
3153 or else (Nkind (Actual) = N_Qualified_Expression
3154 and then Nkind (Expression (Actual)) = N_Aggregate)
3155 then
3156 Force_Evaluation (Actual);
3157 end if;
3159 -- In a remote call, if the formal is of a class-wide type, check
3160 -- that the actual meets the requirements described in E.4(18).
3162 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3163 Insert_Action (Actual,
3164 Make_Transportable_Check (Loc,
3165 Duplicate_Subexpr_Move_Checks (Actual)));
3166 end if;
3168 -- This label is required when skipping extra actual generation for
3169 -- Unchecked_Union parameters.
3171 <<Skip_Extra_Actual_Generation>>
3173 Param_Count := Param_Count + 1;
3174 Next_Actual (Actual);
3175 Next_Formal (Formal);
3176 end loop;
3178 -- If we are calling an Ada 2012 function which needs to have the
3179 -- "accessibility level determined by the point of call" (AI05-0234)
3180 -- passed in to it, then pass it in.
3182 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3183 and then
3184 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3185 then
3186 declare
3187 Ancestor : Node_Id := Parent (Call_Node);
3188 Level : Node_Id := Empty;
3189 Defer : Boolean := False;
3191 begin
3192 -- Unimplemented: if Subp returns an anonymous access type, then
3194 -- a) if the call is the operand of an explict conversion, then
3195 -- the target type of the conversion (a named access type)
3196 -- determines the accessibility level pass in;
3198 -- b) if the call defines an access discriminant of an object
3199 -- (e.g., the discriminant of an object being created by an
3200 -- allocator, or the discriminant of a function result),
3201 -- then the accessibility level to pass in is that of the
3202 -- discriminated object being initialized).
3204 -- ???
3206 while Nkind (Ancestor) = N_Qualified_Expression
3207 loop
3208 Ancestor := Parent (Ancestor);
3209 end loop;
3211 case Nkind (Ancestor) is
3212 when N_Allocator =>
3214 -- At this point, we'd like to assign
3216 -- Level := Dynamic_Accessibility_Level (Ancestor);
3218 -- but Etype of Ancestor may not have been set yet,
3219 -- so that doesn't work.
3221 -- Handle this later in Expand_Allocator_Expression.
3223 Defer := True;
3225 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3226 declare
3227 Def_Id : constant Entity_Id :=
3228 Defining_Identifier (Ancestor);
3230 begin
3231 if Is_Return_Object (Def_Id) then
3232 if Present (Extra_Accessibility_Of_Result
3233 (Return_Applies_To (Scope (Def_Id))))
3234 then
3235 -- Pass along value that was passed in if the
3236 -- routine we are returning from also has an
3237 -- Accessibility_Of_Result formal.
3239 Level :=
3240 New_Occurrence_Of
3241 (Extra_Accessibility_Of_Result
3242 (Return_Applies_To (Scope (Def_Id))), Loc);
3243 end if;
3244 else
3245 Level :=
3246 Make_Integer_Literal (Loc,
3247 Intval => Object_Access_Level (Def_Id));
3248 end if;
3249 end;
3251 when N_Simple_Return_Statement =>
3252 if Present (Extra_Accessibility_Of_Result
3253 (Return_Applies_To
3254 (Return_Statement_Entity (Ancestor))))
3255 then
3256 -- Pass along value that was passed in if the returned
3257 -- routine also has an 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_Occurrence_Of
3421 (First_Tag_Component (Typ), 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_Occurrence_Of
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 -- Deal with case where call is an explicit dereference
3646 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3648 -- Handle case of access to protected subprogram type
3650 if Is_Access_Protected_Subprogram_Type
3651 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3652 then
3653 -- If this is a call through an access to protected operation, the
3654 -- prefix has the form (object'address, operation'access). Rewrite
3655 -- as a for other protected calls: the object is the 1st parameter
3656 -- of the list of actuals.
3658 declare
3659 Call : Node_Id;
3660 Parm : List_Id;
3661 Nam : Node_Id;
3662 Obj : Node_Id;
3663 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3665 T : constant Entity_Id :=
3666 Equivalent_Type (Base_Type (Etype (Ptr)));
3668 D_T : constant Entity_Id :=
3669 Designated_Type (Base_Type (Etype (Ptr)));
3671 begin
3672 Obj :=
3673 Make_Selected_Component (Loc,
3674 Prefix => Unchecked_Convert_To (T, Ptr),
3675 Selector_Name =>
3676 New_Occurrence_Of (First_Entity (T), Loc));
3678 Nam :=
3679 Make_Selected_Component (Loc,
3680 Prefix => Unchecked_Convert_To (T, Ptr),
3681 Selector_Name =>
3682 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3684 Nam :=
3685 Make_Explicit_Dereference (Loc,
3686 Prefix => Nam);
3688 if Present (Parameter_Associations (Call_Node)) then
3689 Parm := Parameter_Associations (Call_Node);
3690 else
3691 Parm := New_List;
3692 end if;
3694 Prepend (Obj, Parm);
3696 if Etype (D_T) = Standard_Void_Type then
3697 Call :=
3698 Make_Procedure_Call_Statement (Loc,
3699 Name => Nam,
3700 Parameter_Associations => Parm);
3701 else
3702 Call :=
3703 Make_Function_Call (Loc,
3704 Name => Nam,
3705 Parameter_Associations => Parm);
3706 end if;
3708 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3709 Set_Etype (Call, Etype (D_T));
3711 -- We do not re-analyze the call to avoid infinite recursion.
3712 -- We analyze separately the prefix and the object, and set
3713 -- the checks on the prefix that would otherwise be emitted
3714 -- when resolving a call.
3716 Rewrite (Call_Node, Call);
3717 Analyze (Nam);
3718 Apply_Access_Check (Nam);
3719 Analyze (Obj);
3720 return;
3721 end;
3722 end if;
3723 end if;
3725 -- If this is a call to an intrinsic subprogram, then perform the
3726 -- appropriate expansion to the corresponding tree node and we
3727 -- are all done (since after that the call is gone).
3729 -- In the case where the intrinsic is to be processed by the back end,
3730 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3731 -- since the idea in this case is to pass the call unchanged. If the
3732 -- intrinsic is an inherited unchecked conversion, and the derived type
3733 -- is the target type of the conversion, we must retain it as the return
3734 -- type of the expression. Otherwise the expansion below, which uses the
3735 -- parent operation, will yield the wrong type.
3737 if Is_Intrinsic_Subprogram (Subp) then
3738 Expand_Intrinsic_Call (Call_Node, Subp);
3740 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3741 and then Parent_Subp /= Orig_Subp
3742 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3743 then
3744 Set_Etype (Call_Node, Etype (Orig_Subp));
3745 end if;
3747 return;
3748 end if;
3750 if Ekind_In (Subp, E_Function, E_Procedure) then
3752 -- We perform two simple optimization on calls:
3754 -- a) replace calls to null procedures unconditionally;
3756 -- b) for To_Address, just do an unchecked conversion. Not only is
3757 -- this efficient, but it also avoids order of elaboration problems
3758 -- when address clauses are inlined (address expression elaborated
3759 -- at the wrong point).
3761 -- We perform these optimization regardless of whether we are in the
3762 -- main unit or in a unit in the context of the main unit, to ensure
3763 -- that tree generated is the same in both cases, for CodePeer use.
3765 if Is_RTE (Subp, RE_To_Address) then
3766 Rewrite (Call_Node,
3767 Unchecked_Convert_To
3768 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3769 return;
3771 elsif Is_Null_Procedure (Subp) then
3772 Rewrite (Call_Node, Make_Null_Statement (Loc));
3773 return;
3774 end if;
3776 -- Handle inlining (old semantics)
3778 if Is_Inlined (Subp) and then not Debug_Flag_Dot_K then
3779 Inlined_Subprogram : declare
3780 Bod : Node_Id;
3781 Must_Inline : Boolean := False;
3782 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3784 begin
3785 -- Verify that the body to inline has already been seen, and
3786 -- that if the body is in the current unit the inlining does
3787 -- not occur earlier. This avoids order-of-elaboration problems
3788 -- in the back end.
3790 -- This should be documented in sinfo/einfo ???
3792 if No (Spec)
3793 or else Nkind (Spec) /= N_Subprogram_Declaration
3794 or else No (Body_To_Inline (Spec))
3795 then
3796 Must_Inline := False;
3798 -- If this an inherited function that returns a private type,
3799 -- do not inline if the full view is an unconstrained array,
3800 -- because such calls cannot be inlined.
3802 elsif Present (Orig_Subp)
3803 and then Is_Array_Type (Etype (Orig_Subp))
3804 and then not Is_Constrained (Etype (Orig_Subp))
3805 then
3806 Must_Inline := False;
3808 elsif In_Unfrozen_Instance (Scope (Subp)) then
3809 Must_Inline := False;
3811 else
3812 Bod := Body_To_Inline (Spec);
3814 if (In_Extended_Main_Code_Unit (Call_Node)
3815 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3816 or else Has_Pragma_Inline_Always (Subp))
3817 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3818 or else
3819 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3820 then
3821 Must_Inline := True;
3823 -- If we are compiling a package body that is not the main
3824 -- unit, it must be for inlining/instantiation purposes,
3825 -- in which case we inline the call to insure that the same
3826 -- temporaries are generated when compiling the body by
3827 -- itself. Otherwise link errors can occur.
3829 -- If the function being called is itself in the main unit,
3830 -- we cannot inline, because there is a risk of double
3831 -- elaboration and/or circularity: the inlining can make
3832 -- visible a private entity in the body of the main unit,
3833 -- that gigi will see before its sees its proper definition.
3835 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3836 and then In_Package_Body
3837 then
3838 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3839 end if;
3840 end if;
3842 if Must_Inline then
3843 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3845 else
3846 -- Let the back end handle it
3848 Add_Inlined_Body (Subp);
3850 if Front_End_Inlining
3851 and then Nkind (Spec) = N_Subprogram_Declaration
3852 and then (In_Extended_Main_Code_Unit (Call_Node))
3853 and then No (Body_To_Inline (Spec))
3854 and then not Has_Completion (Subp)
3855 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3856 then
3857 Cannot_Inline
3858 ("cannot inline& (body not seen yet)?",
3859 Call_Node, Subp);
3860 end if;
3861 end if;
3862 end Inlined_Subprogram;
3864 -- Handle inlining (new semantics)
3866 elsif Is_Inlined (Subp) then
3867 declare
3868 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3870 begin
3871 if Must_Inline (Subp) then
3872 if In_Extended_Main_Code_Unit (Call_Node)
3873 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3874 and then not Has_Completion (Subp)
3875 then
3876 Cannot_Inline
3877 ("cannot inline& (body not seen yet)?",
3878 Call_Node, Subp);
3880 else
3881 Do_Inline_Always (Subp, Orig_Subp);
3882 end if;
3884 elsif Optimization_Level > 0 then
3885 Do_Inline (Subp, Orig_Subp);
3886 end if;
3888 -- The call may have been inlined or may have been passed to
3889 -- the backend. No further action needed if it was inlined.
3891 if Nkind (N) /= N_Function_Call then
3892 return;
3893 end if;
3894 end;
3895 end if;
3896 end if;
3898 -- Check for protected subprogram. This is either an intra-object call,
3899 -- or a protected function call. Protected procedure calls are rewritten
3900 -- as entry calls and handled accordingly.
3902 -- In Ada 2005, this may be an indirect call to an access parameter that
3903 -- is an access_to_subprogram. In that case the anonymous type has a
3904 -- scope that is a protected operation, but the call is a regular one.
3905 -- In either case do not expand call if subprogram is eliminated.
3907 Scop := Scope (Subp);
3909 if Nkind (Call_Node) /= N_Entry_Call_Statement
3910 and then Is_Protected_Type (Scop)
3911 and then Ekind (Subp) /= E_Subprogram_Type
3912 and then not Is_Eliminated (Subp)
3913 then
3914 -- If the call is an internal one, it is rewritten as a call to the
3915 -- corresponding unprotected subprogram.
3917 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3918 end if;
3920 -- Functions returning controlled objects need special attention. If
3921 -- the return type is limited, then the context is initialization and
3922 -- different processing applies. If the call is to a protected function,
3923 -- the expansion above will call Expand_Call recursively. Otherwise the
3924 -- function call is transformed into a temporary which obtains the
3925 -- result from the secondary stack.
3927 if Needs_Finalization (Etype (Subp)) then
3928 if not Is_Limited_View (Etype (Subp))
3929 and then
3930 (No (First_Formal (Subp))
3931 or else
3932 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3933 then
3934 Expand_Ctrl_Function_Call (Call_Node);
3936 -- Build-in-place function calls which appear in anonymous contexts
3937 -- need a transient scope to ensure the proper finalization of the
3938 -- intermediate result after its use.
3940 elsif Is_Build_In_Place_Function_Call (Call_Node)
3941 and then
3942 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3943 N_Function_Call,
3944 N_Indexed_Component,
3945 N_Object_Renaming_Declaration,
3946 N_Procedure_Call_Statement,
3947 N_Selected_Component,
3948 N_Slice)
3949 then
3950 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3951 end if;
3952 end if;
3954 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3955 -- if there are optional parameters at the trailing end.
3956 -- Note: we never delete procedures for call via a pointer.
3958 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3959 and then Present (First_Optional_Parameter (Subp))
3960 then
3961 declare
3962 Last_Keep_Arg : Node_Id;
3964 begin
3965 -- Last_Keep_Arg will hold the last actual that should be kept.
3966 -- If it remains empty at the end, it means that all parameters
3967 -- are optional.
3969 Last_Keep_Arg := Empty;
3971 -- Find first optional parameter, must be present since we checked
3972 -- the validity of the parameter before setting it.
3974 Formal := First_Formal (Subp);
3975 Actual := First_Actual (Call_Node);
3976 while Formal /= First_Optional_Parameter (Subp) loop
3977 Last_Keep_Arg := Actual;
3978 Next_Formal (Formal);
3979 Next_Actual (Actual);
3980 end loop;
3982 -- We have Formal and Actual pointing to the first potentially
3983 -- droppable argument. We can drop all the trailing arguments
3984 -- whose actual matches the default. Note that we know that all
3985 -- remaining formals have defaults, because we checked that this
3986 -- requirement was met before setting First_Optional_Parameter.
3988 -- We use Fully_Conformant_Expressions to check for identity
3989 -- between formals and actuals, which may miss some cases, but
3990 -- on the other hand, this is only an optimization (if we fail
3991 -- to truncate a parameter it does not affect functionality).
3992 -- So if the default is 3 and the actual is 1+2, we consider
3993 -- them unequal, which hardly seems worrisome.
3995 while Present (Formal) loop
3996 if not Fully_Conformant_Expressions
3997 (Actual, Default_Value (Formal))
3998 then
3999 Last_Keep_Arg := Actual;
4000 end if;
4002 Next_Formal (Formal);
4003 Next_Actual (Actual);
4004 end loop;
4006 -- If no arguments, delete entire list, this is the easy case
4008 if No (Last_Keep_Arg) then
4009 Set_Parameter_Associations (Call_Node, No_List);
4010 Set_First_Named_Actual (Call_Node, Empty);
4012 -- Case where at the last retained argument is positional. This
4013 -- is also an easy case, since the retained arguments are already
4014 -- in the right form, and we don't need to worry about the order
4015 -- of arguments that get eliminated.
4017 elsif Is_List_Member (Last_Keep_Arg) then
4018 while Present (Next (Last_Keep_Arg)) loop
4019 Discard_Node (Remove_Next (Last_Keep_Arg));
4020 end loop;
4022 Set_First_Named_Actual (Call_Node, Empty);
4024 -- This is the annoying case where the last retained argument
4025 -- is a named parameter. Since the original arguments are not
4026 -- in declaration order, we may have to delete some fairly
4027 -- random collection of arguments.
4029 else
4030 declare
4031 Temp : Node_Id;
4032 Passoc : Node_Id;
4034 begin
4035 -- First step, remove all the named parameters from the
4036 -- list (they are still chained using First_Named_Actual
4037 -- and Next_Named_Actual, so we have not lost them).
4039 Temp := First (Parameter_Associations (Call_Node));
4041 -- Case of all parameters named, remove them all
4043 if Nkind (Temp) = N_Parameter_Association then
4044 -- Suppress warnings to avoid warning on possible
4045 -- infinite loop (because Call_Node is not modified).
4047 pragma Warnings (Off);
4048 while Is_Non_Empty_List
4049 (Parameter_Associations (Call_Node))
4050 loop
4051 Temp :=
4052 Remove_Head (Parameter_Associations (Call_Node));
4053 end loop;
4054 pragma Warnings (On);
4056 -- Case of mixed positional/named, remove named parameters
4058 else
4059 while Nkind (Next (Temp)) /= N_Parameter_Association loop
4060 Next (Temp);
4061 end loop;
4063 while Present (Next (Temp)) loop
4064 Remove (Next (Temp));
4065 end loop;
4066 end if;
4068 -- Now we loop through the named parameters, till we get
4069 -- to the last one to be retained, adding them to the list.
4070 -- Note that the Next_Named_Actual list does not need to be
4071 -- touched since we are only reordering them on the actual
4072 -- parameter association list.
4074 Passoc := Parent (First_Named_Actual (Call_Node));
4075 loop
4076 Temp := Relocate_Node (Passoc);
4077 Append_To
4078 (Parameter_Associations (Call_Node), Temp);
4079 exit when
4080 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
4081 Passoc := Parent (Next_Named_Actual (Passoc));
4082 end loop;
4084 Set_Next_Named_Actual (Temp, Empty);
4086 loop
4087 Temp := Next_Named_Actual (Passoc);
4088 exit when No (Temp);
4089 Set_Next_Named_Actual
4090 (Passoc, Next_Named_Actual (Parent (Temp)));
4091 end loop;
4092 end;
4094 end if;
4095 end;
4096 end if;
4097 end Expand_Call;
4099 -------------------------------
4100 -- Expand_Ctrl_Function_Call --
4101 -------------------------------
4103 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
4104 begin
4105 -- Optimization, if the returned value (which is on the sec-stack) is
4106 -- returned again, no need to copy/readjust/finalize, we can just pass
4107 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
4108 -- attachment is needed
4110 if Nkind (Parent (N)) = N_Simple_Return_Statement then
4111 return;
4112 end if;
4114 -- Resolution is now finished, make sure we don't start analysis again
4115 -- because of the duplication.
4117 Set_Analyzed (N);
4119 -- A function which returns a controlled object uses the secondary
4120 -- stack. Rewrite the call into a temporary which obtains the result of
4121 -- the function using 'reference.
4123 Remove_Side_Effects (N);
4125 -- When the temporary function result appears inside a case or an if
4126 -- expression, its lifetime must be extended to match that of the
4127 -- context. If not, the function result would be finalized prematurely
4128 -- and the evaluation of the expression could yield the wrong result.
4130 if Within_Case_Or_If_Expression (N)
4131 and then Nkind (N) = N_Explicit_Dereference
4132 then
4133 Set_Is_Processed_Transient (Entity (Prefix (N)));
4134 end if;
4135 end Expand_Ctrl_Function_Call;
4137 -------------------------
4138 -- Expand_Inlined_Call --
4139 -------------------------
4141 procedure Expand_Inlined_Call
4142 (N : Node_Id;
4143 Subp : Entity_Id;
4144 Orig_Subp : Entity_Id)
4146 Loc : constant Source_Ptr := Sloc (N);
4147 Is_Predef : constant Boolean :=
4148 Is_Predefined_File_Name
4149 (Unit_File_Name (Get_Source_Unit (Subp)));
4150 Orig_Bod : constant Node_Id :=
4151 Body_To_Inline (Unit_Declaration_Node (Subp));
4153 Blk : Node_Id;
4154 Decl : Node_Id;
4155 Decls : constant List_Id := New_List;
4156 Exit_Lab : Entity_Id := Empty;
4157 F : Entity_Id;
4158 A : Node_Id;
4159 Lab_Decl : Node_Id;
4160 Lab_Id : Node_Id;
4161 New_A : Node_Id;
4162 Num_Ret : Int := 0;
4163 Ret_Type : Entity_Id;
4165 Targ : Node_Id;
4166 -- The target of the call. If context is an assignment statement then
4167 -- this is the left-hand side of the assignment, else it is a temporary
4168 -- to which the return value is assigned prior to rewriting the call.
4170 Targ1 : Node_Id;
4171 -- A separate target used when the return type is unconstrained
4173 Temp : Entity_Id;
4174 Temp_Typ : Entity_Id;
4176 Return_Object : Entity_Id := Empty;
4177 -- Entity in declaration in an extended_return_statement
4179 Is_Unc : Boolean;
4180 Is_Unc_Decl : Boolean;
4181 -- If the type returned by the function is unconstrained and the call
4182 -- can be inlined, special processing is required.
4184 procedure Make_Exit_Label;
4185 -- Build declaration for exit label to be used in Return statements,
4186 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
4187 -- declaration). Does nothing if Exit_Lab already set.
4189 function Process_Formals (N : Node_Id) return Traverse_Result;
4190 -- Replace occurrence of a formal with the corresponding actual, or the
4191 -- thunk generated for it. Replace a return statement with an assignment
4192 -- to the target of the call, with appropriate conversions if needed.
4194 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
4195 -- If the call being expanded is that of an internal subprogram, set the
4196 -- sloc of the generated block to that of the call itself, so that the
4197 -- expansion is skipped by the "next" command in gdb.
4198 -- Same processing for a subprogram in a predefined file, e.g.
4199 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
4200 -- simplify our own development.
4202 procedure Reset_Dispatching_Calls (N : Node_Id);
4203 -- In subtree N search for occurrences of dispatching calls that use the
4204 -- Ada 2005 Object.Operation notation and the object is a formal of the
4205 -- inlined subprogram. Reset the entity associated with Operation in all
4206 -- the found occurrences.
4208 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
4209 -- If the function body is a single expression, replace call with
4210 -- expression, else insert block appropriately.
4212 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
4213 -- If procedure body has no local variables, inline body without
4214 -- creating block, otherwise rewrite call with block.
4216 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
4217 -- Determine whether a formal parameter is used only once in Orig_Bod
4219 ---------------------
4220 -- Make_Exit_Label --
4221 ---------------------
4223 procedure Make_Exit_Label is
4224 Lab_Ent : Entity_Id;
4225 begin
4226 if No (Exit_Lab) then
4227 Lab_Ent := Make_Temporary (Loc, 'L');
4228 Lab_Id := New_Occurrence_Of (Lab_Ent, Loc);
4229 Exit_Lab := Make_Label (Loc, Lab_Id);
4230 Lab_Decl :=
4231 Make_Implicit_Label_Declaration (Loc,
4232 Defining_Identifier => Lab_Ent,
4233 Label_Construct => Exit_Lab);
4234 end if;
4235 end Make_Exit_Label;
4237 ---------------------
4238 -- Process_Formals --
4239 ---------------------
4241 function Process_Formals (N : Node_Id) return Traverse_Result is
4242 A : Entity_Id;
4243 E : Entity_Id;
4244 Ret : Node_Id;
4246 begin
4247 if Is_Entity_Name (N) and then Present (Entity (N)) then
4248 E := Entity (N);
4250 if Is_Formal (E) and then Scope (E) = Subp then
4251 A := Renamed_Object (E);
4253 -- Rewrite the occurrence of the formal into an occurrence of
4254 -- the actual. Also establish visibility on the proper view of
4255 -- the actual's subtype for the body's context (if the actual's
4256 -- subtype is private at the call point but its full view is
4257 -- visible to the body, then the inlined tree here must be
4258 -- analyzed with the full view).
4260 if Is_Entity_Name (A) then
4261 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
4262 Check_Private_View (N);
4264 elsif Nkind (A) = N_Defining_Identifier then
4265 Rewrite (N, New_Occurrence_Of (A, Loc));
4266 Check_Private_View (N);
4268 -- Numeric literal
4270 else
4271 Rewrite (N, New_Copy (A));
4272 end if;
4273 end if;
4275 return Skip;
4277 elsif Is_Entity_Name (N)
4278 and then Present (Return_Object)
4279 and then Chars (N) = Chars (Return_Object)
4280 then
4281 -- Occurrence within an extended return statement. The return
4282 -- object is local to the body been inlined, and thus the generic
4283 -- copy is not analyzed yet, so we match by name, and replace it
4284 -- with target of call.
4286 if Nkind (Targ) = N_Defining_Identifier then
4287 Rewrite (N, New_Occurrence_Of (Targ, Loc));
4288 else
4289 Rewrite (N, New_Copy_Tree (Targ));
4290 end if;
4292 return Skip;
4294 elsif Nkind (N) = N_Simple_Return_Statement then
4295 if No (Expression (N)) then
4296 Make_Exit_Label;
4297 Rewrite (N,
4298 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4300 else
4301 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
4302 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
4303 then
4304 -- Function body is a single expression. No need for
4305 -- exit label.
4307 null;
4309 else
4310 Num_Ret := Num_Ret + 1;
4311 Make_Exit_Label;
4312 end if;
4314 -- Because of the presence of private types, the views of the
4315 -- expression and the context may be different, so place an
4316 -- unchecked conversion to the context type to avoid spurious
4317 -- errors, e.g. when the expression is a numeric literal and
4318 -- the context is private. If the expression is an aggregate,
4319 -- use a qualified expression, because an aggregate is not a
4320 -- legal argument of a conversion. Ditto for numeric literals,
4321 -- which must be resolved to a specific type.
4323 if Nkind_In (Expression (N), N_Aggregate,
4324 N_Null,
4325 N_Real_Literal,
4326 N_Integer_Literal)
4327 then
4328 Ret :=
4329 Make_Qualified_Expression (Sloc (N),
4330 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
4331 Expression => Relocate_Node (Expression (N)));
4332 else
4333 Ret :=
4334 Unchecked_Convert_To
4335 (Ret_Type, Relocate_Node (Expression (N)));
4336 end if;
4338 if Nkind (Targ) = N_Defining_Identifier then
4339 Rewrite (N,
4340 Make_Assignment_Statement (Loc,
4341 Name => New_Occurrence_Of (Targ, Loc),
4342 Expression => Ret));
4343 else
4344 Rewrite (N,
4345 Make_Assignment_Statement (Loc,
4346 Name => New_Copy (Targ),
4347 Expression => Ret));
4348 end if;
4350 Set_Assignment_OK (Name (N));
4352 if Present (Exit_Lab) then
4353 Insert_After (N,
4354 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
4355 end if;
4356 end if;
4358 return OK;
4360 -- An extended return becomes a block whose first statement is the
4361 -- assignment of the initial expression of the return object to the
4362 -- target of the call itself.
4364 elsif Nkind (N) = N_Extended_Return_Statement then
4365 declare
4366 Return_Decl : constant Entity_Id :=
4367 First (Return_Object_Declarations (N));
4368 Assign : Node_Id;
4370 begin
4371 Return_Object := Defining_Identifier (Return_Decl);
4373 if Present (Expression (Return_Decl)) then
4374 if Nkind (Targ) = N_Defining_Identifier then
4375 Assign :=
4376 Make_Assignment_Statement (Loc,
4377 Name => New_Occurrence_Of (Targ, Loc),
4378 Expression => Expression (Return_Decl));
4379 else
4380 Assign :=
4381 Make_Assignment_Statement (Loc,
4382 Name => New_Copy (Targ),
4383 Expression => Expression (Return_Decl));
4384 end if;
4386 Set_Assignment_OK (Name (Assign));
4388 if No (Handled_Statement_Sequence (N)) then
4389 Set_Handled_Statement_Sequence (N,
4390 Make_Handled_Sequence_Of_Statements (Loc,
4391 Statements => New_List));
4392 end if;
4394 Prepend (Assign,
4395 Statements (Handled_Statement_Sequence (N)));
4396 end if;
4398 Rewrite (N,
4399 Make_Block_Statement (Loc,
4400 Handled_Statement_Sequence =>
4401 Handled_Statement_Sequence (N)));
4403 return OK;
4404 end;
4406 -- Remove pragma Unreferenced since it may refer to formals that
4407 -- are not visible in the inlined body, and in any case we will
4408 -- not be posting warnings on the inlined body so it is unneeded.
4410 elsif Nkind (N) = N_Pragma
4411 and then Pragma_Name (N) = Name_Unreferenced
4412 then
4413 Rewrite (N, Make_Null_Statement (Sloc (N)));
4414 return OK;
4416 else
4417 return OK;
4418 end if;
4419 end Process_Formals;
4421 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
4423 ------------------
4424 -- Process_Sloc --
4425 ------------------
4427 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
4428 begin
4429 if not Debug_Generated_Code then
4430 Set_Sloc (Nod, Sloc (N));
4431 Set_Comes_From_Source (Nod, False);
4432 end if;
4434 return OK;
4435 end Process_Sloc;
4437 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
4439 ------------------------------
4440 -- Reset_Dispatching_Calls --
4441 ------------------------------
4443 procedure Reset_Dispatching_Calls (N : Node_Id) is
4445 function Do_Reset (N : Node_Id) return Traverse_Result;
4446 -- Comment required ???
4448 --------------
4449 -- Do_Reset --
4450 --------------
4452 function Do_Reset (N : Node_Id) return Traverse_Result is
4453 begin
4454 if Nkind (N) = N_Procedure_Call_Statement
4455 and then Nkind (Name (N)) = N_Selected_Component
4456 and then Nkind (Prefix (Name (N))) = N_Identifier
4457 and then Is_Formal (Entity (Prefix (Name (N))))
4458 and then Is_Dispatching_Operation
4459 (Entity (Selector_Name (Name (N))))
4460 then
4461 Set_Entity (Selector_Name (Name (N)), Empty);
4462 end if;
4464 return OK;
4465 end Do_Reset;
4467 function Do_Reset_Calls is new Traverse_Func (Do_Reset);
4469 -- Local variables
4471 Dummy : constant Traverse_Result := Do_Reset_Calls (N);
4472 pragma Unreferenced (Dummy);
4474 -- Start of processing for Reset_Dispatching_Calls
4476 begin
4477 null;
4478 end Reset_Dispatching_Calls;
4480 ---------------------------
4481 -- Rewrite_Function_Call --
4482 ---------------------------
4484 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
4485 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4486 Fst : constant Node_Id := First (Statements (HSS));
4488 begin
4489 -- Optimize simple case: function body is a single return statement,
4490 -- which has been expanded into an assignment.
4492 if Is_Empty_List (Declarations (Blk))
4493 and then Nkind (Fst) = N_Assignment_Statement
4494 and then No (Next (Fst))
4495 then
4496 -- The function call may have been rewritten as the temporary
4497 -- that holds the result of the call, in which case remove the
4498 -- now useless declaration.
4500 if Nkind (N) = N_Identifier
4501 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4502 then
4503 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
4504 end if;
4506 Rewrite (N, Expression (Fst));
4508 elsif Nkind (N) = N_Identifier
4509 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
4510 then
4511 -- The block assigns the result of the call to the temporary
4513 Insert_After (Parent (Entity (N)), Blk);
4515 -- If the context is an assignment, and the left-hand side is free of
4516 -- side-effects, the replacement is also safe.
4517 -- Can this be generalized further???
4519 elsif Nkind (Parent (N)) = N_Assignment_Statement
4520 and then
4521 (Is_Entity_Name (Name (Parent (N)))
4522 or else
4523 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
4524 and then Is_Entity_Name (Prefix (Name (Parent (N)))))
4526 or else
4527 (Nkind (Name (Parent (N))) = N_Selected_Component
4528 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
4529 then
4530 -- Replace assignment with the block
4532 declare
4533 Original_Assignment : constant Node_Id := Parent (N);
4535 begin
4536 -- Preserve the original assignment node to keep the complete
4537 -- assignment subtree consistent enough for Analyze_Assignment
4538 -- to proceed (specifically, the original Lhs node must still
4539 -- have an assignment statement as its parent).
4541 -- We cannot rely on Original_Node to go back from the block
4542 -- node to the assignment node, because the assignment might
4543 -- already be a rewrite substitution.
4545 Discard_Node (Relocate_Node (Original_Assignment));
4546 Rewrite (Original_Assignment, Blk);
4547 end;
4549 elsif Nkind (Parent (N)) = N_Object_Declaration then
4551 -- A call to a function which returns an unconstrained type
4552 -- found in the expression initializing an object-declaration is
4553 -- expanded into a procedure call which must be added after the
4554 -- object declaration.
4556 if Is_Unc_Decl and then Debug_Flag_Dot_K then
4557 Insert_Action_After (Parent (N), Blk);
4558 else
4559 Set_Expression (Parent (N), Empty);
4560 Insert_After (Parent (N), Blk);
4561 end if;
4563 elsif Is_Unc and then not Debug_Flag_Dot_K then
4564 Insert_Before (Parent (N), Blk);
4565 end if;
4566 end Rewrite_Function_Call;
4568 ----------------------------
4569 -- Rewrite_Procedure_Call --
4570 ----------------------------
4572 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
4573 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
4575 begin
4576 -- If there is a transient scope for N, this will be the scope of the
4577 -- actions for N, and the statements in Blk need to be within this
4578 -- scope. For example, they need to have visibility on the constant
4579 -- declarations created for the formals.
4581 -- If N needs no transient scope, and if there are no declarations in
4582 -- the inlined body, we can do a little optimization and insert the
4583 -- statements for the body directly after N, and rewrite N to a
4584 -- null statement, instead of rewriting N into a full-blown block
4585 -- statement.
4587 if not Scope_Is_Transient
4588 and then Is_Empty_List (Declarations (Blk))
4589 then
4590 Insert_List_After (N, Statements (HSS));
4591 Rewrite (N, Make_Null_Statement (Loc));
4592 else
4593 Rewrite (N, Blk);
4594 end if;
4595 end Rewrite_Procedure_Call;
4597 -------------------------
4598 -- Formal_Is_Used_Once --
4599 -------------------------
4601 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
4602 Use_Counter : Int := 0;
4604 function Count_Uses (N : Node_Id) return Traverse_Result;
4605 -- Traverse the tree and count the uses of the formal parameter.
4606 -- In this case, for optimization purposes, we do not need to
4607 -- continue the traversal once more than one use is encountered.
4609 ----------------
4610 -- Count_Uses --
4611 ----------------
4613 function Count_Uses (N : Node_Id) return Traverse_Result is
4614 begin
4615 -- The original node is an identifier
4617 if Nkind (N) = N_Identifier
4618 and then Present (Entity (N))
4620 -- Original node's entity points to the one in the copied body
4622 and then Nkind (Entity (N)) = N_Identifier
4623 and then Present (Entity (Entity (N)))
4625 -- The entity of the copied node is the formal parameter
4627 and then Entity (Entity (N)) = Formal
4628 then
4629 Use_Counter := Use_Counter + 1;
4631 if Use_Counter > 1 then
4633 -- Denote more than one use and abandon the traversal
4635 Use_Counter := 2;
4636 return Abandon;
4638 end if;
4639 end if;
4641 return OK;
4642 end Count_Uses;
4644 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
4646 -- Start of processing for Formal_Is_Used_Once
4648 begin
4649 Count_Formal_Uses (Orig_Bod);
4650 return Use_Counter = 1;
4651 end Formal_Is_Used_Once;
4653 -- Start of processing for Expand_Inlined_Call
4655 begin
4656 -- Initializations for old/new semantics
4658 if not Debug_Flag_Dot_K then
4659 Is_Unc := Is_Array_Type (Etype (Subp))
4660 and then not Is_Constrained (Etype (Subp));
4661 Is_Unc_Decl := False;
4662 else
4663 Is_Unc := Returns_Unconstrained_Type (Subp)
4664 and then Optimization_Level > 0;
4665 Is_Unc_Decl := Nkind (Parent (N)) = N_Object_Declaration
4666 and then Is_Unc;
4667 end if;
4669 -- Check for an illegal attempt to inline a recursive procedure. If the
4670 -- subprogram has parameters this is detected when trying to supply a
4671 -- binding for parameters that already have one. For parameterless
4672 -- subprograms this must be done explicitly.
4674 if In_Open_Scopes (Subp) then
4675 Error_Msg_N ("call to recursive subprogram cannot be inlined??", N);
4676 Set_Is_Inlined (Subp, False);
4677 return;
4679 -- Skip inlining if this is not a true inlining since the attribute
4680 -- Body_To_Inline is also set for renamings (see sinfo.ads)
4682 elsif Nkind (Orig_Bod) in N_Entity then
4683 return;
4685 -- Skip inlining if the function returns an unconstrained type using
4686 -- an extended return statement since this part of the new inlining
4687 -- model which is not yet supported by the current implementation. ???
4689 elsif Is_Unc
4690 and then
4691 Nkind (First (Statements (Handled_Statement_Sequence (Orig_Bod))))
4692 = N_Extended_Return_Statement
4693 and then not Debug_Flag_Dot_K
4694 then
4695 return;
4696 end if;
4698 if Nkind (Orig_Bod) = N_Defining_Identifier
4699 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
4700 then
4701 -- Subprogram is renaming_as_body. Calls occurring after the renaming
4702 -- can be replaced with calls to the renamed entity directly, because
4703 -- the subprograms are subtype conformant. If the renamed subprogram
4704 -- is an inherited operation, we must redo the expansion because
4705 -- implicit conversions may be needed. Similarly, if the renamed
4706 -- entity is inlined, expand the call for further optimizations.
4708 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
4710 if Present (Alias (Orig_Bod)) or else Is_Inlined (Orig_Bod) then
4711 Expand_Call (N);
4712 end if;
4714 return;
4715 end if;
4717 -- Register the call in the list of inlined calls
4719 if Inlined_Calls = No_Elist then
4720 Inlined_Calls := New_Elmt_List;
4721 end if;
4723 Append_Elmt (N, To => Inlined_Calls);
4725 -- Use generic machinery to copy body of inlined subprogram, as if it
4726 -- were an instantiation, resetting source locations appropriately, so
4727 -- that nested inlined calls appear in the main unit.
4729 Save_Env (Subp, Empty);
4730 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
4732 -- Old semantics
4734 if not Debug_Flag_Dot_K then
4735 declare
4736 Bod : Node_Id;
4738 begin
4739 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4740 Blk :=
4741 Make_Block_Statement (Loc,
4742 Declarations => Declarations (Bod),
4743 Handled_Statement_Sequence =>
4744 Handled_Statement_Sequence (Bod));
4746 if No (Declarations (Bod)) then
4747 Set_Declarations (Blk, New_List);
4748 end if;
4750 -- For the unconstrained case, capture the name of the local
4751 -- variable that holds the result. This must be the first
4752 -- declaration in the block, because its bounds cannot depend
4753 -- on local variables. Otherwise there is no way to declare the
4754 -- result outside of the block. Needless to say, in general the
4755 -- bounds will depend on the actuals in the call.
4757 -- If the context is an assignment statement, as is the case
4758 -- for the expansion of an extended return, the left-hand side
4759 -- provides bounds even if the return type is unconstrained.
4761 if Is_Unc then
4762 declare
4763 First_Decl : Node_Id;
4765 begin
4766 First_Decl := First (Declarations (Blk));
4768 if Nkind (First_Decl) /= N_Object_Declaration then
4769 return;
4770 end if;
4772 if Nkind (Parent (N)) /= N_Assignment_Statement then
4773 Targ1 := Defining_Identifier (First_Decl);
4774 else
4775 Targ1 := Name (Parent (N));
4776 end if;
4777 end;
4778 end if;
4779 end;
4781 -- New semantics
4783 else
4784 declare
4785 Bod : Node_Id;
4787 begin
4788 -- General case
4790 if not Is_Unc then
4791 Bod :=
4792 Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
4793 Blk :=
4794 Make_Block_Statement (Loc,
4795 Declarations => Declarations (Bod),
4796 Handled_Statement_Sequence =>
4797 Handled_Statement_Sequence (Bod));
4799 -- Inline a call to a function that returns an unconstrained type.
4800 -- The semantic analyzer checked that frontend-inlined functions
4801 -- returning unconstrained types have no declarations and have
4802 -- a single extended return statement. As part of its processing
4803 -- the function was split in two subprograms: a procedure P and
4804 -- a function F that has a block with a call to procedure P (see
4805 -- Split_Unconstrained_Function).
4807 else
4808 pragma Assert
4809 (Nkind
4810 (First
4811 (Statements (Handled_Statement_Sequence (Orig_Bod))))
4812 = N_Block_Statement);
4814 declare
4815 Blk_Stmt : constant Node_Id :=
4816 First
4817 (Statements
4818 (Handled_Statement_Sequence (Orig_Bod)));
4819 First_Stmt : constant Node_Id :=
4820 First
4821 (Statements
4822 (Handled_Statement_Sequence (Blk_Stmt)));
4823 Second_Stmt : constant Node_Id := Next (First_Stmt);
4825 begin
4826 pragma Assert
4827 (Nkind (First_Stmt) = N_Procedure_Call_Statement
4828 and then Nkind (Second_Stmt) = N_Simple_Return_Statement
4829 and then No (Next (Second_Stmt)));
4831 Bod :=
4832 Copy_Generic_Node
4833 (First
4834 (Statements (Handled_Statement_Sequence (Orig_Bod))),
4835 Empty, Instantiating => True);
4836 Blk := Bod;
4838 -- Capture the name of the local variable that holds the
4839 -- result. This must be the first declaration in the block,
4840 -- because its bounds cannot depend on local variables.
4841 -- Otherwise there is no way to declare the result outside
4842 -- of the block. Needless to say, in general the bounds will
4843 -- depend on the actuals in the call.
4845 if Nkind (Parent (N)) /= N_Assignment_Statement then
4846 Targ1 := Defining_Identifier (First (Declarations (Blk)));
4848 -- If the context is an assignment statement, as is the case
4849 -- for the expansion of an extended return, the left-hand
4850 -- side provides bounds even if the return type is
4851 -- unconstrained.
4853 else
4854 Targ1 := Name (Parent (N));
4855 end if;
4856 end;
4857 end if;
4859 if No (Declarations (Bod)) then
4860 Set_Declarations (Blk, New_List);
4861 end if;
4862 end;
4863 end if;
4865 -- If this is a derived function, establish the proper return type
4867 if Present (Orig_Subp) and then Orig_Subp /= Subp then
4868 Ret_Type := Etype (Orig_Subp);
4869 else
4870 Ret_Type := Etype (Subp);
4871 end if;
4873 -- Create temporaries for the actuals that are expressions, or that are
4874 -- scalars and require copying to preserve semantics.
4876 F := First_Formal (Subp);
4877 A := First_Actual (N);
4878 while Present (F) loop
4879 if Present (Renamed_Object (F)) then
4880 Error_Msg_N ("cannot inline call to recursive subprogram", N);
4881 return;
4882 end if;
4884 -- Reset Last_Assignment for any parameters of mode out or in out, to
4885 -- prevent spurious warnings about overwriting for assignments to the
4886 -- formal in the inlined code.
4888 if Is_Entity_Name (A) and then Ekind (F) /= E_In_Parameter then
4889 Set_Last_Assignment (Entity (A), Empty);
4890 end if;
4892 -- If the argument may be a controlling argument in a call within
4893 -- the inlined body, we must preserve its classwide nature to insure
4894 -- that dynamic dispatching take place subsequently. If the formal
4895 -- has a constraint it must be preserved to retain the semantics of
4896 -- the body.
4898 if Is_Class_Wide_Type (Etype (F))
4899 or else (Is_Access_Type (Etype (F))
4900 and then Is_Class_Wide_Type (Designated_Type (Etype (F))))
4901 then
4902 Temp_Typ := Etype (F);
4904 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
4905 and then Etype (F) /= Base_Type (Etype (F))
4906 then
4907 Temp_Typ := Etype (F);
4908 else
4909 Temp_Typ := Etype (A);
4910 end if;
4912 -- If the actual is a simple name or a literal, no need to
4913 -- create a temporary, object can be used directly.
4915 -- If the actual is a literal and the formal has its address taken,
4916 -- we cannot pass the literal itself as an argument, so its value
4917 -- must be captured in a temporary.
4919 if (Is_Entity_Name (A)
4920 and then
4921 (not Is_Scalar_Type (Etype (A))
4922 or else Ekind (Entity (A)) = E_Enumeration_Literal))
4924 -- When the actual is an identifier and the corresponding formal is
4925 -- used only once in the original body, the formal can be substituted
4926 -- directly with the actual parameter.
4928 or else (Nkind (A) = N_Identifier
4929 and then Formal_Is_Used_Once (F))
4931 or else
4932 (Nkind_In (A, N_Real_Literal,
4933 N_Integer_Literal,
4934 N_Character_Literal)
4935 and then not Address_Taken (F))
4936 then
4937 if Etype (F) /= Etype (A) then
4938 Set_Renamed_Object
4939 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
4940 else
4941 Set_Renamed_Object (F, A);
4942 end if;
4944 else
4945 Temp := Make_Temporary (Loc, 'C');
4947 -- If the actual for an in/in-out parameter is a view conversion,
4948 -- make it into an unchecked conversion, given that an untagged
4949 -- type conversion is not a proper object for a renaming.
4951 -- In-out conversions that involve real conversions have already
4952 -- been transformed in Expand_Actuals.
4954 if Nkind (A) = N_Type_Conversion
4955 and then Ekind (F) /= E_In_Parameter
4956 then
4957 New_A :=
4958 Make_Unchecked_Type_Conversion (Loc,
4959 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
4960 Expression => Relocate_Node (Expression (A)));
4962 elsif Etype (F) /= Etype (A) then
4963 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
4964 Temp_Typ := Etype (F);
4966 else
4967 New_A := Relocate_Node (A);
4968 end if;
4970 Set_Sloc (New_A, Sloc (N));
4972 -- If the actual has a by-reference type, it cannot be copied,
4973 -- so its value is captured in a renaming declaration. Otherwise
4974 -- declare a local constant initialized with the actual.
4976 -- We also use a renaming declaration for expressions of an array
4977 -- type that is not bit-packed, both for efficiency reasons and to
4978 -- respect the semantics of the call: in most cases the original
4979 -- call will pass the parameter by reference, and thus the inlined
4980 -- code will have the same semantics.
4982 if Ekind (F) = E_In_Parameter
4983 and then not Is_By_Reference_Type (Etype (A))
4984 and then
4985 (not Is_Array_Type (Etype (A))
4986 or else not Is_Object_Reference (A)
4987 or else Is_Bit_Packed_Array (Etype (A)))
4988 then
4989 Decl :=
4990 Make_Object_Declaration (Loc,
4991 Defining_Identifier => Temp,
4992 Constant_Present => True,
4993 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
4994 Expression => New_A);
4995 else
4996 Decl :=
4997 Make_Object_Renaming_Declaration (Loc,
4998 Defining_Identifier => Temp,
4999 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
5000 Name => New_A);
5001 end if;
5003 Append (Decl, Decls);
5004 Set_Renamed_Object (F, Temp);
5005 end if;
5007 Next_Formal (F);
5008 Next_Actual (A);
5009 end loop;
5011 -- Establish target of function call. If context is not assignment or
5012 -- declaration, create a temporary as a target. The declaration for the
5013 -- temporary may be subsequently optimized away if the body is a single
5014 -- expression, or if the left-hand side of the assignment is simple
5015 -- enough, i.e. an entity or an explicit dereference of one.
5017 if Ekind (Subp) = E_Function then
5018 if Nkind (Parent (N)) = N_Assignment_Statement
5019 and then Is_Entity_Name (Name (Parent (N)))
5020 then
5021 Targ := Name (Parent (N));
5023 elsif Nkind (Parent (N)) = N_Assignment_Statement
5024 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
5025 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5026 then
5027 Targ := Name (Parent (N));
5029 elsif Nkind (Parent (N)) = N_Assignment_Statement
5030 and then Nkind (Name (Parent (N))) = N_Selected_Component
5031 and then Is_Entity_Name (Prefix (Name (Parent (N))))
5032 then
5033 Targ := New_Copy_Tree (Name (Parent (N)));
5035 elsif Nkind (Parent (N)) = N_Object_Declaration
5036 and then Is_Limited_Type (Etype (Subp))
5037 then
5038 Targ := Defining_Identifier (Parent (N));
5040 -- New semantics: In an object declaration avoid an extra copy
5041 -- of the result of a call to an inlined function that returns
5042 -- an unconstrained type
5044 elsif Debug_Flag_Dot_K
5045 and then Nkind (Parent (N)) = N_Object_Declaration
5046 and then Is_Unc
5047 then
5048 Targ := Defining_Identifier (Parent (N));
5050 else
5051 -- Replace call with temporary and create its declaration
5053 Temp := Make_Temporary (Loc, 'C');
5054 Set_Is_Internal (Temp);
5056 -- For the unconstrained case, the generated temporary has the
5057 -- same constrained declaration as the result variable. It may
5058 -- eventually be possible to remove that temporary and use the
5059 -- result variable directly.
5061 if Is_Unc
5062 and then Nkind (Parent (N)) /= N_Assignment_Statement
5063 then
5064 Decl :=
5065 Make_Object_Declaration (Loc,
5066 Defining_Identifier => Temp,
5067 Object_Definition =>
5068 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5070 Replace_Formals (Decl);
5072 else
5073 Decl :=
5074 Make_Object_Declaration (Loc,
5075 Defining_Identifier => Temp,
5076 Object_Definition => New_Occurrence_Of (Ret_Type, Loc));
5078 Set_Etype (Temp, Ret_Type);
5079 end if;
5081 Set_No_Initialization (Decl);
5082 Append (Decl, Decls);
5083 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5084 Targ := Temp;
5085 end if;
5086 end if;
5088 Insert_Actions (N, Decls);
5090 if Is_Unc_Decl then
5092 -- Special management for inlining a call to a function that returns
5093 -- an unconstrained type and initializes an object declaration: we
5094 -- avoid generating undesired extra calls and goto statements.
5096 -- Given:
5097 -- function Func (...) return ...
5098 -- begin
5099 -- declare
5100 -- Result : String (1 .. 4);
5101 -- begin
5102 -- Proc (Result, ...);
5103 -- return Result;
5104 -- end;
5105 -- end F;
5107 -- Result : String := Func (...);
5109 -- Replace this object declaration by:
5111 -- Result : String (1 .. 4);
5112 -- Proc (Result, ...);
5114 Remove_Homonym (Targ);
5116 Decl :=
5117 Make_Object_Declaration
5118 (Loc,
5119 Defining_Identifier => Targ,
5120 Object_Definition =>
5121 New_Copy_Tree (Object_Definition (Parent (Targ1))));
5122 Replace_Formals (Decl);
5123 Rewrite (Parent (N), Decl);
5124 Analyze (Parent (N));
5126 -- Avoid spurious warnings since we know that this declaration is
5127 -- referenced by the procedure call.
5129 Set_Never_Set_In_Source (Targ, False);
5131 -- Remove the local declaration of the extended return stmt from the
5132 -- inlined code
5134 Remove (Parent (Targ1));
5136 -- Update the reference to the result (since we have rewriten the
5137 -- object declaration)
5139 declare
5140 Blk_Call_Stmt : Node_Id;
5142 begin
5143 -- Capture the call to the procedure
5145 Blk_Call_Stmt :=
5146 First (Statements (Handled_Statement_Sequence (Blk)));
5147 pragma Assert
5148 (Nkind (Blk_Call_Stmt) = N_Procedure_Call_Statement);
5150 Remove (First (Parameter_Associations (Blk_Call_Stmt)));
5151 Prepend_To (Parameter_Associations (Blk_Call_Stmt),
5152 New_Occurrence_Of (Targ, Loc));
5153 end;
5155 -- Remove the return statement
5157 pragma Assert
5158 (Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5159 N_Simple_Return_Statement);
5161 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5162 end if;
5164 -- Traverse the tree and replace formals with actuals or their thunks.
5165 -- Attach block to tree before analysis and rewriting.
5167 Replace_Formals (Blk);
5168 Set_Parent (Blk, N);
5170 if not Comes_From_Source (Subp) or else Is_Predef then
5171 Reset_Slocs (Blk);
5172 end if;
5174 if Is_Unc_Decl then
5176 -- No action needed since return statement has been already removed
5178 null;
5180 elsif Present (Exit_Lab) then
5182 -- If the body was a single expression, the single return statement
5183 -- and the corresponding label are useless.
5185 if Num_Ret = 1
5186 and then
5187 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
5188 N_Goto_Statement
5189 then
5190 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
5191 else
5192 Append (Lab_Decl, (Declarations (Blk)));
5193 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
5194 end if;
5195 end if;
5197 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors
5198 -- on conflicting private views that Gigi would ignore. If this is a
5199 -- predefined unit, analyze with checks off, as is done in the non-
5200 -- inlined run-time units.
5202 declare
5203 I_Flag : constant Boolean := In_Inlined_Body;
5205 begin
5206 In_Inlined_Body := True;
5208 if Is_Predef then
5209 declare
5210 Style : constant Boolean := Style_Check;
5212 begin
5213 Style_Check := False;
5215 -- Search for dispatching calls that use the Object.Operation
5216 -- notation using an Object that is a parameter of the inlined
5217 -- function. We reset the decoration of Operation to force
5218 -- the reanalysis of the inlined dispatching call because
5219 -- the actual object has been inlined.
5221 Reset_Dispatching_Calls (Blk);
5223 Analyze (Blk, Suppress => All_Checks);
5224 Style_Check := Style;
5225 end;
5227 else
5228 Analyze (Blk);
5229 end if;
5231 In_Inlined_Body := I_Flag;
5232 end;
5234 if Ekind (Subp) = E_Procedure then
5235 Rewrite_Procedure_Call (N, Blk);
5237 else
5238 Rewrite_Function_Call (N, Blk);
5240 if Is_Unc_Decl then
5241 null;
5243 -- For the unconstrained case, the replacement of the call has been
5244 -- made prior to the complete analysis of the generated declarations.
5245 -- Propagate the proper type now.
5247 elsif Is_Unc then
5248 if Nkind (N) = N_Identifier then
5249 Set_Etype (N, Etype (Entity (N)));
5250 else
5251 Set_Etype (N, Etype (Targ1));
5252 end if;
5253 end if;
5254 end if;
5256 Restore_Env;
5258 -- Cleanup mapping between formals and actuals for other expansions
5260 F := First_Formal (Subp);
5261 while Present (F) loop
5262 Set_Renamed_Object (F, Empty);
5263 Next_Formal (F);
5264 end loop;
5265 end Expand_Inlined_Call;
5267 ----------------------------------------
5268 -- Expand_N_Extended_Return_Statement --
5269 ----------------------------------------
5271 -- If there is a Handled_Statement_Sequence, we rewrite this:
5273 -- return Result : T := <expression> do
5274 -- <handled_seq_of_stms>
5275 -- end return;
5277 -- to be:
5279 -- declare
5280 -- Result : T := <expression>;
5281 -- begin
5282 -- <handled_seq_of_stms>
5283 -- return Result;
5284 -- end;
5286 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
5288 -- return Result : T := <expression>;
5290 -- to be:
5292 -- return <expression>;
5294 -- unless it's build-in-place or there's no <expression>, in which case
5295 -- we generate:
5297 -- declare
5298 -- Result : T := <expression>;
5299 -- begin
5300 -- return Result;
5301 -- end;
5303 -- Note that this case could have been written by the user as an extended
5304 -- return statement, or could have been transformed to this from a simple
5305 -- return statement.
5307 -- That is, we need to have a reified return object if there are statements
5308 -- (which might refer to it) or if we're doing build-in-place (so we can
5309 -- set its address to the final resting place or if there is no expression
5310 -- (in which case default initial values might need to be set).
5312 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
5313 Loc : constant Source_Ptr := Sloc (N);
5315 Par_Func : constant Entity_Id :=
5316 Return_Applies_To (Return_Statement_Entity (N));
5317 Result_Subt : constant Entity_Id := Etype (Par_Func);
5318 Ret_Obj_Id : constant Entity_Id :=
5319 First_Entity (Return_Statement_Entity (N));
5320 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
5322 Is_Build_In_Place : constant Boolean :=
5323 Is_Build_In_Place_Function (Par_Func);
5325 Exp : Node_Id;
5326 HSS : Node_Id;
5327 Result : Node_Id;
5328 Return_Stmt : Node_Id;
5329 Stmts : List_Id;
5331 function Build_Heap_Allocator
5332 (Temp_Id : Entity_Id;
5333 Temp_Typ : Entity_Id;
5334 Func_Id : Entity_Id;
5335 Ret_Typ : Entity_Id;
5336 Alloc_Expr : Node_Id) return Node_Id;
5337 -- Create the statements necessary to allocate a return object on the
5338 -- caller's master. The master is available through implicit parameter
5339 -- BIPfinalizationmaster.
5341 -- if BIPfinalizationmaster /= null then
5342 -- declare
5343 -- type Ptr_Typ is access Ret_Typ;
5344 -- for Ptr_Typ'Storage_Pool use
5345 -- Base_Pool (BIPfinalizationmaster.all).all;
5346 -- Local : Ptr_Typ;
5348 -- begin
5349 -- procedure Allocate (...) is
5350 -- begin
5351 -- System.Storage_Pools.Subpools.Allocate_Any (...);
5352 -- end Allocate;
5354 -- Local := <Alloc_Expr>;
5355 -- Temp_Id := Temp_Typ (Local);
5356 -- end;
5357 -- end if;
5359 -- Temp_Id is the temporary which is used to reference the internally
5360 -- created object in all allocation forms. Temp_Typ is the type of the
5361 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
5362 -- type of Func_Id. Alloc_Expr is the actual allocator.
5364 function Move_Activation_Chain return Node_Id;
5365 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
5366 -- with parameters:
5367 -- From current activation chain
5368 -- To activation chain passed in by the caller
5369 -- New_Master master passed in by the caller
5371 --------------------------
5372 -- Build_Heap_Allocator --
5373 --------------------------
5375 function Build_Heap_Allocator
5376 (Temp_Id : Entity_Id;
5377 Temp_Typ : Entity_Id;
5378 Func_Id : Entity_Id;
5379 Ret_Typ : Entity_Id;
5380 Alloc_Expr : Node_Id) return Node_Id
5382 begin
5383 pragma Assert (Is_Build_In_Place_Function (Func_Id));
5385 -- Processing for build-in-place object allocation. This is disabled
5386 -- on .NET/JVM because the targets do not support pools.
5388 if VM_Target = No_VM
5389 and then Needs_Finalization (Ret_Typ)
5390 then
5391 declare
5392 Decls : constant List_Id := New_List;
5393 Fin_Mas_Id : constant Entity_Id :=
5394 Build_In_Place_Formal
5395 (Func_Id, BIP_Finalization_Master);
5396 Stmts : constant List_Id := New_List;
5397 Desig_Typ : Entity_Id;
5398 Local_Id : Entity_Id;
5399 Pool_Id : Entity_Id;
5400 Ptr_Typ : Entity_Id;
5402 begin
5403 -- Generate:
5404 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
5406 Pool_Id := Make_Temporary (Loc, 'P');
5408 Append_To (Decls,
5409 Make_Object_Renaming_Declaration (Loc,
5410 Defining_Identifier => Pool_Id,
5411 Subtype_Mark =>
5412 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
5413 Name =>
5414 Make_Explicit_Dereference (Loc,
5415 Prefix =>
5416 Make_Function_Call (Loc,
5417 Name =>
5418 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
5419 Parameter_Associations => New_List (
5420 Make_Explicit_Dereference (Loc,
5421 Prefix =>
5422 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
5424 -- Create an access type which uses the storage pool of the
5425 -- caller's master. This additional type is necessary because
5426 -- the finalization master cannot be associated with the type
5427 -- of the temporary. Otherwise the secondary stack allocation
5428 -- will fail.
5430 Desig_Typ := Ret_Typ;
5432 -- Ensure that the build-in-place machinery uses a fat pointer
5433 -- when allocating an unconstrained array on the heap. In this
5434 -- case the result object type is a constrained array type even
5435 -- though the function type is unconstrained.
5437 if Ekind (Desig_Typ) = E_Array_Subtype then
5438 Desig_Typ := Base_Type (Desig_Typ);
5439 end if;
5441 -- Generate:
5442 -- type Ptr_Typ is access Desig_Typ;
5444 Ptr_Typ := Make_Temporary (Loc, 'P');
5446 Append_To (Decls,
5447 Make_Full_Type_Declaration (Loc,
5448 Defining_Identifier => Ptr_Typ,
5449 Type_Definition =>
5450 Make_Access_To_Object_Definition (Loc,
5451 Subtype_Indication =>
5452 New_Occurrence_Of (Desig_Typ, Loc))));
5454 -- Perform minor decoration in order to set the master and the
5455 -- storage pool attributes.
5457 Set_Ekind (Ptr_Typ, E_Access_Type);
5458 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
5459 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
5461 -- Create the temporary, generate:
5462 -- Local_Id : Ptr_Typ;
5464 Local_Id := Make_Temporary (Loc, 'T');
5466 Append_To (Decls,
5467 Make_Object_Declaration (Loc,
5468 Defining_Identifier => Local_Id,
5469 Object_Definition =>
5470 New_Occurrence_Of (Ptr_Typ, Loc)));
5472 -- Allocate the object, generate:
5473 -- Local_Id := <Alloc_Expr>;
5475 Append_To (Stmts,
5476 Make_Assignment_Statement (Loc,
5477 Name => New_Occurrence_Of (Local_Id, Loc),
5478 Expression => Alloc_Expr));
5480 -- Generate:
5481 -- Temp_Id := Temp_Typ (Local_Id);
5483 Append_To (Stmts,
5484 Make_Assignment_Statement (Loc,
5485 Name => New_Occurrence_Of (Temp_Id, Loc),
5486 Expression =>
5487 Unchecked_Convert_To (Temp_Typ,
5488 New_Occurrence_Of (Local_Id, Loc))));
5490 -- Wrap the allocation in a block. This is further conditioned
5491 -- by checking the caller finalization master at runtime. A
5492 -- null value indicates a non-existent master, most likely due
5493 -- to a Finalize_Storage_Only allocation.
5495 -- Generate:
5496 -- if BIPfinalizationmaster /= null then
5497 -- declare
5498 -- <Decls>
5499 -- begin
5500 -- <Stmts>
5501 -- end;
5502 -- end if;
5504 return
5505 Make_If_Statement (Loc,
5506 Condition =>
5507 Make_Op_Ne (Loc,
5508 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
5509 Right_Opnd => Make_Null (Loc)),
5511 Then_Statements => New_List (
5512 Make_Block_Statement (Loc,
5513 Declarations => Decls,
5514 Handled_Statement_Sequence =>
5515 Make_Handled_Sequence_Of_Statements (Loc,
5516 Statements => Stmts))));
5517 end;
5519 -- For all other cases, generate:
5520 -- Temp_Id := <Alloc_Expr>;
5522 else
5523 return
5524 Make_Assignment_Statement (Loc,
5525 Name => New_Occurrence_Of (Temp_Id, Loc),
5526 Expression => Alloc_Expr);
5527 end if;
5528 end Build_Heap_Allocator;
5530 ---------------------------
5531 -- Move_Activation_Chain --
5532 ---------------------------
5534 function Move_Activation_Chain return Node_Id is
5535 begin
5536 return
5537 Make_Procedure_Call_Statement (Loc,
5538 Name =>
5539 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
5541 Parameter_Associations => New_List (
5543 -- Source chain
5545 Make_Attribute_Reference (Loc,
5546 Prefix => Make_Identifier (Loc, Name_uChain),
5547 Attribute_Name => Name_Unrestricted_Access),
5549 -- Destination chain
5551 New_Occurrence_Of
5552 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
5554 -- New master
5556 New_Occurrence_Of
5557 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
5558 end Move_Activation_Chain;
5560 -- Start of processing for Expand_N_Extended_Return_Statement
5562 begin
5563 -- Given that functionality of interface thunks is simple (just displace
5564 -- the pointer to the object) they are always handled by means of
5565 -- simple return statements.
5567 pragma Assert (not Is_Thunk (Current_Scope));
5569 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
5570 Exp := Expression (Ret_Obj_Decl);
5571 else
5572 Exp := Empty;
5573 end if;
5575 HSS := Handled_Statement_Sequence (N);
5577 -- If the returned object needs finalization actions, the function must
5578 -- perform the appropriate cleanup should it fail to return. The state
5579 -- of the function itself is tracked through a flag which is coupled
5580 -- with the scope finalizer. There is one flag per each return object
5581 -- in case of multiple returns.
5583 if Is_Build_In_Place
5584 and then Needs_Finalization (Etype (Ret_Obj_Id))
5585 then
5586 declare
5587 Flag_Decl : Node_Id;
5588 Flag_Id : Entity_Id;
5589 Func_Bod : Node_Id;
5591 begin
5592 -- Recover the function body
5594 Func_Bod := Unit_Declaration_Node (Par_Func);
5596 if Nkind (Func_Bod) = N_Subprogram_Declaration then
5597 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
5598 end if;
5600 -- Create a flag to track the function state
5602 Flag_Id := Make_Temporary (Loc, 'F');
5603 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
5605 -- Insert the flag at the beginning of the function declarations,
5606 -- generate:
5607 -- Fnn : Boolean := False;
5609 Flag_Decl :=
5610 Make_Object_Declaration (Loc,
5611 Defining_Identifier => Flag_Id,
5612 Object_Definition =>
5613 New_Occurrence_Of (Standard_Boolean, Loc),
5614 Expression =>
5615 New_Occurrence_Of (Standard_False, Loc));
5617 Prepend_To (Declarations (Func_Bod), Flag_Decl);
5618 Analyze (Flag_Decl);
5619 end;
5620 end if;
5622 -- Build a simple_return_statement that returns the return object when
5623 -- there is a statement sequence, or no expression, or the result will
5624 -- be built in place. Note however that we currently do this for all
5625 -- composite cases, even though nonlimited composite results are not yet
5626 -- built in place (though we plan to do so eventually).
5628 if Present (HSS)
5629 or else Is_Composite_Type (Result_Subt)
5630 or else No (Exp)
5631 then
5632 if No (HSS) then
5633 Stmts := New_List;
5635 -- If the extended return has a handled statement sequence, then wrap
5636 -- it in a block and use the block as the first statement.
5638 else
5639 Stmts := New_List (
5640 Make_Block_Statement (Loc,
5641 Declarations => New_List,
5642 Handled_Statement_Sequence => HSS));
5643 end if;
5645 -- If the result type contains tasks, we call Move_Activation_Chain.
5646 -- Later, the cleanup code will call Complete_Master, which will
5647 -- terminate any unactivated tasks belonging to the return statement
5648 -- master. But Move_Activation_Chain updates their master to be that
5649 -- of the caller, so they will not be terminated unless the return
5650 -- statement completes unsuccessfully due to exception, abort, goto,
5651 -- or exit. As a formality, we test whether the function requires the
5652 -- result to be built in place, though that's necessarily true for
5653 -- the case of result types with task parts.
5655 if Is_Build_In_Place
5656 and then Has_Task (Result_Subt)
5657 then
5658 -- The return expression is an aggregate for a complex type which
5659 -- contains tasks. This particular case is left unexpanded since
5660 -- the regular expansion would insert all temporaries and
5661 -- initialization code in the wrong block.
5663 if Nkind (Exp) = N_Aggregate then
5664 Expand_N_Aggregate (Exp);
5665 end if;
5667 -- Do not move the activation chain if the return object does not
5668 -- contain tasks.
5670 if Has_Task (Etype (Ret_Obj_Id)) then
5671 Append_To (Stmts, Move_Activation_Chain);
5672 end if;
5673 end if;
5675 -- Update the state of the function right before the object is
5676 -- returned.
5678 if Is_Build_In_Place
5679 and then Needs_Finalization (Etype (Ret_Obj_Id))
5680 then
5681 declare
5682 Flag_Id : constant Entity_Id :=
5683 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
5685 begin
5686 -- Generate:
5687 -- Fnn := True;
5689 Append_To (Stmts,
5690 Make_Assignment_Statement (Loc,
5691 Name => New_Occurrence_Of (Flag_Id, Loc),
5692 Expression => New_Occurrence_Of (Standard_True, Loc)));
5693 end;
5694 end if;
5696 -- Build a simple_return_statement that returns the return object
5698 Return_Stmt :=
5699 Make_Simple_Return_Statement (Loc,
5700 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
5701 Append_To (Stmts, Return_Stmt);
5703 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
5704 end if;
5706 -- Case where we build a return statement block
5708 if Present (HSS) then
5709 Result :=
5710 Make_Block_Statement (Loc,
5711 Declarations => Return_Object_Declarations (N),
5712 Handled_Statement_Sequence => HSS);
5714 -- We set the entity of the new block statement to be that of the
5715 -- return statement. This is necessary so that various fields, such
5716 -- as Finalization_Chain_Entity carry over from the return statement
5717 -- to the block. Note that this block is unusual, in that its entity
5718 -- is an E_Return_Statement rather than an E_Block.
5720 Set_Identifier
5721 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
5723 -- If the object decl was already rewritten as a renaming, then we
5724 -- don't want to do the object allocation and transformation of of
5725 -- the return object declaration to a renaming. This case occurs
5726 -- when the return object is initialized by a call to another
5727 -- build-in-place function, and that function is responsible for
5728 -- the allocation of the return object.
5730 if Is_Build_In_Place
5731 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
5732 then
5733 pragma Assert
5734 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
5735 and then Is_Build_In_Place_Function_Call
5736 (Expression (Original_Node (Ret_Obj_Decl))));
5738 -- Return the build-in-place result by reference
5740 Set_By_Ref (Return_Stmt);
5742 elsif Is_Build_In_Place then
5744 -- Locate the implicit access parameter associated with the
5745 -- caller-supplied return object and convert the return
5746 -- statement's return object declaration to a renaming of a
5747 -- dereference of the access parameter. If the return object's
5748 -- declaration includes an expression that has not already been
5749 -- expanded as separate assignments, then add an assignment
5750 -- statement to ensure the return object gets initialized.
5752 -- declare
5753 -- Result : T [:= <expression>];
5754 -- begin
5755 -- ...
5757 -- is converted to
5759 -- declare
5760 -- Result : T renames FuncRA.all;
5761 -- [Result := <expression;]
5762 -- begin
5763 -- ...
5765 declare
5766 Return_Obj_Id : constant Entity_Id :=
5767 Defining_Identifier (Ret_Obj_Decl);
5768 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
5769 Return_Obj_Expr : constant Node_Id :=
5770 Expression (Ret_Obj_Decl);
5771 Constr_Result : constant Boolean :=
5772 Is_Constrained (Result_Subt);
5773 Obj_Alloc_Formal : Entity_Id;
5774 Object_Access : Entity_Id;
5775 Obj_Acc_Deref : Node_Id;
5776 Init_Assignment : Node_Id := Empty;
5778 begin
5779 -- Build-in-place results must be returned by reference
5781 Set_By_Ref (Return_Stmt);
5783 -- Retrieve the implicit access parameter passed by the caller
5785 Object_Access :=
5786 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
5788 -- If the return object's declaration includes an expression
5789 -- and the declaration isn't marked as No_Initialization, then
5790 -- we need to generate an assignment to the object and insert
5791 -- it after the declaration before rewriting it as a renaming
5792 -- (otherwise we'll lose the initialization). The case where
5793 -- the result type is an interface (or class-wide interface)
5794 -- is also excluded because the context of the function call
5795 -- must be unconstrained, so the initialization will always
5796 -- be done as part of an allocator evaluation (storage pool
5797 -- or secondary stack), never to a constrained target object
5798 -- passed in by the caller. Besides the assignment being
5799 -- unneeded in this case, it avoids problems with trying to
5800 -- generate a dispatching assignment when the return expression
5801 -- is a nonlimited descendant of a limited interface (the
5802 -- interface has no assignment operation).
5804 if Present (Return_Obj_Expr)
5805 and then not No_Initialization (Ret_Obj_Decl)
5806 and then not Is_Interface (Return_Obj_Typ)
5807 then
5808 Init_Assignment :=
5809 Make_Assignment_Statement (Loc,
5810 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
5811 Expression => Relocate_Node (Return_Obj_Expr));
5813 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
5814 Set_Assignment_OK (Name (Init_Assignment));
5815 Set_No_Ctrl_Actions (Init_Assignment);
5817 Set_Parent (Name (Init_Assignment), Init_Assignment);
5818 Set_Parent (Expression (Init_Assignment), Init_Assignment);
5820 Set_Expression (Ret_Obj_Decl, Empty);
5822 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
5823 and then not Is_Class_Wide_Type
5824 (Etype (Expression (Init_Assignment)))
5825 then
5826 Rewrite (Expression (Init_Assignment),
5827 Make_Type_Conversion (Loc,
5828 Subtype_Mark =>
5829 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
5830 Expression =>
5831 Relocate_Node (Expression (Init_Assignment))));
5832 end if;
5834 -- In the case of functions where the calling context can
5835 -- determine the form of allocation needed, initialization
5836 -- is done with each part of the if statement that handles
5837 -- the different forms of allocation (this is true for
5838 -- unconstrained and tagged result subtypes).
5840 if Constr_Result
5841 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
5842 then
5843 Insert_After (Ret_Obj_Decl, Init_Assignment);
5844 end if;
5845 end if;
5847 -- When the function's subtype is unconstrained, a run-time
5848 -- test is needed to determine the form of allocation to use
5849 -- for the return object. The function has an implicit formal
5850 -- parameter indicating this. If the BIP_Alloc_Form formal has
5851 -- the value one, then the caller has passed access to an
5852 -- existing object for use as the return object. If the value
5853 -- is two, then the return object must be allocated on the
5854 -- secondary stack. Otherwise, the object must be allocated in
5855 -- a storage pool (currently only supported for the global
5856 -- heap, user-defined storage pools TBD ???). We generate an
5857 -- if statement to test the implicit allocation formal and
5858 -- initialize a local access value appropriately, creating
5859 -- allocators in the secondary stack and global heap cases.
5860 -- The special formal also exists and must be tested when the
5861 -- function has a tagged result, even when the result subtype
5862 -- is constrained, because in general such functions can be
5863 -- called in dispatching contexts and must be handled similarly
5864 -- to functions with a class-wide result.
5866 if not Constr_Result
5867 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5868 then
5869 Obj_Alloc_Formal :=
5870 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
5872 declare
5873 Pool_Id : constant Entity_Id :=
5874 Make_Temporary (Loc, 'P');
5875 Alloc_Obj_Id : Entity_Id;
5876 Alloc_Obj_Decl : Node_Id;
5877 Alloc_If_Stmt : Node_Id;
5878 Heap_Allocator : Node_Id;
5879 Pool_Decl : Node_Id;
5880 Pool_Allocator : Node_Id;
5881 Ptr_Type_Decl : Node_Id;
5882 Ref_Type : Entity_Id;
5883 SS_Allocator : Node_Id;
5885 begin
5886 -- Reuse the itype created for the function's implicit
5887 -- access formal. This avoids the need to create a new
5888 -- access type here, plus it allows assigning the access
5889 -- formal directly without applying a conversion.
5891 -- Ref_Type := Etype (Object_Access);
5893 -- Create an access type designating the function's
5894 -- result subtype.
5896 Ref_Type := Make_Temporary (Loc, 'A');
5898 Ptr_Type_Decl :=
5899 Make_Full_Type_Declaration (Loc,
5900 Defining_Identifier => Ref_Type,
5901 Type_Definition =>
5902 Make_Access_To_Object_Definition (Loc,
5903 All_Present => True,
5904 Subtype_Indication =>
5905 New_Occurrence_Of (Return_Obj_Typ, Loc)));
5907 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
5909 -- Create an access object that will be initialized to an
5910 -- access value denoting the return object, either coming
5911 -- from an implicit access value passed in by the caller
5912 -- or from the result of an allocator.
5914 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
5915 Set_Etype (Alloc_Obj_Id, Ref_Type);
5917 Alloc_Obj_Decl :=
5918 Make_Object_Declaration (Loc,
5919 Defining_Identifier => Alloc_Obj_Id,
5920 Object_Definition =>
5921 New_Occurrence_Of (Ref_Type, Loc));
5923 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
5925 -- Create allocators for both the secondary stack and
5926 -- global heap. If there's an initialization expression,
5927 -- then create these as initialized allocators.
5929 if Present (Return_Obj_Expr)
5930 and then not No_Initialization (Ret_Obj_Decl)
5931 then
5932 -- Always use the type of the expression for the
5933 -- qualified expression, rather than the result type.
5934 -- In general we cannot always use the result type
5935 -- for the allocator, because the expression might be
5936 -- of a specific type, such as in the case of an
5937 -- aggregate or even a nonlimited object when the
5938 -- result type is a limited class-wide interface type.
5940 Heap_Allocator :=
5941 Make_Allocator (Loc,
5942 Expression =>
5943 Make_Qualified_Expression (Loc,
5944 Subtype_Mark =>
5945 New_Occurrence_Of
5946 (Etype (Return_Obj_Expr), Loc),
5947 Expression =>
5948 New_Copy_Tree (Return_Obj_Expr)));
5950 else
5951 -- If the function returns a class-wide type we cannot
5952 -- use the return type for the allocator. Instead we
5953 -- use the type of the expression, which must be an
5954 -- aggregate of a definite type.
5956 if Is_Class_Wide_Type (Return_Obj_Typ) then
5957 Heap_Allocator :=
5958 Make_Allocator (Loc,
5959 Expression =>
5960 New_Occurrence_Of
5961 (Etype (Return_Obj_Expr), Loc));
5962 else
5963 Heap_Allocator :=
5964 Make_Allocator (Loc,
5965 Expression =>
5966 New_Occurrence_Of (Return_Obj_Typ, Loc));
5967 end if;
5969 -- If the object requires default initialization then
5970 -- that will happen later following the elaboration of
5971 -- the object renaming. If we don't turn it off here
5972 -- then the object will be default initialized twice.
5974 Set_No_Initialization (Heap_Allocator);
5975 end if;
5977 -- The Pool_Allocator is just like the Heap_Allocator,
5978 -- except we set Storage_Pool and Procedure_To_Call so
5979 -- it will use the user-defined storage pool.
5981 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
5983 -- Do not generate the renaming of the build-in-place
5984 -- pool parameter on .NET/JVM/ZFP because the parameter
5985 -- is not created in the first place.
5987 if VM_Target = No_VM
5988 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
5989 then
5990 Pool_Decl :=
5991 Make_Object_Renaming_Declaration (Loc,
5992 Defining_Identifier => Pool_Id,
5993 Subtype_Mark =>
5994 New_Occurrence_Of
5995 (RTE (RE_Root_Storage_Pool), Loc),
5996 Name =>
5997 Make_Explicit_Dereference (Loc,
5998 New_Occurrence_Of
5999 (Build_In_Place_Formal
6000 (Par_Func, BIP_Storage_Pool), Loc)));
6001 Set_Storage_Pool (Pool_Allocator, Pool_Id);
6002 Set_Procedure_To_Call
6003 (Pool_Allocator, RTE (RE_Allocate_Any));
6004 else
6005 Pool_Decl := Make_Null_Statement (Loc);
6006 end if;
6008 -- If the No_Allocators restriction is active, then only
6009 -- an allocator for secondary stack allocation is needed.
6010 -- It's OK for such allocators to have Comes_From_Source
6011 -- set to False, because gigi knows not to flag them as
6012 -- being a violation of No_Implicit_Heap_Allocations.
6014 if Restriction_Active (No_Allocators) then
6015 SS_Allocator := Heap_Allocator;
6016 Heap_Allocator := Make_Null (Loc);
6017 Pool_Allocator := Make_Null (Loc);
6019 -- Otherwise the heap and pool allocators may be needed,
6020 -- so we make another allocator for secondary stack
6021 -- allocation.
6023 else
6024 SS_Allocator := New_Copy_Tree (Heap_Allocator);
6026 -- The heap and pool allocators are marked as
6027 -- Comes_From_Source since they correspond to an
6028 -- explicit user-written allocator (that is, it will
6029 -- only be executed on behalf of callers that call the
6030 -- function as initialization for such an allocator).
6031 -- Prevents errors when No_Implicit_Heap_Allocations
6032 -- is in force.
6034 Set_Comes_From_Source (Heap_Allocator, True);
6035 Set_Comes_From_Source (Pool_Allocator, True);
6036 end if;
6038 -- The allocator is returned on the secondary stack. We
6039 -- don't do this on VM targets, since the SS is not used.
6041 if VM_Target = No_VM then
6042 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
6043 Set_Procedure_To_Call
6044 (SS_Allocator, RTE (RE_SS_Allocate));
6046 -- The allocator is returned on the secondary stack,
6047 -- so indicate that the function return, as well as
6048 -- the block that encloses the allocator, must not
6049 -- release it. The flags must be set now because
6050 -- the decision to use the secondary stack is done
6051 -- very late in the course of expanding the return
6052 -- statement, past the point where these flags are
6053 -- normally set.
6055 Set_Sec_Stack_Needed_For_Return (Par_Func);
6056 Set_Sec_Stack_Needed_For_Return
6057 (Return_Statement_Entity (N));
6058 Set_Uses_Sec_Stack (Par_Func);
6059 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
6060 end if;
6062 -- Create an if statement to test the BIP_Alloc_Form
6063 -- formal and initialize the access object to either the
6064 -- BIP_Object_Access formal (BIP_Alloc_Form =
6065 -- Caller_Allocation), the result of allocating the
6066 -- object in the secondary stack (BIP_Alloc_Form =
6067 -- Secondary_Stack), or else an allocator to create the
6068 -- return object in the heap or user-defined pool
6069 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
6071 -- ??? An unchecked type conversion must be made in the
6072 -- case of assigning the access object formal to the
6073 -- local access object, because a normal conversion would
6074 -- be illegal in some cases (such as converting access-
6075 -- to-unconstrained to access-to-constrained), but the
6076 -- the unchecked conversion will presumably fail to work
6077 -- right in just such cases. It's not clear at all how to
6078 -- handle this. ???
6080 Alloc_If_Stmt :=
6081 Make_If_Statement (Loc,
6082 Condition =>
6083 Make_Op_Eq (Loc,
6084 Left_Opnd =>
6085 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6086 Right_Opnd =>
6087 Make_Integer_Literal (Loc,
6088 UI_From_Int (BIP_Allocation_Form'Pos
6089 (Caller_Allocation)))),
6091 Then_Statements => New_List (
6092 Make_Assignment_Statement (Loc,
6093 Name =>
6094 New_Occurrence_Of (Alloc_Obj_Id, Loc),
6095 Expression =>
6096 Make_Unchecked_Type_Conversion (Loc,
6097 Subtype_Mark =>
6098 New_Occurrence_Of (Ref_Type, Loc),
6099 Expression =>
6100 New_Occurrence_Of (Object_Access, Loc)))),
6102 Elsif_Parts => New_List (
6103 Make_Elsif_Part (Loc,
6104 Condition =>
6105 Make_Op_Eq (Loc,
6106 Left_Opnd =>
6107 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6108 Right_Opnd =>
6109 Make_Integer_Literal (Loc,
6110 UI_From_Int (BIP_Allocation_Form'Pos
6111 (Secondary_Stack)))),
6113 Then_Statements => New_List (
6114 Make_Assignment_Statement (Loc,
6115 Name =>
6116 New_Occurrence_Of (Alloc_Obj_Id, Loc),
6117 Expression => SS_Allocator))),
6119 Make_Elsif_Part (Loc,
6120 Condition =>
6121 Make_Op_Eq (Loc,
6122 Left_Opnd =>
6123 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
6124 Right_Opnd =>
6125 Make_Integer_Literal (Loc,
6126 UI_From_Int (BIP_Allocation_Form'Pos
6127 (Global_Heap)))),
6129 Then_Statements => New_List (
6130 Build_Heap_Allocator
6131 (Temp_Id => Alloc_Obj_Id,
6132 Temp_Typ => Ref_Type,
6133 Func_Id => Par_Func,
6134 Ret_Typ => Return_Obj_Typ,
6135 Alloc_Expr => Heap_Allocator)))),
6137 Else_Statements => New_List (
6138 Pool_Decl,
6139 Build_Heap_Allocator
6140 (Temp_Id => Alloc_Obj_Id,
6141 Temp_Typ => Ref_Type,
6142 Func_Id => Par_Func,
6143 Ret_Typ => Return_Obj_Typ,
6144 Alloc_Expr => Pool_Allocator)));
6146 -- If a separate initialization assignment was created
6147 -- earlier, append that following the assignment of the
6148 -- implicit access formal to the access object, to ensure
6149 -- that the return object is initialized in that case. In
6150 -- this situation, the target of the assignment must be
6151 -- rewritten to denote a dereference of the access to the
6152 -- return object passed in by the caller.
6154 if Present (Init_Assignment) then
6155 Rewrite (Name (Init_Assignment),
6156 Make_Explicit_Dereference (Loc,
6157 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
6159 Set_Etype
6160 (Name (Init_Assignment), Etype (Return_Obj_Id));
6162 Append_To
6163 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
6164 end if;
6166 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
6168 -- Remember the local access object for use in the
6169 -- dereference of the renaming created below.
6171 Object_Access := Alloc_Obj_Id;
6172 end;
6173 end if;
6175 -- Replace the return object declaration with a renaming of a
6176 -- dereference of the access value designating the return
6177 -- object.
6179 Obj_Acc_Deref :=
6180 Make_Explicit_Dereference (Loc,
6181 Prefix => New_Occurrence_Of (Object_Access, Loc));
6183 Rewrite (Ret_Obj_Decl,
6184 Make_Object_Renaming_Declaration (Loc,
6185 Defining_Identifier => Return_Obj_Id,
6186 Access_Definition => Empty,
6187 Subtype_Mark =>
6188 New_Occurrence_Of (Return_Obj_Typ, Loc),
6189 Name => Obj_Acc_Deref));
6191 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
6192 end;
6193 end if;
6195 -- Case where we do not build a block
6197 else
6198 -- We're about to drop Return_Object_Declarations on the floor, so
6199 -- we need to insert it, in case it got expanded into useful code.
6200 -- Remove side effects from expression, which may be duplicated in
6201 -- subsequent checks (see Expand_Simple_Function_Return).
6203 Insert_List_Before (N, Return_Object_Declarations (N));
6204 Remove_Side_Effects (Exp);
6206 -- Build simple_return_statement that returns the expression directly
6208 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
6209 Result := Return_Stmt;
6210 end if;
6212 -- Set the flag to prevent infinite recursion
6214 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
6216 Rewrite (N, Result);
6217 Analyze (N);
6218 end Expand_N_Extended_Return_Statement;
6220 ----------------------------
6221 -- Expand_N_Function_Call --
6222 ----------------------------
6224 procedure Expand_N_Function_Call (N : Node_Id) is
6225 begin
6226 Expand_Call (N);
6228 -- If the return value of a foreign compiled function is VAX Float, then
6229 -- expand the return (adjusts the location of the return value on
6230 -- Alpha/VMS, no-op everywhere else).
6231 -- Comes_From_Source intercepts recursive expansion.
6233 if Nkind (N) = N_Function_Call
6234 and then Vax_Float (Etype (N))
6235 and then Present (Name (N))
6236 and then Present (Entity (Name (N)))
6237 and then Has_Foreign_Convention (Entity (Name (N)))
6238 and then Comes_From_Source (Parent (N))
6239 then
6240 Expand_Vax_Foreign_Return (N);
6241 end if;
6242 end Expand_N_Function_Call;
6244 ---------------------------------------
6245 -- Expand_N_Procedure_Call_Statement --
6246 ---------------------------------------
6248 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
6249 begin
6250 Expand_Call (N);
6251 end Expand_N_Procedure_Call_Statement;
6253 --------------------------------------
6254 -- Expand_N_Simple_Return_Statement --
6255 --------------------------------------
6257 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
6258 begin
6259 -- Defend against previous errors (i.e. the return statement calls a
6260 -- function that is not available in configurable runtime).
6262 if Present (Expression (N))
6263 and then Nkind (Expression (N)) = N_Empty
6264 then
6265 Check_Error_Detected;
6266 return;
6267 end if;
6269 -- Distinguish the function and non-function cases:
6271 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
6273 when E_Function |
6274 E_Generic_Function =>
6275 Expand_Simple_Function_Return (N);
6277 when E_Procedure |
6278 E_Generic_Procedure |
6279 E_Entry |
6280 E_Entry_Family |
6281 E_Return_Statement =>
6282 Expand_Non_Function_Return (N);
6284 when others =>
6285 raise Program_Error;
6286 end case;
6288 exception
6289 when RE_Not_Available =>
6290 return;
6291 end Expand_N_Simple_Return_Statement;
6293 ------------------------------
6294 -- Expand_N_Subprogram_Body --
6295 ------------------------------
6297 -- Add poll call if ATC polling is enabled, unless the body will be inlined
6298 -- by the back-end.
6300 -- Add dummy push/pop label nodes at start and end to clear any local
6301 -- exception indications if local-exception-to-goto optimization is active.
6303 -- Add return statement if last statement in body is not a return statement
6304 -- (this makes things easier on Gigi which does not want to have to handle
6305 -- a missing return).
6307 -- Add call to Activate_Tasks if body is a task activator
6309 -- Deal with possible detection of infinite recursion
6311 -- Eliminate body completely if convention stubbed
6313 -- Encode entity names within body, since we will not need to reference
6314 -- these entities any longer in the front end.
6316 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
6318 -- Reset Pure indication if any parameter has root type System.Address
6319 -- or has any parameters of limited types, where limited means that the
6320 -- run-time view is limited (i.e. the full type is limited).
6322 -- Wrap thread body
6324 procedure Expand_N_Subprogram_Body (N : Node_Id) is
6325 Loc : constant Source_Ptr := Sloc (N);
6326 H : constant Node_Id := Handled_Statement_Sequence (N);
6327 Body_Id : Entity_Id;
6328 Except_H : Node_Id;
6329 L : List_Id;
6330 Spec_Id : Entity_Id;
6332 procedure Add_Return (S : List_Id);
6333 -- Append a return statement to the statement sequence S if the last
6334 -- statement is not already a return or a goto statement. Note that
6335 -- the latter test is not critical, it does not matter if we add a few
6336 -- extra returns, since they get eliminated anyway later on.
6338 ----------------
6339 -- Add_Return --
6340 ----------------
6342 procedure Add_Return (S : List_Id) is
6343 Last_Stm : Node_Id;
6344 Loc : Source_Ptr;
6346 begin
6347 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
6348 -- not relevant in this context since they are not executable.
6350 Last_Stm := Last (S);
6351 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
6352 Prev (Last_Stm);
6353 end loop;
6355 -- Now insert return unless last statement is a transfer
6357 if not Is_Transfer (Last_Stm) then
6359 -- The source location for the return is the end label of the
6360 -- procedure if present. Otherwise use the sloc of the last
6361 -- statement in the list. If the list comes from a generated
6362 -- exception handler and we are not debugging generated code,
6363 -- all the statements within the handler are made invisible
6364 -- to the debugger.
6366 if Nkind (Parent (S)) = N_Exception_Handler
6367 and then not Comes_From_Source (Parent (S))
6368 then
6369 Loc := Sloc (Last_Stm);
6370 elsif Present (End_Label (H)) then
6371 Loc := Sloc (End_Label (H));
6372 else
6373 Loc := Sloc (Last_Stm);
6374 end if;
6376 declare
6377 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
6379 begin
6380 -- Append return statement, and set analyzed manually. We can't
6381 -- call Analyze on this return since the scope is wrong.
6383 -- Note: it almost works to push the scope and then do the
6384 -- Analyze call, but something goes wrong in some weird cases
6385 -- and it is not worth worrying about ???
6387 Append_To (S, Rtn);
6388 Set_Analyzed (Rtn);
6390 -- Call _Postconditions procedure if appropriate. We need to
6391 -- do this explicitly because we did not analyze the generated
6392 -- return statement above, so the call did not get inserted.
6394 if Ekind (Spec_Id) = E_Procedure
6395 and then Has_Postconditions (Spec_Id)
6396 then
6397 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
6398 Insert_Action (Rtn,
6399 Make_Procedure_Call_Statement (Loc,
6400 Name =>
6401 New_Occurrence_Of
6402 (Postcondition_Proc (Spec_Id), Loc)));
6403 end if;
6404 end;
6405 end if;
6406 end Add_Return;
6408 -- Start of processing for Expand_N_Subprogram_Body
6410 begin
6411 -- Set L to either the list of declarations if present, or to the list
6412 -- of statements if no declarations are present. This is used to insert
6413 -- new stuff at the start.
6415 if Is_Non_Empty_List (Declarations (N)) then
6416 L := Declarations (N);
6417 else
6418 L := Statements (H);
6419 end if;
6421 -- If local-exception-to-goto optimization active, insert dummy push
6422 -- statements at start, and dummy pop statements at end, but inhibit
6423 -- this if we have No_Exception_Handlers, since they are useless and
6424 -- intefere with analysis, e.g. by codepeer.
6426 if (Debug_Flag_Dot_G
6427 or else Restriction_Active (No_Exception_Propagation))
6428 and then not Restriction_Active (No_Exception_Handlers)
6429 and then not CodePeer_Mode
6430 and then Is_Non_Empty_List (L)
6431 then
6432 declare
6433 FS : constant Node_Id := First (L);
6434 FL : constant Source_Ptr := Sloc (FS);
6435 LS : Node_Id;
6436 LL : Source_Ptr;
6438 begin
6439 -- LS points to either last statement, if statements are present
6440 -- or to the last declaration if there are no statements present.
6441 -- It is the node after which the pop's are generated.
6443 if Is_Non_Empty_List (Statements (H)) then
6444 LS := Last (Statements (H));
6445 else
6446 LS := Last (L);
6447 end if;
6449 LL := Sloc (LS);
6451 Insert_List_Before_And_Analyze (FS, New_List (
6452 Make_Push_Constraint_Error_Label (FL),
6453 Make_Push_Program_Error_Label (FL),
6454 Make_Push_Storage_Error_Label (FL)));
6456 Insert_List_After_And_Analyze (LS, New_List (
6457 Make_Pop_Constraint_Error_Label (LL),
6458 Make_Pop_Program_Error_Label (LL),
6459 Make_Pop_Storage_Error_Label (LL)));
6460 end;
6461 end if;
6463 -- Find entity for subprogram
6465 Body_Id := Defining_Entity (N);
6467 if Present (Corresponding_Spec (N)) then
6468 Spec_Id := Corresponding_Spec (N);
6469 else
6470 Spec_Id := Body_Id;
6471 end if;
6473 -- Need poll on entry to subprogram if polling enabled. We only do this
6474 -- for non-empty subprograms, since it does not seem necessary to poll
6475 -- for a dummy null subprogram.
6477 if Is_Non_Empty_List (L) then
6479 -- Do not add a polling call if the subprogram is to be inlined by
6480 -- the back-end, to avoid repeated calls with multiple inlinings.
6482 if Is_Inlined (Spec_Id)
6483 and then Front_End_Inlining
6484 and then Optimization_Level > 1
6485 then
6486 null;
6487 else
6488 Generate_Poll_Call (First (L));
6489 end if;
6490 end if;
6492 -- If this is a Pure function which has any parameters whose root type
6493 -- is System.Address, reset the Pure indication, since it will likely
6494 -- cause incorrect code to be generated as the parameter is probably
6495 -- a pointer, and the fact that the same pointer is passed does not mean
6496 -- that the same value is being referenced.
6498 -- Note that if the programmer gave an explicit Pure_Function pragma,
6499 -- then we believe the programmer, and leave the subprogram Pure.
6501 -- This code should probably be at the freeze point, so that it happens
6502 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
6503 -- semantic tree has Is_Pure set properly ???
6505 if Is_Pure (Spec_Id)
6506 and then Is_Subprogram (Spec_Id)
6507 and then not Has_Pragma_Pure_Function (Spec_Id)
6508 then
6509 declare
6510 F : Entity_Id;
6512 begin
6513 F := First_Formal (Spec_Id);
6514 while Present (F) loop
6515 if Is_Descendent_Of_Address (Etype (F))
6517 -- Note that this test is being made in the body of the
6518 -- subprogram, not the spec, so we are testing the full
6519 -- type for being limited here, as required.
6521 or else Is_Limited_Type (Etype (F))
6522 then
6523 Set_Is_Pure (Spec_Id, False);
6525 if Spec_Id /= Body_Id then
6526 Set_Is_Pure (Body_Id, False);
6527 end if;
6529 exit;
6530 end if;
6532 Next_Formal (F);
6533 end loop;
6534 end;
6535 end if;
6537 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
6539 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
6540 declare
6541 F : Entity_Id;
6542 A : Node_Id;
6544 begin
6545 -- Loop through formals
6547 F := First_Formal (Spec_Id);
6548 while Present (F) loop
6549 if Is_Scalar_Type (Etype (F))
6550 and then Ekind (F) = E_Out_Parameter
6551 then
6552 Check_Restriction (No_Default_Initialization, F);
6554 -- Insert the initialization. We turn off validity checks
6555 -- for this assignment, since we do not want any check on
6556 -- the initial value itself (which may well be invalid).
6557 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
6559 A := Make_Assignment_Statement (Loc,
6560 Name => New_Occurrence_Of (F, Loc),
6561 Expression => Get_Simple_Init_Val (Etype (F), N));
6562 Set_Suppress_Assignment_Checks (A);
6564 Insert_Before_And_Analyze (First (L),
6565 A, Suppress => Validity_Check);
6566 end if;
6568 Next_Formal (F);
6569 end loop;
6570 end;
6571 end if;
6573 -- Clear out statement list for stubbed procedure
6575 if Present (Corresponding_Spec (N)) then
6576 Set_Elaboration_Flag (N, Spec_Id);
6578 if Convention (Spec_Id) = Convention_Stubbed
6579 or else Is_Eliminated (Spec_Id)
6580 then
6581 Set_Declarations (N, Empty_List);
6582 Set_Handled_Statement_Sequence (N,
6583 Make_Handled_Sequence_Of_Statements (Loc,
6584 Statements => New_List (Make_Null_Statement (Loc))));
6585 return;
6586 end if;
6587 end if;
6589 -- Create a set of discriminals for the next protected subprogram body
6591 if Is_List_Member (N)
6592 and then Present (Parent (List_Containing (N)))
6593 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6594 and then Present (Next_Protected_Operation (N))
6595 then
6596 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
6597 end if;
6599 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
6600 -- subprograms with no specs are not frozen.
6602 declare
6603 Typ : constant Entity_Id := Etype (Spec_Id);
6604 Utyp : constant Entity_Id := Underlying_Type (Typ);
6606 begin
6607 if not Acts_As_Spec (N)
6608 and then Nkind (Parent (Parent (Spec_Id))) /=
6609 N_Subprogram_Body_Stub
6610 then
6611 null;
6613 elsif Is_Limited_View (Typ) then
6614 Set_Returns_By_Ref (Spec_Id);
6616 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6617 Set_Returns_By_Ref (Spec_Id);
6618 end if;
6619 end;
6621 -- For a procedure, we add a return for all possible syntactic ends of
6622 -- the subprogram.
6624 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
6625 Add_Return (Statements (H));
6627 if Present (Exception_Handlers (H)) then
6628 Except_H := First_Non_Pragma (Exception_Handlers (H));
6629 while Present (Except_H) loop
6630 Add_Return (Statements (Except_H));
6631 Next_Non_Pragma (Except_H);
6632 end loop;
6633 end if;
6635 -- For a function, we must deal with the case where there is at least
6636 -- one missing return. What we do is to wrap the entire body of the
6637 -- function in a block:
6639 -- begin
6640 -- ...
6641 -- end;
6643 -- becomes
6645 -- begin
6646 -- begin
6647 -- ...
6648 -- end;
6650 -- raise Program_Error;
6651 -- end;
6653 -- This approach is necessary because the raise must be signalled to the
6654 -- caller, not handled by any local handler (RM 6.4(11)).
6656 -- Note: we do not need to analyze the constructed sequence here, since
6657 -- it has no handler, and an attempt to analyze the handled statement
6658 -- sequence twice is risky in various ways (e.g. the issue of expanding
6659 -- cleanup actions twice).
6661 elsif Has_Missing_Return (Spec_Id) then
6662 declare
6663 Hloc : constant Source_Ptr := Sloc (H);
6664 Blok : constant Node_Id :=
6665 Make_Block_Statement (Hloc,
6666 Handled_Statement_Sequence => H);
6667 Rais : constant Node_Id :=
6668 Make_Raise_Program_Error (Hloc,
6669 Reason => PE_Missing_Return);
6671 begin
6672 Set_Handled_Statement_Sequence (N,
6673 Make_Handled_Sequence_Of_Statements (Hloc,
6674 Statements => New_List (Blok, Rais)));
6676 Push_Scope (Spec_Id);
6677 Analyze (Blok);
6678 Analyze (Rais);
6679 Pop_Scope;
6680 end;
6681 end if;
6683 -- If subprogram contains a parameterless recursive call, then we may
6684 -- have an infinite recursion, so see if we can generate code to check
6685 -- for this possibility if storage checks are not suppressed.
6687 if Ekind (Spec_Id) = E_Procedure
6688 and then Has_Recursive_Call (Spec_Id)
6689 and then not Storage_Checks_Suppressed (Spec_Id)
6690 then
6691 Detect_Infinite_Recursion (N, Spec_Id);
6692 end if;
6694 -- Set to encode entity names in package body before gigi is called
6696 Qualify_Entity_Names (N);
6697 end Expand_N_Subprogram_Body;
6699 -----------------------------------
6700 -- Expand_N_Subprogram_Body_Stub --
6701 -----------------------------------
6703 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
6704 begin
6705 if Present (Corresponding_Body (N)) then
6706 Expand_N_Subprogram_Body (
6707 Unit_Declaration_Node (Corresponding_Body (N)));
6708 end if;
6709 end Expand_N_Subprogram_Body_Stub;
6711 -------------------------------------
6712 -- Expand_N_Subprogram_Declaration --
6713 -------------------------------------
6715 -- If the declaration appears within a protected body, it is a private
6716 -- operation of the protected type. We must create the corresponding
6717 -- protected subprogram an associated formals. For a normal protected
6718 -- operation, this is done when expanding the protected type declaration.
6720 -- If the declaration is for a null procedure, emit null body
6722 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
6723 Loc : constant Source_Ptr := Sloc (N);
6724 Subp : constant Entity_Id := Defining_Entity (N);
6725 Scop : constant Entity_Id := Scope (Subp);
6726 Prot_Decl : Node_Id;
6727 Prot_Bod : Node_Id;
6728 Prot_Id : Entity_Id;
6730 begin
6731 -- In SPARK, subprogram declarations are only allowed in package
6732 -- specifications.
6734 if Nkind (Parent (N)) /= N_Package_Specification then
6735 if Nkind (Parent (N)) = N_Compilation_Unit then
6736 Check_SPARK_Restriction
6737 ("subprogram declaration is not a library item", N);
6739 elsif Present (Next (N))
6740 and then Nkind (Next (N)) = N_Pragma
6741 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
6742 then
6743 -- In SPARK, subprogram declarations are also permitted in
6744 -- declarative parts when immediately followed by a corresponding
6745 -- pragma Import. We only check here that there is some pragma
6746 -- Import.
6748 null;
6749 else
6750 Check_SPARK_Restriction
6751 ("subprogram declaration is not allowed here", N);
6752 end if;
6753 end if;
6755 -- Deal with case of protected subprogram. Do not generate protected
6756 -- operation if operation is flagged as eliminated.
6758 if Is_List_Member (N)
6759 and then Present (Parent (List_Containing (N)))
6760 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
6761 and then Is_Protected_Type (Scop)
6762 then
6763 if No (Protected_Body_Subprogram (Subp))
6764 and then not Is_Eliminated (Subp)
6765 then
6766 Prot_Decl :=
6767 Make_Subprogram_Declaration (Loc,
6768 Specification =>
6769 Build_Protected_Sub_Specification
6770 (N, Scop, Unprotected_Mode));
6772 -- The protected subprogram is declared outside of the protected
6773 -- body. Given that the body has frozen all entities so far, we
6774 -- analyze the subprogram and perform freezing actions explicitly.
6775 -- including the generation of an explicit freeze node, to ensure
6776 -- that gigi has the proper order of elaboration.
6777 -- If the body is a subunit, the insertion point is before the
6778 -- stub in the parent.
6780 Prot_Bod := Parent (List_Containing (N));
6782 if Nkind (Parent (Prot_Bod)) = N_Subunit then
6783 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
6784 end if;
6786 Insert_Before (Prot_Bod, Prot_Decl);
6787 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
6788 Set_Has_Delayed_Freeze (Prot_Id);
6790 Push_Scope (Scope (Scop));
6791 Analyze (Prot_Decl);
6792 Freeze_Before (N, Prot_Id);
6793 Set_Protected_Body_Subprogram (Subp, Prot_Id);
6795 -- Create protected operation as well. Even though the operation
6796 -- is only accessible within the body, it is possible to make it
6797 -- available outside of the protected object by using 'Access to
6798 -- provide a callback, so build protected version in all cases.
6800 Prot_Decl :=
6801 Make_Subprogram_Declaration (Loc,
6802 Specification =>
6803 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
6804 Insert_Before (Prot_Bod, Prot_Decl);
6805 Analyze (Prot_Decl);
6807 Pop_Scope;
6808 end if;
6810 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
6811 -- cases this is superfluous because calls to it will be automatically
6812 -- inlined, but we definitely need the body if preconditions for the
6813 -- procedure are present.
6815 elsif Nkind (Specification (N)) = N_Procedure_Specification
6816 and then Null_Present (Specification (N))
6817 then
6818 declare
6819 Bod : constant Node_Id := Body_To_Inline (N);
6821 begin
6822 Set_Has_Completion (Subp, False);
6823 Append_Freeze_Action (Subp, Bod);
6825 -- The body now contains raise statements, so calls to it will
6826 -- not be inlined.
6828 Set_Is_Inlined (Subp, False);
6829 end;
6830 end if;
6831 end Expand_N_Subprogram_Declaration;
6833 --------------------------------
6834 -- Expand_Non_Function_Return --
6835 --------------------------------
6837 procedure Expand_Non_Function_Return (N : Node_Id) is
6838 pragma Assert (No (Expression (N)));
6840 Loc : constant Source_Ptr := Sloc (N);
6841 Scope_Id : Entity_Id :=
6842 Return_Applies_To (Return_Statement_Entity (N));
6843 Kind : constant Entity_Kind := Ekind (Scope_Id);
6844 Call : Node_Id;
6845 Acc_Stat : Node_Id;
6846 Goto_Stat : Node_Id;
6847 Lab_Node : Node_Id;
6849 begin
6850 -- Call _Postconditions procedure if procedure with active
6851 -- postconditions. Here, we use the Postcondition_Proc attribute,
6852 -- which is needed for implicitly-generated returns. Functions
6853 -- never have implicitly-generated returns, and there's no
6854 -- room for Postcondition_Proc in E_Function, so we look up the
6855 -- identifier Name_uPostconditions for function returns (see
6856 -- Expand_Simple_Function_Return).
6858 if Ekind (Scope_Id) = E_Procedure
6859 and then Has_Postconditions (Scope_Id)
6860 then
6861 pragma Assert (Present (Postcondition_Proc (Scope_Id)));
6862 Insert_Action (N,
6863 Make_Procedure_Call_Statement (Loc,
6864 Name => New_Occurrence_Of (Postcondition_Proc (Scope_Id), Loc)));
6865 end if;
6867 -- If it is a return from a procedure do no extra steps
6869 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
6870 return;
6872 -- If it is a nested return within an extended one, replace it with a
6873 -- return of the previously declared return object.
6875 elsif Kind = E_Return_Statement then
6876 Rewrite (N,
6877 Make_Simple_Return_Statement (Loc,
6878 Expression =>
6879 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
6880 Set_Comes_From_Extended_Return_Statement (N);
6881 Set_Return_Statement_Entity (N, Scope_Id);
6882 Expand_Simple_Function_Return (N);
6883 return;
6884 end if;
6886 pragma Assert (Is_Entry (Scope_Id));
6888 -- Look at the enclosing block to see whether the return is from an
6889 -- accept statement or an entry body.
6891 for J in reverse 0 .. Scope_Stack.Last loop
6892 Scope_Id := Scope_Stack.Table (J).Entity;
6893 exit when Is_Concurrent_Type (Scope_Id);
6894 end loop;
6896 -- If it is a return from accept statement it is expanded as call to
6897 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
6899 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
6900 -- Expand_N_Accept_Alternative in exp_ch9.adb)
6902 if Is_Task_Type (Scope_Id) then
6904 Call :=
6905 Make_Procedure_Call_Statement (Loc,
6906 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
6907 Insert_Before (N, Call);
6908 -- why not insert actions here???
6909 Analyze (Call);
6911 Acc_Stat := Parent (N);
6912 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6913 Acc_Stat := Parent (Acc_Stat);
6914 end loop;
6916 Lab_Node := Last (Statements
6917 (Handled_Statement_Sequence (Acc_Stat)));
6919 Goto_Stat := Make_Goto_Statement (Loc,
6920 Name => New_Occurrence_Of
6921 (Entity (Identifier (Lab_Node)), Loc));
6923 Set_Analyzed (Goto_Stat);
6925 Rewrite (N, Goto_Stat);
6926 Analyze (N);
6928 -- If it is a return from an entry body, put a Complete_Entry_Body call
6929 -- in front of the return.
6931 elsif Is_Protected_Type (Scope_Id) then
6932 Call :=
6933 Make_Procedure_Call_Statement (Loc,
6934 Name =>
6935 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
6936 Parameter_Associations => New_List (
6937 Make_Attribute_Reference (Loc,
6938 Prefix =>
6939 New_Occurrence_Of
6940 (Find_Protection_Object (Current_Scope), Loc),
6941 Attribute_Name => Name_Unchecked_Access)));
6943 Insert_Before (N, Call);
6944 Analyze (Call);
6945 end if;
6946 end Expand_Non_Function_Return;
6948 ---------------------------------------
6949 -- Expand_Protected_Object_Reference --
6950 ---------------------------------------
6952 function Expand_Protected_Object_Reference
6953 (N : Node_Id;
6954 Scop : Entity_Id) return Node_Id
6956 Loc : constant Source_Ptr := Sloc (N);
6957 Corr : Entity_Id;
6958 Rec : Node_Id;
6959 Param : Entity_Id;
6960 Proc : Entity_Id;
6962 begin
6963 Rec := Make_Identifier (Loc, Name_uObject);
6964 Set_Etype (Rec, Corresponding_Record_Type (Scop));
6966 -- Find enclosing protected operation, and retrieve its first parameter,
6967 -- which denotes the enclosing protected object. If the enclosing
6968 -- operation is an entry, we are immediately within the protected body,
6969 -- and we can retrieve the object from the service entries procedure. A
6970 -- barrier function has the same signature as an entry. A barrier
6971 -- function is compiled within the protected object, but unlike
6972 -- protected operations its never needs locks, so that its protected
6973 -- body subprogram points to itself.
6975 Proc := Current_Scope;
6976 while Present (Proc)
6977 and then Scope (Proc) /= Scop
6978 loop
6979 Proc := Scope (Proc);
6980 end loop;
6982 Corr := Protected_Body_Subprogram (Proc);
6984 if No (Corr) then
6986 -- Previous error left expansion incomplete.
6987 -- Nothing to do on this call.
6989 return Empty;
6990 end if;
6992 Param :=
6993 Defining_Identifier
6994 (First (Parameter_Specifications (Parent (Corr))));
6996 if Is_Subprogram (Proc)
6997 and then Proc /= Corr
6998 then
6999 -- Protected function or procedure
7001 Set_Entity (Rec, Param);
7003 -- Rec is a reference to an entity which will not be in scope when
7004 -- the call is reanalyzed, and needs no further analysis.
7006 Set_Analyzed (Rec);
7008 else
7009 -- Entry or barrier function for entry body. The first parameter of
7010 -- the entry body procedure is pointer to the object. We create a
7011 -- local variable of the proper type, duplicating what is done to
7012 -- define _object later on.
7014 declare
7015 Decls : List_Id;
7016 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
7018 begin
7019 Decls := New_List (
7020 Make_Full_Type_Declaration (Loc,
7021 Defining_Identifier => Obj_Ptr,
7022 Type_Definition =>
7023 Make_Access_To_Object_Definition (Loc,
7024 Subtype_Indication =>
7025 New_Occurrence_Of
7026 (Corresponding_Record_Type (Scop), Loc))));
7028 Insert_Actions (N, Decls);
7029 Freeze_Before (N, Obj_Ptr);
7031 Rec :=
7032 Make_Explicit_Dereference (Loc,
7033 Prefix =>
7034 Unchecked_Convert_To (Obj_Ptr,
7035 New_Occurrence_Of (Param, Loc)));
7037 -- Analyze new actual. Other actuals in calls are already analyzed
7038 -- and the list of actuals is not reanalyzed after rewriting.
7040 Set_Parent (Rec, N);
7041 Analyze (Rec);
7042 end;
7043 end if;
7045 return Rec;
7046 end Expand_Protected_Object_Reference;
7048 --------------------------------------
7049 -- Expand_Protected_Subprogram_Call --
7050 --------------------------------------
7052 procedure Expand_Protected_Subprogram_Call
7053 (N : Node_Id;
7054 Subp : Entity_Id;
7055 Scop : Entity_Id)
7057 Rec : Node_Id;
7059 begin
7060 -- If the protected object is not an enclosing scope, this is an inter-
7061 -- object function call. Inter-object procedure calls are expanded by
7062 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
7063 -- subprogram being called is in the protected body being compiled, and
7064 -- if the protected object in the call is statically the enclosing type.
7065 -- The object may be an component of some other data structure, in which
7066 -- case this must be handled as an inter-object call.
7068 if not In_Open_Scopes (Scop)
7069 or else not Is_Entity_Name (Name (N))
7070 then
7071 if Nkind (Name (N)) = N_Selected_Component then
7072 Rec := Prefix (Name (N));
7074 else
7075 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
7076 Rec := Prefix (Prefix (Name (N)));
7077 end if;
7079 Build_Protected_Subprogram_Call (N,
7080 Name => New_Occurrence_Of (Subp, Sloc (N)),
7081 Rec => Convert_Concurrent (Rec, Etype (Rec)),
7082 External => True);
7084 else
7085 Rec := Expand_Protected_Object_Reference (N, Scop);
7087 if No (Rec) then
7088 return;
7089 end if;
7091 Build_Protected_Subprogram_Call (N,
7092 Name => Name (N),
7093 Rec => Rec,
7094 External => False);
7096 end if;
7098 -- If it is a function call it can appear in elaboration code and
7099 -- the called entity must be frozen here.
7101 if Ekind (Subp) = E_Function then
7102 Freeze_Expression (Name (N));
7103 end if;
7105 -- Analyze and resolve the new call. The actuals have already been
7106 -- resolved, but expansion of a function call will add extra actuals
7107 -- if needed. Analysis of a procedure call already includes resolution.
7109 Analyze (N);
7111 if Ekind (Subp) = E_Function then
7112 Resolve (N, Etype (Subp));
7113 end if;
7114 end Expand_Protected_Subprogram_Call;
7116 --------------------------------------------
7117 -- Has_Unconstrained_Access_Discriminants --
7118 --------------------------------------------
7120 function Has_Unconstrained_Access_Discriminants
7121 (Subtyp : Entity_Id) return Boolean
7123 Discr : Entity_Id;
7125 begin
7126 if Has_Discriminants (Subtyp)
7127 and then not Is_Constrained (Subtyp)
7128 then
7129 Discr := First_Discriminant (Subtyp);
7130 while Present (Discr) loop
7131 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
7132 return True;
7133 end if;
7135 Next_Discriminant (Discr);
7136 end loop;
7137 end if;
7139 return False;
7140 end Has_Unconstrained_Access_Discriminants;
7142 -----------------------------------
7143 -- Expand_Simple_Function_Return --
7144 -----------------------------------
7146 -- The "simple" comes from the syntax rule simple_return_statement. The
7147 -- semantics are not at all simple.
7149 procedure Expand_Simple_Function_Return (N : Node_Id) is
7150 Loc : constant Source_Ptr := Sloc (N);
7152 Scope_Id : constant Entity_Id :=
7153 Return_Applies_To (Return_Statement_Entity (N));
7154 -- The function we are returning from
7156 R_Type : constant Entity_Id := Etype (Scope_Id);
7157 -- The result type of the function
7159 Utyp : constant Entity_Id := Underlying_Type (R_Type);
7161 Exp : constant Node_Id := Expression (N);
7162 pragma Assert (Present (Exp));
7164 Exptyp : constant Entity_Id := Etype (Exp);
7165 -- The type of the expression (not necessarily the same as R_Type)
7167 Subtype_Ind : Node_Id;
7168 -- If the result type of the function is class-wide and the expression
7169 -- has a specific type, then we use the expression's type as the type of
7170 -- the return object. In cases where the expression is an aggregate that
7171 -- is built in place, this avoids the need for an expensive conversion
7172 -- of the return object to the specific type on assignments to the
7173 -- individual components.
7175 begin
7176 if Is_Class_Wide_Type (R_Type)
7177 and then not Is_Class_Wide_Type (Etype (Exp))
7178 then
7179 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
7180 else
7181 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
7182 end if;
7184 -- For the case of a simple return that does not come from an extended
7185 -- return, in the case of Ada 2005 where we are returning a limited
7186 -- type, we rewrite "return <expression>;" to be:
7188 -- return _anon_ : <return_subtype> := <expression>
7190 -- The expansion produced by Expand_N_Extended_Return_Statement will
7191 -- contain simple return statements (for example, a block containing
7192 -- simple return of the return object), which brings us back here with
7193 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
7194 -- checking for a simple return that does not come from an extended
7195 -- return is to avoid this infinite recursion.
7197 -- The reason for this design is that for Ada 2005 limited returns, we
7198 -- need to reify the return object, so we can build it "in place", and
7199 -- we need a block statement to hang finalization and tasking stuff.
7201 -- ??? In order to avoid disruption, we avoid translating to extended
7202 -- return except in the cases where we really need to (Ada 2005 for
7203 -- inherently limited). We might prefer to do this translation in all
7204 -- cases (except perhaps for the case of Ada 95 inherently limited),
7205 -- in order to fully exercise the Expand_N_Extended_Return_Statement
7206 -- code. This would also allow us to do the build-in-place optimization
7207 -- for efficiency even in cases where it is semantically not required.
7209 -- As before, we check the type of the return expression rather than the
7210 -- return type of the function, because the latter may be a limited
7211 -- class-wide interface type, which is not a limited type, even though
7212 -- the type of the expression may be.
7214 if not Comes_From_Extended_Return_Statement (N)
7215 and then Is_Limited_View (Etype (Expression (N)))
7216 and then Ada_Version >= Ada_2005
7217 and then not Debug_Flag_Dot_L
7219 -- The functionality of interface thunks is simple and it is always
7220 -- handled by means of simple return statements. This leaves their
7221 -- expansion simple and clean.
7223 and then not Is_Thunk (Current_Scope)
7224 then
7225 declare
7226 Return_Object_Entity : constant Entity_Id :=
7227 Make_Temporary (Loc, 'R', Exp);
7229 Obj_Decl : constant Node_Id :=
7230 Make_Object_Declaration (Loc,
7231 Defining_Identifier => Return_Object_Entity,
7232 Object_Definition => Subtype_Ind,
7233 Expression => Exp);
7235 Ext : constant Node_Id :=
7236 Make_Extended_Return_Statement (Loc,
7237 Return_Object_Declarations => New_List (Obj_Decl));
7238 -- Do not perform this high-level optimization if the result type
7239 -- is an interface because the "this" pointer must be displaced.
7241 begin
7242 Rewrite (N, Ext);
7243 Analyze (N);
7244 return;
7245 end;
7246 end if;
7248 -- Here we have a simple return statement that is part of the expansion
7249 -- of an extended return statement (either written by the user, or
7250 -- generated by the above code).
7252 -- Always normalize C/Fortran boolean result. This is not always needed,
7253 -- but it seems a good idea to minimize the passing around of non-
7254 -- normalized values, and in any case this handles the processing of
7255 -- barrier functions for protected types, which turn the condition into
7256 -- a return statement.
7258 if Is_Boolean_Type (Exptyp)
7259 and then Nonzero_Is_True (Exptyp)
7260 then
7261 Adjust_Condition (Exp);
7262 Adjust_Result_Type (Exp, Exptyp);
7263 end if;
7265 -- Do validity check if enabled for returns
7267 if Validity_Checks_On
7268 and then Validity_Check_Returns
7269 then
7270 Ensure_Valid (Exp);
7271 end if;
7273 -- Check the result expression of a scalar function against the subtype
7274 -- of the function by inserting a conversion. This conversion must
7275 -- eventually be performed for other classes of types, but for now it's
7276 -- only done for scalars.
7277 -- ???
7279 if Is_Scalar_Type (Exptyp) then
7280 Rewrite (Exp, Convert_To (R_Type, Exp));
7282 -- The expression is resolved to ensure that the conversion gets
7283 -- expanded to generate a possible constraint check.
7285 Analyze_And_Resolve (Exp, R_Type);
7286 end if;
7288 -- Deal with returning variable length objects and controlled types
7290 -- Nothing to do if we are returning by reference, or this is not a
7291 -- type that requires special processing (indicated by the fact that
7292 -- it requires a cleanup scope for the secondary stack case).
7294 if Is_Limited_View (Exptyp)
7295 or else Is_Limited_Interface (Exptyp)
7296 then
7297 null;
7299 -- No copy needed for thunks returning interface type objects since
7300 -- the object is returned by reference and the maximum functionality
7301 -- required is just to displace the pointer.
7303 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
7304 null;
7306 elsif not Requires_Transient_Scope (R_Type) then
7308 -- Mutable records with no variable length components are not
7309 -- returned on the sec-stack, so we need to make sure that the
7310 -- backend will only copy back the size of the actual value, and not
7311 -- the maximum size. We create an actual subtype for this purpose.
7313 declare
7314 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
7315 Decl : Node_Id;
7316 Ent : Entity_Id;
7317 begin
7318 if Has_Discriminants (Ubt)
7319 and then not Is_Constrained (Ubt)
7320 and then not Has_Unchecked_Union (Ubt)
7321 then
7322 Decl := Build_Actual_Subtype (Ubt, Exp);
7323 Ent := Defining_Identifier (Decl);
7324 Insert_Action (Exp, Decl);
7325 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
7326 Analyze_And_Resolve (Exp);
7327 end if;
7328 end;
7330 -- Here if secondary stack is used
7332 else
7333 -- Make sure that no surrounding block will reclaim the secondary
7334 -- stack on which we are going to put the result. Not only may this
7335 -- introduce secondary stack leaks but worse, if the reclamation is
7336 -- done too early, then the result we are returning may get
7337 -- clobbered.
7339 declare
7340 S : Entity_Id;
7341 begin
7342 S := Current_Scope;
7343 while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
7344 Set_Sec_Stack_Needed_For_Return (S, True);
7345 S := Enclosing_Dynamic_Scope (S);
7346 end loop;
7348 -- The enclosing function itself must be marked as well, to
7349 -- prevent premature secondary stack cleanup.
7351 if Ekind (S) = E_Function then
7352 Set_Sec_Stack_Needed_For_Return (Scope_Id);
7353 end if;
7354 end;
7356 -- Optimize the case where the result is a function call. In this
7357 -- case either the result is already on the secondary stack, or is
7358 -- already being returned with the stack pointer depressed and no
7359 -- further processing is required except to set the By_Ref flag
7360 -- to ensure that gigi does not attempt an extra unnecessary copy.
7361 -- (actually not just unnecessary but harmfully wrong in the case
7362 -- of a controlled type, where gigi does not know how to do a copy).
7363 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
7364 -- for array types if the constrained status of the target type is
7365 -- different from that of the expression.
7367 if Requires_Transient_Scope (Exptyp)
7368 and then
7369 (not Is_Array_Type (Exptyp)
7370 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
7371 or else CW_Or_Has_Controlled_Part (Utyp))
7372 and then Nkind (Exp) = N_Function_Call
7373 then
7374 Set_By_Ref (N);
7376 -- Remove side effects from the expression now so that other parts
7377 -- of the expander do not have to reanalyze this node without this
7378 -- optimization
7380 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
7382 -- For controlled types, do the allocation on the secondary stack
7383 -- manually in order to call adjust at the right time:
7385 -- type Anon1 is access R_Type;
7386 -- for Anon1'Storage_pool use ss_pool;
7387 -- Anon2 : anon1 := new R_Type'(expr);
7388 -- return Anon2.all;
7390 -- We do the same for classwide types that are not potentially
7391 -- controlled (by the virtue of restriction No_Finalization) because
7392 -- gigi is not able to properly allocate class-wide types.
7394 elsif CW_Or_Has_Controlled_Part (Utyp) then
7395 declare
7396 Loc : constant Source_Ptr := Sloc (N);
7397 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
7398 Alloc_Node : Node_Id;
7399 Temp : Entity_Id;
7401 begin
7402 Set_Ekind (Acc_Typ, E_Access_Type);
7404 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
7406 -- This is an allocator for the secondary stack, and it's fine
7407 -- to have Comes_From_Source set False on it, as gigi knows not
7408 -- to flag it as a violation of No_Implicit_Heap_Allocations.
7410 Alloc_Node :=
7411 Make_Allocator (Loc,
7412 Expression =>
7413 Make_Qualified_Expression (Loc,
7414 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
7415 Expression => Relocate_Node (Exp)));
7417 -- We do not want discriminant checks on the declaration,
7418 -- given that it gets its value from the allocator.
7420 Set_No_Initialization (Alloc_Node);
7422 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
7424 Insert_List_Before_And_Analyze (N, New_List (
7425 Make_Full_Type_Declaration (Loc,
7426 Defining_Identifier => Acc_Typ,
7427 Type_Definition =>
7428 Make_Access_To_Object_Definition (Loc,
7429 Subtype_Indication => Subtype_Ind)),
7431 Make_Object_Declaration (Loc,
7432 Defining_Identifier => Temp,
7433 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
7434 Expression => Alloc_Node)));
7436 Rewrite (Exp,
7437 Make_Explicit_Dereference (Loc,
7438 Prefix => New_Occurrence_Of (Temp, Loc)));
7440 -- Ada 2005 (AI-251): If the type of the returned object is
7441 -- an interface then add an implicit type conversion to force
7442 -- displacement of the "this" pointer.
7444 if Is_Interface (R_Type) then
7445 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
7446 end if;
7448 Analyze_And_Resolve (Exp, R_Type);
7449 end;
7451 -- Otherwise use the gigi mechanism to allocate result on the
7452 -- secondary stack.
7454 else
7455 Check_Restriction (No_Secondary_Stack, N);
7456 Set_Storage_Pool (N, RTE (RE_SS_Pool));
7458 -- If we are generating code for the VM do not use
7459 -- SS_Allocate since everything is heap-allocated anyway.
7461 if VM_Target = No_VM then
7462 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
7463 end if;
7464 end if;
7465 end if;
7467 -- Implement the rules of 6.5(8-10), which require a tag check in
7468 -- the case of a limited tagged return type, and tag reassignment for
7469 -- nonlimited tagged results. These actions are needed when the return
7470 -- type is a specific tagged type and the result expression is a
7471 -- conversion or a formal parameter, because in that case the tag of
7472 -- the expression might differ from the tag of the specific result type.
7474 if Is_Tagged_Type (Utyp)
7475 and then not Is_Class_Wide_Type (Utyp)
7476 and then (Nkind_In (Exp, N_Type_Conversion,
7477 N_Unchecked_Type_Conversion)
7478 or else (Is_Entity_Name (Exp)
7479 and then Ekind (Entity (Exp)) in Formal_Kind))
7480 then
7481 -- When the return type is limited, perform a check that the tag of
7482 -- the result is the same as the tag of the return type.
7484 if Is_Limited_Type (R_Type) then
7485 Insert_Action (Exp,
7486 Make_Raise_Constraint_Error (Loc,
7487 Condition =>
7488 Make_Op_Ne (Loc,
7489 Left_Opnd =>
7490 Make_Selected_Component (Loc,
7491 Prefix => Duplicate_Subexpr (Exp),
7492 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7493 Right_Opnd =>
7494 Make_Attribute_Reference (Loc,
7495 Prefix =>
7496 New_Occurrence_Of (Base_Type (Utyp), Loc),
7497 Attribute_Name => Name_Tag)),
7498 Reason => CE_Tag_Check_Failed));
7500 -- If the result type is a specific nonlimited tagged type, then we
7501 -- have to ensure that the tag of the result is that of the result
7502 -- type. This is handled by making a copy of the expression in
7503 -- the case where it might have a different tag, namely when the
7504 -- expression is a conversion or a formal parameter. We create a new
7505 -- object of the result type and initialize it from the expression,
7506 -- which will implicitly force the tag to be set appropriately.
7508 else
7509 declare
7510 ExpR : constant Node_Id := Relocate_Node (Exp);
7511 Result_Id : constant Entity_Id :=
7512 Make_Temporary (Loc, 'R', ExpR);
7513 Result_Exp : constant Node_Id :=
7514 New_Occurrence_Of (Result_Id, Loc);
7515 Result_Obj : constant Node_Id :=
7516 Make_Object_Declaration (Loc,
7517 Defining_Identifier => Result_Id,
7518 Object_Definition =>
7519 New_Occurrence_Of (R_Type, Loc),
7520 Constant_Present => True,
7521 Expression => ExpR);
7523 begin
7524 Set_Assignment_OK (Result_Obj);
7525 Insert_Action (Exp, Result_Obj);
7527 Rewrite (Exp, Result_Exp);
7528 Analyze_And_Resolve (Exp, R_Type);
7529 end;
7530 end if;
7532 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
7533 -- a check that the level of the return expression's underlying type
7534 -- is not deeper than the level of the master enclosing the function.
7535 -- Always generate the check when the type of the return expression
7536 -- is class-wide, when it's a type conversion, or when it's a formal
7537 -- parameter. Otherwise, suppress the check in the case where the
7538 -- return expression has a specific type whose level is known not to
7539 -- be statically deeper than the function's result type.
7541 -- No runtime check needed in interface thunks since it is performed
7542 -- by the target primitive associated with the thunk.
7544 -- Note: accessibility check is skipped in the VM case, since there
7545 -- does not seem to be any practical way to implement this check.
7547 elsif Ada_Version >= Ada_2005
7548 and then Tagged_Type_Expansion
7549 and then Is_Class_Wide_Type (R_Type)
7550 and then not Is_Thunk (Current_Scope)
7551 and then not Scope_Suppress.Suppress (Accessibility_Check)
7552 and then
7553 (Is_Class_Wide_Type (Etype (Exp))
7554 or else Nkind_In (Exp, N_Type_Conversion,
7555 N_Unchecked_Type_Conversion)
7556 or else (Is_Entity_Name (Exp)
7557 and then Ekind (Entity (Exp)) in Formal_Kind)
7558 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
7559 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
7560 then
7561 declare
7562 Tag_Node : Node_Id;
7564 begin
7565 -- Ada 2005 (AI-251): In class-wide interface objects we displace
7566 -- "this" to reference the base of the object. This is required to
7567 -- get access to the TSD of the object.
7569 if Is_Class_Wide_Type (Etype (Exp))
7570 and then Is_Interface (Etype (Exp))
7571 and then Nkind (Exp) = N_Explicit_Dereference
7572 then
7573 Tag_Node :=
7574 Make_Explicit_Dereference (Loc,
7575 Prefix =>
7576 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
7577 Make_Function_Call (Loc,
7578 Name =>
7579 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
7580 Parameter_Associations => New_List (
7581 Unchecked_Convert_To (RTE (RE_Address),
7582 Duplicate_Subexpr (Prefix (Exp)))))));
7583 else
7584 Tag_Node :=
7585 Make_Attribute_Reference (Loc,
7586 Prefix => Duplicate_Subexpr (Exp),
7587 Attribute_Name => Name_Tag);
7588 end if;
7590 Insert_Action (Exp,
7591 Make_Raise_Program_Error (Loc,
7592 Condition =>
7593 Make_Op_Gt (Loc,
7594 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
7595 Right_Opnd =>
7596 Make_Integer_Literal (Loc,
7597 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
7598 Reason => PE_Accessibility_Check_Failed));
7599 end;
7601 -- AI05-0073: If function has a controlling access result, check that
7602 -- the tag of the return value, if it is not null, matches designated
7603 -- type of return type.
7605 -- The return expression is referenced twice in the code below, so it
7606 -- must be made free of side effects. Given that different compilers
7607 -- may evaluate these parameters in different order, both occurrences
7608 -- perform a copy.
7610 elsif Ekind (R_Type) = E_Anonymous_Access_Type
7611 and then Has_Controlling_Result (Scope_Id)
7612 then
7613 Insert_Action (N,
7614 Make_Raise_Constraint_Error (Loc,
7615 Condition =>
7616 Make_And_Then (Loc,
7617 Left_Opnd =>
7618 Make_Op_Ne (Loc,
7619 Left_Opnd => Duplicate_Subexpr (Exp),
7620 Right_Opnd => Make_Null (Loc)),
7622 Right_Opnd => Make_Op_Ne (Loc,
7623 Left_Opnd =>
7624 Make_Selected_Component (Loc,
7625 Prefix => Duplicate_Subexpr (Exp),
7626 Selector_Name => Make_Identifier (Loc, Name_uTag)),
7628 Right_Opnd =>
7629 Make_Attribute_Reference (Loc,
7630 Prefix =>
7631 New_Occurrence_Of (Designated_Type (R_Type), Loc),
7632 Attribute_Name => Name_Tag))),
7634 Reason => CE_Tag_Check_Failed),
7635 Suppress => All_Checks);
7636 end if;
7638 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
7639 -- ensure that the function result does not outlive an
7640 -- object designated by one of it discriminants.
7642 if Present (Extra_Accessibility_Of_Result (Scope_Id))
7643 and then Has_Unconstrained_Access_Discriminants (R_Type)
7644 then
7645 declare
7646 Discrim_Source : Node_Id;
7648 procedure Check_Against_Result_Level (Level : Node_Id);
7649 -- Check the given accessibility level against the level
7650 -- determined by the point of call. (AI05-0234).
7652 --------------------------------
7653 -- Check_Against_Result_Level --
7654 --------------------------------
7656 procedure Check_Against_Result_Level (Level : Node_Id) is
7657 begin
7658 Insert_Action (N,
7659 Make_Raise_Program_Error (Loc,
7660 Condition =>
7661 Make_Op_Gt (Loc,
7662 Left_Opnd => Level,
7663 Right_Opnd =>
7664 New_Occurrence_Of
7665 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
7666 Reason => PE_Accessibility_Check_Failed));
7667 end Check_Against_Result_Level;
7669 begin
7670 Discrim_Source := Exp;
7671 while Nkind (Discrim_Source) = N_Qualified_Expression loop
7672 Discrim_Source := Expression (Discrim_Source);
7673 end loop;
7675 if Nkind (Discrim_Source) = N_Identifier
7676 and then Is_Return_Object (Entity (Discrim_Source))
7677 then
7678 Discrim_Source := Entity (Discrim_Source);
7680 if Is_Constrained (Etype (Discrim_Source)) then
7681 Discrim_Source := Etype (Discrim_Source);
7682 else
7683 Discrim_Source := Expression (Parent (Discrim_Source));
7684 end if;
7686 elsif Nkind (Discrim_Source) = N_Identifier
7687 and then Nkind_In (Original_Node (Discrim_Source),
7688 N_Aggregate, N_Extension_Aggregate)
7689 then
7690 Discrim_Source := Original_Node (Discrim_Source);
7692 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
7693 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
7694 then
7695 Discrim_Source := Original_Node (Discrim_Source);
7696 end if;
7698 while Nkind_In (Discrim_Source, N_Qualified_Expression,
7699 N_Type_Conversion,
7700 N_Unchecked_Type_Conversion)
7701 loop
7702 Discrim_Source := Expression (Discrim_Source);
7703 end loop;
7705 case Nkind (Discrim_Source) is
7706 when N_Defining_Identifier =>
7708 pragma Assert (Is_Composite_Type (Discrim_Source)
7709 and then Has_Discriminants (Discrim_Source)
7710 and then Is_Constrained (Discrim_Source));
7712 declare
7713 Discrim : Entity_Id :=
7714 First_Discriminant (Base_Type (R_Type));
7715 Disc_Elmt : Elmt_Id :=
7716 First_Elmt (Discriminant_Constraint
7717 (Discrim_Source));
7718 begin
7719 loop
7720 if Ekind (Etype (Discrim)) =
7721 E_Anonymous_Access_Type
7722 then
7723 Check_Against_Result_Level
7724 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
7725 end if;
7727 Next_Elmt (Disc_Elmt);
7728 Next_Discriminant (Discrim);
7729 exit when not Present (Discrim);
7730 end loop;
7731 end;
7733 when N_Aggregate | N_Extension_Aggregate =>
7735 -- Unimplemented: extension aggregate case where discrims
7736 -- come from ancestor part, not extension part.
7738 declare
7739 Discrim : Entity_Id :=
7740 First_Discriminant (Base_Type (R_Type));
7742 Disc_Exp : Node_Id := Empty;
7744 Positionals_Exhausted
7745 : Boolean := not Present (Expressions
7746 (Discrim_Source));
7748 function Associated_Expr
7749 (Comp_Id : Entity_Id;
7750 Associations : List_Id) return Node_Id;
7752 -- Given a component and a component associations list,
7753 -- locate the expression for that component; returns
7754 -- Empty if no such expression is found.
7756 ---------------------
7757 -- Associated_Expr --
7758 ---------------------
7760 function Associated_Expr
7761 (Comp_Id : Entity_Id;
7762 Associations : List_Id) return Node_Id
7764 Assoc : Node_Id;
7765 Choice : Node_Id;
7767 begin
7768 -- Simple linear search seems ok here
7770 Assoc := First (Associations);
7771 while Present (Assoc) loop
7772 Choice := First (Choices (Assoc));
7773 while Present (Choice) loop
7774 if (Nkind (Choice) = N_Identifier
7775 and then Chars (Choice) = Chars (Comp_Id))
7776 or else (Nkind (Choice) = N_Others_Choice)
7777 then
7778 return Expression (Assoc);
7779 end if;
7781 Next (Choice);
7782 end loop;
7784 Next (Assoc);
7785 end loop;
7787 return Empty;
7788 end Associated_Expr;
7790 -- Start of processing for Expand_Simple_Function_Return
7792 begin
7793 if not Positionals_Exhausted then
7794 Disc_Exp := First (Expressions (Discrim_Source));
7795 end if;
7797 loop
7798 if Positionals_Exhausted then
7799 Disc_Exp :=
7800 Associated_Expr
7801 (Discrim,
7802 Component_Associations (Discrim_Source));
7803 end if;
7805 if Ekind (Etype (Discrim)) =
7806 E_Anonymous_Access_Type
7807 then
7808 Check_Against_Result_Level
7809 (Dynamic_Accessibility_Level (Disc_Exp));
7810 end if;
7812 Next_Discriminant (Discrim);
7813 exit when not Present (Discrim);
7815 if not Positionals_Exhausted then
7816 Next (Disc_Exp);
7817 Positionals_Exhausted := not Present (Disc_Exp);
7818 end if;
7819 end loop;
7820 end;
7822 when N_Function_Call =>
7824 -- No check needed (check performed by callee)
7826 null;
7828 when others =>
7830 declare
7831 Level : constant Node_Id :=
7832 Make_Integer_Literal (Loc,
7833 Object_Access_Level (Discrim_Source));
7835 begin
7836 -- Unimplemented: check for name prefix that includes
7837 -- a dereference of an access value with a dynamic
7838 -- accessibility level (e.g., an access param or a
7839 -- saooaaat) and use dynamic level in that case. For
7840 -- example:
7841 -- return Access_Param.all(Some_Index).Some_Component;
7842 -- ???
7844 Set_Etype (Level, Standard_Natural);
7845 Check_Against_Result_Level (Level);
7846 end;
7848 end case;
7849 end;
7850 end if;
7852 -- If we are returning an object that may not be bit-aligned, then copy
7853 -- the value into a temporary first. This copy may need to expand to a
7854 -- loop of component operations.
7856 if Is_Possibly_Unaligned_Slice (Exp)
7857 or else Is_Possibly_Unaligned_Object (Exp)
7858 then
7859 declare
7860 ExpR : constant Node_Id := Relocate_Node (Exp);
7861 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7862 begin
7863 Insert_Action (Exp,
7864 Make_Object_Declaration (Loc,
7865 Defining_Identifier => Tnn,
7866 Constant_Present => True,
7867 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7868 Expression => ExpR),
7869 Suppress => All_Checks);
7870 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7871 end;
7872 end if;
7874 -- Generate call to postcondition checks if they are present
7876 if Ekind (Scope_Id) = E_Function
7877 and then Has_Postconditions (Scope_Id)
7878 then
7879 -- We are going to reference the returned value twice in this case,
7880 -- once in the call to _Postconditions, and once in the actual return
7881 -- statement, but we can't have side effects happening twice, and in
7882 -- any case for efficiency we don't want to do the computation twice.
7884 -- If the returned expression is an entity name, we don't need to
7885 -- worry since it is efficient and safe to reference it twice, that's
7886 -- also true for literals other than string literals, and for the
7887 -- case of X.all where X is an entity name.
7889 if Is_Entity_Name (Exp)
7890 or else Nkind_In (Exp, N_Character_Literal,
7891 N_Integer_Literal,
7892 N_Real_Literal)
7893 or else (Nkind (Exp) = N_Explicit_Dereference
7894 and then Is_Entity_Name (Prefix (Exp)))
7895 then
7896 null;
7898 -- Otherwise we are going to need a temporary to capture the value
7900 else
7901 declare
7902 ExpR : Node_Id := Relocate_Node (Exp);
7903 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
7905 begin
7906 -- In the case of discriminated objects, we have created a
7907 -- constrained subtype above, and used the underlying type.
7908 -- This transformation is post-analysis and harmless, except
7909 -- that now the call to the post-condition will be analyzed and
7910 -- type kinds have to match.
7912 if Nkind (ExpR) = N_Unchecked_Type_Conversion
7913 and then
7914 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
7915 then
7916 ExpR := Expression (ExpR);
7917 end if;
7919 -- For a complex expression of an elementary type, capture
7920 -- value in the temporary and use it as the reference.
7922 if Is_Elementary_Type (R_Type) then
7923 Insert_Action (Exp,
7924 Make_Object_Declaration (Loc,
7925 Defining_Identifier => Tnn,
7926 Constant_Present => True,
7927 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7928 Expression => ExpR),
7929 Suppress => All_Checks);
7931 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7933 -- If we have something we can rename, generate a renaming of
7934 -- the object and replace the expression with a reference
7936 elsif Is_Object_Reference (Exp) then
7937 Insert_Action (Exp,
7938 Make_Object_Renaming_Declaration (Loc,
7939 Defining_Identifier => Tnn,
7940 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
7941 Name => ExpR),
7942 Suppress => All_Checks);
7944 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7946 -- Otherwise we have something like a string literal or an
7947 -- aggregate. We could copy the value, but that would be
7948 -- inefficient. Instead we make a reference to the value and
7949 -- capture this reference with a renaming, the expression is
7950 -- then replaced by a dereference of this renaming.
7952 else
7953 -- For now, copy the value, since the code below does not
7954 -- seem to work correctly ???
7956 Insert_Action (Exp,
7957 Make_Object_Declaration (Loc,
7958 Defining_Identifier => Tnn,
7959 Constant_Present => True,
7960 Object_Definition => New_Occurrence_Of (R_Type, Loc),
7961 Expression => Relocate_Node (Exp)),
7962 Suppress => All_Checks);
7964 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
7966 -- Insert_Action (Exp,
7967 -- Make_Object_Renaming_Declaration (Loc,
7968 -- Defining_Identifier => Tnn,
7969 -- Access_Definition =>
7970 -- Make_Access_Definition (Loc,
7971 -- All_Present => True,
7972 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
7973 -- Name =>
7974 -- Make_Reference (Loc,
7975 -- Prefix => Relocate_Node (Exp))),
7976 -- Suppress => All_Checks);
7978 -- Rewrite (Exp,
7979 -- Make_Explicit_Dereference (Loc,
7980 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
7981 end if;
7982 end;
7983 end if;
7985 -- Generate call to _postconditions
7987 Insert_Action (Exp,
7988 Make_Procedure_Call_Statement (Loc,
7989 Name => Make_Identifier (Loc, Name_uPostconditions),
7990 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
7991 end if;
7993 -- Ada 2005 (AI-251): If this return statement corresponds with an
7994 -- simple return statement associated with an extended return statement
7995 -- and the type of the returned object is an interface then generate an
7996 -- implicit conversion to force displacement of the "this" pointer.
7998 if Ada_Version >= Ada_2005
7999 and then Comes_From_Extended_Return_Statement (N)
8000 and then Nkind (Expression (N)) = N_Identifier
8001 and then Is_Interface (Utyp)
8002 and then Utyp /= Underlying_Type (Exptyp)
8003 then
8004 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
8005 Analyze_And_Resolve (Exp);
8006 end if;
8007 end Expand_Simple_Function_Return;
8009 --------------------------------
8010 -- Expand_Subprogram_Contract --
8011 --------------------------------
8013 procedure Expand_Subprogram_Contract
8014 (N : Node_Id;
8015 Spec_Id : Entity_Id;
8016 Body_Id : Entity_Id)
8018 procedure Add_Invariant_And_Predicate_Checks
8019 (Subp_Id : Entity_Id;
8020 Stmts : in out List_Id;
8021 Result : out Node_Id);
8022 -- Process the result of function Subp_Id (if applicable) and all its
8023 -- formals. Add invariant and predicate checks where applicable. The
8024 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
8025 -- function, Result contains the entity of parameter _Result, to be
8026 -- used in the creation of procedure _Postconditions.
8028 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
8029 -- Append a node to a list. If there is no list, create a new one. When
8030 -- the item denotes a pragma, it is added to the list only when it is
8031 -- enabled.
8033 procedure Build_Postconditions_Procedure
8034 (Subp_Id : Entity_Id;
8035 Stmts : List_Id;
8036 Result : Entity_Id);
8037 -- Create the body of procedure _Postconditions which handles various
8038 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
8039 -- of statements to be checked on exit. Parameter Result is the entity
8040 -- of parameter _Result when Subp_Id denotes a function.
8042 function Build_Pragma_Check_Equivalent
8043 (Prag : Node_Id;
8044 Subp_Id : Entity_Id := Empty;
8045 Inher_Id : Entity_Id := Empty) return Node_Id;
8046 -- Transform a [refined] pre- or postcondition denoted by Prag into an
8047 -- equivalent pragma Check. When the pre- or postcondition is inherited,
8048 -- the routine corrects the references of all formals of Inher_Id to
8049 -- point to the formals of Subp_Id.
8051 procedure Collect_Body_Postconditions (Stmts : in out List_Id);
8052 -- Process all postconditions found in the declarations of the body. The
8053 -- routine appends the pragma Check equivalents to list Stmts.
8055 procedure Collect_Spec_Postconditions
8056 (Subp_Id : Entity_Id;
8057 Stmts : in out List_Id);
8058 -- Process all [inherited] postconditions of subprogram spec Subp_Id.
8059 -- The routine appends the pragma Check equivalents to list Stmts.
8061 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id);
8062 -- Process all [inherited] preconditions of subprogram spec Subp_Id. The
8063 -- routine prepends the pragma Check equivalents to the declarations of
8064 -- the body.
8066 procedure Prepend_To_Declarations (Item : Node_Id);
8067 -- Prepend a single item to the declarations of the subprogram body
8069 procedure Process_Contract_Cases
8070 (Subp_Id : Entity_Id;
8071 Stmts : in out List_Id);
8072 -- Process pragma Contract_Cases of subprogram spec Subp_Id. The routine
8073 -- appends the expanded code to list Stmts.
8075 ----------------------------------------
8076 -- Add_Invariant_And_Predicate_Checks --
8077 ----------------------------------------
8079 procedure Add_Invariant_And_Predicate_Checks
8080 (Subp_Id : Entity_Id;
8081 Stmts : in out List_Id;
8082 Result : out Node_Id)
8084 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
8085 -- Id denotes the return value of a function or a formal parameter.
8086 -- Add an invariant check if the type of Id is access to a type with
8087 -- invariants. The routine appends the generated code to Stmts.
8089 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
8090 -- Determine whether type Typ can benefit from invariant checks. To
8091 -- qualify, the type must have a non-null invariant procedure and
8092 -- subprogram Subp_Id must appear visible from the point of view of
8093 -- the type.
8095 function Predicate_Checks_OK (Typ : Entity_Id) return Boolean;
8096 -- Determine whether type Typ can benefit from predicate checks. To
8097 -- qualify, the type must have at least one checked predicate.
8099 ---------------------------------
8100 -- Add_Invariant_Access_Checks --
8101 ---------------------------------
8103 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
8104 Loc : constant Source_Ptr := Sloc (N);
8105 Ref : Node_Id;
8106 Typ : Entity_Id;
8108 begin
8109 Typ := Etype (Id);
8111 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
8112 Typ := Designated_Type (Typ);
8114 if Invariant_Checks_OK (Typ) then
8115 Ref :=
8116 Make_Explicit_Dereference (Loc,
8117 Prefix => New_Occurrence_Of (Id, Loc));
8118 Set_Etype (Ref, Typ);
8120 -- Generate:
8121 -- if <Id> /= null then
8122 -- <invariant_call (<Ref>)>
8123 -- end if;
8125 Append_Enabled_Item
8126 (Item =>
8127 Make_If_Statement (Loc,
8128 Condition =>
8129 Make_Op_Ne (Loc,
8130 Left_Opnd => New_Occurrence_Of (Id, Loc),
8131 Right_Opnd => Make_Null (Loc)),
8132 Then_Statements => New_List (
8133 Make_Invariant_Call (Ref))),
8134 List => Stmts);
8135 end if;
8136 end if;
8137 end Add_Invariant_Access_Checks;
8139 -------------------------
8140 -- Invariant_Checks_OK --
8141 -------------------------
8143 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
8144 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
8145 -- Determine whether the body of procedure Proc_Id contains a sole
8146 -- null statement, possibly followed by an optional return.
8148 function Has_Public_Visibility_Of_Subprogram return Boolean;
8149 -- Determine whether type Typ has public visibility of subprogram
8150 -- Subp_Id.
8152 -------------------
8153 -- Has_Null_Body --
8154 -------------------
8156 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
8157 Body_Id : Entity_Id;
8158 Decl : Node_Id;
8159 Spec : Node_Id;
8160 Stmt1 : Node_Id;
8161 Stmt2 : Node_Id;
8163 begin
8164 Spec := Parent (Proc_Id);
8165 Decl := Parent (Spec);
8167 -- Retrieve the entity of the invariant procedure body
8169 if Nkind (Spec) = N_Procedure_Specification
8170 and then Nkind (Decl) = N_Subprogram_Declaration
8171 then
8172 Body_Id := Corresponding_Body (Decl);
8174 -- The body acts as a spec
8176 else
8177 Body_Id := Proc_Id;
8178 end if;
8180 -- The body will be generated later
8182 if No (Body_Id) then
8183 return False;
8184 end if;
8186 Spec := Parent (Body_Id);
8187 Decl := Parent (Spec);
8189 pragma Assert
8190 (Nkind (Spec) = N_Procedure_Specification
8191 and then Nkind (Decl) = N_Subprogram_Body);
8193 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
8195 -- Look for a null statement followed by an optional return
8196 -- statement.
8198 if Nkind (Stmt1) = N_Null_Statement then
8199 Stmt2 := Next (Stmt1);
8201 if Present (Stmt2) then
8202 return Nkind (Stmt2) = N_Simple_Return_Statement;
8203 else
8204 return True;
8205 end if;
8206 end if;
8208 return False;
8209 end Has_Null_Body;
8211 -----------------------------------------
8212 -- Has_Public_Visibility_Of_Subprogram --
8213 -----------------------------------------
8215 function Has_Public_Visibility_Of_Subprogram return Boolean is
8216 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
8218 begin
8219 -- An Initialization procedure must be considered visible even
8220 -- though it is internally generated.
8222 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
8223 return True;
8225 elsif Ekind (Scope (Typ)) /= E_Package then
8226 return False;
8228 -- Internally generated code is never publicly visible except
8229 -- for a subprogram that is the implementation of an expression
8230 -- function. In that case the visibility is determined by the
8231 -- last check.
8233 elsif not Comes_From_Source (Subp_Decl)
8234 and then
8235 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
8236 or else not
8237 Comes_From_Source (Defining_Entity (Subp_Decl)))
8238 then
8239 return False;
8241 -- Determine whether the subprogram is declared in the visible
8242 -- declarations of the package containing the type.
8244 else
8245 return List_Containing (Subp_Decl) =
8246 Visible_Declarations
8247 (Specification (Unit_Declaration_Node (Scope (Typ))));
8248 end if;
8249 end Has_Public_Visibility_Of_Subprogram;
8251 -- Start of processing for Invariant_Checks_OK
8253 begin
8254 return
8255 Has_Invariants (Typ)
8256 and then Present (Invariant_Procedure (Typ))
8257 and then not Has_Null_Body (Invariant_Procedure (Typ))
8258 and then Has_Public_Visibility_Of_Subprogram;
8259 end Invariant_Checks_OK;
8261 -------------------------
8262 -- Predicate_Checks_OK --
8263 -------------------------
8265 function Predicate_Checks_OK (Typ : Entity_Id) return Boolean is
8266 function Has_Checked_Predicate return Boolean;
8267 -- Determine whether type Typ has or inherits at least one
8268 -- predicate aspect or pragma, for which the applicable policy is
8269 -- Checked.
8271 ---------------------------
8272 -- Has_Checked_Predicate --
8273 ---------------------------
8275 function Has_Checked_Predicate return Boolean is
8276 Anc : Entity_Id;
8277 Pred : Node_Id;
8279 begin
8280 -- Climb the ancestor type chain staring from the input. This
8281 -- is done because the input type may lack aspect/pragma
8282 -- predicate and simply inherit those from its ancestor.
8284 -- Note that predicate pragmas correspond to all three cases
8285 -- of predicate aspects (Predicate, Dynamic_Predicate, and
8286 -- Static_Predicate), so this routine checks for all three
8287 -- cases.
8289 Anc := Typ;
8290 while Present (Anc) loop
8291 Pred := Get_Pragma (Anc, Pragma_Predicate);
8293 if Present (Pred) and then not Is_Ignored (Pred) then
8294 return True;
8295 end if;
8297 Anc := Nearest_Ancestor (Anc);
8298 end loop;
8300 return False;
8301 end Has_Checked_Predicate;
8303 -- Start of processing for Predicate_Checks_OK
8305 begin
8306 return
8307 Has_Predicates (Typ)
8308 and then Present (Predicate_Function (Typ))
8309 and then Has_Checked_Predicate;
8310 end Predicate_Checks_OK;
8312 -- Local variables
8314 Loc : constant Source_Ptr := Sloc (N);
8315 Formal : Entity_Id;
8316 Typ : Entity_Id;
8318 -- Start of processing for Add_Invariant_And_Predicate_Checks
8320 begin
8321 Result := Empty;
8323 -- Do not generate any checks if no code is being generated
8325 if not Expander_Active then
8326 return;
8327 end if;
8329 -- Process the result of a function
8331 if Ekind_In (Subp_Id, E_Function, E_Generic_Function) then
8332 Typ := Etype (Subp_Id);
8334 -- Generate _Result which is used in procedure _Postconditions to
8335 -- verify the return value.
8337 Result := Make_Defining_Identifier (Loc, Name_uResult);
8338 Set_Etype (Result, Typ);
8340 -- Add an invariant check when the return type has invariants and
8341 -- the related function is visible to the outside.
8343 if Invariant_Checks_OK (Typ) then
8344 Append_Enabled_Item
8345 (Item =>
8346 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
8347 List => Stmts);
8348 end if;
8350 -- Add an invariant check when the return type is an access to a
8351 -- type with invariants.
8353 Add_Invariant_Access_Checks (Result);
8354 end if;
8356 -- Add invariant and predicates for all formals that qualify
8358 Formal := First_Formal (Subp_Id);
8359 while Present (Formal) loop
8360 Typ := Etype (Formal);
8362 if Ekind (Formal) /= E_In_Parameter
8363 or else Is_Access_Type (Typ)
8364 then
8365 if Invariant_Checks_OK (Typ) then
8366 Append_Enabled_Item
8367 (Item =>
8368 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
8369 List => Stmts);
8370 end if;
8372 Add_Invariant_Access_Checks (Formal);
8374 if Predicate_Checks_OK (Typ) then
8375 Append_Enabled_Item
8376 (Item =>
8377 Make_Predicate_Check
8378 (Typ, New_Occurrence_Of (Formal, Loc)),
8379 List => Stmts);
8380 end if;
8381 end if;
8383 Next_Formal (Formal);
8384 end loop;
8385 end Add_Invariant_And_Predicate_Checks;
8387 -------------------------
8388 -- Append_Enabled_Item --
8389 -------------------------
8391 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
8392 begin
8393 -- Do not chain ignored or disabled pragmas
8395 if Nkind (Item) = N_Pragma
8396 and then (Is_Ignored (Item) or else Is_Disabled (Item))
8397 then
8398 null;
8400 -- Otherwise, add the item
8402 else
8403 if No (List) then
8404 List := New_List;
8405 end if;
8407 -- If the pragma is a conjunct in a composite postcondition, it
8408 -- has been processed in reverse order. In the postcondition body
8409 -- if must appear before the others.
8411 if Nkind (Item) = N_Pragma
8412 and then From_Aspect_Specification (Item)
8413 and then Split_PPC (Item)
8414 then
8415 Prepend (Item, List);
8416 else
8417 Append (Item, List);
8418 end if;
8419 end if;
8420 end Append_Enabled_Item;
8422 ------------------------------------
8423 -- Build_Postconditions_Procedure --
8424 ------------------------------------
8426 procedure Build_Postconditions_Procedure
8427 (Subp_Id : Entity_Id;
8428 Stmts : List_Id;
8429 Result : Entity_Id)
8431 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
8432 -- Insert node Stmt before the first source declaration of the
8433 -- related subprogram's body. If no such declaration exists, Stmt
8434 -- becomes the last declaration.
8436 --------------------------------------------
8437 -- Insert_Before_First_Source_Declaration --
8438 --------------------------------------------
8440 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
8441 Decls : constant List_Id := Declarations (N);
8442 Decl : Node_Id;
8444 begin
8445 -- Inspect the declarations of the related subprogram body looking
8446 -- for the first source declaration.
8448 if Present (Decls) then
8449 Decl := First (Decls);
8450 while Present (Decl) loop
8451 if Comes_From_Source (Decl) then
8452 Insert_Before (Decl, Stmt);
8453 return;
8454 end if;
8456 Next (Decl);
8457 end loop;
8459 -- If we get there, then the subprogram body lacks any source
8460 -- declarations. The body of _Postconditions now acts as the
8461 -- last declaration.
8463 Append (Stmt, Decls);
8465 -- Ensure that the body has a declaration list
8467 else
8468 Set_Declarations (N, New_List (Stmt));
8469 end if;
8470 end Insert_Before_First_Source_Declaration;
8472 -- Local variables
8474 Loc : constant Source_Ptr := Sloc (N);
8475 Params : List_Id := No_List;
8476 Proc_Id : Entity_Id;
8478 -- Start of processing for Build_Postconditions_Procedure
8480 begin
8481 -- Do not create the routine if no code is being generated
8483 if not Expander_Active then
8484 return;
8486 -- Nothing to do if there are no actions to check on exit
8488 elsif No (Stmts) then
8489 return;
8490 end if;
8492 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
8494 -- The related subprogram is a function, create the specification of
8495 -- parameter _Result.
8497 if Present (Result) then
8498 Params := New_List (
8499 Make_Parameter_Specification (Loc,
8500 Defining_Identifier => Result,
8501 Parameter_Type =>
8502 New_Occurrence_Of (Etype (Result), Loc)));
8503 end if;
8505 -- Insert _Postconditions before the first source declaration of the
8506 -- body. This ensures that the body will not cause any premature
8507 -- freezing as it may mention types:
8509 -- procedure Proc (Obj : Array_Typ) is
8510 -- procedure _postconditions is
8511 -- begin
8512 -- ... Obj ...
8513 -- end _postconditions;
8515 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
8516 -- begin
8518 -- In the example above, Obj is of type T but the incorrect placement
8519 -- of _Postconditions will cause a crash in gigi due to an out of
8520 -- order reference. The body of _Postconditions must be placed after
8521 -- the declaration of Temp to preserve correct visibility.
8523 Insert_Before_First_Source_Declaration (
8524 Make_Subprogram_Body (Loc,
8525 Specification =>
8526 Make_Procedure_Specification (Loc,
8527 Defining_Unit_Name => Proc_Id,
8528 Parameter_Specifications => Params),
8530 Declarations => Empty_List,
8531 Handled_Statement_Sequence =>
8532 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8534 -- Set the attributes of the related subprogram to capture the
8535 -- generated procedure.
8537 if Ekind_In (Subp_Id, E_Generic_Procedure, E_Procedure) then
8538 Set_Postcondition_Proc (Subp_Id, Proc_Id);
8539 end if;
8541 Set_Has_Postconditions (Subp_Id);
8542 end Build_Postconditions_Procedure;
8544 -----------------------------------
8545 -- Build_Pragma_Check_Equivalent --
8546 -----------------------------------
8548 function Build_Pragma_Check_Equivalent
8549 (Prag : Node_Id;
8550 Subp_Id : Entity_Id := Empty;
8551 Inher_Id : Entity_Id := Empty) return Node_Id
8553 Loc : constant Source_Ptr := Sloc (Prag);
8554 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
8555 Check_Prag : Node_Id;
8556 Formals_Map : Elist_Id;
8557 Inher_Formal : Entity_Id;
8558 Msg_Arg : Node_Id;
8559 Nam : Name_Id;
8560 Subp_Formal : Entity_Id;
8562 begin
8563 Formals_Map := No_Elist;
8565 -- When the pre- or postcondition is inherited, map the formals of
8566 -- the inherited subprogram to those of the current subprogram.
8568 if Present (Inher_Id) then
8569 pragma Assert (Present (Subp_Id));
8571 Formals_Map := New_Elmt_List;
8573 -- Create a relation <inherited formal> => <subprogram formal>
8575 Inher_Formal := First_Formal (Inher_Id);
8576 Subp_Formal := First_Formal (Subp_Id);
8577 while Present (Inher_Formal) and then Present (Subp_Formal) loop
8578 Append_Elmt (Inher_Formal, Formals_Map);
8579 Append_Elmt (Subp_Formal, Formals_Map);
8581 Next_Formal (Inher_Formal);
8582 Next_Formal (Subp_Formal);
8583 end loop;
8584 end if;
8586 -- Copy the original pragma while performing substitutions (if
8587 -- applicable).
8589 Check_Prag :=
8590 New_Copy_Tree
8591 (Source => Prag,
8592 Map => Formals_Map,
8593 New_Scope => Current_Scope);
8595 -- Mark the pragma as being internally generated and reset the
8596 -- Analyzed flag.
8598 Set_Comes_From_Source (Check_Prag, False);
8599 Set_Analyzed (Check_Prag, False);
8601 -- For a postcondition pragma within a generic, preserve the pragma
8602 -- for later expansion. This is also used when an error was detected,
8603 -- thus setting Expander_Active to False.
8605 if Prag_Nam = Name_Postcondition and then not Expander_Active then
8606 return Check_Prag;
8607 end if;
8609 if Present (Corresponding_Aspect (Prag)) then
8610 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
8611 else
8612 Nam := Prag_Nam;
8613 end if;
8615 -- Convert the copy into pragma Check by correcting the name and
8616 -- adding a check_kind argument.
8618 Set_Pragma_Identifier
8619 (Check_Prag, Make_Identifier (Loc, Name_Check));
8621 Prepend_To (Pragma_Argument_Associations (Check_Prag),
8622 Make_Pragma_Argument_Association (Loc,
8623 Expression => Make_Identifier (Loc, Nam)));
8625 -- Update the error message when the pragma is inherited
8627 if Present (Inher_Id) then
8628 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
8630 if Chars (Msg_Arg) = Name_Message then
8631 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
8633 -- Insert "inherited" to improve the error message
8635 if Name_Buffer (1 .. 8) = "failed p" then
8636 Insert_Str_In_Name_Buffer ("inherited ", 8);
8637 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
8638 end if;
8639 end if;
8640 end if;
8642 return Check_Prag;
8643 end Build_Pragma_Check_Equivalent;
8645 ---------------------------------
8646 -- Collect_Body_Postconditions --
8647 ---------------------------------
8649 procedure Collect_Body_Postconditions (Stmts : in out List_Id) is
8650 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id);
8651 -- Process all postconditions of the kind denoted by Post_Nam
8653 -----------------------------------------
8654 -- Collect_Body_Postconditions_Of_Kind --
8655 -----------------------------------------
8657 procedure Collect_Body_Postconditions_Of_Kind (Post_Nam : Name_Id) is
8658 procedure Collect_Body_Postconditions_In_Decls
8659 (First_Decl : Node_Id);
8660 -- Process all postconditions found in a declarative list starting
8661 -- with declaration First_Decl.
8663 ------------------------------------------
8664 -- Collect_Body_Postconditions_In_Decls --
8665 ------------------------------------------
8667 procedure Collect_Body_Postconditions_In_Decls
8668 (First_Decl : Node_Id)
8670 Check_Prag : Node_Id;
8671 Decl : Node_Id;
8673 begin
8674 -- Inspect the declarative list looking for a pragma that
8675 -- matches the desired name.
8677 Decl := First_Decl;
8678 while Present (Decl) loop
8680 -- Note that non-matching pragmas are skipped
8682 if Nkind (Decl) = N_Pragma then
8683 if Pragma_Name (Decl) = Post_Nam then
8684 if not Analyzed (Decl) then
8685 Analyze (Decl);
8686 end if;
8688 Check_Prag := Build_Pragma_Check_Equivalent (Decl);
8690 if Expander_Active then
8691 Append_Enabled_Item
8692 (Item => Check_Prag,
8693 List => Stmts);
8695 -- If analyzing a generic unit, save pragma for later
8697 else
8698 Prepend_To_Declarations (Check_Prag);
8699 end if;
8700 end if;
8702 -- Skip internally generated code
8704 elsif not Comes_From_Source (Decl) then
8705 null;
8707 -- Postcondition pragmas are usually grouped together. There
8708 -- is no need to inspect the whole declarative list.
8710 else
8711 exit;
8712 end if;
8714 Next (Decl);
8715 end loop;
8716 end Collect_Body_Postconditions_In_Decls;
8718 -- Local variables
8720 Unit_Decl : constant Node_Id := Parent (N);
8722 -- Start of processing for Collect_Body_Postconditions_Of_Kind
8724 begin
8725 pragma Assert (Nam_In (Post_Nam, Name_Postcondition,
8726 Name_Refined_Post));
8728 -- Inspect the declarations of the subprogram body looking for a
8729 -- pragma that matches the desired name.
8731 Collect_Body_Postconditions_In_Decls
8732 (First_Decl => First (Declarations (N)));
8734 -- The subprogram body being processed is actually the proper body
8735 -- of a stub with a corresponding spec. The subprogram stub may
8736 -- carry a postcondition pragma in which case it must be taken
8737 -- into account. The pragma appears after the stub.
8739 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
8740 Collect_Body_Postconditions_In_Decls
8741 (First_Decl => Next (Corresponding_Stub (Unit_Decl)));
8742 end if;
8743 end Collect_Body_Postconditions_Of_Kind;
8745 -- Start of processing for Collect_Body_Postconditions
8747 begin
8748 Collect_Body_Postconditions_Of_Kind (Name_Refined_Post);
8749 Collect_Body_Postconditions_Of_Kind (Name_Postcondition);
8750 end Collect_Body_Postconditions;
8752 ---------------------------------
8753 -- Collect_Spec_Postconditions --
8754 ---------------------------------
8756 procedure Collect_Spec_Postconditions
8757 (Subp_Id : Entity_Id;
8758 Stmts : in out List_Id)
8760 Inher_Subps : constant Subprogram_List :=
8761 Inherited_Subprograms (Subp_Id);
8762 Check_Prag : Node_Id;
8763 Prag : Node_Id;
8764 Inher_Subp_Id : Entity_Id;
8766 begin
8767 -- Process the contract of the spec
8769 Prag := Pre_Post_Conditions (Contract (Subp_Id));
8770 while Present (Prag) loop
8771 if Pragma_Name (Prag) = Name_Postcondition then
8772 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
8774 if Expander_Active then
8775 Append_Enabled_Item
8776 (Item => Check_Prag,
8777 List => Stmts);
8779 -- When analyzing a generic unit, save the pragma for later
8781 else
8782 Prepend_To_Declarations (Check_Prag);
8783 end if;
8784 end if;
8786 Prag := Next_Pragma (Prag);
8787 end loop;
8789 -- Process the contracts of all inherited subprograms, looking for
8790 -- class-wide postconditions.
8792 for Index in Inher_Subps'Range loop
8793 Inher_Subp_Id := Inher_Subps (Index);
8795 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
8796 while Present (Prag) loop
8797 if Pragma_Name (Prag) = Name_Postcondition
8798 and then Class_Present (Prag)
8799 then
8800 Check_Prag :=
8801 Build_Pragma_Check_Equivalent
8802 (Prag => Prag,
8803 Subp_Id => Subp_Id,
8804 Inher_Id => Inher_Subp_Id);
8806 if Expander_Active then
8807 Append_Enabled_Item
8808 (Item => Check_Prag,
8809 List => Stmts);
8811 -- When analyzing a generic unit, save the pragma for later
8813 else
8814 Prepend_To_Declarations (Check_Prag);
8815 end if;
8816 end if;
8818 Prag := Next_Pragma (Prag);
8819 end loop;
8820 end loop;
8821 end Collect_Spec_Postconditions;
8823 --------------------------------
8824 -- Collect_Spec_Preconditions --
8825 --------------------------------
8827 procedure Collect_Spec_Preconditions (Subp_Id : Entity_Id) is
8828 Class_Pre : Node_Id := Empty;
8829 -- The sole class-wide precondition pragma that applies to the
8830 -- subprogram.
8832 procedure Add_Or_Save_Precondition (Prag : Node_Id);
8833 -- Save a class-wide precondition or add a regulat precondition to
8834 -- the declarative list of the body.
8836 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
8837 -- Merge two class-wide preconditions by "or else"-ing them. The
8838 -- changes are accumulated in parameter Into. Update the error
8839 -- message of Into.
8841 ------------------------------
8842 -- Add_Or_Save_Precondition --
8843 ------------------------------
8845 procedure Add_Or_Save_Precondition (Prag : Node_Id) is
8846 Check_Prag : Node_Id;
8848 begin
8849 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
8851 -- Save the sole class-wide precondition (if any) for the next
8852 -- step where it will be merged with inherited preconditions.
8854 if Class_Present (Prag) then
8855 pragma Assert (No (Class_Pre));
8856 Class_Pre := Check_Prag;
8858 -- Accumulate the corresponding Check pragmas to the top of the
8859 -- declarations. Prepending the items ensures that they will be
8860 -- evaluated in their original order.
8862 else
8863 Prepend_To_Declarations (Check_Prag);
8864 end if;
8865 end Add_Or_Save_Precondition;
8867 -------------------------
8868 -- Merge_Preconditions --
8869 -------------------------
8871 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
8872 function Expression_Arg (Prag : Node_Id) return Node_Id;
8873 -- Return the boolean expression argument of a precondition while
8874 -- updating its parenteses count for the subsequent merge.
8876 function Message_Arg (Prag : Node_Id) return Node_Id;
8877 -- Return the message argument of a precondition
8879 --------------------
8880 -- Expression_Arg --
8881 --------------------
8883 function Expression_Arg (Prag : Node_Id) return Node_Id is
8884 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8885 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
8887 begin
8888 if Paren_Count (Arg) = 0 then
8889 Set_Paren_Count (Arg, 1);
8890 end if;
8892 return Arg;
8893 end Expression_Arg;
8895 -----------------
8896 -- Message_Arg --
8897 -----------------
8899 function Message_Arg (Prag : Node_Id) return Node_Id is
8900 Args : constant List_Id := Pragma_Argument_Associations (Prag);
8901 begin
8902 return Get_Pragma_Arg (Last (Args));
8903 end Message_Arg;
8905 -- Local variables
8907 From_Expr : constant Node_Id := Expression_Arg (From);
8908 From_Msg : constant Node_Id := Message_Arg (From);
8909 Into_Expr : constant Node_Id := Expression_Arg (Into);
8910 Into_Msg : constant Node_Id := Message_Arg (Into);
8911 Loc : constant Source_Ptr := Sloc (Into);
8913 -- Start of processing for Merge_Preconditions
8915 begin
8916 -- Merge the two preconditions by "or else"-ing them
8918 Rewrite (Into_Expr,
8919 Make_Or_Else (Loc,
8920 Right_Opnd => Relocate_Node (Into_Expr),
8921 Left_Opnd => From_Expr));
8923 -- Merge the two error messages to produce a single message of the
8924 -- form:
8926 -- failed precondition from ...
8927 -- also failed inherited precondition from ...
8929 if not Exception_Locations_Suppressed then
8930 Start_String (Strval (Into_Msg));
8931 Store_String_Char (ASCII.LF);
8932 Store_String_Chars (" also ");
8933 Store_String_Chars (Strval (From_Msg));
8935 Set_Strval (Into_Msg, End_String);
8936 end if;
8937 end Merge_Preconditions;
8939 -- Local variables
8941 Inher_Subps : constant Subprogram_List :=
8942 Inherited_Subprograms (Subp_Id);
8943 Subp_Decl : constant Node_Id := Parent (Parent (Subp_Id));
8944 Check_Prag : Node_Id;
8945 Decl : Node_Id;
8946 Inher_Subp_Id : Entity_Id;
8947 Prag : Node_Id;
8949 -- Start of processing for Collect_Spec_Preconditions
8951 begin
8952 -- Process the contract of the spec
8954 Prag := Pre_Post_Conditions (Contract (Subp_Id));
8955 while Present (Prag) loop
8956 if Pragma_Name (Prag) = Name_Precondition then
8957 Add_Or_Save_Precondition (Prag);
8958 end if;
8960 Prag := Next_Pragma (Prag);
8961 end loop;
8963 -- The subprogram declaration being processed is actually a body
8964 -- stub. The stub may carry a precondition pragma in which case it
8965 -- must be taken into account. The pragma appears after the stub.
8967 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
8969 -- Inspect the declarations following the body stub
8971 Decl := Next (Subp_Decl);
8972 while Present (Decl) loop
8974 -- Note that non-matching pragmas are skipped
8976 if Nkind (Decl) = N_Pragma then
8977 if Pragma_Name (Decl) = Name_Precondition then
8978 if not Analyzed (Decl) then
8979 Analyze (Decl);
8980 end if;
8982 Add_Or_Save_Precondition (Decl);
8983 end if;
8985 -- Skip internally generated code
8987 elsif not Comes_From_Source (Decl) then
8988 null;
8990 -- Preconditions are usually grouped together. There is no need
8991 -- to inspect the whole declarative list.
8993 else
8994 exit;
8995 end if;
8997 Next (Decl);
8998 end loop;
8999 end if;
9001 -- Process the contracts of all inherited subprograms, looking for
9002 -- class-wide preconditions.
9004 for Index in Inher_Subps'Range loop
9005 Inher_Subp_Id := Inher_Subps (Index);
9007 Prag := Pre_Post_Conditions (Contract (Inher_Subp_Id));
9008 while Present (Prag) loop
9009 if Pragma_Name (Prag) = Name_Precondition
9010 and then Class_Present (Prag)
9011 then
9012 Check_Prag :=
9013 Build_Pragma_Check_Equivalent
9014 (Prag => Prag,
9015 Subp_Id => Subp_Id,
9016 Inher_Id => Inher_Subp_Id);
9018 -- The spec or an inherited subprogram already yielded a
9019 -- class-wide precondition. Merge the existing precondition
9020 -- with the current one using "or else".
9022 if Present (Class_Pre) then
9023 Merge_Preconditions (Check_Prag, Class_Pre);
9024 else
9025 Class_Pre := Check_Prag;
9026 end if;
9027 end if;
9029 Prag := Next_Pragma (Prag);
9030 end loop;
9031 end loop;
9033 -- Add the merged class-wide preconditions (if any)
9035 if Present (Class_Pre) then
9036 Prepend_To_Declarations (Class_Pre);
9037 end if;
9038 end Collect_Spec_Preconditions;
9040 -----------------------------
9041 -- Prepend_To_Declarations --
9042 -----------------------------
9044 procedure Prepend_To_Declarations (Item : Node_Id) is
9045 Decls : List_Id := Declarations (N);
9047 begin
9048 -- Ensure that the body has a declarative list
9050 if No (Decls) then
9051 Decls := New_List;
9052 Set_Declarations (N, Decls);
9053 end if;
9055 Prepend_To (Decls, Item);
9056 end Prepend_To_Declarations;
9058 ----------------------------
9059 -- Process_Contract_Cases --
9060 ----------------------------
9062 procedure Process_Contract_Cases
9063 (Subp_Id : Entity_Id;
9064 Stmts : in out List_Id)
9066 Prag : Node_Id;
9068 begin
9069 -- Do not build the Contract_Cases circuitry if no code is being
9070 -- generated.
9072 if not Expander_Active then
9073 return;
9074 end if;
9076 Prag := Contract_Test_Cases (Contract (Subp_Id));
9077 while Present (Prag) loop
9078 if Pragma_Name (Prag) = Name_Contract_Cases then
9079 Expand_Contract_Cases
9080 (CCs => Prag,
9081 Subp_Id => Subp_Id,
9082 Decls => Declarations (N),
9083 Stmts => Stmts);
9084 end if;
9086 Prag := Next_Pragma (Prag);
9087 end loop;
9088 end Process_Contract_Cases;
9090 -- Local variables
9092 Post_Stmts : List_Id := No_List;
9093 Result : Entity_Id;
9094 Subp_Id : Entity_Id;
9096 -- Start of processing for Expand_Subprogram_Contract
9098 begin
9099 if Present (Spec_Id) then
9100 Subp_Id := Spec_Id;
9101 else
9102 Subp_Id := Body_Id;
9103 end if;
9105 -- Do not process a predicate function as its body will end up with a
9106 -- recursive call to itself and blow up the stack.
9108 if Ekind (Subp_Id) = E_Function
9109 and then Is_Predicate_Function (Subp_Id)
9110 then
9111 return;
9113 -- Do not process TSS subprograms
9115 elsif Get_TSS_Name (Subp_Id) /= TSS_Null then
9116 return;
9117 end if;
9119 -- The expansion of a subprogram contract involves the relocation of
9120 -- various contract assertions to the declarations of the body in a
9121 -- particular order. The order is as follows:
9123 -- function Example (...) return ... is
9124 -- procedure _Postconditions (...) is
9125 -- begin
9126 -- <refined postconditions from body>
9127 -- <postconditions from body>
9128 -- <postconditions from spec>
9129 -- <inherited postconditions>
9130 -- <contract case consequences>
9131 -- <invariant check of function result (if applicable)>
9132 -- <invariant and predicate checks of parameters>
9133 -- end _Postconditions;
9135 -- <inherited preconditions>
9136 -- <preconditions from spec>
9137 -- <preconditions from body>
9138 -- <refined preconditions from body>
9139 -- <contract case conditions>
9141 -- <source declarations>
9142 -- begin
9143 -- <source statements>
9145 -- _Preconditions (Result);
9146 -- return Result;
9147 -- end Example;
9149 -- Routine _Postconditions holds all contract assertions that must be
9150 -- verified on exit from the related routine.
9152 -- Collect all [inherited] preconditions from the spec, transform them
9153 -- into Check pragmas and add them to the declarations of the body in
9154 -- the order outlined above.
9156 if Present (Spec_Id) then
9157 Collect_Spec_Preconditions (Spec_Id);
9158 end if;
9160 -- Transform all [refined] postconditions of the body into Check
9161 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9163 Collect_Body_Postconditions (Post_Stmts);
9165 -- Transform all [inherited] postconditions from the spec into Check
9166 -- pragmas. The resulting pragmas are accumulated in list Post_Stmts.
9168 if Present (Spec_Id) then
9169 Collect_Spec_Postconditions (Spec_Id, Post_Stmts);
9171 -- Transform pragma Contract_Cases from the spec into its circuitry
9173 Process_Contract_Cases (Spec_Id, Post_Stmts);
9174 end if;
9176 -- Apply invariant and predicate checks on the result of a function (if
9177 -- applicable) and all formals. The resulting checks are accumulated in
9178 -- list Post_Stmts.
9180 Add_Invariant_And_Predicate_Checks (Subp_Id, Post_Stmts, Result);
9182 -- Construct procedure _Postconditions
9184 Build_Postconditions_Procedure (Subp_Id, Post_Stmts, Result);
9185 end Expand_Subprogram_Contract;
9187 --------------------------------
9188 -- Is_Build_In_Place_Function --
9189 --------------------------------
9191 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
9192 begin
9193 -- This function is called from Expand_Subtype_From_Expr during
9194 -- semantic analysis, even when expansion is off. In those cases
9195 -- the build_in_place expansion will not take place.
9197 if not Expander_Active then
9198 return False;
9199 end if;
9201 -- For now we test whether E denotes a function or access-to-function
9202 -- type whose result subtype is inherently limited. Later this test
9203 -- may be revised to allow composite nonlimited types. Functions with
9204 -- a foreign convention or whose result type has a foreign convention
9205 -- never qualify.
9207 if Ekind_In (E, E_Function, E_Generic_Function)
9208 or else (Ekind (E) = E_Subprogram_Type
9209 and then Etype (E) /= Standard_Void_Type)
9210 then
9211 -- Note: If the function has a foreign convention, it cannot build
9212 -- its result in place, so you're on your own. On the other hand,
9213 -- if only the return type has a foreign convention, its layout is
9214 -- intended to be compatible with the other language, but the build-
9215 -- in place machinery can ensure that the object is not copied.
9217 if Has_Foreign_Convention (E) then
9218 return False;
9220 -- In Ada 2005 all functions with an inherently limited return type
9221 -- must be handled using a build-in-place profile, including the case
9222 -- of a function with a limited interface result, where the function
9223 -- may return objects of nonlimited descendants.
9225 else
9226 return Is_Limited_View (Etype (E))
9227 and then Ada_Version >= Ada_2005
9228 and then not Debug_Flag_Dot_L;
9229 end if;
9231 else
9232 return False;
9233 end if;
9234 end Is_Build_In_Place_Function;
9236 -------------------------------------
9237 -- Is_Build_In_Place_Function_Call --
9238 -------------------------------------
9240 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
9241 Exp_Node : Node_Id := N;
9242 Function_Id : Entity_Id;
9244 begin
9245 -- Return False if the expander is currently inactive, since awareness
9246 -- of build-in-place treatment is only relevant during expansion. Note
9247 -- that Is_Build_In_Place_Function, which is called as part of this
9248 -- function, is also conditioned this way, but we need to check here as
9249 -- well to avoid blowing up on processing protected calls when expansion
9250 -- is disabled (such as with -gnatc) since those would trip over the
9251 -- raise of Program_Error below.
9253 -- In SPARK mode, build-in-place calls are not expanded, so that we
9254 -- may end up with a call that is neither resolved to an entity, nor
9255 -- an indirect call.
9257 if not Expander_Active then
9258 return False;
9259 end if;
9261 -- Step past qualification or unchecked conversion (the latter can occur
9262 -- in cases of calls to 'Input).
9264 if Nkind_In (Exp_Node, N_Qualified_Expression,
9265 N_Unchecked_Type_Conversion)
9266 then
9267 Exp_Node := Expression (N);
9268 end if;
9270 if Nkind (Exp_Node) /= N_Function_Call then
9271 return False;
9273 else
9274 if Is_Entity_Name (Name (Exp_Node)) then
9275 Function_Id := Entity (Name (Exp_Node));
9277 -- In the case of an explicitly dereferenced call, use the subprogram
9278 -- type generated for the dereference.
9280 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
9281 Function_Id := Etype (Name (Exp_Node));
9283 -- This may be a call to a protected function.
9285 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
9286 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
9288 else
9289 raise Program_Error;
9290 end if;
9292 return Is_Build_In_Place_Function (Function_Id);
9293 end if;
9294 end Is_Build_In_Place_Function_Call;
9296 -----------------------
9297 -- Freeze_Subprogram --
9298 -----------------------
9300 procedure Freeze_Subprogram (N : Node_Id) is
9301 Loc : constant Source_Ptr := Sloc (N);
9303 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
9304 -- (Ada 2005): Register a predefined primitive in all the secondary
9305 -- dispatch tables of its primitive type.
9307 ----------------------------------
9308 -- Register_Predefined_DT_Entry --
9309 ----------------------------------
9311 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
9312 Iface_DT_Ptr : Elmt_Id;
9313 Tagged_Typ : Entity_Id;
9314 Thunk_Id : Entity_Id;
9315 Thunk_Code : Node_Id;
9317 begin
9318 Tagged_Typ := Find_Dispatching_Type (Prim);
9320 if No (Access_Disp_Table (Tagged_Typ))
9321 or else not Has_Interfaces (Tagged_Typ)
9322 or else not RTE_Available (RE_Interface_Tag)
9323 or else Restriction_Active (No_Dispatching_Calls)
9324 then
9325 return;
9326 end if;
9328 -- Skip the first two access-to-dispatch-table pointers since they
9329 -- leads to the primary dispatch table (predefined DT and user
9330 -- defined DT). We are only concerned with the secondary dispatch
9331 -- table pointers. Note that the access-to- dispatch-table pointer
9332 -- corresponds to the first implemented interface retrieved below.
9334 Iface_DT_Ptr :=
9335 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
9337 while Present (Iface_DT_Ptr)
9338 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
9339 loop
9340 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
9341 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
9343 if Present (Thunk_Code) then
9344 Insert_Actions_After (N, New_List (
9345 Thunk_Code,
9347 Build_Set_Predefined_Prim_Op_Address (Loc,
9348 Tag_Node =>
9349 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
9350 Position => DT_Position (Prim),
9351 Address_Node =>
9352 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
9353 Make_Attribute_Reference (Loc,
9354 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
9355 Attribute_Name => Name_Unrestricted_Access))),
9357 Build_Set_Predefined_Prim_Op_Address (Loc,
9358 Tag_Node =>
9359 New_Occurrence_Of
9360 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
9361 Loc),
9362 Position => DT_Position (Prim),
9363 Address_Node =>
9364 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
9365 Make_Attribute_Reference (Loc,
9366 Prefix => New_Occurrence_Of (Prim, Loc),
9367 Attribute_Name => Name_Unrestricted_Access)))));
9368 end if;
9370 -- Skip the tag of the predefined primitives dispatch table
9372 Next_Elmt (Iface_DT_Ptr);
9373 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
9375 -- Skip tag of the no-thunks dispatch table
9377 Next_Elmt (Iface_DT_Ptr);
9378 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
9380 -- Skip tag of predefined primitives no-thunks dispatch table
9382 Next_Elmt (Iface_DT_Ptr);
9383 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
9385 Next_Elmt (Iface_DT_Ptr);
9386 end loop;
9387 end Register_Predefined_DT_Entry;
9389 -- Local variables
9391 Subp : constant Entity_Id := Entity (N);
9393 -- Start of processing for Freeze_Subprogram
9395 begin
9396 -- We suppress the initialization of the dispatch table entry when
9397 -- VM_Target because the dispatching mechanism is handled internally
9398 -- by the VM.
9400 if Is_Dispatching_Operation (Subp)
9401 and then not Is_Abstract_Subprogram (Subp)
9402 and then Present (DTC_Entity (Subp))
9403 and then Present (Scope (DTC_Entity (Subp)))
9404 and then Tagged_Type_Expansion
9405 and then not Restriction_Active (No_Dispatching_Calls)
9406 and then RTE_Available (RE_Tag)
9407 then
9408 declare
9409 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
9411 begin
9412 -- Handle private overridden primitives
9414 if not Is_CPP_Class (Typ) then
9415 Check_Overriding_Operation (Subp);
9416 end if;
9418 -- We assume that imported CPP primitives correspond with objects
9419 -- whose constructor is in the CPP side; therefore we don't need
9420 -- to generate code to register them in the dispatch table.
9422 if Is_CPP_Class (Typ) then
9423 null;
9425 -- Handle CPP primitives found in derivations of CPP_Class types.
9426 -- These primitives must have been inherited from some parent, and
9427 -- there is no need to register them in the dispatch table because
9428 -- Build_Inherit_Prims takes care of initializing these slots.
9430 elsif Is_Imported (Subp)
9431 and then (Convention (Subp) = Convention_CPP
9432 or else Convention (Subp) = Convention_C)
9433 then
9434 null;
9436 -- Generate code to register the primitive in non statically
9437 -- allocated dispatch tables
9439 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
9441 -- When a primitive is frozen, enter its name in its dispatch
9442 -- table slot.
9444 if not Is_Interface (Typ)
9445 or else Present (Interface_Alias (Subp))
9446 then
9447 if Is_Predefined_Dispatching_Operation (Subp) then
9448 Register_Predefined_DT_Entry (Subp);
9449 end if;
9451 Insert_Actions_After (N,
9452 Register_Primitive (Loc, Prim => Subp));
9453 end if;
9454 end if;
9455 end;
9456 end if;
9458 -- Mark functions that return by reference. Note that it cannot be part
9459 -- of the normal semantic analysis of the spec since the underlying
9460 -- returned type may not be known yet (for private types).
9462 declare
9463 Typ : constant Entity_Id := Etype (Subp);
9464 Utyp : constant Entity_Id := Underlying_Type (Typ);
9465 begin
9466 if Is_Limited_View (Typ) then
9467 Set_Returns_By_Ref (Subp);
9468 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
9469 Set_Returns_By_Ref (Subp);
9470 end if;
9471 end;
9473 -- Wnen freezing a null procedure, analyze its delayed aspects now
9474 -- because we may not have reached the end of the declarative list when
9475 -- delayed aspects are normally analyzed. This ensures that dispatching
9476 -- calls are properly rewritten when the generated _Postcondition
9477 -- procedure is analyzed in the null procedure body.
9479 if Nkind (Parent (Subp)) = N_Procedure_Specification
9480 and then Null_Present (Parent (Subp))
9481 then
9482 Analyze_Subprogram_Contract (Subp);
9483 end if;
9484 end Freeze_Subprogram;
9486 -----------------------
9487 -- Is_Null_Procedure --
9488 -----------------------
9490 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
9491 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
9493 begin
9494 if Ekind (Subp) /= E_Procedure then
9495 return False;
9497 -- Check if this is a declared null procedure
9499 elsif Nkind (Decl) = N_Subprogram_Declaration then
9500 if not Null_Present (Specification (Decl)) then
9501 return False;
9503 elsif No (Body_To_Inline (Decl)) then
9504 return False;
9506 -- Check if the body contains only a null statement, followed by
9507 -- the return statement added during expansion.
9509 else
9510 declare
9511 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
9513 Stat : Node_Id;
9514 Stat2 : Node_Id;
9516 begin
9517 if Nkind (Orig_Bod) /= N_Subprogram_Body then
9518 return False;
9519 else
9520 -- We must skip SCIL nodes because they are currently
9521 -- implemented as special N_Null_Statement nodes.
9523 Stat :=
9524 First_Non_SCIL_Node
9525 (Statements (Handled_Statement_Sequence (Orig_Bod)));
9526 Stat2 := Next_Non_SCIL_Node (Stat);
9528 return
9529 Is_Empty_List (Declarations (Orig_Bod))
9530 and then Nkind (Stat) = N_Null_Statement
9531 and then
9532 (No (Stat2)
9533 or else
9534 (Nkind (Stat2) = N_Simple_Return_Statement
9535 and then No (Next (Stat2))));
9536 end if;
9537 end;
9538 end if;
9540 else
9541 return False;
9542 end if;
9543 end Is_Null_Procedure;
9545 -------------------------------------------
9546 -- Make_Build_In_Place_Call_In_Allocator --
9547 -------------------------------------------
9549 procedure Make_Build_In_Place_Call_In_Allocator
9550 (Allocator : Node_Id;
9551 Function_Call : Node_Id)
9553 Acc_Type : constant Entity_Id := Etype (Allocator);
9554 Loc : Source_Ptr;
9555 Func_Call : Node_Id := Function_Call;
9556 Function_Id : Entity_Id;
9557 Result_Subt : Entity_Id;
9558 New_Allocator : Node_Id;
9559 Return_Obj_Access : Entity_Id;
9561 begin
9562 -- Step past qualification or unchecked conversion (the latter can occur
9563 -- in cases of calls to 'Input).
9565 if Nkind_In (Func_Call,
9566 N_Qualified_Expression,
9567 N_Unchecked_Type_Conversion)
9568 then
9569 Func_Call := Expression (Func_Call);
9570 end if;
9572 -- If the call has already been processed to add build-in-place actuals
9573 -- then return. This should not normally occur in an allocator context,
9574 -- but we add the protection as a defensive measure.
9576 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9577 return;
9578 end if;
9580 -- Mark the call as processed as a build-in-place call
9582 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9584 Loc := Sloc (Function_Call);
9586 if Is_Entity_Name (Name (Func_Call)) then
9587 Function_Id := Entity (Name (Func_Call));
9589 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9590 Function_Id := Etype (Name (Func_Call));
9592 else
9593 raise Program_Error;
9594 end if;
9596 Result_Subt := Available_View (Etype (Function_Id));
9598 -- Check whether return type includes tasks. This may not have been done
9599 -- previously, if the type was a limited view.
9601 if Has_Task (Result_Subt) then
9602 Build_Activation_Chain_Entity (Allocator);
9603 end if;
9605 -- When the result subtype is constrained, the return object must be
9606 -- allocated on the caller side, and access to it is passed to the
9607 -- function.
9609 -- Here and in related routines, we must examine the full view of the
9610 -- type, because the view at the point of call may differ from that
9611 -- that in the function body, and the expansion mechanism depends on
9612 -- the characteristics of the full view.
9614 if Is_Constrained (Underlying_Type (Result_Subt)) then
9616 -- Replace the initialized allocator of form "new T'(Func (...))"
9617 -- with an uninitialized allocator of form "new T", where T is the
9618 -- result subtype of the called function. The call to the function
9619 -- is handled separately further below.
9621 New_Allocator :=
9622 Make_Allocator (Loc,
9623 Expression => New_Occurrence_Of (Result_Subt, Loc));
9624 Set_No_Initialization (New_Allocator);
9626 -- Copy attributes to new allocator. Note that the new allocator
9627 -- logically comes from source if the original one did, so copy the
9628 -- relevant flag. This ensures proper treatment of the restriction
9629 -- No_Implicit_Heap_Allocations in this case.
9631 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9632 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9633 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9635 Rewrite (Allocator, New_Allocator);
9637 -- Create a new access object and initialize it to the result of the
9638 -- new uninitialized allocator. Note: we do not use Allocator as the
9639 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9640 -- as this would create a sort of infinite "recursion".
9642 Return_Obj_Access := Make_Temporary (Loc, 'R');
9643 Set_Etype (Return_Obj_Access, Acc_Type);
9645 Insert_Action (Allocator,
9646 Make_Object_Declaration (Loc,
9647 Defining_Identifier => Return_Obj_Access,
9648 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9649 Expression => Relocate_Node (Allocator)));
9651 -- When the function has a controlling result, an allocation-form
9652 -- parameter must be passed indicating that the caller is allocating
9653 -- the result object. This is needed because such a function can be
9654 -- called as a dispatching operation and must be treated similarly
9655 -- to functions with unconstrained result subtypes.
9657 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9658 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9660 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9661 (Func_Call, Function_Id, Acc_Type);
9663 Add_Task_Actuals_To_Build_In_Place_Call
9664 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
9666 -- Add an implicit actual to the function call that provides access
9667 -- to the allocated object. An unchecked conversion to the (specific)
9668 -- result subtype of the function is inserted to handle cases where
9669 -- the access type of the allocator has a class-wide designated type.
9671 Add_Access_Actual_To_Build_In_Place_Call
9672 (Func_Call,
9673 Function_Id,
9674 Make_Unchecked_Type_Conversion (Loc,
9675 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9676 Expression =>
9677 Make_Explicit_Dereference (Loc,
9678 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc))));
9680 -- When the result subtype is unconstrained, the function itself must
9681 -- perform the allocation of the return object, so we pass parameters
9682 -- indicating that. We don't yet handle the case where the allocation
9683 -- must be done in a user-defined storage pool, which will require
9684 -- passing another actual or two to provide allocation/deallocation
9685 -- operations. ???
9687 else
9688 -- Case of a user-defined storage pool. Pass an allocation parameter
9689 -- indicating that the function should allocate its result in the
9690 -- pool, and pass the pool. Use 'Unrestricted_Access because the
9691 -- pool may not be aliased.
9693 if VM_Target = No_VM
9694 and then Present (Associated_Storage_Pool (Acc_Type))
9695 then
9696 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9697 (Func_Call, Function_Id, Alloc_Form => User_Storage_Pool,
9698 Pool_Actual =>
9699 Make_Attribute_Reference (Loc,
9700 Prefix =>
9701 New_Occurrence_Of
9702 (Associated_Storage_Pool (Acc_Type), Loc),
9703 Attribute_Name => Name_Unrestricted_Access));
9705 -- No user-defined pool; pass an allocation parameter indicating that
9706 -- the function should allocate its result on the heap.
9708 else
9709 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9710 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
9711 end if;
9713 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9714 (Func_Call, Function_Id, Acc_Type);
9716 Add_Task_Actuals_To_Build_In_Place_Call
9717 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
9719 -- The caller does not provide the return object in this case, so we
9720 -- have to pass null for the object access actual.
9722 Add_Access_Actual_To_Build_In_Place_Call
9723 (Func_Call, Function_Id, Return_Object => Empty);
9724 end if;
9726 -- If the build-in-place function call returns a controlled object,
9727 -- the finalization master will require a reference to routine
9728 -- Finalize_Address of the designated type. Setting this attribute
9729 -- is done in the same manner to expansion of allocators.
9731 if Needs_Finalization (Result_Subt) then
9733 -- Controlled types with supressed finalization do not need to
9734 -- associate the address of their Finalize_Address primitives with
9735 -- a master since they do not need a master to begin with.
9737 if Is_Library_Level_Entity (Acc_Type)
9738 and then Finalize_Storage_Only (Result_Subt)
9739 then
9740 null;
9742 -- Do not generate the call to Set_Finalize_Address in CodePeer mode
9743 -- because Finalize_Address is never built.
9745 elsif not CodePeer_Mode then
9746 Insert_Action (Allocator,
9747 Make_Set_Finalize_Address_Call (Loc,
9748 Typ => Etype (Function_Id),
9749 Ptr_Typ => Acc_Type));
9750 end if;
9751 end if;
9753 -- Finally, replace the allocator node with a reference to the result
9754 -- of the function call itself (which will effectively be an access
9755 -- to the object created by the allocator).
9757 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
9759 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9760 -- generate an implicit conversion to force displacement of the "this"
9761 -- pointer.
9763 if Is_Interface (Designated_Type (Acc_Type)) then
9764 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9765 end if;
9767 Analyze_And_Resolve (Allocator, Acc_Type);
9768 end Make_Build_In_Place_Call_In_Allocator;
9770 ---------------------------------------------------
9771 -- Make_Build_In_Place_Call_In_Anonymous_Context --
9772 ---------------------------------------------------
9774 procedure Make_Build_In_Place_Call_In_Anonymous_Context
9775 (Function_Call : Node_Id)
9777 Loc : Source_Ptr;
9778 Func_Call : Node_Id := Function_Call;
9779 Function_Id : Entity_Id;
9780 Result_Subt : Entity_Id;
9781 Return_Obj_Id : Entity_Id;
9782 Return_Obj_Decl : Entity_Id;
9784 begin
9785 -- Step past qualification or unchecked conversion (the latter can occur
9786 -- in cases of calls to 'Input).
9788 if Nkind_In (Func_Call, N_Qualified_Expression,
9789 N_Unchecked_Type_Conversion)
9790 then
9791 Func_Call := Expression (Func_Call);
9792 end if;
9794 -- If the call has already been processed to add build-in-place actuals
9795 -- then return. One place this can occur is for calls to build-in-place
9796 -- functions that occur within a call to a protected operation, where
9797 -- due to rewriting and expansion of the protected call there can be
9798 -- more than one call to Expand_Actuals for the same set of actuals.
9800 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9801 return;
9802 end if;
9804 -- Mark the call as processed as a build-in-place call
9806 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9808 Loc := Sloc (Function_Call);
9810 if Is_Entity_Name (Name (Func_Call)) then
9811 Function_Id := Entity (Name (Func_Call));
9813 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9814 Function_Id := Etype (Name (Func_Call));
9816 else
9817 raise Program_Error;
9818 end if;
9820 Result_Subt := Etype (Function_Id);
9822 -- If the build-in-place function returns a controlled object, then the
9823 -- object needs to be finalized immediately after the context. Since
9824 -- this case produces a transient scope, the servicing finalizer needs
9825 -- to name the returned object. Create a temporary which is initialized
9826 -- with the function call:
9828 -- Temp_Id : Func_Type := BIP_Func_Call;
9830 -- The initialization expression of the temporary will be rewritten by
9831 -- the expander using the appropriate mechanism in Make_Build_In_Place_
9832 -- Call_In_Object_Declaration.
9834 if Needs_Finalization (Result_Subt) then
9835 declare
9836 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
9837 Temp_Decl : Node_Id;
9839 begin
9840 -- Reset the guard on the function call since the following does
9841 -- not perform actual call expansion.
9843 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
9845 Temp_Decl :=
9846 Make_Object_Declaration (Loc,
9847 Defining_Identifier => Temp_Id,
9848 Object_Definition =>
9849 New_Occurrence_Of (Result_Subt, Loc),
9850 Expression =>
9851 New_Copy_Tree (Function_Call));
9853 Insert_Action (Function_Call, Temp_Decl);
9855 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
9856 Analyze (Function_Call);
9857 end;
9859 -- When the result subtype is constrained, an object of the subtype is
9860 -- declared and an access value designating it is passed as an actual.
9862 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
9864 -- Create a temporary object to hold the function result
9866 Return_Obj_Id := Make_Temporary (Loc, 'R');
9867 Set_Etype (Return_Obj_Id, Result_Subt);
9869 Return_Obj_Decl :=
9870 Make_Object_Declaration (Loc,
9871 Defining_Identifier => Return_Obj_Id,
9872 Aliased_Present => True,
9873 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
9875 Set_No_Initialization (Return_Obj_Decl);
9877 Insert_Action (Func_Call, Return_Obj_Decl);
9879 -- When the function has a controlling result, an allocation-form
9880 -- parameter must be passed indicating that the caller is allocating
9881 -- the result object. This is needed because such a function can be
9882 -- called as a dispatching operation and must be treated similarly
9883 -- to functions with unconstrained result subtypes.
9885 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9886 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9888 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9889 (Func_Call, Function_Id);
9891 Add_Task_Actuals_To_Build_In_Place_Call
9892 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9894 -- Add an implicit actual to the function call that provides access
9895 -- to the caller's return object.
9897 Add_Access_Actual_To_Build_In_Place_Call
9898 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
9900 -- When the result subtype is unconstrained, the function must allocate
9901 -- the return object in the secondary stack, so appropriate implicit
9902 -- parameters are added to the call to indicate that. A transient
9903 -- scope is established to ensure eventual cleanup of the result.
9905 else
9906 -- Pass an allocation parameter indicating that the function should
9907 -- allocate its result on the secondary stack.
9909 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9910 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9912 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9913 (Func_Call, Function_Id);
9915 Add_Task_Actuals_To_Build_In_Place_Call
9916 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9918 -- Pass a null value to the function since no return object is
9919 -- available on the caller side.
9921 Add_Access_Actual_To_Build_In_Place_Call
9922 (Func_Call, Function_Id, Empty);
9923 end if;
9924 end Make_Build_In_Place_Call_In_Anonymous_Context;
9926 --------------------------------------------
9927 -- Make_Build_In_Place_Call_In_Assignment --
9928 --------------------------------------------
9930 procedure Make_Build_In_Place_Call_In_Assignment
9931 (Assign : Node_Id;
9932 Function_Call : Node_Id)
9934 Lhs : constant Node_Id := Name (Assign);
9935 Func_Call : Node_Id := Function_Call;
9936 Func_Id : Entity_Id;
9937 Loc : Source_Ptr;
9938 Obj_Decl : Node_Id;
9939 Obj_Id : Entity_Id;
9940 Ptr_Typ : Entity_Id;
9941 Ptr_Typ_Decl : Node_Id;
9942 New_Expr : Node_Id;
9943 Result_Subt : Entity_Id;
9944 Target : Node_Id;
9946 begin
9947 -- Step past qualification or unchecked conversion (the latter can occur
9948 -- in cases of calls to 'Input).
9950 if Nkind_In (Func_Call, N_Qualified_Expression,
9951 N_Unchecked_Type_Conversion)
9952 then
9953 Func_Call := Expression (Func_Call);
9954 end if;
9956 -- If the call has already been processed to add build-in-place actuals
9957 -- then return. This should not normally occur in an assignment context,
9958 -- but we add the protection as a defensive measure.
9960 if Is_Expanded_Build_In_Place_Call (Func_Call) then
9961 return;
9962 end if;
9964 -- Mark the call as processed as a build-in-place call
9966 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
9968 Loc := Sloc (Function_Call);
9970 if Is_Entity_Name (Name (Func_Call)) then
9971 Func_Id := Entity (Name (Func_Call));
9973 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
9974 Func_Id := Etype (Name (Func_Call));
9976 else
9977 raise Program_Error;
9978 end if;
9980 Result_Subt := Etype (Func_Id);
9982 -- When the result subtype is unconstrained, an additional actual must
9983 -- be passed to indicate that the caller is providing the return object.
9984 -- This parameter must also be passed when the called function has a
9985 -- controlling result, because dispatching calls to the function needs
9986 -- to be treated effectively the same as calls to class-wide functions.
9988 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9989 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
9991 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9992 (Func_Call, Func_Id);
9994 Add_Task_Actuals_To_Build_In_Place_Call
9995 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
9997 -- Add an implicit actual to the function call that provides access to
9998 -- the caller's return object.
10000 Add_Access_Actual_To_Build_In_Place_Call
10001 (Func_Call,
10002 Func_Id,
10003 Make_Unchecked_Type_Conversion (Loc,
10004 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10005 Expression => Relocate_Node (Lhs)));
10007 -- Create an access type designating the function's result subtype
10009 Ptr_Typ := Make_Temporary (Loc, 'A');
10011 Ptr_Typ_Decl :=
10012 Make_Full_Type_Declaration (Loc,
10013 Defining_Identifier => Ptr_Typ,
10014 Type_Definition =>
10015 Make_Access_To_Object_Definition (Loc,
10016 All_Present => True,
10017 Subtype_Indication =>
10018 New_Occurrence_Of (Result_Subt, Loc)));
10019 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
10021 -- Finally, create an access object initialized to a reference to the
10022 -- function call. We know this access value is non-null, so mark the
10023 -- entity accordingly to suppress junk access checks.
10025 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
10027 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
10028 Set_Etype (Obj_Id, Ptr_Typ);
10029 Set_Is_Known_Non_Null (Obj_Id);
10031 Obj_Decl :=
10032 Make_Object_Declaration (Loc,
10033 Defining_Identifier => Obj_Id,
10034 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
10035 Expression => New_Expr);
10036 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
10038 Rewrite (Assign, Make_Null_Statement (Loc));
10040 -- Retrieve the target of the assignment
10042 if Nkind (Lhs) = N_Selected_Component then
10043 Target := Selector_Name (Lhs);
10044 elsif Nkind (Lhs) = N_Type_Conversion then
10045 Target := Expression (Lhs);
10046 else
10047 Target := Lhs;
10048 end if;
10050 -- If we are assigning to a return object or this is an expression of
10051 -- an extension aggregate, the target should either be an identifier
10052 -- or a simple expression. All other cases imply a different scenario.
10054 if Nkind (Target) in N_Has_Entity then
10055 Target := Entity (Target);
10056 else
10057 return;
10058 end if;
10059 end Make_Build_In_Place_Call_In_Assignment;
10061 ----------------------------------------------------
10062 -- Make_Build_In_Place_Call_In_Object_Declaration --
10063 ----------------------------------------------------
10065 procedure Make_Build_In_Place_Call_In_Object_Declaration
10066 (Object_Decl : Node_Id;
10067 Function_Call : Node_Id)
10069 Loc : Source_Ptr;
10070 Obj_Def_Id : constant Entity_Id :=
10071 Defining_Identifier (Object_Decl);
10072 Enclosing_Func : constant Entity_Id :=
10073 Enclosing_Subprogram (Obj_Def_Id);
10074 Call_Deref : Node_Id;
10075 Caller_Object : Node_Id;
10076 Def_Id : Entity_Id;
10077 Fmaster_Actual : Node_Id := Empty;
10078 Func_Call : Node_Id := Function_Call;
10079 Function_Id : Entity_Id;
10080 Pool_Actual : Node_Id;
10081 Ptr_Typ_Decl : Node_Id;
10082 Pass_Caller_Acc : Boolean := False;
10083 New_Expr : Node_Id;
10084 Ref_Type : Entity_Id;
10085 Res_Decl : Node_Id;
10086 Result_Subt : Entity_Id;
10088 begin
10089 -- Step past qualification or unchecked conversion (the latter can occur
10090 -- in cases of calls to 'Input).
10092 if Nkind_In (Func_Call, N_Qualified_Expression,
10093 N_Unchecked_Type_Conversion)
10094 then
10095 Func_Call := Expression (Func_Call);
10096 end if;
10098 -- If the call has already been processed to add build-in-place actuals
10099 -- then return. This should not normally occur in an object declaration,
10100 -- but we add the protection as a defensive measure.
10102 if Is_Expanded_Build_In_Place_Call (Func_Call) then
10103 return;
10104 end if;
10106 -- Mark the call as processed as a build-in-place call
10108 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
10110 Loc := Sloc (Function_Call);
10112 if Is_Entity_Name (Name (Func_Call)) then
10113 Function_Id := Entity (Name (Func_Call));
10115 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
10116 Function_Id := Etype (Name (Func_Call));
10118 else
10119 raise Program_Error;
10120 end if;
10122 Result_Subt := Etype (Function_Id);
10124 -- If the the object is a return object of an enclosing build-in-place
10125 -- function, then the implicit build-in-place parameters of the
10126 -- enclosing function are simply passed along to the called function.
10127 -- (Unfortunately, this won't cover the case of extension aggregates
10128 -- where the ancestor part is a build-in-place unconstrained function
10129 -- call that should be passed along the caller's parameters. Currently
10130 -- those get mishandled by reassigning the result of the call to the
10131 -- aggregate return object, when the call result should really be
10132 -- directly built in place in the aggregate and not in a temporary. ???)
10134 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
10135 Pass_Caller_Acc := True;
10137 -- When the enclosing function has a BIP_Alloc_Form formal then we
10138 -- pass it along to the callee (such as when the enclosing function
10139 -- has an unconstrained or tagged result type).
10141 if Needs_BIP_Alloc_Form (Enclosing_Func) then
10142 if VM_Target = No_VM and then
10143 RTE_Available (RE_Root_Storage_Pool_Ptr)
10144 then
10145 Pool_Actual :=
10146 New_Occurrence_Of (Build_In_Place_Formal
10147 (Enclosing_Func, BIP_Storage_Pool), Loc);
10149 -- The build-in-place pool formal is not built on .NET/JVM
10151 else
10152 Pool_Actual := Empty;
10153 end if;
10155 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10156 (Func_Call,
10157 Function_Id,
10158 Alloc_Form_Exp =>
10159 New_Occurrence_Of
10160 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
10161 Loc),
10162 Pool_Actual => Pool_Actual);
10164 -- Otherwise, if enclosing function has a constrained result subtype,
10165 -- then caller allocation will be used.
10167 else
10168 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10169 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
10170 end if;
10172 if Needs_BIP_Finalization_Master (Enclosing_Func) then
10173 Fmaster_Actual :=
10174 New_Occurrence_Of
10175 (Build_In_Place_Formal
10176 (Enclosing_Func, BIP_Finalization_Master), Loc);
10177 end if;
10179 -- Retrieve the BIPacc formal from the enclosing function and convert
10180 -- it to the access type of the callee's BIP_Object_Access formal.
10182 Caller_Object :=
10183 Make_Unchecked_Type_Conversion (Loc,
10184 Subtype_Mark =>
10185 New_Occurrence_Of
10186 (Etype
10187 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
10188 Loc),
10189 Expression =>
10190 New_Occurrence_Of
10191 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
10192 Loc));
10194 -- In the constrained case, add an implicit actual to the function call
10195 -- that provides access to the declared object. An unchecked conversion
10196 -- to the (specific) result type of the function is inserted to handle
10197 -- the case where the object is declared with a class-wide type.
10199 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
10200 Caller_Object :=
10201 Make_Unchecked_Type_Conversion (Loc,
10202 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10203 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
10205 -- When the function has a controlling result, an allocation-form
10206 -- parameter must be passed indicating that the caller is allocating
10207 -- the result object. This is needed because such a function can be
10208 -- called as a dispatching operation and must be treated similarly
10209 -- to functions with unconstrained result subtypes.
10211 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10212 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
10214 -- In other unconstrained cases, pass an indication to do the allocation
10215 -- on the secondary stack and set Caller_Object to Empty so that a null
10216 -- value will be passed for the caller's object address. A transient
10217 -- scope is established to ensure eventual cleanup of the result.
10219 else
10220 Add_Unconstrained_Actuals_To_Build_In_Place_Call
10221 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
10222 Caller_Object := Empty;
10224 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
10225 end if;
10227 -- Pass along any finalization master actual, which is needed in the
10228 -- case where the called function initializes a return object of an
10229 -- enclosing build-in-place function.
10231 Add_Finalization_Master_Actual_To_Build_In_Place_Call
10232 (Func_Call => Func_Call,
10233 Func_Id => Function_Id,
10234 Master_Exp => Fmaster_Actual);
10236 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
10237 and then Has_Task (Result_Subt)
10238 then
10239 -- Here we're passing along the master that was passed in to this
10240 -- function.
10242 Add_Task_Actuals_To_Build_In_Place_Call
10243 (Func_Call, Function_Id,
10244 Master_Actual =>
10245 New_Occurrence_Of (Build_In_Place_Formal
10246 (Enclosing_Func, BIP_Task_Master), Loc));
10248 else
10249 Add_Task_Actuals_To_Build_In_Place_Call
10250 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
10251 end if;
10253 Add_Access_Actual_To_Build_In_Place_Call
10254 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
10256 -- Create an access type designating the function's result subtype. We
10257 -- use the type of the original expression because it may be a call to
10258 -- an inherited operation, which the expansion has replaced with the
10259 -- parent operation that yields the parent type.
10261 Ref_Type := Make_Temporary (Loc, 'A');
10263 Ptr_Typ_Decl :=
10264 Make_Full_Type_Declaration (Loc,
10265 Defining_Identifier => Ref_Type,
10266 Type_Definition =>
10267 Make_Access_To_Object_Definition (Loc,
10268 All_Present => True,
10269 Subtype_Indication =>
10270 New_Occurrence_Of (Etype (Function_Call), Loc)));
10272 -- The access type and its accompanying object must be inserted after
10273 -- the object declaration in the constrained case, so that the function
10274 -- call can be passed access to the object. In the unconstrained case,
10275 -- or if the object declaration is for a return object, the access type
10276 -- and object must be inserted before the object, since the object
10277 -- declaration is rewritten to be a renaming of a dereference of the
10278 -- access object.
10280 if Is_Constrained (Underlying_Type (Result_Subt))
10281 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
10282 then
10283 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
10284 else
10285 Insert_Action (Object_Decl, Ptr_Typ_Decl);
10286 end if;
10288 -- Finally, create an access object initialized to a reference to the
10289 -- function call. We know this access value cannot be null, so mark the
10290 -- entity accordingly to suppress the access check.
10292 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
10294 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
10295 Set_Etype (Def_Id, Ref_Type);
10296 Set_Is_Known_Non_Null (Def_Id);
10298 Res_Decl :=
10299 Make_Object_Declaration (Loc,
10300 Defining_Identifier => Def_Id,
10301 Object_Definition => New_Occurrence_Of (Ref_Type, Loc),
10302 Expression => New_Expr);
10303 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
10305 -- If the result subtype of the called function is constrained and
10306 -- is not itself the return expression of an enclosing BIP function,
10307 -- then mark the object as having no initialization.
10309 if Is_Constrained (Underlying_Type (Result_Subt))
10310 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
10311 then
10312 -- The related object declaration is encased in a transient block
10313 -- because the build-in-place function call contains at least one
10314 -- nested function call that produces a controlled transient
10315 -- temporary:
10317 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
10319 -- Since the build-in-place expansion decouples the call from the
10320 -- object declaration, the finalization machinery lacks the context
10321 -- which prompted the generation of the transient block. To resolve
10322 -- this scenario, store the build-in-place call.
10324 if Scope_Is_Transient
10325 and then Node_To_Be_Wrapped = Object_Decl
10326 then
10327 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
10328 end if;
10330 Set_Expression (Object_Decl, Empty);
10331 Set_No_Initialization (Object_Decl);
10333 -- In case of an unconstrained result subtype, or if the call is the
10334 -- return expression of an enclosing BIP function, rewrite the object
10335 -- declaration as an object renaming where the renamed object is a
10336 -- dereference of <function_Call>'reference:
10338 -- Obj : Subt renames <function_call>'Ref.all;
10340 else
10341 Call_Deref :=
10342 Make_Explicit_Dereference (Loc,
10343 Prefix => New_Occurrence_Of (Def_Id, Loc));
10345 Loc := Sloc (Object_Decl);
10346 Rewrite (Object_Decl,
10347 Make_Object_Renaming_Declaration (Loc,
10348 Defining_Identifier => Make_Temporary (Loc, 'D'),
10349 Access_Definition => Empty,
10350 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
10351 Name => Call_Deref));
10353 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
10355 Analyze (Object_Decl);
10357 -- Replace the internal identifier of the renaming declaration's
10358 -- entity with identifier of the original object entity. We also have
10359 -- to exchange the entities containing their defining identifiers to
10360 -- ensure the correct replacement of the object declaration by the
10361 -- object renaming declaration to avoid homograph conflicts (since
10362 -- the object declaration's defining identifier was already entered
10363 -- in current scope). The Next_Entity links of the two entities also
10364 -- have to be swapped since the entities are part of the return
10365 -- scope's entity list and the list structure would otherwise be
10366 -- corrupted. Finally, the homonym chain must be preserved as well.
10368 declare
10369 Renaming_Def_Id : constant Entity_Id :=
10370 Defining_Identifier (Object_Decl);
10371 Next_Entity_Temp : constant Entity_Id :=
10372 Next_Entity (Renaming_Def_Id);
10373 begin
10374 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
10376 -- Swap next entity links in preparation for exchanging entities
10378 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
10379 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
10380 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
10382 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
10384 -- Preserve source indication of original declaration, so that
10385 -- xref information is properly generated for the right entity.
10387 Preserve_Comes_From_Source
10388 (Object_Decl, Original_Node (Object_Decl));
10390 Preserve_Comes_From_Source
10391 (Obj_Def_Id, Original_Node (Object_Decl));
10393 Set_Comes_From_Source (Renaming_Def_Id, False);
10394 end;
10395 end if;
10397 -- If the object entity has a class-wide Etype, then we need to change
10398 -- it to the result subtype of the function call, because otherwise the
10399 -- object will be class-wide without an explicit initialization and
10400 -- won't be allocated properly by the back end. It seems unclean to make
10401 -- such a revision to the type at this point, and we should try to
10402 -- improve this treatment when build-in-place functions with class-wide
10403 -- results are implemented. ???
10405 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
10406 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
10407 end if;
10408 end Make_Build_In_Place_Call_In_Object_Declaration;
10410 --------------------------------------------
10411 -- Make_CPP_Constructor_Call_In_Allocator --
10412 --------------------------------------------
10414 procedure Make_CPP_Constructor_Call_In_Allocator
10415 (Allocator : Node_Id;
10416 Function_Call : Node_Id)
10418 Loc : constant Source_Ptr := Sloc (Function_Call);
10419 Acc_Type : constant Entity_Id := Etype (Allocator);
10420 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
10421 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
10423 New_Allocator : Node_Id;
10424 Return_Obj_Access : Entity_Id;
10425 Tmp_Obj : Node_Id;
10427 begin
10428 pragma Assert (Nkind (Allocator) = N_Allocator
10429 and then Nkind (Function_Call) = N_Function_Call);
10430 pragma Assert (Convention (Function_Id) = Convention_CPP
10431 and then Is_Constructor (Function_Id));
10432 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
10434 -- Replace the initialized allocator of form "new T'(Func (...))" with
10435 -- an uninitialized allocator of form "new T", where T is the result
10436 -- subtype of the called function. The call to the function is handled
10437 -- separately further below.
10439 New_Allocator :=
10440 Make_Allocator (Loc,
10441 Expression => New_Occurrence_Of (Result_Subt, Loc));
10442 Set_No_Initialization (New_Allocator);
10444 -- Copy attributes to new allocator. Note that the new allocator
10445 -- logically comes from source if the original one did, so copy the
10446 -- relevant flag. This ensures proper treatment of the restriction
10447 -- No_Implicit_Heap_Allocations in this case.
10449 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
10450 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
10451 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
10453 Rewrite (Allocator, New_Allocator);
10455 -- Create a new access object and initialize it to the result of the
10456 -- new uninitialized allocator. Note: we do not use Allocator as the
10457 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
10458 -- as this would create a sort of infinite "recursion".
10460 Return_Obj_Access := Make_Temporary (Loc, 'R');
10461 Set_Etype (Return_Obj_Access, Acc_Type);
10463 -- Generate:
10464 -- Rnnn : constant ptr_T := new (T);
10465 -- Init (Rnn.all,...);
10467 Tmp_Obj :=
10468 Make_Object_Declaration (Loc,
10469 Defining_Identifier => Return_Obj_Access,
10470 Constant_Present => True,
10471 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
10472 Expression => Relocate_Node (Allocator));
10473 Insert_Action (Allocator, Tmp_Obj);
10475 Insert_List_After_And_Analyze (Tmp_Obj,
10476 Build_Initialization_Call (Loc,
10477 Id_Ref =>
10478 Make_Explicit_Dereference (Loc,
10479 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
10480 Typ => Etype (Function_Id),
10481 Constructor_Ref => Function_Call));
10483 -- Finally, replace the allocator node with a reference to the result of
10484 -- the function call itself (which will effectively be an access to the
10485 -- object created by the allocator).
10487 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
10489 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
10490 -- generate an implicit conversion to force displacement of the "this"
10491 -- pointer.
10493 if Is_Interface (Designated_Type (Acc_Type)) then
10494 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
10495 end if;
10497 Analyze_And_Resolve (Allocator, Acc_Type);
10498 end Make_CPP_Constructor_Call_In_Allocator;
10500 -----------------------------------
10501 -- Needs_BIP_Finalization_Master --
10502 -----------------------------------
10504 function Needs_BIP_Finalization_Master
10505 (Func_Id : Entity_Id) return Boolean
10507 pragma Assert (Is_Build_In_Place_Function (Func_Id));
10508 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10509 begin
10510 return
10511 not Restriction_Active (No_Finalization)
10512 and then Needs_Finalization (Func_Typ);
10513 end Needs_BIP_Finalization_Master;
10515 --------------------------
10516 -- Needs_BIP_Alloc_Form --
10517 --------------------------
10519 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
10520 pragma Assert (Is_Build_In_Place_Function (Func_Id));
10521 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10522 begin
10523 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
10524 end Needs_BIP_Alloc_Form;
10526 --------------------------------------
10527 -- Needs_Result_Accessibility_Level --
10528 --------------------------------------
10530 function Needs_Result_Accessibility_Level
10531 (Func_Id : Entity_Id) return Boolean
10533 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
10535 function Has_Unconstrained_Access_Discriminant_Component
10536 (Comp_Typ : Entity_Id) return Boolean;
10537 -- Returns True if any component of the type has an unconstrained access
10538 -- discriminant.
10540 -----------------------------------------------------
10541 -- Has_Unconstrained_Access_Discriminant_Component --
10542 -----------------------------------------------------
10544 function Has_Unconstrained_Access_Discriminant_Component
10545 (Comp_Typ : Entity_Id) return Boolean
10547 begin
10548 if not Is_Limited_Type (Comp_Typ) then
10549 return False;
10551 -- Only limited types can have access discriminants with
10552 -- defaults.
10554 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
10555 return True;
10557 elsif Is_Array_Type (Comp_Typ) then
10558 return Has_Unconstrained_Access_Discriminant_Component
10559 (Underlying_Type (Component_Type (Comp_Typ)));
10561 elsif Is_Record_Type (Comp_Typ) then
10562 declare
10563 Comp : Entity_Id;
10565 begin
10566 Comp := First_Component (Comp_Typ);
10567 while Present (Comp) loop
10568 if Has_Unconstrained_Access_Discriminant_Component
10569 (Underlying_Type (Etype (Comp)))
10570 then
10571 return True;
10572 end if;
10574 Next_Component (Comp);
10575 end loop;
10576 end;
10577 end if;
10579 return False;
10580 end Has_Unconstrained_Access_Discriminant_Component;
10582 Feature_Disabled : constant Boolean := True;
10583 -- Temporary
10585 -- Start of processing for Needs_Result_Accessibility_Level
10587 begin
10588 -- False if completion unavailable (how does this happen???)
10590 if not Present (Func_Typ) then
10591 return False;
10593 elsif Feature_Disabled then
10594 return False;
10596 -- False if not a function, also handle enum-lit renames case
10598 elsif Func_Typ = Standard_Void_Type
10599 or else Is_Scalar_Type (Func_Typ)
10600 then
10601 return False;
10603 -- Handle a corner case, a cross-dialect subp renaming. For example,
10604 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
10605 -- an Ada 2005 (or earlier) unit references predefined run-time units.
10607 elsif Present (Alias (Func_Id)) then
10609 -- Unimplemented: a cross-dialect subp renaming which does not set
10610 -- the Alias attribute (e.g., a rename of a dereference of an access
10611 -- to subprogram value). ???
10613 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
10615 -- Remaining cases require Ada 2012 mode
10617 elsif Ada_Version < Ada_2012 then
10618 return False;
10620 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
10621 or else Is_Tagged_Type (Func_Typ)
10622 then
10623 -- In the case of, say, a null tagged record result type, the need
10624 -- for this extra parameter might not be obvious. This function
10625 -- returns True for all tagged types for compatibility reasons.
10626 -- A function with, say, a tagged null controlling result type might
10627 -- be overridden by a primitive of an extension having an access
10628 -- discriminant and the overrider and overridden must have compatible
10629 -- calling conventions (including implicitly declared parameters).
10630 -- Similarly, values of one access-to-subprogram type might designate
10631 -- both a primitive subprogram of a given type and a function
10632 -- which is, for example, not a primitive subprogram of any type.
10633 -- Again, this requires calling convention compatibility.
10634 -- It might be possible to solve these issues by introducing
10635 -- wrappers, but that is not the approach that was chosen.
10637 return True;
10639 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
10640 return True;
10642 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
10643 return True;
10645 -- False for all other cases
10647 else
10648 return False;
10649 end if;
10650 end Needs_Result_Accessibility_Level;
10652 ------------------------
10653 -- List_Inlining_Info --
10654 ------------------------
10656 procedure List_Inlining_Info is
10657 Elmt : Elmt_Id;
10658 Nod : Node_Id;
10659 Count : Nat;
10661 begin
10662 if not Debug_Flag_Dot_J then
10663 return;
10664 end if;
10666 -- Generate listing of calls inlined by the frontend
10668 if Present (Inlined_Calls) then
10669 Count := 0;
10670 Elmt := First_Elmt (Inlined_Calls);
10671 while Present (Elmt) loop
10672 Nod := Node (Elmt);
10674 if In_Extended_Main_Code_Unit (Nod) then
10675 Count := Count + 1;
10677 if Count = 1 then
10678 Write_Str ("Listing of frontend inlined calls");
10679 Write_Eol;
10680 end if;
10682 Write_Str (" ");
10683 Write_Int (Count);
10684 Write_Str (":");
10685 Write_Location (Sloc (Nod));
10686 Write_Str (":");
10687 Output.Write_Eol;
10688 end if;
10690 Next_Elmt (Elmt);
10691 end loop;
10692 end if;
10694 -- Generate listing of calls passed to the backend
10696 if Present (Backend_Calls) then
10697 Count := 0;
10699 Elmt := First_Elmt (Backend_Calls);
10700 while Present (Elmt) loop
10701 Nod := Node (Elmt);
10703 if In_Extended_Main_Code_Unit (Nod) then
10704 Count := Count + 1;
10706 if Count = 1 then
10707 Write_Str ("Listing of inlined calls passed to the backend");
10708 Write_Eol;
10709 end if;
10711 Write_Str (" ");
10712 Write_Int (Count);
10713 Write_Str (":");
10714 Write_Location (Sloc (Nod));
10715 Output.Write_Eol;
10716 end if;
10718 Next_Elmt (Elmt);
10719 end loop;
10720 end if;
10721 end List_Inlining_Info;
10723 end Exp_Ch6;