2010-07-22 Andi Kleen <ak@linux.intel.com>
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob9ddb278417c23f07dea5752a337a622cf0f2adc3
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-2010, 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_Atag; use Exp_Atag;
33 with Exp_Ch2; use Exp_Ch2;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Intr; use Exp_Intr;
41 with Exp_Pakd; use Exp_Pakd;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Exp_VFpt; use Exp_VFpt;
45 with Fname; use Fname;
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_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
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 Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Validsw; use Validsw;
76 package body Exp_Ch6 is
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 procedure Add_Access_Actual_To_Build_In_Place_Call
83 (Function_Call : Node_Id;
84 Function_Id : Entity_Id;
85 Return_Object : Node_Id;
86 Is_Access : Boolean := False);
87 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
88 -- object name given by Return_Object and add the attribute to the end of
89 -- the actual parameter list associated with the build-in-place function
90 -- call denoted by Function_Call. However, if Is_Access is True, then
91 -- Return_Object is already an access expression, in which case it's passed
92 -- along directly to the build-in-place function. Finally, if Return_Object
93 -- is empty, then pass a null literal as the actual.
95 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
96 (Function_Call : Node_Id;
97 Function_Id : Entity_Id;
98 Alloc_Form : BIP_Allocation_Form := Unspecified;
99 Alloc_Form_Exp : Node_Id := Empty);
100 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
101 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
102 -- present, then use it, otherwise pass a literal corresponding to the
103 -- Alloc_Form parameter (which must not be Unspecified in that case).
105 procedure Add_Extra_Actual_To_Call
106 (Subprogram_Call : Node_Id;
107 Extra_Formal : Entity_Id;
108 Extra_Actual : Node_Id);
109 -- Adds Extra_Actual as a named parameter association for the formal
110 -- Extra_Formal in Subprogram_Call.
112 procedure Add_Final_List_Actual_To_Build_In_Place_Call
113 (Function_Call : Node_Id;
114 Function_Id : Entity_Id;
115 Acc_Type : Entity_Id;
116 Sel_Comp : Node_Id := Empty);
117 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type has
118 -- controlled parts, add an actual parameter that is a pointer to
119 -- appropriate finalization list. The finalization list is that of the
120 -- current scope, except for "new Acc'(F(...))" in which case it's the
121 -- finalization list of the access type returned by the allocator. Acc_Type
122 -- is that type in the allocator case; Empty otherwise. If Sel_Comp is
123 -- not Empty, then it denotes a selected component and the finalization
124 -- list is obtained from the _controller list of the prefix object.
126 procedure Add_Task_Actuals_To_Build_In_Place_Call
127 (Function_Call : Node_Id;
128 Function_Id : Entity_Id;
129 Master_Actual : Node_Id);
130 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
131 -- contains tasks, add two actual parameters: the master, and a pointer to
132 -- the caller's activation chain. Master_Actual is the actual parameter
133 -- expression to pass for the master. In most cases, this is the current
134 -- master (_master). The two exceptions are: If the function call is the
135 -- initialization expression for an allocator, we pass the master of the
136 -- access type. If the function call is the initialization expression for
137 -- a return object, we pass along the master passed in by the caller. The
138 -- activation chain to pass is always the local one.
140 procedure Check_Overriding_Operation (Subp : Entity_Id);
141 -- Subp is a dispatching operation. Check whether it may override an
142 -- inherited private operation, in which case its DT entry is that of
143 -- the hidden operation, not the one it may have received earlier.
144 -- This must be done before emitting the code to set the corresponding
145 -- DT to the address of the subprogram. The actual placement of Subp in
146 -- the proper place in the list of primitive operations is done in
147 -- Declare_Inherited_Private_Subprograms, which also has to deal with
148 -- implicit operations. This duplication is unavoidable for now???
150 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
151 -- This procedure is called only if the subprogram body N, whose spec
152 -- has the given entity Spec, contains a parameterless recursive call.
153 -- It attempts to generate runtime code to detect if this a case of
154 -- infinite recursion.
156 -- The body is scanned to determine dependencies. If the only external
157 -- dependencies are on a small set of scalar variables, then the values
158 -- of these variables are captured on entry to the subprogram, and if
159 -- the values are not changed for the call, we know immediately that
160 -- we have an infinite recursion.
162 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
163 -- For each actual of an in-out or out parameter which is a numeric
164 -- (view) conversion of the form T (A), where A denotes a variable,
165 -- we insert the declaration:
167 -- Temp : T[ := T (A)];
169 -- prior to the call. Then we replace the actual with a reference to Temp,
170 -- and append the assignment:
172 -- A := TypeA (Temp);
174 -- after the call. Here TypeA is the actual type of variable A. For out
175 -- parameters, the initial declaration has no expression. If A is not an
176 -- entity name, we generate instead:
178 -- Var : TypeA renames A;
179 -- Temp : T := Var; -- omitting expression for out parameter.
180 -- ...
181 -- Var := TypeA (Temp);
183 -- For other in-out parameters, we emit the required constraint checks
184 -- before and/or after the call.
186 -- For all parameter modes, actuals that denote components and slices of
187 -- packed arrays are expanded into suitable temporaries.
189 -- For non-scalar objects that are possibly unaligned, add call by copy
190 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
192 procedure Expand_Inlined_Call
193 (N : Node_Id;
194 Subp : Entity_Id;
195 Orig_Subp : Entity_Id);
196 -- If called subprogram can be inlined by the front-end, retrieve the
197 -- analyzed body, replace formals with actuals and expand call in place.
198 -- Generate thunks for actuals that are expressions, and insert the
199 -- corresponding constant declarations before the call. If the original
200 -- call is to a derived operation, the return type is the one of the
201 -- derived operation, but the body is that of the original, so return
202 -- expressions in the body must be converted to the desired type (which
203 -- is simply not noted in the tree without inline expansion).
205 function Expand_Protected_Object_Reference
206 (N : Node_Id;
207 Scop : Entity_Id) return Node_Id;
209 procedure Expand_Protected_Subprogram_Call
210 (N : Node_Id;
211 Subp : Entity_Id;
212 Scop : Entity_Id);
213 -- A call to a protected subprogram within the protected object may appear
214 -- as a regular call. The list of actuals must be expanded to contain a
215 -- reference to the object itself, and the call becomes a call to the
216 -- corresponding protected subprogram.
218 function Is_Null_Procedure (Subp : Entity_Id) return Boolean;
219 -- Predicate to recognize stubbed procedures and null procedures, which
220 -- can be inlined unconditionally in all cases.
222 ----------------------------------------------
223 -- Add_Access_Actual_To_Build_In_Place_Call --
224 ----------------------------------------------
226 procedure Add_Access_Actual_To_Build_In_Place_Call
227 (Function_Call : Node_Id;
228 Function_Id : Entity_Id;
229 Return_Object : Node_Id;
230 Is_Access : Boolean := False)
232 Loc : constant Source_Ptr := Sloc (Function_Call);
233 Obj_Address : Node_Id;
234 Obj_Acc_Formal : Entity_Id;
236 begin
237 -- Locate the implicit access parameter in the called function
239 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
241 -- If no return object is provided, then pass null
243 if not Present (Return_Object) then
244 Obj_Address := Make_Null (Loc);
245 Set_Parent (Obj_Address, Function_Call);
247 -- If Return_Object is already an expression of an access type, then use
248 -- it directly, since it must be an access value denoting the return
249 -- object, and couldn't possibly be the return object itself.
251 elsif Is_Access then
252 Obj_Address := Return_Object;
253 Set_Parent (Obj_Address, Function_Call);
255 -- Apply Unrestricted_Access to caller's return object
257 else
258 Obj_Address :=
259 Make_Attribute_Reference (Loc,
260 Prefix => Return_Object,
261 Attribute_Name => Name_Unrestricted_Access);
263 Set_Parent (Return_Object, Obj_Address);
264 Set_Parent (Obj_Address, Function_Call);
265 end if;
267 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
269 -- Build the parameter association for the new actual and add it to the
270 -- end of the function's actuals.
272 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
273 end Add_Access_Actual_To_Build_In_Place_Call;
275 --------------------------------------------------
276 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
277 --------------------------------------------------
279 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
280 (Function_Call : Node_Id;
281 Function_Id : Entity_Id;
282 Alloc_Form : BIP_Allocation_Form := Unspecified;
283 Alloc_Form_Exp : Node_Id := Empty)
285 Loc : constant Source_Ptr := Sloc (Function_Call);
286 Alloc_Form_Actual : Node_Id;
287 Alloc_Form_Formal : Node_Id;
289 begin
290 -- The allocation form generally doesn't need to be passed in the case
291 -- of a constrained result subtype, since normally the caller performs
292 -- the allocation in that case. However this formal is still needed in
293 -- the case where the function has a tagged result, because generally
294 -- such functions can be called in a dispatching context and such calls
295 -- must be handled like calls to class-wide functions.
297 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
298 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
299 then
300 return;
301 end if;
303 -- Locate the implicit allocation form parameter in the called function.
304 -- Maybe it would be better for each implicit formal of a build-in-place
305 -- function to have a flag or a Uint attribute to identify it. ???
307 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
309 if Present (Alloc_Form_Exp) then
310 pragma Assert (Alloc_Form = Unspecified);
312 Alloc_Form_Actual := Alloc_Form_Exp;
314 else
315 pragma Assert (Alloc_Form /= Unspecified);
317 Alloc_Form_Actual :=
318 Make_Integer_Literal (Loc,
319 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
320 end if;
322 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
324 -- Build the parameter association for the new actual and add it to the
325 -- end of the function's actuals.
327 Add_Extra_Actual_To_Call
328 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
329 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
331 ------------------------------
332 -- Add_Extra_Actual_To_Call --
333 ------------------------------
335 procedure Add_Extra_Actual_To_Call
336 (Subprogram_Call : Node_Id;
337 Extra_Formal : Entity_Id;
338 Extra_Actual : Node_Id)
340 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
341 Param_Assoc : Node_Id;
343 begin
344 Param_Assoc :=
345 Make_Parameter_Association (Loc,
346 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
347 Explicit_Actual_Parameter => Extra_Actual);
349 Set_Parent (Param_Assoc, Subprogram_Call);
350 Set_Parent (Extra_Actual, Param_Assoc);
352 if Present (Parameter_Associations (Subprogram_Call)) then
353 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
354 N_Parameter_Association
355 then
357 -- Find last named actual, and append
359 declare
360 L : Node_Id;
361 begin
362 L := First_Actual (Subprogram_Call);
363 while Present (L) loop
364 if No (Next_Actual (L)) then
365 Set_Next_Named_Actual (Parent (L), Extra_Actual);
366 exit;
367 end if;
368 Next_Actual (L);
369 end loop;
370 end;
372 else
373 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
374 end if;
376 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
378 else
379 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
380 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
381 end if;
382 end Add_Extra_Actual_To_Call;
384 --------------------------------------------------
385 -- Add_Final_List_Actual_To_Build_In_Place_Call --
386 --------------------------------------------------
388 procedure Add_Final_List_Actual_To_Build_In_Place_Call
389 (Function_Call : Node_Id;
390 Function_Id : Entity_Id;
391 Acc_Type : Entity_Id;
392 Sel_Comp : Node_Id := Empty)
394 Loc : constant Source_Ptr := Sloc (Function_Call);
395 Final_List : Node_Id;
396 Final_List_Actual : Node_Id;
397 Final_List_Formal : Node_Id;
398 Is_Ctrl_Result : constant Boolean :=
399 Needs_Finalization
400 (Underlying_Type (Etype (Function_Id)));
402 begin
403 -- No such extra parameter is needed if there are no controlled parts.
404 -- The test for Needs_Finalization accounts for class-wide results
405 -- (which potentially have controlled parts, even if the root type
406 -- doesn't), and the test for a tagged result type is needed because
407 -- calls to such a function can in general occur in dispatching
408 -- contexts, which must be treated the same as a call to class-wide
409 -- functions. Both of these situations require that a finalization list
410 -- be passed.
412 if not Needs_BIP_Final_List (Function_Id) then
413 return;
414 end if;
416 -- Locate implicit finalization list parameter in the called function
418 Final_List_Formal := Build_In_Place_Formal (Function_Id, BIP_Final_List);
420 -- Create the actual which is a pointer to the appropriate finalization
421 -- list. Acc_Type is present if and only if this call is the
422 -- initialization of an allocator. Use the Current_Scope or the
423 -- Acc_Type as appropriate.
425 if Present (Acc_Type)
426 and then (Ekind (Acc_Type) = E_Anonymous_Access_Type
427 or else
428 Present (Associated_Final_Chain (Base_Type (Acc_Type))))
429 then
430 Final_List := Find_Final_List (Acc_Type);
432 -- If Sel_Comp is present and the function result is controlled, then
433 -- the finalization list will be obtained from the _controller list of
434 -- the selected component's prefix object.
436 elsif Present (Sel_Comp) and then Is_Ctrl_Result then
437 Final_List := Find_Final_List (Current_Scope, Sel_Comp);
439 else
440 Final_List := Find_Final_List (Current_Scope);
441 end if;
443 Final_List_Actual :=
444 Make_Attribute_Reference (Loc,
445 Prefix => Final_List,
446 Attribute_Name => Name_Unrestricted_Access);
448 Analyze_And_Resolve (Final_List_Actual, Etype (Final_List_Formal));
450 -- Build the parameter association for the new actual and add it to the
451 -- end of the function's actuals.
453 Add_Extra_Actual_To_Call
454 (Function_Call, Final_List_Formal, Final_List_Actual);
455 end Add_Final_List_Actual_To_Build_In_Place_Call;
457 ---------------------------------------------
458 -- Add_Task_Actuals_To_Build_In_Place_Call --
459 ---------------------------------------------
461 procedure Add_Task_Actuals_To_Build_In_Place_Call
462 (Function_Call : Node_Id;
463 Function_Id : Entity_Id;
464 Master_Actual : Node_Id)
465 -- Note: Master_Actual can be Empty, but only if there are no tasks
467 Loc : constant Source_Ptr := Sloc (Function_Call);
469 begin
470 -- No such extra parameters are needed if there are no tasks
472 if not Has_Task (Etype (Function_Id)) then
473 return;
474 end if;
476 -- The master
478 declare
479 Master_Formal : Node_Id;
480 begin
481 -- Locate implicit master parameter in the called function
483 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
485 Analyze_And_Resolve (Master_Actual, Etype (Master_Formal));
487 -- Build the parameter association for the new actual and add it to
488 -- the end of the function's actuals.
490 Add_Extra_Actual_To_Call
491 (Function_Call, Master_Formal, Master_Actual);
492 end;
494 -- The activation chain
496 declare
497 Activation_Chain_Actual : Node_Id;
498 Activation_Chain_Formal : Node_Id;
500 begin
501 -- Locate implicit activation chain parameter in the called function
503 Activation_Chain_Formal := Build_In_Place_Formal
504 (Function_Id, BIP_Activation_Chain);
506 -- Create the actual which is a pointer to the current activation
507 -- chain
509 Activation_Chain_Actual :=
510 Make_Attribute_Reference (Loc,
511 Prefix => Make_Identifier (Loc, Name_uChain),
512 Attribute_Name => Name_Unrestricted_Access);
514 Analyze_And_Resolve
515 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
517 -- Build the parameter association for the new actual and add it to
518 -- the end of the function's actuals.
520 Add_Extra_Actual_To_Call
521 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
522 end;
523 end Add_Task_Actuals_To_Build_In_Place_Call;
525 -----------------------
526 -- BIP_Formal_Suffix --
527 -----------------------
529 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
530 begin
531 case Kind is
532 when BIP_Alloc_Form =>
533 return "BIPalloc";
534 when BIP_Final_List =>
535 return "BIPfinallist";
536 when BIP_Master =>
537 return "BIPmaster";
538 when BIP_Activation_Chain =>
539 return "BIPactivationchain";
540 when BIP_Object_Access =>
541 return "BIPaccess";
542 end case;
543 end BIP_Formal_Suffix;
545 ---------------------------
546 -- Build_In_Place_Formal --
547 ---------------------------
549 function Build_In_Place_Formal
550 (Func : Entity_Id;
551 Kind : BIP_Formal_Kind) return Entity_Id
553 Extra_Formal : Entity_Id := Extra_Formals (Func);
555 begin
556 -- Maybe it would be better for each implicit formal of a build-in-place
557 -- function to have a flag or a Uint attribute to identify it. ???
559 loop
560 pragma Assert (Present (Extra_Formal));
561 exit when
562 Chars (Extra_Formal) =
563 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
564 Next_Formal_With_Extras (Extra_Formal);
565 end loop;
567 return Extra_Formal;
568 end Build_In_Place_Formal;
570 --------------------------------
571 -- Check_Overriding_Operation --
572 --------------------------------
574 procedure Check_Overriding_Operation (Subp : Entity_Id) is
575 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
576 Op_List : constant Elist_Id := Primitive_Operations (Typ);
577 Op_Elmt : Elmt_Id;
578 Prim_Op : Entity_Id;
579 Par_Op : Entity_Id;
581 begin
582 if Is_Derived_Type (Typ)
583 and then not Is_Private_Type (Typ)
584 and then In_Open_Scopes (Scope (Etype (Typ)))
585 and then Typ = Base_Type (Typ)
586 then
587 -- Subp overrides an inherited private operation if there is an
588 -- inherited operation with a different name than Subp (see
589 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
590 -- same name as Subp.
592 Op_Elmt := First_Elmt (Op_List);
593 while Present (Op_Elmt) loop
594 Prim_Op := Node (Op_Elmt);
595 Par_Op := Alias (Prim_Op);
597 if Present (Par_Op)
598 and then not Comes_From_Source (Prim_Op)
599 and then Chars (Prim_Op) /= Chars (Par_Op)
600 and then Chars (Par_Op) = Chars (Subp)
601 and then Is_Hidden (Par_Op)
602 and then Type_Conformant (Prim_Op, Subp)
603 then
604 Set_DT_Position (Subp, DT_Position (Prim_Op));
605 end if;
607 Next_Elmt (Op_Elmt);
608 end loop;
609 end if;
610 end Check_Overriding_Operation;
612 -------------------------------
613 -- Detect_Infinite_Recursion --
614 -------------------------------
616 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
617 Loc : constant Source_Ptr := Sloc (N);
619 Var_List : constant Elist_Id := New_Elmt_List;
620 -- List of globals referenced by body of procedure
622 Call_List : constant Elist_Id := New_Elmt_List;
623 -- List of recursive calls in body of procedure
625 Shad_List : constant Elist_Id := New_Elmt_List;
626 -- List of entity id's for entities created to capture the value of
627 -- referenced globals on entry to the procedure.
629 Scop : constant Uint := Scope_Depth (Spec);
630 -- This is used to record the scope depth of the current procedure, so
631 -- that we can identify global references.
633 Max_Vars : constant := 4;
634 -- Do not test more than four global variables
636 Count_Vars : Natural := 0;
637 -- Count variables found so far
639 Var : Entity_Id;
640 Elm : Elmt_Id;
641 Ent : Entity_Id;
642 Call : Elmt_Id;
643 Decl : Node_Id;
644 Test : Node_Id;
645 Elm1 : Elmt_Id;
646 Elm2 : Elmt_Id;
647 Last : Node_Id;
649 function Process (Nod : Node_Id) return Traverse_Result;
650 -- Function to traverse the subprogram body (using Traverse_Func)
652 -------------
653 -- Process --
654 -------------
656 function Process (Nod : Node_Id) return Traverse_Result is
657 begin
658 -- Procedure call
660 if Nkind (Nod) = N_Procedure_Call_Statement then
662 -- Case of one of the detected recursive calls
664 if Is_Entity_Name (Name (Nod))
665 and then Has_Recursive_Call (Entity (Name (Nod)))
666 and then Entity (Name (Nod)) = Spec
667 then
668 Append_Elmt (Nod, Call_List);
669 return Skip;
671 -- Any other procedure call may have side effects
673 else
674 return Abandon;
675 end if;
677 -- A call to a pure function can always be ignored
679 elsif Nkind (Nod) = N_Function_Call
680 and then Is_Entity_Name (Name (Nod))
681 and then Is_Pure (Entity (Name (Nod)))
682 then
683 return Skip;
685 -- Case of an identifier reference
687 elsif Nkind (Nod) = N_Identifier then
688 Ent := Entity (Nod);
690 -- If no entity, then ignore the reference
692 -- Not clear why this can happen. To investigate, remove this
693 -- test and look at the crash that occurs here in 3401-004 ???
695 if No (Ent) then
696 return Skip;
698 -- Ignore entities with no Scope, again not clear how this
699 -- can happen, to investigate, look at 4108-008 ???
701 elsif No (Scope (Ent)) then
702 return Skip;
704 -- Ignore the reference if not to a more global object
706 elsif Scope_Depth (Scope (Ent)) >= Scop then
707 return Skip;
709 -- References to types, exceptions and constants are always OK
711 elsif Is_Type (Ent)
712 or else Ekind (Ent) = E_Exception
713 or else Ekind (Ent) = E_Constant
714 then
715 return Skip;
717 -- If other than a non-volatile scalar variable, we have some
718 -- kind of global reference (e.g. to a function) that we cannot
719 -- deal with so we forget the attempt.
721 elsif Ekind (Ent) /= E_Variable
722 or else not Is_Scalar_Type (Etype (Ent))
723 or else Treat_As_Volatile (Ent)
724 then
725 return Abandon;
727 -- Otherwise we have a reference to a global scalar
729 else
730 -- Loop through global entities already detected
732 Elm := First_Elmt (Var_List);
733 loop
734 -- If not detected before, record this new global reference
736 if No (Elm) then
737 Count_Vars := Count_Vars + 1;
739 if Count_Vars <= Max_Vars then
740 Append_Elmt (Entity (Nod), Var_List);
741 else
742 return Abandon;
743 end if;
745 exit;
747 -- If recorded before, ignore
749 elsif Node (Elm) = Entity (Nod) then
750 return Skip;
752 -- Otherwise keep looking
754 else
755 Next_Elmt (Elm);
756 end if;
757 end loop;
759 return Skip;
760 end if;
762 -- For all other node kinds, recursively visit syntactic children
764 else
765 return OK;
766 end if;
767 end Process;
769 function Traverse_Body is new Traverse_Func (Process);
771 -- Start of processing for Detect_Infinite_Recursion
773 begin
774 -- Do not attempt detection in No_Implicit_Conditional mode, since we
775 -- won't be able to generate the code to handle the recursion in any
776 -- case.
778 if Restriction_Active (No_Implicit_Conditionals) then
779 return;
780 end if;
782 -- Otherwise do traversal and quit if we get abandon signal
784 if Traverse_Body (N) = Abandon then
785 return;
787 -- We must have a call, since Has_Recursive_Call was set. If not just
788 -- ignore (this is only an error check, so if we have a funny situation,
789 -- due to bugs or errors, we do not want to bomb!)
791 elsif Is_Empty_Elmt_List (Call_List) then
792 return;
793 end if;
795 -- Here is the case where we detect recursion at compile time
797 -- Push our current scope for analyzing the declarations and code that
798 -- we will insert for the checking.
800 Push_Scope (Spec);
802 -- This loop builds temporary variables for each of the referenced
803 -- globals, so that at the end of the loop the list Shad_List contains
804 -- these temporaries in one-to-one correspondence with the elements in
805 -- Var_List.
807 Last := Empty;
808 Elm := First_Elmt (Var_List);
809 while Present (Elm) loop
810 Var := Node (Elm);
811 Ent := Make_Temporary (Loc, 'S');
812 Append_Elmt (Ent, Shad_List);
814 -- Insert a declaration for this temporary at the start of the
815 -- declarations for the procedure. The temporaries are declared as
816 -- constant objects initialized to the current values of the
817 -- corresponding temporaries.
819 Decl :=
820 Make_Object_Declaration (Loc,
821 Defining_Identifier => Ent,
822 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
823 Constant_Present => True,
824 Expression => New_Occurrence_Of (Var, Loc));
826 if No (Last) then
827 Prepend (Decl, Declarations (N));
828 else
829 Insert_After (Last, Decl);
830 end if;
832 Last := Decl;
833 Analyze (Decl);
834 Next_Elmt (Elm);
835 end loop;
837 -- Loop through calls
839 Call := First_Elmt (Call_List);
840 while Present (Call) loop
842 -- Build a predicate expression of the form
844 -- True
845 -- and then global1 = temp1
846 -- and then global2 = temp2
847 -- ...
849 -- This predicate determines if any of the global values
850 -- referenced by the procedure have changed since the
851 -- current call, if not an infinite recursion is assured.
853 Test := New_Occurrence_Of (Standard_True, Loc);
855 Elm1 := First_Elmt (Var_List);
856 Elm2 := First_Elmt (Shad_List);
857 while Present (Elm1) loop
858 Test :=
859 Make_And_Then (Loc,
860 Left_Opnd => Test,
861 Right_Opnd =>
862 Make_Op_Eq (Loc,
863 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
864 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
866 Next_Elmt (Elm1);
867 Next_Elmt (Elm2);
868 end loop;
870 -- Now we replace the call with the sequence
872 -- if no-changes (see above) then
873 -- raise Storage_Error;
874 -- else
875 -- original-call
876 -- end if;
878 Rewrite (Node (Call),
879 Make_If_Statement (Loc,
880 Condition => Test,
881 Then_Statements => New_List (
882 Make_Raise_Storage_Error (Loc,
883 Reason => SE_Infinite_Recursion)),
885 Else_Statements => New_List (
886 Relocate_Node (Node (Call)))));
888 Analyze (Node (Call));
890 Next_Elmt (Call);
891 end loop;
893 -- Remove temporary scope stack entry used for analysis
895 Pop_Scope;
896 end Detect_Infinite_Recursion;
898 --------------------
899 -- Expand_Actuals --
900 --------------------
902 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
903 Loc : constant Source_Ptr := Sloc (N);
904 Actual : Node_Id;
905 Formal : Entity_Id;
906 N_Node : Node_Id;
907 Post_Call : List_Id;
908 E_Formal : Entity_Id;
910 procedure Add_Call_By_Copy_Code;
911 -- For cases where the parameter must be passed by copy, this routine
912 -- generates a temporary variable into which the actual is copied and
913 -- then passes this as the parameter. For an OUT or IN OUT parameter,
914 -- an assignment is also generated to copy the result back. The call
915 -- also takes care of any constraint checks required for the type
916 -- conversion case (on both the way in and the way out).
918 procedure Add_Simple_Call_By_Copy_Code;
919 -- This is similar to the above, but is used in cases where we know
920 -- that all that is needed is to simply create a temporary and copy
921 -- the value in and out of the temporary.
923 procedure Check_Fortran_Logical;
924 -- A value of type Logical that is passed through a formal parameter
925 -- must be normalized because .TRUE. usually does not have the same
926 -- representation as True. We assume that .FALSE. = False = 0.
927 -- What about functions that return a logical type ???
929 function Is_Legal_Copy return Boolean;
930 -- Check that an actual can be copied before generating the temporary
931 -- to be used in the call. If the actual is of a by_reference type then
932 -- the program is illegal (this can only happen in the presence of
933 -- rep. clauses that force an incorrect alignment). If the formal is
934 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
935 -- the effect that this might lead to unaligned arguments.
937 function Make_Var (Actual : Node_Id) return Entity_Id;
938 -- Returns an entity that refers to the given actual parameter,
939 -- Actual (not including any type conversion). If Actual is an
940 -- entity name, then this entity is returned unchanged, otherwise
941 -- a renaming is created to provide an entity for the actual.
943 procedure Reset_Packed_Prefix;
944 -- The expansion of a packed array component reference is delayed in
945 -- the context of a call. Now we need to complete the expansion, so we
946 -- unmark the analyzed bits in all prefixes.
948 ---------------------------
949 -- Add_Call_By_Copy_Code --
950 ---------------------------
952 procedure Add_Call_By_Copy_Code is
953 Expr : Node_Id;
954 Init : Node_Id;
955 Temp : Entity_Id;
956 Indic : Node_Id;
957 Var : Entity_Id;
958 F_Typ : constant Entity_Id := Etype (Formal);
959 V_Typ : Entity_Id;
960 Crep : Boolean;
962 begin
963 if not Is_Legal_Copy then
964 return;
965 end if;
967 Temp := Make_Temporary (Loc, 'T', Actual);
969 -- Use formal type for temp, unless formal type is an unconstrained
970 -- array, in which case we don't have to worry about bounds checks,
971 -- and we use the actual type, since that has appropriate bounds.
973 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
974 Indic := New_Occurrence_Of (Etype (Actual), Loc);
975 else
976 Indic := New_Occurrence_Of (Etype (Formal), Loc);
977 end if;
979 if Nkind (Actual) = N_Type_Conversion then
980 V_Typ := Etype (Expression (Actual));
982 -- If the formal is an (in-)out parameter, capture the name
983 -- of the variable in order to build the post-call assignment.
985 Var := Make_Var (Expression (Actual));
987 Crep := not Same_Representation
988 (F_Typ, Etype (Expression (Actual)));
990 else
991 V_Typ := Etype (Actual);
992 Var := Make_Var (Actual);
993 Crep := False;
994 end if;
996 -- Setup initialization for case of in out parameter, or an out
997 -- parameter where the formal is an unconstrained array (in the
998 -- latter case, we have to pass in an object with bounds).
1000 -- If this is an out parameter, the initial copy is wasteful, so as
1001 -- an optimization for the one-dimensional case we extract the
1002 -- bounds of the actual and build an uninitialized temporary of the
1003 -- right size.
1005 if Ekind (Formal) = E_In_Out_Parameter
1006 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
1007 then
1008 if Nkind (Actual) = N_Type_Conversion then
1009 if Conversion_OK (Actual) then
1010 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1011 else
1012 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1013 end if;
1015 elsif Ekind (Formal) = E_Out_Parameter
1016 and then Is_Array_Type (F_Typ)
1017 and then Number_Dimensions (F_Typ) = 1
1018 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1019 then
1020 -- Actual is a one-dimensional array or slice, and the type
1021 -- requires no initialization. Create a temporary of the
1022 -- right size, but do not copy actual into it (optimization).
1024 Init := Empty;
1025 Indic :=
1026 Make_Subtype_Indication (Loc,
1027 Subtype_Mark =>
1028 New_Occurrence_Of (F_Typ, Loc),
1029 Constraint =>
1030 Make_Index_Or_Discriminant_Constraint (Loc,
1031 Constraints => New_List (
1032 Make_Range (Loc,
1033 Low_Bound =>
1034 Make_Attribute_Reference (Loc,
1035 Prefix => New_Occurrence_Of (Var, Loc),
1036 Attribute_Name => Name_First),
1037 High_Bound =>
1038 Make_Attribute_Reference (Loc,
1039 Prefix => New_Occurrence_Of (Var, Loc),
1040 Attribute_Name => Name_Last)))));
1042 else
1043 Init := New_Occurrence_Of (Var, Loc);
1044 end if;
1046 -- An initialization is created for packed conversions as
1047 -- actuals for out parameters to enable Make_Object_Declaration
1048 -- to determine the proper subtype for N_Node. Note that this
1049 -- is wasteful because the extra copying on the call side is
1050 -- not required for such out parameters. ???
1052 elsif Ekind (Formal) = E_Out_Parameter
1053 and then Nkind (Actual) = N_Type_Conversion
1054 and then (Is_Bit_Packed_Array (F_Typ)
1055 or else
1056 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1057 then
1058 if Conversion_OK (Actual) then
1059 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1060 else
1061 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1062 end if;
1064 elsif Ekind (Formal) = E_In_Parameter then
1066 -- Handle the case in which the actual is a type conversion
1068 if Nkind (Actual) = N_Type_Conversion then
1069 if Conversion_OK (Actual) then
1070 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1071 else
1072 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1073 end if;
1074 else
1075 Init := New_Occurrence_Of (Var, Loc);
1076 end if;
1078 else
1079 Init := Empty;
1080 end if;
1082 N_Node :=
1083 Make_Object_Declaration (Loc,
1084 Defining_Identifier => Temp,
1085 Object_Definition => Indic,
1086 Expression => Init);
1087 Set_Assignment_OK (N_Node);
1088 Insert_Action (N, N_Node);
1090 -- Now, normally the deal here is that we use the defining
1091 -- identifier created by that object declaration. There is
1092 -- one exception to this. In the change of representation case
1093 -- the above declaration will end up looking like:
1095 -- temp : type := identifier;
1097 -- And in this case we might as well use the identifier directly
1098 -- and eliminate the temporary. Note that the analysis of the
1099 -- declaration was not a waste of time in that case, since it is
1100 -- what generated the necessary change of representation code. If
1101 -- the change of representation introduced additional code, as in
1102 -- a fixed-integer conversion, the expression is not an identifier
1103 -- and must be kept.
1105 if Crep
1106 and then Present (Expression (N_Node))
1107 and then Is_Entity_Name (Expression (N_Node))
1108 then
1109 Temp := Entity (Expression (N_Node));
1110 Rewrite (N_Node, Make_Null_Statement (Loc));
1111 end if;
1113 -- For IN parameter, all we do is to replace the actual
1115 if Ekind (Formal) = E_In_Parameter then
1116 Rewrite (Actual, New_Reference_To (Temp, Loc));
1117 Analyze (Actual);
1119 -- Processing for OUT or IN OUT parameter
1121 else
1122 -- Kill current value indications for the temporary variable we
1123 -- created, since we just passed it as an OUT parameter.
1125 Kill_Current_Values (Temp);
1126 Set_Is_Known_Valid (Temp, False);
1128 -- If type conversion, use reverse conversion on exit
1130 if Nkind (Actual) = N_Type_Conversion then
1131 if Conversion_OK (Actual) then
1132 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1133 else
1134 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1135 end if;
1136 else
1137 Expr := New_Occurrence_Of (Temp, Loc);
1138 end if;
1140 Rewrite (Actual, New_Reference_To (Temp, Loc));
1141 Analyze (Actual);
1143 -- If the actual is a conversion of a packed reference, it may
1144 -- already have been expanded by Remove_Side_Effects, and the
1145 -- resulting variable is a temporary which does not designate
1146 -- the proper out-parameter, which may not be addressable. In
1147 -- that case, generate an assignment to the original expression
1148 -- (before expansion of the packed reference) so that the proper
1149 -- expansion of assignment to a packed component can take place.
1151 declare
1152 Obj : Node_Id;
1153 Lhs : Node_Id;
1155 begin
1156 if Is_Renaming_Of_Object (Var)
1157 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1158 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1159 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1160 = N_Indexed_Component
1161 and then
1162 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1163 then
1164 Obj := Renamed_Object (Var);
1165 Lhs :=
1166 Make_Selected_Component (Loc,
1167 Prefix =>
1168 New_Copy_Tree (Original_Node (Prefix (Obj))),
1169 Selector_Name => New_Copy (Selector_Name (Obj)));
1170 Reset_Analyzed_Flags (Lhs);
1172 else
1173 Lhs := New_Occurrence_Of (Var, Loc);
1174 end if;
1176 Set_Assignment_OK (Lhs);
1178 Append_To (Post_Call,
1179 Make_Assignment_Statement (Loc,
1180 Name => Lhs,
1181 Expression => Expr));
1182 end;
1183 end if;
1184 end Add_Call_By_Copy_Code;
1186 ----------------------------------
1187 -- Add_Simple_Call_By_Copy_Code --
1188 ----------------------------------
1190 procedure Add_Simple_Call_By_Copy_Code is
1191 Temp : Entity_Id;
1192 Decl : Node_Id;
1193 Incod : Node_Id;
1194 Outcod : Node_Id;
1195 Lhs : Node_Id;
1196 Rhs : Node_Id;
1197 Indic : Node_Id;
1198 F_Typ : constant Entity_Id := Etype (Formal);
1200 begin
1201 if not Is_Legal_Copy then
1202 return;
1203 end if;
1205 -- Use formal type for temp, unless formal type is an unconstrained
1206 -- array, in which case we don't have to worry about bounds checks,
1207 -- and we use the actual type, since that has appropriate bounds.
1209 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1210 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1211 else
1212 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1213 end if;
1215 -- Prepare to generate code
1217 Reset_Packed_Prefix;
1219 Temp := Make_Temporary (Loc, 'T', Actual);
1220 Incod := Relocate_Node (Actual);
1221 Outcod := New_Copy_Tree (Incod);
1223 -- Generate declaration of temporary variable, initializing it
1224 -- with the input parameter unless we have an OUT formal or
1225 -- this is an initialization call.
1227 -- If the formal is an out parameter with discriminants, the
1228 -- discriminants must be captured even if the rest of the object
1229 -- is in principle uninitialized, because the discriminants may
1230 -- be read by the called subprogram.
1232 if Ekind (Formal) = E_Out_Parameter then
1233 Incod := Empty;
1235 if Has_Discriminants (Etype (Formal)) then
1236 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1237 end if;
1239 elsif Inside_Init_Proc then
1241 -- Could use a comment here to match comment below ???
1243 if Nkind (Actual) /= N_Selected_Component
1244 or else
1245 not Has_Discriminant_Dependent_Constraint
1246 (Entity (Selector_Name (Actual)))
1247 then
1248 Incod := Empty;
1250 -- Otherwise, keep the component in order to generate the proper
1251 -- actual subtype, that depends on enclosing discriminants.
1253 else
1254 null;
1255 end if;
1256 end if;
1258 Decl :=
1259 Make_Object_Declaration (Loc,
1260 Defining_Identifier => Temp,
1261 Object_Definition => Indic,
1262 Expression => Incod);
1264 if Inside_Init_Proc
1265 and then No (Incod)
1266 then
1267 -- If the call is to initialize a component of a composite type,
1268 -- and the component does not depend on discriminants, use the
1269 -- actual type of the component. This is required in case the
1270 -- component is constrained, because in general the formal of the
1271 -- initialization procedure will be unconstrained. Note that if
1272 -- the component being initialized is constrained by an enclosing
1273 -- discriminant, the presence of the initialization in the
1274 -- declaration will generate an expression for the actual subtype.
1276 Set_No_Initialization (Decl);
1277 Set_Object_Definition (Decl,
1278 New_Occurrence_Of (Etype (Actual), Loc));
1279 end if;
1281 Insert_Action (N, Decl);
1283 -- The actual is simply a reference to the temporary
1285 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1287 -- Generate copy out if OUT or IN OUT parameter
1289 if Ekind (Formal) /= E_In_Parameter then
1290 Lhs := Outcod;
1291 Rhs := New_Occurrence_Of (Temp, Loc);
1293 -- Deal with conversion
1295 if Nkind (Lhs) = N_Type_Conversion then
1296 Lhs := Expression (Lhs);
1297 Rhs := Convert_To (Etype (Actual), Rhs);
1298 end if;
1300 Append_To (Post_Call,
1301 Make_Assignment_Statement (Loc,
1302 Name => Lhs,
1303 Expression => Rhs));
1304 Set_Assignment_OK (Name (Last (Post_Call)));
1305 end if;
1306 end Add_Simple_Call_By_Copy_Code;
1308 ---------------------------
1309 -- Check_Fortran_Logical --
1310 ---------------------------
1312 procedure Check_Fortran_Logical is
1313 Logical : constant Entity_Id := Etype (Formal);
1314 Var : Entity_Id;
1316 -- Note: this is very incomplete, e.g. it does not handle arrays
1317 -- of logical values. This is really not the right approach at all???)
1319 begin
1320 if Convention (Subp) = Convention_Fortran
1321 and then Root_Type (Etype (Formal)) = Standard_Boolean
1322 and then Ekind (Formal) /= E_In_Parameter
1323 then
1324 Var := Make_Var (Actual);
1325 Append_To (Post_Call,
1326 Make_Assignment_Statement (Loc,
1327 Name => New_Occurrence_Of (Var, Loc),
1328 Expression =>
1329 Unchecked_Convert_To (
1330 Logical,
1331 Make_Op_Ne (Loc,
1332 Left_Opnd => New_Occurrence_Of (Var, Loc),
1333 Right_Opnd =>
1334 Unchecked_Convert_To (
1335 Logical,
1336 New_Occurrence_Of (Standard_False, Loc))))));
1337 end if;
1338 end Check_Fortran_Logical;
1340 -------------------
1341 -- Is_Legal_Copy --
1342 -------------------
1344 function Is_Legal_Copy return Boolean is
1345 begin
1346 -- An attempt to copy a value of such a type can only occur if
1347 -- representation clauses give the actual a misaligned address.
1349 if Is_By_Reference_Type (Etype (Formal)) then
1350 Error_Msg_N
1351 ("misaligned actual cannot be passed by reference", Actual);
1352 return False;
1354 -- For users of Starlet, we assume that the specification of by-
1355 -- reference mechanism is mandatory. This may lead to unaligned
1356 -- objects but at least for DEC legacy code it is known to work.
1357 -- The warning will alert users of this code that a problem may
1358 -- be lurking.
1360 elsif Mechanism (Formal) = By_Reference
1361 and then Is_Valued_Procedure (Scope (Formal))
1362 then
1363 Error_Msg_N
1364 ("by_reference actual may be misaligned?", Actual);
1365 return False;
1367 else
1368 return True;
1369 end if;
1370 end Is_Legal_Copy;
1372 --------------
1373 -- Make_Var --
1374 --------------
1376 function Make_Var (Actual : Node_Id) return Entity_Id is
1377 Var : Entity_Id;
1379 begin
1380 if Is_Entity_Name (Actual) then
1381 return Entity (Actual);
1383 else
1384 Var := Make_Temporary (Loc, 'T', Actual);
1386 N_Node :=
1387 Make_Object_Renaming_Declaration (Loc,
1388 Defining_Identifier => Var,
1389 Subtype_Mark =>
1390 New_Occurrence_Of (Etype (Actual), Loc),
1391 Name => Relocate_Node (Actual));
1393 Insert_Action (N, N_Node);
1394 return Var;
1395 end if;
1396 end Make_Var;
1398 -------------------------
1399 -- Reset_Packed_Prefix --
1400 -------------------------
1402 procedure Reset_Packed_Prefix is
1403 Pfx : Node_Id := Actual;
1404 begin
1405 loop
1406 Set_Analyzed (Pfx, False);
1407 exit when
1408 not Nkind_In (Pfx, N_Selected_Component, N_Indexed_Component);
1409 Pfx := Prefix (Pfx);
1410 end loop;
1411 end Reset_Packed_Prefix;
1413 -- Start of processing for Expand_Actuals
1415 begin
1416 Post_Call := New_List;
1418 Formal := First_Formal (Subp);
1419 Actual := First_Actual (N);
1420 while Present (Formal) loop
1421 E_Formal := Etype (Formal);
1423 if Is_Scalar_Type (E_Formal)
1424 or else Nkind (Actual) = N_Slice
1425 then
1426 Check_Fortran_Logical;
1428 -- RM 6.4.1 (11)
1430 elsif Ekind (Formal) /= E_Out_Parameter then
1432 -- The unusual case of the current instance of a protected type
1433 -- requires special handling. This can only occur in the context
1434 -- of a call within the body of a protected operation.
1436 if Is_Entity_Name (Actual)
1437 and then Ekind (Entity (Actual)) = E_Protected_Type
1438 and then In_Open_Scopes (Entity (Actual))
1439 then
1440 if Scope (Subp) /= Entity (Actual) then
1441 Error_Msg_N ("operation outside protected type may not "
1442 & "call back its protected operations?", Actual);
1443 end if;
1445 Rewrite (Actual,
1446 Expand_Protected_Object_Reference (N, Entity (Actual)));
1447 end if;
1449 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1450 -- build-in-place function, then a temporary return object needs
1451 -- to be created and access to it must be passed to the function.
1452 -- Currently we limit such functions to those with inherently
1453 -- limited result subtypes, but eventually we plan to expand the
1454 -- functions that are treated as build-in-place to include other
1455 -- composite result types.
1457 if Ada_Version >= Ada_05
1458 and then Is_Build_In_Place_Function_Call (Actual)
1459 then
1460 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1461 end if;
1463 Apply_Constraint_Check (Actual, E_Formal);
1465 -- Out parameter case. No constraint checks on access type
1466 -- RM 6.4.1 (13)
1468 elsif Is_Access_Type (E_Formal) then
1469 null;
1471 -- RM 6.4.1 (14)
1473 elsif Has_Discriminants (Base_Type (E_Formal))
1474 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1475 then
1476 Apply_Constraint_Check (Actual, E_Formal);
1478 -- RM 6.4.1 (15)
1480 else
1481 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1482 end if;
1484 -- Processing for IN-OUT and OUT parameters
1486 if Ekind (Formal) /= E_In_Parameter then
1488 -- For type conversions of arrays, apply length/range checks
1490 if Is_Array_Type (E_Formal)
1491 and then Nkind (Actual) = N_Type_Conversion
1492 then
1493 if Is_Constrained (E_Formal) then
1494 Apply_Length_Check (Expression (Actual), E_Formal);
1495 else
1496 Apply_Range_Check (Expression (Actual), E_Formal);
1497 end if;
1498 end if;
1500 -- If argument is a type conversion for a type that is passed
1501 -- by copy, then we must pass the parameter by copy.
1503 if Nkind (Actual) = N_Type_Conversion
1504 and then
1505 (Is_Numeric_Type (E_Formal)
1506 or else Is_Access_Type (E_Formal)
1507 or else Is_Enumeration_Type (E_Formal)
1508 or else Is_Bit_Packed_Array (Etype (Formal))
1509 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1511 -- Also pass by copy if change of representation
1513 or else not Same_Representation
1514 (Etype (Formal),
1515 Etype (Expression (Actual))))
1516 then
1517 Add_Call_By_Copy_Code;
1519 -- References to components of bit packed arrays are expanded
1520 -- at this point, rather than at the point of analysis of the
1521 -- actuals, to handle the expansion of the assignment to
1522 -- [in] out parameters.
1524 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1525 Add_Simple_Call_By_Copy_Code;
1527 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1528 -- because the back-end cannot cope with such objects. In other
1529 -- cases where alignment forces a copy, the back-end generates
1530 -- it properly. It should not be generated unconditionally in the
1531 -- front-end because it does not know precisely the alignment
1532 -- requirements of the target, and makes too conservative an
1533 -- estimate, leading to superfluous copies or spurious errors
1534 -- on by-reference parameters.
1536 elsif Nkind (Actual) = N_Selected_Component
1537 and then
1538 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1539 and then not Represented_As_Scalar (Etype (Formal))
1540 then
1541 Add_Simple_Call_By_Copy_Code;
1543 -- References to slices of bit packed arrays are expanded
1545 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1546 Add_Call_By_Copy_Code;
1548 -- References to possibly unaligned slices of arrays are expanded
1550 elsif Is_Possibly_Unaligned_Slice (Actual) then
1551 Add_Call_By_Copy_Code;
1553 -- Deal with access types where the actual subtype and the
1554 -- formal subtype are not the same, requiring a check.
1556 -- It is necessary to exclude tagged types because of "downward
1557 -- conversion" errors.
1559 elsif Is_Access_Type (E_Formal)
1560 and then not Same_Type (E_Formal, Etype (Actual))
1561 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1562 then
1563 Add_Call_By_Copy_Code;
1565 -- If the actual is not a scalar and is marked for volatile
1566 -- treatment, whereas the formal is not volatile, then pass
1567 -- by copy unless it is a by-reference type.
1569 -- Note: we use Is_Volatile here rather than Treat_As_Volatile,
1570 -- because this is the enforcement of a language rule that applies
1571 -- only to "real" volatile variables, not e.g. to the address
1572 -- clause overlay case.
1574 elsif Is_Entity_Name (Actual)
1575 and then Is_Volatile (Entity (Actual))
1576 and then not Is_By_Reference_Type (Etype (Actual))
1577 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1578 and then not Is_Volatile (E_Formal)
1579 then
1580 Add_Call_By_Copy_Code;
1582 elsif Nkind (Actual) = N_Indexed_Component
1583 and then Is_Entity_Name (Prefix (Actual))
1584 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1585 then
1586 Add_Call_By_Copy_Code;
1588 -- Add call-by-copy code for the case of scalar out parameters
1589 -- when it is not known at compile time that the subtype of the
1590 -- formal is a subrange of the subtype of the actual (or vice
1591 -- versa for in out parameters), in order to get range checks
1592 -- on such actuals. (Maybe this case should be handled earlier
1593 -- in the if statement???)
1595 elsif Is_Scalar_Type (E_Formal)
1596 and then
1597 (not In_Subrange_Of (E_Formal, Etype (Actual))
1598 or else
1599 (Ekind (Formal) = E_In_Out_Parameter
1600 and then not In_Subrange_Of (Etype (Actual), E_Formal)))
1601 then
1602 -- Perhaps the setting back to False should be done within
1603 -- Add_Call_By_Copy_Code, since it could get set on other
1604 -- cases occurring above???
1606 if Do_Range_Check (Actual) then
1607 Set_Do_Range_Check (Actual, False);
1608 end if;
1610 Add_Call_By_Copy_Code;
1611 end if;
1613 -- Processing for IN parameters
1615 else
1616 -- For IN parameters is in the packed array case, we expand an
1617 -- indexed component (the circuit in Exp_Ch4 deliberately left
1618 -- indexed components appearing as actuals untouched, so that
1619 -- the special processing above for the OUT and IN OUT cases
1620 -- could be performed. We could make the test in Exp_Ch4 more
1621 -- complex and have it detect the parameter mode, but it is
1622 -- easier simply to handle all cases here.)
1624 if Nkind (Actual) = N_Indexed_Component
1625 and then Is_Packed (Etype (Prefix (Actual)))
1626 then
1627 Reset_Packed_Prefix;
1628 Expand_Packed_Element_Reference (Actual);
1630 -- If we have a reference to a bit packed array, we copy it, since
1631 -- the actual must be byte aligned.
1633 -- Is this really necessary in all cases???
1635 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1636 Add_Simple_Call_By_Copy_Code;
1638 -- If a non-scalar actual is possibly unaligned, we need a copy
1640 elsif Is_Possibly_Unaligned_Object (Actual)
1641 and then not Represented_As_Scalar (Etype (Formal))
1642 then
1643 Add_Simple_Call_By_Copy_Code;
1645 -- Similarly, we have to expand slices of packed arrays here
1646 -- because the result must be byte aligned.
1648 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1649 Add_Call_By_Copy_Code;
1651 -- Only processing remaining is to pass by copy if this is a
1652 -- reference to a possibly unaligned slice, since the caller
1653 -- expects an appropriately aligned argument.
1655 elsif Is_Possibly_Unaligned_Slice (Actual) then
1656 Add_Call_By_Copy_Code;
1657 end if;
1658 end if;
1660 Next_Formal (Formal);
1661 Next_Actual (Actual);
1662 end loop;
1664 -- Find right place to put post call stuff if it is present
1666 if not Is_Empty_List (Post_Call) then
1668 -- If call is not a list member, it must be the triggering statement
1669 -- of a triggering alternative or an entry call alternative, and we
1670 -- can add the post call stuff to the corresponding statement list.
1672 if not Is_List_Member (N) then
1673 declare
1674 P : constant Node_Id := Parent (N);
1676 begin
1677 pragma Assert (Nkind_In (P, N_Triggering_Alternative,
1678 N_Entry_Call_Alternative));
1680 if Is_Non_Empty_List (Statements (P)) then
1681 Insert_List_Before_And_Analyze
1682 (First (Statements (P)), Post_Call);
1683 else
1684 Set_Statements (P, Post_Call);
1685 end if;
1686 end;
1688 -- Otherwise, normal case where N is in a statement sequence,
1689 -- just put the post-call stuff after the call statement.
1691 else
1692 Insert_Actions_After (N, Post_Call);
1693 end if;
1694 end if;
1696 -- The call node itself is re-analyzed in Expand_Call
1698 end Expand_Actuals;
1700 -----------------
1701 -- Expand_Call --
1702 -----------------
1704 -- This procedure handles expansion of function calls and procedure call
1705 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1706 -- Expand_N_Procedure_Call_Statement). Processing for calls includes:
1708 -- Replace call to Raise_Exception by Raise_Exception_Always if possible
1709 -- Provide values of actuals for all formals in Extra_Formals list
1710 -- Replace "call" to enumeration literal function by literal itself
1711 -- Rewrite call to predefined operator as operator
1712 -- Replace actuals to in-out parameters that are numeric conversions,
1713 -- with explicit assignment to temporaries before and after the call.
1714 -- Remove optional actuals if First_Optional_Parameter specified.
1716 -- Note that the list of actuals has been filled with default expressions
1717 -- during semantic analysis of the call. Only the extra actuals required
1718 -- for the 'Constrained attribute and for accessibility checks are added
1719 -- at this point.
1721 procedure Expand_Call (N : Node_Id) is
1722 Loc : constant Source_Ptr := Sloc (N);
1723 Extra_Actuals : List_Id := No_List;
1724 Prev : Node_Id := Empty;
1726 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1727 -- Adds one entry to the end of the actual parameter list. Used for
1728 -- default parameters and for extra actuals (for Extra_Formals). The
1729 -- argument is an N_Parameter_Association node.
1731 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1732 -- Adds an extra actual to the list of extra actuals. Expr is the
1733 -- expression for the value of the actual, EF is the entity for the
1734 -- extra formal.
1736 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1737 -- Within an instance, a type derived from a non-tagged formal derived
1738 -- type inherits from the original parent, not from the actual. The
1739 -- current derivation mechanism has the derived type inherit from the
1740 -- actual, which is only correct outside of the instance. If the
1741 -- subprogram is inherited, we test for this particular case through a
1742 -- convoluted tree traversal before setting the proper subprogram to be
1743 -- called.
1745 --------------------------
1746 -- Add_Actual_Parameter --
1747 --------------------------
1749 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1750 Actual_Expr : constant Node_Id :=
1751 Explicit_Actual_Parameter (Insert_Param);
1753 begin
1754 -- Case of insertion is first named actual
1756 if No (Prev) or else
1757 Nkind (Parent (Prev)) /= N_Parameter_Association
1758 then
1759 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1760 Set_First_Named_Actual (N, Actual_Expr);
1762 if No (Prev) then
1763 if No (Parameter_Associations (N)) then
1764 Set_Parameter_Associations (N, New_List);
1765 Append (Insert_Param, Parameter_Associations (N));
1766 end if;
1767 else
1768 Insert_After (Prev, Insert_Param);
1769 end if;
1771 -- Case of insertion is not first named actual
1773 else
1774 Set_Next_Named_Actual
1775 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1776 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1777 Append (Insert_Param, Parameter_Associations (N));
1778 end if;
1780 Prev := Actual_Expr;
1781 end Add_Actual_Parameter;
1783 ----------------------
1784 -- Add_Extra_Actual --
1785 ----------------------
1787 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1788 Loc : constant Source_Ptr := Sloc (Expr);
1790 begin
1791 if Extra_Actuals = No_List then
1792 Extra_Actuals := New_List;
1793 Set_Parent (Extra_Actuals, N);
1794 end if;
1796 Append_To (Extra_Actuals,
1797 Make_Parameter_Association (Loc,
1798 Explicit_Actual_Parameter => Expr,
1799 Selector_Name =>
1800 Make_Identifier (Loc, Chars (EF))));
1802 Analyze_And_Resolve (Expr, Etype (EF));
1804 if Nkind (N) = N_Function_Call then
1805 Set_Is_Accessibility_Actual (Parent (Expr));
1806 end if;
1807 end Add_Extra_Actual;
1809 ---------------------------
1810 -- Inherited_From_Formal --
1811 ---------------------------
1813 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1814 Par : Entity_Id;
1815 Gen_Par : Entity_Id;
1816 Gen_Prim : Elist_Id;
1817 Elmt : Elmt_Id;
1818 Indic : Node_Id;
1820 begin
1821 -- If the operation is inherited, it is attached to the corresponding
1822 -- type derivation. If the parent in the derivation is a generic
1823 -- actual, it is a subtype of the actual, and we have to recover the
1824 -- original derived type declaration to find the proper parent.
1826 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1827 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1828 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1829 N_Derived_Type_Definition
1830 or else not In_Instance
1831 then
1832 return Empty;
1834 else
1835 Indic :=
1836 Subtype_Indication
1837 (Type_Definition (Original_Node (Parent (S))));
1839 if Nkind (Indic) = N_Subtype_Indication then
1840 Par := Entity (Subtype_Mark (Indic));
1841 else
1842 Par := Entity (Indic);
1843 end if;
1844 end if;
1846 if not Is_Generic_Actual_Type (Par)
1847 or else Is_Tagged_Type (Par)
1848 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1849 or else not In_Open_Scopes (Scope (Par))
1850 then
1851 return Empty;
1852 else
1853 Gen_Par := Generic_Parent_Type (Parent (Par));
1854 end if;
1856 -- If the actual has no generic parent type, the formal is not
1857 -- a formal derived type, so nothing to inherit.
1859 if No (Gen_Par) then
1860 return Empty;
1861 end if;
1863 -- If the generic parent type is still the generic type, this is a
1864 -- private formal, not a derived formal, and there are no operations
1865 -- inherited from the formal.
1867 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1868 return Empty;
1869 end if;
1871 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1873 Elmt := First_Elmt (Gen_Prim);
1874 while Present (Elmt) loop
1875 if Chars (Node (Elmt)) = Chars (S) then
1876 declare
1877 F1 : Entity_Id;
1878 F2 : Entity_Id;
1880 begin
1881 F1 := First_Formal (S);
1882 F2 := First_Formal (Node (Elmt));
1883 while Present (F1)
1884 and then Present (F2)
1885 loop
1886 if Etype (F1) = Etype (F2)
1887 or else Etype (F2) = Gen_Par
1888 then
1889 Next_Formal (F1);
1890 Next_Formal (F2);
1891 else
1892 Next_Elmt (Elmt);
1893 exit; -- not the right subprogram
1894 end if;
1896 return Node (Elmt);
1897 end loop;
1898 end;
1900 else
1901 Next_Elmt (Elmt);
1902 end if;
1903 end loop;
1905 raise Program_Error;
1906 end Inherited_From_Formal;
1908 -- Local variables
1910 Remote : constant Boolean := Is_Remote_Call (N);
1911 Actual : Node_Id;
1912 Formal : Entity_Id;
1913 Orig_Subp : Entity_Id := Empty;
1914 Param_Count : Natural := 0;
1915 Parent_Formal : Entity_Id;
1916 Parent_Subp : Entity_Id;
1917 Scop : Entity_Id;
1918 Subp : Entity_Id;
1920 Prev_Orig : Node_Id;
1921 -- Original node for an actual, which may have been rewritten. If the
1922 -- actual is a function call that has been transformed from a selected
1923 -- component, the original node is unanalyzed. Otherwise, it carries
1924 -- semantic information used to generate additional actuals.
1926 CW_Interface_Formals_Present : Boolean := False;
1928 -- Start of processing for Expand_Call
1930 begin
1931 -- Ignore if previous error
1933 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1934 return;
1935 end if;
1937 -- Call using access to subprogram with explicit dereference
1939 if Nkind (Name (N)) = N_Explicit_Dereference then
1940 Subp := Etype (Name (N));
1941 Parent_Subp := Empty;
1943 -- Case of call to simple entry, where the Name is a selected component
1944 -- whose prefix is the task, and whose selector name is the entry name
1946 elsif Nkind (Name (N)) = N_Selected_Component then
1947 Subp := Entity (Selector_Name (Name (N)));
1948 Parent_Subp := Empty;
1950 -- Case of call to member of entry family, where Name is an indexed
1951 -- component, with the prefix being a selected component giving the
1952 -- task and entry family name, and the index being the entry index.
1954 elsif Nkind (Name (N)) = N_Indexed_Component then
1955 Subp := Entity (Selector_Name (Prefix (Name (N))));
1956 Parent_Subp := Empty;
1958 -- Normal case
1960 else
1961 Subp := Entity (Name (N));
1962 Parent_Subp := Alias (Subp);
1964 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1965 -- if we can tell that the first parameter cannot possibly be null.
1966 -- This improves efficiency by avoiding a run-time test.
1968 -- We do not do this if Raise_Exception_Always does not exist, which
1969 -- can happen in configurable run time profiles which provide only a
1970 -- Raise_Exception.
1972 if Is_RTE (Subp, RE_Raise_Exception)
1973 and then RTE_Available (RE_Raise_Exception_Always)
1974 then
1975 declare
1976 FA : constant Node_Id := Original_Node (First_Actual (N));
1978 begin
1979 -- The case we catch is where the first argument is obtained
1980 -- using the Identity attribute (which must always be
1981 -- non-null).
1983 if Nkind (FA) = N_Attribute_Reference
1984 and then Attribute_Name (FA) = Name_Identity
1985 then
1986 Subp := RTE (RE_Raise_Exception_Always);
1987 Set_Name (N, New_Occurrence_Of (Subp, Loc));
1988 end if;
1989 end;
1990 end if;
1992 if Ekind (Subp) = E_Entry then
1993 Parent_Subp := Empty;
1994 end if;
1995 end if;
1997 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1998 -- alternative in an asynchronous select or as an entry call in
1999 -- a conditional or timed select. Check whether the procedure call
2000 -- is a renaming of an entry and rewrite it as an entry call.
2002 if Ada_Version >= Ada_05
2003 and then Nkind (N) = N_Procedure_Call_Statement
2004 and then
2005 ((Nkind (Parent (N)) = N_Triggering_Alternative
2006 and then Triggering_Statement (Parent (N)) = N)
2007 or else
2008 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2009 and then Entry_Call_Statement (Parent (N)) = N))
2010 then
2011 declare
2012 Ren_Decl : Node_Id;
2013 Ren_Root : Entity_Id := Subp;
2015 begin
2016 -- This may be a chain of renamings, find the root
2018 if Present (Alias (Ren_Root)) then
2019 Ren_Root := Alias (Ren_Root);
2020 end if;
2022 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
2023 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
2025 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
2026 Rewrite (N,
2027 Make_Entry_Call_Statement (Loc,
2028 Name =>
2029 New_Copy_Tree (Name (Ren_Decl)),
2030 Parameter_Associations =>
2031 New_Copy_List_Tree (Parameter_Associations (N))));
2033 return;
2034 end if;
2035 end if;
2036 end;
2037 end if;
2039 -- First step, compute extra actuals, corresponding to any Extra_Formals
2040 -- present. Note that we do not access Extra_Formals directly, instead
2041 -- we simply note the presence of the extra formals as we process the
2042 -- regular formals collecting corresponding actuals in Extra_Actuals.
2044 -- We also generate any required range checks for actuals for in formals
2045 -- as we go through the loop, since this is a convenient place to do it.
2046 -- (Though it seems that this would be better done in Expand_Actuals???)
2048 Formal := First_Formal (Subp);
2049 Actual := First_Actual (N);
2050 Param_Count := 1;
2051 while Present (Formal) loop
2053 -- Generate range check if required
2055 if Do_Range_Check (Actual)
2056 and then Ekind (Formal) = E_In_Parameter
2057 then
2058 Set_Do_Range_Check (Actual, False);
2059 Generate_Range_Check
2060 (Actual, Etype (Formal), CE_Range_Check_Failed);
2061 end if;
2063 -- Prepare to examine current entry
2065 Prev := Actual;
2066 Prev_Orig := Original_Node (Prev);
2068 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2069 -- to expand it in a further round.
2071 CW_Interface_Formals_Present :=
2072 CW_Interface_Formals_Present
2073 or else
2074 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2075 and then Is_Interface (Etype (Etype (Formal))))
2076 or else
2077 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2078 and then Is_Interface (Directly_Designated_Type
2079 (Etype (Etype (Formal)))));
2081 -- Create possible extra actual for constrained case. Usually, the
2082 -- extra actual is of the form actual'constrained, but since this
2083 -- attribute is only available for unconstrained records, TRUE is
2084 -- expanded if the type of the formal happens to be constrained (for
2085 -- instance when this procedure is inherited from an unconstrained
2086 -- record to a constrained one) or if the actual has no discriminant
2087 -- (its type is constrained). An exception to this is the case of a
2088 -- private type without discriminants. In this case we pass FALSE
2089 -- because the object has underlying discriminants with defaults.
2091 if Present (Extra_Constrained (Formal)) then
2092 if Ekind (Etype (Prev)) in Private_Kind
2093 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2094 then
2095 Add_Extra_Actual
2096 (New_Occurrence_Of (Standard_False, Loc),
2097 Extra_Constrained (Formal));
2099 elsif Is_Constrained (Etype (Formal))
2100 or else not Has_Discriminants (Etype (Prev))
2101 then
2102 Add_Extra_Actual
2103 (New_Occurrence_Of (Standard_True, Loc),
2104 Extra_Constrained (Formal));
2106 -- Do not produce extra actuals for Unchecked_Union parameters.
2107 -- Jump directly to the end of the loop.
2109 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2110 goto Skip_Extra_Actual_Generation;
2112 else
2113 -- If the actual is a type conversion, then the constrained
2114 -- test applies to the actual, not the target type.
2116 declare
2117 Act_Prev : Node_Id;
2119 begin
2120 -- Test for unchecked conversions as well, which can occur
2121 -- as out parameter actuals on calls to stream procedures.
2123 Act_Prev := Prev;
2124 while Nkind_In (Act_Prev, N_Type_Conversion,
2125 N_Unchecked_Type_Conversion)
2126 loop
2127 Act_Prev := Expression (Act_Prev);
2128 end loop;
2130 -- If the expression is a conversion of a dereference, this
2131 -- is internally generated code that manipulates addresses,
2132 -- e.g. when building interface tables. No check should
2133 -- occur in this case, and the discriminated object is not
2134 -- directly a hand.
2136 if not Comes_From_Source (Actual)
2137 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2138 and then Nkind (Act_Prev) = N_Explicit_Dereference
2139 then
2140 Add_Extra_Actual
2141 (New_Occurrence_Of (Standard_False, Loc),
2142 Extra_Constrained (Formal));
2144 else
2145 Add_Extra_Actual
2146 (Make_Attribute_Reference (Sloc (Prev),
2147 Prefix =>
2148 Duplicate_Subexpr_No_Checks
2149 (Act_Prev, Name_Req => True),
2150 Attribute_Name => Name_Constrained),
2151 Extra_Constrained (Formal));
2152 end if;
2153 end;
2154 end if;
2155 end if;
2157 -- Create possible extra actual for accessibility level
2159 if Present (Extra_Accessibility (Formal)) then
2161 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2162 -- attribute, then the original actual may be an aliased object
2163 -- occurring as the prefix in a call using "Object.Operation"
2164 -- notation. In that case we must pass the level of the object,
2165 -- so Prev_Orig is reset to Prev and the attribute will be
2166 -- processed by the code for Access attributes further below.
2168 if Prev_Orig /= Prev
2169 and then Nkind (Prev) = N_Attribute_Reference
2170 and then
2171 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2172 and then Is_Aliased_View (Prev_Orig)
2173 then
2174 Prev_Orig := Prev;
2175 end if;
2177 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals
2178 -- of accessibility levels.
2180 if Ekind (Current_Scope) in Subprogram_Kind
2181 and then Is_Thunk (Current_Scope)
2182 then
2183 declare
2184 Parm_Ent : Entity_Id;
2186 begin
2187 if Is_Controlling_Actual (Actual) then
2189 -- Find the corresponding actual of the thunk
2191 Parm_Ent := First_Entity (Current_Scope);
2192 for J in 2 .. Param_Count loop
2193 Next_Entity (Parm_Ent);
2194 end loop;
2196 else pragma Assert (Is_Entity_Name (Actual));
2197 Parm_Ent := Entity (Actual);
2198 end if;
2200 Add_Extra_Actual
2201 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2202 Extra_Accessibility (Formal));
2203 end;
2205 elsif Is_Entity_Name (Prev_Orig) then
2207 -- When passing an access parameter, or a renaming of an access
2208 -- parameter, as the actual to another access parameter we need
2209 -- to pass along the actual's own access level parameter. This
2210 -- is done if we are within the scope of the formal access
2211 -- parameter (if this is an inlined body the extra formal is
2212 -- irrelevant).
2214 if (Is_Formal (Entity (Prev_Orig))
2215 or else
2216 (Present (Renamed_Object (Entity (Prev_Orig)))
2217 and then
2218 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2219 and then
2220 Is_Formal
2221 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2222 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2223 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2224 then
2225 declare
2226 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2228 begin
2229 pragma Assert (Present (Parm_Ent));
2231 if Present (Extra_Accessibility (Parm_Ent)) then
2232 Add_Extra_Actual
2233 (New_Occurrence_Of
2234 (Extra_Accessibility (Parm_Ent), Loc),
2235 Extra_Accessibility (Formal));
2237 -- If the actual access parameter does not have an
2238 -- associated extra formal providing its scope level,
2239 -- then treat the actual as having library-level
2240 -- accessibility.
2242 else
2243 Add_Extra_Actual
2244 (Make_Integer_Literal (Loc,
2245 Intval => Scope_Depth (Standard_Standard)),
2246 Extra_Accessibility (Formal));
2247 end if;
2248 end;
2250 -- The actual is a normal access value, so just pass the level
2251 -- of the actual's access type.
2253 else
2254 Add_Extra_Actual
2255 (Make_Integer_Literal (Loc,
2256 Intval => Type_Access_Level (Etype (Prev_Orig))),
2257 Extra_Accessibility (Formal));
2258 end if;
2260 -- If the actual is an access discriminant, then pass the level
2261 -- of the enclosing object (RM05-3.10.2(12.4/2)).
2263 elsif Nkind (Prev_Orig) = N_Selected_Component
2264 and then Ekind (Entity (Selector_Name (Prev_Orig))) =
2265 E_Discriminant
2266 and then Ekind (Etype (Entity (Selector_Name (Prev_Orig)))) =
2267 E_Anonymous_Access_Type
2268 then
2269 Add_Extra_Actual
2270 (Make_Integer_Literal (Loc,
2271 Intval => Object_Access_Level (Prefix (Prev_Orig))),
2272 Extra_Accessibility (Formal));
2274 -- All other cases
2276 else
2277 case Nkind (Prev_Orig) is
2279 when N_Attribute_Reference =>
2280 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2282 -- For X'Access, pass on the level of the prefix X
2284 when Attribute_Access =>
2285 Add_Extra_Actual
2286 (Make_Integer_Literal (Loc,
2287 Intval =>
2288 Object_Access_Level
2289 (Prefix (Prev_Orig))),
2290 Extra_Accessibility (Formal));
2292 -- Treat the unchecked attributes as library-level
2294 when Attribute_Unchecked_Access |
2295 Attribute_Unrestricted_Access =>
2296 Add_Extra_Actual
2297 (Make_Integer_Literal (Loc,
2298 Intval => Scope_Depth (Standard_Standard)),
2299 Extra_Accessibility (Formal));
2301 -- No other cases of attributes returning access
2302 -- values that can be passed to access parameters
2304 when others =>
2305 raise Program_Error;
2307 end case;
2309 -- For allocators we pass the level of the execution of the
2310 -- called subprogram, which is one greater than the current
2311 -- scope level.
2313 when N_Allocator =>
2314 Add_Extra_Actual
2315 (Make_Integer_Literal (Loc,
2316 Intval => Scope_Depth (Current_Scope) + 1),
2317 Extra_Accessibility (Formal));
2319 -- For other cases we simply pass the level of the actual's
2320 -- access type. The type is retrieved from Prev rather than
2321 -- Prev_Orig, because in some cases Prev_Orig denotes an
2322 -- original expression that has not been analyzed.
2324 when others =>
2325 Add_Extra_Actual
2326 (Make_Integer_Literal (Loc,
2327 Intval => Type_Access_Level (Etype (Prev))),
2328 Extra_Accessibility (Formal));
2329 end case;
2330 end if;
2331 end if;
2333 -- Perform the check of 4.6(49) that prevents a null value from being
2334 -- passed as an actual to an access parameter. Note that the check is
2335 -- elided in the common cases of passing an access attribute or
2336 -- access parameter as an actual. Also, we currently don't enforce
2337 -- this check for expander-generated actuals and when -gnatdj is set.
2339 if Ada_Version >= Ada_05 then
2341 -- Ada 2005 (AI-231): Check null-excluding access types
2343 if Is_Access_Type (Etype (Formal))
2344 and then Can_Never_Be_Null (Etype (Formal))
2345 and then Nkind (Prev) /= N_Raise_Constraint_Error
2346 and then (Known_Null (Prev)
2347 or else not Can_Never_Be_Null (Etype (Prev)))
2348 then
2349 Install_Null_Excluding_Check (Prev);
2350 end if;
2352 -- Ada_Version < Ada_05
2354 else
2355 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2356 or else Access_Checks_Suppressed (Subp)
2357 then
2358 null;
2360 elsif Debug_Flag_J then
2361 null;
2363 elsif not Comes_From_Source (Prev) then
2364 null;
2366 elsif Is_Entity_Name (Prev)
2367 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2368 then
2369 null;
2371 elsif Nkind_In (Prev, N_Allocator, N_Attribute_Reference) then
2372 null;
2374 -- Suppress null checks when passing to access parameters of Java
2375 -- and CIL subprograms. (Should this be done for other foreign
2376 -- conventions as well ???)
2378 elsif Convention (Subp) = Convention_Java
2379 or else Convention (Subp) = Convention_CIL
2380 then
2381 null;
2383 else
2384 Install_Null_Excluding_Check (Prev);
2385 end if;
2386 end if;
2388 -- Perform appropriate validity checks on parameters that
2389 -- are entities.
2391 if Validity_Checks_On then
2392 if (Ekind (Formal) = E_In_Parameter
2393 and then Validity_Check_In_Params)
2394 or else
2395 (Ekind (Formal) = E_In_Out_Parameter
2396 and then Validity_Check_In_Out_Params)
2397 then
2398 -- If the actual is an indexed component of a packed type (or
2399 -- is an indexed or selected component whose prefix recursively
2400 -- meets this condition), it has not been expanded yet. It will
2401 -- be copied in the validity code that follows, and has to be
2402 -- expanded appropriately, so reanalyze it.
2404 -- What we do is just to unset analyzed bits on prefixes till
2405 -- we reach something that does not have a prefix.
2407 declare
2408 Nod : Node_Id;
2410 begin
2411 Nod := Actual;
2412 while Nkind_In (Nod, N_Indexed_Component,
2413 N_Selected_Component)
2414 loop
2415 Set_Analyzed (Nod, False);
2416 Nod := Prefix (Nod);
2417 end loop;
2418 end;
2420 Ensure_Valid (Actual);
2421 end if;
2422 end if;
2424 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2425 -- since this is a left side reference. We only do this for calls
2426 -- from the source program since we assume that compiler generated
2427 -- calls explicitly generate any required checks. We also need it
2428 -- only if we are doing standard validity checks, since clearly it
2429 -- is not needed if validity checks are off, and in subscript
2430 -- validity checking mode, all indexed components are checked with
2431 -- a call directly from Expand_N_Indexed_Component.
2433 if Comes_From_Source (N)
2434 and then Ekind (Formal) /= E_In_Parameter
2435 and then Validity_Checks_On
2436 and then Validity_Check_Default
2437 and then not Validity_Check_Subscripts
2438 then
2439 Check_Valid_Lvalue_Subscripts (Actual);
2440 end if;
2442 -- Mark any scalar OUT parameter that is a simple variable as no
2443 -- longer known to be valid (unless the type is always valid). This
2444 -- reflects the fact that if an OUT parameter is never set in a
2445 -- procedure, then it can become invalid on the procedure return.
2447 if Ekind (Formal) = E_Out_Parameter
2448 and then Is_Entity_Name (Actual)
2449 and then Ekind (Entity (Actual)) = E_Variable
2450 and then not Is_Known_Valid (Etype (Actual))
2451 then
2452 Set_Is_Known_Valid (Entity (Actual), False);
2453 end if;
2455 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2456 -- clear current values, since they can be clobbered. We are probably
2457 -- doing this in more places than we need to, but better safe than
2458 -- sorry when it comes to retaining bad current values!
2460 if Ekind (Formal) /= E_In_Parameter
2461 and then Is_Entity_Name (Actual)
2462 and then Present (Entity (Actual))
2463 then
2464 declare
2465 Ent : constant Entity_Id := Entity (Actual);
2466 Sav : Node_Id;
2468 begin
2469 -- For an OUT or IN OUT parameter that is an assignable entity,
2470 -- we do not want to clobber the Last_Assignment field, since
2471 -- if it is set, it was precisely because it is indeed an OUT
2472 -- or IN OUT parameter! We do reset the Is_Known_Valid flag
2473 -- since the subprogram could have returned in invalid value.
2475 if (Ekind (Formal) = E_Out_Parameter
2476 or else
2477 Ekind (Formal) = E_In_Out_Parameter)
2478 and then Is_Assignable (Ent)
2479 then
2480 Sav := Last_Assignment (Ent);
2481 Kill_Current_Values (Ent);
2482 Set_Last_Assignment (Ent, Sav);
2483 Set_Is_Known_Valid (Ent, False);
2485 -- For all other cases, just kill the current values
2487 else
2488 Kill_Current_Values (Ent);
2489 end if;
2490 end;
2491 end if;
2493 -- If the formal is class wide and the actual is an aggregate, force
2494 -- evaluation so that the back end who does not know about class-wide
2495 -- type, does not generate a temporary of the wrong size.
2497 if not Is_Class_Wide_Type (Etype (Formal)) then
2498 null;
2500 elsif Nkind (Actual) = N_Aggregate
2501 or else (Nkind (Actual) = N_Qualified_Expression
2502 and then Nkind (Expression (Actual)) = N_Aggregate)
2503 then
2504 Force_Evaluation (Actual);
2505 end if;
2507 -- In a remote call, if the formal is of a class-wide type, check
2508 -- that the actual meets the requirements described in E.4(18).
2510 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2511 Insert_Action (Actual,
2512 Make_Transportable_Check (Loc,
2513 Duplicate_Subexpr_Move_Checks (Actual)));
2514 end if;
2516 -- This label is required when skipping extra actual generation for
2517 -- Unchecked_Union parameters.
2519 <<Skip_Extra_Actual_Generation>>
2521 Param_Count := Param_Count + 1;
2522 Next_Actual (Actual);
2523 Next_Formal (Formal);
2524 end loop;
2526 -- If we are expanding a rhs of an assignment we need to check if tag
2527 -- propagation is needed. You might expect this processing to be in
2528 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2529 -- assignment might be transformed to a declaration for an unconstrained
2530 -- value if the expression is classwide.
2532 if Nkind (N) = N_Function_Call
2533 and then Is_Tag_Indeterminate (N)
2534 and then Is_Entity_Name (Name (N))
2535 then
2536 declare
2537 Ass : Node_Id := Empty;
2539 begin
2540 if Nkind (Parent (N)) = N_Assignment_Statement then
2541 Ass := Parent (N);
2543 elsif Nkind (Parent (N)) = N_Qualified_Expression
2544 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2545 then
2546 Ass := Parent (Parent (N));
2548 elsif Nkind (Parent (N)) = N_Explicit_Dereference
2549 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2550 then
2551 Ass := Parent (Parent (N));
2552 end if;
2554 if Present (Ass)
2555 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2556 then
2557 if Is_Access_Type (Etype (N)) then
2558 if Designated_Type (Etype (N)) /=
2559 Root_Type (Etype (Name (Ass)))
2560 then
2561 Error_Msg_NE
2562 ("tag-indeterminate expression "
2563 & " must have designated type& (RM 5.2 (6))",
2564 N, Root_Type (Etype (Name (Ass))));
2565 else
2566 Propagate_Tag (Name (Ass), N);
2567 end if;
2569 elsif Etype (N) /= Root_Type (Etype (Name (Ass))) then
2570 Error_Msg_NE
2571 ("tag-indeterminate expression must have type&"
2572 & "(RM 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
2574 else
2575 Propagate_Tag (Name (Ass), N);
2576 end if;
2578 -- The call will be rewritten as a dispatching call, and
2579 -- expanded as such.
2581 return;
2582 end if;
2583 end;
2584 end if;
2586 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2587 -- it to point to the correct secondary virtual table
2589 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2590 and then CW_Interface_Formals_Present
2591 then
2592 Expand_Interface_Actuals (N);
2593 end if;
2595 -- Deals with Dispatch_Call if we still have a call, before expanding
2596 -- extra actuals since this will be done on the re-analysis of the
2597 -- dispatching call. Note that we do not try to shorten the actual
2598 -- list for a dispatching call, it would not make sense to do so.
2599 -- Expansion of dispatching calls is suppressed when VM_Target, because
2600 -- the VM back-ends directly handle the generation of dispatching
2601 -- calls and would have to undo any expansion to an indirect call.
2603 if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement)
2604 and then Present (Controlling_Argument (N))
2605 then
2606 if Tagged_Type_Expansion then
2607 Expand_Dispatching_Call (N);
2609 -- The following return is worrisome. Is it really OK to
2610 -- skip all remaining processing in this procedure ???
2612 return;
2614 else
2615 Apply_Tag_Checks (N);
2617 -- Expansion of a dispatching call results in an indirect call,
2618 -- which in turn causes current values to be killed (see
2619 -- Resolve_Call), so on VM targets we do the call here to ensure
2620 -- consistent warnings between VM and non-VM targets.
2622 Kill_Current_Values;
2623 end if;
2624 end if;
2626 -- Similarly, expand calls to RCI subprograms on which pragma
2627 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2628 -- later. Do this only when the call comes from source since we do
2629 -- not want such a rewriting to occur in expanded code.
2631 if Is_All_Remote_Call (N) then
2632 Expand_All_Calls_Remote_Subprogram_Call (N);
2634 -- Similarly, do not add extra actuals for an entry call whose entity
2635 -- is a protected procedure, or for an internal protected subprogram
2636 -- call, because it will be rewritten as a protected subprogram call
2637 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2639 elsif Is_Protected_Type (Scope (Subp))
2640 and then (Ekind (Subp) = E_Procedure
2641 or else Ekind (Subp) = E_Function)
2642 then
2643 null;
2645 -- During that loop we gathered the extra actuals (the ones that
2646 -- correspond to Extra_Formals), so now they can be appended.
2648 else
2649 while Is_Non_Empty_List (Extra_Actuals) loop
2650 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2651 end loop;
2652 end if;
2654 -- At this point we have all the actuals, so this is the point at
2655 -- which the various expansion activities for actuals is carried out.
2657 Expand_Actuals (N, Subp);
2659 -- If the subprogram is a renaming, or if it is inherited, replace it
2660 -- in the call with the name of the actual subprogram being called.
2661 -- If this is a dispatching call, the run-time decides what to call.
2662 -- The Alias attribute does not apply to entries.
2664 if Nkind (N) /= N_Entry_Call_Statement
2665 and then No (Controlling_Argument (N))
2666 and then Present (Parent_Subp)
2667 then
2668 if Present (Inherited_From_Formal (Subp)) then
2669 Parent_Subp := Inherited_From_Formal (Subp);
2670 else
2671 Parent_Subp := Ultimate_Alias (Parent_Subp);
2672 end if;
2674 -- The below setting of Entity is suspect, see F109-018 discussion???
2676 Set_Entity (Name (N), Parent_Subp);
2678 if Is_Abstract_Subprogram (Parent_Subp)
2679 and then not In_Instance
2680 then
2681 Error_Msg_NE
2682 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2683 end if;
2685 -- Inspect all formals of derived subprogram Subp. Compare parameter
2686 -- types with the parent subprogram and check whether an actual may
2687 -- need a type conversion to the corresponding formal of the parent
2688 -- subprogram.
2690 -- Not clear whether intrinsic subprograms need such conversions. ???
2692 if not Is_Intrinsic_Subprogram (Parent_Subp)
2693 or else Is_Generic_Instance (Parent_Subp)
2694 then
2695 declare
2696 procedure Convert (Act : Node_Id; Typ : Entity_Id);
2697 -- Rewrite node Act as a type conversion of Act to Typ. Analyze
2698 -- and resolve the newly generated construct.
2700 -------------
2701 -- Convert --
2702 -------------
2704 procedure Convert (Act : Node_Id; Typ : Entity_Id) is
2705 begin
2706 Rewrite (Act, OK_Convert_To (Typ, Relocate_Node (Act)));
2707 Analyze (Act);
2708 Resolve (Act, Typ);
2709 end Convert;
2711 -- Local variables
2713 Actual_Typ : Entity_Id;
2714 Formal_Typ : Entity_Id;
2715 Parent_Typ : Entity_Id;
2717 begin
2718 Actual := First_Actual (N);
2719 Formal := First_Formal (Subp);
2720 Parent_Formal := First_Formal (Parent_Subp);
2721 while Present (Formal) loop
2722 Actual_Typ := Etype (Actual);
2723 Formal_Typ := Etype (Formal);
2724 Parent_Typ := Etype (Parent_Formal);
2726 -- For an IN parameter of a scalar type, the parent formal
2727 -- type and derived formal type differ or the parent formal
2728 -- type and actual type do not match statically.
2730 if Is_Scalar_Type (Formal_Typ)
2731 and then Ekind (Formal) = E_In_Parameter
2732 and then Formal_Typ /= Parent_Typ
2733 and then
2734 not Subtypes_Statically_Match (Parent_Typ, Actual_Typ)
2735 and then not Raises_Constraint_Error (Actual)
2736 then
2737 Convert (Actual, Parent_Typ);
2738 Enable_Range_Check (Actual);
2740 -- If the actual has been marked as requiring a range
2741 -- check, then generate it here.
2743 if Do_Range_Check (Actual) then
2744 Set_Do_Range_Check (Actual, False);
2745 Generate_Range_Check
2746 (Actual, Etype (Formal), CE_Range_Check_Failed);
2747 end if;
2749 -- For access types, the parent formal type and actual type
2750 -- differ.
2752 elsif Is_Access_Type (Formal_Typ)
2753 and then Base_Type (Parent_Typ) /= Base_Type (Actual_Typ)
2754 then
2755 if Ekind (Formal) /= E_In_Parameter then
2756 Convert (Actual, Parent_Typ);
2758 elsif Ekind (Parent_Typ) = E_Anonymous_Access_Type
2759 and then Designated_Type (Parent_Typ) /=
2760 Designated_Type (Actual_Typ)
2761 and then not Is_Controlling_Formal (Formal)
2762 then
2763 -- This unchecked conversion is not necessary unless
2764 -- inlining is enabled, because in that case the type
2765 -- mismatch may become visible in the body about to be
2766 -- inlined.
2768 Rewrite (Actual,
2769 Unchecked_Convert_To (Parent_Typ,
2770 Relocate_Node (Actual)));
2771 Analyze (Actual);
2772 Resolve (Actual, Parent_Typ);
2773 end if;
2775 -- For array and record types, the parent formal type and
2776 -- derived formal type have different sizes or pragma Pack
2777 -- status.
2779 elsif ((Is_Array_Type (Formal_Typ)
2780 and then Is_Array_Type (Parent_Typ))
2781 or else
2782 (Is_Record_Type (Formal_Typ)
2783 and then Is_Record_Type (Parent_Typ)))
2784 and then
2785 (Esize (Formal_Typ) /= Esize (Parent_Typ)
2786 or else Has_Pragma_Pack (Formal_Typ) /=
2787 Has_Pragma_Pack (Parent_Typ))
2788 then
2789 Convert (Actual, Parent_Typ);
2790 end if;
2792 Next_Actual (Actual);
2793 Next_Formal (Formal);
2794 Next_Formal (Parent_Formal);
2795 end loop;
2796 end;
2797 end if;
2799 Orig_Subp := Subp;
2800 Subp := Parent_Subp;
2801 end if;
2803 -- Check for violation of No_Abort_Statements
2805 if Is_RTE (Subp, RE_Abort_Task) then
2806 Check_Restriction (No_Abort_Statements, N);
2808 -- Check for violation of No_Dynamic_Attachment
2810 elsif RTU_Loaded (Ada_Interrupts)
2811 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2812 Is_RTE (Subp, RE_Is_Attached) or else
2813 Is_RTE (Subp, RE_Current_Handler) or else
2814 Is_RTE (Subp, RE_Attach_Handler) or else
2815 Is_RTE (Subp, RE_Exchange_Handler) or else
2816 Is_RTE (Subp, RE_Detach_Handler) or else
2817 Is_RTE (Subp, RE_Reference))
2818 then
2819 Check_Restriction (No_Dynamic_Attachment, N);
2820 end if;
2822 -- Deal with case where call is an explicit dereference
2824 if Nkind (Name (N)) = N_Explicit_Dereference then
2826 -- Handle case of access to protected subprogram type
2828 if Is_Access_Protected_Subprogram_Type
2829 (Base_Type (Etype (Prefix (Name (N)))))
2830 then
2831 -- If this is a call through an access to protected operation,
2832 -- the prefix has the form (object'address, operation'access).
2833 -- Rewrite as a for other protected calls: the object is the
2834 -- first parameter of the list of actuals.
2836 declare
2837 Call : Node_Id;
2838 Parm : List_Id;
2839 Nam : Node_Id;
2840 Obj : Node_Id;
2841 Ptr : constant Node_Id := Prefix (Name (N));
2843 T : constant Entity_Id :=
2844 Equivalent_Type (Base_Type (Etype (Ptr)));
2846 D_T : constant Entity_Id :=
2847 Designated_Type (Base_Type (Etype (Ptr)));
2849 begin
2850 Obj :=
2851 Make_Selected_Component (Loc,
2852 Prefix => Unchecked_Convert_To (T, Ptr),
2853 Selector_Name =>
2854 New_Occurrence_Of (First_Entity (T), Loc));
2856 Nam :=
2857 Make_Selected_Component (Loc,
2858 Prefix => Unchecked_Convert_To (T, Ptr),
2859 Selector_Name =>
2860 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2862 Nam :=
2863 Make_Explicit_Dereference (Loc,
2864 Prefix => Nam);
2866 if Present (Parameter_Associations (N)) then
2867 Parm := Parameter_Associations (N);
2868 else
2869 Parm := New_List;
2870 end if;
2872 Prepend (Obj, Parm);
2874 if Etype (D_T) = Standard_Void_Type then
2875 Call :=
2876 Make_Procedure_Call_Statement (Loc,
2877 Name => Nam,
2878 Parameter_Associations => Parm);
2879 else
2880 Call :=
2881 Make_Function_Call (Loc,
2882 Name => Nam,
2883 Parameter_Associations => Parm);
2884 end if;
2886 Set_First_Named_Actual (Call, First_Named_Actual (N));
2887 Set_Etype (Call, Etype (D_T));
2889 -- We do not re-analyze the call to avoid infinite recursion.
2890 -- We analyze separately the prefix and the object, and set
2891 -- the checks on the prefix that would otherwise be emitted
2892 -- when resolving a call.
2894 Rewrite (N, Call);
2895 Analyze (Nam);
2896 Apply_Access_Check (Nam);
2897 Analyze (Obj);
2898 return;
2899 end;
2900 end if;
2901 end if;
2903 -- If this is a call to an intrinsic subprogram, then perform the
2904 -- appropriate expansion to the corresponding tree node and we
2905 -- are all done (since after that the call is gone!)
2907 -- In the case where the intrinsic is to be processed by the back end,
2908 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2909 -- since the idea in this case is to pass the call unchanged.
2910 -- If the intrinsic is an inherited unchecked conversion, and the
2911 -- derived type is the target type of the conversion, we must retain
2912 -- it as the return type of the expression. Otherwise the expansion
2913 -- below, which uses the parent operation, will yield the wrong type.
2915 if Is_Intrinsic_Subprogram (Subp) then
2916 Expand_Intrinsic_Call (N, Subp);
2918 if Nkind (N) = N_Unchecked_Type_Conversion
2919 and then Parent_Subp /= Orig_Subp
2920 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2921 then
2922 Set_Etype (N, Etype (Orig_Subp));
2923 end if;
2925 return;
2926 end if;
2928 if Ekind_In (Subp, E_Function, E_Procedure) then
2930 -- We perform two simple optimization on calls:
2932 -- a) replace calls to null procedures unconditionally;
2934 -- b) for To_Address, just do an unchecked conversion. Not only is
2935 -- this efficient, but it also avoids order of elaboration problems
2936 -- when address clauses are inlined (address expression elaborated
2937 -- at the wrong point).
2939 -- We perform these optimization regardless of whether we are in the
2940 -- main unit or in a unit in the context of the main unit, to ensure
2941 -- that tree generated is the same in both cases, for Inspector use.
2943 if Is_RTE (Subp, RE_To_Address) then
2944 Rewrite (N,
2945 Unchecked_Convert_To
2946 (RTE (RE_Address), Relocate_Node (First_Actual (N))));
2947 return;
2949 elsif Is_Null_Procedure (Subp) then
2950 Rewrite (N, Make_Null_Statement (Loc));
2951 return;
2952 end if;
2954 if Is_Inlined (Subp) then
2956 Inlined_Subprogram : declare
2957 Bod : Node_Id;
2958 Must_Inline : Boolean := False;
2959 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2960 Scop : constant Entity_Id := Scope (Subp);
2962 function In_Unfrozen_Instance return Boolean;
2963 -- If the subprogram comes from an instance in the same unit,
2964 -- and the instance is not yet frozen, inlining might trigger
2965 -- order-of-elaboration problems in gigi.
2967 --------------------------
2968 -- In_Unfrozen_Instance --
2969 --------------------------
2971 function In_Unfrozen_Instance return Boolean is
2972 S : Entity_Id;
2974 begin
2975 S := Scop;
2976 while Present (S)
2977 and then S /= Standard_Standard
2978 loop
2979 if Is_Generic_Instance (S)
2980 and then Present (Freeze_Node (S))
2981 and then not Analyzed (Freeze_Node (S))
2982 then
2983 return True;
2984 end if;
2986 S := Scope (S);
2987 end loop;
2989 return False;
2990 end In_Unfrozen_Instance;
2992 -- Start of processing for Inlined_Subprogram
2994 begin
2995 -- Verify that the body to inline has already been seen, and
2996 -- that if the body is in the current unit the inlining does
2997 -- not occur earlier. This avoids order-of-elaboration problems
2998 -- in the back end.
3000 -- This should be documented in sinfo/einfo ???
3002 if No (Spec)
3003 or else Nkind (Spec) /= N_Subprogram_Declaration
3004 or else No (Body_To_Inline (Spec))
3005 then
3006 Must_Inline := False;
3008 -- If this an inherited function that returns a private type,
3009 -- do not inline if the full view is an unconstrained array,
3010 -- because such calls cannot be inlined.
3012 elsif Present (Orig_Subp)
3013 and then Is_Array_Type (Etype (Orig_Subp))
3014 and then not Is_Constrained (Etype (Orig_Subp))
3015 then
3016 Must_Inline := False;
3018 elsif In_Unfrozen_Instance then
3019 Must_Inline := False;
3021 else
3022 Bod := Body_To_Inline (Spec);
3024 if (In_Extended_Main_Code_Unit (N)
3025 or else In_Extended_Main_Code_Unit (Parent (N))
3026 or else Has_Pragma_Inline_Always (Subp))
3027 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
3028 or else
3029 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
3030 then
3031 Must_Inline := True;
3033 -- If we are compiling a package body that is not the main
3034 -- unit, it must be for inlining/instantiation purposes,
3035 -- in which case we inline the call to insure that the same
3036 -- temporaries are generated when compiling the body by
3037 -- itself. Otherwise link errors can occur.
3039 -- If the function being called is itself in the main unit,
3040 -- we cannot inline, because there is a risk of double
3041 -- elaboration and/or circularity: the inlining can make
3042 -- visible a private entity in the body of the main unit,
3043 -- that gigi will see before its sees its proper definition.
3045 elsif not (In_Extended_Main_Code_Unit (N))
3046 and then In_Package_Body
3047 then
3048 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
3049 end if;
3050 end if;
3052 if Must_Inline then
3053 Expand_Inlined_Call (N, Subp, Orig_Subp);
3055 else
3056 -- Let the back end handle it
3058 Add_Inlined_Body (Subp);
3060 if Front_End_Inlining
3061 and then Nkind (Spec) = N_Subprogram_Declaration
3062 and then (In_Extended_Main_Code_Unit (N))
3063 and then No (Body_To_Inline (Spec))
3064 and then not Has_Completion (Subp)
3065 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
3066 then
3067 Cannot_Inline
3068 ("cannot inline& (body not seen yet)?", N, Subp);
3069 end if;
3070 end if;
3071 end Inlined_Subprogram;
3072 end if;
3073 end if;
3075 -- Check for protected subprogram. This is either an intra-object call,
3076 -- or a protected function call. Protected procedure calls are rewritten
3077 -- as entry calls and handled accordingly.
3079 -- In Ada 2005, this may be an indirect call to an access parameter that
3080 -- is an access_to_subprogram. In that case the anonymous type has a
3081 -- scope that is a protected operation, but the call is a regular one.
3082 -- In either case do not expand call if subprogram is eliminated.
3084 Scop := Scope (Subp);
3086 if Nkind (N) /= N_Entry_Call_Statement
3087 and then Is_Protected_Type (Scop)
3088 and then Ekind (Subp) /= E_Subprogram_Type
3089 and then not Is_Eliminated (Subp)
3090 then
3091 -- If the call is an internal one, it is rewritten as a call to the
3092 -- corresponding unprotected subprogram.
3094 Expand_Protected_Subprogram_Call (N, Subp, Scop);
3095 end if;
3097 -- Functions returning controlled objects need special attention:
3098 -- if the return type is limited, the context is an initialization
3099 -- and different processing applies. If the call is to a protected
3100 -- function, the expansion above will call Expand_Call recusively.
3101 -- To prevent a double attachment, check that the current call is
3102 -- not a rewriting of a protected function call.
3104 if Needs_Finalization (Etype (Subp))
3105 and then not Is_Inherently_Limited_Type (Etype (Subp))
3106 and then
3107 (No (First_Formal (Subp))
3108 or else
3109 not Is_Concurrent_Record_Type (Etype (First_Formal (Subp))))
3110 then
3111 Expand_Ctrl_Function_Call (N);
3112 end if;
3114 -- Test for First_Optional_Parameter, and if so, truncate parameter list
3115 -- if there are optional parameters at the trailing end.
3116 -- Note: we never delete procedures for call via a pointer.
3118 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
3119 and then Present (First_Optional_Parameter (Subp))
3120 then
3121 declare
3122 Last_Keep_Arg : Node_Id;
3124 begin
3125 -- Last_Keep_Arg will hold the last actual that should be kept.
3126 -- If it remains empty at the end, it means that all parameters
3127 -- are optional.
3129 Last_Keep_Arg := Empty;
3131 -- Find first optional parameter, must be present since we checked
3132 -- the validity of the parameter before setting it.
3134 Formal := First_Formal (Subp);
3135 Actual := First_Actual (N);
3136 while Formal /= First_Optional_Parameter (Subp) loop
3137 Last_Keep_Arg := Actual;
3138 Next_Formal (Formal);
3139 Next_Actual (Actual);
3140 end loop;
3142 -- We have Formal and Actual pointing to the first potentially
3143 -- droppable argument. We can drop all the trailing arguments
3144 -- whose actual matches the default. Note that we know that all
3145 -- remaining formals have defaults, because we checked that this
3146 -- requirement was met before setting First_Optional_Parameter.
3148 -- We use Fully_Conformant_Expressions to check for identity
3149 -- between formals and actuals, which may miss some cases, but
3150 -- on the other hand, this is only an optimization (if we fail
3151 -- to truncate a parameter it does not affect functionality).
3152 -- So if the default is 3 and the actual is 1+2, we consider
3153 -- them unequal, which hardly seems worrisome.
3155 while Present (Formal) loop
3156 if not Fully_Conformant_Expressions
3157 (Actual, Default_Value (Formal))
3158 then
3159 Last_Keep_Arg := Actual;
3160 end if;
3162 Next_Formal (Formal);
3163 Next_Actual (Actual);
3164 end loop;
3166 -- If no arguments, delete entire list, this is the easy case
3168 if No (Last_Keep_Arg) then
3169 Set_Parameter_Associations (N, No_List);
3170 Set_First_Named_Actual (N, Empty);
3172 -- Case where at the last retained argument is positional. This
3173 -- is also an easy case, since the retained arguments are already
3174 -- in the right form, and we don't need to worry about the order
3175 -- of arguments that get eliminated.
3177 elsif Is_List_Member (Last_Keep_Arg) then
3178 while Present (Next (Last_Keep_Arg)) loop
3179 Discard_Node (Remove_Next (Last_Keep_Arg));
3180 end loop;
3182 Set_First_Named_Actual (N, Empty);
3184 -- This is the annoying case where the last retained argument
3185 -- is a named parameter. Since the original arguments are not
3186 -- in declaration order, we may have to delete some fairly
3187 -- random collection of arguments.
3189 else
3190 declare
3191 Temp : Node_Id;
3192 Passoc : Node_Id;
3194 begin
3195 -- First step, remove all the named parameters from the
3196 -- list (they are still chained using First_Named_Actual
3197 -- and Next_Named_Actual, so we have not lost them!)
3199 Temp := First (Parameter_Associations (N));
3201 -- Case of all parameters named, remove them all
3203 if Nkind (Temp) = N_Parameter_Association then
3204 while Is_Non_Empty_List (Parameter_Associations (N)) loop
3205 Temp := Remove_Head (Parameter_Associations (N));
3206 end loop;
3208 -- Case of mixed positional/named, remove named parameters
3210 else
3211 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3212 Next (Temp);
3213 end loop;
3215 while Present (Next (Temp)) loop
3216 Remove (Next (Temp));
3217 end loop;
3218 end if;
3220 -- Now we loop through the named parameters, till we get
3221 -- to the last one to be retained, adding them to the list.
3222 -- Note that the Next_Named_Actual list does not need to be
3223 -- touched since we are only reordering them on the actual
3224 -- parameter association list.
3226 Passoc := Parent (First_Named_Actual (N));
3227 loop
3228 Temp := Relocate_Node (Passoc);
3229 Append_To
3230 (Parameter_Associations (N), Temp);
3231 exit when
3232 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3233 Passoc := Parent (Next_Named_Actual (Passoc));
3234 end loop;
3236 Set_Next_Named_Actual (Temp, Empty);
3238 loop
3239 Temp := Next_Named_Actual (Passoc);
3240 exit when No (Temp);
3241 Set_Next_Named_Actual
3242 (Passoc, Next_Named_Actual (Parent (Temp)));
3243 end loop;
3244 end;
3246 end if;
3247 end;
3248 end if;
3249 end Expand_Call;
3251 --------------------------
3252 -- Expand_Inlined_Call --
3253 --------------------------
3255 procedure Expand_Inlined_Call
3256 (N : Node_Id;
3257 Subp : Entity_Id;
3258 Orig_Subp : Entity_Id)
3260 Loc : constant Source_Ptr := Sloc (N);
3261 Is_Predef : constant Boolean :=
3262 Is_Predefined_File_Name
3263 (Unit_File_Name (Get_Source_Unit (Subp)));
3264 Orig_Bod : constant Node_Id :=
3265 Body_To_Inline (Unit_Declaration_Node (Subp));
3267 Blk : Node_Id;
3268 Bod : Node_Id;
3269 Decl : Node_Id;
3270 Decls : constant List_Id := New_List;
3271 Exit_Lab : Entity_Id := Empty;
3272 F : Entity_Id;
3273 A : Node_Id;
3274 Lab_Decl : Node_Id;
3275 Lab_Id : Node_Id;
3276 New_A : Node_Id;
3277 Num_Ret : Int := 0;
3278 Ret_Type : Entity_Id;
3279 Targ : Node_Id;
3280 Targ1 : Node_Id;
3281 Temp : Entity_Id;
3282 Temp_Typ : Entity_Id;
3284 Return_Object : Entity_Id := Empty;
3285 -- Entity in declaration in an extended_return_statement
3287 Is_Unc : constant Boolean :=
3288 Is_Array_Type (Etype (Subp))
3289 and then not Is_Constrained (Etype (Subp));
3290 -- If the type returned by the function is unconstrained and the call
3291 -- can be inlined, special processing is required.
3293 procedure Make_Exit_Label;
3294 -- Build declaration for exit label to be used in Return statements,
3295 -- sets Exit_Lab (the label node) and Lab_Decl (corresponding implicit
3296 -- declaration). Does nothing if Exit_Lab already set.
3298 function Process_Formals (N : Node_Id) return Traverse_Result;
3299 -- Replace occurrence of a formal with the corresponding actual, or the
3300 -- thunk generated for it.
3302 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3303 -- If the call being expanded is that of an internal subprogram, set the
3304 -- sloc of the generated block to that of the call itself, so that the
3305 -- expansion is skipped by the "next" command in gdb.
3306 -- Same processing for a subprogram in a predefined file, e.g.
3307 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change to
3308 -- simplify our own development.
3310 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3311 -- If the function body is a single expression, replace call with
3312 -- expression, else insert block appropriately.
3314 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3315 -- If procedure body has no local variables, inline body without
3316 -- creating block, otherwise rewrite call with block.
3318 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3319 -- Determine whether a formal parameter is used only once in Orig_Bod
3321 ---------------------
3322 -- Make_Exit_Label --
3323 ---------------------
3325 procedure Make_Exit_Label is
3326 Lab_Ent : Entity_Id;
3327 begin
3328 if No (Exit_Lab) then
3329 Lab_Ent := Make_Temporary (Loc, 'L');
3330 Lab_Id := New_Reference_To (Lab_Ent, Loc);
3331 Exit_Lab := Make_Label (Loc, Lab_Id);
3332 Lab_Decl :=
3333 Make_Implicit_Label_Declaration (Loc,
3334 Defining_Identifier => Lab_Ent,
3335 Label_Construct => Exit_Lab);
3336 end if;
3337 end Make_Exit_Label;
3339 ---------------------
3340 -- Process_Formals --
3341 ---------------------
3343 function Process_Formals (N : Node_Id) return Traverse_Result is
3344 A : Entity_Id;
3345 E : Entity_Id;
3346 Ret : Node_Id;
3348 begin
3349 if Is_Entity_Name (N)
3350 and then Present (Entity (N))
3351 then
3352 E := Entity (N);
3354 if Is_Formal (E)
3355 and then Scope (E) = Subp
3356 then
3357 A := Renamed_Object (E);
3359 -- Rewrite the occurrence of the formal into an occurrence of
3360 -- the actual. Also establish visibility on the proper view of
3361 -- the actual's subtype for the body's context (if the actual's
3362 -- subtype is private at the call point but its full view is
3363 -- visible to the body, then the inlined tree here must be
3364 -- analyzed with the full view).
3366 if Is_Entity_Name (A) then
3367 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3368 Check_Private_View (N);
3370 elsif Nkind (A) = N_Defining_Identifier then
3371 Rewrite (N, New_Occurrence_Of (A, Loc));
3372 Check_Private_View (N);
3374 -- Numeric literal
3376 else
3377 Rewrite (N, New_Copy (A));
3378 end if;
3379 end if;
3380 return Skip;
3382 elsif Is_Entity_Name (N)
3383 and then Present (Return_Object)
3384 and then Chars (N) = Chars (Return_Object)
3385 then
3386 -- Occurrence within an extended return statement. The return
3387 -- object is local to the body been inlined, and thus the generic
3388 -- copy is not analyzed yet, so we match by name, and replace it
3389 -- with target of call.
3391 if Nkind (Targ) = N_Defining_Identifier then
3392 Rewrite (N, New_Occurrence_Of (Targ, Loc));
3393 else
3394 Rewrite (N, New_Copy_Tree (Targ));
3395 end if;
3397 return Skip;
3399 elsif Nkind (N) = N_Simple_Return_Statement then
3400 if No (Expression (N)) then
3401 Make_Exit_Label;
3402 Rewrite (N,
3403 Make_Goto_Statement (Loc, Name => New_Copy (Lab_Id)));
3405 else
3406 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3407 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3408 then
3409 -- Function body is a single expression. No need for
3410 -- exit label.
3412 null;
3414 else
3415 Num_Ret := Num_Ret + 1;
3416 Make_Exit_Label;
3417 end if;
3419 -- Because of the presence of private types, the views of the
3420 -- expression and the context may be different, so place an
3421 -- unchecked conversion to the context type to avoid spurious
3422 -- errors, e.g. when the expression is a numeric literal and
3423 -- the context is private. If the expression is an aggregate,
3424 -- use a qualified expression, because an aggregate is not a
3425 -- legal argument of a conversion.
3427 if Nkind_In (Expression (N), N_Aggregate, N_Null) then
3428 Ret :=
3429 Make_Qualified_Expression (Sloc (N),
3430 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3431 Expression => Relocate_Node (Expression (N)));
3432 else
3433 Ret :=
3434 Unchecked_Convert_To
3435 (Ret_Type, Relocate_Node (Expression (N)));
3436 end if;
3438 if Nkind (Targ) = N_Defining_Identifier then
3439 Rewrite (N,
3440 Make_Assignment_Statement (Loc,
3441 Name => New_Occurrence_Of (Targ, Loc),
3442 Expression => Ret));
3443 else
3444 Rewrite (N,
3445 Make_Assignment_Statement (Loc,
3446 Name => New_Copy (Targ),
3447 Expression => Ret));
3448 end if;
3450 Set_Assignment_OK (Name (N));
3452 if Present (Exit_Lab) then
3453 Insert_After (N,
3454 Make_Goto_Statement (Loc,
3455 Name => New_Copy (Lab_Id)));
3456 end if;
3457 end if;
3459 return OK;
3461 elsif Nkind (N) = N_Extended_Return_Statement then
3463 -- An extended return becomes a block whose first statement is
3464 -- the assignment of the initial expression of the return object
3465 -- to the target of the call itself.
3467 declare
3468 Return_Decl : constant Entity_Id :=
3469 First (Return_Object_Declarations (N));
3470 Assign : Node_Id;
3472 begin
3473 Return_Object := Defining_Identifier (Return_Decl);
3475 if Present (Expression (Return_Decl)) then
3476 if Nkind (Targ) = N_Defining_Identifier then
3477 Assign :=
3478 Make_Assignment_Statement (Loc,
3479 Name => New_Occurrence_Of (Targ, Loc),
3480 Expression => Expression (Return_Decl));
3481 else
3482 Assign :=
3483 Make_Assignment_Statement (Loc,
3484 Name => New_Copy (Targ),
3485 Expression => Expression (Return_Decl));
3486 end if;
3488 Set_Assignment_OK (Name (Assign));
3489 Prepend (Assign,
3490 Statements (Handled_Statement_Sequence (N)));
3491 end if;
3493 Rewrite (N,
3494 Make_Block_Statement (Loc,
3495 Handled_Statement_Sequence =>
3496 Handled_Statement_Sequence (N)));
3498 return OK;
3499 end;
3501 -- Remove pragma Unreferenced since it may refer to formals that
3502 -- are not visible in the inlined body, and in any case we will
3503 -- not be posting warnings on the inlined body so it is unneeded.
3505 elsif Nkind (N) = N_Pragma
3506 and then Pragma_Name (N) = Name_Unreferenced
3507 then
3508 Rewrite (N, Make_Null_Statement (Sloc (N)));
3509 return OK;
3511 else
3512 return OK;
3513 end if;
3514 end Process_Formals;
3516 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3518 ------------------
3519 -- Process_Sloc --
3520 ------------------
3522 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3523 begin
3524 if not Debug_Generated_Code then
3525 Set_Sloc (Nod, Sloc (N));
3526 Set_Comes_From_Source (Nod, False);
3527 end if;
3529 return OK;
3530 end Process_Sloc;
3532 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3534 ---------------------------
3535 -- Rewrite_Function_Call --
3536 ---------------------------
3538 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3539 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3540 Fst : constant Node_Id := First (Statements (HSS));
3542 begin
3543 -- Optimize simple case: function body is a single return statement,
3544 -- which has been expanded into an assignment.
3546 if Is_Empty_List (Declarations (Blk))
3547 and then Nkind (Fst) = N_Assignment_Statement
3548 and then No (Next (Fst))
3549 then
3551 -- The function call may have been rewritten as the temporary
3552 -- that holds the result of the call, in which case remove the
3553 -- now useless declaration.
3555 if Nkind (N) = N_Identifier
3556 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3557 then
3558 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3559 end if;
3561 Rewrite (N, Expression (Fst));
3563 elsif Nkind (N) = N_Identifier
3564 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3565 then
3566 -- The block assigns the result of the call to the temporary
3568 Insert_After (Parent (Entity (N)), Blk);
3570 elsif Nkind (Parent (N)) = N_Assignment_Statement
3571 and then
3572 (Is_Entity_Name (Name (Parent (N)))
3573 or else
3574 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3575 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3576 then
3577 -- Replace assignment with the block
3579 declare
3580 Original_Assignment : constant Node_Id := Parent (N);
3582 begin
3583 -- Preserve the original assignment node to keep the complete
3584 -- assignment subtree consistent enough for Analyze_Assignment
3585 -- to proceed (specifically, the original Lhs node must still
3586 -- have an assignment statement as its parent).
3588 -- We cannot rely on Original_Node to go back from the block
3589 -- node to the assignment node, because the assignment might
3590 -- already be a rewrite substitution.
3592 Discard_Node (Relocate_Node (Original_Assignment));
3593 Rewrite (Original_Assignment, Blk);
3594 end;
3596 elsif Nkind (Parent (N)) = N_Object_Declaration then
3597 Set_Expression (Parent (N), Empty);
3598 Insert_After (Parent (N), Blk);
3600 elsif Is_Unc then
3601 Insert_Before (Parent (N), Blk);
3602 end if;
3603 end Rewrite_Function_Call;
3605 ----------------------------
3606 -- Rewrite_Procedure_Call --
3607 ----------------------------
3609 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3610 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3611 begin
3612 -- If there is a transient scope for N, this will be the scope of the
3613 -- actions for N, and the statements in Blk need to be within this
3614 -- scope. For example, they need to have visibility on the constant
3615 -- declarations created for the formals.
3617 -- If N needs no transient scope, and if there are no declarations in
3618 -- the inlined body, we can do a little optimization and insert the
3619 -- statements for the body directly after N, and rewrite N to a
3620 -- null statement, instead of rewriting N into a full-blown block
3621 -- statement.
3623 if not Scope_Is_Transient
3624 and then Is_Empty_List (Declarations (Blk))
3625 then
3626 Insert_List_After (N, Statements (HSS));
3627 Rewrite (N, Make_Null_Statement (Loc));
3628 else
3629 Rewrite (N, Blk);
3630 end if;
3631 end Rewrite_Procedure_Call;
3633 -------------------------
3634 -- Formal_Is_Used_Once --
3635 -------------------------
3637 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3638 Use_Counter : Int := 0;
3640 function Count_Uses (N : Node_Id) return Traverse_Result;
3641 -- Traverse the tree and count the uses of the formal parameter.
3642 -- In this case, for optimization purposes, we do not need to
3643 -- continue the traversal once more than one use is encountered.
3645 ----------------
3646 -- Count_Uses --
3647 ----------------
3649 function Count_Uses (N : Node_Id) return Traverse_Result is
3650 begin
3651 -- The original node is an identifier
3653 if Nkind (N) = N_Identifier
3654 and then Present (Entity (N))
3656 -- Original node's entity points to the one in the copied body
3658 and then Nkind (Entity (N)) = N_Identifier
3659 and then Present (Entity (Entity (N)))
3661 -- The entity of the copied node is the formal parameter
3663 and then Entity (Entity (N)) = Formal
3664 then
3665 Use_Counter := Use_Counter + 1;
3667 if Use_Counter > 1 then
3669 -- Denote more than one use and abandon the traversal
3671 Use_Counter := 2;
3672 return Abandon;
3674 end if;
3675 end if;
3677 return OK;
3678 end Count_Uses;
3680 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3682 -- Start of processing for Formal_Is_Used_Once
3684 begin
3685 Count_Formal_Uses (Orig_Bod);
3686 return Use_Counter = 1;
3687 end Formal_Is_Used_Once;
3689 -- Start of processing for Expand_Inlined_Call
3691 begin
3693 -- Check for an illegal attempt to inline a recursive procedure. If the
3694 -- subprogram has parameters this is detected when trying to supply a
3695 -- binding for parameters that already have one. For parameterless
3696 -- subprograms this must be done explicitly.
3698 if In_Open_Scopes (Subp) then
3699 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3700 Set_Is_Inlined (Subp, False);
3701 return;
3702 end if;
3704 if Nkind (Orig_Bod) = N_Defining_Identifier
3705 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3706 then
3707 -- Subprogram is renaming_as_body. Calls occurring after the renaming
3708 -- can be replaced with calls to the renamed entity directly, because
3709 -- the subprograms are subtype conformant. If the renamed subprogram
3710 -- is an inherited operation, we must redo the expansion because
3711 -- implicit conversions may be needed. Similarly, if the renamed
3712 -- entity is inlined, expand the call for further optimizations.
3714 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3716 if Present (Alias (Orig_Bod))
3717 or else Is_Inlined (Orig_Bod)
3718 then
3719 Expand_Call (N);
3720 end if;
3722 return;
3723 end if;
3725 -- Use generic machinery to copy body of inlined subprogram, as if it
3726 -- were an instantiation, resetting source locations appropriately, so
3727 -- that nested inlined calls appear in the main unit.
3729 Save_Env (Subp, Empty);
3730 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3732 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3733 Blk :=
3734 Make_Block_Statement (Loc,
3735 Declarations => Declarations (Bod),
3736 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3738 if No (Declarations (Bod)) then
3739 Set_Declarations (Blk, New_List);
3740 end if;
3742 -- For the unconstrained case, capture the name of the local
3743 -- variable that holds the result. This must be the first declaration
3744 -- in the block, because its bounds cannot depend on local variables.
3745 -- Otherwise there is no way to declare the result outside of the
3746 -- block. Needless to say, in general the bounds will depend on the
3747 -- actuals in the call.
3749 if Is_Unc then
3750 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3751 end if;
3753 -- If this is a derived function, establish the proper return type
3755 if Present (Orig_Subp)
3756 and then Orig_Subp /= Subp
3757 then
3758 Ret_Type := Etype (Orig_Subp);
3759 else
3760 Ret_Type := Etype (Subp);
3761 end if;
3763 -- Create temporaries for the actuals that are expressions, or that
3764 -- are scalars and require copying to preserve semantics.
3766 F := First_Formal (Subp);
3767 A := First_Actual (N);
3768 while Present (F) loop
3769 if Present (Renamed_Object (F)) then
3770 Error_Msg_N ("cannot inline call to recursive subprogram", N);
3771 return;
3772 end if;
3774 -- If the argument may be a controlling argument in a call within
3775 -- the inlined body, we must preserve its classwide nature to insure
3776 -- that dynamic dispatching take place subsequently. If the formal
3777 -- has a constraint it must be preserved to retain the semantics of
3778 -- the body.
3780 if Is_Class_Wide_Type (Etype (F))
3781 or else (Is_Access_Type (Etype (F))
3782 and then
3783 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3784 then
3785 Temp_Typ := Etype (F);
3787 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3788 and then Etype (F) /= Base_Type (Etype (F))
3789 then
3790 Temp_Typ := Etype (F);
3792 else
3793 Temp_Typ := Etype (A);
3794 end if;
3796 -- If the actual is a simple name or a literal, no need to
3797 -- create a temporary, object can be used directly.
3799 -- If the actual is a literal and the formal has its address taken,
3800 -- we cannot pass the literal itself as an argument, so its value
3801 -- must be captured in a temporary.
3803 if (Is_Entity_Name (A)
3804 and then
3805 (not Is_Scalar_Type (Etype (A))
3806 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3808 -- When the actual is an identifier and the corresponding formal
3809 -- is used only once in the original body, the formal can be
3810 -- substituted directly with the actual parameter.
3812 or else (Nkind (A) = N_Identifier
3813 and then Formal_Is_Used_Once (F))
3815 or else
3816 (Nkind_In (A, N_Real_Literal,
3817 N_Integer_Literal,
3818 N_Character_Literal)
3819 and then not Address_Taken (F))
3820 then
3821 if Etype (F) /= Etype (A) then
3822 Set_Renamed_Object
3823 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3824 else
3825 Set_Renamed_Object (F, A);
3826 end if;
3828 else
3829 Temp := Make_Temporary (Loc, 'C');
3831 -- If the actual for an in/in-out parameter is a view conversion,
3832 -- make it into an unchecked conversion, given that an untagged
3833 -- type conversion is not a proper object for a renaming.
3835 -- In-out conversions that involve real conversions have already
3836 -- been transformed in Expand_Actuals.
3838 if Nkind (A) = N_Type_Conversion
3839 and then Ekind (F) /= E_In_Parameter
3840 then
3841 New_A :=
3842 Make_Unchecked_Type_Conversion (Loc,
3843 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3844 Expression => Relocate_Node (Expression (A)));
3846 elsif Etype (F) /= Etype (A) then
3847 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3848 Temp_Typ := Etype (F);
3850 else
3851 New_A := Relocate_Node (A);
3852 end if;
3854 Set_Sloc (New_A, Sloc (N));
3856 -- If the actual has a by-reference type, it cannot be copied, so
3857 -- its value is captured in a renaming declaration. Otherwise
3858 -- declare a local constant initialized with the actual.
3860 -- We also use a renaming declaration for expressions of an array
3861 -- type that is not bit-packed, both for efficiency reasons and to
3862 -- respect the semantics of the call: in most cases the original
3863 -- call will pass the parameter by reference, and thus the inlined
3864 -- code will have the same semantics.
3866 if Ekind (F) = E_In_Parameter
3867 and then not Is_Limited_Type (Etype (A))
3868 and then not Is_Tagged_Type (Etype (A))
3869 and then
3870 (not Is_Array_Type (Etype (A))
3871 or else not Is_Object_Reference (A)
3872 or else Is_Bit_Packed_Array (Etype (A)))
3873 then
3874 Decl :=
3875 Make_Object_Declaration (Loc,
3876 Defining_Identifier => Temp,
3877 Constant_Present => True,
3878 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3879 Expression => New_A);
3880 else
3881 Decl :=
3882 Make_Object_Renaming_Declaration (Loc,
3883 Defining_Identifier => Temp,
3884 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3885 Name => New_A);
3886 end if;
3888 Append (Decl, Decls);
3889 Set_Renamed_Object (F, Temp);
3890 end if;
3892 Next_Formal (F);
3893 Next_Actual (A);
3894 end loop;
3896 -- Establish target of function call. If context is not assignment or
3897 -- declaration, create a temporary as a target. The declaration for
3898 -- the temporary may be subsequently optimized away if the body is a
3899 -- single expression, or if the left-hand side of the assignment is
3900 -- simple enough, i.e. an entity or an explicit dereference of one.
3902 if Ekind (Subp) = E_Function then
3903 if Nkind (Parent (N)) = N_Assignment_Statement
3904 and then Is_Entity_Name (Name (Parent (N)))
3905 then
3906 Targ := Name (Parent (N));
3908 elsif Nkind (Parent (N)) = N_Assignment_Statement
3909 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3910 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3911 then
3912 Targ := Name (Parent (N));
3914 elsif Nkind (Parent (N)) = N_Object_Declaration
3915 and then Is_Limited_Type (Etype (Subp))
3916 then
3917 Targ := Defining_Identifier (Parent (N));
3919 else
3920 -- Replace call with temporary and create its declaration
3922 Temp := Make_Temporary (Loc, 'C');
3923 Set_Is_Internal (Temp);
3925 -- For the unconstrained case, the generated temporary has the
3926 -- same constrained declaration as the result variable. It may
3927 -- eventually be possible to remove that temporary and use the
3928 -- result variable directly.
3930 if Is_Unc then
3931 Decl :=
3932 Make_Object_Declaration (Loc,
3933 Defining_Identifier => Temp,
3934 Object_Definition =>
3935 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3937 Replace_Formals (Decl);
3939 else
3940 Decl :=
3941 Make_Object_Declaration (Loc,
3942 Defining_Identifier => Temp,
3943 Object_Definition =>
3944 New_Occurrence_Of (Ret_Type, Loc));
3946 Set_Etype (Temp, Ret_Type);
3947 end if;
3949 Set_No_Initialization (Decl);
3950 Append (Decl, Decls);
3951 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3952 Targ := Temp;
3953 end if;
3954 end if;
3956 Insert_Actions (N, Decls);
3958 -- Traverse the tree and replace formals with actuals or their thunks.
3959 -- Attach block to tree before analysis and rewriting.
3961 Replace_Formals (Blk);
3962 Set_Parent (Blk, N);
3964 if not Comes_From_Source (Subp)
3965 or else Is_Predef
3966 then
3967 Reset_Slocs (Blk);
3968 end if;
3970 if Present (Exit_Lab) then
3972 -- If the body was a single expression, the single return statement
3973 -- and the corresponding label are useless.
3975 if Num_Ret = 1
3976 and then
3977 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3978 N_Goto_Statement
3979 then
3980 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3981 else
3982 Append (Lab_Decl, (Declarations (Blk)));
3983 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3984 end if;
3985 end if;
3987 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
3988 -- conflicting private views that Gigi would ignore. If this is a
3989 -- predefined unit, analyze with checks off, as is done in the non-
3990 -- inlined run-time units.
3992 declare
3993 I_Flag : constant Boolean := In_Inlined_Body;
3995 begin
3996 In_Inlined_Body := True;
3998 if Is_Predef then
3999 declare
4000 Style : constant Boolean := Style_Check;
4001 begin
4002 Style_Check := False;
4003 Analyze (Blk, Suppress => All_Checks);
4004 Style_Check := Style;
4005 end;
4007 else
4008 Analyze (Blk);
4009 end if;
4011 In_Inlined_Body := I_Flag;
4012 end;
4014 if Ekind (Subp) = E_Procedure then
4015 Rewrite_Procedure_Call (N, Blk);
4016 else
4017 Rewrite_Function_Call (N, Blk);
4019 -- For the unconstrained case, the replacement of the call has been
4020 -- made prior to the complete analysis of the generated declarations.
4021 -- Propagate the proper type now.
4023 if Is_Unc then
4024 if Nkind (N) = N_Identifier then
4025 Set_Etype (N, Etype (Entity (N)));
4026 else
4027 Set_Etype (N, Etype (Targ1));
4028 end if;
4029 end if;
4030 end if;
4032 Restore_Env;
4034 -- Cleanup mapping between formals and actuals for other expansions
4036 F := First_Formal (Subp);
4037 while Present (F) loop
4038 Set_Renamed_Object (F, Empty);
4039 Next_Formal (F);
4040 end loop;
4041 end Expand_Inlined_Call;
4043 ----------------------------
4044 -- Expand_N_Function_Call --
4045 ----------------------------
4047 procedure Expand_N_Function_Call (N : Node_Id) is
4048 begin
4049 Expand_Call (N);
4051 -- If the return value of a foreign compiled function is VAX Float, then
4052 -- expand the return (adjusts the location of the return value on
4053 -- Alpha/VMS, no-op everywhere else).
4054 -- Comes_From_Source intercepts recursive expansion.
4056 if Vax_Float (Etype (N))
4057 and then Nkind (N) = N_Function_Call
4058 and then Present (Name (N))
4059 and then Present (Entity (Name (N)))
4060 and then Has_Foreign_Convention (Entity (Name (N)))
4061 and then Comes_From_Source (Parent (N))
4062 then
4063 Expand_Vax_Foreign_Return (N);
4064 end if;
4065 end Expand_N_Function_Call;
4067 ---------------------------------------
4068 -- Expand_N_Procedure_Call_Statement --
4069 ---------------------------------------
4071 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4072 begin
4073 Expand_Call (N);
4074 end Expand_N_Procedure_Call_Statement;
4076 ------------------------------
4077 -- Expand_N_Subprogram_Body --
4078 ------------------------------
4080 -- Add poll call if ATC polling is enabled, unless the body will be inlined
4081 -- by the back-end.
4083 -- Add dummy push/pop label nodes at start and end to clear any local
4084 -- exception indications if local-exception-to-goto optimization is active.
4086 -- Add return statement if last statement in body is not a return statement
4087 -- (this makes things easier on Gigi which does not want to have to handle
4088 -- a missing return).
4090 -- Add call to Activate_Tasks if body is a task activator
4092 -- Deal with possible detection of infinite recursion
4094 -- Eliminate body completely if convention stubbed
4096 -- Encode entity names within body, since we will not need to reference
4097 -- these entities any longer in the front end.
4099 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4101 -- Reset Pure indication if any parameter has root type System.Address
4103 -- Wrap thread body
4105 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4106 Loc : constant Source_Ptr := Sloc (N);
4107 H : constant Node_Id := Handled_Statement_Sequence (N);
4108 Body_Id : Entity_Id;
4109 Except_H : Node_Id;
4110 L : List_Id;
4111 Spec_Id : Entity_Id;
4113 procedure Add_Return (S : List_Id);
4114 -- Append a return statement to the statement sequence S if the last
4115 -- statement is not already a return or a goto statement. Note that
4116 -- the latter test is not critical, it does not matter if we add a few
4117 -- extra returns, since they get eliminated anyway later on.
4119 ----------------
4120 -- Add_Return --
4121 ----------------
4123 procedure Add_Return (S : List_Id) is
4124 Last_Stm : Node_Id;
4125 Loc : Source_Ptr;
4127 begin
4128 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4129 -- not relevant in this context since they are not executable.
4131 Last_Stm := Last (S);
4132 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4133 Prev (Last_Stm);
4134 end loop;
4136 -- Now insert return unless last statement is a transfer
4138 if not Is_Transfer (Last_Stm) then
4140 -- The source location for the return is the end label of the
4141 -- procedure if present. Otherwise use the sloc of the last
4142 -- statement in the list. If the list comes from a generated
4143 -- exception handler and we are not debugging generated code,
4144 -- all the statements within the handler are made invisible
4145 -- to the debugger.
4147 if Nkind (Parent (S)) = N_Exception_Handler
4148 and then not Comes_From_Source (Parent (S))
4149 then
4150 Loc := Sloc (Last_Stm);
4152 elsif Present (End_Label (H)) then
4153 Loc := Sloc (End_Label (H));
4155 else
4156 Loc := Sloc (Last_Stm);
4157 end if;
4159 declare
4160 Rtn : constant Node_Id := Make_Simple_Return_Statement (Loc);
4162 begin
4163 -- Append return statement, and set analyzed manually. We can't
4164 -- call Analyze on this return since the scope is wrong.
4166 -- Note: it almost works to push the scope and then do the
4167 -- Analyze call, but something goes wrong in some weird cases
4168 -- and it is not worth worrying about ???
4170 Append_To (S, Rtn);
4171 Set_Analyzed (Rtn);
4173 -- Call _Postconditions procedure if appropriate. We need to
4174 -- do this explicitly because we did not analyze the generated
4175 -- return statement above, so the call did not get inserted.
4177 if Ekind (Spec_Id) = E_Procedure
4178 and then Has_Postconditions (Spec_Id)
4179 then
4180 pragma Assert (Present (Postcondition_Proc (Spec_Id)));
4181 Insert_Action (Rtn,
4182 Make_Procedure_Call_Statement (Loc,
4183 Name =>
4184 New_Reference_To (Postcondition_Proc (Spec_Id), Loc)));
4185 end if;
4186 end;
4187 end if;
4188 end Add_Return;
4190 -- Start of processing for Expand_N_Subprogram_Body
4192 begin
4193 -- Set L to either the list of declarations if present, or to the list
4194 -- of statements if no declarations are present. This is used to insert
4195 -- new stuff at the start.
4197 if Is_Non_Empty_List (Declarations (N)) then
4198 L := Declarations (N);
4199 else
4200 L := Statements (H);
4201 end if;
4203 -- If local-exception-to-goto optimization active, insert dummy push
4204 -- statements at start, and dummy pop statements at end.
4206 if (Debug_Flag_Dot_G
4207 or else Restriction_Active (No_Exception_Propagation))
4208 and then Is_Non_Empty_List (L)
4209 then
4210 declare
4211 FS : constant Node_Id := First (L);
4212 FL : constant Source_Ptr := Sloc (FS);
4213 LS : Node_Id;
4214 LL : Source_Ptr;
4216 begin
4217 -- LS points to either last statement, if statements are present
4218 -- or to the last declaration if there are no statements present.
4219 -- It is the node after which the pop's are generated.
4221 if Is_Non_Empty_List (Statements (H)) then
4222 LS := Last (Statements (H));
4223 else
4224 LS := Last (L);
4225 end if;
4227 LL := Sloc (LS);
4229 Insert_List_Before_And_Analyze (FS, New_List (
4230 Make_Push_Constraint_Error_Label (FL),
4231 Make_Push_Program_Error_Label (FL),
4232 Make_Push_Storage_Error_Label (FL)));
4234 Insert_List_After_And_Analyze (LS, New_List (
4235 Make_Pop_Constraint_Error_Label (LL),
4236 Make_Pop_Program_Error_Label (LL),
4237 Make_Pop_Storage_Error_Label (LL)));
4238 end;
4239 end if;
4241 -- Find entity for subprogram
4243 Body_Id := Defining_Entity (N);
4245 if Present (Corresponding_Spec (N)) then
4246 Spec_Id := Corresponding_Spec (N);
4247 else
4248 Spec_Id := Body_Id;
4249 end if;
4251 -- Need poll on entry to subprogram if polling enabled. We only do this
4252 -- for non-empty subprograms, since it does not seem necessary to poll
4253 -- for a dummy null subprogram.
4255 if Is_Non_Empty_List (L) then
4257 -- Do not add a polling call if the subprogram is to be inlined by
4258 -- the back-end, to avoid repeated calls with multiple inlinings.
4260 if Is_Inlined (Spec_Id)
4261 and then Front_End_Inlining
4262 and then Optimization_Level > 1
4263 then
4264 null;
4265 else
4266 Generate_Poll_Call (First (L));
4267 end if;
4268 end if;
4270 -- If this is a Pure function which has any parameters whose root type
4271 -- is System.Address, reset the Pure indication, since it will likely
4272 -- cause incorrect code to be generated as the parameter is probably
4273 -- a pointer, and the fact that the same pointer is passed does not mean
4274 -- that the same value is being referenced.
4276 -- Note that if the programmer gave an explicit Pure_Function pragma,
4277 -- then we believe the programmer, and leave the subprogram Pure.
4279 -- This code should probably be at the freeze point, so that it happens
4280 -- even on a -gnatc (or more importantly -gnatt) compile, so that the
4281 -- semantic tree has Is_Pure set properly ???
4283 if Is_Pure (Spec_Id)
4284 and then Is_Subprogram (Spec_Id)
4285 and then not Has_Pragma_Pure_Function (Spec_Id)
4286 then
4287 declare
4288 F : Entity_Id;
4290 begin
4291 F := First_Formal (Spec_Id);
4292 while Present (F) loop
4293 if Is_Descendent_Of_Address (Etype (F)) then
4294 Set_Is_Pure (Spec_Id, False);
4296 if Spec_Id /= Body_Id then
4297 Set_Is_Pure (Body_Id, False);
4298 end if;
4300 exit;
4301 end if;
4303 Next_Formal (F);
4304 end loop;
4305 end;
4306 end if;
4308 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
4310 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
4311 declare
4312 F : Entity_Id;
4314 begin
4315 -- Loop through formals
4317 F := First_Formal (Spec_Id);
4318 while Present (F) loop
4319 if Is_Scalar_Type (Etype (F))
4320 and then Ekind (F) = E_Out_Parameter
4321 then
4322 Check_Restriction (No_Default_Initialization, F);
4324 -- Insert the initialization. We turn off validity checks
4325 -- for this assignment, since we do not want any check on
4326 -- the initial value itself (which may well be invalid).
4328 Insert_Before_And_Analyze (First (L),
4329 Make_Assignment_Statement (Loc,
4330 Name => New_Occurrence_Of (F, Loc),
4331 Expression => Get_Simple_Init_Val (Etype (F), N)),
4332 Suppress => Validity_Check);
4333 end if;
4335 Next_Formal (F);
4336 end loop;
4337 end;
4338 end if;
4340 -- Clear out statement list for stubbed procedure
4342 if Present (Corresponding_Spec (N)) then
4343 Set_Elaboration_Flag (N, Spec_Id);
4345 if Convention (Spec_Id) = Convention_Stubbed
4346 or else Is_Eliminated (Spec_Id)
4347 then
4348 Set_Declarations (N, Empty_List);
4349 Set_Handled_Statement_Sequence (N,
4350 Make_Handled_Sequence_Of_Statements (Loc,
4351 Statements => New_List (
4352 Make_Null_Statement (Loc))));
4353 return;
4354 end if;
4355 end if;
4357 -- Create a set of discriminals for the next protected subprogram body
4359 if Is_List_Member (N)
4360 and then Present (Parent (List_Containing (N)))
4361 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4362 and then Present (Next_Protected_Operation (N))
4363 then
4364 Set_Discriminals (Parent (Base_Type (Scope (Spec_Id))));
4365 end if;
4367 -- Returns_By_Ref flag is normally set when the subprogram is frozen but
4368 -- subprograms with no specs are not frozen.
4370 declare
4371 Typ : constant Entity_Id := Etype (Spec_Id);
4372 Utyp : constant Entity_Id := Underlying_Type (Typ);
4374 begin
4375 if not Acts_As_Spec (N)
4376 and then Nkind (Parent (Parent (Spec_Id))) /=
4377 N_Subprogram_Body_Stub
4378 then
4379 null;
4381 elsif Is_Inherently_Limited_Type (Typ) then
4382 Set_Returns_By_Ref (Spec_Id);
4384 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4385 Set_Returns_By_Ref (Spec_Id);
4386 end if;
4387 end;
4389 -- For a procedure, we add a return for all possible syntactic ends of
4390 -- the subprogram.
4392 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure) then
4393 Add_Return (Statements (H));
4395 if Present (Exception_Handlers (H)) then
4396 Except_H := First_Non_Pragma (Exception_Handlers (H));
4397 while Present (Except_H) loop
4398 Add_Return (Statements (Except_H));
4399 Next_Non_Pragma (Except_H);
4400 end loop;
4401 end if;
4403 -- For a function, we must deal with the case where there is at least
4404 -- one missing return. What we do is to wrap the entire body of the
4405 -- function in a block:
4407 -- begin
4408 -- ...
4409 -- end;
4411 -- becomes
4413 -- begin
4414 -- begin
4415 -- ...
4416 -- end;
4418 -- raise Program_Error;
4419 -- end;
4421 -- This approach is necessary because the raise must be signalled to the
4422 -- caller, not handled by any local handler (RM 6.4(11)).
4424 -- Note: we do not need to analyze the constructed sequence here, since
4425 -- it has no handler, and an attempt to analyze the handled statement
4426 -- sequence twice is risky in various ways (e.g. the issue of expanding
4427 -- cleanup actions twice).
4429 elsif Has_Missing_Return (Spec_Id) then
4430 declare
4431 Hloc : constant Source_Ptr := Sloc (H);
4432 Blok : constant Node_Id :=
4433 Make_Block_Statement (Hloc,
4434 Handled_Statement_Sequence => H);
4435 Rais : constant Node_Id :=
4436 Make_Raise_Program_Error (Hloc,
4437 Reason => PE_Missing_Return);
4439 begin
4440 Set_Handled_Statement_Sequence (N,
4441 Make_Handled_Sequence_Of_Statements (Hloc,
4442 Statements => New_List (Blok, Rais)));
4444 Push_Scope (Spec_Id);
4445 Analyze (Blok);
4446 Analyze (Rais);
4447 Pop_Scope;
4448 end;
4449 end if;
4451 -- If subprogram contains a parameterless recursive call, then we may
4452 -- have an infinite recursion, so see if we can generate code to check
4453 -- for this possibility if storage checks are not suppressed.
4455 if Ekind (Spec_Id) = E_Procedure
4456 and then Has_Recursive_Call (Spec_Id)
4457 and then not Storage_Checks_Suppressed (Spec_Id)
4458 then
4459 Detect_Infinite_Recursion (N, Spec_Id);
4460 end if;
4462 -- Set to encode entity names in package body before gigi is called
4464 Qualify_Entity_Names (N);
4465 end Expand_N_Subprogram_Body;
4467 -----------------------------------
4468 -- Expand_N_Subprogram_Body_Stub --
4469 -----------------------------------
4471 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
4472 begin
4473 if Present (Corresponding_Body (N)) then
4474 Expand_N_Subprogram_Body (
4475 Unit_Declaration_Node (Corresponding_Body (N)));
4476 end if;
4477 end Expand_N_Subprogram_Body_Stub;
4479 -------------------------------------
4480 -- Expand_N_Subprogram_Declaration --
4481 -------------------------------------
4483 -- If the declaration appears within a protected body, it is a private
4484 -- operation of the protected type. We must create the corresponding
4485 -- protected subprogram an associated formals. For a normal protected
4486 -- operation, this is done when expanding the protected type declaration.
4488 -- If the declaration is for a null procedure, emit null body
4490 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
4491 Loc : constant Source_Ptr := Sloc (N);
4492 Subp : constant Entity_Id := Defining_Entity (N);
4493 Scop : constant Entity_Id := Scope (Subp);
4494 Prot_Decl : Node_Id;
4495 Prot_Bod : Node_Id;
4496 Prot_Id : Entity_Id;
4498 begin
4499 -- Deal with case of protected subprogram. Do not generate protected
4500 -- operation if operation is flagged as eliminated.
4502 if Is_List_Member (N)
4503 and then Present (Parent (List_Containing (N)))
4504 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4505 and then Is_Protected_Type (Scop)
4506 then
4507 if No (Protected_Body_Subprogram (Subp))
4508 and then not Is_Eliminated (Subp)
4509 then
4510 Prot_Decl :=
4511 Make_Subprogram_Declaration (Loc,
4512 Specification =>
4513 Build_Protected_Sub_Specification
4514 (N, Scop, Unprotected_Mode));
4516 -- The protected subprogram is declared outside of the protected
4517 -- body. Given that the body has frozen all entities so far, we
4518 -- analyze the subprogram and perform freezing actions explicitly.
4519 -- including the generation of an explicit freeze node, to ensure
4520 -- that gigi has the proper order of elaboration.
4521 -- If the body is a subunit, the insertion point is before the
4522 -- stub in the parent.
4524 Prot_Bod := Parent (List_Containing (N));
4526 if Nkind (Parent (Prot_Bod)) = N_Subunit then
4527 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
4528 end if;
4530 Insert_Before (Prot_Bod, Prot_Decl);
4531 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
4532 Set_Has_Delayed_Freeze (Prot_Id);
4534 Push_Scope (Scope (Scop));
4535 Analyze (Prot_Decl);
4536 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
4537 Set_Protected_Body_Subprogram (Subp, Prot_Id);
4539 -- Create protected operation as well. Even though the operation
4540 -- is only accessible within the body, it is possible to make it
4541 -- available outside of the protected object by using 'Access to
4542 -- provide a callback, so build protected version in all cases.
4544 Prot_Decl :=
4545 Make_Subprogram_Declaration (Loc,
4546 Specification =>
4547 Build_Protected_Sub_Specification (N, Scop, Protected_Mode));
4548 Insert_Before (Prot_Bod, Prot_Decl);
4549 Analyze (Prot_Decl);
4551 Pop_Scope;
4552 end if;
4554 -- Ada 2005 (AI-348): Generate body for a null procedure.
4555 -- In most cases this is superfluous because calls to it
4556 -- will be automatically inlined, but we definitely need
4557 -- the body if preconditions for the procedure are present.
4559 elsif Nkind (Specification (N)) = N_Procedure_Specification
4560 and then Null_Present (Specification (N))
4561 then
4562 declare
4563 Bod : constant Node_Id := Body_To_Inline (N);
4565 begin
4566 Set_Has_Completion (Subp, False);
4567 Append_Freeze_Action (Subp, Bod);
4569 -- The body now contains raise statements, so calls to it will
4570 -- not be inlined.
4572 Set_Is_Inlined (Subp, False);
4573 end;
4574 end if;
4575 end Expand_N_Subprogram_Declaration;
4577 ---------------------------------------
4578 -- Expand_Protected_Object_Reference --
4579 ---------------------------------------
4581 function Expand_Protected_Object_Reference
4582 (N : Node_Id;
4583 Scop : Entity_Id) return Node_Id
4585 Loc : constant Source_Ptr := Sloc (N);
4586 Corr : Entity_Id;
4587 Rec : Node_Id;
4588 Param : Entity_Id;
4589 Proc : Entity_Id;
4591 begin
4592 Rec :=
4593 Make_Identifier (Loc,
4594 Chars => Name_uObject);
4595 Set_Etype (Rec, Corresponding_Record_Type (Scop));
4597 -- Find enclosing protected operation, and retrieve its first parameter,
4598 -- which denotes the enclosing protected object. If the enclosing
4599 -- operation is an entry, we are immediately within the protected body,
4600 -- and we can retrieve the object from the service entries procedure. A
4601 -- barrier function has the same signature as an entry. A barrier
4602 -- function is compiled within the protected object, but unlike
4603 -- protected operations its never needs locks, so that its protected
4604 -- body subprogram points to itself.
4606 Proc := Current_Scope;
4607 while Present (Proc)
4608 and then Scope (Proc) /= Scop
4609 loop
4610 Proc := Scope (Proc);
4611 end loop;
4613 Corr := Protected_Body_Subprogram (Proc);
4615 if No (Corr) then
4617 -- Previous error left expansion incomplete.
4618 -- Nothing to do on this call.
4620 return Empty;
4621 end if;
4623 Param :=
4624 Defining_Identifier
4625 (First (Parameter_Specifications (Parent (Corr))));
4627 if Is_Subprogram (Proc)
4628 and then Proc /= Corr
4629 then
4630 -- Protected function or procedure
4632 Set_Entity (Rec, Param);
4634 -- Rec is a reference to an entity which will not be in scope when
4635 -- the call is reanalyzed, and needs no further analysis.
4637 Set_Analyzed (Rec);
4639 else
4640 -- Entry or barrier function for entry body. The first parameter of
4641 -- the entry body procedure is pointer to the object. We create a
4642 -- local variable of the proper type, duplicating what is done to
4643 -- define _object later on.
4645 declare
4646 Decls : List_Id;
4647 Obj_Ptr : constant Entity_Id := Make_Temporary (Loc, 'T');
4649 begin
4650 Decls := New_List (
4651 Make_Full_Type_Declaration (Loc,
4652 Defining_Identifier => Obj_Ptr,
4653 Type_Definition =>
4654 Make_Access_To_Object_Definition (Loc,
4655 Subtype_Indication =>
4656 New_Reference_To
4657 (Corresponding_Record_Type (Scop), Loc))));
4659 Insert_Actions (N, Decls);
4660 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4662 Rec :=
4663 Make_Explicit_Dereference (Loc,
4664 Unchecked_Convert_To (Obj_Ptr,
4665 New_Occurrence_Of (Param, Loc)));
4667 -- Analyze new actual. Other actuals in calls are already analyzed
4668 -- and the list of actuals is not reanalyzed after rewriting.
4670 Set_Parent (Rec, N);
4671 Analyze (Rec);
4672 end;
4673 end if;
4675 return Rec;
4676 end Expand_Protected_Object_Reference;
4678 --------------------------------------
4679 -- Expand_Protected_Subprogram_Call --
4680 --------------------------------------
4682 procedure Expand_Protected_Subprogram_Call
4683 (N : Node_Id;
4684 Subp : Entity_Id;
4685 Scop : Entity_Id)
4687 Rec : Node_Id;
4689 begin
4690 -- If the protected object is not an enclosing scope, this is
4691 -- an inter-object function call. Inter-object procedure
4692 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4693 -- The call is intra-object only if the subprogram being
4694 -- called is in the protected body being compiled, and if the
4695 -- protected object in the call is statically the enclosing type.
4696 -- The object may be an component of some other data structure,
4697 -- in which case this must be handled as an inter-object call.
4699 if not In_Open_Scopes (Scop)
4700 or else not Is_Entity_Name (Name (N))
4701 then
4702 if Nkind (Name (N)) = N_Selected_Component then
4703 Rec := Prefix (Name (N));
4705 else
4706 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4707 Rec := Prefix (Prefix (Name (N)));
4708 end if;
4710 Build_Protected_Subprogram_Call (N,
4711 Name => New_Occurrence_Of (Subp, Sloc (N)),
4712 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4713 External => True);
4715 else
4716 Rec := Expand_Protected_Object_Reference (N, Scop);
4718 if No (Rec) then
4719 return;
4720 end if;
4722 Build_Protected_Subprogram_Call (N,
4723 Name => Name (N),
4724 Rec => Rec,
4725 External => False);
4727 end if;
4729 -- If it is a function call it can appear in elaboration code and
4730 -- the called entity must be frozen here.
4732 if Ekind (Subp) = E_Function then
4733 Freeze_Expression (Name (N));
4734 end if;
4736 -- Analyze and resolve the new call. The actuals have already been
4737 -- resolved, but expansion of a function call will add extra actuals
4738 -- if needed. Analysis of a procedure call already includes resolution.
4740 Analyze (N);
4742 if Ekind (Subp) = E_Function then
4743 Resolve (N, Etype (Subp));
4744 end if;
4745 end Expand_Protected_Subprogram_Call;
4747 --------------------------------
4748 -- Is_Build_In_Place_Function --
4749 --------------------------------
4751 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
4752 begin
4753 -- This function is called from Expand_Subtype_From_Expr during
4754 -- semantic analysis, even when expansion is off. In those cases
4755 -- the build_in_place expansion will not take place.
4757 if not Expander_Active then
4758 return False;
4759 end if;
4761 -- For now we test whether E denotes a function or access-to-function
4762 -- type whose result subtype is inherently limited. Later this test may
4763 -- be revised to allow composite nonlimited types. Functions with a
4764 -- foreign convention or whose result type has a foreign convention
4765 -- never qualify.
4767 if Ekind_In (E, E_Function, E_Generic_Function)
4768 or else (Ekind (E) = E_Subprogram_Type
4769 and then Etype (E) /= Standard_Void_Type)
4770 then
4771 -- Note: If you have Convention (C) on an inherently limited type,
4772 -- you're on your own. That is, the C code will have to be carefully
4773 -- written to know about the Ada conventions.
4775 if Has_Foreign_Convention (E)
4776 or else Has_Foreign_Convention (Etype (E))
4777 then
4778 return False;
4780 -- In Ada 2005 all functions with an inherently limited return type
4781 -- must be handled using a build-in-place profile, including the case
4782 -- of a function with a limited interface result, where the function
4783 -- may return objects of nonlimited descendants.
4785 else
4786 return Is_Inherently_Limited_Type (Etype (E))
4787 and then Ada_Version >= Ada_05
4788 and then not Debug_Flag_Dot_L;
4789 end if;
4791 else
4792 return False;
4793 end if;
4794 end Is_Build_In_Place_Function;
4796 -------------------------------------
4797 -- Is_Build_In_Place_Function_Call --
4798 -------------------------------------
4800 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
4801 Exp_Node : Node_Id := N;
4802 Function_Id : Entity_Id;
4804 begin
4805 -- Step past qualification or unchecked conversion (the latter can occur
4806 -- in cases of calls to 'Input).
4808 if Nkind_In
4809 (Exp_Node, N_Qualified_Expression, N_Unchecked_Type_Conversion)
4810 then
4811 Exp_Node := Expression (N);
4812 end if;
4814 if Nkind (Exp_Node) /= N_Function_Call then
4815 return False;
4817 else
4818 if Is_Entity_Name (Name (Exp_Node)) then
4819 Function_Id := Entity (Name (Exp_Node));
4821 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
4822 Function_Id := Etype (Name (Exp_Node));
4823 end if;
4825 return Is_Build_In_Place_Function (Function_Id);
4826 end if;
4827 end Is_Build_In_Place_Function_Call;
4829 -----------------------
4830 -- Freeze_Subprogram --
4831 -----------------------
4833 procedure Freeze_Subprogram (N : Node_Id) is
4834 Loc : constant Source_Ptr := Sloc (N);
4836 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4837 -- (Ada 2005): Register a predefined primitive in all the secondary
4838 -- dispatch tables of its primitive type.
4840 ----------------------------------
4841 -- Register_Predefined_DT_Entry --
4842 ----------------------------------
4844 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4845 Iface_DT_Ptr : Elmt_Id;
4846 Tagged_Typ : Entity_Id;
4847 Thunk_Id : Entity_Id;
4848 Thunk_Code : Node_Id;
4850 begin
4851 Tagged_Typ := Find_Dispatching_Type (Prim);
4853 if No (Access_Disp_Table (Tagged_Typ))
4854 or else not Has_Interfaces (Tagged_Typ)
4855 or else not RTE_Available (RE_Interface_Tag)
4856 or else Restriction_Active (No_Dispatching_Calls)
4857 then
4858 return;
4859 end if;
4861 -- Skip the first two access-to-dispatch-table pointers since they
4862 -- leads to the primary dispatch table (predefined DT and user
4863 -- defined DT). We are only concerned with the secondary dispatch
4864 -- table pointers. Note that the access-to- dispatch-table pointer
4865 -- corresponds to the first implemented interface retrieved below.
4867 Iface_DT_Ptr :=
4868 Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ))));
4870 while Present (Iface_DT_Ptr)
4871 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
4872 loop
4873 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
4874 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
4876 if Present (Thunk_Code) then
4877 Insert_Actions_After (N, New_List (
4878 Thunk_Code,
4880 Build_Set_Predefined_Prim_Op_Address (Loc,
4881 Tag_Node =>
4882 New_Reference_To (Node (Next_Elmt (Iface_DT_Ptr)), Loc),
4883 Position => DT_Position (Prim),
4884 Address_Node =>
4885 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
4886 Make_Attribute_Reference (Loc,
4887 Prefix => New_Reference_To (Thunk_Id, Loc),
4888 Attribute_Name => Name_Unrestricted_Access))),
4890 Build_Set_Predefined_Prim_Op_Address (Loc,
4891 Tag_Node =>
4892 New_Reference_To
4893 (Node (Next_Elmt (Next_Elmt (Next_Elmt (Iface_DT_Ptr)))),
4894 Loc),
4895 Position => DT_Position (Prim),
4896 Address_Node =>
4897 Unchecked_Convert_To (RTE (RE_Prim_Ptr),
4898 Make_Attribute_Reference (Loc,
4899 Prefix => New_Reference_To (Prim, Loc),
4900 Attribute_Name => Name_Unrestricted_Access)))));
4901 end if;
4903 -- Skip the tag of the predefined primitives dispatch table
4905 Next_Elmt (Iface_DT_Ptr);
4906 pragma Assert (Has_Thunks (Node (Iface_DT_Ptr)));
4908 -- Skip the tag of the no-thunks dispatch table
4910 Next_Elmt (Iface_DT_Ptr);
4911 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4913 -- Skip the tag of the predefined primitives no-thunks dispatch
4914 -- table
4916 Next_Elmt (Iface_DT_Ptr);
4917 pragma Assert (not Has_Thunks (Node (Iface_DT_Ptr)));
4919 Next_Elmt (Iface_DT_Ptr);
4920 end loop;
4921 end Register_Predefined_DT_Entry;
4923 -- Local variables
4925 Subp : constant Entity_Id := Entity (N);
4927 -- Start of processing for Freeze_Subprogram
4929 begin
4930 -- We suppress the initialization of the dispatch table entry when
4931 -- VM_Target because the dispatching mechanism is handled internally
4932 -- by the VM.
4934 if Is_Dispatching_Operation (Subp)
4935 and then not Is_Abstract_Subprogram (Subp)
4936 and then Present (DTC_Entity (Subp))
4937 and then Present (Scope (DTC_Entity (Subp)))
4938 and then Tagged_Type_Expansion
4939 and then not Restriction_Active (No_Dispatching_Calls)
4940 and then RTE_Available (RE_Tag)
4941 then
4942 declare
4943 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
4945 begin
4946 -- Handle private overridden primitives
4948 if not Is_CPP_Class (Typ) then
4949 Check_Overriding_Operation (Subp);
4950 end if;
4952 -- We assume that imported CPP primitives correspond with objects
4953 -- whose constructor is in the CPP side; therefore we don't need
4954 -- to generate code to register them in the dispatch table.
4956 if Is_CPP_Class (Typ) then
4957 null;
4959 -- Handle CPP primitives found in derivations of CPP_Class types.
4960 -- These primitives must have been inherited from some parent, and
4961 -- there is no need to register them in the dispatch table because
4962 -- Build_Inherit_Prims takes care of the initialization of these
4963 -- slots.
4965 elsif Is_Imported (Subp)
4966 and then (Convention (Subp) = Convention_CPP
4967 or else Convention (Subp) = Convention_C)
4968 then
4969 null;
4971 -- Generate code to register the primitive in non statically
4972 -- allocated dispatch tables
4974 elsif not Static_Dispatch_Tables
4975 or else not
4976 Is_Library_Level_Tagged_Type (Scope (DTC_Entity (Subp)))
4977 then
4978 -- When a primitive is frozen, enter its name in its dispatch
4979 -- table slot.
4981 if not Is_Interface (Typ)
4982 or else Present (Interface_Alias (Subp))
4983 then
4984 if Is_Predefined_Dispatching_Operation (Subp) then
4985 Register_Predefined_DT_Entry (Subp);
4986 end if;
4988 Insert_Actions_After (N,
4989 Register_Primitive (Loc, Prim => Subp));
4990 end if;
4991 end if;
4992 end;
4993 end if;
4995 -- Mark functions that return by reference. Note that it cannot be part
4996 -- of the normal semantic analysis of the spec since the underlying
4997 -- returned type may not be known yet (for private types).
4999 declare
5000 Typ : constant Entity_Id := Etype (Subp);
5001 Utyp : constant Entity_Id := Underlying_Type (Typ);
5002 begin
5003 if Is_Inherently_Limited_Type (Typ) then
5004 Set_Returns_By_Ref (Subp);
5005 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5006 Set_Returns_By_Ref (Subp);
5007 end if;
5008 end;
5009 end Freeze_Subprogram;
5011 -----------------------
5012 -- Is_Null_Procedure --
5013 -----------------------
5015 function Is_Null_Procedure (Subp : Entity_Id) return Boolean is
5016 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
5018 begin
5019 if Ekind (Subp) /= E_Procedure then
5020 return False;
5022 -- Check if this is a declared null procedure
5024 elsif Nkind (Decl) = N_Subprogram_Declaration then
5025 if not Null_Present (Specification (Decl)) then
5026 return False;
5028 elsif No (Body_To_Inline (Decl)) then
5029 return False;
5031 -- Check if the body contains only a null statement, followed by
5032 -- the return statement added during expansion.
5034 else
5035 declare
5036 Orig_Bod : constant Node_Id := Body_To_Inline (Decl);
5038 Stat : Node_Id;
5039 Stat2 : Node_Id;
5041 begin
5042 if Nkind (Orig_Bod) /= N_Subprogram_Body then
5043 return False;
5044 else
5045 -- We must skip SCIL nodes because they are currently
5046 -- implemented as special N_Null_Statement nodes.
5048 Stat :=
5049 First_Non_SCIL_Node
5050 (Statements (Handled_Statement_Sequence (Orig_Bod)));
5051 Stat2 := Next_Non_SCIL_Node (Stat);
5053 return
5054 Is_Empty_List (Declarations (Orig_Bod))
5055 and then Nkind (Stat) = N_Null_Statement
5056 and then
5057 (No (Stat2)
5058 or else
5059 (Nkind (Stat2) = N_Simple_Return_Statement
5060 and then No (Next (Stat2))));
5061 end if;
5062 end;
5063 end if;
5065 else
5066 return False;
5067 end if;
5068 end Is_Null_Procedure;
5070 -------------------------------------------
5071 -- Make_Build_In_Place_Call_In_Allocator --
5072 -------------------------------------------
5074 procedure Make_Build_In_Place_Call_In_Allocator
5075 (Allocator : Node_Id;
5076 Function_Call : Node_Id)
5078 Loc : Source_Ptr;
5079 Func_Call : Node_Id := Function_Call;
5080 Function_Id : Entity_Id;
5081 Result_Subt : Entity_Id;
5082 Acc_Type : constant Entity_Id := Etype (Allocator);
5083 New_Allocator : Node_Id;
5084 Return_Obj_Access : Entity_Id;
5086 begin
5087 -- Step past qualification or unchecked conversion (the latter can occur
5088 -- in cases of calls to 'Input).
5090 if Nkind_In (Func_Call,
5091 N_Qualified_Expression,
5092 N_Unchecked_Type_Conversion)
5093 then
5094 Func_Call := Expression (Func_Call);
5095 end if;
5097 -- If the call has already been processed to add build-in-place actuals
5098 -- then return. This should not normally occur in an allocator context,
5099 -- but we add the protection as a defensive measure.
5101 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5102 return;
5103 end if;
5105 -- Mark the call as processed as a build-in-place call
5107 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5109 Loc := Sloc (Function_Call);
5111 if Is_Entity_Name (Name (Func_Call)) then
5112 Function_Id := Entity (Name (Func_Call));
5114 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5115 Function_Id := Etype (Name (Func_Call));
5117 else
5118 raise Program_Error;
5119 end if;
5121 Result_Subt := Etype (Function_Id);
5123 -- When the result subtype is constrained, the return object must be
5124 -- allocated on the caller side, and access to it is passed to the
5125 -- function.
5127 -- Here and in related routines, we must examine the full view of the
5128 -- type, because the view at the point of call may differ from that
5129 -- that in the function body, and the expansion mechanism depends on
5130 -- the characteristics of the full view.
5132 if Is_Constrained (Underlying_Type (Result_Subt)) then
5134 -- Replace the initialized allocator of form "new T'(Func (...))"
5135 -- with an uninitialized allocator of form "new T", where T is the
5136 -- result subtype of the called function. The call to the function
5137 -- is handled separately further below.
5139 New_Allocator :=
5140 Make_Allocator (Loc,
5141 Expression => New_Reference_To (Result_Subt, Loc));
5142 Set_No_Initialization (New_Allocator);
5144 -- Copy attributes to new allocator. Note that the new allocator
5145 -- logically comes from source if the original one did, so copy the
5146 -- relevant flag. This ensures proper treatment of the restriction
5147 -- No_Implicit_Heap_Allocations in this case.
5149 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
5150 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
5151 Set_Comes_From_Source (New_Allocator, Comes_From_Source (Allocator));
5153 Rewrite (Allocator, New_Allocator);
5155 -- Create a new access object and initialize it to the result of the
5156 -- new uninitialized allocator. Note: we do not use Allocator as the
5157 -- Related_Node of Return_Obj_Access in call to Make_Temporary below
5158 -- as this would create a sort of infinite "recursion".
5160 Return_Obj_Access := Make_Temporary (Loc, 'R');
5161 Set_Etype (Return_Obj_Access, Acc_Type);
5163 Insert_Action (Allocator,
5164 Make_Object_Declaration (Loc,
5165 Defining_Identifier => Return_Obj_Access,
5166 Object_Definition => New_Reference_To (Acc_Type, Loc),
5167 Expression => Relocate_Node (Allocator)));
5169 -- When the function has a controlling result, an allocation-form
5170 -- parameter must be passed indicating that the caller is allocating
5171 -- the result object. This is needed because such a function can be
5172 -- called as a dispatching operation and must be treated similarly
5173 -- to functions with unconstrained result subtypes.
5175 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5176 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5178 Add_Final_List_Actual_To_Build_In_Place_Call
5179 (Func_Call, Function_Id, Acc_Type);
5181 Add_Task_Actuals_To_Build_In_Place_Call
5182 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5184 -- Add an implicit actual to the function call that provides access
5185 -- to the allocated object. An unchecked conversion to the (specific)
5186 -- result subtype of the function is inserted to handle cases where
5187 -- the access type of the allocator has a class-wide designated type.
5189 Add_Access_Actual_To_Build_In_Place_Call
5190 (Func_Call,
5191 Function_Id,
5192 Make_Unchecked_Type_Conversion (Loc,
5193 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5194 Expression =>
5195 Make_Explicit_Dereference (Loc,
5196 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
5198 -- When the result subtype is unconstrained, the function itself must
5199 -- perform the allocation of the return object, so we pass parameters
5200 -- indicating that. We don't yet handle the case where the allocation
5201 -- must be done in a user-defined storage pool, which will require
5202 -- passing another actual or two to provide allocation/deallocation
5203 -- operations. ???
5205 else
5207 -- Pass an allocation parameter indicating that the function should
5208 -- allocate its result on the heap.
5210 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5211 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
5213 Add_Final_List_Actual_To_Build_In_Place_Call
5214 (Func_Call, Function_Id, Acc_Type);
5216 Add_Task_Actuals_To_Build_In_Place_Call
5217 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5219 -- The caller does not provide the return object in this case, so we
5220 -- have to pass null for the object access actual.
5222 Add_Access_Actual_To_Build_In_Place_Call
5223 (Func_Call, Function_Id, Return_Object => Empty);
5224 end if;
5226 -- Finally, replace the allocator node with a reference to the result
5227 -- of the function call itself (which will effectively be an access
5228 -- to the object created by the allocator).
5230 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
5231 Analyze_And_Resolve (Allocator, Acc_Type);
5232 end Make_Build_In_Place_Call_In_Allocator;
5234 ---------------------------------------------------
5235 -- Make_Build_In_Place_Call_In_Anonymous_Context --
5236 ---------------------------------------------------
5238 procedure Make_Build_In_Place_Call_In_Anonymous_Context
5239 (Function_Call : Node_Id)
5241 Loc : Source_Ptr;
5242 Func_Call : Node_Id := Function_Call;
5243 Function_Id : Entity_Id;
5244 Result_Subt : Entity_Id;
5245 Return_Obj_Id : Entity_Id;
5246 Return_Obj_Decl : Entity_Id;
5248 begin
5249 -- Step past qualification or unchecked conversion (the latter can occur
5250 -- in cases of calls to 'Input).
5252 if Nkind_In (Func_Call, N_Qualified_Expression,
5253 N_Unchecked_Type_Conversion)
5254 then
5255 Func_Call := Expression (Func_Call);
5256 end if;
5258 -- If the call has already been processed to add build-in-place actuals
5259 -- then return. One place this can occur is for calls to build-in-place
5260 -- functions that occur within a call to a protected operation, where
5261 -- due to rewriting and expansion of the protected call there can be
5262 -- more than one call to Expand_Actuals for the same set of actuals.
5264 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5265 return;
5266 end if;
5268 -- Mark the call as processed as a build-in-place call
5270 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5272 Loc := Sloc (Function_Call);
5274 if Is_Entity_Name (Name (Func_Call)) then
5275 Function_Id := Entity (Name (Func_Call));
5277 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5278 Function_Id := Etype (Name (Func_Call));
5280 else
5281 raise Program_Error;
5282 end if;
5284 Result_Subt := Etype (Function_Id);
5286 -- When the result subtype is constrained, an object of the subtype is
5287 -- declared and an access value designating it is passed as an actual.
5289 if Is_Constrained (Underlying_Type (Result_Subt)) then
5291 -- Create a temporary object to hold the function result
5293 Return_Obj_Id := Make_Temporary (Loc, 'R');
5294 Set_Etype (Return_Obj_Id, Result_Subt);
5296 Return_Obj_Decl :=
5297 Make_Object_Declaration (Loc,
5298 Defining_Identifier => Return_Obj_Id,
5299 Aliased_Present => True,
5300 Object_Definition => New_Reference_To (Result_Subt, Loc));
5302 Set_No_Initialization (Return_Obj_Decl);
5304 Insert_Action (Func_Call, Return_Obj_Decl);
5306 -- When the function has a controlling result, an allocation-form
5307 -- parameter must be passed indicating that the caller is allocating
5308 -- the result object. This is needed because such a function can be
5309 -- called as a dispatching operation and must be treated similarly
5310 -- to functions with unconstrained result subtypes.
5312 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5313 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5315 Add_Final_List_Actual_To_Build_In_Place_Call
5316 (Func_Call, Function_Id, Acc_Type => Empty);
5318 Add_Task_Actuals_To_Build_In_Place_Call
5319 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5321 -- Add an implicit actual to the function call that provides access
5322 -- to the caller's return object.
5324 Add_Access_Actual_To_Build_In_Place_Call
5325 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
5327 -- When the result subtype is unconstrained, the function must allocate
5328 -- the return object in the secondary stack, so appropriate implicit
5329 -- parameters are added to the call to indicate that. A transient
5330 -- scope is established to ensure eventual cleanup of the result.
5332 else
5334 -- Pass an allocation parameter indicating that the function should
5335 -- allocate its result on the secondary stack.
5337 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5338 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
5340 Add_Final_List_Actual_To_Build_In_Place_Call
5341 (Func_Call, Function_Id, Acc_Type => Empty);
5343 Add_Task_Actuals_To_Build_In_Place_Call
5344 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5346 -- Pass a null value to the function since no return object is
5347 -- available on the caller side.
5349 Add_Access_Actual_To_Build_In_Place_Call
5350 (Func_Call, Function_Id, Empty);
5352 Establish_Transient_Scope (Func_Call, Sec_Stack => True);
5353 end if;
5354 end Make_Build_In_Place_Call_In_Anonymous_Context;
5356 --------------------------------------------
5357 -- Make_Build_In_Place_Call_In_Assignment --
5358 --------------------------------------------
5360 procedure Make_Build_In_Place_Call_In_Assignment
5361 (Assign : Node_Id;
5362 Function_Call : Node_Id)
5364 Lhs : constant Node_Id := Name (Assign);
5365 Func_Call : Node_Id := Function_Call;
5366 Func_Id : Entity_Id;
5367 Loc : Source_Ptr;
5368 Obj_Decl : Node_Id;
5369 Obj_Id : Entity_Id;
5370 Ptr_Typ : Entity_Id;
5371 Ptr_Typ_Decl : Node_Id;
5372 Result_Subt : Entity_Id;
5373 Target : Node_Id;
5375 begin
5376 -- Step past qualification or unchecked conversion (the latter can occur
5377 -- in cases of calls to 'Input).
5379 if Nkind_In (Func_Call, N_Qualified_Expression,
5380 N_Unchecked_Type_Conversion)
5381 then
5382 Func_Call := Expression (Func_Call);
5383 end if;
5385 -- If the call has already been processed to add build-in-place actuals
5386 -- then return. This should not normally occur in an assignment context,
5387 -- but we add the protection as a defensive measure.
5389 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5390 return;
5391 end if;
5393 -- Mark the call as processed as a build-in-place call
5395 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5397 Loc := Sloc (Function_Call);
5399 if Is_Entity_Name (Name (Func_Call)) then
5400 Func_Id := Entity (Name (Func_Call));
5402 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5403 Func_Id := Etype (Name (Func_Call));
5405 else
5406 raise Program_Error;
5407 end if;
5409 Result_Subt := Etype (Func_Id);
5411 -- When the result subtype is unconstrained, an additional actual must
5412 -- be passed to indicate that the caller is providing the return object.
5413 -- This parameter must also be passed when the called function has a
5414 -- controlling result, because dispatching calls to the function needs
5415 -- to be treated effectively the same as calls to class-wide functions.
5417 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5418 (Func_Call, Func_Id, Alloc_Form => Caller_Allocation);
5420 -- If Lhs is a selected component, then pass it along so that its prefix
5421 -- object will be used as the source of the finalization list.
5423 if Nkind (Lhs) = N_Selected_Component then
5424 Add_Final_List_Actual_To_Build_In_Place_Call
5425 (Func_Call, Func_Id, Acc_Type => Empty, Sel_Comp => Lhs);
5426 else
5427 Add_Final_List_Actual_To_Build_In_Place_Call
5428 (Func_Call, Func_Id, Acc_Type => Empty);
5429 end if;
5431 Add_Task_Actuals_To_Build_In_Place_Call
5432 (Func_Call, Func_Id, Make_Identifier (Loc, Name_uMaster));
5434 -- Add an implicit actual to the function call that provides access to
5435 -- the caller's return object.
5437 Add_Access_Actual_To_Build_In_Place_Call
5438 (Func_Call,
5439 Func_Id,
5440 Make_Unchecked_Type_Conversion (Loc,
5441 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5442 Expression => Relocate_Node (Lhs)));
5444 -- Create an access type designating the function's result subtype
5446 Ptr_Typ := Make_Temporary (Loc, 'A');
5448 Ptr_Typ_Decl :=
5449 Make_Full_Type_Declaration (Loc,
5450 Defining_Identifier => Ptr_Typ,
5451 Type_Definition =>
5452 Make_Access_To_Object_Definition (Loc,
5453 All_Present => True,
5454 Subtype_Indication =>
5455 New_Reference_To (Result_Subt, Loc)));
5456 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
5458 -- Finally, create an access object initialized to a reference to the
5459 -- function call.
5461 Obj_Id := Make_Temporary (Loc, 'R');
5462 Set_Etype (Obj_Id, Ptr_Typ);
5464 Obj_Decl :=
5465 Make_Object_Declaration (Loc,
5466 Defining_Identifier => Obj_Id,
5467 Object_Definition =>
5468 New_Reference_To (Ptr_Typ, Loc),
5469 Expression =>
5470 Make_Reference (Loc,
5471 Prefix => Relocate_Node (Func_Call)));
5472 Insert_After_And_Analyze (Ptr_Typ_Decl, Obj_Decl);
5474 Rewrite (Assign, Make_Null_Statement (Loc));
5476 -- Retrieve the target of the assignment
5478 if Nkind (Lhs) = N_Selected_Component then
5479 Target := Selector_Name (Lhs);
5480 elsif Nkind (Lhs) = N_Type_Conversion then
5481 Target := Expression (Lhs);
5482 else
5483 Target := Lhs;
5484 end if;
5486 -- If we are assigning to a return object or this is an expression of
5487 -- an extension aggregate, the target should either be an identifier
5488 -- or a simple expression. All other cases imply a different scenario.
5490 if Nkind (Target) in N_Has_Entity then
5491 Target := Entity (Target);
5492 else
5493 return;
5494 end if;
5496 -- When the target of the assignment is a return object of an enclosing
5497 -- build-in-place function and also requires finalization, the list
5498 -- generated for the assignment must be moved to that of the enclosing
5499 -- function.
5501 -- function Enclosing_BIP_Function return Ctrl_Typ is
5502 -- begin
5503 -- return (Ctrl_Parent_Part => BIP_Function with ...);
5504 -- end Enclosing_BIP_Function;
5506 if Is_Return_Object (Target)
5507 and then Needs_Finalization (Etype (Target))
5508 and then Needs_Finalization (Result_Subt)
5509 then
5510 declare
5511 Obj_List : constant Node_Id := Find_Final_List (Obj_Id);
5512 Encl_List : Node_Id;
5513 Encl_Scop : Entity_Id;
5515 begin
5516 Encl_Scop := Scope (Target);
5518 -- Locate the scope of the extended return statement
5520 while Present (Encl_Scop)
5521 and then Ekind (Encl_Scop) /= E_Return_Statement
5522 loop
5523 Encl_Scop := Scope (Encl_Scop);
5524 end loop;
5526 -- A return object should always be enclosed by a return statement
5527 -- scope at some level.
5529 pragma Assert (Present (Encl_Scop));
5531 Encl_List :=
5532 Make_Attribute_Reference (Loc,
5533 Prefix =>
5534 New_Reference_To (
5535 Finalization_Chain_Entity (Encl_Scop), Loc),
5536 Attribute_Name => Name_Unrestricted_Access);
5538 -- Generate a call to move final list
5540 Insert_After_And_Analyze (Obj_Decl,
5541 Make_Procedure_Call_Statement (Loc,
5542 Name =>
5543 New_Reference_To (RTE (RE_Move_Final_List), Loc),
5544 Parameter_Associations => New_List (Obj_List, Encl_List)));
5545 end;
5546 end if;
5547 end Make_Build_In_Place_Call_In_Assignment;
5549 ----------------------------------------------------
5550 -- Make_Build_In_Place_Call_In_Object_Declaration --
5551 ----------------------------------------------------
5553 procedure Make_Build_In_Place_Call_In_Object_Declaration
5554 (Object_Decl : Node_Id;
5555 Function_Call : Node_Id)
5557 Loc : Source_Ptr;
5558 Obj_Def_Id : constant Entity_Id :=
5559 Defining_Identifier (Object_Decl);
5561 Func_Call : Node_Id := Function_Call;
5562 Function_Id : Entity_Id;
5563 Result_Subt : Entity_Id;
5564 Caller_Object : Node_Id;
5565 Call_Deref : Node_Id;
5566 Ref_Type : Entity_Id;
5567 Ptr_Typ_Decl : Node_Id;
5568 Def_Id : Entity_Id;
5569 New_Expr : Node_Id;
5570 Enclosing_Func : Entity_Id;
5571 Pass_Caller_Acc : Boolean := False;
5573 begin
5574 -- Step past qualification or unchecked conversion (the latter can occur
5575 -- in cases of calls to 'Input).
5577 if Nkind_In (Func_Call, N_Qualified_Expression,
5578 N_Unchecked_Type_Conversion)
5579 then
5580 Func_Call := Expression (Func_Call);
5581 end if;
5583 -- If the call has already been processed to add build-in-place actuals
5584 -- then return. This should not normally occur in an object declaration,
5585 -- but we add the protection as a defensive measure.
5587 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5588 return;
5589 end if;
5591 -- Mark the call as processed as a build-in-place call
5593 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5595 Loc := Sloc (Function_Call);
5597 if Is_Entity_Name (Name (Func_Call)) then
5598 Function_Id := Entity (Name (Func_Call));
5600 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5601 Function_Id := Etype (Name (Func_Call));
5603 else
5604 raise Program_Error;
5605 end if;
5607 Result_Subt := Etype (Function_Id);
5609 -- In the constrained case, add an implicit actual to the function call
5610 -- that provides access to the declared object. An unchecked conversion
5611 -- to the (specific) result type of the function is inserted to handle
5612 -- the case where the object is declared with a class-wide type.
5614 if Is_Constrained (Underlying_Type (Result_Subt)) then
5615 Caller_Object :=
5616 Make_Unchecked_Type_Conversion (Loc,
5617 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5618 Expression => New_Reference_To (Obj_Def_Id, Loc));
5620 -- When the function has a controlling result, an allocation-form
5621 -- parameter must be passed indicating that the caller is allocating
5622 -- the result object. This is needed because such a function can be
5623 -- called as a dispatching operation and must be treated similarly
5624 -- to functions with unconstrained result subtypes.
5626 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5627 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5629 -- If the function's result subtype is unconstrained and the object is
5630 -- a return object of an enclosing build-in-place function, then the
5631 -- implicit build-in-place parameters of the enclosing function must be
5632 -- passed along to the called function. (Unfortunately, this won't cover
5633 -- the case of extension aggregates where the ancestor part is a build-
5634 -- in-place unconstrained function call that should be passed along the
5635 -- caller's parameters. Currently those get mishandled by reassigning
5636 -- the result of the call to the aggregate return object, when the call
5637 -- result should really be directly built in place in the aggregate and
5638 -- not built in a temporary. ???)
5640 elsif Is_Return_Object (Defining_Identifier (Object_Decl)) then
5641 Pass_Caller_Acc := True;
5643 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5645 -- If the enclosing function has a constrained result type, then
5646 -- caller allocation will be used.
5648 if Is_Constrained (Etype (Enclosing_Func)) then
5649 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5650 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5652 -- Otherwise, when the enclosing function has an unconstrained result
5653 -- type, the BIP_Alloc_Form formal of the enclosing function must be
5654 -- passed along to the callee.
5656 else
5657 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5658 (Func_Call,
5659 Function_Id,
5660 Alloc_Form_Exp =>
5661 New_Reference_To
5662 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
5663 Loc));
5664 end if;
5666 -- Retrieve the BIPacc formal from the enclosing function and convert
5667 -- it to the access type of the callee's BIP_Object_Access formal.
5669 Caller_Object :=
5670 Make_Unchecked_Type_Conversion (Loc,
5671 Subtype_Mark =>
5672 New_Reference_To
5673 (Etype
5674 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
5675 Loc),
5676 Expression =>
5677 New_Reference_To
5678 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
5679 Loc));
5681 -- In other unconstrained cases, pass an indication to do the allocation
5682 -- on the secondary stack and set Caller_Object to Empty so that a null
5683 -- value will be passed for the caller's object address. A transient
5684 -- scope is established to ensure eventual cleanup of the result.
5686 else
5687 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5688 (Func_Call,
5689 Function_Id,
5690 Alloc_Form => Secondary_Stack);
5691 Caller_Object := Empty;
5693 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
5694 end if;
5696 Add_Final_List_Actual_To_Build_In_Place_Call
5697 (Func_Call, Function_Id, Acc_Type => Empty);
5699 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
5700 and then Has_Task (Result_Subt)
5701 then
5702 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5704 -- Here we're passing along the master that was passed in to this
5705 -- function.
5707 Add_Task_Actuals_To_Build_In_Place_Call
5708 (Func_Call, Function_Id,
5709 Master_Actual =>
5710 New_Reference_To
5711 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
5713 else
5714 Add_Task_Actuals_To_Build_In_Place_Call
5715 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5716 end if;
5718 Add_Access_Actual_To_Build_In_Place_Call
5719 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
5721 -- Create an access type designating the function's result subtype. We
5722 -- use the type of the original expression because it may be a call to
5723 -- an inherited operation, which the expansion has replaced with the
5724 -- parent operation that yields the parent type.
5726 Ref_Type := Make_Temporary (Loc, 'A');
5728 Ptr_Typ_Decl :=
5729 Make_Full_Type_Declaration (Loc,
5730 Defining_Identifier => Ref_Type,
5731 Type_Definition =>
5732 Make_Access_To_Object_Definition (Loc,
5733 All_Present => True,
5734 Subtype_Indication =>
5735 New_Reference_To (Etype (Function_Call), Loc)));
5737 -- The access type and its accompanying object must be inserted after
5738 -- the object declaration in the constrained case, so that the function
5739 -- call can be passed access to the object. In the unconstrained case,
5740 -- the access type and object must be inserted before the object, since
5741 -- the object declaration is rewritten to be a renaming of a dereference
5742 -- of the access object.
5744 if Is_Constrained (Underlying_Type (Result_Subt)) then
5745 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
5746 else
5747 Insert_Action (Object_Decl, Ptr_Typ_Decl);
5748 end if;
5750 -- Finally, create an access object initialized to a reference to the
5751 -- function call.
5753 New_Expr :=
5754 Make_Reference (Loc,
5755 Prefix => Relocate_Node (Func_Call));
5757 Def_Id := Make_Temporary (Loc, 'R', New_Expr);
5758 Set_Etype (Def_Id, Ref_Type);
5760 Insert_After_And_Analyze (Ptr_Typ_Decl,
5761 Make_Object_Declaration (Loc,
5762 Defining_Identifier => Def_Id,
5763 Object_Definition => New_Reference_To (Ref_Type, Loc),
5764 Expression => New_Expr));
5766 if Is_Constrained (Underlying_Type (Result_Subt)) then
5767 Set_Expression (Object_Decl, Empty);
5768 Set_No_Initialization (Object_Decl);
5770 -- In case of an unconstrained result subtype, rewrite the object
5771 -- declaration as an object renaming where the renamed object is a
5772 -- dereference of <function_Call>'reference:
5774 -- Obj : Subt renames <function_call>'Ref.all;
5776 else
5777 Call_Deref :=
5778 Make_Explicit_Dereference (Loc,
5779 Prefix => New_Reference_To (Def_Id, Loc));
5781 Rewrite (Object_Decl,
5782 Make_Object_Renaming_Declaration (Loc,
5783 Defining_Identifier => Make_Temporary (Loc, 'D'),
5784 Access_Definition => Empty,
5785 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
5786 Name => Call_Deref));
5788 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
5790 Analyze (Object_Decl);
5792 -- Replace the internal identifier of the renaming declaration's
5793 -- entity with identifier of the original object entity. We also have
5794 -- to exchange the entities containing their defining identifiers to
5795 -- ensure the correct replacement of the object declaration by the
5796 -- object renaming declaration to avoid homograph conflicts (since
5797 -- the object declaration's defining identifier was already entered
5798 -- in current scope). The Next_Entity links of the two entities also
5799 -- have to be swapped since the entities are part of the return
5800 -- scope's entity list and the list structure would otherwise be
5801 -- corrupted. Finally, the homonym chain must be preserved as well.
5803 declare
5804 Renaming_Def_Id : constant Entity_Id :=
5805 Defining_Identifier (Object_Decl);
5806 Next_Entity_Temp : constant Entity_Id :=
5807 Next_Entity (Renaming_Def_Id);
5808 begin
5809 Set_Chars (Renaming_Def_Id, Chars (Obj_Def_Id));
5811 -- Swap next entity links in preparation for exchanging entities
5813 Set_Next_Entity (Renaming_Def_Id, Next_Entity (Obj_Def_Id));
5814 Set_Next_Entity (Obj_Def_Id, Next_Entity_Temp);
5815 Set_Homonym (Renaming_Def_Id, Homonym (Obj_Def_Id));
5817 Exchange_Entities (Renaming_Def_Id, Obj_Def_Id);
5818 end;
5819 end if;
5821 -- If the object entity has a class-wide Etype, then we need to change
5822 -- it to the result subtype of the function call, because otherwise the
5823 -- object will be class-wide without an explicit initialization and
5824 -- won't be allocated properly by the back end. It seems unclean to make
5825 -- such a revision to the type at this point, and we should try to
5826 -- improve this treatment when build-in-place functions with class-wide
5827 -- results are implemented. ???
5829 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
5830 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
5831 end if;
5832 end Make_Build_In_Place_Call_In_Object_Declaration;
5834 --------------------------
5835 -- Needs_BIP_Final_List --
5836 --------------------------
5838 function Needs_BIP_Final_List (E : Entity_Id) return Boolean is
5839 pragma Assert (Is_Build_In_Place_Function (E));
5840 Result_Subt : constant Entity_Id := Underlying_Type (Etype (E));
5842 begin
5843 -- We need the BIP_Final_List if the result type needs finalization. We
5844 -- also need it for tagged types, even if not class-wide, because some
5845 -- type extension might need finalization, and all overriding functions
5846 -- must have the same calling conventions. However, if there is a
5847 -- pragma Restrictions (No_Finalization), we never need this parameter.
5849 return (Needs_Finalization (Result_Subt)
5850 or else Is_Tagged_Type (Underlying_Type (Result_Subt)))
5851 and then not Restriction_Active (No_Finalization);
5852 end Needs_BIP_Final_List;
5854 end Exp_Ch6;