Mark ChangeLog
[official-gcc.git] / gcc / ada / exp_ch6.adb
blob6305f5dd74616f4c432ebd520ca22607939e00e7
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-2005, 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_Dispatching_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 -- In the case where the intrinsic is to be processed by the back end,
1978 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
1979 -- since the idea in this case is to pass the call unchanged.
1981 if Is_Intrinsic_Subprogram (Subp) then
1982 Expand_Intrinsic_Call (N, Subp);
1983 return;
1984 end if;
1986 if Ekind (Subp) = E_Function
1987 or else Ekind (Subp) = E_Procedure
1988 then
1989 if Is_Inlined (Subp) then
1991 Inlined_Subprogram : declare
1992 Bod : Node_Id;
1993 Must_Inline : Boolean := False;
1994 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
1995 Scop : constant Entity_Id := Scope (Subp);
1997 function In_Unfrozen_Instance return Boolean;
1998 -- If the subprogram comes from an instance in the same
1999 -- unit, and the instance is not yet frozen, inlining might
2000 -- trigger order-of-elaboration problems in gigi.
2002 --------------------------
2003 -- In_Unfrozen_Instance --
2004 --------------------------
2006 function In_Unfrozen_Instance return Boolean is
2007 S : Entity_Id := Scop;
2009 begin
2010 while Present (S)
2011 and then S /= Standard_Standard
2012 loop
2013 if Is_Generic_Instance (S)
2014 and then Present (Freeze_Node (S))
2015 and then not Analyzed (Freeze_Node (S))
2016 then
2017 return True;
2018 end if;
2020 S := Scope (S);
2021 end loop;
2023 return False;
2024 end In_Unfrozen_Instance;
2026 -- Start of processing for Inlined_Subprogram
2028 begin
2029 -- Verify that the body to inline has already been seen,
2030 -- and that if the body is in the current unit the inlining
2031 -- does not occur earlier. This avoids order-of-elaboration
2032 -- problems in gigi.
2034 if No (Spec)
2035 or else Nkind (Spec) /= N_Subprogram_Declaration
2036 or else No (Body_To_Inline (Spec))
2037 then
2038 Must_Inline := False;
2040 -- If this an inherited function that returns a private
2041 -- type, do not inline if the full view is an unconstrained
2042 -- array, because such calls cannot be inlined.
2044 elsif Present (Orig_Subp)
2045 and then Is_Array_Type (Etype (Orig_Subp))
2046 and then not Is_Constrained (Etype (Orig_Subp))
2047 then
2048 Must_Inline := False;
2050 elsif In_Unfrozen_Instance then
2051 Must_Inline := False;
2053 else
2054 Bod := Body_To_Inline (Spec);
2056 if (In_Extended_Main_Code_Unit (N)
2057 or else In_Extended_Main_Code_Unit (Parent (N))
2058 or else Is_Always_Inlined (Subp))
2059 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
2060 or else
2061 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
2062 then
2063 Must_Inline := True;
2065 -- If we are compiling a package body that is not the main
2066 -- unit, it must be for inlining/instantiation purposes,
2067 -- in which case we inline the call to insure that the same
2068 -- temporaries are generated when compiling the body by
2069 -- itself. Otherwise link errors can occur.
2071 -- If the function being called is itself in the main unit,
2072 -- we cannot inline, because there is a risk of double
2073 -- elaboration and/or circularity: the inlining can make
2074 -- visible a private entity in the body of the main unit,
2075 -- that gigi will see before its sees its proper definition.
2077 elsif not (In_Extended_Main_Code_Unit (N))
2078 and then In_Package_Body
2079 then
2080 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
2081 end if;
2082 end if;
2084 if Must_Inline then
2085 Expand_Inlined_Call (N, Subp, Orig_Subp);
2087 else
2088 -- Let the back end handle it
2090 Add_Inlined_Body (Subp);
2092 if Front_End_Inlining
2093 and then Nkind (Spec) = N_Subprogram_Declaration
2094 and then (In_Extended_Main_Code_Unit (N))
2095 and then No (Body_To_Inline (Spec))
2096 and then not Has_Completion (Subp)
2097 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
2098 then
2099 Cannot_Inline
2100 ("cannot inline& (body not seen yet)?",
2101 N, Subp);
2102 end if;
2103 end if;
2104 end Inlined_Subprogram;
2105 end if;
2106 end if;
2108 -- Check for a protected subprogram. This is either an intra-object
2109 -- call, or a protected function call. Protected procedure calls are
2110 -- rewritten as entry calls and handled accordingly.
2112 Scop := Scope (Subp);
2114 if Nkind (N) /= N_Entry_Call_Statement
2115 and then Is_Protected_Type (Scop)
2116 then
2117 -- If the call is an internal one, it is rewritten as a call to
2118 -- to the corresponding unprotected subprogram.
2120 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2121 end if;
2123 -- Functions returning controlled objects need special attention
2125 if Controlled_Type (Etype (Subp))
2126 and then not Is_Return_By_Reference_Type (Etype (Subp))
2127 then
2128 Expand_Ctrl_Function_Call (N);
2129 end if;
2131 -- Test for First_Optional_Parameter, and if so, truncate parameter
2132 -- list if there are optional parameters at the trailing end.
2133 -- Note we never delete procedures for call via a pointer.
2135 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2136 and then Present (First_Optional_Parameter (Subp))
2137 then
2138 declare
2139 Last_Keep_Arg : Node_Id;
2141 begin
2142 -- Last_Keep_Arg will hold the last actual that should be
2143 -- retained. If it remains empty at the end, it means that
2144 -- all parameters are optional.
2146 Last_Keep_Arg := Empty;
2148 -- Find first optional parameter, must be present since we
2149 -- checked the validity of the parameter before setting it.
2151 Formal := First_Formal (Subp);
2152 Actual := First_Actual (N);
2153 while Formal /= First_Optional_Parameter (Subp) loop
2154 Last_Keep_Arg := Actual;
2155 Next_Formal (Formal);
2156 Next_Actual (Actual);
2157 end loop;
2159 -- We have Formal and Actual pointing to the first potentially
2160 -- droppable argument. We can drop all the trailing arguments
2161 -- whose actual matches the default. Note that we know that all
2162 -- remaining formals have defaults, because we checked that this
2163 -- requirement was met before setting First_Optional_Parameter.
2165 -- We use Fully_Conformant_Expressions to check for identity
2166 -- between formals and actuals, which may miss some cases, but
2167 -- on the other hand, this is only an optimization (if we fail
2168 -- to truncate a parameter it does not affect functionality).
2169 -- So if the default is 3 and the actual is 1+2, we consider
2170 -- them unequal, which hardly seems worrisome.
2172 while Present (Formal) loop
2173 if not Fully_Conformant_Expressions
2174 (Actual, Default_Value (Formal))
2175 then
2176 Last_Keep_Arg := Actual;
2177 end if;
2179 Next_Formal (Formal);
2180 Next_Actual (Actual);
2181 end loop;
2183 -- If no arguments, delete entire list, this is the easy case
2185 if No (Last_Keep_Arg) then
2186 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2187 Delete_Tree (Remove_Head (Parameter_Associations (N)));
2188 end loop;
2190 Set_Parameter_Associations (N, No_List);
2191 Set_First_Named_Actual (N, Empty);
2193 -- Case where at the last retained argument is positional. This
2194 -- is also an easy case, since the retained arguments are already
2195 -- in the right form, and we don't need to worry about the order
2196 -- of arguments that get eliminated.
2198 elsif Is_List_Member (Last_Keep_Arg) then
2199 while Present (Next (Last_Keep_Arg)) loop
2200 Delete_Tree (Remove_Next (Last_Keep_Arg));
2201 end loop;
2203 Set_First_Named_Actual (N, Empty);
2205 -- This is the annoying case where the last retained argument
2206 -- is a named parameter. Since the original arguments are not
2207 -- in declaration order, we may have to delete some fairly
2208 -- random collection of arguments.
2210 else
2211 declare
2212 Temp : Node_Id;
2213 Passoc : Node_Id;
2215 Discard : Node_Id;
2216 pragma Warnings (Off, Discard);
2218 begin
2219 -- First step, remove all the named parameters from the
2220 -- list (they are still chained using First_Named_Actual
2221 -- and Next_Named_Actual, so we have not lost them!)
2223 Temp := First (Parameter_Associations (N));
2225 -- Case of all parameters named, remove them all
2227 if Nkind (Temp) = N_Parameter_Association then
2228 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2229 Temp := Remove_Head (Parameter_Associations (N));
2230 end loop;
2232 -- Case of mixed positional/named, remove named parameters
2234 else
2235 while Nkind (Next (Temp)) /= N_Parameter_Association loop
2236 Next (Temp);
2237 end loop;
2239 while Present (Next (Temp)) loop
2240 Discard := Remove_Next (Temp);
2241 end loop;
2242 end if;
2244 -- Now we loop through the named parameters, till we get
2245 -- to the last one to be retained, adding them to the list.
2246 -- Note that the Next_Named_Actual list does not need to be
2247 -- touched since we are only reordering them on the actual
2248 -- parameter association list.
2250 Passoc := Parent (First_Named_Actual (N));
2251 loop
2252 Temp := Relocate_Node (Passoc);
2253 Append_To
2254 (Parameter_Associations (N), Temp);
2255 exit when
2256 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
2257 Passoc := Parent (Next_Named_Actual (Passoc));
2258 end loop;
2260 Set_Next_Named_Actual (Temp, Empty);
2262 loop
2263 Temp := Next_Named_Actual (Passoc);
2264 exit when No (Temp);
2265 Set_Next_Named_Actual
2266 (Passoc, Next_Named_Actual (Parent (Temp)));
2267 Delete_Tree (Temp);
2268 end loop;
2269 end;
2270 end if;
2271 end;
2272 end if;
2273 end Expand_Call;
2275 --------------------------
2276 -- Expand_Inlined_Call --
2277 --------------------------
2279 procedure Expand_Inlined_Call
2280 (N : Node_Id;
2281 Subp : Entity_Id;
2282 Orig_Subp : Entity_Id)
2284 Loc : constant Source_Ptr := Sloc (N);
2285 Is_Predef : constant Boolean :=
2286 Is_Predefined_File_Name
2287 (Unit_File_Name (Get_Source_Unit (Subp)));
2288 Orig_Bod : constant Node_Id :=
2289 Body_To_Inline (Unit_Declaration_Node (Subp));
2291 Blk : Node_Id;
2292 Bod : Node_Id;
2293 Decl : Node_Id;
2294 Exit_Lab : Entity_Id := Empty;
2295 F : Entity_Id;
2296 A : Node_Id;
2297 Lab_Decl : Node_Id;
2298 Lab_Id : Node_Id;
2299 New_A : Node_Id;
2300 Num_Ret : Int := 0;
2301 Ret_Type : Entity_Id;
2302 Targ : Node_Id;
2303 Temp : Entity_Id;
2304 Temp_Typ : Entity_Id;
2306 procedure Make_Exit_Label;
2307 -- Build declaration for exit label to be used in Return statements
2309 function Process_Formals (N : Node_Id) return Traverse_Result;
2310 -- Replace occurrence of a formal with the corresponding actual, or
2311 -- the thunk generated for it.
2313 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2314 -- If the call being expanded is that of an internal subprogram,
2315 -- set the sloc of the generated block to that of the call itself,
2316 -- so that the expansion is skipped by the -next- command in gdb.
2317 -- Same processing for a subprogram in a predefined file, e.g.
2318 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2319 -- to simplify our own development.
2321 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2322 -- If the function body is a single expression, replace call with
2323 -- expression, else insert block appropriately.
2325 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2326 -- If procedure body has no local variables, inline body without
2327 -- creating block, otherwise rewrite call with block.
2329 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2330 -- Determine whether a formal parameter is used only once in Orig_Bod
2332 ---------------------
2333 -- Make_Exit_Label --
2334 ---------------------
2336 procedure Make_Exit_Label is
2337 begin
2338 -- Create exit label for subprogram if one does not exist yet
2340 if No (Exit_Lab) then
2341 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
2342 Set_Entity (Lab_Id,
2343 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
2344 Exit_Lab := Make_Label (Loc, Lab_Id);
2346 Lab_Decl :=
2347 Make_Implicit_Label_Declaration (Loc,
2348 Defining_Identifier => Entity (Lab_Id),
2349 Label_Construct => Exit_Lab);
2350 end if;
2351 end Make_Exit_Label;
2353 ---------------------
2354 -- Process_Formals --
2355 ---------------------
2357 function Process_Formals (N : Node_Id) return Traverse_Result is
2358 A : Entity_Id;
2359 E : Entity_Id;
2360 Ret : Node_Id;
2362 begin
2363 if Is_Entity_Name (N)
2364 and then Present (Entity (N))
2365 then
2366 E := Entity (N);
2368 if Is_Formal (E)
2369 and then Scope (E) = Subp
2370 then
2371 A := Renamed_Object (E);
2373 if Is_Entity_Name (A) then
2374 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2376 elsif Nkind (A) = N_Defining_Identifier then
2377 Rewrite (N, New_Occurrence_Of (A, Loc));
2379 else -- numeric literal
2380 Rewrite (N, New_Copy (A));
2381 end if;
2382 end if;
2384 return Skip;
2386 elsif Nkind (N) = N_Return_Statement then
2388 if No (Expression (N)) then
2389 Make_Exit_Label;
2390 Rewrite (N, Make_Goto_Statement (Loc,
2391 Name => New_Copy (Lab_Id)));
2393 else
2394 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2395 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2396 then
2397 -- Function body is a single expression. No need for
2398 -- exit label.
2400 null;
2402 else
2403 Num_Ret := Num_Ret + 1;
2404 Make_Exit_Label;
2405 end if;
2407 -- Because of the presence of private types, the views of the
2408 -- expression and the context may be different, so place an
2409 -- unchecked conversion to the context type to avoid spurious
2410 -- errors, eg. when the expression is a numeric literal and
2411 -- the context is private. If the expression is an aggregate,
2412 -- use a qualified expression, because an aggregate is not a
2413 -- legal argument of a conversion.
2415 if Nkind (Expression (N)) = N_Aggregate
2416 or else Nkind (Expression (N)) = N_Null
2417 then
2418 Ret :=
2419 Make_Qualified_Expression (Sloc (N),
2420 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2421 Expression => Relocate_Node (Expression (N)));
2422 else
2423 Ret :=
2424 Unchecked_Convert_To
2425 (Ret_Type, Relocate_Node (Expression (N)));
2426 end if;
2428 if Nkind (Targ) = N_Defining_Identifier then
2429 Rewrite (N,
2430 Make_Assignment_Statement (Loc,
2431 Name => New_Occurrence_Of (Targ, Loc),
2432 Expression => Ret));
2433 else
2434 Rewrite (N,
2435 Make_Assignment_Statement (Loc,
2436 Name => New_Copy (Targ),
2437 Expression => Ret));
2438 end if;
2440 Set_Assignment_OK (Name (N));
2442 if Present (Exit_Lab) then
2443 Insert_After (N,
2444 Make_Goto_Statement (Loc,
2445 Name => New_Copy (Lab_Id)));
2446 end if;
2447 end if;
2449 return OK;
2451 -- Remove pragma Unreferenced since it may refer to formals that
2452 -- are not visible in the inlined body, and in any case we will
2453 -- not be posting warnings on the inlined body so it is unneeded.
2455 elsif Nkind (N) = N_Pragma
2456 and then Chars (N) = Name_Unreferenced
2457 then
2458 Rewrite (N, Make_Null_Statement (Sloc (N)));
2459 return OK;
2461 else
2462 return OK;
2463 end if;
2464 end Process_Formals;
2466 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2468 ------------------
2469 -- Process_Sloc --
2470 ------------------
2472 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2473 begin
2474 if not Debug_Generated_Code then
2475 Set_Sloc (Nod, Sloc (N));
2476 Set_Comes_From_Source (Nod, False);
2477 end if;
2479 return OK;
2480 end Process_Sloc;
2482 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2484 ---------------------------
2485 -- Rewrite_Function_Call --
2486 ---------------------------
2488 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2489 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2490 Fst : constant Node_Id := First (Statements (HSS));
2492 begin
2493 -- Optimize simple case: function body is a single return statement,
2494 -- which has been expanded into an assignment.
2496 if Is_Empty_List (Declarations (Blk))
2497 and then Nkind (Fst) = N_Assignment_Statement
2498 and then No (Next (Fst))
2499 then
2501 -- The function call may have been rewritten as the temporary
2502 -- that holds the result of the call, in which case remove the
2503 -- now useless declaration.
2505 if Nkind (N) = N_Identifier
2506 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2507 then
2508 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2509 end if;
2511 Rewrite (N, Expression (Fst));
2513 elsif Nkind (N) = N_Identifier
2514 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2515 then
2516 -- The block assigns the result of the call to the temporary
2518 Insert_After (Parent (Entity (N)), Blk);
2520 elsif Nkind (Parent (N)) = N_Assignment_Statement
2521 and then Is_Entity_Name (Name (Parent (N)))
2522 then
2523 -- Replace assignment with the block
2525 declare
2526 Original_Assignment : constant Node_Id := Parent (N);
2528 begin
2529 -- Preserve the original assignment node to keep the
2530 -- complete assignment subtree consistent enough for
2531 -- Analyze_Assignment to proceed (specifically, the
2532 -- original Lhs node must still have an assignment
2533 -- statement as its parent).
2535 -- We cannot rely on Original_Node to go back from the
2536 -- block node to the assignment node, because the
2537 -- assignment might already be a rewrite substitution.
2539 Discard_Node (Relocate_Node (Original_Assignment));
2540 Rewrite (Original_Assignment, Blk);
2541 end;
2543 elsif Nkind (Parent (N)) = N_Object_Declaration then
2544 Set_Expression (Parent (N), Empty);
2545 Insert_After (Parent (N), Blk);
2546 end if;
2547 end Rewrite_Function_Call;
2549 ----------------------------
2550 -- Rewrite_Procedure_Call --
2551 ----------------------------
2553 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2554 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2555 begin
2556 if Is_Empty_List (Declarations (Blk)) then
2557 Insert_List_After (N, Statements (HSS));
2558 Rewrite (N, Make_Null_Statement (Loc));
2559 else
2560 Rewrite (N, Blk);
2561 end if;
2562 end Rewrite_Procedure_Call;
2564 -------------------------
2565 -- Formal_Is_Used_Once --
2566 ------------------------
2568 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2569 Use_Counter : Int := 0;
2571 function Count_Uses (N : Node_Id) return Traverse_Result;
2572 -- Traverse the tree and count the uses of the formal parameter.
2573 -- In this case, for optimization purposes, we do not need to
2574 -- continue the traversal once more than one use is encountered.
2576 ----------------
2577 -- Count_Uses --
2578 ----------------
2580 function Count_Uses (N : Node_Id) return Traverse_Result is
2581 begin
2582 -- The original node is an identifier
2584 if Nkind (N) = N_Identifier
2585 and then Present (Entity (N))
2587 -- The original node's entity points to the one in the
2588 -- copied body.
2590 and then Nkind (Entity (N)) = N_Identifier
2591 and then Present (Entity (Entity (N)))
2593 -- The entity of the copied node is the formal parameter
2595 and then Entity (Entity (N)) = Formal
2596 then
2597 Use_Counter := Use_Counter + 1;
2599 if Use_Counter > 1 then
2601 -- Denote more than one use and abandon the traversal
2603 Use_Counter := 2;
2604 return Abandon;
2606 end if;
2607 end if;
2609 return OK;
2610 end Count_Uses;
2612 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2614 -- Start of processing for Formal_Is_Used_Once
2616 begin
2617 Count_Formal_Uses (Orig_Bod);
2618 return Use_Counter = 1;
2619 end Formal_Is_Used_Once;
2621 -- Start of processing for Expand_Inlined_Call
2623 begin
2624 -- Check for special case of To_Address call, and if so, just
2625 -- do an unchecked conversion instead of expanding the call.
2626 -- Not only is this more efficient, but it also avoids a
2627 -- problem with order of elaboration when address clauses
2628 -- are inlined (address expr elaborated at wrong point).
2630 if Subp = RTE (RE_To_Address) then
2631 Rewrite (N,
2632 Unchecked_Convert_To
2633 (RTE (RE_Address),
2634 Relocate_Node (First_Actual (N))));
2635 return;
2636 end if;
2638 if Nkind (Orig_Bod) = N_Defining_Identifier then
2640 -- Subprogram is a renaming_as_body. Calls appearing after the
2641 -- renaming can be replaced with calls to the renamed entity
2642 -- directly, because the subprograms are subtype conformant.
2644 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2645 return;
2646 end if;
2648 -- Use generic machinery to copy body of inlined subprogram, as if it
2649 -- were an instantiation, resetting source locations appropriately, so
2650 -- that nested inlined calls appear in the main unit.
2652 Save_Env (Subp, Empty);
2653 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2655 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2656 Blk :=
2657 Make_Block_Statement (Loc,
2658 Declarations => Declarations (Bod),
2659 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
2661 if No (Declarations (Bod)) then
2662 Set_Declarations (Blk, New_List);
2663 end if;
2665 -- If this is a derived function, establish the proper return type
2667 if Present (Orig_Subp)
2668 and then Orig_Subp /= Subp
2669 then
2670 Ret_Type := Etype (Orig_Subp);
2671 else
2672 Ret_Type := Etype (Subp);
2673 end if;
2675 F := First_Formal (Subp);
2676 A := First_Actual (N);
2678 -- Create temporaries for the actuals that are expressions, or that
2679 -- are scalars and require copying to preserve semantics.
2681 while Present (F) loop
2682 if Present (Renamed_Object (F)) then
2683 Error_Msg_N (" cannot inline call to recursive subprogram", N);
2684 return;
2685 end if;
2687 -- If the argument may be a controlling argument in a call within
2688 -- the inlined body, we must preserve its classwide nature to
2689 -- insure that dynamic dispatching take place subsequently.
2690 -- If the formal has a constraint it must be preserved to retain
2691 -- the semantics of the body.
2693 if Is_Class_Wide_Type (Etype (F))
2694 or else (Is_Access_Type (Etype (F))
2695 and then
2696 Is_Class_Wide_Type (Designated_Type (Etype (F))))
2697 then
2698 Temp_Typ := Etype (F);
2700 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
2701 and then Etype (F) /= Base_Type (Etype (F))
2702 then
2703 Temp_Typ := Etype (F);
2705 else
2706 Temp_Typ := Etype (A);
2707 end if;
2709 -- If the actual is a simple name or a literal, no need to
2710 -- create a temporary, object can be used directly.
2712 if (Is_Entity_Name (A)
2713 and then
2714 (not Is_Scalar_Type (Etype (A))
2715 or else Ekind (Entity (A)) = E_Enumeration_Literal))
2717 -- When the actual is an identifier and the corresponding formal
2718 -- is used only once in the original body, the formal can be
2719 -- substituted directly with the actual parameter.
2721 or else (Nkind (A) = N_Identifier
2722 and then Formal_Is_Used_Once (F))
2724 or else Nkind (A) = N_Real_Literal
2725 or else Nkind (A) = N_Integer_Literal
2726 or else Nkind (A) = N_Character_Literal
2727 then
2728 if Etype (F) /= Etype (A) then
2729 Set_Renamed_Object
2730 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
2731 else
2732 Set_Renamed_Object (F, A);
2733 end if;
2735 else
2736 Temp :=
2737 Make_Defining_Identifier (Loc,
2738 Chars => New_Internal_Name ('C'));
2740 -- If the actual for an in/in-out parameter is a view conversion,
2741 -- make it into an unchecked conversion, given that an untagged
2742 -- type conversion is not a proper object for a renaming.
2744 -- In-out conversions that involve real conversions have already
2745 -- been transformed in Expand_Actuals.
2747 if Nkind (A) = N_Type_Conversion
2748 and then Ekind (F) /= E_In_Parameter
2749 then
2750 New_A := Make_Unchecked_Type_Conversion (Loc,
2751 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
2752 Expression => Relocate_Node (Expression (A)));
2754 elsif Etype (F) /= Etype (A) then
2755 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
2756 Temp_Typ := Etype (F);
2758 else
2759 New_A := Relocate_Node (A);
2760 end if;
2762 Set_Sloc (New_A, Sloc (N));
2764 if Ekind (F) = E_In_Parameter
2765 and then not Is_Limited_Type (Etype (A))
2766 then
2767 Decl :=
2768 Make_Object_Declaration (Loc,
2769 Defining_Identifier => Temp,
2770 Constant_Present => True,
2771 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
2772 Expression => New_A);
2773 else
2774 Decl :=
2775 Make_Object_Renaming_Declaration (Loc,
2776 Defining_Identifier => Temp,
2777 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
2778 Name => New_A);
2779 end if;
2781 Prepend (Decl, Declarations (Blk));
2782 Set_Renamed_Object (F, Temp);
2783 end if;
2785 Next_Formal (F);
2786 Next_Actual (A);
2787 end loop;
2789 -- Establish target of function call. If context is not assignment or
2790 -- declaration, create a temporary as a target. The declaration for
2791 -- the temporary may be subsequently optimized away if the body is a
2792 -- single expression, or if the left-hand side of the assignment is
2793 -- simple enough.
2795 if Ekind (Subp) = E_Function then
2796 if Nkind (Parent (N)) = N_Assignment_Statement
2797 and then Is_Entity_Name (Name (Parent (N)))
2798 then
2799 Targ := Name (Parent (N));
2801 else
2802 -- Replace call with temporary and create its declaration
2804 Temp :=
2805 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2807 Decl :=
2808 Make_Object_Declaration (Loc,
2809 Defining_Identifier => Temp,
2810 Object_Definition =>
2811 New_Occurrence_Of (Ret_Type, Loc));
2813 Set_No_Initialization (Decl);
2814 Insert_Action (N, Decl);
2815 Rewrite (N, New_Occurrence_Of (Temp, Loc));
2816 Targ := Temp;
2817 end if;
2818 end if;
2820 -- Traverse the tree and replace formals with actuals or their thunks.
2821 -- Attach block to tree before analysis and rewriting.
2823 Replace_Formals (Blk);
2824 Set_Parent (Blk, N);
2826 if not Comes_From_Source (Subp)
2827 or else Is_Predef
2828 then
2829 Reset_Slocs (Blk);
2830 end if;
2832 if Present (Exit_Lab) then
2834 -- If the body was a single expression, the single return statement
2835 -- and the corresponding label are useless.
2837 if Num_Ret = 1
2838 and then
2839 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
2840 N_Goto_Statement
2841 then
2842 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
2843 else
2844 Append (Lab_Decl, (Declarations (Blk)));
2845 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
2846 end if;
2847 end if;
2849 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
2850 -- conflicting private views that Gigi would ignore. If this is a
2851 -- predefined unit, analyze with checks off, as is done in the non-
2852 -- inlined run-time units.
2854 declare
2855 I_Flag : constant Boolean := In_Inlined_Body;
2857 begin
2858 In_Inlined_Body := True;
2860 if Is_Predef then
2861 declare
2862 Style : constant Boolean := Style_Check;
2863 begin
2864 Style_Check := False;
2865 Analyze (Blk, Suppress => All_Checks);
2866 Style_Check := Style;
2867 end;
2869 else
2870 Analyze (Blk);
2871 end if;
2873 In_Inlined_Body := I_Flag;
2874 end;
2876 if Ekind (Subp) = E_Procedure then
2877 Rewrite_Procedure_Call (N, Blk);
2878 else
2879 Rewrite_Function_Call (N, Blk);
2880 end if;
2882 Restore_Env;
2884 -- Cleanup mapping between formals and actuals for other expansions
2886 F := First_Formal (Subp);
2888 while Present (F) loop
2889 Set_Renamed_Object (F, Empty);
2890 Next_Formal (F);
2891 end loop;
2892 end Expand_Inlined_Call;
2894 ----------------------------
2895 -- Expand_N_Function_Call --
2896 ----------------------------
2898 procedure Expand_N_Function_Call (N : Node_Id) is
2899 Typ : constant Entity_Id := Etype (N);
2901 function Returned_By_Reference return Boolean;
2902 -- If the return type is returned through the secondary stack. that is
2903 -- by reference, we don't want to create a temp to force stack checking.
2904 -- Shouldn't this function be moved to exp_util???
2906 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean;
2907 -- If the call is the right side of an assignment or the expression in
2908 -- an object declaration, we don't need to create a temp as the left
2909 -- side will already trigger stack checking if necessary.
2911 ---------------------------
2912 -- Returned_By_Reference --
2913 ---------------------------
2915 function Returned_By_Reference return Boolean is
2916 S : Entity_Id := Current_Scope;
2918 begin
2919 if Is_Return_By_Reference_Type (Typ) then
2920 return True;
2922 elsif Nkind (Parent (N)) /= N_Return_Statement then
2923 return False;
2925 elsif Requires_Transient_Scope (Typ) then
2927 -- Verify that the return type of the enclosing function has
2928 -- the same constrained status as that of the expression.
2930 while Ekind (S) /= E_Function loop
2931 S := Scope (S);
2932 end loop;
2934 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
2935 else
2936 return False;
2937 end if;
2938 end Returned_By_Reference;
2940 ---------------------------
2941 -- Rhs_Of_Assign_Or_Decl --
2942 ---------------------------
2944 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean is
2945 begin
2946 if (Nkind (Parent (N)) = N_Assignment_Statement
2947 and then Expression (Parent (N)) = N)
2948 or else
2949 (Nkind (Parent (N)) = N_Qualified_Expression
2950 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2951 and then Expression (Parent (Parent (N))) = Parent (N))
2952 or else
2953 (Nkind (Parent (N)) = N_Object_Declaration
2954 and then Expression (Parent (N)) = N)
2955 or else
2956 (Nkind (Parent (N)) = N_Component_Association
2957 and then Expression (Parent (N)) = N
2958 and then Nkind (Parent (Parent (N))) = N_Aggregate
2959 and then Rhs_Of_Assign_Or_Decl (Parent (Parent (N))))
2960 then
2961 return True;
2962 else
2963 return False;
2964 end if;
2965 end Rhs_Of_Assign_Or_Decl;
2967 -- Start of processing for Expand_N_Function_Call
2969 begin
2970 -- A special check. If stack checking is enabled, and the return type
2971 -- might generate a large temporary, and the call is not the right
2972 -- side of an assignment, then generate an explicit temporary. We do
2973 -- this because otherwise gigi may generate a large temporary on the
2974 -- fly and this can cause trouble with stack checking.
2976 -- This is unecessary if the call is the expression in an object
2977 -- declaration, or if it appears outside of any library unit. This
2978 -- can only happen if it appears as an actual in a library-level
2979 -- instance, in which case a temporary will be generated for it once
2980 -- the instance itself is installed.
2982 if May_Generate_Large_Temp (Typ)
2983 and then not Rhs_Of_Assign_Or_Decl (N)
2984 and then not Returned_By_Reference
2985 and then Current_Scope /= Standard_Standard
2986 then
2987 if Stack_Checking_Enabled then
2989 -- Note: it might be thought that it would be OK to use a call
2990 -- to Force_Evaluation here, but that's not good enough, because
2991 -- that can results in a 'Reference construct that may still
2992 -- need a temporary.
2994 declare
2995 Loc : constant Source_Ptr := Sloc (N);
2996 Temp_Obj : constant Entity_Id :=
2997 Make_Defining_Identifier (Loc,
2998 Chars => New_Internal_Name ('F'));
2999 Temp_Typ : Entity_Id := Typ;
3000 Decl : Node_Id;
3001 A : Node_Id;
3002 F : Entity_Id;
3003 Proc : Entity_Id;
3005 begin
3006 if Is_Tagged_Type (Typ)
3007 and then Present (Controlling_Argument (N))
3008 then
3009 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
3010 and then Nkind (Parent (N)) /= N_Function_Call
3011 then
3012 -- If this is a tag-indeterminate call, the object must
3013 -- be classwide.
3015 if Is_Tag_Indeterminate (N) then
3016 Temp_Typ := Class_Wide_Type (Typ);
3017 end if;
3019 else
3020 -- If this is a dispatching call that is itself the
3021 -- controlling argument of an enclosing call, the
3022 -- nominal subtype of the object that replaces it must
3023 -- be classwide, so that dispatching will take place
3024 -- properly. If it is not a controlling argument, the
3025 -- object is not classwide.
3027 Proc := Entity (Name (Parent (N)));
3028 F := First_Formal (Proc);
3029 A := First_Actual (Parent (N));
3031 while A /= N loop
3032 Next_Formal (F);
3033 Next_Actual (A);
3034 end loop;
3036 if Is_Controlling_Formal (F) then
3037 Temp_Typ := Class_Wide_Type (Typ);
3038 end if;
3039 end if;
3040 end if;
3042 Decl :=
3043 Make_Object_Declaration (Loc,
3044 Defining_Identifier => Temp_Obj,
3045 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3046 Constant_Present => True,
3047 Expression => Relocate_Node (N));
3048 Set_Assignment_OK (Decl);
3050 Insert_Actions (N, New_List (Decl));
3051 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
3052 end;
3054 else
3055 -- If stack-checking is not enabled, increment serial number
3056 -- for internal names, so that subsequent symbols are consistent
3057 -- with and without stack-checking.
3059 Synchronize_Serial_Number;
3061 -- Now we can expand the call with consistent symbol names
3063 Expand_Call (N);
3064 end if;
3066 -- Normal case, expand the call
3068 else
3069 Expand_Call (N);
3070 end if;
3071 end Expand_N_Function_Call;
3073 ---------------------------------------
3074 -- Expand_N_Procedure_Call_Statement --
3075 ---------------------------------------
3077 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
3078 begin
3079 Expand_Call (N);
3080 end Expand_N_Procedure_Call_Statement;
3082 ------------------------------
3083 -- Expand_N_Subprogram_Body --
3084 ------------------------------
3086 -- Add poll call if ATC polling is enabled, unless the body will be
3087 -- inlined by the back-end.
3089 -- Add return statement if last statement in body is not a return
3090 -- statement (this makes things easier on Gigi which does not want
3091 -- to have to handle a missing return).
3093 -- Add call to Activate_Tasks if body is a task activator
3095 -- Deal with possible detection of infinite recursion
3097 -- Eliminate body completely if convention stubbed
3099 -- Encode entity names within body, since we will not need to reference
3100 -- these entities any longer in the front end.
3102 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
3104 -- Reset Pure indication if any parameter has root type System.Address
3106 -- Wrap thread body
3108 procedure Expand_N_Subprogram_Body (N : Node_Id) is
3109 Loc : constant Source_Ptr := Sloc (N);
3110 H : constant Node_Id := Handled_Statement_Sequence (N);
3111 Body_Id : Entity_Id;
3112 Spec_Id : Entity_Id;
3113 Except_H : Node_Id;
3114 Scop : Entity_Id;
3115 Dec : Node_Id;
3116 Next_Op : Node_Id;
3117 L : List_Id;
3119 procedure Add_Return (S : List_Id);
3120 -- Append a return statement to the statement sequence S if the last
3121 -- statement is not already a return or a goto statement. Note that
3122 -- the latter test is not critical, it does not matter if we add a
3123 -- few extra returns, since they get eliminated anyway later on.
3125 procedure Expand_Thread_Body;
3126 -- Perform required expansion of a thread body
3128 ----------------
3129 -- Add_Return --
3130 ----------------
3132 procedure Add_Return (S : List_Id) is
3133 begin
3134 if not Is_Transfer (Last (S)) then
3136 -- The source location for the return is the end label
3137 -- of the procedure in all cases. This is a bit odd when
3138 -- there are exception handlers, but not much else we can do.
3140 Append_To (S, Make_Return_Statement (Sloc (End_Label (H))));
3141 end if;
3142 end Add_Return;
3144 ------------------------
3145 -- Expand_Thread_Body --
3146 ------------------------
3148 -- The required expansion of a thread body is as follows
3150 -- procedure <thread body procedure name> is
3152 -- _Secondary_Stack : aliased
3153 -- Storage_Elements.Storage_Array
3154 -- (1 .. Storage_Offset (Sec_Stack_Size));
3155 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
3157 -- _Process_ATSD : aliased System.Threads.ATSD;
3159 -- begin
3160 -- System.Threads.Thread_Body_Enter;
3161 -- (_Secondary_Stack'Address,
3162 -- _Secondary_Stack'Length,
3163 -- _Process_ATSD'Address);
3165 -- declare
3166 -- <user declarations>
3167 -- begin
3168 -- <user statements>
3169 -- <user exception handlers>
3170 -- end;
3172 -- System.Threads.Thread_Body_Leave;
3174 -- exception
3175 -- when E : others =>
3176 -- System.Threads.Thread_Body_Exceptional_Exit (E);
3177 -- end;
3179 -- Note the exception handler is omitted if pragma Restriction
3180 -- No_Exception_Handlers is currently active.
3182 procedure Expand_Thread_Body is
3183 User_Decls : constant List_Id := Declarations (N);
3184 Sec_Stack_Len : Node_Id;
3186 TB_Pragma : constant Node_Id :=
3187 Get_Rep_Pragma (Spec_Id, Name_Thread_Body);
3189 Ent_SS : Entity_Id;
3190 Ent_ATSD : Entity_Id;
3191 Ent_EO : Entity_Id;
3193 Decl_SS : Node_Id;
3194 Decl_ATSD : Node_Id;
3196 Excep_Handlers : List_Id;
3198 begin
3199 New_Scope (Spec_Id);
3201 -- Get proper setting for secondary stack size
3203 if List_Length (Pragma_Argument_Associations (TB_Pragma)) = 2 then
3204 Sec_Stack_Len :=
3205 Expression (Last (Pragma_Argument_Associations (TB_Pragma)));
3206 else
3207 Sec_Stack_Len :=
3208 New_Occurrence_Of (RTE (RE_Default_Secondary_Stack_Size), Loc);
3209 end if;
3211 Sec_Stack_Len := Convert_To (RTE (RE_Storage_Offset), Sec_Stack_Len);
3213 -- Build and set declarations for the wrapped thread body
3215 Ent_SS := Make_Defining_Identifier (Loc, Name_uSecondary_Stack);
3216 Ent_ATSD := Make_Defining_Identifier (Loc, Name_uProcess_ATSD);
3218 Decl_SS :=
3219 Make_Object_Declaration (Loc,
3220 Defining_Identifier => Ent_SS,
3221 Aliased_Present => True,
3222 Object_Definition =>
3223 Make_Subtype_Indication (Loc,
3224 Subtype_Mark =>
3225 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
3226 Constraint =>
3227 Make_Index_Or_Discriminant_Constraint (Loc,
3228 Constraints => New_List (
3229 Make_Range (Loc,
3230 Low_Bound => Make_Integer_Literal (Loc, 1),
3231 High_Bound => Sec_Stack_Len)))));
3233 Decl_ATSD :=
3234 Make_Object_Declaration (Loc,
3235 Defining_Identifier => Ent_ATSD,
3236 Aliased_Present => True,
3237 Object_Definition => New_Occurrence_Of (RTE (RE_ATSD), Loc));
3239 Set_Declarations (N, New_List (Decl_SS, Decl_ATSD));
3240 Analyze (Decl_SS);
3241 Analyze (Decl_ATSD);
3242 Set_Alignment (Ent_SS, UI_From_Int (Maximum_Alignment));
3244 -- Create new exception handler
3246 if Restriction_Active (No_Exception_Handlers) then
3247 Excep_Handlers := No_List;
3249 else
3250 Check_Restriction (No_Exception_Handlers, N);
3252 Ent_EO := Make_Defining_Identifier (Loc, Name_uE);
3254 Excep_Handlers := New_List (
3255 Make_Exception_Handler (Loc,
3256 Choice_Parameter => Ent_EO,
3257 Exception_Choices => New_List (
3258 Make_Others_Choice (Loc)),
3259 Statements => New_List (
3260 Make_Procedure_Call_Statement (Loc,
3261 Name =>
3262 New_Occurrence_Of
3263 (RTE (RE_Thread_Body_Exceptional_Exit), Loc),
3264 Parameter_Associations => New_List (
3265 New_Occurrence_Of (Ent_EO, Loc))))));
3266 end if;
3268 -- Now build new handled statement sequence and analyze it
3270 Set_Handled_Statement_Sequence (N,
3271 Make_Handled_Sequence_Of_Statements (Loc,
3272 Statements => New_List (
3274 Make_Procedure_Call_Statement (Loc,
3275 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Enter), Loc),
3276 Parameter_Associations => New_List (
3278 Make_Attribute_Reference (Loc,
3279 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3280 Attribute_Name => Name_Address),
3282 Make_Attribute_Reference (Loc,
3283 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3284 Attribute_Name => Name_Length),
3286 Make_Attribute_Reference (Loc,
3287 Prefix => New_Occurrence_Of (Ent_ATSD, Loc),
3288 Attribute_Name => Name_Address))),
3290 Make_Block_Statement (Loc,
3291 Declarations => User_Decls,
3292 Handled_Statement_Sequence => H),
3294 Make_Procedure_Call_Statement (Loc,
3295 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Leave), Loc))),
3297 Exception_Handlers => Excep_Handlers));
3299 Analyze (Handled_Statement_Sequence (N));
3300 End_Scope;
3301 end Expand_Thread_Body;
3303 -- Start of processing for Expand_N_Subprogram_Body
3305 begin
3306 -- Set L to either the list of declarations if present, or
3307 -- to the list of statements if no declarations are present.
3308 -- This is used to insert new stuff at the start.
3310 if Is_Non_Empty_List (Declarations (N)) then
3311 L := Declarations (N);
3312 else
3313 L := Statements (Handled_Statement_Sequence (N));
3314 end if;
3316 -- Find entity for subprogram
3318 Body_Id := Defining_Entity (N);
3320 if Present (Corresponding_Spec (N)) then
3321 Spec_Id := Corresponding_Spec (N);
3322 else
3323 Spec_Id := Body_Id;
3324 end if;
3326 -- Need poll on entry to subprogram if polling enabled. We only
3327 -- do this for non-empty subprograms, since it does not seem
3328 -- necessary to poll for a dummy null subprogram. Do not add polling
3329 -- point if calls to this subprogram will be inlined by the back-end,
3330 -- to avoid repeated polling points in nested inlinings.
3332 if Is_Non_Empty_List (L) then
3333 if Is_Inlined (Spec_Id)
3334 and then Front_End_Inlining
3335 and then Optimization_Level > 1
3336 then
3337 null;
3338 else
3339 Generate_Poll_Call (First (L));
3340 end if;
3341 end if;
3343 -- If this is a Pure function which has any parameters whose root
3344 -- type is System.Address, reset the Pure indication, since it will
3345 -- likely cause incorrect code to be generated as the parameter is
3346 -- probably a pointer, and the fact that the same pointer is passed
3347 -- does not mean that the same value is being referenced.
3349 -- Note that if the programmer gave an explicit Pure_Function pragma,
3350 -- then we believe the programmer, and leave the subprogram Pure.
3352 -- This code should probably be at the freeze point, so that it
3353 -- happens even on a -gnatc (or more importantly -gnatt) compile
3354 -- so that the semantic tree has Is_Pure set properly ???
3356 if Is_Pure (Spec_Id)
3357 and then Is_Subprogram (Spec_Id)
3358 and then not Has_Pragma_Pure_Function (Spec_Id)
3359 then
3360 declare
3361 F : Entity_Id := First_Formal (Spec_Id);
3363 begin
3364 while Present (F) loop
3365 if Is_Descendent_Of_Address (Etype (F)) then
3366 Set_Is_Pure (Spec_Id, False);
3368 if Spec_Id /= Body_Id then
3369 Set_Is_Pure (Body_Id, False);
3370 end if;
3372 exit;
3373 end if;
3375 Next_Formal (F);
3376 end loop;
3377 end;
3378 end if;
3380 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3382 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
3383 declare
3384 F : Entity_Id := First_Formal (Spec_Id);
3385 V : constant Boolean := Validity_Checks_On;
3387 begin
3388 -- We turn off validity checking, since we do not want any
3389 -- check on the initializing value itself (which we know
3390 -- may well be invalid!)
3392 Validity_Checks_On := False;
3394 -- Loop through formals
3396 while Present (F) loop
3397 if Is_Scalar_Type (Etype (F))
3398 and then Ekind (F) = E_Out_Parameter
3399 then
3400 Insert_Before_And_Analyze (First (L),
3401 Make_Assignment_Statement (Loc,
3402 Name => New_Occurrence_Of (F, Loc),
3403 Expression => Get_Simple_Init_Val (Etype (F), Loc)));
3404 end if;
3406 Next_Formal (F);
3407 end loop;
3409 Validity_Checks_On := V;
3410 end;
3411 end if;
3413 Scop := Scope (Spec_Id);
3415 -- Add discriminal renamings to protected subprograms.
3416 -- Install new discriminals for expansion of the next
3417 -- subprogram of this protected type, if any.
3419 if Is_List_Member (N)
3420 and then Present (Parent (List_Containing (N)))
3421 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3422 then
3423 Add_Discriminal_Declarations
3424 (Declarations (N), Scop, Name_uObject, Loc);
3425 Add_Private_Declarations (Declarations (N), Scop, Name_uObject, Loc);
3427 -- Associate privals and discriminals with the next protected
3428 -- operation body to be expanded. These are used to expand
3429 -- references to private data objects and discriminants,
3430 -- respectively.
3432 Next_Op := Next_Protected_Operation (N);
3434 if Present (Next_Op) then
3435 Dec := Parent (Base_Type (Scop));
3436 Set_Privals (Dec, Next_Op, Loc);
3437 Set_Discriminals (Dec);
3438 end if;
3439 end if;
3441 -- Clear out statement list for stubbed procedure
3443 if Present (Corresponding_Spec (N)) then
3444 Set_Elaboration_Flag (N, Spec_Id);
3446 if Convention (Spec_Id) = Convention_Stubbed
3447 or else Is_Eliminated (Spec_Id)
3448 then
3449 Set_Declarations (N, Empty_List);
3450 Set_Handled_Statement_Sequence (N,
3451 Make_Handled_Sequence_Of_Statements (Loc,
3452 Statements => New_List (
3453 Make_Null_Statement (Loc))));
3454 return;
3455 end if;
3456 end if;
3458 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3459 -- but subprograms with no specs are not frozen
3461 declare
3462 Typ : constant Entity_Id := Etype (Spec_Id);
3463 Utyp : constant Entity_Id := Underlying_Type (Typ);
3465 begin
3466 if not Acts_As_Spec (N)
3467 and then Nkind (Parent (Parent (Spec_Id))) /=
3468 N_Subprogram_Body_Stub
3469 then
3470 null;
3472 elsif Is_Return_By_Reference_Type (Typ) then
3473 Set_Returns_By_Ref (Spec_Id);
3475 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3476 Set_Returns_By_Ref (Spec_Id);
3477 end if;
3478 end;
3480 -- For a procedure, we add a return for all possible syntactic ends
3481 -- of the subprogram. Note that reanalysis is not necessary in this
3482 -- case since it would require a lot of work and accomplish nothing.
3484 if Ekind (Spec_Id) = E_Procedure
3485 or else Ekind (Spec_Id) = E_Generic_Procedure
3486 then
3487 Add_Return (Statements (H));
3489 if Present (Exception_Handlers (H)) then
3490 Except_H := First_Non_Pragma (Exception_Handlers (H));
3492 while Present (Except_H) loop
3493 Add_Return (Statements (Except_H));
3494 Next_Non_Pragma (Except_H);
3495 end loop;
3496 end if;
3498 -- For a function, we must deal with the case where there is at least
3499 -- one missing return. What we do is to wrap the entire body of the
3500 -- function in a block:
3502 -- begin
3503 -- ...
3504 -- end;
3506 -- becomes
3508 -- begin
3509 -- begin
3510 -- ...
3511 -- end;
3513 -- raise Program_Error;
3514 -- end;
3516 -- This approach is necessary because the raise must be signalled
3517 -- to the caller, not handled by any local handler (RM 6.4(11)).
3519 -- Note: we do not need to analyze the constructed sequence here,
3520 -- since it has no handler, and an attempt to analyze the handled
3521 -- statement sequence twice is risky in various ways (e.g. the
3522 -- issue of expanding cleanup actions twice).
3524 elsif Has_Missing_Return (Spec_Id) then
3525 declare
3526 Hloc : constant Source_Ptr := Sloc (H);
3527 Blok : constant Node_Id :=
3528 Make_Block_Statement (Hloc,
3529 Handled_Statement_Sequence => H);
3530 Rais : constant Node_Id :=
3531 Make_Raise_Program_Error (Hloc,
3532 Reason => PE_Missing_Return);
3534 begin
3535 Set_Handled_Statement_Sequence (N,
3536 Make_Handled_Sequence_Of_Statements (Hloc,
3537 Statements => New_List (Blok, Rais)));
3539 New_Scope (Spec_Id);
3540 Analyze (Blok);
3541 Analyze (Rais);
3542 Pop_Scope;
3543 end;
3544 end if;
3546 -- If subprogram contains a parameterless recursive call, then we may
3547 -- have an infinite recursion, so see if we can generate code to check
3548 -- for this possibility if storage checks are not suppressed.
3550 if Ekind (Spec_Id) = E_Procedure
3551 and then Has_Recursive_Call (Spec_Id)
3552 and then not Storage_Checks_Suppressed (Spec_Id)
3553 then
3554 Detect_Infinite_Recursion (N, Spec_Id);
3555 end if;
3557 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3558 -- parameters must be initialized to the appropriate default value.
3560 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
3561 declare
3562 Floc : Source_Ptr;
3563 Formal : Entity_Id;
3564 Stm : Node_Id;
3566 begin
3567 Formal := First_Formal (Spec_Id);
3569 while Present (Formal) loop
3570 Floc := Sloc (Formal);
3572 if Ekind (Formal) = E_Out_Parameter
3573 and then Is_Scalar_Type (Etype (Formal))
3574 then
3575 Stm :=
3576 Make_Assignment_Statement (Floc,
3577 Name => New_Occurrence_Of (Formal, Floc),
3578 Expression =>
3579 Get_Simple_Init_Val (Etype (Formal), Floc));
3580 Prepend (Stm, Declarations (N));
3581 Analyze (Stm);
3582 end if;
3584 Next_Formal (Formal);
3585 end loop;
3586 end;
3587 end if;
3589 -- Deal with thread body
3591 if Is_Thread_Body (Spec_Id) then
3592 Expand_Thread_Body;
3593 end if;
3595 -- If the subprogram does not have pending instantiations, then we
3596 -- must generate the subprogram descriptor now, since the code for
3597 -- the subprogram is complete, and this is our last chance. However
3598 -- if there are pending instantiations, then the code is not
3599 -- complete, and we will delay the generation.
3601 if Is_Subprogram (Spec_Id)
3602 and then not Delay_Subprogram_Descriptors (Spec_Id)
3603 then
3604 Generate_Subprogram_Descriptor_For_Subprogram (N, Spec_Id);
3605 end if;
3607 -- Set to encode entity names in package body before gigi is called
3609 Qualify_Entity_Names (N);
3610 end Expand_N_Subprogram_Body;
3612 -----------------------------------
3613 -- Expand_N_Subprogram_Body_Stub --
3614 -----------------------------------
3616 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
3617 begin
3618 if Present (Corresponding_Body (N)) then
3619 Expand_N_Subprogram_Body (
3620 Unit_Declaration_Node (Corresponding_Body (N)));
3621 end if;
3622 end Expand_N_Subprogram_Body_Stub;
3624 -------------------------------------
3625 -- Expand_N_Subprogram_Declaration --
3626 -------------------------------------
3628 -- If the declaration appears within a protected body, it is a private
3629 -- operation of the protected type. We must create the corresponding
3630 -- protected subprogram an associated formals. For a normal protected
3631 -- operation, this is done when expanding the protected type declaration.
3633 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
3634 Loc : constant Source_Ptr := Sloc (N);
3635 Subp : constant Entity_Id := Defining_Entity (N);
3636 Scop : constant Entity_Id := Scope (Subp);
3637 Prot_Decl : Node_Id;
3638 Prot_Bod : Node_Id;
3639 Prot_Id : Entity_Id;
3641 begin
3642 -- Deal with case of protected subprogram. Do not generate
3643 -- protected operation if operation is flagged as eliminated.
3645 if Is_List_Member (N)
3646 and then Present (Parent (List_Containing (N)))
3647 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3648 and then Is_Protected_Type (Scop)
3649 then
3650 if No (Protected_Body_Subprogram (Subp))
3651 and then not Is_Eliminated (Subp)
3652 then
3653 Prot_Decl :=
3654 Make_Subprogram_Declaration (Loc,
3655 Specification =>
3656 Build_Protected_Sub_Specification
3657 (N, Scop, Unprotected => True));
3659 -- The protected subprogram is declared outside of the protected
3660 -- body. Given that the body has frozen all entities so far, we
3661 -- analyze the subprogram and perform freezing actions explicitly.
3662 -- If the body is a subunit, the insertion point is before the
3663 -- stub in the parent.
3665 Prot_Bod := Parent (List_Containing (N));
3667 if Nkind (Parent (Prot_Bod)) = N_Subunit then
3668 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
3669 end if;
3671 Insert_Before (Prot_Bod, Prot_Decl);
3672 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
3674 New_Scope (Scope (Scop));
3675 Analyze (Prot_Decl);
3676 Create_Extra_Formals (Prot_Id);
3677 Set_Protected_Body_Subprogram (Subp, Prot_Id);
3678 Pop_Scope;
3679 end if;
3680 end if;
3681 end Expand_N_Subprogram_Declaration;
3683 ---------------------------------------
3684 -- Expand_Protected_Object_Reference --
3685 ---------------------------------------
3687 function Expand_Protected_Object_Reference
3688 (N : Node_Id;
3689 Scop : Entity_Id)
3690 return Node_Id
3692 Loc : constant Source_Ptr := Sloc (N);
3693 Corr : Entity_Id;
3694 Rec : Node_Id;
3695 Param : Entity_Id;
3696 Proc : Entity_Id;
3698 begin
3699 Rec := Make_Identifier (Loc, Name_uObject);
3700 Set_Etype (Rec, Corresponding_Record_Type (Scop));
3702 -- Find enclosing protected operation, and retrieve its first
3703 -- parameter, which denotes the enclosing protected object.
3704 -- If the enclosing operation is an entry, we are immediately
3705 -- within the protected body, and we can retrieve the object
3706 -- from the service entries procedure. A barrier function has
3707 -- has the same signature as an entry. A barrier function is
3708 -- compiled within the protected object, but unlike protected
3709 -- operations its never needs locks, so that its protected body
3710 -- subprogram points to itself.
3712 Proc := Current_Scope;
3714 while Present (Proc)
3715 and then Scope (Proc) /= Scop
3716 loop
3717 Proc := Scope (Proc);
3718 end loop;
3720 Corr := Protected_Body_Subprogram (Proc);
3722 if No (Corr) then
3724 -- Previous error left expansion incomplete.
3725 -- Nothing to do on this call.
3727 return Empty;
3728 end if;
3730 Param :=
3731 Defining_Identifier
3732 (First (Parameter_Specifications (Parent (Corr))));
3734 if Is_Subprogram (Proc)
3735 and then Proc /= Corr
3736 then
3737 -- Protected function or procedure
3739 Set_Entity (Rec, Param);
3741 -- Rec is a reference to an entity which will not be in scope
3742 -- when the call is reanalyzed, and needs no further analysis.
3744 Set_Analyzed (Rec);
3746 else
3747 -- Entry or barrier function for entry body.
3748 -- The first parameter of the entry body procedure is a
3749 -- pointer to the object. We create a local variable
3750 -- of the proper type, duplicating what is done to define
3751 -- _object later on.
3753 declare
3754 Decls : List_Id;
3755 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
3756 Chars =>
3757 New_Internal_Name ('T'));
3759 begin
3760 Decls := New_List (
3761 Make_Full_Type_Declaration (Loc,
3762 Defining_Identifier => Obj_Ptr,
3763 Type_Definition =>
3764 Make_Access_To_Object_Definition (Loc,
3765 Subtype_Indication =>
3766 New_Reference_To
3767 (Corresponding_Record_Type (Scop), Loc))));
3769 Insert_Actions (N, Decls);
3770 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
3772 Rec :=
3773 Make_Explicit_Dereference (Loc,
3774 Unchecked_Convert_To (Obj_Ptr,
3775 New_Occurrence_Of (Param, Loc)));
3777 -- Analyze new actual. Other actuals in calls are already
3778 -- analyzed and the list of actuals is not renalyzed after
3779 -- rewriting.
3781 Set_Parent (Rec, N);
3782 Analyze (Rec);
3783 end;
3784 end if;
3786 return Rec;
3787 end Expand_Protected_Object_Reference;
3789 --------------------------------------
3790 -- Expand_Protected_Subprogram_Call --
3791 --------------------------------------
3793 procedure Expand_Protected_Subprogram_Call
3794 (N : Node_Id;
3795 Subp : Entity_Id;
3796 Scop : Entity_Id)
3798 Rec : Node_Id;
3800 begin
3801 -- If the protected object is not an enclosing scope, this is
3802 -- an inter-object function call. Inter-object procedure
3803 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
3804 -- The call is intra-object only if the subprogram being
3805 -- called is in the protected body being compiled, and if the
3806 -- protected object in the call is statically the enclosing type.
3807 -- The object may be an component of some other data structure,
3808 -- in which case this must be handled as an inter-object call.
3810 if not In_Open_Scopes (Scop)
3811 or else not Is_Entity_Name (Name (N))
3812 then
3813 if Nkind (Name (N)) = N_Selected_Component then
3814 Rec := Prefix (Name (N));
3816 else
3817 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
3818 Rec := Prefix (Prefix (Name (N)));
3819 end if;
3821 Build_Protected_Subprogram_Call (N,
3822 Name => New_Occurrence_Of (Subp, Sloc (N)),
3823 Rec => Convert_Concurrent (Rec, Etype (Rec)),
3824 External => True);
3826 else
3827 Rec := Expand_Protected_Object_Reference (N, Scop);
3829 if No (Rec) then
3830 return;
3831 end if;
3833 Build_Protected_Subprogram_Call (N,
3834 Name => Name (N),
3835 Rec => Rec,
3836 External => False);
3838 end if;
3840 Analyze (N);
3842 -- If it is a function call it can appear in elaboration code and
3843 -- the called entity must be frozen here.
3845 if Ekind (Subp) = E_Function then
3846 Freeze_Expression (Name (N));
3847 end if;
3848 end Expand_Protected_Subprogram_Call;
3850 -----------------------
3851 -- Freeze_Subprogram --
3852 -----------------------
3854 procedure Freeze_Subprogram (N : Node_Id) is
3855 E : constant Entity_Id := Entity (N);
3857 begin
3858 -- When a primitive is frozen, enter its name in the corresponding
3859 -- dispatch table. If the DTC_Entity field is not set this is an
3860 -- overridden primitive that can be ignored. We suppress the
3861 -- initialization of the dispatch table entry when Java_VM because
3862 -- the dispatching mechanism is handled internally by the JVM.
3864 if Is_Dispatching_Operation (E)
3865 and then not Is_Abstract (E)
3866 and then Present (DTC_Entity (E))
3867 and then not Is_CPP_Class (Scope (DTC_Entity (E)))
3868 and then not Java_VM
3869 then
3870 Check_Overriding_Operation (E);
3871 Insert_After (N, Fill_DT_Entry (Sloc (N), E));
3872 end if;
3874 -- Mark functions that return by reference. Note that it cannot be
3875 -- part of the normal semantic analysis of the spec since the
3876 -- underlying returned type may not be known yet (for private types)
3878 declare
3879 Typ : constant Entity_Id := Etype (E);
3880 Utyp : constant Entity_Id := Underlying_Type (Typ);
3882 begin
3883 if Is_Return_By_Reference_Type (Typ) then
3884 Set_Returns_By_Ref (E);
3886 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3887 Set_Returns_By_Ref (E);
3888 end if;
3889 end;
3890 end Freeze_Subprogram;
3892 end Exp_Ch6;