1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Restrict; use Restrict;
54 with Rtsfind; use Rtsfind;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Ch13; use Sem_Ch13;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Eval; use Sem_Eval;
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.
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
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
145 procedure Expand_Protected_Subprogram_Call
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);
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)
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);
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)
188 Set_DT_Position (Subp, DT_Position (Prim_Op));
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
233 function Process (Nod : Node_Id) return Traverse_Result;
234 -- Function to traverse the subprogram body (using Traverse_Func)
240 function Process (Nod : Node_Id) return Traverse_Result is
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
252 Append_Elmt (Nod, Call_List);
255 -- Any other procedure call may have side effects
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)))
269 -- Case of an identifier reference
271 elsif Nkind (Nod) = N_Identifier then
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 ???
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
288 -- Ignore the reference if not to a more global object
290 elsif Scope_Depth (Scope (Ent)) >= Scop then
293 -- References to types, exceptions and constants are always OK
296 or else Ekind (Ent) = E_Exception
297 or else Ekind (Ent) = E_Constant
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)
311 -- Otherwise we have a reference to a global scalar
314 -- Loop through global entities already detected
316 Elm := First_Elmt (Var_List);
318 -- If not detected before, record this new global reference
321 Count_Vars := Count_Vars + 1;
323 if Count_Vars <= Max_Vars then
324 Append_Elmt (Entity (Nod), Var_List);
331 -- If recorded before, ignore
333 elsif Node (Elm) = Entity (Nod) then
336 -- Otherwise keep looking
346 -- For all other node kinds, recursively visit syntactic children
353 function Traverse_Body is new Traverse_Func;
355 -- Start of processing for Detect_Infinite_Recursion
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 Restrictions (No_Implicit_Conditionals) then
366 -- Otherwise do traversal and quit if we get abandon signal
368 if Traverse_Body (N) = Abandon then
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
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.
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.
392 Elm := First_Elmt (Var_List);
393 while Present (Elm) loop
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.
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));
413 Prepend (Decl, Declarations (N));
415 Insert_After (Last, Decl);
423 -- Loop through calls
425 Call := First_Elmt (Call_List);
426 while Present (Call) loop
428 -- Build a predicate expression of the form
431 -- and then global1 = temp1
432 -- and then global2 = temp2
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
449 Left_Opnd => New_Occurrence_Of (Node (Elm1), Loc),
450 Right_Opnd => New_Occurrence_Of (Node (Elm2), Loc)));
456 -- Now we replace the call with the sequence
458 -- if no-changes (see above) then
459 -- raise Storage_Error;
464 Rewrite (Node (Call),
465 Make_If_Statement (Loc,
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));
479 -- Remove temporary scope stack entry used for analysis
482 end Detect_Infinite_Recursion;
488 procedure Expand_Actuals (N : Node_Id; Subp : Entity_Id) is
489 Loc : constant Source_Ptr := Sloc (N);
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
540 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
542 if Nkind (Actual) = N_Type_Conversion then
543 V_Typ := Etype (Expression (Actual));
545 -- If the formal is an (in-)out parameter, capture the name
546 -- of the variable in order to build the post-call assignment.
548 Var := Make_Var (Expression (Actual));
550 Crep := not Same_Representation
551 (Etype (Formal), Etype (Expression (Actual)));
553 V_Typ := Etype (Actual);
554 Var := Make_Var (Actual);
558 -- Setup initialization for case of in out parameter, or an out
559 -- parameter where the formal is an unconstrained array (in the
560 -- latter case, we have to pass in an object with bounds).
562 if Ekind (Formal) = E_In_Out_Parameter
563 or else (Is_Array_Type (Etype (Formal))
565 not Is_Constrained (Etype (Formal)))
567 if Nkind (Actual) = N_Type_Conversion then
568 if Conversion_OK (Actual) then
569 Init := OK_Convert_To
570 (Etype (Formal), New_Occurrence_Of (Var, Loc));
573 (Etype (Formal), New_Occurrence_Of (Var, Loc));
576 Init := New_Occurrence_Of (Var, Loc);
579 -- An initialization is created for packed conversions as
580 -- actuals for out parameters to enable Make_Object_Declaration
581 -- to determine the proper subtype for N_Node. Note that this
582 -- is wasteful because the extra copying on the call side is
583 -- not required for such out parameters. ???
585 elsif Ekind (Formal) = E_Out_Parameter
586 and then Nkind (Actual) = N_Type_Conversion
587 and then (Is_Bit_Packed_Array (Etype (Formal))
589 Is_Bit_Packed_Array (Etype (Expression (Actual))))
591 if Conversion_OK (Actual) then
593 OK_Convert_To (Etype (Formal), New_Occurrence_Of (Var, Loc));
596 Convert_To (Etype (Formal), New_Occurrence_Of (Var, Loc));
603 Make_Object_Declaration (Loc,
604 Defining_Identifier => Temp,
606 New_Occurrence_Of (Etype (Formal), Loc),
608 Set_Assignment_OK (N_Node);
609 Insert_Action (N, N_Node);
611 -- Now, normally the deal here is that we use the defining
612 -- identifier created by that object declaration. There is
613 -- one exception to this. In the change of representation case
614 -- the above declaration will end up looking like:
616 -- temp : type := identifier;
618 -- And in this case we might as well use the identifier directly
619 -- and eliminate the temporary. Note that the analysis of the
620 -- declaration was not a waste of time in that case, since it is
621 -- what generated the necessary change of representation code. If
622 -- the change of representation introduced additional code, as in
623 -- a fixed-integer conversion, the expression is not an identifier
627 and then Present (Expression (N_Node))
628 and then Is_Entity_Name (Expression (N_Node))
630 Temp := Entity (Expression (N_Node));
631 Rewrite (N_Node, Make_Null_Statement (Loc));
634 -- For IN parameter, all we do is to replace the actual
636 if Ekind (Formal) = E_In_Parameter then
637 Rewrite (Actual, New_Reference_To (Temp, Loc));
640 -- Processing for OUT or IN OUT parameter
643 -- If type conversion, use reverse conversion on exit
645 if Nkind (Actual) = N_Type_Conversion then
646 if Conversion_OK (Actual) then
647 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
649 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
652 Expr := New_Occurrence_Of (Temp, Loc);
655 Rewrite (Actual, New_Reference_To (Temp, Loc));
658 Append_To (Post_Call,
659 Make_Assignment_Statement (Loc,
660 Name => New_Occurrence_Of (Var, Loc),
661 Expression => Expr));
663 Set_Assignment_OK (Name (Last (Post_Call)));
665 end Add_Call_By_Copy_Code;
667 ----------------------------------
668 -- Add_Packed_Call_By_Copy_Code --
669 ----------------------------------
671 procedure Add_Packed_Call_By_Copy_Code is
681 -- Prepare to generate code
683 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
684 Incod := Relocate_Node (Actual);
685 Outcod := New_Copy_Tree (Incod);
687 -- Generate declaration of temporary variable, initializing it
688 -- with the input parameter unless we have an OUT variable.
690 if Ekind (Formal) = E_Out_Parameter then
695 Make_Object_Declaration (Loc,
696 Defining_Identifier => Temp,
698 New_Occurrence_Of (Etype (Formal), Loc),
699 Expression => Incod));
701 -- The actual is simply a reference to the temporary
703 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
705 -- Generate copy out if OUT or IN OUT parameter
707 if Ekind (Formal) /= E_In_Parameter then
709 Rhs := New_Occurrence_Of (Temp, Loc);
711 -- Deal with conversion
713 if Nkind (Lhs) = N_Type_Conversion then
714 Lhs := Expression (Lhs);
715 Rhs := Convert_To (Etype (Actual), Rhs);
718 Append_To (Post_Call,
719 Make_Assignment_Statement (Loc,
723 end Add_Packed_Call_By_Copy_Code;
725 ---------------------------
726 -- Check_Fortran_Logical --
727 ---------------------------
729 procedure Check_Fortran_Logical is
730 Logical : constant Entity_Id := Etype (Formal);
733 -- Note: this is very incomplete, e.g. it does not handle arrays
734 -- of logical values. This is really not the right approach at all???)
737 if Convention (Subp) = Convention_Fortran
738 and then Root_Type (Etype (Formal)) = Standard_Boolean
739 and then Ekind (Formal) /= E_In_Parameter
741 Var := Make_Var (Actual);
742 Append_To (Post_Call,
743 Make_Assignment_Statement (Loc,
744 Name => New_Occurrence_Of (Var, Loc),
746 Unchecked_Convert_To (
749 Left_Opnd => New_Occurrence_Of (Var, Loc),
751 Unchecked_Convert_To (
753 New_Occurrence_Of (Standard_False, Loc))))));
755 end Check_Fortran_Logical;
761 function Make_Var (Actual : Node_Id) return Entity_Id is
765 if Is_Entity_Name (Actual) then
766 return Entity (Actual);
769 Var := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
772 Make_Object_Renaming_Declaration (Loc,
773 Defining_Identifier => Var,
775 New_Occurrence_Of (Etype (Actual), Loc),
776 Name => Relocate_Node (Actual));
778 Insert_Action (N, N_Node);
783 -------------------------
784 -- Reset_Packed_Prefix --
785 -------------------------
787 procedure Reset_Packed_Prefix is
788 Pfx : Node_Id := Actual;
792 Set_Analyzed (Pfx, False);
793 exit when Nkind (Pfx) /= N_Selected_Component
794 and then Nkind (Pfx) /= N_Indexed_Component;
797 end Reset_Packed_Prefix;
799 -- Start of processing for Expand_Actuals
802 Formal := First_Formal (Subp);
803 Actual := First_Actual (N);
805 Post_Call := New_List;
807 while Present (Formal) loop
808 E_Formal := Etype (Formal);
810 if Is_Scalar_Type (E_Formal)
811 or else Nkind (Actual) = N_Slice
813 Check_Fortran_Logical;
817 elsif Ekind (Formal) /= E_Out_Parameter then
819 -- The unusual case of the current instance of a protected type
820 -- requires special handling. This can only occur in the context
821 -- of a call within the body of a protected operation.
823 if Is_Entity_Name (Actual)
824 and then Ekind (Entity (Actual)) = E_Protected_Type
825 and then In_Open_Scopes (Entity (Actual))
827 if Scope (Subp) /= Entity (Actual) then
828 Error_Msg_N ("operation outside protected type may not "
829 & "call back its protected operations?", Actual);
833 Expand_Protected_Object_Reference (N, Entity (Actual)));
836 Apply_Constraint_Check (Actual, E_Formal);
838 -- Out parameter case. No constraint checks on access type
841 elsif Is_Access_Type (E_Formal) then
846 elsif Has_Discriminants (Base_Type (E_Formal))
847 or else Has_Non_Null_Base_Init_Proc (E_Formal)
849 Apply_Constraint_Check (Actual, E_Formal);
854 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
857 -- Processing for IN-OUT and OUT parameters
859 if Ekind (Formal) /= E_In_Parameter then
861 -- For type conversions of arrays, apply length/range checks
863 if Is_Array_Type (E_Formal)
864 and then Nkind (Actual) = N_Type_Conversion
866 if Is_Constrained (E_Formal) then
867 Apply_Length_Check (Expression (Actual), E_Formal);
869 Apply_Range_Check (Expression (Actual), E_Formal);
873 -- If argument is a type conversion for a type that is passed
874 -- by copy, then we must pass the parameter by copy.
876 if Nkind (Actual) = N_Type_Conversion
878 (Is_Numeric_Type (E_Formal)
879 or else Is_Access_Type (E_Formal)
880 or else Is_Enumeration_Type (E_Formal)
881 or else Is_Bit_Packed_Array (Etype (Formal))
882 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
884 -- Also pass by copy if change of representation
886 or else not Same_Representation
888 Etype (Expression (Actual))))
890 Add_Call_By_Copy_Code;
892 -- References to components of bit packed arrays are expanded
893 -- at this point, rather than at the point of analysis of the
894 -- actuals, to handle the expansion of the assignment to
895 -- [in] out parameters.
897 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
898 Add_Packed_Call_By_Copy_Code;
900 -- References to slices of bit packed arrays are expanded
902 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
903 Add_Call_By_Copy_Code;
905 -- References to possibly unaligned slices of arrays are expanded
907 elsif Is_Possibly_Unaligned_Slice (Actual) then
908 Add_Call_By_Copy_Code;
910 -- Deal with access types where the actual subtpe and the
911 -- formal subtype are not the same, requiring a check.
913 -- It is necessary to exclude tagged types because of "downward
914 -- conversion" errors and a strange assertion error in namet
915 -- from gnatf in bug 1215-001 ???
917 elsif Is_Access_Type (E_Formal)
918 and then not Same_Type (E_Formal, Etype (Actual))
919 and then not Is_Tagged_Type (Designated_Type (E_Formal))
921 Add_Call_By_Copy_Code;
923 elsif Is_Entity_Name (Actual)
924 and then Treat_As_Volatile (Entity (Actual))
925 and then not Is_Scalar_Type (Etype (Entity (Actual)))
926 and then not Treat_As_Volatile (E_Formal)
928 Add_Call_By_Copy_Code;
930 elsif Nkind (Actual) = N_Indexed_Component
931 and then Is_Entity_Name (Prefix (Actual))
932 and then Has_Volatile_Components (Entity (Prefix (Actual)))
934 Add_Call_By_Copy_Code;
937 -- Processing for IN parameters
940 -- For IN parameters is in the packed array case, we expand an
941 -- indexed component (the circuit in Exp_Ch4 deliberately left
942 -- indexed components appearing as actuals untouched, so that
943 -- the special processing above for the OUT and IN OUT cases
944 -- could be performed. We could make the test in Exp_Ch4 more
945 -- complex and have it detect the parameter mode, but it is
946 -- easier simply to handle all cases here.
948 if Nkind (Actual) = N_Indexed_Component
949 and then Is_Packed (Etype (Prefix (Actual)))
952 Expand_Packed_Element_Reference (Actual);
954 -- If we have a reference to a bit packed array, we copy it,
955 -- since the actual must be byte aligned.
957 -- Is this really necessary in all cases???
959 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
960 Add_Packed_Call_By_Copy_Code;
962 -- Similarly, we have to expand slices of packed arrays here
963 -- because the result must be byte aligned.
965 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
966 Add_Call_By_Copy_Code;
968 -- Only processing remaining is to pass by copy if this is a
969 -- reference to a possibly unaligned slice, since the caller
970 -- expects an appropriately aligned argument.
972 elsif Is_Possibly_Unaligned_Slice (Actual) then
973 Add_Call_By_Copy_Code;
977 Next_Formal (Formal);
978 Next_Actual (Actual);
981 -- Find right place to put post call stuff if it is present
983 if not Is_Empty_List (Post_Call) then
985 -- If call is not a list member, it must be the triggering
986 -- statement of a triggering alternative or an entry call
987 -- alternative, and we can add the post call stuff to the
988 -- corresponding statement list.
990 if not Is_List_Member (N) then
992 P : constant Node_Id := Parent (N);
995 pragma Assert (Nkind (P) = N_Triggering_Alternative
996 or else Nkind (P) = N_Entry_Call_Alternative);
998 if Is_Non_Empty_List (Statements (P)) then
999 Insert_List_Before_And_Analyze
1000 (First (Statements (P)), Post_Call);
1002 Set_Statements (P, Post_Call);
1006 -- Otherwise, normal case where N is in a statement sequence,
1007 -- just put the post-call stuff after the call statement.
1010 Insert_Actions_After (N, Post_Call);
1014 -- The call node itself is re-analyzed in Expand_Call.
1022 -- This procedure handles expansion of function calls and procedure call
1023 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1024 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1026 -- Replace call to Raise_Exception by Raise_Exception always if possible
1027 -- Provide values of actuals for all formals in Extra_Formals list
1028 -- Replace "call" to enumeration literal function by literal itself
1029 -- Rewrite call to predefined operator as operator
1030 -- Replace actuals to in-out parameters that are numeric conversions,
1031 -- with explicit assignment to temporaries before and after the call.
1032 -- Remove optional actuals if First_Optional_Parameter specified.
1034 -- Note that the list of actuals has been filled with default expressions
1035 -- during semantic analysis of the call. Only the extra actuals required
1036 -- for the 'Constrained attribute and for accessibility checks are added
1039 procedure Expand_Call (N : Node_Id) is
1040 Loc : constant Source_Ptr := Sloc (N);
1041 Remote : constant Boolean := Is_Remote_Call (N);
1043 Orig_Subp : Entity_Id := Empty;
1044 Parent_Subp : Entity_Id;
1045 Parent_Formal : Entity_Id;
1048 Prev : Node_Id := Empty;
1049 Prev_Orig : Node_Id;
1051 Extra_Actuals : List_Id := No_List;
1054 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1055 -- Adds one entry to the end of the actual parameter list. Used for
1056 -- default parameters and for extra actuals (for Extra_Formals).
1057 -- The argument is an N_Parameter_Association node.
1059 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1060 -- Adds an extra actual to the list of extra actuals. Expr
1061 -- is the expression for the value of the actual, EF is the
1062 -- entity for the extra formal.
1064 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1065 -- Within an instance, a type derived from a non-tagged formal derived
1066 -- type inherits from the original parent, not from the actual. This is
1067 -- tested in 4723-003. The current derivation mechanism has the derived
1068 -- type inherit from the actual, which is only correct outside of the
1069 -- instance. If the subprogram is inherited, we test for this particular
1070 -- case through a convoluted tree traversal before setting the proper
1071 -- subprogram to be called.
1073 --------------------------
1074 -- Add_Actual_Parameter --
1075 --------------------------
1077 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1078 Actual_Expr : constant Node_Id :=
1079 Explicit_Actual_Parameter (Insert_Param);
1082 -- Case of insertion is first named actual
1084 if No (Prev) or else
1085 Nkind (Parent (Prev)) /= N_Parameter_Association
1087 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1088 Set_First_Named_Actual (N, Actual_Expr);
1091 if not Present (Parameter_Associations (N)) then
1092 Set_Parameter_Associations (N, New_List);
1093 Append (Insert_Param, Parameter_Associations (N));
1096 Insert_After (Prev, Insert_Param);
1099 -- Case of insertion is not first named actual
1102 Set_Next_Named_Actual
1103 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1104 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1105 Append (Insert_Param, Parameter_Associations (N));
1108 Prev := Actual_Expr;
1109 end Add_Actual_Parameter;
1111 ----------------------
1112 -- Add_Extra_Actual --
1113 ----------------------
1115 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1116 Loc : constant Source_Ptr := Sloc (Expr);
1119 if Extra_Actuals = No_List then
1120 Extra_Actuals := New_List;
1121 Set_Parent (Extra_Actuals, N);
1124 Append_To (Extra_Actuals,
1125 Make_Parameter_Association (Loc,
1126 Explicit_Actual_Parameter => Expr,
1128 Make_Identifier (Loc, Chars (EF))));
1130 Analyze_And_Resolve (Expr, Etype (EF));
1131 end Add_Extra_Actual;
1133 ---------------------------
1134 -- Inherited_From_Formal --
1135 ---------------------------
1137 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1139 Gen_Par : Entity_Id;
1140 Gen_Prim : Elist_Id;
1145 -- If the operation is inherited, it is attached to the corresponding
1146 -- type derivation. If the parent in the derivation is a generic
1147 -- actual, it is a subtype of the actual, and we have to recover the
1148 -- original derived type declaration to find the proper parent.
1150 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1151 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1152 or else Nkind (Type_Definition (Original_Node (Parent (S))))
1153 /= N_Derived_Type_Definition
1154 or else not In_Instance
1161 (Type_Definition (Original_Node (Parent (S)))));
1163 if Nkind (Indic) = N_Subtype_Indication then
1164 Par := Entity (Subtype_Mark (Indic));
1166 Par := Entity (Indic);
1170 if not Is_Generic_Actual_Type (Par)
1171 or else Is_Tagged_Type (Par)
1172 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1173 or else not In_Open_Scopes (Scope (Par))
1178 Gen_Par := Generic_Parent_Type (Parent (Par));
1181 -- If the generic parent type is still the generic type, this
1182 -- is a private formal, not a derived formal, and there are no
1183 -- operations inherited from the formal.
1185 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1189 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1190 Elmt := First_Elmt (Gen_Prim);
1192 while Present (Elmt) loop
1193 if Chars (Node (Elmt)) = Chars (S) then
1199 F1 := First_Formal (S);
1200 F2 := First_Formal (Node (Elmt));
1203 and then Present (F2)
1206 if Etype (F1) = Etype (F2)
1207 or else Etype (F2) = Gen_Par
1213 exit; -- not the right subprogram
1225 raise Program_Error;
1226 end Inherited_From_Formal;
1228 -- Start of processing for Expand_Call
1231 -- Ignore if previous error
1233 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1237 -- Call using access to subprogram with explicit dereference
1239 if Nkind (Name (N)) = N_Explicit_Dereference then
1240 Subp := Etype (Name (N));
1241 Parent_Subp := Empty;
1243 -- Case of call to simple entry, where the Name is a selected component
1244 -- whose prefix is the task, and whose selector name is the entry name
1246 elsif Nkind (Name (N)) = N_Selected_Component then
1247 Subp := Entity (Selector_Name (Name (N)));
1248 Parent_Subp := Empty;
1250 -- Case of call to member of entry family, where Name is an indexed
1251 -- component, with the prefix being a selected component giving the
1252 -- task and entry family name, and the index being the entry index.
1254 elsif Nkind (Name (N)) = N_Indexed_Component then
1255 Subp := Entity (Selector_Name (Prefix (Name (N))));
1256 Parent_Subp := Empty;
1261 Subp := Entity (Name (N));
1262 Parent_Subp := Alias (Subp);
1264 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1265 -- if we can tell that the first parameter cannot possibly be null.
1266 -- This helps optimization and also generation of warnings.
1268 if not Restrictions (No_Exception_Handlers)
1269 and then Is_RTE (Subp, RE_Raise_Exception)
1272 FA : constant Node_Id := Original_Node (First_Actual (N));
1275 -- The case we catch is where the first argument is obtained
1276 -- using the Identity attribute (which must always be non-null)
1278 if Nkind (FA) = N_Attribute_Reference
1279 and then Attribute_Name (FA) = Name_Identity
1281 Subp := RTE (RE_Raise_Exception_Always);
1282 Set_Entity (Name (N), Subp);
1287 if Ekind (Subp) = E_Entry then
1288 Parent_Subp := Empty;
1292 -- First step, compute extra actuals, corresponding to any
1293 -- Extra_Formals present. Note that we do not access Extra_Formals
1294 -- directly, instead we simply note the presence of the extra
1295 -- formals as we process the regular formals and collect the
1296 -- corresponding actuals in Extra_Actuals.
1298 -- We also generate any required range checks for actuals as we go
1299 -- through the loop, since this is a convenient place to do this.
1301 Formal := First_Formal (Subp);
1302 Actual := First_Actual (N);
1303 while Present (Formal) loop
1305 -- Generate range check if required (not activated yet ???)
1307 -- if Do_Range_Check (Actual) then
1308 -- Set_Do_Range_Check (Actual, False);
1309 -- Generate_Range_Check
1310 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
1313 -- Prepare to examine current entry
1316 Prev_Orig := Original_Node (Prev);
1318 -- Create possible extra actual for constrained case. Usually,
1319 -- the extra actual is of the form actual'constrained, but since
1320 -- this attribute is only available for unconstrained records,
1321 -- TRUE is expanded if the type of the formal happens to be
1322 -- constrained (for instance when this procedure is inherited
1323 -- from an unconstrained record to a constrained one) or if the
1324 -- actual has no discriminant (its type is constrained). An
1325 -- exception to this is the case of a private type without
1326 -- discriminants. In this case we pass FALSE because the
1327 -- object has underlying discriminants with defaults.
1329 if Present (Extra_Constrained (Formal)) then
1330 if Ekind (Etype (Prev)) in Private_Kind
1331 and then not Has_Discriminants (Base_Type (Etype (Prev)))
1334 New_Occurrence_Of (Standard_False, Loc),
1335 Extra_Constrained (Formal));
1337 elsif Is_Constrained (Etype (Formal))
1338 or else not Has_Discriminants (Etype (Prev))
1341 New_Occurrence_Of (Standard_True, Loc),
1342 Extra_Constrained (Formal));
1345 -- If the actual is a type conversion, then the constrained
1346 -- test applies to the actual, not the target type.
1349 Act_Prev : Node_Id := Prev;
1352 -- Test for unchecked conversions as well, which can
1353 -- occur as out parameter actuals on calls to stream
1356 while Nkind (Act_Prev) = N_Type_Conversion
1357 or else Nkind (Act_Prev) = N_Unchecked_Type_Conversion
1359 Act_Prev := Expression (Act_Prev);
1363 Make_Attribute_Reference (Sloc (Prev),
1365 Duplicate_Subexpr_No_Checks
1366 (Act_Prev, Name_Req => True),
1367 Attribute_Name => Name_Constrained),
1368 Extra_Constrained (Formal));
1373 -- Create possible extra actual for accessibility level
1375 if Present (Extra_Accessibility (Formal)) then
1376 if Is_Entity_Name (Prev_Orig) then
1378 -- When passing an access parameter as the actual to another
1379 -- access parameter we need to pass along the actual's own
1380 -- associated access level parameter. This is done is we are
1381 -- in the scope of the formal access parameter (if this is an
1382 -- inlined body the extra formal is irrelevant).
1384 if Ekind (Entity (Prev_Orig)) in Formal_Kind
1385 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
1386 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
1389 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
1392 pragma Assert (Present (Parm_Ent));
1394 if Present (Extra_Accessibility (Parm_Ent)) then
1397 (Extra_Accessibility (Parm_Ent), Loc),
1398 Extra_Accessibility (Formal));
1400 -- If the actual access parameter does not have an
1401 -- associated extra formal providing its scope level,
1402 -- then treat the actual as having library-level
1407 Make_Integer_Literal (Loc,
1408 Intval => Scope_Depth (Standard_Standard)),
1409 Extra_Accessibility (Formal));
1413 -- The actual is a normal access value, so just pass the
1414 -- level of the actual's access type.
1418 Make_Integer_Literal (Loc,
1419 Intval => Type_Access_Level (Etype (Prev_Orig))),
1420 Extra_Accessibility (Formal));
1424 case Nkind (Prev_Orig) is
1426 when N_Attribute_Reference =>
1428 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
1430 -- For X'Access, pass on the level of the prefix X
1432 when Attribute_Access =>
1434 Make_Integer_Literal (Loc,
1436 Object_Access_Level (Prefix (Prev_Orig))),
1437 Extra_Accessibility (Formal));
1439 -- Treat the unchecked attributes as library-level
1441 when Attribute_Unchecked_Access |
1442 Attribute_Unrestricted_Access =>
1444 Make_Integer_Literal (Loc,
1445 Intval => Scope_Depth (Standard_Standard)),
1446 Extra_Accessibility (Formal));
1448 -- No other cases of attributes returning access
1449 -- values that can be passed to access parameters
1452 raise Program_Error;
1456 -- For allocators we pass the level of the execution of
1457 -- the called subprogram, which is one greater than the
1458 -- current scope level.
1462 Make_Integer_Literal (Loc,
1463 Scope_Depth (Current_Scope) + 1),
1464 Extra_Accessibility (Formal));
1466 -- For other cases we simply pass the level of the
1467 -- actual's access type.
1471 Make_Integer_Literal (Loc,
1472 Intval => Type_Access_Level (Etype (Prev_Orig))),
1473 Extra_Accessibility (Formal));
1479 -- Perform the check of 4.6(49) that prevents a null value
1480 -- from being passed as an actual to an access parameter.
1481 -- Note that the check is elided in the common cases of
1482 -- passing an access attribute or access parameter as an
1483 -- actual. Also, we currently don't enforce this check for
1484 -- expander-generated actuals and when -gnatdj is set.
1486 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
1487 or else Access_Checks_Suppressed (Subp)
1491 elsif Debug_Flag_J then
1494 elsif not Comes_From_Source (Prev) then
1497 elsif Is_Entity_Name (Prev)
1498 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
1502 elsif Nkind (Prev) = N_Allocator
1503 or else Nkind (Prev) = N_Attribute_Reference
1507 -- Suppress null checks when passing to access parameters
1508 -- of Java subprograms. (Should this be done for other
1509 -- foreign conventions as well ???)
1511 elsif Convention (Subp) = Convention_Java then
1517 Left_Opnd => Duplicate_Subexpr_No_Checks (Prev),
1518 Right_Opnd => Make_Null (Loc));
1519 Insert_Action (Prev,
1520 Make_Raise_Constraint_Error (Loc,
1522 Reason => CE_Access_Parameter_Is_Null));
1525 -- Perform appropriate validity checks on parameters that
1528 if Validity_Checks_On then
1529 if Ekind (Formal) = E_In_Parameter
1530 and then Validity_Check_In_Params
1531 and then Is_Entity_Name (Actual)
1533 Ensure_Valid (Actual);
1535 elsif Ekind (Formal) = E_In_Out_Parameter
1536 and then Validity_Check_In_Out_Params
1538 Ensure_Valid (Actual);
1542 -- For IN OUT and OUT parameters, ensure that subscripts are valid
1543 -- since this is a left side reference. We only do this for calls
1544 -- from the source program since we assume that compiler generated
1545 -- calls explicitly generate any required checks. We also need it
1546 -- only if we are doing standard validity checks, since clearly it
1547 -- is not needed if validity checks are off, and in subscript
1548 -- validity checking mode, all indexed components are checked with
1549 -- a call directly from Expand_N_Indexed_Component.
1551 if Comes_From_Source (N)
1552 and then Ekind (Formal) /= E_In_Parameter
1553 and then Validity_Checks_On
1554 and then Validity_Check_Default
1555 and then not Validity_Check_Subscripts
1557 Check_Valid_Lvalue_Subscripts (Actual);
1560 -- Mark any scalar OUT parameter that is a simple variable
1561 -- as no longer known to be valid (unless the type is always
1562 -- valid). This reflects the fact that if an OUT parameter
1563 -- is never set in a procedure, then it can become invalid
1564 -- on return from the procedure.
1566 if Ekind (Formal) = E_Out_Parameter
1567 and then Is_Entity_Name (Actual)
1568 and then Ekind (Entity (Actual)) = E_Variable
1569 and then not Is_Known_Valid (Etype (Actual))
1571 Set_Is_Known_Valid (Entity (Actual), False);
1574 -- For an OUT or IN OUT parameter of an access type, if the
1575 -- actual is an entity, then it is no longer known to be non-null.
1577 if Ekind (Formal) /= E_In_Parameter
1578 and then Is_Entity_Name (Actual)
1579 and then Is_Access_Type (Etype (Actual))
1581 Set_Is_Known_Non_Null (Entity (Actual), False);
1584 -- If the formal is class wide and the actual is an aggregate, force
1585 -- evaluation so that the back end who does not know about class-wide
1586 -- type, does not generate a temporary of the wrong size.
1588 if not Is_Class_Wide_Type (Etype (Formal)) then
1591 elsif Nkind (Actual) = N_Aggregate
1592 or else (Nkind (Actual) = N_Qualified_Expression
1593 and then Nkind (Expression (Actual)) = N_Aggregate)
1595 Force_Evaluation (Actual);
1598 -- In a remote call, if the formal is of a class-wide type, check
1599 -- that the actual meets the requirements described in E.4(18).
1602 and then Is_Class_Wide_Type (Etype (Formal))
1604 Insert_Action (Actual,
1605 Make_Implicit_If_Statement (N,
1608 Get_Remotely_Callable
1609 (Duplicate_Subexpr_Move_Checks (Actual))),
1610 Then_Statements => New_List (
1611 Make_Procedure_Call_Statement (Loc,
1612 New_Occurrence_Of (RTE
1613 (RE_Raise_Program_Error_For_E_4_18), Loc)))));
1616 Next_Actual (Actual);
1617 Next_Formal (Formal);
1620 -- If we are expanding a rhs of an assignement we need to check if
1621 -- tag propagation is needed. This code belongs theorically in Analyze
1622 -- Assignment but has to be done earlier (bottom-up) because the
1623 -- assignment might be transformed into a declaration for an uncons-
1624 -- trained value, if the expression is classwide.
1626 if Nkind (N) = N_Function_Call
1627 and then Is_Tag_Indeterminate (N)
1628 and then Is_Entity_Name (Name (N))
1631 Ass : Node_Id := Empty;
1634 if Nkind (Parent (N)) = N_Assignment_Statement then
1637 elsif Nkind (Parent (N)) = N_Qualified_Expression
1638 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
1640 Ass := Parent (Parent (N));
1644 and then Is_Class_Wide_Type (Etype (Name (Ass)))
1646 if Etype (N) /= Root_Type (Etype (Name (Ass))) then
1648 ("tag-indeterminate expression must have type&"
1649 & "('R'M 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
1651 Propagate_Tag (Name (Ass), N);
1654 -- The call will be rewritten as a dispatching call, and
1655 -- expanded as such.
1662 -- Deals with Dispatch_Call if we still have a call, before expanding
1663 -- extra actuals since this will be done on the re-analysis of the
1664 -- dispatching call. Note that we do not try to shorten the actual
1665 -- list for a dispatching call, it would not make sense to do so.
1666 -- Expansion of dispatching calls is suppressed when Java_VM, because
1667 -- the JVM back end directly handles the generation of dispatching
1668 -- calls and would have to undo any expansion to an indirect call.
1670 if (Nkind (N) = N_Function_Call
1671 or else Nkind (N) = N_Procedure_Call_Statement)
1672 and then Present (Controlling_Argument (N))
1673 and then not Java_VM
1675 Expand_Dispatch_Call (N);
1677 -- The following return is worrisome. Is it really OK to
1678 -- skip all remaining processing in this procedure ???
1682 -- Similarly, expand calls to RCI subprograms on which pragma
1683 -- All_Calls_Remote applies. The rewriting will be reanalyzed
1684 -- later. Do this only when the call comes from source since we do
1685 -- not want such a rewritting to occur in expanded code.
1687 elsif Is_All_Remote_Call (N) then
1688 Expand_All_Calls_Remote_Subprogram_Call (N);
1690 -- Similarly, do not add extra actuals for an entry call whose entity
1691 -- is a protected procedure, or for an internal protected subprogram
1692 -- call, because it will be rewritten as a protected subprogram call
1693 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
1695 elsif Is_Protected_Type (Scope (Subp))
1696 and then (Ekind (Subp) = E_Procedure
1697 or else Ekind (Subp) = E_Function)
1701 -- During that loop we gathered the extra actuals (the ones that
1702 -- correspond to Extra_Formals), so now they can be appended.
1705 while Is_Non_Empty_List (Extra_Actuals) loop
1706 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
1710 if Ekind (Subp) = E_Procedure
1711 or else (Ekind (Subp) = E_Subprogram_Type
1712 and then Etype (Subp) = Standard_Void_Type)
1713 or else Is_Entry (Subp)
1715 Expand_Actuals (N, Subp);
1718 -- If the subprogram is a renaming, or if it is inherited, replace it
1719 -- in the call with the name of the actual subprogram being called.
1720 -- If this is a dispatching call, the run-time decides what to call.
1721 -- The Alias attribute does not apply to entries.
1723 if Nkind (N) /= N_Entry_Call_Statement
1724 and then No (Controlling_Argument (N))
1725 and then Present (Parent_Subp)
1727 if Present (Inherited_From_Formal (Subp)) then
1728 Parent_Subp := Inherited_From_Formal (Subp);
1730 while Present (Alias (Parent_Subp)) loop
1731 Parent_Subp := Alias (Parent_Subp);
1735 Set_Entity (Name (N), Parent_Subp);
1737 if Is_Abstract (Parent_Subp)
1738 and then not In_Instance
1741 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
1744 -- Add an explicit conversion for parameter of the derived type.
1745 -- This is only done for scalar and access in-parameters. Others
1746 -- have been expanded in expand_actuals.
1748 Formal := First_Formal (Subp);
1749 Parent_Formal := First_Formal (Parent_Subp);
1750 Actual := First_Actual (N);
1752 -- It is not clear that conversion is needed for intrinsic
1753 -- subprograms, but it certainly is for those that are user-
1754 -- defined, and that can be inherited on derivation, namely
1755 -- unchecked conversion and deallocation.
1756 -- General case needs study ???
1758 if not Is_Intrinsic_Subprogram (Parent_Subp)
1759 or else Is_Generic_Instance (Parent_Subp)
1761 while Present (Formal) loop
1763 if Etype (Formal) /= Etype (Parent_Formal)
1764 and then Is_Scalar_Type (Etype (Formal))
1765 and then Ekind (Formal) = E_In_Parameter
1766 and then not Raises_Constraint_Error (Actual)
1769 OK_Convert_To (Etype (Parent_Formal),
1770 Relocate_Node (Actual)));
1773 Resolve (Actual, Etype (Parent_Formal));
1774 Enable_Range_Check (Actual);
1776 elsif Is_Access_Type (Etype (Formal))
1777 and then Base_Type (Etype (Parent_Formal))
1778 /= Base_Type (Etype (Actual))
1780 if Ekind (Formal) /= E_In_Parameter then
1782 Convert_To (Etype (Parent_Formal),
1783 Relocate_Node (Actual)));
1786 Resolve (Actual, Etype (Parent_Formal));
1789 Ekind (Etype (Parent_Formal)) = E_Anonymous_Access_Type
1790 and then Designated_Type (Etype (Parent_Formal))
1792 Designated_Type (Etype (Actual))
1793 and then not Is_Controlling_Formal (Formal)
1795 -- This unchecked conversion is not necessary unless
1796 -- inlining is enabled, because in that case the type
1797 -- mismatch may become visible in the body about to be
1801 Unchecked_Convert_To (Etype (Parent_Formal),
1802 Relocate_Node (Actual)));
1805 Resolve (Actual, Etype (Parent_Formal));
1809 Next_Formal (Formal);
1810 Next_Formal (Parent_Formal);
1811 Next_Actual (Actual);
1816 Subp := Parent_Subp;
1819 if Is_RTE (Subp, RE_Abort_Task) then
1820 Check_Restriction (No_Abort_Statements, N);
1823 -- Some more special cases for cases other than explicit dereference
1825 if Nkind (Name (N)) /= N_Explicit_Dereference then
1827 -- Calls to an enumeration literal are replaced by the literal
1828 -- This case occurs only when we have a call to a function that
1829 -- is a renaming of an enumeration literal. The normal case of
1830 -- a direct reference to an enumeration literal has already been
1831 -- been dealt with by Resolve_Call. If the function is itself
1832 -- inherited (see 7423-001) the literal of the parent type must
1833 -- be explicitly converted to the return type of the function.
1835 if Ekind (Subp) = E_Enumeration_Literal then
1836 if Base_Type (Etype (Subp)) /= Base_Type (Etype (N)) then
1838 (N, Convert_To (Etype (N), New_Occurrence_Of (Subp, Loc)));
1840 Rewrite (N, New_Occurrence_Of (Subp, Loc));
1846 -- Handle case of access to protected subprogram type
1849 if Ekind (Base_Type (Etype (Prefix (Name (N))))) =
1850 E_Access_Protected_Subprogram_Type
1852 -- If this is a call through an access to protected operation,
1853 -- the prefix has the form (object'address, operation'access).
1854 -- Rewrite as a for other protected calls: the object is the
1855 -- first parameter of the list of actuals.
1862 Ptr : constant Node_Id := Prefix (Name (N));
1864 T : constant Entity_Id :=
1865 Equivalent_Type (Base_Type (Etype (Ptr)));
1867 D_T : constant Entity_Id :=
1868 Designated_Type (Base_Type (Etype (Ptr)));
1871 Obj := Make_Selected_Component (Loc,
1872 Prefix => Unchecked_Convert_To (T, Ptr),
1873 Selector_Name => New_Occurrence_Of (First_Entity (T), Loc));
1875 Nam := Make_Selected_Component (Loc,
1876 Prefix => Unchecked_Convert_To (T, Ptr),
1877 Selector_Name => New_Occurrence_Of (
1878 Next_Entity (First_Entity (T)), Loc));
1880 Nam := Make_Explicit_Dereference (Loc, Nam);
1882 if Present (Parameter_Associations (N)) then
1883 Parm := Parameter_Associations (N);
1888 Prepend (Obj, Parm);
1890 if Etype (D_T) = Standard_Void_Type then
1891 Call := Make_Procedure_Call_Statement (Loc,
1893 Parameter_Associations => Parm);
1895 Call := Make_Function_Call (Loc,
1897 Parameter_Associations => Parm);
1900 Set_First_Named_Actual (Call, First_Named_Actual (N));
1901 Set_Etype (Call, Etype (D_T));
1903 -- We do not re-analyze the call to avoid infinite recursion.
1904 -- We analyze separately the prefix and the object, and set
1905 -- the checks on the prefix that would otherwise be emitted
1906 -- when resolving a call.
1910 Apply_Access_Check (Nam);
1917 -- If this is a call to an intrinsic subprogram, then perform the
1918 -- appropriate expansion to the corresponding tree node and we
1919 -- are all done (since after that the call is gone!)
1921 if Is_Intrinsic_Subprogram (Subp) then
1922 Expand_Intrinsic_Call (N, Subp);
1926 if Ekind (Subp) = E_Function
1927 or else Ekind (Subp) = E_Procedure
1929 if Is_Inlined (Subp) then
1933 Must_Inline : Boolean := False;
1934 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
1937 -- Verify that the body to inline has already been seen,
1938 -- and that if the body is in the current unit the inlining
1939 -- does not occur earlier. This avoids order-of-elaboration
1940 -- problems in gigi.
1943 or else Nkind (Spec) /= N_Subprogram_Declaration
1944 or else No (Body_To_Inline (Spec))
1946 Must_Inline := False;
1949 Bod := Body_To_Inline (Spec);
1951 if (In_Extended_Main_Code_Unit (N)
1952 or else In_Extended_Main_Code_Unit (Parent (N))
1953 or else Is_Always_Inlined (Subp))
1954 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
1956 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
1958 Must_Inline := True;
1960 -- If we are compiling a package body that is not the main
1961 -- unit, it must be for inlining/instantiation purposes,
1962 -- in which case we inline the call to insure that the same
1963 -- temporaries are generated when compiling the body by
1964 -- itself. Otherwise link errors can occur.
1966 elsif not (In_Extended_Main_Code_Unit (N))
1967 and then In_Package_Body
1969 Must_Inline := True;
1974 Expand_Inlined_Call (N, Subp, Orig_Subp);
1977 -- Let the back end handle it
1979 Add_Inlined_Body (Subp);
1981 if Front_End_Inlining
1982 and then Nkind (Spec) = N_Subprogram_Declaration
1983 and then (In_Extended_Main_Code_Unit (N))
1984 and then No (Body_To_Inline (Spec))
1985 and then not Has_Completion (Subp)
1986 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
1989 ("cannot inline& (body not seen yet)?",
1997 -- Check for a protected subprogram. This is either an intra-object
1998 -- call, or a protected function call. Protected procedure calls are
1999 -- rewritten as entry calls and handled accordingly.
2001 Scop := Scope (Subp);
2003 if Nkind (N) /= N_Entry_Call_Statement
2004 and then Is_Protected_Type (Scop)
2006 -- If the call is an internal one, it is rewritten as a call to
2007 -- to the corresponding unprotected subprogram.
2009 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2012 -- Functions returning controlled objects need special attention
2014 if Controlled_Type (Etype (Subp))
2015 and then not Is_Return_By_Reference_Type (Etype (Subp))
2017 Expand_Ctrl_Function_Call (N);
2020 -- Test for First_Optional_Parameter, and if so, truncate parameter
2021 -- list if there are optional parameters at the trailing end.
2022 -- Note we never delete procedures for call via a pointer.
2024 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2025 and then Present (First_Optional_Parameter (Subp))
2028 Last_Keep_Arg : Node_Id;
2031 -- Last_Keep_Arg will hold the last actual that should be
2032 -- retained. If it remains empty at the end, it means that
2033 -- all parameters are optional.
2035 Last_Keep_Arg := Empty;
2037 -- Find first optional parameter, must be present since we
2038 -- checked the validity of the parameter before setting it.
2040 Formal := First_Formal (Subp);
2041 Actual := First_Actual (N);
2042 while Formal /= First_Optional_Parameter (Subp) loop
2043 Last_Keep_Arg := Actual;
2044 Next_Formal (Formal);
2045 Next_Actual (Actual);
2048 -- We have Formal and Actual pointing to the first potentially
2049 -- droppable argument. We can drop all the trailing arguments
2050 -- whose actual matches the default. Note that we know that all
2051 -- remaining formals have defaults, because we checked that this
2052 -- requirement was met before setting First_Optional_Parameter.
2054 -- We use Fully_Conformant_Expressions to check for identity
2055 -- between formals and actuals, which may miss some cases, but
2056 -- on the other hand, this is only an optimization (if we fail
2057 -- to truncate a parameter it does not affect functionality).
2058 -- So if the default is 3 and the actual is 1+2, we consider
2059 -- them unequal, which hardly seems worrisome.
2061 while Present (Formal) loop
2062 if not Fully_Conformant_Expressions
2063 (Actual, Default_Value (Formal))
2065 Last_Keep_Arg := Actual;
2068 Next_Formal (Formal);
2069 Next_Actual (Actual);
2072 -- If no arguments, delete entire list, this is the easy case
2074 if No (Last_Keep_Arg) then
2075 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2076 Delete_Tree (Remove_Head (Parameter_Associations (N)));
2079 Set_Parameter_Associations (N, No_List);
2080 Set_First_Named_Actual (N, Empty);
2082 -- Case where at the last retained argument is positional. This
2083 -- is also an easy case, since the retained arguments are already
2084 -- in the right form, and we don't need to worry about the order
2085 -- of arguments that get eliminated.
2087 elsif Is_List_Member (Last_Keep_Arg) then
2088 while Present (Next (Last_Keep_Arg)) loop
2089 Delete_Tree (Remove_Next (Last_Keep_Arg));
2092 Set_First_Named_Actual (N, Empty);
2094 -- This is the annoying case where the last retained argument
2095 -- is a named parameter. Since the original arguments are not
2096 -- in declaration order, we may have to delete some fairly
2097 -- random collection of arguments.
2105 pragma Warnings (Off, Discard);
2108 -- First step, remove all the named parameters from the
2109 -- list (they are still chained using First_Named_Actual
2110 -- and Next_Named_Actual, so we have not lost them!)
2112 Temp := First (Parameter_Associations (N));
2114 -- Case of all parameters named, remove them all
2116 if Nkind (Temp) = N_Parameter_Association then
2117 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2118 Temp := Remove_Head (Parameter_Associations (N));
2121 -- Case of mixed positional/named, remove named parameters
2124 while Nkind (Next (Temp)) /= N_Parameter_Association loop
2128 while Present (Next (Temp)) loop
2129 Discard := Remove_Next (Temp);
2133 -- Now we loop through the named parameters, till we get
2134 -- to the last one to be retained, adding them to the list.
2135 -- Note that the Next_Named_Actual list does not need to be
2136 -- touched since we are only reordering them on the actual
2137 -- parameter association list.
2139 Passoc := Parent (First_Named_Actual (N));
2141 Temp := Relocate_Node (Passoc);
2143 (Parameter_Associations (N), Temp);
2145 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
2146 Passoc := Parent (Next_Named_Actual (Passoc));
2149 Set_Next_Named_Actual (Temp, Empty);
2152 Temp := Next_Named_Actual (Passoc);
2153 exit when No (Temp);
2154 Set_Next_Named_Actual
2155 (Passoc, Next_Named_Actual (Parent (Temp)));
2164 --------------------------
2165 -- Expand_Inlined_Call --
2166 --------------------------
2168 procedure Expand_Inlined_Call
2171 Orig_Subp : Entity_Id)
2173 Loc : constant Source_Ptr := Sloc (N);
2174 Is_Predef : constant Boolean :=
2175 Is_Predefined_File_Name
2176 (Unit_File_Name (Get_Source_Unit (Subp)));
2177 Orig_Bod : constant Node_Id :=
2178 Body_To_Inline (Unit_Declaration_Node (Subp));
2183 Exit_Lab : Entity_Id := Empty;
2190 Ret_Type : Entity_Id;
2193 Temp_Typ : Entity_Id;
2195 procedure Make_Exit_Label;
2196 -- Build declaration for exit label to be used in Return statements.
2198 function Process_Formals (N : Node_Id) return Traverse_Result;
2199 -- Replace occurrence of a formal with the corresponding actual, or
2200 -- the thunk generated for it.
2202 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2203 -- If the call being expanded is that of an internal subprogram,
2204 -- set the sloc of the generated block to that of the call itself,
2205 -- so that the expansion is skipped by the -next- command in gdb.
2206 -- Same processing for a subprogram in a predefined file, e.g.
2207 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2208 -- to simplify our own development.
2210 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2211 -- If the function body is a single expression, replace call with
2212 -- expression, else insert block appropriately.
2214 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2215 -- If procedure body has no local variables, inline body without
2216 -- creating block, otherwise rewrite call with block.
2218 ---------------------
2219 -- Make_Exit_Label --
2220 ---------------------
2222 procedure Make_Exit_Label is
2224 -- Create exit label for subprogram, if one doesn't exist yet.
2226 if No (Exit_Lab) then
2227 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
2229 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
2230 Exit_Lab := Make_Label (Loc, Lab_Id);
2233 Make_Implicit_Label_Declaration (Loc,
2234 Defining_Identifier => Entity (Lab_Id),
2235 Label_Construct => Exit_Lab);
2237 end Make_Exit_Label;
2239 ---------------------
2240 -- Process_Formals --
2241 ---------------------
2243 function Process_Formals (N : Node_Id) return Traverse_Result is
2249 if Is_Entity_Name (N)
2250 and then Present (Entity (N))
2255 and then Scope (E) = Subp
2257 A := Renamed_Object (E);
2259 if Is_Entity_Name (A) then
2260 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2262 elsif Nkind (A) = N_Defining_Identifier then
2263 Rewrite (N, New_Occurrence_Of (A, Loc));
2265 else -- numeric literal
2266 Rewrite (N, New_Copy (A));
2272 elsif Nkind (N) = N_Return_Statement then
2274 if No (Expression (N)) then
2276 Rewrite (N, Make_Goto_Statement (Loc,
2277 Name => New_Copy (Lab_Id)));
2280 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2281 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2283 -- Function body is a single expression. No need for
2289 Num_Ret := Num_Ret + 1;
2293 -- Because of the presence of private types, the views of the
2294 -- expression and the context may be different, so place an
2295 -- unchecked conversion to the context type to avoid spurious
2296 -- errors, eg. when the expression is a numeric literal and
2297 -- the context is private. If the expression is an aggregate,
2298 -- use a qualified expression, because an aggregate is not a
2299 -- legal argument of a conversion.
2301 if Nkind (Expression (N)) = N_Aggregate
2302 or else Nkind (Expression (N)) = N_Null
2305 Make_Qualified_Expression (Sloc (N),
2306 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2307 Expression => Relocate_Node (Expression (N)));
2310 Unchecked_Convert_To
2311 (Ret_Type, Relocate_Node (Expression (N)));
2314 if Nkind (Targ) = N_Defining_Identifier then
2316 Make_Assignment_Statement (Loc,
2317 Name => New_Occurrence_Of (Targ, Loc),
2318 Expression => Ret));
2321 Make_Assignment_Statement (Loc,
2322 Name => New_Copy (Targ),
2323 Expression => Ret));
2326 Set_Assignment_OK (Name (N));
2328 if Present (Exit_Lab) then
2330 Make_Goto_Statement (Loc,
2331 Name => New_Copy (Lab_Id)));
2337 -- Remove pragma Unreferenced since it may refer to formals that
2338 -- are not visible in the inlined body, and in any case we will
2339 -- not be posting warnings on the inlined body so it is unneeded.
2341 elsif Nkind (N) = N_Pragma
2342 and then Chars (N) = Name_Unreferenced
2344 Rewrite (N, Make_Null_Statement (Sloc (N)));
2350 end Process_Formals;
2352 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2358 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2360 if not Debug_Generated_Code then
2361 Set_Sloc (Nod, Sloc (N));
2362 Set_Comes_From_Source (Nod, False);
2368 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2370 ---------------------------
2371 -- Rewrite_Function_Call --
2372 ---------------------------
2374 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2375 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2376 Fst : constant Node_Id := First (Statements (HSS));
2379 -- Optimize simple case: function body is a single return statement,
2380 -- which has been expanded into an assignment.
2382 if Is_Empty_List (Declarations (Blk))
2383 and then Nkind (Fst) = N_Assignment_Statement
2384 and then No (Next (Fst))
2387 -- The function call may have been rewritten as the temporary
2388 -- that holds the result of the call, in which case remove the
2389 -- now useless declaration.
2391 if Nkind (N) = N_Identifier
2392 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2394 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2397 Rewrite (N, Expression (Fst));
2399 elsif Nkind (N) = N_Identifier
2400 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2403 -- The block assigns the result of the call to the temporary.
2405 Insert_After (Parent (Entity (N)), Blk);
2407 elsif Nkind (Parent (N)) = N_Assignment_Statement
2408 and then Is_Entity_Name (Name (Parent (N)))
2411 -- Replace assignment with the block
2413 Rewrite (Parent (N), Blk);
2415 elsif Nkind (Parent (N)) = N_Object_Declaration then
2416 Set_Expression (Parent (N), Empty);
2417 Insert_After (Parent (N), Blk);
2419 end Rewrite_Function_Call;
2421 ----------------------------
2422 -- Rewrite_Procedure_Call --
2423 ----------------------------
2425 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2426 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2429 if Is_Empty_List (Declarations (Blk)) then
2430 Insert_List_After (N, Statements (HSS));
2431 Rewrite (N, Make_Null_Statement (Loc));
2435 end Rewrite_Procedure_Call;
2437 -- Start of processing for Expand_Inlined_Call
2440 -- Check for special case of To_Address call, and if so, just
2441 -- do an unchecked conversion instead of expanding the call.
2442 -- Not only is this more efficient, but it also avoids a
2443 -- problem with order of elaboration when address clauses
2444 -- are inlined (address expr elaborated at wrong point).
2446 if Subp = RTE (RE_To_Address) then
2448 Unchecked_Convert_To
2450 Relocate_Node (First_Actual (N))));
2454 if Nkind (Orig_Bod) = N_Defining_Identifier then
2456 -- Subprogram is a renaming_as_body. Calls appearing after the
2457 -- renaming can be replaced with calls to the renamed entity
2458 -- directly, because the subprograms are subtype conformant.
2460 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2464 -- Use generic machinery to copy body of inlined subprogram, as if it
2465 -- were an instantiation, resetting source locations appropriately, so
2466 -- that nested inlined calls appear in the main unit.
2468 Save_Env (Subp, Empty);
2469 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2471 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2473 Make_Block_Statement (Loc,
2474 Declarations => Declarations (Bod),
2475 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
2477 if No (Declarations (Bod)) then
2478 Set_Declarations (Blk, New_List);
2481 -- If this is a derived function, establish the proper return type.
2483 if Present (Orig_Subp)
2484 and then Orig_Subp /= Subp
2486 Ret_Type := Etype (Orig_Subp);
2488 Ret_Type := Etype (Subp);
2491 F := First_Formal (Subp);
2492 A := First_Actual (N);
2494 -- Create temporaries for the actuals that are expressions, or that
2495 -- are scalars and require copying to preserve semantics.
2497 while Present (F) loop
2498 if Present (Renamed_Object (F)) then
2499 Error_Msg_N (" cannot inline call to recursive subprogram", N);
2503 -- If the argument may be a controlling argument in a call within
2504 -- the inlined body, we must preserve its classwide nature to
2505 -- insure that dynamic dispatching take place subsequently.
2506 -- If the formal has a constraint it must be preserved to retain
2507 -- the semantics of the body.
2509 if Is_Class_Wide_Type (Etype (F))
2510 or else (Is_Access_Type (Etype (F))
2512 Is_Class_Wide_Type (Designated_Type (Etype (F))))
2514 Temp_Typ := Etype (F);
2516 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
2517 and then Etype (F) /= Base_Type (Etype (F))
2519 Temp_Typ := Etype (F);
2522 Temp_Typ := Etype (A);
2525 -- Comments needed here ???
2527 if (Is_Entity_Name (A)
2529 (not Is_Scalar_Type (Etype (A))
2530 or else Ekind (Entity (A)) = E_Enumeration_Literal))
2532 or else Nkind (A) = N_Real_Literal
2533 or else Nkind (A) = N_Integer_Literal
2534 or else Nkind (A) = N_Character_Literal
2536 if Etype (F) /= Etype (A) then
2538 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
2540 Set_Renamed_Object (F, A);
2545 Make_Defining_Identifier (Loc,
2546 Chars => New_Internal_Name ('C'));
2548 -- If the actual for an in/in-out parameter is a view conversion,
2549 -- make it into an unchecked conversion, given that an untagged
2550 -- type conversion is not a proper object for a renaming.
2552 -- In-out conversions that involve real conversions have already
2553 -- been transformed in Expand_Actuals.
2555 if Nkind (A) = N_Type_Conversion
2556 and then Ekind (F) /= E_In_Parameter
2558 New_A := Make_Unchecked_Type_Conversion (Loc,
2559 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
2560 Expression => Relocate_Node (Expression (A)));
2562 elsif Etype (F) /= Etype (A) then
2563 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
2564 Temp_Typ := Etype (F);
2567 New_A := Relocate_Node (A);
2570 Set_Sloc (New_A, Sloc (N));
2572 if Ekind (F) = E_In_Parameter
2573 and then not Is_Limited_Type (Etype (A))
2576 Make_Object_Declaration (Loc,
2577 Defining_Identifier => Temp,
2578 Constant_Present => True,
2579 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
2580 Expression => New_A);
2583 Make_Object_Renaming_Declaration (Loc,
2584 Defining_Identifier => Temp,
2585 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
2589 Prepend (Decl, Declarations (Blk));
2590 Set_Renamed_Object (F, Temp);
2597 -- Establish target of function call. If context is not assignment or
2598 -- declaration, create a temporary as a target. The declaration for
2599 -- the temporary may be subsequently optimized away if the body is a
2600 -- single expression, or if the left-hand side of the assignment is
2603 if Ekind (Subp) = E_Function then
2604 if Nkind (Parent (N)) = N_Assignment_Statement
2605 and then Is_Entity_Name (Name (Parent (N)))
2607 Targ := Name (Parent (N));
2610 -- Replace call with temporary, and create its declaration.
2613 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2616 Make_Object_Declaration (Loc,
2617 Defining_Identifier => Temp,
2618 Object_Definition =>
2619 New_Occurrence_Of (Ret_Type, Loc));
2621 Set_No_Initialization (Decl);
2622 Insert_Action (N, Decl);
2623 Rewrite (N, New_Occurrence_Of (Temp, Loc));
2628 -- Traverse the tree and replace formals with actuals or their thunks.
2629 -- Attach block to tree before analysis and rewriting.
2631 Replace_Formals (Blk);
2632 Set_Parent (Blk, N);
2634 if not Comes_From_Source (Subp)
2640 if Present (Exit_Lab) then
2642 -- If the body was a single expression, the single return statement
2643 -- and the corresponding label are useless.
2647 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
2650 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
2652 Append (Lab_Decl, (Declarations (Blk)));
2653 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
2657 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
2658 -- conflicting private views that Gigi would ignore. If this is a
2659 -- predefined unit, analyze with checks off, as is done in the non-
2660 -- inlined run-time units.
2663 I_Flag : constant Boolean := In_Inlined_Body;
2666 In_Inlined_Body := True;
2670 Style : constant Boolean := Style_Check;
2672 Style_Check := False;
2673 Analyze (Blk, Suppress => All_Checks);
2674 Style_Check := Style;
2681 In_Inlined_Body := I_Flag;
2684 if Ekind (Subp) = E_Procedure then
2685 Rewrite_Procedure_Call (N, Blk);
2687 Rewrite_Function_Call (N, Blk);
2692 -- Cleanup mapping between formals and actuals, for other expansions.
2694 F := First_Formal (Subp);
2696 while Present (F) loop
2697 Set_Renamed_Object (F, Empty);
2700 end Expand_Inlined_Call;
2702 ----------------------------
2703 -- Expand_N_Function_Call --
2704 ----------------------------
2706 procedure Expand_N_Function_Call (N : Node_Id) is
2707 Typ : constant Entity_Id := Etype (N);
2709 function Returned_By_Reference return Boolean;
2710 -- If the return type is returned through the secondary stack. that is
2711 -- by reference, we don't want to create a temp to force stack checking.
2713 function Returned_By_Reference return Boolean is
2714 S : Entity_Id := Current_Scope;
2717 if Is_Return_By_Reference_Type (Typ) then
2720 elsif Nkind (Parent (N)) /= N_Return_Statement then
2723 elsif Requires_Transient_Scope (Typ) then
2725 -- Verify that the return type of the enclosing function has
2726 -- the same constrained status as that of the expression.
2728 while Ekind (S) /= E_Function loop
2732 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
2736 end Returned_By_Reference;
2738 -- Start of processing for Expand_N_Function_Call
2741 -- A special check. If stack checking is enabled, and the return type
2742 -- might generate a large temporary, and the call is not the right
2743 -- side of an assignment, then generate an explicit temporary. We do
2744 -- this because otherwise gigi may generate a large temporary on the
2745 -- fly and this can cause trouble with stack checking.
2747 if May_Generate_Large_Temp (Typ)
2748 and then Nkind (Parent (N)) /= N_Assignment_Statement
2750 (Nkind (Parent (N)) /= N_Qualified_Expression
2751 or else Nkind (Parent (Parent (N))) /= N_Assignment_Statement)
2753 (Nkind (Parent (N)) /= N_Object_Declaration
2754 or else Expression (Parent (N)) /= N)
2755 and then not Returned_By_Reference
2757 -- Note: it might be thought that it would be OK to use a call to
2758 -- Force_Evaluation here, but that's not good enough, because that
2759 -- results in a 'Reference construct that may still need a temporary.
2762 Loc : constant Source_Ptr := Sloc (N);
2763 Temp_Obj : constant Entity_Id :=
2764 Make_Defining_Identifier (Loc,
2765 Chars => New_Internal_Name ('F'));
2766 Temp_Typ : Entity_Id := Typ;
2773 if Is_Tagged_Type (Typ)
2774 and then Present (Controlling_Argument (N))
2776 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
2777 and then Nkind (Parent (N)) /= N_Function_Call
2779 -- If this is a tag-indeterminate call, the object must
2782 if Is_Tag_Indeterminate (N) then
2783 Temp_Typ := Class_Wide_Type (Typ);
2787 -- If this is a dispatching call that is itself the
2788 -- controlling argument of an enclosing call, the nominal
2789 -- subtype of the object that replaces it must be classwide,
2790 -- so that dispatching will take place properly. If it is
2791 -- not a controlling argument, the object is not classwide.
2793 Proc := Entity (Name (Parent (N)));
2794 F := First_Formal (Proc);
2795 A := First_Actual (Parent (N));
2802 if Is_Controlling_Formal (F) then
2803 Temp_Typ := Class_Wide_Type (Typ);
2809 Make_Object_Declaration (Loc,
2810 Defining_Identifier => Temp_Obj,
2811 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
2812 Constant_Present => True,
2813 Expression => Relocate_Node (N));
2814 Set_Assignment_OK (Decl);
2816 Insert_Actions (N, New_List (Decl));
2817 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
2820 -- Normal case, expand the call
2825 end Expand_N_Function_Call;
2827 ---------------------------------------
2828 -- Expand_N_Procedure_Call_Statement --
2829 ---------------------------------------
2831 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
2834 end Expand_N_Procedure_Call_Statement;
2836 ------------------------------
2837 -- Expand_N_Subprogram_Body --
2838 ------------------------------
2840 -- Add poll call if ATC polling is enabled
2842 -- Add return statement if last statement in body is not a return
2843 -- statement (this makes things easier on Gigi which does not want
2844 -- to have to handle a missing return).
2846 -- Add call to Activate_Tasks if body is a task activator
2848 -- Deal with possible detection of infinite recursion
2850 -- Eliminate body completely if convention stubbed
2852 -- Encode entity names within body, since we will not need to reference
2853 -- these entities any longer in the front end.
2855 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
2857 -- Reset Pure indication if any parameter has root type System.Address
2861 procedure Expand_N_Subprogram_Body (N : Node_Id) is
2862 Loc : constant Source_Ptr := Sloc (N);
2863 H : constant Node_Id := Handled_Statement_Sequence (N);
2864 Body_Id : Entity_Id;
2865 Spec_Id : Entity_Id;
2872 procedure Add_Return (S : List_Id);
2873 -- Append a return statement to the statement sequence S if the last
2874 -- statement is not already a return or a goto statement. Note that
2875 -- the latter test is not critical, it does not matter if we add a
2876 -- few extra returns, since they get eliminated anyway later on.
2878 procedure Expand_Thread_Body;
2879 -- Perform required expansion of a thread body
2885 procedure Add_Return (S : List_Id) is
2887 if not Is_Transfer (Last (S)) then
2889 -- The source location for the return is the end label
2890 -- of the procedure in all cases. This is a bit odd when
2891 -- there are exception handlers, but not much else we can do.
2893 Append_To (S, Make_Return_Statement (Sloc (End_Label (H))));
2897 ------------------------
2898 -- Expand_Thread_Body --
2899 ------------------------
2901 -- The required expansion of a thread body is as follows
2903 -- procedure <thread body procedure name> is
2905 -- _Secondary_Stack : aliased
2906 -- Storage_Elements.Storage_Array
2907 -- (1 .. Storage_Offset (Sec_Stack_Size));
2908 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
2910 -- _Process_ATSD : aliased System.Threads.ATSD;
2913 -- System.Threads.Thread_Body_Enter;
2914 -- (_Secondary_Stack'Address,
2915 -- _Secondary_Stack'Length,
2916 -- _Process_ATSD'Address);
2919 -- <user declarations>
2921 -- <user statements>
2922 -- <user exception handlers>
2925 -- System.Threads.Thread_Body_Leave;
2928 -- when E : others =>
2929 -- System.Threads.Thread_Body_Exceptional_Exit (E);
2932 -- Note the exception handler is omitted if pragma Restriction
2933 -- No_Exception_Handlers is currently active.
2935 procedure Expand_Thread_Body is
2936 User_Decls : constant List_Id := Declarations (N);
2937 Sec_Stack_Len : Node_Id;
2939 TB_Pragma : constant Node_Id :=
2940 Get_Rep_Pragma (Spec_Id, Name_Thread_Body);
2943 Ent_ATSD : Entity_Id;
2947 Decl_ATSD : Node_Id;
2949 Excep_Handlers : List_Id;
2952 New_Scope (Spec_Id);
2954 -- Get proper setting for secondary stack size
2956 if List_Length (Pragma_Argument_Associations (TB_Pragma)) = 2 then
2958 Expression (Last (Pragma_Argument_Associations (TB_Pragma)));
2961 Make_Integer_Literal (Loc,
2964 (Expression (RTE (RE_Default_Secondary_Stack_Size))));
2967 Sec_Stack_Len := Convert_To (RTE (RE_Storage_Offset), Sec_Stack_Len);
2969 -- Build and set declarations for the wrapped thread body
2971 Ent_SS := Make_Defining_Identifier (Loc, Name_uSecondary_Stack);
2972 Ent_ATSD := Make_Defining_Identifier (Loc, Name_uProcess_ATSD);
2975 Make_Object_Declaration (Loc,
2976 Defining_Identifier => Ent_SS,
2977 Aliased_Present => True,
2978 Object_Definition =>
2979 Make_Subtype_Indication (Loc,
2981 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
2983 Make_Index_Or_Discriminant_Constraint (Loc,
2984 Constraints => New_List (
2986 Low_Bound => Make_Integer_Literal (Loc, 1),
2987 High_Bound => Sec_Stack_Len)))));
2990 Make_Object_Declaration (Loc,
2991 Defining_Identifier => Ent_ATSD,
2992 Aliased_Present => True,
2993 Object_Definition => New_Occurrence_Of (RTE (RE_ATSD), Loc));
2995 Set_Declarations (N, New_List (Decl_SS, Decl_ATSD));
2997 Analyze (Decl_ATSD);
2998 Set_Alignment (Ent_SS, UI_From_Int (Maximum_Alignment));
3000 -- Create new exception handler
3002 if Restrictions (No_Exception_Handlers) then
3003 Excep_Handlers := No_List;
3006 Check_Restriction (No_Exception_Handlers, N);
3008 Ent_EO := Make_Defining_Identifier (Loc, Name_uE);
3010 Excep_Handlers := New_List (
3011 Make_Exception_Handler (Loc,
3012 Choice_Parameter => Ent_EO,
3013 Exception_Choices => New_List (
3014 Make_Others_Choice (Loc)),
3015 Statements => New_List (
3016 Make_Procedure_Call_Statement (Loc,
3019 (RTE (RE_Thread_Body_Exceptional_Exit), Loc),
3020 Parameter_Associations => New_List (
3021 New_Occurrence_Of (Ent_EO, Loc))))));
3024 -- Now build new handled statement sequence and analyze it
3026 Set_Handled_Statement_Sequence (N,
3027 Make_Handled_Sequence_Of_Statements (Loc,
3028 Statements => New_List (
3030 Make_Procedure_Call_Statement (Loc,
3031 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Enter), Loc),
3032 Parameter_Associations => New_List (
3034 Make_Attribute_Reference (Loc,
3035 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3036 Attribute_Name => Name_Address),
3038 Make_Attribute_Reference (Loc,
3039 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3040 Attribute_Name => Name_Length),
3042 Make_Attribute_Reference (Loc,
3043 Prefix => New_Occurrence_Of (Ent_ATSD, Loc),
3044 Attribute_Name => Name_Address))),
3046 Make_Block_Statement (Loc,
3047 Declarations => User_Decls,
3048 Handled_Statement_Sequence => H),
3050 Make_Procedure_Call_Statement (Loc,
3051 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Leave), Loc))),
3053 Exception_Handlers => Excep_Handlers));
3055 Analyze (Handled_Statement_Sequence (N));
3057 end Expand_Thread_Body;
3059 -- Start of processing for Expand_N_Subprogram_Body
3062 -- Set L to either the list of declarations if present, or
3063 -- to the list of statements if no declarations are present.
3064 -- This is used to insert new stuff at the start.
3066 if Is_Non_Empty_List (Declarations (N)) then
3067 L := Declarations (N);
3069 L := Statements (Handled_Statement_Sequence (N));
3072 -- Need poll on entry to subprogram if polling enabled. We only
3073 -- do this for non-empty subprograms, since it does not seem
3074 -- necessary to poll for a dummy null subprogram.
3076 if Is_Non_Empty_List (L) then
3077 Generate_Poll_Call (First (L));
3080 -- Find entity for subprogram
3082 Body_Id := Defining_Entity (N);
3084 if Present (Corresponding_Spec (N)) then
3085 Spec_Id := Corresponding_Spec (N);
3090 -- If this is a Pure function which has any parameters whose root
3091 -- type is System.Address, reset the Pure indication, since it will
3092 -- likely cause incorrect code to be generated.
3094 if Is_Pure (Spec_Id)
3095 and then Is_Subprogram (Spec_Id)
3096 and then not Has_Pragma_Pure_Function (Spec_Id)
3099 F : Entity_Id := First_Formal (Spec_Id);
3102 while Present (F) loop
3103 if Is_RTE (Root_Type (Etype (F)), RE_Address) then
3104 Set_Is_Pure (Spec_Id, False);
3106 if Spec_Id /= Body_Id then
3107 Set_Is_Pure (Body_Id, False);
3118 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3120 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
3122 F : Entity_Id := First_Formal (Spec_Id);
3123 V : constant Boolean := Validity_Checks_On;
3126 -- We turn off validity checking, since we do not want any
3127 -- check on the initializing value itself (which we know
3128 -- may well be invalid!)
3130 Validity_Checks_On := False;
3132 -- Loop through formals
3134 while Present (F) loop
3135 if Is_Scalar_Type (Etype (F))
3136 and then Ekind (F) = E_Out_Parameter
3138 Insert_Before_And_Analyze (First (L),
3139 Make_Assignment_Statement (Loc,
3140 Name => New_Occurrence_Of (F, Loc),
3141 Expression => Get_Simple_Init_Val (Etype (F), Loc)));
3147 Validity_Checks_On := V;
3151 -- Clear out statement list for stubbed procedure
3153 if Present (Corresponding_Spec (N)) then
3154 Set_Elaboration_Flag (N, Spec_Id);
3156 if Convention (Spec_Id) = Convention_Stubbed
3157 or else Is_Eliminated (Spec_Id)
3159 Set_Declarations (N, Empty_List);
3160 Set_Handled_Statement_Sequence (N,
3161 Make_Handled_Sequence_Of_Statements (Loc,
3162 Statements => New_List (
3163 Make_Null_Statement (Loc))));
3168 Scop := Scope (Spec_Id);
3170 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3171 -- but subprograms with no specs are not frozen
3174 Typ : constant Entity_Id := Etype (Spec_Id);
3175 Utyp : constant Entity_Id := Underlying_Type (Typ);
3178 if not Acts_As_Spec (N)
3179 and then Nkind (Parent (Parent (Spec_Id))) /=
3180 N_Subprogram_Body_Stub
3184 elsif Is_Return_By_Reference_Type (Typ) then
3185 Set_Returns_By_Ref (Spec_Id);
3187 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3188 Set_Returns_By_Ref (Spec_Id);
3192 -- For a procedure, we add a return for all possible syntactic ends
3193 -- of the subprogram. Note that reanalysis is not necessary in this
3194 -- case since it would require a lot of work and accomplish nothing.
3196 if Ekind (Spec_Id) = E_Procedure
3197 or else Ekind (Spec_Id) = E_Generic_Procedure
3199 Add_Return (Statements (H));
3201 if Present (Exception_Handlers (H)) then
3202 Except_H := First_Non_Pragma (Exception_Handlers (H));
3204 while Present (Except_H) loop
3205 Add_Return (Statements (Except_H));
3206 Next_Non_Pragma (Except_H);
3210 -- For a function, we must deal with the case where there is at
3211 -- least one missing return. What we do is to wrap the entire body
3212 -- of the function in a block:
3225 -- raise Program_Error;
3228 -- This approach is necessary because the raise must be signalled
3229 -- to the caller, not handled by any local handler (RM 6.4(11)).
3231 -- Note: we do not need to analyze the constructed sequence here,
3232 -- since it has no handler, and an attempt to analyze the handled
3233 -- statement sequence twice is risky in various ways (e.g. the
3234 -- issue of expanding cleanup actions twice).
3236 elsif Has_Missing_Return (Spec_Id) then
3238 Hloc : constant Source_Ptr := Sloc (H);
3239 Blok : constant Node_Id :=
3240 Make_Block_Statement (Hloc,
3241 Handled_Statement_Sequence => H);
3242 Rais : constant Node_Id :=
3243 Make_Raise_Program_Error (Hloc,
3244 Reason => PE_Missing_Return);
3247 Set_Handled_Statement_Sequence (N,
3248 Make_Handled_Sequence_Of_Statements (Hloc,
3249 Statements => New_List (Blok, Rais)));
3251 New_Scope (Spec_Id);
3258 -- Add discriminal renamings to protected subprograms.
3259 -- Install new discriminals for expansion of the next
3260 -- subprogram of this protected type, if any.
3262 if Is_List_Member (N)
3263 and then Present (Parent (List_Containing (N)))
3264 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3266 Add_Discriminal_Declarations
3267 (Declarations (N), Scop, Name_uObject, Loc);
3268 Add_Private_Declarations (Declarations (N), Scop, Name_uObject, Loc);
3270 -- Associate privals and discriminals with the next protected
3271 -- operation body to be expanded. These are used to expand
3272 -- references to private data objects and discriminants,
3275 Next_Op := Next_Protected_Operation (N);
3277 if Present (Next_Op) then
3278 Dec := Parent (Base_Type (Scop));
3279 Set_Privals (Dec, Next_Op, Loc);
3280 Set_Discriminals (Dec);
3284 -- If subprogram contains a parameterless recursive call, then we may
3285 -- have an infinite recursion, so see if we can generate code to check
3286 -- for this possibility if storage checks are not suppressed.
3288 if Ekind (Spec_Id) = E_Procedure
3289 and then Has_Recursive_Call (Spec_Id)
3290 and then not Storage_Checks_Suppressed (Spec_Id)
3292 Detect_Infinite_Recursion (N, Spec_Id);
3295 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3296 -- parameters must be initialized to the appropriate default value.
3298 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
3305 Formal := First_Formal (Spec_Id);
3307 while Present (Formal) loop
3308 Floc := Sloc (Formal);
3310 if Ekind (Formal) = E_Out_Parameter
3311 and then Is_Scalar_Type (Etype (Formal))
3314 Make_Assignment_Statement (Floc,
3315 Name => New_Occurrence_Of (Formal, Floc),
3317 Get_Simple_Init_Val (Etype (Formal), Floc));
3318 Prepend (Stm, Declarations (N));
3322 Next_Formal (Formal);
3327 -- Deal with thread body
3329 if Is_Thread_Body (Spec_Id) then
3333 -- If the subprogram does not have pending instantiations, then we
3334 -- must generate the subprogram descriptor now, since the code for
3335 -- the subprogram is complete, and this is our last chance. However
3336 -- if there are pending instantiations, then the code is not
3337 -- complete, and we will delay the generation.
3339 if Is_Subprogram (Spec_Id)
3340 and then not Delay_Subprogram_Descriptors (Spec_Id)
3342 Generate_Subprogram_Descriptor_For_Subprogram (N, Spec_Id);
3345 -- Set to encode entity names in package body before gigi is called
3347 Qualify_Entity_Names (N);
3348 end Expand_N_Subprogram_Body;
3350 -----------------------------------
3351 -- Expand_N_Subprogram_Body_Stub --
3352 -----------------------------------
3354 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
3356 if Present (Corresponding_Body (N)) then
3357 Expand_N_Subprogram_Body (
3358 Unit_Declaration_Node (Corresponding_Body (N)));
3360 end Expand_N_Subprogram_Body_Stub;
3362 -------------------------------------
3363 -- Expand_N_Subprogram_Declaration --
3364 -------------------------------------
3366 -- If the declaration appears within a protected body, it is a private
3367 -- operation of the protected type. We must create the corresponding
3368 -- protected subprogram an associated formals. For a normal protected
3369 -- operation, this is done when expanding the protected type declaration.
3371 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
3372 Loc : constant Source_Ptr := Sloc (N);
3373 Subp : constant Entity_Id := Defining_Entity (N);
3374 Scop : constant Entity_Id := Scope (Subp);
3375 Prot_Decl : Node_Id;
3377 Prot_Id : Entity_Id;
3380 -- Deal with case of protected subprogram
3382 if Is_List_Member (N)
3383 and then Present (Parent (List_Containing (N)))
3384 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3385 and then Is_Protected_Type (Scop)
3387 if No (Protected_Body_Subprogram (Subp)) then
3389 Make_Subprogram_Declaration (Loc,
3391 Build_Protected_Sub_Specification
3392 (N, Scop, Unprotected => True));
3394 -- The protected subprogram is declared outside of the protected
3395 -- body. Given that the body has frozen all entities so far, we
3396 -- analyze the subprogram and perform freezing actions explicitly.
3397 -- If the body is a subunit, the insertion point is before the
3398 -- stub in the parent.
3400 Prot_Bod := Parent (List_Containing (N));
3402 if Nkind (Parent (Prot_Bod)) = N_Subunit then
3403 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
3406 Insert_Before (Prot_Bod, Prot_Decl);
3407 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
3409 New_Scope (Scope (Scop));
3410 Analyze (Prot_Decl);
3411 Create_Extra_Formals (Prot_Id);
3412 Set_Protected_Body_Subprogram (Subp, Prot_Id);
3416 end Expand_N_Subprogram_Declaration;
3418 ---------------------------------------
3419 -- Expand_Protected_Object_Reference --
3420 ---------------------------------------
3422 function Expand_Protected_Object_Reference
3427 Loc : constant Source_Ptr := Sloc (N);
3434 Rec := Make_Identifier (Loc, Name_uObject);
3435 Set_Etype (Rec, Corresponding_Record_Type (Scop));
3437 -- Find enclosing protected operation, and retrieve its first
3438 -- parameter, which denotes the enclosing protected object.
3439 -- If the enclosing operation is an entry, we are immediately
3440 -- within the protected body, and we can retrieve the object
3441 -- from the service entries procedure. A barrier function has
3442 -- has the same signature as an entry. A barrier function is
3443 -- compiled within the protected object, but unlike protected
3444 -- operations its never needs locks, so that its protected body
3445 -- subprogram points to itself.
3447 Proc := Current_Scope;
3449 while Present (Proc)
3450 and then Scope (Proc) /= Scop
3452 Proc := Scope (Proc);
3455 Corr := Protected_Body_Subprogram (Proc);
3459 -- Previous error left expansion incomplete.
3460 -- Nothing to do on this call.
3467 (First (Parameter_Specifications (Parent (Corr))));
3469 if Is_Subprogram (Proc)
3470 and then Proc /= Corr
3472 -- Protected function or procedure.
3474 Set_Entity (Rec, Param);
3476 -- Rec is a reference to an entity which will not be in scope
3477 -- when the call is reanalyzed, and needs no further analysis.
3482 -- Entry or barrier function for entry body.
3483 -- The first parameter of the entry body procedure is a
3484 -- pointer to the object. We create a local variable
3485 -- of the proper type, duplicating what is done to define
3486 -- _object later on.
3490 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
3492 New_Internal_Name ('T'));
3496 Make_Full_Type_Declaration (Loc,
3497 Defining_Identifier => Obj_Ptr,
3499 Make_Access_To_Object_Definition (Loc,
3500 Subtype_Indication =>
3502 (Corresponding_Record_Type (Scop), Loc))));
3504 Insert_Actions (N, Decls);
3505 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
3508 Make_Explicit_Dereference (Loc,
3509 Unchecked_Convert_To (Obj_Ptr,
3510 New_Occurrence_Of (Param, Loc)));
3512 -- Analyze new actual. Other actuals in calls are already
3513 -- analyzed and the list of actuals is not renalyzed after
3516 Set_Parent (Rec, N);
3522 end Expand_Protected_Object_Reference;
3524 --------------------------------------
3525 -- Expand_Protected_Subprogram_Call --
3526 --------------------------------------
3528 procedure Expand_Protected_Subprogram_Call
3536 -- If the protected object is not an enclosing scope, this is
3537 -- an inter-object function call. Inter-object procedure
3538 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
3539 -- The call is intra-object only if the subprogram being
3540 -- called is in the protected body being compiled, and if the
3541 -- protected object in the call is statically the enclosing type.
3542 -- The object may be an component of some other data structure,
3543 -- in which case this must be handled as an inter-object call.
3545 if not In_Open_Scopes (Scop)
3546 or else not Is_Entity_Name (Name (N))
3548 if Nkind (Name (N)) = N_Selected_Component then
3549 Rec := Prefix (Name (N));
3552 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
3553 Rec := Prefix (Prefix (Name (N)));
3556 Build_Protected_Subprogram_Call (N,
3557 Name => New_Occurrence_Of (Subp, Sloc (N)),
3558 Rec => Convert_Concurrent (Rec, Etype (Rec)),
3562 Rec := Expand_Protected_Object_Reference (N, Scop);
3568 Build_Protected_Subprogram_Call (N,
3577 -- If it is a function call it can appear in elaboration code and
3578 -- the called entity must be frozen here.
3580 if Ekind (Subp) = E_Function then
3581 Freeze_Expression (Name (N));
3583 end Expand_Protected_Subprogram_Call;
3585 -----------------------
3586 -- Freeze_Subprogram --
3587 -----------------------
3589 procedure Freeze_Subprogram (N : Node_Id) is
3590 E : constant Entity_Id := Entity (N);
3593 -- When a primitive is frozen, enter its name in the corresponding
3594 -- dispatch table. If the DTC_Entity field is not set this is an
3595 -- overridden primitive that can be ignored. We suppress the
3596 -- initialization of the dispatch table entry when Java_VM because
3597 -- the dispatching mechanism is handled internally by the JVM.
3599 if Is_Dispatching_Operation (E)
3600 and then not Is_Abstract (E)
3601 and then Present (DTC_Entity (E))
3602 and then not Is_CPP_Class (Scope (DTC_Entity (E)))
3603 and then not Java_VM
3605 Check_Overriding_Operation (E);
3606 Insert_After (N, Fill_DT_Entry (Sloc (N), E));
3609 -- Mark functions that return by reference. Note that it cannot be
3610 -- part of the normal semantic analysis of the spec since the
3611 -- underlying returned type may not be known yet (for private types)
3614 Typ : constant Entity_Id := Etype (E);
3615 Utyp : constant Entity_Id := Underlying_Type (Typ);
3618 if Is_Return_By_Reference_Type (Typ) then
3619 Set_Returns_By_Ref (E);
3621 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3622 Set_Returns_By_Ref (E);
3625 end Freeze_Subprogram;