1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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 Fname; use Fname;
45 with Freeze; use Freeze;
46 with Inline; use Inline;
48 with Namet; use Namet;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
54 with Rtsfind; use Rtsfind;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Res; use Sem_Res;
65 with Sem_Util; use Sem_Util;
66 with Sinfo; use Sinfo;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with Targparm; use Targparm;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Validsw; use Validsw;
74 package body Exp_Ch6 is
76 -----------------------
77 -- Local Subprograms --
78 -----------------------
80 procedure Add_Access_Actual_To_Build_In_Place_Call
81 (Function_Call : Node_Id;
82 Function_Id : Entity_Id;
83 Return_Object : Node_Id;
84 Is_Access : Boolean := False);
85 -- Ada 2005 (AI-318-02): Apply the Unrestricted_Access attribute to the
86 -- object name given by Return_Object and add the attribute to the end of
87 -- the actual parameter list associated with the build-in-place function
88 -- call denoted by Function_Call. However, if Is_Access is True, then
89 -- Return_Object is already an access expression, in which case it's passed
90 -- along directly to the build-in-place function. Finally, if Return_Object
91 -- is empty, then pass a null literal as the actual.
93 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
94 (Function_Call : Node_Id;
95 Function_Id : Entity_Id;
96 Alloc_Form : BIP_Allocation_Form := Unspecified;
97 Alloc_Form_Exp : Node_Id := Empty);
98 -- Ada 2005 (AI-318-02): Add an actual indicating the form of allocation,
99 -- if any, to be done by a build-in-place function. If Alloc_Form_Exp is
100 -- present, then use it, otherwise pass a literal corresponding to the
101 -- Alloc_Form parameter (which must not be Unspecified in that case).
103 procedure Add_Extra_Actual_To_Call
104 (Subprogram_Call : Node_Id;
105 Extra_Formal : Entity_Id;
106 Extra_Actual : Node_Id);
107 -- Adds Extra_Actual as a named parameter association for the formal
108 -- Extra_Formal in Subprogram_Call.
110 procedure Add_Final_List_Actual_To_Build_In_Place_Call
111 (Function_Call : Node_Id;
112 Function_Id : Entity_Id;
113 Acc_Type : Entity_Id);
114 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type has
115 -- controlled parts, add an actual parameter that is a pointer to
116 -- appropriate finalization list. The finalization list is that of the
117 -- current scope, except for "new Acc'(F(...))" in which case it's the
118 -- finalization list of the access type returned by the allocator. Acc_Type
119 -- is that type in the allocator case; Empty otherwise.
121 procedure Add_Task_Actuals_To_Build_In_Place_Call
122 (Function_Call : Node_Id;
123 Function_Id : Entity_Id;
124 Master_Actual : Node_Id);
125 -- Ada 2005 (AI-318-02): For a build-in-place call, if the result type
126 -- contains tasks, add two actual parameters: the master, and a pointer to
127 -- the caller's activation chain. Master_Actual is the actual parameter
128 -- expression to pass for the master. In most cases, this is the current
129 -- master (_master). The two exceptions are: If the function call is the
130 -- initialization expression for an allocator, we pass the master of the
131 -- access type. If the function call is the initialization expression for
132 -- a return object, we pass along the master passed in by the caller. The
133 -- activation chain to pass is always the local one.
135 procedure Check_Overriding_Operation (Subp : Entity_Id);
136 -- Subp is a dispatching operation. Check whether it may override an
137 -- inherited private operation, in which case its DT entry is that of
138 -- the hidden operation, not the one it may have received earlier.
139 -- This must be done before emitting the code to set the corresponding
140 -- DT to the address of the subprogram. The actual placement of Subp in
141 -- the proper place in the list of primitive operations is done in
142 -- Declare_Inherited_Private_Subprograms, which also has to deal with
143 -- implicit operations. This duplication is unavoidable for now???
145 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
146 -- This procedure is called only if the subprogram body N, whose spec
147 -- has the given entity Spec, contains a parameterless recursive call.
148 -- It attempts to generate runtime code to detect if this a case of
149 -- infinite recursion.
151 -- The body is scanned to determine dependencies. If the only external
152 -- dependencies are on a small set of scalar variables, then the values
153 -- of these variables are captured on entry to the subprogram, and if
154 -- the values are not changed for the call, we know immediately that
155 -- we have an infinite recursion.
157 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
158 -- For each actual of an in-out or out parameter which is a numeric
159 -- (view) conversion of the form T (A), where A denotes a variable,
160 -- we insert the declaration:
162 -- Temp : T[ := T (A)];
164 -- prior to the call. Then we replace the actual with a reference to Temp,
165 -- and append the assignment:
167 -- A := TypeA (Temp);
169 -- after the call. Here TypeA is the actual type of variable A.
170 -- For out parameters, the initial declaration has no expression.
171 -- If A is not an entity name, we generate instead:
173 -- Var : TypeA renames A;
174 -- Temp : T := Var; -- omitting expression for out parameter.
176 -- Var := TypeA (Temp);
178 -- For other in-out parameters, we emit the required constraint checks
179 -- before and/or after the call.
181 -- For all parameter modes, actuals that denote components and slices
182 -- of packed arrays are expanded into suitable temporaries.
184 -- For non-scalar objects that are possibly unaligned, add call by copy
185 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
187 procedure Expand_Inlined_Call
190 Orig_Subp : Entity_Id);
191 -- If called subprogram can be inlined by the front-end, retrieve the
192 -- analyzed body, replace formals with actuals and expand call in place.
193 -- Generate thunks for actuals that are expressions, and insert the
194 -- corresponding constant declarations before the call. If the original
195 -- call is to a derived operation, the return type is the one of the
196 -- derived operation, but the body is that of the original, so return
197 -- expressions in the body must be converted to the desired type (which
198 -- is simply not noted in the tree without inline expansion).
200 function Expand_Protected_Object_Reference
202 Scop : Entity_Id) return Node_Id;
204 procedure Expand_Protected_Subprogram_Call
208 -- A call to a protected subprogram within the protected object may appear
209 -- as a regular call. The list of actuals must be expanded to contain a
210 -- reference to the object itself, and the call becomes a call to the
211 -- corresponding protected subprogram.
213 ----------------------------------------------
214 -- Add_Access_Actual_To_Build_In_Place_Call --
215 ----------------------------------------------
217 procedure Add_Access_Actual_To_Build_In_Place_Call
218 (Function_Call : Node_Id;
219 Function_Id : Entity_Id;
220 Return_Object : Node_Id;
221 Is_Access : Boolean := False)
223 Loc : constant Source_Ptr := Sloc (Function_Call);
224 Obj_Address : Node_Id;
225 Obj_Acc_Formal : Entity_Id;
228 -- Locate the implicit access parameter in the called function
230 Obj_Acc_Formal := Build_In_Place_Formal (Function_Id, BIP_Object_Access);
232 -- If no return object is provided, then pass null
234 if not Present (Return_Object) then
235 Obj_Address := Make_Null (Loc);
236 Set_Parent (Obj_Address, Function_Call);
238 -- If Return_Object is already an expression of an access type, then use
239 -- it directly, since it must be an access value denoting the return
240 -- object, and couldn't possibly be the return object itself.
243 Obj_Address := Return_Object;
244 Set_Parent (Obj_Address, Function_Call);
246 -- Apply Unrestricted_Access to caller's return object
250 Make_Attribute_Reference (Loc,
251 Prefix => Return_Object,
252 Attribute_Name => Name_Unrestricted_Access);
254 Set_Parent (Return_Object, Obj_Address);
255 Set_Parent (Obj_Address, Function_Call);
258 Analyze_And_Resolve (Obj_Address, Etype (Obj_Acc_Formal));
260 -- Build the parameter association for the new actual and add it to the
261 -- end of the function's actuals.
263 Add_Extra_Actual_To_Call (Function_Call, Obj_Acc_Formal, Obj_Address);
264 end Add_Access_Actual_To_Build_In_Place_Call;
266 --------------------------------------------------
267 -- Add_Alloc_Form_Actual_To_Build_In_Place_Call --
268 --------------------------------------------------
270 procedure Add_Alloc_Form_Actual_To_Build_In_Place_Call
271 (Function_Call : Node_Id;
272 Function_Id : Entity_Id;
273 Alloc_Form : BIP_Allocation_Form := Unspecified;
274 Alloc_Form_Exp : Node_Id := Empty)
276 Loc : constant Source_Ptr := Sloc (Function_Call);
277 Alloc_Form_Actual : Node_Id;
278 Alloc_Form_Formal : Node_Id;
281 -- The allocation form generally doesn't need to be passed in the case
282 -- of a constrained result subtype, since normally the caller performs
283 -- the allocation in that case. However this formal is still needed in
284 -- the case where the function has a tagged result, because generally
285 -- such functions can be called in a dispatching context and such calls
286 -- must be handled like calls to class-wide functions.
288 if Is_Constrained (Underlying_Type (Etype (Function_Id)))
289 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
294 -- Locate the implicit allocation form parameter in the called function.
295 -- Maybe it would be better for each implicit formal of a build-in-place
296 -- function to have a flag or a Uint attribute to identify it. ???
298 Alloc_Form_Formal := Build_In_Place_Formal (Function_Id, BIP_Alloc_Form);
300 if Present (Alloc_Form_Exp) then
301 pragma Assert (Alloc_Form = Unspecified);
303 Alloc_Form_Actual := Alloc_Form_Exp;
306 pragma Assert (Alloc_Form /= Unspecified);
309 Make_Integer_Literal (Loc,
310 Intval => UI_From_Int (BIP_Allocation_Form'Pos (Alloc_Form)));
313 Analyze_And_Resolve (Alloc_Form_Actual, Etype (Alloc_Form_Formal));
315 -- Build the parameter association for the new actual and add it to the
316 -- end of the function's actuals.
318 Add_Extra_Actual_To_Call
319 (Function_Call, Alloc_Form_Formal, Alloc_Form_Actual);
320 end Add_Alloc_Form_Actual_To_Build_In_Place_Call;
322 ------------------------------
323 -- Add_Extra_Actual_To_Call --
324 ------------------------------
326 procedure Add_Extra_Actual_To_Call
327 (Subprogram_Call : Node_Id;
328 Extra_Formal : Entity_Id;
329 Extra_Actual : Node_Id)
331 Loc : constant Source_Ptr := Sloc (Subprogram_Call);
332 Param_Assoc : Node_Id;
336 Make_Parameter_Association (Loc,
337 Selector_Name => New_Occurrence_Of (Extra_Formal, Loc),
338 Explicit_Actual_Parameter => Extra_Actual);
340 Set_Parent (Param_Assoc, Subprogram_Call);
341 Set_Parent (Extra_Actual, Param_Assoc);
343 if Present (Parameter_Associations (Subprogram_Call)) then
344 if Nkind (Last (Parameter_Associations (Subprogram_Call))) =
345 N_Parameter_Association
348 -- Find last named actual, and append
353 L := First_Actual (Subprogram_Call);
354 while Present (L) loop
355 if No (Next_Actual (L)) then
356 Set_Next_Named_Actual (Parent (L), Extra_Actual);
364 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
367 Append (Param_Assoc, To => Parameter_Associations (Subprogram_Call));
370 Set_Parameter_Associations (Subprogram_Call, New_List (Param_Assoc));
371 Set_First_Named_Actual (Subprogram_Call, Extra_Actual);
373 end Add_Extra_Actual_To_Call;
375 --------------------------------------------------
376 -- Add_Final_List_Actual_To_Build_In_Place_Call --
377 --------------------------------------------------
379 procedure Add_Final_List_Actual_To_Build_In_Place_Call
380 (Function_Call : Node_Id;
381 Function_Id : Entity_Id;
382 Acc_Type : Entity_Id)
384 Loc : constant Source_Ptr := Sloc (Function_Call);
385 Final_List : Node_Id;
386 Final_List_Actual : Node_Id;
387 Final_List_Formal : Node_Id;
390 -- No such extra parameter is needed if there are no controlled parts.
391 -- The test for Controlled_Type accounts for class-wide results (which
392 -- potentially have controlled parts, even if the root type doesn't),
393 -- and the test for a tagged result type is needed because calls to
394 -- such a function can in general occur in dispatching contexts, which
395 -- must be treated the same as a call to class-wide functions. Both of
396 -- these situations require that a finalization list be passed.
398 if not Controlled_Type (Underlying_Type (Etype (Function_Id)))
399 and then not Is_Tagged_Type (Underlying_Type (Etype (Function_Id)))
404 -- Locate implicit finalization list parameter in the called function
406 Final_List_Formal := Build_In_Place_Formal (Function_Id, BIP_Final_List);
408 -- Create the actual which is a pointer to the appropriate finalization
409 -- list. Acc_Type is present if and only if this call is the
410 -- initialization of an allocator. Use the Current_Scope or the Acc_Type
413 if Present (Acc_Type)
414 and then (Ekind (Acc_Type) = E_Anonymous_Access_Type
416 Present (Associated_Final_Chain (Base_Type (Acc_Type))))
418 Final_List := Find_Final_List (Acc_Type);
420 Final_List := Find_Final_List (Current_Scope);
424 Make_Attribute_Reference (Loc,
425 Prefix => Final_List,
426 Attribute_Name => Name_Unrestricted_Access);
428 Analyze_And_Resolve (Final_List_Actual, Etype (Final_List_Formal));
430 -- Build the parameter association for the new actual and add it to the
431 -- end of the function's actuals.
433 Add_Extra_Actual_To_Call
434 (Function_Call, Final_List_Formal, Final_List_Actual);
435 end Add_Final_List_Actual_To_Build_In_Place_Call;
437 ---------------------------------------------
438 -- Add_Task_Actuals_To_Build_In_Place_Call --
439 ---------------------------------------------
441 procedure Add_Task_Actuals_To_Build_In_Place_Call
442 (Function_Call : Node_Id;
443 Function_Id : Entity_Id;
444 Master_Actual : Node_Id)
445 -- Note: Master_Actual can be Empty, but only if there are no tasks
447 Loc : constant Source_Ptr := Sloc (Function_Call);
450 -- No such extra parameters are needed if there are no tasks
452 if not Has_Task (Etype (Function_Id)) then
459 Master_Formal : Node_Id;
461 -- Locate implicit master parameter in the called function
463 Master_Formal := Build_In_Place_Formal (Function_Id, BIP_Master);
465 Analyze_And_Resolve (Master_Actual, Etype (Master_Formal));
467 -- Build the parameter association for the new actual and add it to
468 -- the end of the function's actuals.
470 Add_Extra_Actual_To_Call
471 (Function_Call, Master_Formal, Master_Actual);
474 -- The activation chain
477 Activation_Chain_Actual : Node_Id;
478 Activation_Chain_Formal : Node_Id;
480 -- Locate implicit activation chain parameter in the called function
482 Activation_Chain_Formal := Build_In_Place_Formal
483 (Function_Id, BIP_Activation_Chain);
485 -- Create the actual which is a pointer to the current activation
488 Activation_Chain_Actual :=
489 Make_Attribute_Reference (Loc,
490 Prefix => Make_Identifier (Loc, Name_uChain),
491 Attribute_Name => Name_Unrestricted_Access);
494 (Activation_Chain_Actual, Etype (Activation_Chain_Formal));
496 -- Build the parameter association for the new actual and add it to
497 -- the end of the function's actuals.
499 Add_Extra_Actual_To_Call
500 (Function_Call, Activation_Chain_Formal, Activation_Chain_Actual);
502 end Add_Task_Actuals_To_Build_In_Place_Call;
504 -----------------------
505 -- BIP_Formal_Suffix --
506 -----------------------
508 function BIP_Formal_Suffix (Kind : BIP_Formal_Kind) return String is
511 when BIP_Alloc_Form =>
513 when BIP_Final_List =>
514 return "BIPfinallist";
517 when BIP_Activation_Chain =>
518 return "BIPactivationchain";
519 when BIP_Object_Access =>
522 end BIP_Formal_Suffix;
524 ---------------------------
525 -- Build_In_Place_Formal --
526 ---------------------------
528 function Build_In_Place_Formal
530 Kind : BIP_Formal_Kind) return Entity_Id
532 Extra_Formal : Entity_Id := Extra_Formals (Func);
535 -- Maybe it would be better for each implicit formal of a build-in-place
536 -- function to have a flag or a Uint attribute to identify it. ???
539 pragma Assert (Present (Extra_Formal));
541 Chars (Extra_Formal) =
542 New_External_Name (Chars (Func), BIP_Formal_Suffix (Kind));
543 Next_Formal_With_Extras (Extra_Formal);
547 end Build_In_Place_Formal;
549 --------------------------------
550 -- Check_Overriding_Operation --
551 --------------------------------
553 procedure Check_Overriding_Operation (Subp : Entity_Id) is
554 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
555 Op_List : constant Elist_Id := Primitive_Operations (Typ);
561 if Is_Derived_Type (Typ)
562 and then not Is_Private_Type (Typ)
563 and then In_Open_Scopes (Scope (Etype (Typ)))
564 and then Typ = Base_Type (Typ)
566 -- Subp overrides an inherited private operation if there is an
567 -- inherited operation with a different name than Subp (see
568 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
569 -- same name as Subp.
571 Op_Elmt := First_Elmt (Op_List);
572 while Present (Op_Elmt) loop
573 Prim_Op := Node (Op_Elmt);
574 Par_Op := Alias (Prim_Op);
577 and then not Comes_From_Source (Prim_Op)
578 and then Chars (Prim_Op) /= Chars (Par_Op)
579 and then Chars (Par_Op) = Chars (Subp)
580 and then Is_Hidden (Par_Op)
581 and then Type_Conformant (Prim_Op, Subp)
583 Set_DT_Position (Subp, DT_Position (Prim_Op));
589 end Check_Overriding_Operation;
591 -------------------------------
592 -- Detect_Infinite_Recursion --
593 -------------------------------
595 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
596 Loc : constant Source_Ptr := Sloc (N);
598 Var_List : constant Elist_Id := New_Elmt_List;
599 -- List of globals referenced by body of procedure
601 Call_List : constant Elist_Id := New_Elmt_List;
602 -- List of recursive calls in body of procedure
604 Shad_List : constant Elist_Id := New_Elmt_List;
605 -- List of entity id's for entities created to capture the value of
606 -- referenced globals on entry to the procedure.
608 Scop : constant Uint := Scope_Depth (Spec);
609 -- This is used to record the scope depth of the current procedure, so
610 -- that we can identify global references.
612 Max_Vars : constant := 4;
613 -- Do not test more than four global variables
615 Count_Vars : Natural := 0;
616 -- Count variables found so far
628 function Process (Nod : Node_Id) return Traverse_Result;
629 -- Function to traverse the subprogram body (using Traverse_Func)
635 function Process (Nod : Node_Id) return Traverse_Result is
639 if Nkind (Nod) = N_Procedure_Call_Statement then
641 -- Case of one of the detected recursive calls
643 if Is_Entity_Name (Name (Nod))
644 and then Has_Recursive_Call (Entity (Name (Nod)))
645 and then Entity (Name (Nod)) = Spec
647 Append_Elmt (Nod, Call_List);
650 -- Any other procedure call may have side effects
656 -- A call to a pure function can always be ignored
658 elsif Nkind (Nod) = N_Function_Call
659 and then Is_Entity_Name (Name (Nod))
660 and then Is_Pure (Entity (Name (Nod)))
664 -- Case of an identifier reference
666 elsif Nkind (Nod) = N_Identifier then
669 -- If no entity, then ignore the reference
671 -- Not clear why this can happen. To investigate, remove this
672 -- test and look at the crash that occurs here in 3401-004 ???
677 -- Ignore entities with no Scope, again not clear how this
678 -- can happen, to investigate, look at 4108-008 ???
680 elsif No (Scope (Ent)) then
683 -- Ignore the reference if not to a more global object
685 elsif Scope_Depth (Scope (Ent)) >= Scop then
688 -- References to types, exceptions and constants are always OK
691 or else Ekind (Ent) = E_Exception
692 or else Ekind (Ent) = E_Constant
696 -- If other than a non-volatile scalar variable, we have some
697 -- kind of global reference (e.g. to a function) that we cannot
698 -- deal with so we forget the attempt.
700 elsif Ekind (Ent) /= E_Variable
701 or else not Is_Scalar_Type (Etype (Ent))
702 or else Treat_As_Volatile (Ent)
706 -- Otherwise we have a reference to a global scalar
709 -- Loop through global entities already detected
711 Elm := First_Elmt (Var_List);
713 -- If not detected before, record this new global reference
716 Count_Vars := Count_Vars + 1;
718 if Count_Vars <= Max_Vars then
719 Append_Elmt (Entity (Nod), Var_List);
726 -- If recorded before, ignore
728 elsif Node (Elm) = Entity (Nod) then
731 -- Otherwise keep looking
741 -- For all other node kinds, recursively visit syntactic children
748 function Traverse_Body is new Traverse_Func (Process);
750 -- Start of processing for Detect_Infinite_Recursion
753 -- Do not attempt detection in No_Implicit_Conditional mode, since we
754 -- won't be able to generate the code to handle the recursion in any
757 if Restriction_Active (No_Implicit_Conditionals) then
761 -- Otherwise do traversal and quit if we get abandon signal
763 if Traverse_Body (N) = Abandon then
766 -- We must have a call, since Has_Recursive_Call was set. If not just
767 -- ignore (this is only an error check, so if we have a funny situation,
768 -- due to bugs or errors, we do not want to bomb!)
770 elsif Is_Empty_Elmt_List (Call_List) then
774 -- Here is the case where we detect recursion at compile time
776 -- Push our current scope for analyzing the declarations and code that
777 -- we will insert for the checking.
781 -- This loop builds temporary variables for each of the referenced
782 -- globals, so that at the end of the loop the list Shad_List contains
783 -- these temporaries in one-to-one correspondence with the elements in
787 Elm := First_Elmt (Var_List);
788 while Present (Elm) loop
791 Make_Defining_Identifier (Loc,
792 Chars => New_Internal_Name ('S'));
793 Append_Elmt (Ent, Shad_List);
795 -- Insert a declaration for this temporary at the start of the
796 -- declarations for the procedure. The temporaries are declared as
797 -- constant objects initialized to the current values of the
798 -- corresponding temporaries.
801 Make_Object_Declaration (Loc,
802 Defining_Identifier => Ent,
803 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
804 Constant_Present => True,
805 Expression => New_Occurrence_Of (Var, Loc));
808 Prepend (Decl, Declarations (N));
810 Insert_After (Last, Decl);
818 -- Loop through calls
820 Call := First_Elmt (Call_List);
821 while Present (Call) loop
823 -- Build a predicate expression of the form
826 -- and then global1 = temp1
827 -- and then global2 = temp2
830 -- This predicate determines if any of the global values
831 -- referenced by the procedure have changed since the
832 -- current call, if not an infinite recursion is assured.
834 Test := New_Occurrence_Of (Standard_True, Loc);
836 Elm1 := First_Elmt (Var_List);
837 Elm2 := First_Elmt (Shad_List);
838 while Present (Elm1) loop
844 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
845 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
851 -- Now we replace the call with the sequence
853 -- if no-changes (see above) then
854 -- raise Storage_Error;
859 Rewrite (Node (Call),
860 Make_If_Statement (Loc,
862 Then_Statements => New_List (
863 Make_Raise_Storage_Error (Loc,
864 Reason => SE_Infinite_Recursion)),
866 Else_Statements => New_List (
867 Relocate_Node (Node (Call)))));
869 Analyze (Node (Call));
874 -- Remove temporary scope stack entry used for analysis
877 end Detect_Infinite_Recursion;
883 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
884 Loc : constant Source_Ptr := Sloc (N);
889 E_Formal : Entity_Id;
891 procedure Add_Call_By_Copy_Code;
892 -- For cases where the parameter must be passed by copy, this routine
893 -- generates a temporary variable into which the actual is copied and
894 -- then passes this as the parameter. For an OUT or IN OUT parameter,
895 -- an assignment is also generated to copy the result back. The call
896 -- also takes care of any constraint checks required for the type
897 -- conversion case (on both the way in and the way out).
899 procedure Add_Simple_Call_By_Copy_Code;
900 -- This is similar to the above, but is used in cases where we know
901 -- that all that is needed is to simply create a temporary and copy
902 -- the value in and out of the temporary.
904 procedure Check_Fortran_Logical;
905 -- A value of type Logical that is passed through a formal parameter
906 -- must be normalized because .TRUE. usually does not have the same
907 -- representation as True. We assume that .FALSE. = False = 0.
908 -- What about functions that return a logical type ???
910 function Is_Legal_Copy return Boolean;
911 -- Check that an actual can be copied before generating the temporary
912 -- to be used in the call. If the actual is of a by_reference type then
913 -- the program is illegal (this can only happen in the presence of
914 -- rep. clauses that force an incorrect alignment). If the formal is
915 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
916 -- the effect that this might lead to unaligned arguments.
918 function Make_Var (Actual : Node_Id) return Entity_Id;
919 -- Returns an entity that refers to the given actual parameter,
920 -- Actual (not including any type conversion). If Actual is an
921 -- entity name, then this entity is returned unchanged, otherwise
922 -- a renaming is created to provide an entity for the actual.
924 procedure Reset_Packed_Prefix;
925 -- The expansion of a packed array component reference is delayed in
926 -- the context of a call. Now we need to complete the expansion, so we
927 -- unmark the analyzed bits in all prefixes.
929 ---------------------------
930 -- Add_Call_By_Copy_Code --
931 ---------------------------
933 procedure Add_Call_By_Copy_Code is
939 F_Typ : constant Entity_Id := Etype (Formal);
944 if not Is_Legal_Copy then
949 Make_Defining_Identifier (Loc,
950 Chars => New_Internal_Name ('T'));
952 -- Use formal type for temp, unless formal type is an unconstrained
953 -- array, in which case we don't have to worry about bounds checks,
954 -- and we use the actual type, since that has appropriate bounds.
956 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
957 Indic := New_Occurrence_Of (Etype (Actual), Loc);
959 Indic := New_Occurrence_Of (Etype (Formal), Loc);
962 if Nkind (Actual) = N_Type_Conversion then
963 V_Typ := Etype (Expression (Actual));
965 -- If the formal is an (in-)out parameter, capture the name
966 -- of the variable in order to build the post-call assignment.
968 Var := Make_Var (Expression (Actual));
970 Crep := not Same_Representation
971 (F_Typ, Etype (Expression (Actual)));
974 V_Typ := Etype (Actual);
975 Var := Make_Var (Actual);
979 -- Setup initialization for case of in out parameter, or an out
980 -- parameter where the formal is an unconstrained array (in the
981 -- latter case, we have to pass in an object with bounds).
983 -- If this is an out parameter, the initial copy is wasteful, so as
984 -- an optimization for the one-dimensional case we extract the
985 -- bounds of the actual and build an uninitialized temporary of the
988 if Ekind (Formal) = E_In_Out_Parameter
989 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
991 if Nkind (Actual) = N_Type_Conversion then
992 if Conversion_OK (Actual) then
993 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
995 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
998 elsif Ekind (Formal) = E_Out_Parameter
999 and then Is_Array_Type (F_Typ)
1000 and then Number_Dimensions (F_Typ) = 1
1001 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
1003 -- Actual is a one-dimensional array or slice, and the type
1004 -- requires no initialization. Create a temporary of the
1005 -- right size, but do not copy actual into it (optimization).
1009 Make_Subtype_Indication (Loc,
1011 New_Occurrence_Of (F_Typ, Loc),
1013 Make_Index_Or_Discriminant_Constraint (Loc,
1014 Constraints => New_List (
1017 Make_Attribute_Reference (Loc,
1018 Prefix => New_Occurrence_Of (Var, Loc),
1019 Attribute_name => Name_First),
1021 Make_Attribute_Reference (Loc,
1022 Prefix => New_Occurrence_Of (Var, Loc),
1023 Attribute_Name => Name_Last)))));
1026 Init := New_Occurrence_Of (Var, Loc);
1029 -- An initialization is created for packed conversions as
1030 -- actuals for out parameters to enable Make_Object_Declaration
1031 -- to determine the proper subtype for N_Node. Note that this
1032 -- is wasteful because the extra copying on the call side is
1033 -- not required for such out parameters. ???
1035 elsif Ekind (Formal) = E_Out_Parameter
1036 and then Nkind (Actual) = N_Type_Conversion
1037 and then (Is_Bit_Packed_Array (F_Typ)
1039 Is_Bit_Packed_Array (Etype (Expression (Actual))))
1041 if Conversion_OK (Actual) then
1042 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1044 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1047 elsif Ekind (Formal) = E_In_Parameter then
1049 -- Handle the case in which the actual is a type conversion
1051 if Nkind (Actual) = N_Type_Conversion then
1052 if Conversion_OK (Actual) then
1053 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1055 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
1058 Init := New_Occurrence_Of (Var, Loc);
1066 Make_Object_Declaration (Loc,
1067 Defining_Identifier => Temp,
1068 Object_Definition => Indic,
1069 Expression => Init);
1070 Set_Assignment_OK (N_Node);
1071 Insert_Action (N, N_Node);
1073 -- Now, normally the deal here is that we use the defining
1074 -- identifier created by that object declaration. There is
1075 -- one exception to this. In the change of representation case
1076 -- the above declaration will end up looking like:
1078 -- temp : type := identifier;
1080 -- And in this case we might as well use the identifier directly
1081 -- and eliminate the temporary. Note that the analysis of the
1082 -- declaration was not a waste of time in that case, since it is
1083 -- what generated the necessary change of representation code. If
1084 -- the change of representation introduced additional code, as in
1085 -- a fixed-integer conversion, the expression is not an identifier
1086 -- and must be kept.
1089 and then Present (Expression (N_Node))
1090 and then Is_Entity_Name (Expression (N_Node))
1092 Temp := Entity (Expression (N_Node));
1093 Rewrite (N_Node, Make_Null_Statement (Loc));
1096 -- For IN parameter, all we do is to replace the actual
1098 if Ekind (Formal) = E_In_Parameter then
1099 Rewrite (Actual, New_Reference_To (Temp, Loc));
1102 -- Processing for OUT or IN OUT parameter
1105 -- Kill current value indications for the temporary variable we
1106 -- created, since we just passed it as an OUT parameter.
1108 Kill_Current_Values (Temp);
1110 -- If type conversion, use reverse conversion on exit
1112 if Nkind (Actual) = N_Type_Conversion then
1113 if Conversion_OK (Actual) then
1114 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1116 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
1119 Expr := New_Occurrence_Of (Temp, Loc);
1122 Rewrite (Actual, New_Reference_To (Temp, Loc));
1125 -- If the actual is a conversion of a packed reference, it may
1126 -- already have been expanded by Remove_Side_Effects, and the
1127 -- resulting variable is a temporary which does not designate
1128 -- the proper out-parameter, which may not be addressable. In
1129 -- that case, generate an assignment to the original expression
1130 -- (before expansion of the packed reference) so that the proper
1131 -- expansion of assignment to a packed component can take place.
1138 if Is_Renaming_Of_Object (Var)
1139 and then Nkind (Renamed_Object (Var)) = N_Selected_Component
1140 and then Is_Entity_Name (Prefix (Renamed_Object (Var)))
1141 and then Nkind (Original_Node (Prefix (Renamed_Object (Var))))
1142 = N_Indexed_Component
1144 Has_Non_Standard_Rep (Etype (Prefix (Renamed_Object (Var))))
1146 Obj := Renamed_Object (Var);
1148 Make_Selected_Component (Loc,
1150 New_Copy_Tree (Original_Node (Prefix (Obj))),
1151 Selector_Name => New_Copy (Selector_Name (Obj)));
1152 Reset_Analyzed_Flags (Lhs);
1155 Lhs := New_Occurrence_Of (Var, Loc);
1158 Set_Assignment_OK (Lhs);
1160 Append_To (Post_Call,
1161 Make_Assignment_Statement (Loc,
1163 Expression => Expr));
1167 end Add_Call_By_Copy_Code;
1169 ----------------------------------
1170 -- Add_Simple_Call_By_Copy_Code --
1171 ----------------------------------
1173 procedure Add_Simple_Call_By_Copy_Code is
1181 F_Typ : constant Entity_Id := Etype (Formal);
1184 if not Is_Legal_Copy then
1188 -- Use formal type for temp, unless formal type is an unconstrained
1189 -- array, in which case we don't have to worry about bounds checks,
1190 -- and we use the actual type, since that has appropriate bounds.
1192 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
1193 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1195 Indic := New_Occurrence_Of (Etype (Formal), Loc);
1198 -- Prepare to generate code
1200 Reset_Packed_Prefix;
1203 Make_Defining_Identifier (Loc,
1204 Chars => New_Internal_Name ('T'));
1205 Incod := Relocate_Node (Actual);
1206 Outcod := New_Copy_Tree (Incod);
1208 -- Generate declaration of temporary variable, initializing it
1209 -- with the input parameter unless we have an OUT formal or
1210 -- this is an initialization call.
1212 -- If the formal is an out parameter with discriminants, the
1213 -- discriminants must be captured even if the rest of the object
1214 -- is in principle uninitialized, because the discriminants may
1215 -- be read by the called subprogram.
1217 if Ekind (Formal) = E_Out_Parameter then
1220 if Has_Discriminants (Etype (Formal)) then
1221 Indic := New_Occurrence_Of (Etype (Actual), Loc);
1224 elsif Inside_Init_Proc then
1226 -- Could use a comment here to match comment below ???
1228 if Nkind (Actual) /= N_Selected_Component
1230 not Has_Discriminant_Dependent_Constraint
1231 (Entity (Selector_Name (Actual)))
1235 -- Otherwise, keep the component in order to generate the proper
1236 -- actual subtype, that depends on enclosing discriminants.
1244 Make_Object_Declaration (Loc,
1245 Defining_Identifier => Temp,
1246 Object_Definition => Indic,
1247 Expression => Incod);
1252 -- If the call is to initialize a component of a composite type,
1253 -- and the component does not depend on discriminants, use the
1254 -- actual type of the component. This is required in case the
1255 -- component is constrained, because in general the formal of the
1256 -- initialization procedure will be unconstrained. Note that if
1257 -- the component being initialized is constrained by an enclosing
1258 -- discriminant, the presence of the initialization in the
1259 -- declaration will generate an expression for the actual subtype.
1261 Set_No_Initialization (Decl);
1262 Set_Object_Definition (Decl,
1263 New_Occurrence_Of (Etype (Actual), Loc));
1266 Insert_Action (N, Decl);
1268 -- The actual is simply a reference to the temporary
1270 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
1272 -- Generate copy out if OUT or IN OUT parameter
1274 if Ekind (Formal) /= E_In_Parameter then
1276 Rhs := New_Occurrence_Of (Temp, Loc);
1278 -- Deal with conversion
1280 if Nkind (Lhs) = N_Type_Conversion then
1281 Lhs := Expression (Lhs);
1282 Rhs := Convert_To (Etype (Actual), Rhs);
1285 Append_To (Post_Call,
1286 Make_Assignment_Statement (Loc,
1288 Expression => Rhs));
1289 Set_Assignment_OK (Name (Last (Post_Call)));
1291 end Add_Simple_Call_By_Copy_Code;
1293 ---------------------------
1294 -- Check_Fortran_Logical --
1295 ---------------------------
1297 procedure Check_Fortran_Logical is
1298 Logical : constant Entity_Id := Etype (Formal);
1301 -- Note: this is very incomplete, e.g. it does not handle arrays
1302 -- of logical values. This is really not the right approach at all???)
1305 if Convention (Subp) = Convention_Fortran
1306 and then Root_Type (Etype (Formal)) = Standard_Boolean
1307 and then Ekind (Formal) /= E_In_Parameter
1309 Var := Make_Var (Actual);
1310 Append_To (Post_Call,
1311 Make_Assignment_Statement (Loc,
1312 Name => New_Occurrence_Of (Var, Loc),
1314 Unchecked_Convert_To (
1317 Left_Opnd => New_Occurrence_Of (Var, Loc),
1319 Unchecked_Convert_To (
1321 New_Occurrence_Of (Standard_False, Loc))))));
1323 end Check_Fortran_Logical;
1329 function Is_Legal_Copy return Boolean is
1331 -- An attempt to copy a value of such a type can only occur if
1332 -- representation clauses give the actual a misaligned address.
1334 if Is_By_Reference_Type (Etype (Formal)) then
1336 ("misaligned actual cannot be passed by reference", Actual);
1339 -- For users of Starlet, we assume that the specification of by-
1340 -- reference mechanism is mandatory. This may lead to unaligned
1341 -- objects but at least for DEC legacy code it is known to work.
1342 -- The warning will alert users of this code that a problem may
1345 elsif Mechanism (Formal) = By_Reference
1346 and then Is_Valued_Procedure (Scope (Formal))
1349 ("by_reference actual may be misaligned?", Actual);
1361 function Make_Var (Actual : Node_Id) return Entity_Id is
1365 if Is_Entity_Name (Actual) then
1366 return Entity (Actual);
1370 Make_Defining_Identifier (Loc,
1371 Chars => New_Internal_Name ('T'));
1374 Make_Object_Renaming_Declaration (Loc,
1375 Defining_Identifier => Var,
1377 New_Occurrence_Of (Etype (Actual), Loc),
1378 Name => Relocate_Node (Actual));
1380 Insert_Action (N, N_Node);
1385 -------------------------
1386 -- Reset_Packed_Prefix --
1387 -------------------------
1389 procedure Reset_Packed_Prefix is
1390 Pfx : Node_Id := Actual;
1393 Set_Analyzed (Pfx, False);
1394 exit when Nkind (Pfx) /= N_Selected_Component
1395 and then Nkind (Pfx) /= N_Indexed_Component;
1396 Pfx := Prefix (Pfx);
1398 end Reset_Packed_Prefix;
1400 -- Start of processing for Expand_Actuals
1403 Post_Call := New_List;
1405 Formal := First_Formal (Subp);
1406 Actual := First_Actual (N);
1407 while Present (Formal) loop
1408 E_Formal := Etype (Formal);
1410 if Is_Scalar_Type (E_Formal)
1411 or else Nkind (Actual) = N_Slice
1413 Check_Fortran_Logical;
1417 elsif Ekind (Formal) /= E_Out_Parameter then
1419 -- The unusual case of the current instance of a protected type
1420 -- requires special handling. This can only occur in the context
1421 -- of a call within the body of a protected operation.
1423 if Is_Entity_Name (Actual)
1424 and then Ekind (Entity (Actual)) = E_Protected_Type
1425 and then In_Open_Scopes (Entity (Actual))
1427 if Scope (Subp) /= Entity (Actual) then
1428 Error_Msg_N ("operation outside protected type may not "
1429 & "call back its protected operations?", Actual);
1433 Expand_Protected_Object_Reference (N, Entity (Actual)));
1436 -- Ada 2005 (AI-318-02): If the actual parameter is a call to a
1437 -- build-in-place function, then a temporary return object needs
1438 -- to be created and access to it must be passed to the function.
1439 -- Currently we limit such functions to those with inherently
1440 -- limited result subtypes, but eventually we plan to expand the
1441 -- functions that are treated as build-in-place to include other
1442 -- composite result types.
1444 if Ada_Version >= Ada_05
1445 and then Is_Build_In_Place_Function_Call (Actual)
1447 Make_Build_In_Place_Call_In_Anonymous_Context (Actual);
1450 Apply_Constraint_Check (Actual, E_Formal);
1452 -- Out parameter case. No constraint checks on access type
1455 elsif Is_Access_Type (E_Formal) then
1460 elsif Has_Discriminants (Base_Type (E_Formal))
1461 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1463 Apply_Constraint_Check (Actual, E_Formal);
1468 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1471 -- Processing for IN-OUT and OUT parameters
1473 if Ekind (Formal) /= E_In_Parameter then
1475 -- For type conversions of arrays, apply length/range checks
1477 if Is_Array_Type (E_Formal)
1478 and then Nkind (Actual) = N_Type_Conversion
1480 if Is_Constrained (E_Formal) then
1481 Apply_Length_Check (Expression (Actual), E_Formal);
1483 Apply_Range_Check (Expression (Actual), E_Formal);
1487 -- If argument is a type conversion for a type that is passed
1488 -- by copy, then we must pass the parameter by copy.
1490 if Nkind (Actual) = N_Type_Conversion
1492 (Is_Numeric_Type (E_Formal)
1493 or else Is_Access_Type (E_Formal)
1494 or else Is_Enumeration_Type (E_Formal)
1495 or else Is_Bit_Packed_Array (Etype (Formal))
1496 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1498 -- Also pass by copy if change of representation
1500 or else not Same_Representation
1502 Etype (Expression (Actual))))
1504 Add_Call_By_Copy_Code;
1506 -- References to components of bit packed arrays are expanded
1507 -- at this point, rather than at the point of analysis of the
1508 -- actuals, to handle the expansion of the assignment to
1509 -- [in] out parameters.
1511 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1512 Add_Simple_Call_By_Copy_Code;
1514 -- If a non-scalar actual is possibly bit-aligned, we need a copy
1515 -- because the back-end cannot cope with such objects. In other
1516 -- cases where alignment forces a copy, the back-end generates
1517 -- it properly. It should not be generated unconditionally in the
1518 -- front-end because it does not know precisely the alignment
1519 -- requirements of the target, and makes too conservative an
1520 -- estimate, leading to superfluous copies or spurious errors
1521 -- on by-reference parameters.
1523 elsif Nkind (Actual) = N_Selected_Component
1525 Component_May_Be_Bit_Aligned (Entity (Selector_Name (Actual)))
1526 and then not Represented_As_Scalar (Etype (Formal))
1528 Add_Simple_Call_By_Copy_Code;
1530 -- References to slices of bit packed arrays are expanded
1532 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1533 Add_Call_By_Copy_Code;
1535 -- References to possibly unaligned slices of arrays are expanded
1537 elsif Is_Possibly_Unaligned_Slice (Actual) then
1538 Add_Call_By_Copy_Code;
1540 -- Deal with access types where the actual subtype and the
1541 -- formal subtype are not the same, requiring a check.
1543 -- It is necessary to exclude tagged types because of "downward
1544 -- conversion" errors and a strange assertion error in namet
1545 -- from gnatf in bug 1215-001 ???
1547 elsif Is_Access_Type (E_Formal)
1548 and then not Same_Type (E_Formal, Etype (Actual))
1549 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1551 Add_Call_By_Copy_Code;
1553 -- If the actual is not a scalar and is marked for volatile
1554 -- treatment, whereas the formal is not volatile, then pass
1555 -- by copy unless it is a by-reference type.
1557 elsif Is_Entity_Name (Actual)
1558 and then Treat_As_Volatile (Entity (Actual))
1559 and then not Is_By_Reference_Type (Etype (Actual))
1560 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1561 and then not Treat_As_Volatile (E_Formal)
1563 Add_Call_By_Copy_Code;
1565 elsif Nkind (Actual) = N_Indexed_Component
1566 and then Is_Entity_Name (Prefix (Actual))
1567 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1569 Add_Call_By_Copy_Code;
1572 -- Processing for IN parameters
1575 -- For IN parameters is in the packed array case, we expand an
1576 -- indexed component (the circuit in Exp_Ch4 deliberately left
1577 -- indexed components appearing as actuals untouched, so that
1578 -- the special processing above for the OUT and IN OUT cases
1579 -- could be performed. We could make the test in Exp_Ch4 more
1580 -- complex and have it detect the parameter mode, but it is
1581 -- easier simply to handle all cases here.)
1583 if Nkind (Actual) = N_Indexed_Component
1584 and then Is_Packed (Etype (Prefix (Actual)))
1586 Reset_Packed_Prefix;
1587 Expand_Packed_Element_Reference (Actual);
1589 -- If we have a reference to a bit packed array, we copy it,
1590 -- since the actual must be byte aligned.
1592 -- Is this really necessary in all cases???
1594 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1595 Add_Simple_Call_By_Copy_Code;
1597 -- If a non-scalar actual is possibly unaligned, we need a copy
1599 elsif Is_Possibly_Unaligned_Object (Actual)
1600 and then not Represented_As_Scalar (Etype (Formal))
1602 Add_Simple_Call_By_Copy_Code;
1604 -- Similarly, we have to expand slices of packed arrays here
1605 -- because the result must be byte aligned.
1607 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1608 Add_Call_By_Copy_Code;
1610 -- Only processing remaining is to pass by copy if this is a
1611 -- reference to a possibly unaligned slice, since the caller
1612 -- expects an appropriately aligned argument.
1614 elsif Is_Possibly_Unaligned_Slice (Actual) then
1615 Add_Call_By_Copy_Code;
1619 Next_Formal (Formal);
1620 Next_Actual (Actual);
1623 -- Find right place to put post call stuff if it is present
1625 if not Is_Empty_List (Post_Call) then
1627 -- If call is not a list member, it must be the triggering statement
1628 -- of a triggering alternative or an entry call alternative, and we
1629 -- can add the post call stuff to the corresponding statement list.
1631 if not Is_List_Member (N) then
1633 P : constant Node_Id := Parent (N);
1636 pragma Assert (Nkind (P) = N_Triggering_Alternative
1637 or else Nkind (P) = N_Entry_Call_Alternative);
1639 if Is_Non_Empty_List (Statements (P)) then
1640 Insert_List_Before_And_Analyze
1641 (First (Statements (P)), Post_Call);
1643 Set_Statements (P, Post_Call);
1647 -- Otherwise, normal case where N is in a statement sequence,
1648 -- just put the post-call stuff after the call statement.
1651 Insert_Actions_After (N, Post_Call);
1655 -- The call node itself is re-analyzed in Expand_Call
1663 -- This procedure handles expansion of function calls and procedure call
1664 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1665 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1667 -- Replace call to Raise_Exception by Raise_Exception always if possible
1668 -- Provide values of actuals for all formals in Extra_Formals list
1669 -- Replace "call" to enumeration literal function by literal itself
1670 -- Rewrite call to predefined operator as operator
1671 -- Replace actuals to in-out parameters that are numeric conversions,
1672 -- with explicit assignment to temporaries before and after the call.
1673 -- Remove optional actuals if First_Optional_Parameter specified.
1675 -- Note that the list of actuals has been filled with default expressions
1676 -- during semantic analysis of the call. Only the extra actuals required
1677 -- for the 'Constrained attribute and for accessibility checks are added
1680 procedure Expand_Call (N : Node_Id) is
1681 Loc : constant Source_Ptr := Sloc (N);
1682 Extra_Actuals : List_Id := No_List;
1683 Prev : Node_Id := Empty;
1685 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1686 -- Adds one entry to the end of the actual parameter list. Used for
1687 -- default parameters and for extra actuals (for Extra_Formals). The
1688 -- argument is an N_Parameter_Association node.
1690 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1691 -- Adds an extra actual to the list of extra actuals. Expr is the
1692 -- expression for the value of the actual, EF is the entity for the
1695 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1696 -- Within an instance, a type derived from a non-tagged formal derived
1697 -- type inherits from the original parent, not from the actual. This is
1698 -- tested in 4723-003. The current derivation mechanism has the derived
1699 -- type inherit from the actual, which is only correct outside of the
1700 -- instance. If the subprogram is inherited, we test for this particular
1701 -- case through a convoluted tree traversal before setting the proper
1702 -- subprogram to be called.
1704 --------------------------
1705 -- Add_Actual_Parameter --
1706 --------------------------
1708 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1709 Actual_Expr : constant Node_Id :=
1710 Explicit_Actual_Parameter (Insert_Param);
1713 -- Case of insertion is first named actual
1715 if No (Prev) or else
1716 Nkind (Parent (Prev)) /= N_Parameter_Association
1718 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1719 Set_First_Named_Actual (N, Actual_Expr);
1722 if No (Parameter_Associations (N)) then
1723 Set_Parameter_Associations (N, New_List);
1724 Append (Insert_Param, Parameter_Associations (N));
1727 Insert_After (Prev, Insert_Param);
1730 -- Case of insertion is not first named actual
1733 Set_Next_Named_Actual
1734 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1735 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1736 Append (Insert_Param, Parameter_Associations (N));
1739 Prev := Actual_Expr;
1740 end Add_Actual_Parameter;
1742 ----------------------
1743 -- Add_Extra_Actual --
1744 ----------------------
1746 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1747 Loc : constant Source_Ptr := Sloc (Expr);
1750 if Extra_Actuals = No_List then
1751 Extra_Actuals := New_List;
1752 Set_Parent (Extra_Actuals, N);
1755 Append_To (Extra_Actuals,
1756 Make_Parameter_Association (Loc,
1757 Explicit_Actual_Parameter => Expr,
1759 Make_Identifier (Loc, Chars (EF))));
1761 Analyze_And_Resolve (Expr, Etype (EF));
1762 end Add_Extra_Actual;
1764 ---------------------------
1765 -- Inherited_From_Formal --
1766 ---------------------------
1768 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1770 Gen_Par : Entity_Id;
1771 Gen_Prim : Elist_Id;
1776 -- If the operation is inherited, it is attached to the corresponding
1777 -- type derivation. If the parent in the derivation is a generic
1778 -- actual, it is a subtype of the actual, and we have to recover the
1779 -- original derived type declaration to find the proper parent.
1781 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1782 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1783 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1784 N_Derived_Type_Definition
1785 or else not In_Instance
1792 (Type_Definition (Original_Node (Parent (S)))));
1794 if Nkind (Indic) = N_Subtype_Indication then
1795 Par := Entity (Subtype_Mark (Indic));
1797 Par := Entity (Indic);
1801 if not Is_Generic_Actual_Type (Par)
1802 or else Is_Tagged_Type (Par)
1803 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1804 or else not In_Open_Scopes (Scope (Par))
1809 Gen_Par := Generic_Parent_Type (Parent (Par));
1812 -- If the actual has no generic parent type, the formal is not
1813 -- a formal derived type, so nothing to inherit.
1815 if No (Gen_Par) then
1819 -- If the generic parent type is still the generic type, this is a
1820 -- private formal, not a derived formal, and there are no operations
1821 -- inherited from the formal.
1823 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1827 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1829 Elmt := First_Elmt (Gen_Prim);
1830 while Present (Elmt) loop
1831 if Chars (Node (Elmt)) = Chars (S) then
1837 F1 := First_Formal (S);
1838 F2 := First_Formal (Node (Elmt));
1840 and then Present (F2)
1842 if Etype (F1) = Etype (F2)
1843 or else Etype (F2) = Gen_Par
1849 exit; -- not the right subprogram
1861 raise Program_Error;
1862 end Inherited_From_Formal;
1866 Remote : constant Boolean := Is_Remote_Call (N);
1869 Orig_Subp : Entity_Id := Empty;
1870 Param_Count : Natural := 0;
1871 Parent_Formal : Entity_Id;
1872 Parent_Subp : Entity_Id;
1876 Prev_Orig : Node_Id;
1877 -- Original node for an actual, which may have been rewritten. If the
1878 -- actual is a function call that has been transformed from a selected
1879 -- component, the original node is unanalyzed. Otherwise, it carries
1880 -- semantic information used to generate additional actuals.
1882 CW_Interface_Formals_Present : Boolean := False;
1884 -- Start of processing for Expand_Call
1887 -- Ignore if previous error
1889 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1893 -- Call using access to subprogram with explicit dereference
1895 if Nkind (Name (N)) = N_Explicit_Dereference then
1896 Subp := Etype (Name (N));
1897 Parent_Subp := Empty;
1899 -- Case of call to simple entry, where the Name is a selected component
1900 -- whose prefix is the task, and whose selector name is the entry name
1902 elsif Nkind (Name (N)) = N_Selected_Component then
1903 Subp := Entity (Selector_Name (Name (N)));
1904 Parent_Subp := Empty;
1906 -- Case of call to member of entry family, where Name is an indexed
1907 -- component, with the prefix being a selected component giving the
1908 -- task and entry family name, and the index being the entry index.
1910 elsif Nkind (Name (N)) = N_Indexed_Component then
1911 Subp := Entity (Selector_Name (Prefix (Name (N))));
1912 Parent_Subp := Empty;
1917 Subp := Entity (Name (N));
1918 Parent_Subp := Alias (Subp);
1920 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1921 -- if we can tell that the first parameter cannot possibly be null.
1922 -- This helps optimization and also generation of warnings.
1924 -- We do not do this if Raise_Exception_Always does not exist, which
1925 -- can happen in configurable run time profiles which provide only a
1926 -- Raise_Exception, which is in fact an unconditional raise anyway.
1928 if Is_RTE (Subp, RE_Raise_Exception)
1929 and then RTE_Available (RE_Raise_Exception_Always)
1932 FA : constant Node_Id := Original_Node (First_Actual (N));
1935 -- The case we catch is where the first argument is obtained
1936 -- using the Identity attribute (which must always be
1939 if Nkind (FA) = N_Attribute_Reference
1940 and then Attribute_Name (FA) = Name_Identity
1942 Subp := RTE (RE_Raise_Exception_Always);
1943 Set_Name (N, New_Occurrence_Of (Subp, Loc));
1948 if Ekind (Subp) = E_Entry then
1949 Parent_Subp := Empty;
1953 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1954 -- alternative in an asynchronous select or as an entry call in
1955 -- a conditional or timed select. Check whether the procedure call
1956 -- is a renaming of an entry and rewrite it as an entry call.
1958 if Ada_Version >= Ada_05
1959 and then Nkind (N) = N_Procedure_Call_Statement
1961 ((Nkind (Parent (N)) = N_Triggering_Alternative
1962 and then Triggering_Statement (Parent (N)) = N)
1964 (Nkind (Parent (N)) = N_Entry_Call_Alternative
1965 and then Entry_Call_Statement (Parent (N)) = N))
1969 Ren_Root : Entity_Id := Subp;
1972 -- This may be a chain of renamings, find the root
1974 if Present (Alias (Ren_Root)) then
1975 Ren_Root := Alias (Ren_Root);
1978 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
1979 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
1981 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
1983 Make_Entry_Call_Statement (Loc,
1985 New_Copy_Tree (Name (Ren_Decl)),
1986 Parameter_Associations =>
1987 New_Copy_List_Tree (Parameter_Associations (N))));
1995 -- First step, compute extra actuals, corresponding to any
1996 -- Extra_Formals present. Note that we do not access Extra_Formals
1997 -- directly, instead we simply note the presence of the extra
1998 -- formals as we process the regular formals and collect the
1999 -- corresponding actuals in Extra_Actuals.
2001 -- We also generate any required range checks for actuals as we go
2002 -- through the loop, since this is a convenient place to do this.
2004 Formal := First_Formal (Subp);
2005 Actual := First_Actual (N);
2007 while Present (Formal) loop
2009 -- Generate range check if required (not activated yet ???)
2011 -- if Do_Range_Check (Actual) then
2012 -- Set_Do_Range_Check (Actual, False);
2013 -- Generate_Range_Check
2014 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
2017 -- Prepare to examine current entry
2020 Prev_Orig := Original_Node (Prev);
2022 -- The original actual may have been a call written in prefix
2023 -- form, and rewritten before analysis.
2025 if not Analyzed (Prev_Orig)
2027 (Nkind (Actual) = N_Function_Call
2029 Nkind (Actual) = N_Identifier)
2034 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
2035 -- to expand it in a further round.
2037 CW_Interface_Formals_Present :=
2038 CW_Interface_Formals_Present
2040 (Ekind (Etype (Formal)) = E_Class_Wide_Type
2041 and then Is_Interface (Etype (Etype (Formal))))
2043 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
2044 and then Is_Interface (Directly_Designated_Type
2045 (Etype (Etype (Formal)))));
2047 -- Create possible extra actual for constrained case. Usually, the
2048 -- extra actual is of the form actual'constrained, but since this
2049 -- attribute is only available for unconstrained records, TRUE is
2050 -- expanded if the type of the formal happens to be constrained (for
2051 -- instance when this procedure is inherited from an unconstrained
2052 -- record to a constrained one) or if the actual has no discriminant
2053 -- (its type is constrained). An exception to this is the case of a
2054 -- private type without discriminants. In this case we pass FALSE
2055 -- because the object has underlying discriminants with defaults.
2057 if Present (Extra_Constrained (Formal)) then
2058 if Ekind (Etype (Prev)) in Private_Kind
2059 and then not Has_Discriminants (Base_Type (Etype (Prev)))
2062 New_Occurrence_Of (Standard_False, Loc),
2063 Extra_Constrained (Formal));
2065 elsif Is_Constrained (Etype (Formal))
2066 or else not Has_Discriminants (Etype (Prev))
2069 New_Occurrence_Of (Standard_True, Loc),
2070 Extra_Constrained (Formal));
2072 -- Do not produce extra actuals for Unchecked_Union parameters.
2073 -- Jump directly to the end of the loop.
2075 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
2076 goto Skip_Extra_Actual_Generation;
2079 -- If the actual is a type conversion, then the constrained
2080 -- test applies to the actual, not the target type.
2086 -- Test for unchecked conversions as well, which can occur
2087 -- as out parameter actuals on calls to stream procedures.
2090 while Nkind (Act_Prev) = N_Type_Conversion
2091 or else Nkind (Act_Prev) = N_Unchecked_Type_Conversion
2093 Act_Prev := Expression (Act_Prev);
2096 -- If the expression is a conversion of a dereference,
2097 -- this is internally generated code that manipulates
2098 -- addresses, e.g. when building interface tables. No
2099 -- check should occur in this case, and the discriminated
2100 -- object is not directly a hand.
2102 if not Comes_From_Source (Actual)
2103 and then Nkind (Actual) = N_Unchecked_Type_Conversion
2104 and then Nkind (Act_Prev) = N_Explicit_Dereference
2107 (New_Occurrence_Of (Standard_False, Loc),
2108 Extra_Constrained (Formal));
2112 (Make_Attribute_Reference (Sloc (Prev),
2114 Duplicate_Subexpr_No_Checks
2115 (Act_Prev, Name_Req => True),
2116 Attribute_Name => Name_Constrained),
2117 Extra_Constrained (Formal));
2123 -- Create possible extra actual for accessibility level
2125 if Present (Extra_Accessibility (Formal)) then
2127 -- Ada 2005 (AI-252): If the actual was rewritten as an Access
2128 -- attribute, then the original actual may be an aliased object
2129 -- occurring as the prefix in a call using "Object.Operation"
2130 -- notation. In that case we must pass the level of the object,
2131 -- so Prev_Orig is reset to Prev and the attribute will be
2132 -- processed by the code for Access attributes further below.
2134 if Prev_Orig /= Prev
2135 and then Nkind (Prev) = N_Attribute_Reference
2137 Get_Attribute_Id (Attribute_Name (Prev)) = Attribute_Access
2138 and then Is_Aliased_View (Prev_Orig)
2143 -- Ada 2005 (AI-251): Thunks must propagate the extra actuals
2144 -- of accessibility levels.
2146 if Ekind (Current_Scope) in Subprogram_Kind
2147 and then Is_Thunk (Current_Scope)
2150 Parm_Ent : Entity_Id;
2153 if Is_Controlling_Actual (Actual) then
2155 -- Find the corresponding actual of the thunk
2157 Parm_Ent := First_Entity (Current_Scope);
2158 for J in 2 .. Param_Count loop
2159 Next_Entity (Parm_Ent);
2162 else pragma Assert (Is_Entity_Name (Actual));
2163 Parm_Ent := Entity (Actual);
2167 (New_Occurrence_Of (Extra_Accessibility (Parm_Ent), Loc),
2168 Extra_Accessibility (Formal));
2171 elsif Is_Entity_Name (Prev_Orig) then
2173 -- When passing an access parameter, or a renaming of an access
2174 -- parameter, as the actual to another access parameter we need
2175 -- to pass along the actual's own access level parameter. This
2176 -- is done if we are within the scope of the formal access
2177 -- parameter (if this is an inlined body the extra formal is
2180 if (Is_Formal (Entity (Prev_Orig))
2182 (Present (Renamed_Object (Entity (Prev_Orig)))
2184 Is_Entity_Name (Renamed_Object (Entity (Prev_Orig)))
2187 (Entity (Renamed_Object (Entity (Prev_Orig))))))
2188 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
2189 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
2192 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
2195 pragma Assert (Present (Parm_Ent));
2197 if Present (Extra_Accessibility (Parm_Ent)) then
2200 (Extra_Accessibility (Parm_Ent), Loc),
2201 Extra_Accessibility (Formal));
2203 -- If the actual access parameter does not have an
2204 -- associated extra formal providing its scope level,
2205 -- then treat the actual as having library-level
2210 (Make_Integer_Literal (Loc,
2211 Intval => Scope_Depth (Standard_Standard)),
2212 Extra_Accessibility (Formal));
2216 -- The actual is a normal access value, so just pass the level
2217 -- of the actual's access type.
2221 (Make_Integer_Literal (Loc,
2222 Intval => Type_Access_Level (Etype (Prev_Orig))),
2223 Extra_Accessibility (Formal));
2226 -- All cases other than thunks
2229 case Nkind (Prev_Orig) is
2231 when N_Attribute_Reference =>
2232 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
2234 -- For X'Access, pass on the level of the prefix X
2236 when Attribute_Access =>
2238 Make_Integer_Literal (Loc,
2240 Object_Access_Level (Prefix (Prev_Orig))),
2241 Extra_Accessibility (Formal));
2243 -- Treat the unchecked attributes as library-level
2245 when Attribute_Unchecked_Access |
2246 Attribute_Unrestricted_Access =>
2248 Make_Integer_Literal (Loc,
2249 Intval => Scope_Depth (Standard_Standard)),
2250 Extra_Accessibility (Formal));
2252 -- No other cases of attributes returning access
2253 -- values that can be passed to access parameters
2256 raise Program_Error;
2260 -- For allocators we pass the level of the execution of
2261 -- the called subprogram, which is one greater than the
2262 -- current scope level.
2266 Make_Integer_Literal (Loc,
2267 Scope_Depth (Current_Scope) + 1),
2268 Extra_Accessibility (Formal));
2270 -- For other cases we simply pass the level of the
2271 -- actual's access type.
2275 Make_Integer_Literal (Loc,
2276 Intval => Type_Access_Level (Etype (Prev_Orig))),
2277 Extra_Accessibility (Formal));
2283 -- Perform the check of 4.6(49) that prevents a null value from being
2284 -- passed as an actual to an access parameter. Note that the check is
2285 -- elided in the common cases of passing an access attribute or
2286 -- access parameter as an actual. Also, we currently don't enforce
2287 -- this check for expander-generated actuals and when -gnatdj is set.
2289 if Ada_Version >= Ada_05 then
2291 -- Ada 2005 (AI-231): Check null-excluding access types
2293 if Is_Access_Type (Etype (Formal))
2294 and then Can_Never_Be_Null (Etype (Formal))
2295 and then Nkind (Prev) /= N_Raise_Constraint_Error
2296 and then (Known_Null (Prev)
2297 or else not Can_Never_Be_Null (Etype (Prev)))
2299 Install_Null_Excluding_Check (Prev);
2302 -- Ada_Version < Ada_05
2305 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
2306 or else Access_Checks_Suppressed (Subp)
2310 elsif Debug_Flag_J then
2313 elsif not Comes_From_Source (Prev) then
2316 elsif Is_Entity_Name (Prev)
2317 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
2321 elsif Nkind (Prev) = N_Allocator
2322 or else Nkind (Prev) = N_Attribute_Reference
2326 -- Suppress null checks when passing to access parameters of Java
2327 -- and CIL subprograms. (Should this be done for other foreign
2328 -- conventions as well ???)
2330 elsif Convention (Subp) = Convention_Java
2331 or else Convention (Subp) = Convention_CIL
2336 Install_Null_Excluding_Check (Prev);
2340 -- Perform appropriate validity checks on parameters that
2343 if Validity_Checks_On then
2344 if (Ekind (Formal) = E_In_Parameter
2345 and then Validity_Check_In_Params)
2347 (Ekind (Formal) = E_In_Out_Parameter
2348 and then Validity_Check_In_Out_Params)
2350 -- If the actual is an indexed component of a packed type (or
2351 -- is an indexed or selected component whose prefix recursively
2352 -- meets this condition), it has not been expanded yet. It will
2353 -- be copied in the validity code that follows, and has to be
2354 -- expanded appropriately, so reanalyze it.
2356 -- What we do is just to unset analyzed bits on prefixes till
2357 -- we reach something that does not have a prefix.
2364 while Nkind (Nod) = N_Indexed_Component
2366 Nkind (Nod) = N_Selected_Component
2368 Set_Analyzed (Nod, False);
2369 Nod := Prefix (Nod);
2373 Ensure_Valid (Actual);
2377 -- For IN OUT and OUT parameters, ensure that subscripts are valid
2378 -- since this is a left side reference. We only do this for calls
2379 -- from the source program since we assume that compiler generated
2380 -- calls explicitly generate any required checks. We also need it
2381 -- only if we are doing standard validity checks, since clearly it
2382 -- is not needed if validity checks are off, and in subscript
2383 -- validity checking mode, all indexed components are checked with
2384 -- a call directly from Expand_N_Indexed_Component.
2386 if Comes_From_Source (N)
2387 and then Ekind (Formal) /= E_In_Parameter
2388 and then Validity_Checks_On
2389 and then Validity_Check_Default
2390 and then not Validity_Check_Subscripts
2392 Check_Valid_Lvalue_Subscripts (Actual);
2395 -- Mark any scalar OUT parameter that is a simple variable as no
2396 -- longer known to be valid (unless the type is always valid). This
2397 -- reflects the fact that if an OUT parameter is never set in a
2398 -- procedure, then it can become invalid on the procedure return.
2400 if Ekind (Formal) = E_Out_Parameter
2401 and then Is_Entity_Name (Actual)
2402 and then Ekind (Entity (Actual)) = E_Variable
2403 and then not Is_Known_Valid (Etype (Actual))
2405 Set_Is_Known_Valid (Entity (Actual), False);
2408 -- For an OUT or IN OUT parameter, if the actual is an entity, then
2409 -- clear current values, since they can be clobbered. We are probably
2410 -- doing this in more places than we need to, but better safe than
2411 -- sorry when it comes to retaining bad current values!
2413 if Ekind (Formal) /= E_In_Parameter
2414 and then Is_Entity_Name (Actual)
2416 Kill_Current_Values (Entity (Actual));
2419 -- If the formal is class wide and the actual is an aggregate, force
2420 -- evaluation so that the back end who does not know about class-wide
2421 -- type, does not generate a temporary of the wrong size.
2423 if not Is_Class_Wide_Type (Etype (Formal)) then
2426 elsif Nkind (Actual) = N_Aggregate
2427 or else (Nkind (Actual) = N_Qualified_Expression
2428 and then Nkind (Expression (Actual)) = N_Aggregate)
2430 Force_Evaluation (Actual);
2433 -- In a remote call, if the formal is of a class-wide type, check
2434 -- that the actual meets the requirements described in E.4(18).
2436 if Remote and then Is_Class_Wide_Type (Etype (Formal)) then
2437 Insert_Action (Actual,
2438 Make_Transportable_Check (Loc,
2439 Duplicate_Subexpr_Move_Checks (Actual)));
2442 -- This label is required when skipping extra actual generation for
2443 -- Unchecked_Union parameters.
2445 <<Skip_Extra_Actual_Generation>>
2447 Param_Count := Param_Count + 1;
2448 Next_Actual (Actual);
2449 Next_Formal (Formal);
2452 -- If we are expanding a rhs of an assignment we need to check if tag
2453 -- propagation is needed. You might expect this processing to be in
2454 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
2455 -- assignment might be transformed to a declaration for an unconstrained
2456 -- value if the expression is classwide.
2458 if Nkind (N) = N_Function_Call
2459 and then Is_Tag_Indeterminate (N)
2460 and then Is_Entity_Name (Name (N))
2463 Ass : Node_Id := Empty;
2466 if Nkind (Parent (N)) = N_Assignment_Statement then
2469 elsif Nkind (Parent (N)) = N_Qualified_Expression
2470 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2472 Ass := Parent (Parent (N));
2474 elsif Nkind (Parent (N)) = N_Explicit_Dereference
2475 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2477 Ass := Parent (Parent (N));
2481 and then Is_Class_Wide_Type (Etype (Name (Ass)))
2483 if Is_Access_Type (Etype (N)) then
2484 if Designated_Type (Etype (N)) /=
2485 Root_Type (Etype (Name (Ass)))
2488 ("tag-indeterminate expression "
2489 & " must have designated type& (RM 5.2 (6))",
2490 N, Root_Type (Etype (Name (Ass))));
2492 Propagate_Tag (Name (Ass), N);
2495 elsif Etype (N) /= Root_Type (Etype (Name (Ass))) then
2497 ("tag-indeterminate expression must have type&"
2498 & "(RM 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
2501 Propagate_Tag (Name (Ass), N);
2504 -- The call will be rewritten as a dispatching call, and
2505 -- expanded as such.
2512 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
2513 -- it to point to the correct secondary virtual table
2515 if (Nkind (N) = N_Function_Call
2516 or else Nkind (N) = N_Procedure_Call_Statement)
2517 and then CW_Interface_Formals_Present
2519 Expand_Interface_Actuals (N);
2522 -- Deals with Dispatch_Call if we still have a call, before expanding
2523 -- extra actuals since this will be done on the re-analysis of the
2524 -- dispatching call. Note that we do not try to shorten the actual
2525 -- list for a dispatching call, it would not make sense to do so.
2526 -- Expansion of dispatching calls is suppressed when VM_Target, because
2527 -- the VM back-ends directly handle the generation of dispatching
2528 -- calls and would have to undo any expansion to an indirect call.
2530 if (Nkind (N) = N_Function_Call
2531 or else Nkind (N) = N_Procedure_Call_Statement)
2532 and then Present (Controlling_Argument (N))
2533 and then VM_Target = No_VM
2535 Expand_Dispatching_Call (N);
2537 -- The following return is worrisome. Is it really OK to
2538 -- skip all remaining processing in this procedure ???
2542 -- Similarly, expand calls to RCI subprograms on which pragma
2543 -- All_Calls_Remote applies. The rewriting will be reanalyzed
2544 -- later. Do this only when the call comes from source since we do
2545 -- not want such a rewritting to occur in expanded code.
2547 elsif Is_All_Remote_Call (N) then
2548 Expand_All_Calls_Remote_Subprogram_Call (N);
2550 -- Similarly, do not add extra actuals for an entry call whose entity
2551 -- is a protected procedure, or for an internal protected subprogram
2552 -- call, because it will be rewritten as a protected subprogram call
2553 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
2555 elsif Is_Protected_Type (Scope (Subp))
2556 and then (Ekind (Subp) = E_Procedure
2557 or else Ekind (Subp) = E_Function)
2561 -- During that loop we gathered the extra actuals (the ones that
2562 -- correspond to Extra_Formals), so now they can be appended.
2565 while Is_Non_Empty_List (Extra_Actuals) loop
2566 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
2570 -- At this point we have all the actuals, so this is the point at
2571 -- which the various expansion activities for actuals is carried out.
2573 Expand_Actuals (N, Subp);
2575 -- If the subprogram is a renaming, or if it is inherited, replace it
2576 -- in the call with the name of the actual subprogram being called.
2577 -- If this is a dispatching call, the run-time decides what to call.
2578 -- The Alias attribute does not apply to entries.
2580 if Nkind (N) /= N_Entry_Call_Statement
2581 and then No (Controlling_Argument (N))
2582 and then Present (Parent_Subp)
2584 if Present (Inherited_From_Formal (Subp)) then
2585 Parent_Subp := Inherited_From_Formal (Subp);
2587 while Present (Alias (Parent_Subp)) loop
2588 Parent_Subp := Alias (Parent_Subp);
2592 -- The below setting of Entity is suspect, see F109-018 discussion???
2594 Set_Entity (Name (N), Parent_Subp);
2596 if Is_Abstract_Subprogram (Parent_Subp)
2597 and then not In_Instance
2600 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2603 -- Add an explicit conversion for parameter of the derived type.
2604 -- This is only done for scalar and access in-parameters. Others
2605 -- have been expanded in expand_actuals.
2607 Formal := First_Formal (Subp);
2608 Parent_Formal := First_Formal (Parent_Subp);
2609 Actual := First_Actual (N);
2611 -- It is not clear that conversion is needed for intrinsic
2612 -- subprograms, but it certainly is for those that are user-
2613 -- defined, and that can be inherited on derivation, namely
2614 -- unchecked conversion and deallocation.
2615 -- General case needs study ???
2617 if not Is_Intrinsic_Subprogram (Parent_Subp)
2618 or else Is_Generic_Instance (Parent_Subp)
2620 while Present (Formal) loop
2621 if Etype (Formal) /= Etype (Parent_Formal)
2622 and then Is_Scalar_Type (Etype (Formal))
2623 and then Ekind (Formal) = E_In_Parameter
2625 not Subtypes_Statically_Match
2626 (Etype (Parent_Formal), Etype (Actual))
2627 and then not Raises_Constraint_Error (Actual)
2630 OK_Convert_To (Etype (Parent_Formal),
2631 Relocate_Node (Actual)));
2634 Resolve (Actual, Etype (Parent_Formal));
2635 Enable_Range_Check (Actual);
2637 elsif Is_Access_Type (Etype (Formal))
2638 and then Base_Type (Etype (Parent_Formal)) /=
2639 Base_Type (Etype (Actual))
2641 if Ekind (Formal) /= E_In_Parameter then
2643 Convert_To (Etype (Parent_Formal),
2644 Relocate_Node (Actual)));
2647 Resolve (Actual, Etype (Parent_Formal));
2650 Ekind (Etype (Parent_Formal)) = E_Anonymous_Access_Type
2651 and then Designated_Type (Etype (Parent_Formal))
2653 Designated_Type (Etype (Actual))
2654 and then not Is_Controlling_Formal (Formal)
2656 -- This unchecked conversion is not necessary unless
2657 -- inlining is enabled, because in that case the type
2658 -- mismatch may become visible in the body about to be
2662 Unchecked_Convert_To (Etype (Parent_Formal),
2663 Relocate_Node (Actual)));
2666 Resolve (Actual, Etype (Parent_Formal));
2670 Next_Formal (Formal);
2671 Next_Formal (Parent_Formal);
2672 Next_Actual (Actual);
2677 Subp := Parent_Subp;
2680 -- Check for violation of No_Abort_Statements
2682 if Is_RTE (Subp, RE_Abort_Task) then
2683 Check_Restriction (No_Abort_Statements, N);
2685 -- Check for violation of No_Dynamic_Attachment
2687 elsif RTU_Loaded (Ada_Interrupts)
2688 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2689 Is_RTE (Subp, RE_Is_Attached) or else
2690 Is_RTE (Subp, RE_Current_Handler) or else
2691 Is_RTE (Subp, RE_Attach_Handler) or else
2692 Is_RTE (Subp, RE_Exchange_Handler) or else
2693 Is_RTE (Subp, RE_Detach_Handler) or else
2694 Is_RTE (Subp, RE_Reference))
2696 Check_Restriction (No_Dynamic_Attachment, N);
2699 -- Deal with case where call is an explicit dereference
2701 if Nkind (Name (N)) = N_Explicit_Dereference then
2703 -- Handle case of access to protected subprogram type
2705 if Is_Access_Protected_Subprogram_Type
2706 (Base_Type (Etype (Prefix (Name (N)))))
2708 -- If this is a call through an access to protected operation,
2709 -- the prefix has the form (object'address, operation'access).
2710 -- Rewrite as a for other protected calls: the object is the
2711 -- first parameter of the list of actuals.
2718 Ptr : constant Node_Id := Prefix (Name (N));
2720 T : constant Entity_Id :=
2721 Equivalent_Type (Base_Type (Etype (Ptr)));
2723 D_T : constant Entity_Id :=
2724 Designated_Type (Base_Type (Etype (Ptr)));
2728 Make_Selected_Component (Loc,
2729 Prefix => Unchecked_Convert_To (T, Ptr),
2731 New_Occurrence_Of (First_Entity (T), Loc));
2734 Make_Selected_Component (Loc,
2735 Prefix => Unchecked_Convert_To (T, Ptr),
2737 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2740 Make_Explicit_Dereference (Loc,
2743 if Present (Parameter_Associations (N)) then
2744 Parm := Parameter_Associations (N);
2749 Prepend (Obj, Parm);
2751 if Etype (D_T) = Standard_Void_Type then
2753 Make_Procedure_Call_Statement (Loc,
2755 Parameter_Associations => Parm);
2758 Make_Function_Call (Loc,
2760 Parameter_Associations => Parm);
2763 Set_First_Named_Actual (Call, First_Named_Actual (N));
2764 Set_Etype (Call, Etype (D_T));
2766 -- We do not re-analyze the call to avoid infinite recursion.
2767 -- We analyze separately the prefix and the object, and set
2768 -- the checks on the prefix that would otherwise be emitted
2769 -- when resolving a call.
2773 Apply_Access_Check (Nam);
2780 -- If this is a call to an intrinsic subprogram, then perform the
2781 -- appropriate expansion to the corresponding tree node and we
2782 -- are all done (since after that the call is gone!)
2784 -- In the case where the intrinsic is to be processed by the back end,
2785 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2786 -- since the idea in this case is to pass the call unchanged.
2787 -- If the intrinsic is an inherited unchecked conversion, and the
2788 -- derived type is the target type of the conversion, we must retain
2789 -- it as the return type of the expression. Otherwise the expansion
2790 -- below, which uses the parent operation, will yield the wrong type.
2792 if Is_Intrinsic_Subprogram (Subp) then
2793 Expand_Intrinsic_Call (N, Subp);
2795 if Nkind (N) = N_Unchecked_Type_Conversion
2796 and then Parent_Subp /= Orig_Subp
2797 and then Etype (Parent_Subp) /= Etype (Orig_Subp)
2799 Set_Etype (N, Etype (Orig_Subp));
2805 if Ekind (Subp) = E_Function
2806 or else Ekind (Subp) = E_Procedure
2808 if Is_Inlined (Subp) then
2810 Inlined_Subprogram : declare
2812 Must_Inline : Boolean := False;
2813 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2814 Scop : constant Entity_Id := Scope (Subp);
2816 function In_Unfrozen_Instance return Boolean;
2817 -- If the subprogram comes from an instance in the same
2818 -- unit, and the instance is not yet frozen, inlining might
2819 -- trigger order-of-elaboration problems in gigi.
2821 --------------------------
2822 -- In_Unfrozen_Instance --
2823 --------------------------
2825 function In_Unfrozen_Instance return Boolean is
2831 and then S /= Standard_Standard
2833 if Is_Generic_Instance (S)
2834 and then Present (Freeze_Node (S))
2835 and then not Analyzed (Freeze_Node (S))
2844 end In_Unfrozen_Instance;
2846 -- Start of processing for Inlined_Subprogram
2849 -- Verify that the body to inline has already been seen, and
2850 -- that if the body is in the current unit the inlining does
2851 -- not occur earlier. This avoids order-of-elaboration problems
2854 -- This should be documented in sinfo/einfo ???
2857 or else Nkind (Spec) /= N_Subprogram_Declaration
2858 or else No (Body_To_Inline (Spec))
2860 Must_Inline := False;
2862 -- If this an inherited function that returns a private
2863 -- type, do not inline if the full view is an unconstrained
2864 -- array, because such calls cannot be inlined.
2866 elsif Present (Orig_Subp)
2867 and then Is_Array_Type (Etype (Orig_Subp))
2868 and then not Is_Constrained (Etype (Orig_Subp))
2870 Must_Inline := False;
2872 elsif In_Unfrozen_Instance then
2873 Must_Inline := False;
2876 Bod := Body_To_Inline (Spec);
2878 if (In_Extended_Main_Code_Unit (N)
2879 or else In_Extended_Main_Code_Unit (Parent (N))
2880 or else Is_Always_Inlined (Subp))
2881 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
2883 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
2885 Must_Inline := True;
2887 -- If we are compiling a package body that is not the main
2888 -- unit, it must be for inlining/instantiation purposes,
2889 -- in which case we inline the call to insure that the same
2890 -- temporaries are generated when compiling the body by
2891 -- itself. Otherwise link errors can occur.
2893 -- If the function being called is itself in the main unit,
2894 -- we cannot inline, because there is a risk of double
2895 -- elaboration and/or circularity: the inlining can make
2896 -- visible a private entity in the body of the main unit,
2897 -- that gigi will see before its sees its proper definition.
2899 elsif not (In_Extended_Main_Code_Unit (N))
2900 and then In_Package_Body
2902 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
2907 Expand_Inlined_Call (N, Subp, Orig_Subp);
2910 -- Let the back end handle it
2912 Add_Inlined_Body (Subp);
2914 if Front_End_Inlining
2915 and then Nkind (Spec) = N_Subprogram_Declaration
2916 and then (In_Extended_Main_Code_Unit (N))
2917 and then No (Body_To_Inline (Spec))
2918 and then not Has_Completion (Subp)
2919 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
2922 ("cannot inline& (body not seen yet)?",
2926 end Inlined_Subprogram;
2930 -- Check for a protected subprogram. This is either an intra-object
2931 -- call, or a protected function call. Protected procedure calls are
2932 -- rewritten as entry calls and handled accordingly.
2934 -- In Ada 2005, this may be an indirect call to an access parameter
2935 -- that is an access_to_subprogram. In that case the anonymous type
2936 -- has a scope that is a protected operation, but the call is a
2939 Scop := Scope (Subp);
2941 if Nkind (N) /= N_Entry_Call_Statement
2942 and then Is_Protected_Type (Scop)
2943 and then Ekind (Subp) /= E_Subprogram_Type
2945 -- If the call is an internal one, it is rewritten as a call to
2946 -- to the corresponding unprotected subprogram.
2948 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2951 -- Functions returning controlled objects need special attention
2952 -- If the return type is limited the context is an initialization
2953 -- and different processing applies.
2955 if Controlled_Type (Etype (Subp))
2956 and then not Is_Inherently_Limited_Type (Etype (Subp))
2957 and then not Is_Limited_Interface (Etype (Subp))
2959 Expand_Ctrl_Function_Call (N);
2962 -- Test for First_Optional_Parameter, and if so, truncate parameter
2963 -- list if there are optional parameters at the trailing end.
2964 -- Note we never delete procedures for call via a pointer.
2966 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2967 and then Present (First_Optional_Parameter (Subp))
2970 Last_Keep_Arg : Node_Id;
2973 -- Last_Keep_Arg will hold the last actual that should be
2974 -- retained. If it remains empty at the end, it means that
2975 -- all parameters are optional.
2977 Last_Keep_Arg := Empty;
2979 -- Find first optional parameter, must be present since we
2980 -- checked the validity of the parameter before setting it.
2982 Formal := First_Formal (Subp);
2983 Actual := First_Actual (N);
2984 while Formal /= First_Optional_Parameter (Subp) loop
2985 Last_Keep_Arg := Actual;
2986 Next_Formal (Formal);
2987 Next_Actual (Actual);
2990 -- We have Formal and Actual pointing to the first potentially
2991 -- droppable argument. We can drop all the trailing arguments
2992 -- whose actual matches the default. Note that we know that all
2993 -- remaining formals have defaults, because we checked that this
2994 -- requirement was met before setting First_Optional_Parameter.
2996 -- We use Fully_Conformant_Expressions to check for identity
2997 -- between formals and actuals, which may miss some cases, but
2998 -- on the other hand, this is only an optimization (if we fail
2999 -- to truncate a parameter it does not affect functionality).
3000 -- So if the default is 3 and the actual is 1+2, we consider
3001 -- them unequal, which hardly seems worrisome.
3003 while Present (Formal) loop
3004 if not Fully_Conformant_Expressions
3005 (Actual, Default_Value (Formal))
3007 Last_Keep_Arg := Actual;
3010 Next_Formal (Formal);
3011 Next_Actual (Actual);
3014 -- If no arguments, delete entire list, this is the easy case
3016 if No (Last_Keep_Arg) then
3017 while Is_Non_Empty_List (Parameter_Associations (N)) loop
3018 Delete_Tree (Remove_Head (Parameter_Associations (N)));
3021 Set_Parameter_Associations (N, No_List);
3022 Set_First_Named_Actual (N, Empty);
3024 -- Case where at the last retained argument is positional. This
3025 -- is also an easy case, since the retained arguments are already
3026 -- in the right form, and we don't need to worry about the order
3027 -- of arguments that get eliminated.
3029 elsif Is_List_Member (Last_Keep_Arg) then
3030 while Present (Next (Last_Keep_Arg)) loop
3031 Delete_Tree (Remove_Next (Last_Keep_Arg));
3034 Set_First_Named_Actual (N, Empty);
3036 -- This is the annoying case where the last retained argument
3037 -- is a named parameter. Since the original arguments are not
3038 -- in declaration order, we may have to delete some fairly
3039 -- random collection of arguments.
3047 -- First step, remove all the named parameters from the
3048 -- list (they are still chained using First_Named_Actual
3049 -- and Next_Named_Actual, so we have not lost them!)
3051 Temp := First (Parameter_Associations (N));
3053 -- Case of all parameters named, remove them all
3055 if Nkind (Temp) = N_Parameter_Association then
3056 while Is_Non_Empty_List (Parameter_Associations (N)) loop
3057 Temp := Remove_Head (Parameter_Associations (N));
3060 -- Case of mixed positional/named, remove named parameters
3063 while Nkind (Next (Temp)) /= N_Parameter_Association loop
3067 while Present (Next (Temp)) loop
3068 Remove (Next (Temp));
3072 -- Now we loop through the named parameters, till we get
3073 -- to the last one to be retained, adding them to the list.
3074 -- Note that the Next_Named_Actual list does not need to be
3075 -- touched since we are only reordering them on the actual
3076 -- parameter association list.
3078 Passoc := Parent (First_Named_Actual (N));
3080 Temp := Relocate_Node (Passoc);
3082 (Parameter_Associations (N), Temp);
3084 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
3085 Passoc := Parent (Next_Named_Actual (Passoc));
3088 Set_Next_Named_Actual (Temp, Empty);
3091 Temp := Next_Named_Actual (Passoc);
3092 exit when No (Temp);
3093 Set_Next_Named_Actual
3094 (Passoc, Next_Named_Actual (Parent (Temp)));
3102 -- Special processing for Ada 2005 AI-329, which requires a call to
3103 -- Raise_Exception to raise Constraint_Error if the Exception_Id is
3104 -- null. Note that we never need to do this in GNAT mode, or if the
3105 -- parameter to Raise_Exception is a use of Identity, since in these
3106 -- cases we know that the parameter is never null.
3108 -- Note: We must check that the node has not been inlined. This is
3109 -- required because under zfp the Raise_Exception subprogram has the
3110 -- pragma inline_always (and hence the call has been expanded above
3111 -- into a block containing the code of the subprogram).
3113 if Ada_Version >= Ada_05
3114 and then not GNAT_Mode
3115 and then Is_RTE (Subp, RE_Raise_Exception)
3116 and then Nkind (N) = N_Procedure_Call_Statement
3117 and then (Nkind (First_Actual (N)) /= N_Attribute_Reference
3118 or else Attribute_Name (First_Actual (N)) /= Name_Identity)
3121 RCE : constant Node_Id :=
3122 Make_Raise_Constraint_Error (Loc,
3123 Reason => CE_Null_Exception_Id);
3125 Insert_After (N, RCE);
3131 --------------------------
3132 -- Expand_Inlined_Call --
3133 --------------------------
3135 procedure Expand_Inlined_Call
3138 Orig_Subp : Entity_Id)
3140 Loc : constant Source_Ptr := Sloc (N);
3141 Is_Predef : constant Boolean :=
3142 Is_Predefined_File_Name
3143 (Unit_File_Name (Get_Source_Unit (Subp)));
3144 Orig_Bod : constant Node_Id :=
3145 Body_To_Inline (Unit_Declaration_Node (Subp));
3150 Decls : constant List_Id := New_List;
3151 Exit_Lab : Entity_Id := Empty;
3158 Ret_Type : Entity_Id;
3162 Temp_Typ : Entity_Id;
3164 Is_Unc : constant Boolean :=
3165 Is_Array_Type (Etype (Subp))
3166 and then not Is_Constrained (Etype (Subp));
3167 -- If the type returned by the function is unconstrained and the
3168 -- call can be inlined, special processing is required.
3170 function Is_Null_Procedure return Boolean;
3171 -- Predicate to recognize stubbed procedures and null procedures, for
3172 -- which there is no need for the full inlining mechanism.
3174 procedure Make_Exit_Label;
3175 -- Build declaration for exit label to be used in Return statements
3177 function Process_Formals (N : Node_Id) return Traverse_Result;
3178 -- Replace occurrence of a formal with the corresponding actual, or
3179 -- the thunk generated for it.
3181 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
3182 -- If the call being expanded is that of an internal subprogram,
3183 -- set the sloc of the generated block to that of the call itself,
3184 -- so that the expansion is skipped by the -next- command in gdb.
3185 -- Same processing for a subprogram in a predefined file, e.g.
3186 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
3187 -- to simplify our own development.
3189 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
3190 -- If the function body is a single expression, replace call with
3191 -- expression, else insert block appropriately.
3193 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
3194 -- If procedure body has no local variables, inline body without
3195 -- creating block, otherwise rewrite call with block.
3197 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
3198 -- Determine whether a formal parameter is used only once in Orig_Bod
3200 -----------------------
3201 -- Is_Null_Procedure --
3202 -----------------------
3204 function Is_Null_Procedure return Boolean is
3205 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3208 if Ekind (Subp) /= E_Procedure then
3211 elsif Nkind (Orig_Bod) /= N_Subprogram_Body then
3214 -- Check if this is an Ada 2005 null procedure
3216 elsif Nkind (Decl) = N_Subprogram_Declaration
3217 and then Null_Present (Specification (Decl))
3221 -- Check if the body contains only a null statement, followed by the
3222 -- return statement added during expansion.
3226 Stat : constant Node_Id :=
3228 (Statements (Handled_Statement_Sequence (Orig_Bod)));
3230 Stat2 : constant Node_Id := Next (Stat);
3234 Nkind (Stat) = N_Null_Statement
3238 (Nkind (Stat2) = N_Simple_Return_Statement
3239 and then No (Next (Stat2))));
3242 end Is_Null_Procedure;
3244 ---------------------
3245 -- Make_Exit_Label --
3246 ---------------------
3248 procedure Make_Exit_Label is
3250 -- Create exit label for subprogram if one does not exist yet
3252 if No (Exit_Lab) then
3254 Make_Identifier (Loc,
3255 Chars => New_Internal_Name ('L'));
3257 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3258 Exit_Lab := Make_Label (Loc, Lab_Id);
3261 Make_Implicit_Label_Declaration (Loc,
3262 Defining_Identifier => Entity (Lab_Id),
3263 Label_Construct => Exit_Lab);
3265 end Make_Exit_Label;
3267 ---------------------
3268 -- Process_Formals --
3269 ---------------------
3271 function Process_Formals (N : Node_Id) return Traverse_Result is
3277 if Is_Entity_Name (N)
3278 and then Present (Entity (N))
3283 and then Scope (E) = Subp
3285 A := Renamed_Object (E);
3287 -- Rewrite the occurrence of the formal into an occurrence of
3288 -- the actual. Also establish visibility on the proper view of
3289 -- the actual's subtype for the body's context (if the actual's
3290 -- subtype is private at the call point but its full view is
3291 -- visible to the body, then the inlined tree here must be
3292 -- analyzed with the full view).
3294 if Is_Entity_Name (A) then
3295 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
3296 Check_Private_View (N);
3298 elsif Nkind (A) = N_Defining_Identifier then
3299 Rewrite (N, New_Occurrence_Of (A, Loc));
3300 Check_Private_View (N);
3305 Rewrite (N, New_Copy (A));
3311 elsif Nkind (N) = N_Simple_Return_Statement then
3312 if No (Expression (N)) then
3315 Make_Goto_Statement (Loc,
3316 Name => New_Copy (Lab_Id)));
3319 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
3320 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
3322 -- Function body is a single expression. No need for
3328 Num_Ret := Num_Ret + 1;
3332 -- Because of the presence of private types, the views of the
3333 -- expression and the context may be different, so place an
3334 -- unchecked conversion to the context type to avoid spurious
3335 -- errors, eg. when the expression is a numeric literal and
3336 -- the context is private. If the expression is an aggregate,
3337 -- use a qualified expression, because an aggregate is not a
3338 -- legal argument of a conversion.
3340 if Nkind (Expression (N)) = N_Aggregate
3341 or else Nkind (Expression (N)) = N_Null
3344 Make_Qualified_Expression (Sloc (N),
3345 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
3346 Expression => Relocate_Node (Expression (N)));
3349 Unchecked_Convert_To
3350 (Ret_Type, Relocate_Node (Expression (N)));
3353 if Nkind (Targ) = N_Defining_Identifier then
3355 Make_Assignment_Statement (Loc,
3356 Name => New_Occurrence_Of (Targ, Loc),
3357 Expression => Ret));
3360 Make_Assignment_Statement (Loc,
3361 Name => New_Copy (Targ),
3362 Expression => Ret));
3365 Set_Assignment_OK (Name (N));
3367 if Present (Exit_Lab) then
3369 Make_Goto_Statement (Loc,
3370 Name => New_Copy (Lab_Id)));
3376 -- Remove pragma Unreferenced since it may refer to formals that
3377 -- are not visible in the inlined body, and in any case we will
3378 -- not be posting warnings on the inlined body so it is unneeded.
3380 elsif Nkind (N) = N_Pragma
3381 and then Chars (N) = Name_Unreferenced
3383 Rewrite (N, Make_Null_Statement (Sloc (N)));
3389 end Process_Formals;
3391 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
3397 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
3399 if not Debug_Generated_Code then
3400 Set_Sloc (Nod, Sloc (N));
3401 Set_Comes_From_Source (Nod, False);
3407 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
3409 ---------------------------
3410 -- Rewrite_Function_Call --
3411 ---------------------------
3413 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
3414 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3415 Fst : constant Node_Id := First (Statements (HSS));
3418 -- Optimize simple case: function body is a single return statement,
3419 -- which has been expanded into an assignment.
3421 if Is_Empty_List (Declarations (Blk))
3422 and then Nkind (Fst) = N_Assignment_Statement
3423 and then No (Next (Fst))
3426 -- The function call may have been rewritten as the temporary
3427 -- that holds the result of the call, in which case remove the
3428 -- now useless declaration.
3430 if Nkind (N) = N_Identifier
3431 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3433 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
3436 Rewrite (N, Expression (Fst));
3438 elsif Nkind (N) = N_Identifier
3439 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
3441 -- The block assigns the result of the call to the temporary
3443 Insert_After (Parent (Entity (N)), Blk);
3445 elsif Nkind (Parent (N)) = N_Assignment_Statement
3447 (Is_Entity_Name (Name (Parent (N)))
3449 (Nkind (Name (Parent (N))) = N_Explicit_Dereference
3450 and then Is_Entity_Name (Prefix (Name (Parent (N))))))
3452 -- Replace assignment with the block
3455 Original_Assignment : constant Node_Id := Parent (N);
3458 -- Preserve the original assignment node to keep the complete
3459 -- assignment subtree consistent enough for Analyze_Assignment
3460 -- to proceed (specifically, the original Lhs node must still
3461 -- have an assignment statement as its parent).
3463 -- We cannot rely on Original_Node to go back from the block
3464 -- node to the assignment node, because the assignment might
3465 -- already be a rewrite substitution.
3467 Discard_Node (Relocate_Node (Original_Assignment));
3468 Rewrite (Original_Assignment, Blk);
3471 elsif Nkind (Parent (N)) = N_Object_Declaration then
3472 Set_Expression (Parent (N), Empty);
3473 Insert_After (Parent (N), Blk);
3476 Insert_Before (Parent (N), Blk);
3478 end Rewrite_Function_Call;
3480 ----------------------------
3481 -- Rewrite_Procedure_Call --
3482 ----------------------------
3484 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
3485 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
3487 -- If there is a transient scope for N, this will be the scope of the
3488 -- actions for N, and the statements in Blk need to be within this
3489 -- scope. For example, they need to have visibility on the constant
3490 -- declarations created for the formals.
3492 -- If N needs no transient scope, and if there are no declarations in
3493 -- the inlined body, we can do a little optimization and insert the
3494 -- statements for the body directly after N, and rewrite N to a
3495 -- null statement, instead of rewriting N into a full-blown block
3498 if not Scope_Is_Transient
3499 and then Is_Empty_List (Declarations (Blk))
3501 Insert_List_After (N, Statements (HSS));
3502 Rewrite (N, Make_Null_Statement (Loc));
3506 end Rewrite_Procedure_Call;
3508 -------------------------
3509 -- Formal_Is_Used_Once --
3510 -------------------------
3512 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
3513 Use_Counter : Int := 0;
3515 function Count_Uses (N : Node_Id) return Traverse_Result;
3516 -- Traverse the tree and count the uses of the formal parameter.
3517 -- In this case, for optimization purposes, we do not need to
3518 -- continue the traversal once more than one use is encountered.
3524 function Count_Uses (N : Node_Id) return Traverse_Result is
3526 -- The original node is an identifier
3528 if Nkind (N) = N_Identifier
3529 and then Present (Entity (N))
3531 -- Original node's entity points to the one in the copied body
3533 and then Nkind (Entity (N)) = N_Identifier
3534 and then Present (Entity (Entity (N)))
3536 -- The entity of the copied node is the formal parameter
3538 and then Entity (Entity (N)) = Formal
3540 Use_Counter := Use_Counter + 1;
3542 if Use_Counter > 1 then
3544 -- Denote more than one use and abandon the traversal
3555 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
3557 -- Start of processing for Formal_Is_Used_Once
3560 Count_Formal_Uses (Orig_Bod);
3561 return Use_Counter = 1;
3562 end Formal_Is_Used_Once;
3564 -- Start of processing for Expand_Inlined_Call
3567 -- Check for special case of To_Address call, and if so, just do an
3568 -- unchecked conversion instead of expanding the call. Not only is this
3569 -- more efficient, but it also avoids problem with order of elaboration
3570 -- when address clauses are inlined (address expression elaborated at
3573 if Subp = RTE (RE_To_Address) then
3575 Unchecked_Convert_To
3577 Relocate_Node (First_Actual (N))));
3580 elsif Is_Null_Procedure then
3581 Rewrite (N, Make_Null_Statement (Loc));
3585 -- Check for an illegal attempt to inline a recursive procedure. If the
3586 -- subprogram has parameters this is detected when trying to supply a
3587 -- binding for parameters that already have one. For parameterless
3588 -- subprograms this must be done explicitly.
3590 if In_Open_Scopes (Subp) then
3591 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
3592 Set_Is_Inlined (Subp, False);
3596 if Nkind (Orig_Bod) = N_Defining_Identifier
3597 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
3599 -- Subprogram is a renaming_as_body. Calls appearing after the
3600 -- renaming can be replaced with calls to the renamed entity
3601 -- directly, because the subprograms are subtype conformant. If
3602 -- the renamed subprogram is an inherited operation, we must redo
3603 -- the expansion because implicit conversions may be needed.
3605 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
3607 if Present (Alias (Orig_Bod)) then
3614 -- Use generic machinery to copy body of inlined subprogram, as if it
3615 -- were an instantiation, resetting source locations appropriately, so
3616 -- that nested inlined calls appear in the main unit.
3618 Save_Env (Subp, Empty);
3619 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
3621 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
3623 Make_Block_Statement (Loc,
3624 Declarations => Declarations (Bod),
3625 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
3627 if No (Declarations (Bod)) then
3628 Set_Declarations (Blk, New_List);
3631 -- For the unconstrained case, capture the name of the local
3632 -- variable that holds the result. This must be the first declaration
3633 -- in the block, because its bounds cannot depend on local variables.
3634 -- Otherwise there is no way to declare the result outside of the
3635 -- block. Needless to say, in general the bounds will depend on the
3636 -- actuals in the call.
3639 Targ1 := Defining_Identifier (First (Declarations (Blk)));
3642 -- If this is a derived function, establish the proper return type
3644 if Present (Orig_Subp)
3645 and then Orig_Subp /= Subp
3647 Ret_Type := Etype (Orig_Subp);
3649 Ret_Type := Etype (Subp);
3652 -- Create temporaries for the actuals that are expressions, or that
3653 -- are scalars and require copying to preserve semantics.
3655 F := First_Formal (Subp);
3656 A := First_Actual (N);
3657 while Present (F) loop
3658 if Present (Renamed_Object (F)) then
3659 Error_Msg_N ("cannot inline call to recursive subprogram", N);
3663 -- If the argument may be a controlling argument in a call within
3664 -- the inlined body, we must preserve its classwide nature to insure
3665 -- that dynamic dispatching take place subsequently. If the formal
3666 -- has a constraint it must be preserved to retain the semantics of
3669 if Is_Class_Wide_Type (Etype (F))
3670 or else (Is_Access_Type (Etype (F))
3672 Is_Class_Wide_Type (Designated_Type (Etype (F))))
3674 Temp_Typ := Etype (F);
3676 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
3677 and then Etype (F) /= Base_Type (Etype (F))
3679 Temp_Typ := Etype (F);
3682 Temp_Typ := Etype (A);
3685 -- If the actual is a simple name or a literal, no need to
3686 -- create a temporary, object can be used directly.
3688 -- If the actual is a literal and the formal has its address taken,
3689 -- we cannot pass the literal itself as an argument, so its value
3690 -- must be captured in a temporary.
3692 if (Is_Entity_Name (A)
3694 (not Is_Scalar_Type (Etype (A))
3695 or else Ekind (Entity (A)) = E_Enumeration_Literal))
3697 -- When the actual is an identifier and the corresponding formal
3698 -- is used only once in the original body, the formal can be
3699 -- substituted directly with the actual parameter.
3701 or else (Nkind (A) = N_Identifier
3702 and then Formal_Is_Used_Once (F))
3705 ((Nkind (A) = N_Real_Literal or else
3706 Nkind (A) = N_Integer_Literal or else
3707 Nkind (A) = N_Character_Literal)
3708 and then not Address_Taken (F))
3710 if Etype (F) /= Etype (A) then
3712 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
3714 Set_Renamed_Object (F, A);
3719 Make_Defining_Identifier (Loc,
3720 Chars => New_Internal_Name ('C'));
3722 -- If the actual for an in/in-out parameter is a view conversion,
3723 -- make it into an unchecked conversion, given that an untagged
3724 -- type conversion is not a proper object for a renaming.
3726 -- In-out conversions that involve real conversions have already
3727 -- been transformed in Expand_Actuals.
3729 if Nkind (A) = N_Type_Conversion
3730 and then Ekind (F) /= E_In_Parameter
3733 Make_Unchecked_Type_Conversion (Loc,
3734 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
3735 Expression => Relocate_Node (Expression (A)));
3737 elsif Etype (F) /= Etype (A) then
3738 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3739 Temp_Typ := Etype (F);
3742 New_A := Relocate_Node (A);
3745 Set_Sloc (New_A, Sloc (N));
3747 -- If the actual has a by-reference type, it cannot be copied, so
3748 -- its value is captured in a renaming declaration. Otherwise
3749 -- declare a local constant initialized with the actual.
3751 if Ekind (F) = E_In_Parameter
3752 and then not Is_Limited_Type (Etype (A))
3753 and then not Is_Tagged_Type (Etype (A))
3756 Make_Object_Declaration (Loc,
3757 Defining_Identifier => Temp,
3758 Constant_Present => True,
3759 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3760 Expression => New_A);
3763 Make_Object_Renaming_Declaration (Loc,
3764 Defining_Identifier => Temp,
3765 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3769 Append (Decl, Decls);
3770 Set_Renamed_Object (F, Temp);
3777 -- Establish target of function call. If context is not assignment or
3778 -- declaration, create a temporary as a target. The declaration for
3779 -- the temporary may be subsequently optimized away if the body is a
3780 -- single expression, or if the left-hand side of the assignment is
3781 -- simple enough, i.e. an entity or an explicit dereference of one.
3783 if Ekind (Subp) = E_Function then
3784 if Nkind (Parent (N)) = N_Assignment_Statement
3785 and then Is_Entity_Name (Name (Parent (N)))
3787 Targ := Name (Parent (N));
3789 elsif Nkind (Parent (N)) = N_Assignment_Statement
3790 and then Nkind (Name (Parent (N))) = N_Explicit_Dereference
3791 and then Is_Entity_Name (Prefix (Name (Parent (N))))
3793 Targ := Name (Parent (N));
3796 -- Replace call with temporary and create its declaration
3799 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3800 Set_Is_Internal (Temp);
3802 -- For the unconstrained case. the generated temporary has the
3803 -- same constrained declaration as the result variable.
3804 -- It may eventually be possible to remove that temporary and
3805 -- use the result variable directly.
3809 Make_Object_Declaration (Loc,
3810 Defining_Identifier => Temp,
3811 Object_Definition =>
3812 New_Copy_Tree (Object_Definition (Parent (Targ1))));
3814 Replace_Formals (Decl);
3818 Make_Object_Declaration (Loc,
3819 Defining_Identifier => Temp,
3820 Object_Definition =>
3821 New_Occurrence_Of (Ret_Type, Loc));
3823 Set_Etype (Temp, Ret_Type);
3826 Set_No_Initialization (Decl);
3827 Append (Decl, Decls);
3828 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3833 Insert_Actions (N, Decls);
3835 -- Traverse the tree and replace formals with actuals or their thunks.
3836 -- Attach block to tree before analysis and rewriting.
3838 Replace_Formals (Blk);
3839 Set_Parent (Blk, N);
3841 if not Comes_From_Source (Subp)
3847 if Present (Exit_Lab) then
3849 -- If the body was a single expression, the single return statement
3850 -- and the corresponding label are useless.
3854 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3857 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3859 Append (Lab_Decl, (Declarations (Blk)));
3860 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3864 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
3865 -- conflicting private views that Gigi would ignore. If this is
3866 -- predefined unit, analyze with checks off, as is done in the non-
3867 -- inlined run-time units.
3870 I_Flag : constant Boolean := In_Inlined_Body;
3873 In_Inlined_Body := True;
3877 Style : constant Boolean := Style_Check;
3879 Style_Check := False;
3880 Analyze (Blk, Suppress => All_Checks);
3881 Style_Check := Style;
3888 In_Inlined_Body := I_Flag;
3891 if Ekind (Subp) = E_Procedure then
3892 Rewrite_Procedure_Call (N, Blk);
3894 Rewrite_Function_Call (N, Blk);
3896 -- For the unconstrained case, the replacement of the call has been
3897 -- made prior to the complete analysis of the generated declarations.
3898 -- Propagate the proper type now.
3901 if Nkind (N) = N_Identifier then
3902 Set_Etype (N, Etype (Entity (N)));
3904 Set_Etype (N, Etype (Targ1));
3911 -- Cleanup mapping between formals and actuals for other expansions
3913 F := First_Formal (Subp);
3914 while Present (F) loop
3915 Set_Renamed_Object (F, Empty);
3918 end Expand_Inlined_Call;
3920 ----------------------------
3921 -- Expand_N_Function_Call --
3922 ----------------------------
3924 procedure Expand_N_Function_Call (N : Node_Id) is
3925 Typ : constant Entity_Id := Etype (N);
3927 function Returned_By_Reference return Boolean;
3928 -- If the return type is returned through the secondary stack; that is
3929 -- by reference, we don't want to create a temp to force stack checking.
3930 -- ???"sec stack" is not right -- Ada 95 return-by-reference object are
3931 -- returned wherever they are.
3932 -- Shouldn't this function be moved to exp_util???
3934 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean;
3935 -- If the call is the right side of an assignment or the expression in
3936 -- an object declaration, we don't need to create a temp as the left
3937 -- side will already trigger stack checking if necessary.
3939 -- If the call is a component in an extension aggregate, it will be
3940 -- expanded into assignments as well, so no temporary is needed. This
3941 -- also solves the problem of functions returning types with unknown
3942 -- discriminants, where it is not possible to declare an object of the
3945 ---------------------------
3946 -- Returned_By_Reference --
3947 ---------------------------
3949 function Returned_By_Reference return Boolean is
3953 if Is_Inherently_Limited_Type (Typ) then
3956 elsif Nkind (Parent (N)) /= N_Simple_Return_Statement then
3959 elsif Requires_Transient_Scope (Typ) then
3961 -- Verify that the return type of the enclosing function has the
3962 -- same constrained status as that of the expression.
3965 while Ekind (S) /= E_Function loop
3969 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
3973 end Returned_By_Reference;
3975 ---------------------------
3976 -- Rhs_Of_Assign_Or_Decl --
3977 ---------------------------
3979 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean is
3981 if (Nkind (Parent (N)) = N_Assignment_Statement
3982 and then Expression (Parent (N)) = N)
3984 (Nkind (Parent (N)) = N_Qualified_Expression
3985 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
3986 and then Expression (Parent (Parent (N))) = Parent (N))
3988 (Nkind (Parent (N)) = N_Object_Declaration
3989 and then Expression (Parent (N)) = N)
3991 (Nkind (Parent (N)) = N_Component_Association
3992 and then Expression (Parent (N)) = N
3993 and then Nkind (Parent (Parent (N))) = N_Aggregate
3994 and then Rhs_Of_Assign_Or_Decl (Parent (Parent (N))))
3996 (Nkind (Parent (N)) = N_Extension_Aggregate
3997 and then Is_Private_Type (Etype (Typ)))
4003 end Rhs_Of_Assign_Or_Decl;
4005 -- Start of processing for Expand_N_Function_Call
4008 -- A special check. If stack checking is enabled, and the return type
4009 -- might generate a large temporary, and the call is not the right side
4010 -- of an assignment, then generate an explicit temporary. We do this
4011 -- because otherwise gigi may generate a large temporary on the fly and
4012 -- this can cause trouble with stack checking.
4014 -- This is unnecessary if the call is the expression in an object
4015 -- declaration, or if it appears outside of any library unit. This can
4016 -- only happen if it appears as an actual in a library-level instance,
4017 -- in which case a temporary will be generated for it once the instance
4018 -- itself is installed.
4020 if May_Generate_Large_Temp (Typ)
4021 and then not Rhs_Of_Assign_Or_Decl (N)
4022 and then not Returned_By_Reference
4023 and then Current_Scope /= Standard_Standard
4025 if Stack_Checking_Enabled then
4027 -- Note: it might be thought that it would be OK to use a call to
4028 -- Force_Evaluation here, but that's not good enough, because
4029 -- that can results in a 'Reference construct that may still need
4033 Loc : constant Source_Ptr := Sloc (N);
4034 Temp_Obj : constant Entity_Id :=
4035 Make_Defining_Identifier (Loc,
4036 Chars => New_Internal_Name ('F'));
4037 Temp_Typ : Entity_Id := Typ;
4044 if Is_Tagged_Type (Typ)
4045 and then Present (Controlling_Argument (N))
4047 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
4048 and then Nkind (Parent (N)) /= N_Function_Call
4050 -- If this is a tag-indeterminate call, the object must
4053 if Is_Tag_Indeterminate (N) then
4054 Temp_Typ := Class_Wide_Type (Typ);
4058 -- If this is a dispatching call that is itself the
4059 -- controlling argument of an enclosing call, the
4060 -- nominal subtype of the object that replaces it must
4061 -- be classwide, so that dispatching will take place
4062 -- properly. If it is not a controlling argument, the
4063 -- object is not classwide.
4065 Proc := Entity (Name (Parent (N)));
4067 F := First_Formal (Proc);
4068 A := First_Actual (Parent (N));
4074 if Is_Controlling_Formal (F) then
4075 Temp_Typ := Class_Wide_Type (Typ);
4081 Make_Object_Declaration (Loc,
4082 Defining_Identifier => Temp_Obj,
4083 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
4084 Constant_Present => True,
4085 Expression => Relocate_Node (N));
4086 Set_Assignment_OK (Decl);
4088 Insert_Actions (N, New_List (Decl));
4089 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
4093 -- If stack-checking is not enabled, increment serial number
4094 -- for internal names, so that subsequent symbols are consistent
4095 -- with and without stack-checking.
4097 Synchronize_Serial_Number;
4099 -- Now we can expand the call with consistent symbol names
4104 -- Normal case, expand the call
4109 end Expand_N_Function_Call;
4111 ---------------------------------------
4112 -- Expand_N_Procedure_Call_Statement --
4113 ---------------------------------------
4115 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
4118 end Expand_N_Procedure_Call_Statement;
4120 ------------------------------
4121 -- Expand_N_Subprogram_Body --
4122 ------------------------------
4124 -- Add poll call if ATC polling is enabled, unless the body will be
4125 -- inlined by the back-end.
4127 -- Add dummy push/pop label nodes at start and end to clear any local
4128 -- exception indications if local-exception-to-goto optimization active.
4130 -- Add return statement if last statement in body is not a return statement
4131 -- (this makes things easier on Gigi which does not want to have to handle
4132 -- a missing return).
4134 -- Add call to Activate_Tasks if body is a task activator
4136 -- Deal with possible detection of infinite recursion
4138 -- Eliminate body completely if convention stubbed
4140 -- Encode entity names within body, since we will not need to reference
4141 -- these entities any longer in the front end.
4143 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
4145 -- Reset Pure indication if any parameter has root type System.Address
4149 procedure Expand_N_Subprogram_Body (N : Node_Id) is
4150 Loc : constant Source_Ptr := Sloc (N);
4151 H : constant Node_Id := Handled_Statement_Sequence (N);
4152 Body_Id : Entity_Id;
4153 Spec_Id : Entity_Id;
4160 procedure Add_Return (S : List_Id);
4161 -- Append a return statement to the statement sequence S if the last
4162 -- statement is not already a return or a goto statement. Note that
4163 -- the latter test is not critical, it does not matter if we add a
4164 -- few extra returns, since they get eliminated anyway later on.
4170 procedure Add_Return (S : List_Id) is
4175 -- Get last statement, ignoring any Pop_xxx_Label nodes, which are
4176 -- not relevant in this context since they are not executable.
4178 Last_Stm := Last (S);
4179 while Nkind (Last_Stm) in N_Pop_xxx_Label loop
4183 -- Now insert return unless last statement is a transfer
4185 if not Is_Transfer (Last_Stm) then
4187 -- The source location for the return is the end label of the
4188 -- procedure if present. Otherwise use the sloc of the last
4189 -- statement in the list. If the list comes from a generated
4190 -- exception handler and we are not debugging generated code,
4191 -- all the statements within the handler are made invisible
4194 if Nkind (Parent (S)) = N_Exception_Handler
4195 and then not Comes_From_Source (Parent (S))
4197 Loc := Sloc (Last_Stm);
4199 elsif Present (End_Label (H)) then
4200 Loc := Sloc (End_Label (H));
4203 Loc := Sloc (Last_Stm);
4206 Append_To (S, Make_Simple_Return_Statement (Loc));
4210 -- Start of processing for Expand_N_Subprogram_Body
4213 -- Set L to either the list of declarations if present, or
4214 -- to the list of statements if no declarations are present.
4215 -- This is used to insert new stuff at the start.
4217 if Is_Non_Empty_List (Declarations (N)) then
4218 L := Declarations (N);
4220 L := Statements (H);
4223 -- If local-exception-to-goto optimization active, insert dummy push
4224 -- statements at start, and dummy pop statements at end.
4226 if (Debug_Flag_Dot_G
4227 or else Restriction_Active (No_Exception_Propagation))
4228 and then Is_Non_Empty_List (L)
4231 FS : constant Node_Id := First (L);
4232 FL : constant Source_Ptr := Sloc (FS);
4237 -- LS points to either last statement, if statements are present
4238 -- or to the last declaration if there are no statements present.
4239 -- It is the node after which the pop's are generated.
4241 if Is_Non_Empty_List (Statements (H)) then
4242 LS := Last (Statements (H));
4249 Insert_List_Before_And_Analyze (FS, New_List (
4250 Make_Push_Constraint_Error_Label (FL),
4251 Make_Push_Program_Error_Label (FL),
4252 Make_Push_Storage_Error_Label (FL)));
4254 Insert_List_After_And_Analyze (LS, New_List (
4255 Make_Pop_Constraint_Error_Label (LL),
4256 Make_Pop_Program_Error_Label (LL),
4257 Make_Pop_Storage_Error_Label (LL)));
4261 -- Find entity for subprogram
4263 Body_Id := Defining_Entity (N);
4265 if Present (Corresponding_Spec (N)) then
4266 Spec_Id := Corresponding_Spec (N);
4271 -- Need poll on entry to subprogram if polling enabled. We only do this
4272 -- for non-empty subprograms, since it does not seem necessary to poll
4273 -- for a dummy null subprogram. Do not add polling point if calls to
4274 -- this subprogram will be inlined by the back-end, to avoid repeated
4275 -- polling points in nested inlinings.
4277 if Is_Non_Empty_List (L) then
4278 if Is_Inlined (Spec_Id)
4279 and then Front_End_Inlining
4280 and then Optimization_Level > 1
4284 Generate_Poll_Call (First (L));
4288 -- If this is a Pure function which has any parameters whose root
4289 -- type is System.Address, reset the Pure indication, since it will
4290 -- likely cause incorrect code to be generated as the parameter is
4291 -- probably a pointer, and the fact that the same pointer is passed
4292 -- does not mean that the same value is being referenced.
4294 -- Note that if the programmer gave an explicit Pure_Function pragma,
4295 -- then we believe the programmer, and leave the subprogram Pure.
4297 -- This code should probably be at the freeze point, so that it
4298 -- happens even on a -gnatc (or more importantly -gnatt) compile
4299 -- so that the semantic tree has Is_Pure set properly ???
4301 if Is_Pure (Spec_Id)
4302 and then Is_Subprogram (Spec_Id)
4303 and then not Has_Pragma_Pure_Function (Spec_Id)
4309 F := First_Formal (Spec_Id);
4310 while Present (F) loop
4311 if Is_Descendent_Of_Address (Etype (F)) then
4312 Set_Is_Pure (Spec_Id, False);
4314 if Spec_Id /= Body_Id then
4315 Set_Is_Pure (Body_Id, False);
4326 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
4328 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
4333 -- Loop through formals
4335 F := First_Formal (Spec_Id);
4336 while Present (F) loop
4337 if Is_Scalar_Type (Etype (F))
4338 and then Ekind (F) = E_Out_Parameter
4340 -- Insert the initialization. We turn off validity checks
4341 -- for this assignment, since we do not want any check on
4342 -- the initial value itself (which may well be invalid).
4344 Insert_Before_And_Analyze (First (L),
4345 Make_Assignment_Statement (Loc,
4346 Name => New_Occurrence_Of (F, Loc),
4347 Expression => Get_Simple_Init_Val (Etype (F), Loc)),
4348 Suppress => Validity_Check);
4356 Scop := Scope (Spec_Id);
4358 -- Add discriminal renamings to protected subprograms. Install new
4359 -- discriminals for expansion of the next subprogram of this protected
4362 if Is_List_Member (N)
4363 and then Present (Parent (List_Containing (N)))
4364 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4366 Add_Discriminal_Declarations
4367 (Declarations (N), Scop, Name_uObject, Loc);
4368 Add_Private_Declarations
4369 (Declarations (N), Scop, Name_uObject, Loc);
4371 -- Associate privals and discriminals with the next protected
4372 -- operation body to be expanded. These are used to expand references
4373 -- to private data objects and discriminants, respectively.
4375 Next_Op := Next_Protected_Operation (N);
4377 if Present (Next_Op) then
4378 Dec := Parent (Base_Type (Scop));
4379 Set_Privals (Dec, Next_Op, Loc);
4380 Set_Discriminals (Dec);
4384 -- Clear out statement list for stubbed procedure
4386 if Present (Corresponding_Spec (N)) then
4387 Set_Elaboration_Flag (N, Spec_Id);
4389 if Convention (Spec_Id) = Convention_Stubbed
4390 or else Is_Eliminated (Spec_Id)
4392 Set_Declarations (N, Empty_List);
4393 Set_Handled_Statement_Sequence (N,
4394 Make_Handled_Sequence_Of_Statements (Loc,
4395 Statements => New_List (
4396 Make_Null_Statement (Loc))));
4401 -- Returns_By_Ref flag is normally set when the subprogram is frozen
4402 -- but subprograms with no specs are not frozen.
4405 Typ : constant Entity_Id := Etype (Spec_Id);
4406 Utyp : constant Entity_Id := Underlying_Type (Typ);
4409 if not Acts_As_Spec (N)
4410 and then Nkind (Parent (Parent (Spec_Id))) /=
4411 N_Subprogram_Body_Stub
4415 elsif Is_Inherently_Limited_Type (Typ) then
4416 Set_Returns_By_Ref (Spec_Id);
4418 elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
4419 Set_Returns_By_Ref (Spec_Id);
4423 -- For a procedure, we add a return for all possible syntactic ends
4424 -- of the subprogram. Note that reanalysis is not necessary in this
4425 -- case since it would require a lot of work and accomplish nothing.
4427 if Ekind (Spec_Id) = E_Procedure
4428 or else Ekind (Spec_Id) = E_Generic_Procedure
4430 Add_Return (Statements (H));
4432 if Present (Exception_Handlers (H)) then
4433 Except_H := First_Non_Pragma (Exception_Handlers (H));
4434 while Present (Except_H) loop
4435 Add_Return (Statements (Except_H));
4436 Next_Non_Pragma (Except_H);
4440 -- For a function, we must deal with the case where there is at least
4441 -- one missing return. What we do is to wrap the entire body of the
4442 -- function in a block:
4455 -- raise Program_Error;
4458 -- This approach is necessary because the raise must be signalled
4459 -- to the caller, not handled by any local handler (RM 6.4(11)).
4461 -- Note: we do not need to analyze the constructed sequence here,
4462 -- since it has no handler, and an attempt to analyze the handled
4463 -- statement sequence twice is risky in various ways (e.g. the
4464 -- issue of expanding cleanup actions twice).
4466 elsif Has_Missing_Return (Spec_Id) then
4468 Hloc : constant Source_Ptr := Sloc (H);
4469 Blok : constant Node_Id :=
4470 Make_Block_Statement (Hloc,
4471 Handled_Statement_Sequence => H);
4472 Rais : constant Node_Id :=
4473 Make_Raise_Program_Error (Hloc,
4474 Reason => PE_Missing_Return);
4477 Set_Handled_Statement_Sequence (N,
4478 Make_Handled_Sequence_Of_Statements (Hloc,
4479 Statements => New_List (Blok, Rais)));
4481 Push_Scope (Spec_Id);
4488 -- If subprogram contains a parameterless recursive call, then we may
4489 -- have an infinite recursion, so see if we can generate code to check
4490 -- for this possibility if storage checks are not suppressed.
4492 if Ekind (Spec_Id) = E_Procedure
4493 and then Has_Recursive_Call (Spec_Id)
4494 and then not Storage_Checks_Suppressed (Spec_Id)
4496 Detect_Infinite_Recursion (N, Spec_Id);
4499 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
4500 -- parameters must be initialized to the appropriate default value.
4502 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
4509 Formal := First_Formal (Spec_Id);
4510 while Present (Formal) loop
4511 Floc := Sloc (Formal);
4513 if Ekind (Formal) = E_Out_Parameter
4514 and then Is_Scalar_Type (Etype (Formal))
4517 Make_Assignment_Statement (Floc,
4518 Name => New_Occurrence_Of (Formal, Floc),
4520 Get_Simple_Init_Val (Etype (Formal), Floc));
4521 Prepend (Stm, Declarations (N));
4525 Next_Formal (Formal);
4530 -- Set to encode entity names in package body before gigi is called
4532 Qualify_Entity_Names (N);
4533 end Expand_N_Subprogram_Body;
4535 -----------------------------------
4536 -- Expand_N_Subprogram_Body_Stub --
4537 -----------------------------------
4539 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
4541 if Present (Corresponding_Body (N)) then
4542 Expand_N_Subprogram_Body (
4543 Unit_Declaration_Node (Corresponding_Body (N)));
4545 end Expand_N_Subprogram_Body_Stub;
4547 -------------------------------------
4548 -- Expand_N_Subprogram_Declaration --
4549 -------------------------------------
4551 -- If the declaration appears within a protected body, it is a private
4552 -- operation of the protected type. We must create the corresponding
4553 -- protected subprogram an associated formals. For a normal protected
4554 -- operation, this is done when expanding the protected type declaration.
4556 -- If the declaration is for a null procedure, emit null body
4558 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
4559 Loc : constant Source_Ptr := Sloc (N);
4560 Subp : constant Entity_Id := Defining_Entity (N);
4561 Scop : constant Entity_Id := Scope (Subp);
4562 Prot_Decl : Node_Id;
4564 Prot_Id : Entity_Id;
4567 -- Deal with case of protected subprogram. Do not generate protected
4568 -- operation if operation is flagged as eliminated.
4570 if Is_List_Member (N)
4571 and then Present (Parent (List_Containing (N)))
4572 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
4573 and then Is_Protected_Type (Scop)
4575 if No (Protected_Body_Subprogram (Subp))
4576 and then not Is_Eliminated (Subp)
4579 Make_Subprogram_Declaration (Loc,
4581 Build_Protected_Sub_Specification
4582 (N, Scop, Unprotected_Mode));
4584 -- The protected subprogram is declared outside of the protected
4585 -- body. Given that the body has frozen all entities so far, we
4586 -- analyze the subprogram and perform freezing actions explicitly.
4587 -- including the generation of an explicit freeze node, to ensure
4588 -- that gigi has the proper order of elaboration.
4589 -- If the body is a subunit, the insertion point is before the
4590 -- stub in the parent.
4592 Prot_Bod := Parent (List_Containing (N));
4594 if Nkind (Parent (Prot_Bod)) = N_Subunit then
4595 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
4598 Insert_Before (Prot_Bod, Prot_Decl);
4599 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
4600 Set_Has_Delayed_Freeze (Prot_Id);
4602 Push_Scope (Scope (Scop));
4603 Analyze (Prot_Decl);
4604 Insert_Actions (N, Freeze_Entity (Prot_Id, Loc));
4605 Set_Protected_Body_Subprogram (Subp, Prot_Id);
4609 -- Ada 2005 (AI-348): Generation of the null body
4611 elsif Nkind (Specification (N)) = N_Procedure_Specification
4612 and then Null_Present (Specification (N))
4615 Bod : constant Node_Id :=
4616 Make_Subprogram_Body (Loc,
4618 New_Copy_Tree (Specification (N)),
4619 Declarations => New_List,
4620 Handled_Statement_Sequence =>
4621 Make_Handled_Sequence_Of_Statements (Loc,
4622 Statements => New_List (Make_Null_Statement (Loc))));
4624 Set_Body_To_Inline (N, Bod);
4625 Insert_After (N, Bod);
4628 -- Corresponding_Spec isn't being set by Analyze_Subprogram_Body,
4629 -- evidently because Set_Has_Completion is called earlier for null
4630 -- procedures in Analyze_Subprogram_Declaration, so we force its
4631 -- setting here. If the setting of Has_Completion is not set
4632 -- earlier, then it can result in missing body errors if other
4633 -- errors were already reported (since expansion is turned off).
4635 -- Should creation of the empty body be moved to the analyzer???
4637 Set_Corresponding_Spec (Bod, Defining_Entity (Specification (N)));
4640 end Expand_N_Subprogram_Declaration;
4642 ---------------------------------------
4643 -- Expand_Protected_Object_Reference --
4644 ---------------------------------------
4646 function Expand_Protected_Object_Reference
4648 Scop : Entity_Id) return Node_Id
4650 Loc : constant Source_Ptr := Sloc (N);
4658 Make_Identifier (Loc,
4659 Chars => Name_uObject);
4660 Set_Etype (Rec, Corresponding_Record_Type (Scop));
4662 -- Find enclosing protected operation, and retrieve its first parameter,
4663 -- which denotes the enclosing protected object. If the enclosing
4664 -- operation is an entry, we are immediately within the protected body,
4665 -- and we can retrieve the object from the service entries procedure. A
4666 -- barrier function has has the same signature as an entry. A barrier
4667 -- function is compiled within the protected object, but unlike
4668 -- protected operations its never needs locks, so that its protected
4669 -- body subprogram points to itself.
4671 Proc := Current_Scope;
4672 while Present (Proc)
4673 and then Scope (Proc) /= Scop
4675 Proc := Scope (Proc);
4678 Corr := Protected_Body_Subprogram (Proc);
4682 -- Previous error left expansion incomplete.
4683 -- Nothing to do on this call.
4690 (First (Parameter_Specifications (Parent (Corr))));
4692 if Is_Subprogram (Proc)
4693 and then Proc /= Corr
4695 -- Protected function or procedure
4697 Set_Entity (Rec, Param);
4699 -- Rec is a reference to an entity which will not be in scope when
4700 -- the call is reanalyzed, and needs no further analysis.
4705 -- Entry or barrier function for entry body. The first parameter of
4706 -- the entry body procedure is pointer to the object. We create a
4707 -- local variable of the proper type, duplicating what is done to
4708 -- define _object later on.
4712 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
4714 New_Internal_Name ('T'));
4718 Make_Full_Type_Declaration (Loc,
4719 Defining_Identifier => Obj_Ptr,
4721 Make_Access_To_Object_Definition (Loc,
4722 Subtype_Indication =>
4724 (Corresponding_Record_Type (Scop), Loc))));
4726 Insert_Actions (N, Decls);
4727 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4730 Make_Explicit_Dereference (Loc,
4731 Unchecked_Convert_To (Obj_Ptr,
4732 New_Occurrence_Of (Param, Loc)));
4734 -- Analyze new actual. Other actuals in calls are already analyzed
4735 -- and the list of actuals is not reanalyzed after rewriting.
4737 Set_Parent (Rec, N);
4743 end Expand_Protected_Object_Reference;
4745 --------------------------------------
4746 -- Expand_Protected_Subprogram_Call --
4747 --------------------------------------
4749 procedure Expand_Protected_Subprogram_Call
4757 -- If the protected object is not an enclosing scope, this is
4758 -- an inter-object function call. Inter-object procedure
4759 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4760 -- The call is intra-object only if the subprogram being
4761 -- called is in the protected body being compiled, and if the
4762 -- protected object in the call is statically the enclosing type.
4763 -- The object may be an component of some other data structure,
4764 -- in which case this must be handled as an inter-object call.
4766 if not In_Open_Scopes (Scop)
4767 or else not Is_Entity_Name (Name (N))
4769 if Nkind (Name (N)) = N_Selected_Component then
4770 Rec := Prefix (Name (N));
4773 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4774 Rec := Prefix (Prefix (Name (N)));
4777 Build_Protected_Subprogram_Call (N,
4778 Name => New_Occurrence_Of (Subp, Sloc (N)),
4779 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4783 Rec := Expand_Protected_Object_Reference (N, Scop);
4789 Build_Protected_Subprogram_Call (N,
4798 -- If it is a function call it can appear in elaboration code and
4799 -- the called entity must be frozen here.
4801 if Ekind (Subp) = E_Function then
4802 Freeze_Expression (Name (N));
4804 end Expand_Protected_Subprogram_Call;
4806 --------------------------------
4807 -- Is_Build_In_Place_Function --
4808 --------------------------------
4810 function Is_Build_In_Place_Function (E : Entity_Id) return Boolean is
4812 -- For now we test whether E denotes a function or access-to-function
4813 -- type whose result subtype is inherently limited. Later this test may
4814 -- be revised to allow composite nonlimited types. Functions with a
4815 -- foreign convention or whose result type has a foreign convention
4818 if Ekind (E) = E_Function
4819 or else Ekind (E) = E_Generic_Function
4820 or else (Ekind (E) = E_Subprogram_Type
4821 and then Etype (E) /= Standard_Void_Type)
4823 -- Note: If you have Convention (C) on an inherently limited type,
4824 -- you're on your own. That is, the C code will have to be carefully
4825 -- written to know about the Ada conventions.
4827 if Has_Foreign_Convention (E)
4828 or else Has_Foreign_Convention (Etype (E))
4832 -- If the return type is a limited interface it has to be treated
4833 -- as a return in place, even if the actual object is some non-
4834 -- limited descendant.
4836 elsif Is_Limited_Interface (Etype (E)) then
4840 return Is_Inherently_Limited_Type (Etype (E))
4841 and then Ada_Version >= Ada_05
4842 and then not Debug_Flag_Dot_L;
4848 end Is_Build_In_Place_Function;
4850 -------------------------------------
4851 -- Is_Build_In_Place_Function_Call --
4852 -------------------------------------
4854 function Is_Build_In_Place_Function_Call (N : Node_Id) return Boolean is
4855 Exp_Node : Node_Id := N;
4856 Function_Id : Entity_Id;
4859 -- Step past qualification or unchecked conversion (the latter can occur
4860 -- in cases of calls to 'Input).
4862 if Nkind (Exp_Node) = N_Qualified_Expression
4863 or else Nkind (Exp_Node) = N_Unchecked_Type_Conversion
4865 Exp_Node := Expression (N);
4868 if Nkind (Exp_Node) /= N_Function_Call then
4872 if Is_Entity_Name (Name (Exp_Node)) then
4873 Function_Id := Entity (Name (Exp_Node));
4875 elsif Nkind (Name (Exp_Node)) = N_Explicit_Dereference then
4876 Function_Id := Etype (Name (Exp_Node));
4879 return Is_Build_In_Place_Function (Function_Id);
4881 end Is_Build_In_Place_Function_Call;
4883 ---------------------------------------
4884 -- Is_Build_In_Place_Function_Return --
4885 ---------------------------------------
4887 function Is_Build_In_Place_Function_Return (N : Node_Id) return Boolean is
4889 if Nkind (N) = N_Simple_Return_Statement
4890 or else Nkind (N) = N_Extended_Return_Statement
4892 return Is_Build_In_Place_Function
4893 (Return_Applies_To (Return_Statement_Entity (N)));
4897 end Is_Build_In_Place_Function_Return;
4899 -----------------------
4900 -- Freeze_Subprogram --
4901 -----------------------
4903 procedure Freeze_Subprogram (N : Node_Id) is
4904 Loc : constant Source_Ptr := Sloc (N);
4906 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4907 -- (Ada 2005): Register a predefined primitive in all the secondary
4908 -- dispatch tables of its primitive type.
4910 ----------------------------------
4911 -- Register_Predefined_DT_Entry --
4912 ----------------------------------
4914 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4915 Iface_DT_Ptr : Elmt_Id;
4916 Tagged_Typ : Entity_Id;
4917 Thunk_Id : Entity_Id;
4918 Thunk_Code : Node_Id;
4921 Tagged_Typ := Find_Dispatching_Type (Prim);
4923 if No (Access_Disp_Table (Tagged_Typ))
4924 or else not Has_Abstract_Interfaces (Tagged_Typ)
4925 or else not RTE_Available (RE_Interface_Tag)
4926 or else Restriction_Active (No_Dispatching_Calls)
4931 -- Skip the first access-to-dispatch-table pointer since it leads
4932 -- to the primary dispatch table. We are only concerned with the
4933 -- secondary dispatch table pointers. Note that the access-to-
4934 -- dispatch-table pointer corresponds to the first implemented
4935 -- interface retrieved below.
4938 Next_Elmt (First_Elmt (Access_Disp_Table (Tagged_Typ)));
4940 while Present (Iface_DT_Ptr)
4941 and then Ekind (Node (Iface_DT_Ptr)) = E_Constant
4943 Expand_Interface_Thunk (Prim, Thunk_Id, Thunk_Code);
4945 if Present (Thunk_Code) then
4946 Insert_Actions (N, New_List (
4949 Build_Set_Predefined_Prim_Op_Address (Loc,
4950 Tag_Node => New_Reference_To (Node (Iface_DT_Ptr), Loc),
4951 Position => DT_Position (Prim),
4953 Make_Attribute_Reference (Loc,
4954 Prefix => New_Reference_To (Thunk_Id, Loc),
4955 Attribute_Name => Name_Address))));
4958 Next_Elmt (Iface_DT_Ptr);
4960 end Register_Predefined_DT_Entry;
4964 Subp : constant Entity_Id := Entity (N);
4967 -- We suppress the initialization of the dispatch table entry when
4968 -- VM_Target because the dispatching mechanism is handled internally
4971 if Is_Dispatching_Operation (Subp)
4972 and then not Is_Abstract_Subprogram (Subp)
4973 and then Present (DTC_Entity (Subp))
4974 and then Present (Scope (DTC_Entity (Subp)))
4975 and then VM_Target = No_VM
4976 and then not Restriction_Active (No_Dispatching_Calls)
4977 and then RTE_Available (RE_Tag)
4980 Typ : constant Entity_Id := Scope (DTC_Entity (Subp));
4983 -- Handle private overriden primitives
4985 if not Is_CPP_Class (Typ) then
4986 Check_Overriding_Operation (Subp);
4989 -- We assume that imported CPP primitives correspond with objects
4990 -- whose constructor is in the CPP side; therefore we don't need
4991 -- to generate code to register them in the dispatch table.
4993 if Is_CPP_Class (Typ) then
4996 -- Handle CPP primitives found in derivations of CPP_Class types.
4997 -- These primitives must have been inherited from some parent, and
4998 -- there is no need to register them in the dispatch table because
4999 -- Build_Inherit_Prims takes care of the initialization of these
5002 elsif Is_Imported (Subp)
5003 and then (Convention (Subp) = Convention_CPP
5004 or else Convention (Subp) = Convention_C)
5008 -- Generate code to register the primitive in non statically
5009 -- allocated dispatch tables
5011 elsif not Static_Dispatch_Tables
5013 Is_Library_Level_Tagged_Type (Scope (DTC_Entity (Subp)))
5015 -- When a primitive is frozen, enter its name in its dispatch
5018 if not Is_Interface (Typ)
5019 or else Present (Abstract_Interface_Alias (Subp))
5021 if Is_Predefined_Dispatching_Operation (Subp) then
5022 Register_Predefined_DT_Entry (Subp);
5025 Register_Primitive (Loc,
5033 -- Mark functions that return by reference. Note that it cannot be part
5034 -- of the normal semantic analysis of the spec since the underlying
5035 -- returned type may not be known yet (for private types).
5038 Typ : constant Entity_Id := Etype (Subp);
5039 Utyp : constant Entity_Id := Underlying_Type (Typ);
5041 if Is_Inherently_Limited_Type (Typ) then
5042 Set_Returns_By_Ref (Subp);
5043 elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
5044 Set_Returns_By_Ref (Subp);
5047 end Freeze_Subprogram;
5049 -------------------------------------------
5050 -- Make_Build_In_Place_Call_In_Allocator --
5051 -------------------------------------------
5053 procedure Make_Build_In_Place_Call_In_Allocator
5054 (Allocator : Node_Id;
5055 Function_Call : Node_Id)
5058 Func_Call : Node_Id := Function_Call;
5059 Function_Id : Entity_Id;
5060 Result_Subt : Entity_Id;
5061 Acc_Type : constant Entity_Id := Etype (Allocator);
5062 New_Allocator : Node_Id;
5063 Return_Obj_Access : Entity_Id;
5066 -- Step past qualification or unchecked conversion (the latter can occur
5067 -- in cases of calls to 'Input).
5069 if Nkind (Func_Call) = N_Qualified_Expression
5070 or else Nkind (Func_Call) = N_Unchecked_Type_Conversion
5072 Func_Call := Expression (Func_Call);
5075 -- If the call has already been processed to add build-in-place actuals
5076 -- then return. This should not normally occur in an allocator context,
5077 -- but we add the protection as a defensive measure.
5079 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5083 -- Mark the call as processed as a build-in-place call
5085 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5087 Loc := Sloc (Function_Call);
5089 if Is_Entity_Name (Name (Func_Call)) then
5090 Function_Id := Entity (Name (Func_Call));
5092 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5093 Function_Id := Etype (Name (Func_Call));
5096 raise Program_Error;
5099 Result_Subt := Etype (Function_Id);
5101 -- When the result subtype is constrained, the return object must be
5102 -- allocated on the caller side, and access to it is passed to the
5105 -- Here and in related routines, we must examine the full view of the
5106 -- type, because the view at the point of call may differ from that
5107 -- that in the function body, and the expansion mechanism depends on
5108 -- the characteristics of the full view.
5110 if Is_Constrained (Underlying_Type (Result_Subt)) then
5112 -- Replace the initialized allocator of form "new T'(Func (...))"
5113 -- with an uninitialized allocator of form "new T", where T is the
5114 -- result subtype of the called function. The call to the function
5115 -- is handled separately further below.
5118 Make_Allocator (Loc, New_Reference_To (Result_Subt, Loc));
5120 Set_Storage_Pool (New_Allocator, Storage_Pool (Allocator));
5121 Set_Procedure_To_Call (New_Allocator, Procedure_To_Call (Allocator));
5122 Set_No_Initialization (New_Allocator);
5124 Rewrite (Allocator, New_Allocator);
5126 -- Create a new access object and initialize it to the result of the
5127 -- new uninitialized allocator.
5129 Return_Obj_Access :=
5130 Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
5131 Set_Etype (Return_Obj_Access, Acc_Type);
5133 Insert_Action (Allocator,
5134 Make_Object_Declaration (Loc,
5135 Defining_Identifier => Return_Obj_Access,
5136 Object_Definition => New_Reference_To (Acc_Type, Loc),
5137 Expression => Relocate_Node (Allocator)));
5139 -- When the function has a controlling result, an allocation-form
5140 -- parameter must be passed indicating that the caller is allocating
5141 -- the result object. This is needed because such a function can be
5142 -- called as a dispatching operation and must be treated similarly
5143 -- to functions with unconstrained result subtypes.
5145 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5146 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5148 Add_Final_List_Actual_To_Build_In_Place_Call
5149 (Func_Call, Function_Id, Acc_Type);
5151 Add_Task_Actuals_To_Build_In_Place_Call
5152 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5154 -- Add an implicit actual to the function call that provides access
5155 -- to the allocated object. An unchecked conversion to the (specific)
5156 -- result subtype of the function is inserted to handle cases where
5157 -- the access type of the allocator has a class-wide designated type.
5159 Add_Access_Actual_To_Build_In_Place_Call
5162 Make_Unchecked_Type_Conversion (Loc,
5163 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5165 Make_Explicit_Dereference (Loc,
5166 Prefix => New_Reference_To (Return_Obj_Access, Loc))));
5168 -- When the result subtype is unconstrained, the function itself must
5169 -- perform the allocation of the return object, so we pass parameters
5170 -- indicating that. We don't yet handle the case where the allocation
5171 -- must be done in a user-defined storage pool, which will require
5172 -- passing another actual or two to provide allocation/deallocation
5177 -- Pass an allocation parameter indicating that the function should
5178 -- allocate its result on the heap.
5180 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5181 (Func_Call, Function_Id, Alloc_Form => Global_Heap);
5183 Add_Final_List_Actual_To_Build_In_Place_Call
5184 (Func_Call, Function_Id, Acc_Type);
5186 Add_Task_Actuals_To_Build_In_Place_Call
5187 (Func_Call, Function_Id, Master_Actual => Master_Id (Acc_Type));
5189 -- The caller does not provide the return object in this case, so we
5190 -- have to pass null for the object access actual.
5192 Add_Access_Actual_To_Build_In_Place_Call
5193 (Func_Call, Function_Id, Return_Object => Empty);
5196 -- Finally, replace the allocator node with a reference to the result
5197 -- of the function call itself (which will effectively be an access
5198 -- to the object created by the allocator).
5200 Rewrite (Allocator, Make_Reference (Loc, Relocate_Node (Function_Call)));
5201 Analyze_And_Resolve (Allocator, Acc_Type);
5202 end Make_Build_In_Place_Call_In_Allocator;
5204 ---------------------------------------------------
5205 -- Make_Build_In_Place_Call_In_Anonymous_Context --
5206 ---------------------------------------------------
5208 procedure Make_Build_In_Place_Call_In_Anonymous_Context
5209 (Function_Call : Node_Id)
5212 Func_Call : Node_Id := Function_Call;
5213 Function_Id : Entity_Id;
5214 Result_Subt : Entity_Id;
5215 Return_Obj_Id : Entity_Id;
5216 Return_Obj_Decl : Entity_Id;
5219 -- Step past qualification or unchecked conversion (the latter can occur
5220 -- in cases of calls to 'Input).
5222 if Nkind (Func_Call) = N_Qualified_Expression
5223 or else Nkind (Func_Call) = N_Unchecked_Type_Conversion
5225 Func_Call := Expression (Func_Call);
5228 -- If the call has already been processed to add build-in-place actuals
5229 -- then return. One place this can occur is for calls to build-in-place
5230 -- functions that occur within a call to a protected operation, where
5231 -- due to rewriting and expansion of the protected call there can be
5232 -- more than one call to Expand_Actuals for the same set of actuals.
5234 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5238 -- Mark the call as processed as a build-in-place call
5240 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5242 Loc := Sloc (Function_Call);
5244 if Is_Entity_Name (Name (Func_Call)) then
5245 Function_Id := Entity (Name (Func_Call));
5247 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5248 Function_Id := Etype (Name (Func_Call));
5251 raise Program_Error;
5254 Result_Subt := Etype (Function_Id);
5256 -- When the result subtype is constrained, an object of the subtype is
5257 -- declared and an access value designating it is passed as an actual.
5259 if Is_Constrained (Underlying_Type (Result_Subt)) then
5261 -- Create a temporary object to hold the function result
5264 Make_Defining_Identifier (Loc,
5265 Chars => New_Internal_Name ('R'));
5266 Set_Etype (Return_Obj_Id, Result_Subt);
5269 Make_Object_Declaration (Loc,
5270 Defining_Identifier => Return_Obj_Id,
5271 Aliased_Present => True,
5272 Object_Definition => New_Reference_To (Result_Subt, Loc));
5274 Set_No_Initialization (Return_Obj_Decl);
5276 Insert_Action (Func_Call, Return_Obj_Decl);
5278 -- When the function has a controlling result, an allocation-form
5279 -- parameter must be passed indicating that the caller is allocating
5280 -- the result object. This is needed because such a function can be
5281 -- called as a dispatching operation and must be treated similarly
5282 -- to functions with unconstrained result subtypes.
5284 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5285 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5287 Add_Final_List_Actual_To_Build_In_Place_Call
5288 (Func_Call, Function_Id, Acc_Type => Empty);
5290 Add_Task_Actuals_To_Build_In_Place_Call
5291 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5293 -- Add an implicit actual to the function call that provides access
5294 -- to the caller's return object.
5296 Add_Access_Actual_To_Build_In_Place_Call
5297 (Func_Call, Function_Id, New_Reference_To (Return_Obj_Id, Loc));
5299 -- When the result subtype is unconstrained, the function must allocate
5300 -- the return object in the secondary stack, so appropriate implicit
5301 -- parameters are added to the call to indicate that. A transient
5302 -- scope is established to ensure eventual cleanup of the result.
5306 -- Pass an allocation parameter indicating that the function should
5307 -- allocate its result on the secondary stack.
5309 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5310 (Func_Call, Function_Id, Alloc_Form => Secondary_Stack);
5312 Add_Final_List_Actual_To_Build_In_Place_Call
5313 (Func_Call, Function_Id, Acc_Type => Empty);
5315 Add_Task_Actuals_To_Build_In_Place_Call
5316 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5318 -- Pass a null value to the function since no return object is
5319 -- available on the caller side.
5321 Add_Access_Actual_To_Build_In_Place_Call
5322 (Func_Call, Function_Id, Empty);
5324 Establish_Transient_Scope (Func_Call, Sec_Stack => True);
5326 end Make_Build_In_Place_Call_In_Anonymous_Context;
5328 ---------------------------------------------------
5329 -- Make_Build_In_Place_Call_In_Assignment --
5330 ---------------------------------------------------
5332 procedure Make_Build_In_Place_Call_In_Assignment
5334 Function_Call : Node_Id)
5336 Lhs : constant Node_Id := Name (Assign);
5338 Func_Call : Node_Id := Function_Call;
5339 Function_Id : Entity_Id;
5340 Result_Subt : Entity_Id;
5341 Ref_Type : Entity_Id;
5342 Ptr_Typ_Decl : Node_Id;
5347 -- Step past qualification or unchecked conversion (the latter can occur
5348 -- in cases of calls to 'Input).
5350 if Nkind (Func_Call) = N_Qualified_Expression
5351 or else Nkind (Func_Call) = N_Unchecked_Type_Conversion
5353 Func_Call := Expression (Func_Call);
5356 -- If the call has already been processed to add build-in-place actuals
5357 -- then return. This should not normally occur in an assignment context,
5358 -- but we add the protection as a defensive measure.
5360 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5364 -- Mark the call as processed as a build-in-place call
5366 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5368 Loc := Sloc (Function_Call);
5370 if Is_Entity_Name (Name (Func_Call)) then
5371 Function_Id := Entity (Name (Func_Call));
5373 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5374 Function_Id := Etype (Name (Func_Call));
5377 raise Program_Error;
5380 Result_Subt := Etype (Function_Id);
5382 -- When the result subtype is unconstrained, an additional actual must
5383 -- be passed to indicate that the caller is providing the return object.
5384 -- This parameter must also be passed when the called function has a
5385 -- controlling result, because dispatching calls to the function needs
5386 -- to be treated effectively the same as calls to class-wide functions.
5388 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5389 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5391 Add_Final_List_Actual_To_Build_In_Place_Call
5392 (Func_Call, Function_Id, Acc_Type => Empty);
5394 Add_Task_Actuals_To_Build_In_Place_Call
5395 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5397 -- Add an implicit actual to the function call that provides access to
5398 -- the caller's return object.
5400 Add_Access_Actual_To_Build_In_Place_Call
5403 Make_Unchecked_Type_Conversion (Loc,
5404 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5405 Expression => Relocate_Node (Lhs)));
5407 -- Create an access type designating the function's result subtype
5410 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5413 Make_Full_Type_Declaration (Loc,
5414 Defining_Identifier => Ref_Type,
5416 Make_Access_To_Object_Definition (Loc,
5417 All_Present => True,
5418 Subtype_Indication =>
5419 New_Reference_To (Result_Subt, Loc)));
5421 Insert_After_And_Analyze (Assign, Ptr_Typ_Decl);
5423 -- Finally, create an access object initialized to a reference to the
5427 Make_Defining_Identifier (Loc,
5428 Chars => New_Internal_Name ('R'));
5429 Set_Etype (Def_Id, Ref_Type);
5432 Make_Reference (Loc,
5433 Prefix => Relocate_Node (Func_Call));
5435 Insert_After_And_Analyze (Ptr_Typ_Decl,
5436 Make_Object_Declaration (Loc,
5437 Defining_Identifier => Def_Id,
5438 Object_Definition => New_Reference_To (Ref_Type, Loc),
5439 Expression => New_Expr));
5441 Rewrite (Assign, Make_Null_Statement (Loc));
5442 end Make_Build_In_Place_Call_In_Assignment;
5444 ----------------------------------------------------
5445 -- Make_Build_In_Place_Call_In_Object_Declaration --
5446 ----------------------------------------------------
5448 procedure Make_Build_In_Place_Call_In_Object_Declaration
5449 (Object_Decl : Node_Id;
5450 Function_Call : Node_Id)
5453 Obj_Def_Id : constant Entity_Id :=
5454 Defining_Identifier (Object_Decl);
5456 Func_Call : Node_Id := Function_Call;
5457 Function_Id : Entity_Id;
5458 Result_Subt : Entity_Id;
5459 Caller_Object : Node_Id;
5460 Call_Deref : Node_Id;
5461 Ref_Type : Entity_Id;
5462 Ptr_Typ_Decl : Node_Id;
5465 Enclosing_Func : Entity_Id;
5466 Pass_Caller_Acc : Boolean := False;
5469 -- Step past qualification or unchecked conversion (the latter can occur
5470 -- in cases of calls to 'Input).
5472 if Nkind (Func_Call) = N_Qualified_Expression
5473 or else Nkind (Func_Call) = N_Unchecked_Type_Conversion
5475 Func_Call := Expression (Func_Call);
5478 -- If the call has already been processed to add build-in-place actuals
5479 -- then return. This should not normally occur in an object declaration,
5480 -- but we add the protection as a defensive measure.
5482 if Is_Expanded_Build_In_Place_Call (Func_Call) then
5486 -- Mark the call as processed as a build-in-place call
5488 Set_Is_Expanded_Build_In_Place_Call (Func_Call);
5490 Loc := Sloc (Function_Call);
5492 if Is_Entity_Name (Name (Func_Call)) then
5493 Function_Id := Entity (Name (Func_Call));
5495 elsif Nkind (Name (Func_Call)) = N_Explicit_Dereference then
5496 Function_Id := Etype (Name (Func_Call));
5499 raise Program_Error;
5502 Result_Subt := Etype (Function_Id);
5504 -- In the constrained case, add an implicit actual to the function call
5505 -- that provides access to the declared object. An unchecked conversion
5506 -- to the (specific) result type of the function is inserted to handle
5507 -- the case where the object is declared with a class-wide type.
5509 if Is_Constrained (Underlying_Type (Result_Subt)) then
5511 Make_Unchecked_Type_Conversion (Loc,
5512 Subtype_Mark => New_Reference_To (Result_Subt, Loc),
5513 Expression => New_Reference_To (Obj_Def_Id, Loc));
5515 -- When the function has a controlling result, an allocation-form
5516 -- parameter must be passed indicating that the caller is allocating
5517 -- the result object. This is needed because such a function can be
5518 -- called as a dispatching operation and must be treated similarly
5519 -- to functions with unconstrained result subtypes.
5521 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5522 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5524 -- If the function's result subtype is unconstrained and the object is
5525 -- a return object of an enclosing build-in-place function, then the
5526 -- implicit build-in-place parameters of the enclosing function must be
5527 -- passed along to the called function.
5529 elsif Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement then
5530 Pass_Caller_Acc := True;
5532 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5534 -- If the enclosing function has a constrained result type, then
5535 -- caller allocation will be used.
5537 if Is_Constrained (Etype (Enclosing_Func)) then
5538 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5539 (Func_Call, Function_Id, Alloc_Form => Caller_Allocation);
5541 -- Otherwise, when the enclosing function has an unconstrained result
5542 -- type, the BIP_Alloc_Form formal of the enclosing function must be
5543 -- passed along to the callee.
5546 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5551 (Build_In_Place_Formal (Enclosing_Func, BIP_Alloc_Form),
5555 -- Retrieve the BIPacc formal from the enclosing function and convert
5556 -- it to the access type of the callee's BIP_Object_Access formal.
5559 Make_Unchecked_Type_Conversion (Loc,
5563 (Build_In_Place_Formal (Function_Id, BIP_Object_Access)),
5567 (Build_In_Place_Formal (Enclosing_Func, BIP_Object_Access),
5570 -- In other unconstrained cases, pass an indication to do the allocation
5571 -- on the secondary stack and set Caller_Object to Empty so that a null
5572 -- value will be passed for the caller's object address. A transient
5573 -- scope is established to ensure eventual cleanup of the result.
5576 Add_Alloc_Form_Actual_To_Build_In_Place_Call
5579 Alloc_Form => Secondary_Stack);
5580 Caller_Object := Empty;
5582 Establish_Transient_Scope (Object_Decl, Sec_Stack => True);
5585 Add_Final_List_Actual_To_Build_In_Place_Call
5586 (Func_Call, Function_Id, Acc_Type => Empty);
5588 if Nkind (Parent (Object_Decl)) = N_Extended_Return_Statement
5589 and then Has_Task (Result_Subt)
5591 Enclosing_Func := Enclosing_Subprogram (Obj_Def_Id);
5593 -- Here we're passing along the master that was passed in to this
5596 Add_Task_Actuals_To_Build_In_Place_Call
5597 (Func_Call, Function_Id,
5600 (Build_In_Place_Formal (Enclosing_Func, BIP_Master), Loc));
5603 Add_Task_Actuals_To_Build_In_Place_Call
5604 (Func_Call, Function_Id, Make_Identifier (Loc, Name_uMaster));
5607 Add_Access_Actual_To_Build_In_Place_Call
5608 (Func_Call, Function_Id, Caller_Object, Is_Access => Pass_Caller_Acc);
5610 -- Create an access type designating the function's result subtype
5613 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5616 Make_Full_Type_Declaration (Loc,
5617 Defining_Identifier => Ref_Type,
5619 Make_Access_To_Object_Definition (Loc,
5620 All_Present => True,
5621 Subtype_Indication =>
5622 New_Reference_To (Result_Subt, Loc)));
5624 -- The access type and its accompanying object must be inserted after
5625 -- the object declaration in the constrained case, so that the function
5626 -- call can be passed access to the object. In the unconstrained case,
5627 -- the access type and object must be inserted before the object, since
5628 -- the object declaration is rewritten to be a renaming of a dereference
5629 -- of the access object.
5631 if Is_Constrained (Underlying_Type (Result_Subt)) then
5632 Insert_After_And_Analyze (Object_Decl, Ptr_Typ_Decl);
5634 Insert_Before_And_Analyze (Object_Decl, Ptr_Typ_Decl);
5637 -- Finally, create an access object initialized to a reference to the
5641 Make_Defining_Identifier (Loc,
5642 Chars => New_Internal_Name ('R'));
5643 Set_Etype (Def_Id, Ref_Type);
5646 Make_Reference (Loc,
5647 Prefix => Relocate_Node (Func_Call));
5649 Insert_After_And_Analyze (Ptr_Typ_Decl,
5650 Make_Object_Declaration (Loc,
5651 Defining_Identifier => Def_Id,
5652 Object_Definition => New_Reference_To (Ref_Type, Loc),
5653 Expression => New_Expr));
5655 if Is_Constrained (Underlying_Type (Result_Subt)) then
5656 Set_Expression (Object_Decl, Empty);
5657 Set_No_Initialization (Object_Decl);
5659 -- In case of an unconstrained result subtype, rewrite the object
5660 -- declaration as an object renaming where the renamed object is a
5661 -- dereference of <function_Call>'reference:
5663 -- Obj : Subt renames <function_call>'Ref.all;
5667 Make_Explicit_Dereference (Loc,
5668 Prefix => New_Reference_To (Def_Id, Loc));
5670 Rewrite (Object_Decl,
5671 Make_Object_Renaming_Declaration (Loc,
5672 Defining_Identifier => Make_Defining_Identifier (Loc,
5673 New_Internal_Name ('D')),
5674 Access_Definition => Empty,
5675 Subtype_Mark => New_Occurrence_Of (Result_Subt, Loc),
5676 Name => Call_Deref));
5678 Set_Renamed_Object (Defining_Identifier (Object_Decl), Call_Deref);
5680 Analyze (Object_Decl);
5682 -- Replace the internal identifier of the renaming declaration's
5683 -- entity with identifier of the original object entity. We also have
5684 -- to exchange the entities containing their defining identifiers to
5685 -- ensure the correct replacement of the object declaration by the
5686 -- object renaming declaration to avoid homograph conflicts (since
5687 -- the object declaration's defining identifier was already entered
5688 -- in current scope).
5690 Set_Chars (Defining_Identifier (Object_Decl), Chars (Obj_Def_Id));
5691 Exchange_Entities (Defining_Identifier (Object_Decl), Obj_Def_Id);
5694 -- If the object entity has a class-wide Etype, then we need to change
5695 -- it to the result subtype of the function call, because otherwise the
5696 -- object will be class-wide without an explicit intialization and won't
5697 -- be allocated properly by the back end. It seems unclean to make such
5698 -- a revision to the type at this point, and we should try to improve
5699 -- this treatment when build-in-place functions with class-wide results
5700 -- are implemented. ???
5702 if Is_Class_Wide_Type (Etype (Defining_Identifier (Object_Decl))) then
5703 Set_Etype (Defining_Identifier (Object_Decl), Result_Subt);
5705 end Make_Build_In_Place_Call_In_Object_Declaration;