2004-12-07 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / ada / exp_ch6.adb
blobbe9463ba1a201b25c2f7be9edbadacefca4f3659
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Elists; use Elists;
33 with Exp_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_Ch11; use Exp_Ch11;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Fname; use Fname;
46 with Freeze; use Freeze;
47 with Hostparm; use Hostparm;
48 with Inline; use Inline;
49 with Lib; use Lib;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Dist; use Sem_Dist;
63 with Sem_Res; use Sem_Res;
64 with Sem_Util; use Sem_Util;
65 with Sinfo; use Sinfo;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Tbuild; use Tbuild;
69 with Ttypes; use Ttypes;
70 with Uintp; use Uintp;
71 with Validsw; use Validsw;
73 package body Exp_Ch6 is
75 -----------------------
76 -- Local Subprograms --
77 -----------------------
79 procedure Check_Overriding_Operation (Subp : Entity_Id);
80 -- Subp is a dispatching operation. Check whether it may override an
81 -- inherited private operation, in which case its DT entry is that of
82 -- the hidden operation, not the one it may have received earlier.
83 -- This must be done before emitting the code to set the corresponding
84 -- DT to the address of the subprogram. The actual placement of Subp in
85 -- the proper place in the list of primitive operations is done in
86 -- Declare_Inherited_Private_Subprograms, which also has to deal with
87 -- implicit operations. This duplication is unavoidable for now???
89 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id);
90 -- This procedure is called only if the subprogram body N, whose spec
91 -- has the given entity Spec, contains a parameterless recursive call.
92 -- It attempts to generate runtime code to detect if this a case of
93 -- infinite recursion.
95 -- The body is scanned to determine dependencies. If the only external
96 -- dependencies are on a small set of scalar variables, then the values
97 -- of these variables are captured on entry to the subprogram, and if
98 -- the values are not changed for the call, we know immediately that
99 -- we have an infinite recursion.
101 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id);
102 -- For each actual of an in-out parameter which is a numeric conversion
103 -- of the form T(A), where A denotes a variable, we insert the declaration:
105 -- Temp : T := T (A);
107 -- prior to the call. Then we replace the actual with a reference to Temp,
108 -- and append the assignment:
110 -- A := TypeA (Temp);
112 -- after the call. Here TypeA is the actual type of variable A.
113 -- For out parameters, the initial declaration has no expression.
114 -- If A is not an entity name, we generate instead:
116 -- Var : TypeA renames A;
117 -- Temp : T := Var; -- omitting expression for out parameter.
118 -- ...
119 -- Var := TypeA (Temp);
121 -- For other in-out parameters, we emit the required constraint checks
122 -- before and/or after the call.
124 -- For all parameter modes, actuals that denote components and slices
125 -- of packed arrays are expanded into suitable temporaries.
127 procedure Expand_Inlined_Call
128 (N : Node_Id;
129 Subp : Entity_Id;
130 Orig_Subp : Entity_Id);
131 -- If called subprogram can be inlined by the front-end, retrieve the
132 -- analyzed body, replace formals with actuals and expand call in place.
133 -- Generate thunks for actuals that are expressions, and insert the
134 -- corresponding constant declarations before the call. If the original
135 -- call is to a derived operation, the return type is the one of the
136 -- derived operation, but the body is that of the original, so return
137 -- expressions in the body must be converted to the desired type (which
138 -- is simply not noted in the tree without inline expansion).
140 function Expand_Protected_Object_Reference
141 (N : Node_Id;
142 Scop : Entity_Id)
143 return Node_Id;
145 procedure Expand_Protected_Subprogram_Call
146 (N : Node_Id;
147 Subp : Entity_Id;
148 Scop : Entity_Id);
149 -- A call to a protected subprogram within the protected object may appear
150 -- as a regular call. The list of actuals must be expanded to contain a
151 -- reference to the object itself, and the call becomes a call to the
152 -- corresponding protected subprogram.
154 --------------------------------
155 -- Check_Overriding_Operation --
156 --------------------------------
158 procedure Check_Overriding_Operation (Subp : Entity_Id) is
159 Typ : constant Entity_Id := Find_Dispatching_Type (Subp);
160 Op_List : constant Elist_Id := Primitive_Operations (Typ);
161 Op_Elmt : Elmt_Id;
162 Prim_Op : Entity_Id;
163 Par_Op : Entity_Id;
165 begin
166 if Is_Derived_Type (Typ)
167 and then not Is_Private_Type (Typ)
168 and then In_Open_Scopes (Scope (Etype (Typ)))
169 and then Typ = Base_Type (Typ)
170 then
171 -- Subp overrides an inherited private operation if there is
172 -- an inherited operation with a different name than Subp (see
173 -- Derive_Subprogram) whose Alias is a hidden subprogram with
174 -- the same name as Subp.
176 Op_Elmt := First_Elmt (Op_List);
177 while Present (Op_Elmt) loop
178 Prim_Op := Node (Op_Elmt);
179 Par_Op := Alias (Prim_Op);
181 if Present (Par_Op)
182 and then not Comes_From_Source (Prim_Op)
183 and then Chars (Prim_Op) /= Chars (Par_Op)
184 and then Chars (Par_Op) = Chars (Subp)
185 and then Is_Hidden (Par_Op)
186 and then Type_Conformant (Prim_Op, Subp)
187 then
188 Set_DT_Position (Subp, DT_Position (Prim_Op));
189 end if;
191 Next_Elmt (Op_Elmt);
192 end loop;
193 end if;
194 end Check_Overriding_Operation;
196 -------------------------------
197 -- Detect_Infinite_Recursion --
198 -------------------------------
200 procedure Detect_Infinite_Recursion (N : Node_Id; Spec : Entity_Id) is
201 Loc : constant Source_Ptr := Sloc (N);
203 Var_List : constant Elist_Id := New_Elmt_List;
204 -- List of globals referenced by body of procedure
206 Call_List : constant Elist_Id := New_Elmt_List;
207 -- List of recursive calls in body of procedure
209 Shad_List : constant Elist_Id := New_Elmt_List;
210 -- List of entity id's for entities created to capture the
211 -- value of referenced globals on entry to the procedure.
213 Scop : constant Uint := Scope_Depth (Spec);
214 -- This is used to record the scope depth of the current
215 -- procedure, so that we can identify global references.
217 Max_Vars : constant := 4;
218 -- Do not test more than four global variables
220 Count_Vars : Natural := 0;
221 -- Count variables found so far
223 Var : Entity_Id;
224 Elm : Elmt_Id;
225 Ent : Entity_Id;
226 Call : Elmt_Id;
227 Decl : Node_Id;
228 Test : Node_Id;
229 Elm1 : Elmt_Id;
230 Elm2 : Elmt_Id;
231 Last : Node_Id;
233 function Process (Nod : Node_Id) return Traverse_Result;
234 -- Function to traverse the subprogram body (using Traverse_Func)
236 -------------
237 -- Process --
238 -------------
240 function Process (Nod : Node_Id) return Traverse_Result is
241 begin
242 -- Procedure call
244 if Nkind (Nod) = N_Procedure_Call_Statement then
246 -- Case of one of the detected recursive calls
248 if Is_Entity_Name (Name (Nod))
249 and then Has_Recursive_Call (Entity (Name (Nod)))
250 and then Entity (Name (Nod)) = Spec
251 then
252 Append_Elmt (Nod, Call_List);
253 return Skip;
255 -- Any other procedure call may have side effects
257 else
258 return Abandon;
259 end if;
261 -- A call to a pure function can always be ignored
263 elsif Nkind (Nod) = N_Function_Call
264 and then Is_Entity_Name (Name (Nod))
265 and then Is_Pure (Entity (Name (Nod)))
266 then
267 return Skip;
269 -- Case of an identifier reference
271 elsif Nkind (Nod) = N_Identifier then
272 Ent := Entity (Nod);
274 -- If no entity, then ignore the reference
276 -- Not clear why this can happen. To investigate, remove this
277 -- test and look at the crash that occurs here in 3401-004 ???
279 if No (Ent) then
280 return Skip;
282 -- Ignore entities with no Scope, again not clear how this
283 -- can happen, to investigate, look at 4108-008 ???
285 elsif No (Scope (Ent)) then
286 return Skip;
288 -- Ignore the reference if not to a more global object
290 elsif Scope_Depth (Scope (Ent)) >= Scop then
291 return Skip;
293 -- References to types, exceptions and constants are always OK
295 elsif Is_Type (Ent)
296 or else Ekind (Ent) = E_Exception
297 or else Ekind (Ent) = E_Constant
298 then
299 return Skip;
301 -- If other than a non-volatile scalar variable, we have some
302 -- kind of global reference (e.g. to a function) that we cannot
303 -- deal with so we forget the attempt.
305 elsif Ekind (Ent) /= E_Variable
306 or else not Is_Scalar_Type (Etype (Ent))
307 or else Treat_As_Volatile (Ent)
308 then
309 return Abandon;
311 -- Otherwise we have a reference to a global scalar
313 else
314 -- Loop through global entities already detected
316 Elm := First_Elmt (Var_List);
317 loop
318 -- If not detected before, record this new global reference
320 if No (Elm) then
321 Count_Vars := Count_Vars + 1;
323 if Count_Vars <= Max_Vars then
324 Append_Elmt (Entity (Nod), Var_List);
325 else
326 return Abandon;
327 end if;
329 exit;
331 -- If recorded before, ignore
333 elsif Node (Elm) = Entity (Nod) then
334 return Skip;
336 -- Otherwise keep looking
338 else
339 Next_Elmt (Elm);
340 end if;
341 end loop;
343 return Skip;
344 end if;
346 -- For all other node kinds, recursively visit syntactic children
348 else
349 return OK;
350 end if;
351 end Process;
353 function Traverse_Body is new Traverse_Func;
355 -- Start of processing for Detect_Infinite_Recursion
357 begin
358 -- Do not attempt detection in No_Implicit_Conditional mode,
359 -- since we won't be able to generate the code to handle the
360 -- recursion in any case.
362 if Restriction_Active (No_Implicit_Conditionals) then
363 return;
364 end if;
366 -- Otherwise do traversal and quit if we get abandon signal
368 if Traverse_Body (N) = Abandon then
369 return;
371 -- We must have a call, since Has_Recursive_Call was set. If not
372 -- just ignore (this is only an error check, so if we have a funny
373 -- situation, due to bugs or errors, we do not want to bomb!)
375 elsif Is_Empty_Elmt_List (Call_List) then
376 return;
377 end if;
379 -- Here is the case where we detect recursion at compile time
381 -- Push our current scope for analyzing the declarations and
382 -- code that we will insert for the checking.
384 New_Scope (Spec);
386 -- This loop builds temporary variables for each of the
387 -- referenced globals, so that at the end of the loop the
388 -- list Shad_List contains these temporaries in one-to-one
389 -- correspondence with the elements in Var_List.
391 Last := Empty;
392 Elm := First_Elmt (Var_List);
393 while Present (Elm) loop
394 Var := Node (Elm);
395 Ent :=
396 Make_Defining_Identifier (Loc,
397 Chars => New_Internal_Name ('S'));
398 Append_Elmt (Ent, Shad_List);
400 -- Insert a declaration for this temporary at the start of
401 -- the declarations for the procedure. The temporaries are
402 -- declared as constant objects initialized to the current
403 -- values of the corresponding temporaries.
405 Decl :=
406 Make_Object_Declaration (Loc,
407 Defining_Identifier => Ent,
408 Object_Definition => New_Occurrence_Of (Etype (Var), Loc),
409 Constant_Present => True,
410 Expression => New_Occurrence_Of (Var, Loc));
412 if No (Last) then
413 Prepend (Decl, Declarations (N));
414 else
415 Insert_After (Last, Decl);
416 end if;
418 Last := Decl;
419 Analyze (Decl);
420 Next_Elmt (Elm);
421 end loop;
423 -- Loop through calls
425 Call := First_Elmt (Call_List);
426 while Present (Call) loop
428 -- Build a predicate expression of the form
430 -- True
431 -- and then global1 = temp1
432 -- and then global2 = temp2
433 -- ...
435 -- This predicate determines if any of the global values
436 -- referenced by the procedure have changed since the
437 -- current call, if not an infinite recursion is assured.
439 Test := New_Occurrence_Of (Standard_True, Loc);
441 Elm1 := First_Elmt (Var_List);
442 Elm2 := First_Elmt (Shad_List);
443 while Present (Elm1) loop
444 Test :=
445 Make_And_Then (Loc,
446 Left_Opnd => Test,
447 Right_Opnd =>
448 Make_Op_Eq (Loc,
449 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
450 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
452 Next_Elmt (Elm1);
453 Next_Elmt (Elm2);
454 end loop;
456 -- Now we replace the call with the sequence
458 -- if no-changes (see above) then
459 -- raise Storage_Error;
460 -- else
461 -- original-call
462 -- end if;
464 Rewrite (Node (Call),
465 Make_If_Statement (Loc,
466 Condition => Test,
467 Then_Statements => New_List (
468 Make_Raise_Storage_Error (Loc,
469 Reason => SE_Infinite_Recursion)),
471 Else_Statements => New_List (
472 Relocate_Node (Node (Call)))));
474 Analyze (Node (Call));
476 Next_Elmt (Call);
477 end loop;
479 -- Remove temporary scope stack entry used for analysis
481 Pop_Scope;
482 end Detect_Infinite_Recursion;
484 --------------------
485 -- Expand_Actuals --
486 --------------------
488 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
489 Loc : constant Source_Ptr := Sloc (N);
490 Actual : Node_Id;
491 Formal : Entity_Id;
492 N_Node : Node_Id;
493 Post_Call : List_Id;
494 E_Formal : Entity_Id;
496 procedure Add_Call_By_Copy_Code;
497 -- For cases where the parameter must be passed by copy, this routine
498 -- generates a temporary variable into which the actual is copied and
499 -- then passes this as the parameter. For an OUT or IN OUT parameter,
500 -- an assignment is also generated to copy the result back. The call
501 -- also takes care of any constraint checks required for the type
502 -- conversion case (on both the way in and the way out).
504 procedure Add_Packed_Call_By_Copy_Code;
505 -- This is used when the actual involves a reference to an element
506 -- of a packed array, where we can appropriately use a simpler
507 -- approach than the full call by copy code. We just copy the value
508 -- in and out of an appropriate temporary.
510 procedure Check_Fortran_Logical;
511 -- A value of type Logical that is passed through a formal parameter
512 -- must be normalized because .TRUE. usually does not have the same
513 -- representation as True. We assume that .FALSE. = False = 0.
514 -- What about functions that return a logical type ???
516 function Make_Var (Actual : Node_Id) return Entity_Id;
517 -- Returns an entity that refers to the given actual parameter,
518 -- Actual (not including any type conversion). If Actual is an
519 -- entity name, then this entity is returned unchanged, otherwise
520 -- a renaming is created to provide an entity for the actual.
522 procedure Reset_Packed_Prefix;
523 -- The expansion of a packed array component reference is delayed in
524 -- the context of a call. Now we need to complete the expansion, so we
525 -- unmark the analyzed bits in all prefixes.
527 ---------------------------
528 -- Add_Call_By_Copy_Code --
529 ---------------------------
531 procedure Add_Call_By_Copy_Code is
532 Expr : Node_Id;
533 Init : Node_Id;
534 Temp : Entity_Id;
535 Indic : Node_Id := New_Occurrence_Of (Etype (Formal), Loc);
536 Var : Entity_Id;
537 F_Typ : constant Entity_Id := Etype (Formal);
538 V_Typ : Entity_Id;
539 Crep : Boolean;
541 begin
542 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
544 if Nkind (Actual) = N_Type_Conversion then
545 V_Typ := Etype (Expression (Actual));
547 -- If the formal is an (in-)out parameter, capture the name
548 -- of the variable in order to build the post-call assignment.
550 Var := Make_Var (Expression (Actual));
552 Crep := not Same_Representation
553 (F_Typ, Etype (Expression (Actual)));
555 else
556 V_Typ := Etype (Actual);
557 Var := Make_Var (Actual);
558 Crep := False;
559 end if;
561 -- Setup initialization for case of in out parameter, or an out
562 -- parameter where the formal is an unconstrained array (in the
563 -- latter case, we have to pass in an object with bounds).
565 -- If this is an out parameter, the initial copy is wasteful, so as
566 -- an optimization for the one-dimensional case we extract the
567 -- bounds of the actual and build an uninitialized temporary of the
568 -- right size.
570 if Ekind (Formal) = E_In_Out_Parameter
571 or else (Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ))
572 then
573 if Nkind (Actual) = N_Type_Conversion then
574 if Conversion_OK (Actual) then
575 Init := OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
576 else
577 Init := Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
578 end if;
580 elsif Ekind (Formal) = E_Out_Parameter
581 and then Is_Array_Type (F_Typ)
582 and then Number_Dimensions (F_Typ) = 1
583 and then not Has_Non_Null_Base_Init_Proc (F_Typ)
584 then
585 -- Actual is a one-dimensional array or slice, and the type
586 -- requires no initialization. Create a temporary of the
587 -- right size, but do copy actual into it (optimization).
589 Init := Empty;
590 Indic :=
591 Make_Subtype_Indication (Loc,
592 Subtype_Mark =>
593 New_Occurrence_Of (F_Typ, Loc),
594 Constraint =>
595 Make_Index_Or_Discriminant_Constraint (Loc,
596 Constraints => New_List (
597 Make_Range (Loc,
598 Low_Bound =>
599 Make_Attribute_Reference (Loc,
600 Prefix => New_Occurrence_Of (Var, Loc),
601 Attribute_name => Name_First),
602 High_Bound =>
603 Make_Attribute_Reference (Loc,
604 Prefix => New_Occurrence_Of (Var, Loc),
605 Attribute_Name => Name_Last)))));
607 else
608 Init := New_Occurrence_Of (Var, Loc);
609 end if;
611 -- An initialization is created for packed conversions as
612 -- actuals for out parameters to enable Make_Object_Declaration
613 -- to determine the proper subtype for N_Node. Note that this
614 -- is wasteful because the extra copying on the call side is
615 -- not required for such out parameters. ???
617 elsif Ekind (Formal) = E_Out_Parameter
618 and then Nkind (Actual) = N_Type_Conversion
619 and then (Is_Bit_Packed_Array (F_Typ)
620 or else
621 Is_Bit_Packed_Array (Etype (Expression (Actual))))
622 then
623 if Conversion_OK (Actual) then
624 Init :=
625 OK_Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
626 else
627 Init :=
628 Convert_To (F_Typ, New_Occurrence_Of (Var, Loc));
629 end if;
631 elsif Ekind (Formal) = E_In_Parameter then
632 Init := New_Occurrence_Of (Var, Loc);
634 else
635 Init := Empty;
636 end if;
638 N_Node :=
639 Make_Object_Declaration (Loc,
640 Defining_Identifier => Temp,
641 Object_Definition => Indic,
642 Expression => Init);
643 Set_Assignment_OK (N_Node);
644 Insert_Action (N, N_Node);
646 -- Now, normally the deal here is that we use the defining
647 -- identifier created by that object declaration. There is
648 -- one exception to this. In the change of representation case
649 -- the above declaration will end up looking like:
651 -- temp : type := identifier;
653 -- And in this case we might as well use the identifier directly
654 -- and eliminate the temporary. Note that the analysis of the
655 -- declaration was not a waste of time in that case, since it is
656 -- what generated the necessary change of representation code. If
657 -- the change of representation introduced additional code, as in
658 -- a fixed-integer conversion, the expression is not an identifier
659 -- and must be kept.
661 if Crep
662 and then Present (Expression (N_Node))
663 and then Is_Entity_Name (Expression (N_Node))
664 then
665 Temp := Entity (Expression (N_Node));
666 Rewrite (N_Node, Make_Null_Statement (Loc));
667 end if;
669 -- For IN parameter, all we do is to replace the actual
671 if Ekind (Formal) = E_In_Parameter then
672 Rewrite (Actual, New_Reference_To (Temp, Loc));
673 Analyze (Actual);
675 -- Processing for OUT or IN OUT parameter
677 else
678 -- If type conversion, use reverse conversion on exit
680 if Nkind (Actual) = N_Type_Conversion then
681 if Conversion_OK (Actual) then
682 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
683 else
684 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
685 end if;
686 else
687 Expr := New_Occurrence_Of (Temp, Loc);
688 end if;
690 Rewrite (Actual, New_Reference_To (Temp, Loc));
691 Analyze (Actual);
693 Append_To (Post_Call,
694 Make_Assignment_Statement (Loc,
695 Name => New_Occurrence_Of (Var, Loc),
696 Expression => Expr));
698 Set_Assignment_OK (Name (Last (Post_Call)));
699 end if;
700 end Add_Call_By_Copy_Code;
702 ----------------------------------
703 -- Add_Packed_Call_By_Copy_Code --
704 ----------------------------------
706 procedure Add_Packed_Call_By_Copy_Code is
707 Temp : Entity_Id;
708 Incod : Node_Id;
709 Outcod : Node_Id;
710 Lhs : Node_Id;
711 Rhs : Node_Id;
713 begin
714 Reset_Packed_Prefix;
716 -- Prepare to generate code
718 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
719 Incod := Relocate_Node (Actual);
720 Outcod := New_Copy_Tree (Incod);
722 -- Generate declaration of temporary variable, initializing it
723 -- with the input parameter unless we have an OUT variable.
725 if Ekind (Formal) = E_Out_Parameter then
726 Incod := Empty;
727 end if;
729 Insert_Action (N,
730 Make_Object_Declaration (Loc,
731 Defining_Identifier => Temp,
732 Object_Definition =>
733 New_Occurrence_Of (Etype (Formal), Loc),
734 Expression => Incod));
736 -- The actual is simply a reference to the temporary
738 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
740 -- Generate copy out if OUT or IN OUT parameter
742 if Ekind (Formal) /= E_In_Parameter then
743 Lhs := Outcod;
744 Rhs := New_Occurrence_Of (Temp, Loc);
746 -- Deal with conversion
748 if Nkind (Lhs) = N_Type_Conversion then
749 Lhs := Expression (Lhs);
750 Rhs := Convert_To (Etype (Actual), Rhs);
751 end if;
753 Append_To (Post_Call,
754 Make_Assignment_Statement (Loc,
755 Name => Lhs,
756 Expression => Rhs));
757 end if;
758 end Add_Packed_Call_By_Copy_Code;
760 ---------------------------
761 -- Check_Fortran_Logical --
762 ---------------------------
764 procedure Check_Fortran_Logical is
765 Logical : constant Entity_Id := Etype (Formal);
766 Var : Entity_Id;
768 -- Note: this is very incomplete, e.g. it does not handle arrays
769 -- of logical values. This is really not the right approach at all???)
771 begin
772 if Convention (Subp) = Convention_Fortran
773 and then Root_Type (Etype (Formal)) = Standard_Boolean
774 and then Ekind (Formal) /= E_In_Parameter
775 then
776 Var := Make_Var (Actual);
777 Append_To (Post_Call,
778 Make_Assignment_Statement (Loc,
779 Name => New_Occurrence_Of (Var, Loc),
780 Expression =>
781 Unchecked_Convert_To (
782 Logical,
783 Make_Op_Ne (Loc,
784 Left_Opnd => New_Occurrence_Of (Var, Loc),
785 Right_Opnd =>
786 Unchecked_Convert_To (
787 Logical,
788 New_Occurrence_Of (Standard_False, Loc))))));
789 end if;
790 end Check_Fortran_Logical;
792 --------------
793 -- Make_Var --
794 --------------
796 function Make_Var (Actual : Node_Id) return Entity_Id is
797 Var : Entity_Id;
799 begin
800 if Is_Entity_Name (Actual) then
801 return Entity (Actual);
803 else
804 Var := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
806 N_Node :=
807 Make_Object_Renaming_Declaration (Loc,
808 Defining_Identifier => Var,
809 Subtype_Mark =>
810 New_Occurrence_Of (Etype (Actual), Loc),
811 Name => Relocate_Node (Actual));
813 Insert_Action (N, N_Node);
814 return Var;
815 end if;
816 end Make_Var;
818 -------------------------
819 -- Reset_Packed_Prefix --
820 -------------------------
822 procedure Reset_Packed_Prefix is
823 Pfx : Node_Id := Actual;
825 begin
826 loop
827 Set_Analyzed (Pfx, False);
828 exit when Nkind (Pfx) /= N_Selected_Component
829 and then Nkind (Pfx) /= N_Indexed_Component;
830 Pfx := Prefix (Pfx);
831 end loop;
832 end Reset_Packed_Prefix;
834 -- Start of processing for Expand_Actuals
836 begin
837 Formal := First_Formal (Subp);
838 Actual := First_Actual (N);
840 Post_Call := New_List;
842 while Present (Formal) loop
843 E_Formal := Etype (Formal);
845 if Is_Scalar_Type (E_Formal)
846 or else Nkind (Actual) = N_Slice
847 then
848 Check_Fortran_Logical;
850 -- RM 6.4.1 (11)
852 elsif Ekind (Formal) /= E_Out_Parameter then
854 -- The unusual case of the current instance of a protected type
855 -- requires special handling. This can only occur in the context
856 -- of a call within the body of a protected operation.
858 if Is_Entity_Name (Actual)
859 and then Ekind (Entity (Actual)) = E_Protected_Type
860 and then In_Open_Scopes (Entity (Actual))
861 then
862 if Scope (Subp) /= Entity (Actual) then
863 Error_Msg_N ("operation outside protected type may not "
864 & "call back its protected operations?", Actual);
865 end if;
867 Rewrite (Actual,
868 Expand_Protected_Object_Reference (N, Entity (Actual)));
869 end if;
871 Apply_Constraint_Check (Actual, E_Formal);
873 -- Out parameter case. No constraint checks on access type
874 -- RM 6.4.1 (13)
876 elsif Is_Access_Type (E_Formal) then
877 null;
879 -- RM 6.4.1 (14)
881 elsif Has_Discriminants (Base_Type (E_Formal))
882 or else Has_Non_Null_Base_Init_Proc (E_Formal)
883 then
884 Apply_Constraint_Check (Actual, E_Formal);
886 -- RM 6.4.1 (15)
888 else
889 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
890 end if;
892 -- Processing for IN-OUT and OUT parameters
894 if Ekind (Formal) /= E_In_Parameter then
896 -- For type conversions of arrays, apply length/range checks
898 if Is_Array_Type (E_Formal)
899 and then Nkind (Actual) = N_Type_Conversion
900 then
901 if Is_Constrained (E_Formal) then
902 Apply_Length_Check (Expression (Actual), E_Formal);
903 else
904 Apply_Range_Check (Expression (Actual), E_Formal);
905 end if;
906 end if;
908 -- If argument is a type conversion for a type that is passed
909 -- by copy, then we must pass the parameter by copy.
911 if Nkind (Actual) = N_Type_Conversion
912 and then
913 (Is_Numeric_Type (E_Formal)
914 or else Is_Access_Type (E_Formal)
915 or else Is_Enumeration_Type (E_Formal)
916 or else Is_Bit_Packed_Array (Etype (Formal))
917 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
919 -- Also pass by copy if change of representation
921 or else not Same_Representation
922 (Etype (Formal),
923 Etype (Expression (Actual))))
924 then
925 Add_Call_By_Copy_Code;
927 -- References to components of bit packed arrays are expanded
928 -- at this point, rather than at the point of analysis of the
929 -- actuals, to handle the expansion of the assignment to
930 -- [in] out parameters.
932 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
933 Add_Packed_Call_By_Copy_Code;
935 -- References to slices of bit packed arrays are expanded
937 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
938 Add_Call_By_Copy_Code;
940 -- References to possibly unaligned slices of arrays are expanded
942 elsif Is_Possibly_Unaligned_Slice (Actual) then
943 Add_Call_By_Copy_Code;
945 -- Deal with access types where the actual subtpe and the
946 -- formal subtype are not the same, requiring a check.
948 -- It is necessary to exclude tagged types because of "downward
949 -- conversion" errors and a strange assertion error in namet
950 -- from gnatf in bug 1215-001 ???
952 elsif Is_Access_Type (E_Formal)
953 and then not Same_Type (E_Formal, Etype (Actual))
954 and then not Is_Tagged_Type (Designated_Type (E_Formal))
955 then
956 Add_Call_By_Copy_Code;
958 -- If the actual is not a scalar and is marked for volatile
959 -- treatment, whereas the formal is not volatile, then pass
960 -- by copy unless it is a by-reference type.
962 elsif Is_Entity_Name (Actual)
963 and then Treat_As_Volatile (Entity (Actual))
964 and then not Is_By_Reference_Type (Etype (Actual))
965 and then not Is_Scalar_Type (Etype (Entity (Actual)))
966 and then not Treat_As_Volatile (E_Formal)
967 then
968 Add_Call_By_Copy_Code;
970 elsif Nkind (Actual) = N_Indexed_Component
971 and then Is_Entity_Name (Prefix (Actual))
972 and then Has_Volatile_Components (Entity (Prefix (Actual)))
973 then
974 Add_Call_By_Copy_Code;
975 end if;
977 -- Processing for IN parameters
979 else
980 -- For IN parameters is in the packed array case, we expand an
981 -- indexed component (the circuit in Exp_Ch4 deliberately left
982 -- indexed components appearing as actuals untouched, so that
983 -- the special processing above for the OUT and IN OUT cases
984 -- could be performed. We could make the test in Exp_Ch4 more
985 -- complex and have it detect the parameter mode, but it is
986 -- easier simply to handle all cases here.
988 if Nkind (Actual) = N_Indexed_Component
989 and then Is_Packed (Etype (Prefix (Actual)))
990 then
991 Reset_Packed_Prefix;
992 Expand_Packed_Element_Reference (Actual);
994 -- If we have a reference to a bit packed array, we copy it,
995 -- since the actual must be byte aligned.
997 -- Is this really necessary in all cases???
999 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1000 Add_Packed_Call_By_Copy_Code;
1002 -- Similarly, we have to expand slices of packed arrays here
1003 -- because the result must be byte aligned.
1005 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1006 Add_Call_By_Copy_Code;
1008 -- Only processing remaining is to pass by copy if this is a
1009 -- reference to a possibly unaligned slice, since the caller
1010 -- expects an appropriately aligned argument.
1012 elsif Is_Possibly_Unaligned_Slice (Actual) then
1013 Add_Call_By_Copy_Code;
1014 end if;
1015 end if;
1017 Next_Formal (Formal);
1018 Next_Actual (Actual);
1019 end loop;
1021 -- Find right place to put post call stuff if it is present
1023 if not Is_Empty_List (Post_Call) then
1025 -- If call is not a list member, it must be the triggering
1026 -- statement of a triggering alternative or an entry call
1027 -- alternative, and we can add the post call stuff to the
1028 -- corresponding statement list.
1030 if not Is_List_Member (N) then
1031 declare
1032 P : constant Node_Id := Parent (N);
1034 begin
1035 pragma Assert (Nkind (P) = N_Triggering_Alternative
1036 or else Nkind (P) = N_Entry_Call_Alternative);
1038 if Is_Non_Empty_List (Statements (P)) then
1039 Insert_List_Before_And_Analyze
1040 (First (Statements (P)), Post_Call);
1041 else
1042 Set_Statements (P, Post_Call);
1043 end if;
1044 end;
1046 -- Otherwise, normal case where N is in a statement sequence,
1047 -- just put the post-call stuff after the call statement.
1049 else
1050 Insert_Actions_After (N, Post_Call);
1051 end if;
1052 end if;
1054 -- The call node itself is re-analyzed in Expand_Call.
1056 end Expand_Actuals;
1058 -----------------
1059 -- Expand_Call --
1060 -----------------
1062 -- This procedure handles expansion of function calls and procedure call
1063 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1064 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1066 -- Replace call to Raise_Exception by Raise_Exception always if possible
1067 -- Provide values of actuals for all formals in Extra_Formals list
1068 -- Replace "call" to enumeration literal function by literal itself
1069 -- Rewrite call to predefined operator as operator
1070 -- Replace actuals to in-out parameters that are numeric conversions,
1071 -- with explicit assignment to temporaries before and after the call.
1072 -- Remove optional actuals if First_Optional_Parameter specified.
1074 -- Note that the list of actuals has been filled with default expressions
1075 -- during semantic analysis of the call. Only the extra actuals required
1076 -- for the 'Constrained attribute and for accessibility checks are added
1077 -- at this point.
1079 procedure Expand_Call (N : Node_Id) is
1080 Loc : constant Source_Ptr := Sloc (N);
1081 Remote : constant Boolean := Is_Remote_Call (N);
1082 Subp : Entity_Id;
1083 Orig_Subp : Entity_Id := Empty;
1084 Parent_Subp : Entity_Id;
1085 Parent_Formal : Entity_Id;
1086 Actual : Node_Id;
1087 Formal : Entity_Id;
1088 Prev : Node_Id := Empty;
1089 Prev_Orig : Node_Id;
1090 Scop : Entity_Id;
1091 Extra_Actuals : List_Id := No_List;
1092 Cond : Node_Id;
1094 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1095 -- Adds one entry to the end of the actual parameter list. Used for
1096 -- default parameters and for extra actuals (for Extra_Formals).
1097 -- The argument is an N_Parameter_Association node.
1099 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1100 -- Adds an extra actual to the list of extra actuals. Expr
1101 -- is the expression for the value of the actual, EF is the
1102 -- entity for the extra formal.
1104 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1105 -- Within an instance, a type derived from a non-tagged formal derived
1106 -- type inherits from the original parent, not from the actual. This is
1107 -- tested in 4723-003. The current derivation mechanism has the derived
1108 -- type inherit from the actual, which is only correct outside of the
1109 -- instance. If the subprogram is inherited, we test for this particular
1110 -- case through a convoluted tree traversal before setting the proper
1111 -- subprogram to be called.
1113 --------------------------
1114 -- Add_Actual_Parameter --
1115 --------------------------
1117 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1118 Actual_Expr : constant Node_Id :=
1119 Explicit_Actual_Parameter (Insert_Param);
1121 begin
1122 -- Case of insertion is first named actual
1124 if No (Prev) or else
1125 Nkind (Parent (Prev)) /= N_Parameter_Association
1126 then
1127 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1128 Set_First_Named_Actual (N, Actual_Expr);
1130 if No (Prev) then
1131 if not Present (Parameter_Associations (N)) then
1132 Set_Parameter_Associations (N, New_List);
1133 Append (Insert_Param, Parameter_Associations (N));
1134 end if;
1135 else
1136 Insert_After (Prev, Insert_Param);
1137 end if;
1139 -- Case of insertion is not first named actual
1141 else
1142 Set_Next_Named_Actual
1143 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1144 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1145 Append (Insert_Param, Parameter_Associations (N));
1146 end if;
1148 Prev := Actual_Expr;
1149 end Add_Actual_Parameter;
1151 ----------------------
1152 -- Add_Extra_Actual --
1153 ----------------------
1155 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1156 Loc : constant Source_Ptr := Sloc (Expr);
1158 begin
1159 if Extra_Actuals = No_List then
1160 Extra_Actuals := New_List;
1161 Set_Parent (Extra_Actuals, N);
1162 end if;
1164 Append_To (Extra_Actuals,
1165 Make_Parameter_Association (Loc,
1166 Explicit_Actual_Parameter => Expr,
1167 Selector_Name =>
1168 Make_Identifier (Loc, Chars (EF))));
1170 Analyze_And_Resolve (Expr, Etype (EF));
1171 end Add_Extra_Actual;
1173 ---------------------------
1174 -- Inherited_From_Formal --
1175 ---------------------------
1177 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1178 Par : Entity_Id;
1179 Gen_Par : Entity_Id;
1180 Gen_Prim : Elist_Id;
1181 Elmt : Elmt_Id;
1182 Indic : Node_Id;
1184 begin
1185 -- If the operation is inherited, it is attached to the corresponding
1186 -- type derivation. If the parent in the derivation is a generic
1187 -- actual, it is a subtype of the actual, and we have to recover the
1188 -- original derived type declaration to find the proper parent.
1190 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1191 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1192 or else Nkind (Type_Definition (Original_Node (Parent (S))))
1193 /= N_Derived_Type_Definition
1194 or else not In_Instance
1195 then
1196 return Empty;
1198 else
1199 Indic :=
1200 (Subtype_Indication
1201 (Type_Definition (Original_Node (Parent (S)))));
1203 if Nkind (Indic) = N_Subtype_Indication then
1204 Par := Entity (Subtype_Mark (Indic));
1205 else
1206 Par := Entity (Indic);
1207 end if;
1208 end if;
1210 if not Is_Generic_Actual_Type (Par)
1211 or else Is_Tagged_Type (Par)
1212 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1213 or else not In_Open_Scopes (Scope (Par))
1214 then
1215 return Empty;
1217 else
1218 Gen_Par := Generic_Parent_Type (Parent (Par));
1219 end if;
1221 -- If the generic parent type is still the generic type, this
1222 -- is a private formal, not a derived formal, and there are no
1223 -- operations inherited from the formal.
1225 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1226 return Empty;
1227 end if;
1229 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1230 Elmt := First_Elmt (Gen_Prim);
1232 while Present (Elmt) loop
1233 if Chars (Node (Elmt)) = Chars (S) then
1234 declare
1235 F1 : Entity_Id;
1236 F2 : Entity_Id;
1237 begin
1239 F1 := First_Formal (S);
1240 F2 := First_Formal (Node (Elmt));
1242 while Present (F1)
1243 and then Present (F2)
1244 loop
1246 if Etype (F1) = Etype (F2)
1247 or else Etype (F2) = Gen_Par
1248 then
1249 Next_Formal (F1);
1250 Next_Formal (F2);
1251 else
1252 Next_Elmt (Elmt);
1253 exit; -- not the right subprogram
1254 end if;
1256 return Node (Elmt);
1257 end loop;
1258 end;
1260 else
1261 Next_Elmt (Elmt);
1262 end if;
1263 end loop;
1265 raise Program_Error;
1266 end Inherited_From_Formal;
1268 -- Start of processing for Expand_Call
1270 begin
1271 -- Ignore if previous error
1273 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1274 return;
1275 end if;
1277 -- Call using access to subprogram with explicit dereference
1279 if Nkind (Name (N)) = N_Explicit_Dereference then
1280 Subp := Etype (Name (N));
1281 Parent_Subp := Empty;
1283 -- Case of call to simple entry, where the Name is a selected component
1284 -- whose prefix is the task, and whose selector name is the entry name
1286 elsif Nkind (Name (N)) = N_Selected_Component then
1287 Subp := Entity (Selector_Name (Name (N)));
1288 Parent_Subp := Empty;
1290 -- Case of call to member of entry family, where Name is an indexed
1291 -- component, with the prefix being a selected component giving the
1292 -- task and entry family name, and the index being the entry index.
1294 elsif Nkind (Name (N)) = N_Indexed_Component then
1295 Subp := Entity (Selector_Name (Prefix (Name (N))));
1296 Parent_Subp := Empty;
1298 -- Normal case
1300 else
1301 Subp := Entity (Name (N));
1302 Parent_Subp := Alias (Subp);
1304 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1305 -- if we can tell that the first parameter cannot possibly be null.
1306 -- This helps optimization and also generation of warnings.
1308 if not Restriction_Active (No_Exception_Handlers)
1309 and then Is_RTE (Subp, RE_Raise_Exception)
1310 then
1311 declare
1312 FA : constant Node_Id := Original_Node (First_Actual (N));
1314 begin
1315 -- The case we catch is where the first argument is obtained
1316 -- using the Identity attribute (which must always be non-null)
1318 if Nkind (FA) = N_Attribute_Reference
1319 and then Attribute_Name (FA) = Name_Identity
1320 then
1321 Subp := RTE (RE_Raise_Exception_Always);
1322 Set_Entity (Name (N), Subp);
1323 end if;
1324 end;
1325 end if;
1327 if Ekind (Subp) = E_Entry then
1328 Parent_Subp := Empty;
1329 end if;
1330 end if;
1332 -- First step, compute extra actuals, corresponding to any
1333 -- Extra_Formals present. Note that we do not access Extra_Formals
1334 -- directly, instead we simply note the presence of the extra
1335 -- formals as we process the regular formals and collect the
1336 -- corresponding actuals in Extra_Actuals.
1338 -- We also generate any required range checks for actuals as we go
1339 -- through the loop, since this is a convenient place to do this.
1341 Formal := First_Formal (Subp);
1342 Actual := First_Actual (N);
1343 while Present (Formal) loop
1345 -- Generate range check if required (not activated yet ???)
1347 -- if Do_Range_Check (Actual) then
1348 -- Set_Do_Range_Check (Actual, False);
1349 -- Generate_Range_Check
1350 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
1351 -- end if;
1353 -- Prepare to examine current entry
1355 Prev := Actual;
1356 Prev_Orig := Original_Node (Prev);
1358 -- Create possible extra actual for constrained case. Usually,
1359 -- the extra actual is of the form actual'constrained, but since
1360 -- this attribute is only available for unconstrained records,
1361 -- TRUE is expanded if the type of the formal happens to be
1362 -- constrained (for instance when this procedure is inherited
1363 -- from an unconstrained record to a constrained one) or if the
1364 -- actual has no discriminant (its type is constrained). An
1365 -- exception to this is the case of a private type without
1366 -- discriminants. In this case we pass FALSE because the
1367 -- object has underlying discriminants with defaults.
1369 if Present (Extra_Constrained (Formal)) then
1370 if Ekind (Etype (Prev)) in Private_Kind
1371 and then not Has_Discriminants (Base_Type (Etype (Prev)))
1372 then
1373 Add_Extra_Actual (
1374 New_Occurrence_Of (Standard_False, Loc),
1375 Extra_Constrained (Formal));
1377 elsif Is_Constrained (Etype (Formal))
1378 or else not Has_Discriminants (Etype (Prev))
1379 then
1380 Add_Extra_Actual (
1381 New_Occurrence_Of (Standard_True, Loc),
1382 Extra_Constrained (Formal));
1384 -- Do not produce extra actuals for Unchecked_Union parameters.
1385 -- Jump directly to the end of the loop.
1387 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
1388 goto Skip_Extra_Actual_Generation;
1390 else
1391 -- If the actual is a type conversion, then the constrained
1392 -- test applies to the actual, not the target type.
1394 declare
1395 Act_Prev : Node_Id := Prev;
1397 begin
1398 -- Test for unchecked conversions as well, which can
1399 -- occur as out parameter actuals on calls to stream
1400 -- procedures.
1402 while Nkind (Act_Prev) = N_Type_Conversion
1403 or else Nkind (Act_Prev) = N_Unchecked_Type_Conversion
1404 loop
1405 Act_Prev := Expression (Act_Prev);
1406 end loop;
1408 Add_Extra_Actual (
1409 Make_Attribute_Reference (Sloc (Prev),
1410 Prefix =>
1411 Duplicate_Subexpr_No_Checks
1412 (Act_Prev, Name_Req => True),
1413 Attribute_Name => Name_Constrained),
1414 Extra_Constrained (Formal));
1415 end;
1416 end if;
1417 end if;
1419 -- Create possible extra actual for accessibility level
1421 if Present (Extra_Accessibility (Formal)) then
1422 if Is_Entity_Name (Prev_Orig) then
1424 -- When passing an access parameter as the actual to another
1425 -- access parameter we need to pass along the actual's own
1426 -- associated access level parameter. This is done if we are
1427 -- in the scope of the formal access parameter (if this is an
1428 -- inlined body the extra formal is irrelevant).
1430 if Ekind (Entity (Prev_Orig)) in Formal_Kind
1431 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
1432 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
1433 then
1434 declare
1435 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
1437 begin
1438 pragma Assert (Present (Parm_Ent));
1440 if Present (Extra_Accessibility (Parm_Ent)) then
1441 Add_Extra_Actual (
1442 New_Occurrence_Of
1443 (Extra_Accessibility (Parm_Ent), Loc),
1444 Extra_Accessibility (Formal));
1446 -- If the actual access parameter does not have an
1447 -- associated extra formal providing its scope level,
1448 -- then treat the actual as having library-level
1449 -- accessibility.
1451 else
1452 Add_Extra_Actual (
1453 Make_Integer_Literal (Loc,
1454 Intval => Scope_Depth (Standard_Standard)),
1455 Extra_Accessibility (Formal));
1456 end if;
1457 end;
1459 -- The actual is a normal access value, so just pass the
1460 -- level of the actual's access type.
1462 else
1463 Add_Extra_Actual (
1464 Make_Integer_Literal (Loc,
1465 Intval => Type_Access_Level (Etype (Prev_Orig))),
1466 Extra_Accessibility (Formal));
1467 end if;
1469 else
1470 case Nkind (Prev_Orig) is
1472 when N_Attribute_Reference =>
1474 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
1476 -- For X'Access, pass on the level of the prefix X
1478 when Attribute_Access =>
1479 Add_Extra_Actual (
1480 Make_Integer_Literal (Loc,
1481 Intval =>
1482 Object_Access_Level (Prefix (Prev_Orig))),
1483 Extra_Accessibility (Formal));
1485 -- Treat the unchecked attributes as library-level
1487 when Attribute_Unchecked_Access |
1488 Attribute_Unrestricted_Access =>
1489 Add_Extra_Actual (
1490 Make_Integer_Literal (Loc,
1491 Intval => Scope_Depth (Standard_Standard)),
1492 Extra_Accessibility (Formal));
1494 -- No other cases of attributes returning access
1495 -- values that can be passed to access parameters
1497 when others =>
1498 raise Program_Error;
1500 end case;
1502 -- For allocators we pass the level of the execution of
1503 -- the called subprogram, which is one greater than the
1504 -- current scope level.
1506 when N_Allocator =>
1507 Add_Extra_Actual (
1508 Make_Integer_Literal (Loc,
1509 Scope_Depth (Current_Scope) + 1),
1510 Extra_Accessibility (Formal));
1512 -- For other cases we simply pass the level of the
1513 -- actual's access type.
1515 when others =>
1516 Add_Extra_Actual (
1517 Make_Integer_Literal (Loc,
1518 Intval => Type_Access_Level (Etype (Prev_Orig))),
1519 Extra_Accessibility (Formal));
1521 end case;
1522 end if;
1523 end if;
1525 -- Perform the check of 4.6(49) that prevents a null value
1526 -- from being passed as an actual to an access parameter.
1527 -- Note that the check is elided in the common cases of
1528 -- passing an access attribute or access parameter as an
1529 -- actual. Also, we currently don't enforce this check for
1530 -- expander-generated actuals and when -gnatdj is set.
1532 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
1533 or else Access_Checks_Suppressed (Subp)
1534 then
1535 null;
1537 elsif Debug_Flag_J then
1538 null;
1540 elsif not Comes_From_Source (Prev) then
1541 null;
1543 elsif Is_Entity_Name (Prev)
1544 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
1545 then
1546 null;
1548 elsif Nkind (Prev) = N_Allocator
1549 or else Nkind (Prev) = N_Attribute_Reference
1550 then
1551 null;
1553 -- Suppress null checks when passing to access parameters
1554 -- of Java subprograms. (Should this be done for other
1555 -- foreign conventions as well ???)
1557 elsif Convention (Subp) = Convention_Java then
1558 null;
1560 -- Ada 2005 (AI-231): do not force the check in case of Ada 2005
1561 -- unless it is a null-excluding type
1563 elsif Ada_Version < Ada_05
1564 or else Can_Never_Be_Null (Etype (Prev))
1565 then
1566 Cond :=
1567 Make_Op_Eq (Loc,
1568 Left_Opnd => Duplicate_Subexpr_No_Checks (Prev),
1569 Right_Opnd => Make_Null (Loc));
1570 Insert_Action (Prev,
1571 Make_Raise_Constraint_Error (Loc,
1572 Condition => Cond,
1573 Reason => CE_Access_Parameter_Is_Null));
1574 end if;
1576 -- Perform appropriate validity checks on parameters that
1577 -- are entities.
1579 if Validity_Checks_On then
1580 if (Ekind (Formal) = E_In_Parameter
1581 and then Validity_Check_In_Params)
1582 or else
1583 (Ekind (Formal) = E_In_Out_Parameter
1584 and then Validity_Check_In_Out_Params)
1585 then
1586 -- If the actual is an indexed component of a packed
1587 -- type, it has not been expanded yet. It will be
1588 -- copied in the validity code that follows, and has
1589 -- to be expanded appropriately, so reanalyze it.
1591 if Nkind (Actual) = N_Indexed_Component then
1592 Set_Analyzed (Actual, False);
1593 end if;
1595 Ensure_Valid (Actual);
1596 end if;
1597 end if;
1599 -- For IN OUT and OUT parameters, ensure that subscripts are valid
1600 -- since this is a left side reference. We only do this for calls
1601 -- from the source program since we assume that compiler generated
1602 -- calls explicitly generate any required checks. We also need it
1603 -- only if we are doing standard validity checks, since clearly it
1604 -- is not needed if validity checks are off, and in subscript
1605 -- validity checking mode, all indexed components are checked with
1606 -- a call directly from Expand_N_Indexed_Component.
1608 if Comes_From_Source (N)
1609 and then Ekind (Formal) /= E_In_Parameter
1610 and then Validity_Checks_On
1611 and then Validity_Check_Default
1612 and then not Validity_Check_Subscripts
1613 then
1614 Check_Valid_Lvalue_Subscripts (Actual);
1615 end if;
1617 -- Mark any scalar OUT parameter that is a simple variable
1618 -- as no longer known to be valid (unless the type is always
1619 -- valid). This reflects the fact that if an OUT parameter
1620 -- is never set in a procedure, then it can become invalid
1621 -- on return from the procedure.
1623 if Ekind (Formal) = E_Out_Parameter
1624 and then Is_Entity_Name (Actual)
1625 and then Ekind (Entity (Actual)) = E_Variable
1626 and then not Is_Known_Valid (Etype (Actual))
1627 then
1628 Set_Is_Known_Valid (Entity (Actual), False);
1629 end if;
1631 -- For an OUT or IN OUT parameter of an access type, if the
1632 -- actual is an entity, then it is no longer known to be non-null.
1634 if Ekind (Formal) /= E_In_Parameter
1635 and then Is_Entity_Name (Actual)
1636 and then Is_Access_Type (Etype (Actual))
1637 then
1638 Set_Is_Known_Non_Null (Entity (Actual), False);
1639 end if;
1641 -- If the formal is class wide and the actual is an aggregate, force
1642 -- evaluation so that the back end who does not know about class-wide
1643 -- type, does not generate a temporary of the wrong size.
1645 if not Is_Class_Wide_Type (Etype (Formal)) then
1646 null;
1648 elsif Nkind (Actual) = N_Aggregate
1649 or else (Nkind (Actual) = N_Qualified_Expression
1650 and then Nkind (Expression (Actual)) = N_Aggregate)
1651 then
1652 Force_Evaluation (Actual);
1653 end if;
1655 -- In a remote call, if the formal is of a class-wide type, check
1656 -- that the actual meets the requirements described in E.4(18).
1658 if Remote
1659 and then Is_Class_Wide_Type (Etype (Formal))
1660 then
1661 Insert_Action (Actual,
1662 Make_Implicit_If_Statement (N,
1663 Condition =>
1664 Make_Op_Not (Loc,
1665 Get_Remotely_Callable
1666 (Duplicate_Subexpr_Move_Checks (Actual))),
1667 Then_Statements => New_List (
1668 Make_Raise_Program_Error (Loc,
1669 Reason => PE_Illegal_RACW_E_4_18))));
1670 end if;
1672 -- This label is required when skipping extra actual generation for
1673 -- Unchecked_Union parameters.
1675 <<Skip_Extra_Actual_Generation>>
1677 Next_Actual (Actual);
1678 Next_Formal (Formal);
1679 end loop;
1681 -- If we are expanding a rhs of an assignement we need to check if
1682 -- tag propagation is needed. This code belongs theorically in Analyze
1683 -- Assignment but has to be done earlier (bottom-up) because the
1684 -- assignment might be transformed into a declaration for an uncons-
1685 -- trained value, if the expression is classwide.
1687 if Nkind (N) = N_Function_Call
1688 and then Is_Tag_Indeterminate (N)
1689 and then Is_Entity_Name (Name (N))
1690 then
1691 declare
1692 Ass : Node_Id := Empty;
1694 begin
1695 if Nkind (Parent (N)) = N_Assignment_Statement then
1696 Ass := Parent (N);
1698 elsif Nkind (Parent (N)) = N_Qualified_Expression
1699 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
1700 then
1701 Ass := Parent (Parent (N));
1702 end if;
1704 if Present (Ass)
1705 and then Is_Class_Wide_Type (Etype (Name (Ass)))
1706 then
1707 if Etype (N) /= Root_Type (Etype (Name (Ass))) then
1708 Error_Msg_NE
1709 ("tag-indeterminate expression must have type&"
1710 & "('R'M 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
1711 else
1712 Propagate_Tag (Name (Ass), N);
1713 end if;
1715 -- The call will be rewritten as a dispatching call, and
1716 -- expanded as such.
1718 return;
1719 end if;
1720 end;
1721 end if;
1723 -- Deals with Dispatch_Call if we still have a call, before expanding
1724 -- extra actuals since this will be done on the re-analysis of the
1725 -- dispatching call. Note that we do not try to shorten the actual
1726 -- list for a dispatching call, it would not make sense to do so.
1727 -- Expansion of dispatching calls is suppressed when Java_VM, because
1728 -- the JVM back end directly handles the generation of dispatching
1729 -- calls and would have to undo any expansion to an indirect call.
1731 if (Nkind (N) = N_Function_Call
1732 or else Nkind (N) = N_Procedure_Call_Statement)
1733 and then Present (Controlling_Argument (N))
1734 and then not Java_VM
1735 then
1736 Expand_Dispatch_Call (N);
1738 -- The following return is worrisome. Is it really OK to
1739 -- skip all remaining processing in this procedure ???
1741 return;
1743 -- Similarly, expand calls to RCI subprograms on which pragma
1744 -- All_Calls_Remote applies. The rewriting will be reanalyzed
1745 -- later. Do this only when the call comes from source since we do
1746 -- not want such a rewritting to occur in expanded code.
1748 elsif Is_All_Remote_Call (N) then
1749 Expand_All_Calls_Remote_Subprogram_Call (N);
1751 -- Similarly, do not add extra actuals for an entry call whose entity
1752 -- is a protected procedure, or for an internal protected subprogram
1753 -- call, because it will be rewritten as a protected subprogram call
1754 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
1756 elsif Is_Protected_Type (Scope (Subp))
1757 and then (Ekind (Subp) = E_Procedure
1758 or else Ekind (Subp) = E_Function)
1759 then
1760 null;
1762 -- During that loop we gathered the extra actuals (the ones that
1763 -- correspond to Extra_Formals), so now they can be appended.
1765 else
1766 while Is_Non_Empty_List (Extra_Actuals) loop
1767 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
1768 end loop;
1769 end if;
1771 if Ekind (Subp) = E_Procedure
1772 or else (Ekind (Subp) = E_Subprogram_Type
1773 and then Etype (Subp) = Standard_Void_Type)
1774 or else Is_Entry (Subp)
1775 then
1776 Expand_Actuals (N, Subp);
1777 end if;
1779 -- If the subprogram is a renaming, or if it is inherited, replace it
1780 -- in the call with the name of the actual subprogram being called.
1781 -- If this is a dispatching call, the run-time decides what to call.
1782 -- The Alias attribute does not apply to entries.
1784 if Nkind (N) /= N_Entry_Call_Statement
1785 and then No (Controlling_Argument (N))
1786 and then Present (Parent_Subp)
1787 then
1788 if Present (Inherited_From_Formal (Subp)) then
1789 Parent_Subp := Inherited_From_Formal (Subp);
1790 else
1791 while Present (Alias (Parent_Subp)) loop
1792 Parent_Subp := Alias (Parent_Subp);
1793 end loop;
1794 end if;
1796 Set_Entity (Name (N), Parent_Subp);
1798 if Is_Abstract (Parent_Subp)
1799 and then not In_Instance
1800 then
1801 Error_Msg_NE
1802 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
1803 end if;
1805 -- Add an explicit conversion for parameter of the derived type.
1806 -- This is only done for scalar and access in-parameters. Others
1807 -- have been expanded in expand_actuals.
1809 Formal := First_Formal (Subp);
1810 Parent_Formal := First_Formal (Parent_Subp);
1811 Actual := First_Actual (N);
1813 -- It is not clear that conversion is needed for intrinsic
1814 -- subprograms, but it certainly is for those that are user-
1815 -- defined, and that can be inherited on derivation, namely
1816 -- unchecked conversion and deallocation.
1817 -- General case needs study ???
1819 if not Is_Intrinsic_Subprogram (Parent_Subp)
1820 or else Is_Generic_Instance (Parent_Subp)
1821 then
1822 while Present (Formal) loop
1824 if Etype (Formal) /= Etype (Parent_Formal)
1825 and then Is_Scalar_Type (Etype (Formal))
1826 and then Ekind (Formal) = E_In_Parameter
1827 and then not Raises_Constraint_Error (Actual)
1828 then
1829 Rewrite (Actual,
1830 OK_Convert_To (Etype (Parent_Formal),
1831 Relocate_Node (Actual)));
1833 Analyze (Actual);
1834 Resolve (Actual, Etype (Parent_Formal));
1835 Enable_Range_Check (Actual);
1837 elsif Is_Access_Type (Etype (Formal))
1838 and then Base_Type (Etype (Parent_Formal))
1839 /= Base_Type (Etype (Actual))
1840 then
1841 if Ekind (Formal) /= E_In_Parameter then
1842 Rewrite (Actual,
1843 Convert_To (Etype (Parent_Formal),
1844 Relocate_Node (Actual)));
1846 Analyze (Actual);
1847 Resolve (Actual, Etype (Parent_Formal));
1849 elsif
1850 Ekind (Etype (Parent_Formal)) = E_Anonymous_Access_Type
1851 and then Designated_Type (Etype (Parent_Formal))
1853 Designated_Type (Etype (Actual))
1854 and then not Is_Controlling_Formal (Formal)
1855 then
1856 -- This unchecked conversion is not necessary unless
1857 -- inlining is enabled, because in that case the type
1858 -- mismatch may become visible in the body about to be
1859 -- inlined.
1861 Rewrite (Actual,
1862 Unchecked_Convert_To (Etype (Parent_Formal),
1863 Relocate_Node (Actual)));
1865 Analyze (Actual);
1866 Resolve (Actual, Etype (Parent_Formal));
1867 end if;
1868 end if;
1870 Next_Formal (Formal);
1871 Next_Formal (Parent_Formal);
1872 Next_Actual (Actual);
1873 end loop;
1874 end if;
1876 Orig_Subp := Subp;
1877 Subp := Parent_Subp;
1878 end if;
1880 -- Check for violation of No_Abort_Statements
1882 if Is_RTE (Subp, RE_Abort_Task) then
1883 Check_Restriction (No_Abort_Statements, N);
1885 -- Check for violation of No_Dynamic_Attachment
1887 elsif RTU_Loaded (Ada_Interrupts)
1888 and then (Is_RTE (Subp, RE_Is_Reserved) or else
1889 Is_RTE (Subp, RE_Is_Attached) or else
1890 Is_RTE (Subp, RE_Current_Handler) or else
1891 Is_RTE (Subp, RE_Attach_Handler) or else
1892 Is_RTE (Subp, RE_Exchange_Handler) or else
1893 Is_RTE (Subp, RE_Detach_Handler) or else
1894 Is_RTE (Subp, RE_Reference))
1895 then
1896 Check_Restriction (No_Dynamic_Attachment, N);
1897 end if;
1899 -- Deal with case where call is an explicit dereference
1901 if Nkind (Name (N)) = N_Explicit_Dereference then
1903 -- Handle case of access to protected subprogram type
1905 if Ekind (Base_Type (Etype (Prefix (Name (N))))) =
1906 E_Access_Protected_Subprogram_Type
1907 then
1908 -- If this is a call through an access to protected operation,
1909 -- the prefix has the form (object'address, operation'access).
1910 -- Rewrite as a for other protected calls: the object is the
1911 -- first parameter of the list of actuals.
1913 declare
1914 Call : Node_Id;
1915 Parm : List_Id;
1916 Nam : Node_Id;
1917 Obj : Node_Id;
1918 Ptr : constant Node_Id := Prefix (Name (N));
1920 T : constant Entity_Id :=
1921 Equivalent_Type (Base_Type (Etype (Ptr)));
1923 D_T : constant Entity_Id :=
1924 Designated_Type (Base_Type (Etype (Ptr)));
1926 begin
1927 Obj := Make_Selected_Component (Loc,
1928 Prefix => Unchecked_Convert_To (T, Ptr),
1929 Selector_Name => New_Occurrence_Of (First_Entity (T), Loc));
1931 Nam := Make_Selected_Component (Loc,
1932 Prefix => Unchecked_Convert_To (T, Ptr),
1933 Selector_Name => New_Occurrence_Of (
1934 Next_Entity (First_Entity (T)), Loc));
1936 Nam := Make_Explicit_Dereference (Loc, Nam);
1938 if Present (Parameter_Associations (N)) then
1939 Parm := Parameter_Associations (N);
1940 else
1941 Parm := New_List;
1942 end if;
1944 Prepend (Obj, Parm);
1946 if Etype (D_T) = Standard_Void_Type then
1947 Call := Make_Procedure_Call_Statement (Loc,
1948 Name => Nam,
1949 Parameter_Associations => Parm);
1950 else
1951 Call := Make_Function_Call (Loc,
1952 Name => Nam,
1953 Parameter_Associations => Parm);
1954 end if;
1956 Set_First_Named_Actual (Call, First_Named_Actual (N));
1957 Set_Etype (Call, Etype (D_T));
1959 -- We do not re-analyze the call to avoid infinite recursion.
1960 -- We analyze separately the prefix and the object, and set
1961 -- the checks on the prefix that would otherwise be emitted
1962 -- when resolving a call.
1964 Rewrite (N, Call);
1965 Analyze (Nam);
1966 Apply_Access_Check (Nam);
1967 Analyze (Obj);
1968 return;
1969 end;
1970 end if;
1971 end if;
1973 -- If this is a call to an intrinsic subprogram, then perform the
1974 -- appropriate expansion to the corresponding tree node and we
1975 -- are all done (since after that the call is gone!)
1977 if Is_Intrinsic_Subprogram (Subp) then
1978 Expand_Intrinsic_Call (N, Subp);
1979 return;
1980 end if;
1982 if Ekind (Subp) = E_Function
1983 or else Ekind (Subp) = E_Procedure
1984 then
1985 if Is_Inlined (Subp) then
1987 Inlined_Subprogram : declare
1988 Bod : Node_Id;
1989 Must_Inline : Boolean := False;
1990 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
1991 Scop : constant Entity_Id := Scope (Subp);
1993 function In_Unfrozen_Instance return Boolean;
1994 -- If the subprogram comes from an instance in the same
1995 -- unit, and the instance is not yet frozen, inlining might
1996 -- trigger order-of-elaboration problems in gigi.
1998 --------------------------
1999 -- In_Unfrozen_Instance --
2000 --------------------------
2002 function In_Unfrozen_Instance return Boolean is
2003 S : Entity_Id := Scop;
2005 begin
2006 while Present (S)
2007 and then S /= Standard_Standard
2008 loop
2009 if Is_Generic_Instance (S)
2010 and then Present (Freeze_Node (S))
2011 and then not Analyzed (Freeze_Node (S))
2012 then
2013 return True;
2014 end if;
2016 S := Scope (S);
2017 end loop;
2019 return False;
2020 end In_Unfrozen_Instance;
2022 -- Start of processing for Inlined_Subprogram
2024 begin
2025 -- Verify that the body to inline has already been seen,
2026 -- and that if the body is in the current unit the inlining
2027 -- does not occur earlier. This avoids order-of-elaboration
2028 -- problems in gigi.
2030 if No (Spec)
2031 or else Nkind (Spec) /= N_Subprogram_Declaration
2032 or else No (Body_To_Inline (Spec))
2033 then
2034 Must_Inline := False;
2036 -- If this an inherited function that returns a private
2037 -- type, do not inline if the full view is an unconstrained
2038 -- array, because such calls cannot be inlined.
2040 elsif Present (Orig_Subp)
2041 and then Is_Array_Type (Etype (Orig_Subp))
2042 and then not Is_Constrained (Etype (Orig_Subp))
2043 then
2044 Must_Inline := False;
2046 elsif In_Unfrozen_Instance then
2047 Must_Inline := False;
2049 else
2050 Bod := Body_To_Inline (Spec);
2052 if (In_Extended_Main_Code_Unit (N)
2053 or else In_Extended_Main_Code_Unit (Parent (N))
2054 or else Is_Always_Inlined (Subp))
2055 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
2056 or else
2057 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
2058 then
2059 Must_Inline := True;
2061 -- If we are compiling a package body that is not the main
2062 -- unit, it must be for inlining/instantiation purposes,
2063 -- in which case we inline the call to insure that the same
2064 -- temporaries are generated when compiling the body by
2065 -- itself. Otherwise link errors can occur.
2067 -- If the function being called is itself in the main unit,
2068 -- we cannot inline, because there is a risk of double
2069 -- elaboration and/or circularity: the inlining can make
2070 -- visible a private entity in the body of the main unit,
2071 -- that gigi will see before its sees its proper definition.
2073 elsif not (In_Extended_Main_Code_Unit (N))
2074 and then In_Package_Body
2075 then
2076 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
2077 end if;
2078 end if;
2080 if Must_Inline then
2081 Expand_Inlined_Call (N, Subp, Orig_Subp);
2083 else
2084 -- Let the back end handle it
2086 Add_Inlined_Body (Subp);
2088 if Front_End_Inlining
2089 and then Nkind (Spec) = N_Subprogram_Declaration
2090 and then (In_Extended_Main_Code_Unit (N))
2091 and then No (Body_To_Inline (Spec))
2092 and then not Has_Completion (Subp)
2093 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
2094 then
2095 Cannot_Inline
2096 ("cannot inline& (body not seen yet)?",
2097 N, Subp);
2098 end if;
2099 end if;
2100 end Inlined_Subprogram;
2101 end if;
2102 end if;
2104 -- Check for a protected subprogram. This is either an intra-object
2105 -- call, or a protected function call. Protected procedure calls are
2106 -- rewritten as entry calls and handled accordingly.
2108 Scop := Scope (Subp);
2110 if Nkind (N) /= N_Entry_Call_Statement
2111 and then Is_Protected_Type (Scop)
2112 then
2113 -- If the call is an internal one, it is rewritten as a call to
2114 -- to the corresponding unprotected subprogram.
2116 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2117 end if;
2119 -- Functions returning controlled objects need special attention
2121 if Controlled_Type (Etype (Subp))
2122 and then not Is_Return_By_Reference_Type (Etype (Subp))
2123 then
2124 Expand_Ctrl_Function_Call (N);
2125 end if;
2127 -- Test for First_Optional_Parameter, and if so, truncate parameter
2128 -- list if there are optional parameters at the trailing end.
2129 -- Note we never delete procedures for call via a pointer.
2131 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2132 and then Present (First_Optional_Parameter (Subp))
2133 then
2134 declare
2135 Last_Keep_Arg : Node_Id;
2137 begin
2138 -- Last_Keep_Arg will hold the last actual that should be
2139 -- retained. If it remains empty at the end, it means that
2140 -- all parameters are optional.
2142 Last_Keep_Arg := Empty;
2144 -- Find first optional parameter, must be present since we
2145 -- checked the validity of the parameter before setting it.
2147 Formal := First_Formal (Subp);
2148 Actual := First_Actual (N);
2149 while Formal /= First_Optional_Parameter (Subp) loop
2150 Last_Keep_Arg := Actual;
2151 Next_Formal (Formal);
2152 Next_Actual (Actual);
2153 end loop;
2155 -- We have Formal and Actual pointing to the first potentially
2156 -- droppable argument. We can drop all the trailing arguments
2157 -- whose actual matches the default. Note that we know that all
2158 -- remaining formals have defaults, because we checked that this
2159 -- requirement was met before setting First_Optional_Parameter.
2161 -- We use Fully_Conformant_Expressions to check for identity
2162 -- between formals and actuals, which may miss some cases, but
2163 -- on the other hand, this is only an optimization (if we fail
2164 -- to truncate a parameter it does not affect functionality).
2165 -- So if the default is 3 and the actual is 1+2, we consider
2166 -- them unequal, which hardly seems worrisome.
2168 while Present (Formal) loop
2169 if not Fully_Conformant_Expressions
2170 (Actual, Default_Value (Formal))
2171 then
2172 Last_Keep_Arg := Actual;
2173 end if;
2175 Next_Formal (Formal);
2176 Next_Actual (Actual);
2177 end loop;
2179 -- If no arguments, delete entire list, this is the easy case
2181 if No (Last_Keep_Arg) then
2182 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2183 Delete_Tree (Remove_Head (Parameter_Associations (N)));
2184 end loop;
2186 Set_Parameter_Associations (N, No_List);
2187 Set_First_Named_Actual (N, Empty);
2189 -- Case where at the last retained argument is positional. This
2190 -- is also an easy case, since the retained arguments are already
2191 -- in the right form, and we don't need to worry about the order
2192 -- of arguments that get eliminated.
2194 elsif Is_List_Member (Last_Keep_Arg) then
2195 while Present (Next (Last_Keep_Arg)) loop
2196 Delete_Tree (Remove_Next (Last_Keep_Arg));
2197 end loop;
2199 Set_First_Named_Actual (N, Empty);
2201 -- This is the annoying case where the last retained argument
2202 -- is a named parameter. Since the original arguments are not
2203 -- in declaration order, we may have to delete some fairly
2204 -- random collection of arguments.
2206 else
2207 declare
2208 Temp : Node_Id;
2209 Passoc : Node_Id;
2211 Discard : Node_Id;
2212 pragma Warnings (Off, Discard);
2214 begin
2215 -- First step, remove all the named parameters from the
2216 -- list (they are still chained using First_Named_Actual
2217 -- and Next_Named_Actual, so we have not lost them!)
2219 Temp := First (Parameter_Associations (N));
2221 -- Case of all parameters named, remove them all
2223 if Nkind (Temp) = N_Parameter_Association then
2224 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2225 Temp := Remove_Head (Parameter_Associations (N));
2226 end loop;
2228 -- Case of mixed positional/named, remove named parameters
2230 else
2231 while Nkind (Next (Temp)) /= N_Parameter_Association loop
2232 Next (Temp);
2233 end loop;
2235 while Present (Next (Temp)) loop
2236 Discard := Remove_Next (Temp);
2237 end loop;
2238 end if;
2240 -- Now we loop through the named parameters, till we get
2241 -- to the last one to be retained, adding them to the list.
2242 -- Note that the Next_Named_Actual list does not need to be
2243 -- touched since we are only reordering them on the actual
2244 -- parameter association list.
2246 Passoc := Parent (First_Named_Actual (N));
2247 loop
2248 Temp := Relocate_Node (Passoc);
2249 Append_To
2250 (Parameter_Associations (N), Temp);
2251 exit when
2252 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
2253 Passoc := Parent (Next_Named_Actual (Passoc));
2254 end loop;
2256 Set_Next_Named_Actual (Temp, Empty);
2258 loop
2259 Temp := Next_Named_Actual (Passoc);
2260 exit when No (Temp);
2261 Set_Next_Named_Actual
2262 (Passoc, Next_Named_Actual (Parent (Temp)));
2263 Delete_Tree (Temp);
2264 end loop;
2265 end;
2266 end if;
2267 end;
2268 end if;
2269 end Expand_Call;
2271 --------------------------
2272 -- Expand_Inlined_Call --
2273 --------------------------
2275 procedure Expand_Inlined_Call
2276 (N : Node_Id;
2277 Subp : Entity_Id;
2278 Orig_Subp : Entity_Id)
2280 Loc : constant Source_Ptr := Sloc (N);
2281 Is_Predef : constant Boolean :=
2282 Is_Predefined_File_Name
2283 (Unit_File_Name (Get_Source_Unit (Subp)));
2284 Orig_Bod : constant Node_Id :=
2285 Body_To_Inline (Unit_Declaration_Node (Subp));
2287 Blk : Node_Id;
2288 Bod : Node_Id;
2289 Decl : Node_Id;
2290 Exit_Lab : Entity_Id := Empty;
2291 F : Entity_Id;
2292 A : Node_Id;
2293 Lab_Decl : Node_Id;
2294 Lab_Id : Node_Id;
2295 New_A : Node_Id;
2296 Num_Ret : Int := 0;
2297 Ret_Type : Entity_Id;
2298 Targ : Node_Id;
2299 Temp : Entity_Id;
2300 Temp_Typ : Entity_Id;
2302 procedure Make_Exit_Label;
2303 -- Build declaration for exit label to be used in Return statements.
2305 function Process_Formals (N : Node_Id) return Traverse_Result;
2306 -- Replace occurrence of a formal with the corresponding actual, or
2307 -- the thunk generated for it.
2309 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2310 -- If the call being expanded is that of an internal subprogram,
2311 -- set the sloc of the generated block to that of the call itself,
2312 -- so that the expansion is skipped by the -next- command in gdb.
2313 -- Same processing for a subprogram in a predefined file, e.g.
2314 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2315 -- to simplify our own development.
2317 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2318 -- If the function body is a single expression, replace call with
2319 -- expression, else insert block appropriately.
2321 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2322 -- If procedure body has no local variables, inline body without
2323 -- creating block, otherwise rewrite call with block.
2325 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2326 -- Determine whether a formal parameter is used only once in Orig_Bod
2328 ---------------------
2329 -- Make_Exit_Label --
2330 ---------------------
2332 procedure Make_Exit_Label is
2333 begin
2334 -- Create exit label for subprogram, if one doesn't exist yet.
2336 if No (Exit_Lab) then
2337 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
2338 Set_Entity (Lab_Id,
2339 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
2340 Exit_Lab := Make_Label (Loc, Lab_Id);
2342 Lab_Decl :=
2343 Make_Implicit_Label_Declaration (Loc,
2344 Defining_Identifier => Entity (Lab_Id),
2345 Label_Construct => Exit_Lab);
2346 end if;
2347 end Make_Exit_Label;
2349 ---------------------
2350 -- Process_Formals --
2351 ---------------------
2353 function Process_Formals (N : Node_Id) return Traverse_Result is
2354 A : Entity_Id;
2355 E : Entity_Id;
2356 Ret : Node_Id;
2358 begin
2359 if Is_Entity_Name (N)
2360 and then Present (Entity (N))
2361 then
2362 E := Entity (N);
2364 if Is_Formal (E)
2365 and then Scope (E) = Subp
2366 then
2367 A := Renamed_Object (E);
2369 if Is_Entity_Name (A) then
2370 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2372 elsif Nkind (A) = N_Defining_Identifier then
2373 Rewrite (N, New_Occurrence_Of (A, Loc));
2375 else -- numeric literal
2376 Rewrite (N, New_Copy (A));
2377 end if;
2378 end if;
2380 return Skip;
2382 elsif Nkind (N) = N_Return_Statement then
2384 if No (Expression (N)) then
2385 Make_Exit_Label;
2386 Rewrite (N, Make_Goto_Statement (Loc,
2387 Name => New_Copy (Lab_Id)));
2389 else
2390 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2391 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2392 then
2393 -- Function body is a single expression. No need for
2394 -- exit label.
2396 null;
2398 else
2399 Num_Ret := Num_Ret + 1;
2400 Make_Exit_Label;
2401 end if;
2403 -- Because of the presence of private types, the views of the
2404 -- expression and the context may be different, so place an
2405 -- unchecked conversion to the context type to avoid spurious
2406 -- errors, eg. when the expression is a numeric literal and
2407 -- the context is private. If the expression is an aggregate,
2408 -- use a qualified expression, because an aggregate is not a
2409 -- legal argument of a conversion.
2411 if Nkind (Expression (N)) = N_Aggregate
2412 or else Nkind (Expression (N)) = N_Null
2413 then
2414 Ret :=
2415 Make_Qualified_Expression (Sloc (N),
2416 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2417 Expression => Relocate_Node (Expression (N)));
2418 else
2419 Ret :=
2420 Unchecked_Convert_To
2421 (Ret_Type, Relocate_Node (Expression (N)));
2422 end if;
2424 if Nkind (Targ) = N_Defining_Identifier then
2425 Rewrite (N,
2426 Make_Assignment_Statement (Loc,
2427 Name => New_Occurrence_Of (Targ, Loc),
2428 Expression => Ret));
2429 else
2430 Rewrite (N,
2431 Make_Assignment_Statement (Loc,
2432 Name => New_Copy (Targ),
2433 Expression => Ret));
2434 end if;
2436 Set_Assignment_OK (Name (N));
2438 if Present (Exit_Lab) then
2439 Insert_After (N,
2440 Make_Goto_Statement (Loc,
2441 Name => New_Copy (Lab_Id)));
2442 end if;
2443 end if;
2445 return OK;
2447 -- Remove pragma Unreferenced since it may refer to formals that
2448 -- are not visible in the inlined body, and in any case we will
2449 -- not be posting warnings on the inlined body so it is unneeded.
2451 elsif Nkind (N) = N_Pragma
2452 and then Chars (N) = Name_Unreferenced
2453 then
2454 Rewrite (N, Make_Null_Statement (Sloc (N)));
2455 return OK;
2457 else
2458 return OK;
2459 end if;
2460 end Process_Formals;
2462 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2464 ------------------
2465 -- Process_Sloc --
2466 ------------------
2468 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2469 begin
2470 if not Debug_Generated_Code then
2471 Set_Sloc (Nod, Sloc (N));
2472 Set_Comes_From_Source (Nod, False);
2473 end if;
2475 return OK;
2476 end Process_Sloc;
2478 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2480 ---------------------------
2481 -- Rewrite_Function_Call --
2482 ---------------------------
2484 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2485 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2486 Fst : constant Node_Id := First (Statements (HSS));
2488 begin
2489 -- Optimize simple case: function body is a single return statement,
2490 -- which has been expanded into an assignment.
2492 if Is_Empty_List (Declarations (Blk))
2493 and then Nkind (Fst) = N_Assignment_Statement
2494 and then No (Next (Fst))
2495 then
2497 -- The function call may have been rewritten as the temporary
2498 -- that holds the result of the call, in which case remove the
2499 -- now useless declaration.
2501 if Nkind (N) = N_Identifier
2502 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2503 then
2504 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2505 end if;
2507 Rewrite (N, Expression (Fst));
2509 elsif Nkind (N) = N_Identifier
2510 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2511 then
2513 -- The block assigns the result of the call to the temporary.
2515 Insert_After (Parent (Entity (N)), Blk);
2517 elsif Nkind (Parent (N)) = N_Assignment_Statement
2518 and then Is_Entity_Name (Name (Parent (N)))
2519 then
2521 -- Replace assignment with the block
2523 declare
2524 Original_Assignment : constant Node_Id := Parent (N);
2526 begin
2527 -- Preserve the original assignment node to keep the
2528 -- complete assignment subtree consistent enough for
2529 -- Analyze_Assignment to proceed (specifically, the
2530 -- original Lhs node must still have an assignment
2531 -- statement as its parent).
2533 -- We cannot rely on Original_Node to go back from the
2534 -- block node to the assignment node, because the
2535 -- assignment might already be a rewrite substitution.
2537 Discard_Node (Relocate_Node (Original_Assignment));
2538 Rewrite (Original_Assignment, Blk);
2539 end;
2541 elsif Nkind (Parent (N)) = N_Object_Declaration then
2542 Set_Expression (Parent (N), Empty);
2543 Insert_After (Parent (N), Blk);
2544 end if;
2545 end Rewrite_Function_Call;
2547 ----------------------------
2548 -- Rewrite_Procedure_Call --
2549 ----------------------------
2551 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2552 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2553 begin
2554 if Is_Empty_List (Declarations (Blk)) then
2555 Insert_List_After (N, Statements (HSS));
2556 Rewrite (N, Make_Null_Statement (Loc));
2557 else
2558 Rewrite (N, Blk);
2559 end if;
2560 end Rewrite_Procedure_Call;
2562 -------------------------
2563 -- Formal_Is_Used_Once --
2564 ------------------------
2566 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2567 Use_Counter : Int := 0;
2569 function Count_Uses (N : Node_Id) return Traverse_Result;
2570 -- Traverse the tree and count the uses of the formal parameter.
2571 -- In this case, for optimization purposes, we do not need to
2572 -- continue the traversal once more than one use is encountered.
2574 ----------------
2575 -- Count_Uses --
2576 ----------------
2578 function Count_Uses (N : Node_Id) return Traverse_Result is
2579 begin
2580 -- The original node is an identifier
2582 if Nkind (N) = N_Identifier
2583 and then Present (Entity (N))
2585 -- The original node's entity points to the one in the
2586 -- copied body.
2588 and then Nkind (Entity (N)) = N_Identifier
2589 and then Present (Entity (Entity (N)))
2591 -- The entity of the copied node is the formal parameter
2593 and then Entity (Entity (N)) = Formal
2594 then
2595 Use_Counter := Use_Counter + 1;
2597 if Use_Counter > 1 then
2599 -- Denote more than one use and abandon the traversal
2601 Use_Counter := 2;
2602 return Abandon;
2604 end if;
2605 end if;
2607 return OK;
2608 end Count_Uses;
2610 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2612 -- Start of processing for Formal_Is_Used_Once
2614 begin
2615 Count_Formal_Uses (Orig_Bod);
2616 return Use_Counter = 1;
2617 end Formal_Is_Used_Once;
2619 -- Start of processing for Expand_Inlined_Call
2621 begin
2622 -- Check for special case of To_Address call, and if so, just
2623 -- do an unchecked conversion instead of expanding the call.
2624 -- Not only is this more efficient, but it also avoids a
2625 -- problem with order of elaboration when address clauses
2626 -- are inlined (address expr elaborated at wrong point).
2628 if Subp = RTE (RE_To_Address) then
2629 Rewrite (N,
2630 Unchecked_Convert_To
2631 (RTE (RE_Address),
2632 Relocate_Node (First_Actual (N))));
2633 return;
2634 end if;
2636 if Nkind (Orig_Bod) = N_Defining_Identifier then
2638 -- Subprogram is a renaming_as_body. Calls appearing after the
2639 -- renaming can be replaced with calls to the renamed entity
2640 -- directly, because the subprograms are subtype conformant.
2642 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2643 return;
2644 end if;
2646 -- Use generic machinery to copy body of inlined subprogram, as if it
2647 -- were an instantiation, resetting source locations appropriately, so
2648 -- that nested inlined calls appear in the main unit.
2650 Save_Env (Subp, Empty);
2651 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2653 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2654 Blk :=
2655 Make_Block_Statement (Loc,
2656 Declarations => Declarations (Bod),
2657 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
2659 if No (Declarations (Bod)) then
2660 Set_Declarations (Blk, New_List);
2661 end if;
2663 -- If this is a derived function, establish the proper return type.
2665 if Present (Orig_Subp)
2666 and then Orig_Subp /= Subp
2667 then
2668 Ret_Type := Etype (Orig_Subp);
2669 else
2670 Ret_Type := Etype (Subp);
2671 end if;
2673 F := First_Formal (Subp);
2674 A := First_Actual (N);
2676 -- Create temporaries for the actuals that are expressions, or that
2677 -- are scalars and require copying to preserve semantics.
2679 while Present (F) loop
2680 if Present (Renamed_Object (F)) then
2681 Error_Msg_N (" cannot inline call to recursive subprogram", N);
2682 return;
2683 end if;
2685 -- If the argument may be a controlling argument in a call within
2686 -- the inlined body, we must preserve its classwide nature to
2687 -- insure that dynamic dispatching take place subsequently.
2688 -- If the formal has a constraint it must be preserved to retain
2689 -- the semantics of the body.
2691 if Is_Class_Wide_Type (Etype (F))
2692 or else (Is_Access_Type (Etype (F))
2693 and then
2694 Is_Class_Wide_Type (Designated_Type (Etype (F))))
2695 then
2696 Temp_Typ := Etype (F);
2698 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
2699 and then Etype (F) /= Base_Type (Etype (F))
2700 then
2701 Temp_Typ := Etype (F);
2703 else
2704 Temp_Typ := Etype (A);
2705 end if;
2707 -- If the actual is a simple name or a literal, no need to
2708 -- create a temporary, object can be used directly.
2710 if (Is_Entity_Name (A)
2711 and then
2712 (not Is_Scalar_Type (Etype (A))
2713 or else Ekind (Entity (A)) = E_Enumeration_Literal))
2715 -- When the actual is an identifier and the corresponding formal
2716 -- is used only once in the original body, the formal can be
2717 -- substituted directly with the actual parameter.
2719 or else (Nkind (A) = N_Identifier
2720 and then Formal_Is_Used_Once (F))
2722 or else Nkind (A) = N_Real_Literal
2723 or else Nkind (A) = N_Integer_Literal
2724 or else Nkind (A) = N_Character_Literal
2725 then
2726 if Etype (F) /= Etype (A) then
2727 Set_Renamed_Object
2728 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
2729 else
2730 Set_Renamed_Object (F, A);
2731 end if;
2733 else
2734 Temp :=
2735 Make_Defining_Identifier (Loc,
2736 Chars => New_Internal_Name ('C'));
2738 -- If the actual for an in/in-out parameter is a view conversion,
2739 -- make it into an unchecked conversion, given that an untagged
2740 -- type conversion is not a proper object for a renaming.
2742 -- In-out conversions that involve real conversions have already
2743 -- been transformed in Expand_Actuals.
2745 if Nkind (A) = N_Type_Conversion
2746 and then Ekind (F) /= E_In_Parameter
2747 then
2748 New_A := Make_Unchecked_Type_Conversion (Loc,
2749 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
2750 Expression => Relocate_Node (Expression (A)));
2752 elsif Etype (F) /= Etype (A) then
2753 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
2754 Temp_Typ := Etype (F);
2756 else
2757 New_A := Relocate_Node (A);
2758 end if;
2760 Set_Sloc (New_A, Sloc (N));
2762 if Ekind (F) = E_In_Parameter
2763 and then not Is_Limited_Type (Etype (A))
2764 then
2765 Decl :=
2766 Make_Object_Declaration (Loc,
2767 Defining_Identifier => Temp,
2768 Constant_Present => True,
2769 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
2770 Expression => New_A);
2771 else
2772 Decl :=
2773 Make_Object_Renaming_Declaration (Loc,
2774 Defining_Identifier => Temp,
2775 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
2776 Name => New_A);
2777 end if;
2779 Prepend (Decl, Declarations (Blk));
2780 Set_Renamed_Object (F, Temp);
2781 end if;
2783 Next_Formal (F);
2784 Next_Actual (A);
2785 end loop;
2787 -- Establish target of function call. If context is not assignment or
2788 -- declaration, create a temporary as a target. The declaration for
2789 -- the temporary may be subsequently optimized away if the body is a
2790 -- single expression, or if the left-hand side of the assignment is
2791 -- simple enough.
2793 if Ekind (Subp) = E_Function then
2794 if Nkind (Parent (N)) = N_Assignment_Statement
2795 and then Is_Entity_Name (Name (Parent (N)))
2796 then
2797 Targ := Name (Parent (N));
2799 else
2800 -- Replace call with temporary, and create its declaration.
2802 Temp :=
2803 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2805 Decl :=
2806 Make_Object_Declaration (Loc,
2807 Defining_Identifier => Temp,
2808 Object_Definition =>
2809 New_Occurrence_Of (Ret_Type, Loc));
2811 Set_No_Initialization (Decl);
2812 Insert_Action (N, Decl);
2813 Rewrite (N, New_Occurrence_Of (Temp, Loc));
2814 Targ := Temp;
2815 end if;
2816 end if;
2818 -- Traverse the tree and replace formals with actuals or their thunks.
2819 -- Attach block to tree before analysis and rewriting.
2821 Replace_Formals (Blk);
2822 Set_Parent (Blk, N);
2824 if not Comes_From_Source (Subp)
2825 or else Is_Predef
2826 then
2827 Reset_Slocs (Blk);
2828 end if;
2830 if Present (Exit_Lab) then
2832 -- If the body was a single expression, the single return statement
2833 -- and the corresponding label are useless.
2835 if Num_Ret = 1
2836 and then
2837 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
2838 N_Goto_Statement
2839 then
2840 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
2841 else
2842 Append (Lab_Decl, (Declarations (Blk)));
2843 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
2844 end if;
2845 end if;
2847 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
2848 -- conflicting private views that Gigi would ignore. If this is a
2849 -- predefined unit, analyze with checks off, as is done in the non-
2850 -- inlined run-time units.
2852 declare
2853 I_Flag : constant Boolean := In_Inlined_Body;
2855 begin
2856 In_Inlined_Body := True;
2858 if Is_Predef then
2859 declare
2860 Style : constant Boolean := Style_Check;
2861 begin
2862 Style_Check := False;
2863 Analyze (Blk, Suppress => All_Checks);
2864 Style_Check := Style;
2865 end;
2867 else
2868 Analyze (Blk);
2869 end if;
2871 In_Inlined_Body := I_Flag;
2872 end;
2874 if Ekind (Subp) = E_Procedure then
2875 Rewrite_Procedure_Call (N, Blk);
2876 else
2877 Rewrite_Function_Call (N, Blk);
2878 end if;
2880 Restore_Env;
2882 -- Cleanup mapping between formals and actuals, for other expansions.
2884 F := First_Formal (Subp);
2886 while Present (F) loop
2887 Set_Renamed_Object (F, Empty);
2888 Next_Formal (F);
2889 end loop;
2890 end Expand_Inlined_Call;
2892 ----------------------------
2893 -- Expand_N_Function_Call --
2894 ----------------------------
2896 procedure Expand_N_Function_Call (N : Node_Id) is
2897 Typ : constant Entity_Id := Etype (N);
2899 function Returned_By_Reference return Boolean;
2900 -- If the return type is returned through the secondary stack. that is
2901 -- by reference, we don't want to create a temp to force stack checking.
2902 -- Shouldn't this function be moved to exp_util???
2904 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean;
2905 -- If the call is the right side of an assignment or the expression in
2906 -- an object declaration, we don't need to create a temp as the left
2907 -- side will already trigger stack checking if necessary.
2909 ---------------------------
2910 -- Returned_By_Reference --
2911 ---------------------------
2913 function Returned_By_Reference return Boolean is
2914 S : Entity_Id := Current_Scope;
2916 begin
2917 if Is_Return_By_Reference_Type (Typ) then
2918 return True;
2920 elsif Nkind (Parent (N)) /= N_Return_Statement then
2921 return False;
2923 elsif Requires_Transient_Scope (Typ) then
2925 -- Verify that the return type of the enclosing function has
2926 -- the same constrained status as that of the expression.
2928 while Ekind (S) /= E_Function loop
2929 S := Scope (S);
2930 end loop;
2932 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
2933 else
2934 return False;
2935 end if;
2936 end Returned_By_Reference;
2938 ---------------------------
2939 -- Rhs_Of_Assign_Or_Decl --
2940 ---------------------------
2942 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean is
2943 begin
2944 if (Nkind (Parent (N)) = N_Assignment_Statement
2945 and then Expression (Parent (N)) = N)
2946 or else
2947 (Nkind (Parent (N)) = N_Qualified_Expression
2948 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2949 and then Expression (Parent (Parent (N))) = Parent (N))
2950 or else
2951 (Nkind (Parent (N)) = N_Object_Declaration
2952 and then Expression (Parent (N)) = N)
2953 or else
2954 (Nkind (Parent (N)) = N_Component_Association
2955 and then Expression (Parent (N)) = N
2956 and then Nkind (Parent (Parent (N))) = N_Aggregate
2957 and then Rhs_Of_Assign_Or_Decl (Parent (Parent (N))))
2958 then
2959 return True;
2960 else
2961 return False;
2962 end if;
2963 end Rhs_Of_Assign_Or_Decl;
2965 -- Start of processing for Expand_N_Function_Call
2967 begin
2968 -- A special check. If stack checking is enabled, and the return type
2969 -- might generate a large temporary, and the call is not the right
2970 -- side of an assignment, then generate an explicit temporary. We do
2971 -- this because otherwise gigi may generate a large temporary on the
2972 -- fly and this can cause trouble with stack checking.
2974 -- This is unecessary if the call is the expression in an object
2975 -- declaration, or if it appears outside of any library unit. This
2976 -- can only happen if it appears as an actual in a library-level
2977 -- instance, in which case a temporary will be generated for it once
2978 -- the instance itself is installed.
2980 if May_Generate_Large_Temp (Typ)
2981 and then not Rhs_Of_Assign_Or_Decl (N)
2982 and then not Returned_By_Reference
2983 and then Current_Scope /= Standard_Standard
2984 then
2985 if Stack_Checking_Enabled then
2987 -- Note: it might be thought that it would be OK to use a call
2988 -- to Force_Evaluation here, but that's not good enough, because
2989 -- that can results in a 'Reference construct that may still
2990 -- need a temporary.
2992 declare
2993 Loc : constant Source_Ptr := Sloc (N);
2994 Temp_Obj : constant Entity_Id :=
2995 Make_Defining_Identifier (Loc,
2996 Chars => New_Internal_Name ('F'));
2997 Temp_Typ : Entity_Id := Typ;
2998 Decl : Node_Id;
2999 A : Node_Id;
3000 F : Entity_Id;
3001 Proc : Entity_Id;
3003 begin
3004 if Is_Tagged_Type (Typ)
3005 and then Present (Controlling_Argument (N))
3006 then
3007 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
3008 and then Nkind (Parent (N)) /= N_Function_Call
3009 then
3010 -- If this is a tag-indeterminate call, the object must
3011 -- be classwide.
3013 if Is_Tag_Indeterminate (N) then
3014 Temp_Typ := Class_Wide_Type (Typ);
3015 end if;
3017 else
3018 -- If this is a dispatching call that is itself the
3019 -- controlling argument of an enclosing call, the
3020 -- nominal subtype of the object that replaces it must
3021 -- be classwide, so that dispatching will take place
3022 -- properly. If it is not a controlling argument, the
3023 -- object is not classwide.
3025 Proc := Entity (Name (Parent (N)));
3026 F := First_Formal (Proc);
3027 A := First_Actual (Parent (N));
3029 while A /= N loop
3030 Next_Formal (F);
3031 Next_Actual (A);
3032 end loop;
3034 if Is_Controlling_Formal (F) then
3035 Temp_Typ := Class_Wide_Type (Typ);
3036 end if;
3037 end if;
3038 end if;
3040 Decl :=
3041 Make_Object_Declaration (Loc,
3042 Defining_Identifier => Temp_Obj,
3043 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3044 Constant_Present => True,
3045 Expression => Relocate_Node (N));
3046 Set_Assignment_OK (Decl);
3048 Insert_Actions (N, New_List (Decl));
3049 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
3050 end;
3052 else
3053 -- If stack-checking is not enabled, increment serial number
3054 -- for internal names, so that subsequent symbols are consistent
3055 -- with and without stack-checking.
3057 Synchronize_Serial_Number;
3059 -- Now we can expand the call with consistent symbol names
3061 Expand_Call (N);
3062 end if;
3064 -- Normal case, expand the call
3066 else
3067 Expand_Call (N);
3068 end if;
3069 end Expand_N_Function_Call;
3071 ---------------------------------------
3072 -- Expand_N_Procedure_Call_Statement --
3073 ---------------------------------------
3075 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
3076 begin
3077 Expand_Call (N);
3078 end Expand_N_Procedure_Call_Statement;
3080 ------------------------------
3081 -- Expand_N_Subprogram_Body --
3082 ------------------------------
3084 -- Add poll call if ATC polling is enabled, unless the body will be
3085 -- inlined by the back-end.
3087 -- Add return statement if last statement in body is not a return
3088 -- statement (this makes things easier on Gigi which does not want
3089 -- to have to handle a missing return).
3091 -- Add call to Activate_Tasks if body is a task activator
3093 -- Deal with possible detection of infinite recursion
3095 -- Eliminate body completely if convention stubbed
3097 -- Encode entity names within body, since we will not need to reference
3098 -- these entities any longer in the front end.
3100 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
3102 -- Reset Pure indication if any parameter has root type System.Address
3104 -- Wrap thread body
3106 procedure Expand_N_Subprogram_Body (N : Node_Id) is
3107 Loc : constant Source_Ptr := Sloc (N);
3108 H : constant Node_Id := Handled_Statement_Sequence (N);
3109 Body_Id : Entity_Id;
3110 Spec_Id : Entity_Id;
3111 Except_H : Node_Id;
3112 Scop : Entity_Id;
3113 Dec : Node_Id;
3114 Next_Op : Node_Id;
3115 L : List_Id;
3117 procedure Add_Return (S : List_Id);
3118 -- Append a return statement to the statement sequence S if the last
3119 -- statement is not already a return or a goto statement. Note that
3120 -- the latter test is not critical, it does not matter if we add a
3121 -- few extra returns, since they get eliminated anyway later on.
3123 procedure Expand_Thread_Body;
3124 -- Perform required expansion of a thread body
3126 ----------------
3127 -- Add_Return --
3128 ----------------
3130 procedure Add_Return (S : List_Id) is
3131 begin
3132 if not Is_Transfer (Last (S)) then
3134 -- The source location for the return is the end label
3135 -- of the procedure in all cases. This is a bit odd when
3136 -- there are exception handlers, but not much else we can do.
3138 Append_To (S, Make_Return_Statement (Sloc (End_Label (H))));
3139 end if;
3140 end Add_Return;
3142 ------------------------
3143 -- Expand_Thread_Body --
3144 ------------------------
3146 -- The required expansion of a thread body is as follows
3148 -- procedure <thread body procedure name> is
3150 -- _Secondary_Stack : aliased
3151 -- Storage_Elements.Storage_Array
3152 -- (1 .. Storage_Offset (Sec_Stack_Size));
3153 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
3155 -- _Process_ATSD : aliased System.Threads.ATSD;
3157 -- begin
3158 -- System.Threads.Thread_Body_Enter;
3159 -- (_Secondary_Stack'Address,
3160 -- _Secondary_Stack'Length,
3161 -- _Process_ATSD'Address);
3163 -- declare
3164 -- <user declarations>
3165 -- begin
3166 -- <user statements>
3167 -- <user exception handlers>
3168 -- end;
3170 -- System.Threads.Thread_Body_Leave;
3172 -- exception
3173 -- when E : others =>
3174 -- System.Threads.Thread_Body_Exceptional_Exit (E);
3175 -- end;
3177 -- Note the exception handler is omitted if pragma Restriction
3178 -- No_Exception_Handlers is currently active.
3180 procedure Expand_Thread_Body is
3181 User_Decls : constant List_Id := Declarations (N);
3182 Sec_Stack_Len : Node_Id;
3184 TB_Pragma : constant Node_Id :=
3185 Get_Rep_Pragma (Spec_Id, Name_Thread_Body);
3187 Ent_SS : Entity_Id;
3188 Ent_ATSD : Entity_Id;
3189 Ent_EO : Entity_Id;
3191 Decl_SS : Node_Id;
3192 Decl_ATSD : Node_Id;
3194 Excep_Handlers : List_Id;
3196 begin
3197 New_Scope (Spec_Id);
3199 -- Get proper setting for secondary stack size
3201 if List_Length (Pragma_Argument_Associations (TB_Pragma)) = 2 then
3202 Sec_Stack_Len :=
3203 Expression (Last (Pragma_Argument_Associations (TB_Pragma)));
3204 else
3205 Sec_Stack_Len :=
3206 New_Occurrence_Of (RTE (RE_Default_Secondary_Stack_Size), Loc);
3207 end if;
3209 Sec_Stack_Len := Convert_To (RTE (RE_Storage_Offset), Sec_Stack_Len);
3211 -- Build and set declarations for the wrapped thread body
3213 Ent_SS := Make_Defining_Identifier (Loc, Name_uSecondary_Stack);
3214 Ent_ATSD := Make_Defining_Identifier (Loc, Name_uProcess_ATSD);
3216 Decl_SS :=
3217 Make_Object_Declaration (Loc,
3218 Defining_Identifier => Ent_SS,
3219 Aliased_Present => True,
3220 Object_Definition =>
3221 Make_Subtype_Indication (Loc,
3222 Subtype_Mark =>
3223 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
3224 Constraint =>
3225 Make_Index_Or_Discriminant_Constraint (Loc,
3226 Constraints => New_List (
3227 Make_Range (Loc,
3228 Low_Bound => Make_Integer_Literal (Loc, 1),
3229 High_Bound => Sec_Stack_Len)))));
3231 Decl_ATSD :=
3232 Make_Object_Declaration (Loc,
3233 Defining_Identifier => Ent_ATSD,
3234 Aliased_Present => True,
3235 Object_Definition => New_Occurrence_Of (RTE (RE_ATSD), Loc));
3237 Set_Declarations (N, New_List (Decl_SS, Decl_ATSD));
3238 Analyze (Decl_SS);
3239 Analyze (Decl_ATSD);
3240 Set_Alignment (Ent_SS, UI_From_Int (Maximum_Alignment));
3242 -- Create new exception handler
3244 if Restriction_Active (No_Exception_Handlers) then
3245 Excep_Handlers := No_List;
3247 else
3248 Check_Restriction (No_Exception_Handlers, N);
3250 Ent_EO := Make_Defining_Identifier (Loc, Name_uE);
3252 Excep_Handlers := New_List (
3253 Make_Exception_Handler (Loc,
3254 Choice_Parameter => Ent_EO,
3255 Exception_Choices => New_List (
3256 Make_Others_Choice (Loc)),
3257 Statements => New_List (
3258 Make_Procedure_Call_Statement (Loc,
3259 Name =>
3260 New_Occurrence_Of
3261 (RTE (RE_Thread_Body_Exceptional_Exit), Loc),
3262 Parameter_Associations => New_List (
3263 New_Occurrence_Of (Ent_EO, Loc))))));
3264 end if;
3266 -- Now build new handled statement sequence and analyze it
3268 Set_Handled_Statement_Sequence (N,
3269 Make_Handled_Sequence_Of_Statements (Loc,
3270 Statements => New_List (
3272 Make_Procedure_Call_Statement (Loc,
3273 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Enter), Loc),
3274 Parameter_Associations => New_List (
3276 Make_Attribute_Reference (Loc,
3277 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3278 Attribute_Name => Name_Address),
3280 Make_Attribute_Reference (Loc,
3281 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3282 Attribute_Name => Name_Length),
3284 Make_Attribute_Reference (Loc,
3285 Prefix => New_Occurrence_Of (Ent_ATSD, Loc),
3286 Attribute_Name => Name_Address))),
3288 Make_Block_Statement (Loc,
3289 Declarations => User_Decls,
3290 Handled_Statement_Sequence => H),
3292 Make_Procedure_Call_Statement (Loc,
3293 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Leave), Loc))),
3295 Exception_Handlers => Excep_Handlers));
3297 Analyze (Handled_Statement_Sequence (N));
3298 End_Scope;
3299 end Expand_Thread_Body;
3301 -- Start of processing for Expand_N_Subprogram_Body
3303 begin
3304 -- Set L to either the list of declarations if present, or
3305 -- to the list of statements if no declarations are present.
3306 -- This is used to insert new stuff at the start.
3308 if Is_Non_Empty_List (Declarations (N)) then
3309 L := Declarations (N);
3310 else
3311 L := Statements (Handled_Statement_Sequence (N));
3312 end if;
3314 -- Find entity for subprogram
3316 Body_Id := Defining_Entity (N);
3318 if Present (Corresponding_Spec (N)) then
3319 Spec_Id := Corresponding_Spec (N);
3320 else
3321 Spec_Id := Body_Id;
3322 end if;
3324 -- Need poll on entry to subprogram if polling enabled. We only
3325 -- do this for non-empty subprograms, since it does not seem
3326 -- necessary to poll for a dummy null subprogram. Do not add polling
3327 -- point if calls to this subprogram will be inlined by the back-end,
3328 -- to avoid repeated polling points in nested inlinings.
3330 if Is_Non_Empty_List (L) then
3331 if Is_Inlined (Spec_Id)
3332 and then Front_End_Inlining
3333 and then Optimization_Level > 1
3334 then
3335 null;
3336 else
3337 Generate_Poll_Call (First (L));
3338 end if;
3339 end if;
3341 -- If this is a Pure function which has any parameters whose root
3342 -- type is System.Address, reset the Pure indication, since it will
3343 -- likely cause incorrect code to be generated as the parameter is
3344 -- probably a pointer, and the fact that the same pointer is passed
3345 -- does not mean that the same value is being referenced.
3347 -- Note that if the programmer gave an explicit Pure_Function pragma,
3348 -- then we believe the programmer, and leave the subprogram Pure.
3350 -- This code should probably be at the freeze point, so that it
3351 -- happens even on a -gnatc (or more importantly -gnatt) compile
3352 -- so that the semantic tree has Is_Pure set properly ???
3354 if Is_Pure (Spec_Id)
3355 and then Is_Subprogram (Spec_Id)
3356 and then not Has_Pragma_Pure_Function (Spec_Id)
3357 then
3358 declare
3359 F : Entity_Id := First_Formal (Spec_Id);
3361 begin
3362 while Present (F) loop
3363 if Is_Descendent_Of_Address (Etype (F)) then
3364 Set_Is_Pure (Spec_Id, False);
3366 if Spec_Id /= Body_Id then
3367 Set_Is_Pure (Body_Id, False);
3368 end if;
3370 exit;
3371 end if;
3373 Next_Formal (F);
3374 end loop;
3375 end;
3376 end if;
3378 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3380 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
3381 declare
3382 F : Entity_Id := First_Formal (Spec_Id);
3383 V : constant Boolean := Validity_Checks_On;
3385 begin
3386 -- We turn off validity checking, since we do not want any
3387 -- check on the initializing value itself (which we know
3388 -- may well be invalid!)
3390 Validity_Checks_On := False;
3392 -- Loop through formals
3394 while Present (F) loop
3395 if Is_Scalar_Type (Etype (F))
3396 and then Ekind (F) = E_Out_Parameter
3397 then
3398 Insert_Before_And_Analyze (First (L),
3399 Make_Assignment_Statement (Loc,
3400 Name => New_Occurrence_Of (F, Loc),
3401 Expression => Get_Simple_Init_Val (Etype (F), Loc)));
3402 end if;
3404 Next_Formal (F);
3405 end loop;
3407 Validity_Checks_On := V;
3408 end;
3409 end if;
3411 Scop := Scope (Spec_Id);
3413 -- Add discriminal renamings to protected subprograms.
3414 -- Install new discriminals for expansion of the next
3415 -- subprogram of this protected type, if any.
3417 if Is_List_Member (N)
3418 and then Present (Parent (List_Containing (N)))
3419 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3420 then
3421 Add_Discriminal_Declarations
3422 (Declarations (N), Scop, Name_uObject, Loc);
3423 Add_Private_Declarations (Declarations (N), Scop, Name_uObject, Loc);
3425 -- Associate privals and discriminals with the next protected
3426 -- operation body to be expanded. These are used to expand
3427 -- references to private data objects and discriminants,
3428 -- respectively.
3430 Next_Op := Next_Protected_Operation (N);
3432 if Present (Next_Op) then
3433 Dec := Parent (Base_Type (Scop));
3434 Set_Privals (Dec, Next_Op, Loc);
3435 Set_Discriminals (Dec);
3436 end if;
3437 end if;
3439 -- Clear out statement list for stubbed procedure
3441 if Present (Corresponding_Spec (N)) then
3442 Set_Elaboration_Flag (N, Spec_Id);
3444 if Convention (Spec_Id) = Convention_Stubbed
3445 or else Is_Eliminated (Spec_Id)
3446 then
3447 Set_Declarations (N, Empty_List);
3448 Set_Handled_Statement_Sequence (N,
3449 Make_Handled_Sequence_Of_Statements (Loc,
3450 Statements => New_List (
3451 Make_Null_Statement (Loc))));
3452 return;
3453 end if;
3454 end if;
3456 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3457 -- but subprograms with no specs are not frozen
3459 declare
3460 Typ : constant Entity_Id := Etype (Spec_Id);
3461 Utyp : constant Entity_Id := Underlying_Type (Typ);
3463 begin
3464 if not Acts_As_Spec (N)
3465 and then Nkind (Parent (Parent (Spec_Id))) /=
3466 N_Subprogram_Body_Stub
3467 then
3468 null;
3470 elsif Is_Return_By_Reference_Type (Typ) then
3471 Set_Returns_By_Ref (Spec_Id);
3473 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3474 Set_Returns_By_Ref (Spec_Id);
3475 end if;
3476 end;
3478 -- For a procedure, we add a return for all possible syntactic ends
3479 -- of the subprogram. Note that reanalysis is not necessary in this
3480 -- case since it would require a lot of work and accomplish nothing.
3482 if Ekind (Spec_Id) = E_Procedure
3483 or else Ekind (Spec_Id) = E_Generic_Procedure
3484 then
3485 Add_Return (Statements (H));
3487 if Present (Exception_Handlers (H)) then
3488 Except_H := First_Non_Pragma (Exception_Handlers (H));
3490 while Present (Except_H) loop
3491 Add_Return (Statements (Except_H));
3492 Next_Non_Pragma (Except_H);
3493 end loop;
3494 end if;
3496 -- For a function, we must deal with the case where there is at
3497 -- least one missing return. What we do is to wrap the entire body
3498 -- of the function in a block:
3500 -- begin
3501 -- ...
3502 -- end;
3504 -- becomes
3506 -- begin
3507 -- begin
3508 -- ...
3509 -- end;
3511 -- raise Program_Error;
3512 -- end;
3514 -- This approach is necessary because the raise must be signalled
3515 -- to the caller, not handled by any local handler (RM 6.4(11)).
3517 -- Note: we do not need to analyze the constructed sequence here,
3518 -- since it has no handler, and an attempt to analyze the handled
3519 -- statement sequence twice is risky in various ways (e.g. the
3520 -- issue of expanding cleanup actions twice).
3522 elsif Has_Missing_Return (Spec_Id) then
3523 declare
3524 Hloc : constant Source_Ptr := Sloc (H);
3525 Blok : constant Node_Id :=
3526 Make_Block_Statement (Hloc,
3527 Handled_Statement_Sequence => H);
3528 Rais : constant Node_Id :=
3529 Make_Raise_Program_Error (Hloc,
3530 Reason => PE_Missing_Return);
3532 begin
3533 Set_Handled_Statement_Sequence (N,
3534 Make_Handled_Sequence_Of_Statements (Hloc,
3535 Statements => New_List (Blok, Rais)));
3537 New_Scope (Spec_Id);
3538 Analyze (Blok);
3539 Analyze (Rais);
3540 Pop_Scope;
3541 end;
3542 end if;
3544 -- If subprogram contains a parameterless recursive call, then we may
3545 -- have an infinite recursion, so see if we can generate code to check
3546 -- for this possibility if storage checks are not suppressed.
3548 if Ekind (Spec_Id) = E_Procedure
3549 and then Has_Recursive_Call (Spec_Id)
3550 and then not Storage_Checks_Suppressed (Spec_Id)
3551 then
3552 Detect_Infinite_Recursion (N, Spec_Id);
3553 end if;
3555 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3556 -- parameters must be initialized to the appropriate default value.
3558 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
3559 declare
3560 Floc : Source_Ptr;
3561 Formal : Entity_Id;
3562 Stm : Node_Id;
3564 begin
3565 Formal := First_Formal (Spec_Id);
3567 while Present (Formal) loop
3568 Floc := Sloc (Formal);
3570 if Ekind (Formal) = E_Out_Parameter
3571 and then Is_Scalar_Type (Etype (Formal))
3572 then
3573 Stm :=
3574 Make_Assignment_Statement (Floc,
3575 Name => New_Occurrence_Of (Formal, Floc),
3576 Expression =>
3577 Get_Simple_Init_Val (Etype (Formal), Floc));
3578 Prepend (Stm, Declarations (N));
3579 Analyze (Stm);
3580 end if;
3582 Next_Formal (Formal);
3583 end loop;
3584 end;
3585 end if;
3587 -- Deal with thread body
3589 if Is_Thread_Body (Spec_Id) then
3590 Expand_Thread_Body;
3591 end if;
3593 -- If the subprogram does not have pending instantiations, then we
3594 -- must generate the subprogram descriptor now, since the code for
3595 -- the subprogram is complete, and this is our last chance. However
3596 -- if there are pending instantiations, then the code is not
3597 -- complete, and we will delay the generation.
3599 if Is_Subprogram (Spec_Id)
3600 and then not Delay_Subprogram_Descriptors (Spec_Id)
3601 then
3602 Generate_Subprogram_Descriptor_For_Subprogram (N, Spec_Id);
3603 end if;
3605 -- Set to encode entity names in package body before gigi is called
3607 Qualify_Entity_Names (N);
3608 end Expand_N_Subprogram_Body;
3610 -----------------------------------
3611 -- Expand_N_Subprogram_Body_Stub --
3612 -----------------------------------
3614 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
3615 begin
3616 if Present (Corresponding_Body (N)) then
3617 Expand_N_Subprogram_Body (
3618 Unit_Declaration_Node (Corresponding_Body (N)));
3619 end if;
3620 end Expand_N_Subprogram_Body_Stub;
3622 -------------------------------------
3623 -- Expand_N_Subprogram_Declaration --
3624 -------------------------------------
3626 -- If the declaration appears within a protected body, it is a private
3627 -- operation of the protected type. We must create the corresponding
3628 -- protected subprogram an associated formals. For a normal protected
3629 -- operation, this is done when expanding the protected type declaration.
3631 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
3632 Loc : constant Source_Ptr := Sloc (N);
3633 Subp : constant Entity_Id := Defining_Entity (N);
3634 Scop : constant Entity_Id := Scope (Subp);
3635 Prot_Decl : Node_Id;
3636 Prot_Bod : Node_Id;
3637 Prot_Id : Entity_Id;
3639 begin
3640 -- Deal with case of protected subprogram. Do not generate
3641 -- protected operation if operation is flagged as eliminated.
3643 if Is_List_Member (N)
3644 and then Present (Parent (List_Containing (N)))
3645 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3646 and then Is_Protected_Type (Scop)
3647 then
3648 if No (Protected_Body_Subprogram (Subp))
3649 and then not Is_Eliminated (Subp)
3650 then
3651 Prot_Decl :=
3652 Make_Subprogram_Declaration (Loc,
3653 Specification =>
3654 Build_Protected_Sub_Specification
3655 (N, Scop, Unprotected => True));
3657 -- The protected subprogram is declared outside of the protected
3658 -- body. Given that the body has frozen all entities so far, we
3659 -- analyze the subprogram and perform freezing actions explicitly.
3660 -- If the body is a subunit, the insertion point is before the
3661 -- stub in the parent.
3663 Prot_Bod := Parent (List_Containing (N));
3665 if Nkind (Parent (Prot_Bod)) = N_Subunit then
3666 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
3667 end if;
3669 Insert_Before (Prot_Bod, Prot_Decl);
3670 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
3672 New_Scope (Scope (Scop));
3673 Analyze (Prot_Decl);
3674 Create_Extra_Formals (Prot_Id);
3675 Set_Protected_Body_Subprogram (Subp, Prot_Id);
3676 Pop_Scope;
3677 end if;
3678 end if;
3679 end Expand_N_Subprogram_Declaration;
3681 ---------------------------------------
3682 -- Expand_Protected_Object_Reference --
3683 ---------------------------------------
3685 function Expand_Protected_Object_Reference
3686 (N : Node_Id;
3687 Scop : Entity_Id)
3688 return Node_Id
3690 Loc : constant Source_Ptr := Sloc (N);
3691 Corr : Entity_Id;
3692 Rec : Node_Id;
3693 Param : Entity_Id;
3694 Proc : Entity_Id;
3696 begin
3697 Rec := Make_Identifier (Loc, Name_uObject);
3698 Set_Etype (Rec, Corresponding_Record_Type (Scop));
3700 -- Find enclosing protected operation, and retrieve its first
3701 -- parameter, which denotes the enclosing protected object.
3702 -- If the enclosing operation is an entry, we are immediately
3703 -- within the protected body, and we can retrieve the object
3704 -- from the service entries procedure. A barrier function has
3705 -- has the same signature as an entry. A barrier function is
3706 -- compiled within the protected object, but unlike protected
3707 -- operations its never needs locks, so that its protected body
3708 -- subprogram points to itself.
3710 Proc := Current_Scope;
3712 while Present (Proc)
3713 and then Scope (Proc) /= Scop
3714 loop
3715 Proc := Scope (Proc);
3716 end loop;
3718 Corr := Protected_Body_Subprogram (Proc);
3720 if No (Corr) then
3722 -- Previous error left expansion incomplete.
3723 -- Nothing to do on this call.
3725 return Empty;
3726 end if;
3728 Param :=
3729 Defining_Identifier
3730 (First (Parameter_Specifications (Parent (Corr))));
3732 if Is_Subprogram (Proc)
3733 and then Proc /= Corr
3734 then
3735 -- Protected function or procedure.
3737 Set_Entity (Rec, Param);
3739 -- Rec is a reference to an entity which will not be in scope
3740 -- when the call is reanalyzed, and needs no further analysis.
3742 Set_Analyzed (Rec);
3744 else
3745 -- Entry or barrier function for entry body.
3746 -- The first parameter of the entry body procedure is a
3747 -- pointer to the object. We create a local variable
3748 -- of the proper type, duplicating what is done to define
3749 -- _object later on.
3751 declare
3752 Decls : List_Id;
3753 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
3754 Chars =>
3755 New_Internal_Name ('T'));
3757 begin
3758 Decls := New_List (
3759 Make_Full_Type_Declaration (Loc,
3760 Defining_Identifier => Obj_Ptr,
3761 Type_Definition =>
3762 Make_Access_To_Object_Definition (Loc,
3763 Subtype_Indication =>
3764 New_Reference_To
3765 (Corresponding_Record_Type (Scop), Loc))));
3767 Insert_Actions (N, Decls);
3768 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
3770 Rec :=
3771 Make_Explicit_Dereference (Loc,
3772 Unchecked_Convert_To (Obj_Ptr,
3773 New_Occurrence_Of (Param, Loc)));
3775 -- Analyze new actual. Other actuals in calls are already
3776 -- analyzed and the list of actuals is not renalyzed after
3777 -- rewriting.
3779 Set_Parent (Rec, N);
3780 Analyze (Rec);
3781 end;
3782 end if;
3784 return Rec;
3785 end Expand_Protected_Object_Reference;
3787 --------------------------------------
3788 -- Expand_Protected_Subprogram_Call --
3789 --------------------------------------
3791 procedure Expand_Protected_Subprogram_Call
3792 (N : Node_Id;
3793 Subp : Entity_Id;
3794 Scop : Entity_Id)
3796 Rec : Node_Id;
3798 begin
3799 -- If the protected object is not an enclosing scope, this is
3800 -- an inter-object function call. Inter-object procedure
3801 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
3802 -- The call is intra-object only if the subprogram being
3803 -- called is in the protected body being compiled, and if the
3804 -- protected object in the call is statically the enclosing type.
3805 -- The object may be an component of some other data structure,
3806 -- in which case this must be handled as an inter-object call.
3808 if not In_Open_Scopes (Scop)
3809 or else not Is_Entity_Name (Name (N))
3810 then
3811 if Nkind (Name (N)) = N_Selected_Component then
3812 Rec := Prefix (Name (N));
3814 else
3815 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
3816 Rec := Prefix (Prefix (Name (N)));
3817 end if;
3819 Build_Protected_Subprogram_Call (N,
3820 Name => New_Occurrence_Of (Subp, Sloc (N)),
3821 Rec => Convert_Concurrent (Rec, Etype (Rec)),
3822 External => True);
3824 else
3825 Rec := Expand_Protected_Object_Reference (N, Scop);
3827 if No (Rec) then
3828 return;
3829 end if;
3831 Build_Protected_Subprogram_Call (N,
3832 Name => Name (N),
3833 Rec => Rec,
3834 External => False);
3836 end if;
3838 Analyze (N);
3840 -- If it is a function call it can appear in elaboration code and
3841 -- the called entity must be frozen here.
3843 if Ekind (Subp) = E_Function then
3844 Freeze_Expression (Name (N));
3845 end if;
3846 end Expand_Protected_Subprogram_Call;
3848 -----------------------
3849 -- Freeze_Subprogram --
3850 -----------------------
3852 procedure Freeze_Subprogram (N : Node_Id) is
3853 E : constant Entity_Id := Entity (N);
3855 begin
3856 -- When a primitive is frozen, enter its name in the corresponding
3857 -- dispatch table. If the DTC_Entity field is not set this is an
3858 -- overridden primitive that can be ignored. We suppress the
3859 -- initialization of the dispatch table entry when Java_VM because
3860 -- the dispatching mechanism is handled internally by the JVM.
3862 if Is_Dispatching_Operation (E)
3863 and then not Is_Abstract (E)
3864 and then Present (DTC_Entity (E))
3865 and then not Is_CPP_Class (Scope (DTC_Entity (E)))
3866 and then not Java_VM
3867 then
3868 Check_Overriding_Operation (E);
3869 Insert_After (N, Fill_DT_Entry (Sloc (N), E));
3870 end if;
3872 -- Mark functions that return by reference. Note that it cannot be
3873 -- part of the normal semantic analysis of the spec since the
3874 -- underlying returned type may not be known yet (for private types)
3876 declare
3877 Typ : constant Entity_Id := Etype (E);
3878 Utyp : constant Entity_Id := Underlying_Type (Typ);
3880 begin
3881 if Is_Return_By_Reference_Type (Typ) then
3882 Set_Returns_By_Ref (E);
3884 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3885 Set_Returns_By_Ref (E);
3886 end if;
3887 end;
3888 end Freeze_Subprogram;
3890 end Exp_Ch6;