2015-05-12 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob8677562f435802d19ddb4111e8f7ef576050560d
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-2015, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Elists; use Elists;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Prag; use Exp_Prag;
44 with Exp_Tss; use Exp_Tss;
45 with Exp_Unst; use Exp_Unst;
46 with Exp_Util; use Exp_Util;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
49 with Lib; use Lib;
50 with Namet; use Namet;
51 with Nlists; use Nlists;
52 with Nmake; use Nmake;
53 with Opt; use Opt;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Ch6; use Sem_Ch6;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Res; use Sem_Res;
68 with Sem_SCIL; use Sem_SCIL;
69 with Sem_Util; use Sem_Util;
70 with Sinfo; use Sinfo;
71 with Snames; use Snames;
72 with Stand; use Stand;
73 with Stringt; use Stringt;
74 with Table;
75 with Targparm; use Targparm;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Validsw; use Validsw;
80 package body Exp_Ch6 is
82 -------------------------------------
83 -- Table for Unnesting Subprograms --
84 -------------------------------------
86 -- When we expand a subprogram body, if it has nested subprograms and if
87 -- we are in Unnest_Subprogram_Mode, then we record the subprogram entity
88 -- and the body in this table, to later be passed to Unnest_Subprogram.
90 -- We need this delaying mechanism, because we have to wait untiil all
91 -- instantiated bodies have been inserted before doing the unnesting.
93 type Unest_Entry is record
94 Ent : Entity_Id;
95 -- Entity for subprogram to be unnested
97 Bod : Node_Id;
98 -- Subprogram body to be unnested
99 end record;
101 package Unest_Bodies is new Table.Table (
102 Table_Component_Type => Unest_Entry,
103 Table_Index_Type => Nat,
104 Table_Low_Bound => 1,
105 Table_Initial => 100,
106 Table_Increment => 200,
107 Table_Name => "Unest_Bodies");
109 -----------------------
110 -- Local Subprograms --
111 -----------------------
113 procedure Add_Access_Actual_To_Build_In_Place_Call
114 (Function_Call : Node_Id;
115 Function_Id : Entity_Id;
116 Return_Object : Node_Id;
117 Is_Access : Boolean := False);
118 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
119 -- object name given by Return_Object and add the attribute to the end of
120 -- the actual parameter list associated with the build-in-place function
121 -- call denoted by Function_Call. However, if Is_Access is True, then
122 -- Return_Object is already an access expression, in which case it's passed
123 -- along directly to the build-in-place function. Finally, if Return_Object
124 -- is empty, then pass a null literal as the actual.
126 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
127 (Function_Call : Node_Id;
128 Function_Id : Entity_Id;
129 Alloc_Form : BIP_Allocation_Form := Unspecified;
130 Alloc_Form_Exp : Node_Id := Empty;
131 Pool_Actual : Node_Id := Make_Null (No_Location));
132 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
133 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
134 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
135 -- otherwise pass a literal corresponding to the Alloc_Form parameter
136 -- (which must not be Unspecified in that case). Pool_Actual is the
137 -- parameter to pass to BIP_Storage_Pool.
139 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
140 (Func_Call : Node_Id;
141 Func_Id : Entity_Id;
142 Ptr_Typ : Entity_Id := Empty;
143 Master_Exp : Node_Id := Empty);
144 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
145 -- finalization actions, add an actual parameter which is a pointer to the
146 -- finalization master of the caller. If Master_Exp is not Empty, then that
147 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
148 -- will result in an automatic "null" value for the actual.
150 procedure Add_Task_Actuals_To_Build_In_Place_Call
151 (Function_Call : Node_Id;
152 Function_Id : Entity_Id;
153 Master_Actual : Node_Id;
154 Chain : Node_Id := Empty);
155 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
156 -- contains tasks, add two actual parameters: the master, and a pointer to
157 -- the caller's activation chain. Master_Actual is the actual parameter
158 -- expression to pass for the master. In most cases, this is the current
159 -- master (_master). The two exceptions are: If the function call is the
160 -- initialization expression for an allocator, we pass the master of the
161 -- access type. If the function call is the initialization expression for a
162 -- return object, we pass along the master passed in by the caller. In most
163 -- contexts, the activation chain to pass is the local one, which is
164 -- indicated by No (Chain). However, in an allocator, the caller passes in
165 -- the activation Chain. Note: Master_Actual can be Empty, but only if
166 -- there are no tasks.
168 procedure Check_Overriding_Operation (Subp : Entity_Id);
169 -- Subp is a dispatching operation. Check whether it may override an
170 -- inherited private operation, in which case its DT entry is that of
171 -- the hidden operation, not the one it may have received earlier.
172 -- This must be done before emitting the code to set the corresponding
173 -- DT to the address of the subprogram. The actual placement of Subp in
174 -- the proper place in the list of primitive operations is done in
175 -- Declare_Inherited_Private_Subprograms, which also has to deal with
176 -- implicit operations. This duplication is unavoidable for now???
178 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
179 -- This procedure is called only if the subprogram body N, whose spec
180 -- has the given entity Spec, contains a parameterless recursive call.
181 -- It attempts to generate runtime code to detect if this a case of
182 -- infinite recursion.
184 -- The body is scanned to determine dependencies. If the only external
185 -- dependencies are on a small set of scalar variables, then the values
186 -- of these variables are captured on entry to the subprogram, and if
187 -- the values are not changed for the call, we know immediately that
188 -- we have an infinite recursion.
190 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id);
191 -- For each actual of an in-out or out parameter which is a numeric
192 -- (view) conversion of the form T (A), where A denotes a variable,
193 -- we insert the declaration:
195 -- Temp : T[ := T (A)];
197 -- prior to the call. Then we replace the actual with a reference to Temp,
198 -- and append the assignment:
200 -- A := TypeA (Temp);
202 -- after the call. Here TypeA is the actual type of variable A. For out
203 -- parameters, the initial declaration has no expression. If A is not an
204 -- entity name, we generate instead:
206 -- Var : TypeA renames A;
207 -- Temp : T := Var; -- omitting expression for out parameter.
208 -- ...
209 -- Var := TypeA (Temp);
211 -- For other in-out parameters, we emit the required constraint checks
212 -- before and/or after the call.
214 -- For all parameter modes, actuals that denote components and slices of
215 -- packed arrays are expanded into suitable temporaries.
217 -- For non-scalar objects that are possibly unaligned, add call by copy
218 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
220 -- For OUT and IN OUT parameters, add predicate checks after the call
221 -- based on the predicates of the actual type.
223 -- The parameter N is IN OUT because in some cases, the expansion code
224 -- rewrites the call as an expression actions with the call inside. In
225 -- this case N is reset to point to the inside call so that the caller
226 -- can continue processing of this call.
228 procedure Expand_Ctrl_Function_Call (N : Node_Id);
229 -- N is a function call which returns a controlled object. Transform the
230 -- call into a temporary which retrieves the returned object from the
231 -- secondary stack using 'reference.
233 procedure Expand_Non_Function_Return (N : Node_Id);
234 -- Expand a simple return statement found in a procedure body, entry body,
235 -- accept statement, or an extended return statement. Note that all non-
236 -- function returns are simple return statements.
238 function Expand_Protected_Object_Reference
239 (N : Node_Id;
240 Scop : Entity_Id) return Node_Id;
242 procedure Expand_Protected_Subprogram_Call
243 (N : Node_Id;
244 Subp : Entity_Id;
245 Scop : Entity_Id);
246 -- A call to a protected subprogram within the protected object may appear
247 -- as a regular call. The list of actuals must be expanded to contain a
248 -- reference to the object itself, and the call becomes a call to the
249 -- corresponding protected subprogram.
251 function Has_Unconstrained_Access_Discriminants
252 (Subtyp : Entity_Id) return Boolean;
253 -- Returns True if the given subtype is unconstrained and has one
254 -- or more access discriminants.
256 procedure Expand_Simple_Function_Return (N : Node_Id);
257 -- Expand simple return from function. In the case where we are returning
258 -- from a function body this is called by Expand_N_Simple_Return_Statement.
260 ----------------------------------------------
261 -- Add_Access_Actual_To_Build_In_Place_Call --
262 ----------------------------------------------
264 procedure Add_Access_Actual_To_Build_In_Place_Call
265 (Function_Call : Node_Id;
266 Function_Id : Entity_Id;
267 Return_Object : Node_Id;
268 Is_Access : Boolean := False)
270 Loc : constant Source_Ptr := Sloc (Function_Call);
271 Obj_Address : Node_Id;
272 Obj_Acc_Formal : Entity_Id;
274 begin
275 -- Locate the implicit access parameter in the called function
277 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
279 -- If no return object is provided, then pass null
281 if not Present (Return_Object) then
282 Obj_Address := Make_Null (Loc);
283 Set_Parent (Obj_Address, Function_Call);
285 -- If Return_Object is already an expression of an access type, then use
286 -- it directly, since it must be an access value denoting the return
287 -- object, and couldn't possibly be the return object itself.
289 elsif Is_Access then
290 Obj_Address := Return_Object;
291 Set_Parent (Obj_Address, Function_Call);
293 -- Apply Unrestricted_Access to caller's return object
295 else
296 Obj_Address :=
297 Make_Attribute_Reference (Loc,
298 Prefix => Return_Object,
299 Attribute_Name => Name_Unrestricted_Access);
301 Set_Parent (Return_Object, Obj_Address);
302 Set_Parent (Obj_Address, Function_Call);
303 end if;
305 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
307 -- Build the parameter association for the new actual and add it to the
308 -- end of the function's actuals.
310 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
311 end Add_Access_Actual_To_Build_In_Place_Call;
313 ------------------------------------------------------
314 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
315 ------------------------------------------------------
317 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
318 (Function_Call : Node_Id;
319 Function_Id : Entity_Id;
320 Alloc_Form : BIP_Allocation_Form := Unspecified;
321 Alloc_Form_Exp : Node_Id := Empty;
322 Pool_Actual : Node_Id := Make_Null (No_Location))
324 Loc : constant Source_Ptr := Sloc (Function_Call);
325 Alloc_Form_Actual : Node_Id;
326 Alloc_Form_Formal : Node_Id;
327 Pool_Formal : Node_Id;
329 begin
330 -- The allocation form generally doesn't need to be passed in the case
331 -- of a constrained result subtype, since normally the caller performs
332 -- the allocation in that case. However this formal is still needed in
333 -- the case where the function has a tagged result, because generally
334 -- such functions can be called in a dispatching context and such calls
335 -- must be handled like calls to class-wide functions.
337 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
338 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
339 then
340 return;
341 end if;
343 -- Locate the implicit allocation form parameter in the called function.
344 -- Maybe it would be better for each implicit formal of a build-in-place
345 -- function to have a flag or a Uint attribute to identify it. ???
347 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
349 if Present (Alloc_Form_Exp) then
350 pragma Assert (Alloc_Form = Unspecified);
352 Alloc_Form_Actual := Alloc_Form_Exp;
354 else
355 pragma Assert (Alloc_Form /= Unspecified);
357 Alloc_Form_Actual :=
358 Make_Integer_Literal (Loc,
359 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
360 end if;
362 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
364 -- Build the parameter association for the new actual and add it to the
365 -- end of the function's actuals.
367 Add_Extra_Actual_To_Call
368 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
370 -- Pass the Storage_Pool parameter. This parameter is omitted on
371 -- .NET/JVM/ZFP as those targets do not support pools.
373 if VM_Target = No_VM
374 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
375 then
376 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
377 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
378 Add_Extra_Actual_To_Call
379 (Function_Call, Pool_Formal, Pool_Actual);
380 end if;
381 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
383 -----------------------------------------------------------
384 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
385 -----------------------------------------------------------
387 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
388 (Func_Call : Node_Id;
389 Func_Id : Entity_Id;
390 Ptr_Typ : Entity_Id := Empty;
391 Master_Exp : Node_Id := Empty)
393 begin
394 if not Needs_BIP_Finalization_Master (Func_Id) then
395 return;
396 end if;
398 declare
399 Formal : constant Entity_Id :=
400 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
401 Loc : constant Source_Ptr := Sloc (Func_Call);
403 Actual : Node_Id;
404 Desig_Typ : Entity_Id;
406 begin
407 -- If there is a finalization master actual, such as the implicit
408 -- finalization master of an enclosing build-in-place function,
409 -- then this must be added as an extra actual of the call.
411 if Present (Master_Exp) then
412 Actual := Master_Exp;
414 -- Case where the context does not require an actual master
416 elsif No (Ptr_Typ) then
417 Actual := Make_Null (Loc);
419 else
420 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
422 -- Check for a library-level access type whose designated type has
423 -- supressed finalization. Such an access types lack a master.
424 -- Pass a null actual to the callee in order to signal a missing
425 -- master.
427 if Is_Library_Level_Entity (Ptr_Typ)
428 and then Finalize_Storage_Only (Desig_Typ)
429 then
430 Actual := Make_Null (Loc);
432 -- Types in need of finalization actions
434 elsif Needs_Finalization (Desig_Typ) then
436 -- The general mechanism of creating finalization masters for
437 -- anonymous access types is disabled by default, otherwise
438 -- finalization masters will pop all over the place. Such types
439 -- use context-specific masters.
441 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
442 and then No (Finalization_Master (Ptr_Typ))
443 then
444 Build_Finalization_Master
445 (Typ => Ptr_Typ,
446 For_Anonymous => True,
447 Context_Scope => Scope (Ptr_Typ),
448 Insertion_Node => Associated_Node_For_Itype (Ptr_Typ));
449 end if;
451 -- Access-to-controlled types should always have a master
453 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
455 Actual :=
456 Make_Attribute_Reference (Loc,
457 Prefix =>
458 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
459 Attribute_Name => Name_Unrestricted_Access);
461 -- Tagged types
463 else
464 Actual := Make_Null (Loc);
465 end if;
466 end if;
468 Analyze_And_Resolve (Actual, Etype (Formal));
470 -- Build the parameter association for the new actual and add it to
471 -- the end of the function's actuals.
473 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
474 end;
475 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
477 ------------------------------
478 -- Add_Extra_Actual_To_Call --
479 ------------------------------
481 procedure Add_Extra_Actual_To_Call
482 (Subprogram_Call : Node_Id;
483 Extra_Formal : Entity_Id;
484 Extra_Actual : Node_Id)
486 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
487 Param_Assoc : Node_Id;
489 begin
490 Param_Assoc :=
491 Make_Parameter_Association (Loc,
492 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
493 Explicit_Actual_Parameter => Extra_Actual);
495 Set_Parent (Param_Assoc, Subprogram_Call);
496 Set_Parent (Extra_Actual, Param_Assoc);
498 if Present (Parameter_Associations (Subprogram_Call)) then
499 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
500 N_Parameter_Association
501 then
503 -- Find last named actual, and append
505 declare
506 L : Node_Id;
507 begin
508 L := First_Actual (Subprogram_Call);
509 while Present (L) loop
510 if No (Next_Actual (L)) then
511 Set_Next_Named_Actual (Parent (L), Extra_Actual);
512 exit;
513 end if;
514 Next_Actual (L);
515 end loop;
516 end;
518 else
519 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
520 end if;
522 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
524 else
525 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
526 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
527 end if;
528 end Add_Extra_Actual_To_Call;
530 ---------------------------------------------
531 -- Add_Task_Actuals_To_Build_In_Place_Call --
532 ---------------------------------------------
534 procedure Add_Task_Actuals_To_Build_In_Place_Call
535 (Function_Call : Node_Id;
536 Function_Id : Entity_Id;
537 Master_Actual : Node_Id;
538 Chain : Node_Id := Empty)
540 Loc : constant Source_Ptr := Sloc (Function_Call);
541 Result_Subt : constant Entity_Id :=
542 Available_View (Etype (Function_Id));
543 Actual : Node_Id;
544 Chain_Actual : Node_Id;
545 Chain_Formal : Node_Id;
546 Master_Formal : Node_Id;
548 begin
549 -- No such extra parameters are needed if there are no tasks
551 if not Has_Task (Result_Subt) then
552 return;
553 end if;
555 Actual := Master_Actual;
557 -- Use a dummy _master actual in case of No_Task_Hierarchy
559 if Restriction_Active (No_Task_Hierarchy) then
560 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
562 -- In the case where we use the master associated with an access type,
563 -- the actual is an entity and requires an explicit reference.
565 elsif Nkind (Actual) = N_Defining_Identifier then
566 Actual := New_Occurrence_Of (Actual, Loc);
567 end if;
569 -- Locate the implicit master parameter in the called function
571 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
572 Analyze_And_Resolve (Actual, Etype (Master_Formal));
574 -- Build the parameter association for the new actual and add it to the
575 -- end of the function's actuals.
577 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
579 -- Locate the implicit activation chain parameter in the called function
581 Chain_Formal :=
582 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
584 -- Create the actual which is a pointer to the current activation chain
586 if No (Chain) then
587 Chain_Actual :=
588 Make_Attribute_Reference (Loc,
589 Prefix => Make_Identifier (Loc, Name_uChain),
590 Attribute_Name => Name_Unrestricted_Access);
592 -- Allocator case; make a reference to the Chain passed in by the caller
594 else
595 Chain_Actual :=
596 Make_Attribute_Reference (Loc,
597 Prefix => New_Occurrence_Of (Chain, Loc),
598 Attribute_Name => Name_Unrestricted_Access);
599 end if;
601 Analyze_And_Resolve (Chain_Actual, Etype (Chain_Formal));
603 -- Build the parameter association for the new actual and add it to the
604 -- end of the function's actuals.
606 Add_Extra_Actual_To_Call (Function_Call, Chain_Formal, Chain_Actual);
607 end Add_Task_Actuals_To_Build_In_Place_Call;
609 -----------------------
610 -- BIP_Formal_Suffix --
611 -----------------------
613 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
614 begin
615 case Kind is
616 when BIP_Alloc_Form =>
617 return "BIPalloc";
618 when BIP_Storage_Pool =>
619 return "BIPstoragepool";
620 when BIP_Finalization_Master =>
621 return "BIPfinalizationmaster";
622 when BIP_Task_Master =>
623 return "BIPtaskmaster";
624 when BIP_Activation_Chain =>
625 return "BIPactivationchain";
626 when BIP_Object_Access =>
627 return "BIPaccess";
628 end case;
629 end BIP_Formal_Suffix;
631 ---------------------------
632 -- Build_In_Place_Formal --
633 ---------------------------
635 function Build_In_Place_Formal
636 (Func : Entity_Id;
637 Kind : BIP_Formal_Kind) return Entity_Id
639 Formal_Name : constant Name_Id :=
640 New_External_Name
641 (Chars (Func), BIP_Formal_Suffix (Kind));
642 Extra_Formal : Entity_Id := Extra_Formals (Func);
644 begin
645 -- Maybe it would be better for each implicit formal of a build-in-place
646 -- function to have a flag or a Uint attribute to identify it. ???
648 -- The return type in the function declaration may have been a limited
649 -- view, and the extra formals for the function were not generated at
650 -- that point. At the point of call the full view must be available and
651 -- the extra formals can be created.
653 if No (Extra_Formal) then
654 Create_Extra_Formals (Func);
655 Extra_Formal := Extra_Formals (Func);
656 end if;
658 loop
659 pragma Assert (Present (Extra_Formal));
660 exit when Chars (Extra_Formal) = Formal_Name;
662 Next_Formal_With_Extras (Extra_Formal);
663 end loop;
665 return Extra_Formal;
666 end Build_In_Place_Formal;
668 --------------------------------
669 -- Check_Overriding_Operation --
670 --------------------------------
672 procedure Check_Overriding_Operation (Subp : Entity_Id) is
673 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
674 Op_List : constant Elist_Id := Primitive_Operations (Typ);
675 Op_Elmt : Elmt_Id;
676 Prim_Op : Entity_Id;
677 Par_Op : Entity_Id;
679 begin
680 if Is_Derived_Type (Typ)
681 and then not Is_Private_Type (Typ)
682 and then In_Open_Scopes (Scope (Etype (Typ)))
683 and then Is_Base_Type (Typ)
684 then
685 -- Subp overrides an inherited private operation if there is an
686 -- inherited operation with a different name than Subp (see
687 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
688 -- same name as Subp.
690 Op_Elmt := First_Elmt (Op_List);
691 while Present (Op_Elmt) loop
692 Prim_Op := Node (Op_Elmt);
693 Par_Op := Alias (Prim_Op);
695 if Present (Par_Op)
696 and then not Comes_From_Source (Prim_Op)
697 and then Chars (Prim_Op) /= Chars (Par_Op)
698 and then Chars (Par_Op) = Chars (Subp)
699 and then Is_Hidden (Par_Op)
700 and then Type_Conformant (Prim_Op, Subp)
701 then
702 Set_DT_Position_Value (Subp, DT_Position (Prim_Op));
703 end if;
705 Next_Elmt (Op_Elmt);
706 end loop;
707 end if;
708 end Check_Overriding_Operation;
710 -------------------------------
711 -- Detect_Infinite_Recursion --
712 -------------------------------
714 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
715 Loc : constant Source_Ptr := Sloc (N);
717 Var_List : constant Elist_Id := New_Elmt_List;
718 -- List of globals referenced by body of procedure
720 Call_List : constant Elist_Id := New_Elmt_List;
721 -- List of recursive calls in body of procedure
723 Shad_List : constant Elist_Id := New_Elmt_List;
724 -- List of entity id's for entities created to capture the value of
725 -- referenced globals on entry to the procedure.
727 Scop : constant Uint := Scope_Depth (Spec);
728 -- This is used to record the scope depth of the current procedure, so
729 -- that we can identify global references.
731 Max_Vars : constant := 4;
732 -- Do not test more than four global variables
734 Count_Vars : Natural := 0;
735 -- Count variables found so far
737 Var : Entity_Id;
738 Elm : Elmt_Id;
739 Ent : Entity_Id;
740 Call : Elmt_Id;
741 Decl : Node_Id;
742 Test : Node_Id;
743 Elm1 : Elmt_Id;
744 Elm2 : Elmt_Id;
745 Last : Node_Id;
747 function Process (Nod : Node_Id) return Traverse_Result;
748 -- Function to traverse the subprogram body (using Traverse_Func)
750 -------------
751 -- Process --
752 -------------
754 function Process (Nod : Node_Id) return Traverse_Result is
755 begin
756 -- Procedure call
758 if Nkind (Nod) = N_Procedure_Call_Statement then
760 -- Case of one of the detected recursive calls
762 if Is_Entity_Name (Name (Nod))
763 and then Has_Recursive_Call (Entity (Name (Nod)))
764 and then Entity (Name (Nod)) = Spec
765 then
766 Append_Elmt (Nod, Call_List);
767 return Skip;
769 -- Any other procedure call may have side effects
771 else
772 return Abandon;
773 end if;
775 -- A call to a pure function can always be ignored
777 elsif Nkind (Nod) = N_Function_Call
778 and then Is_Entity_Name (Name (Nod))
779 and then Is_Pure (Entity (Name (Nod)))
780 then
781 return Skip;
783 -- Case of an identifier reference
785 elsif Nkind (Nod) = N_Identifier then
786 Ent := Entity (Nod);
788 -- If no entity, then ignore the reference
790 -- Not clear why this can happen. To investigate, remove this
791 -- test and look at the crash that occurs here in 3401-004 ???
793 if No (Ent) then
794 return Skip;
796 -- Ignore entities with no Scope, again not clear how this
797 -- can happen, to investigate, look at 4108-008 ???
799 elsif No (Scope (Ent)) then
800 return Skip;
802 -- Ignore the reference if not to a more global object
804 elsif Scope_Depth (Scope (Ent)) >= Scop then
805 return Skip;
807 -- References to types, exceptions and constants are always OK
809 elsif Is_Type (Ent)
810 or else Ekind (Ent) = E_Exception
811 or else Ekind (Ent) = E_Constant
812 then
813 return Skip;
815 -- If other than a non-volatile scalar variable, we have some
816 -- kind of global reference (e.g. to a function) that we cannot
817 -- deal with so we forget the attempt.
819 elsif Ekind (Ent) /= E_Variable
820 or else not Is_Scalar_Type (Etype (Ent))
821 or else Treat_As_Volatile (Ent)
822 then
823 return Abandon;
825 -- Otherwise we have a reference to a global scalar
827 else
828 -- Loop through global entities already detected
830 Elm := First_Elmt (Var_List);
831 loop
832 -- If not detected before, record this new global reference
834 if No (Elm) then
835 Count_Vars := Count_Vars + 1;
837 if Count_Vars <= Max_Vars then
838 Append_Elmt (Entity (Nod), Var_List);
839 else
840 return Abandon;
841 end if;
843 exit;
845 -- If recorded before, ignore
847 elsif Node (Elm) = Entity (Nod) then
848 return Skip;
850 -- Otherwise keep looking
852 else
853 Next_Elmt (Elm);
854 end if;
855 end loop;
857 return Skip;
858 end if;
860 -- For all other node kinds, recursively visit syntactic children
862 else
863 return OK;
864 end if;
865 end Process;
867 function Traverse_Body is new Traverse_Func (Process);
869 -- Start of processing for Detect_Infinite_Recursion
871 begin
872 -- Do not attempt detection in No_Implicit_Conditional mode, since we
873 -- won't be able to generate the code to handle the recursion in any
874 -- case.
876 if Restriction_Active (No_Implicit_Conditionals) then
877 return;
878 end if;
880 -- Otherwise do traversal and quit if we get abandon signal
882 if Traverse_Body (N) = Abandon then
883 return;
885 -- We must have a call, since Has_Recursive_Call was set. If not just
886 -- ignore (this is only an error check, so if we have a funny situation,
887 -- due to bugs or errors, we do not want to bomb).
889 elsif Is_Empty_Elmt_List (Call_List) then
890 return;
891 end if;
893 -- Here is the case where we detect recursion at compile time
895 -- Push our current scope for analyzing the declarations and code that
896 -- we will insert for the checking.
898 Push_Scope (Spec);
900 -- This loop builds temporary variables for each of the referenced
901 -- globals, so that at the end of the loop the list Shad_List contains
902 -- these temporaries in one-to-one correspondence with the elements in
903 -- Var_List.
905 Last := Empty;
906 Elm := First_Elmt (Var_List);
907 while Present (Elm) loop
908 Var := Node (Elm);
909 Ent := Make_Temporary (Loc, 'S');
910 Append_Elmt (Ent, Shad_List);
912 -- Insert a declaration for this temporary at the start of the
913 -- declarations for the procedure. The temporaries are declared as
914 -- constant objects initialized to the current values of the
915 -- corresponding temporaries.
917 Decl :=
918 Make_Object_Declaration (Loc,
919 Defining_Identifier => Ent,
920 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
921 Constant_Present => True,
922 Expression => New_Occurrence_Of (Var, Loc));
924 if No (Last) then
925 Prepend (Decl, Declarations (N));
926 else
927 Insert_After (Last, Decl);
928 end if;
930 Last := Decl;
931 Analyze (Decl);
932 Next_Elmt (Elm);
933 end loop;
935 -- Loop through calls
937 Call := First_Elmt (Call_List);
938 while Present (Call) loop
940 -- Build a predicate expression of the form
942 -- True
943 -- and then global1 = temp1
944 -- and then global2 = temp2
945 -- ...
947 -- This predicate determines if any of the global values
948 -- referenced by the procedure have changed since the
949 -- current call, if not an infinite recursion is assured.
951 Test := New_Occurrence_Of (Standard_True, Loc);
953 Elm1 := First_Elmt (Var_List);
954 Elm2 := First_Elmt (Shad_List);
955 while Present (Elm1) loop
956 Test :=
957 Make_And_Then (Loc,
958 Left_Opnd => Test,
959 Right_Opnd =>
960 Make_Op_Eq (Loc,
961 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
962 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
964 Next_Elmt (Elm1);
965 Next_Elmt (Elm2);
966 end loop;
968 -- Now we replace the call with the sequence
970 -- if no-changes (see above) then
971 -- raise Storage_Error;
972 -- else
973 -- original-call
974 -- end if;
976 Rewrite (Node (Call),
977 Make_If_Statement (Loc,
978 Condition => Test,
979 Then_Statements => New_List (
980 Make_Raise_Storage_Error (Loc,
981 Reason => SE_Infinite_Recursion)),
983 Else_Statements => New_List (
984 Relocate_Node (Node (Call)))));
986 Analyze (Node (Call));
988 Next_Elmt (Call);
989 end loop;
991 -- Remove temporary scope stack entry used for analysis
993 Pop_Scope;
994 end Detect_Infinite_Recursion;
996 --------------------
997 -- Expand_Actuals --
998 --------------------
1000 --------------------
1001 -- Expand_Actuals --
1002 --------------------
1004 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
1005 Loc : constant Source_Ptr := Sloc (N);
1006 Actual : Node_Id;
1007 Formal : Entity_Id;
1008 N_Node : Node_Id;
1009 Post_Call : List_Id;
1010 E_Actual : Entity_Id;
1011 E_Formal : Entity_Id;
1013 procedure Add_Call_By_Copy_Code;
1014 -- For cases where the parameter must be passed by copy, this routine
1015 -- generates a temporary variable into which the actual is copied and
1016 -- then passes this as the parameter. For an OUT or IN OUT parameter,
1017 -- an assignment is also generated to copy the result back. The call
1018 -- also takes care of any constraint checks required for the type
1019 -- conversion case (on both the way in and the way out).
1021 procedure Add_Simple_Call_By_Copy_Code;
1022 -- This is similar to the above, but is used in cases where we know
1023 -- that all that is needed is to simply create a temporary and copy
1024 -- the value in and out of the temporary.
1026 procedure Check_Fortran_Logical;
1027 -- A value of type Logical that is passed through a formal parameter
1028 -- must be normalized because .TRUE. usually does not have the same
1029 -- representation as True. We assume that .FALSE. = False = 0.
1030 -- What about functions that return a logical type ???
1032 function Is_Legal_Copy return Boolean;
1033 -- Check that an actual can be copied before generating the temporary
1034 -- to be used in the call. If the actual is of a by_reference type then
1035 -- the program is illegal (this can only happen in the presence of
1036 -- rep. clauses that force an incorrect alignment). If the formal is
1037 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1038 -- the effect that this might lead to unaligned arguments.
1040 function Make_Var (Actual : Node_Id) return Entity_Id;
1041 -- Returns an entity that refers to the given actual parameter, Actual
1042 -- (not including any type conversion). If Actual is an entity name,
1043 -- then this entity is returned unchanged, otherwise a renaming is
1044 -- created to provide an entity for the actual.
1046 procedure Reset_Packed_Prefix;
1047 -- The expansion of a packed array component reference is delayed in
1048 -- the context of a call. Now we need to complete the expansion, so we
1049 -- unmark the analyzed bits in all prefixes.
1051 ---------------------------
1052 -- Add_Call_By_Copy_Code --
1053 ---------------------------
1055 procedure Add_Call_By_Copy_Code is
1056 Expr : Node_Id;
1057 Init : Node_Id;
1058 Temp : Entity_Id;
1059 Indic : Node_Id;
1060 Var : Entity_Id;
1061 F_Typ : constant Entity_Id := Etype (Formal);
1062 V_Typ : Entity_Id;
1063 Crep : Boolean;
1065 begin
1066 if not Is_Legal_Copy then
1067 return;
1068 end if;
1070 Temp := Make_Temporary (Loc, 'T', Actual);
1072 -- Use formal type for temp, unless formal type is an unconstrained
1073 -- array, in which case we don't have to worry about bounds checks,
1074 -- and we use the actual type, since that has appropriate bounds.
1076 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1077 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1078 else
1079 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1080 end if;
1082 if Nkind (Actual) = N_Type_Conversion then
1083 V_Typ := Etype (Expression (Actual));
1085 -- If the formal is an (in-)out parameter, capture the name
1086 -- of the variable in order to build the post-call assignment.
1088 Var := Make_Var (Expression (Actual));
1090 Crep := not Same_Representation
1091 (F_Typ, Etype (Expression (Actual)));
1093 else
1094 V_Typ := Etype (Actual);
1095 Var := Make_Var (Actual);
1096 Crep := False;
1097 end if;
1099 -- Setup initialization for case of in out parameter, or an out
1100 -- parameter where the formal is an unconstrained array (in the
1101 -- latter case, we have to pass in an object with bounds).
1103 -- If this is an out parameter, the initial copy is wasteful, so as
1104 -- an optimization for the one-dimensional case we extract the
1105 -- bounds of the actual and build an uninitialized temporary of the
1106 -- right size.
1108 if Ekind (Formal) = E_In_Out_Parameter
1109 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1110 then
1111 if Nkind (Actual) = N_Type_Conversion then
1112 if Conversion_OK (Actual) then
1113 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1114 else
1115 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1116 end if;
1118 elsif Ekind (Formal) = E_Out_Parameter
1119 and then Is_Array_Type (F_Typ)
1120 and then Number_Dimensions (F_Typ) = 1
1121 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1122 then
1123 -- Actual is a one-dimensional array or slice, and the type
1124 -- requires no initialization. Create a temporary of the
1125 -- right size, but do not copy actual into it (optimization).
1127 Init := Empty;
1128 Indic :=
1129 Make_Subtype_Indication (Loc,
1130 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1131 Constraint =>
1132 Make_Index_Or_Discriminant_Constraint (Loc,
1133 Constraints => New_List (
1134 Make_Range (Loc,
1135 Low_Bound =>
1136 Make_Attribute_Reference (Loc,
1137 Prefix => New_Occurrence_Of (Var, Loc),
1138 Attribute_Name => Name_First),
1139 High_Bound =>
1140 Make_Attribute_Reference (Loc,
1141 Prefix => New_Occurrence_Of (Var, Loc),
1142 Attribute_Name => Name_Last)))));
1144 else
1145 Init := New_Occurrence_Of (Var, Loc);
1146 end if;
1148 -- An initialization is created for packed conversions as
1149 -- actuals for out parameters to enable Make_Object_Declaration
1150 -- to determine the proper subtype for N_Node. Note that this
1151 -- is wasteful because the extra copying on the call side is
1152 -- not required for such out parameters. ???
1154 elsif Ekind (Formal) = E_Out_Parameter
1155 and then Nkind (Actual) = N_Type_Conversion
1156 and then (Is_Bit_Packed_Array (F_Typ)
1157 or else
1158 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1159 then
1160 if Conversion_OK (Actual) then
1161 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1162 else
1163 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1164 end if;
1166 elsif Ekind (Formal) = E_In_Parameter then
1168 -- Handle the case in which the actual is a type conversion
1170 if Nkind (Actual) = N_Type_Conversion then
1171 if Conversion_OK (Actual) then
1172 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1173 else
1174 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1175 end if;
1176 else
1177 Init := New_Occurrence_Of (Var, Loc);
1178 end if;
1180 else
1181 Init := Empty;
1182 end if;
1184 N_Node :=
1185 Make_Object_Declaration (Loc,
1186 Defining_Identifier => Temp,
1187 Object_Definition => Indic,
1188 Expression => Init);
1189 Set_Assignment_OK (N_Node);
1190 Insert_Action (N, N_Node);
1192 -- Now, normally the deal here is that we use the defining
1193 -- identifier created by that object declaration. There is
1194 -- one exception to this. In the change of representation case
1195 -- the above declaration will end up looking like:
1197 -- temp : type := identifier;
1199 -- And in this case we might as well use the identifier directly
1200 -- and eliminate the temporary. Note that the analysis of the
1201 -- declaration was not a waste of time in that case, since it is
1202 -- what generated the necessary change of representation code. If
1203 -- the change of representation introduced additional code, as in
1204 -- a fixed-integer conversion, the expression is not an identifier
1205 -- and must be kept.
1207 if Crep
1208 and then Present (Expression (N_Node))
1209 and then Is_Entity_Name (Expression (N_Node))
1210 then
1211 Temp := Entity (Expression (N_Node));
1212 Rewrite (N_Node, Make_Null_Statement (Loc));
1213 end if;
1215 -- For IN parameter, all we do is to replace the actual
1217 if Ekind (Formal) = E_In_Parameter then
1218 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1219 Analyze (Actual);
1221 -- Processing for OUT or IN OUT parameter
1223 else
1224 -- Kill current value indications for the temporary variable we
1225 -- created, since we just passed it as an OUT parameter.
1227 Kill_Current_Values (Temp);
1228 Set_Is_Known_Valid (Temp, False);
1230 -- If type conversion, use reverse conversion on exit
1232 if Nkind (Actual) = N_Type_Conversion then
1233 if Conversion_OK (Actual) then
1234 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1235 else
1236 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1237 end if;
1238 else
1239 Expr := New_Occurrence_Of (Temp, Loc);
1240 end if;
1242 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1243 Analyze (Actual);
1245 -- If the actual is a conversion of a packed reference, it may
1246 -- already have been expanded by Remove_Side_Effects, and the
1247 -- resulting variable is a temporary which does not designate
1248 -- the proper out-parameter, which may not be addressable. In
1249 -- that case, generate an assignment to the original expression
1250 -- (before expansion of the packed reference) so that the proper
1251 -- expansion of assignment to a packed component can take place.
1253 declare
1254 Obj : Node_Id;
1255 Lhs : Node_Id;
1257 begin
1258 if Is_Renaming_Of_Object (Var)
1259 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1260 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1261 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1262 = N_Indexed_Component
1263 and then
1264 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1265 then
1266 Obj := Renamed_Object (Var);
1267 Lhs :=
1268 Make_Selected_Component (Loc,
1269 Prefix =>
1270 New_Copy_Tree (Original_Node (Prefix (Obj))),
1271 Selector_Name => New_Copy (Selector_Name (Obj)));
1272 Reset_Analyzed_Flags (Lhs);
1274 else
1275 Lhs := New_Occurrence_Of (Var, Loc);
1276 end if;
1278 Set_Assignment_OK (Lhs);
1280 if Is_Access_Type (E_Formal)
1281 and then Is_Entity_Name (Lhs)
1282 and then
1283 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1284 then
1285 -- Copyback target is an Ada 2012 stand-alone object of an
1286 -- anonymous access type.
1288 pragma Assert (Ada_Version >= Ada_2012);
1290 if Type_Access_Level (E_Formal) >
1291 Object_Access_Level (Lhs)
1292 then
1293 Append_To (Post_Call,
1294 Make_Raise_Program_Error (Loc,
1295 Reason => PE_Accessibility_Check_Failed));
1296 end if;
1298 Append_To (Post_Call,
1299 Make_Assignment_Statement (Loc,
1300 Name => Lhs,
1301 Expression => Expr));
1303 -- We would like to somehow suppress generation of the
1304 -- extra_accessibility assignment generated by the expansion
1305 -- of the above assignment statement. It's not a correctness
1306 -- issue because the following assignment renders it dead,
1307 -- but generating back-to-back assignments to the same
1308 -- target is undesirable. ???
1310 Append_To (Post_Call,
1311 Make_Assignment_Statement (Loc,
1312 Name => New_Occurrence_Of (
1313 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1314 Expression => Make_Integer_Literal (Loc,
1315 Type_Access_Level (E_Formal))));
1317 else
1318 Append_To (Post_Call,
1319 Make_Assignment_Statement (Loc,
1320 Name => Lhs,
1321 Expression => Expr));
1322 end if;
1323 end;
1324 end if;
1325 end Add_Call_By_Copy_Code;
1327 ----------------------------------
1328 -- Add_Simple_Call_By_Copy_Code --
1329 ----------------------------------
1331 procedure Add_Simple_Call_By_Copy_Code is
1332 Temp : Entity_Id;
1333 Decl : Node_Id;
1334 Incod : Node_Id;
1335 Outcod : Node_Id;
1336 Lhs : Node_Id;
1337 Rhs : Node_Id;
1338 Indic : Node_Id;
1339 F_Typ : constant Entity_Id := Etype (Formal);
1341 begin
1342 if not Is_Legal_Copy then
1343 return;
1344 end if;
1346 -- Use formal type for temp, unless formal type is an unconstrained
1347 -- array, in which case we don't have to worry about bounds checks,
1348 -- and we use the actual type, since that has appropriate bounds.
1350 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1351 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1352 else
1353 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1354 end if;
1356 -- Prepare to generate code
1358 Reset_Packed_Prefix;
1360 Temp := Make_Temporary (Loc, 'T', Actual);
1361 Incod := Relocate_Node (Actual);
1362 Outcod := New_Copy_Tree (Incod);
1364 -- Generate declaration of temporary variable, initializing it
1365 -- with the input parameter unless we have an OUT formal or
1366 -- this is an initialization call.
1368 -- If the formal is an out parameter with discriminants, the
1369 -- discriminants must be captured even if the rest of the object
1370 -- is in principle uninitialized, because the discriminants may
1371 -- be read by the called subprogram.
1373 if Ekind (Formal) = E_Out_Parameter then
1374 Incod := Empty;
1376 if Has_Discriminants (Etype (Formal)) then
1377 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1378 end if;
1380 elsif Inside_Init_Proc then
1382 -- Could use a comment here to match comment below ???
1384 if Nkind (Actual) /= N_Selected_Component
1385 or else
1386 not Has_Discriminant_Dependent_Constraint
1387 (Entity (Selector_Name (Actual)))
1388 then
1389 Incod := Empty;
1391 -- Otherwise, keep the component in order to generate the proper
1392 -- actual subtype, that depends on enclosing discriminants.
1394 else
1395 null;
1396 end if;
1397 end if;
1399 Decl :=
1400 Make_Object_Declaration (Loc,
1401 Defining_Identifier => Temp,
1402 Object_Definition => Indic,
1403 Expression => Incod);
1405 if Inside_Init_Proc
1406 and then No (Incod)
1407 then
1408 -- If the call is to initialize a component of a composite type,
1409 -- and the component does not depend on discriminants, use the
1410 -- actual type of the component. This is required in case the
1411 -- component is constrained, because in general the formal of the
1412 -- initialization procedure will be unconstrained. Note that if
1413 -- the component being initialized is constrained by an enclosing
1414 -- discriminant, the presence of the initialization in the
1415 -- declaration will generate an expression for the actual subtype.
1417 Set_No_Initialization (Decl);
1418 Set_Object_Definition (Decl,
1419 New_Occurrence_Of (Etype (Actual), Loc));
1420 end if;
1422 Insert_Action (N, Decl);
1424 -- The actual is simply a reference to the temporary
1426 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1428 -- Generate copy out if OUT or IN OUT parameter
1430 if Ekind (Formal) /= E_In_Parameter then
1431 Lhs := Outcod;
1432 Rhs := New_Occurrence_Of (Temp, Loc);
1434 -- Deal with conversion
1436 if Nkind (Lhs) = N_Type_Conversion then
1437 Lhs := Expression (Lhs);
1438 Rhs := Convert_To (Etype (Actual), Rhs);
1439 end if;
1441 Append_To (Post_Call,
1442 Make_Assignment_Statement (Loc,
1443 Name => Lhs,
1444 Expression => Rhs));
1445 Set_Assignment_OK (Name (Last (Post_Call)));
1446 end if;
1447 end Add_Simple_Call_By_Copy_Code;
1449 ---------------------------
1450 -- Check_Fortran_Logical --
1451 ---------------------------
1453 procedure Check_Fortran_Logical is
1454 Logical : constant Entity_Id := Etype (Formal);
1455 Var : Entity_Id;
1457 -- Note: this is very incomplete, e.g. it does not handle arrays
1458 -- of logical values. This is really not the right approach at all???)
1460 begin
1461 if Convention (Subp) = Convention_Fortran
1462 and then Root_Type (Etype (Formal)) = Standard_Boolean
1463 and then Ekind (Formal) /= E_In_Parameter
1464 then
1465 Var := Make_Var (Actual);
1466 Append_To (Post_Call,
1467 Make_Assignment_Statement (Loc,
1468 Name => New_Occurrence_Of (Var, Loc),
1469 Expression =>
1470 Unchecked_Convert_To (
1471 Logical,
1472 Make_Op_Ne (Loc,
1473 Left_Opnd => New_Occurrence_Of (Var, Loc),
1474 Right_Opnd =>
1475 Unchecked_Convert_To (
1476 Logical,
1477 New_Occurrence_Of (Standard_False, Loc))))));
1478 end if;
1479 end Check_Fortran_Logical;
1481 -------------------
1482 -- Is_Legal_Copy --
1483 -------------------
1485 function Is_Legal_Copy return Boolean is
1486 begin
1487 -- An attempt to copy a value of such a type can only occur if
1488 -- representation clauses give the actual a misaligned address.
1490 if Is_By_Reference_Type (Etype (Formal)) then
1492 -- If the front-end does not perform full type layout, the actual
1493 -- may in fact be properly aligned but there is not enough front-
1494 -- end information to determine this. In that case gigi will emit
1495 -- an error if a copy is not legal, or generate the proper code.
1496 -- For other backends we report the error now.
1498 -- Seems wrong to be issuing an error in the expander, since it
1499 -- will be missed in -gnatc mode ???
1501 if Frontend_Layout_On_Target then
1502 Error_Msg_N
1503 ("misaligned actual cannot be passed by reference", Actual);
1504 end if;
1506 return False;
1508 -- For users of Starlet, we assume that the specification of by-
1509 -- reference mechanism is mandatory. This may lead to unaligned
1510 -- objects but at least for DEC legacy code it is known to work.
1511 -- The warning will alert users of this code that a problem may
1512 -- be lurking.
1514 elsif Mechanism (Formal) = By_Reference
1515 and then Is_Valued_Procedure (Scope (Formal))
1516 then
1517 Error_Msg_N
1518 ("by_reference actual may be misaligned??", Actual);
1519 return False;
1521 else
1522 return True;
1523 end if;
1524 end Is_Legal_Copy;
1526 --------------
1527 -- Make_Var --
1528 --------------
1530 function Make_Var (Actual : Node_Id) return Entity_Id is
1531 Var : Entity_Id;
1533 begin
1534 if Is_Entity_Name (Actual) then
1535 return Entity (Actual);
1537 else
1538 Var := Make_Temporary (Loc, 'T', Actual);
1540 N_Node :=
1541 Make_Object_Renaming_Declaration (Loc,
1542 Defining_Identifier => Var,
1543 Subtype_Mark =>
1544 New_Occurrence_Of (Etype (Actual), Loc),
1545 Name => Relocate_Node (Actual));
1547 Insert_Action (N, N_Node);
1548 return Var;
1549 end if;
1550 end Make_Var;
1552 -------------------------
1553 -- Reset_Packed_Prefix --
1554 -------------------------
1556 procedure Reset_Packed_Prefix is
1557 Pfx : Node_Id := Actual;
1558 begin
1559 loop
1560 Set_Analyzed (Pfx, False);
1561 exit when
1562 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1563 Pfx := Prefix (Pfx);
1564 end loop;
1565 end Reset_Packed_Prefix;
1567 -- Start of processing for Expand_Actuals
1569 begin
1570 Post_Call := New_List;
1572 Formal := First_Formal (Subp);
1573 Actual := First_Actual (N);
1574 while Present (Formal) loop
1575 E_Formal := Etype (Formal);
1576 E_Actual := Etype (Actual);
1578 if Is_Scalar_Type (E_Formal)
1579 or else Nkind (Actual) = N_Slice
1580 then
1581 Check_Fortran_Logical;
1583 -- RM 6.4.1 (11)
1585 elsif Ekind (Formal) /= E_Out_Parameter then
1587 -- The unusual case of the current instance of a protected type
1588 -- requires special handling. This can only occur in the context
1589 -- of a call within the body of a protected operation.
1591 if Is_Entity_Name (Actual)
1592 and then Ekind (Entity (Actual)) = E_Protected_Type
1593 and then In_Open_Scopes (Entity (Actual))
1594 then
1595 if Scope (Subp) /= Entity (Actual) then
1596 Error_Msg_N
1597 ("operation outside protected type may not "
1598 & "call back its protected operations??", Actual);
1599 end if;
1601 Rewrite (Actual,
1602 Expand_Protected_Object_Reference (N, Entity (Actual)));
1603 end if;
1605 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1606 -- build-in-place function, then a temporary return object needs
1607 -- to be created and access to it must be passed to the function.
1608 -- Currently we limit such functions to those with inherently
1609 -- limited result subtypes, but eventually we plan to expand the
1610 -- functions that are treated as build-in-place to include other
1611 -- composite result types.
1613 if Is_Build_In_Place_Function_Call (Actual) then
1614 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1615 end if;
1617 Apply_Constraint_Check (Actual, E_Formal);
1619 -- Out parameter case. No constraint checks on access type
1620 -- RM 6.4.1 (13)
1622 elsif Is_Access_Type (E_Formal) then
1623 null;
1625 -- RM 6.4.1 (14)
1627 elsif Has_Discriminants (Base_Type (E_Formal))
1628 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1629 then
1630 Apply_Constraint_Check (Actual, E_Formal);
1632 -- RM 6.4.1 (15)
1634 else
1635 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1636 end if;
1638 -- Processing for IN-OUT and OUT parameters
1640 if Ekind (Formal) /= E_In_Parameter then
1642 -- For type conversions of arrays, apply length/range checks
1644 if Is_Array_Type (E_Formal)
1645 and then Nkind (Actual) = N_Type_Conversion
1646 then
1647 if Is_Constrained (E_Formal) then
1648 Apply_Length_Check (Expression (Actual), E_Formal);
1649 else
1650 Apply_Range_Check (Expression (Actual), E_Formal);
1651 end if;
1652 end if;
1654 -- If argument is a type conversion for a type that is passed
1655 -- by copy, then we must pass the parameter by copy.
1657 if Nkind (Actual) = N_Type_Conversion
1658 and then
1659 (Is_Numeric_Type (E_Formal)
1660 or else Is_Access_Type (E_Formal)
1661 or else Is_Enumeration_Type (E_Formal)
1662 or else Is_Bit_Packed_Array (Etype (Formal))
1663 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1665 -- Also pass by copy if change of representation
1667 or else not Same_Representation
1668 (Etype (Formal),
1669 Etype (Expression (Actual))))
1670 then
1671 Add_Call_By_Copy_Code;
1673 -- References to components of bit packed arrays are expanded
1674 -- at this point, rather than at the point of analysis of the
1675 -- actuals, to handle the expansion of the assignment to
1676 -- [in] out parameters.
1678 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1679 Add_Simple_Call_By_Copy_Code;
1681 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1682 -- because the back-end cannot cope with such objects. In other
1683 -- cases where alignment forces a copy, the back-end generates
1684 -- it properly. It should not be generated unconditionally in the
1685 -- front-end because it does not know precisely the alignment
1686 -- requirements of the target, and makes too conservative an
1687 -- estimate, leading to superfluous copies or spurious errors
1688 -- on by-reference parameters.
1690 elsif Nkind (Actual) = N_Selected_Component
1691 and then
1692 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1693 and then not Represented_As_Scalar (Etype (Formal))
1694 then
1695 Add_Simple_Call_By_Copy_Code;
1697 -- References to slices of bit packed arrays are expanded
1699 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1700 Add_Call_By_Copy_Code;
1702 -- References to possibly unaligned slices of arrays are expanded
1704 elsif Is_Possibly_Unaligned_Slice (Actual) then
1705 Add_Call_By_Copy_Code;
1707 -- Deal with access types where the actual subtype and the
1708 -- formal subtype are not the same, requiring a check.
1710 -- It is necessary to exclude tagged types because of "downward
1711 -- conversion" errors.
1713 elsif Is_Access_Type (E_Formal)
1714 and then not Same_Type (E_Formal, E_Actual)
1715 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1716 then
1717 Add_Call_By_Copy_Code;
1719 -- If the actual is not a scalar and is marked for volatile
1720 -- treatment, whereas the formal is not volatile, then pass
1721 -- by copy unless it is a by-reference type.
1723 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1724 -- because this is the enforcement of a language rule that applies
1725 -- only to "real" volatile variables, not e.g. to the address
1726 -- clause overlay case.
1728 elsif Is_Entity_Name (Actual)
1729 and then Is_Volatile (Entity (Actual))
1730 and then not Is_By_Reference_Type (E_Actual)
1731 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1732 and then not Is_Volatile (E_Formal)
1733 then
1734 Add_Call_By_Copy_Code;
1736 elsif Nkind (Actual) = N_Indexed_Component
1737 and then Is_Entity_Name (Prefix (Actual))
1738 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1739 then
1740 Add_Call_By_Copy_Code;
1742 -- Add call-by-copy code for the case of scalar out parameters
1743 -- when it is not known at compile time that the subtype of the
1744 -- formal is a subrange of the subtype of the actual (or vice
1745 -- versa for in out parameters), in order to get range checks
1746 -- on such actuals. (Maybe this case should be handled earlier
1747 -- in the if statement???)
1749 elsif Is_Scalar_Type (E_Formal)
1750 and then
1751 (not In_Subrange_Of (E_Formal, E_Actual)
1752 or else
1753 (Ekind (Formal) = E_In_Out_Parameter
1754 and then not In_Subrange_Of (E_Actual, E_Formal)))
1755 then
1756 -- Perhaps the setting back to False should be done within
1757 -- Add_Call_By_Copy_Code, since it could get set on other
1758 -- cases occurring above???
1760 if Do_Range_Check (Actual) then
1761 Set_Do_Range_Check (Actual, False);
1762 end if;
1764 Add_Call_By_Copy_Code;
1765 end if;
1767 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1768 -- by-reference parameters on exit from the call. If the actual
1769 -- is a derived type and the operation is inherited, the body
1770 -- of the operation will not contain a call to the predicate
1771 -- function, so it must be done explicitly after the call. Ditto
1772 -- if the actual is an entity of a predicated subtype.
1774 -- The rule refers to by-reference types, but a check is needed
1775 -- for by-copy types as well. That check is subsumed by the rule
1776 -- for subtype conversion on assignment, but we can generate the
1777 -- required check now.
1779 -- Note also that Subp may be either a subprogram entity for
1780 -- direct calls, or a type entity for indirect calls, which must
1781 -- be handled separately because the name does not denote an
1782 -- overloadable entity.
1784 By_Ref_Predicate_Check : declare
1785 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1786 Atyp : Entity_Id;
1788 function Is_Public_Subp return Boolean;
1789 -- Check whether the subprogram being called is a visible
1790 -- operation of the type of the actual. Used to determine
1791 -- whether an invariant check must be generated on the
1792 -- caller side.
1794 ---------------------
1795 -- Is_Public_Subp --
1796 ---------------------
1798 function Is_Public_Subp return Boolean is
1799 Pack : constant Entity_Id := Scope (Subp);
1800 Subp_Decl : Node_Id;
1802 begin
1803 if not Is_Subprogram (Subp) then
1804 return False;
1806 -- The operation may be inherited, or a primitive of the
1807 -- root type.
1809 elsif
1810 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
1811 N_Full_Type_Declaration)
1812 then
1813 Subp_Decl := Parent (Subp);
1815 else
1816 Subp_Decl := Unit_Declaration_Node (Subp);
1817 end if;
1819 return Ekind (Pack) = E_Package
1820 and then
1821 List_Containing (Subp_Decl) =
1822 Visible_Declarations
1823 (Specification (Unit_Declaration_Node (Pack)));
1824 end Is_Public_Subp;
1826 -- Start of processing for By_Ref_Predicate_Check
1828 begin
1829 if No (Aund) then
1830 Atyp := E_Actual;
1831 else
1832 Atyp := Aund;
1833 end if;
1835 if Has_Predicates (Atyp)
1836 and then Present (Predicate_Function (Atyp))
1838 -- Skip predicate checks for special cases
1840 and then Predicate_Tests_On_Arguments (Subp)
1841 then
1842 Append_To (Post_Call,
1843 Make_Predicate_Check (Atyp, Actual));
1844 end if;
1846 -- We generated caller-side invariant checks in two cases:
1848 -- a) when calling an inherited operation, where there is an
1849 -- implicit view conversion of the actual to the parent type.
1851 -- b) When the conversion is explicit
1853 -- We treat these cases separately because the required
1854 -- conversion for a) is added later when expanding the call.
1856 if Has_Invariants (Etype (Actual))
1857 and then
1858 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
1859 then
1860 if Comes_From_Source (N) and then Is_Public_Subp then
1861 Append_To (Post_Call, Make_Invariant_Call (Actual));
1862 end if;
1864 elsif Nkind (Actual) = N_Type_Conversion
1865 and then Has_Invariants (Etype (Expression (Actual)))
1866 then
1867 if Comes_From_Source (N) and then Is_Public_Subp then
1868 Append_To (Post_Call,
1869 Make_Invariant_Call (Expression (Actual)));
1870 end if;
1871 end if;
1872 end By_Ref_Predicate_Check;
1874 -- Processing for IN parameters
1876 else
1877 -- For IN parameters is in the packed array case, we expand an
1878 -- indexed component (the circuit in Exp_Ch4 deliberately left
1879 -- indexed components appearing as actuals untouched, so that
1880 -- the special processing above for the OUT and IN OUT cases
1881 -- could be performed. We could make the test in Exp_Ch4 more
1882 -- complex and have it detect the parameter mode, but it is
1883 -- easier simply to handle all cases here.)
1885 if Nkind (Actual) = N_Indexed_Component
1886 and then Is_Packed (Etype (Prefix (Actual)))
1887 then
1888 Reset_Packed_Prefix;
1889 Expand_Packed_Element_Reference (Actual);
1891 -- If we have a reference to a bit packed array, we copy it, since
1892 -- the actual must be byte aligned.
1894 -- Is this really necessary in all cases???
1896 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1897 Add_Simple_Call_By_Copy_Code;
1899 -- If a non-scalar actual is possibly unaligned, we need a copy
1901 elsif Is_Possibly_Unaligned_Object (Actual)
1902 and then not Represented_As_Scalar (Etype (Formal))
1903 then
1904 Add_Simple_Call_By_Copy_Code;
1906 -- Similarly, we have to expand slices of packed arrays here
1907 -- because the result must be byte aligned.
1909 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1910 Add_Call_By_Copy_Code;
1912 -- Only processing remaining is to pass by copy if this is a
1913 -- reference to a possibly unaligned slice, since the caller
1914 -- expects an appropriately aligned argument.
1916 elsif Is_Possibly_Unaligned_Slice (Actual) then
1917 Add_Call_By_Copy_Code;
1919 -- An unusual case: a current instance of an enclosing task can be
1920 -- an actual, and must be replaced by a reference to self.
1922 elsif Is_Entity_Name (Actual)
1923 and then Is_Task_Type (Entity (Actual))
1924 then
1925 if In_Open_Scopes (Entity (Actual)) then
1926 Rewrite (Actual,
1927 (Make_Function_Call (Loc,
1928 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1929 Analyze (Actual);
1931 -- A task type cannot otherwise appear as an actual
1933 else
1934 raise Program_Error;
1935 end if;
1936 end if;
1937 end if;
1939 Next_Formal (Formal);
1940 Next_Actual (Actual);
1941 end loop;
1943 -- Find right place to put post call stuff if it is present
1945 if not Is_Empty_List (Post_Call) then
1947 -- Cases where the call is not a member of a statement list
1949 if not Is_List_Member (N) then
1951 -- In Ada 2012 the call may be a function call in an expression
1952 -- (since OUT and IN OUT parameters are now allowed for such
1953 -- calls). The write-back of (in)-out parameters is handled
1954 -- by the back-end, but the constraint checks generated when
1955 -- subtypes of formal and actual don't match must be inserted
1956 -- in the form of assignments.
1958 if Ada_Version >= Ada_2012
1959 and then Nkind (N) = N_Function_Call
1960 then
1961 -- We used to just do handle this by climbing up parents to
1962 -- a non-statement/declaration and then simply making a call
1963 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1964 -- work. If we are in the middle of an expression, e.g. the
1965 -- condition of an IF, this call would insert after the IF
1966 -- statement, which is much too late to be doing the write
1967 -- back. For example:
1969 -- if Clobber (X) then
1970 -- Put_Line (X'Img);
1971 -- else
1972 -- goto Junk
1973 -- end if;
1975 -- Now assume Clobber changes X, if we put the write back
1976 -- after the IF, the Put_Line gets the wrong value and the
1977 -- goto causes the write back to be skipped completely.
1979 -- To deal with this, we replace the call by
1981 -- do
1982 -- Tnnn : function-result-type renames function-call;
1983 -- Post_Call actions
1984 -- in
1985 -- Tnnn;
1986 -- end;
1988 -- Note: this won't do in Modify_Tree_For_C mode, but we
1989 -- will deal with that later (it will require creating a
1990 -- declaration for Temp, using Insert_Declaration) ???
1992 declare
1993 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
1994 FRTyp : constant Entity_Id := Etype (N);
1995 Name : constant Node_Id := Relocate_Node (N);
1997 begin
1998 Prepend_To (Post_Call,
1999 Make_Object_Renaming_Declaration (Loc,
2000 Defining_Identifier => Tnnn,
2001 Subtype_Mark => New_Occurrence_Of (FRTyp, Loc),
2002 Name => Name));
2004 Rewrite (N,
2005 Make_Expression_With_Actions (Loc,
2006 Actions => Post_Call,
2007 Expression => New_Occurrence_Of (Tnnn, Loc)));
2009 -- We don't want to just blindly call Analyze_And_Resolve
2010 -- because that would cause unwanted recursion on the call.
2011 -- So for a moment set the call as analyzed to prevent that
2012 -- recursion, and get the rest analyzed properly, then reset
2013 -- the analyzed flag, so our caller can continue.
2015 Set_Analyzed (Name, True);
2016 Analyze_And_Resolve (N, FRTyp);
2017 Set_Analyzed (Name, False);
2019 -- Reset calling argument to point to function call inside
2020 -- the expression with actions so the caller can continue
2021 -- to process the call.
2023 N := Name;
2024 end;
2026 -- If not the special Ada 2012 case of a function call, then
2027 -- we must have the triggering statement of a triggering
2028 -- alternative or an entry call alternative, and we can add
2029 -- the post call stuff to the corresponding statement list.
2031 else
2032 declare
2033 P : Node_Id;
2035 begin
2036 P := Parent (N);
2037 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
2038 N_Entry_Call_Alternative));
2040 if Is_Non_Empty_List (Statements (P)) then
2041 Insert_List_Before_And_Analyze
2042 (First (Statements (P)), Post_Call);
2043 else
2044 Set_Statements (P, Post_Call);
2045 end if;
2047 return;
2048 end;
2049 end if;
2051 -- Otherwise, normal case where N is in a statement sequence,
2052 -- just put the post-call stuff after the call statement.
2054 else
2055 Insert_Actions_After (N, Post_Call);
2056 return;
2057 end if;
2058 end if;
2060 -- The call node itself is re-analyzed in Expand_Call
2062 end Expand_Actuals;
2064 -----------------
2065 -- Expand_Call --
2066 -----------------
2068 -- This procedure handles expansion of function calls and procedure call
2069 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2070 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2072 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2073 -- Provide values of actuals for all formals in Extra_Formals list
2074 -- Replace "call" to enumeration literal function by literal itself
2075 -- Rewrite call to predefined operator as operator
2076 -- Replace actuals to in-out parameters that are numeric conversions,
2077 -- with explicit assignment to temporaries before and after the call.
2079 -- Note that the list of actuals has been filled with default expressions
2080 -- during semantic analysis of the call. Only the extra actuals required
2081 -- for the 'Constrained attribute and for accessibility checks are added
2082 -- at this point.
2084 procedure Expand_Call (N : Node_Id) is
2085 Loc : constant Source_Ptr := Sloc (N);
2086 Call_Node : Node_Id := N;
2087 Extra_Actuals : List_Id := No_List;
2088 Prev : Node_Id := Empty;
2090 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2091 -- Adds one entry to the end of the actual parameter list. Used for
2092 -- default parameters and for extra actuals (for Extra_Formals). The
2093 -- argument is an N_Parameter_Association node.
2095 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2096 -- Adds an extra actual to the list of extra actuals. Expr is the
2097 -- expression for the value of the actual, EF is the entity for the
2098 -- extra formal.
2100 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2101 -- Within an instance, a type derived from an untagged formal derived
2102 -- type inherits from the original parent, not from the actual. The
2103 -- current derivation mechanism has the derived type inherit from the
2104 -- actual, which is only correct outside of the instance. If the
2105 -- subprogram is inherited, we test for this particular case through a
2106 -- convoluted tree traversal before setting the proper subprogram to be
2107 -- called.
2109 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2110 -- Return true if E comes from an instance that is not yet frozen
2112 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2113 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2115 function New_Value (From : Node_Id) return Node_Id;
2116 -- From is the original Expression. New_Value is equivalent to a call
2117 -- to Duplicate_Subexpr with an explicit dereference when From is an
2118 -- access parameter.
2120 --------------------------
2121 -- Add_Actual_Parameter --
2122 --------------------------
2124 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2125 Actual_Expr : constant Node_Id :=
2126 Explicit_Actual_Parameter (Insert_Param);
2128 begin
2129 -- Case of insertion is first named actual
2131 if No (Prev) or else
2132 Nkind (Parent (Prev)) /= N_Parameter_Association
2133 then
2134 Set_Next_Named_Actual
2135 (Insert_Param, First_Named_Actual (Call_Node));
2136 Set_First_Named_Actual (Call_Node, Actual_Expr);
2138 if No (Prev) then
2139 if No (Parameter_Associations (Call_Node)) then
2140 Set_Parameter_Associations (Call_Node, New_List);
2141 end if;
2143 Append (Insert_Param, Parameter_Associations (Call_Node));
2145 else
2146 Insert_After (Prev, Insert_Param);
2147 end if;
2149 -- Case of insertion is not first named actual
2151 else
2152 Set_Next_Named_Actual
2153 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2154 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2155 Append (Insert_Param, Parameter_Associations (Call_Node));
2156 end if;
2158 Prev := Actual_Expr;
2159 end Add_Actual_Parameter;
2161 ----------------------
2162 -- Add_Extra_Actual --
2163 ----------------------
2165 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2166 Loc : constant Source_Ptr := Sloc (Expr);
2168 begin
2169 if Extra_Actuals = No_List then
2170 Extra_Actuals := New_List;
2171 Set_Parent (Extra_Actuals, Call_Node);
2172 end if;
2174 Append_To (Extra_Actuals,
2175 Make_Parameter_Association (Loc,
2176 Selector_Name => New_Occurrence_Of (EF, Loc),
2177 Explicit_Actual_Parameter => Expr));
2179 Analyze_And_Resolve (Expr, Etype (EF));
2181 if Nkind (Call_Node) = N_Function_Call then
2182 Set_Is_Accessibility_Actual (Parent (Expr));
2183 end if;
2184 end Add_Extra_Actual;
2186 ---------------------------
2187 -- Inherited_From_Formal --
2188 ---------------------------
2190 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2191 Par : Entity_Id;
2192 Gen_Par : Entity_Id;
2193 Gen_Prim : Elist_Id;
2194 Elmt : Elmt_Id;
2195 Indic : Node_Id;
2197 begin
2198 -- If the operation is inherited, it is attached to the corresponding
2199 -- type derivation. If the parent in the derivation is a generic
2200 -- actual, it is a subtype of the actual, and we have to recover the
2201 -- original derived type declaration to find the proper parent.
2203 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2204 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2205 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2206 N_Derived_Type_Definition
2207 or else not In_Instance
2208 then
2209 return Empty;
2211 else
2212 Indic :=
2213 Subtype_Indication
2214 (Type_Definition (Original_Node (Parent (S))));
2216 if Nkind (Indic) = N_Subtype_Indication then
2217 Par := Entity (Subtype_Mark (Indic));
2218 else
2219 Par := Entity (Indic);
2220 end if;
2221 end if;
2223 if not Is_Generic_Actual_Type (Par)
2224 or else Is_Tagged_Type (Par)
2225 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2226 or else not In_Open_Scopes (Scope (Par))
2227 then
2228 return Empty;
2229 else
2230 Gen_Par := Generic_Parent_Type (Parent (Par));
2231 end if;
2233 -- If the actual has no generic parent type, the formal is not
2234 -- a formal derived type, so nothing to inherit.
2236 if No (Gen_Par) then
2237 return Empty;
2238 end if;
2240 -- If the generic parent type is still the generic type, this is a
2241 -- private formal, not a derived formal, and there are no operations
2242 -- inherited from the formal.
2244 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2245 return Empty;
2246 end if;
2248 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2250 Elmt := First_Elmt (Gen_Prim);
2251 while Present (Elmt) loop
2252 if Chars (Node (Elmt)) = Chars (S) then
2253 declare
2254 F1 : Entity_Id;
2255 F2 : Entity_Id;
2257 begin
2258 F1 := First_Formal (S);
2259 F2 := First_Formal (Node (Elmt));
2260 while Present (F1)
2261 and then Present (F2)
2262 loop
2263 if Etype (F1) = Etype (F2)
2264 or else Etype (F2) = Gen_Par
2265 then
2266 Next_Formal (F1);
2267 Next_Formal (F2);
2268 else
2269 Next_Elmt (Elmt);
2270 exit; -- not the right subprogram
2271 end if;
2273 return Node (Elmt);
2274 end loop;
2275 end;
2277 else
2278 Next_Elmt (Elmt);
2279 end if;
2280 end loop;
2282 raise Program_Error;
2283 end Inherited_From_Formal;
2285 --------------------------
2286 -- In_Unfrozen_Instance --
2287 --------------------------
2289 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2290 S : Entity_Id;
2292 begin
2293 S := E;
2294 while Present (S) and then S /= Standard_Standard loop
2295 if Is_Generic_Instance (S)
2296 and then Present (Freeze_Node (S))
2297 and then not Analyzed (Freeze_Node (S))
2298 then
2299 return True;
2300 end if;
2302 S := Scope (S);
2303 end loop;
2305 return False;
2306 end In_Unfrozen_Instance;
2308 -------------------------
2309 -- Is_Direct_Deep_Call --
2310 -------------------------
2312 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2313 begin
2314 if Is_TSS (Subp, TSS_Deep_Adjust)
2315 or else Is_TSS (Subp, TSS_Deep_Finalize)
2316 or else Is_TSS (Subp, TSS_Deep_Initialize)
2317 then
2318 declare
2319 Actual : Node_Id;
2320 Formal : Node_Id;
2322 begin
2323 Actual := First (Parameter_Associations (N));
2324 Formal := First_Formal (Subp);
2325 while Present (Actual)
2326 and then Present (Formal)
2327 loop
2328 if Nkind (Actual) = N_Identifier
2329 and then Is_Controlling_Actual (Actual)
2330 and then Etype (Actual) = Etype (Formal)
2331 then
2332 return True;
2333 end if;
2335 Next (Actual);
2336 Next_Formal (Formal);
2337 end loop;
2338 end;
2339 end if;
2341 return False;
2342 end Is_Direct_Deep_Call;
2344 ---------------
2345 -- New_Value --
2346 ---------------
2348 function New_Value (From : Node_Id) return Node_Id is
2349 Res : constant Node_Id := Duplicate_Subexpr (From);
2350 begin
2351 if Is_Access_Type (Etype (From)) then
2352 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2353 else
2354 return Res;
2355 end if;
2356 end New_Value;
2358 -- Local variables
2360 Curr_S : constant Entity_Id := Current_Scope;
2361 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2362 Actual : Node_Id;
2363 Formal : Entity_Id;
2364 Orig_Subp : Entity_Id := Empty;
2365 Param_Count : Natural := 0;
2366 Parent_Formal : Entity_Id;
2367 Parent_Subp : Entity_Id;
2368 Scop : Entity_Id;
2369 Subp : Entity_Id;
2371 Prev_Orig : Node_Id;
2372 -- Original node for an actual, which may have been rewritten. If the
2373 -- actual is a function call that has been transformed from a selected
2374 -- component, the original node is unanalyzed. Otherwise, it carries
2375 -- semantic information used to generate additional actuals.
2377 CW_Interface_Formals_Present : Boolean := False;
2379 -- Start of processing for Expand_Call
2381 begin
2382 -- Expand the procedure call if the first actual has a dimension and if
2383 -- the procedure is Put (Ada 2012).
2385 if Ada_Version >= Ada_2012
2386 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2387 and then Present (Parameter_Associations (Call_Node))
2388 then
2389 Expand_Put_Call_With_Symbol (Call_Node);
2390 end if;
2392 -- Ignore if previous error
2394 if Nkind (Call_Node) in N_Has_Etype
2395 and then Etype (Call_Node) = Any_Type
2396 then
2397 return;
2398 end if;
2400 -- Call using access to subprogram with explicit dereference
2402 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2403 Subp := Etype (Name (Call_Node));
2404 Parent_Subp := Empty;
2406 -- Case of call to simple entry, where the Name is a selected component
2407 -- whose prefix is the task, and whose selector name is the entry name
2409 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2410 Subp := Entity (Selector_Name (Name (Call_Node)));
2411 Parent_Subp := Empty;
2413 -- Case of call to member of entry family, where Name is an indexed
2414 -- component, with the prefix being a selected component giving the
2415 -- task and entry family name, and the index being the entry index.
2417 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2418 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2419 Parent_Subp := Empty;
2421 -- Normal case
2423 else
2424 Subp := Entity (Name (Call_Node));
2425 Parent_Subp := Alias (Subp);
2427 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2428 -- if we can tell that the first parameter cannot possibly be null.
2429 -- This improves efficiency by avoiding a run-time test.
2431 -- We do not do this if Raise_Exception_Always does not exist, which
2432 -- can happen in configurable run time profiles which provide only a
2433 -- Raise_Exception.
2435 if Is_RTE (Subp, RE_Raise_Exception)
2436 and then RTE_Available (RE_Raise_Exception_Always)
2437 then
2438 declare
2439 FA : constant Node_Id :=
2440 Original_Node (First_Actual (Call_Node));
2442 begin
2443 -- The case we catch is where the first argument is obtained
2444 -- using the Identity attribute (which must always be
2445 -- non-null).
2447 if Nkind (FA) = N_Attribute_Reference
2448 and then Attribute_Name (FA) = Name_Identity
2449 then
2450 Subp := RTE (RE_Raise_Exception_Always);
2451 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2452 end if;
2453 end;
2454 end if;
2456 if Ekind (Subp) = E_Entry then
2457 Parent_Subp := Empty;
2458 end if;
2459 end if;
2461 -- Detect the following code in System.Finalization_Masters only on
2462 -- .NET/JVM targets:
2464 -- procedure Finalize (Master : in out Finalization_Master) is
2465 -- begin
2466 -- . . .
2467 -- begin
2468 -- Finalize (Curr_Ptr.all);
2470 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2471 -- cannot be named in library or user code, the compiler has to deal
2472 -- with this by transforming the call to Finalize into Deep_Finalize.
2474 if VM_Target /= No_VM
2475 and then Chars (Subp) = Name_Finalize
2476 and then Ekind (Curr_S) = E_Block
2477 and then Ekind (Scope (Curr_S)) = E_Procedure
2478 and then Chars (Scope (Curr_S)) = Name_Finalize
2479 and then Etype (First_Formal (Scope (Curr_S))) =
2480 RTE (RE_Finalization_Master)
2481 then
2482 declare
2483 Deep_Fin : constant Entity_Id :=
2484 Find_Prim_Op (RTE (RE_Root_Controlled),
2485 TSS_Deep_Finalize);
2486 begin
2487 -- Since Root_Controlled is a tagged type, the compiler should
2488 -- always generate Deep_Finalize for it.
2490 pragma Assert (Present (Deep_Fin));
2492 -- Generate:
2493 -- Deep_Finalize (Curr_Ptr.all);
2495 Rewrite (N,
2496 Make_Procedure_Call_Statement (Loc,
2497 Name =>
2498 New_Occurrence_Of (Deep_Fin, Loc),
2499 Parameter_Associations =>
2500 New_Copy_List_Tree (Parameter_Associations (N))));
2502 Analyze (N);
2503 return;
2504 end;
2505 end if;
2507 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2508 -- alternative in an asynchronous select or as an entry call in
2509 -- a conditional or timed select. Check whether the procedure call
2510 -- is a renaming of an entry and rewrite it as an entry call.
2512 if Ada_Version >= Ada_2005
2513 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2514 and then
2515 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2516 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2517 or else
2518 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2519 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2520 then
2521 declare
2522 Ren_Decl : Node_Id;
2523 Ren_Root : Entity_Id := Subp;
2525 begin
2526 -- This may be a chain of renamings, find the root
2528 if Present (Alias (Ren_Root)) then
2529 Ren_Root := Alias (Ren_Root);
2530 end if;
2532 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2533 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2535 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2536 Rewrite (Call_Node,
2537 Make_Entry_Call_Statement (Loc,
2538 Name =>
2539 New_Copy_Tree (Name (Ren_Decl)),
2540 Parameter_Associations =>
2541 New_Copy_List_Tree
2542 (Parameter_Associations (Call_Node))));
2544 return;
2545 end if;
2546 end if;
2547 end;
2548 end if;
2550 -- First step, compute extra actuals, corresponding to any Extra_Formals
2551 -- present. Note that we do not access Extra_Formals directly, instead
2552 -- we simply note the presence of the extra formals as we process the
2553 -- regular formals collecting corresponding actuals in Extra_Actuals.
2555 -- We also generate any required range checks for actuals for in formals
2556 -- as we go through the loop, since this is a convenient place to do it.
2557 -- (Though it seems that this would be better done in Expand_Actuals???)
2559 -- Special case: Thunks must not compute the extra actuals; they must
2560 -- just propagate to the target primitive their extra actuals.
2562 if Is_Thunk (Current_Scope)
2563 and then Thunk_Entity (Current_Scope) = Subp
2564 and then Present (Extra_Formals (Subp))
2565 then
2566 pragma Assert (Present (Extra_Formals (Current_Scope)));
2568 declare
2569 Target_Formal : Entity_Id;
2570 Thunk_Formal : Entity_Id;
2572 begin
2573 Target_Formal := Extra_Formals (Subp);
2574 Thunk_Formal := Extra_Formals (Current_Scope);
2575 while Present (Target_Formal) loop
2576 Add_Extra_Actual
2577 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2579 Target_Formal := Extra_Formal (Target_Formal);
2580 Thunk_Formal := Extra_Formal (Thunk_Formal);
2581 end loop;
2583 while Is_Non_Empty_List (Extra_Actuals) loop
2584 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2585 end loop;
2587 Expand_Actuals (Call_Node, Subp);
2588 return;
2589 end;
2590 end if;
2592 Formal := First_Formal (Subp);
2593 Actual := First_Actual (Call_Node);
2594 Param_Count := 1;
2595 while Present (Formal) loop
2597 -- Generate range check if required
2599 if Do_Range_Check (Actual)
2600 and then Ekind (Formal) = E_In_Parameter
2601 then
2602 Generate_Range_Check
2603 (Actual, Etype (Formal), CE_Range_Check_Failed);
2604 end if;
2606 -- Prepare to examine current entry
2608 Prev := Actual;
2609 Prev_Orig := Original_Node (Prev);
2611 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2612 -- to expand it in a further round.
2614 CW_Interface_Formals_Present :=
2615 CW_Interface_Formals_Present
2616 or else
2617 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2618 and then Is_Interface (Etype (Etype (Formal))))
2619 or else
2620 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2621 and then Is_Interface (Directly_Designated_Type
2622 (Etype (Etype (Formal)))));
2624 -- Create possible extra actual for constrained case. Usually, the
2625 -- extra actual is of the form actual'constrained, but since this
2626 -- attribute is only available for unconstrained records, TRUE is
2627 -- expanded if the type of the formal happens to be constrained (for
2628 -- instance when this procedure is inherited from an unconstrained
2629 -- record to a constrained one) or if the actual has no discriminant
2630 -- (its type is constrained). An exception to this is the case of a
2631 -- private type without discriminants. In this case we pass FALSE
2632 -- because the object has underlying discriminants with defaults.
2634 if Present (Extra_Constrained (Formal)) then
2635 if Ekind (Etype (Prev)) in Private_Kind
2636 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2637 then
2638 Add_Extra_Actual
2639 (New_Occurrence_Of (Standard_False, Loc),
2640 Extra_Constrained (Formal));
2642 elsif Is_Constrained (Etype (Formal))
2643 or else not Has_Discriminants (Etype (Prev))
2644 then
2645 Add_Extra_Actual
2646 (New_Occurrence_Of (Standard_True, Loc),
2647 Extra_Constrained (Formal));
2649 -- Do not produce extra actuals for Unchecked_Union parameters.
2650 -- Jump directly to the end of the loop.
2652 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2653 goto Skip_Extra_Actual_Generation;
2655 else
2656 -- If the actual is a type conversion, then the constrained
2657 -- test applies to the actual, not the target type.
2659 declare
2660 Act_Prev : Node_Id;
2662 begin
2663 -- Test for unchecked conversions as well, which can occur
2664 -- as out parameter actuals on calls to stream procedures.
2666 Act_Prev := Prev;
2667 while Nkind_In (Act_Prev, N_Type_Conversion,
2668 N_Unchecked_Type_Conversion)
2669 loop
2670 Act_Prev := Expression (Act_Prev);
2671 end loop;
2673 -- If the expression is a conversion of a dereference, this
2674 -- is internally generated code that manipulates addresses,
2675 -- e.g. when building interface tables. No check should
2676 -- occur in this case, and the discriminated object is not
2677 -- directly a hand.
2679 if not Comes_From_Source (Actual)
2680 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2681 and then Nkind (Act_Prev) = N_Explicit_Dereference
2682 then
2683 Add_Extra_Actual
2684 (New_Occurrence_Of (Standard_False, Loc),
2685 Extra_Constrained (Formal));
2687 else
2688 Add_Extra_Actual
2689 (Make_Attribute_Reference (Sloc (Prev),
2690 Prefix =>
2691 Duplicate_Subexpr_No_Checks
2692 (Act_Prev, Name_Req => True),
2693 Attribute_Name => Name_Constrained),
2694 Extra_Constrained (Formal));
2695 end if;
2696 end;
2697 end if;
2698 end if;
2700 -- Create possible extra actual for accessibility level
2702 if Present (Extra_Accessibility (Formal)) then
2704 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2705 -- attribute, then the original actual may be an aliased object
2706 -- occurring as the prefix in a call using "Object.Operation"
2707 -- notation. In that case we must pass the level of the object,
2708 -- so Prev_Orig is reset to Prev and the attribute will be
2709 -- processed by the code for Access attributes further below.
2711 if Prev_Orig /= Prev
2712 and then Nkind (Prev) = N_Attribute_Reference
2713 and then
2714 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2715 and then Is_Aliased_View (Prev_Orig)
2716 then
2717 Prev_Orig := Prev;
2718 end if;
2720 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2721 -- accessibility levels.
2723 if Is_Thunk (Current_Scope) then
2724 declare
2725 Parm_Ent : Entity_Id;
2727 begin
2728 if Is_Controlling_Actual (Actual) then
2730 -- Find the corresponding actual of the thunk
2732 Parm_Ent := First_Entity (Current_Scope);
2733 for J in 2 .. Param_Count loop
2734 Next_Entity (Parm_Ent);
2735 end loop;
2737 -- Handle unchecked conversion of access types generated
2738 -- in thunks (cf. Expand_Interface_Thunk).
2740 elsif Is_Access_Type (Etype (Actual))
2741 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2742 then
2743 Parm_Ent := Entity (Expression (Actual));
2745 else pragma Assert (Is_Entity_Name (Actual));
2746 Parm_Ent := Entity (Actual);
2747 end if;
2749 Add_Extra_Actual
2750 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2751 Extra_Accessibility (Formal));
2752 end;
2754 elsif Is_Entity_Name (Prev_Orig) then
2756 -- When passing an access parameter, or a renaming of an access
2757 -- parameter, as the actual to another access parameter we need
2758 -- to pass along the actual's own access level parameter. This
2759 -- is done if we are within the scope of the formal access
2760 -- parameter (if this is an inlined body the extra formal is
2761 -- irrelevant).
2763 if (Is_Formal (Entity (Prev_Orig))
2764 or else
2765 (Present (Renamed_Object (Entity (Prev_Orig)))
2766 and then
2767 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2768 and then
2769 Is_Formal
2770 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2771 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2772 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2773 then
2774 declare
2775 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2777 begin
2778 pragma Assert (Present (Parm_Ent));
2780 if Present (Extra_Accessibility (Parm_Ent)) then
2781 Add_Extra_Actual
2782 (New_Occurrence_Of
2783 (Extra_Accessibility (Parm_Ent), Loc),
2784 Extra_Accessibility (Formal));
2786 -- If the actual access parameter does not have an
2787 -- associated extra formal providing its scope level,
2788 -- then treat the actual as having library-level
2789 -- accessibility.
2791 else
2792 Add_Extra_Actual
2793 (Make_Integer_Literal (Loc,
2794 Intval => Scope_Depth (Standard_Standard)),
2795 Extra_Accessibility (Formal));
2796 end if;
2797 end;
2799 -- The actual is a normal access value, so just pass the level
2800 -- of the actual's access type.
2802 else
2803 Add_Extra_Actual
2804 (Dynamic_Accessibility_Level (Prev_Orig),
2805 Extra_Accessibility (Formal));
2806 end if;
2808 -- If the actual is an access discriminant, then pass the level
2809 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2811 elsif Nkind (Prev_Orig) = N_Selected_Component
2812 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2813 E_Discriminant
2814 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2815 E_Anonymous_Access_Type
2816 then
2817 Add_Extra_Actual
2818 (Make_Integer_Literal (Loc,
2819 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2820 Extra_Accessibility (Formal));
2822 -- All other cases
2824 else
2825 case Nkind (Prev_Orig) is
2827 when N_Attribute_Reference =>
2828 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2830 -- For X'Access, pass on the level of the prefix X
2832 when Attribute_Access =>
2834 -- If this is an Access attribute applied to the
2835 -- the current instance object passed to a type
2836 -- initialization procedure, then use the level
2837 -- of the type itself. This is not really correct,
2838 -- as there should be an extra level parameter
2839 -- passed in with _init formals (only in the case
2840 -- where the type is immutably limited), but we
2841 -- don't have an easy way currently to create such
2842 -- an extra formal (init procs aren't ever frozen).
2843 -- For now we just use the level of the type,
2844 -- which may be too shallow, but that works better
2845 -- than passing Object_Access_Level of the type,
2846 -- which can be one level too deep in some cases.
2847 -- ???
2849 if Is_Entity_Name (Prefix (Prev_Orig))
2850 and then Is_Type (Entity (Prefix (Prev_Orig)))
2851 then
2852 Add_Extra_Actual
2853 (Make_Integer_Literal (Loc,
2854 Intval =>
2855 Type_Access_Level
2856 (Entity (Prefix (Prev_Orig)))),
2857 Extra_Accessibility (Formal));
2859 else
2860 Add_Extra_Actual
2861 (Make_Integer_Literal (Loc,
2862 Intval =>
2863 Object_Access_Level
2864 (Prefix (Prev_Orig))),
2865 Extra_Accessibility (Formal));
2866 end if;
2868 -- Treat the unchecked attributes as library-level
2870 when Attribute_Unchecked_Access |
2871 Attribute_Unrestricted_Access =>
2872 Add_Extra_Actual
2873 (Make_Integer_Literal (Loc,
2874 Intval => Scope_Depth (Standard_Standard)),
2875 Extra_Accessibility (Formal));
2877 -- No other cases of attributes returning access
2878 -- values that can be passed to access parameters.
2880 when others =>
2881 raise Program_Error;
2883 end case;
2885 -- For allocators we pass the level of the execution of the
2886 -- called subprogram, which is one greater than the current
2887 -- scope level.
2889 when N_Allocator =>
2890 Add_Extra_Actual
2891 (Make_Integer_Literal (Loc,
2892 Intval => Scope_Depth (Current_Scope) + 1),
2893 Extra_Accessibility (Formal));
2895 -- For most other cases we simply pass the level of the
2896 -- actual's access type. The type is retrieved from
2897 -- Prev rather than Prev_Orig, because in some cases
2898 -- Prev_Orig denotes an original expression that has
2899 -- not been analyzed.
2901 when others =>
2902 Add_Extra_Actual
2903 (Dynamic_Accessibility_Level (Prev),
2904 Extra_Accessibility (Formal));
2905 end case;
2906 end if;
2907 end if;
2909 -- Perform the check of 4.6(49) that prevents a null value from being
2910 -- passed as an actual to an access parameter. Note that the check
2911 -- is elided in the common cases of passing an access attribute or
2912 -- access parameter as an actual. Also, we currently don't enforce
2913 -- this check for expander-generated actuals and when -gnatdj is set.
2915 if Ada_Version >= Ada_2005 then
2917 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2918 -- the intent of 6.4.1(13) is that null-exclusion checks should
2919 -- not be done for 'out' parameters, even though it refers only
2920 -- to constraint checks, and a null_exclusion is not a constraint.
2921 -- Note that AI05-0196-1 corrects this mistake in the RM.
2923 if Is_Access_Type (Etype (Formal))
2924 and then Can_Never_Be_Null (Etype (Formal))
2925 and then Ekind (Formal) /= E_Out_Parameter
2926 and then Nkind (Prev) /= N_Raise_Constraint_Error
2927 and then (Known_Null (Prev)
2928 or else not Can_Never_Be_Null (Etype (Prev)))
2929 then
2930 Install_Null_Excluding_Check (Prev);
2931 end if;
2933 -- Ada_Version < Ada_2005
2935 else
2936 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2937 or else Access_Checks_Suppressed (Subp)
2938 then
2939 null;
2941 elsif Debug_Flag_J then
2942 null;
2944 elsif not Comes_From_Source (Prev) then
2945 null;
2947 elsif Is_Entity_Name (Prev)
2948 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2949 then
2950 null;
2952 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2953 null;
2955 -- Suppress null checks when passing to access parameters of Java
2956 -- and CIL subprograms. (Should this be done for other foreign
2957 -- conventions as well ???)
2959 elsif Convention (Subp) = Convention_Java
2960 or else Convention (Subp) = Convention_CIL
2961 then
2962 null;
2964 else
2965 Install_Null_Excluding_Check (Prev);
2966 end if;
2967 end if;
2969 -- Perform appropriate validity checks on parameters that
2970 -- are entities.
2972 if Validity_Checks_On then
2973 if (Ekind (Formal) = E_In_Parameter
2974 and then Validity_Check_In_Params)
2975 or else
2976 (Ekind (Formal) = E_In_Out_Parameter
2977 and then Validity_Check_In_Out_Params)
2978 then
2979 -- If the actual is an indexed component of a packed type (or
2980 -- is an indexed or selected component whose prefix recursively
2981 -- meets this condition), it has not been expanded yet. It will
2982 -- be copied in the validity code that follows, and has to be
2983 -- expanded appropriately, so reanalyze it.
2985 -- What we do is just to unset analyzed bits on prefixes till
2986 -- we reach something that does not have a prefix.
2988 declare
2989 Nod : Node_Id;
2991 begin
2992 Nod := Actual;
2993 while Nkind_In (Nod, N_Indexed_Component,
2994 N_Selected_Component)
2995 loop
2996 Set_Analyzed (Nod, False);
2997 Nod := Prefix (Nod);
2998 end loop;
2999 end;
3001 Ensure_Valid (Actual);
3002 end if;
3003 end if;
3005 -- For IN OUT and OUT parameters, ensure that subscripts are valid
3006 -- since this is a left side reference. We only do this for calls
3007 -- from the source program since we assume that compiler generated
3008 -- calls explicitly generate any required checks. We also need it
3009 -- only if we are doing standard validity checks, since clearly it is
3010 -- not needed if validity checks are off, and in subscript validity
3011 -- checking mode, all indexed components are checked with a call
3012 -- directly from Expand_N_Indexed_Component.
3014 if Comes_From_Source (Call_Node)
3015 and then Ekind (Formal) /= E_In_Parameter
3016 and then Validity_Checks_On
3017 and then Validity_Check_Default
3018 and then not Validity_Check_Subscripts
3019 then
3020 Check_Valid_Lvalue_Subscripts (Actual);
3021 end if;
3023 -- Mark any scalar OUT parameter that is a simple variable as no
3024 -- longer known to be valid (unless the type is always valid). This
3025 -- reflects the fact that if an OUT parameter is never set in a
3026 -- procedure, then it can become invalid on the procedure return.
3028 if Ekind (Formal) = E_Out_Parameter
3029 and then Is_Entity_Name (Actual)
3030 and then Ekind (Entity (Actual)) = E_Variable
3031 and then not Is_Known_Valid (Etype (Actual))
3032 then
3033 Set_Is_Known_Valid (Entity (Actual), False);
3034 end if;
3036 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3037 -- clear current values, since they can be clobbered. We are probably
3038 -- doing this in more places than we need to, but better safe than
3039 -- sorry when it comes to retaining bad current values.
3041 if Ekind (Formal) /= E_In_Parameter
3042 and then Is_Entity_Name (Actual)
3043 and then Present (Entity (Actual))
3044 then
3045 declare
3046 Ent : constant Entity_Id := Entity (Actual);
3047 Sav : Node_Id;
3049 begin
3050 -- For an OUT or IN OUT parameter that is an assignable entity,
3051 -- we do not want to clobber the Last_Assignment field, since
3052 -- if it is set, it was precisely because it is indeed an OUT
3053 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3054 -- since the subprogram could have returned in invalid value.
3056 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3057 and then Is_Assignable (Ent)
3058 then
3059 Sav := Last_Assignment (Ent);
3060 Kill_Current_Values (Ent);
3061 Set_Last_Assignment (Ent, Sav);
3062 Set_Is_Known_Valid (Ent, False);
3064 -- For all other cases, just kill the current values
3066 else
3067 Kill_Current_Values (Ent);
3068 end if;
3069 end;
3070 end if;
3072 -- If the formal is class wide and the actual is an aggregate, force
3073 -- evaluation so that the back end who does not know about class-wide
3074 -- type, does not generate a temporary of the wrong size.
3076 if not Is_Class_Wide_Type (Etype (Formal)) then
3077 null;
3079 elsif Nkind (Actual) = N_Aggregate
3080 or else (Nkind (Actual) = N_Qualified_Expression
3081 and then Nkind (Expression (Actual)) = N_Aggregate)
3082 then
3083 Force_Evaluation (Actual);
3084 end if;
3086 -- In a remote call, if the formal is of a class-wide type, check
3087 -- that the actual meets the requirements described in E.4(18).
3089 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3090 Insert_Action (Actual,
3091 Make_Transportable_Check (Loc,
3092 Duplicate_Subexpr_Move_Checks (Actual)));
3093 end if;
3095 -- This label is required when skipping extra actual generation for
3096 -- Unchecked_Union parameters.
3098 <<Skip_Extra_Actual_Generation>>
3100 Param_Count := Param_Count + 1;
3101 Next_Actual (Actual);
3102 Next_Formal (Formal);
3103 end loop;
3105 -- If we are calling an Ada 2012 function which needs to have the
3106 -- "accessibility level determined by the point of call" (AI05-0234)
3107 -- passed in to it, then pass it in.
3109 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3110 and then
3111 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3112 then
3113 declare
3114 Ancestor : Node_Id := Parent (Call_Node);
3115 Level : Node_Id := Empty;
3116 Defer : Boolean := False;
3118 begin
3119 -- Unimplemented: if Subp returns an anonymous access type, then
3121 -- a) if the call is the operand of an explict conversion, then
3122 -- the target type of the conversion (a named access type)
3123 -- determines the accessibility level pass in;
3125 -- b) if the call defines an access discriminant of an object
3126 -- (e.g., the discriminant of an object being created by an
3127 -- allocator, or the discriminant of a function result),
3128 -- then the accessibility level to pass in is that of the
3129 -- discriminated object being initialized).
3131 -- ???
3133 while Nkind (Ancestor) = N_Qualified_Expression
3134 loop
3135 Ancestor := Parent (Ancestor);
3136 end loop;
3138 case Nkind (Ancestor) is
3139 when N_Allocator =>
3141 -- At this point, we'd like to assign
3143 -- Level := Dynamic_Accessibility_Level (Ancestor);
3145 -- but Etype of Ancestor may not have been set yet,
3146 -- so that doesn't work.
3148 -- Handle this later in Expand_Allocator_Expression.
3150 Defer := True;
3152 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3153 declare
3154 Def_Id : constant Entity_Id :=
3155 Defining_Identifier (Ancestor);
3157 begin
3158 if Is_Return_Object (Def_Id) then
3159 if Present (Extra_Accessibility_Of_Result
3160 (Return_Applies_To (Scope (Def_Id))))
3161 then
3162 -- Pass along value that was passed in if the
3163 -- routine we are returning from also has an
3164 -- Accessibility_Of_Result formal.
3166 Level :=
3167 New_Occurrence_Of
3168 (Extra_Accessibility_Of_Result
3169 (Return_Applies_To (Scope (Def_Id))), Loc);
3170 end if;
3171 else
3172 Level :=
3173 Make_Integer_Literal (Loc,
3174 Intval => Object_Access_Level (Def_Id));
3175 end if;
3176 end;
3178 when N_Simple_Return_Statement =>
3179 if Present (Extra_Accessibility_Of_Result
3180 (Return_Applies_To
3181 (Return_Statement_Entity (Ancestor))))
3182 then
3183 -- Pass along value that was passed in if the returned
3184 -- routine also has an Accessibility_Of_Result formal.
3186 Level :=
3187 New_Occurrence_Of
3188 (Extra_Accessibility_Of_Result
3189 (Return_Applies_To
3190 (Return_Statement_Entity (Ancestor))), Loc);
3191 end if;
3193 when others =>
3194 null;
3195 end case;
3197 if not Defer then
3198 if not Present (Level) then
3200 -- The "innermost master that evaluates the function call".
3202 -- ??? - Should we use Integer'Last here instead in order
3203 -- to deal with (some of) the problems associated with
3204 -- calls to subps whose enclosing scope is unknown (e.g.,
3205 -- Anon_Access_To_Subp_Param.all)?
3207 Level := Make_Integer_Literal (Loc,
3208 Scope_Depth (Current_Scope) + 1);
3209 end if;
3211 Add_Extra_Actual
3212 (Level,
3213 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3214 end if;
3215 end;
3216 end if;
3218 -- If we are expanding the RHS of an assignment we need to check if tag
3219 -- propagation is needed. You might expect this processing to be in
3220 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3221 -- assignment might be transformed to a declaration for an unconstrained
3222 -- value if the expression is classwide.
3224 if Nkind (Call_Node) = N_Function_Call
3225 and then Is_Tag_Indeterminate (Call_Node)
3226 and then Is_Entity_Name (Name (Call_Node))
3227 then
3228 declare
3229 Ass : Node_Id := Empty;
3231 begin
3232 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3233 Ass := Parent (Call_Node);
3235 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3236 and then Nkind (Parent (Parent (Call_Node))) =
3237 N_Assignment_Statement
3238 then
3239 Ass := Parent (Parent (Call_Node));
3241 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3242 and then Nkind (Parent (Parent (Call_Node))) =
3243 N_Assignment_Statement
3244 then
3245 Ass := Parent (Parent (Call_Node));
3246 end if;
3248 if Present (Ass)
3249 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3250 then
3251 if Is_Access_Type (Etype (Call_Node)) then
3252 if Designated_Type (Etype (Call_Node)) /=
3253 Root_Type (Etype (Name (Ass)))
3254 then
3255 Error_Msg_NE
3256 ("tag-indeterminate expression "
3257 & " must have designated type& (RM 5.2 (6))",
3258 Call_Node, Root_Type (Etype (Name (Ass))));
3259 else
3260 Propagate_Tag (Name (Ass), Call_Node);
3261 end if;
3263 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3264 Error_Msg_NE
3265 ("tag-indeterminate expression must have type&"
3266 & "(RM 5.2 (6))",
3267 Call_Node, Root_Type (Etype (Name (Ass))));
3269 else
3270 Propagate_Tag (Name (Ass), Call_Node);
3271 end if;
3273 -- The call will be rewritten as a dispatching call, and
3274 -- expanded as such.
3276 return;
3277 end if;
3278 end;
3279 end if;
3281 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3282 -- it to point to the correct secondary virtual table
3284 if Nkind (Call_Node) in N_Subprogram_Call
3285 and then CW_Interface_Formals_Present
3286 then
3287 Expand_Interface_Actuals (Call_Node);
3288 end if;
3290 -- Deals with Dispatch_Call if we still have a call, before expanding
3291 -- extra actuals since this will be done on the re-analysis of the
3292 -- dispatching call. Note that we do not try to shorten the actual list
3293 -- for a dispatching call, it would not make sense to do so. Expansion
3294 -- of dispatching calls is suppressed when VM_Target, because the VM
3295 -- back-ends directly handle the generation of dispatching calls and
3296 -- would have to undo any expansion to an indirect call.
3298 if Nkind (Call_Node) in N_Subprogram_Call
3299 and then Present (Controlling_Argument (Call_Node))
3300 then
3301 declare
3302 Call_Typ : constant Entity_Id := Etype (Call_Node);
3303 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3304 Eq_Prim_Op : Entity_Id := Empty;
3305 New_Call : Node_Id;
3306 Param : Node_Id;
3307 Prev_Call : Node_Id;
3309 begin
3310 if not Is_Limited_Type (Typ) then
3311 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3312 end if;
3314 if Tagged_Type_Expansion then
3315 Expand_Dispatching_Call (Call_Node);
3317 -- The following return is worrisome. Is it really OK to skip
3318 -- all remaining processing in this procedure ???
3320 return;
3322 -- VM targets
3324 else
3325 Apply_Tag_Checks (Call_Node);
3327 -- If this is a dispatching "=", we must first compare the
3328 -- tags so we generate: x.tag = y.tag and then x = y
3330 if Subp = Eq_Prim_Op then
3332 -- Mark the node as analyzed to avoid reanalizing this
3333 -- dispatching call (which would cause a never-ending loop)
3335 Prev_Call := Relocate_Node (Call_Node);
3336 Set_Analyzed (Prev_Call);
3338 Param := First_Actual (Call_Node);
3339 New_Call :=
3340 Make_And_Then (Loc,
3341 Left_Opnd =>
3342 Make_Op_Eq (Loc,
3343 Left_Opnd =>
3344 Make_Selected_Component (Loc,
3345 Prefix => New_Value (Param),
3346 Selector_Name =>
3347 New_Occurrence_Of
3348 (First_Tag_Component (Typ), Loc)),
3350 Right_Opnd =>
3351 Make_Selected_Component (Loc,
3352 Prefix =>
3353 Unchecked_Convert_To (Typ,
3354 New_Value (Next_Actual (Param))),
3355 Selector_Name =>
3356 New_Occurrence_Of
3357 (First_Tag_Component (Typ), Loc))),
3358 Right_Opnd => Prev_Call);
3360 Rewrite (Call_Node, New_Call);
3362 Analyze_And_Resolve
3363 (Call_Node, Call_Typ, Suppress => All_Checks);
3364 end if;
3366 -- Expansion of a dispatching call results in an indirect call,
3367 -- which in turn causes current values to be killed (see
3368 -- Resolve_Call), so on VM targets we do the call here to
3369 -- ensure consistent warnings between VM and non-VM targets.
3371 Kill_Current_Values;
3372 end if;
3374 -- If this is a dispatching "=" then we must update the reference
3375 -- to the call node because we generated:
3376 -- x.tag = y.tag and then x = y
3378 if Subp = Eq_Prim_Op then
3379 Call_Node := Right_Opnd (Call_Node);
3380 end if;
3381 end;
3382 end if;
3384 -- Similarly, expand calls to RCI subprograms on which pragma
3385 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3386 -- later. Do this only when the call comes from source since we
3387 -- do not want such a rewriting to occur in expanded code.
3389 if Is_All_Remote_Call (Call_Node) then
3390 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3392 -- Similarly, do not add extra actuals for an entry call whose entity
3393 -- is a protected procedure, or for an internal protected subprogram
3394 -- call, because it will be rewritten as a protected subprogram call
3395 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3397 elsif Is_Protected_Type (Scope (Subp))
3398 and then (Ekind (Subp) = E_Procedure
3399 or else Ekind (Subp) = E_Function)
3400 then
3401 null;
3403 -- During that loop we gathered the extra actuals (the ones that
3404 -- correspond to Extra_Formals), so now they can be appended.
3406 else
3407 while Is_Non_Empty_List (Extra_Actuals) loop
3408 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3409 end loop;
3410 end if;
3412 -- At this point we have all the actuals, so this is the point at which
3413 -- the various expansion activities for actuals is carried out.
3415 Expand_Actuals (Call_Node, Subp);
3417 -- Verify that the actuals do not share storage. This check must be done
3418 -- on the caller side rather that inside the subprogram to avoid issues
3419 -- of parameter passing.
3421 if Check_Aliasing_Of_Parameters then
3422 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3423 end if;
3425 -- If the subprogram is a renaming, or if it is inherited, replace it in
3426 -- the call with the name of the actual subprogram being called. If this
3427 -- is a dispatching call, the run-time decides what to call. The Alias
3428 -- attribute does not apply to entries.
3430 if Nkind (Call_Node) /= N_Entry_Call_Statement
3431 and then No (Controlling_Argument (Call_Node))
3432 and then Present (Parent_Subp)
3433 and then not Is_Direct_Deep_Call (Subp)
3434 then
3435 if Present (Inherited_From_Formal (Subp)) then
3436 Parent_Subp := Inherited_From_Formal (Subp);
3437 else
3438 Parent_Subp := Ultimate_Alias (Parent_Subp);
3439 end if;
3441 -- The below setting of Entity is suspect, see F109-018 discussion???
3443 Set_Entity (Name (Call_Node), Parent_Subp);
3445 if Is_Abstract_Subprogram (Parent_Subp)
3446 and then not In_Instance
3447 then
3448 Error_Msg_NE
3449 ("cannot call abstract subprogram &!",
3450 Name (Call_Node), Parent_Subp);
3451 end if;
3453 -- Inspect all formals of derived subprogram Subp. Compare parameter
3454 -- types with the parent subprogram and check whether an actual may
3455 -- need a type conversion to the corresponding formal of the parent
3456 -- subprogram.
3458 -- Not clear whether intrinsic subprograms need such conversions. ???
3460 if not Is_Intrinsic_Subprogram (Parent_Subp)
3461 or else Is_Generic_Instance (Parent_Subp)
3462 then
3463 declare
3464 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3465 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3466 -- and resolve the newly generated construct.
3468 -------------
3469 -- Convert --
3470 -------------
3472 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3473 begin
3474 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3475 Analyze (Act);
3476 Resolve (Act, Typ);
3477 end Convert;
3479 -- Local variables
3481 Actual_Typ : Entity_Id;
3482 Formal_Typ : Entity_Id;
3483 Parent_Typ : Entity_Id;
3485 begin
3486 Actual := First_Actual (Call_Node);
3487 Formal := First_Formal (Subp);
3488 Parent_Formal := First_Formal (Parent_Subp);
3489 while Present (Formal) loop
3490 Actual_Typ := Etype (Actual);
3491 Formal_Typ := Etype (Formal);
3492 Parent_Typ := Etype (Parent_Formal);
3494 -- For an IN parameter of a scalar type, the parent formal
3495 -- type and derived formal type differ or the parent formal
3496 -- type and actual type do not match statically.
3498 if Is_Scalar_Type (Formal_Typ)
3499 and then Ekind (Formal) = E_In_Parameter
3500 and then Formal_Typ /= Parent_Typ
3501 and then
3502 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3503 and then not Raises_Constraint_Error (Actual)
3504 then
3505 Convert (Actual, Parent_Typ);
3506 Enable_Range_Check (Actual);
3508 -- If the actual has been marked as requiring a range
3509 -- check, then generate it here.
3511 if Do_Range_Check (Actual) then
3512 Generate_Range_Check
3513 (Actual, Etype (Formal), CE_Range_Check_Failed);
3514 end if;
3516 -- For access types, the parent formal type and actual type
3517 -- differ.
3519 elsif Is_Access_Type (Formal_Typ)
3520 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3521 then
3522 if Ekind (Formal) /= E_In_Parameter then
3523 Convert (Actual, Parent_Typ);
3525 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3526 and then Designated_Type (Parent_Typ) /=
3527 Designated_Type (Actual_Typ)
3528 and then not Is_Controlling_Formal (Formal)
3529 then
3530 -- This unchecked conversion is not necessary unless
3531 -- inlining is enabled, because in that case the type
3532 -- mismatch may become visible in the body about to be
3533 -- inlined.
3535 Rewrite (Actual,
3536 Unchecked_Convert_To (Parent_Typ,
3537 Relocate_Node (Actual)));
3538 Analyze (Actual);
3539 Resolve (Actual, Parent_Typ);
3540 end if;
3542 -- If there is a change of representation, then generate a
3543 -- warning, and do the change of representation.
3545 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3546 Error_Msg_N
3547 ("??change of representation required", Actual);
3548 Convert (Actual, Parent_Typ);
3550 -- For array and record types, the parent formal type and
3551 -- derived formal type have different sizes or pragma Pack
3552 -- status.
3554 elsif ((Is_Array_Type (Formal_Typ)
3555 and then Is_Array_Type (Parent_Typ))
3556 or else
3557 (Is_Record_Type (Formal_Typ)
3558 and then Is_Record_Type (Parent_Typ)))
3559 and then
3560 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3561 or else Has_Pragma_Pack (Formal_Typ) /=
3562 Has_Pragma_Pack (Parent_Typ))
3563 then
3564 Convert (Actual, Parent_Typ);
3565 end if;
3567 Next_Actual (Actual);
3568 Next_Formal (Formal);
3569 Next_Formal (Parent_Formal);
3570 end loop;
3571 end;
3572 end if;
3574 Orig_Subp := Subp;
3575 Subp := Parent_Subp;
3576 end if;
3578 -- Deal with case where call is an explicit dereference
3580 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3582 -- Handle case of access to protected subprogram type
3584 if Is_Access_Protected_Subprogram_Type
3585 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3586 then
3587 -- If this is a call through an access to protected operation, the
3588 -- prefix has the form (object'address, operation'access). Rewrite
3589 -- as a for other protected calls: the object is the 1st parameter
3590 -- of the list of actuals.
3592 declare
3593 Call : Node_Id;
3594 Parm : List_Id;
3595 Nam : Node_Id;
3596 Obj : Node_Id;
3597 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3599 T : constant Entity_Id :=
3600 Equivalent_Type (Base_Type (Etype (Ptr)));
3602 D_T : constant Entity_Id :=
3603 Designated_Type (Base_Type (Etype (Ptr)));
3605 begin
3606 Obj :=
3607 Make_Selected_Component (Loc,
3608 Prefix => Unchecked_Convert_To (T, Ptr),
3609 Selector_Name =>
3610 New_Occurrence_Of (First_Entity (T), Loc));
3612 Nam :=
3613 Make_Selected_Component (Loc,
3614 Prefix => Unchecked_Convert_To (T, Ptr),
3615 Selector_Name =>
3616 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3618 Nam :=
3619 Make_Explicit_Dereference (Loc,
3620 Prefix => Nam);
3622 if Present (Parameter_Associations (Call_Node)) then
3623 Parm := Parameter_Associations (Call_Node);
3624 else
3625 Parm := New_List;
3626 end if;
3628 Prepend (Obj, Parm);
3630 if Etype (D_T) = Standard_Void_Type then
3631 Call :=
3632 Make_Procedure_Call_Statement (Loc,
3633 Name => Nam,
3634 Parameter_Associations => Parm);
3635 else
3636 Call :=
3637 Make_Function_Call (Loc,
3638 Name => Nam,
3639 Parameter_Associations => Parm);
3640 end if;
3642 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3643 Set_Etype (Call, Etype (D_T));
3645 -- We do not re-analyze the call to avoid infinite recursion.
3646 -- We analyze separately the prefix and the object, and set
3647 -- the checks on the prefix that would otherwise be emitted
3648 -- when resolving a call.
3650 Rewrite (Call_Node, Call);
3651 Analyze (Nam);
3652 Apply_Access_Check (Nam);
3653 Analyze (Obj);
3654 return;
3655 end;
3656 end if;
3657 end if;
3659 -- If this is a call to an intrinsic subprogram, then perform the
3660 -- appropriate expansion to the corresponding tree node and we
3661 -- are all done (since after that the call is gone).
3663 -- In the case where the intrinsic is to be processed by the back end,
3664 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3665 -- since the idea in this case is to pass the call unchanged. If the
3666 -- intrinsic is an inherited unchecked conversion, and the derived type
3667 -- is the target type of the conversion, we must retain it as the return
3668 -- type of the expression. Otherwise the expansion below, which uses the
3669 -- parent operation, will yield the wrong type.
3671 if Is_Intrinsic_Subprogram (Subp) then
3672 Expand_Intrinsic_Call (Call_Node, Subp);
3674 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3675 and then Parent_Subp /= Orig_Subp
3676 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3677 then
3678 Set_Etype (Call_Node, Etype (Orig_Subp));
3679 end if;
3681 return;
3682 end if;
3684 if Ekind_In (Subp, E_Function, E_Procedure) then
3686 -- We perform two simple optimization on calls:
3688 -- a) replace calls to null procedures unconditionally;
3690 -- b) for To_Address, just do an unchecked conversion. Not only is
3691 -- this efficient, but it also avoids order of elaboration problems
3692 -- when address clauses are inlined (address expression elaborated
3693 -- at the wrong point).
3695 -- We perform these optimization regardless of whether we are in the
3696 -- main unit or in a unit in the context of the main unit, to ensure
3697 -- that tree generated is the same in both cases, for CodePeer use.
3699 if Is_RTE (Subp, RE_To_Address) then
3700 Rewrite (Call_Node,
3701 Unchecked_Convert_To
3702 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3703 return;
3705 elsif Is_Null_Procedure (Subp) then
3706 Rewrite (Call_Node, Make_Null_Statement (Loc));
3707 return;
3708 end if;
3710 -- Handle inlining. No action needed if the subprogram is not inlined
3712 if not Is_Inlined (Subp) then
3713 null;
3715 -- Handle frontend inlining
3717 elsif not Back_End_Inlining then
3718 Inlined_Subprogram : declare
3719 Bod : Node_Id;
3720 Must_Inline : Boolean := False;
3721 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3723 begin
3724 -- Verify that the body to inline has already been seen, and
3725 -- that if the body is in the current unit the inlining does
3726 -- not occur earlier. This avoids order-of-elaboration problems
3727 -- in the back end.
3729 -- This should be documented in sinfo/einfo ???
3731 if No (Spec)
3732 or else Nkind (Spec) /= N_Subprogram_Declaration
3733 or else No (Body_To_Inline (Spec))
3734 then
3735 Must_Inline := False;
3737 -- If this an inherited function that returns a private type,
3738 -- do not inline if the full view is an unconstrained array,
3739 -- because such calls cannot be inlined.
3741 elsif Present (Orig_Subp)
3742 and then Is_Array_Type (Etype (Orig_Subp))
3743 and then not Is_Constrained (Etype (Orig_Subp))
3744 then
3745 Must_Inline := False;
3747 elsif In_Unfrozen_Instance (Scope (Subp)) then
3748 Must_Inline := False;
3750 else
3751 Bod := Body_To_Inline (Spec);
3753 if (In_Extended_Main_Code_Unit (Call_Node)
3754 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3755 or else Has_Pragma_Inline_Always (Subp))
3756 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3757 or else
3758 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3759 then
3760 Must_Inline := True;
3762 -- If we are compiling a package body that is not the main
3763 -- unit, it must be for inlining/instantiation purposes,
3764 -- in which case we inline the call to insure that the same
3765 -- temporaries are generated when compiling the body by
3766 -- itself. Otherwise link errors can occur.
3768 -- If the function being called is itself in the main unit,
3769 -- we cannot inline, because there is a risk of double
3770 -- elaboration and/or circularity: the inlining can make
3771 -- visible a private entity in the body of the main unit,
3772 -- that gigi will see before its sees its proper definition.
3774 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3775 and then In_Package_Body
3776 then
3777 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3778 end if;
3779 end if;
3781 if Must_Inline then
3782 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3784 else
3785 -- Let the back end handle it
3787 Add_Inlined_Body (Subp, Call_Node);
3789 if Front_End_Inlining
3790 and then Nkind (Spec) = N_Subprogram_Declaration
3791 and then (In_Extended_Main_Code_Unit (Call_Node))
3792 and then No (Body_To_Inline (Spec))
3793 and then not Has_Completion (Subp)
3794 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3795 then
3796 Cannot_Inline
3797 ("cannot inline& (body not seen yet)?",
3798 Call_Node, Subp);
3799 end if;
3800 end if;
3801 end Inlined_Subprogram;
3803 -- Back end inlining: let the back end handle it
3805 elsif No (Unit_Declaration_Node (Subp))
3806 or else Nkind (Unit_Declaration_Node (Subp)) /=
3807 N_Subprogram_Declaration
3808 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
3809 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
3810 N_Entity
3811 then
3812 Add_Inlined_Body (Subp, Call_Node);
3814 -- Front end expansion of simple functions returning unconstrained
3815 -- types (see Check_And_Split_Unconstrained_Function). Note that the
3816 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
3817 -- also Build_Renamed_Body) cannot be expanded here because this may
3818 -- give rise to order-of-elaboration issues for the types of the
3819 -- parameters of the subprogram, if any.
3821 else
3822 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3823 end if;
3824 end if;
3826 -- Check for protected subprogram. This is either an intra-object call,
3827 -- or a protected function call. Protected procedure calls are rewritten
3828 -- as entry calls and handled accordingly.
3830 -- In Ada 2005, this may be an indirect call to an access parameter that
3831 -- is an access_to_subprogram. In that case the anonymous type has a
3832 -- scope that is a protected operation, but the call is a regular one.
3833 -- In either case do not expand call if subprogram is eliminated.
3835 Scop := Scope (Subp);
3837 if Nkind (Call_Node) /= N_Entry_Call_Statement
3838 and then Is_Protected_Type (Scop)
3839 and then Ekind (Subp) /= E_Subprogram_Type
3840 and then not Is_Eliminated (Subp)
3841 then
3842 -- If the call is an internal one, it is rewritten as a call to the
3843 -- corresponding unprotected subprogram.
3845 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3846 end if;
3848 -- Functions returning controlled objects need special attention. If
3849 -- the return type is limited, then the context is initialization and
3850 -- different processing applies. If the call is to a protected function,
3851 -- the expansion above will call Expand_Call recursively. Otherwise the
3852 -- function call is transformed into a temporary which obtains the
3853 -- result from the secondary stack.
3855 if Needs_Finalization (Etype (Subp)) then
3856 if not Is_Limited_View (Etype (Subp))
3857 and then
3858 (No (First_Formal (Subp))
3859 or else
3860 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3861 then
3862 Expand_Ctrl_Function_Call (Call_Node);
3864 -- Build-in-place function calls which appear in anonymous contexts
3865 -- need a transient scope to ensure the proper finalization of the
3866 -- intermediate result after its use.
3868 elsif Is_Build_In_Place_Function_Call (Call_Node)
3869 and then
3870 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3871 N_Function_Call,
3872 N_Indexed_Component,
3873 N_Object_Renaming_Declaration,
3874 N_Procedure_Call_Statement,
3875 N_Selected_Component,
3876 N_Slice)
3877 then
3878 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3879 end if;
3880 end if;
3881 end Expand_Call;
3883 -------------------------------
3884 -- Expand_Ctrl_Function_Call --
3885 -------------------------------
3887 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3888 function Is_Element_Reference (N : Node_Id) return Boolean;
3889 -- Determine whether node N denotes a reference to an Ada 2012 container
3890 -- element.
3892 --------------------------
3893 -- Is_Element_Reference --
3894 --------------------------
3896 function Is_Element_Reference (N : Node_Id) return Boolean is
3897 Ref : constant Node_Id := Original_Node (N);
3899 begin
3900 -- Analysis marks an element reference by setting the generalized
3901 -- indexing attribute of an indexed component before the component
3902 -- is rewritten into a function call.
3904 return
3905 Nkind (Ref) = N_Indexed_Component
3906 and then Present (Generalized_Indexing (Ref));
3907 end Is_Element_Reference;
3909 -- Local variables
3911 Is_Elem_Ref : constant Boolean := Is_Element_Reference (N);
3913 -- Start of processing for Expand_Ctrl_Function_Call
3915 begin
3916 -- Optimization, if the returned value (which is on the sec-stack) is
3917 -- returned again, no need to copy/readjust/finalize, we can just pass
3918 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3919 -- attachment is needed
3921 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3922 return;
3923 end if;
3925 -- Resolution is now finished, make sure we don't start analysis again
3926 -- because of the duplication.
3928 Set_Analyzed (N);
3930 -- A function which returns a controlled object uses the secondary
3931 -- stack. Rewrite the call into a temporary which obtains the result of
3932 -- the function using 'reference.
3934 Remove_Side_Effects (N);
3936 -- When the temporary function result appears inside a case expression
3937 -- or an if expression, its lifetime must be extended to match that of
3938 -- the context. If not, the function result will be finalized too early
3939 -- and the evaluation of the expression could yield incorrect result. An
3940 -- exception to this rule are references to Ada 2012 container elements.
3941 -- Such references must be finalized at the end of each iteration of the
3942 -- related quantified expression, otherwise the container will remain
3943 -- busy.
3945 if not Is_Elem_Ref
3946 and then Within_Case_Or_If_Expression (N)
3947 and then Nkind (N) = N_Explicit_Dereference
3948 then
3949 Set_Is_Processed_Transient (Entity (Prefix (N)));
3950 end if;
3951 end Expand_Ctrl_Function_Call;
3953 ----------------------------------------
3954 -- Expand_N_Extended_Return_Statement --
3955 ----------------------------------------
3957 -- If there is a Handled_Statement_Sequence, we rewrite this:
3959 -- return Result : T := <expression> do
3960 -- <handled_seq_of_stms>
3961 -- end return;
3963 -- to be:
3965 -- declare
3966 -- Result : T := <expression>;
3967 -- begin
3968 -- <handled_seq_of_stms>
3969 -- return Result;
3970 -- end;
3972 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3974 -- return Result : T := <expression>;
3976 -- to be:
3978 -- return <expression>;
3980 -- unless it's build-in-place or there's no <expression>, in which case
3981 -- we generate:
3983 -- declare
3984 -- Result : T := <expression>;
3985 -- begin
3986 -- return Result;
3987 -- end;
3989 -- Note that this case could have been written by the user as an extended
3990 -- return statement, or could have been transformed to this from a simple
3991 -- return statement.
3993 -- That is, we need to have a reified return object if there are statements
3994 -- (which might refer to it) or if we're doing build-in-place (so we can
3995 -- set its address to the final resting place or if there is no expression
3996 -- (in which case default initial values might need to be set).
3998 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
3999 Loc : constant Source_Ptr := Sloc (N);
4001 Par_Func : constant Entity_Id :=
4002 Return_Applies_To (Return_Statement_Entity (N));
4003 Result_Subt : constant Entity_Id := Etype (Par_Func);
4004 Ret_Obj_Id : constant Entity_Id :=
4005 First_Entity (Return_Statement_Entity (N));
4006 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
4008 Is_Build_In_Place : constant Boolean :=
4009 Is_Build_In_Place_Function (Par_Func);
4011 Exp : Node_Id;
4012 HSS : Node_Id;
4013 Result : Node_Id;
4014 Return_Stmt : Node_Id;
4015 Stmts : List_Id;
4017 function Build_Heap_Allocator
4018 (Temp_Id : Entity_Id;
4019 Temp_Typ : Entity_Id;
4020 Func_Id : Entity_Id;
4021 Ret_Typ : Entity_Id;
4022 Alloc_Expr : Node_Id) return Node_Id;
4023 -- Create the statements necessary to allocate a return object on the
4024 -- caller's master. The master is available through implicit parameter
4025 -- BIPfinalizationmaster.
4027 -- if BIPfinalizationmaster /= null then
4028 -- declare
4029 -- type Ptr_Typ is access Ret_Typ;
4030 -- for Ptr_Typ'Storage_Pool use
4031 -- Base_Pool (BIPfinalizationmaster.all).all;
4032 -- Local : Ptr_Typ;
4034 -- begin
4035 -- procedure Allocate (...) is
4036 -- begin
4037 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4038 -- end Allocate;
4040 -- Local := <Alloc_Expr>;
4041 -- Temp_Id := Temp_Typ (Local);
4042 -- end;
4043 -- end if;
4045 -- Temp_Id is the temporary which is used to reference the internally
4046 -- created object in all allocation forms. Temp_Typ is the type of the
4047 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4048 -- type of Func_Id. Alloc_Expr is the actual allocator.
4050 function Move_Activation_Chain return Node_Id;
4051 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4052 -- with parameters:
4053 -- From current activation chain
4054 -- To activation chain passed in by the caller
4055 -- New_Master master passed in by the caller
4057 --------------------------
4058 -- Build_Heap_Allocator --
4059 --------------------------
4061 function Build_Heap_Allocator
4062 (Temp_Id : Entity_Id;
4063 Temp_Typ : Entity_Id;
4064 Func_Id : Entity_Id;
4065 Ret_Typ : Entity_Id;
4066 Alloc_Expr : Node_Id) return Node_Id
4068 begin
4069 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4071 -- Processing for build-in-place object allocation. This is disabled
4072 -- on .NET/JVM because the targets do not support pools.
4074 if VM_Target = No_VM
4075 and then Needs_Finalization (Ret_Typ)
4076 then
4077 declare
4078 Decls : constant List_Id := New_List;
4079 Fin_Mas_Id : constant Entity_Id :=
4080 Build_In_Place_Formal
4081 (Func_Id, BIP_Finalization_Master);
4082 Stmts : constant List_Id := New_List;
4083 Desig_Typ : Entity_Id;
4084 Local_Id : Entity_Id;
4085 Pool_Id : Entity_Id;
4086 Ptr_Typ : Entity_Id;
4088 begin
4089 -- Generate:
4090 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4092 Pool_Id := Make_Temporary (Loc, 'P');
4094 Append_To (Decls,
4095 Make_Object_Renaming_Declaration (Loc,
4096 Defining_Identifier => Pool_Id,
4097 Subtype_Mark =>
4098 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4099 Name =>
4100 Make_Explicit_Dereference (Loc,
4101 Prefix =>
4102 Make_Function_Call (Loc,
4103 Name =>
4104 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4105 Parameter_Associations => New_List (
4106 Make_Explicit_Dereference (Loc,
4107 Prefix =>
4108 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4110 -- Create an access type which uses the storage pool of the
4111 -- caller's master. This additional type is necessary because
4112 -- the finalization master cannot be associated with the type
4113 -- of the temporary. Otherwise the secondary stack allocation
4114 -- will fail.
4116 Desig_Typ := Ret_Typ;
4118 -- Ensure that the build-in-place machinery uses a fat pointer
4119 -- when allocating an unconstrained array on the heap. In this
4120 -- case the result object type is a constrained array type even
4121 -- though the function type is unconstrained.
4123 if Ekind (Desig_Typ) = E_Array_Subtype then
4124 Desig_Typ := Base_Type (Desig_Typ);
4125 end if;
4127 -- Generate:
4128 -- type Ptr_Typ is access Desig_Typ;
4130 Ptr_Typ := Make_Temporary (Loc, 'P');
4132 Append_To (Decls,
4133 Make_Full_Type_Declaration (Loc,
4134 Defining_Identifier => Ptr_Typ,
4135 Type_Definition =>
4136 Make_Access_To_Object_Definition (Loc,
4137 Subtype_Indication =>
4138 New_Occurrence_Of (Desig_Typ, Loc))));
4140 -- Perform minor decoration in order to set the master and the
4141 -- storage pool attributes.
4143 Set_Ekind (Ptr_Typ, E_Access_Type);
4144 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4145 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4147 -- Create the temporary, generate:
4148 -- Local_Id : Ptr_Typ;
4150 Local_Id := Make_Temporary (Loc, 'T');
4152 Append_To (Decls,
4153 Make_Object_Declaration (Loc,
4154 Defining_Identifier => Local_Id,
4155 Object_Definition =>
4156 New_Occurrence_Of (Ptr_Typ, Loc)));
4158 -- Allocate the object, generate:
4159 -- Local_Id := <Alloc_Expr>;
4161 Append_To (Stmts,
4162 Make_Assignment_Statement (Loc,
4163 Name => New_Occurrence_Of (Local_Id, Loc),
4164 Expression => Alloc_Expr));
4166 -- Generate:
4167 -- Temp_Id := Temp_Typ (Local_Id);
4169 Append_To (Stmts,
4170 Make_Assignment_Statement (Loc,
4171 Name => New_Occurrence_Of (Temp_Id, Loc),
4172 Expression =>
4173 Unchecked_Convert_To (Temp_Typ,
4174 New_Occurrence_Of (Local_Id, Loc))));
4176 -- Wrap the allocation in a block. This is further conditioned
4177 -- by checking the caller finalization master at runtime. A
4178 -- null value indicates a non-existent master, most likely due
4179 -- to a Finalize_Storage_Only allocation.
4181 -- Generate:
4182 -- if BIPfinalizationmaster /= null then
4183 -- declare
4184 -- <Decls>
4185 -- begin
4186 -- <Stmts>
4187 -- end;
4188 -- end if;
4190 return
4191 Make_If_Statement (Loc,
4192 Condition =>
4193 Make_Op_Ne (Loc,
4194 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4195 Right_Opnd => Make_Null (Loc)),
4197 Then_Statements => New_List (
4198 Make_Block_Statement (Loc,
4199 Declarations => Decls,
4200 Handled_Statement_Sequence =>
4201 Make_Handled_Sequence_Of_Statements (Loc,
4202 Statements => Stmts))));
4203 end;
4205 -- For all other cases, generate:
4206 -- Temp_Id := <Alloc_Expr>;
4208 else
4209 return
4210 Make_Assignment_Statement (Loc,
4211 Name => New_Occurrence_Of (Temp_Id, Loc),
4212 Expression => Alloc_Expr);
4213 end if;
4214 end Build_Heap_Allocator;
4216 ---------------------------
4217 -- Move_Activation_Chain --
4218 ---------------------------
4220 function Move_Activation_Chain return Node_Id is
4221 begin
4222 return
4223 Make_Procedure_Call_Statement (Loc,
4224 Name =>
4225 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4227 Parameter_Associations => New_List (
4229 -- Source chain
4231 Make_Attribute_Reference (Loc,
4232 Prefix => Make_Identifier (Loc, Name_uChain),
4233 Attribute_Name => Name_Unrestricted_Access),
4235 -- Destination chain
4237 New_Occurrence_Of
4238 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
4240 -- New master
4242 New_Occurrence_Of
4243 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
4244 end Move_Activation_Chain;
4246 -- Start of processing for Expand_N_Extended_Return_Statement
4248 begin
4249 -- Given that functionality of interface thunks is simple (just displace
4250 -- the pointer to the object) they are always handled by means of
4251 -- simple return statements.
4253 pragma Assert (not Is_Thunk (Current_Scope));
4255 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4256 Exp := Expression (Ret_Obj_Decl);
4257 else
4258 Exp := Empty;
4259 end if;
4261 HSS := Handled_Statement_Sequence (N);
4263 -- If the returned object needs finalization actions, the function must
4264 -- perform the appropriate cleanup should it fail to return. The state
4265 -- of the function itself is tracked through a flag which is coupled
4266 -- with the scope finalizer. There is one flag per each return object
4267 -- in case of multiple returns.
4269 if Is_Build_In_Place
4270 and then Needs_Finalization (Etype (Ret_Obj_Id))
4271 then
4272 declare
4273 Flag_Decl : Node_Id;
4274 Flag_Id : Entity_Id;
4275 Func_Bod : Node_Id;
4277 begin
4278 -- Recover the function body
4280 Func_Bod := Unit_Declaration_Node (Par_Func);
4282 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4283 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4284 end if;
4286 -- Create a flag to track the function state
4288 Flag_Id := Make_Temporary (Loc, 'F');
4289 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4291 -- Insert the flag at the beginning of the function declarations,
4292 -- generate:
4293 -- Fnn : Boolean := False;
4295 Flag_Decl :=
4296 Make_Object_Declaration (Loc,
4297 Defining_Identifier => Flag_Id,
4298 Object_Definition =>
4299 New_Occurrence_Of (Standard_Boolean, Loc),
4300 Expression =>
4301 New_Occurrence_Of (Standard_False, Loc));
4303 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4304 Analyze (Flag_Decl);
4305 end;
4306 end if;
4308 -- Build a simple_return_statement that returns the return object when
4309 -- there is a statement sequence, or no expression, or the result will
4310 -- be built in place. Note however that we currently do this for all
4311 -- composite cases, even though nonlimited composite results are not yet
4312 -- built in place (though we plan to do so eventually).
4314 if Present (HSS)
4315 or else Is_Composite_Type (Result_Subt)
4316 or else No (Exp)
4317 then
4318 if No (HSS) then
4319 Stmts := New_List;
4321 -- If the extended return has a handled statement sequence, then wrap
4322 -- it in a block and use the block as the first statement.
4324 else
4325 Stmts := New_List (
4326 Make_Block_Statement (Loc,
4327 Declarations => New_List,
4328 Handled_Statement_Sequence => HSS));
4329 end if;
4331 -- If the result type contains tasks, we call Move_Activation_Chain.
4332 -- Later, the cleanup code will call Complete_Master, which will
4333 -- terminate any unactivated tasks belonging to the return statement
4334 -- master. But Move_Activation_Chain updates their master to be that
4335 -- of the caller, so they will not be terminated unless the return
4336 -- statement completes unsuccessfully due to exception, abort, goto,
4337 -- or exit. As a formality, we test whether the function requires the
4338 -- result to be built in place, though that's necessarily true for
4339 -- the case of result types with task parts.
4341 if Is_Build_In_Place
4342 and then Has_Task (Result_Subt)
4343 then
4344 -- The return expression is an aggregate for a complex type which
4345 -- contains tasks. This particular case is left unexpanded since
4346 -- the regular expansion would insert all temporaries and
4347 -- initialization code in the wrong block.
4349 if Nkind (Exp) = N_Aggregate then
4350 Expand_N_Aggregate (Exp);
4351 end if;
4353 -- Do not move the activation chain if the return object does not
4354 -- contain tasks.
4356 if Has_Task (Etype (Ret_Obj_Id)) then
4357 Append_To (Stmts, Move_Activation_Chain);
4358 end if;
4359 end if;
4361 -- Update the state of the function right before the object is
4362 -- returned.
4364 if Is_Build_In_Place
4365 and then Needs_Finalization (Etype (Ret_Obj_Id))
4366 then
4367 declare
4368 Flag_Id : constant Entity_Id :=
4369 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4371 begin
4372 -- Generate:
4373 -- Fnn := True;
4375 Append_To (Stmts,
4376 Make_Assignment_Statement (Loc,
4377 Name => New_Occurrence_Of (Flag_Id, Loc),
4378 Expression => New_Occurrence_Of (Standard_True, Loc)));
4379 end;
4380 end if;
4382 -- Build a simple_return_statement that returns the return object
4384 Return_Stmt :=
4385 Make_Simple_Return_Statement (Loc,
4386 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4387 Append_To (Stmts, Return_Stmt);
4389 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4390 end if;
4392 -- Case where we build a return statement block
4394 if Present (HSS) then
4395 Result :=
4396 Make_Block_Statement (Loc,
4397 Declarations => Return_Object_Declarations (N),
4398 Handled_Statement_Sequence => HSS);
4400 -- We set the entity of the new block statement to be that of the
4401 -- return statement. This is necessary so that various fields, such
4402 -- as Finalization_Chain_Entity carry over from the return statement
4403 -- to the block. Note that this block is unusual, in that its entity
4404 -- is an E_Return_Statement rather than an E_Block.
4406 Set_Identifier
4407 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4409 -- If the object decl was already rewritten as a renaming, then we
4410 -- don't want to do the object allocation and transformation of
4411 -- the return object declaration to a renaming. This case occurs
4412 -- when the return object is initialized by a call to another
4413 -- build-in-place function, and that function is responsible for
4414 -- the allocation of the return object.
4416 if Is_Build_In_Place
4417 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4418 then
4419 pragma Assert
4420 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4421 and then Is_Build_In_Place_Function_Call
4422 (Expression (Original_Node (Ret_Obj_Decl))));
4424 -- Return the build-in-place result by reference
4426 Set_By_Ref (Return_Stmt);
4428 elsif Is_Build_In_Place then
4430 -- Locate the implicit access parameter associated with the
4431 -- caller-supplied return object and convert the return
4432 -- statement's return object declaration to a renaming of a
4433 -- dereference of the access parameter. If the return object's
4434 -- declaration includes an expression that has not already been
4435 -- expanded as separate assignments, then add an assignment
4436 -- statement to ensure the return object gets initialized.
4438 -- declare
4439 -- Result : T [:= <expression>];
4440 -- begin
4441 -- ...
4443 -- is converted to
4445 -- declare
4446 -- Result : T renames FuncRA.all;
4447 -- [Result := <expression;]
4448 -- begin
4449 -- ...
4451 declare
4452 Return_Obj_Id : constant Entity_Id :=
4453 Defining_Identifier (Ret_Obj_Decl);
4454 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4455 Return_Obj_Expr : constant Node_Id :=
4456 Expression (Ret_Obj_Decl);
4457 Constr_Result : constant Boolean :=
4458 Is_Constrained (Result_Subt);
4459 Obj_Alloc_Formal : Entity_Id;
4460 Object_Access : Entity_Id;
4461 Obj_Acc_Deref : Node_Id;
4462 Init_Assignment : Node_Id := Empty;
4464 begin
4465 -- Build-in-place results must be returned by reference
4467 Set_By_Ref (Return_Stmt);
4469 -- Retrieve the implicit access parameter passed by the caller
4471 Object_Access :=
4472 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
4474 -- If the return object's declaration includes an expression
4475 -- and the declaration isn't marked as No_Initialization, then
4476 -- we need to generate an assignment to the object and insert
4477 -- it after the declaration before rewriting it as a renaming
4478 -- (otherwise we'll lose the initialization). The case where
4479 -- the result type is an interface (or class-wide interface)
4480 -- is also excluded because the context of the function call
4481 -- must be unconstrained, so the initialization will always
4482 -- be done as part of an allocator evaluation (storage pool
4483 -- or secondary stack), never to a constrained target object
4484 -- passed in by the caller. Besides the assignment being
4485 -- unneeded in this case, it avoids problems with trying to
4486 -- generate a dispatching assignment when the return expression
4487 -- is a nonlimited descendant of a limited interface (the
4488 -- interface has no assignment operation).
4490 if Present (Return_Obj_Expr)
4491 and then not No_Initialization (Ret_Obj_Decl)
4492 and then not Is_Interface (Return_Obj_Typ)
4493 then
4494 Init_Assignment :=
4495 Make_Assignment_Statement (Loc,
4496 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
4497 Expression => Relocate_Node (Return_Obj_Expr));
4499 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4500 Set_Assignment_OK (Name (Init_Assignment));
4501 Set_No_Ctrl_Actions (Init_Assignment);
4503 Set_Parent (Name (Init_Assignment), Init_Assignment);
4504 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4506 Set_Expression (Ret_Obj_Decl, Empty);
4508 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4509 and then not Is_Class_Wide_Type
4510 (Etype (Expression (Init_Assignment)))
4511 then
4512 Rewrite (Expression (Init_Assignment),
4513 Make_Type_Conversion (Loc,
4514 Subtype_Mark =>
4515 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
4516 Expression =>
4517 Relocate_Node (Expression (Init_Assignment))));
4518 end if;
4520 -- In the case of functions where the calling context can
4521 -- determine the form of allocation needed, initialization
4522 -- is done with each part of the if statement that handles
4523 -- the different forms of allocation (this is true for
4524 -- unconstrained and tagged result subtypes).
4526 if Constr_Result
4527 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4528 then
4529 Insert_After (Ret_Obj_Decl, Init_Assignment);
4530 end if;
4531 end if;
4533 -- When the function's subtype is unconstrained, a run-time
4534 -- test is needed to determine the form of allocation to use
4535 -- for the return object. The function has an implicit formal
4536 -- parameter indicating this. If the BIP_Alloc_Form formal has
4537 -- the value one, then the caller has passed access to an
4538 -- existing object for use as the return object. If the value
4539 -- is two, then the return object must be allocated on the
4540 -- secondary stack. Otherwise, the object must be allocated in
4541 -- a storage pool (currently only supported for the global
4542 -- heap, user-defined storage pools TBD ???). We generate an
4543 -- if statement to test the implicit allocation formal and
4544 -- initialize a local access value appropriately, creating
4545 -- allocators in the secondary stack and global heap cases.
4546 -- The special formal also exists and must be tested when the
4547 -- function has a tagged result, even when the result subtype
4548 -- is constrained, because in general such functions can be
4549 -- called in dispatching contexts and must be handled similarly
4550 -- to functions with a class-wide result.
4552 if not Constr_Result
4553 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4554 then
4555 Obj_Alloc_Formal :=
4556 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
4558 declare
4559 Pool_Id : constant Entity_Id :=
4560 Make_Temporary (Loc, 'P');
4561 Alloc_Obj_Id : Entity_Id;
4562 Alloc_Obj_Decl : Node_Id;
4563 Alloc_If_Stmt : Node_Id;
4564 Heap_Allocator : Node_Id;
4565 Pool_Decl : Node_Id;
4566 Pool_Allocator : Node_Id;
4567 Ptr_Type_Decl : Node_Id;
4568 Ref_Type : Entity_Id;
4569 SS_Allocator : Node_Id;
4571 begin
4572 -- Reuse the itype created for the function's implicit
4573 -- access formal. This avoids the need to create a new
4574 -- access type here, plus it allows assigning the access
4575 -- formal directly without applying a conversion.
4577 -- Ref_Type := Etype (Object_Access);
4579 -- Create an access type designating the function's
4580 -- result subtype.
4582 Ref_Type := Make_Temporary (Loc, 'A');
4584 Ptr_Type_Decl :=
4585 Make_Full_Type_Declaration (Loc,
4586 Defining_Identifier => Ref_Type,
4587 Type_Definition =>
4588 Make_Access_To_Object_Definition (Loc,
4589 All_Present => True,
4590 Subtype_Indication =>
4591 New_Occurrence_Of (Return_Obj_Typ, Loc)));
4593 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4595 -- Create an access object that will be initialized to an
4596 -- access value denoting the return object, either coming
4597 -- from an implicit access value passed in by the caller
4598 -- or from the result of an allocator.
4600 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4601 Set_Etype (Alloc_Obj_Id, Ref_Type);
4603 Alloc_Obj_Decl :=
4604 Make_Object_Declaration (Loc,
4605 Defining_Identifier => Alloc_Obj_Id,
4606 Object_Definition =>
4607 New_Occurrence_Of (Ref_Type, Loc));
4609 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4611 -- Create allocators for both the secondary stack and
4612 -- global heap. If there's an initialization expression,
4613 -- then create these as initialized allocators.
4615 if Present (Return_Obj_Expr)
4616 and then not No_Initialization (Ret_Obj_Decl)
4617 then
4618 -- Always use the type of the expression for the
4619 -- qualified expression, rather than the result type.
4620 -- In general we cannot always use the result type
4621 -- for the allocator, because the expression might be
4622 -- of a specific type, such as in the case of an
4623 -- aggregate or even a nonlimited object when the
4624 -- result type is a limited class-wide interface type.
4626 Heap_Allocator :=
4627 Make_Allocator (Loc,
4628 Expression =>
4629 Make_Qualified_Expression (Loc,
4630 Subtype_Mark =>
4631 New_Occurrence_Of
4632 (Etype (Return_Obj_Expr), Loc),
4633 Expression =>
4634 New_Copy_Tree (Return_Obj_Expr)));
4636 else
4637 -- If the function returns a class-wide type we cannot
4638 -- use the return type for the allocator. Instead we
4639 -- use the type of the expression, which must be an
4640 -- aggregate of a definite type.
4642 if Is_Class_Wide_Type (Return_Obj_Typ) then
4643 Heap_Allocator :=
4644 Make_Allocator (Loc,
4645 Expression =>
4646 New_Occurrence_Of
4647 (Etype (Return_Obj_Expr), Loc));
4648 else
4649 Heap_Allocator :=
4650 Make_Allocator (Loc,
4651 Expression =>
4652 New_Occurrence_Of (Return_Obj_Typ, Loc));
4653 end if;
4655 -- If the object requires default initialization then
4656 -- that will happen later following the elaboration of
4657 -- the object renaming. If we don't turn it off here
4658 -- then the object will be default initialized twice.
4660 Set_No_Initialization (Heap_Allocator);
4661 end if;
4663 -- The Pool_Allocator is just like the Heap_Allocator,
4664 -- except we set Storage_Pool and Procedure_To_Call so
4665 -- it will use the user-defined storage pool.
4667 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4669 -- Do not generate the renaming of the build-in-place
4670 -- pool parameter on .NET/JVM/ZFP because the parameter
4671 -- is not created in the first place.
4673 if VM_Target = No_VM
4674 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
4675 then
4676 Pool_Decl :=
4677 Make_Object_Renaming_Declaration (Loc,
4678 Defining_Identifier => Pool_Id,
4679 Subtype_Mark =>
4680 New_Occurrence_Of
4681 (RTE (RE_Root_Storage_Pool), Loc),
4682 Name =>
4683 Make_Explicit_Dereference (Loc,
4684 New_Occurrence_Of
4685 (Build_In_Place_Formal
4686 (Par_Func, BIP_Storage_Pool), Loc)));
4687 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4688 Set_Procedure_To_Call
4689 (Pool_Allocator, RTE (RE_Allocate_Any));
4690 else
4691 Pool_Decl := Make_Null_Statement (Loc);
4692 end if;
4694 -- If the No_Allocators restriction is active, then only
4695 -- an allocator for secondary stack allocation is needed.
4696 -- It's OK for such allocators to have Comes_From_Source
4697 -- set to False, because gigi knows not to flag them as
4698 -- being a violation of No_Implicit_Heap_Allocations.
4700 if Restriction_Active (No_Allocators) then
4701 SS_Allocator := Heap_Allocator;
4702 Heap_Allocator := Make_Null (Loc);
4703 Pool_Allocator := Make_Null (Loc);
4705 -- Otherwise the heap and pool allocators may be needed,
4706 -- so we make another allocator for secondary stack
4707 -- allocation.
4709 else
4710 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4712 -- The heap and pool allocators are marked as
4713 -- Comes_From_Source since they correspond to an
4714 -- explicit user-written allocator (that is, it will
4715 -- only be executed on behalf of callers that call the
4716 -- function as initialization for such an allocator).
4717 -- Prevents errors when No_Implicit_Heap_Allocations
4718 -- is in force.
4720 Set_Comes_From_Source (Heap_Allocator, True);
4721 Set_Comes_From_Source (Pool_Allocator, True);
4722 end if;
4724 -- The allocator is returned on the secondary stack. We
4725 -- don't do this on VM targets, since the SS is not used.
4727 if VM_Target = No_VM then
4728 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4729 Set_Procedure_To_Call
4730 (SS_Allocator, RTE (RE_SS_Allocate));
4732 -- The allocator is returned on the secondary stack,
4733 -- so indicate that the function return, as well as
4734 -- the block that encloses the allocator, must not
4735 -- release it. The flags must be set now because
4736 -- the decision to use the secondary stack is done
4737 -- very late in the course of expanding the return
4738 -- statement, past the point where these flags are
4739 -- normally set.
4741 Set_Sec_Stack_Needed_For_Return (Par_Func);
4742 Set_Sec_Stack_Needed_For_Return
4743 (Return_Statement_Entity (N));
4744 Set_Uses_Sec_Stack (Par_Func);
4745 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4746 end if;
4748 -- Create an if statement to test the BIP_Alloc_Form
4749 -- formal and initialize the access object to either the
4750 -- BIP_Object_Access formal (BIP_Alloc_Form =
4751 -- Caller_Allocation), the result of allocating the
4752 -- object in the secondary stack (BIP_Alloc_Form =
4753 -- Secondary_Stack), or else an allocator to create the
4754 -- return object in the heap or user-defined pool
4755 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4757 -- ??? An unchecked type conversion must be made in the
4758 -- case of assigning the access object formal to the
4759 -- local access object, because a normal conversion would
4760 -- be illegal in some cases (such as converting access-
4761 -- to-unconstrained to access-to-constrained), but the
4762 -- the unchecked conversion will presumably fail to work
4763 -- right in just such cases. It's not clear at all how to
4764 -- handle this. ???
4766 Alloc_If_Stmt :=
4767 Make_If_Statement (Loc,
4768 Condition =>
4769 Make_Op_Eq (Loc,
4770 Left_Opnd =>
4771 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4772 Right_Opnd =>
4773 Make_Integer_Literal (Loc,
4774 UI_From_Int (BIP_Allocation_Form'Pos
4775 (Caller_Allocation)))),
4777 Then_Statements => New_List (
4778 Make_Assignment_Statement (Loc,
4779 Name =>
4780 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4781 Expression =>
4782 Make_Unchecked_Type_Conversion (Loc,
4783 Subtype_Mark =>
4784 New_Occurrence_Of (Ref_Type, Loc),
4785 Expression =>
4786 New_Occurrence_Of (Object_Access, Loc)))),
4788 Elsif_Parts => New_List (
4789 Make_Elsif_Part (Loc,
4790 Condition =>
4791 Make_Op_Eq (Loc,
4792 Left_Opnd =>
4793 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4794 Right_Opnd =>
4795 Make_Integer_Literal (Loc,
4796 UI_From_Int (BIP_Allocation_Form'Pos
4797 (Secondary_Stack)))),
4799 Then_Statements => New_List (
4800 Make_Assignment_Statement (Loc,
4801 Name =>
4802 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4803 Expression => SS_Allocator))),
4805 Make_Elsif_Part (Loc,
4806 Condition =>
4807 Make_Op_Eq (Loc,
4808 Left_Opnd =>
4809 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4810 Right_Opnd =>
4811 Make_Integer_Literal (Loc,
4812 UI_From_Int (BIP_Allocation_Form'Pos
4813 (Global_Heap)))),
4815 Then_Statements => New_List (
4816 Build_Heap_Allocator
4817 (Temp_Id => Alloc_Obj_Id,
4818 Temp_Typ => Ref_Type,
4819 Func_Id => Par_Func,
4820 Ret_Typ => Return_Obj_Typ,
4821 Alloc_Expr => Heap_Allocator)))),
4823 Else_Statements => New_List (
4824 Pool_Decl,
4825 Build_Heap_Allocator
4826 (Temp_Id => Alloc_Obj_Id,
4827 Temp_Typ => Ref_Type,
4828 Func_Id => Par_Func,
4829 Ret_Typ => Return_Obj_Typ,
4830 Alloc_Expr => Pool_Allocator)));
4832 -- If a separate initialization assignment was created
4833 -- earlier, append that following the assignment of the
4834 -- implicit access formal to the access object, to ensure
4835 -- that the return object is initialized in that case. In
4836 -- this situation, the target of the assignment must be
4837 -- rewritten to denote a dereference of the access to the
4838 -- return object passed in by the caller.
4840 if Present (Init_Assignment) then
4841 Rewrite (Name (Init_Assignment),
4842 Make_Explicit_Dereference (Loc,
4843 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
4845 Set_Etype
4846 (Name (Init_Assignment), Etype (Return_Obj_Id));
4848 Append_To
4849 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
4850 end if;
4852 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
4854 -- Remember the local access object for use in the
4855 -- dereference of the renaming created below.
4857 Object_Access := Alloc_Obj_Id;
4858 end;
4859 end if;
4861 -- Replace the return object declaration with a renaming of a
4862 -- dereference of the access value designating the return
4863 -- object.
4865 Obj_Acc_Deref :=
4866 Make_Explicit_Dereference (Loc,
4867 Prefix => New_Occurrence_Of (Object_Access, Loc));
4869 Rewrite (Ret_Obj_Decl,
4870 Make_Object_Renaming_Declaration (Loc,
4871 Defining_Identifier => Return_Obj_Id,
4872 Access_Definition => Empty,
4873 Subtype_Mark =>
4874 New_Occurrence_Of (Return_Obj_Typ, Loc),
4875 Name => Obj_Acc_Deref));
4877 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4878 end;
4879 end if;
4881 -- Case where we do not build a block
4883 else
4884 -- We're about to drop Return_Object_Declarations on the floor, so
4885 -- we need to insert it, in case it got expanded into useful code.
4886 -- Remove side effects from expression, which may be duplicated in
4887 -- subsequent checks (see Expand_Simple_Function_Return).
4889 Insert_List_Before (N, Return_Object_Declarations (N));
4890 Remove_Side_Effects (Exp);
4892 -- Build simple_return_statement that returns the expression directly
4894 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
4895 Result := Return_Stmt;
4896 end if;
4898 -- Set the flag to prevent infinite recursion
4900 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
4902 Rewrite (N, Result);
4903 Analyze (N);
4904 end Expand_N_Extended_Return_Statement;
4906 ----------------------------
4907 -- Expand_N_Function_Call --
4908 ----------------------------
4910 procedure Expand_N_Function_Call (N : Node_Id) is
4911 begin
4912 Expand_Call (N);
4913 end Expand_N_Function_Call;
4915 ---------------------------------------
4916 -- Expand_N_Procedure_Call_Statement --
4917 ---------------------------------------
4919 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4920 begin
4921 Expand_Call (N);
4922 end Expand_N_Procedure_Call_Statement;
4924 --------------------------------------
4925 -- Expand_N_Simple_Return_Statement --
4926 --------------------------------------
4928 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4929 begin
4930 -- Defend against previous errors (i.e. the return statement calls a
4931 -- function that is not available in configurable runtime).
4933 if Present (Expression (N))
4934 and then Nkind (Expression (N)) = N_Empty
4935 then
4936 Check_Error_Detected;
4937 return;
4938 end if;
4940 -- Distinguish the function and non-function cases:
4942 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4944 when E_Function |
4945 E_Generic_Function =>
4946 Expand_Simple_Function_Return (N);
4948 when E_Procedure |
4949 E_Generic_Procedure |
4950 E_Entry |
4951 E_Entry_Family |
4952 E_Return_Statement =>
4953 Expand_Non_Function_Return (N);
4955 when others =>
4956 raise Program_Error;
4957 end case;
4959 exception
4960 when RE_Not_Available =>
4961 return;
4962 end Expand_N_Simple_Return_Statement;
4964 ------------------------------
4965 -- Expand_N_Subprogram_Body --
4966 ------------------------------
4968 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4969 -- by the back-end.
4971 -- Add dummy push/pop label nodes at start and end to clear any local
4972 -- exception indications if local-exception-to-goto optimization is active.
4974 -- Add return statement if last statement in body is not a return statement
4975 -- (this makes things easier on Gigi which does not want to have to handle
4976 -- a missing return).
4978 -- Add call to Activate_Tasks if body is a task activator
4980 -- Deal with possible detection of infinite recursion
4982 -- Eliminate body completely if convention stubbed
4984 -- Encode entity names within body, since we will not need to reference
4985 -- these entities any longer in the front end.
4987 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4989 -- Reset Pure indication if any parameter has root type System.Address
4990 -- or has any parameters of limited types, where limited means that the
4991 -- run-time view is limited (i.e. the full type is limited).
4993 -- Wrap thread body
4995 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4996 Loc : constant Source_Ptr := Sloc (N);
4997 H : constant Node_Id := Handled_Statement_Sequence (N);
4998 Body_Id : Entity_Id;
4999 Except_H : Node_Id;
5000 L : List_Id;
5001 Spec_Id : Entity_Id;
5003 procedure Add_Return (S : List_Id);
5004 -- Append a return statement to the statement sequence S if the last
5005 -- statement is not already a return or a goto statement. Note that
5006 -- the latter test is not critical, it does not matter if we add a few
5007 -- extra returns, since they get eliminated anyway later on.
5009 ----------------
5010 -- Add_Return --
5011 ----------------
5013 procedure Add_Return (S : List_Id) is
5014 Last_Stmt : Node_Id;
5015 Loc : Source_Ptr;
5016 Stmt : Node_Id;
5018 begin
5019 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
5020 -- not relevant in this context since they are not executable.
5022 Last_Stmt := Last (S);
5023 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
5024 Prev (Last_Stmt);
5025 end loop;
5027 -- Now insert return unless last statement is a transfer
5029 if not Is_Transfer (Last_Stmt) then
5031 -- The source location for the return is the end label of the
5032 -- procedure if present. Otherwise use the sloc of the last
5033 -- statement in the list. If the list comes from a generated
5034 -- exception handler and we are not debugging generated code,
5035 -- all the statements within the handler are made invisible
5036 -- to the debugger.
5038 if Nkind (Parent (S)) = N_Exception_Handler
5039 and then not Comes_From_Source (Parent (S))
5040 then
5041 Loc := Sloc (Last_Stmt);
5042 elsif Present (End_Label (H)) then
5043 Loc := Sloc (End_Label (H));
5044 else
5045 Loc := Sloc (Last_Stmt);
5046 end if;
5048 -- Append return statement, and set analyzed manually. We can't
5049 -- call Analyze on this return since the scope is wrong.
5051 -- Note: it almost works to push the scope and then do the Analyze
5052 -- call, but something goes wrong in some weird cases and it is
5053 -- not worth worrying about ???
5055 Stmt := Make_Simple_Return_Statement (Loc);
5057 -- The return statement is handled properly, and the call to the
5058 -- postcondition, inserted below, does not require information
5059 -- from the body either. However, that call is analyzed in the
5060 -- enclosing scope, and an elaboration check might improperly be
5061 -- added to it. A guard in Sem_Elab is needed to prevent that
5062 -- spurious check, see Check_Elab_Call.
5064 Append_To (S, Stmt);
5065 Set_Analyzed (Stmt);
5067 -- Call the _Postconditions procedure if the related subprogram
5068 -- has contract assertions that need to be verified on exit.
5070 if Ekind (Spec_Id) = E_Procedure
5071 and then Present (Postconditions_Proc (Spec_Id))
5072 then
5073 Insert_Action (Stmt,
5074 Make_Procedure_Call_Statement (Loc,
5075 Name =>
5076 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5077 end if;
5078 end if;
5079 end Add_Return;
5081 -- Start of processing for Expand_N_Subprogram_Body
5083 begin
5084 -- Set L to either the list of declarations if present, or to the list
5085 -- of statements if no declarations are present. This is used to insert
5086 -- new stuff at the start.
5088 if Is_Non_Empty_List (Declarations (N)) then
5089 L := Declarations (N);
5090 else
5091 L := Statements (H);
5092 end if;
5094 -- If local-exception-to-goto optimization active, insert dummy push
5095 -- statements at start, and dummy pop statements at end, but inhibit
5096 -- this if we have No_Exception_Handlers, since they are useless and
5097 -- intefere with analysis, e.g. by codepeer.
5099 if (Debug_Flag_Dot_G
5100 or else Restriction_Active (No_Exception_Propagation))
5101 and then not Restriction_Active (No_Exception_Handlers)
5102 and then not CodePeer_Mode
5103 and then Is_Non_Empty_List (L)
5104 then
5105 declare
5106 FS : constant Node_Id := First (L);
5107 FL : constant Source_Ptr := Sloc (FS);
5108 LS : Node_Id;
5109 LL : Source_Ptr;
5111 begin
5112 -- LS points to either last statement, if statements are present
5113 -- or to the last declaration if there are no statements present.
5114 -- It is the node after which the pop's are generated.
5116 if Is_Non_Empty_List (Statements (H)) then
5117 LS := Last (Statements (H));
5118 else
5119 LS := Last (L);
5120 end if;
5122 LL := Sloc (LS);
5124 Insert_List_Before_And_Analyze (FS, New_List (
5125 Make_Push_Constraint_Error_Label (FL),
5126 Make_Push_Program_Error_Label (FL),
5127 Make_Push_Storage_Error_Label (FL)));
5129 Insert_List_After_And_Analyze (LS, New_List (
5130 Make_Pop_Constraint_Error_Label (LL),
5131 Make_Pop_Program_Error_Label (LL),
5132 Make_Pop_Storage_Error_Label (LL)));
5133 end;
5134 end if;
5136 -- Find entity for subprogram
5138 Body_Id := Defining_Entity (N);
5140 if Present (Corresponding_Spec (N)) then
5141 Spec_Id := Corresponding_Spec (N);
5142 else
5143 Spec_Id := Body_Id;
5144 end if;
5146 -- Need poll on entry to subprogram if polling enabled. We only do this
5147 -- for non-empty subprograms, since it does not seem necessary to poll
5148 -- for a dummy null subprogram.
5150 if Is_Non_Empty_List (L) then
5152 -- Do not add a polling call if the subprogram is to be inlined by
5153 -- the back-end, to avoid repeated calls with multiple inlinings.
5155 if Is_Inlined (Spec_Id)
5156 and then Front_End_Inlining
5157 and then Optimization_Level > 1
5158 then
5159 null;
5160 else
5161 Generate_Poll_Call (First (L));
5162 end if;
5163 end if;
5165 -- If this is a Pure function which has any parameters whose root type
5166 -- is System.Address, reset the Pure indication, since it will likely
5167 -- cause incorrect code to be generated as the parameter is probably
5168 -- a pointer, and the fact that the same pointer is passed does not mean
5169 -- that the same value is being referenced.
5171 -- Note that if the programmer gave an explicit Pure_Function pragma,
5172 -- then we believe the programmer, and leave the subprogram Pure.
5174 -- This code should probably be at the freeze point, so that it happens
5175 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5176 -- semantic tree has Is_Pure set properly ???
5178 if Is_Pure (Spec_Id)
5179 and then Is_Subprogram (Spec_Id)
5180 and then not Has_Pragma_Pure_Function (Spec_Id)
5181 then
5182 declare
5183 F : Entity_Id;
5185 begin
5186 F := First_Formal (Spec_Id);
5187 while Present (F) loop
5188 if Is_Descendent_Of_Address (Etype (F))
5190 -- Note that this test is being made in the body of the
5191 -- subprogram, not the spec, so we are testing the full
5192 -- type for being limited here, as required.
5194 or else Is_Limited_Type (Etype (F))
5195 then
5196 Set_Is_Pure (Spec_Id, False);
5198 if Spec_Id /= Body_Id then
5199 Set_Is_Pure (Body_Id, False);
5200 end if;
5202 exit;
5203 end if;
5205 Next_Formal (F);
5206 end loop;
5207 end;
5208 end if;
5210 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5212 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5213 declare
5214 F : Entity_Id;
5215 A : Node_Id;
5217 begin
5218 -- Loop through formals
5220 F := First_Formal (Spec_Id);
5221 while Present (F) loop
5222 if Is_Scalar_Type (Etype (F))
5223 and then Ekind (F) = E_Out_Parameter
5224 then
5225 Check_Restriction (No_Default_Initialization, F);
5227 -- Insert the initialization. We turn off validity checks
5228 -- for this assignment, since we do not want any check on
5229 -- the initial value itself (which may well be invalid).
5230 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5232 A :=
5233 Make_Assignment_Statement (Loc,
5234 Name => New_Occurrence_Of (F, Loc),
5235 Expression => Get_Simple_Init_Val (Etype (F), N));
5236 Set_Suppress_Assignment_Checks (A);
5238 Insert_Before_And_Analyze (First (L),
5239 A, Suppress => Validity_Check);
5240 end if;
5242 Next_Formal (F);
5243 end loop;
5244 end;
5245 end if;
5247 -- Clear out statement list for stubbed procedure
5249 if Present (Corresponding_Spec (N)) then
5250 Set_Elaboration_Flag (N, Spec_Id);
5252 if Convention (Spec_Id) = Convention_Stubbed
5253 or else Is_Eliminated (Spec_Id)
5254 then
5255 Set_Declarations (N, Empty_List);
5256 Set_Handled_Statement_Sequence (N,
5257 Make_Handled_Sequence_Of_Statements (Loc,
5258 Statements => New_List (Make_Null_Statement (Loc))));
5259 return;
5260 end if;
5261 end if;
5263 -- Create a set of discriminals for the next protected subprogram body
5265 if Is_List_Member (N)
5266 and then Present (Parent (List_Containing (N)))
5267 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5268 and then Present (Next_Protected_Operation (N))
5269 then
5270 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5271 end if;
5273 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5274 -- subprograms with no specs are not frozen.
5276 declare
5277 Typ : constant Entity_Id := Etype (Spec_Id);
5278 Utyp : constant Entity_Id := Underlying_Type (Typ);
5280 begin
5281 if not Acts_As_Spec (N)
5282 and then Nkind (Parent (Parent (Spec_Id))) /=
5283 N_Subprogram_Body_Stub
5284 then
5285 null;
5287 elsif Is_Limited_View (Typ) then
5288 Set_Returns_By_Ref (Spec_Id);
5290 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5291 Set_Returns_By_Ref (Spec_Id);
5292 end if;
5293 end;
5295 -- For a procedure, we add a return for all possible syntactic ends of
5296 -- the subprogram.
5298 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5299 Add_Return (Statements (H));
5301 if Present (Exception_Handlers (H)) then
5302 Except_H := First_Non_Pragma (Exception_Handlers (H));
5303 while Present (Except_H) loop
5304 Add_Return (Statements (Except_H));
5305 Next_Non_Pragma (Except_H);
5306 end loop;
5307 end if;
5309 -- For a function, we must deal with the case where there is at least
5310 -- one missing return. What we do is to wrap the entire body of the
5311 -- function in a block:
5313 -- begin
5314 -- ...
5315 -- end;
5317 -- becomes
5319 -- begin
5320 -- begin
5321 -- ...
5322 -- end;
5324 -- raise Program_Error;
5325 -- end;
5327 -- This approach is necessary because the raise must be signalled to the
5328 -- caller, not handled by any local handler (RM 6.4(11)).
5330 -- Note: we do not need to analyze the constructed sequence here, since
5331 -- it has no handler, and an attempt to analyze the handled statement
5332 -- sequence twice is risky in various ways (e.g. the issue of expanding
5333 -- cleanup actions twice).
5335 elsif Has_Missing_Return (Spec_Id) then
5336 declare
5337 Hloc : constant Source_Ptr := Sloc (H);
5338 Blok : constant Node_Id :=
5339 Make_Block_Statement (Hloc,
5340 Handled_Statement_Sequence => H);
5341 Rais : constant Node_Id :=
5342 Make_Raise_Program_Error (Hloc,
5343 Reason => PE_Missing_Return);
5345 begin
5346 Set_Handled_Statement_Sequence (N,
5347 Make_Handled_Sequence_Of_Statements (Hloc,
5348 Statements => New_List (Blok, Rais)));
5350 Push_Scope (Spec_Id);
5351 Analyze (Blok);
5352 Analyze (Rais);
5353 Pop_Scope;
5354 end;
5355 end if;
5357 -- If subprogram contains a parameterless recursive call, then we may
5358 -- have an infinite recursion, so see if we can generate code to check
5359 -- for this possibility if storage checks are not suppressed.
5361 if Ekind (Spec_Id) = E_Procedure
5362 and then Has_Recursive_Call (Spec_Id)
5363 and then not Storage_Checks_Suppressed (Spec_Id)
5364 then
5365 Detect_Infinite_Recursion (N, Spec_Id);
5366 end if;
5368 -- Set to encode entity names in package body before gigi is called
5370 Qualify_Entity_Names (N);
5372 -- If we are unnesting procedures, and this is an outer level procedure
5373 -- with nested subprograms, do the unnesting operation now.
5375 if Opt.Unnest_Subprogram_Mode
5377 -- We are only interested in subprograms (not generic subprograms)
5379 and then Is_Subprogram (Spec_Id)
5381 -- Only deal with outer level subprograms. Nested subprograms are
5382 -- handled as part of dealing with the outer level subprogram in
5383 -- which they are nested.
5385 and then Enclosing_Subprogram (Spec_Id) = Empty
5387 -- We are only interested in subprograms that have nested subprograms
5389 and then Has_Nested_Subprogram (Spec_Id)
5390 then
5391 Unest_Bodies.Append ((Spec_Id, N));
5392 end if;
5393 end Expand_N_Subprogram_Body;
5395 -----------------------------------
5396 -- Expand_N_Subprogram_Body_Stub --
5397 -----------------------------------
5399 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5400 begin
5401 if Present (Corresponding_Body (N)) then
5402 Expand_N_Subprogram_Body (
5403 Unit_Declaration_Node (Corresponding_Body (N)));
5404 end if;
5405 end Expand_N_Subprogram_Body_Stub;
5407 -------------------------------------
5408 -- Expand_N_Subprogram_Declaration --
5409 -------------------------------------
5411 -- If the declaration appears within a protected body, it is a private
5412 -- operation of the protected type. We must create the corresponding
5413 -- protected subprogram an associated formals. For a normal protected
5414 -- operation, this is done when expanding the protected type declaration.
5416 -- If the declaration is for a null procedure, emit null body
5418 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5419 Loc : constant Source_Ptr := Sloc (N);
5420 Subp : constant Entity_Id := Defining_Entity (N);
5421 Scop : constant Entity_Id := Scope (Subp);
5422 Prot_Decl : Node_Id;
5423 Prot_Bod : Node_Id;
5424 Prot_Id : Entity_Id;
5426 begin
5427 -- In SPARK, subprogram declarations are only allowed in package
5428 -- specifications.
5430 if Nkind (Parent (N)) /= N_Package_Specification then
5431 if Nkind (Parent (N)) = N_Compilation_Unit then
5432 Check_SPARK_05_Restriction
5433 ("subprogram declaration is not a library item", N);
5435 elsif Present (Next (N))
5436 and then Nkind (Next (N)) = N_Pragma
5437 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
5438 then
5439 -- In SPARK, subprogram declarations are also permitted in
5440 -- declarative parts when immediately followed by a corresponding
5441 -- pragma Import. We only check here that there is some pragma
5442 -- Import.
5444 null;
5445 else
5446 Check_SPARK_05_Restriction
5447 ("subprogram declaration is not allowed here", N);
5448 end if;
5449 end if;
5451 -- Deal with case of protected subprogram. Do not generate protected
5452 -- operation if operation is flagged as eliminated.
5454 if Is_List_Member (N)
5455 and then Present (Parent (List_Containing (N)))
5456 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5457 and then Is_Protected_Type (Scop)
5458 then
5459 if No (Protected_Body_Subprogram (Subp))
5460 and then not Is_Eliminated (Subp)
5461 then
5462 Prot_Decl :=
5463 Make_Subprogram_Declaration (Loc,
5464 Specification =>
5465 Build_Protected_Sub_Specification
5466 (N, Scop, Unprotected_Mode));
5468 -- The protected subprogram is declared outside of the protected
5469 -- body. Given that the body has frozen all entities so far, we
5470 -- analyze the subprogram and perform freezing actions explicitly.
5471 -- including the generation of an explicit freeze node, to ensure
5472 -- that gigi has the proper order of elaboration.
5473 -- If the body is a subunit, the insertion point is before the
5474 -- stub in the parent.
5476 Prot_Bod := Parent (List_Containing (N));
5478 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5479 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5480 end if;
5482 Insert_Before (Prot_Bod, Prot_Decl);
5483 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5484 Set_Has_Delayed_Freeze (Prot_Id);
5486 Push_Scope (Scope (Scop));
5487 Analyze (Prot_Decl);
5488 Freeze_Before (N, Prot_Id);
5489 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5491 -- Create protected operation as well. Even though the operation
5492 -- is only accessible within the body, it is possible to make it
5493 -- available outside of the protected object by using 'Access to
5494 -- provide a callback, so build protected version in all cases.
5496 Prot_Decl :=
5497 Make_Subprogram_Declaration (Loc,
5498 Specification =>
5499 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5500 Insert_Before (Prot_Bod, Prot_Decl);
5501 Analyze (Prot_Decl);
5503 Pop_Scope;
5504 end if;
5506 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5507 -- cases this is superfluous because calls to it will be automatically
5508 -- inlined, but we definitely need the body if preconditions for the
5509 -- procedure are present.
5511 elsif Nkind (Specification (N)) = N_Procedure_Specification
5512 and then Null_Present (Specification (N))
5513 then
5514 declare
5515 Bod : constant Node_Id := Body_To_Inline (N);
5517 begin
5518 Set_Has_Completion (Subp, False);
5519 Append_Freeze_Action (Subp, Bod);
5521 -- The body now contains raise statements, so calls to it will
5522 -- not be inlined.
5524 Set_Is_Inlined (Subp, False);
5525 end;
5526 end if;
5527 end Expand_N_Subprogram_Declaration;
5529 --------------------------------
5530 -- Expand_Non_Function_Return --
5531 --------------------------------
5533 procedure Expand_Non_Function_Return (N : Node_Id) is
5534 pragma Assert (No (Expression (N)));
5536 Loc : constant Source_Ptr := Sloc (N);
5537 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5538 Kind : constant Entity_Kind := Ekind (Scope_Id);
5539 Call : Node_Id;
5540 Acc_Stat : Node_Id;
5541 Goto_Stat : Node_Id;
5542 Lab_Node : Node_Id;
5544 begin
5545 -- Call the _Postconditions procedure if the related subprogram has
5546 -- contract assertions that need to be verified on exit.
5548 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5549 and then Present (Postconditions_Proc (Scope_Id))
5550 then
5551 Insert_Action (N,
5552 Make_Procedure_Call_Statement (Loc,
5553 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5554 end if;
5556 -- If it is a return from a procedure do no extra steps
5558 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5559 return;
5561 -- If it is a nested return within an extended one, replace it with a
5562 -- return of the previously declared return object.
5564 elsif Kind = E_Return_Statement then
5565 Rewrite (N,
5566 Make_Simple_Return_Statement (Loc,
5567 Expression =>
5568 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5569 Set_Comes_From_Extended_Return_Statement (N);
5570 Set_Return_Statement_Entity (N, Scope_Id);
5571 Expand_Simple_Function_Return (N);
5572 return;
5573 end if;
5575 pragma Assert (Is_Entry (Scope_Id));
5577 -- Look at the enclosing block to see whether the return is from an
5578 -- accept statement or an entry body.
5580 for J in reverse 0 .. Scope_Stack.Last loop
5581 Scope_Id := Scope_Stack.Table (J).Entity;
5582 exit when Is_Concurrent_Type (Scope_Id);
5583 end loop;
5585 -- If it is a return from accept statement it is expanded as call to
5586 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5588 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5589 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5591 if Is_Task_Type (Scope_Id) then
5593 Call :=
5594 Make_Procedure_Call_Statement (Loc,
5595 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5596 Insert_Before (N, Call);
5597 -- why not insert actions here???
5598 Analyze (Call);
5600 Acc_Stat := Parent (N);
5601 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5602 Acc_Stat := Parent (Acc_Stat);
5603 end loop;
5605 Lab_Node := Last (Statements
5606 (Handled_Statement_Sequence (Acc_Stat)));
5608 Goto_Stat := Make_Goto_Statement (Loc,
5609 Name => New_Occurrence_Of
5610 (Entity (Identifier (Lab_Node)), Loc));
5612 Set_Analyzed (Goto_Stat);
5614 Rewrite (N, Goto_Stat);
5615 Analyze (N);
5617 -- If it is a return from an entry body, put a Complete_Entry_Body call
5618 -- in front of the return.
5620 elsif Is_Protected_Type (Scope_Id) then
5621 Call :=
5622 Make_Procedure_Call_Statement (Loc,
5623 Name =>
5624 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5625 Parameter_Associations => New_List (
5626 Make_Attribute_Reference (Loc,
5627 Prefix =>
5628 New_Occurrence_Of
5629 (Find_Protection_Object (Current_Scope), Loc),
5630 Attribute_Name => Name_Unchecked_Access)));
5632 Insert_Before (N, Call);
5633 Analyze (Call);
5634 end if;
5635 end Expand_Non_Function_Return;
5637 ---------------------------------------
5638 -- Expand_Protected_Object_Reference --
5639 ---------------------------------------
5641 function Expand_Protected_Object_Reference
5642 (N : Node_Id;
5643 Scop : Entity_Id) return Node_Id
5645 Loc : constant Source_Ptr := Sloc (N);
5646 Corr : Entity_Id;
5647 Rec : Node_Id;
5648 Param : Entity_Id;
5649 Proc : Entity_Id;
5651 begin
5652 Rec := Make_Identifier (Loc, Name_uObject);
5653 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5655 -- Find enclosing protected operation, and retrieve its first parameter,
5656 -- which denotes the enclosing protected object. If the enclosing
5657 -- operation is an entry, we are immediately within the protected body,
5658 -- and we can retrieve the object from the service entries procedure. A
5659 -- barrier function has the same signature as an entry. A barrier
5660 -- function is compiled within the protected object, but unlike
5661 -- protected operations its never needs locks, so that its protected
5662 -- body subprogram points to itself.
5664 Proc := Current_Scope;
5665 while Present (Proc)
5666 and then Scope (Proc) /= Scop
5667 loop
5668 Proc := Scope (Proc);
5669 end loop;
5671 Corr := Protected_Body_Subprogram (Proc);
5673 if No (Corr) then
5675 -- Previous error left expansion incomplete.
5676 -- Nothing to do on this call.
5678 return Empty;
5679 end if;
5681 Param :=
5682 Defining_Identifier
5683 (First (Parameter_Specifications (Parent (Corr))));
5685 if Is_Subprogram (Proc) and then Proc /= Corr then
5687 -- Protected function or procedure
5689 Set_Entity (Rec, Param);
5691 -- Rec is a reference to an entity which will not be in scope when
5692 -- the call is reanalyzed, and needs no further analysis.
5694 Set_Analyzed (Rec);
5696 else
5697 -- Entry or barrier function for entry body. The first parameter of
5698 -- the entry body procedure is pointer to the object. We create a
5699 -- local variable of the proper type, duplicating what is done to
5700 -- define _object later on.
5702 declare
5703 Decls : List_Id;
5704 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5706 begin
5707 Decls := New_List (
5708 Make_Full_Type_Declaration (Loc,
5709 Defining_Identifier => Obj_Ptr,
5710 Type_Definition =>
5711 Make_Access_To_Object_Definition (Loc,
5712 Subtype_Indication =>
5713 New_Occurrence_Of
5714 (Corresponding_Record_Type (Scop), Loc))));
5716 Insert_Actions (N, Decls);
5717 Freeze_Before (N, Obj_Ptr);
5719 Rec :=
5720 Make_Explicit_Dereference (Loc,
5721 Prefix =>
5722 Unchecked_Convert_To (Obj_Ptr,
5723 New_Occurrence_Of (Param, Loc)));
5725 -- Analyze new actual. Other actuals in calls are already analyzed
5726 -- and the list of actuals is not reanalyzed after rewriting.
5728 Set_Parent (Rec, N);
5729 Analyze (Rec);
5730 end;
5731 end if;
5733 return Rec;
5734 end Expand_Protected_Object_Reference;
5736 --------------------------------------
5737 -- Expand_Protected_Subprogram_Call --
5738 --------------------------------------
5740 procedure Expand_Protected_Subprogram_Call
5741 (N : Node_Id;
5742 Subp : Entity_Id;
5743 Scop : Entity_Id)
5745 Rec : Node_Id;
5747 procedure Freeze_Called_Function;
5748 -- If it is a function call it can appear in elaboration code and
5749 -- the called entity must be frozen before the call. This must be
5750 -- done before the call is expanded, as the expansion may rewrite it
5751 -- to something other than a call (e.g. a temporary initialized in a
5752 -- transient block).
5754 ----------------------------
5755 -- Freeze_Called_Function --
5756 ----------------------------
5758 procedure Freeze_Called_Function is
5759 begin
5760 if Ekind (Subp) = E_Function then
5761 Freeze_Expression (Name (N));
5762 end if;
5763 end Freeze_Called_Function;
5765 -- Start of processing for Expand_Protected_Subprogram_Call
5767 begin
5768 -- If the protected object is not an enclosing scope, this is an inter-
5769 -- object function call. Inter-object procedure calls are expanded by
5770 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5771 -- subprogram being called is in the protected body being compiled, and
5772 -- if the protected object in the call is statically the enclosing type.
5773 -- The object may be an component of some other data structure, in which
5774 -- case this must be handled as an inter-object call.
5776 if not In_Open_Scopes (Scop)
5777 or else not Is_Entity_Name (Name (N))
5778 then
5779 if Nkind (Name (N)) = N_Selected_Component then
5780 Rec := Prefix (Name (N));
5782 else
5783 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5784 Rec := Prefix (Prefix (Name (N)));
5785 end if;
5787 Freeze_Called_Function;
5788 Build_Protected_Subprogram_Call (N,
5789 Name => New_Occurrence_Of (Subp, Sloc (N)),
5790 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5791 External => True);
5793 else
5794 Rec := Expand_Protected_Object_Reference (N, Scop);
5796 if No (Rec) then
5797 return;
5798 end if;
5800 Freeze_Called_Function;
5801 Build_Protected_Subprogram_Call (N,
5802 Name => Name (N),
5803 Rec => Rec,
5804 External => False);
5806 end if;
5808 -- Analyze and resolve the new call. The actuals have already been
5809 -- resolved, but expansion of a function call will add extra actuals
5810 -- if needed. Analysis of a procedure call already includes resolution.
5812 Analyze (N);
5814 if Ekind (Subp) = E_Function then
5815 Resolve (N, Etype (Subp));
5816 end if;
5817 end Expand_Protected_Subprogram_Call;
5819 -----------------------------------
5820 -- Expand_Simple_Function_Return --
5821 -----------------------------------
5823 -- The "simple" comes from the syntax rule simple_return_statement. The
5824 -- semantics are not at all simple.
5826 procedure Expand_Simple_Function_Return (N : Node_Id) is
5827 Loc : constant Source_Ptr := Sloc (N);
5829 Scope_Id : constant Entity_Id :=
5830 Return_Applies_To (Return_Statement_Entity (N));
5831 -- The function we are returning from
5833 R_Type : constant Entity_Id := Etype (Scope_Id);
5834 -- The result type of the function
5836 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5838 Exp : constant Node_Id := Expression (N);
5839 pragma Assert (Present (Exp));
5841 Exptyp : constant Entity_Id := Etype (Exp);
5842 -- The type of the expression (not necessarily the same as R_Type)
5844 Subtype_Ind : Node_Id;
5845 -- If the result type of the function is class-wide and the expression
5846 -- has a specific type, then we use the expression's type as the type of
5847 -- the return object. In cases where the expression is an aggregate that
5848 -- is built in place, this avoids the need for an expensive conversion
5849 -- of the return object to the specific type on assignments to the
5850 -- individual components.
5852 begin
5853 if Is_Class_Wide_Type (R_Type)
5854 and then not Is_Class_Wide_Type (Etype (Exp))
5855 then
5856 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5857 else
5858 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5859 end if;
5861 -- For the case of a simple return that does not come from an extended
5862 -- return, in the case of Ada 2005 where we are returning a limited
5863 -- type, we rewrite "return <expression>;" to be:
5865 -- return _anon_ : <return_subtype> := <expression>
5867 -- The expansion produced by Expand_N_Extended_Return_Statement will
5868 -- contain simple return statements (for example, a block containing
5869 -- simple return of the return object), which brings us back here with
5870 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5871 -- checking for a simple return that does not come from an extended
5872 -- return is to avoid this infinite recursion.
5874 -- The reason for this design is that for Ada 2005 limited returns, we
5875 -- need to reify the return object, so we can build it "in place", and
5876 -- we need a block statement to hang finalization and tasking stuff.
5878 -- ??? In order to avoid disruption, we avoid translating to extended
5879 -- return except in the cases where we really need to (Ada 2005 for
5880 -- inherently limited). We might prefer to do this translation in all
5881 -- cases (except perhaps for the case of Ada 95 inherently limited),
5882 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5883 -- code. This would also allow us to do the build-in-place optimization
5884 -- for efficiency even in cases where it is semantically not required.
5886 -- As before, we check the type of the return expression rather than the
5887 -- return type of the function, because the latter may be a limited
5888 -- class-wide interface type, which is not a limited type, even though
5889 -- the type of the expression may be.
5891 if not Comes_From_Extended_Return_Statement (N)
5892 and then Is_Limited_View (Etype (Expression (N)))
5893 and then Ada_Version >= Ada_2005
5894 and then not Debug_Flag_Dot_L
5896 -- The functionality of interface thunks is simple and it is always
5897 -- handled by means of simple return statements. This leaves their
5898 -- expansion simple and clean.
5900 and then not Is_Thunk (Current_Scope)
5901 then
5902 declare
5903 Return_Object_Entity : constant Entity_Id :=
5904 Make_Temporary (Loc, 'R', Exp);
5906 Obj_Decl : constant Node_Id :=
5907 Make_Object_Declaration (Loc,
5908 Defining_Identifier => Return_Object_Entity,
5909 Object_Definition => Subtype_Ind,
5910 Expression => Exp);
5912 Ext : constant Node_Id :=
5913 Make_Extended_Return_Statement (Loc,
5914 Return_Object_Declarations => New_List (Obj_Decl));
5915 -- Do not perform this high-level optimization if the result type
5916 -- is an interface because the "this" pointer must be displaced.
5918 begin
5919 Rewrite (N, Ext);
5920 Analyze (N);
5921 return;
5922 end;
5923 end if;
5925 -- Here we have a simple return statement that is part of the expansion
5926 -- of an extended return statement (either written by the user, or
5927 -- generated by the above code).
5929 -- Always normalize C/Fortran boolean result. This is not always needed,
5930 -- but it seems a good idea to minimize the passing around of non-
5931 -- normalized values, and in any case this handles the processing of
5932 -- barrier functions for protected types, which turn the condition into
5933 -- a return statement.
5935 if Is_Boolean_Type (Exptyp)
5936 and then Nonzero_Is_True (Exptyp)
5937 then
5938 Adjust_Condition (Exp);
5939 Adjust_Result_Type (Exp, Exptyp);
5940 end if;
5942 -- Do validity check if enabled for returns
5944 if Validity_Checks_On
5945 and then Validity_Check_Returns
5946 then
5947 Ensure_Valid (Exp);
5948 end if;
5950 -- Check the result expression of a scalar function against the subtype
5951 -- of the function by inserting a conversion. This conversion must
5952 -- eventually be performed for other classes of types, but for now it's
5953 -- only done for scalars.
5954 -- ???
5956 if Is_Scalar_Type (Exptyp) then
5957 Rewrite (Exp, Convert_To (R_Type, Exp));
5959 -- The expression is resolved to ensure that the conversion gets
5960 -- expanded to generate a possible constraint check.
5962 Analyze_And_Resolve (Exp, R_Type);
5963 end if;
5965 -- Deal with returning variable length objects and controlled types
5967 -- Nothing to do if we are returning by reference, or this is not a
5968 -- type that requires special processing (indicated by the fact that
5969 -- it requires a cleanup scope for the secondary stack case).
5971 if Is_Limited_View (Exptyp)
5972 or else Is_Limited_Interface (Exptyp)
5973 then
5974 null;
5976 -- No copy needed for thunks returning interface type objects since
5977 -- the object is returned by reference and the maximum functionality
5978 -- required is just to displace the pointer.
5980 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
5981 null;
5983 -- If the call is within a thunk and the type is a limited view, the
5984 -- backend will eventually see the non-limited view of the type.
5986 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
5987 return;
5989 elsif not Requires_Transient_Scope (R_Type) then
5991 -- Mutable records with no variable length components are not
5992 -- returned on the sec-stack, so we need to make sure that the
5993 -- backend will only copy back the size of the actual value, and not
5994 -- the maximum size. We create an actual subtype for this purpose.
5996 declare
5997 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
5998 Decl : Node_Id;
5999 Ent : Entity_Id;
6000 begin
6001 if Has_Discriminants (Ubt)
6002 and then not Is_Constrained (Ubt)
6003 and then not Has_Unchecked_Union (Ubt)
6004 then
6005 Decl := Build_Actual_Subtype (Ubt, Exp);
6006 Ent := Defining_Identifier (Decl);
6007 Insert_Action (Exp, Decl);
6008 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
6009 Analyze_And_Resolve (Exp);
6010 end if;
6011 end;
6013 -- Here if secondary stack is used
6015 else
6016 -- Prevent the reclamation of the secondary stack by all enclosing
6017 -- blocks and loops as well as the related function, otherwise the
6018 -- result will be reclaimed too early or even clobbered. Due to a
6019 -- possible mix of internally generated blocks, source blocks and
6020 -- loops, the scope stack may not be contiguous as all labels are
6021 -- inserted at the top level within the related function. Instead,
6022 -- perform a parent-based traversal and mark all appropriate
6023 -- constructs.
6025 declare
6026 P : Node_Id;
6028 begin
6029 P := N;
6030 while Present (P) loop
6032 -- Mark the label of a source or internally generated block or
6033 -- loop.
6035 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
6036 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
6038 -- Mark the enclosing function
6040 elsif Nkind (P) = N_Subprogram_Body then
6041 if Present (Corresponding_Spec (P)) then
6042 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
6043 else
6044 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
6045 end if;
6047 -- Do not go beyond the enclosing function
6049 exit;
6050 end if;
6052 P := Parent (P);
6053 end loop;
6054 end;
6056 -- Optimize the case where the result is a function call. In this
6057 -- case either the result is already on the secondary stack, or is
6058 -- already being returned with the stack pointer depressed and no
6059 -- further processing is required except to set the By_Ref flag
6060 -- to ensure that gigi does not attempt an extra unnecessary copy.
6061 -- (actually not just unnecessary but harmfully wrong in the case
6062 -- of a controlled type, where gigi does not know how to do a copy).
6063 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6064 -- for array types if the constrained status of the target type is
6065 -- different from that of the expression.
6067 if Requires_Transient_Scope (Exptyp)
6068 and then
6069 (not Is_Array_Type (Exptyp)
6070 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6071 or else CW_Or_Has_Controlled_Part (Utyp))
6072 and then Nkind (Exp) = N_Function_Call
6073 then
6074 Set_By_Ref (N);
6076 -- Remove side effects from the expression now so that other parts
6077 -- of the expander do not have to reanalyze this node without this
6078 -- optimization
6080 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6082 -- For controlled types, do the allocation on the secondary stack
6083 -- manually in order to call adjust at the right time:
6085 -- type Anon1 is access R_Type;
6086 -- for Anon1'Storage_pool use ss_pool;
6087 -- Anon2 : anon1 := new R_Type'(expr);
6088 -- return Anon2.all;
6090 -- We do the same for classwide types that are not potentially
6091 -- controlled (by the virtue of restriction No_Finalization) because
6092 -- gigi is not able to properly allocate class-wide types.
6094 elsif CW_Or_Has_Controlled_Part (Utyp) then
6095 declare
6096 Loc : constant Source_Ptr := Sloc (N);
6097 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6098 Alloc_Node : Node_Id;
6099 Temp : Entity_Id;
6101 begin
6102 Set_Ekind (Acc_Typ, E_Access_Type);
6104 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6106 -- This is an allocator for the secondary stack, and it's fine
6107 -- to have Comes_From_Source set False on it, as gigi knows not
6108 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6110 Alloc_Node :=
6111 Make_Allocator (Loc,
6112 Expression =>
6113 Make_Qualified_Expression (Loc,
6114 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6115 Expression => Relocate_Node (Exp)));
6117 -- We do not want discriminant checks on the declaration,
6118 -- given that it gets its value from the allocator.
6120 Set_No_Initialization (Alloc_Node);
6122 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6124 Insert_List_Before_And_Analyze (N, New_List (
6125 Make_Full_Type_Declaration (Loc,
6126 Defining_Identifier => Acc_Typ,
6127 Type_Definition =>
6128 Make_Access_To_Object_Definition (Loc,
6129 Subtype_Indication => Subtype_Ind)),
6131 Make_Object_Declaration (Loc,
6132 Defining_Identifier => Temp,
6133 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6134 Expression => Alloc_Node)));
6136 Rewrite (Exp,
6137 Make_Explicit_Dereference (Loc,
6138 Prefix => New_Occurrence_Of (Temp, Loc)));
6140 -- Ada 2005 (AI-251): If the type of the returned object is
6141 -- an interface then add an implicit type conversion to force
6142 -- displacement of the "this" pointer.
6144 if Is_Interface (R_Type) then
6145 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6146 end if;
6148 Analyze_And_Resolve (Exp, R_Type);
6149 end;
6151 -- Otherwise use the gigi mechanism to allocate result on the
6152 -- secondary stack.
6154 else
6155 Check_Restriction (No_Secondary_Stack, N);
6156 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6158 -- If we are generating code for the VM do not use
6159 -- SS_Allocate since everything is heap-allocated anyway.
6161 if VM_Target = No_VM then
6162 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6163 end if;
6164 end if;
6165 end if;
6167 -- Implement the rules of 6.5(8-10), which require a tag check in
6168 -- the case of a limited tagged return type, and tag reassignment for
6169 -- nonlimited tagged results. These actions are needed when the return
6170 -- type is a specific tagged type and the result expression is a
6171 -- conversion or a formal parameter, because in that case the tag of
6172 -- the expression might differ from the tag of the specific result type.
6174 if Is_Tagged_Type (Utyp)
6175 and then not Is_Class_Wide_Type (Utyp)
6176 and then (Nkind_In (Exp, N_Type_Conversion,
6177 N_Unchecked_Type_Conversion)
6178 or else (Is_Entity_Name (Exp)
6179 and then Ekind (Entity (Exp)) in Formal_Kind))
6180 then
6181 -- When the return type is limited, perform a check that the tag of
6182 -- the result is the same as the tag of the return type.
6184 if Is_Limited_Type (R_Type) then
6185 Insert_Action (Exp,
6186 Make_Raise_Constraint_Error (Loc,
6187 Condition =>
6188 Make_Op_Ne (Loc,
6189 Left_Opnd =>
6190 Make_Selected_Component (Loc,
6191 Prefix => Duplicate_Subexpr (Exp),
6192 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6193 Right_Opnd =>
6194 Make_Attribute_Reference (Loc,
6195 Prefix =>
6196 New_Occurrence_Of (Base_Type (Utyp), Loc),
6197 Attribute_Name => Name_Tag)),
6198 Reason => CE_Tag_Check_Failed));
6200 -- If the result type is a specific nonlimited tagged type, then we
6201 -- have to ensure that the tag of the result is that of the result
6202 -- type. This is handled by making a copy of the expression in
6203 -- the case where it might have a different tag, namely when the
6204 -- expression is a conversion or a formal parameter. We create a new
6205 -- object of the result type and initialize it from the expression,
6206 -- which will implicitly force the tag to be set appropriately.
6208 else
6209 declare
6210 ExpR : constant Node_Id := Relocate_Node (Exp);
6211 Result_Id : constant Entity_Id :=
6212 Make_Temporary (Loc, 'R', ExpR);
6213 Result_Exp : constant Node_Id :=
6214 New_Occurrence_Of (Result_Id, Loc);
6215 Result_Obj : constant Node_Id :=
6216 Make_Object_Declaration (Loc,
6217 Defining_Identifier => Result_Id,
6218 Object_Definition =>
6219 New_Occurrence_Of (R_Type, Loc),
6220 Constant_Present => True,
6221 Expression => ExpR);
6223 begin
6224 Set_Assignment_OK (Result_Obj);
6225 Insert_Action (Exp, Result_Obj);
6227 Rewrite (Exp, Result_Exp);
6228 Analyze_And_Resolve (Exp, R_Type);
6229 end;
6230 end if;
6232 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6233 -- a check that the level of the return expression's underlying type
6234 -- is not deeper than the level of the master enclosing the function.
6235 -- Always generate the check when the type of the return expression
6236 -- is class-wide, when it's a type conversion, or when it's a formal
6237 -- parameter. Otherwise, suppress the check in the case where the
6238 -- return expression has a specific type whose level is known not to
6239 -- be statically deeper than the function's result type.
6241 -- No runtime check needed in interface thunks since it is performed
6242 -- by the target primitive associated with the thunk.
6244 -- Note: accessibility check is skipped in the VM case, since there
6245 -- does not seem to be any practical way to implement this check.
6247 elsif Ada_Version >= Ada_2005
6248 and then Tagged_Type_Expansion
6249 and then Is_Class_Wide_Type (R_Type)
6250 and then not Is_Thunk (Current_Scope)
6251 and then not Scope_Suppress.Suppress (Accessibility_Check)
6252 and then
6253 (Is_Class_Wide_Type (Etype (Exp))
6254 or else Nkind_In (Exp, N_Type_Conversion,
6255 N_Unchecked_Type_Conversion)
6256 or else (Is_Entity_Name (Exp)
6257 and then Ekind (Entity (Exp)) in Formal_Kind)
6258 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6259 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6260 then
6261 declare
6262 Tag_Node : Node_Id;
6264 begin
6265 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6266 -- "this" to reference the base of the object. This is required to
6267 -- get access to the TSD of the object.
6269 if Is_Class_Wide_Type (Etype (Exp))
6270 and then Is_Interface (Etype (Exp))
6271 then
6272 -- If the expression is an explicit dereference then we can
6273 -- directly displace the pointer to reference the base of
6274 -- the object.
6276 if Nkind (Exp) = N_Explicit_Dereference then
6277 Tag_Node :=
6278 Make_Explicit_Dereference (Loc,
6279 Prefix =>
6280 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6281 Make_Function_Call (Loc,
6282 Name =>
6283 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6284 Parameter_Associations => New_List (
6285 Unchecked_Convert_To (RTE (RE_Address),
6286 Duplicate_Subexpr (Prefix (Exp)))))));
6288 -- Similar case to the previous one but the expression is a
6289 -- renaming of an explicit dereference.
6291 elsif Nkind (Exp) = N_Identifier
6292 and then Present (Renamed_Object (Entity (Exp)))
6293 and then Nkind (Renamed_Object (Entity (Exp)))
6294 = N_Explicit_Dereference
6295 then
6296 Tag_Node :=
6297 Make_Explicit_Dereference (Loc,
6298 Prefix =>
6299 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6300 Make_Function_Call (Loc,
6301 Name =>
6302 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6303 Parameter_Associations => New_List (
6304 Unchecked_Convert_To (RTE (RE_Address),
6305 Duplicate_Subexpr
6306 (Prefix
6307 (Renamed_Object (Entity (Exp)))))))));
6309 -- Common case: obtain the address of the actual object and
6310 -- displace the pointer to reference the base of the object.
6312 else
6313 Tag_Node :=
6314 Make_Explicit_Dereference (Loc,
6315 Prefix =>
6316 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6317 Make_Function_Call (Loc,
6318 Name =>
6319 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6320 Parameter_Associations => New_List (
6321 Make_Attribute_Reference (Loc,
6322 Prefix => Duplicate_Subexpr (Exp),
6323 Attribute_Name => Name_Address)))));
6324 end if;
6325 else
6326 Tag_Node :=
6327 Make_Attribute_Reference (Loc,
6328 Prefix => Duplicate_Subexpr (Exp),
6329 Attribute_Name => Name_Tag);
6330 end if;
6332 Insert_Action (Exp,
6333 Make_Raise_Program_Error (Loc,
6334 Condition =>
6335 Make_Op_Gt (Loc,
6336 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6337 Right_Opnd =>
6338 Make_Integer_Literal (Loc,
6339 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6340 Reason => PE_Accessibility_Check_Failed));
6341 end;
6343 -- AI05-0073: If function has a controlling access result, check that
6344 -- the tag of the return value, if it is not null, matches designated
6345 -- type of return type.
6347 -- The return expression is referenced twice in the code below, so it
6348 -- must be made free of side effects. Given that different compilers
6349 -- may evaluate these parameters in different order, both occurrences
6350 -- perform a copy.
6352 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6353 and then Has_Controlling_Result (Scope_Id)
6354 then
6355 Insert_Action (N,
6356 Make_Raise_Constraint_Error (Loc,
6357 Condition =>
6358 Make_And_Then (Loc,
6359 Left_Opnd =>
6360 Make_Op_Ne (Loc,
6361 Left_Opnd => Duplicate_Subexpr (Exp),
6362 Right_Opnd => Make_Null (Loc)),
6364 Right_Opnd => Make_Op_Ne (Loc,
6365 Left_Opnd =>
6366 Make_Selected_Component (Loc,
6367 Prefix => Duplicate_Subexpr (Exp),
6368 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6370 Right_Opnd =>
6371 Make_Attribute_Reference (Loc,
6372 Prefix =>
6373 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6374 Attribute_Name => Name_Tag))),
6376 Reason => CE_Tag_Check_Failed),
6377 Suppress => All_Checks);
6378 end if;
6380 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6381 -- ensure that the function result does not outlive an
6382 -- object designated by one of it discriminants.
6384 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6385 and then Has_Unconstrained_Access_Discriminants (R_Type)
6386 then
6387 declare
6388 Discrim_Source : Node_Id;
6390 procedure Check_Against_Result_Level (Level : Node_Id);
6391 -- Check the given accessibility level against the level
6392 -- determined by the point of call. (AI05-0234).
6394 --------------------------------
6395 -- Check_Against_Result_Level --
6396 --------------------------------
6398 procedure Check_Against_Result_Level (Level : Node_Id) is
6399 begin
6400 Insert_Action (N,
6401 Make_Raise_Program_Error (Loc,
6402 Condition =>
6403 Make_Op_Gt (Loc,
6404 Left_Opnd => Level,
6405 Right_Opnd =>
6406 New_Occurrence_Of
6407 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6408 Reason => PE_Accessibility_Check_Failed));
6409 end Check_Against_Result_Level;
6411 begin
6412 Discrim_Source := Exp;
6413 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6414 Discrim_Source := Expression (Discrim_Source);
6415 end loop;
6417 if Nkind (Discrim_Source) = N_Identifier
6418 and then Is_Return_Object (Entity (Discrim_Source))
6419 then
6420 Discrim_Source := Entity (Discrim_Source);
6422 if Is_Constrained (Etype (Discrim_Source)) then
6423 Discrim_Source := Etype (Discrim_Source);
6424 else
6425 Discrim_Source := Expression (Parent (Discrim_Source));
6426 end if;
6428 elsif Nkind (Discrim_Source) = N_Identifier
6429 and then Nkind_In (Original_Node (Discrim_Source),
6430 N_Aggregate, N_Extension_Aggregate)
6431 then
6432 Discrim_Source := Original_Node (Discrim_Source);
6434 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6435 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6436 then
6437 Discrim_Source := Original_Node (Discrim_Source);
6438 end if;
6440 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6441 N_Type_Conversion,
6442 N_Unchecked_Type_Conversion)
6443 loop
6444 Discrim_Source := Expression (Discrim_Source);
6445 end loop;
6447 case Nkind (Discrim_Source) is
6448 when N_Defining_Identifier =>
6450 pragma Assert (Is_Composite_Type (Discrim_Source)
6451 and then Has_Discriminants (Discrim_Source)
6452 and then Is_Constrained (Discrim_Source));
6454 declare
6455 Discrim : Entity_Id :=
6456 First_Discriminant (Base_Type (R_Type));
6457 Disc_Elmt : Elmt_Id :=
6458 First_Elmt (Discriminant_Constraint
6459 (Discrim_Source));
6460 begin
6461 loop
6462 if Ekind (Etype (Discrim)) =
6463 E_Anonymous_Access_Type
6464 then
6465 Check_Against_Result_Level
6466 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6467 end if;
6469 Next_Elmt (Disc_Elmt);
6470 Next_Discriminant (Discrim);
6471 exit when not Present (Discrim);
6472 end loop;
6473 end;
6475 when N_Aggregate | N_Extension_Aggregate =>
6477 -- Unimplemented: extension aggregate case where discrims
6478 -- come from ancestor part, not extension part.
6480 declare
6481 Discrim : Entity_Id :=
6482 First_Discriminant (Base_Type (R_Type));
6484 Disc_Exp : Node_Id := Empty;
6486 Positionals_Exhausted
6487 : Boolean := not Present (Expressions
6488 (Discrim_Source));
6490 function Associated_Expr
6491 (Comp_Id : Entity_Id;
6492 Associations : List_Id) return Node_Id;
6494 -- Given a component and a component associations list,
6495 -- locate the expression for that component; returns
6496 -- Empty if no such expression is found.
6498 ---------------------
6499 -- Associated_Expr --
6500 ---------------------
6502 function Associated_Expr
6503 (Comp_Id : Entity_Id;
6504 Associations : List_Id) return Node_Id
6506 Assoc : Node_Id;
6507 Choice : Node_Id;
6509 begin
6510 -- Simple linear search seems ok here
6512 Assoc := First (Associations);
6513 while Present (Assoc) loop
6514 Choice := First (Choices (Assoc));
6515 while Present (Choice) loop
6516 if (Nkind (Choice) = N_Identifier
6517 and then Chars (Choice) = Chars (Comp_Id))
6518 or else (Nkind (Choice) = N_Others_Choice)
6519 then
6520 return Expression (Assoc);
6521 end if;
6523 Next (Choice);
6524 end loop;
6526 Next (Assoc);
6527 end loop;
6529 return Empty;
6530 end Associated_Expr;
6532 -- Start of processing for Expand_Simple_Function_Return
6534 begin
6535 if not Positionals_Exhausted then
6536 Disc_Exp := First (Expressions (Discrim_Source));
6537 end if;
6539 loop
6540 if Positionals_Exhausted then
6541 Disc_Exp :=
6542 Associated_Expr
6543 (Discrim,
6544 Component_Associations (Discrim_Source));
6545 end if;
6547 if Ekind (Etype (Discrim)) =
6548 E_Anonymous_Access_Type
6549 then
6550 Check_Against_Result_Level
6551 (Dynamic_Accessibility_Level (Disc_Exp));
6552 end if;
6554 Next_Discriminant (Discrim);
6555 exit when not Present (Discrim);
6557 if not Positionals_Exhausted then
6558 Next (Disc_Exp);
6559 Positionals_Exhausted := not Present (Disc_Exp);
6560 end if;
6561 end loop;
6562 end;
6564 when N_Function_Call =>
6566 -- No check needed (check performed by callee)
6568 null;
6570 when others =>
6572 declare
6573 Level : constant Node_Id :=
6574 Make_Integer_Literal (Loc,
6575 Object_Access_Level (Discrim_Source));
6577 begin
6578 -- Unimplemented: check for name prefix that includes
6579 -- a dereference of an access value with a dynamic
6580 -- accessibility level (e.g., an access param or a
6581 -- saooaaat) and use dynamic level in that case. For
6582 -- example:
6583 -- return Access_Param.all(Some_Index).Some_Component;
6584 -- ???
6586 Set_Etype (Level, Standard_Natural);
6587 Check_Against_Result_Level (Level);
6588 end;
6590 end case;
6591 end;
6592 end if;
6594 -- If we are returning an object that may not be bit-aligned, then copy
6595 -- the value into a temporary first. This copy may need to expand to a
6596 -- loop of component operations.
6598 if Is_Possibly_Unaligned_Slice (Exp)
6599 or else Is_Possibly_Unaligned_Object (Exp)
6600 then
6601 declare
6602 ExpR : constant Node_Id := Relocate_Node (Exp);
6603 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6604 begin
6605 Insert_Action (Exp,
6606 Make_Object_Declaration (Loc,
6607 Defining_Identifier => Tnn,
6608 Constant_Present => True,
6609 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6610 Expression => ExpR),
6611 Suppress => All_Checks);
6612 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6613 end;
6614 end if;
6616 -- Call the _Postconditions procedure if the related function has
6617 -- contract assertions that need to be verified on exit.
6619 if Ekind (Scope_Id) = E_Function
6620 and then Present (Postconditions_Proc (Scope_Id))
6621 then
6622 -- We are going to reference the returned value twice in this case,
6623 -- once in the call to _Postconditions, and once in the actual return
6624 -- statement, but we can't have side effects happening twice, and in
6625 -- any case for efficiency we don't want to do the computation twice.
6627 -- If the returned expression is an entity name, we don't need to
6628 -- worry since it is efficient and safe to reference it twice, that's
6629 -- also true for literals other than string literals, and for the
6630 -- case of X.all where X is an entity name.
6632 if Is_Entity_Name (Exp)
6633 or else Nkind_In (Exp, N_Character_Literal,
6634 N_Integer_Literal,
6635 N_Real_Literal)
6636 or else (Nkind (Exp) = N_Explicit_Dereference
6637 and then Is_Entity_Name (Prefix (Exp)))
6638 then
6639 null;
6641 -- Otherwise we are going to need a temporary to capture the value
6643 else
6644 declare
6645 ExpR : Node_Id := Relocate_Node (Exp);
6646 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6648 begin
6649 -- In the case of discriminated objects, we have created a
6650 -- constrained subtype above, and used the underlying type.
6651 -- This transformation is post-analysis and harmless, except
6652 -- that now the call to the post-condition will be analyzed and
6653 -- type kinds have to match.
6655 if Nkind (ExpR) = N_Unchecked_Type_Conversion
6656 and then
6657 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
6658 then
6659 ExpR := Expression (ExpR);
6660 end if;
6662 -- For a complex expression of an elementary type, capture
6663 -- value in the temporary and use it as the reference.
6665 if Is_Elementary_Type (R_Type) then
6666 Insert_Action (Exp,
6667 Make_Object_Declaration (Loc,
6668 Defining_Identifier => Tnn,
6669 Constant_Present => True,
6670 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6671 Expression => ExpR),
6672 Suppress => All_Checks);
6674 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6676 -- If we have something we can rename, generate a renaming of
6677 -- the object and replace the expression with a reference
6679 elsif Is_Object_Reference (Exp) then
6680 Insert_Action (Exp,
6681 Make_Object_Renaming_Declaration (Loc,
6682 Defining_Identifier => Tnn,
6683 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6684 Name => ExpR),
6685 Suppress => All_Checks);
6687 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6689 -- Otherwise we have something like a string literal or an
6690 -- aggregate. We could copy the value, but that would be
6691 -- inefficient. Instead we make a reference to the value and
6692 -- capture this reference with a renaming, the expression is
6693 -- then replaced by a dereference of this renaming.
6695 else
6696 -- For now, copy the value, since the code below does not
6697 -- seem to work correctly ???
6699 Insert_Action (Exp,
6700 Make_Object_Declaration (Loc,
6701 Defining_Identifier => Tnn,
6702 Constant_Present => True,
6703 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6704 Expression => Relocate_Node (Exp)),
6705 Suppress => All_Checks);
6707 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6709 -- Insert_Action (Exp,
6710 -- Make_Object_Renaming_Declaration (Loc,
6711 -- Defining_Identifier => Tnn,
6712 -- Access_Definition =>
6713 -- Make_Access_Definition (Loc,
6714 -- All_Present => True,
6715 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6716 -- Name =>
6717 -- Make_Reference (Loc,
6718 -- Prefix => Relocate_Node (Exp))),
6719 -- Suppress => All_Checks);
6721 -- Rewrite (Exp,
6722 -- Make_Explicit_Dereference (Loc,
6723 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6724 end if;
6725 end;
6726 end if;
6728 -- Generate call to _Postconditions
6730 Insert_Action (Exp,
6731 Make_Procedure_Call_Statement (Loc,
6732 Name =>
6733 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6734 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6735 end if;
6737 -- Ada 2005 (AI-251): If this return statement corresponds with an
6738 -- simple return statement associated with an extended return statement
6739 -- and the type of the returned object is an interface then generate an
6740 -- implicit conversion to force displacement of the "this" pointer.
6742 if Ada_Version >= Ada_2005
6743 and then Comes_From_Extended_Return_Statement (N)
6744 and then Nkind (Expression (N)) = N_Identifier
6745 and then Is_Interface (Utyp)
6746 and then Utyp /= Underlying_Type (Exptyp)
6747 then
6748 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6749 Analyze_And_Resolve (Exp);
6750 end if;
6751 end Expand_Simple_Function_Return;
6753 --------------------------------
6754 -- Expand_Subprogram_Contract --
6755 --------------------------------
6757 procedure Expand_Subprogram_Contract (N : Node_Id) is
6758 Body_Id : constant Entity_Id := Defining_Entity (N);
6759 Spec_Id : constant Entity_Id := Corresponding_Spec (N);
6761 procedure Add_Invariant_And_Predicate_Checks
6762 (Subp_Id : Entity_Id;
6763 Stmts : in out List_Id;
6764 Result : out Node_Id);
6765 -- Process the result of function Subp_Id (if applicable) and all its
6766 -- formals. Add invariant and predicate checks where applicable. The
6767 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6768 -- function, Result contains the entity of parameter _Result, to be
6769 -- used in the creation of procedure _Postconditions.
6771 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
6772 -- Append a node to a list. If there is no list, create a new one. When
6773 -- the item denotes a pragma, it is added to the list only when it is
6774 -- enabled.
6776 procedure Build_Postconditions_Procedure
6777 (Subp_Id : Entity_Id;
6778 Stmts : List_Id;
6779 Result : Entity_Id);
6780 -- Create the body of procedure _Postconditions which handles various
6781 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6782 -- of statements to be checked on exit. Parameter Result is the entity
6783 -- of parameter _Result when Subp_Id denotes a function.
6785 function Build_Pragma_Check_Equivalent
6786 (Prag : Node_Id;
6787 Subp_Id : Entity_Id := Empty;
6788 Inher_Id : Entity_Id := Empty) return Node_Id;
6789 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6790 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6791 -- the routine corrects the references of all formals of Inher_Id to
6792 -- point to the formals of Subp_Id.
6794 procedure Process_Contract_Cases (Stmts : in out List_Id);
6795 -- Process pragma Contract_Cases. This routine prepends items to the
6796 -- body declarations and appends items to list Stmts.
6798 procedure Process_Postconditions (Stmts : in out List_Id);
6799 -- Collect all [inherited] spec and body postconditions and accumulate
6800 -- their pragma Check equivalents in list Stmts.
6802 procedure Process_Preconditions;
6803 -- Collect all [inherited] spec and body preconditions and prepend their
6804 -- pragma Check equivalents to the declarations of the body.
6806 ----------------------------------------
6807 -- Add_Invariant_And_Predicate_Checks --
6808 ----------------------------------------
6810 procedure Add_Invariant_And_Predicate_Checks
6811 (Subp_Id : Entity_Id;
6812 Stmts : in out List_Id;
6813 Result : out Node_Id)
6815 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
6816 -- Id denotes the return value of a function or a formal parameter.
6817 -- Add an invariant check if the type of Id is access to a type with
6818 -- invariants. The routine appends the generated code to Stmts.
6820 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
6821 -- Determine whether type Typ can benefit from invariant checks. To
6822 -- qualify, the type must have a non-null invariant procedure and
6823 -- subprogram Subp_Id must appear visible from the point of view of
6824 -- the type.
6826 ---------------------------------
6827 -- Add_Invariant_Access_Checks --
6828 ---------------------------------
6830 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
6831 Loc : constant Source_Ptr := Sloc (N);
6832 Ref : Node_Id;
6833 Typ : Entity_Id;
6835 begin
6836 Typ := Etype (Id);
6838 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
6839 Typ := Designated_Type (Typ);
6841 if Invariant_Checks_OK (Typ) then
6842 Ref :=
6843 Make_Explicit_Dereference (Loc,
6844 Prefix => New_Occurrence_Of (Id, Loc));
6845 Set_Etype (Ref, Typ);
6847 -- Generate:
6848 -- if <Id> /= null then
6849 -- <invariant_call (<Ref>)>
6850 -- end if;
6852 Append_Enabled_Item
6853 (Item =>
6854 Make_If_Statement (Loc,
6855 Condition =>
6856 Make_Op_Ne (Loc,
6857 Left_Opnd => New_Occurrence_Of (Id, Loc),
6858 Right_Opnd => Make_Null (Loc)),
6859 Then_Statements => New_List (
6860 Make_Invariant_Call (Ref))),
6861 List => Stmts);
6862 end if;
6863 end if;
6864 end Add_Invariant_Access_Checks;
6866 -------------------------
6867 -- Invariant_Checks_OK --
6868 -------------------------
6870 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
6871 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
6872 -- Determine whether the body of procedure Proc_Id contains a sole
6873 -- null statement, possibly followed by an optional return.
6875 function Has_Public_Visibility_Of_Subprogram return Boolean;
6876 -- Determine whether type Typ has public visibility of subprogram
6877 -- Subp_Id.
6879 -------------------
6880 -- Has_Null_Body --
6881 -------------------
6883 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
6884 Body_Id : Entity_Id;
6885 Decl : Node_Id;
6886 Spec : Node_Id;
6887 Stmt1 : Node_Id;
6888 Stmt2 : Node_Id;
6890 begin
6891 Spec := Parent (Proc_Id);
6892 Decl := Parent (Spec);
6894 -- Retrieve the entity of the invariant procedure body
6896 if Nkind (Spec) = N_Procedure_Specification
6897 and then Nkind (Decl) = N_Subprogram_Declaration
6898 then
6899 Body_Id := Corresponding_Body (Decl);
6901 -- The body acts as a spec
6903 else
6904 Body_Id := Proc_Id;
6905 end if;
6907 -- The body will be generated later
6909 if No (Body_Id) then
6910 return False;
6911 end if;
6913 Spec := Parent (Body_Id);
6914 Decl := Parent (Spec);
6916 pragma Assert
6917 (Nkind (Spec) = N_Procedure_Specification
6918 and then Nkind (Decl) = N_Subprogram_Body);
6920 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
6922 -- Look for a null statement followed by an optional return
6923 -- statement.
6925 if Nkind (Stmt1) = N_Null_Statement then
6926 Stmt2 := Next (Stmt1);
6928 if Present (Stmt2) then
6929 return Nkind (Stmt2) = N_Simple_Return_Statement;
6930 else
6931 return True;
6932 end if;
6933 end if;
6935 return False;
6936 end Has_Null_Body;
6938 -----------------------------------------
6939 -- Has_Public_Visibility_Of_Subprogram --
6940 -----------------------------------------
6942 function Has_Public_Visibility_Of_Subprogram return Boolean is
6943 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
6945 begin
6946 -- An Initialization procedure must be considered visible even
6947 -- though it is internally generated.
6949 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
6950 return True;
6952 elsif Ekind (Scope (Typ)) /= E_Package then
6953 return False;
6955 -- Internally generated code is never publicly visible except
6956 -- for a subprogram that is the implementation of an expression
6957 -- function. In that case the visibility is determined by the
6958 -- last check.
6960 elsif not Comes_From_Source (Subp_Decl)
6961 and then
6962 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
6963 or else not
6964 Comes_From_Source (Defining_Entity (Subp_Decl)))
6965 then
6966 return False;
6968 -- Determine whether the subprogram is declared in the visible
6969 -- declarations of the package containing the type.
6971 else
6972 return List_Containing (Subp_Decl) =
6973 Visible_Declarations
6974 (Specification (Unit_Declaration_Node (Scope (Typ))));
6975 end if;
6976 end Has_Public_Visibility_Of_Subprogram;
6978 -- Start of processing for Invariant_Checks_OK
6980 begin
6981 return
6982 Has_Invariants (Typ)
6983 and then Present (Invariant_Procedure (Typ))
6984 and then not Has_Null_Body (Invariant_Procedure (Typ))
6985 and then Has_Public_Visibility_Of_Subprogram;
6986 end Invariant_Checks_OK;
6988 -- Local variables
6990 Loc : constant Source_Ptr := Sloc (N);
6991 -- Source location of subprogram contract
6993 Formal : Entity_Id;
6994 Typ : Entity_Id;
6996 -- Start of processing for Add_Invariant_And_Predicate_Checks
6998 begin
6999 Result := Empty;
7001 -- Process the result of a function
7003 if Ekind (Subp_Id) = E_Function then
7004 Typ := Etype (Subp_Id);
7006 -- Generate _Result which is used in procedure _Postconditions to
7007 -- verify the return value.
7009 Result := Make_Defining_Identifier (Loc, Name_uResult);
7010 Set_Etype (Result, Typ);
7012 -- Add an invariant check when the return type has invariants and
7013 -- the related function is visible to the outside.
7015 if Invariant_Checks_OK (Typ) then
7016 Append_Enabled_Item
7017 (Item =>
7018 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
7019 List => Stmts);
7020 end if;
7022 -- Add an invariant check when the return type is an access to a
7023 -- type with invariants.
7025 Add_Invariant_Access_Checks (Result);
7026 end if;
7028 -- Add invariant and predicates for all formals that qualify
7030 Formal := First_Formal (Subp_Id);
7031 while Present (Formal) loop
7032 Typ := Etype (Formal);
7034 if Ekind (Formal) /= E_In_Parameter
7035 or else Is_Access_Type (Typ)
7036 then
7037 if Invariant_Checks_OK (Typ) then
7038 Append_Enabled_Item
7039 (Item =>
7040 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
7041 List => Stmts);
7042 end if;
7044 Add_Invariant_Access_Checks (Formal);
7046 -- Note: we used to add predicate checks for OUT and IN OUT
7047 -- formals here, but that was misguided, since such checks are
7048 -- performed on the caller side, based on the predicate of the
7049 -- actual, rather than the predicate of the formal.
7051 end if;
7053 Next_Formal (Formal);
7054 end loop;
7055 end Add_Invariant_And_Predicate_Checks;
7057 -------------------------
7058 -- Append_Enabled_Item --
7059 -------------------------
7061 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
7062 begin
7063 -- Do not chain ignored or disabled pragmas
7065 if Nkind (Item) = N_Pragma
7066 and then (Is_Ignored (Item) or else Is_Disabled (Item))
7067 then
7068 null;
7070 -- Otherwise, add the item
7072 else
7073 if No (List) then
7074 List := New_List;
7075 end if;
7077 -- If the pragma is a conjunct in a composite postcondition, it
7078 -- has been processed in reverse order. In the postcondition body
7079 -- if must appear before the others.
7081 if Nkind (Item) = N_Pragma
7082 and then From_Aspect_Specification (Item)
7083 and then Split_PPC (Item)
7084 then
7085 Prepend (Item, List);
7086 else
7087 Append (Item, List);
7088 end if;
7089 end if;
7090 end Append_Enabled_Item;
7092 ------------------------------------
7093 -- Build_Postconditions_Procedure --
7094 ------------------------------------
7096 procedure Build_Postconditions_Procedure
7097 (Subp_Id : Entity_Id;
7098 Stmts : List_Id;
7099 Result : Entity_Id)
7101 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
7102 -- Insert node Stmt before the first source declaration of the
7103 -- related subprogram's body. If no such declaration exists, Stmt
7104 -- becomes the last declaration.
7106 --------------------------------------------
7107 -- Insert_Before_First_Source_Declaration --
7108 --------------------------------------------
7110 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
7111 Decls : constant List_Id := Declarations (N);
7112 Decl : Node_Id;
7114 begin
7115 -- Inspect the declarations of the related subprogram body looking
7116 -- for the first source declaration.
7118 if Present (Decls) then
7119 Decl := First (Decls);
7120 while Present (Decl) loop
7121 if Comes_From_Source (Decl) then
7122 Insert_Before (Decl, Stmt);
7123 return;
7124 end if;
7126 Next (Decl);
7127 end loop;
7129 -- If we get there, then the subprogram body lacks any source
7130 -- declarations. The body of _Postconditions now acts as the
7131 -- last declaration.
7133 Append (Stmt, Decls);
7135 -- Ensure that the body has a declaration list
7137 else
7138 Set_Declarations (N, New_List (Stmt));
7139 end if;
7140 end Insert_Before_First_Source_Declaration;
7142 -- Local variables
7144 Loc : constant Source_Ptr := Sloc (N);
7145 Params : List_Id := No_List;
7146 Proc_Bod : Node_Id;
7147 Proc_Id : Entity_Id;
7149 -- Start of processing for Build_Postconditions_Procedure
7151 begin
7152 -- Nothing to do if there are no actions to check on exit
7154 if No (Stmts) then
7155 return;
7156 end if;
7158 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
7159 Set_Debug_Info_Needed (Proc_Id);
7160 Set_Postconditions_Proc (Subp_Id, Proc_Id);
7162 -- The related subprogram is a function, create the specification of
7163 -- parameter _Result.
7165 if Present (Result) then
7166 Params := New_List (
7167 Make_Parameter_Specification (Loc,
7168 Defining_Identifier => Result,
7169 Parameter_Type =>
7170 New_Occurrence_Of (Etype (Result), Loc)));
7171 end if;
7173 -- Insert _Postconditions before the first source declaration of the
7174 -- body. This ensures that the body will not cause any premature
7175 -- freezing as it may mention types:
7177 -- procedure Proc (Obj : Array_Typ) is
7178 -- procedure _postconditions is
7179 -- begin
7180 -- ... Obj ...
7181 -- end _postconditions;
7183 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7184 -- begin
7186 -- In the example above, Obj is of type T but the incorrect placement
7187 -- of _Postconditions will cause a crash in gigi due to an out of
7188 -- order reference. The body of _Postconditions must be placed after
7189 -- the declaration of Temp to preserve correct visibility.
7191 -- Set an explicit End_Lavel to override the sloc of the implicit
7192 -- RETURN statement, and prevent it from inheriting the sloc of one
7193 -- the postconditions: this would cause confusing debug into to be
7194 -- produced, interfering with coverage analysis tools.
7196 Proc_Bod :=
7197 Make_Subprogram_Body (Loc,
7198 Specification =>
7199 Make_Procedure_Specification (Loc,
7200 Defining_Unit_Name => Proc_Id,
7201 Parameter_Specifications => Params),
7203 Declarations => Empty_List,
7204 Handled_Statement_Sequence =>
7205 Make_Handled_Sequence_Of_Statements (Loc,
7206 Statements => Stmts,
7207 End_Label => Make_Identifier (Loc, Chars (Proc_Id))));
7209 Insert_Before_First_Source_Declaration (Proc_Bod);
7210 Analyze (Proc_Bod);
7211 end Build_Postconditions_Procedure;
7213 -----------------------------------
7214 -- Build_Pragma_Check_Equivalent --
7215 -----------------------------------
7217 function Build_Pragma_Check_Equivalent
7218 (Prag : Node_Id;
7219 Subp_Id : Entity_Id := Empty;
7220 Inher_Id : Entity_Id := Empty) return Node_Id
7222 function Suppress_Reference (N : Node_Id) return Traverse_Result;
7223 -- Detect whether node N references a formal parameter subject to
7224 -- pragma Unreferenced. If this is the case, set Comes_From_Source
7225 -- to False to suppress the generation of a reference when analyzing
7226 -- N later on.
7228 ------------------------
7229 -- Suppress_Reference --
7230 ------------------------
7232 function Suppress_Reference (N : Node_Id) return Traverse_Result is
7233 Formal : Entity_Id;
7235 begin
7236 if Is_Entity_Name (N) and then Present (Entity (N)) then
7237 Formal := Entity (N);
7239 -- The formal parameter is subject to pragma Unreferenced.
7240 -- Prevent the generation of a reference by resetting the
7241 -- Comes_From_Source flag.
7243 if Is_Formal (Formal)
7244 and then Has_Pragma_Unreferenced (Formal)
7245 then
7246 Set_Comes_From_Source (N, False);
7247 end if;
7248 end if;
7250 return OK;
7251 end Suppress_Reference;
7253 procedure Suppress_References is
7254 new Traverse_Proc (Suppress_Reference);
7256 -- Local variables
7258 Loc : constant Source_Ptr := Sloc (Prag);
7259 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
7260 Check_Prag : Node_Id;
7261 Formals_Map : Elist_Id;
7262 Inher_Formal : Entity_Id;
7263 Msg_Arg : Node_Id;
7264 Nam : Name_Id;
7265 Subp_Formal : Entity_Id;
7267 -- Start of processing for Build_Pragma_Check_Equivalent
7269 begin
7270 Formals_Map := No_Elist;
7272 -- When the pre- or postcondition is inherited, map the formals of
7273 -- the inherited subprogram to those of the current subprogram.
7275 if Present (Inher_Id) then
7276 pragma Assert (Present (Subp_Id));
7278 Formals_Map := New_Elmt_List;
7280 -- Create a relation <inherited formal> => <subprogram formal>
7282 Inher_Formal := First_Formal (Inher_Id);
7283 Subp_Formal := First_Formal (Subp_Id);
7284 while Present (Inher_Formal) and then Present (Subp_Formal) loop
7285 Append_Elmt (Inher_Formal, Formals_Map);
7286 Append_Elmt (Subp_Formal, Formals_Map);
7288 Next_Formal (Inher_Formal);
7289 Next_Formal (Subp_Formal);
7290 end loop;
7291 end if;
7293 -- Copy the original pragma while performing substitutions (if
7294 -- applicable).
7296 Check_Prag :=
7297 New_Copy_Tree
7298 (Source => Prag,
7299 Map => Formals_Map,
7300 New_Scope => Current_Scope);
7302 -- Mark the pragma as being internally generated and reset the
7303 -- Analyzed flag.
7305 Set_Analyzed (Check_Prag, False);
7306 Set_Comes_From_Source (Check_Prag, False);
7308 -- The tree of the original pragma may contain references to the
7309 -- formal parameters of the related subprogram. At the same time
7310 -- the corresponding body may mark the formals as unreferenced:
7312 -- procedure Proc (Formal : ...)
7313 -- with Pre => Formal ...;
7315 -- procedure Proc (Formal : ...) is
7316 -- pragma Unreferenced (Formal);
7317 -- ...
7319 -- This creates problems because all pragma Check equivalents are
7320 -- analyzed at the end of the body declarations. Since all source
7321 -- references have already been accounted for, reset any references
7322 -- to such formals in the generated pragma Check equivalent.
7324 Suppress_References (Check_Prag);
7326 if Present (Corresponding_Aspect (Prag)) then
7327 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
7328 else
7329 Nam := Prag_Nam;
7330 end if;
7332 -- Convert the copy into pragma Check by correcting the name and
7333 -- adding a check_kind argument.
7335 Set_Pragma_Identifier
7336 (Check_Prag, Make_Identifier (Loc, Name_Check));
7338 Prepend_To (Pragma_Argument_Associations (Check_Prag),
7339 Make_Pragma_Argument_Association (Loc,
7340 Expression => Make_Identifier (Loc, Nam)));
7342 -- Update the error message when the pragma is inherited
7344 if Present (Inher_Id) then
7345 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
7347 if Chars (Msg_Arg) = Name_Message then
7348 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
7350 -- Insert "inherited" to improve the error message
7352 if Name_Buffer (1 .. 8) = "failed p" then
7353 Insert_Str_In_Name_Buffer ("inherited ", 8);
7354 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
7355 end if;
7356 end if;
7357 end if;
7359 return Check_Prag;
7360 end Build_Pragma_Check_Equivalent;
7362 ----------------------------
7363 -- Process_Contract_Cases --
7364 ----------------------------
7366 procedure Process_Contract_Cases (Stmts : in out List_Id) is
7367 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id);
7368 -- Process pragma Contract_Cases for subprogram Subp_Id
7370 --------------------------------
7371 -- Process_Contract_Cases_For --
7372 --------------------------------
7374 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id) is
7375 Items : constant Node_Id := Contract (Subp_Id);
7376 Prag : Node_Id;
7378 begin
7379 if Present (Items) then
7380 Prag := Contract_Test_Cases (Items);
7381 while Present (Prag) loop
7382 if Pragma_Name (Prag) = Name_Contract_Cases then
7383 Expand_Contract_Cases
7384 (CCs => Prag,
7385 Subp_Id => Subp_Id,
7386 Decls => Declarations (N),
7387 Stmts => Stmts);
7388 end if;
7390 Prag := Next_Pragma (Prag);
7391 end loop;
7392 end if;
7393 end Process_Contract_Cases_For;
7395 -- Start of processing for Process_Contract_Cases
7397 begin
7398 Process_Contract_Cases_For (Body_Id);
7400 if Present (Spec_Id) then
7401 Process_Contract_Cases_For (Spec_Id);
7402 end if;
7403 end Process_Contract_Cases;
7405 ----------------------------
7406 -- Process_Postconditions --
7407 ----------------------------
7409 procedure Process_Postconditions (Stmts : in out List_Id) is
7410 procedure Process_Body_Postconditions (Post_Nam : Name_Id);
7411 -- Collect all [refined] postconditions of a specific kind denoted
7412 -- by Post_Nam that belong to the body and generate pragma Check
7413 -- equivalents in list Stmts.
7415 procedure Process_Spec_Postconditions;
7416 -- Collect all [inherited] postconditions of the spec and generate
7417 -- pragma Check equivalents in list Stmts.
7419 ---------------------------------
7420 -- Process_Body_Postconditions --
7421 ---------------------------------
7423 procedure Process_Body_Postconditions (Post_Nam : Name_Id) is
7424 Items : constant Node_Id := Contract (Body_Id);
7425 Unit_Decl : constant Node_Id := Parent (N);
7426 Decl : Node_Id;
7427 Prag : Node_Id;
7429 begin
7430 -- Process the contract
7432 if Present (Items) then
7433 Prag := Pre_Post_Conditions (Items);
7434 while Present (Prag) loop
7435 if Pragma_Name (Prag) = Post_Nam then
7436 Append_Enabled_Item
7437 (Item => Build_Pragma_Check_Equivalent (Prag),
7438 List => Stmts);
7439 end if;
7441 Prag := Next_Pragma (Prag);
7442 end loop;
7443 end if;
7445 -- The subprogram body being processed is actually the proper body
7446 -- of a stub with a corresponding spec. The subprogram stub may
7447 -- carry a postcondition pragma in which case it must be taken
7448 -- into account. The pragma appears after the stub.
7450 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
7451 Decl := Next (Corresponding_Stub (Unit_Decl));
7452 while Present (Decl) loop
7454 -- Note that non-matching pragmas are skipped
7456 if Nkind (Decl) = N_Pragma then
7457 if Pragma_Name (Decl) = Post_Nam then
7458 Append_Enabled_Item
7459 (Item => Build_Pragma_Check_Equivalent (Decl),
7460 List => Stmts);
7461 end if;
7463 -- Skip internally generated code
7465 elsif not Comes_From_Source (Decl) then
7466 null;
7468 -- Postcondition pragmas are usually grouped together. There
7469 -- is no need to inspect the whole declarative list.
7471 else
7472 exit;
7473 end if;
7475 Next (Decl);
7476 end loop;
7477 end if;
7478 end Process_Body_Postconditions;
7480 ---------------------------------
7481 -- Process_Spec_Postconditions --
7482 ---------------------------------
7484 procedure Process_Spec_Postconditions is
7485 Subps : constant Subprogram_List :=
7486 Inherited_Subprograms (Spec_Id);
7487 Items : Node_Id;
7488 Prag : Node_Id;
7489 Subp_Id : Entity_Id;
7491 begin
7492 -- Process the contract
7494 Items := Contract (Spec_Id);
7496 if Present (Items) then
7497 Prag := Pre_Post_Conditions (Items);
7498 while Present (Prag) loop
7499 if Pragma_Name (Prag) = Name_Postcondition then
7500 Append_Enabled_Item
7501 (Item => Build_Pragma_Check_Equivalent (Prag),
7502 List => Stmts);
7503 end if;
7505 Prag := Next_Pragma (Prag);
7506 end loop;
7507 end if;
7509 -- Process the contracts of all inherited subprograms, looking for
7510 -- class-wide postconditions.
7512 for Index in Subps'Range loop
7513 Subp_Id := Subps (Index);
7514 Items := Contract (Subp_Id);
7516 if Present (Items) then
7517 Prag := Pre_Post_Conditions (Items);
7518 while Present (Prag) loop
7519 if Pragma_Name (Prag) = Name_Postcondition
7520 and then Class_Present (Prag)
7521 then
7522 Append_Enabled_Item
7523 (Item =>
7524 Build_Pragma_Check_Equivalent
7525 (Prag => Prag,
7526 Subp_Id => Spec_Id,
7527 Inher_Id => Subp_Id),
7528 List => Stmts);
7529 end if;
7531 Prag := Next_Pragma (Prag);
7532 end loop;
7533 end if;
7534 end loop;
7535 end Process_Spec_Postconditions;
7537 -- Start of processing for Process_Postconditions
7539 begin
7540 -- The processing of postconditions is done in reverse order (body
7541 -- first) to ensure the following arrangement:
7543 -- <refined postconditions from body>
7544 -- <postconditions from body>
7545 -- <postconditions from spec>
7546 -- <inherited postconditions>
7548 Process_Body_Postconditions (Name_Refined_Post);
7549 Process_Body_Postconditions (Name_Postcondition);
7551 if Present (Spec_Id) then
7552 Process_Spec_Postconditions;
7553 end if;
7554 end Process_Postconditions;
7556 ---------------------------
7557 -- Process_Preconditions --
7558 ---------------------------
7560 procedure Process_Preconditions is
7561 Class_Pre : Node_Id := Empty;
7562 -- The sole [inherited] class-wide precondition pragma that applies
7563 -- to the subprogram.
7565 Insert_Node : Node_Id := Empty;
7566 -- The insertion node after which all pragma Check equivalents are
7567 -- inserted.
7569 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
7570 -- Merge two class-wide preconditions by "or else"-ing them. The
7571 -- changes are accumulated in parameter Into. Update the error
7572 -- message of Into.
7574 procedure Prepend_To_Decls (Item : Node_Id);
7575 -- Prepend a single item to the declarations of the subprogram body
7577 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id);
7578 -- Save a class-wide precondition into Class_Pre or prepend a normal
7579 -- precondition ot the declarations of the body and analyze it.
7581 procedure Process_Inherited_Preconditions;
7582 -- Collect all inherited class-wide preconditions and merge them into
7583 -- one big precondition to be evaluated as pragma Check.
7585 procedure Process_Preconditions_For (Subp_Id : Entity_Id);
7586 -- Collect all preconditions of subprogram Subp_Id and prepend their
7587 -- pragma Check equivalents to the declarations of the body.
7589 -------------------------
7590 -- Merge_Preconditions --
7591 -------------------------
7593 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
7594 function Expression_Arg (Prag : Node_Id) return Node_Id;
7595 -- Return the boolean expression argument of a precondition while
7596 -- updating its parenteses count for the subsequent merge.
7598 function Message_Arg (Prag : Node_Id) return Node_Id;
7599 -- Return the message argument of a precondition
7601 --------------------
7602 -- Expression_Arg --
7603 --------------------
7605 function Expression_Arg (Prag : Node_Id) return Node_Id is
7606 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7607 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
7609 begin
7610 if Paren_Count (Arg) = 0 then
7611 Set_Paren_Count (Arg, 1);
7612 end if;
7614 return Arg;
7615 end Expression_Arg;
7617 -----------------
7618 -- Message_Arg --
7619 -----------------
7621 function Message_Arg (Prag : Node_Id) return Node_Id is
7622 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7623 begin
7624 return Get_Pragma_Arg (Last (Args));
7625 end Message_Arg;
7627 -- Local variables
7629 From_Expr : constant Node_Id := Expression_Arg (From);
7630 From_Msg : constant Node_Id := Message_Arg (From);
7631 Into_Expr : constant Node_Id := Expression_Arg (Into);
7632 Into_Msg : constant Node_Id := Message_Arg (Into);
7633 Loc : constant Source_Ptr := Sloc (Into);
7635 -- Start of processing for Merge_Preconditions
7637 begin
7638 -- Merge the two preconditions by "or else"-ing them
7640 Rewrite (Into_Expr,
7641 Make_Or_Else (Loc,
7642 Right_Opnd => Relocate_Node (Into_Expr),
7643 Left_Opnd => From_Expr));
7645 -- Merge the two error messages to produce a single message of the
7646 -- form:
7648 -- failed precondition from ...
7649 -- also failed inherited precondition from ...
7651 if not Exception_Locations_Suppressed then
7652 Start_String (Strval (Into_Msg));
7653 Store_String_Char (ASCII.LF);
7654 Store_String_Chars (" also ");
7655 Store_String_Chars (Strval (From_Msg));
7657 Set_Strval (Into_Msg, End_String);
7658 end if;
7659 end Merge_Preconditions;
7661 ----------------------
7662 -- Prepend_To_Decls --
7663 ----------------------
7665 procedure Prepend_To_Decls (Item : Node_Id) is
7666 Decls : List_Id := Declarations (N);
7668 begin
7669 -- Ensure that the body has a declarative list
7671 if No (Decls) then
7672 Decls := New_List;
7673 Set_Declarations (N, Decls);
7674 end if;
7676 Prepend_To (Decls, Item);
7677 end Prepend_To_Decls;
7679 ------------------------------
7680 -- Prepend_To_Decls_Or_Save --
7681 ------------------------------
7683 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id) is
7684 Check_Prag : Node_Id;
7686 begin
7687 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
7689 -- Save the sole class-wide precondition (if any) for the next
7690 -- step where it will be merged with inherited preconditions.
7692 if Class_Present (Prag) then
7693 pragma Assert (No (Class_Pre));
7694 Class_Pre := Check_Prag;
7696 -- Accumulate the corresponding Check pragmas at the top of the
7697 -- declarations. Prepending the items ensures that they will be
7698 -- evaluated in their original order.
7700 else
7701 if Present (Insert_Node) then
7702 Insert_After (Insert_Node, Check_Prag);
7703 else
7704 Prepend_To_Decls (Check_Prag);
7705 end if;
7707 Analyze (Check_Prag);
7708 end if;
7709 end Prepend_To_Decls_Or_Save;
7711 -------------------------------------
7712 -- Process_Inherited_Preconditions --
7713 -------------------------------------
7715 procedure Process_Inherited_Preconditions is
7716 Subps : constant Subprogram_List :=
7717 Inherited_Subprograms (Spec_Id);
7718 Check_Prag : Node_Id;
7719 Items : Node_Id;
7720 Prag : Node_Id;
7721 Subp_Id : Entity_Id;
7723 begin
7724 -- Process the contracts of all inherited subprograms, looking for
7725 -- class-wide preconditions.
7727 for Index in Subps'Range loop
7728 Subp_Id := Subps (Index);
7729 Items := Contract (Subp_Id);
7731 if Present (Items) then
7732 Prag := Pre_Post_Conditions (Items);
7733 while Present (Prag) loop
7734 if Pragma_Name (Prag) = Name_Precondition
7735 and then Class_Present (Prag)
7736 then
7737 Check_Prag :=
7738 Build_Pragma_Check_Equivalent
7739 (Prag => Prag,
7740 Subp_Id => Spec_Id,
7741 Inher_Id => Subp_Id);
7743 -- The spec or an inherited subprogram already yielded
7744 -- a class-wide precondition. Merge the existing
7745 -- precondition with the current one using "or else".
7747 if Present (Class_Pre) then
7748 Merge_Preconditions (Check_Prag, Class_Pre);
7749 else
7750 Class_Pre := Check_Prag;
7751 end if;
7752 end if;
7754 Prag := Next_Pragma (Prag);
7755 end loop;
7756 end if;
7757 end loop;
7759 -- Add the merged class-wide preconditions
7761 if Present (Class_Pre) then
7762 Prepend_To_Decls (Class_Pre);
7763 Analyze (Class_Pre);
7764 end if;
7765 end Process_Inherited_Preconditions;
7767 -------------------------------
7768 -- Process_Preconditions_For --
7769 -------------------------------
7771 procedure Process_Preconditions_For (Subp_Id : Entity_Id) is
7772 Items : constant Node_Id := Contract (Subp_Id);
7773 Decl : Node_Id;
7774 Prag : Node_Id;
7775 Subp_Decl : Node_Id;
7777 begin
7778 -- Process the contract
7780 if Present (Items) then
7781 Prag := Pre_Post_Conditions (Items);
7782 while Present (Prag) loop
7783 if Pragma_Name (Prag) = Name_Precondition then
7784 Prepend_To_Decls_Or_Save (Prag);
7785 end if;
7787 Prag := Next_Pragma (Prag);
7788 end loop;
7789 end if;
7791 -- The subprogram declaration being processed is actually a body
7792 -- stub. The stub may carry a precondition pragma in which case it
7793 -- must be taken into account. The pragma appears after the stub.
7795 Subp_Decl := Unit_Declaration_Node (Subp_Id);
7797 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
7799 -- Inspect the declarations following the body stub
7801 Decl := Next (Subp_Decl);
7802 while Present (Decl) loop
7804 -- Note that non-matching pragmas are skipped
7806 if Nkind (Decl) = N_Pragma then
7807 if Pragma_Name (Decl) = Name_Precondition then
7808 Prepend_To_Decls_Or_Save (Decl);
7809 end if;
7811 -- Skip internally generated code
7813 elsif not Comes_From_Source (Decl) then
7814 null;
7816 -- Preconditions are usually grouped together. There is no
7817 -- need to inspect the whole declarative list.
7819 else
7820 exit;
7821 end if;
7823 Next (Decl);
7824 end loop;
7825 end if;
7826 end Process_Preconditions_For;
7828 -- Local variables
7830 Decls : constant List_Id := Declarations (N);
7831 Decl : Node_Id;
7833 -- Start of processing for Process_Preconditions
7835 begin
7836 -- Find the last internally generate declaration starting from the
7837 -- top of the body declarations. This ensures that discriminals and
7838 -- subtypes are properly visible to the pragma Check equivalents.
7840 if Present (Decls) then
7841 Decl := First (Decls);
7842 while Present (Decl) loop
7843 exit when Comes_From_Source (Decl);
7844 Insert_Node := Decl;
7845 Next (Decl);
7846 end loop;
7847 end if;
7849 -- The processing of preconditions is done in reverse order (body
7850 -- first) because each pragma Check equivalent is inserted at the
7851 -- top of the declarations. This ensures that the final order is
7852 -- consistent with following diagram:
7854 -- <inherited preconditions>
7855 -- <preconditions from spec>
7856 -- <preconditions from body>
7858 Process_Preconditions_For (Body_Id);
7860 if Present (Spec_Id) then
7861 Process_Preconditions_For (Spec_Id);
7862 Process_Inherited_Preconditions;
7863 end if;
7864 end Process_Preconditions;
7866 -- Local variables
7868 Restore_Scope : Boolean := False;
7869 Result : Entity_Id;
7870 Stmts : List_Id := No_List;
7871 Subp_Id : Entity_Id;
7873 -- Start of processing for Expand_Subprogram_Contract
7875 begin
7876 -- Obtain the entity of the initial declaration
7878 if Present (Spec_Id) then
7879 Subp_Id := Spec_Id;
7880 else
7881 Subp_Id := Body_Id;
7882 end if;
7884 -- Do not perform expansion activity when it is not needed
7886 if not Expander_Active then
7887 return;
7889 -- ASIS requires an unaltered tree
7891 elsif ASIS_Mode then
7892 return;
7894 -- GNATprove does not need the executable semantics of a contract
7896 elsif GNATprove_Mode then
7897 return;
7899 -- The contract of a generic subprogram or one declared in a generic
7900 -- context is not expanded as the corresponding instance will provide
7901 -- the executable semantics of the contract.
7903 elsif Is_Generic_Subprogram (Subp_Id) or else Inside_A_Generic then
7904 return;
7906 -- All subprograms carry a contract, but for some it is not significant
7907 -- and should not be processed. This is a small optimization.
7909 elsif not Has_Significant_Contract (Subp_Id) then
7910 return;
7911 end if;
7913 -- Do not re-expand the same contract. This scenario occurs when a
7914 -- construct is rewritten into something else during its analysis
7915 -- (expression functions for instance).
7917 if Has_Expanded_Contract (Subp_Id) then
7918 return;
7920 -- Otherwise mark the subprogram
7922 else
7923 Set_Has_Expanded_Contract (Subp_Id);
7924 end if;
7926 -- Ensure that the formal parameters are visible when expanding all
7927 -- contract items.
7929 if not In_Open_Scopes (Subp_Id) then
7930 Restore_Scope := True;
7931 Push_Scope (Subp_Id);
7933 if Is_Generic_Subprogram (Subp_Id) then
7934 Install_Generic_Formals (Subp_Id);
7935 else
7936 Install_Formals (Subp_Id);
7937 end if;
7938 end if;
7940 -- The expansion of a subprogram contract involves the creation of Check
7941 -- pragmas to verify the contract assertions of the spec and body in a
7942 -- particular order. The order is as follows:
7944 -- function Example (...) return ... is
7945 -- procedure _Postconditions (...) is
7946 -- begin
7947 -- <refined postconditions from body>
7948 -- <postconditions from body>
7949 -- <postconditions from spec>
7950 -- <inherited postconditions>
7951 -- <contract case consequences>
7952 -- <invariant check of function result>
7953 -- <invariant and predicate checks of parameters>
7954 -- end _Postconditions;
7956 -- <inherited preconditions>
7957 -- <preconditions from spec>
7958 -- <preconditions from body>
7959 -- <contract case conditions>
7961 -- <source declarations>
7962 -- begin
7963 -- <source statements>
7965 -- _Preconditions (Result);
7966 -- return Result;
7967 -- end Example;
7969 -- Routine _Postconditions holds all contract assertions that must be
7970 -- verified on exit from the related subprogram.
7972 -- Step 1: Handle all preconditions. This action must come before the
7973 -- processing of pragma Contract_Cases because the pragma prepends items
7974 -- to the body declarations.
7976 Process_Preconditions;
7978 -- Step 2: Handle all postconditions. This action must come before the
7979 -- processing of pragma Contract_Cases because the pragma appends items
7980 -- to list Stmts.
7982 Process_Postconditions (Stmts);
7984 -- Step 3: Handle pragma Contract_Cases. This action must come before
7985 -- the processing of invariants and predicates because those append
7986 -- items to list Smts.
7988 Process_Contract_Cases (Stmts);
7990 -- Step 4: Apply invariant and predicate checks on a function result and
7991 -- all formals. The resulting checks are accumulated in list Stmts.
7993 Add_Invariant_And_Predicate_Checks (Subp_Id, Stmts, Result);
7995 -- Step 5: Construct procedure _Postconditions
7997 Build_Postconditions_Procedure (Subp_Id, Stmts, Result);
7999 if Restore_Scope then
8000 End_Scope;
8001 end if;
8002 end Expand_Subprogram_Contract;
8004 --------------------------------------------
8005 -- Has_Unconstrained_Access_Discriminants --
8006 --------------------------------------------
8008 function Has_Unconstrained_Access_Discriminants
8009 (Subtyp : Entity_Id) return Boolean
8011 Discr : Entity_Id;
8013 begin
8014 if Has_Discriminants (Subtyp)
8015 and then not Is_Constrained (Subtyp)
8016 then
8017 Discr := First_Discriminant (Subtyp);
8018 while Present (Discr) loop
8019 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
8020 return True;
8021 end if;
8023 Next_Discriminant (Discr);
8024 end loop;
8025 end if;
8027 return False;
8028 end Has_Unconstrained_Access_Discriminants;
8030 ----------------
8031 -- Initialize --
8032 ----------------
8034 procedure Initialize is
8035 begin
8036 Unest_Bodies.Init;
8037 end Initialize;
8039 --------------------------------
8040 -- Is_Build_In_Place_Function --
8041 --------------------------------
8043 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
8044 begin
8045 -- This function is called from Expand_Subtype_From_Expr during
8046 -- semantic analysis, even when expansion is off. In those cases
8047 -- the build_in_place expansion will not take place.
8049 if not Expander_Active then
8050 return False;
8051 end if;
8053 -- For now we test whether E denotes a function or access-to-function
8054 -- type whose result subtype is inherently limited. Later this test
8055 -- may be revised to allow composite nonlimited types. Functions with
8056 -- a foreign convention or whose result type has a foreign convention
8057 -- never qualify.
8059 if Ekind_In (E, E_Function, E_Generic_Function)
8060 or else (Ekind (E) = E_Subprogram_Type
8061 and then Etype (E) /= Standard_Void_Type)
8062 then
8063 -- Note: If the function has a foreign convention, it cannot build
8064 -- its result in place, so you're on your own. On the other hand,
8065 -- if only the return type has a foreign convention, its layout is
8066 -- intended to be compatible with the other language, but the build-
8067 -- in place machinery can ensure that the object is not copied.
8069 if Has_Foreign_Convention (E) then
8070 return False;
8072 -- In Ada 2005 all functions with an inherently limited return type
8073 -- must be handled using a build-in-place profile, including the case
8074 -- of a function with a limited interface result, where the function
8075 -- may return objects of nonlimited descendants.
8077 else
8078 return Is_Limited_View (Etype (E))
8079 and then Ada_Version >= Ada_2005
8080 and then not Debug_Flag_Dot_L;
8081 end if;
8083 else
8084 return False;
8085 end if;
8086 end Is_Build_In_Place_Function;
8088 -------------------------------------
8089 -- Is_Build_In_Place_Function_Call --
8090 -------------------------------------
8092 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
8093 Exp_Node : Node_Id := N;
8094 Function_Id : Entity_Id;
8096 begin
8097 -- Return False if the expander is currently inactive, since awareness
8098 -- of build-in-place treatment is only relevant during expansion. Note
8099 -- that Is_Build_In_Place_Function, which is called as part of this
8100 -- function, is also conditioned this way, but we need to check here as
8101 -- well to avoid blowing up on processing protected calls when expansion
8102 -- is disabled (such as with -gnatc) since those would trip over the
8103 -- raise of Program_Error below.
8105 -- In SPARK mode, build-in-place calls are not expanded, so that we
8106 -- may end up with a call that is neither resolved to an entity, nor
8107 -- an indirect call.
8109 if not Expander_Active then
8110 return False;
8111 end if;
8113 -- Step past qualification or unchecked conversion (the latter can occur
8114 -- in cases of calls to 'Input).
8116 if Nkind_In (Exp_Node, N_Qualified_Expression,
8117 N_Unchecked_Type_Conversion)
8118 then
8119 Exp_Node := Expression (N);
8120 end if;
8122 if Nkind (Exp_Node) /= N_Function_Call then
8123 return False;
8125 else
8126 if Is_Entity_Name (Name (Exp_Node)) then
8127 Function_Id := Entity (Name (Exp_Node));
8129 -- In the case of an explicitly dereferenced call, use the subprogram
8130 -- type generated for the dereference.
8132 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
8133 Function_Id := Etype (Name (Exp_Node));
8135 -- This may be a call to a protected function.
8137 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
8138 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
8140 else
8141 raise Program_Error;
8142 end if;
8144 return Is_Build_In_Place_Function (Function_Id);
8145 end if;
8146 end Is_Build_In_Place_Function_Call;
8148 -----------------------
8149 -- Freeze_Subprogram --
8150 -----------------------
8152 procedure Freeze_Subprogram (N : Node_Id) is
8153 Loc : constant Source_Ptr := Sloc (N);
8155 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
8156 -- (Ada 2005): Register a predefined primitive in all the secondary
8157 -- dispatch tables of its primitive type.
8159 ----------------------------------
8160 -- Register_Predefined_DT_Entry --
8161 ----------------------------------
8163 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
8164 Iface_DT_Ptr : Elmt_Id;
8165 Tagged_Typ : Entity_Id;
8166 Thunk_Id : Entity_Id;
8167 Thunk_Code : Node_Id;
8169 begin
8170 Tagged_Typ := Find_Dispatching_Type (Prim);
8172 if No (Access_Disp_Table (Tagged_Typ))
8173 or else not Has_Interfaces (Tagged_Typ)
8174 or else not RTE_Available (RE_Interface_Tag)
8175 or else Restriction_Active (No_Dispatching_Calls)
8176 then
8177 return;
8178 end if;
8180 -- Skip the first two access-to-dispatch-table pointers since they
8181 -- leads to the primary dispatch table (predefined DT and user
8182 -- defined DT). We are only concerned with the secondary dispatch
8183 -- table pointers. Note that the access-to- dispatch-table pointer
8184 -- corresponds to the first implemented interface retrieved below.
8186 Iface_DT_Ptr :=
8187 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
8189 while Present (Iface_DT_Ptr)
8190 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
8191 loop
8192 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8193 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
8195 if Present (Thunk_Code) then
8196 Insert_Actions_After (N, New_List (
8197 Thunk_Code,
8199 Build_Set_Predefined_Prim_Op_Address (Loc,
8200 Tag_Node =>
8201 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
8202 Position => DT_Position (Prim),
8203 Address_Node =>
8204 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8205 Make_Attribute_Reference (Loc,
8206 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
8207 Attribute_Name => Name_Unrestricted_Access))),
8209 Build_Set_Predefined_Prim_Op_Address (Loc,
8210 Tag_Node =>
8211 New_Occurrence_Of
8212 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
8213 Loc),
8214 Position => DT_Position (Prim),
8215 Address_Node =>
8216 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8217 Make_Attribute_Reference (Loc,
8218 Prefix => New_Occurrence_Of (Prim, Loc),
8219 Attribute_Name => Name_Unrestricted_Access)))));
8220 end if;
8222 -- Skip the tag of the predefined primitives dispatch table
8224 Next_Elmt (Iface_DT_Ptr);
8225 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8227 -- Skip tag of the no-thunks dispatch table
8229 Next_Elmt (Iface_DT_Ptr);
8230 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8232 -- Skip tag of predefined primitives no-thunks dispatch table
8234 Next_Elmt (Iface_DT_Ptr);
8235 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8237 Next_Elmt (Iface_DT_Ptr);
8238 end loop;
8239 end Register_Predefined_DT_Entry;
8241 -- Local variables
8243 Subp : constant Entity_Id := Entity (N);
8245 -- Start of processing for Freeze_Subprogram
8247 begin
8248 -- We suppress the initialization of the dispatch table entry when
8249 -- VM_Target because the dispatching mechanism is handled internally
8250 -- by the VM.
8252 if Is_Dispatching_Operation (Subp)
8253 and then not Is_Abstract_Subprogram (Subp)
8254 and then Present (DTC_Entity (Subp))
8255 and then Present (Scope (DTC_Entity (Subp)))
8256 and then Tagged_Type_Expansion
8257 and then not Restriction_Active (No_Dispatching_Calls)
8258 and then RTE_Available (RE_Tag)
8259 then
8260 declare
8261 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
8263 begin
8264 -- Handle private overridden primitives
8266 if not Is_CPP_Class (Typ) then
8267 Check_Overriding_Operation (Subp);
8268 end if;
8270 -- We assume that imported CPP primitives correspond with objects
8271 -- whose constructor is in the CPP side; therefore we don't need
8272 -- to generate code to register them in the dispatch table.
8274 if Is_CPP_Class (Typ) then
8275 null;
8277 -- Handle CPP primitives found in derivations of CPP_Class types.
8278 -- These primitives must have been inherited from some parent, and
8279 -- there is no need to register them in the dispatch table because
8280 -- Build_Inherit_Prims takes care of initializing these slots.
8282 elsif Is_Imported (Subp)
8283 and then (Convention (Subp) = Convention_CPP
8284 or else Convention (Subp) = Convention_C)
8285 then
8286 null;
8288 -- Generate code to register the primitive in non statically
8289 -- allocated dispatch tables
8291 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8293 -- When a primitive is frozen, enter its name in its dispatch
8294 -- table slot.
8296 if not Is_Interface (Typ)
8297 or else Present (Interface_Alias (Subp))
8298 then
8299 if Is_Predefined_Dispatching_Operation (Subp) then
8300 Register_Predefined_DT_Entry (Subp);
8301 end if;
8303 Insert_Actions_After (N,
8304 Register_Primitive (Loc, Prim => Subp));
8305 end if;
8306 end if;
8307 end;
8308 end if;
8310 -- Mark functions that return by reference. Note that it cannot be part
8311 -- of the normal semantic analysis of the spec since the underlying
8312 -- returned type may not be known yet (for private types).
8314 declare
8315 Typ : constant Entity_Id := Etype (Subp);
8316 Utyp : constant Entity_Id := Underlying_Type (Typ);
8317 begin
8318 if Is_Limited_View (Typ) then
8319 Set_Returns_By_Ref (Subp);
8320 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8321 Set_Returns_By_Ref (Subp);
8322 end if;
8323 end;
8325 -- Wnen freezing a null procedure, analyze its delayed aspects now
8326 -- because we may not have reached the end of the declarative list when
8327 -- delayed aspects are normally analyzed. This ensures that dispatching
8328 -- calls are properly rewritten when the generated _Postcondition
8329 -- procedure is analyzed in the null procedure body.
8331 if Nkind (Parent (Subp)) = N_Procedure_Specification
8332 and then Null_Present (Parent (Subp))
8333 then
8334 Analyze_Subprogram_Contract (Subp);
8335 end if;
8336 end Freeze_Subprogram;
8338 -----------------------
8339 -- Is_Null_Procedure --
8340 -----------------------
8342 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8343 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8345 begin
8346 if Ekind (Subp) /= E_Procedure then
8347 return False;
8349 -- Check if this is a declared null procedure
8351 elsif Nkind (Decl) = N_Subprogram_Declaration then
8352 if not Null_Present (Specification (Decl)) then
8353 return False;
8355 elsif No (Body_To_Inline (Decl)) then
8356 return False;
8358 -- Check if the body contains only a null statement, followed by
8359 -- the return statement added during expansion.
8361 else
8362 declare
8363 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8365 Stat : Node_Id;
8366 Stat2 : Node_Id;
8368 begin
8369 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8370 return False;
8371 else
8372 -- We must skip SCIL nodes because they are currently
8373 -- implemented as special N_Null_Statement nodes.
8375 Stat :=
8376 First_Non_SCIL_Node
8377 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8378 Stat2 := Next_Non_SCIL_Node (Stat);
8380 return
8381 Is_Empty_List (Declarations (Orig_Bod))
8382 and then Nkind (Stat) = N_Null_Statement
8383 and then
8384 (No (Stat2)
8385 or else
8386 (Nkind (Stat2) = N_Simple_Return_Statement
8387 and then No (Next (Stat2))));
8388 end if;
8389 end;
8390 end if;
8392 else
8393 return False;
8394 end if;
8395 end Is_Null_Procedure;
8397 -------------------------------------------
8398 -- Make_Build_In_Place_Call_In_Allocator --
8399 -------------------------------------------
8401 procedure Make_Build_In_Place_Call_In_Allocator
8402 (Allocator : Node_Id;
8403 Function_Call : Node_Id)
8405 Acc_Type : constant Entity_Id := Etype (Allocator);
8406 Loc : Source_Ptr;
8407 Func_Call : Node_Id := Function_Call;
8408 Ref_Func_Call : Node_Id;
8409 Function_Id : Entity_Id;
8410 Result_Subt : Entity_Id;
8411 New_Allocator : Node_Id;
8412 Return_Obj_Access : Entity_Id; -- temp for function result
8413 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8414 Alloc_Form : BIP_Allocation_Form;
8415 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8416 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8417 Chain : Entity_Id; -- activation chain, in case of tasks
8419 begin
8420 -- Step past qualification or unchecked conversion (the latter can occur
8421 -- in cases of calls to 'Input).
8423 if Nkind_In (Func_Call,
8424 N_Qualified_Expression,
8425 N_Unchecked_Type_Conversion)
8426 then
8427 Func_Call := Expression (Func_Call);
8428 end if;
8430 -- If the call has already been processed to add build-in-place actuals
8431 -- then return. This should not normally occur in an allocator context,
8432 -- but we add the protection as a defensive measure.
8434 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8435 return;
8436 end if;
8438 -- Mark the call as processed as a build-in-place call
8440 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8442 Loc := Sloc (Function_Call);
8444 if Is_Entity_Name (Name (Func_Call)) then
8445 Function_Id := Entity (Name (Func_Call));
8447 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8448 Function_Id := Etype (Name (Func_Call));
8450 else
8451 raise Program_Error;
8452 end if;
8454 Result_Subt := Available_View (Etype (Function_Id));
8456 -- Create a temp for the function result. In the caller-allocates case,
8457 -- this will be initialized to the result of a new uninitialized
8458 -- allocator. Note: we do not use Allocator as the Related_Node of
8459 -- Return_Obj_Access in call to Make_Temporary below as this would
8460 -- create a sort of infinite "recursion".
8462 Return_Obj_Access := Make_Temporary (Loc, 'R');
8463 Set_Etype (Return_Obj_Access, Acc_Type);
8465 -- When the result subtype is constrained, the return object is
8466 -- allocated on the caller side, and access to it is passed to the
8467 -- function.
8469 -- Here and in related routines, we must examine the full view of the
8470 -- type, because the view at the point of call may differ from that
8471 -- that in the function body, and the expansion mechanism depends on
8472 -- the characteristics of the full view.
8474 if Is_Constrained (Underlying_Type (Result_Subt)) then
8476 -- Replace the initialized allocator of form "new T'(Func (...))"
8477 -- with an uninitialized allocator of form "new T", where T is the
8478 -- result subtype of the called function. The call to the function
8479 -- is handled separately further below.
8481 New_Allocator :=
8482 Make_Allocator (Loc,
8483 Expression => New_Occurrence_Of (Result_Subt, Loc));
8484 Set_No_Initialization (New_Allocator);
8486 -- Copy attributes to new allocator. Note that the new allocator
8487 -- logically comes from source if the original one did, so copy the
8488 -- relevant flag. This ensures proper treatment of the restriction
8489 -- No_Implicit_Heap_Allocations in this case.
8491 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8492 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8493 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8495 Rewrite (Allocator, New_Allocator);
8497 -- Initial value of the temp is the result of the uninitialized
8498 -- allocator
8500 Temp_Init := Relocate_Node (Allocator);
8502 -- Indicate that caller allocates, and pass in the return object
8504 Alloc_Form := Caller_Allocation;
8505 Pool := Make_Null (No_Location);
8506 Return_Obj_Actual :=
8507 Make_Unchecked_Type_Conversion (Loc,
8508 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8509 Expression =>
8510 Make_Explicit_Dereference (Loc,
8511 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8513 -- When the result subtype is unconstrained, the function itself must
8514 -- perform the allocation of the return object, so we pass parameters
8515 -- indicating that.
8517 else
8518 Temp_Init := Empty;
8520 -- Case of a user-defined storage pool. Pass an allocation parameter
8521 -- indicating that the function should allocate its result in the
8522 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8523 -- pool may not be aliased.
8525 if VM_Target = No_VM
8526 and then Present (Associated_Storage_Pool (Acc_Type))
8527 then
8528 Alloc_Form := User_Storage_Pool;
8529 Pool :=
8530 Make_Attribute_Reference (Loc,
8531 Prefix =>
8532 New_Occurrence_Of
8533 (Associated_Storage_Pool (Acc_Type), Loc),
8534 Attribute_Name => Name_Unrestricted_Access);
8536 -- No user-defined pool; pass an allocation parameter indicating that
8537 -- the function should allocate its result on the heap.
8539 else
8540 Alloc_Form := Global_Heap;
8541 Pool := Make_Null (No_Location);
8542 end if;
8544 -- The caller does not provide the return object in this case, so we
8545 -- have to pass null for the object access actual.
8547 Return_Obj_Actual := Empty;
8548 end if;
8550 -- Declare the temp object
8552 Insert_Action (Allocator,
8553 Make_Object_Declaration (Loc,
8554 Defining_Identifier => Return_Obj_Access,
8555 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8556 Expression => Temp_Init));
8558 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8560 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8561 -- then generate an implicit conversion to force displacement of the
8562 -- "this" pointer.
8564 if Is_Interface (Designated_Type (Acc_Type)) then
8565 Rewrite
8566 (Ref_Func_Call,
8567 OK_Convert_To (Acc_Type, Ref_Func_Call));
8568 end if;
8570 declare
8571 Assign : constant Node_Id :=
8572 Make_Assignment_Statement (Loc,
8573 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8574 Expression => Ref_Func_Call);
8575 -- Assign the result of the function call into the temp. In the
8576 -- caller-allocates case, this is overwriting the temp with its
8577 -- initial value, which has no effect. In the callee-allocates case,
8578 -- this is setting the temp to point to the object allocated by the
8579 -- callee.
8581 Actions : List_Id;
8582 -- Actions to be inserted. If there are no tasks, this is just the
8583 -- assignment statement. If the allocated object has tasks, we need
8584 -- to wrap the assignment in a block that activates them. The
8585 -- activation chain of that block must be passed to the function,
8586 -- rather than some outer chain.
8587 begin
8588 if Has_Task (Result_Subt) then
8589 Actions := New_List;
8590 Build_Task_Allocate_Block_With_Init_Stmts
8591 (Actions, Allocator, Init_Stmts => New_List (Assign));
8592 Chain := Activation_Chain_Entity (Last (Actions));
8593 else
8594 Actions := New_List (Assign);
8595 Chain := Empty;
8596 end if;
8598 Insert_Actions (Allocator, Actions);
8599 end;
8601 -- When the function has a controlling result, an allocation-form
8602 -- parameter must be passed indicating that the caller is allocating
8603 -- the result object. This is needed because such a function can be
8604 -- called as a dispatching operation and must be treated similarly
8605 -- to functions with unconstrained result subtypes.
8607 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8608 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8610 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8611 (Func_Call, Function_Id, Acc_Type);
8613 Add_Task_Actuals_To_Build_In_Place_Call
8614 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8615 Chain => Chain);
8617 -- Add an implicit actual to the function call that provides access
8618 -- to the allocated object. An unchecked conversion to the (specific)
8619 -- result subtype of the function is inserted to handle cases where
8620 -- the access type of the allocator has a class-wide designated type.
8622 Add_Access_Actual_To_Build_In_Place_Call
8623 (Func_Call, Function_Id, Return_Obj_Actual);
8625 -- Finally, replace the allocator node with a reference to the temp
8627 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8629 Analyze_And_Resolve (Allocator, Acc_Type);
8630 end Make_Build_In_Place_Call_In_Allocator;
8632 ---------------------------------------------------
8633 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8634 ---------------------------------------------------
8636 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8637 (Function_Call : Node_Id)
8639 Loc : Source_Ptr;
8640 Func_Call : Node_Id := Function_Call;
8641 Function_Id : Entity_Id;
8642 Result_Subt : Entity_Id;
8643 Return_Obj_Id : Entity_Id;
8644 Return_Obj_Decl : Entity_Id;
8646 begin
8647 -- Step past qualification or unchecked conversion (the latter can occur
8648 -- in cases of calls to 'Input).
8650 if Nkind_In (Func_Call, N_Qualified_Expression,
8651 N_Unchecked_Type_Conversion)
8652 then
8653 Func_Call := Expression (Func_Call);
8654 end if;
8656 -- If the call has already been processed to add build-in-place actuals
8657 -- then return. One place this can occur is for calls to build-in-place
8658 -- functions that occur within a call to a protected operation, where
8659 -- due to rewriting and expansion of the protected call there can be
8660 -- more than one call to Expand_Actuals for the same set of actuals.
8662 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8663 return;
8664 end if;
8666 -- Mark the call as processed as a build-in-place call
8668 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8670 Loc := Sloc (Function_Call);
8672 if Is_Entity_Name (Name (Func_Call)) then
8673 Function_Id := Entity (Name (Func_Call));
8675 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8676 Function_Id := Etype (Name (Func_Call));
8678 else
8679 raise Program_Error;
8680 end if;
8682 Result_Subt := Etype (Function_Id);
8684 -- If the build-in-place function returns a controlled object, then the
8685 -- object needs to be finalized immediately after the context. Since
8686 -- this case produces a transient scope, the servicing finalizer needs
8687 -- to name the returned object. Create a temporary which is initialized
8688 -- with the function call:
8690 -- Temp_Id : Func_Type := BIP_Func_Call;
8692 -- The initialization expression of the temporary will be rewritten by
8693 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8694 -- Call_In_Object_Declaration.
8696 if Needs_Finalization (Result_Subt) then
8697 declare
8698 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8699 Temp_Decl : Node_Id;
8701 begin
8702 -- Reset the guard on the function call since the following does
8703 -- not perform actual call expansion.
8705 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8707 Temp_Decl :=
8708 Make_Object_Declaration (Loc,
8709 Defining_Identifier => Temp_Id,
8710 Object_Definition =>
8711 New_Occurrence_Of (Result_Subt, Loc),
8712 Expression =>
8713 New_Copy_Tree (Function_Call));
8715 Insert_Action (Function_Call, Temp_Decl);
8717 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8718 Analyze (Function_Call);
8719 end;
8721 -- When the result subtype is constrained, an object of the subtype is
8722 -- declared and an access value designating it is passed as an actual.
8724 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8726 -- Create a temporary object to hold the function result
8728 Return_Obj_Id := Make_Temporary (Loc, 'R');
8729 Set_Etype (Return_Obj_Id, Result_Subt);
8731 Return_Obj_Decl :=
8732 Make_Object_Declaration (Loc,
8733 Defining_Identifier => Return_Obj_Id,
8734 Aliased_Present => True,
8735 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8737 Set_No_Initialization (Return_Obj_Decl);
8739 Insert_Action (Func_Call, Return_Obj_Decl);
8741 -- When the function has a controlling result, an allocation-form
8742 -- parameter must be passed indicating that the caller is allocating
8743 -- the result object. This is needed because such a function can be
8744 -- called as a dispatching operation and must be treated similarly
8745 -- to functions with unconstrained result subtypes.
8747 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8748 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8750 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8751 (Func_Call, Function_Id);
8753 Add_Task_Actuals_To_Build_In_Place_Call
8754 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8756 -- Add an implicit actual to the function call that provides access
8757 -- to the caller's return object.
8759 Add_Access_Actual_To_Build_In_Place_Call
8760 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8762 -- When the result subtype is unconstrained, the function must allocate
8763 -- the return object in the secondary stack, so appropriate implicit
8764 -- parameters are added to the call to indicate that. A transient
8765 -- scope is established to ensure eventual cleanup of the result.
8767 else
8768 -- Pass an allocation parameter indicating that the function should
8769 -- allocate its result on the secondary stack.
8771 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8772 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8774 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8775 (Func_Call, Function_Id);
8777 Add_Task_Actuals_To_Build_In_Place_Call
8778 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8780 -- Pass a null value to the function since no return object is
8781 -- available on the caller side.
8783 Add_Access_Actual_To_Build_In_Place_Call
8784 (Func_Call, Function_Id, Empty);
8785 end if;
8786 end Make_Build_In_Place_Call_In_Anonymous_Context;
8788 --------------------------------------------
8789 -- Make_Build_In_Place_Call_In_Assignment --
8790 --------------------------------------------
8792 procedure Make_Build_In_Place_Call_In_Assignment
8793 (Assign : Node_Id;
8794 Function_Call : Node_Id)
8796 Lhs : constant Node_Id := Name (Assign);
8797 Func_Call : Node_Id := Function_Call;
8798 Func_Id : Entity_Id;
8799 Loc : Source_Ptr;
8800 Obj_Decl : Node_Id;
8801 Obj_Id : Entity_Id;
8802 Ptr_Typ : Entity_Id;
8803 Ptr_Typ_Decl : Node_Id;
8804 New_Expr : Node_Id;
8805 Result_Subt : Entity_Id;
8806 Target : Node_Id;
8808 begin
8809 -- Step past qualification or unchecked conversion (the latter can occur
8810 -- in cases of calls to 'Input).
8812 if Nkind_In (Func_Call, N_Qualified_Expression,
8813 N_Unchecked_Type_Conversion)
8814 then
8815 Func_Call := Expression (Func_Call);
8816 end if;
8818 -- If the call has already been processed to add build-in-place actuals
8819 -- then return. This should not normally occur in an assignment context,
8820 -- but we add the protection as a defensive measure.
8822 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8823 return;
8824 end if;
8826 -- Mark the call as processed as a build-in-place call
8828 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8830 Loc := Sloc (Function_Call);
8832 if Is_Entity_Name (Name (Func_Call)) then
8833 Func_Id := Entity (Name (Func_Call));
8835 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8836 Func_Id := Etype (Name (Func_Call));
8838 else
8839 raise Program_Error;
8840 end if;
8842 Result_Subt := Etype (Func_Id);
8844 -- When the result subtype is unconstrained, an additional actual must
8845 -- be passed to indicate that the caller is providing the return object.
8846 -- This parameter must also be passed when the called function has a
8847 -- controlling result, because dispatching calls to the function needs
8848 -- to be treated effectively the same as calls to class-wide functions.
8850 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8851 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8853 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8854 (Func_Call, Func_Id);
8856 Add_Task_Actuals_To_Build_In_Place_Call
8857 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8859 -- Add an implicit actual to the function call that provides access to
8860 -- the caller's return object.
8862 Add_Access_Actual_To_Build_In_Place_Call
8863 (Func_Call,
8864 Func_Id,
8865 Make_Unchecked_Type_Conversion (Loc,
8866 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8867 Expression => Relocate_Node (Lhs)));
8869 -- Create an access type designating the function's result subtype
8871 Ptr_Typ := Make_Temporary (Loc, 'A');
8873 Ptr_Typ_Decl :=
8874 Make_Full_Type_Declaration (Loc,
8875 Defining_Identifier => Ptr_Typ,
8876 Type_Definition =>
8877 Make_Access_To_Object_Definition (Loc,
8878 All_Present => True,
8879 Subtype_Indication =>
8880 New_Occurrence_Of (Result_Subt, Loc)));
8881 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8883 -- Finally, create an access object initialized to a reference to the
8884 -- function call. We know this access value is non-null, so mark the
8885 -- entity accordingly to suppress junk access checks.
8887 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8889 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8890 Set_Etype (Obj_Id, Ptr_Typ);
8891 Set_Is_Known_Non_Null (Obj_Id);
8893 Obj_Decl :=
8894 Make_Object_Declaration (Loc,
8895 Defining_Identifier => Obj_Id,
8896 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8897 Expression => New_Expr);
8898 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8900 Rewrite (Assign, Make_Null_Statement (Loc));
8902 -- Retrieve the target of the assignment
8904 if Nkind (Lhs) = N_Selected_Component then
8905 Target := Selector_Name (Lhs);
8906 elsif Nkind (Lhs) = N_Type_Conversion then
8907 Target := Expression (Lhs);
8908 else
8909 Target := Lhs;
8910 end if;
8912 -- If we are assigning to a return object or this is an expression of
8913 -- an extension aggregate, the target should either be an identifier
8914 -- or a simple expression. All other cases imply a different scenario.
8916 if Nkind (Target) in N_Has_Entity then
8917 Target := Entity (Target);
8918 else
8919 return;
8920 end if;
8921 end Make_Build_In_Place_Call_In_Assignment;
8923 ----------------------------------------------------
8924 -- Make_Build_In_Place_Call_In_Object_Declaration --
8925 ----------------------------------------------------
8927 procedure Make_Build_In_Place_Call_In_Object_Declaration
8928 (Object_Decl : Node_Id;
8929 Function_Call : Node_Id)
8931 Loc : Source_Ptr;
8932 Obj_Def_Id : constant Entity_Id :=
8933 Defining_Identifier (Object_Decl);
8934 Enclosing_Func : constant Entity_Id :=
8935 Enclosing_Subprogram (Obj_Def_Id);
8936 Call_Deref : Node_Id;
8937 Caller_Object : Node_Id;
8938 Def_Id : Entity_Id;
8939 Fmaster_Actual : Node_Id := Empty;
8940 Func_Call : Node_Id := Function_Call;
8941 Function_Id : Entity_Id;
8942 Pool_Actual : Node_Id;
8943 Ptr_Typ : Entity_Id;
8944 Ptr_Typ_Decl : Node_Id;
8945 Pass_Caller_Acc : Boolean := False;
8946 Res_Decl : Node_Id;
8947 Result_Subt : Entity_Id;
8949 begin
8950 -- Step past qualification or unchecked conversion (the latter can occur
8951 -- in cases of calls to 'Input).
8953 if Nkind_In (Func_Call, N_Qualified_Expression,
8954 N_Unchecked_Type_Conversion)
8955 then
8956 Func_Call := Expression (Func_Call);
8957 end if;
8959 -- If the call has already been processed to add build-in-place actuals
8960 -- then return. This should not normally occur in an object declaration,
8961 -- but we add the protection as a defensive measure.
8963 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8964 return;
8965 end if;
8967 -- Mark the call as processed as a build-in-place call
8969 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8971 Loc := Sloc (Function_Call);
8973 if Is_Entity_Name (Name (Func_Call)) then
8974 Function_Id := Entity (Name (Func_Call));
8976 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8977 Function_Id := Etype (Name (Func_Call));
8979 else
8980 raise Program_Error;
8981 end if;
8983 Result_Subt := Etype (Function_Id);
8985 -- Create an access type designating the function's result subtype. We
8986 -- use the type of the original call because it may be a call to an
8987 -- inherited operation, which the expansion has replaced with the parent
8988 -- operation that yields the parent type. Note that this access type
8989 -- must be declared before we establish a transient scope, so that it
8990 -- receives the proper accessibility level.
8992 Ptr_Typ := Make_Temporary (Loc, 'A');
8993 Ptr_Typ_Decl :=
8994 Make_Full_Type_Declaration (Loc,
8995 Defining_Identifier => Ptr_Typ,
8996 Type_Definition =>
8997 Make_Access_To_Object_Definition (Loc,
8998 All_Present => True,
8999 Subtype_Indication =>
9000 New_Occurrence_Of (Etype (Function_Call), Loc)));
9002 -- The access type and its accompanying object must be inserted after
9003 -- the object declaration in the constrained case, so that the function
9004 -- call can be passed access to the object. In the unconstrained case,
9005 -- or if the object declaration is for a return object, the access type
9006 -- and object must be inserted before the object, since the object
9007 -- declaration is rewritten to be a renaming of a dereference of the
9008 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
9009 -- the result object is in a different (transient) scope, so won't
9010 -- cause freezing.
9012 if Is_Constrained (Underlying_Type (Result_Subt))
9013 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9014 then
9015 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
9016 else
9017 Insert_Action (Object_Decl, Ptr_Typ_Decl);
9018 end if;
9020 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
9021 -- elaborated in an inner (transient) scope and thus won't cause
9022 -- freezing by itself.
9024 declare
9025 Ptr_Typ_Freeze_Ref : constant Node_Id :=
9026 New_Occurrence_Of (Ptr_Typ, Loc);
9027 begin
9028 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
9029 Freeze_Expression (Ptr_Typ_Freeze_Ref);
9030 end;
9032 -- If the the object is a return object of an enclosing build-in-place
9033 -- function, then the implicit build-in-place parameters of the
9034 -- enclosing function are simply passed along to the called function.
9035 -- (Unfortunately, this won't cover the case of extension aggregates
9036 -- where the ancestor part is a build-in-place unconstrained function
9037 -- call that should be passed along the caller's parameters. Currently
9038 -- those get mishandled by reassigning the result of the call to the
9039 -- aggregate return object, when the call result should really be
9040 -- directly built in place in the aggregate and not in a temporary. ???)
9042 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
9043 Pass_Caller_Acc := True;
9045 -- When the enclosing function has a BIP_Alloc_Form formal then we
9046 -- pass it along to the callee (such as when the enclosing function
9047 -- has an unconstrained or tagged result type).
9049 if Needs_BIP_Alloc_Form (Enclosing_Func) then
9050 if VM_Target = No_VM and then
9051 RTE_Available (RE_Root_Storage_Pool_Ptr)
9052 then
9053 Pool_Actual :=
9054 New_Occurrence_Of (Build_In_Place_Formal
9055 (Enclosing_Func, BIP_Storage_Pool), Loc);
9057 -- The build-in-place pool formal is not built on .NET/JVM
9059 else
9060 Pool_Actual := Empty;
9061 end if;
9063 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9064 (Func_Call,
9065 Function_Id,
9066 Alloc_Form_Exp =>
9067 New_Occurrence_Of
9068 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
9069 Loc),
9070 Pool_Actual => Pool_Actual);
9072 -- Otherwise, if enclosing function has a constrained result subtype,
9073 -- then caller allocation will be used.
9075 else
9076 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9077 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9078 end if;
9080 if Needs_BIP_Finalization_Master (Enclosing_Func) then
9081 Fmaster_Actual :=
9082 New_Occurrence_Of
9083 (Build_In_Place_Formal
9084 (Enclosing_Func, BIP_Finalization_Master), Loc);
9085 end if;
9087 -- Retrieve the BIPacc formal from the enclosing function and convert
9088 -- it to the access type of the callee's BIP_Object_Access formal.
9090 Caller_Object :=
9091 Make_Unchecked_Type_Conversion (Loc,
9092 Subtype_Mark =>
9093 New_Occurrence_Of
9094 (Etype
9095 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
9096 Loc),
9097 Expression =>
9098 New_Occurrence_Of
9099 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
9100 Loc));
9102 -- In the constrained case, add an implicit actual to the function call
9103 -- that provides access to the declared object. An unchecked conversion
9104 -- to the (specific) result type of the function is inserted to handle
9105 -- the case where the object is declared with a class-wide type.
9107 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
9108 Caller_Object :=
9109 Make_Unchecked_Type_Conversion (Loc,
9110 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9111 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
9113 -- When the function has a controlling result, an allocation-form
9114 -- parameter must be passed indicating that the caller is allocating
9115 -- the result object. This is needed because such a function can be
9116 -- called as a dispatching operation and must be treated similarly
9117 -- to functions with unconstrained result subtypes.
9119 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9120 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
9122 -- In other unconstrained cases, pass an indication to do the allocation
9123 -- on the secondary stack and set Caller_Object to Empty so that a null
9124 -- value will be passed for the caller's object address. A transient
9125 -- scope is established to ensure eventual cleanup of the result.
9127 else
9128 Add_Unconstrained_Actuals_To_Build_In_Place_Call
9129 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
9130 Caller_Object := Empty;
9132 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
9133 end if;
9135 -- Pass along any finalization master actual, which is needed in the
9136 -- case where the called function initializes a return object of an
9137 -- enclosing build-in-place function.
9139 Add_Finalization_Master_Actual_To_Build_In_Place_Call
9140 (Func_Call => Func_Call,
9141 Func_Id => Function_Id,
9142 Master_Exp => Fmaster_Actual);
9144 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
9145 and then Has_Task (Result_Subt)
9146 then
9147 -- Here we're passing along the master that was passed in to this
9148 -- function.
9150 Add_Task_Actuals_To_Build_In_Place_Call
9151 (Func_Call, Function_Id,
9152 Master_Actual =>
9153 New_Occurrence_Of (Build_In_Place_Formal
9154 (Enclosing_Func, BIP_Task_Master), Loc));
9156 else
9157 Add_Task_Actuals_To_Build_In_Place_Call
9158 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9159 end if;
9161 Add_Access_Actual_To_Build_In_Place_Call
9162 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
9164 -- Finally, create an access object initialized to a reference to the
9165 -- function call. We know this access value cannot be null, so mark the
9166 -- entity accordingly to suppress the access check.
9168 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9169 Set_Etype (Def_Id, Ptr_Typ);
9170 Set_Is_Known_Non_Null (Def_Id);
9172 Res_Decl :=
9173 Make_Object_Declaration (Loc,
9174 Defining_Identifier => Def_Id,
9175 Constant_Present => True,
9176 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9177 Expression =>
9178 Make_Reference (Loc, Relocate_Node (Func_Call)));
9180 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9182 -- If the result subtype of the called function is constrained and
9183 -- is not itself the return expression of an enclosing BIP function,
9184 -- then mark the object as having no initialization.
9186 if Is_Constrained (Underlying_Type (Result_Subt))
9187 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9188 then
9189 -- The related object declaration is encased in a transient block
9190 -- because the build-in-place function call contains at least one
9191 -- nested function call that produces a controlled transient
9192 -- temporary:
9194 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9196 -- Since the build-in-place expansion decouples the call from the
9197 -- object declaration, the finalization machinery lacks the context
9198 -- which prompted the generation of the transient block. To resolve
9199 -- this scenario, store the build-in-place call.
9201 if Scope_Is_Transient
9202 and then Node_To_Be_Wrapped = Object_Decl
9203 then
9204 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9205 end if;
9207 Set_Expression (Object_Decl, Empty);
9208 Set_No_Initialization (Object_Decl);
9210 -- In case of an unconstrained result subtype, or if the call is the
9211 -- return expression of an enclosing BIP function, rewrite the object
9212 -- declaration as an object renaming where the renamed object is a
9213 -- dereference of <function_Call>'reference:
9215 -- Obj : Subt renames <function_call>'Ref.all;
9217 else
9218 Call_Deref :=
9219 Make_Explicit_Dereference (Loc,
9220 Prefix => New_Occurrence_Of (Def_Id, Loc));
9222 Loc := Sloc (Object_Decl);
9223 Rewrite (Object_Decl,
9224 Make_Object_Renaming_Declaration (Loc,
9225 Defining_Identifier => Make_Temporary (Loc, 'D'),
9226 Access_Definition => Empty,
9227 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9228 Name => Call_Deref));
9230 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
9232 Analyze (Object_Decl);
9234 -- Replace the internal identifier of the renaming declaration's
9235 -- entity with identifier of the original object entity. We also have
9236 -- to exchange the entities containing their defining identifiers to
9237 -- ensure the correct replacement of the object declaration by the
9238 -- object renaming declaration to avoid homograph conflicts (since
9239 -- the object declaration's defining identifier was already entered
9240 -- in current scope). The Next_Entity links of the two entities also
9241 -- have to be swapped since the entities are part of the return
9242 -- scope's entity list and the list structure would otherwise be
9243 -- corrupted. Finally, the homonym chain must be preserved as well.
9245 declare
9246 Renaming_Def_Id : constant Entity_Id :=
9247 Defining_Identifier (Object_Decl);
9248 Next_Entity_Temp : constant Entity_Id :=
9249 Next_Entity (Renaming_Def_Id);
9250 begin
9251 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
9253 -- Swap next entity links in preparation for exchanging entities
9255 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9256 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9257 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9259 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9261 -- Preserve source indication of original declaration, so that
9262 -- xref information is properly generated for the right entity.
9264 Preserve_Comes_From_Source
9265 (Object_Decl, Original_Node (Object_Decl));
9267 Preserve_Comes_From_Source
9268 (Obj_Def_Id, Original_Node (Object_Decl));
9270 Set_Comes_From_Source (Renaming_Def_Id, False);
9271 end;
9272 end if;
9274 -- If the object entity has a class-wide Etype, then we need to change
9275 -- it to the result subtype of the function call, because otherwise the
9276 -- object will be class-wide without an explicit initialization and
9277 -- won't be allocated properly by the back end. It seems unclean to make
9278 -- such a revision to the type at this point, and we should try to
9279 -- improve this treatment when build-in-place functions with class-wide
9280 -- results are implemented. ???
9282 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9283 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9284 end if;
9285 end Make_Build_In_Place_Call_In_Object_Declaration;
9287 --------------------------------------------
9288 -- Make_CPP_Constructor_Call_In_Allocator --
9289 --------------------------------------------
9291 procedure Make_CPP_Constructor_Call_In_Allocator
9292 (Allocator : Node_Id;
9293 Function_Call : Node_Id)
9295 Loc : constant Source_Ptr := Sloc (Function_Call);
9296 Acc_Type : constant Entity_Id := Etype (Allocator);
9297 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9298 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9300 New_Allocator : Node_Id;
9301 Return_Obj_Access : Entity_Id;
9302 Tmp_Obj : Node_Id;
9304 begin
9305 pragma Assert (Nkind (Allocator) = N_Allocator
9306 and then Nkind (Function_Call) = N_Function_Call);
9307 pragma Assert (Convention (Function_Id) = Convention_CPP
9308 and then Is_Constructor (Function_Id));
9309 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9311 -- Replace the initialized allocator of form "new T'(Func (...))" with
9312 -- an uninitialized allocator of form "new T", where T is the result
9313 -- subtype of the called function. The call to the function is handled
9314 -- separately further below.
9316 New_Allocator :=
9317 Make_Allocator (Loc,
9318 Expression => New_Occurrence_Of (Result_Subt, Loc));
9319 Set_No_Initialization (New_Allocator);
9321 -- Copy attributes to new allocator. Note that the new allocator
9322 -- logically comes from source if the original one did, so copy the
9323 -- relevant flag. This ensures proper treatment of the restriction
9324 -- No_Implicit_Heap_Allocations in this case.
9326 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9327 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9328 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9330 Rewrite (Allocator, New_Allocator);
9332 -- Create a new access object and initialize it to the result of the
9333 -- new uninitialized allocator. Note: we do not use Allocator as the
9334 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9335 -- as this would create a sort of infinite "recursion".
9337 Return_Obj_Access := Make_Temporary (Loc, 'R');
9338 Set_Etype (Return_Obj_Access, Acc_Type);
9340 -- Generate:
9341 -- Rnnn : constant ptr_T := new (T);
9342 -- Init (Rnn.all,...);
9344 Tmp_Obj :=
9345 Make_Object_Declaration (Loc,
9346 Defining_Identifier => Return_Obj_Access,
9347 Constant_Present => True,
9348 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9349 Expression => Relocate_Node (Allocator));
9350 Insert_Action (Allocator, Tmp_Obj);
9352 Insert_List_After_And_Analyze (Tmp_Obj,
9353 Build_Initialization_Call (Loc,
9354 Id_Ref =>
9355 Make_Explicit_Dereference (Loc,
9356 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9357 Typ => Etype (Function_Id),
9358 Constructor_Ref => Function_Call));
9360 -- Finally, replace the allocator node with a reference to the result of
9361 -- the function call itself (which will effectively be an access to the
9362 -- object created by the allocator).
9364 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9366 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9367 -- generate an implicit conversion to force displacement of the "this"
9368 -- pointer.
9370 if Is_Interface (Designated_Type (Acc_Type)) then
9371 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9372 end if;
9374 Analyze_And_Resolve (Allocator, Acc_Type);
9375 end Make_CPP_Constructor_Call_In_Allocator;
9377 -----------------------------------
9378 -- Needs_BIP_Finalization_Master --
9379 -----------------------------------
9381 function Needs_BIP_Finalization_Master
9382 (Func_Id : Entity_Id) return Boolean
9384 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9385 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9386 begin
9387 return
9388 not Restriction_Active (No_Finalization)
9389 and then Needs_Finalization (Func_Typ);
9390 end Needs_BIP_Finalization_Master;
9392 --------------------------
9393 -- Needs_BIP_Alloc_Form --
9394 --------------------------
9396 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9397 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9398 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9399 begin
9400 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9401 end Needs_BIP_Alloc_Form;
9403 --------------------------------------
9404 -- Needs_Result_Accessibility_Level --
9405 --------------------------------------
9407 function Needs_Result_Accessibility_Level
9408 (Func_Id : Entity_Id) return Boolean
9410 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9412 function Has_Unconstrained_Access_Discriminant_Component
9413 (Comp_Typ : Entity_Id) return Boolean;
9414 -- Returns True if any component of the type has an unconstrained access
9415 -- discriminant.
9417 -----------------------------------------------------
9418 -- Has_Unconstrained_Access_Discriminant_Component --
9419 -----------------------------------------------------
9421 function Has_Unconstrained_Access_Discriminant_Component
9422 (Comp_Typ : Entity_Id) return Boolean
9424 begin
9425 if not Is_Limited_Type (Comp_Typ) then
9426 return False;
9428 -- Only limited types can have access discriminants with
9429 -- defaults.
9431 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9432 return True;
9434 elsif Is_Array_Type (Comp_Typ) then
9435 return Has_Unconstrained_Access_Discriminant_Component
9436 (Underlying_Type (Component_Type (Comp_Typ)));
9438 elsif Is_Record_Type (Comp_Typ) then
9439 declare
9440 Comp : Entity_Id;
9442 begin
9443 Comp := First_Component (Comp_Typ);
9444 while Present (Comp) loop
9445 if Has_Unconstrained_Access_Discriminant_Component
9446 (Underlying_Type (Etype (Comp)))
9447 then
9448 return True;
9449 end if;
9451 Next_Component (Comp);
9452 end loop;
9453 end;
9454 end if;
9456 return False;
9457 end Has_Unconstrained_Access_Discriminant_Component;
9459 Feature_Disabled : constant Boolean := True;
9460 -- Temporary
9462 -- Start of processing for Needs_Result_Accessibility_Level
9464 begin
9465 -- False if completion unavailable (how does this happen???)
9467 if not Present (Func_Typ) then
9468 return False;
9470 elsif Feature_Disabled then
9471 return False;
9473 -- False if not a function, also handle enum-lit renames case
9475 elsif Func_Typ = Standard_Void_Type
9476 or else Is_Scalar_Type (Func_Typ)
9477 then
9478 return False;
9480 -- Handle a corner case, a cross-dialect subp renaming. For example,
9481 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9482 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9484 elsif Present (Alias (Func_Id)) then
9486 -- Unimplemented: a cross-dialect subp renaming which does not set
9487 -- the Alias attribute (e.g., a rename of a dereference of an access
9488 -- to subprogram value). ???
9490 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9492 -- Remaining cases require Ada 2012 mode
9494 elsif Ada_Version < Ada_2012 then
9495 return False;
9497 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9498 or else Is_Tagged_Type (Func_Typ)
9499 then
9500 -- In the case of, say, a null tagged record result type, the need
9501 -- for this extra parameter might not be obvious. This function
9502 -- returns True for all tagged types for compatibility reasons.
9503 -- A function with, say, a tagged null controlling result type might
9504 -- be overridden by a primitive of an extension having an access
9505 -- discriminant and the overrider and overridden must have compatible
9506 -- calling conventions (including implicitly declared parameters).
9507 -- Similarly, values of one access-to-subprogram type might designate
9508 -- both a primitive subprogram of a given type and a function
9509 -- which is, for example, not a primitive subprogram of any type.
9510 -- Again, this requires calling convention compatibility.
9511 -- It might be possible to solve these issues by introducing
9512 -- wrappers, but that is not the approach that was chosen.
9514 return True;
9516 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9517 return True;
9519 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9520 return True;
9522 -- False for all other cases
9524 else
9525 return False;
9526 end if;
9527 end Needs_Result_Accessibility_Level;
9529 ------------------------
9530 -- Unnest_Subprograms --
9531 ------------------------
9533 procedure Unnest_Subprograms is
9534 begin
9535 for J in Unest_Bodies.First .. Unest_Bodies.Last loop
9536 declare
9537 UBJ : Unest_Entry renames Unest_Bodies.Table (J);
9538 begin
9539 Unnest_Subprogram (UBJ.Ent, UBJ.Bod);
9540 end;
9541 end loop;
9542 end Unnest_Subprograms;
9544 end Exp_Ch6;