2015-03-02 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob1311615c8a7fa1a8f9ed5b86ace26c7e9e57eb10
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_Util; use Exp_Util;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Mech; use Sem_Mech;
66 with Sem_Res; use Sem_Res;
67 with Sem_SCIL; use Sem_SCIL;
68 with Sem_Util; use Sem_Util;
69 with Sinfo; use Sinfo;
70 with Snames; use Snames;
71 with Stand; use Stand;
72 with Stringt; use Stringt;
73 with Targparm; use Targparm;
74 with Tbuild; use Tbuild;
75 with Uintp; use Uintp;
76 with Validsw; use Validsw;
78 package body Exp_Ch6 is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Add_Access_Actual_To_Build_In_Place_Call
85 (Function_Call : Node_Id;
86 Function_Id : Entity_Id;
87 Return_Object : Node_Id;
88 Is_Access : Boolean := False);
89 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
90 -- object name given by Return_Object and add the attribute to the end of
91 -- the actual parameter list associated with the build-in-place function
92 -- call denoted by Function_Call. However, if Is_Access is True, then
93 -- Return_Object is already an access expression, in which case it's passed
94 -- along directly to the build-in-place function. Finally, if Return_Object
95 -- is empty, then pass a null literal as the actual.
97 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
98 (Function_Call : Node_Id;
99 Function_Id : Entity_Id;
100 Alloc_Form : BIP_Allocation_Form := Unspecified;
101 Alloc_Form_Exp : Node_Id := Empty;
102 Pool_Actual : Node_Id := Make_Null (No_Location));
103 -- Ada 2005 (AI-318-02): Add the actuals needed for a build-in-place
104 -- function call that returns a caller-unknown-size result (BIP_Alloc_Form
105 -- and BIP_Storage_Pool). If Alloc_Form_Exp is present, then use it,
106 -- otherwise pass a literal corresponding to the Alloc_Form parameter
107 -- (which must not be Unspecified in that case). Pool_Actual is the
108 -- parameter to pass to BIP_Storage_Pool.
110 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
111 (Func_Call : Node_Id;
112 Func_Id : Entity_Id;
113 Ptr_Typ : Entity_Id := Empty;
114 Master_Exp : Node_Id := Empty);
115 -- Ada 2005 (AI-318-02): If the result type of a build-in-place call needs
116 -- finalization actions, add an actual parameter which is a pointer to the
117 -- finalization master of the caller. If Master_Exp is not Empty, then that
118 -- will be passed as the actual. Otherwise, if Ptr_Typ is left Empty, this
119 -- will result in an automatic "null" value for the actual.
121 procedure Add_Task_Actuals_To_Build_In_Place_Call
122 (Function_Call : Node_Id;
123 Function_Id : Entity_Id;
124 Master_Actual : Node_Id;
125 Chain : Node_Id := Empty);
126 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
127 -- contains tasks, add two actual parameters: the master, and a pointer to
128 -- the caller's activation chain. Master_Actual is the actual parameter
129 -- expression to pass for the master. In most cases, this is the current
130 -- master (_master). The two exceptions are: If the function call is the
131 -- initialization expression for an allocator, we pass the master of the
132 -- access type. If the function call is the initialization expression for a
133 -- return object, we pass along the master passed in by the caller. In most
134 -- contexts, the activation chain to pass is the local one, which is
135 -- indicated by No (Chain). However, in an allocator, the caller passes in
136 -- the activation Chain. Note: Master_Actual can be Empty, but only if
137 -- there are no tasks.
139 procedure Check_Overriding_Operation (Subp : Entity_Id);
140 -- Subp is a dispatching operation. Check whether it may override an
141 -- inherited private operation, in which case its DT entry is that of
142 -- the hidden operation, not the one it may have received earlier.
143 -- This must be done before emitting the code to set the corresponding
144 -- DT to the address of the subprogram. The actual placement of Subp in
145 -- the proper place in the list of primitive operations is done in
146 -- Declare_Inherited_Private_Subprograms, which also has to deal with
147 -- implicit operations. This duplication is unavoidable for now???
149 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
150 -- This procedure is called only if the subprogram body N, whose spec
151 -- has the given entity Spec, contains a parameterless recursive call.
152 -- It attempts to generate runtime code to detect if this a case of
153 -- infinite recursion.
155 -- The body is scanned to determine dependencies. If the only external
156 -- dependencies are on a small set of scalar variables, then the values
157 -- of these variables are captured on entry to the subprogram, and if
158 -- the values are not changed for the call, we know immediately that
159 -- we have an infinite recursion.
161 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id);
162 -- For each actual of an in-out or out parameter which is a numeric
163 -- (view) conversion of the form T (A), where A denotes a variable,
164 -- we insert the declaration:
166 -- Temp : T[ := T (A)];
168 -- prior to the call. Then we replace the actual with a reference to Temp,
169 -- and append the assignment:
171 -- A := TypeA (Temp);
173 -- after the call. Here TypeA is the actual type of variable A. For out
174 -- parameters, the initial declaration has no expression. If A is not an
175 -- entity name, we generate instead:
177 -- Var : TypeA renames A;
178 -- Temp : T := Var; -- omitting expression for out parameter.
179 -- ...
180 -- Var := TypeA (Temp);
182 -- For other in-out parameters, we emit the required constraint checks
183 -- before and/or after the call.
185 -- For all parameter modes, actuals that denote components and slices of
186 -- packed arrays are expanded into suitable temporaries.
188 -- For non-scalar objects that are possibly unaligned, add call by copy
189 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
191 -- For OUT and IN OUT parameters, add predicate checks after the call
192 -- based on the predicates of the actual type.
194 -- The parameter N is IN OUT because in some cases, the expansion code
195 -- rewrites the call as an expression actions with the call inside. In
196 -- this case N is reset to point to the inside call so that the caller
197 -- can continue processing of this call.
199 procedure Expand_Ctrl_Function_Call (N : Node_Id);
200 -- N is a function call which returns a controlled object. Transform the
201 -- call into a temporary which retrieves the returned object from the
202 -- secondary stack using 'reference.
204 procedure Expand_Non_Function_Return (N : Node_Id);
205 -- Expand a simple return statement found in a procedure body, entry body,
206 -- accept statement, or an extended return statement. Note that all non-
207 -- function returns are simple return statements.
209 function Expand_Protected_Object_Reference
210 (N : Node_Id;
211 Scop : Entity_Id) return Node_Id;
213 procedure Expand_Protected_Subprogram_Call
214 (N : Node_Id;
215 Subp : Entity_Id;
216 Scop : Entity_Id);
217 -- A call to a protected subprogram within the protected object may appear
218 -- as a regular call. The list of actuals must be expanded to contain a
219 -- reference to the object itself, and the call becomes a call to the
220 -- corresponding protected subprogram.
222 function Has_Unconstrained_Access_Discriminants
223 (Subtyp : Entity_Id) return Boolean;
224 -- Returns True if the given subtype is unconstrained and has one
225 -- or more access discriminants.
227 procedure Expand_Simple_Function_Return (N : Node_Id);
228 -- Expand simple return from function. In the case where we are returning
229 -- from a function body this is called by Expand_N_Simple_Return_Statement.
231 ----------------------------------------------
232 -- Add_Access_Actual_To_Build_In_Place_Call --
233 ----------------------------------------------
235 procedure Add_Access_Actual_To_Build_In_Place_Call
236 (Function_Call : Node_Id;
237 Function_Id : Entity_Id;
238 Return_Object : Node_Id;
239 Is_Access : Boolean := False)
241 Loc : constant Source_Ptr := Sloc (Function_Call);
242 Obj_Address : Node_Id;
243 Obj_Acc_Formal : Entity_Id;
245 begin
246 -- Locate the implicit access parameter in the called function
248 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
250 -- If no return object is provided, then pass null
252 if not Present (Return_Object) then
253 Obj_Address := Make_Null (Loc);
254 Set_Parent (Obj_Address, Function_Call);
256 -- If Return_Object is already an expression of an access type, then use
257 -- it directly, since it must be an access value denoting the return
258 -- object, and couldn't possibly be the return object itself.
260 elsif Is_Access then
261 Obj_Address := Return_Object;
262 Set_Parent (Obj_Address, Function_Call);
264 -- Apply Unrestricted_Access to caller's return object
266 else
267 Obj_Address :=
268 Make_Attribute_Reference (Loc,
269 Prefix => Return_Object,
270 Attribute_Name => Name_Unrestricted_Access);
272 Set_Parent (Return_Object, Obj_Address);
273 Set_Parent (Obj_Address, Function_Call);
274 end if;
276 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
278 -- Build the parameter association for the new actual and add it to the
279 -- end of the function's actuals.
281 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
282 end Add_Access_Actual_To_Build_In_Place_Call;
284 ------------------------------------------------------
285 -- Add_Unconstrained_Actuals_To_Build_In_Place_Call --
286 ------------------------------------------------------
288 procedure Add_Unconstrained_Actuals_To_Build_In_Place_Call
289 (Function_Call : Node_Id;
290 Function_Id : Entity_Id;
291 Alloc_Form : BIP_Allocation_Form := Unspecified;
292 Alloc_Form_Exp : Node_Id := Empty;
293 Pool_Actual : Node_Id := Make_Null (No_Location))
295 Loc : constant Source_Ptr := Sloc (Function_Call);
296 Alloc_Form_Actual : Node_Id;
297 Alloc_Form_Formal : Node_Id;
298 Pool_Formal : Node_Id;
300 begin
301 -- The allocation form generally doesn't need to be passed in the case
302 -- of a constrained result subtype, since normally the caller performs
303 -- the allocation in that case. However this formal is still needed in
304 -- the case where the function has a tagged result, because generally
305 -- such functions can be called in a dispatching context and such calls
306 -- must be handled like calls to class-wide functions.
308 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
309 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
310 then
311 return;
312 end if;
314 -- Locate the implicit allocation form parameter in the called function.
315 -- Maybe it would be better for each implicit formal of a build-in-place
316 -- function to have a flag or a Uint attribute to identify it. ???
318 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
320 if Present (Alloc_Form_Exp) then
321 pragma Assert (Alloc_Form = Unspecified);
323 Alloc_Form_Actual := Alloc_Form_Exp;
325 else
326 pragma Assert (Alloc_Form /= Unspecified);
328 Alloc_Form_Actual :=
329 Make_Integer_Literal (Loc,
330 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
331 end if;
333 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
335 -- Build the parameter association for the new actual and add it to the
336 -- end of the function's actuals.
338 Add_Extra_Actual_To_Call
339 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
341 -- Pass the Storage_Pool parameter. This parameter is omitted on
342 -- .NET/JVM/ZFP as those targets do not support pools.
344 if VM_Target = No_VM
345 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
346 then
347 Pool_Formal := Build_In_Place_Formal (Function_Id, BIP_Storage_Pool);
348 Analyze_And_Resolve (Pool_Actual, Etype (Pool_Formal));
349 Add_Extra_Actual_To_Call
350 (Function_Call, Pool_Formal, Pool_Actual);
351 end if;
352 end Add_Unconstrained_Actuals_To_Build_In_Place_Call;
354 -----------------------------------------------------------
355 -- Add_Finalization_Master_Actual_To_Build_In_Place_Call --
356 -----------------------------------------------------------
358 procedure Add_Finalization_Master_Actual_To_Build_In_Place_Call
359 (Func_Call : Node_Id;
360 Func_Id : Entity_Id;
361 Ptr_Typ : Entity_Id := Empty;
362 Master_Exp : Node_Id := Empty)
364 begin
365 if not Needs_BIP_Finalization_Master (Func_Id) then
366 return;
367 end if;
369 declare
370 Formal : constant Entity_Id :=
371 Build_In_Place_Formal (Func_Id, BIP_Finalization_Master);
372 Loc : constant Source_Ptr := Sloc (Func_Call);
374 Actual : Node_Id;
375 Desig_Typ : Entity_Id;
377 begin
378 -- If there is a finalization master actual, such as the implicit
379 -- finalization master of an enclosing build-in-place function,
380 -- then this must be added as an extra actual of the call.
382 if Present (Master_Exp) then
383 Actual := Master_Exp;
385 -- Case where the context does not require an actual master
387 elsif No (Ptr_Typ) then
388 Actual := Make_Null (Loc);
390 else
391 Desig_Typ := Directly_Designated_Type (Ptr_Typ);
393 -- Check for a library-level access type whose designated type has
394 -- supressed finalization. Such an access types lack a master.
395 -- Pass a null actual to the callee in order to signal a missing
396 -- master.
398 if Is_Library_Level_Entity (Ptr_Typ)
399 and then Finalize_Storage_Only (Desig_Typ)
400 then
401 Actual := Make_Null (Loc);
403 -- Types in need of finalization actions
405 elsif Needs_Finalization (Desig_Typ) then
407 -- The general mechanism of creating finalization masters for
408 -- anonymous access types is disabled by default, otherwise
409 -- finalization masters will pop all over the place. Such types
410 -- use context-specific masters.
412 if Ekind (Ptr_Typ) = E_Anonymous_Access_Type
413 and then No (Finalization_Master (Ptr_Typ))
414 then
415 Build_Finalization_Master
416 (Typ => Ptr_Typ,
417 For_Anonymous => True,
418 Context_Scope => Scope (Ptr_Typ),
419 Insertion_Node => Associated_Node_For_Itype (Ptr_Typ));
420 end if;
422 -- Access-to-controlled types should always have a master
424 pragma Assert (Present (Finalization_Master (Ptr_Typ)));
426 Actual :=
427 Make_Attribute_Reference (Loc,
428 Prefix =>
429 New_Occurrence_Of (Finalization_Master (Ptr_Typ), Loc),
430 Attribute_Name => Name_Unrestricted_Access);
432 -- Tagged types
434 else
435 Actual := Make_Null (Loc);
436 end if;
437 end if;
439 Analyze_And_Resolve (Actual, Etype (Formal));
441 -- Build the parameter association for the new actual and add it to
442 -- the end of the function's actuals.
444 Add_Extra_Actual_To_Call (Func_Call, Formal, Actual);
445 end;
446 end Add_Finalization_Master_Actual_To_Build_In_Place_Call;
448 ------------------------------
449 -- Add_Extra_Actual_To_Call --
450 ------------------------------
452 procedure Add_Extra_Actual_To_Call
453 (Subprogram_Call : Node_Id;
454 Extra_Formal : Entity_Id;
455 Extra_Actual : Node_Id)
457 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
458 Param_Assoc : Node_Id;
460 begin
461 Param_Assoc :=
462 Make_Parameter_Association (Loc,
463 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
464 Explicit_Actual_Parameter => Extra_Actual);
466 Set_Parent (Param_Assoc, Subprogram_Call);
467 Set_Parent (Extra_Actual, Param_Assoc);
469 if Present (Parameter_Associations (Subprogram_Call)) then
470 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
471 N_Parameter_Association
472 then
474 -- Find last named actual, and append
476 declare
477 L : Node_Id;
478 begin
479 L := First_Actual (Subprogram_Call);
480 while Present (L) loop
481 if No (Next_Actual (L)) then
482 Set_Next_Named_Actual (Parent (L), Extra_Actual);
483 exit;
484 end if;
485 Next_Actual (L);
486 end loop;
487 end;
489 else
490 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
491 end if;
493 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
495 else
496 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
497 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
498 end if;
499 end Add_Extra_Actual_To_Call;
501 ---------------------------------------------
502 -- Add_Task_Actuals_To_Build_In_Place_Call --
503 ---------------------------------------------
505 procedure Add_Task_Actuals_To_Build_In_Place_Call
506 (Function_Call : Node_Id;
507 Function_Id : Entity_Id;
508 Master_Actual : Node_Id;
509 Chain : Node_Id := Empty)
511 Loc : constant Source_Ptr := Sloc (Function_Call);
512 Result_Subt : constant Entity_Id :=
513 Available_View (Etype (Function_Id));
514 Actual : Node_Id;
515 Chain_Actual : Node_Id;
516 Chain_Formal : Node_Id;
517 Master_Formal : Node_Id;
519 begin
520 -- No such extra parameters are needed if there are no tasks
522 if not Has_Task (Result_Subt) then
523 return;
524 end if;
526 Actual := Master_Actual;
528 -- Use a dummy _master actual in case of No_Task_Hierarchy
530 if Restriction_Active (No_Task_Hierarchy) then
531 Actual := New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc);
533 -- In the case where we use the master associated with an access type,
534 -- the actual is an entity and requires an explicit reference.
536 elsif Nkind (Actual) = N_Defining_Identifier then
537 Actual := New_Occurrence_Of (Actual, Loc);
538 end if;
540 -- Locate the implicit master parameter in the called function
542 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Task_Master);
543 Analyze_And_Resolve (Actual, Etype (Master_Formal));
545 -- Build the parameter association for the new actual and add it to the
546 -- end of the function's actuals.
548 Add_Extra_Actual_To_Call (Function_Call, Master_Formal, Actual);
550 -- Locate the implicit activation chain parameter in the called function
552 Chain_Formal :=
553 Build_In_Place_Formal (Function_Id, BIP_Activation_Chain);
555 -- Create the actual which is a pointer to the current activation chain
557 if No (Chain) then
558 Chain_Actual :=
559 Make_Attribute_Reference (Loc,
560 Prefix => Make_Identifier (Loc, Name_uChain),
561 Attribute_Name => Name_Unrestricted_Access);
563 -- Allocator case; make a reference to the Chain passed in by the caller
565 else
566 Chain_Actual :=
567 Make_Attribute_Reference (Loc,
568 Prefix => New_Occurrence_Of (Chain, Loc),
569 Attribute_Name => Name_Unrestricted_Access);
570 end if;
572 Analyze_And_Resolve (Chain_Actual, Etype (Chain_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, Chain_Formal, Chain_Actual);
578 end Add_Task_Actuals_To_Build_In_Place_Call;
580 -----------------------
581 -- BIP_Formal_Suffix --
582 -----------------------
584 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
585 begin
586 case Kind is
587 when BIP_Alloc_Form =>
588 return "BIPalloc";
589 when BIP_Storage_Pool =>
590 return "BIPstoragepool";
591 when BIP_Finalization_Master =>
592 return "BIPfinalizationmaster";
593 when BIP_Task_Master =>
594 return "BIPtaskmaster";
595 when BIP_Activation_Chain =>
596 return "BIPactivationchain";
597 when BIP_Object_Access =>
598 return "BIPaccess";
599 end case;
600 end BIP_Formal_Suffix;
602 ---------------------------
603 -- Build_In_Place_Formal --
604 ---------------------------
606 function Build_In_Place_Formal
607 (Func : Entity_Id;
608 Kind : BIP_Formal_Kind) return Entity_Id
610 Formal_Name : constant Name_Id :=
611 New_External_Name
612 (Chars (Func), BIP_Formal_Suffix (Kind));
613 Extra_Formal : Entity_Id := Extra_Formals (Func);
615 begin
616 -- Maybe it would be better for each implicit formal of a build-in-place
617 -- function to have a flag or a Uint attribute to identify it. ???
619 -- The return type in the function declaration may have been a limited
620 -- view, and the extra formals for the function were not generated at
621 -- that point. At the point of call the full view must be available and
622 -- the extra formals can be created.
624 if No (Extra_Formal) then
625 Create_Extra_Formals (Func);
626 Extra_Formal := Extra_Formals (Func);
627 end if;
629 loop
630 pragma Assert (Present (Extra_Formal));
631 exit when Chars (Extra_Formal) = Formal_Name;
633 Next_Formal_With_Extras (Extra_Formal);
634 end loop;
636 return Extra_Formal;
637 end Build_In_Place_Formal;
639 --------------------------------
640 -- Check_Overriding_Operation --
641 --------------------------------
643 procedure Check_Overriding_Operation (Subp : Entity_Id) is
644 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
645 Op_List : constant Elist_Id := Primitive_Operations (Typ);
646 Op_Elmt : Elmt_Id;
647 Prim_Op : Entity_Id;
648 Par_Op : Entity_Id;
650 begin
651 if Is_Derived_Type (Typ)
652 and then not Is_Private_Type (Typ)
653 and then In_Open_Scopes (Scope (Etype (Typ)))
654 and then Is_Base_Type (Typ)
655 then
656 -- Subp overrides an inherited private operation if there is an
657 -- inherited operation with a different name than Subp (see
658 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
659 -- same name as Subp.
661 Op_Elmt := First_Elmt (Op_List);
662 while Present (Op_Elmt) loop
663 Prim_Op := Node (Op_Elmt);
664 Par_Op := Alias (Prim_Op);
666 if Present (Par_Op)
667 and then not Comes_From_Source (Prim_Op)
668 and then Chars (Prim_Op) /= Chars (Par_Op)
669 and then Chars (Par_Op) = Chars (Subp)
670 and then Is_Hidden (Par_Op)
671 and then Type_Conformant (Prim_Op, Subp)
672 then
673 Set_DT_Position (Subp, DT_Position (Prim_Op));
674 end if;
676 Next_Elmt (Op_Elmt);
677 end loop;
678 end if;
679 end Check_Overriding_Operation;
681 -------------------------------
682 -- Detect_Infinite_Recursion --
683 -------------------------------
685 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
686 Loc : constant Source_Ptr := Sloc (N);
688 Var_List : constant Elist_Id := New_Elmt_List;
689 -- List of globals referenced by body of procedure
691 Call_List : constant Elist_Id := New_Elmt_List;
692 -- List of recursive calls in body of procedure
694 Shad_List : constant Elist_Id := New_Elmt_List;
695 -- List of entity id's for entities created to capture the value of
696 -- referenced globals on entry to the procedure.
698 Scop : constant Uint := Scope_Depth (Spec);
699 -- This is used to record the scope depth of the current procedure, so
700 -- that we can identify global references.
702 Max_Vars : constant := 4;
703 -- Do not test more than four global variables
705 Count_Vars : Natural := 0;
706 -- Count variables found so far
708 Var : Entity_Id;
709 Elm : Elmt_Id;
710 Ent : Entity_Id;
711 Call : Elmt_Id;
712 Decl : Node_Id;
713 Test : Node_Id;
714 Elm1 : Elmt_Id;
715 Elm2 : Elmt_Id;
716 Last : Node_Id;
718 function Process (Nod : Node_Id) return Traverse_Result;
719 -- Function to traverse the subprogram body (using Traverse_Func)
721 -------------
722 -- Process --
723 -------------
725 function Process (Nod : Node_Id) return Traverse_Result is
726 begin
727 -- Procedure call
729 if Nkind (Nod) = N_Procedure_Call_Statement then
731 -- Case of one of the detected recursive calls
733 if Is_Entity_Name (Name (Nod))
734 and then Has_Recursive_Call (Entity (Name (Nod)))
735 and then Entity (Name (Nod)) = Spec
736 then
737 Append_Elmt (Nod, Call_List);
738 return Skip;
740 -- Any other procedure call may have side effects
742 else
743 return Abandon;
744 end if;
746 -- A call to a pure function can always be ignored
748 elsif Nkind (Nod) = N_Function_Call
749 and then Is_Entity_Name (Name (Nod))
750 and then Is_Pure (Entity (Name (Nod)))
751 then
752 return Skip;
754 -- Case of an identifier reference
756 elsif Nkind (Nod) = N_Identifier then
757 Ent := Entity (Nod);
759 -- If no entity, then ignore the reference
761 -- Not clear why this can happen. To investigate, remove this
762 -- test and look at the crash that occurs here in 3401-004 ???
764 if No (Ent) then
765 return Skip;
767 -- Ignore entities with no Scope, again not clear how this
768 -- can happen, to investigate, look at 4108-008 ???
770 elsif No (Scope (Ent)) then
771 return Skip;
773 -- Ignore the reference if not to a more global object
775 elsif Scope_Depth (Scope (Ent)) >= Scop then
776 return Skip;
778 -- References to types, exceptions and constants are always OK
780 elsif Is_Type (Ent)
781 or else Ekind (Ent) = E_Exception
782 or else Ekind (Ent) = E_Constant
783 then
784 return Skip;
786 -- If other than a non-volatile scalar variable, we have some
787 -- kind of global reference (e.g. to a function) that we cannot
788 -- deal with so we forget the attempt.
790 elsif Ekind (Ent) /= E_Variable
791 or else not Is_Scalar_Type (Etype (Ent))
792 or else Treat_As_Volatile (Ent)
793 then
794 return Abandon;
796 -- Otherwise we have a reference to a global scalar
798 else
799 -- Loop through global entities already detected
801 Elm := First_Elmt (Var_List);
802 loop
803 -- If not detected before, record this new global reference
805 if No (Elm) then
806 Count_Vars := Count_Vars + 1;
808 if Count_Vars <= Max_Vars then
809 Append_Elmt (Entity (Nod), Var_List);
810 else
811 return Abandon;
812 end if;
814 exit;
816 -- If recorded before, ignore
818 elsif Node (Elm) = Entity (Nod) then
819 return Skip;
821 -- Otherwise keep looking
823 else
824 Next_Elmt (Elm);
825 end if;
826 end loop;
828 return Skip;
829 end if;
831 -- For all other node kinds, recursively visit syntactic children
833 else
834 return OK;
835 end if;
836 end Process;
838 function Traverse_Body is new Traverse_Func (Process);
840 -- Start of processing for Detect_Infinite_Recursion
842 begin
843 -- Do not attempt detection in No_Implicit_Conditional mode, since we
844 -- won't be able to generate the code to handle the recursion in any
845 -- case.
847 if Restriction_Active (No_Implicit_Conditionals) then
848 return;
849 end if;
851 -- Otherwise do traversal and quit if we get abandon signal
853 if Traverse_Body (N) = Abandon then
854 return;
856 -- We must have a call, since Has_Recursive_Call was set. If not just
857 -- ignore (this is only an error check, so if we have a funny situation,
858 -- due to bugs or errors, we do not want to bomb).
860 elsif Is_Empty_Elmt_List (Call_List) then
861 return;
862 end if;
864 -- Here is the case where we detect recursion at compile time
866 -- Push our current scope for analyzing the declarations and code that
867 -- we will insert for the checking.
869 Push_Scope (Spec);
871 -- This loop builds temporary variables for each of the referenced
872 -- globals, so that at the end of the loop the list Shad_List contains
873 -- these temporaries in one-to-one correspondence with the elements in
874 -- Var_List.
876 Last := Empty;
877 Elm := First_Elmt (Var_List);
878 while Present (Elm) loop
879 Var := Node (Elm);
880 Ent := Make_Temporary (Loc, 'S');
881 Append_Elmt (Ent, Shad_List);
883 -- Insert a declaration for this temporary at the start of the
884 -- declarations for the procedure. The temporaries are declared as
885 -- constant objects initialized to the current values of the
886 -- corresponding temporaries.
888 Decl :=
889 Make_Object_Declaration (Loc,
890 Defining_Identifier => Ent,
891 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
892 Constant_Present => True,
893 Expression => New_Occurrence_Of (Var, Loc));
895 if No (Last) then
896 Prepend (Decl, Declarations (N));
897 else
898 Insert_After (Last, Decl);
899 end if;
901 Last := Decl;
902 Analyze (Decl);
903 Next_Elmt (Elm);
904 end loop;
906 -- Loop through calls
908 Call := First_Elmt (Call_List);
909 while Present (Call) loop
911 -- Build a predicate expression of the form
913 -- True
914 -- and then global1 = temp1
915 -- and then global2 = temp2
916 -- ...
918 -- This predicate determines if any of the global values
919 -- referenced by the procedure have changed since the
920 -- current call, if not an infinite recursion is assured.
922 Test := New_Occurrence_Of (Standard_True, Loc);
924 Elm1 := First_Elmt (Var_List);
925 Elm2 := First_Elmt (Shad_List);
926 while Present (Elm1) loop
927 Test :=
928 Make_And_Then (Loc,
929 Left_Opnd => Test,
930 Right_Opnd =>
931 Make_Op_Eq (Loc,
932 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
933 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
935 Next_Elmt (Elm1);
936 Next_Elmt (Elm2);
937 end loop;
939 -- Now we replace the call with the sequence
941 -- if no-changes (see above) then
942 -- raise Storage_Error;
943 -- else
944 -- original-call
945 -- end if;
947 Rewrite (Node (Call),
948 Make_If_Statement (Loc,
949 Condition => Test,
950 Then_Statements => New_List (
951 Make_Raise_Storage_Error (Loc,
952 Reason => SE_Infinite_Recursion)),
954 Else_Statements => New_List (
955 Relocate_Node (Node (Call)))));
957 Analyze (Node (Call));
959 Next_Elmt (Call);
960 end loop;
962 -- Remove temporary scope stack entry used for analysis
964 Pop_Scope;
965 end Detect_Infinite_Recursion;
967 --------------------
968 -- Expand_Actuals --
969 --------------------
971 --------------------
972 -- Expand_Actuals --
973 --------------------
975 procedure Expand_Actuals (N : in out Node_Id; Subp : Entity_Id) is
976 Loc : constant Source_Ptr := Sloc (N);
977 Actual : Node_Id;
978 Formal : Entity_Id;
979 N_Node : Node_Id;
980 Post_Call : List_Id;
981 E_Actual : Entity_Id;
982 E_Formal : Entity_Id;
984 procedure Add_Call_By_Copy_Code;
985 -- For cases where the parameter must be passed by copy, this routine
986 -- generates a temporary variable into which the actual is copied and
987 -- then passes this as the parameter. For an OUT or IN OUT parameter,
988 -- an assignment is also generated to copy the result back. The call
989 -- also takes care of any constraint checks required for the type
990 -- conversion case (on both the way in and the way out).
992 procedure Add_Simple_Call_By_Copy_Code;
993 -- This is similar to the above, but is used in cases where we know
994 -- that all that is needed is to simply create a temporary and copy
995 -- the value in and out of the temporary.
997 procedure Check_Fortran_Logical;
998 -- A value of type Logical that is passed through a formal parameter
999 -- must be normalized because .TRUE. usually does not have the same
1000 -- representation as True. We assume that .FALSE. = False = 0.
1001 -- What about functions that return a logical type ???
1003 function Is_Legal_Copy return Boolean;
1004 -- Check that an actual can be copied before generating the temporary
1005 -- to be used in the call. If the actual is of a by_reference type then
1006 -- the program is illegal (this can only happen in the presence of
1007 -- rep. clauses that force an incorrect alignment). If the formal is
1008 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
1009 -- the effect that this might lead to unaligned arguments.
1011 function Make_Var (Actual : Node_Id) return Entity_Id;
1012 -- Returns an entity that refers to the given actual parameter, Actual
1013 -- (not including any type conversion). If Actual is an entity name,
1014 -- then this entity is returned unchanged, otherwise a renaming is
1015 -- created to provide an entity for the actual.
1017 procedure Reset_Packed_Prefix;
1018 -- The expansion of a packed array component reference is delayed in
1019 -- the context of a call. Now we need to complete the expansion, so we
1020 -- unmark the analyzed bits in all prefixes.
1022 ---------------------------
1023 -- Add_Call_By_Copy_Code --
1024 ---------------------------
1026 procedure Add_Call_By_Copy_Code is
1027 Expr : Node_Id;
1028 Init : Node_Id;
1029 Temp : Entity_Id;
1030 Indic : Node_Id;
1031 Var : Entity_Id;
1032 F_Typ : constant Entity_Id := Etype (Formal);
1033 V_Typ : Entity_Id;
1034 Crep : Boolean;
1036 begin
1037 if not Is_Legal_Copy then
1038 return;
1039 end if;
1041 Temp := Make_Temporary (Loc, 'T', Actual);
1043 -- Use formal type for temp, unless formal type is an unconstrained
1044 -- array, in which case we don't have to worry about bounds checks,
1045 -- and we use the actual type, since that has appropriate bounds.
1047 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1048 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1049 else
1050 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1051 end if;
1053 if Nkind (Actual) = N_Type_Conversion then
1054 V_Typ := Etype (Expression (Actual));
1056 -- If the formal is an (in-)out parameter, capture the name
1057 -- of the variable in order to build the post-call assignment.
1059 Var := Make_Var (Expression (Actual));
1061 Crep := not Same_Representation
1062 (F_Typ, Etype (Expression (Actual)));
1064 else
1065 V_Typ := Etype (Actual);
1066 Var := Make_Var (Actual);
1067 Crep := False;
1068 end if;
1070 -- Setup initialization for case of in out parameter, or an out
1071 -- parameter where the formal is an unconstrained array (in the
1072 -- latter case, we have to pass in an object with bounds).
1074 -- If this is an out parameter, the initial copy is wasteful, so as
1075 -- an optimization for the one-dimensional case we extract the
1076 -- bounds of the actual and build an uninitialized temporary of the
1077 -- right size.
1079 if Ekind (Formal) = E_In_Out_Parameter
1080 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1081 then
1082 if Nkind (Actual) = N_Type_Conversion then
1083 if Conversion_OK (Actual) then
1084 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1085 else
1086 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1087 end if;
1089 elsif Ekind (Formal) = E_Out_Parameter
1090 and then Is_Array_Type (F_Typ)
1091 and then Number_Dimensions (F_Typ) = 1
1092 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1093 then
1094 -- Actual is a one-dimensional array or slice, and the type
1095 -- requires no initialization. Create a temporary of the
1096 -- right size, but do not copy actual into it (optimization).
1098 Init := Empty;
1099 Indic :=
1100 Make_Subtype_Indication (Loc,
1101 Subtype_Mark => New_Occurrence_Of (F_Typ, Loc),
1102 Constraint =>
1103 Make_Index_Or_Discriminant_Constraint (Loc,
1104 Constraints => New_List (
1105 Make_Range (Loc,
1106 Low_Bound =>
1107 Make_Attribute_Reference (Loc,
1108 Prefix => New_Occurrence_Of (Var, Loc),
1109 Attribute_Name => Name_First),
1110 High_Bound =>
1111 Make_Attribute_Reference (Loc,
1112 Prefix => New_Occurrence_Of (Var, Loc),
1113 Attribute_Name => Name_Last)))));
1115 else
1116 Init := New_Occurrence_Of (Var, Loc);
1117 end if;
1119 -- An initialization is created for packed conversions as
1120 -- actuals for out parameters to enable Make_Object_Declaration
1121 -- to determine the proper subtype for N_Node. Note that this
1122 -- is wasteful because the extra copying on the call side is
1123 -- not required for such out parameters. ???
1125 elsif Ekind (Formal) = E_Out_Parameter
1126 and then Nkind (Actual) = N_Type_Conversion
1127 and then (Is_Bit_Packed_Array (F_Typ)
1128 or else
1129 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1130 then
1131 if Conversion_OK (Actual) then
1132 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1133 else
1134 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1135 end if;
1137 elsif Ekind (Formal) = E_In_Parameter then
1139 -- Handle the case in which the actual is a type conversion
1141 if Nkind (Actual) = N_Type_Conversion then
1142 if Conversion_OK (Actual) then
1143 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1144 else
1145 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1146 end if;
1147 else
1148 Init := New_Occurrence_Of (Var, Loc);
1149 end if;
1151 else
1152 Init := Empty;
1153 end if;
1155 N_Node :=
1156 Make_Object_Declaration (Loc,
1157 Defining_Identifier => Temp,
1158 Object_Definition => Indic,
1159 Expression => Init);
1160 Set_Assignment_OK (N_Node);
1161 Insert_Action (N, N_Node);
1163 -- Now, normally the deal here is that we use the defining
1164 -- identifier created by that object declaration. There is
1165 -- one exception to this. In the change of representation case
1166 -- the above declaration will end up looking like:
1168 -- temp : type := identifier;
1170 -- And in this case we might as well use the identifier directly
1171 -- and eliminate the temporary. Note that the analysis of the
1172 -- declaration was not a waste of time in that case, since it is
1173 -- what generated the necessary change of representation code. If
1174 -- the change of representation introduced additional code, as in
1175 -- a fixed-integer conversion, the expression is not an identifier
1176 -- and must be kept.
1178 if Crep
1179 and then Present (Expression (N_Node))
1180 and then Is_Entity_Name (Expression (N_Node))
1181 then
1182 Temp := Entity (Expression (N_Node));
1183 Rewrite (N_Node, Make_Null_Statement (Loc));
1184 end if;
1186 -- For IN parameter, all we do is to replace the actual
1188 if Ekind (Formal) = E_In_Parameter then
1189 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1190 Analyze (Actual);
1192 -- Processing for OUT or IN OUT parameter
1194 else
1195 -- Kill current value indications for the temporary variable we
1196 -- created, since we just passed it as an OUT parameter.
1198 Kill_Current_Values (Temp);
1199 Set_Is_Known_Valid (Temp, False);
1201 -- If type conversion, use reverse conversion on exit
1203 if Nkind (Actual) = N_Type_Conversion then
1204 if Conversion_OK (Actual) then
1205 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1206 else
1207 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1208 end if;
1209 else
1210 Expr := New_Occurrence_Of (Temp, Loc);
1211 end if;
1213 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1214 Analyze (Actual);
1216 -- If the actual is a conversion of a packed reference, it may
1217 -- already have been expanded by Remove_Side_Effects, and the
1218 -- resulting variable is a temporary which does not designate
1219 -- the proper out-parameter, which may not be addressable. In
1220 -- that case, generate an assignment to the original expression
1221 -- (before expansion of the packed reference) so that the proper
1222 -- expansion of assignment to a packed component can take place.
1224 declare
1225 Obj : Node_Id;
1226 Lhs : Node_Id;
1228 begin
1229 if Is_Renaming_Of_Object (Var)
1230 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1231 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1232 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1233 = N_Indexed_Component
1234 and then
1235 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1236 then
1237 Obj := Renamed_Object (Var);
1238 Lhs :=
1239 Make_Selected_Component (Loc,
1240 Prefix =>
1241 New_Copy_Tree (Original_Node (Prefix (Obj))),
1242 Selector_Name => New_Copy (Selector_Name (Obj)));
1243 Reset_Analyzed_Flags (Lhs);
1245 else
1246 Lhs := New_Occurrence_Of (Var, Loc);
1247 end if;
1249 Set_Assignment_OK (Lhs);
1251 if Is_Access_Type (E_Formal)
1252 and then Is_Entity_Name (Lhs)
1253 and then
1254 Present (Effective_Extra_Accessibility (Entity (Lhs)))
1255 then
1256 -- Copyback target is an Ada 2012 stand-alone object of an
1257 -- anonymous access type.
1259 pragma Assert (Ada_Version >= Ada_2012);
1261 if Type_Access_Level (E_Formal) >
1262 Object_Access_Level (Lhs)
1263 then
1264 Append_To (Post_Call,
1265 Make_Raise_Program_Error (Loc,
1266 Reason => PE_Accessibility_Check_Failed));
1267 end if;
1269 Append_To (Post_Call,
1270 Make_Assignment_Statement (Loc,
1271 Name => Lhs,
1272 Expression => Expr));
1274 -- We would like to somehow suppress generation of the
1275 -- extra_accessibility assignment generated by the expansion
1276 -- of the above assignment statement. It's not a correctness
1277 -- issue because the following assignment renders it dead,
1278 -- but generating back-to-back assignments to the same
1279 -- target is undesirable. ???
1281 Append_To (Post_Call,
1282 Make_Assignment_Statement (Loc,
1283 Name => New_Occurrence_Of (
1284 Effective_Extra_Accessibility (Entity (Lhs)), Loc),
1285 Expression => Make_Integer_Literal (Loc,
1286 Type_Access_Level (E_Formal))));
1288 else
1289 Append_To (Post_Call,
1290 Make_Assignment_Statement (Loc,
1291 Name => Lhs,
1292 Expression => Expr));
1293 end if;
1294 end;
1295 end if;
1296 end Add_Call_By_Copy_Code;
1298 ----------------------------------
1299 -- Add_Simple_Call_By_Copy_Code --
1300 ----------------------------------
1302 procedure Add_Simple_Call_By_Copy_Code is
1303 Temp : Entity_Id;
1304 Decl : Node_Id;
1305 Incod : Node_Id;
1306 Outcod : Node_Id;
1307 Lhs : Node_Id;
1308 Rhs : Node_Id;
1309 Indic : Node_Id;
1310 F_Typ : constant Entity_Id := Etype (Formal);
1312 begin
1313 if not Is_Legal_Copy then
1314 return;
1315 end if;
1317 -- Use formal type for temp, unless formal type is an unconstrained
1318 -- array, in which case we don't have to worry about bounds checks,
1319 -- and we use the actual type, since that has appropriate bounds.
1321 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1322 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1323 else
1324 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1325 end if;
1327 -- Prepare to generate code
1329 Reset_Packed_Prefix;
1331 Temp := Make_Temporary (Loc, 'T', Actual);
1332 Incod := Relocate_Node (Actual);
1333 Outcod := New_Copy_Tree (Incod);
1335 -- Generate declaration of temporary variable, initializing it
1336 -- with the input parameter unless we have an OUT formal or
1337 -- this is an initialization call.
1339 -- If the formal is an out parameter with discriminants, the
1340 -- discriminants must be captured even if the rest of the object
1341 -- is in principle uninitialized, because the discriminants may
1342 -- be read by the called subprogram.
1344 if Ekind (Formal) = E_Out_Parameter then
1345 Incod := Empty;
1347 if Has_Discriminants (Etype (Formal)) then
1348 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1349 end if;
1351 elsif Inside_Init_Proc then
1353 -- Could use a comment here to match comment below ???
1355 if Nkind (Actual) /= N_Selected_Component
1356 or else
1357 not Has_Discriminant_Dependent_Constraint
1358 (Entity (Selector_Name (Actual)))
1359 then
1360 Incod := Empty;
1362 -- Otherwise, keep the component in order to generate the proper
1363 -- actual subtype, that depends on enclosing discriminants.
1365 else
1366 null;
1367 end if;
1368 end if;
1370 Decl :=
1371 Make_Object_Declaration (Loc,
1372 Defining_Identifier => Temp,
1373 Object_Definition => Indic,
1374 Expression => Incod);
1376 if Inside_Init_Proc
1377 and then No (Incod)
1378 then
1379 -- If the call is to initialize a component of a composite type,
1380 -- and the component does not depend on discriminants, use the
1381 -- actual type of the component. This is required in case the
1382 -- component is constrained, because in general the formal of the
1383 -- initialization procedure will be unconstrained. Note that if
1384 -- the component being initialized is constrained by an enclosing
1385 -- discriminant, the presence of the initialization in the
1386 -- declaration will generate an expression for the actual subtype.
1388 Set_No_Initialization (Decl);
1389 Set_Object_Definition (Decl,
1390 New_Occurrence_Of (Etype (Actual), Loc));
1391 end if;
1393 Insert_Action (N, Decl);
1395 -- The actual is simply a reference to the temporary
1397 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1399 -- Generate copy out if OUT or IN OUT parameter
1401 if Ekind (Formal) /= E_In_Parameter then
1402 Lhs := Outcod;
1403 Rhs := New_Occurrence_Of (Temp, Loc);
1405 -- Deal with conversion
1407 if Nkind (Lhs) = N_Type_Conversion then
1408 Lhs := Expression (Lhs);
1409 Rhs := Convert_To (Etype (Actual), Rhs);
1410 end if;
1412 Append_To (Post_Call,
1413 Make_Assignment_Statement (Loc,
1414 Name => Lhs,
1415 Expression => Rhs));
1416 Set_Assignment_OK (Name (Last (Post_Call)));
1417 end if;
1418 end Add_Simple_Call_By_Copy_Code;
1420 ---------------------------
1421 -- Check_Fortran_Logical --
1422 ---------------------------
1424 procedure Check_Fortran_Logical is
1425 Logical : constant Entity_Id := Etype (Formal);
1426 Var : Entity_Id;
1428 -- Note: this is very incomplete, e.g. it does not handle arrays
1429 -- of logical values. This is really not the right approach at all???)
1431 begin
1432 if Convention (Subp) = Convention_Fortran
1433 and then Root_Type (Etype (Formal)) = Standard_Boolean
1434 and then Ekind (Formal) /= E_In_Parameter
1435 then
1436 Var := Make_Var (Actual);
1437 Append_To (Post_Call,
1438 Make_Assignment_Statement (Loc,
1439 Name => New_Occurrence_Of (Var, Loc),
1440 Expression =>
1441 Unchecked_Convert_To (
1442 Logical,
1443 Make_Op_Ne (Loc,
1444 Left_Opnd => New_Occurrence_Of (Var, Loc),
1445 Right_Opnd =>
1446 Unchecked_Convert_To (
1447 Logical,
1448 New_Occurrence_Of (Standard_False, Loc))))));
1449 end if;
1450 end Check_Fortran_Logical;
1452 -------------------
1453 -- Is_Legal_Copy --
1454 -------------------
1456 function Is_Legal_Copy return Boolean is
1457 begin
1458 -- An attempt to copy a value of such a type can only occur if
1459 -- representation clauses give the actual a misaligned address.
1461 if Is_By_Reference_Type (Etype (Formal)) then
1463 -- If the front-end does not perform full type layout, the actual
1464 -- may in fact be properly aligned but there is not enough front-
1465 -- end information to determine this. In that case gigi will emit
1466 -- an error if a copy is not legal, or generate the proper code.
1467 -- For other backends we report the error now.
1469 -- Seems wrong to be issuing an error in the expander, since it
1470 -- will be missed in -gnatc mode ???
1472 if Frontend_Layout_On_Target then
1473 Error_Msg_N
1474 ("misaligned actual cannot be passed by reference", Actual);
1475 end if;
1477 return False;
1479 -- For users of Starlet, we assume that the specification of by-
1480 -- reference mechanism is mandatory. This may lead to unaligned
1481 -- objects but at least for DEC legacy code it is known to work.
1482 -- The warning will alert users of this code that a problem may
1483 -- be lurking.
1485 elsif Mechanism (Formal) = By_Reference
1486 and then Is_Valued_Procedure (Scope (Formal))
1487 then
1488 Error_Msg_N
1489 ("by_reference actual may be misaligned??", Actual);
1490 return False;
1492 else
1493 return True;
1494 end if;
1495 end Is_Legal_Copy;
1497 --------------
1498 -- Make_Var --
1499 --------------
1501 function Make_Var (Actual : Node_Id) return Entity_Id is
1502 Var : Entity_Id;
1504 begin
1505 if Is_Entity_Name (Actual) then
1506 return Entity (Actual);
1508 else
1509 Var := Make_Temporary (Loc, 'T', Actual);
1511 N_Node :=
1512 Make_Object_Renaming_Declaration (Loc,
1513 Defining_Identifier => Var,
1514 Subtype_Mark =>
1515 New_Occurrence_Of (Etype (Actual), Loc),
1516 Name => Relocate_Node (Actual));
1518 Insert_Action (N, N_Node);
1519 return Var;
1520 end if;
1521 end Make_Var;
1523 -------------------------
1524 -- Reset_Packed_Prefix --
1525 -------------------------
1527 procedure Reset_Packed_Prefix is
1528 Pfx : Node_Id := Actual;
1529 begin
1530 loop
1531 Set_Analyzed (Pfx, False);
1532 exit when
1533 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1534 Pfx := Prefix (Pfx);
1535 end loop;
1536 end Reset_Packed_Prefix;
1538 -- Start of processing for Expand_Actuals
1540 begin
1541 Post_Call := New_List;
1543 Formal := First_Formal (Subp);
1544 Actual := First_Actual (N);
1545 while Present (Formal) loop
1546 E_Formal := Etype (Formal);
1547 E_Actual := Etype (Actual);
1549 if Is_Scalar_Type (E_Formal)
1550 or else Nkind (Actual) = N_Slice
1551 then
1552 Check_Fortran_Logical;
1554 -- RM 6.4.1 (11)
1556 elsif Ekind (Formal) /= E_Out_Parameter then
1558 -- The unusual case of the current instance of a protected type
1559 -- requires special handling. This can only occur in the context
1560 -- of a call within the body of a protected operation.
1562 if Is_Entity_Name (Actual)
1563 and then Ekind (Entity (Actual)) = E_Protected_Type
1564 and then In_Open_Scopes (Entity (Actual))
1565 then
1566 if Scope (Subp) /= Entity (Actual) then
1567 Error_Msg_N
1568 ("operation outside protected type may not "
1569 & "call back its protected operations??", Actual);
1570 end if;
1572 Rewrite (Actual,
1573 Expand_Protected_Object_Reference (N, Entity (Actual)));
1574 end if;
1576 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1577 -- build-in-place function, then a temporary return object needs
1578 -- to be created and access to it must be passed to the function.
1579 -- Currently we limit such functions to those with inherently
1580 -- limited result subtypes, but eventually we plan to expand the
1581 -- functions that are treated as build-in-place to include other
1582 -- composite result types.
1584 if Is_Build_In_Place_Function_Call (Actual) then
1585 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1586 end if;
1588 Apply_Constraint_Check (Actual, E_Formal);
1590 -- Out parameter case. No constraint checks on access type
1591 -- RM 6.4.1 (13)
1593 elsif Is_Access_Type (E_Formal) then
1594 null;
1596 -- RM 6.4.1 (14)
1598 elsif Has_Discriminants (Base_Type (E_Formal))
1599 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1600 then
1601 Apply_Constraint_Check (Actual, E_Formal);
1603 -- RM 6.4.1 (15)
1605 else
1606 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1607 end if;
1609 -- Processing for IN-OUT and OUT parameters
1611 if Ekind (Formal) /= E_In_Parameter then
1613 -- For type conversions of arrays, apply length/range checks
1615 if Is_Array_Type (E_Formal)
1616 and then Nkind (Actual) = N_Type_Conversion
1617 then
1618 if Is_Constrained (E_Formal) then
1619 Apply_Length_Check (Expression (Actual), E_Formal);
1620 else
1621 Apply_Range_Check (Expression (Actual), E_Formal);
1622 end if;
1623 end if;
1625 -- If argument is a type conversion for a type that is passed
1626 -- by copy, then we must pass the parameter by copy.
1628 if Nkind (Actual) = N_Type_Conversion
1629 and then
1630 (Is_Numeric_Type (E_Formal)
1631 or else Is_Access_Type (E_Formal)
1632 or else Is_Enumeration_Type (E_Formal)
1633 or else Is_Bit_Packed_Array (Etype (Formal))
1634 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1636 -- Also pass by copy if change of representation
1638 or else not Same_Representation
1639 (Etype (Formal),
1640 Etype (Expression (Actual))))
1641 then
1642 Add_Call_By_Copy_Code;
1644 -- References to components of bit packed arrays are expanded
1645 -- at this point, rather than at the point of analysis of the
1646 -- actuals, to handle the expansion of the assignment to
1647 -- [in] out parameters.
1649 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1650 Add_Simple_Call_By_Copy_Code;
1652 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1653 -- because the back-end cannot cope with such objects. In other
1654 -- cases where alignment forces a copy, the back-end generates
1655 -- it properly. It should not be generated unconditionally in the
1656 -- front-end because it does not know precisely the alignment
1657 -- requirements of the target, and makes too conservative an
1658 -- estimate, leading to superfluous copies or spurious errors
1659 -- on by-reference parameters.
1661 elsif Nkind (Actual) = N_Selected_Component
1662 and then
1663 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1664 and then not Represented_As_Scalar (Etype (Formal))
1665 then
1666 Add_Simple_Call_By_Copy_Code;
1668 -- References to slices of bit packed arrays are expanded
1670 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1671 Add_Call_By_Copy_Code;
1673 -- References to possibly unaligned slices of arrays are expanded
1675 elsif Is_Possibly_Unaligned_Slice (Actual) then
1676 Add_Call_By_Copy_Code;
1678 -- Deal with access types where the actual subtype and the
1679 -- formal subtype are not the same, requiring a check.
1681 -- It is necessary to exclude tagged types because of "downward
1682 -- conversion" errors.
1684 elsif Is_Access_Type (E_Formal)
1685 and then not Same_Type (E_Formal, E_Actual)
1686 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1687 then
1688 Add_Call_By_Copy_Code;
1690 -- If the actual is not a scalar and is marked for volatile
1691 -- treatment, whereas the formal is not volatile, then pass
1692 -- by copy unless it is a by-reference type.
1694 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1695 -- because this is the enforcement of a language rule that applies
1696 -- only to "real" volatile variables, not e.g. to the address
1697 -- clause overlay case.
1699 elsif Is_Entity_Name (Actual)
1700 and then Is_Volatile (Entity (Actual))
1701 and then not Is_By_Reference_Type (E_Actual)
1702 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1703 and then not Is_Volatile (E_Formal)
1704 then
1705 Add_Call_By_Copy_Code;
1707 elsif Nkind (Actual) = N_Indexed_Component
1708 and then Is_Entity_Name (Prefix (Actual))
1709 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1710 then
1711 Add_Call_By_Copy_Code;
1713 -- Add call-by-copy code for the case of scalar out parameters
1714 -- when it is not known at compile time that the subtype of the
1715 -- formal is a subrange of the subtype of the actual (or vice
1716 -- versa for in out parameters), in order to get range checks
1717 -- on such actuals. (Maybe this case should be handled earlier
1718 -- in the if statement???)
1720 elsif Is_Scalar_Type (E_Formal)
1721 and then
1722 (not In_Subrange_Of (E_Formal, E_Actual)
1723 or else
1724 (Ekind (Formal) = E_In_Out_Parameter
1725 and then not In_Subrange_Of (E_Actual, E_Formal)))
1726 then
1727 -- Perhaps the setting back to False should be done within
1728 -- Add_Call_By_Copy_Code, since it could get set on other
1729 -- cases occurring above???
1731 if Do_Range_Check (Actual) then
1732 Set_Do_Range_Check (Actual, False);
1733 end if;
1735 Add_Call_By_Copy_Code;
1736 end if;
1738 -- RM 3.2.4 (23/3): A predicate is checked on in-out and out
1739 -- by-reference parameters on exit from the call. If the actual
1740 -- is a derived type and the operation is inherited, the body
1741 -- of the operation will not contain a call to the predicate
1742 -- function, so it must be done explicitly after the call. Ditto
1743 -- if the actual is an entity of a predicated subtype.
1745 -- The rule refers to by-reference types, but a check is needed
1746 -- for by-copy types as well. That check is subsumed by the rule
1747 -- for subtype conversion on assignment, but we can generate the
1748 -- required check now.
1750 -- Note also that Subp may be either a subprogram entity for
1751 -- direct calls, or a type entity for indirect calls, which must
1752 -- be handled separately because the name does not denote an
1753 -- overloadable entity.
1755 By_Ref_Predicate_Check : declare
1756 Aund : constant Entity_Id := Underlying_Type (E_Actual);
1757 Atyp : Entity_Id;
1759 function Is_Public_Subp return Boolean;
1760 -- Check whether the subprogram being called is a visible
1761 -- operation of the type of the actual. Used to determine
1762 -- whether an invariant check must be generated on the
1763 -- caller side.
1765 ---------------------
1766 -- Is_Public_Subp --
1767 ---------------------
1769 function Is_Public_Subp return Boolean is
1770 Pack : constant Entity_Id := Scope (Subp);
1771 Subp_Decl : Node_Id;
1773 begin
1774 if not Is_Subprogram (Subp) then
1775 return False;
1777 -- The operation may be inherited, or a primitive of the
1778 -- root type.
1780 elsif
1781 Nkind_In (Parent (Subp), N_Private_Extension_Declaration,
1782 N_Full_Type_Declaration)
1783 then
1784 Subp_Decl := Parent (Subp);
1786 else
1787 Subp_Decl := Unit_Declaration_Node (Subp);
1788 end if;
1790 return Ekind (Pack) = E_Package
1791 and then
1792 List_Containing (Subp_Decl) =
1793 Visible_Declarations
1794 (Specification (Unit_Declaration_Node (Pack)));
1795 end Is_Public_Subp;
1797 -- Start of processing for By_Ref_Predicate_Check
1799 begin
1800 if No (Aund) then
1801 Atyp := E_Actual;
1802 else
1803 Atyp := Aund;
1804 end if;
1806 if Has_Predicates (Atyp)
1807 and then Present (Predicate_Function (Atyp))
1809 -- Skip predicate checks for special cases
1811 and then Predicate_Tests_On_Arguments (Subp)
1812 then
1813 Append_To (Post_Call,
1814 Make_Predicate_Check (Atyp, Actual));
1815 end if;
1817 -- We generated caller-side invariant checks in two cases:
1819 -- a) when calling an inherited operation, where there is an
1820 -- implicit view conversion of the actual to the parent type.
1822 -- b) When the conversion is explicit
1824 -- We treat these cases separately because the required
1825 -- conversion for a) is added later when expanding the call.
1827 if Has_Invariants (Etype (Actual))
1828 and then
1829 Nkind (Parent (Subp)) = N_Private_Extension_Declaration
1830 then
1831 if Comes_From_Source (N) and then Is_Public_Subp then
1832 Append_To (Post_Call, Make_Invariant_Call (Actual));
1833 end if;
1835 elsif Nkind (Actual) = N_Type_Conversion
1836 and then Has_Invariants (Etype (Expression (Actual)))
1837 then
1838 if Comes_From_Source (N) and then Is_Public_Subp then
1839 Append_To (Post_Call,
1840 Make_Invariant_Call (Expression (Actual)));
1841 end if;
1842 end if;
1843 end By_Ref_Predicate_Check;
1845 -- Processing for IN parameters
1847 else
1848 -- For IN parameters is in the packed array case, we expand an
1849 -- indexed component (the circuit in Exp_Ch4 deliberately left
1850 -- indexed components appearing as actuals untouched, so that
1851 -- the special processing above for the OUT and IN OUT cases
1852 -- could be performed. We could make the test in Exp_Ch4 more
1853 -- complex and have it detect the parameter mode, but it is
1854 -- easier simply to handle all cases here.)
1856 if Nkind (Actual) = N_Indexed_Component
1857 and then Is_Packed (Etype (Prefix (Actual)))
1858 then
1859 Reset_Packed_Prefix;
1860 Expand_Packed_Element_Reference (Actual);
1862 -- If we have a reference to a bit packed array, we copy it, since
1863 -- the actual must be byte aligned.
1865 -- Is this really necessary in all cases???
1867 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1868 Add_Simple_Call_By_Copy_Code;
1870 -- If a non-scalar actual is possibly unaligned, we need a copy
1872 elsif Is_Possibly_Unaligned_Object (Actual)
1873 and then not Represented_As_Scalar (Etype (Formal))
1874 then
1875 Add_Simple_Call_By_Copy_Code;
1877 -- Similarly, we have to expand slices of packed arrays here
1878 -- because the result must be byte aligned.
1880 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1881 Add_Call_By_Copy_Code;
1883 -- Only processing remaining is to pass by copy if this is a
1884 -- reference to a possibly unaligned slice, since the caller
1885 -- expects an appropriately aligned argument.
1887 elsif Is_Possibly_Unaligned_Slice (Actual) then
1888 Add_Call_By_Copy_Code;
1890 -- An unusual case: a current instance of an enclosing task can be
1891 -- an actual, and must be replaced by a reference to self.
1893 elsif Is_Entity_Name (Actual)
1894 and then Is_Task_Type (Entity (Actual))
1895 then
1896 if In_Open_Scopes (Entity (Actual)) then
1897 Rewrite (Actual,
1898 (Make_Function_Call (Loc,
1899 Name => New_Occurrence_Of (RTE (RE_Self), Loc))));
1900 Analyze (Actual);
1902 -- A task type cannot otherwise appear as an actual
1904 else
1905 raise Program_Error;
1906 end if;
1907 end if;
1908 end if;
1910 Next_Formal (Formal);
1911 Next_Actual (Actual);
1912 end loop;
1914 -- Find right place to put post call stuff if it is present
1916 if not Is_Empty_List (Post_Call) then
1918 -- Cases where the call is not a member of a statement list
1920 if not Is_List_Member (N) then
1922 -- In Ada 2012 the call may be a function call in an expression
1923 -- (since OUT and IN OUT parameters are now allowed for such
1924 -- calls). The write-back of (in)-out parameters is handled
1925 -- by the back-end, but the constraint checks generated when
1926 -- subtypes of formal and actual don't match must be inserted
1927 -- in the form of assignments.
1929 if Ada_Version >= Ada_2012
1930 and then Nkind (N) = N_Function_Call
1931 then
1932 -- We used to just do handle this by climbing up parents to
1933 -- a non-statement/declaration and then simply making a call
1934 -- to Insert_Actions_After (P, Post_Call), but that doesn't
1935 -- work. If we are in the middle of an expression, e.g. the
1936 -- condition of an IF, this call would insert after the IF
1937 -- statement, which is much too late to be doing the write
1938 -- back. For example:
1940 -- if Clobber (X) then
1941 -- Put_Line (X'Img);
1942 -- else
1943 -- goto Junk
1944 -- end if;
1946 -- Now assume Clobber changes X, if we put the write back
1947 -- after the IF, the Put_Line gets the wrong value and the
1948 -- goto causes the write back to be skipped completely.
1950 -- To deal with this, we replace the call by
1952 -- do
1953 -- Tnnn : function-result-type renames function-call;
1954 -- Post_Call actions
1955 -- in
1956 -- Tnnn;
1957 -- end;
1959 -- Note: this won't do in Modify_Tree_For_C mode, but we
1960 -- will deal with that later (it will require creating a
1961 -- declaration for Temp, using Insert_Declaration) ???
1963 declare
1964 Tnnn : constant Entity_Id := Make_Temporary (Loc, 'T');
1965 FRTyp : constant Entity_Id := Etype (N);
1966 Name : constant Node_Id := Relocate_Node (N);
1968 begin
1969 Prepend_To (Post_Call,
1970 Make_Object_Renaming_Declaration (Loc,
1971 Defining_Identifier => Tnnn,
1972 Subtype_Mark => New_Occurrence_Of (FRTyp, Loc),
1973 Name => Name));
1975 Rewrite (N,
1976 Make_Expression_With_Actions (Loc,
1977 Actions => Post_Call,
1978 Expression => New_Occurrence_Of (Tnnn, Loc)));
1980 -- We don't want to just blindly call Analyze_And_Resolve
1981 -- because that would cause unwanted recursion on the call.
1982 -- So for a moment set the call as analyzed to prevent that
1983 -- recursion, and get the rest analyzed properly, then reset
1984 -- the analyzed flag, so our caller can continue.
1986 Set_Analyzed (Name, True);
1987 Analyze_And_Resolve (N, FRTyp);
1988 Set_Analyzed (Name, False);
1990 -- Reset calling argument to point to function call inside
1991 -- the expression with actions so the caller can continue
1992 -- to process the call.
1994 N := Name;
1995 end;
1997 -- If not the special Ada 2012 case of a function call, then
1998 -- we must have the triggering statement of a triggering
1999 -- alternative or an entry call alternative, and we can add
2000 -- the post call stuff to the corresponding statement list.
2002 else
2003 declare
2004 P : Node_Id;
2006 begin
2007 P := Parent (N);
2008 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
2009 N_Entry_Call_Alternative));
2011 if Is_Non_Empty_List (Statements (P)) then
2012 Insert_List_Before_And_Analyze
2013 (First (Statements (P)), Post_Call);
2014 else
2015 Set_Statements (P, Post_Call);
2016 end if;
2018 return;
2019 end;
2020 end if;
2022 -- Otherwise, normal case where N is in a statement sequence,
2023 -- just put the post-call stuff after the call statement.
2025 else
2026 Insert_Actions_After (N, Post_Call);
2027 return;
2028 end if;
2029 end if;
2031 -- The call node itself is re-analyzed in Expand_Call
2033 end Expand_Actuals;
2035 -----------------
2036 -- Expand_Call --
2037 -----------------
2039 -- This procedure handles expansion of function calls and procedure call
2040 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
2041 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
2043 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
2044 -- Provide values of actuals for all formals in Extra_Formals list
2045 -- Replace "call" to enumeration literal function by literal itself
2046 -- Rewrite call to predefined operator as operator
2047 -- Replace actuals to in-out parameters that are numeric conversions,
2048 -- with explicit assignment to temporaries before and after the call.
2050 -- Note that the list of actuals has been filled with default expressions
2051 -- during semantic analysis of the call. Only the extra actuals required
2052 -- for the 'Constrained attribute and for accessibility checks are added
2053 -- at this point.
2055 procedure Expand_Call (N : Node_Id) is
2056 Loc : constant Source_Ptr := Sloc (N);
2057 Call_Node : Node_Id := N;
2058 Extra_Actuals : List_Id := No_List;
2059 Prev : Node_Id := Empty;
2061 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
2062 -- Adds one entry to the end of the actual parameter list. Used for
2063 -- default parameters and for extra actuals (for Extra_Formals). The
2064 -- argument is an N_Parameter_Association node.
2066 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
2067 -- Adds an extra actual to the list of extra actuals. Expr is the
2068 -- expression for the value of the actual, EF is the entity for the
2069 -- extra formal.
2071 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
2072 -- Within an instance, a type derived from an untagged formal derived
2073 -- type inherits from the original parent, not from the actual. The
2074 -- current derivation mechanism has the derived type inherit from the
2075 -- actual, which is only correct outside of the instance. If the
2076 -- subprogram is inherited, we test for this particular case through a
2077 -- convoluted tree traversal before setting the proper subprogram to be
2078 -- called.
2080 function In_Unfrozen_Instance (E : Entity_Id) return Boolean;
2081 -- Return true if E comes from an instance that is not yet frozen
2083 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean;
2084 -- Determine if Subp denotes a non-dispatching call to a Deep routine
2086 function New_Value (From : Node_Id) return Node_Id;
2087 -- From is the original Expression. New_Value is equivalent to a call
2088 -- to Duplicate_Subexpr with an explicit dereference when From is an
2089 -- access parameter.
2091 --------------------------
2092 -- Add_Actual_Parameter --
2093 --------------------------
2095 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
2096 Actual_Expr : constant Node_Id :=
2097 Explicit_Actual_Parameter (Insert_Param);
2099 begin
2100 -- Case of insertion is first named actual
2102 if No (Prev) or else
2103 Nkind (Parent (Prev)) /= N_Parameter_Association
2104 then
2105 Set_Next_Named_Actual
2106 (Insert_Param, First_Named_Actual (Call_Node));
2107 Set_First_Named_Actual (Call_Node, Actual_Expr);
2109 if No (Prev) then
2110 if No (Parameter_Associations (Call_Node)) then
2111 Set_Parameter_Associations (Call_Node, New_List);
2112 end if;
2114 Append (Insert_Param, Parameter_Associations (Call_Node));
2116 else
2117 Insert_After (Prev, Insert_Param);
2118 end if;
2120 -- Case of insertion is not first named actual
2122 else
2123 Set_Next_Named_Actual
2124 (Insert_Param, Next_Named_Actual (Parent (Prev)));
2125 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
2126 Append (Insert_Param, Parameter_Associations (Call_Node));
2127 end if;
2129 Prev := Actual_Expr;
2130 end Add_Actual_Parameter;
2132 ----------------------
2133 -- Add_Extra_Actual --
2134 ----------------------
2136 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
2137 Loc : constant Source_Ptr := Sloc (Expr);
2139 begin
2140 if Extra_Actuals = No_List then
2141 Extra_Actuals := New_List;
2142 Set_Parent (Extra_Actuals, Call_Node);
2143 end if;
2145 Append_To (Extra_Actuals,
2146 Make_Parameter_Association (Loc,
2147 Selector_Name => New_Occurrence_Of (EF, Loc),
2148 Explicit_Actual_Parameter => Expr));
2150 Analyze_And_Resolve (Expr, Etype (EF));
2152 if Nkind (Call_Node) = N_Function_Call then
2153 Set_Is_Accessibility_Actual (Parent (Expr));
2154 end if;
2155 end Add_Extra_Actual;
2157 ---------------------------
2158 -- Inherited_From_Formal --
2159 ---------------------------
2161 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
2162 Par : Entity_Id;
2163 Gen_Par : Entity_Id;
2164 Gen_Prim : Elist_Id;
2165 Elmt : Elmt_Id;
2166 Indic : Node_Id;
2168 begin
2169 -- If the operation is inherited, it is attached to the corresponding
2170 -- type derivation. If the parent in the derivation is a generic
2171 -- actual, it is a subtype of the actual, and we have to recover the
2172 -- original derived type declaration to find the proper parent.
2174 if Nkind (Parent (S)) /= N_Full_Type_Declaration
2175 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
2176 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
2177 N_Derived_Type_Definition
2178 or else not In_Instance
2179 then
2180 return Empty;
2182 else
2183 Indic :=
2184 Subtype_Indication
2185 (Type_Definition (Original_Node (Parent (S))));
2187 if Nkind (Indic) = N_Subtype_Indication then
2188 Par := Entity (Subtype_Mark (Indic));
2189 else
2190 Par := Entity (Indic);
2191 end if;
2192 end if;
2194 if not Is_Generic_Actual_Type (Par)
2195 or else Is_Tagged_Type (Par)
2196 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
2197 or else not In_Open_Scopes (Scope (Par))
2198 then
2199 return Empty;
2200 else
2201 Gen_Par := Generic_Parent_Type (Parent (Par));
2202 end if;
2204 -- If the actual has no generic parent type, the formal is not
2205 -- a formal derived type, so nothing to inherit.
2207 if No (Gen_Par) then
2208 return Empty;
2209 end if;
2211 -- If the generic parent type is still the generic type, this is a
2212 -- private formal, not a derived formal, and there are no operations
2213 -- inherited from the formal.
2215 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
2216 return Empty;
2217 end if;
2219 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
2221 Elmt := First_Elmt (Gen_Prim);
2222 while Present (Elmt) loop
2223 if Chars (Node (Elmt)) = Chars (S) then
2224 declare
2225 F1 : Entity_Id;
2226 F2 : Entity_Id;
2228 begin
2229 F1 := First_Formal (S);
2230 F2 := First_Formal (Node (Elmt));
2231 while Present (F1)
2232 and then Present (F2)
2233 loop
2234 if Etype (F1) = Etype (F2)
2235 or else Etype (F2) = Gen_Par
2236 then
2237 Next_Formal (F1);
2238 Next_Formal (F2);
2239 else
2240 Next_Elmt (Elmt);
2241 exit; -- not the right subprogram
2242 end if;
2244 return Node (Elmt);
2245 end loop;
2246 end;
2248 else
2249 Next_Elmt (Elmt);
2250 end if;
2251 end loop;
2253 raise Program_Error;
2254 end Inherited_From_Formal;
2256 --------------------------
2257 -- In_Unfrozen_Instance --
2258 --------------------------
2260 function In_Unfrozen_Instance (E : Entity_Id) return Boolean is
2261 S : Entity_Id;
2263 begin
2264 S := E;
2265 while Present (S) and then S /= Standard_Standard loop
2266 if Is_Generic_Instance (S)
2267 and then Present (Freeze_Node (S))
2268 and then not Analyzed (Freeze_Node (S))
2269 then
2270 return True;
2271 end if;
2273 S := Scope (S);
2274 end loop;
2276 return False;
2277 end In_Unfrozen_Instance;
2279 -------------------------
2280 -- Is_Direct_Deep_Call --
2281 -------------------------
2283 function Is_Direct_Deep_Call (Subp : Entity_Id) return Boolean is
2284 begin
2285 if Is_TSS (Subp, TSS_Deep_Adjust)
2286 or else Is_TSS (Subp, TSS_Deep_Finalize)
2287 or else Is_TSS (Subp, TSS_Deep_Initialize)
2288 then
2289 declare
2290 Actual : Node_Id;
2291 Formal : Node_Id;
2293 begin
2294 Actual := First (Parameter_Associations (N));
2295 Formal := First_Formal (Subp);
2296 while Present (Actual)
2297 and then Present (Formal)
2298 loop
2299 if Nkind (Actual) = N_Identifier
2300 and then Is_Controlling_Actual (Actual)
2301 and then Etype (Actual) = Etype (Formal)
2302 then
2303 return True;
2304 end if;
2306 Next (Actual);
2307 Next_Formal (Formal);
2308 end loop;
2309 end;
2310 end if;
2312 return False;
2313 end Is_Direct_Deep_Call;
2315 ---------------
2316 -- New_Value --
2317 ---------------
2319 function New_Value (From : Node_Id) return Node_Id is
2320 Res : constant Node_Id := Duplicate_Subexpr (From);
2321 begin
2322 if Is_Access_Type (Etype (From)) then
2323 return Make_Explicit_Dereference (Sloc (From), Prefix => Res);
2324 else
2325 return Res;
2326 end if;
2327 end New_Value;
2329 -- Local variables
2331 Curr_S : constant Entity_Id := Current_Scope;
2332 Remote : constant Boolean := Is_Remote_Call (Call_Node);
2333 Actual : Node_Id;
2334 Formal : Entity_Id;
2335 Orig_Subp : Entity_Id := Empty;
2336 Param_Count : Natural := 0;
2337 Parent_Formal : Entity_Id;
2338 Parent_Subp : Entity_Id;
2339 Scop : Entity_Id;
2340 Subp : Entity_Id;
2342 Prev_Orig : Node_Id;
2343 -- Original node for an actual, which may have been rewritten. If the
2344 -- actual is a function call that has been transformed from a selected
2345 -- component, the original node is unanalyzed. Otherwise, it carries
2346 -- semantic information used to generate additional actuals.
2348 CW_Interface_Formals_Present : Boolean := False;
2350 -- Start of processing for Expand_Call
2352 begin
2353 -- Expand the procedure call if the first actual has a dimension and if
2354 -- the procedure is Put (Ada 2012).
2356 if Ada_Version >= Ada_2012
2357 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2358 and then Present (Parameter_Associations (Call_Node))
2359 then
2360 Expand_Put_Call_With_Symbol (Call_Node);
2361 end if;
2363 -- Ignore if previous error
2365 if Nkind (Call_Node) in N_Has_Etype
2366 and then Etype (Call_Node) = Any_Type
2367 then
2368 return;
2369 end if;
2371 -- Call using access to subprogram with explicit dereference
2373 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
2374 Subp := Etype (Name (Call_Node));
2375 Parent_Subp := Empty;
2377 -- Case of call to simple entry, where the Name is a selected component
2378 -- whose prefix is the task, and whose selector name is the entry name
2380 elsif Nkind (Name (Call_Node)) = N_Selected_Component then
2381 Subp := Entity (Selector_Name (Name (Call_Node)));
2382 Parent_Subp := Empty;
2384 -- Case of call to member of entry family, where Name is an indexed
2385 -- component, with the prefix being a selected component giving the
2386 -- task and entry family name, and the index being the entry index.
2388 elsif Nkind (Name (Call_Node)) = N_Indexed_Component then
2389 Subp := Entity (Selector_Name (Prefix (Name (Call_Node))));
2390 Parent_Subp := Empty;
2392 -- Normal case
2394 else
2395 Subp := Entity (Name (Call_Node));
2396 Parent_Subp := Alias (Subp);
2398 -- Replace call to Raise_Exception by call to Raise_Exception_Always
2399 -- if we can tell that the first parameter cannot possibly be null.
2400 -- This improves efficiency by avoiding a run-time test.
2402 -- We do not do this if Raise_Exception_Always does not exist, which
2403 -- can happen in configurable run time profiles which provide only a
2404 -- Raise_Exception.
2406 if Is_RTE (Subp, RE_Raise_Exception)
2407 and then RTE_Available (RE_Raise_Exception_Always)
2408 then
2409 declare
2410 FA : constant Node_Id :=
2411 Original_Node (First_Actual (Call_Node));
2413 begin
2414 -- The case we catch is where the first argument is obtained
2415 -- using the Identity attribute (which must always be
2416 -- non-null).
2418 if Nkind (FA) = N_Attribute_Reference
2419 and then Attribute_Name (FA) = Name_Identity
2420 then
2421 Subp := RTE (RE_Raise_Exception_Always);
2422 Set_Name (Call_Node, New_Occurrence_Of (Subp, Loc));
2423 end if;
2424 end;
2425 end if;
2427 if Ekind (Subp) = E_Entry then
2428 Parent_Subp := Empty;
2429 end if;
2430 end if;
2432 -- Detect the following code in System.Finalization_Masters only on
2433 -- .NET/JVM targets:
2435 -- procedure Finalize (Master : in out Finalization_Master) is
2436 -- begin
2437 -- . . .
2438 -- begin
2439 -- Finalize (Curr_Ptr.all);
2441 -- Since .NET/JVM compilers lack address arithmetic and Deep_Finalize
2442 -- cannot be named in library or user code, the compiler has to deal
2443 -- with this by transforming the call to Finalize into Deep_Finalize.
2445 if VM_Target /= No_VM
2446 and then Chars (Subp) = Name_Finalize
2447 and then Ekind (Curr_S) = E_Block
2448 and then Ekind (Scope (Curr_S)) = E_Procedure
2449 and then Chars (Scope (Curr_S)) = Name_Finalize
2450 and then Etype (First_Formal (Scope (Curr_S))) =
2451 RTE (RE_Finalization_Master)
2452 then
2453 declare
2454 Deep_Fin : constant Entity_Id :=
2455 Find_Prim_Op (RTE (RE_Root_Controlled),
2456 TSS_Deep_Finalize);
2457 begin
2458 -- Since Root_Controlled is a tagged type, the compiler should
2459 -- always generate Deep_Finalize for it.
2461 pragma Assert (Present (Deep_Fin));
2463 -- Generate:
2464 -- Deep_Finalize (Curr_Ptr.all);
2466 Rewrite (N,
2467 Make_Procedure_Call_Statement (Loc,
2468 Name =>
2469 New_Occurrence_Of (Deep_Fin, Loc),
2470 Parameter_Associations =>
2471 New_Copy_List_Tree (Parameter_Associations (N))));
2473 Analyze (N);
2474 return;
2475 end;
2476 end if;
2478 -- Ada 2005 (AI-345): We have a procedure call as a triggering
2479 -- alternative in an asynchronous select or as an entry call in
2480 -- a conditional or timed select. Check whether the procedure call
2481 -- is a renaming of an entry and rewrite it as an entry call.
2483 if Ada_Version >= Ada_2005
2484 and then Nkind (Call_Node) = N_Procedure_Call_Statement
2485 and then
2486 ((Nkind (Parent (Call_Node)) = N_Triggering_Alternative
2487 and then Triggering_Statement (Parent (Call_Node)) = Call_Node)
2488 or else
2489 (Nkind (Parent (Call_Node)) = N_Entry_Call_Alternative
2490 and then Entry_Call_Statement (Parent (Call_Node)) = Call_Node))
2491 then
2492 declare
2493 Ren_Decl : Node_Id;
2494 Ren_Root : Entity_Id := Subp;
2496 begin
2497 -- This may be a chain of renamings, find the root
2499 if Present (Alias (Ren_Root)) then
2500 Ren_Root := Alias (Ren_Root);
2501 end if;
2503 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2504 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2506 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2507 Rewrite (Call_Node,
2508 Make_Entry_Call_Statement (Loc,
2509 Name =>
2510 New_Copy_Tree (Name (Ren_Decl)),
2511 Parameter_Associations =>
2512 New_Copy_List_Tree
2513 (Parameter_Associations (Call_Node))));
2515 return;
2516 end if;
2517 end if;
2518 end;
2519 end if;
2521 -- First step, compute extra actuals, corresponding to any Extra_Formals
2522 -- present. Note that we do not access Extra_Formals directly, instead
2523 -- we simply note the presence of the extra formals as we process the
2524 -- regular formals collecting corresponding actuals in Extra_Actuals.
2526 -- We also generate any required range checks for actuals for in formals
2527 -- as we go through the loop, since this is a convenient place to do it.
2528 -- (Though it seems that this would be better done in Expand_Actuals???)
2530 -- Special case: Thunks must not compute the extra actuals; they must
2531 -- just propagate to the target primitive their extra actuals.
2533 if Is_Thunk (Current_Scope)
2534 and then Thunk_Entity (Current_Scope) = Subp
2535 and then Present (Extra_Formals (Subp))
2536 then
2537 pragma Assert (Present (Extra_Formals (Current_Scope)));
2539 declare
2540 Target_Formal : Entity_Id;
2541 Thunk_Formal : Entity_Id;
2543 begin
2544 Target_Formal := Extra_Formals (Subp);
2545 Thunk_Formal := Extra_Formals (Current_Scope);
2546 while Present (Target_Formal) loop
2547 Add_Extra_Actual
2548 (New_Occurrence_Of (Thunk_Formal, Loc), Thunk_Formal);
2550 Target_Formal := Extra_Formal (Target_Formal);
2551 Thunk_Formal := Extra_Formal (Thunk_Formal);
2552 end loop;
2554 while Is_Non_Empty_List (Extra_Actuals) loop
2555 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2556 end loop;
2558 Expand_Actuals (Call_Node, Subp);
2559 return;
2560 end;
2561 end if;
2563 Formal := First_Formal (Subp);
2564 Actual := First_Actual (Call_Node);
2565 Param_Count := 1;
2566 while Present (Formal) loop
2568 -- Generate range check if required
2570 if Do_Range_Check (Actual)
2571 and then Ekind (Formal) = E_In_Parameter
2572 then
2573 Generate_Range_Check
2574 (Actual, Etype (Formal), CE_Range_Check_Failed);
2575 end if;
2577 -- Prepare to examine current entry
2579 Prev := Actual;
2580 Prev_Orig := Original_Node (Prev);
2582 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2583 -- to expand it in a further round.
2585 CW_Interface_Formals_Present :=
2586 CW_Interface_Formals_Present
2587 or else
2588 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2589 and then Is_Interface (Etype (Etype (Formal))))
2590 or else
2591 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2592 and then Is_Interface (Directly_Designated_Type
2593 (Etype (Etype (Formal)))));
2595 -- Create possible extra actual for constrained case. Usually, the
2596 -- extra actual is of the form actual'constrained, but since this
2597 -- attribute is only available for unconstrained records, TRUE is
2598 -- expanded if the type of the formal happens to be constrained (for
2599 -- instance when this procedure is inherited from an unconstrained
2600 -- record to a constrained one) or if the actual has no discriminant
2601 -- (its type is constrained). An exception to this is the case of a
2602 -- private type without discriminants. In this case we pass FALSE
2603 -- because the object has underlying discriminants with defaults.
2605 if Present (Extra_Constrained (Formal)) then
2606 if Ekind (Etype (Prev)) in Private_Kind
2607 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2608 then
2609 Add_Extra_Actual
2610 (New_Occurrence_Of (Standard_False, Loc),
2611 Extra_Constrained (Formal));
2613 elsif Is_Constrained (Etype (Formal))
2614 or else not Has_Discriminants (Etype (Prev))
2615 then
2616 Add_Extra_Actual
2617 (New_Occurrence_Of (Standard_True, Loc),
2618 Extra_Constrained (Formal));
2620 -- Do not produce extra actuals for Unchecked_Union parameters.
2621 -- Jump directly to the end of the loop.
2623 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2624 goto Skip_Extra_Actual_Generation;
2626 else
2627 -- If the actual is a type conversion, then the constrained
2628 -- test applies to the actual, not the target type.
2630 declare
2631 Act_Prev : Node_Id;
2633 begin
2634 -- Test for unchecked conversions as well, which can occur
2635 -- as out parameter actuals on calls to stream procedures.
2637 Act_Prev := Prev;
2638 while Nkind_In (Act_Prev, N_Type_Conversion,
2639 N_Unchecked_Type_Conversion)
2640 loop
2641 Act_Prev := Expression (Act_Prev);
2642 end loop;
2644 -- If the expression is a conversion of a dereference, this
2645 -- is internally generated code that manipulates addresses,
2646 -- e.g. when building interface tables. No check should
2647 -- occur in this case, and the discriminated object is not
2648 -- directly a hand.
2650 if not Comes_From_Source (Actual)
2651 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2652 and then Nkind (Act_Prev) = N_Explicit_Dereference
2653 then
2654 Add_Extra_Actual
2655 (New_Occurrence_Of (Standard_False, Loc),
2656 Extra_Constrained (Formal));
2658 else
2659 Add_Extra_Actual
2660 (Make_Attribute_Reference (Sloc (Prev),
2661 Prefix =>
2662 Duplicate_Subexpr_No_Checks
2663 (Act_Prev, Name_Req => True),
2664 Attribute_Name => Name_Constrained),
2665 Extra_Constrained (Formal));
2666 end if;
2667 end;
2668 end if;
2669 end if;
2671 -- Create possible extra actual for accessibility level
2673 if Present (Extra_Accessibility (Formal)) then
2675 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2676 -- attribute, then the original actual may be an aliased object
2677 -- occurring as the prefix in a call using "Object.Operation"
2678 -- notation. In that case we must pass the level of the object,
2679 -- so Prev_Orig is reset to Prev and the attribute will be
2680 -- processed by the code for Access attributes further below.
2682 if Prev_Orig /= Prev
2683 and then Nkind (Prev) = N_Attribute_Reference
2684 and then
2685 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2686 and then Is_Aliased_View (Prev_Orig)
2687 then
2688 Prev_Orig := Prev;
2689 end if;
2691 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals of
2692 -- accessibility levels.
2694 if Is_Thunk (Current_Scope) then
2695 declare
2696 Parm_Ent : Entity_Id;
2698 begin
2699 if Is_Controlling_Actual (Actual) then
2701 -- Find the corresponding actual of the thunk
2703 Parm_Ent := First_Entity (Current_Scope);
2704 for J in 2 .. Param_Count loop
2705 Next_Entity (Parm_Ent);
2706 end loop;
2708 -- Handle unchecked conversion of access types generated
2709 -- in thunks (cf. Expand_Interface_Thunk).
2711 elsif Is_Access_Type (Etype (Actual))
2712 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2713 then
2714 Parm_Ent := Entity (Expression (Actual));
2716 else pragma Assert (Is_Entity_Name (Actual));
2717 Parm_Ent := Entity (Actual);
2718 end if;
2720 Add_Extra_Actual
2721 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2722 Extra_Accessibility (Formal));
2723 end;
2725 elsif Is_Entity_Name (Prev_Orig) then
2727 -- When passing an access parameter, or a renaming of an access
2728 -- parameter, as the actual to another access parameter we need
2729 -- to pass along the actual's own access level parameter. This
2730 -- is done if we are within the scope of the formal access
2731 -- parameter (if this is an inlined body the extra formal is
2732 -- irrelevant).
2734 if (Is_Formal (Entity (Prev_Orig))
2735 or else
2736 (Present (Renamed_Object (Entity (Prev_Orig)))
2737 and then
2738 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2739 and then
2740 Is_Formal
2741 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2742 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2743 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2744 then
2745 declare
2746 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2748 begin
2749 pragma Assert (Present (Parm_Ent));
2751 if Present (Extra_Accessibility (Parm_Ent)) then
2752 Add_Extra_Actual
2753 (New_Occurrence_Of
2754 (Extra_Accessibility (Parm_Ent), Loc),
2755 Extra_Accessibility (Formal));
2757 -- If the actual access parameter does not have an
2758 -- associated extra formal providing its scope level,
2759 -- then treat the actual as having library-level
2760 -- accessibility.
2762 else
2763 Add_Extra_Actual
2764 (Make_Integer_Literal (Loc,
2765 Intval => Scope_Depth (Standard_Standard)),
2766 Extra_Accessibility (Formal));
2767 end if;
2768 end;
2770 -- The actual is a normal access value, so just pass the level
2771 -- of the actual's access type.
2773 else
2774 Add_Extra_Actual
2775 (Dynamic_Accessibility_Level (Prev_Orig),
2776 Extra_Accessibility (Formal));
2777 end if;
2779 -- If the actual is an access discriminant, then pass the level
2780 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2782 elsif Nkind (Prev_Orig) = N_Selected_Component
2783 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2784 E_Discriminant
2785 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2786 E_Anonymous_Access_Type
2787 then
2788 Add_Extra_Actual
2789 (Make_Integer_Literal (Loc,
2790 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2791 Extra_Accessibility (Formal));
2793 -- All other cases
2795 else
2796 case Nkind (Prev_Orig) is
2798 when N_Attribute_Reference =>
2799 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2801 -- For X'Access, pass on the level of the prefix X
2803 when Attribute_Access =>
2805 -- If this is an Access attribute applied to the
2806 -- the current instance object passed to a type
2807 -- initialization procedure, then use the level
2808 -- of the type itself. This is not really correct,
2809 -- as there should be an extra level parameter
2810 -- passed in with _init formals (only in the case
2811 -- where the type is immutably limited), but we
2812 -- don't have an easy way currently to create such
2813 -- an extra formal (init procs aren't ever frozen).
2814 -- For now we just use the level of the type,
2815 -- which may be too shallow, but that works better
2816 -- than passing Object_Access_Level of the type,
2817 -- which can be one level too deep in some cases.
2818 -- ???
2820 if Is_Entity_Name (Prefix (Prev_Orig))
2821 and then Is_Type (Entity (Prefix (Prev_Orig)))
2822 then
2823 Add_Extra_Actual
2824 (Make_Integer_Literal (Loc,
2825 Intval =>
2826 Type_Access_Level
2827 (Entity (Prefix (Prev_Orig)))),
2828 Extra_Accessibility (Formal));
2830 else
2831 Add_Extra_Actual
2832 (Make_Integer_Literal (Loc,
2833 Intval =>
2834 Object_Access_Level
2835 (Prefix (Prev_Orig))),
2836 Extra_Accessibility (Formal));
2837 end if;
2839 -- Treat the unchecked attributes as library-level
2841 when Attribute_Unchecked_Access |
2842 Attribute_Unrestricted_Access =>
2843 Add_Extra_Actual
2844 (Make_Integer_Literal (Loc,
2845 Intval => Scope_Depth (Standard_Standard)),
2846 Extra_Accessibility (Formal));
2848 -- No other cases of attributes returning access
2849 -- values that can be passed to access parameters.
2851 when others =>
2852 raise Program_Error;
2854 end case;
2856 -- For allocators we pass the level of the execution of the
2857 -- called subprogram, which is one greater than the current
2858 -- scope level.
2860 when N_Allocator =>
2861 Add_Extra_Actual
2862 (Make_Integer_Literal (Loc,
2863 Intval => Scope_Depth (Current_Scope) + 1),
2864 Extra_Accessibility (Formal));
2866 -- For most other cases we simply pass the level of the
2867 -- actual's access type. The type is retrieved from
2868 -- Prev rather than Prev_Orig, because in some cases
2869 -- Prev_Orig denotes an original expression that has
2870 -- not been analyzed.
2872 when others =>
2873 Add_Extra_Actual
2874 (Dynamic_Accessibility_Level (Prev),
2875 Extra_Accessibility (Formal));
2876 end case;
2877 end if;
2878 end if;
2880 -- Perform the check of 4.6(49) that prevents a null value from being
2881 -- passed as an actual to an access parameter. Note that the check
2882 -- is elided in the common cases of passing an access attribute or
2883 -- access parameter as an actual. Also, we currently don't enforce
2884 -- this check for expander-generated actuals and when -gnatdj is set.
2886 if Ada_Version >= Ada_2005 then
2888 -- Ada 2005 (AI-231): Check null-excluding access types. Note that
2889 -- the intent of 6.4.1(13) is that null-exclusion checks should
2890 -- not be done for 'out' parameters, even though it refers only
2891 -- to constraint checks, and a null_exclusion is not a constraint.
2892 -- Note that AI05-0196-1 corrects this mistake in the RM.
2894 if Is_Access_Type (Etype (Formal))
2895 and then Can_Never_Be_Null (Etype (Formal))
2896 and then Ekind (Formal) /= E_Out_Parameter
2897 and then Nkind (Prev) /= N_Raise_Constraint_Error
2898 and then (Known_Null (Prev)
2899 or else not Can_Never_Be_Null (Etype (Prev)))
2900 then
2901 Install_Null_Excluding_Check (Prev);
2902 end if;
2904 -- Ada_Version < Ada_2005
2906 else
2907 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2908 or else Access_Checks_Suppressed (Subp)
2909 then
2910 null;
2912 elsif Debug_Flag_J then
2913 null;
2915 elsif not Comes_From_Source (Prev) then
2916 null;
2918 elsif Is_Entity_Name (Prev)
2919 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2920 then
2921 null;
2923 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2924 null;
2926 -- Suppress null checks when passing to access parameters of Java
2927 -- and CIL subprograms. (Should this be done for other foreign
2928 -- conventions as well ???)
2930 elsif Convention (Subp) = Convention_Java
2931 or else Convention (Subp) = Convention_CIL
2932 then
2933 null;
2935 else
2936 Install_Null_Excluding_Check (Prev);
2937 end if;
2938 end if;
2940 -- Perform appropriate validity checks on parameters that
2941 -- are entities.
2943 if Validity_Checks_On then
2944 if (Ekind (Formal) = E_In_Parameter
2945 and then Validity_Check_In_Params)
2946 or else
2947 (Ekind (Formal) = E_In_Out_Parameter
2948 and then Validity_Check_In_Out_Params)
2949 then
2950 -- If the actual is an indexed component of a packed type (or
2951 -- is an indexed or selected component whose prefix recursively
2952 -- meets this condition), it has not been expanded yet. It will
2953 -- be copied in the validity code that follows, and has to be
2954 -- expanded appropriately, so reanalyze it.
2956 -- What we do is just to unset analyzed bits on prefixes till
2957 -- we reach something that does not have a prefix.
2959 declare
2960 Nod : Node_Id;
2962 begin
2963 Nod := Actual;
2964 while Nkind_In (Nod, N_Indexed_Component,
2965 N_Selected_Component)
2966 loop
2967 Set_Analyzed (Nod, False);
2968 Nod := Prefix (Nod);
2969 end loop;
2970 end;
2972 Ensure_Valid (Actual);
2973 end if;
2974 end if;
2976 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2977 -- since this is a left side reference. We only do this for calls
2978 -- from the source program since we assume that compiler generated
2979 -- calls explicitly generate any required checks. We also need it
2980 -- only if we are doing standard validity checks, since clearly it is
2981 -- not needed if validity checks are off, and in subscript validity
2982 -- checking mode, all indexed components are checked with a call
2983 -- directly from Expand_N_Indexed_Component.
2985 if Comes_From_Source (Call_Node)
2986 and then Ekind (Formal) /= E_In_Parameter
2987 and then Validity_Checks_On
2988 and then Validity_Check_Default
2989 and then not Validity_Check_Subscripts
2990 then
2991 Check_Valid_Lvalue_Subscripts (Actual);
2992 end if;
2994 -- Mark any scalar OUT parameter that is a simple variable as no
2995 -- longer known to be valid (unless the type is always valid). This
2996 -- reflects the fact that if an OUT parameter is never set in a
2997 -- procedure, then it can become invalid on the procedure return.
2999 if Ekind (Formal) = E_Out_Parameter
3000 and then Is_Entity_Name (Actual)
3001 and then Ekind (Entity (Actual)) = E_Variable
3002 and then not Is_Known_Valid (Etype (Actual))
3003 then
3004 Set_Is_Known_Valid (Entity (Actual), False);
3005 end if;
3007 -- For an OUT or IN OUT parameter, if the actual is an entity, then
3008 -- clear current values, since they can be clobbered. We are probably
3009 -- doing this in more places than we need to, but better safe than
3010 -- sorry when it comes to retaining bad current values.
3012 if Ekind (Formal) /= E_In_Parameter
3013 and then Is_Entity_Name (Actual)
3014 and then Present (Entity (Actual))
3015 then
3016 declare
3017 Ent : constant Entity_Id := Entity (Actual);
3018 Sav : Node_Id;
3020 begin
3021 -- For an OUT or IN OUT parameter that is an assignable entity,
3022 -- we do not want to clobber the Last_Assignment field, since
3023 -- if it is set, it was precisely because it is indeed an OUT
3024 -- or IN OUT parameter. We do reset the Is_Known_Valid flag
3025 -- since the subprogram could have returned in invalid value.
3027 if Ekind_In (Formal, E_Out_Parameter, E_In_Out_Parameter)
3028 and then Is_Assignable (Ent)
3029 then
3030 Sav := Last_Assignment (Ent);
3031 Kill_Current_Values (Ent);
3032 Set_Last_Assignment (Ent, Sav);
3033 Set_Is_Known_Valid (Ent, False);
3035 -- For all other cases, just kill the current values
3037 else
3038 Kill_Current_Values (Ent);
3039 end if;
3040 end;
3041 end if;
3043 -- If the formal is class wide and the actual is an aggregate, force
3044 -- evaluation so that the back end who does not know about class-wide
3045 -- type, does not generate a temporary of the wrong size.
3047 if not Is_Class_Wide_Type (Etype (Formal)) then
3048 null;
3050 elsif Nkind (Actual) = N_Aggregate
3051 or else (Nkind (Actual) = N_Qualified_Expression
3052 and then Nkind (Expression (Actual)) = N_Aggregate)
3053 then
3054 Force_Evaluation (Actual);
3055 end if;
3057 -- In a remote call, if the formal is of a class-wide type, check
3058 -- that the actual meets the requirements described in E.4(18).
3060 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
3061 Insert_Action (Actual,
3062 Make_Transportable_Check (Loc,
3063 Duplicate_Subexpr_Move_Checks (Actual)));
3064 end if;
3066 -- This label is required when skipping extra actual generation for
3067 -- Unchecked_Union parameters.
3069 <<Skip_Extra_Actual_Generation>>
3071 Param_Count := Param_Count + 1;
3072 Next_Actual (Actual);
3073 Next_Formal (Formal);
3074 end loop;
3076 -- If we are calling an Ada 2012 function which needs to have the
3077 -- "accessibility level determined by the point of call" (AI05-0234)
3078 -- passed in to it, then pass it in.
3080 if Ekind_In (Subp, E_Function, E_Operator, E_Subprogram_Type)
3081 and then
3082 Present (Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)))
3083 then
3084 declare
3085 Ancestor : Node_Id := Parent (Call_Node);
3086 Level : Node_Id := Empty;
3087 Defer : Boolean := False;
3089 begin
3090 -- Unimplemented: if Subp returns an anonymous access type, then
3092 -- a) if the call is the operand of an explict conversion, then
3093 -- the target type of the conversion (a named access type)
3094 -- determines the accessibility level pass in;
3096 -- b) if the call defines an access discriminant of an object
3097 -- (e.g., the discriminant of an object being created by an
3098 -- allocator, or the discriminant of a function result),
3099 -- then the accessibility level to pass in is that of the
3100 -- discriminated object being initialized).
3102 -- ???
3104 while Nkind (Ancestor) = N_Qualified_Expression
3105 loop
3106 Ancestor := Parent (Ancestor);
3107 end loop;
3109 case Nkind (Ancestor) is
3110 when N_Allocator =>
3112 -- At this point, we'd like to assign
3114 -- Level := Dynamic_Accessibility_Level (Ancestor);
3116 -- but Etype of Ancestor may not have been set yet,
3117 -- so that doesn't work.
3119 -- Handle this later in Expand_Allocator_Expression.
3121 Defer := True;
3123 when N_Object_Declaration | N_Object_Renaming_Declaration =>
3124 declare
3125 Def_Id : constant Entity_Id :=
3126 Defining_Identifier (Ancestor);
3128 begin
3129 if Is_Return_Object (Def_Id) then
3130 if Present (Extra_Accessibility_Of_Result
3131 (Return_Applies_To (Scope (Def_Id))))
3132 then
3133 -- Pass along value that was passed in if the
3134 -- routine we are returning from also has an
3135 -- Accessibility_Of_Result formal.
3137 Level :=
3138 New_Occurrence_Of
3139 (Extra_Accessibility_Of_Result
3140 (Return_Applies_To (Scope (Def_Id))), Loc);
3141 end if;
3142 else
3143 Level :=
3144 Make_Integer_Literal (Loc,
3145 Intval => Object_Access_Level (Def_Id));
3146 end if;
3147 end;
3149 when N_Simple_Return_Statement =>
3150 if Present (Extra_Accessibility_Of_Result
3151 (Return_Applies_To
3152 (Return_Statement_Entity (Ancestor))))
3153 then
3154 -- Pass along value that was passed in if the returned
3155 -- routine also has an Accessibility_Of_Result formal.
3157 Level :=
3158 New_Occurrence_Of
3159 (Extra_Accessibility_Of_Result
3160 (Return_Applies_To
3161 (Return_Statement_Entity (Ancestor))), Loc);
3162 end if;
3164 when others =>
3165 null;
3166 end case;
3168 if not Defer then
3169 if not Present (Level) then
3171 -- The "innermost master that evaluates the function call".
3173 -- ??? - Should we use Integer'Last here instead in order
3174 -- to deal with (some of) the problems associated with
3175 -- calls to subps whose enclosing scope is unknown (e.g.,
3176 -- Anon_Access_To_Subp_Param.all)?
3178 Level := Make_Integer_Literal (Loc,
3179 Scope_Depth (Current_Scope) + 1);
3180 end if;
3182 Add_Extra_Actual
3183 (Level,
3184 Extra_Accessibility_Of_Result (Ultimate_Alias (Subp)));
3185 end if;
3186 end;
3187 end if;
3189 -- If we are expanding the RHS of an assignment we need to check if tag
3190 -- propagation is needed. You might expect this processing to be in
3191 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
3192 -- assignment might be transformed to a declaration for an unconstrained
3193 -- value if the expression is classwide.
3195 if Nkind (Call_Node) = N_Function_Call
3196 and then Is_Tag_Indeterminate (Call_Node)
3197 and then Is_Entity_Name (Name (Call_Node))
3198 then
3199 declare
3200 Ass : Node_Id := Empty;
3202 begin
3203 if Nkind (Parent (Call_Node)) = N_Assignment_Statement then
3204 Ass := Parent (Call_Node);
3206 elsif Nkind (Parent (Call_Node)) = N_Qualified_Expression
3207 and then Nkind (Parent (Parent (Call_Node))) =
3208 N_Assignment_Statement
3209 then
3210 Ass := Parent (Parent (Call_Node));
3212 elsif Nkind (Parent (Call_Node)) = N_Explicit_Dereference
3213 and then Nkind (Parent (Parent (Call_Node))) =
3214 N_Assignment_Statement
3215 then
3216 Ass := Parent (Parent (Call_Node));
3217 end if;
3219 if Present (Ass)
3220 and then Is_Class_Wide_Type (Etype (Name (Ass)))
3221 then
3222 if Is_Access_Type (Etype (Call_Node)) then
3223 if Designated_Type (Etype (Call_Node)) /=
3224 Root_Type (Etype (Name (Ass)))
3225 then
3226 Error_Msg_NE
3227 ("tag-indeterminate expression "
3228 & " must have designated type& (RM 5.2 (6))",
3229 Call_Node, Root_Type (Etype (Name (Ass))));
3230 else
3231 Propagate_Tag (Name (Ass), Call_Node);
3232 end if;
3234 elsif Etype (Call_Node) /= Root_Type (Etype (Name (Ass))) then
3235 Error_Msg_NE
3236 ("tag-indeterminate expression must have type&"
3237 & "(RM 5.2 (6))",
3238 Call_Node, Root_Type (Etype (Name (Ass))));
3240 else
3241 Propagate_Tag (Name (Ass), Call_Node);
3242 end if;
3244 -- The call will be rewritten as a dispatching call, and
3245 -- expanded as such.
3247 return;
3248 end if;
3249 end;
3250 end if;
3252 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
3253 -- it to point to the correct secondary virtual table
3255 if Nkind (Call_Node) in N_Subprogram_Call
3256 and then CW_Interface_Formals_Present
3257 then
3258 Expand_Interface_Actuals (Call_Node);
3259 end if;
3261 -- Deals with Dispatch_Call if we still have a call, before expanding
3262 -- extra actuals since this will be done on the re-analysis of the
3263 -- dispatching call. Note that we do not try to shorten the actual list
3264 -- for a dispatching call, it would not make sense to do so. Expansion
3265 -- of dispatching calls is suppressed when VM_Target, because the VM
3266 -- back-ends directly handle the generation of dispatching calls and
3267 -- would have to undo any expansion to an indirect call.
3269 if Nkind (Call_Node) in N_Subprogram_Call
3270 and then Present (Controlling_Argument (Call_Node))
3271 then
3272 declare
3273 Call_Typ : constant Entity_Id := Etype (Call_Node);
3274 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
3275 Eq_Prim_Op : Entity_Id := Empty;
3276 New_Call : Node_Id;
3277 Param : Node_Id;
3278 Prev_Call : Node_Id;
3280 begin
3281 if not Is_Limited_Type (Typ) then
3282 Eq_Prim_Op := Find_Prim_Op (Typ, Name_Op_Eq);
3283 end if;
3285 if Tagged_Type_Expansion then
3286 Expand_Dispatching_Call (Call_Node);
3288 -- The following return is worrisome. Is it really OK to skip
3289 -- all remaining processing in this procedure ???
3291 return;
3293 -- VM targets
3295 else
3296 Apply_Tag_Checks (Call_Node);
3298 -- If this is a dispatching "=", we must first compare the
3299 -- tags so we generate: x.tag = y.tag and then x = y
3301 if Subp = Eq_Prim_Op then
3303 -- Mark the node as analyzed to avoid reanalizing this
3304 -- dispatching call (which would cause a never-ending loop)
3306 Prev_Call := Relocate_Node (Call_Node);
3307 Set_Analyzed (Prev_Call);
3309 Param := First_Actual (Call_Node);
3310 New_Call :=
3311 Make_And_Then (Loc,
3312 Left_Opnd =>
3313 Make_Op_Eq (Loc,
3314 Left_Opnd =>
3315 Make_Selected_Component (Loc,
3316 Prefix => New_Value (Param),
3317 Selector_Name =>
3318 New_Occurrence_Of
3319 (First_Tag_Component (Typ), Loc)),
3321 Right_Opnd =>
3322 Make_Selected_Component (Loc,
3323 Prefix =>
3324 Unchecked_Convert_To (Typ,
3325 New_Value (Next_Actual (Param))),
3326 Selector_Name =>
3327 New_Occurrence_Of
3328 (First_Tag_Component (Typ), Loc))),
3329 Right_Opnd => Prev_Call);
3331 Rewrite (Call_Node, New_Call);
3333 Analyze_And_Resolve
3334 (Call_Node, Call_Typ, Suppress => All_Checks);
3335 end if;
3337 -- Expansion of a dispatching call results in an indirect call,
3338 -- which in turn causes current values to be killed (see
3339 -- Resolve_Call), so on VM targets we do the call here to
3340 -- ensure consistent warnings between VM and non-VM targets.
3342 Kill_Current_Values;
3343 end if;
3345 -- If this is a dispatching "=" then we must update the reference
3346 -- to the call node because we generated:
3347 -- x.tag = y.tag and then x = y
3349 if Subp = Eq_Prim_Op then
3350 Call_Node := Right_Opnd (Call_Node);
3351 end if;
3352 end;
3353 end if;
3355 -- Similarly, expand calls to RCI subprograms on which pragma
3356 -- All_Calls_Remote applies. The rewriting will be reanalyzed
3357 -- later. Do this only when the call comes from source since we
3358 -- do not want such a rewriting to occur in expanded code.
3360 if Is_All_Remote_Call (Call_Node) then
3361 Expand_All_Calls_Remote_Subprogram_Call (Call_Node);
3363 -- Similarly, do not add extra actuals for an entry call whose entity
3364 -- is a protected procedure, or for an internal protected subprogram
3365 -- call, because it will be rewritten as a protected subprogram call
3366 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
3368 elsif Is_Protected_Type (Scope (Subp))
3369 and then (Ekind (Subp) = E_Procedure
3370 or else Ekind (Subp) = E_Function)
3371 then
3372 null;
3374 -- During that loop we gathered the extra actuals (the ones that
3375 -- correspond to Extra_Formals), so now they can be appended.
3377 else
3378 while Is_Non_Empty_List (Extra_Actuals) loop
3379 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
3380 end loop;
3381 end if;
3383 -- At this point we have all the actuals, so this is the point at which
3384 -- the various expansion activities for actuals is carried out.
3386 Expand_Actuals (Call_Node, Subp);
3388 -- Verify that the actuals do not share storage. This check must be done
3389 -- on the caller side rather that inside the subprogram to avoid issues
3390 -- of parameter passing.
3392 if Check_Aliasing_Of_Parameters then
3393 Apply_Parameter_Aliasing_Checks (Call_Node, Subp);
3394 end if;
3396 -- If the subprogram is a renaming, or if it is inherited, replace it in
3397 -- the call with the name of the actual subprogram being called. If this
3398 -- is a dispatching call, the run-time decides what to call. The Alias
3399 -- attribute does not apply to entries.
3401 if Nkind (Call_Node) /= N_Entry_Call_Statement
3402 and then No (Controlling_Argument (Call_Node))
3403 and then Present (Parent_Subp)
3404 and then not Is_Direct_Deep_Call (Subp)
3405 then
3406 if Present (Inherited_From_Formal (Subp)) then
3407 Parent_Subp := Inherited_From_Formal (Subp);
3408 else
3409 Parent_Subp := Ultimate_Alias (Parent_Subp);
3410 end if;
3412 -- The below setting of Entity is suspect, see F109-018 discussion???
3414 Set_Entity (Name (Call_Node), Parent_Subp);
3416 if Is_Abstract_Subprogram (Parent_Subp)
3417 and then not In_Instance
3418 then
3419 Error_Msg_NE
3420 ("cannot call abstract subprogram &!",
3421 Name (Call_Node), Parent_Subp);
3422 end if;
3424 -- Inspect all formals of derived subprogram Subp. Compare parameter
3425 -- types with the parent subprogram and check whether an actual may
3426 -- need a type conversion to the corresponding formal of the parent
3427 -- subprogram.
3429 -- Not clear whether intrinsic subprograms need such conversions. ???
3431 if not Is_Intrinsic_Subprogram (Parent_Subp)
3432 or else Is_Generic_Instance (Parent_Subp)
3433 then
3434 declare
3435 procedure Convert (Act : Node_Id; Typ : Entity_Id);
3436 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
3437 -- and resolve the newly generated construct.
3439 -------------
3440 -- Convert --
3441 -------------
3443 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
3444 begin
3445 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
3446 Analyze (Act);
3447 Resolve (Act, Typ);
3448 end Convert;
3450 -- Local variables
3452 Actual_Typ : Entity_Id;
3453 Formal_Typ : Entity_Id;
3454 Parent_Typ : Entity_Id;
3456 begin
3457 Actual := First_Actual (Call_Node);
3458 Formal := First_Formal (Subp);
3459 Parent_Formal := First_Formal (Parent_Subp);
3460 while Present (Formal) loop
3461 Actual_Typ := Etype (Actual);
3462 Formal_Typ := Etype (Formal);
3463 Parent_Typ := Etype (Parent_Formal);
3465 -- For an IN parameter of a scalar type, the parent formal
3466 -- type and derived formal type differ or the parent formal
3467 -- type and actual type do not match statically.
3469 if Is_Scalar_Type (Formal_Typ)
3470 and then Ekind (Formal) = E_In_Parameter
3471 and then Formal_Typ /= Parent_Typ
3472 and then
3473 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
3474 and then not Raises_Constraint_Error (Actual)
3475 then
3476 Convert (Actual, Parent_Typ);
3477 Enable_Range_Check (Actual);
3479 -- If the actual has been marked as requiring a range
3480 -- check, then generate it here.
3482 if Do_Range_Check (Actual) then
3483 Generate_Range_Check
3484 (Actual, Etype (Formal), CE_Range_Check_Failed);
3485 end if;
3487 -- For access types, the parent formal type and actual type
3488 -- differ.
3490 elsif Is_Access_Type (Formal_Typ)
3491 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
3492 then
3493 if Ekind (Formal) /= E_In_Parameter then
3494 Convert (Actual, Parent_Typ);
3496 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
3497 and then Designated_Type (Parent_Typ) /=
3498 Designated_Type (Actual_Typ)
3499 and then not Is_Controlling_Formal (Formal)
3500 then
3501 -- This unchecked conversion is not necessary unless
3502 -- inlining is enabled, because in that case the type
3503 -- mismatch may become visible in the body about to be
3504 -- inlined.
3506 Rewrite (Actual,
3507 Unchecked_Convert_To (Parent_Typ,
3508 Relocate_Node (Actual)));
3509 Analyze (Actual);
3510 Resolve (Actual, Parent_Typ);
3511 end if;
3513 -- If there is a change of representation, then generate a
3514 -- warning, and do the change of representation.
3516 elsif not Same_Representation (Formal_Typ, Parent_Typ) then
3517 Error_Msg_N
3518 ("??change of representation required", Actual);
3519 Convert (Actual, Parent_Typ);
3521 -- For array and record types, the parent formal type and
3522 -- derived formal type have different sizes or pragma Pack
3523 -- status.
3525 elsif ((Is_Array_Type (Formal_Typ)
3526 and then Is_Array_Type (Parent_Typ))
3527 or else
3528 (Is_Record_Type (Formal_Typ)
3529 and then Is_Record_Type (Parent_Typ)))
3530 and then
3531 (Esize (Formal_Typ) /= Esize (Parent_Typ)
3532 or else Has_Pragma_Pack (Formal_Typ) /=
3533 Has_Pragma_Pack (Parent_Typ))
3534 then
3535 Convert (Actual, Parent_Typ);
3536 end if;
3538 Next_Actual (Actual);
3539 Next_Formal (Formal);
3540 Next_Formal (Parent_Formal);
3541 end loop;
3542 end;
3543 end if;
3545 Orig_Subp := Subp;
3546 Subp := Parent_Subp;
3547 end if;
3549 -- Deal with case where call is an explicit dereference
3551 if Nkind (Name (Call_Node)) = N_Explicit_Dereference then
3553 -- Handle case of access to protected subprogram type
3555 if Is_Access_Protected_Subprogram_Type
3556 (Base_Type (Etype (Prefix (Name (Call_Node)))))
3557 then
3558 -- If this is a call through an access to protected operation, the
3559 -- prefix has the form (object'address, operation'access). Rewrite
3560 -- as a for other protected calls: the object is the 1st parameter
3561 -- of the list of actuals.
3563 declare
3564 Call : Node_Id;
3565 Parm : List_Id;
3566 Nam : Node_Id;
3567 Obj : Node_Id;
3568 Ptr : constant Node_Id := Prefix (Name (Call_Node));
3570 T : constant Entity_Id :=
3571 Equivalent_Type (Base_Type (Etype (Ptr)));
3573 D_T : constant Entity_Id :=
3574 Designated_Type (Base_Type (Etype (Ptr)));
3576 begin
3577 Obj :=
3578 Make_Selected_Component (Loc,
3579 Prefix => Unchecked_Convert_To (T, Ptr),
3580 Selector_Name =>
3581 New_Occurrence_Of (First_Entity (T), Loc));
3583 Nam :=
3584 Make_Selected_Component (Loc,
3585 Prefix => Unchecked_Convert_To (T, Ptr),
3586 Selector_Name =>
3587 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
3589 Nam :=
3590 Make_Explicit_Dereference (Loc,
3591 Prefix => Nam);
3593 if Present (Parameter_Associations (Call_Node)) then
3594 Parm := Parameter_Associations (Call_Node);
3595 else
3596 Parm := New_List;
3597 end if;
3599 Prepend (Obj, Parm);
3601 if Etype (D_T) = Standard_Void_Type then
3602 Call :=
3603 Make_Procedure_Call_Statement (Loc,
3604 Name => Nam,
3605 Parameter_Associations => Parm);
3606 else
3607 Call :=
3608 Make_Function_Call (Loc,
3609 Name => Nam,
3610 Parameter_Associations => Parm);
3611 end if;
3613 Set_First_Named_Actual (Call, First_Named_Actual (Call_Node));
3614 Set_Etype (Call, Etype (D_T));
3616 -- We do not re-analyze the call to avoid infinite recursion.
3617 -- We analyze separately the prefix and the object, and set
3618 -- the checks on the prefix that would otherwise be emitted
3619 -- when resolving a call.
3621 Rewrite (Call_Node, Call);
3622 Analyze (Nam);
3623 Apply_Access_Check (Nam);
3624 Analyze (Obj);
3625 return;
3626 end;
3627 end if;
3628 end if;
3630 -- If this is a call to an intrinsic subprogram, then perform the
3631 -- appropriate expansion to the corresponding tree node and we
3632 -- are all done (since after that the call is gone).
3634 -- In the case where the intrinsic is to be processed by the back end,
3635 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
3636 -- since the idea in this case is to pass the call unchanged. If the
3637 -- intrinsic is an inherited unchecked conversion, and the derived type
3638 -- is the target type of the conversion, we must retain it as the return
3639 -- type of the expression. Otherwise the expansion below, which uses the
3640 -- parent operation, will yield the wrong type.
3642 if Is_Intrinsic_Subprogram (Subp) then
3643 Expand_Intrinsic_Call (Call_Node, Subp);
3645 if Nkind (Call_Node) = N_Unchecked_Type_Conversion
3646 and then Parent_Subp /= Orig_Subp
3647 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
3648 then
3649 Set_Etype (Call_Node, Etype (Orig_Subp));
3650 end if;
3652 return;
3653 end if;
3655 if Ekind_In (Subp, E_Function, E_Procedure) then
3657 -- We perform two simple optimization on calls:
3659 -- a) replace calls to null procedures unconditionally;
3661 -- b) for To_Address, just do an unchecked conversion. Not only is
3662 -- this efficient, but it also avoids order of elaboration problems
3663 -- when address clauses are inlined (address expression elaborated
3664 -- at the wrong point).
3666 -- We perform these optimization regardless of whether we are in the
3667 -- main unit or in a unit in the context of the main unit, to ensure
3668 -- that tree generated is the same in both cases, for CodePeer use.
3670 if Is_RTE (Subp, RE_To_Address) then
3671 Rewrite (Call_Node,
3672 Unchecked_Convert_To
3673 (RTE (RE_Address), Relocate_Node (First_Actual (Call_Node))));
3674 return;
3676 elsif Is_Null_Procedure (Subp) then
3677 Rewrite (Call_Node, Make_Null_Statement (Loc));
3678 return;
3679 end if;
3681 -- Handle inlining. No action needed if the subprogram is not inlined
3683 if not Is_Inlined (Subp) then
3684 null;
3686 -- Handle frontend inlining
3688 elsif not Back_End_Inlining then
3689 Inlined_Subprogram : declare
3690 Bod : Node_Id;
3691 Must_Inline : Boolean := False;
3692 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
3694 begin
3695 -- Verify that the body to inline has already been seen, and
3696 -- that if the body is in the current unit the inlining does
3697 -- not occur earlier. This avoids order-of-elaboration problems
3698 -- in the back end.
3700 -- This should be documented in sinfo/einfo ???
3702 if No (Spec)
3703 or else Nkind (Spec) /= N_Subprogram_Declaration
3704 or else No (Body_To_Inline (Spec))
3705 then
3706 Must_Inline := False;
3708 -- If this an inherited function that returns a private type,
3709 -- do not inline if the full view is an unconstrained array,
3710 -- because such calls cannot be inlined.
3712 elsif Present (Orig_Subp)
3713 and then Is_Array_Type (Etype (Orig_Subp))
3714 and then not Is_Constrained (Etype (Orig_Subp))
3715 then
3716 Must_Inline := False;
3718 elsif In_Unfrozen_Instance (Scope (Subp)) then
3719 Must_Inline := False;
3721 else
3722 Bod := Body_To_Inline (Spec);
3724 if (In_Extended_Main_Code_Unit (Call_Node)
3725 or else In_Extended_Main_Code_Unit (Parent (Call_Node))
3726 or else Has_Pragma_Inline_Always (Subp))
3727 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3728 or else
3729 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3730 then
3731 Must_Inline := True;
3733 -- If we are compiling a package body that is not the main
3734 -- unit, it must be for inlining/instantiation purposes,
3735 -- in which case we inline the call to insure that the same
3736 -- temporaries are generated when compiling the body by
3737 -- itself. Otherwise link errors can occur.
3739 -- If the function being called is itself in the main unit,
3740 -- we cannot inline, because there is a risk of double
3741 -- elaboration and/or circularity: the inlining can make
3742 -- visible a private entity in the body of the main unit,
3743 -- that gigi will see before its sees its proper definition.
3745 elsif not (In_Extended_Main_Code_Unit (Call_Node))
3746 and then In_Package_Body
3747 then
3748 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3749 end if;
3750 end if;
3752 if Must_Inline then
3753 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3755 else
3756 -- Let the back end handle it
3758 Add_Inlined_Body (Subp, Call_Node);
3760 if Front_End_Inlining
3761 and then Nkind (Spec) = N_Subprogram_Declaration
3762 and then (In_Extended_Main_Code_Unit (Call_Node))
3763 and then No (Body_To_Inline (Spec))
3764 and then not Has_Completion (Subp)
3765 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3766 then
3767 Cannot_Inline
3768 ("cannot inline& (body not seen yet)?",
3769 Call_Node, Subp);
3770 end if;
3771 end if;
3772 end Inlined_Subprogram;
3774 -- Back end inlining: let the back end handle it
3776 elsif No (Unit_Declaration_Node (Subp))
3777 or else Nkind (Unit_Declaration_Node (Subp)) /=
3778 N_Subprogram_Declaration
3779 or else No (Body_To_Inline (Unit_Declaration_Node (Subp)))
3780 or else Nkind (Body_To_Inline (Unit_Declaration_Node (Subp))) in
3781 N_Entity
3782 then
3783 Add_Inlined_Body (Subp, Call_Node);
3785 -- Front end expansion of simple functions returning unconstrained
3786 -- types (see Check_And_Split_Unconstrained_Function). Note that the
3787 -- case of a simple renaming (Body_To_Inline in N_Entity above, see
3788 -- also Build_Renamed_Body) cannot be expanded here because this may
3789 -- give rise to order-of-elaboration issues for the types of the
3790 -- parameters of the subprogram, if any.
3792 else
3793 Expand_Inlined_Call (Call_Node, Subp, Orig_Subp);
3794 end if;
3795 end if;
3797 -- Check for protected subprogram. This is either an intra-object call,
3798 -- or a protected function call. Protected procedure calls are rewritten
3799 -- as entry calls and handled accordingly.
3801 -- In Ada 2005, this may be an indirect call to an access parameter that
3802 -- is an access_to_subprogram. In that case the anonymous type has a
3803 -- scope that is a protected operation, but the call is a regular one.
3804 -- In either case do not expand call if subprogram is eliminated.
3806 Scop := Scope (Subp);
3808 if Nkind (Call_Node) /= N_Entry_Call_Statement
3809 and then Is_Protected_Type (Scop)
3810 and then Ekind (Subp) /= E_Subprogram_Type
3811 and then not Is_Eliminated (Subp)
3812 then
3813 -- If the call is an internal one, it is rewritten as a call to the
3814 -- corresponding unprotected subprogram.
3816 Expand_Protected_Subprogram_Call (Call_Node, Subp, Scop);
3817 end if;
3819 -- Functions returning controlled objects need special attention. If
3820 -- the return type is limited, then the context is initialization and
3821 -- different processing applies. If the call is to a protected function,
3822 -- the expansion above will call Expand_Call recursively. Otherwise the
3823 -- function call is transformed into a temporary which obtains the
3824 -- result from the secondary stack.
3826 if Needs_Finalization (Etype (Subp)) then
3827 if not Is_Limited_View (Etype (Subp))
3828 and then
3829 (No (First_Formal (Subp))
3830 or else
3831 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3832 then
3833 Expand_Ctrl_Function_Call (Call_Node);
3835 -- Build-in-place function calls which appear in anonymous contexts
3836 -- need a transient scope to ensure the proper finalization of the
3837 -- intermediate result after its use.
3839 elsif Is_Build_In_Place_Function_Call (Call_Node)
3840 and then
3841 Nkind_In (Parent (Call_Node), N_Attribute_Reference,
3842 N_Function_Call,
3843 N_Indexed_Component,
3844 N_Object_Renaming_Declaration,
3845 N_Procedure_Call_Statement,
3846 N_Selected_Component,
3847 N_Slice)
3848 then
3849 Establish_Transient_Scope (Call_Node, Sec_Stack => True);
3850 end if;
3851 end if;
3852 end Expand_Call;
3854 -------------------------------
3855 -- Expand_Ctrl_Function_Call --
3856 -------------------------------
3858 procedure Expand_Ctrl_Function_Call (N : Node_Id) is
3859 function Is_Element_Reference (N : Node_Id) return Boolean;
3860 -- Determine whether node N denotes a reference to an Ada 2012 container
3861 -- element.
3863 --------------------------
3864 -- Is_Element_Reference --
3865 --------------------------
3867 function Is_Element_Reference (N : Node_Id) return Boolean is
3868 Ref : constant Node_Id := Original_Node (N);
3870 begin
3871 -- Analysis marks an element reference by setting the generalized
3872 -- indexing attribute of an indexed component before the component
3873 -- is rewritten into a function call.
3875 return
3876 Nkind (Ref) = N_Indexed_Component
3877 and then Present (Generalized_Indexing (Ref));
3878 end Is_Element_Reference;
3880 -- Local variables
3882 Is_Elem_Ref : constant Boolean := Is_Element_Reference (N);
3884 -- Start of processing for Expand_Ctrl_Function_Call
3886 begin
3887 -- Optimization, if the returned value (which is on the sec-stack) is
3888 -- returned again, no need to copy/readjust/finalize, we can just pass
3889 -- the value thru (see Expand_N_Simple_Return_Statement), and thus no
3890 -- attachment is needed
3892 if Nkind (Parent (N)) = N_Simple_Return_Statement then
3893 return;
3894 end if;
3896 -- Resolution is now finished, make sure we don't start analysis again
3897 -- because of the duplication.
3899 Set_Analyzed (N);
3901 -- A function which returns a controlled object uses the secondary
3902 -- stack. Rewrite the call into a temporary which obtains the result of
3903 -- the function using 'reference.
3905 Remove_Side_Effects (N);
3907 -- When the temporary function result appears inside a case expression
3908 -- or an if expression, its lifetime must be extended to match that of
3909 -- the context. If not, the function result will be finalized too early
3910 -- and the evaluation of the expression could yield incorrect result. An
3911 -- exception to this rule are references to Ada 2012 container elements.
3912 -- Such references must be finalized at the end of each iteration of the
3913 -- related quantified expression, otherwise the container will remain
3914 -- busy.
3916 if not Is_Elem_Ref
3917 and then Within_Case_Or_If_Expression (N)
3918 and then Nkind (N) = N_Explicit_Dereference
3919 then
3920 Set_Is_Processed_Transient (Entity (Prefix (N)));
3921 end if;
3922 end Expand_Ctrl_Function_Call;
3924 ----------------------------------------
3925 -- Expand_N_Extended_Return_Statement --
3926 ----------------------------------------
3928 -- If there is a Handled_Statement_Sequence, we rewrite this:
3930 -- return Result : T := <expression> do
3931 -- <handled_seq_of_stms>
3932 -- end return;
3934 -- to be:
3936 -- declare
3937 -- Result : T := <expression>;
3938 -- begin
3939 -- <handled_seq_of_stms>
3940 -- return Result;
3941 -- end;
3943 -- Otherwise (no Handled_Statement_Sequence), we rewrite this:
3945 -- return Result : T := <expression>;
3947 -- to be:
3949 -- return <expression>;
3951 -- unless it's build-in-place or there's no <expression>, in which case
3952 -- we generate:
3954 -- declare
3955 -- Result : T := <expression>;
3956 -- begin
3957 -- return Result;
3958 -- end;
3960 -- Note that this case could have been written by the user as an extended
3961 -- return statement, or could have been transformed to this from a simple
3962 -- return statement.
3964 -- That is, we need to have a reified return object if there are statements
3965 -- (which might refer to it) or if we're doing build-in-place (so we can
3966 -- set its address to the final resting place or if there is no expression
3967 -- (in which case default initial values might need to be set).
3969 procedure Expand_N_Extended_Return_Statement (N : Node_Id) is
3970 Loc : constant Source_Ptr := Sloc (N);
3972 Par_Func : constant Entity_Id :=
3973 Return_Applies_To (Return_Statement_Entity (N));
3974 Result_Subt : constant Entity_Id := Etype (Par_Func);
3975 Ret_Obj_Id : constant Entity_Id :=
3976 First_Entity (Return_Statement_Entity (N));
3977 Ret_Obj_Decl : constant Node_Id := Parent (Ret_Obj_Id);
3979 Is_Build_In_Place : constant Boolean :=
3980 Is_Build_In_Place_Function (Par_Func);
3982 Exp : Node_Id;
3983 HSS : Node_Id;
3984 Result : Node_Id;
3985 Return_Stmt : Node_Id;
3986 Stmts : List_Id;
3988 function Build_Heap_Allocator
3989 (Temp_Id : Entity_Id;
3990 Temp_Typ : Entity_Id;
3991 Func_Id : Entity_Id;
3992 Ret_Typ : Entity_Id;
3993 Alloc_Expr : Node_Id) return Node_Id;
3994 -- Create the statements necessary to allocate a return object on the
3995 -- caller's master. The master is available through implicit parameter
3996 -- BIPfinalizationmaster.
3998 -- if BIPfinalizationmaster /= null then
3999 -- declare
4000 -- type Ptr_Typ is access Ret_Typ;
4001 -- for Ptr_Typ'Storage_Pool use
4002 -- Base_Pool (BIPfinalizationmaster.all).all;
4003 -- Local : Ptr_Typ;
4005 -- begin
4006 -- procedure Allocate (...) is
4007 -- begin
4008 -- System.Storage_Pools.Subpools.Allocate_Any (...);
4009 -- end Allocate;
4011 -- Local := <Alloc_Expr>;
4012 -- Temp_Id := Temp_Typ (Local);
4013 -- end;
4014 -- end if;
4016 -- Temp_Id is the temporary which is used to reference the internally
4017 -- created object in all allocation forms. Temp_Typ is the type of the
4018 -- temporary. Func_Id is the enclosing function. Ret_Typ is the return
4019 -- type of Func_Id. Alloc_Expr is the actual allocator.
4021 function Move_Activation_Chain return Node_Id;
4022 -- Construct a call to System.Tasking.Stages.Move_Activation_Chain
4023 -- with parameters:
4024 -- From current activation chain
4025 -- To activation chain passed in by the caller
4026 -- New_Master master passed in by the caller
4028 --------------------------
4029 -- Build_Heap_Allocator --
4030 --------------------------
4032 function Build_Heap_Allocator
4033 (Temp_Id : Entity_Id;
4034 Temp_Typ : Entity_Id;
4035 Func_Id : Entity_Id;
4036 Ret_Typ : Entity_Id;
4037 Alloc_Expr : Node_Id) return Node_Id
4039 begin
4040 pragma Assert (Is_Build_In_Place_Function (Func_Id));
4042 -- Processing for build-in-place object allocation. This is disabled
4043 -- on .NET/JVM because the targets do not support pools.
4045 if VM_Target = No_VM
4046 and then Needs_Finalization (Ret_Typ)
4047 then
4048 declare
4049 Decls : constant List_Id := New_List;
4050 Fin_Mas_Id : constant Entity_Id :=
4051 Build_In_Place_Formal
4052 (Func_Id, BIP_Finalization_Master);
4053 Stmts : constant List_Id := New_List;
4054 Desig_Typ : Entity_Id;
4055 Local_Id : Entity_Id;
4056 Pool_Id : Entity_Id;
4057 Ptr_Typ : Entity_Id;
4059 begin
4060 -- Generate:
4061 -- Pool_Id renames Base_Pool (BIPfinalizationmaster.all).all;
4063 Pool_Id := Make_Temporary (Loc, 'P');
4065 Append_To (Decls,
4066 Make_Object_Renaming_Declaration (Loc,
4067 Defining_Identifier => Pool_Id,
4068 Subtype_Mark =>
4069 New_Occurrence_Of (RTE (RE_Root_Storage_Pool), Loc),
4070 Name =>
4071 Make_Explicit_Dereference (Loc,
4072 Prefix =>
4073 Make_Function_Call (Loc,
4074 Name =>
4075 New_Occurrence_Of (RTE (RE_Base_Pool), Loc),
4076 Parameter_Associations => New_List (
4077 Make_Explicit_Dereference (Loc,
4078 Prefix =>
4079 New_Occurrence_Of (Fin_Mas_Id, Loc)))))));
4081 -- Create an access type which uses the storage pool of the
4082 -- caller's master. This additional type is necessary because
4083 -- the finalization master cannot be associated with the type
4084 -- of the temporary. Otherwise the secondary stack allocation
4085 -- will fail.
4087 Desig_Typ := Ret_Typ;
4089 -- Ensure that the build-in-place machinery uses a fat pointer
4090 -- when allocating an unconstrained array on the heap. In this
4091 -- case the result object type is a constrained array type even
4092 -- though the function type is unconstrained.
4094 if Ekind (Desig_Typ) = E_Array_Subtype then
4095 Desig_Typ := Base_Type (Desig_Typ);
4096 end if;
4098 -- Generate:
4099 -- type Ptr_Typ is access Desig_Typ;
4101 Ptr_Typ := Make_Temporary (Loc, 'P');
4103 Append_To (Decls,
4104 Make_Full_Type_Declaration (Loc,
4105 Defining_Identifier => Ptr_Typ,
4106 Type_Definition =>
4107 Make_Access_To_Object_Definition (Loc,
4108 Subtype_Indication =>
4109 New_Occurrence_Of (Desig_Typ, Loc))));
4111 -- Perform minor decoration in order to set the master and the
4112 -- storage pool attributes.
4114 Set_Ekind (Ptr_Typ, E_Access_Type);
4115 Set_Finalization_Master (Ptr_Typ, Fin_Mas_Id);
4116 Set_Associated_Storage_Pool (Ptr_Typ, Pool_Id);
4118 -- Create the temporary, generate:
4119 -- Local_Id : Ptr_Typ;
4121 Local_Id := Make_Temporary (Loc, 'T');
4123 Append_To (Decls,
4124 Make_Object_Declaration (Loc,
4125 Defining_Identifier => Local_Id,
4126 Object_Definition =>
4127 New_Occurrence_Of (Ptr_Typ, Loc)));
4129 -- Allocate the object, generate:
4130 -- Local_Id := <Alloc_Expr>;
4132 Append_To (Stmts,
4133 Make_Assignment_Statement (Loc,
4134 Name => New_Occurrence_Of (Local_Id, Loc),
4135 Expression => Alloc_Expr));
4137 -- Generate:
4138 -- Temp_Id := Temp_Typ (Local_Id);
4140 Append_To (Stmts,
4141 Make_Assignment_Statement (Loc,
4142 Name => New_Occurrence_Of (Temp_Id, Loc),
4143 Expression =>
4144 Unchecked_Convert_To (Temp_Typ,
4145 New_Occurrence_Of (Local_Id, Loc))));
4147 -- Wrap the allocation in a block. This is further conditioned
4148 -- by checking the caller finalization master at runtime. A
4149 -- null value indicates a non-existent master, most likely due
4150 -- to a Finalize_Storage_Only allocation.
4152 -- Generate:
4153 -- if BIPfinalizationmaster /= null then
4154 -- declare
4155 -- <Decls>
4156 -- begin
4157 -- <Stmts>
4158 -- end;
4159 -- end if;
4161 return
4162 Make_If_Statement (Loc,
4163 Condition =>
4164 Make_Op_Ne (Loc,
4165 Left_Opnd => New_Occurrence_Of (Fin_Mas_Id, Loc),
4166 Right_Opnd => Make_Null (Loc)),
4168 Then_Statements => New_List (
4169 Make_Block_Statement (Loc,
4170 Declarations => Decls,
4171 Handled_Statement_Sequence =>
4172 Make_Handled_Sequence_Of_Statements (Loc,
4173 Statements => Stmts))));
4174 end;
4176 -- For all other cases, generate:
4177 -- Temp_Id := <Alloc_Expr>;
4179 else
4180 return
4181 Make_Assignment_Statement (Loc,
4182 Name => New_Occurrence_Of (Temp_Id, Loc),
4183 Expression => Alloc_Expr);
4184 end if;
4185 end Build_Heap_Allocator;
4187 ---------------------------
4188 -- Move_Activation_Chain --
4189 ---------------------------
4191 function Move_Activation_Chain return Node_Id is
4192 begin
4193 return
4194 Make_Procedure_Call_Statement (Loc,
4195 Name =>
4196 New_Occurrence_Of (RTE (RE_Move_Activation_Chain), Loc),
4198 Parameter_Associations => New_List (
4200 -- Source chain
4202 Make_Attribute_Reference (Loc,
4203 Prefix => Make_Identifier (Loc, Name_uChain),
4204 Attribute_Name => Name_Unrestricted_Access),
4206 -- Destination chain
4208 New_Occurrence_Of
4209 (Build_In_Place_Formal (Par_Func, BIP_Activation_Chain), Loc),
4211 -- New master
4213 New_Occurrence_Of
4214 (Build_In_Place_Formal (Par_Func, BIP_Task_Master), Loc)));
4215 end Move_Activation_Chain;
4217 -- Start of processing for Expand_N_Extended_Return_Statement
4219 begin
4220 -- Given that functionality of interface thunks is simple (just displace
4221 -- the pointer to the object) they are always handled by means of
4222 -- simple return statements.
4224 pragma Assert (not Is_Thunk (Current_Scope));
4226 if Nkind (Ret_Obj_Decl) = N_Object_Declaration then
4227 Exp := Expression (Ret_Obj_Decl);
4228 else
4229 Exp := Empty;
4230 end if;
4232 HSS := Handled_Statement_Sequence (N);
4234 -- If the returned object needs finalization actions, the function must
4235 -- perform the appropriate cleanup should it fail to return. The state
4236 -- of the function itself is tracked through a flag which is coupled
4237 -- with the scope finalizer. There is one flag per each return object
4238 -- in case of multiple returns.
4240 if Is_Build_In_Place
4241 and then Needs_Finalization (Etype (Ret_Obj_Id))
4242 then
4243 declare
4244 Flag_Decl : Node_Id;
4245 Flag_Id : Entity_Id;
4246 Func_Bod : Node_Id;
4248 begin
4249 -- Recover the function body
4251 Func_Bod := Unit_Declaration_Node (Par_Func);
4253 if Nkind (Func_Bod) = N_Subprogram_Declaration then
4254 Func_Bod := Parent (Parent (Corresponding_Body (Func_Bod)));
4255 end if;
4257 -- Create a flag to track the function state
4259 Flag_Id := Make_Temporary (Loc, 'F');
4260 Set_Status_Flag_Or_Transient_Decl (Ret_Obj_Id, Flag_Id);
4262 -- Insert the flag at the beginning of the function declarations,
4263 -- generate:
4264 -- Fnn : Boolean := False;
4266 Flag_Decl :=
4267 Make_Object_Declaration (Loc,
4268 Defining_Identifier => Flag_Id,
4269 Object_Definition =>
4270 New_Occurrence_Of (Standard_Boolean, Loc),
4271 Expression =>
4272 New_Occurrence_Of (Standard_False, Loc));
4274 Prepend_To (Declarations (Func_Bod), Flag_Decl);
4275 Analyze (Flag_Decl);
4276 end;
4277 end if;
4279 -- Build a simple_return_statement that returns the return object when
4280 -- there is a statement sequence, or no expression, or the result will
4281 -- be built in place. Note however that we currently do this for all
4282 -- composite cases, even though nonlimited composite results are not yet
4283 -- built in place (though we plan to do so eventually).
4285 if Present (HSS)
4286 or else Is_Composite_Type (Result_Subt)
4287 or else No (Exp)
4288 then
4289 if No (HSS) then
4290 Stmts := New_List;
4292 -- If the extended return has a handled statement sequence, then wrap
4293 -- it in a block and use the block as the first statement.
4295 else
4296 Stmts := New_List (
4297 Make_Block_Statement (Loc,
4298 Declarations => New_List,
4299 Handled_Statement_Sequence => HSS));
4300 end if;
4302 -- If the result type contains tasks, we call Move_Activation_Chain.
4303 -- Later, the cleanup code will call Complete_Master, which will
4304 -- terminate any unactivated tasks belonging to the return statement
4305 -- master. But Move_Activation_Chain updates their master to be that
4306 -- of the caller, so they will not be terminated unless the return
4307 -- statement completes unsuccessfully due to exception, abort, goto,
4308 -- or exit. As a formality, we test whether the function requires the
4309 -- result to be built in place, though that's necessarily true for
4310 -- the case of result types with task parts.
4312 if Is_Build_In_Place
4313 and then Has_Task (Result_Subt)
4314 then
4315 -- The return expression is an aggregate for a complex type which
4316 -- contains tasks. This particular case is left unexpanded since
4317 -- the regular expansion would insert all temporaries and
4318 -- initialization code in the wrong block.
4320 if Nkind (Exp) = N_Aggregate then
4321 Expand_N_Aggregate (Exp);
4322 end if;
4324 -- Do not move the activation chain if the return object does not
4325 -- contain tasks.
4327 if Has_Task (Etype (Ret_Obj_Id)) then
4328 Append_To (Stmts, Move_Activation_Chain);
4329 end if;
4330 end if;
4332 -- Update the state of the function right before the object is
4333 -- returned.
4335 if Is_Build_In_Place
4336 and then Needs_Finalization (Etype (Ret_Obj_Id))
4337 then
4338 declare
4339 Flag_Id : constant Entity_Id :=
4340 Status_Flag_Or_Transient_Decl (Ret_Obj_Id);
4342 begin
4343 -- Generate:
4344 -- Fnn := True;
4346 Append_To (Stmts,
4347 Make_Assignment_Statement (Loc,
4348 Name => New_Occurrence_Of (Flag_Id, Loc),
4349 Expression => New_Occurrence_Of (Standard_True, Loc)));
4350 end;
4351 end if;
4353 -- Build a simple_return_statement that returns the return object
4355 Return_Stmt :=
4356 Make_Simple_Return_Statement (Loc,
4357 Expression => New_Occurrence_Of (Ret_Obj_Id, Loc));
4358 Append_To (Stmts, Return_Stmt);
4360 HSS := Make_Handled_Sequence_Of_Statements (Loc, Stmts);
4361 end if;
4363 -- Case where we build a return statement block
4365 if Present (HSS) then
4366 Result :=
4367 Make_Block_Statement (Loc,
4368 Declarations => Return_Object_Declarations (N),
4369 Handled_Statement_Sequence => HSS);
4371 -- We set the entity of the new block statement to be that of the
4372 -- return statement. This is necessary so that various fields, such
4373 -- as Finalization_Chain_Entity carry over from the return statement
4374 -- to the block. Note that this block is unusual, in that its entity
4375 -- is an E_Return_Statement rather than an E_Block.
4377 Set_Identifier
4378 (Result, New_Occurrence_Of (Return_Statement_Entity (N), Loc));
4380 -- If the object decl was already rewritten as a renaming, then we
4381 -- don't want to do the object allocation and transformation of of
4382 -- the return object declaration to a renaming. This case occurs
4383 -- when the return object is initialized by a call to another
4384 -- build-in-place function, and that function is responsible for
4385 -- the allocation of the return object.
4387 if Is_Build_In_Place
4388 and then Nkind (Ret_Obj_Decl) = N_Object_Renaming_Declaration
4389 then
4390 pragma Assert
4391 (Nkind (Original_Node (Ret_Obj_Decl)) = N_Object_Declaration
4392 and then Is_Build_In_Place_Function_Call
4393 (Expression (Original_Node (Ret_Obj_Decl))));
4395 -- Return the build-in-place result by reference
4397 Set_By_Ref (Return_Stmt);
4399 elsif Is_Build_In_Place then
4401 -- Locate the implicit access parameter associated with the
4402 -- caller-supplied return object and convert the return
4403 -- statement's return object declaration to a renaming of a
4404 -- dereference of the access parameter. If the return object's
4405 -- declaration includes an expression that has not already been
4406 -- expanded as separate assignments, then add an assignment
4407 -- statement to ensure the return object gets initialized.
4409 -- declare
4410 -- Result : T [:= <expression>];
4411 -- begin
4412 -- ...
4414 -- is converted to
4416 -- declare
4417 -- Result : T renames FuncRA.all;
4418 -- [Result := <expression;]
4419 -- begin
4420 -- ...
4422 declare
4423 Return_Obj_Id : constant Entity_Id :=
4424 Defining_Identifier (Ret_Obj_Decl);
4425 Return_Obj_Typ : constant Entity_Id := Etype (Return_Obj_Id);
4426 Return_Obj_Expr : constant Node_Id :=
4427 Expression (Ret_Obj_Decl);
4428 Constr_Result : constant Boolean :=
4429 Is_Constrained (Result_Subt);
4430 Obj_Alloc_Formal : Entity_Id;
4431 Object_Access : Entity_Id;
4432 Obj_Acc_Deref : Node_Id;
4433 Init_Assignment : Node_Id := Empty;
4435 begin
4436 -- Build-in-place results must be returned by reference
4438 Set_By_Ref (Return_Stmt);
4440 -- Retrieve the implicit access parameter passed by the caller
4442 Object_Access :=
4443 Build_In_Place_Formal (Par_Func, BIP_Object_Access);
4445 -- If the return object's declaration includes an expression
4446 -- and the declaration isn't marked as No_Initialization, then
4447 -- we need to generate an assignment to the object and insert
4448 -- it after the declaration before rewriting it as a renaming
4449 -- (otherwise we'll lose the initialization). The case where
4450 -- the result type is an interface (or class-wide interface)
4451 -- is also excluded because the context of the function call
4452 -- must be unconstrained, so the initialization will always
4453 -- be done as part of an allocator evaluation (storage pool
4454 -- or secondary stack), never to a constrained target object
4455 -- passed in by the caller. Besides the assignment being
4456 -- unneeded in this case, it avoids problems with trying to
4457 -- generate a dispatching assignment when the return expression
4458 -- is a nonlimited descendant of a limited interface (the
4459 -- interface has no assignment operation).
4461 if Present (Return_Obj_Expr)
4462 and then not No_Initialization (Ret_Obj_Decl)
4463 and then not Is_Interface (Return_Obj_Typ)
4464 then
4465 Init_Assignment :=
4466 Make_Assignment_Statement (Loc,
4467 Name => New_Occurrence_Of (Return_Obj_Id, Loc),
4468 Expression => Relocate_Node (Return_Obj_Expr));
4470 Set_Etype (Name (Init_Assignment), Etype (Return_Obj_Id));
4471 Set_Assignment_OK (Name (Init_Assignment));
4472 Set_No_Ctrl_Actions (Init_Assignment);
4474 Set_Parent (Name (Init_Assignment), Init_Assignment);
4475 Set_Parent (Expression (Init_Assignment), Init_Assignment);
4477 Set_Expression (Ret_Obj_Decl, Empty);
4479 if Is_Class_Wide_Type (Etype (Return_Obj_Id))
4480 and then not Is_Class_Wide_Type
4481 (Etype (Expression (Init_Assignment)))
4482 then
4483 Rewrite (Expression (Init_Assignment),
4484 Make_Type_Conversion (Loc,
4485 Subtype_Mark =>
4486 New_Occurrence_Of (Etype (Return_Obj_Id), Loc),
4487 Expression =>
4488 Relocate_Node (Expression (Init_Assignment))));
4489 end if;
4491 -- In the case of functions where the calling context can
4492 -- determine the form of allocation needed, initialization
4493 -- is done with each part of the if statement that handles
4494 -- the different forms of allocation (this is true for
4495 -- unconstrained and tagged result subtypes).
4497 if Constr_Result
4498 and then not Is_Tagged_Type (Underlying_Type (Result_Subt))
4499 then
4500 Insert_After (Ret_Obj_Decl, Init_Assignment);
4501 end if;
4502 end if;
4504 -- When the function's subtype is unconstrained, a run-time
4505 -- test is needed to determine the form of allocation to use
4506 -- for the return object. The function has an implicit formal
4507 -- parameter indicating this. If the BIP_Alloc_Form formal has
4508 -- the value one, then the caller has passed access to an
4509 -- existing object for use as the return object. If the value
4510 -- is two, then the return object must be allocated on the
4511 -- secondary stack. Otherwise, the object must be allocated in
4512 -- a storage pool (currently only supported for the global
4513 -- heap, user-defined storage pools TBD ???). We generate an
4514 -- if statement to test the implicit allocation formal and
4515 -- initialize a local access value appropriately, creating
4516 -- allocators in the secondary stack and global heap cases.
4517 -- The special formal also exists and must be tested when the
4518 -- function has a tagged result, even when the result subtype
4519 -- is constrained, because in general such functions can be
4520 -- called in dispatching contexts and must be handled similarly
4521 -- to functions with a class-wide result.
4523 if not Constr_Result
4524 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
4525 then
4526 Obj_Alloc_Formal :=
4527 Build_In_Place_Formal (Par_Func, BIP_Alloc_Form);
4529 declare
4530 Pool_Id : constant Entity_Id :=
4531 Make_Temporary (Loc, 'P');
4532 Alloc_Obj_Id : Entity_Id;
4533 Alloc_Obj_Decl : Node_Id;
4534 Alloc_If_Stmt : Node_Id;
4535 Heap_Allocator : Node_Id;
4536 Pool_Decl : Node_Id;
4537 Pool_Allocator : Node_Id;
4538 Ptr_Type_Decl : Node_Id;
4539 Ref_Type : Entity_Id;
4540 SS_Allocator : Node_Id;
4542 begin
4543 -- Reuse the itype created for the function's implicit
4544 -- access formal. This avoids the need to create a new
4545 -- access type here, plus it allows assigning the access
4546 -- formal directly without applying a conversion.
4548 -- Ref_Type := Etype (Object_Access);
4550 -- Create an access type designating the function's
4551 -- result subtype.
4553 Ref_Type := Make_Temporary (Loc, 'A');
4555 Ptr_Type_Decl :=
4556 Make_Full_Type_Declaration (Loc,
4557 Defining_Identifier => Ref_Type,
4558 Type_Definition =>
4559 Make_Access_To_Object_Definition (Loc,
4560 All_Present => True,
4561 Subtype_Indication =>
4562 New_Occurrence_Of (Return_Obj_Typ, Loc)));
4564 Insert_Before (Ret_Obj_Decl, Ptr_Type_Decl);
4566 -- Create an access object that will be initialized to an
4567 -- access value denoting the return object, either coming
4568 -- from an implicit access value passed in by the caller
4569 -- or from the result of an allocator.
4571 Alloc_Obj_Id := Make_Temporary (Loc, 'R');
4572 Set_Etype (Alloc_Obj_Id, Ref_Type);
4574 Alloc_Obj_Decl :=
4575 Make_Object_Declaration (Loc,
4576 Defining_Identifier => Alloc_Obj_Id,
4577 Object_Definition =>
4578 New_Occurrence_Of (Ref_Type, Loc));
4580 Insert_Before (Ret_Obj_Decl, Alloc_Obj_Decl);
4582 -- Create allocators for both the secondary stack and
4583 -- global heap. If there's an initialization expression,
4584 -- then create these as initialized allocators.
4586 if Present (Return_Obj_Expr)
4587 and then not No_Initialization (Ret_Obj_Decl)
4588 then
4589 -- Always use the type of the expression for the
4590 -- qualified expression, rather than the result type.
4591 -- In general we cannot always use the result type
4592 -- for the allocator, because the expression might be
4593 -- of a specific type, such as in the case of an
4594 -- aggregate or even a nonlimited object when the
4595 -- result type is a limited class-wide interface type.
4597 Heap_Allocator :=
4598 Make_Allocator (Loc,
4599 Expression =>
4600 Make_Qualified_Expression (Loc,
4601 Subtype_Mark =>
4602 New_Occurrence_Of
4603 (Etype (Return_Obj_Expr), Loc),
4604 Expression =>
4605 New_Copy_Tree (Return_Obj_Expr)));
4607 else
4608 -- If the function returns a class-wide type we cannot
4609 -- use the return type for the allocator. Instead we
4610 -- use the type of the expression, which must be an
4611 -- aggregate of a definite type.
4613 if Is_Class_Wide_Type (Return_Obj_Typ) then
4614 Heap_Allocator :=
4615 Make_Allocator (Loc,
4616 Expression =>
4617 New_Occurrence_Of
4618 (Etype (Return_Obj_Expr), Loc));
4619 else
4620 Heap_Allocator :=
4621 Make_Allocator (Loc,
4622 Expression =>
4623 New_Occurrence_Of (Return_Obj_Typ, Loc));
4624 end if;
4626 -- If the object requires default initialization then
4627 -- that will happen later following the elaboration of
4628 -- the object renaming. If we don't turn it off here
4629 -- then the object will be default initialized twice.
4631 Set_No_Initialization (Heap_Allocator);
4632 end if;
4634 -- The Pool_Allocator is just like the Heap_Allocator,
4635 -- except we set Storage_Pool and Procedure_To_Call so
4636 -- it will use the user-defined storage pool.
4638 Pool_Allocator := New_Copy_Tree (Heap_Allocator);
4640 -- Do not generate the renaming of the build-in-place
4641 -- pool parameter on .NET/JVM/ZFP because the parameter
4642 -- is not created in the first place.
4644 if VM_Target = No_VM
4645 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
4646 then
4647 Pool_Decl :=
4648 Make_Object_Renaming_Declaration (Loc,
4649 Defining_Identifier => Pool_Id,
4650 Subtype_Mark =>
4651 New_Occurrence_Of
4652 (RTE (RE_Root_Storage_Pool), Loc),
4653 Name =>
4654 Make_Explicit_Dereference (Loc,
4655 New_Occurrence_Of
4656 (Build_In_Place_Formal
4657 (Par_Func, BIP_Storage_Pool), Loc)));
4658 Set_Storage_Pool (Pool_Allocator, Pool_Id);
4659 Set_Procedure_To_Call
4660 (Pool_Allocator, RTE (RE_Allocate_Any));
4661 else
4662 Pool_Decl := Make_Null_Statement (Loc);
4663 end if;
4665 -- If the No_Allocators restriction is active, then only
4666 -- an allocator for secondary stack allocation is needed.
4667 -- It's OK for such allocators to have Comes_From_Source
4668 -- set to False, because gigi knows not to flag them as
4669 -- being a violation of No_Implicit_Heap_Allocations.
4671 if Restriction_Active (No_Allocators) then
4672 SS_Allocator := Heap_Allocator;
4673 Heap_Allocator := Make_Null (Loc);
4674 Pool_Allocator := Make_Null (Loc);
4676 -- Otherwise the heap and pool allocators may be needed,
4677 -- so we make another allocator for secondary stack
4678 -- allocation.
4680 else
4681 SS_Allocator := New_Copy_Tree (Heap_Allocator);
4683 -- The heap and pool allocators are marked as
4684 -- Comes_From_Source since they correspond to an
4685 -- explicit user-written allocator (that is, it will
4686 -- only be executed on behalf of callers that call the
4687 -- function as initialization for such an allocator).
4688 -- Prevents errors when No_Implicit_Heap_Allocations
4689 -- is in force.
4691 Set_Comes_From_Source (Heap_Allocator, True);
4692 Set_Comes_From_Source (Pool_Allocator, True);
4693 end if;
4695 -- The allocator is returned on the secondary stack. We
4696 -- don't do this on VM targets, since the SS is not used.
4698 if VM_Target = No_VM then
4699 Set_Storage_Pool (SS_Allocator, RTE (RE_SS_Pool));
4700 Set_Procedure_To_Call
4701 (SS_Allocator, RTE (RE_SS_Allocate));
4703 -- The allocator is returned on the secondary stack,
4704 -- so indicate that the function return, as well as
4705 -- the block that encloses the allocator, must not
4706 -- release it. The flags must be set now because
4707 -- the decision to use the secondary stack is done
4708 -- very late in the course of expanding the return
4709 -- statement, past the point where these flags are
4710 -- normally set.
4712 Set_Sec_Stack_Needed_For_Return (Par_Func);
4713 Set_Sec_Stack_Needed_For_Return
4714 (Return_Statement_Entity (N));
4715 Set_Uses_Sec_Stack (Par_Func);
4716 Set_Uses_Sec_Stack (Return_Statement_Entity (N));
4717 end if;
4719 -- Create an if statement to test the BIP_Alloc_Form
4720 -- formal and initialize the access object to either the
4721 -- BIP_Object_Access formal (BIP_Alloc_Form =
4722 -- Caller_Allocation), the result of allocating the
4723 -- object in the secondary stack (BIP_Alloc_Form =
4724 -- Secondary_Stack), or else an allocator to create the
4725 -- return object in the heap or user-defined pool
4726 -- (BIP_Alloc_Form = Global_Heap or User_Storage_Pool).
4728 -- ??? An unchecked type conversion must be made in the
4729 -- case of assigning the access object formal to the
4730 -- local access object, because a normal conversion would
4731 -- be illegal in some cases (such as converting access-
4732 -- to-unconstrained to access-to-constrained), but the
4733 -- the unchecked conversion will presumably fail to work
4734 -- right in just such cases. It's not clear at all how to
4735 -- handle this. ???
4737 Alloc_If_Stmt :=
4738 Make_If_Statement (Loc,
4739 Condition =>
4740 Make_Op_Eq (Loc,
4741 Left_Opnd =>
4742 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4743 Right_Opnd =>
4744 Make_Integer_Literal (Loc,
4745 UI_From_Int (BIP_Allocation_Form'Pos
4746 (Caller_Allocation)))),
4748 Then_Statements => New_List (
4749 Make_Assignment_Statement (Loc,
4750 Name =>
4751 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4752 Expression =>
4753 Make_Unchecked_Type_Conversion (Loc,
4754 Subtype_Mark =>
4755 New_Occurrence_Of (Ref_Type, Loc),
4756 Expression =>
4757 New_Occurrence_Of (Object_Access, Loc)))),
4759 Elsif_Parts => New_List (
4760 Make_Elsif_Part (Loc,
4761 Condition =>
4762 Make_Op_Eq (Loc,
4763 Left_Opnd =>
4764 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4765 Right_Opnd =>
4766 Make_Integer_Literal (Loc,
4767 UI_From_Int (BIP_Allocation_Form'Pos
4768 (Secondary_Stack)))),
4770 Then_Statements => New_List (
4771 Make_Assignment_Statement (Loc,
4772 Name =>
4773 New_Occurrence_Of (Alloc_Obj_Id, Loc),
4774 Expression => SS_Allocator))),
4776 Make_Elsif_Part (Loc,
4777 Condition =>
4778 Make_Op_Eq (Loc,
4779 Left_Opnd =>
4780 New_Occurrence_Of (Obj_Alloc_Formal, Loc),
4781 Right_Opnd =>
4782 Make_Integer_Literal (Loc,
4783 UI_From_Int (BIP_Allocation_Form'Pos
4784 (Global_Heap)))),
4786 Then_Statements => New_List (
4787 Build_Heap_Allocator
4788 (Temp_Id => Alloc_Obj_Id,
4789 Temp_Typ => Ref_Type,
4790 Func_Id => Par_Func,
4791 Ret_Typ => Return_Obj_Typ,
4792 Alloc_Expr => Heap_Allocator)))),
4794 Else_Statements => New_List (
4795 Pool_Decl,
4796 Build_Heap_Allocator
4797 (Temp_Id => Alloc_Obj_Id,
4798 Temp_Typ => Ref_Type,
4799 Func_Id => Par_Func,
4800 Ret_Typ => Return_Obj_Typ,
4801 Alloc_Expr => Pool_Allocator)));
4803 -- If a separate initialization assignment was created
4804 -- earlier, append that following the assignment of the
4805 -- implicit access formal to the access object, to ensure
4806 -- that the return object is initialized in that case. In
4807 -- this situation, the target of the assignment must be
4808 -- rewritten to denote a dereference of the access to the
4809 -- return object passed in by the caller.
4811 if Present (Init_Assignment) then
4812 Rewrite (Name (Init_Assignment),
4813 Make_Explicit_Dereference (Loc,
4814 Prefix => New_Occurrence_Of (Alloc_Obj_Id, Loc)));
4816 Set_Etype
4817 (Name (Init_Assignment), Etype (Return_Obj_Id));
4819 Append_To
4820 (Then_Statements (Alloc_If_Stmt), Init_Assignment);
4821 end if;
4823 Insert_Before (Ret_Obj_Decl, Alloc_If_Stmt);
4825 -- Remember the local access object for use in the
4826 -- dereference of the renaming created below.
4828 Object_Access := Alloc_Obj_Id;
4829 end;
4830 end if;
4832 -- Replace the return object declaration with a renaming of a
4833 -- dereference of the access value designating the return
4834 -- object.
4836 Obj_Acc_Deref :=
4837 Make_Explicit_Dereference (Loc,
4838 Prefix => New_Occurrence_Of (Object_Access, Loc));
4840 Rewrite (Ret_Obj_Decl,
4841 Make_Object_Renaming_Declaration (Loc,
4842 Defining_Identifier => Return_Obj_Id,
4843 Access_Definition => Empty,
4844 Subtype_Mark =>
4845 New_Occurrence_Of (Return_Obj_Typ, Loc),
4846 Name => Obj_Acc_Deref));
4848 Set_Renamed_Object (Return_Obj_Id, Obj_Acc_Deref);
4849 end;
4850 end if;
4852 -- Case where we do not build a block
4854 else
4855 -- We're about to drop Return_Object_Declarations on the floor, so
4856 -- we need to insert it, in case it got expanded into useful code.
4857 -- Remove side effects from expression, which may be duplicated in
4858 -- subsequent checks (see Expand_Simple_Function_Return).
4860 Insert_List_Before (N, Return_Object_Declarations (N));
4861 Remove_Side_Effects (Exp);
4863 -- Build simple_return_statement that returns the expression directly
4865 Return_Stmt := Make_Simple_Return_Statement (Loc, Expression => Exp);
4866 Result := Return_Stmt;
4867 end if;
4869 -- Set the flag to prevent infinite recursion
4871 Set_Comes_From_Extended_Return_Statement (Return_Stmt);
4873 Rewrite (N, Result);
4874 Analyze (N);
4875 end Expand_N_Extended_Return_Statement;
4877 ----------------------------
4878 -- Expand_N_Function_Call --
4879 ----------------------------
4881 procedure Expand_N_Function_Call (N : Node_Id) is
4882 begin
4883 Expand_Call (N);
4884 end Expand_N_Function_Call;
4886 ---------------------------------------
4887 -- Expand_N_Procedure_Call_Statement --
4888 ---------------------------------------
4890 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4891 begin
4892 Expand_Call (N);
4893 end Expand_N_Procedure_Call_Statement;
4895 --------------------------------------
4896 -- Expand_N_Simple_Return_Statement --
4897 --------------------------------------
4899 procedure Expand_N_Simple_Return_Statement (N : Node_Id) is
4900 begin
4901 -- Defend against previous errors (i.e. the return statement calls a
4902 -- function that is not available in configurable runtime).
4904 if Present (Expression (N))
4905 and then Nkind (Expression (N)) = N_Empty
4906 then
4907 Check_Error_Detected;
4908 return;
4909 end if;
4911 -- Distinguish the function and non-function cases:
4913 case Ekind (Return_Applies_To (Return_Statement_Entity (N))) is
4915 when E_Function |
4916 E_Generic_Function =>
4917 Expand_Simple_Function_Return (N);
4919 when E_Procedure |
4920 E_Generic_Procedure |
4921 E_Entry |
4922 E_Entry_Family |
4923 E_Return_Statement =>
4924 Expand_Non_Function_Return (N);
4926 when others =>
4927 raise Program_Error;
4928 end case;
4930 exception
4931 when RE_Not_Available =>
4932 return;
4933 end Expand_N_Simple_Return_Statement;
4935 ------------------------------
4936 -- Expand_N_Subprogram_Body --
4937 ------------------------------
4939 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4940 -- by the back-end.
4942 -- Add dummy push/pop label nodes at start and end to clear any local
4943 -- exception indications if local-exception-to-goto optimization is active.
4945 -- Add return statement if last statement in body is not a return statement
4946 -- (this makes things easier on Gigi which does not want to have to handle
4947 -- a missing return).
4949 -- Add call to Activate_Tasks if body is a task activator
4951 -- Deal with possible detection of infinite recursion
4953 -- Eliminate body completely if convention stubbed
4955 -- Encode entity names within body, since we will not need to reference
4956 -- these entities any longer in the front end.
4958 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4960 -- Reset Pure indication if any parameter has root type System.Address
4961 -- or has any parameters of limited types, where limited means that the
4962 -- run-time view is limited (i.e. the full type is limited).
4964 -- Wrap thread body
4966 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4967 Loc : constant Source_Ptr := Sloc (N);
4968 H : constant Node_Id := Handled_Statement_Sequence (N);
4969 Body_Id : Entity_Id;
4970 Except_H : Node_Id;
4971 L : List_Id;
4972 Spec_Id : Entity_Id;
4974 procedure Add_Return (S : List_Id);
4975 -- Append a return statement to the statement sequence S if the last
4976 -- statement is not already a return or a goto statement. Note that
4977 -- the latter test is not critical, it does not matter if we add a few
4978 -- extra returns, since they get eliminated anyway later on.
4980 ----------------
4981 -- Add_Return --
4982 ----------------
4984 procedure Add_Return (S : List_Id) is
4985 Last_Stmt : Node_Id;
4986 Loc : Source_Ptr;
4987 Stmt : Node_Id;
4989 begin
4990 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4991 -- not relevant in this context since they are not executable.
4993 Last_Stmt := Last (S);
4994 while Nkind (Last_Stmt) in N_Pop_xxx_Label loop
4995 Prev (Last_Stmt);
4996 end loop;
4998 -- Now insert return unless last statement is a transfer
5000 if not Is_Transfer (Last_Stmt) then
5002 -- The source location for the return is the end label of the
5003 -- procedure if present. Otherwise use the sloc of the last
5004 -- statement in the list. If the list comes from a generated
5005 -- exception handler and we are not debugging generated code,
5006 -- all the statements within the handler are made invisible
5007 -- to the debugger.
5009 if Nkind (Parent (S)) = N_Exception_Handler
5010 and then not Comes_From_Source (Parent (S))
5011 then
5012 Loc := Sloc (Last_Stmt);
5013 elsif Present (End_Label (H)) then
5014 Loc := Sloc (End_Label (H));
5015 else
5016 Loc := Sloc (Last_Stmt);
5017 end if;
5019 -- Append return statement, and set analyzed manually. We can't
5020 -- call Analyze on this return since the scope is wrong.
5022 -- Note: it almost works to push the scope and then do the Analyze
5023 -- call, but something goes wrong in some weird cases and it is
5024 -- not worth worrying about ???
5026 Stmt := Make_Simple_Return_Statement (Loc);
5028 -- The return statement is handled properly, and the call to the
5029 -- postcondition, inserted below, does not require information
5030 -- from the body either. However, that call is analyzed in the
5031 -- enclosing scope, and an elaboration check might improperly be
5032 -- added to it. A guard in Sem_Elab is needed to prevent that
5033 -- spurious check, see Check_Elab_Call.
5035 Append_To (S, Stmt);
5036 Set_Analyzed (Stmt);
5038 -- Call the _Postconditions procedure if the related subprogram
5039 -- has contract assertions that need to be verified on exit.
5041 if Ekind (Spec_Id) = E_Procedure
5042 and then Present (Postconditions_Proc (Spec_Id))
5043 then
5044 Insert_Action (Stmt,
5045 Make_Procedure_Call_Statement (Loc,
5046 Name =>
5047 New_Occurrence_Of (Postconditions_Proc (Spec_Id), Loc)));
5048 end if;
5049 end if;
5050 end Add_Return;
5052 -- Start of processing for Expand_N_Subprogram_Body
5054 begin
5055 -- Set L to either the list of declarations if present, or to the list
5056 -- of statements if no declarations are present. This is used to insert
5057 -- new stuff at the start.
5059 if Is_Non_Empty_List (Declarations (N)) then
5060 L := Declarations (N);
5061 else
5062 L := Statements (H);
5063 end if;
5065 -- If local-exception-to-goto optimization active, insert dummy push
5066 -- statements at start, and dummy pop statements at end, but inhibit
5067 -- this if we have No_Exception_Handlers, since they are useless and
5068 -- intefere with analysis, e.g. by codepeer.
5070 if (Debug_Flag_Dot_G
5071 or else Restriction_Active (No_Exception_Propagation))
5072 and then not Restriction_Active (No_Exception_Handlers)
5073 and then not CodePeer_Mode
5074 and then Is_Non_Empty_List (L)
5075 then
5076 declare
5077 FS : constant Node_Id := First (L);
5078 FL : constant Source_Ptr := Sloc (FS);
5079 LS : Node_Id;
5080 LL : Source_Ptr;
5082 begin
5083 -- LS points to either last statement, if statements are present
5084 -- or to the last declaration if there are no statements present.
5085 -- It is the node after which the pop's are generated.
5087 if Is_Non_Empty_List (Statements (H)) then
5088 LS := Last (Statements (H));
5089 else
5090 LS := Last (L);
5091 end if;
5093 LL := Sloc (LS);
5095 Insert_List_Before_And_Analyze (FS, New_List (
5096 Make_Push_Constraint_Error_Label (FL),
5097 Make_Push_Program_Error_Label (FL),
5098 Make_Push_Storage_Error_Label (FL)));
5100 Insert_List_After_And_Analyze (LS, New_List (
5101 Make_Pop_Constraint_Error_Label (LL),
5102 Make_Pop_Program_Error_Label (LL),
5103 Make_Pop_Storage_Error_Label (LL)));
5104 end;
5105 end if;
5107 -- Find entity for subprogram
5109 Body_Id := Defining_Entity (N);
5111 if Present (Corresponding_Spec (N)) then
5112 Spec_Id := Corresponding_Spec (N);
5113 else
5114 Spec_Id := Body_Id;
5115 end if;
5117 -- Need poll on entry to subprogram if polling enabled. We only do this
5118 -- for non-empty subprograms, since it does not seem necessary to poll
5119 -- for a dummy null subprogram.
5121 if Is_Non_Empty_List (L) then
5123 -- Do not add a polling call if the subprogram is to be inlined by
5124 -- the back-end, to avoid repeated calls with multiple inlinings.
5126 if Is_Inlined (Spec_Id)
5127 and then Front_End_Inlining
5128 and then Optimization_Level > 1
5129 then
5130 null;
5131 else
5132 Generate_Poll_Call (First (L));
5133 end if;
5134 end if;
5136 -- If this is a Pure function which has any parameters whose root type
5137 -- is System.Address, reset the Pure indication, since it will likely
5138 -- cause incorrect code to be generated as the parameter is probably
5139 -- a pointer, and the fact that the same pointer is passed does not mean
5140 -- that the same value is being referenced.
5142 -- Note that if the programmer gave an explicit Pure_Function pragma,
5143 -- then we believe the programmer, and leave the subprogram Pure.
5145 -- This code should probably be at the freeze point, so that it happens
5146 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
5147 -- semantic tree has Is_Pure set properly ???
5149 if Is_Pure (Spec_Id)
5150 and then Is_Subprogram (Spec_Id)
5151 and then not Has_Pragma_Pure_Function (Spec_Id)
5152 then
5153 declare
5154 F : Entity_Id;
5156 begin
5157 F := First_Formal (Spec_Id);
5158 while Present (F) loop
5159 if Is_Descendent_Of_Address (Etype (F))
5161 -- Note that this test is being made in the body of the
5162 -- subprogram, not the spec, so we are testing the full
5163 -- type for being limited here, as required.
5165 or else Is_Limited_Type (Etype (F))
5166 then
5167 Set_Is_Pure (Spec_Id, False);
5169 if Spec_Id /= Body_Id then
5170 Set_Is_Pure (Body_Id, False);
5171 end if;
5173 exit;
5174 end if;
5176 Next_Formal (F);
5177 end loop;
5178 end;
5179 end if;
5181 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
5183 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
5184 declare
5185 F : Entity_Id;
5186 A : Node_Id;
5188 begin
5189 -- Loop through formals
5191 F := First_Formal (Spec_Id);
5192 while Present (F) loop
5193 if Is_Scalar_Type (Etype (F))
5194 and then Ekind (F) = E_Out_Parameter
5195 then
5196 Check_Restriction (No_Default_Initialization, F);
5198 -- Insert the initialization. We turn off validity checks
5199 -- for this assignment, since we do not want any check on
5200 -- the initial value itself (which may well be invalid).
5201 -- Predicate checks are disabled as well (RM 6.4.1 (13/3))
5203 A :=
5204 Make_Assignment_Statement (Loc,
5205 Name => New_Occurrence_Of (F, Loc),
5206 Expression => Get_Simple_Init_Val (Etype (F), N));
5207 Set_Suppress_Assignment_Checks (A);
5209 Insert_Before_And_Analyze (First (L),
5210 A, Suppress => Validity_Check);
5211 end if;
5213 Next_Formal (F);
5214 end loop;
5215 end;
5216 end if;
5218 -- Clear out statement list for stubbed procedure
5220 if Present (Corresponding_Spec (N)) then
5221 Set_Elaboration_Flag (N, Spec_Id);
5223 if Convention (Spec_Id) = Convention_Stubbed
5224 or else Is_Eliminated (Spec_Id)
5225 then
5226 Set_Declarations (N, Empty_List);
5227 Set_Handled_Statement_Sequence (N,
5228 Make_Handled_Sequence_Of_Statements (Loc,
5229 Statements => New_List (Make_Null_Statement (Loc))));
5230 return;
5231 end if;
5232 end if;
5234 -- Create a set of discriminals for the next protected subprogram body
5236 if Is_List_Member (N)
5237 and then Present (Parent (List_Containing (N)))
5238 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5239 and then Present (Next_Protected_Operation (N))
5240 then
5241 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
5242 end if;
5244 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
5245 -- subprograms with no specs are not frozen.
5247 declare
5248 Typ : constant Entity_Id := Etype (Spec_Id);
5249 Utyp : constant Entity_Id := Underlying_Type (Typ);
5251 begin
5252 if not Acts_As_Spec (N)
5253 and then Nkind (Parent (Parent (Spec_Id))) /=
5254 N_Subprogram_Body_Stub
5255 then
5256 null;
5258 elsif Is_Limited_View (Typ) then
5259 Set_Returns_By_Ref (Spec_Id);
5261 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5262 Set_Returns_By_Ref (Spec_Id);
5263 end if;
5264 end;
5266 -- For a procedure, we add a return for all possible syntactic ends of
5267 -- the subprogram.
5269 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
5270 Add_Return (Statements (H));
5272 if Present (Exception_Handlers (H)) then
5273 Except_H := First_Non_Pragma (Exception_Handlers (H));
5274 while Present (Except_H) loop
5275 Add_Return (Statements (Except_H));
5276 Next_Non_Pragma (Except_H);
5277 end loop;
5278 end if;
5280 -- For a function, we must deal with the case where there is at least
5281 -- one missing return. What we do is to wrap the entire body of the
5282 -- function in a block:
5284 -- begin
5285 -- ...
5286 -- end;
5288 -- becomes
5290 -- begin
5291 -- begin
5292 -- ...
5293 -- end;
5295 -- raise Program_Error;
5296 -- end;
5298 -- This approach is necessary because the raise must be signalled to the
5299 -- caller, not handled by any local handler (RM 6.4(11)).
5301 -- Note: we do not need to analyze the constructed sequence here, since
5302 -- it has no handler, and an attempt to analyze the handled statement
5303 -- sequence twice is risky in various ways (e.g. the issue of expanding
5304 -- cleanup actions twice).
5306 elsif Has_Missing_Return (Spec_Id) then
5307 declare
5308 Hloc : constant Source_Ptr := Sloc (H);
5309 Blok : constant Node_Id :=
5310 Make_Block_Statement (Hloc,
5311 Handled_Statement_Sequence => H);
5312 Rais : constant Node_Id :=
5313 Make_Raise_Program_Error (Hloc,
5314 Reason => PE_Missing_Return);
5316 begin
5317 Set_Handled_Statement_Sequence (N,
5318 Make_Handled_Sequence_Of_Statements (Hloc,
5319 Statements => New_List (Blok, Rais)));
5321 Push_Scope (Spec_Id);
5322 Analyze (Blok);
5323 Analyze (Rais);
5324 Pop_Scope;
5325 end;
5326 end if;
5328 -- If subprogram contains a parameterless recursive call, then we may
5329 -- have an infinite recursion, so see if we can generate code to check
5330 -- for this possibility if storage checks are not suppressed.
5332 if Ekind (Spec_Id) = E_Procedure
5333 and then Has_Recursive_Call (Spec_Id)
5334 and then not Storage_Checks_Suppressed (Spec_Id)
5335 then
5336 Detect_Infinite_Recursion (N, Spec_Id);
5337 end if;
5339 -- Set to encode entity names in package body before gigi is called
5341 Qualify_Entity_Names (N);
5342 end Expand_N_Subprogram_Body;
5344 -----------------------------------
5345 -- Expand_N_Subprogram_Body_Stub --
5346 -----------------------------------
5348 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
5349 begin
5350 if Present (Corresponding_Body (N)) then
5351 Expand_N_Subprogram_Body (
5352 Unit_Declaration_Node (Corresponding_Body (N)));
5353 end if;
5354 end Expand_N_Subprogram_Body_Stub;
5356 -------------------------------------
5357 -- Expand_N_Subprogram_Declaration --
5358 -------------------------------------
5360 -- If the declaration appears within a protected body, it is a private
5361 -- operation of the protected type. We must create the corresponding
5362 -- protected subprogram an associated formals. For a normal protected
5363 -- operation, this is done when expanding the protected type declaration.
5365 -- If the declaration is for a null procedure, emit null body
5367 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
5368 Loc : constant Source_Ptr := Sloc (N);
5369 Subp : constant Entity_Id := Defining_Entity (N);
5370 Scop : constant Entity_Id := Scope (Subp);
5371 Prot_Decl : Node_Id;
5372 Prot_Bod : Node_Id;
5373 Prot_Id : Entity_Id;
5375 begin
5376 -- In SPARK, subprogram declarations are only allowed in package
5377 -- specifications.
5379 if Nkind (Parent (N)) /= N_Package_Specification then
5380 if Nkind (Parent (N)) = N_Compilation_Unit then
5381 Check_SPARK_05_Restriction
5382 ("subprogram declaration is not a library item", N);
5384 elsif Present (Next (N))
5385 and then Nkind (Next (N)) = N_Pragma
5386 and then Get_Pragma_Id (Pragma_Name (Next (N))) = Pragma_Import
5387 then
5388 -- In SPARK, subprogram declarations are also permitted in
5389 -- declarative parts when immediately followed by a corresponding
5390 -- pragma Import. We only check here that there is some pragma
5391 -- Import.
5393 null;
5394 else
5395 Check_SPARK_05_Restriction
5396 ("subprogram declaration is not allowed here", N);
5397 end if;
5398 end if;
5400 -- Deal with case of protected subprogram. Do not generate protected
5401 -- operation if operation is flagged as eliminated.
5403 if Is_List_Member (N)
5404 and then Present (Parent (List_Containing (N)))
5405 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
5406 and then Is_Protected_Type (Scop)
5407 then
5408 if No (Protected_Body_Subprogram (Subp))
5409 and then not Is_Eliminated (Subp)
5410 then
5411 Prot_Decl :=
5412 Make_Subprogram_Declaration (Loc,
5413 Specification =>
5414 Build_Protected_Sub_Specification
5415 (N, Scop, Unprotected_Mode));
5417 -- The protected subprogram is declared outside of the protected
5418 -- body. Given that the body has frozen all entities so far, we
5419 -- analyze the subprogram and perform freezing actions explicitly.
5420 -- including the generation of an explicit freeze node, to ensure
5421 -- that gigi has the proper order of elaboration.
5422 -- If the body is a subunit, the insertion point is before the
5423 -- stub in the parent.
5425 Prot_Bod := Parent (List_Containing (N));
5427 if Nkind (Parent (Prot_Bod)) = N_Subunit then
5428 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
5429 end if;
5431 Insert_Before (Prot_Bod, Prot_Decl);
5432 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
5433 Set_Has_Delayed_Freeze (Prot_Id);
5435 Push_Scope (Scope (Scop));
5436 Analyze (Prot_Decl);
5437 Freeze_Before (N, Prot_Id);
5438 Set_Protected_Body_Subprogram (Subp, Prot_Id);
5440 -- Create protected operation as well. Even though the operation
5441 -- is only accessible within the body, it is possible to make it
5442 -- available outside of the protected object by using 'Access to
5443 -- provide a callback, so build protected version in all cases.
5445 Prot_Decl :=
5446 Make_Subprogram_Declaration (Loc,
5447 Specification =>
5448 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
5449 Insert_Before (Prot_Bod, Prot_Decl);
5450 Analyze (Prot_Decl);
5452 Pop_Scope;
5453 end if;
5455 -- Ada 2005 (AI-348): Generate body for a null procedure. In most
5456 -- cases this is superfluous because calls to it will be automatically
5457 -- inlined, but we definitely need the body if preconditions for the
5458 -- procedure are present.
5460 elsif Nkind (Specification (N)) = N_Procedure_Specification
5461 and then Null_Present (Specification (N))
5462 then
5463 declare
5464 Bod : constant Node_Id := Body_To_Inline (N);
5466 begin
5467 Set_Has_Completion (Subp, False);
5468 Append_Freeze_Action (Subp, Bod);
5470 -- The body now contains raise statements, so calls to it will
5471 -- not be inlined.
5473 Set_Is_Inlined (Subp, False);
5474 end;
5475 end if;
5476 end Expand_N_Subprogram_Declaration;
5478 --------------------------------
5479 -- Expand_Non_Function_Return --
5480 --------------------------------
5482 procedure Expand_Non_Function_Return (N : Node_Id) is
5483 pragma Assert (No (Expression (N)));
5485 Loc : constant Source_Ptr := Sloc (N);
5486 Scope_Id : Entity_Id := Return_Applies_To (Return_Statement_Entity (N));
5487 Kind : constant Entity_Kind := Ekind (Scope_Id);
5488 Call : Node_Id;
5489 Acc_Stat : Node_Id;
5490 Goto_Stat : Node_Id;
5491 Lab_Node : Node_Id;
5493 begin
5494 -- Call the _Postconditions procedure if the related subprogram has
5495 -- contract assertions that need to be verified on exit.
5497 if Ekind_In (Scope_Id, E_Entry, E_Entry_Family, E_Procedure)
5498 and then Present (Postconditions_Proc (Scope_Id))
5499 then
5500 Insert_Action (N,
5501 Make_Procedure_Call_Statement (Loc,
5502 Name => New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc)));
5503 end if;
5505 -- If it is a return from a procedure do no extra steps
5507 if Kind = E_Procedure or else Kind = E_Generic_Procedure then
5508 return;
5510 -- If it is a nested return within an extended one, replace it with a
5511 -- return of the previously declared return object.
5513 elsif Kind = E_Return_Statement then
5514 Rewrite (N,
5515 Make_Simple_Return_Statement (Loc,
5516 Expression =>
5517 New_Occurrence_Of (First_Entity (Scope_Id), Loc)));
5518 Set_Comes_From_Extended_Return_Statement (N);
5519 Set_Return_Statement_Entity (N, Scope_Id);
5520 Expand_Simple_Function_Return (N);
5521 return;
5522 end if;
5524 pragma Assert (Is_Entry (Scope_Id));
5526 -- Look at the enclosing block to see whether the return is from an
5527 -- accept statement or an entry body.
5529 for J in reverse 0 .. Scope_Stack.Last loop
5530 Scope_Id := Scope_Stack.Table (J).Entity;
5531 exit when Is_Concurrent_Type (Scope_Id);
5532 end loop;
5534 -- If it is a return from accept statement it is expanded as call to
5535 -- RTS Complete_Rendezvous and a goto to the end of the accept body.
5537 -- (cf : Expand_N_Accept_Statement, Expand_N_Selective_Accept,
5538 -- Expand_N_Accept_Alternative in exp_ch9.adb)
5540 if Is_Task_Type (Scope_Id) then
5542 Call :=
5543 Make_Procedure_Call_Statement (Loc,
5544 Name => New_Occurrence_Of (RTE (RE_Complete_Rendezvous), Loc));
5545 Insert_Before (N, Call);
5546 -- why not insert actions here???
5547 Analyze (Call);
5549 Acc_Stat := Parent (N);
5550 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5551 Acc_Stat := Parent (Acc_Stat);
5552 end loop;
5554 Lab_Node := Last (Statements
5555 (Handled_Statement_Sequence (Acc_Stat)));
5557 Goto_Stat := Make_Goto_Statement (Loc,
5558 Name => New_Occurrence_Of
5559 (Entity (Identifier (Lab_Node)), Loc));
5561 Set_Analyzed (Goto_Stat);
5563 Rewrite (N, Goto_Stat);
5564 Analyze (N);
5566 -- If it is a return from an entry body, put a Complete_Entry_Body call
5567 -- in front of the return.
5569 elsif Is_Protected_Type (Scope_Id) then
5570 Call :=
5571 Make_Procedure_Call_Statement (Loc,
5572 Name =>
5573 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
5574 Parameter_Associations => New_List (
5575 Make_Attribute_Reference (Loc,
5576 Prefix =>
5577 New_Occurrence_Of
5578 (Find_Protection_Object (Current_Scope), Loc),
5579 Attribute_Name => Name_Unchecked_Access)));
5581 Insert_Before (N, Call);
5582 Analyze (Call);
5583 end if;
5584 end Expand_Non_Function_Return;
5586 ---------------------------------------
5587 -- Expand_Protected_Object_Reference --
5588 ---------------------------------------
5590 function Expand_Protected_Object_Reference
5591 (N : Node_Id;
5592 Scop : Entity_Id) return Node_Id
5594 Loc : constant Source_Ptr := Sloc (N);
5595 Corr : Entity_Id;
5596 Rec : Node_Id;
5597 Param : Entity_Id;
5598 Proc : Entity_Id;
5600 begin
5601 Rec := Make_Identifier (Loc, Name_uObject);
5602 Set_Etype (Rec, Corresponding_Record_Type (Scop));
5604 -- Find enclosing protected operation, and retrieve its first parameter,
5605 -- which denotes the enclosing protected object. If the enclosing
5606 -- operation is an entry, we are immediately within the protected body,
5607 -- and we can retrieve the object from the service entries procedure. A
5608 -- barrier function has the same signature as an entry. A barrier
5609 -- function is compiled within the protected object, but unlike
5610 -- protected operations its never needs locks, so that its protected
5611 -- body subprogram points to itself.
5613 Proc := Current_Scope;
5614 while Present (Proc)
5615 and then Scope (Proc) /= Scop
5616 loop
5617 Proc := Scope (Proc);
5618 end loop;
5620 Corr := Protected_Body_Subprogram (Proc);
5622 if No (Corr) then
5624 -- Previous error left expansion incomplete.
5625 -- Nothing to do on this call.
5627 return Empty;
5628 end if;
5630 Param :=
5631 Defining_Identifier
5632 (First (Parameter_Specifications (Parent (Corr))));
5634 if Is_Subprogram (Proc) and then Proc /= Corr then
5636 -- Protected function or procedure
5638 Set_Entity (Rec, Param);
5640 -- Rec is a reference to an entity which will not be in scope when
5641 -- the call is reanalyzed, and needs no further analysis.
5643 Set_Analyzed (Rec);
5645 else
5646 -- Entry or barrier function for entry body. The first parameter of
5647 -- the entry body procedure is pointer to the object. We create a
5648 -- local variable of the proper type, duplicating what is done to
5649 -- define _object later on.
5651 declare
5652 Decls : List_Id;
5653 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
5655 begin
5656 Decls := New_List (
5657 Make_Full_Type_Declaration (Loc,
5658 Defining_Identifier => Obj_Ptr,
5659 Type_Definition =>
5660 Make_Access_To_Object_Definition (Loc,
5661 Subtype_Indication =>
5662 New_Occurrence_Of
5663 (Corresponding_Record_Type (Scop), Loc))));
5665 Insert_Actions (N, Decls);
5666 Freeze_Before (N, Obj_Ptr);
5668 Rec :=
5669 Make_Explicit_Dereference (Loc,
5670 Prefix =>
5671 Unchecked_Convert_To (Obj_Ptr,
5672 New_Occurrence_Of (Param, Loc)));
5674 -- Analyze new actual. Other actuals in calls are already analyzed
5675 -- and the list of actuals is not reanalyzed after rewriting.
5677 Set_Parent (Rec, N);
5678 Analyze (Rec);
5679 end;
5680 end if;
5682 return Rec;
5683 end Expand_Protected_Object_Reference;
5685 --------------------------------------
5686 -- Expand_Protected_Subprogram_Call --
5687 --------------------------------------
5689 procedure Expand_Protected_Subprogram_Call
5690 (N : Node_Id;
5691 Subp : Entity_Id;
5692 Scop : Entity_Id)
5694 Rec : Node_Id;
5696 procedure Freeze_Called_Function;
5697 -- If it is a function call it can appear in elaboration code and
5698 -- the called entity must be frozen before the call. This must be
5699 -- done before the call is expanded, as the expansion may rewrite it
5700 -- to something other than a call (e.g. a temporary initialized in a
5701 -- transient block).
5703 ----------------------------
5704 -- Freeze_Called_Function --
5705 ----------------------------
5707 procedure Freeze_Called_Function is
5708 begin
5709 if Ekind (Subp) = E_Function then
5710 Freeze_Expression (Name (N));
5711 end if;
5712 end Freeze_Called_Function;
5714 -- Start of processing for Expand_Protected_Subprogram_Call
5716 begin
5717 -- If the protected object is not an enclosing scope, this is an inter-
5718 -- object function call. Inter-object procedure calls are expanded by
5719 -- Exp_Ch9.Build_Simple_Entry_Call. The call is intra-object only if the
5720 -- subprogram being called is in the protected body being compiled, and
5721 -- if the protected object in the call is statically the enclosing type.
5722 -- The object may be an component of some other data structure, in which
5723 -- case this must be handled as an inter-object call.
5725 if not In_Open_Scopes (Scop)
5726 or else not Is_Entity_Name (Name (N))
5727 then
5728 if Nkind (Name (N)) = N_Selected_Component then
5729 Rec := Prefix (Name (N));
5731 else
5732 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
5733 Rec := Prefix (Prefix (Name (N)));
5734 end if;
5736 Freeze_Called_Function;
5737 Build_Protected_Subprogram_Call (N,
5738 Name => New_Occurrence_Of (Subp, Sloc (N)),
5739 Rec => Convert_Concurrent (Rec, Etype (Rec)),
5740 External => True);
5742 else
5743 Rec := Expand_Protected_Object_Reference (N, Scop);
5745 if No (Rec) then
5746 return;
5747 end if;
5749 Freeze_Called_Function;
5750 Build_Protected_Subprogram_Call (N,
5751 Name => Name (N),
5752 Rec => Rec,
5753 External => False);
5755 end if;
5757 -- Analyze and resolve the new call. The actuals have already been
5758 -- resolved, but expansion of a function call will add extra actuals
5759 -- if needed. Analysis of a procedure call already includes resolution.
5761 Analyze (N);
5763 if Ekind (Subp) = E_Function then
5764 Resolve (N, Etype (Subp));
5765 end if;
5766 end Expand_Protected_Subprogram_Call;
5768 --------------------------------------------
5769 -- Has_Unconstrained_Access_Discriminants --
5770 --------------------------------------------
5772 function Has_Unconstrained_Access_Discriminants
5773 (Subtyp : Entity_Id) return Boolean
5775 Discr : Entity_Id;
5777 begin
5778 if Has_Discriminants (Subtyp)
5779 and then not Is_Constrained (Subtyp)
5780 then
5781 Discr := First_Discriminant (Subtyp);
5782 while Present (Discr) loop
5783 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
5784 return True;
5785 end if;
5787 Next_Discriminant (Discr);
5788 end loop;
5789 end if;
5791 return False;
5792 end Has_Unconstrained_Access_Discriminants;
5794 -----------------------------------
5795 -- Expand_Simple_Function_Return --
5796 -----------------------------------
5798 -- The "simple" comes from the syntax rule simple_return_statement. The
5799 -- semantics are not at all simple.
5801 procedure Expand_Simple_Function_Return (N : Node_Id) is
5802 Loc : constant Source_Ptr := Sloc (N);
5804 Scope_Id : constant Entity_Id :=
5805 Return_Applies_To (Return_Statement_Entity (N));
5806 -- The function we are returning from
5808 R_Type : constant Entity_Id := Etype (Scope_Id);
5809 -- The result type of the function
5811 Utyp : constant Entity_Id := Underlying_Type (R_Type);
5813 Exp : constant Node_Id := Expression (N);
5814 pragma Assert (Present (Exp));
5816 Exptyp : constant Entity_Id := Etype (Exp);
5817 -- The type of the expression (not necessarily the same as R_Type)
5819 Subtype_Ind : Node_Id;
5820 -- If the result type of the function is class-wide and the expression
5821 -- has a specific type, then we use the expression's type as the type of
5822 -- the return object. In cases where the expression is an aggregate that
5823 -- is built in place, this avoids the need for an expensive conversion
5824 -- of the return object to the specific type on assignments to the
5825 -- individual components.
5827 begin
5828 if Is_Class_Wide_Type (R_Type)
5829 and then not Is_Class_Wide_Type (Etype (Exp))
5830 then
5831 Subtype_Ind := New_Occurrence_Of (Etype (Exp), Loc);
5832 else
5833 Subtype_Ind := New_Occurrence_Of (R_Type, Loc);
5834 end if;
5836 -- For the case of a simple return that does not come from an extended
5837 -- return, in the case of Ada 2005 where we are returning a limited
5838 -- type, we rewrite "return <expression>;" to be:
5840 -- return _anon_ : <return_subtype> := <expression>
5842 -- The expansion produced by Expand_N_Extended_Return_Statement will
5843 -- contain simple return statements (for example, a block containing
5844 -- simple return of the return object), which brings us back here with
5845 -- Comes_From_Extended_Return_Statement set. The reason for the barrier
5846 -- checking for a simple return that does not come from an extended
5847 -- return is to avoid this infinite recursion.
5849 -- The reason for this design is that for Ada 2005 limited returns, we
5850 -- need to reify the return object, so we can build it "in place", and
5851 -- we need a block statement to hang finalization and tasking stuff.
5853 -- ??? In order to avoid disruption, we avoid translating to extended
5854 -- return except in the cases where we really need to (Ada 2005 for
5855 -- inherently limited). We might prefer to do this translation in all
5856 -- cases (except perhaps for the case of Ada 95 inherently limited),
5857 -- in order to fully exercise the Expand_N_Extended_Return_Statement
5858 -- code. This would also allow us to do the build-in-place optimization
5859 -- for efficiency even in cases where it is semantically not required.
5861 -- As before, we check the type of the return expression rather than the
5862 -- return type of the function, because the latter may be a limited
5863 -- class-wide interface type, which is not a limited type, even though
5864 -- the type of the expression may be.
5866 if not Comes_From_Extended_Return_Statement (N)
5867 and then Is_Limited_View (Etype (Expression (N)))
5868 and then Ada_Version >= Ada_2005
5869 and then not Debug_Flag_Dot_L
5871 -- The functionality of interface thunks is simple and it is always
5872 -- handled by means of simple return statements. This leaves their
5873 -- expansion simple and clean.
5875 and then not Is_Thunk (Current_Scope)
5876 then
5877 declare
5878 Return_Object_Entity : constant Entity_Id :=
5879 Make_Temporary (Loc, 'R', Exp);
5881 Obj_Decl : constant Node_Id :=
5882 Make_Object_Declaration (Loc,
5883 Defining_Identifier => Return_Object_Entity,
5884 Object_Definition => Subtype_Ind,
5885 Expression => Exp);
5887 Ext : constant Node_Id :=
5888 Make_Extended_Return_Statement (Loc,
5889 Return_Object_Declarations => New_List (Obj_Decl));
5890 -- Do not perform this high-level optimization if the result type
5891 -- is an interface because the "this" pointer must be displaced.
5893 begin
5894 Rewrite (N, Ext);
5895 Analyze (N);
5896 return;
5897 end;
5898 end if;
5900 -- Here we have a simple return statement that is part of the expansion
5901 -- of an extended return statement (either written by the user, or
5902 -- generated by the above code).
5904 -- Always normalize C/Fortran boolean result. This is not always needed,
5905 -- but it seems a good idea to minimize the passing around of non-
5906 -- normalized values, and in any case this handles the processing of
5907 -- barrier functions for protected types, which turn the condition into
5908 -- a return statement.
5910 if Is_Boolean_Type (Exptyp)
5911 and then Nonzero_Is_True (Exptyp)
5912 then
5913 Adjust_Condition (Exp);
5914 Adjust_Result_Type (Exp, Exptyp);
5915 end if;
5917 -- Do validity check if enabled for returns
5919 if Validity_Checks_On
5920 and then Validity_Check_Returns
5921 then
5922 Ensure_Valid (Exp);
5923 end if;
5925 -- Check the result expression of a scalar function against the subtype
5926 -- of the function by inserting a conversion. This conversion must
5927 -- eventually be performed for other classes of types, but for now it's
5928 -- only done for scalars.
5929 -- ???
5931 if Is_Scalar_Type (Exptyp) then
5932 Rewrite (Exp, Convert_To (R_Type, Exp));
5934 -- The expression is resolved to ensure that the conversion gets
5935 -- expanded to generate a possible constraint check.
5937 Analyze_And_Resolve (Exp, R_Type);
5938 end if;
5940 -- Deal with returning variable length objects and controlled types
5942 -- Nothing to do if we are returning by reference, or this is not a
5943 -- type that requires special processing (indicated by the fact that
5944 -- it requires a cleanup scope for the secondary stack case).
5946 if Is_Limited_View (Exptyp)
5947 or else Is_Limited_Interface (Exptyp)
5948 then
5949 null;
5951 -- No copy needed for thunks returning interface type objects since
5952 -- the object is returned by reference and the maximum functionality
5953 -- required is just to displace the pointer.
5955 elsif Is_Thunk (Current_Scope) and then Is_Interface (Exptyp) then
5956 null;
5958 -- If the call is within a thunk and the type is a limited view, the
5959 -- backend will eventually see the non-limited view of the type.
5961 elsif Is_Thunk (Current_Scope) and then Is_Incomplete_Type (Exptyp) then
5962 return;
5964 elsif not Requires_Transient_Scope (R_Type) then
5966 -- Mutable records with no variable length components are not
5967 -- returned on the sec-stack, so we need to make sure that the
5968 -- backend will only copy back the size of the actual value, and not
5969 -- the maximum size. We create an actual subtype for this purpose.
5971 declare
5972 Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exptyp));
5973 Decl : Node_Id;
5974 Ent : Entity_Id;
5975 begin
5976 if Has_Discriminants (Ubt)
5977 and then not Is_Constrained (Ubt)
5978 and then not Has_Unchecked_Union (Ubt)
5979 then
5980 Decl := Build_Actual_Subtype (Ubt, Exp);
5981 Ent := Defining_Identifier (Decl);
5982 Insert_Action (Exp, Decl);
5983 Rewrite (Exp, Unchecked_Convert_To (Ent, Exp));
5984 Analyze_And_Resolve (Exp);
5985 end if;
5986 end;
5988 -- Here if secondary stack is used
5990 else
5991 -- Prevent the reclamation of the secondary stack by all enclosing
5992 -- blocks and loops as well as the related function, otherwise the
5993 -- result will be reclaimed too early or even clobbered. Due to a
5994 -- possible mix of internally generated blocks, source blocks and
5995 -- loops, the scope stack may not be contiguous as all labels are
5996 -- inserted at the top level within the related function. Instead,
5997 -- perform a parent-based traversal and mark all appropriate
5998 -- constructs.
6000 declare
6001 P : Node_Id;
6003 begin
6004 P := N;
6005 while Present (P) loop
6007 -- Mark the label of a source or internally generated block or
6008 -- loop.
6010 if Nkind_In (P, N_Block_Statement, N_Loop_Statement) then
6011 Set_Sec_Stack_Needed_For_Return (Entity (Identifier (P)));
6013 -- Mark the enclosing function
6015 elsif Nkind (P) = N_Subprogram_Body then
6016 if Present (Corresponding_Spec (P)) then
6017 Set_Sec_Stack_Needed_For_Return (Corresponding_Spec (P));
6018 else
6019 Set_Sec_Stack_Needed_For_Return (Defining_Entity (P));
6020 end if;
6022 -- Do not go beyond the enclosing function
6024 exit;
6025 end if;
6027 P := Parent (P);
6028 end loop;
6029 end;
6031 -- Optimize the case where the result is a function call. In this
6032 -- case either the result is already on the secondary stack, or is
6033 -- already being returned with the stack pointer depressed and no
6034 -- further processing is required except to set the By_Ref flag
6035 -- to ensure that gigi does not attempt an extra unnecessary copy.
6036 -- (actually not just unnecessary but harmfully wrong in the case
6037 -- of a controlled type, where gigi does not know how to do a copy).
6038 -- To make up for a gcc 2.8.1 deficiency (???), we perform the copy
6039 -- for array types if the constrained status of the target type is
6040 -- different from that of the expression.
6042 if Requires_Transient_Scope (Exptyp)
6043 and then
6044 (not Is_Array_Type (Exptyp)
6045 or else Is_Constrained (Exptyp) = Is_Constrained (R_Type)
6046 or else CW_Or_Has_Controlled_Part (Utyp))
6047 and then Nkind (Exp) = N_Function_Call
6048 then
6049 Set_By_Ref (N);
6051 -- Remove side effects from the expression now so that other parts
6052 -- of the expander do not have to reanalyze this node without this
6053 -- optimization
6055 Rewrite (Exp, Duplicate_Subexpr_No_Checks (Exp));
6057 -- For controlled types, do the allocation on the secondary stack
6058 -- manually in order to call adjust at the right time:
6060 -- type Anon1 is access R_Type;
6061 -- for Anon1'Storage_pool use ss_pool;
6062 -- Anon2 : anon1 := new R_Type'(expr);
6063 -- return Anon2.all;
6065 -- We do the same for classwide types that are not potentially
6066 -- controlled (by the virtue of restriction No_Finalization) because
6067 -- gigi is not able to properly allocate class-wide types.
6069 elsif CW_Or_Has_Controlled_Part (Utyp) then
6070 declare
6071 Loc : constant Source_Ptr := Sloc (N);
6072 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6073 Alloc_Node : Node_Id;
6074 Temp : Entity_Id;
6076 begin
6077 Set_Ekind (Acc_Typ, E_Access_Type);
6079 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
6081 -- This is an allocator for the secondary stack, and it's fine
6082 -- to have Comes_From_Source set False on it, as gigi knows not
6083 -- to flag it as a violation of No_Implicit_Heap_Allocations.
6085 Alloc_Node :=
6086 Make_Allocator (Loc,
6087 Expression =>
6088 Make_Qualified_Expression (Loc,
6089 Subtype_Mark => New_Occurrence_Of (Etype (Exp), Loc),
6090 Expression => Relocate_Node (Exp)));
6092 -- We do not want discriminant checks on the declaration,
6093 -- given that it gets its value from the allocator.
6095 Set_No_Initialization (Alloc_Node);
6097 Temp := Make_Temporary (Loc, 'R', Alloc_Node);
6099 Insert_List_Before_And_Analyze (N, New_List (
6100 Make_Full_Type_Declaration (Loc,
6101 Defining_Identifier => Acc_Typ,
6102 Type_Definition =>
6103 Make_Access_To_Object_Definition (Loc,
6104 Subtype_Indication => Subtype_Ind)),
6106 Make_Object_Declaration (Loc,
6107 Defining_Identifier => Temp,
6108 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
6109 Expression => Alloc_Node)));
6111 Rewrite (Exp,
6112 Make_Explicit_Dereference (Loc,
6113 Prefix => New_Occurrence_Of (Temp, Loc)));
6115 -- Ada 2005 (AI-251): If the type of the returned object is
6116 -- an interface then add an implicit type conversion to force
6117 -- displacement of the "this" pointer.
6119 if Is_Interface (R_Type) then
6120 Rewrite (Exp, Convert_To (R_Type, Relocate_Node (Exp)));
6121 end if;
6123 Analyze_And_Resolve (Exp, R_Type);
6124 end;
6126 -- Otherwise use the gigi mechanism to allocate result on the
6127 -- secondary stack.
6129 else
6130 Check_Restriction (No_Secondary_Stack, N);
6131 Set_Storage_Pool (N, RTE (RE_SS_Pool));
6133 -- If we are generating code for the VM do not use
6134 -- SS_Allocate since everything is heap-allocated anyway.
6136 if VM_Target = No_VM then
6137 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
6138 end if;
6139 end if;
6140 end if;
6142 -- Implement the rules of 6.5(8-10), which require a tag check in
6143 -- the case of a limited tagged return type, and tag reassignment for
6144 -- nonlimited tagged results. These actions are needed when the return
6145 -- type is a specific tagged type and the result expression is a
6146 -- conversion or a formal parameter, because in that case the tag of
6147 -- the expression might differ from the tag of the specific result type.
6149 if Is_Tagged_Type (Utyp)
6150 and then not Is_Class_Wide_Type (Utyp)
6151 and then (Nkind_In (Exp, N_Type_Conversion,
6152 N_Unchecked_Type_Conversion)
6153 or else (Is_Entity_Name (Exp)
6154 and then Ekind (Entity (Exp)) in Formal_Kind))
6155 then
6156 -- When the return type is limited, perform a check that the tag of
6157 -- the result is the same as the tag of the return type.
6159 if Is_Limited_Type (R_Type) then
6160 Insert_Action (Exp,
6161 Make_Raise_Constraint_Error (Loc,
6162 Condition =>
6163 Make_Op_Ne (Loc,
6164 Left_Opnd =>
6165 Make_Selected_Component (Loc,
6166 Prefix => Duplicate_Subexpr (Exp),
6167 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6168 Right_Opnd =>
6169 Make_Attribute_Reference (Loc,
6170 Prefix =>
6171 New_Occurrence_Of (Base_Type (Utyp), Loc),
6172 Attribute_Name => Name_Tag)),
6173 Reason => CE_Tag_Check_Failed));
6175 -- If the result type is a specific nonlimited tagged type, then we
6176 -- have to ensure that the tag of the result is that of the result
6177 -- type. This is handled by making a copy of the expression in
6178 -- the case where it might have a different tag, namely when the
6179 -- expression is a conversion or a formal parameter. We create a new
6180 -- object of the result type and initialize it from the expression,
6181 -- which will implicitly force the tag to be set appropriately.
6183 else
6184 declare
6185 ExpR : constant Node_Id := Relocate_Node (Exp);
6186 Result_Id : constant Entity_Id :=
6187 Make_Temporary (Loc, 'R', ExpR);
6188 Result_Exp : constant Node_Id :=
6189 New_Occurrence_Of (Result_Id, Loc);
6190 Result_Obj : constant Node_Id :=
6191 Make_Object_Declaration (Loc,
6192 Defining_Identifier => Result_Id,
6193 Object_Definition =>
6194 New_Occurrence_Of (R_Type, Loc),
6195 Constant_Present => True,
6196 Expression => ExpR);
6198 begin
6199 Set_Assignment_OK (Result_Obj);
6200 Insert_Action (Exp, Result_Obj);
6202 Rewrite (Exp, Result_Exp);
6203 Analyze_And_Resolve (Exp, R_Type);
6204 end;
6205 end if;
6207 -- Ada 2005 (AI-344): If the result type is class-wide, then insert
6208 -- a check that the level of the return expression's underlying type
6209 -- is not deeper than the level of the master enclosing the function.
6210 -- Always generate the check when the type of the return expression
6211 -- is class-wide, when it's a type conversion, or when it's a formal
6212 -- parameter. Otherwise, suppress the check in the case where the
6213 -- return expression has a specific type whose level is known not to
6214 -- be statically deeper than the function's result type.
6216 -- No runtime check needed in interface thunks since it is performed
6217 -- by the target primitive associated with the thunk.
6219 -- Note: accessibility check is skipped in the VM case, since there
6220 -- does not seem to be any practical way to implement this check.
6222 elsif Ada_Version >= Ada_2005
6223 and then Tagged_Type_Expansion
6224 and then Is_Class_Wide_Type (R_Type)
6225 and then not Is_Thunk (Current_Scope)
6226 and then not Scope_Suppress.Suppress (Accessibility_Check)
6227 and then
6228 (Is_Class_Wide_Type (Etype (Exp))
6229 or else Nkind_In (Exp, N_Type_Conversion,
6230 N_Unchecked_Type_Conversion)
6231 or else (Is_Entity_Name (Exp)
6232 and then Ekind (Entity (Exp)) in Formal_Kind)
6233 or else Scope_Depth (Enclosing_Dynamic_Scope (Etype (Exp))) >
6234 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))
6235 then
6236 declare
6237 Tag_Node : Node_Id;
6239 begin
6240 -- Ada 2005 (AI-251): In class-wide interface objects we displace
6241 -- "this" to reference the base of the object. This is required to
6242 -- get access to the TSD of the object.
6244 if Is_Class_Wide_Type (Etype (Exp))
6245 and then Is_Interface (Etype (Exp))
6246 and then Nkind (Exp) = N_Explicit_Dereference
6247 then
6248 Tag_Node :=
6249 Make_Explicit_Dereference (Loc,
6250 Prefix =>
6251 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
6252 Make_Function_Call (Loc,
6253 Name =>
6254 New_Occurrence_Of (RTE (RE_Base_Address), Loc),
6255 Parameter_Associations => New_List (
6256 Unchecked_Convert_To (RTE (RE_Address),
6257 Duplicate_Subexpr (Prefix (Exp)))))));
6258 else
6259 Tag_Node :=
6260 Make_Attribute_Reference (Loc,
6261 Prefix => Duplicate_Subexpr (Exp),
6262 Attribute_Name => Name_Tag);
6263 end if;
6265 Insert_Action (Exp,
6266 Make_Raise_Program_Error (Loc,
6267 Condition =>
6268 Make_Op_Gt (Loc,
6269 Left_Opnd => Build_Get_Access_Level (Loc, Tag_Node),
6270 Right_Opnd =>
6271 Make_Integer_Literal (Loc,
6272 Scope_Depth (Enclosing_Dynamic_Scope (Scope_Id)))),
6273 Reason => PE_Accessibility_Check_Failed));
6274 end;
6276 -- AI05-0073: If function has a controlling access result, check that
6277 -- the tag of the return value, if it is not null, matches designated
6278 -- type of return type.
6280 -- The return expression is referenced twice in the code below, so it
6281 -- must be made free of side effects. Given that different compilers
6282 -- may evaluate these parameters in different order, both occurrences
6283 -- perform a copy.
6285 elsif Ekind (R_Type) = E_Anonymous_Access_Type
6286 and then Has_Controlling_Result (Scope_Id)
6287 then
6288 Insert_Action (N,
6289 Make_Raise_Constraint_Error (Loc,
6290 Condition =>
6291 Make_And_Then (Loc,
6292 Left_Opnd =>
6293 Make_Op_Ne (Loc,
6294 Left_Opnd => Duplicate_Subexpr (Exp),
6295 Right_Opnd => Make_Null (Loc)),
6297 Right_Opnd => Make_Op_Ne (Loc,
6298 Left_Opnd =>
6299 Make_Selected_Component (Loc,
6300 Prefix => Duplicate_Subexpr (Exp),
6301 Selector_Name => Make_Identifier (Loc, Name_uTag)),
6303 Right_Opnd =>
6304 Make_Attribute_Reference (Loc,
6305 Prefix =>
6306 New_Occurrence_Of (Designated_Type (R_Type), Loc),
6307 Attribute_Name => Name_Tag))),
6309 Reason => CE_Tag_Check_Failed),
6310 Suppress => All_Checks);
6311 end if;
6313 -- AI05-0234: RM 6.5(21/3). Check access discriminants to
6314 -- ensure that the function result does not outlive an
6315 -- object designated by one of it discriminants.
6317 if Present (Extra_Accessibility_Of_Result (Scope_Id))
6318 and then Has_Unconstrained_Access_Discriminants (R_Type)
6319 then
6320 declare
6321 Discrim_Source : Node_Id;
6323 procedure Check_Against_Result_Level (Level : Node_Id);
6324 -- Check the given accessibility level against the level
6325 -- determined by the point of call. (AI05-0234).
6327 --------------------------------
6328 -- Check_Against_Result_Level --
6329 --------------------------------
6331 procedure Check_Against_Result_Level (Level : Node_Id) is
6332 begin
6333 Insert_Action (N,
6334 Make_Raise_Program_Error (Loc,
6335 Condition =>
6336 Make_Op_Gt (Loc,
6337 Left_Opnd => Level,
6338 Right_Opnd =>
6339 New_Occurrence_Of
6340 (Extra_Accessibility_Of_Result (Scope_Id), Loc)),
6341 Reason => PE_Accessibility_Check_Failed));
6342 end Check_Against_Result_Level;
6344 begin
6345 Discrim_Source := Exp;
6346 while Nkind (Discrim_Source) = N_Qualified_Expression loop
6347 Discrim_Source := Expression (Discrim_Source);
6348 end loop;
6350 if Nkind (Discrim_Source) = N_Identifier
6351 and then Is_Return_Object (Entity (Discrim_Source))
6352 then
6353 Discrim_Source := Entity (Discrim_Source);
6355 if Is_Constrained (Etype (Discrim_Source)) then
6356 Discrim_Source := Etype (Discrim_Source);
6357 else
6358 Discrim_Source := Expression (Parent (Discrim_Source));
6359 end if;
6361 elsif Nkind (Discrim_Source) = N_Identifier
6362 and then Nkind_In (Original_Node (Discrim_Source),
6363 N_Aggregate, N_Extension_Aggregate)
6364 then
6365 Discrim_Source := Original_Node (Discrim_Source);
6367 elsif Nkind (Discrim_Source) = N_Explicit_Dereference and then
6368 Nkind (Original_Node (Discrim_Source)) = N_Function_Call
6369 then
6370 Discrim_Source := Original_Node (Discrim_Source);
6371 end if;
6373 while Nkind_In (Discrim_Source, N_Qualified_Expression,
6374 N_Type_Conversion,
6375 N_Unchecked_Type_Conversion)
6376 loop
6377 Discrim_Source := Expression (Discrim_Source);
6378 end loop;
6380 case Nkind (Discrim_Source) is
6381 when N_Defining_Identifier =>
6383 pragma Assert (Is_Composite_Type (Discrim_Source)
6384 and then Has_Discriminants (Discrim_Source)
6385 and then Is_Constrained (Discrim_Source));
6387 declare
6388 Discrim : Entity_Id :=
6389 First_Discriminant (Base_Type (R_Type));
6390 Disc_Elmt : Elmt_Id :=
6391 First_Elmt (Discriminant_Constraint
6392 (Discrim_Source));
6393 begin
6394 loop
6395 if Ekind (Etype (Discrim)) =
6396 E_Anonymous_Access_Type
6397 then
6398 Check_Against_Result_Level
6399 (Dynamic_Accessibility_Level (Node (Disc_Elmt)));
6400 end if;
6402 Next_Elmt (Disc_Elmt);
6403 Next_Discriminant (Discrim);
6404 exit when not Present (Discrim);
6405 end loop;
6406 end;
6408 when N_Aggregate | N_Extension_Aggregate =>
6410 -- Unimplemented: extension aggregate case where discrims
6411 -- come from ancestor part, not extension part.
6413 declare
6414 Discrim : Entity_Id :=
6415 First_Discriminant (Base_Type (R_Type));
6417 Disc_Exp : Node_Id := Empty;
6419 Positionals_Exhausted
6420 : Boolean := not Present (Expressions
6421 (Discrim_Source));
6423 function Associated_Expr
6424 (Comp_Id : Entity_Id;
6425 Associations : List_Id) return Node_Id;
6427 -- Given a component and a component associations list,
6428 -- locate the expression for that component; returns
6429 -- Empty if no such expression is found.
6431 ---------------------
6432 -- Associated_Expr --
6433 ---------------------
6435 function Associated_Expr
6436 (Comp_Id : Entity_Id;
6437 Associations : List_Id) return Node_Id
6439 Assoc : Node_Id;
6440 Choice : Node_Id;
6442 begin
6443 -- Simple linear search seems ok here
6445 Assoc := First (Associations);
6446 while Present (Assoc) loop
6447 Choice := First (Choices (Assoc));
6448 while Present (Choice) loop
6449 if (Nkind (Choice) = N_Identifier
6450 and then Chars (Choice) = Chars (Comp_Id))
6451 or else (Nkind (Choice) = N_Others_Choice)
6452 then
6453 return Expression (Assoc);
6454 end if;
6456 Next (Choice);
6457 end loop;
6459 Next (Assoc);
6460 end loop;
6462 return Empty;
6463 end Associated_Expr;
6465 -- Start of processing for Expand_Simple_Function_Return
6467 begin
6468 if not Positionals_Exhausted then
6469 Disc_Exp := First (Expressions (Discrim_Source));
6470 end if;
6472 loop
6473 if Positionals_Exhausted then
6474 Disc_Exp :=
6475 Associated_Expr
6476 (Discrim,
6477 Component_Associations (Discrim_Source));
6478 end if;
6480 if Ekind (Etype (Discrim)) =
6481 E_Anonymous_Access_Type
6482 then
6483 Check_Against_Result_Level
6484 (Dynamic_Accessibility_Level (Disc_Exp));
6485 end if;
6487 Next_Discriminant (Discrim);
6488 exit when not Present (Discrim);
6490 if not Positionals_Exhausted then
6491 Next (Disc_Exp);
6492 Positionals_Exhausted := not Present (Disc_Exp);
6493 end if;
6494 end loop;
6495 end;
6497 when N_Function_Call =>
6499 -- No check needed (check performed by callee)
6501 null;
6503 when others =>
6505 declare
6506 Level : constant Node_Id :=
6507 Make_Integer_Literal (Loc,
6508 Object_Access_Level (Discrim_Source));
6510 begin
6511 -- Unimplemented: check for name prefix that includes
6512 -- a dereference of an access value with a dynamic
6513 -- accessibility level (e.g., an access param or a
6514 -- saooaaat) and use dynamic level in that case. For
6515 -- example:
6516 -- return Access_Param.all(Some_Index).Some_Component;
6517 -- ???
6519 Set_Etype (Level, Standard_Natural);
6520 Check_Against_Result_Level (Level);
6521 end;
6523 end case;
6524 end;
6525 end if;
6527 -- If we are returning an object that may not be bit-aligned, then copy
6528 -- the value into a temporary first. This copy may need to expand to a
6529 -- loop of component operations.
6531 if Is_Possibly_Unaligned_Slice (Exp)
6532 or else Is_Possibly_Unaligned_Object (Exp)
6533 then
6534 declare
6535 ExpR : constant Node_Id := Relocate_Node (Exp);
6536 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6537 begin
6538 Insert_Action (Exp,
6539 Make_Object_Declaration (Loc,
6540 Defining_Identifier => Tnn,
6541 Constant_Present => True,
6542 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6543 Expression => ExpR),
6544 Suppress => All_Checks);
6545 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6546 end;
6547 end if;
6549 -- Call the _Postconditions procedure if the related function has
6550 -- contract assertions that need to be verified on exit.
6552 if Ekind (Scope_Id) = E_Function
6553 and then Present (Postconditions_Proc (Scope_Id))
6554 then
6555 -- We are going to reference the returned value twice in this case,
6556 -- once in the call to _Postconditions, and once in the actual return
6557 -- statement, but we can't have side effects happening twice, and in
6558 -- any case for efficiency we don't want to do the computation twice.
6560 -- If the returned expression is an entity name, we don't need to
6561 -- worry since it is efficient and safe to reference it twice, that's
6562 -- also true for literals other than string literals, and for the
6563 -- case of X.all where X is an entity name.
6565 if Is_Entity_Name (Exp)
6566 or else Nkind_In (Exp, N_Character_Literal,
6567 N_Integer_Literal,
6568 N_Real_Literal)
6569 or else (Nkind (Exp) = N_Explicit_Dereference
6570 and then Is_Entity_Name (Prefix (Exp)))
6571 then
6572 null;
6574 -- Otherwise we are going to need a temporary to capture the value
6576 else
6577 declare
6578 ExpR : Node_Id := Relocate_Node (Exp);
6579 Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', ExpR);
6581 begin
6582 -- In the case of discriminated objects, we have created a
6583 -- constrained subtype above, and used the underlying type.
6584 -- This transformation is post-analysis and harmless, except
6585 -- that now the call to the post-condition will be analyzed and
6586 -- type kinds have to match.
6588 if Nkind (ExpR) = N_Unchecked_Type_Conversion
6589 and then
6590 Is_Private_Type (R_Type) /= Is_Private_Type (Etype (ExpR))
6591 then
6592 ExpR := Expression (ExpR);
6593 end if;
6595 -- For a complex expression of an elementary type, capture
6596 -- value in the temporary and use it as the reference.
6598 if Is_Elementary_Type (R_Type) then
6599 Insert_Action (Exp,
6600 Make_Object_Declaration (Loc,
6601 Defining_Identifier => Tnn,
6602 Constant_Present => True,
6603 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6604 Expression => ExpR),
6605 Suppress => All_Checks);
6607 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6609 -- If we have something we can rename, generate a renaming of
6610 -- the object and replace the expression with a reference
6612 elsif Is_Object_Reference (Exp) then
6613 Insert_Action (Exp,
6614 Make_Object_Renaming_Declaration (Loc,
6615 Defining_Identifier => Tnn,
6616 Subtype_Mark => New_Occurrence_Of (R_Type, Loc),
6617 Name => ExpR),
6618 Suppress => All_Checks);
6620 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6622 -- Otherwise we have something like a string literal or an
6623 -- aggregate. We could copy the value, but that would be
6624 -- inefficient. Instead we make a reference to the value and
6625 -- capture this reference with a renaming, the expression is
6626 -- then replaced by a dereference of this renaming.
6628 else
6629 -- For now, copy the value, since the code below does not
6630 -- seem to work correctly ???
6632 Insert_Action (Exp,
6633 Make_Object_Declaration (Loc,
6634 Defining_Identifier => Tnn,
6635 Constant_Present => True,
6636 Object_Definition => New_Occurrence_Of (R_Type, Loc),
6637 Expression => Relocate_Node (Exp)),
6638 Suppress => All_Checks);
6640 Rewrite (Exp, New_Occurrence_Of (Tnn, Loc));
6642 -- Insert_Action (Exp,
6643 -- Make_Object_Renaming_Declaration (Loc,
6644 -- Defining_Identifier => Tnn,
6645 -- Access_Definition =>
6646 -- Make_Access_Definition (Loc,
6647 -- All_Present => True,
6648 -- Subtype_Mark => New_Occurrence_Of (R_Type, Loc)),
6649 -- Name =>
6650 -- Make_Reference (Loc,
6651 -- Prefix => Relocate_Node (Exp))),
6652 -- Suppress => All_Checks);
6654 -- Rewrite (Exp,
6655 -- Make_Explicit_Dereference (Loc,
6656 -- Prefix => New_Occurrence_Of (Tnn, Loc)));
6657 end if;
6658 end;
6659 end if;
6661 -- Generate call to _Postconditions
6663 Insert_Action (Exp,
6664 Make_Procedure_Call_Statement (Loc,
6665 Name =>
6666 New_Occurrence_Of (Postconditions_Proc (Scope_Id), Loc),
6667 Parameter_Associations => New_List (Duplicate_Subexpr (Exp))));
6668 end if;
6670 -- Ada 2005 (AI-251): If this return statement corresponds with an
6671 -- simple return statement associated with an extended return statement
6672 -- and the type of the returned object is an interface then generate an
6673 -- implicit conversion to force displacement of the "this" pointer.
6675 if Ada_Version >= Ada_2005
6676 and then Comes_From_Extended_Return_Statement (N)
6677 and then Nkind (Expression (N)) = N_Identifier
6678 and then Is_Interface (Utyp)
6679 and then Utyp /= Underlying_Type (Exptyp)
6680 then
6681 Rewrite (Exp, Convert_To (Utyp, Relocate_Node (Exp)));
6682 Analyze_And_Resolve (Exp);
6683 end if;
6684 end Expand_Simple_Function_Return;
6686 --------------------------------
6687 -- Expand_Subprogram_Contract --
6688 --------------------------------
6690 procedure Expand_Subprogram_Contract (N : Node_Id) is
6691 Body_Id : constant Entity_Id := Defining_Entity (N);
6692 Spec_Id : constant Entity_Id := Corresponding_Spec (N);
6694 procedure Add_Invariant_And_Predicate_Checks
6695 (Subp_Id : Entity_Id;
6696 Stmts : in out List_Id;
6697 Result : out Node_Id);
6698 -- Process the result of function Subp_Id (if applicable) and all its
6699 -- formals. Add invariant and predicate checks where applicable. The
6700 -- routine appends all the checks to list Stmts. If Subp_Id denotes a
6701 -- function, Result contains the entity of parameter _Result, to be
6702 -- used in the creation of procedure _Postconditions.
6704 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id);
6705 -- Append a node to a list. If there is no list, create a new one. When
6706 -- the item denotes a pragma, it is added to the list only when it is
6707 -- enabled.
6709 procedure Build_Postconditions_Procedure
6710 (Subp_Id : Entity_Id;
6711 Stmts : List_Id;
6712 Result : Entity_Id);
6713 -- Create the body of procedure _Postconditions which handles various
6714 -- assertion actions on exit from subprogram Subp_Id. Stmts is the list
6715 -- of statements to be checked on exit. Parameter Result is the entity
6716 -- of parameter _Result when Subp_Id denotes a function.
6718 function Build_Pragma_Check_Equivalent
6719 (Prag : Node_Id;
6720 Subp_Id : Entity_Id := Empty;
6721 Inher_Id : Entity_Id := Empty) return Node_Id;
6722 -- Transform a [refined] pre- or postcondition denoted by Prag into an
6723 -- equivalent pragma Check. When the pre- or postcondition is inherited,
6724 -- the routine corrects the references of all formals of Inher_Id to
6725 -- point to the formals of Subp_Id.
6727 procedure Process_Contract_Cases (Stmts : in out List_Id);
6728 -- Process pragma Contract_Cases. This routine prepends items to the
6729 -- body declarations and appends items to list Stmts.
6731 procedure Process_Postconditions (Stmts : in out List_Id);
6732 -- Collect all [inherited] spec and body postconditions and accumulate
6733 -- their pragma Check equivalents in list Stmts.
6735 procedure Process_Preconditions;
6736 -- Collect all [inherited] spec and body preconditions and prepend their
6737 -- pragma Check equivalents to the declarations of the body.
6739 ----------------------------------------
6740 -- Add_Invariant_And_Predicate_Checks --
6741 ----------------------------------------
6743 procedure Add_Invariant_And_Predicate_Checks
6744 (Subp_Id : Entity_Id;
6745 Stmts : in out List_Id;
6746 Result : out Node_Id)
6748 procedure Add_Invariant_Access_Checks (Id : Entity_Id);
6749 -- Id denotes the return value of a function or a formal parameter.
6750 -- Add an invariant check if the type of Id is access to a type with
6751 -- invariants. The routine appends the generated code to Stmts.
6753 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean;
6754 -- Determine whether type Typ can benefit from invariant checks. To
6755 -- qualify, the type must have a non-null invariant procedure and
6756 -- subprogram Subp_Id must appear visible from the point of view of
6757 -- the type.
6759 ---------------------------------
6760 -- Add_Invariant_Access_Checks --
6761 ---------------------------------
6763 procedure Add_Invariant_Access_Checks (Id : Entity_Id) is
6764 Loc : constant Source_Ptr := Sloc (N);
6765 Ref : Node_Id;
6766 Typ : Entity_Id;
6768 begin
6769 Typ := Etype (Id);
6771 if Is_Access_Type (Typ) and then not Is_Access_Constant (Typ) then
6772 Typ := Designated_Type (Typ);
6774 if Invariant_Checks_OK (Typ) then
6775 Ref :=
6776 Make_Explicit_Dereference (Loc,
6777 Prefix => New_Occurrence_Of (Id, Loc));
6778 Set_Etype (Ref, Typ);
6780 -- Generate:
6781 -- if <Id> /= null then
6782 -- <invariant_call (<Ref>)>
6783 -- end if;
6785 Append_Enabled_Item
6786 (Item =>
6787 Make_If_Statement (Loc,
6788 Condition =>
6789 Make_Op_Ne (Loc,
6790 Left_Opnd => New_Occurrence_Of (Id, Loc),
6791 Right_Opnd => Make_Null (Loc)),
6792 Then_Statements => New_List (
6793 Make_Invariant_Call (Ref))),
6794 List => Stmts);
6795 end if;
6796 end if;
6797 end Add_Invariant_Access_Checks;
6799 -------------------------
6800 -- Invariant_Checks_OK --
6801 -------------------------
6803 function Invariant_Checks_OK (Typ : Entity_Id) return Boolean is
6804 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean;
6805 -- Determine whether the body of procedure Proc_Id contains a sole
6806 -- null statement, possibly followed by an optional return.
6808 function Has_Public_Visibility_Of_Subprogram return Boolean;
6809 -- Determine whether type Typ has public visibility of subprogram
6810 -- Subp_Id.
6812 -------------------
6813 -- Has_Null_Body --
6814 -------------------
6816 function Has_Null_Body (Proc_Id : Entity_Id) return Boolean is
6817 Body_Id : Entity_Id;
6818 Decl : Node_Id;
6819 Spec : Node_Id;
6820 Stmt1 : Node_Id;
6821 Stmt2 : Node_Id;
6823 begin
6824 Spec := Parent (Proc_Id);
6825 Decl := Parent (Spec);
6827 -- Retrieve the entity of the invariant procedure body
6829 if Nkind (Spec) = N_Procedure_Specification
6830 and then Nkind (Decl) = N_Subprogram_Declaration
6831 then
6832 Body_Id := Corresponding_Body (Decl);
6834 -- The body acts as a spec
6836 else
6837 Body_Id := Proc_Id;
6838 end if;
6840 -- The body will be generated later
6842 if No (Body_Id) then
6843 return False;
6844 end if;
6846 Spec := Parent (Body_Id);
6847 Decl := Parent (Spec);
6849 pragma Assert
6850 (Nkind (Spec) = N_Procedure_Specification
6851 and then Nkind (Decl) = N_Subprogram_Body);
6853 Stmt1 := First (Statements (Handled_Statement_Sequence (Decl)));
6855 -- Look for a null statement followed by an optional return
6856 -- statement.
6858 if Nkind (Stmt1) = N_Null_Statement then
6859 Stmt2 := Next (Stmt1);
6861 if Present (Stmt2) then
6862 return Nkind (Stmt2) = N_Simple_Return_Statement;
6863 else
6864 return True;
6865 end if;
6866 end if;
6868 return False;
6869 end Has_Null_Body;
6871 -----------------------------------------
6872 -- Has_Public_Visibility_Of_Subprogram --
6873 -----------------------------------------
6875 function Has_Public_Visibility_Of_Subprogram return Boolean is
6876 Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id);
6878 begin
6879 -- An Initialization procedure must be considered visible even
6880 -- though it is internally generated.
6882 if Is_Init_Proc (Defining_Entity (Subp_Decl)) then
6883 return True;
6885 elsif Ekind (Scope (Typ)) /= E_Package then
6886 return False;
6888 -- Internally generated code is never publicly visible except
6889 -- for a subprogram that is the implementation of an expression
6890 -- function. In that case the visibility is determined by the
6891 -- last check.
6893 elsif not Comes_From_Source (Subp_Decl)
6894 and then
6895 (Nkind (Original_Node (Subp_Decl)) /= N_Expression_Function
6896 or else not
6897 Comes_From_Source (Defining_Entity (Subp_Decl)))
6898 then
6899 return False;
6901 -- Determine whether the subprogram is declared in the visible
6902 -- declarations of the package containing the type.
6904 else
6905 return List_Containing (Subp_Decl) =
6906 Visible_Declarations
6907 (Specification (Unit_Declaration_Node (Scope (Typ))));
6908 end if;
6909 end Has_Public_Visibility_Of_Subprogram;
6911 -- Start of processing for Invariant_Checks_OK
6913 begin
6914 return
6915 Has_Invariants (Typ)
6916 and then Present (Invariant_Procedure (Typ))
6917 and then not Has_Null_Body (Invariant_Procedure (Typ))
6918 and then Has_Public_Visibility_Of_Subprogram;
6919 end Invariant_Checks_OK;
6921 -- Local variables
6923 Loc : constant Source_Ptr := Sloc (N);
6924 -- Source location of subprogram contract
6926 Formal : Entity_Id;
6927 Typ : Entity_Id;
6929 -- Start of processing for Add_Invariant_And_Predicate_Checks
6931 begin
6932 Result := Empty;
6934 -- Process the result of a function
6936 if Ekind (Subp_Id) = E_Function then
6937 Typ := Etype (Subp_Id);
6939 -- Generate _Result which is used in procedure _Postconditions to
6940 -- verify the return value.
6942 Result := Make_Defining_Identifier (Loc, Name_uResult);
6943 Set_Etype (Result, Typ);
6945 -- Add an invariant check when the return type has invariants and
6946 -- the related function is visible to the outside.
6948 if Invariant_Checks_OK (Typ) then
6949 Append_Enabled_Item
6950 (Item =>
6951 Make_Invariant_Call (New_Occurrence_Of (Result, Loc)),
6952 List => Stmts);
6953 end if;
6955 -- Add an invariant check when the return type is an access to a
6956 -- type with invariants.
6958 Add_Invariant_Access_Checks (Result);
6959 end if;
6961 -- Add invariant and predicates for all formals that qualify
6963 Formal := First_Formal (Subp_Id);
6964 while Present (Formal) loop
6965 Typ := Etype (Formal);
6967 if Ekind (Formal) /= E_In_Parameter
6968 or else Is_Access_Type (Typ)
6969 then
6970 if Invariant_Checks_OK (Typ) then
6971 Append_Enabled_Item
6972 (Item =>
6973 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)),
6974 List => Stmts);
6975 end if;
6977 Add_Invariant_Access_Checks (Formal);
6979 -- Note: we used to add predicate checks for OUT and IN OUT
6980 -- formals here, but that was misguided, since such checks are
6981 -- performed on the caller side, based on the predicate of the
6982 -- actual, rather than the predicate of the formal.
6984 end if;
6986 Next_Formal (Formal);
6987 end loop;
6988 end Add_Invariant_And_Predicate_Checks;
6990 -------------------------
6991 -- Append_Enabled_Item --
6992 -------------------------
6994 procedure Append_Enabled_Item (Item : Node_Id; List : in out List_Id) is
6995 begin
6996 -- Do not chain ignored or disabled pragmas
6998 if Nkind (Item) = N_Pragma
6999 and then (Is_Ignored (Item) or else Is_Disabled (Item))
7000 then
7001 null;
7003 -- Otherwise, add the item
7005 else
7006 if No (List) then
7007 List := New_List;
7008 end if;
7010 -- If the pragma is a conjunct in a composite postcondition, it
7011 -- has been processed in reverse order. In the postcondition body
7012 -- if must appear before the others.
7014 if Nkind (Item) = N_Pragma
7015 and then From_Aspect_Specification (Item)
7016 and then Split_PPC (Item)
7017 then
7018 Prepend (Item, List);
7019 else
7020 Append (Item, List);
7021 end if;
7022 end if;
7023 end Append_Enabled_Item;
7025 ------------------------------------
7026 -- Build_Postconditions_Procedure --
7027 ------------------------------------
7029 procedure Build_Postconditions_Procedure
7030 (Subp_Id : Entity_Id;
7031 Stmts : List_Id;
7032 Result : Entity_Id)
7034 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id);
7035 -- Insert node Stmt before the first source declaration of the
7036 -- related subprogram's body. If no such declaration exists, Stmt
7037 -- becomes the last declaration.
7039 --------------------------------------------
7040 -- Insert_Before_First_Source_Declaration --
7041 --------------------------------------------
7043 procedure Insert_Before_First_Source_Declaration (Stmt : Node_Id) is
7044 Decls : constant List_Id := Declarations (N);
7045 Decl : Node_Id;
7047 begin
7048 -- Inspect the declarations of the related subprogram body looking
7049 -- for the first source declaration.
7051 if Present (Decls) then
7052 Decl := First (Decls);
7053 while Present (Decl) loop
7054 if Comes_From_Source (Decl) then
7055 Insert_Before (Decl, Stmt);
7056 return;
7057 end if;
7059 Next (Decl);
7060 end loop;
7062 -- If we get there, then the subprogram body lacks any source
7063 -- declarations. The body of _Postconditions now acts as the
7064 -- last declaration.
7066 Append (Stmt, Decls);
7068 -- Ensure that the body has a declaration list
7070 else
7071 Set_Declarations (N, New_List (Stmt));
7072 end if;
7073 end Insert_Before_First_Source_Declaration;
7075 -- Local variables
7077 Loc : constant Source_Ptr := Sloc (N);
7078 Params : List_Id := No_List;
7079 Proc_Bod : Node_Id;
7080 Proc_Id : Entity_Id;
7082 -- Start of processing for Build_Postconditions_Procedure
7084 begin
7085 -- Nothing to do if there are no actions to check on exit
7087 if No (Stmts) then
7088 return;
7089 end if;
7091 Proc_Id := Make_Defining_Identifier (Loc, Name_uPostconditions);
7092 Set_Debug_Info_Needed (Proc_Id);
7093 Set_Postconditions_Proc (Subp_Id, Proc_Id);
7095 -- The related subprogram is a function, create the specification of
7096 -- parameter _Result.
7098 if Present (Result) then
7099 Params := New_List (
7100 Make_Parameter_Specification (Loc,
7101 Defining_Identifier => Result,
7102 Parameter_Type =>
7103 New_Occurrence_Of (Etype (Result), Loc)));
7104 end if;
7106 -- Insert _Postconditions before the first source declaration of the
7107 -- body. This ensures that the body will not cause any premature
7108 -- freezing as it may mention types:
7110 -- procedure Proc (Obj : Array_Typ) is
7111 -- procedure _postconditions is
7112 -- begin
7113 -- ... Obj ...
7114 -- end _postconditions;
7116 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
7117 -- begin
7119 -- In the example above, Obj is of type T but the incorrect placement
7120 -- of _Postconditions will cause a crash in gigi due to an out of
7121 -- order reference. The body of _Postconditions must be placed after
7122 -- the declaration of Temp to preserve correct visibility.
7124 -- Set an explicit End_Lavel to override the sloc of the implicit
7125 -- RETURN statement, and prevent it from inheriting the sloc of one
7126 -- the postconditions: this would cause confusing debug into to be
7127 -- produced, interfering with coverage analysis tools.
7129 Proc_Bod :=
7130 Make_Subprogram_Body (Loc,
7131 Specification =>
7132 Make_Procedure_Specification (Loc,
7133 Defining_Unit_Name => Proc_Id,
7134 Parameter_Specifications => Params),
7136 Declarations => Empty_List,
7137 Handled_Statement_Sequence =>
7138 Make_Handled_Sequence_Of_Statements (Loc,
7139 Statements => Stmts,
7140 End_Label => Make_Identifier (Loc, Chars (Proc_Id))));
7142 Insert_Before_First_Source_Declaration (Proc_Bod);
7143 Analyze (Proc_Bod);
7144 end Build_Postconditions_Procedure;
7146 -----------------------------------
7147 -- Build_Pragma_Check_Equivalent --
7148 -----------------------------------
7150 function Build_Pragma_Check_Equivalent
7151 (Prag : Node_Id;
7152 Subp_Id : Entity_Id := Empty;
7153 Inher_Id : Entity_Id := Empty) return Node_Id
7155 Loc : constant Source_Ptr := Sloc (Prag);
7156 Prag_Nam : constant Name_Id := Pragma_Name (Prag);
7157 Check_Prag : Node_Id;
7158 Formals_Map : Elist_Id;
7159 Inher_Formal : Entity_Id;
7160 Msg_Arg : Node_Id;
7161 Nam : Name_Id;
7162 Subp_Formal : Entity_Id;
7164 begin
7165 Formals_Map := No_Elist;
7167 -- When the pre- or postcondition is inherited, map the formals of
7168 -- the inherited subprogram to those of the current subprogram.
7170 if Present (Inher_Id) then
7171 pragma Assert (Present (Subp_Id));
7173 Formals_Map := New_Elmt_List;
7175 -- Create a relation <inherited formal> => <subprogram formal>
7177 Inher_Formal := First_Formal (Inher_Id);
7178 Subp_Formal := First_Formal (Subp_Id);
7179 while Present (Inher_Formal) and then Present (Subp_Formal) loop
7180 Append_Elmt (Inher_Formal, Formals_Map);
7181 Append_Elmt (Subp_Formal, Formals_Map);
7183 Next_Formal (Inher_Formal);
7184 Next_Formal (Subp_Formal);
7185 end loop;
7186 end if;
7188 -- Copy the original pragma while performing substitutions (if
7189 -- applicable).
7191 Check_Prag :=
7192 New_Copy_Tree
7193 (Source => Prag,
7194 Map => Formals_Map,
7195 New_Scope => Current_Scope);
7197 -- Mark the pragma as being internally generated and reset the
7198 -- Analyzed flag.
7200 Set_Comes_From_Source (Check_Prag, False);
7201 Set_Analyzed (Check_Prag, False);
7203 if Present (Corresponding_Aspect (Prag)) then
7204 Nam := Chars (Identifier (Corresponding_Aspect (Prag)));
7205 else
7206 Nam := Prag_Nam;
7207 end if;
7209 -- Convert the copy into pragma Check by correcting the name and
7210 -- adding a check_kind argument.
7212 Set_Pragma_Identifier
7213 (Check_Prag, Make_Identifier (Loc, Name_Check));
7215 Prepend_To (Pragma_Argument_Associations (Check_Prag),
7216 Make_Pragma_Argument_Association (Loc,
7217 Expression => Make_Identifier (Loc, Nam)));
7219 -- Update the error message when the pragma is inherited
7221 if Present (Inher_Id) then
7222 Msg_Arg := Last (Pragma_Argument_Associations (Check_Prag));
7224 if Chars (Msg_Arg) = Name_Message then
7225 String_To_Name_Buffer (Strval (Expression (Msg_Arg)));
7227 -- Insert "inherited" to improve the error message
7229 if Name_Buffer (1 .. 8) = "failed p" then
7230 Insert_Str_In_Name_Buffer ("inherited ", 8);
7231 Set_Strval (Expression (Msg_Arg), String_From_Name_Buffer);
7232 end if;
7233 end if;
7234 end if;
7236 return Check_Prag;
7237 end Build_Pragma_Check_Equivalent;
7239 ----------------------------
7240 -- Process_Contract_Cases --
7241 ----------------------------
7243 procedure Process_Contract_Cases (Stmts : in out List_Id) is
7244 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id);
7245 -- Process pragma Contract_Cases for subprogram Subp_Id
7247 --------------------------------
7248 -- Process_Contract_Cases_For --
7249 --------------------------------
7251 procedure Process_Contract_Cases_For (Subp_Id : Entity_Id) is
7252 Items : constant Node_Id := Contract (Subp_Id);
7253 Prag : Node_Id;
7255 begin
7256 if Present (Items) then
7257 Prag := Contract_Test_Cases (Items);
7258 while Present (Prag) loop
7259 if Pragma_Name (Prag) = Name_Contract_Cases then
7260 Expand_Contract_Cases
7261 (CCs => Prag,
7262 Subp_Id => Subp_Id,
7263 Decls => Declarations (N),
7264 Stmts => Stmts);
7265 end if;
7267 Prag := Next_Pragma (Prag);
7268 end loop;
7269 end if;
7270 end Process_Contract_Cases_For;
7272 -- Start of processing for Process_Contract_Cases
7274 begin
7275 Process_Contract_Cases_For (Body_Id);
7277 if Present (Spec_Id) then
7278 Process_Contract_Cases_For (Spec_Id);
7279 end if;
7280 end Process_Contract_Cases;
7282 ----------------------------
7283 -- Process_Postconditions --
7284 ----------------------------
7286 procedure Process_Postconditions (Stmts : in out List_Id) is
7287 procedure Process_Body_Postconditions (Post_Nam : Name_Id);
7288 -- Collect all [refined] postconditions of a specific kind denoted
7289 -- by Post_Nam that belong to the body and generate pragma Check
7290 -- equivalents in list Stmts.
7292 procedure Process_Spec_Postconditions;
7293 -- Collect all [inherited] postconditions of the spec and generate
7294 -- pragma Check equivalents in list Stmts.
7296 ---------------------------------
7297 -- Process_Body_Postconditions --
7298 ---------------------------------
7300 procedure Process_Body_Postconditions (Post_Nam : Name_Id) is
7301 Items : constant Node_Id := Contract (Body_Id);
7302 Unit_Decl : constant Node_Id := Parent (N);
7303 Decl : Node_Id;
7304 Prag : Node_Id;
7306 begin
7307 -- Process the contract
7309 if Present (Items) then
7310 Prag := Pre_Post_Conditions (Items);
7311 while Present (Prag) loop
7312 if Pragma_Name (Prag) = Post_Nam then
7313 Append_Enabled_Item
7314 (Item => Build_Pragma_Check_Equivalent (Prag),
7315 List => Stmts);
7316 end if;
7318 Prag := Next_Pragma (Prag);
7319 end loop;
7320 end if;
7322 -- The subprogram body being processed is actually the proper body
7323 -- of a stub with a corresponding spec. The subprogram stub may
7324 -- carry a postcondition pragma in which case it must be taken
7325 -- into account. The pragma appears after the stub.
7327 if Present (Spec_Id) and then Nkind (Unit_Decl) = N_Subunit then
7328 Decl := Next (Corresponding_Stub (Unit_Decl));
7329 while Present (Decl) loop
7331 -- Note that non-matching pragmas are skipped
7333 if Nkind (Decl) = N_Pragma then
7334 if Pragma_Name (Decl) = Post_Nam then
7335 Append_Enabled_Item
7336 (Item => Build_Pragma_Check_Equivalent (Decl),
7337 List => Stmts);
7338 end if;
7340 -- Skip internally generated code
7342 elsif not Comes_From_Source (Decl) then
7343 null;
7345 -- Postcondition pragmas are usually grouped together. There
7346 -- is no need to inspect the whole declarative list.
7348 else
7349 exit;
7350 end if;
7352 Next (Decl);
7353 end loop;
7354 end if;
7355 end Process_Body_Postconditions;
7357 ---------------------------------
7358 -- Process_Spec_Postconditions --
7359 ---------------------------------
7361 procedure Process_Spec_Postconditions is
7362 Subps : constant Subprogram_List :=
7363 Inherited_Subprograms (Spec_Id);
7364 Items : Node_Id;
7365 Prag : Node_Id;
7366 Subp_Id : Entity_Id;
7368 begin
7369 -- Process the contract
7371 Items := Contract (Spec_Id);
7373 if Present (Items) then
7374 Prag := Pre_Post_Conditions (Items);
7375 while Present (Prag) loop
7376 if Pragma_Name (Prag) = Name_Postcondition then
7377 Append_Enabled_Item
7378 (Item => Build_Pragma_Check_Equivalent (Prag),
7379 List => Stmts);
7380 end if;
7382 Prag := Next_Pragma (Prag);
7383 end loop;
7384 end if;
7386 -- Process the contracts of all inherited subprograms, looking for
7387 -- class-wide postconditions.
7389 for Index in Subps'Range loop
7390 Subp_Id := Subps (Index);
7391 Items := Contract (Subp_Id);
7393 if Present (Items) then
7394 Prag := Pre_Post_Conditions (Items);
7395 while Present (Prag) loop
7396 if Pragma_Name (Prag) = Name_Postcondition
7397 and then Class_Present (Prag)
7398 then
7399 Append_Enabled_Item
7400 (Item =>
7401 Build_Pragma_Check_Equivalent
7402 (Prag => Prag,
7403 Subp_Id => Spec_Id,
7404 Inher_Id => Subp_Id),
7405 List => Stmts);
7406 end if;
7408 Prag := Next_Pragma (Prag);
7409 end loop;
7410 end if;
7411 end loop;
7412 end Process_Spec_Postconditions;
7414 -- Start of processing for Process_Postconditions
7416 begin
7417 -- The processing of postconditions is done in reverse order (body
7418 -- first) to ensure the following arrangement:
7420 -- <refined postconditions from body>
7421 -- <postconditions from body>
7422 -- <postconditions from spec>
7423 -- <inherited postconditions>
7425 Process_Body_Postconditions (Name_Refined_Post);
7426 Process_Body_Postconditions (Name_Postcondition);
7428 if Present (Spec_Id) then
7429 Process_Spec_Postconditions;
7430 end if;
7431 end Process_Postconditions;
7433 ---------------------------
7434 -- Process_Preconditions --
7435 ---------------------------
7437 procedure Process_Preconditions is
7438 Class_Pre : Node_Id := Empty;
7439 -- The sole [inherited] class-wide precondition pragma that applies
7440 -- to the subprogram.
7442 Insert_Node : Node_Id := Empty;
7443 -- The insertion node after which all pragma Check equivalents are
7444 -- inserted.
7446 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id);
7447 -- Merge two class-wide preconditions by "or else"-ing them. The
7448 -- changes are accumulated in parameter Into. Update the error
7449 -- message of Into.
7451 procedure Prepend_To_Decls (Item : Node_Id);
7452 -- Prepend a single item to the declarations of the subprogram body
7454 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id);
7455 -- Save a class-wide precondition into Class_Pre or prepend a normal
7456 -- precondition ot the declarations of the body and analyze it.
7458 procedure Process_Inherited_Preconditions;
7459 -- Collect all inherited class-wide preconditions and merge them into
7460 -- one big precondition to be evaluated as pragma Check.
7462 procedure Process_Preconditions_For (Subp_Id : Entity_Id);
7463 -- Collect all preconditions of subprogram Subp_Id and prepend their
7464 -- pragma Check equivalents to the declarations of the body.
7466 -------------------------
7467 -- Merge_Preconditions --
7468 -------------------------
7470 procedure Merge_Preconditions (From : Node_Id; Into : Node_Id) is
7471 function Expression_Arg (Prag : Node_Id) return Node_Id;
7472 -- Return the boolean expression argument of a precondition while
7473 -- updating its parenteses count for the subsequent merge.
7475 function Message_Arg (Prag : Node_Id) return Node_Id;
7476 -- Return the message argument of a precondition
7478 --------------------
7479 -- Expression_Arg --
7480 --------------------
7482 function Expression_Arg (Prag : Node_Id) return Node_Id is
7483 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7484 Arg : constant Node_Id := Get_Pragma_Arg (Next (First (Args)));
7486 begin
7487 if Paren_Count (Arg) = 0 then
7488 Set_Paren_Count (Arg, 1);
7489 end if;
7491 return Arg;
7492 end Expression_Arg;
7494 -----------------
7495 -- Message_Arg --
7496 -----------------
7498 function Message_Arg (Prag : Node_Id) return Node_Id is
7499 Args : constant List_Id := Pragma_Argument_Associations (Prag);
7500 begin
7501 return Get_Pragma_Arg (Last (Args));
7502 end Message_Arg;
7504 -- Local variables
7506 From_Expr : constant Node_Id := Expression_Arg (From);
7507 From_Msg : constant Node_Id := Message_Arg (From);
7508 Into_Expr : constant Node_Id := Expression_Arg (Into);
7509 Into_Msg : constant Node_Id := Message_Arg (Into);
7510 Loc : constant Source_Ptr := Sloc (Into);
7512 -- Start of processing for Merge_Preconditions
7514 begin
7515 -- Merge the two preconditions by "or else"-ing them
7517 Rewrite (Into_Expr,
7518 Make_Or_Else (Loc,
7519 Right_Opnd => Relocate_Node (Into_Expr),
7520 Left_Opnd => From_Expr));
7522 -- Merge the two error messages to produce a single message of the
7523 -- form:
7525 -- failed precondition from ...
7526 -- also failed inherited precondition from ...
7528 if not Exception_Locations_Suppressed then
7529 Start_String (Strval (Into_Msg));
7530 Store_String_Char (ASCII.LF);
7531 Store_String_Chars (" also ");
7532 Store_String_Chars (Strval (From_Msg));
7534 Set_Strval (Into_Msg, End_String);
7535 end if;
7536 end Merge_Preconditions;
7538 ----------------------
7539 -- Prepend_To_Decls --
7540 ----------------------
7542 procedure Prepend_To_Decls (Item : Node_Id) is
7543 Decls : List_Id := Declarations (N);
7545 begin
7546 -- Ensure that the body has a declarative list
7548 if No (Decls) then
7549 Decls := New_List;
7550 Set_Declarations (N, Decls);
7551 end if;
7553 Prepend_To (Decls, Item);
7554 end Prepend_To_Decls;
7556 ------------------------------
7557 -- Prepend_To_Decls_Or_Save --
7558 ------------------------------
7560 procedure Prepend_To_Decls_Or_Save (Prag : Node_Id) is
7561 Check_Prag : Node_Id;
7563 begin
7564 Check_Prag := Build_Pragma_Check_Equivalent (Prag);
7566 -- Save the sole class-wide precondition (if any) for the next
7567 -- step where it will be merged with inherited preconditions.
7569 if Class_Present (Prag) then
7570 pragma Assert (No (Class_Pre));
7571 Class_Pre := Check_Prag;
7573 -- Accumulate the corresponding Check pragmas at the top of the
7574 -- declarations. Prepending the items ensures that they will be
7575 -- evaluated in their original order.
7577 else
7578 if Present (Insert_Node) then
7579 Insert_After (Insert_Node, Check_Prag);
7580 else
7581 Prepend_To_Decls (Check_Prag);
7582 end if;
7584 Analyze (Check_Prag);
7585 end if;
7586 end Prepend_To_Decls_Or_Save;
7588 -------------------------------------
7589 -- Process_Inherited_Preconditions --
7590 -------------------------------------
7592 procedure Process_Inherited_Preconditions is
7593 Subps : constant Subprogram_List :=
7594 Inherited_Subprograms (Spec_Id);
7595 Check_Prag : Node_Id;
7596 Items : Node_Id;
7597 Prag : Node_Id;
7598 Subp_Id : Entity_Id;
7600 begin
7601 -- Process the contracts of all inherited subprograms, looking for
7602 -- class-wide preconditions.
7604 for Index in Subps'Range loop
7605 Subp_Id := Subps (Index);
7606 Items := Contract (Subp_Id);
7608 if Present (Items) then
7609 Prag := Pre_Post_Conditions (Items);
7610 while Present (Prag) loop
7611 if Pragma_Name (Prag) = Name_Precondition
7612 and then Class_Present (Prag)
7613 then
7614 Check_Prag :=
7615 Build_Pragma_Check_Equivalent
7616 (Prag => Prag,
7617 Subp_Id => Spec_Id,
7618 Inher_Id => Subp_Id);
7620 -- The spec or an inherited subprogram already yielded
7621 -- a class-wide precondition. Merge the existing
7622 -- precondition with the current one using "or else".
7624 if Present (Class_Pre) then
7625 Merge_Preconditions (Check_Prag, Class_Pre);
7626 else
7627 Class_Pre := Check_Prag;
7628 end if;
7629 end if;
7631 Prag := Next_Pragma (Prag);
7632 end loop;
7633 end if;
7634 end loop;
7636 -- Add the merged class-wide preconditions
7638 if Present (Class_Pre) then
7639 Prepend_To_Decls (Class_Pre);
7640 Analyze (Class_Pre);
7641 end if;
7642 end Process_Inherited_Preconditions;
7644 -------------------------------
7645 -- Process_Preconditions_For --
7646 -------------------------------
7648 procedure Process_Preconditions_For (Subp_Id : Entity_Id) is
7649 Items : constant Node_Id := Contract (Subp_Id);
7650 Decl : Node_Id;
7651 Prag : Node_Id;
7652 Subp_Decl : Node_Id;
7654 begin
7655 -- Process the contract
7657 if Present (Items) then
7658 Prag := Pre_Post_Conditions (Items);
7659 while Present (Prag) loop
7660 if Pragma_Name (Prag) = Name_Precondition then
7661 Prepend_To_Decls_Or_Save (Prag);
7662 end if;
7664 Prag := Next_Pragma (Prag);
7665 end loop;
7666 end if;
7668 -- The subprogram declaration being processed is actually a body
7669 -- stub. The stub may carry a precondition pragma in which case it
7670 -- must be taken into account. The pragma appears after the stub.
7672 Subp_Decl := Unit_Declaration_Node (Subp_Id);
7674 if Nkind (Subp_Decl) = N_Subprogram_Body_Stub then
7676 -- Inspect the declarations following the body stub
7678 Decl := Next (Subp_Decl);
7679 while Present (Decl) loop
7681 -- Note that non-matching pragmas are skipped
7683 if Nkind (Decl) = N_Pragma then
7684 if Pragma_Name (Decl) = Name_Precondition then
7685 Prepend_To_Decls_Or_Save (Decl);
7686 end if;
7688 -- Skip internally generated code
7690 elsif not Comes_From_Source (Decl) then
7691 null;
7693 -- Preconditions are usually grouped together. There is no
7694 -- need to inspect the whole declarative list.
7696 else
7697 exit;
7698 end if;
7700 Next (Decl);
7701 end loop;
7702 end if;
7703 end Process_Preconditions_For;
7705 -- Local variables
7707 Decls : constant List_Id := Declarations (N);
7708 Decl : Node_Id;
7710 -- Start of processing for Process_Preconditions
7712 begin
7713 -- Find the last internally generate declaration starting from the
7714 -- top of the body declarations. This ensures that discriminals and
7715 -- subtypes are properly visible to the pragma Check equivalents.
7717 if Present (Decls) then
7718 Decl := First (Decls);
7720 while Present (Decl) loop
7721 if Comes_From_Source (Decl) then
7722 exit;
7723 else
7724 Insert_Node := Decl;
7725 end if;
7727 Next (Decl);
7728 end loop;
7729 end if;
7731 -- The processing of preconditions is done in reverse order (body
7732 -- first) because each pragma Check equivalent is inserted at the
7733 -- top of the declarations. This ensures that the final order is
7734 -- consistent with following diagram:
7736 -- <inherited preconditions>
7737 -- <preconditions from spec>
7738 -- <preconditions from body>
7740 Process_Preconditions_For (Body_Id);
7742 if Present (Spec_Id) then
7743 Process_Preconditions_For (Spec_Id);
7744 Process_Inherited_Preconditions;
7745 end if;
7746 end Process_Preconditions;
7748 -- Local variables
7750 Restore_Scope : Boolean := False;
7751 Result : Entity_Id;
7752 Stmts : List_Id := No_List;
7753 Subp_Id : Entity_Id;
7755 -- Start of processing for Expand_Subprogram_Contract
7757 begin
7758 -- Obtain the entity of the initial declaration
7760 if Present (Spec_Id) then
7761 Subp_Id := Spec_Id;
7762 else
7763 Subp_Id := Body_Id;
7764 end if;
7766 -- Do not perform expansion activity when it is not needed
7768 if not Expander_Active then
7769 return;
7771 -- ASIS requires an unaltered tree
7773 elsif ASIS_Mode then
7774 return;
7776 -- GNATprove does not need the executable semantics of a contract
7778 elsif GNATprove_Mode then
7779 return;
7781 -- The contract of a generic subprogram or one declared in a generic
7782 -- context is not expanded as the corresponding instance will provide
7783 -- the executable semantics of the contract.
7785 elsif Is_Generic_Subprogram (Subp_Id) or else Inside_A_Generic then
7786 return;
7788 -- All subprograms carry a contract, but for some it is not significant
7789 -- and should not be processed. This is a small optimization.
7791 elsif not Has_Significant_Contract (Subp_Id) then
7792 return;
7793 end if;
7795 -- Do not re-expand the same contract. This scenario occurs when a
7796 -- construct is rewritten into something else during its analysis
7797 -- (expression functions for instance).
7799 if Has_Expanded_Contract (Subp_Id) then
7800 return;
7802 -- Otherwise mark the subprogram
7804 else
7805 Set_Has_Expanded_Contract (Subp_Id);
7806 end if;
7808 -- Ensure that the formal parameters are visible when expanding all
7809 -- contract items.
7811 if not In_Open_Scopes (Subp_Id) then
7812 Restore_Scope := True;
7813 Push_Scope (Subp_Id);
7815 if Is_Generic_Subprogram (Subp_Id) then
7816 Install_Generic_Formals (Subp_Id);
7817 else
7818 Install_Formals (Subp_Id);
7819 end if;
7820 end if;
7822 -- The expansion of a subprogram contract involves the creation of Check
7823 -- pragmas to verify the contract assertions of the spec and body in a
7824 -- particular order. The order is as follows:
7826 -- function Example (...) return ... is
7827 -- procedure _Postconditions (...) is
7828 -- begin
7829 -- <refined postconditions from body>
7830 -- <postconditions from body>
7831 -- <postconditions from spec>
7832 -- <inherited postconditions>
7833 -- <contract case consequences>
7834 -- <invariant check of function result>
7835 -- <invariant and predicate checks of parameters>
7836 -- end _Postconditions;
7838 -- <inherited preconditions>
7839 -- <preconditions from spec>
7840 -- <preconditions from body>
7841 -- <contract case conditions>
7843 -- <source declarations>
7844 -- begin
7845 -- <source statements>
7847 -- _Preconditions (Result);
7848 -- return Result;
7849 -- end Example;
7851 -- Routine _Postconditions holds all contract assertions that must be
7852 -- verified on exit from the related subprogram.
7854 -- Step 1: Handle all preconditions. This action must come before the
7855 -- processing of pragma Contract_Cases because the pragma prepends items
7856 -- to the body declarations.
7858 Process_Preconditions;
7860 -- Step 2: Handle all postconditions. This action must come before the
7861 -- processing of pragma Contract_Cases because the pragma appends items
7862 -- to list Stmts.
7864 Process_Postconditions (Stmts);
7866 -- Step 3: Handle pragma Contract_Cases. This action must come before
7867 -- the processing of invariants and predicates because those append
7868 -- items to list Smts.
7870 Process_Contract_Cases (Stmts);
7872 -- Step 4: Apply invariant and predicate checks on a function result and
7873 -- all formals. The resulting checks are accumulated in list Stmts.
7875 Add_Invariant_And_Predicate_Checks (Subp_Id, Stmts, Result);
7877 -- Step 5: Construct procedure _Postconditions
7879 Build_Postconditions_Procedure (Subp_Id, Stmts, Result);
7881 if Restore_Scope then
7882 End_Scope;
7883 end if;
7884 end Expand_Subprogram_Contract;
7886 --------------------------------
7887 -- Is_Build_In_Place_Function --
7888 --------------------------------
7890 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
7891 begin
7892 -- This function is called from Expand_Subtype_From_Expr during
7893 -- semantic analysis, even when expansion is off. In those cases
7894 -- the build_in_place expansion will not take place.
7896 if not Expander_Active then
7897 return False;
7898 end if;
7900 -- For now we test whether E denotes a function or access-to-function
7901 -- type whose result subtype is inherently limited. Later this test
7902 -- may be revised to allow composite nonlimited types. Functions with
7903 -- a foreign convention or whose result type has a foreign convention
7904 -- never qualify.
7906 if Ekind_In (E, E_Function, E_Generic_Function)
7907 or else (Ekind (E) = E_Subprogram_Type
7908 and then Etype (E) /= Standard_Void_Type)
7909 then
7910 -- Note: If the function has a foreign convention, it cannot build
7911 -- its result in place, so you're on your own. On the other hand,
7912 -- if only the return type has a foreign convention, its layout is
7913 -- intended to be compatible with the other language, but the build-
7914 -- in place machinery can ensure that the object is not copied.
7916 if Has_Foreign_Convention (E) then
7917 return False;
7919 -- In Ada 2005 all functions with an inherently limited return type
7920 -- must be handled using a build-in-place profile, including the case
7921 -- of a function with a limited interface result, where the function
7922 -- may return objects of nonlimited descendants.
7924 else
7925 return Is_Limited_View (Etype (E))
7926 and then Ada_Version >= Ada_2005
7927 and then not Debug_Flag_Dot_L;
7928 end if;
7930 else
7931 return False;
7932 end if;
7933 end Is_Build_In_Place_Function;
7935 -------------------------------------
7936 -- Is_Build_In_Place_Function_Call --
7937 -------------------------------------
7939 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
7940 Exp_Node : Node_Id := N;
7941 Function_Id : Entity_Id;
7943 begin
7944 -- Return False if the expander is currently inactive, since awareness
7945 -- of build-in-place treatment is only relevant during expansion. Note
7946 -- that Is_Build_In_Place_Function, which is called as part of this
7947 -- function, is also conditioned this way, but we need to check here as
7948 -- well to avoid blowing up on processing protected calls when expansion
7949 -- is disabled (such as with -gnatc) since those would trip over the
7950 -- raise of Program_Error below.
7952 -- In SPARK mode, build-in-place calls are not expanded, so that we
7953 -- may end up with a call that is neither resolved to an entity, nor
7954 -- an indirect call.
7956 if not Expander_Active then
7957 return False;
7958 end if;
7960 -- Step past qualification or unchecked conversion (the latter can occur
7961 -- in cases of calls to 'Input).
7963 if Nkind_In (Exp_Node, N_Qualified_Expression,
7964 N_Unchecked_Type_Conversion)
7965 then
7966 Exp_Node := Expression (N);
7967 end if;
7969 if Nkind (Exp_Node) /= N_Function_Call then
7970 return False;
7972 else
7973 if Is_Entity_Name (Name (Exp_Node)) then
7974 Function_Id := Entity (Name (Exp_Node));
7976 -- In the case of an explicitly dereferenced call, use the subprogram
7977 -- type generated for the dereference.
7979 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
7980 Function_Id := Etype (Name (Exp_Node));
7982 -- This may be a call to a protected function.
7984 elsif Nkind (Name (Exp_Node)) = N_Selected_Component then
7985 Function_Id := Etype (Entity (Selector_Name (Name (Exp_Node))));
7987 else
7988 raise Program_Error;
7989 end if;
7991 return Is_Build_In_Place_Function (Function_Id);
7992 end if;
7993 end Is_Build_In_Place_Function_Call;
7995 -----------------------
7996 -- Freeze_Subprogram --
7997 -----------------------
7999 procedure Freeze_Subprogram (N : Node_Id) is
8000 Loc : constant Source_Ptr := Sloc (N);
8002 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
8003 -- (Ada 2005): Register a predefined primitive in all the secondary
8004 -- dispatch tables of its primitive type.
8006 ----------------------------------
8007 -- Register_Predefined_DT_Entry --
8008 ----------------------------------
8010 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
8011 Iface_DT_Ptr : Elmt_Id;
8012 Tagged_Typ : Entity_Id;
8013 Thunk_Id : Entity_Id;
8014 Thunk_Code : Node_Id;
8016 begin
8017 Tagged_Typ := Find_Dispatching_Type (Prim);
8019 if No (Access_Disp_Table (Tagged_Typ))
8020 or else not Has_Interfaces (Tagged_Typ)
8021 or else not RTE_Available (RE_Interface_Tag)
8022 or else Restriction_Active (No_Dispatching_Calls)
8023 then
8024 return;
8025 end if;
8027 -- Skip the first two access-to-dispatch-table pointers since they
8028 -- leads to the primary dispatch table (predefined DT and user
8029 -- defined DT). We are only concerned with the secondary dispatch
8030 -- table pointers. Note that the access-to- dispatch-table pointer
8031 -- corresponds to the first implemented interface retrieved below.
8033 Iface_DT_Ptr :=
8034 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
8036 while Present (Iface_DT_Ptr)
8037 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
8038 loop
8039 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8040 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
8042 if Present (Thunk_Code) then
8043 Insert_Actions_After (N, New_List (
8044 Thunk_Code,
8046 Build_Set_Predefined_Prim_Op_Address (Loc,
8047 Tag_Node =>
8048 New_Occurrence_Of (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
8049 Position => DT_Position (Prim),
8050 Address_Node =>
8051 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8052 Make_Attribute_Reference (Loc,
8053 Prefix => New_Occurrence_Of (Thunk_Id, Loc),
8054 Attribute_Name => Name_Unrestricted_Access))),
8056 Build_Set_Predefined_Prim_Op_Address (Loc,
8057 Tag_Node =>
8058 New_Occurrence_Of
8059 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
8060 Loc),
8061 Position => DT_Position (Prim),
8062 Address_Node =>
8063 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
8064 Make_Attribute_Reference (Loc,
8065 Prefix => New_Occurrence_Of (Prim, Loc),
8066 Attribute_Name => Name_Unrestricted_Access)))));
8067 end if;
8069 -- Skip the tag of the predefined primitives dispatch table
8071 Next_Elmt (Iface_DT_Ptr);
8072 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
8074 -- Skip tag of the no-thunks dispatch table
8076 Next_Elmt (Iface_DT_Ptr);
8077 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8079 -- Skip tag of predefined primitives no-thunks dispatch table
8081 Next_Elmt (Iface_DT_Ptr);
8082 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
8084 Next_Elmt (Iface_DT_Ptr);
8085 end loop;
8086 end Register_Predefined_DT_Entry;
8088 -- Local variables
8090 Subp : constant Entity_Id := Entity (N);
8092 -- Start of processing for Freeze_Subprogram
8094 begin
8095 -- We suppress the initialization of the dispatch table entry when
8096 -- VM_Target because the dispatching mechanism is handled internally
8097 -- by the VM.
8099 if Is_Dispatching_Operation (Subp)
8100 and then not Is_Abstract_Subprogram (Subp)
8101 and then Present (DTC_Entity (Subp))
8102 and then Present (Scope (DTC_Entity (Subp)))
8103 and then Tagged_Type_Expansion
8104 and then not Restriction_Active (No_Dispatching_Calls)
8105 and then RTE_Available (RE_Tag)
8106 then
8107 declare
8108 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
8110 begin
8111 -- Handle private overridden primitives
8113 if not Is_CPP_Class (Typ) then
8114 Check_Overriding_Operation (Subp);
8115 end if;
8117 -- We assume that imported CPP primitives correspond with objects
8118 -- whose constructor is in the CPP side; therefore we don't need
8119 -- to generate code to register them in the dispatch table.
8121 if Is_CPP_Class (Typ) then
8122 null;
8124 -- Handle CPP primitives found in derivations of CPP_Class types.
8125 -- These primitives must have been inherited from some parent, and
8126 -- there is no need to register them in the dispatch table because
8127 -- Build_Inherit_Prims takes care of initializing these slots.
8129 elsif Is_Imported (Subp)
8130 and then (Convention (Subp) = Convention_CPP
8131 or else Convention (Subp) = Convention_C)
8132 then
8133 null;
8135 -- Generate code to register the primitive in non statically
8136 -- allocated dispatch tables
8138 elsif not Building_Static_DT (Scope (DTC_Entity (Subp))) then
8140 -- When a primitive is frozen, enter its name in its dispatch
8141 -- table slot.
8143 if not Is_Interface (Typ)
8144 or else Present (Interface_Alias (Subp))
8145 then
8146 if Is_Predefined_Dispatching_Operation (Subp) then
8147 Register_Predefined_DT_Entry (Subp);
8148 end if;
8150 Insert_Actions_After (N,
8151 Register_Primitive (Loc, Prim => Subp));
8152 end if;
8153 end if;
8154 end;
8155 end if;
8157 -- Mark functions that return by reference. Note that it cannot be part
8158 -- of the normal semantic analysis of the spec since the underlying
8159 -- returned type may not be known yet (for private types).
8161 declare
8162 Typ : constant Entity_Id := Etype (Subp);
8163 Utyp : constant Entity_Id := Underlying_Type (Typ);
8164 begin
8165 if Is_Limited_View (Typ) then
8166 Set_Returns_By_Ref (Subp);
8167 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
8168 Set_Returns_By_Ref (Subp);
8169 end if;
8170 end;
8172 -- Wnen freezing a null procedure, analyze its delayed aspects now
8173 -- because we may not have reached the end of the declarative list when
8174 -- delayed aspects are normally analyzed. This ensures that dispatching
8175 -- calls are properly rewritten when the generated _Postcondition
8176 -- procedure is analyzed in the null procedure body.
8178 if Nkind (Parent (Subp)) = N_Procedure_Specification
8179 and then Null_Present (Parent (Subp))
8180 then
8181 Analyze_Subprogram_Contract (Subp);
8182 end if;
8183 end Freeze_Subprogram;
8185 -----------------------
8186 -- Is_Null_Procedure --
8187 -----------------------
8189 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
8190 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
8192 begin
8193 if Ekind (Subp) /= E_Procedure then
8194 return False;
8196 -- Check if this is a declared null procedure
8198 elsif Nkind (Decl) = N_Subprogram_Declaration then
8199 if not Null_Present (Specification (Decl)) then
8200 return False;
8202 elsif No (Body_To_Inline (Decl)) then
8203 return False;
8205 -- Check if the body contains only a null statement, followed by
8206 -- the return statement added during expansion.
8208 else
8209 declare
8210 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
8212 Stat : Node_Id;
8213 Stat2 : Node_Id;
8215 begin
8216 if Nkind (Orig_Bod) /= N_Subprogram_Body then
8217 return False;
8218 else
8219 -- We must skip SCIL nodes because they are currently
8220 -- implemented as special N_Null_Statement nodes.
8222 Stat :=
8223 First_Non_SCIL_Node
8224 (Statements (Handled_Statement_Sequence (Orig_Bod)));
8225 Stat2 := Next_Non_SCIL_Node (Stat);
8227 return
8228 Is_Empty_List (Declarations (Orig_Bod))
8229 and then Nkind (Stat) = N_Null_Statement
8230 and then
8231 (No (Stat2)
8232 or else
8233 (Nkind (Stat2) = N_Simple_Return_Statement
8234 and then No (Next (Stat2))));
8235 end if;
8236 end;
8237 end if;
8239 else
8240 return False;
8241 end if;
8242 end Is_Null_Procedure;
8244 -------------------------------------------
8245 -- Make_Build_In_Place_Call_In_Allocator --
8246 -------------------------------------------
8248 procedure Make_Build_In_Place_Call_In_Allocator
8249 (Allocator : Node_Id;
8250 Function_Call : Node_Id)
8252 Acc_Type : constant Entity_Id := Etype (Allocator);
8253 Loc : Source_Ptr;
8254 Func_Call : Node_Id := Function_Call;
8255 Ref_Func_Call : Node_Id;
8256 Function_Id : Entity_Id;
8257 Result_Subt : Entity_Id;
8258 New_Allocator : Node_Id;
8259 Return_Obj_Access : Entity_Id; -- temp for function result
8260 Temp_Init : Node_Id; -- initial value of Return_Obj_Access
8261 Alloc_Form : BIP_Allocation_Form;
8262 Pool : Node_Id; -- nonnull if Alloc_Form = User_Storage_Pool
8263 Return_Obj_Actual : Node_Id; -- the temp.all, in caller-allocates case
8264 Chain : Entity_Id; -- activation chain, in case of tasks
8266 begin
8267 -- Step past qualification or unchecked conversion (the latter can occur
8268 -- in cases of calls to 'Input).
8270 if Nkind_In (Func_Call,
8271 N_Qualified_Expression,
8272 N_Unchecked_Type_Conversion)
8273 then
8274 Func_Call := Expression (Func_Call);
8275 end if;
8277 -- If the call has already been processed to add build-in-place actuals
8278 -- then return. This should not normally occur in an allocator context,
8279 -- but we add the protection as a defensive measure.
8281 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8282 return;
8283 end if;
8285 -- Mark the call as processed as a build-in-place call
8287 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8289 Loc := Sloc (Function_Call);
8291 if Is_Entity_Name (Name (Func_Call)) then
8292 Function_Id := Entity (Name (Func_Call));
8294 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8295 Function_Id := Etype (Name (Func_Call));
8297 else
8298 raise Program_Error;
8299 end if;
8301 Result_Subt := Available_View (Etype (Function_Id));
8303 -- Create a temp for the function result. In the caller-allocates case,
8304 -- this will be initialized to the result of a new uninitialized
8305 -- allocator. Note: we do not use Allocator as the Related_Node of
8306 -- Return_Obj_Access in call to Make_Temporary below as this would
8307 -- create a sort of infinite "recursion".
8309 Return_Obj_Access := Make_Temporary (Loc, 'R');
8310 Set_Etype (Return_Obj_Access, Acc_Type);
8312 -- When the result subtype is constrained, the return object is
8313 -- allocated on the caller side, and access to it is passed to the
8314 -- function.
8316 -- Here and in related routines, we must examine the full view of the
8317 -- type, because the view at the point of call may differ from that
8318 -- that in the function body, and the expansion mechanism depends on
8319 -- the characteristics of the full view.
8321 if Is_Constrained (Underlying_Type (Result_Subt)) then
8323 -- Replace the initialized allocator of form "new T'(Func (...))"
8324 -- with an uninitialized allocator of form "new T", where T is the
8325 -- result subtype of the called function. The call to the function
8326 -- is handled separately further below.
8328 New_Allocator :=
8329 Make_Allocator (Loc,
8330 Expression => New_Occurrence_Of (Result_Subt, Loc));
8331 Set_No_Initialization (New_Allocator);
8333 -- Copy attributes to new allocator. Note that the new allocator
8334 -- logically comes from source if the original one did, so copy the
8335 -- relevant flag. This ensures proper treatment of the restriction
8336 -- No_Implicit_Heap_Allocations in this case.
8338 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
8339 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
8340 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
8342 Rewrite (Allocator, New_Allocator);
8344 -- Initial value of the temp is the result of the uninitialized
8345 -- allocator
8347 Temp_Init := Relocate_Node (Allocator);
8349 -- Indicate that caller allocates, and pass in the return object
8351 Alloc_Form := Caller_Allocation;
8352 Pool := Make_Null (No_Location);
8353 Return_Obj_Actual :=
8354 Make_Unchecked_Type_Conversion (Loc,
8355 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8356 Expression =>
8357 Make_Explicit_Dereference (Loc,
8358 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)));
8360 -- When the result subtype is unconstrained, the function itself must
8361 -- perform the allocation of the return object, so we pass parameters
8362 -- indicating that.
8364 else
8365 Temp_Init := Empty;
8367 -- Case of a user-defined storage pool. Pass an allocation parameter
8368 -- indicating that the function should allocate its result in the
8369 -- pool, and pass the pool. Use 'Unrestricted_Access because the
8370 -- pool may not be aliased.
8372 if VM_Target = No_VM
8373 and then Present (Associated_Storage_Pool (Acc_Type))
8374 then
8375 Alloc_Form := User_Storage_Pool;
8376 Pool :=
8377 Make_Attribute_Reference (Loc,
8378 Prefix =>
8379 New_Occurrence_Of
8380 (Associated_Storage_Pool (Acc_Type), Loc),
8381 Attribute_Name => Name_Unrestricted_Access);
8383 -- No user-defined pool; pass an allocation parameter indicating that
8384 -- the function should allocate its result on the heap.
8386 else
8387 Alloc_Form := Global_Heap;
8388 Pool := Make_Null (No_Location);
8389 end if;
8391 -- The caller does not provide the return object in this case, so we
8392 -- have to pass null for the object access actual.
8394 Return_Obj_Actual := Empty;
8395 end if;
8397 -- Declare the temp object
8399 Insert_Action (Allocator,
8400 Make_Object_Declaration (Loc,
8401 Defining_Identifier => Return_Obj_Access,
8402 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
8403 Expression => Temp_Init));
8405 Ref_Func_Call := Make_Reference (Loc, Func_Call);
8407 -- Ada 2005 (AI-251): If the type of the allocator is an interface
8408 -- then generate an implicit conversion to force displacement of the
8409 -- "this" pointer.
8411 if Is_Interface (Designated_Type (Acc_Type)) then
8412 Rewrite
8413 (Ref_Func_Call,
8414 OK_Convert_To (Acc_Type, Ref_Func_Call));
8415 end if;
8417 declare
8418 Assign : constant Node_Id :=
8419 Make_Assignment_Statement (Loc,
8420 Name => New_Occurrence_Of (Return_Obj_Access, Loc),
8421 Expression => Ref_Func_Call);
8422 -- Assign the result of the function call into the temp. In the
8423 -- caller-allocates case, this is overwriting the temp with its
8424 -- initial value, which has no effect. In the callee-allocates case,
8425 -- this is setting the temp to point to the object allocated by the
8426 -- callee.
8428 Actions : List_Id;
8429 -- Actions to be inserted. If there are no tasks, this is just the
8430 -- assignment statement. If the allocated object has tasks, we need
8431 -- to wrap the assignment in a block that activates them. The
8432 -- activation chain of that block must be passed to the function,
8433 -- rather than some outer chain.
8434 begin
8435 if Has_Task (Result_Subt) then
8436 Actions := New_List;
8437 Build_Task_Allocate_Block_With_Init_Stmts
8438 (Actions, Allocator, Init_Stmts => New_List (Assign));
8439 Chain := Activation_Chain_Entity (Last (Actions));
8440 else
8441 Actions := New_List (Assign);
8442 Chain := Empty;
8443 end if;
8445 Insert_Actions (Allocator, Actions);
8446 end;
8448 -- When the function has a controlling result, an allocation-form
8449 -- parameter must be passed indicating that the caller is allocating
8450 -- the result object. This is needed because such a function can be
8451 -- called as a dispatching operation and must be treated similarly
8452 -- to functions with unconstrained result subtypes.
8454 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8455 (Func_Call, Function_Id, Alloc_Form, Pool_Actual => Pool);
8457 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8458 (Func_Call, Function_Id, Acc_Type);
8460 Add_Task_Actuals_To_Build_In_Place_Call
8461 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type),
8462 Chain => Chain);
8464 -- Add an implicit actual to the function call that provides access
8465 -- to the allocated object. An unchecked conversion to the (specific)
8466 -- result subtype of the function is inserted to handle cases where
8467 -- the access type of the allocator has a class-wide designated type.
8469 Add_Access_Actual_To_Build_In_Place_Call
8470 (Func_Call, Function_Id, Return_Obj_Actual);
8472 -- Finally, replace the allocator node with a reference to the temp
8474 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
8476 Analyze_And_Resolve (Allocator, Acc_Type);
8477 end Make_Build_In_Place_Call_In_Allocator;
8479 ---------------------------------------------------
8480 -- Make_Build_In_Place_Call_In_Anonymous_Context --
8481 ---------------------------------------------------
8483 procedure Make_Build_In_Place_Call_In_Anonymous_Context
8484 (Function_Call : Node_Id)
8486 Loc : Source_Ptr;
8487 Func_Call : Node_Id := Function_Call;
8488 Function_Id : Entity_Id;
8489 Result_Subt : Entity_Id;
8490 Return_Obj_Id : Entity_Id;
8491 Return_Obj_Decl : Entity_Id;
8493 begin
8494 -- Step past qualification or unchecked conversion (the latter can occur
8495 -- in cases of calls to 'Input).
8497 if Nkind_In (Func_Call, N_Qualified_Expression,
8498 N_Unchecked_Type_Conversion)
8499 then
8500 Func_Call := Expression (Func_Call);
8501 end if;
8503 -- If the call has already been processed to add build-in-place actuals
8504 -- then return. One place this can occur is for calls to build-in-place
8505 -- functions that occur within a call to a protected operation, where
8506 -- due to rewriting and expansion of the protected call there can be
8507 -- more than one call to Expand_Actuals for the same set of actuals.
8509 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8510 return;
8511 end if;
8513 -- Mark the call as processed as a build-in-place call
8515 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8517 Loc := Sloc (Function_Call);
8519 if Is_Entity_Name (Name (Func_Call)) then
8520 Function_Id := Entity (Name (Func_Call));
8522 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8523 Function_Id := Etype (Name (Func_Call));
8525 else
8526 raise Program_Error;
8527 end if;
8529 Result_Subt := Etype (Function_Id);
8531 -- If the build-in-place function returns a controlled object, then the
8532 -- object needs to be finalized immediately after the context. Since
8533 -- this case produces a transient scope, the servicing finalizer needs
8534 -- to name the returned object. Create a temporary which is initialized
8535 -- with the function call:
8537 -- Temp_Id : Func_Type := BIP_Func_Call;
8539 -- The initialization expression of the temporary will be rewritten by
8540 -- the expander using the appropriate mechanism in Make_Build_In_Place_
8541 -- Call_In_Object_Declaration.
8543 if Needs_Finalization (Result_Subt) then
8544 declare
8545 Temp_Id : constant Entity_Id := Make_Temporary (Loc, 'R');
8546 Temp_Decl : Node_Id;
8548 begin
8549 -- Reset the guard on the function call since the following does
8550 -- not perform actual call expansion.
8552 Set_Is_Expanded_Build_In_Place_Call (Func_Call, False);
8554 Temp_Decl :=
8555 Make_Object_Declaration (Loc,
8556 Defining_Identifier => Temp_Id,
8557 Object_Definition =>
8558 New_Occurrence_Of (Result_Subt, Loc),
8559 Expression =>
8560 New_Copy_Tree (Function_Call));
8562 Insert_Action (Function_Call, Temp_Decl);
8564 Rewrite (Function_Call, New_Occurrence_Of (Temp_Id, Loc));
8565 Analyze (Function_Call);
8566 end;
8568 -- When the result subtype is constrained, an object of the subtype is
8569 -- declared and an access value designating it is passed as an actual.
8571 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8573 -- Create a temporary object to hold the function result
8575 Return_Obj_Id := Make_Temporary (Loc, 'R');
8576 Set_Etype (Return_Obj_Id, Result_Subt);
8578 Return_Obj_Decl :=
8579 Make_Object_Declaration (Loc,
8580 Defining_Identifier => Return_Obj_Id,
8581 Aliased_Present => True,
8582 Object_Definition => New_Occurrence_Of (Result_Subt, Loc));
8584 Set_No_Initialization (Return_Obj_Decl);
8586 Insert_Action (Func_Call, Return_Obj_Decl);
8588 -- When the function has a controlling result, an allocation-form
8589 -- parameter must be passed indicating that the caller is allocating
8590 -- the result object. This is needed because such a function can be
8591 -- called as a dispatching operation and must be treated similarly
8592 -- to functions with unconstrained result subtypes.
8594 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8595 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8597 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8598 (Func_Call, Function_Id);
8600 Add_Task_Actuals_To_Build_In_Place_Call
8601 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8603 -- Add an implicit actual to the function call that provides access
8604 -- to the caller's return object.
8606 Add_Access_Actual_To_Build_In_Place_Call
8607 (Func_Call, Function_Id, New_Occurrence_Of (Return_Obj_Id, Loc));
8609 -- When the result subtype is unconstrained, the function must allocate
8610 -- the return object in the secondary stack, so appropriate implicit
8611 -- parameters are added to the call to indicate that. A transient
8612 -- scope is established to ensure eventual cleanup of the result.
8614 else
8615 -- Pass an allocation parameter indicating that the function should
8616 -- allocate its result on the secondary stack.
8618 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8619 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8621 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8622 (Func_Call, Function_Id);
8624 Add_Task_Actuals_To_Build_In_Place_Call
8625 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
8627 -- Pass a null value to the function since no return object is
8628 -- available on the caller side.
8630 Add_Access_Actual_To_Build_In_Place_Call
8631 (Func_Call, Function_Id, Empty);
8632 end if;
8633 end Make_Build_In_Place_Call_In_Anonymous_Context;
8635 --------------------------------------------
8636 -- Make_Build_In_Place_Call_In_Assignment --
8637 --------------------------------------------
8639 procedure Make_Build_In_Place_Call_In_Assignment
8640 (Assign : Node_Id;
8641 Function_Call : Node_Id)
8643 Lhs : constant Node_Id := Name (Assign);
8644 Func_Call : Node_Id := Function_Call;
8645 Func_Id : Entity_Id;
8646 Loc : Source_Ptr;
8647 Obj_Decl : Node_Id;
8648 Obj_Id : Entity_Id;
8649 Ptr_Typ : Entity_Id;
8650 Ptr_Typ_Decl : Node_Id;
8651 New_Expr : Node_Id;
8652 Result_Subt : Entity_Id;
8653 Target : Node_Id;
8655 begin
8656 -- Step past qualification or unchecked conversion (the latter can occur
8657 -- in cases of calls to 'Input).
8659 if Nkind_In (Func_Call, N_Qualified_Expression,
8660 N_Unchecked_Type_Conversion)
8661 then
8662 Func_Call := Expression (Func_Call);
8663 end if;
8665 -- If the call has already been processed to add build-in-place actuals
8666 -- then return. This should not normally occur in an assignment context,
8667 -- but we add the protection as a defensive measure.
8669 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8670 return;
8671 end if;
8673 -- Mark the call as processed as a build-in-place call
8675 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8677 Loc := Sloc (Function_Call);
8679 if Is_Entity_Name (Name (Func_Call)) then
8680 Func_Id := Entity (Name (Func_Call));
8682 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8683 Func_Id := Etype (Name (Func_Call));
8685 else
8686 raise Program_Error;
8687 end if;
8689 Result_Subt := Etype (Func_Id);
8691 -- When the result subtype is unconstrained, an additional actual must
8692 -- be passed to indicate that the caller is providing the return object.
8693 -- This parameter must also be passed when the called function has a
8694 -- controlling result, because dispatching calls to the function needs
8695 -- to be treated effectively the same as calls to class-wide functions.
8697 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8698 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
8700 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8701 (Func_Call, Func_Id);
8703 Add_Task_Actuals_To_Build_In_Place_Call
8704 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
8706 -- Add an implicit actual to the function call that provides access to
8707 -- the caller's return object.
8709 Add_Access_Actual_To_Build_In_Place_Call
8710 (Func_Call,
8711 Func_Id,
8712 Make_Unchecked_Type_Conversion (Loc,
8713 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8714 Expression => Relocate_Node (Lhs)));
8716 -- Create an access type designating the function's result subtype
8718 Ptr_Typ := Make_Temporary (Loc, 'A');
8720 Ptr_Typ_Decl :=
8721 Make_Full_Type_Declaration (Loc,
8722 Defining_Identifier => Ptr_Typ,
8723 Type_Definition =>
8724 Make_Access_To_Object_Definition (Loc,
8725 All_Present => True,
8726 Subtype_Indication =>
8727 New_Occurrence_Of (Result_Subt, Loc)));
8728 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
8730 -- Finally, create an access object initialized to a reference to the
8731 -- function call. We know this access value is non-null, so mark the
8732 -- entity accordingly to suppress junk access checks.
8734 New_Expr := Make_Reference (Loc, Relocate_Node (Func_Call));
8736 Obj_Id := Make_Temporary (Loc, 'R', New_Expr);
8737 Set_Etype (Obj_Id, Ptr_Typ);
8738 Set_Is_Known_Non_Null (Obj_Id);
8740 Obj_Decl :=
8741 Make_Object_Declaration (Loc,
8742 Defining_Identifier => Obj_Id,
8743 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
8744 Expression => New_Expr);
8745 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
8747 Rewrite (Assign, Make_Null_Statement (Loc));
8749 -- Retrieve the target of the assignment
8751 if Nkind (Lhs) = N_Selected_Component then
8752 Target := Selector_Name (Lhs);
8753 elsif Nkind (Lhs) = N_Type_Conversion then
8754 Target := Expression (Lhs);
8755 else
8756 Target := Lhs;
8757 end if;
8759 -- If we are assigning to a return object or this is an expression of
8760 -- an extension aggregate, the target should either be an identifier
8761 -- or a simple expression. All other cases imply a different scenario.
8763 if Nkind (Target) in N_Has_Entity then
8764 Target := Entity (Target);
8765 else
8766 return;
8767 end if;
8768 end Make_Build_In_Place_Call_In_Assignment;
8770 ----------------------------------------------------
8771 -- Make_Build_In_Place_Call_In_Object_Declaration --
8772 ----------------------------------------------------
8774 procedure Make_Build_In_Place_Call_In_Object_Declaration
8775 (Object_Decl : Node_Id;
8776 Function_Call : Node_Id)
8778 Loc : Source_Ptr;
8779 Obj_Def_Id : constant Entity_Id :=
8780 Defining_Identifier (Object_Decl);
8781 Enclosing_Func : constant Entity_Id :=
8782 Enclosing_Subprogram (Obj_Def_Id);
8783 Call_Deref : Node_Id;
8784 Caller_Object : Node_Id;
8785 Def_Id : Entity_Id;
8786 Fmaster_Actual : Node_Id := Empty;
8787 Func_Call : Node_Id := Function_Call;
8788 Function_Id : Entity_Id;
8789 Pool_Actual : Node_Id;
8790 Ptr_Typ : Entity_Id;
8791 Ptr_Typ_Decl : Node_Id;
8792 Pass_Caller_Acc : Boolean := False;
8793 Res_Decl : Node_Id;
8794 Result_Subt : Entity_Id;
8796 begin
8797 -- Step past qualification or unchecked conversion (the latter can occur
8798 -- in cases of calls to 'Input).
8800 if Nkind_In (Func_Call, N_Qualified_Expression,
8801 N_Unchecked_Type_Conversion)
8802 then
8803 Func_Call := Expression (Func_Call);
8804 end if;
8806 -- If the call has already been processed to add build-in-place actuals
8807 -- then return. This should not normally occur in an object declaration,
8808 -- but we add the protection as a defensive measure.
8810 if Is_Expanded_Build_In_Place_Call (Func_Call) then
8811 return;
8812 end if;
8814 -- Mark the call as processed as a build-in-place call
8816 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
8818 Loc := Sloc (Function_Call);
8820 if Is_Entity_Name (Name (Func_Call)) then
8821 Function_Id := Entity (Name (Func_Call));
8823 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
8824 Function_Id := Etype (Name (Func_Call));
8826 else
8827 raise Program_Error;
8828 end if;
8830 Result_Subt := Etype (Function_Id);
8832 -- Create an access type designating the function's result subtype. We
8833 -- use the type of the original call because it may be a call to an
8834 -- inherited operation, which the expansion has replaced with the parent
8835 -- operation that yields the parent type. Note that this access type
8836 -- must be declared before we establish a transient scope, so that it
8837 -- receives the proper accessibility level.
8839 Ptr_Typ := Make_Temporary (Loc, 'A');
8840 Ptr_Typ_Decl :=
8841 Make_Full_Type_Declaration (Loc,
8842 Defining_Identifier => Ptr_Typ,
8843 Type_Definition =>
8844 Make_Access_To_Object_Definition (Loc,
8845 All_Present => True,
8846 Subtype_Indication =>
8847 New_Occurrence_Of (Etype (Function_Call), Loc)));
8849 -- The access type and its accompanying object must be inserted after
8850 -- the object declaration in the constrained case, so that the function
8851 -- call can be passed access to the object. In the unconstrained case,
8852 -- or if the object declaration is for a return object, the access type
8853 -- and object must be inserted before the object, since the object
8854 -- declaration is rewritten to be a renaming of a dereference of the
8855 -- access object. Note: we need to freeze Ptr_Typ explicitly, because
8856 -- the result object is in a different (transient) scope, so won't
8857 -- cause freezing.
8859 if Is_Constrained (Underlying_Type (Result_Subt))
8860 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
8861 then
8862 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
8863 else
8864 Insert_Action (Object_Decl, Ptr_Typ_Decl);
8865 end if;
8867 -- Force immediate freezing of Ptr_Typ because Res_Decl will be
8868 -- elaborated in an inner (transient) scope and thus won't cause
8869 -- freezing by itself.
8871 declare
8872 Ptr_Typ_Freeze_Ref : constant Node_Id :=
8873 New_Occurrence_Of (Ptr_Typ, Loc);
8874 begin
8875 Set_Parent (Ptr_Typ_Freeze_Ref, Ptr_Typ_Decl);
8876 Freeze_Expression (Ptr_Typ_Freeze_Ref);
8877 end;
8879 -- If the the object is a return object of an enclosing build-in-place
8880 -- function, then the implicit build-in-place parameters of the
8881 -- enclosing function are simply passed along to the called function.
8882 -- (Unfortunately, this won't cover the case of extension aggregates
8883 -- where the ancestor part is a build-in-place unconstrained function
8884 -- call that should be passed along the caller's parameters. Currently
8885 -- those get mishandled by reassigning the result of the call to the
8886 -- aggregate return object, when the call result should really be
8887 -- directly built in place in the aggregate and not in a temporary. ???)
8889 if Is_Return_Object (Defining_Identifier (Object_Decl)) then
8890 Pass_Caller_Acc := True;
8892 -- When the enclosing function has a BIP_Alloc_Form formal then we
8893 -- pass it along to the callee (such as when the enclosing function
8894 -- has an unconstrained or tagged result type).
8896 if Needs_BIP_Alloc_Form (Enclosing_Func) then
8897 if VM_Target = No_VM and then
8898 RTE_Available (RE_Root_Storage_Pool_Ptr)
8899 then
8900 Pool_Actual :=
8901 New_Occurrence_Of (Build_In_Place_Formal
8902 (Enclosing_Func, BIP_Storage_Pool), Loc);
8904 -- The build-in-place pool formal is not built on .NET/JVM
8906 else
8907 Pool_Actual := Empty;
8908 end if;
8910 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8911 (Func_Call,
8912 Function_Id,
8913 Alloc_Form_Exp =>
8914 New_Occurrence_Of
8915 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
8916 Loc),
8917 Pool_Actual => Pool_Actual);
8919 -- Otherwise, if enclosing function has a constrained result subtype,
8920 -- then caller allocation will be used.
8922 else
8923 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8924 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8925 end if;
8927 if Needs_BIP_Finalization_Master (Enclosing_Func) then
8928 Fmaster_Actual :=
8929 New_Occurrence_Of
8930 (Build_In_Place_Formal
8931 (Enclosing_Func, BIP_Finalization_Master), Loc);
8932 end if;
8934 -- Retrieve the BIPacc formal from the enclosing function and convert
8935 -- it to the access type of the callee's BIP_Object_Access formal.
8937 Caller_Object :=
8938 Make_Unchecked_Type_Conversion (Loc,
8939 Subtype_Mark =>
8940 New_Occurrence_Of
8941 (Etype
8942 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
8943 Loc),
8944 Expression =>
8945 New_Occurrence_Of
8946 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
8947 Loc));
8949 -- In the constrained case, add an implicit actual to the function call
8950 -- that provides access to the declared object. An unchecked conversion
8951 -- to the (specific) result type of the function is inserted to handle
8952 -- the case where the object is declared with a class-wide type.
8954 elsif Is_Constrained (Underlying_Type (Result_Subt)) then
8955 Caller_Object :=
8956 Make_Unchecked_Type_Conversion (Loc,
8957 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
8958 Expression => New_Occurrence_Of (Obj_Def_Id, Loc));
8960 -- When the function has a controlling result, an allocation-form
8961 -- parameter must be passed indicating that the caller is allocating
8962 -- the result object. This is needed because such a function can be
8963 -- called as a dispatching operation and must be treated similarly
8964 -- to functions with unconstrained result subtypes.
8966 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8967 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
8969 -- In other unconstrained cases, pass an indication to do the allocation
8970 -- on the secondary stack and set Caller_Object to Empty so that a null
8971 -- value will be passed for the caller's object address. A transient
8972 -- scope is established to ensure eventual cleanup of the result.
8974 else
8975 Add_Unconstrained_Actuals_To_Build_In_Place_Call
8976 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
8977 Caller_Object := Empty;
8979 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
8980 end if;
8982 -- Pass along any finalization master actual, which is needed in the
8983 -- case where the called function initializes a return object of an
8984 -- enclosing build-in-place function.
8986 Add_Finalization_Master_Actual_To_Build_In_Place_Call
8987 (Func_Call => Func_Call,
8988 Func_Id => Function_Id,
8989 Master_Exp => Fmaster_Actual);
8991 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
8992 and then Has_Task (Result_Subt)
8993 then
8994 -- Here we're passing along the master that was passed in to this
8995 -- function.
8997 Add_Task_Actuals_To_Build_In_Place_Call
8998 (Func_Call, Function_Id,
8999 Master_Actual =>
9000 New_Occurrence_Of (Build_In_Place_Formal
9001 (Enclosing_Func, BIP_Task_Master), Loc));
9003 else
9004 Add_Task_Actuals_To_Build_In_Place_Call
9005 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
9006 end if;
9008 Add_Access_Actual_To_Build_In_Place_Call
9009 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
9011 -- Finally, create an access object initialized to a reference to the
9012 -- function call. We know this access value cannot be null, so mark the
9013 -- entity accordingly to suppress the access check.
9015 Def_Id := Make_Temporary (Loc, 'R', Func_Call);
9016 Set_Etype (Def_Id, Ptr_Typ);
9017 Set_Is_Known_Non_Null (Def_Id);
9019 Res_Decl :=
9020 Make_Object_Declaration (Loc,
9021 Defining_Identifier => Def_Id,
9022 Constant_Present => True,
9023 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc),
9024 Expression =>
9025 Make_Reference (Loc, Relocate_Node (Func_Call)));
9027 Insert_After_And_Analyze (Ptr_Typ_Decl, Res_Decl);
9029 -- If the result subtype of the called function is constrained and
9030 -- is not itself the return expression of an enclosing BIP function,
9031 -- then mark the object as having no initialization.
9033 if Is_Constrained (Underlying_Type (Result_Subt))
9034 and then not Is_Return_Object (Defining_Identifier (Object_Decl))
9035 then
9036 -- The related object declaration is encased in a transient block
9037 -- because the build-in-place function call contains at least one
9038 -- nested function call that produces a controlled transient
9039 -- temporary:
9041 -- Obj : ... := BIP_Func_Call (Ctrl_Func_Call);
9043 -- Since the build-in-place expansion decouples the call from the
9044 -- object declaration, the finalization machinery lacks the context
9045 -- which prompted the generation of the transient block. To resolve
9046 -- this scenario, store the build-in-place call.
9048 if Scope_Is_Transient
9049 and then Node_To_Be_Wrapped = Object_Decl
9050 then
9051 Set_BIP_Initialization_Call (Obj_Def_Id, Res_Decl);
9052 end if;
9054 Set_Expression (Object_Decl, Empty);
9055 Set_No_Initialization (Object_Decl);
9057 -- In case of an unconstrained result subtype, or if the call is the
9058 -- return expression of an enclosing BIP function, rewrite the object
9059 -- declaration as an object renaming where the renamed object is a
9060 -- dereference of <function_Call>'reference:
9062 -- Obj : Subt renames <function_call>'Ref.all;
9064 else
9065 Call_Deref :=
9066 Make_Explicit_Dereference (Loc,
9067 Prefix => New_Occurrence_Of (Def_Id, Loc));
9069 Loc := Sloc (Object_Decl);
9070 Rewrite (Object_Decl,
9071 Make_Object_Renaming_Declaration (Loc,
9072 Defining_Identifier => Make_Temporary (Loc, 'D'),
9073 Access_Definition => Empty,
9074 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
9075 Name => Call_Deref));
9077 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
9079 Analyze (Object_Decl);
9081 -- Replace the internal identifier of the renaming declaration's
9082 -- entity with identifier of the original object entity. We also have
9083 -- to exchange the entities containing their defining identifiers to
9084 -- ensure the correct replacement of the object declaration by the
9085 -- object renaming declaration to avoid homograph conflicts (since
9086 -- the object declaration's defining identifier was already entered
9087 -- in current scope). The Next_Entity links of the two entities also
9088 -- have to be swapped since the entities are part of the return
9089 -- scope's entity list and the list structure would otherwise be
9090 -- corrupted. Finally, the homonym chain must be preserved as well.
9092 declare
9093 Renaming_Def_Id : constant Entity_Id :=
9094 Defining_Identifier (Object_Decl);
9095 Next_Entity_Temp : constant Entity_Id :=
9096 Next_Entity (Renaming_Def_Id);
9097 begin
9098 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
9100 -- Swap next entity links in preparation for exchanging entities
9102 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
9103 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
9104 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
9106 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
9108 -- Preserve source indication of original declaration, so that
9109 -- xref information is properly generated for the right entity.
9111 Preserve_Comes_From_Source
9112 (Object_Decl, Original_Node (Object_Decl));
9114 Preserve_Comes_From_Source
9115 (Obj_Def_Id, Original_Node (Object_Decl));
9117 Set_Comes_From_Source (Renaming_Def_Id, False);
9118 end;
9119 end if;
9121 -- If the object entity has a class-wide Etype, then we need to change
9122 -- it to the result subtype of the function call, because otherwise the
9123 -- object will be class-wide without an explicit initialization and
9124 -- won't be allocated properly by the back end. It seems unclean to make
9125 -- such a revision to the type at this point, and we should try to
9126 -- improve this treatment when build-in-place functions with class-wide
9127 -- results are implemented. ???
9129 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
9130 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
9131 end if;
9132 end Make_Build_In_Place_Call_In_Object_Declaration;
9134 --------------------------------------------
9135 -- Make_CPP_Constructor_Call_In_Allocator --
9136 --------------------------------------------
9138 procedure Make_CPP_Constructor_Call_In_Allocator
9139 (Allocator : Node_Id;
9140 Function_Call : Node_Id)
9142 Loc : constant Source_Ptr := Sloc (Function_Call);
9143 Acc_Type : constant Entity_Id := Etype (Allocator);
9144 Function_Id : constant Entity_Id := Entity (Name (Function_Call));
9145 Result_Subt : constant Entity_Id := Available_View (Etype (Function_Id));
9147 New_Allocator : Node_Id;
9148 Return_Obj_Access : Entity_Id;
9149 Tmp_Obj : Node_Id;
9151 begin
9152 pragma Assert (Nkind (Allocator) = N_Allocator
9153 and then Nkind (Function_Call) = N_Function_Call);
9154 pragma Assert (Convention (Function_Id) = Convention_CPP
9155 and then Is_Constructor (Function_Id));
9156 pragma Assert (Is_Constrained (Underlying_Type (Result_Subt)));
9158 -- Replace the initialized allocator of form "new T'(Func (...))" with
9159 -- an uninitialized allocator of form "new T", where T is the result
9160 -- subtype of the called function. The call to the function is handled
9161 -- separately further below.
9163 New_Allocator :=
9164 Make_Allocator (Loc,
9165 Expression => New_Occurrence_Of (Result_Subt, Loc));
9166 Set_No_Initialization (New_Allocator);
9168 -- Copy attributes to new allocator. Note that the new allocator
9169 -- logically comes from source if the original one did, so copy the
9170 -- relevant flag. This ensures proper treatment of the restriction
9171 -- No_Implicit_Heap_Allocations in this case.
9173 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
9174 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
9175 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
9177 Rewrite (Allocator, New_Allocator);
9179 -- Create a new access object and initialize it to the result of the
9180 -- new uninitialized allocator. Note: we do not use Allocator as the
9181 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
9182 -- as this would create a sort of infinite "recursion".
9184 Return_Obj_Access := Make_Temporary (Loc, 'R');
9185 Set_Etype (Return_Obj_Access, Acc_Type);
9187 -- Generate:
9188 -- Rnnn : constant ptr_T := new (T);
9189 -- Init (Rnn.all,...);
9191 Tmp_Obj :=
9192 Make_Object_Declaration (Loc,
9193 Defining_Identifier => Return_Obj_Access,
9194 Constant_Present => True,
9195 Object_Definition => New_Occurrence_Of (Acc_Type, Loc),
9196 Expression => Relocate_Node (Allocator));
9197 Insert_Action (Allocator, Tmp_Obj);
9199 Insert_List_After_And_Analyze (Tmp_Obj,
9200 Build_Initialization_Call (Loc,
9201 Id_Ref =>
9202 Make_Explicit_Dereference (Loc,
9203 Prefix => New_Occurrence_Of (Return_Obj_Access, Loc)),
9204 Typ => Etype (Function_Id),
9205 Constructor_Ref => Function_Call));
9207 -- Finally, replace the allocator node with a reference to the result of
9208 -- the function call itself (which will effectively be an access to the
9209 -- object created by the allocator).
9211 Rewrite (Allocator, New_Occurrence_Of (Return_Obj_Access, Loc));
9213 -- Ada 2005 (AI-251): If the type of the allocator is an interface then
9214 -- generate an implicit conversion to force displacement of the "this"
9215 -- pointer.
9217 if Is_Interface (Designated_Type (Acc_Type)) then
9218 Rewrite (Allocator, Convert_To (Acc_Type, Relocate_Node (Allocator)));
9219 end if;
9221 Analyze_And_Resolve (Allocator, Acc_Type);
9222 end Make_CPP_Constructor_Call_In_Allocator;
9224 -----------------------------------
9225 -- Needs_BIP_Finalization_Master --
9226 -----------------------------------
9228 function Needs_BIP_Finalization_Master
9229 (Func_Id : Entity_Id) return Boolean
9231 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9232 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9233 begin
9234 return
9235 not Restriction_Active (No_Finalization)
9236 and then Needs_Finalization (Func_Typ);
9237 end Needs_BIP_Finalization_Master;
9239 --------------------------
9240 -- Needs_BIP_Alloc_Form --
9241 --------------------------
9243 function Needs_BIP_Alloc_Form (Func_Id : Entity_Id) return Boolean is
9244 pragma Assert (Is_Build_In_Place_Function (Func_Id));
9245 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9246 begin
9247 return not Is_Constrained (Func_Typ) or else Is_Tagged_Type (Func_Typ);
9248 end Needs_BIP_Alloc_Form;
9250 --------------------------------------
9251 -- Needs_Result_Accessibility_Level --
9252 --------------------------------------
9254 function Needs_Result_Accessibility_Level
9255 (Func_Id : Entity_Id) return Boolean
9257 Func_Typ : constant Entity_Id := Underlying_Type (Etype (Func_Id));
9259 function Has_Unconstrained_Access_Discriminant_Component
9260 (Comp_Typ : Entity_Id) return Boolean;
9261 -- Returns True if any component of the type has an unconstrained access
9262 -- discriminant.
9264 -----------------------------------------------------
9265 -- Has_Unconstrained_Access_Discriminant_Component --
9266 -----------------------------------------------------
9268 function Has_Unconstrained_Access_Discriminant_Component
9269 (Comp_Typ : Entity_Id) return Boolean
9271 begin
9272 if not Is_Limited_Type (Comp_Typ) then
9273 return False;
9275 -- Only limited types can have access discriminants with
9276 -- defaults.
9278 elsif Has_Unconstrained_Access_Discriminants (Comp_Typ) then
9279 return True;
9281 elsif Is_Array_Type (Comp_Typ) then
9282 return Has_Unconstrained_Access_Discriminant_Component
9283 (Underlying_Type (Component_Type (Comp_Typ)));
9285 elsif Is_Record_Type (Comp_Typ) then
9286 declare
9287 Comp : Entity_Id;
9289 begin
9290 Comp := First_Component (Comp_Typ);
9291 while Present (Comp) loop
9292 if Has_Unconstrained_Access_Discriminant_Component
9293 (Underlying_Type (Etype (Comp)))
9294 then
9295 return True;
9296 end if;
9298 Next_Component (Comp);
9299 end loop;
9300 end;
9301 end if;
9303 return False;
9304 end Has_Unconstrained_Access_Discriminant_Component;
9306 Feature_Disabled : constant Boolean := True;
9307 -- Temporary
9309 -- Start of processing for Needs_Result_Accessibility_Level
9311 begin
9312 -- False if completion unavailable (how does this happen???)
9314 if not Present (Func_Typ) then
9315 return False;
9317 elsif Feature_Disabled then
9318 return False;
9320 -- False if not a function, also handle enum-lit renames case
9322 elsif Func_Typ = Standard_Void_Type
9323 or else Is_Scalar_Type (Func_Typ)
9324 then
9325 return False;
9327 -- Handle a corner case, a cross-dialect subp renaming. For example,
9328 -- an Ada 2012 renaming of an Ada 2005 subprogram. This can occur when
9329 -- an Ada 2005 (or earlier) unit references predefined run-time units.
9331 elsif Present (Alias (Func_Id)) then
9333 -- Unimplemented: a cross-dialect subp renaming which does not set
9334 -- the Alias attribute (e.g., a rename of a dereference of an access
9335 -- to subprogram value). ???
9337 return Present (Extra_Accessibility_Of_Result (Alias (Func_Id)));
9339 -- Remaining cases require Ada 2012 mode
9341 elsif Ada_Version < Ada_2012 then
9342 return False;
9344 elsif Ekind (Func_Typ) = E_Anonymous_Access_Type
9345 or else Is_Tagged_Type (Func_Typ)
9346 then
9347 -- In the case of, say, a null tagged record result type, the need
9348 -- for this extra parameter might not be obvious. This function
9349 -- returns True for all tagged types for compatibility reasons.
9350 -- A function with, say, a tagged null controlling result type might
9351 -- be overridden by a primitive of an extension having an access
9352 -- discriminant and the overrider and overridden must have compatible
9353 -- calling conventions (including implicitly declared parameters).
9354 -- Similarly, values of one access-to-subprogram type might designate
9355 -- both a primitive subprogram of a given type and a function
9356 -- which is, for example, not a primitive subprogram of any type.
9357 -- Again, this requires calling convention compatibility.
9358 -- It might be possible to solve these issues by introducing
9359 -- wrappers, but that is not the approach that was chosen.
9361 return True;
9363 elsif Has_Unconstrained_Access_Discriminants (Func_Typ) then
9364 return True;
9366 elsif Has_Unconstrained_Access_Discriminant_Component (Func_Typ) then
9367 return True;
9369 -- False for all other cases
9371 else
9372 return False;
9373 end if;
9374 end Needs_Result_Accessibility_Level;
9376 end Exp_Ch6;