1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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_Dbug
; use Exp_Dbug
;
38 with Exp_Disp
; use Exp_Disp
;
39 with Exp_Dist
; use Exp_Dist
;
40 with Exp_Intr
; use Exp_Intr
;
41 with Exp_Pakd
; use Exp_Pakd
;
42 with Exp_Tss
; use Exp_Tss
;
43 with Exp_Util
; use Exp_Util
;
44 with Fname
; use Fname
;
45 with Freeze
; use Freeze
;
46 with Hostparm
; use Hostparm
;
47 with Inline
; use Inline
;
49 with Nlists
; use Nlists
;
50 with Nmake
; use Nmake
;
52 with Restrict
; use Restrict
;
53 with Rident
; use Rident
;
54 with Rtsfind
; use Rtsfind
;
56 with Sem_Ch6
; use Sem_Ch6
;
57 with Sem_Ch8
; use Sem_Ch8
;
58 with Sem_Ch12
; use Sem_Ch12
;
59 with Sem_Ch13
; use Sem_Ch13
;
60 with Sem_Disp
; use Sem_Disp
;
61 with Sem_Dist
; use Sem_Dist
;
62 with Sem_Mech
; use Sem_Mech
;
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 or out parameter which is a numeric
103 -- (view) conversion of the form T (A), where A denotes a variable,
104 -- we insert the declaration:
106 -- Temp : T[ := T (A)];
108 -- prior to the call. Then we replace the actual with a reference to Temp,
109 -- and append the assignment:
111 -- A := TypeA (Temp);
113 -- after the call. Here TypeA is the actual type of variable A.
114 -- For out parameters, the initial declaration has no expression.
115 -- If A is not an entity name, we generate instead:
117 -- Var : TypeA renames A;
118 -- Temp : T := Var; -- omitting expression for out parameter.
120 -- Var := TypeA (Temp);
122 -- For other in-out parameters, we emit the required constraint checks
123 -- before and/or after the call.
125 -- For all parameter modes, actuals that denote components and slices
126 -- of packed arrays are expanded into suitable temporaries.
128 -- For non-scalar objects that are possibly unaligned, add call by copy
129 -- code (copy in for IN and IN OUT, copy out for OUT and IN OUT).
131 procedure Expand_Inlined_Call
134 Orig_Subp
: Entity_Id
);
135 -- If called subprogram can be inlined by the front-end, retrieve the
136 -- analyzed body, replace formals with actuals and expand call in place.
137 -- Generate thunks for actuals that are expressions, and insert the
138 -- corresponding constant declarations before the call. If the original
139 -- call is to a derived operation, the return type is the one of the
140 -- derived operation, but the body is that of the original, so return
141 -- expressions in the body must be converted to the desired type (which
142 -- is simply not noted in the tree without inline expansion).
144 function Expand_Protected_Object_Reference
149 procedure Expand_Protected_Subprogram_Call
153 -- A call to a protected subprogram within the protected object may appear
154 -- as a regular call. The list of actuals must be expanded to contain a
155 -- reference to the object itself, and the call becomes a call to the
156 -- corresponding protected subprogram.
158 --------------------------------
159 -- Check_Overriding_Operation --
160 --------------------------------
162 procedure Check_Overriding_Operation
(Subp
: Entity_Id
) is
163 Typ
: constant Entity_Id
:= Find_Dispatching_Type
(Subp
);
164 Op_List
: constant Elist_Id
:= Primitive_Operations
(Typ
);
170 if Is_Derived_Type
(Typ
)
171 and then not Is_Private_Type
(Typ
)
172 and then In_Open_Scopes
(Scope
(Etype
(Typ
)))
173 and then Typ
= Base_Type
(Typ
)
175 -- Subp overrides an inherited private operation if there is an
176 -- inherited operation with a different name than Subp (see
177 -- Derive_Subprogram) whose Alias is a hidden subprogram with the
178 -- same name as Subp.
180 Op_Elmt
:= First_Elmt
(Op_List
);
181 while Present
(Op_Elmt
) loop
182 Prim_Op
:= Node
(Op_Elmt
);
183 Par_Op
:= Alias
(Prim_Op
);
186 and then not Comes_From_Source
(Prim_Op
)
187 and then Chars
(Prim_Op
) /= Chars
(Par_Op
)
188 and then Chars
(Par_Op
) = Chars
(Subp
)
189 and then Is_Hidden
(Par_Op
)
190 and then Type_Conformant
(Prim_Op
, Subp
)
192 Set_DT_Position
(Subp
, DT_Position
(Prim_Op
));
198 end Check_Overriding_Operation
;
200 -------------------------------
201 -- Detect_Infinite_Recursion --
202 -------------------------------
204 procedure Detect_Infinite_Recursion
(N
: Node_Id
; Spec
: Entity_Id
) is
205 Loc
: constant Source_Ptr
:= Sloc
(N
);
207 Var_List
: constant Elist_Id
:= New_Elmt_List
;
208 -- List of globals referenced by body of procedure
210 Call_List
: constant Elist_Id
:= New_Elmt_List
;
211 -- List of recursive calls in body of procedure
213 Shad_List
: constant Elist_Id
:= New_Elmt_List
;
214 -- List of entity id's for entities created to capture the value of
215 -- referenced globals on entry to the procedure.
217 Scop
: constant Uint
:= Scope_Depth
(Spec
);
218 -- This is used to record the scope depth of the current procedure, so
219 -- that we can identify global references.
221 Max_Vars
: constant := 4;
222 -- Do not test more than four global variables
224 Count_Vars
: Natural := 0;
225 -- Count variables found so far
237 function Process
(Nod
: Node_Id
) return Traverse_Result
;
238 -- Function to traverse the subprogram body (using Traverse_Func)
244 function Process
(Nod
: Node_Id
) return Traverse_Result
is
248 if Nkind
(Nod
) = N_Procedure_Call_Statement
then
250 -- Case of one of the detected recursive calls
252 if Is_Entity_Name
(Name
(Nod
))
253 and then Has_Recursive_Call
(Entity
(Name
(Nod
)))
254 and then Entity
(Name
(Nod
)) = Spec
256 Append_Elmt
(Nod
, Call_List
);
259 -- Any other procedure call may have side effects
265 -- A call to a pure function can always be ignored
267 elsif Nkind
(Nod
) = N_Function_Call
268 and then Is_Entity_Name
(Name
(Nod
))
269 and then Is_Pure
(Entity
(Name
(Nod
)))
273 -- Case of an identifier reference
275 elsif Nkind
(Nod
) = N_Identifier
then
278 -- If no entity, then ignore the reference
280 -- Not clear why this can happen. To investigate, remove this
281 -- test and look at the crash that occurs here in 3401-004 ???
286 -- Ignore entities with no Scope, again not clear how this
287 -- can happen, to investigate, look at 4108-008 ???
289 elsif No
(Scope
(Ent
)) then
292 -- Ignore the reference if not to a more global object
294 elsif Scope_Depth
(Scope
(Ent
)) >= Scop
then
297 -- References to types, exceptions and constants are always OK
300 or else Ekind
(Ent
) = E_Exception
301 or else Ekind
(Ent
) = E_Constant
305 -- If other than a non-volatile scalar variable, we have some
306 -- kind of global reference (e.g. to a function) that we cannot
307 -- deal with so we forget the attempt.
309 elsif Ekind
(Ent
) /= E_Variable
310 or else not Is_Scalar_Type
(Etype
(Ent
))
311 or else Treat_As_Volatile
(Ent
)
315 -- Otherwise we have a reference to a global scalar
318 -- Loop through global entities already detected
320 Elm
:= First_Elmt
(Var_List
);
322 -- If not detected before, record this new global reference
325 Count_Vars
:= Count_Vars
+ 1;
327 if Count_Vars
<= Max_Vars
then
328 Append_Elmt
(Entity
(Nod
), Var_List
);
335 -- If recorded before, ignore
337 elsif Node
(Elm
) = Entity
(Nod
) then
340 -- Otherwise keep looking
350 -- For all other node kinds, recursively visit syntactic children
357 function Traverse_Body
is new Traverse_Func
;
359 -- Start of processing for Detect_Infinite_Recursion
362 -- Do not attempt detection in No_Implicit_Conditional mode, since we
363 -- won't be able to generate the code to handle the recursion in any
366 if Restriction_Active
(No_Implicit_Conditionals
) then
370 -- Otherwise do traversal and quit if we get abandon signal
372 if Traverse_Body
(N
) = Abandon
then
375 -- We must have a call, since Has_Recursive_Call was set. If not just
376 -- ignore (this is only an error check, so if we have a funny situation,
377 -- due to bugs or errors, we do not want to bomb!)
379 elsif Is_Empty_Elmt_List
(Call_List
) then
383 -- Here is the case where we detect recursion at compile time
385 -- Push our current scope for analyzing the declarations and code that
386 -- we will insert for the checking.
390 -- This loop builds temporary variables for each of the referenced
391 -- globals, so that at the end of the loop the list Shad_List contains
392 -- these temporaries in one-to-one correspondence with the elements in
396 Elm
:= First_Elmt
(Var_List
);
397 while Present
(Elm
) loop
400 Make_Defining_Identifier
(Loc
,
401 Chars
=> New_Internal_Name
('S'));
402 Append_Elmt
(Ent
, Shad_List
);
404 -- Insert a declaration for this temporary at the start of the
405 -- declarations for the procedure. The temporaries are declared as
406 -- constant objects initialized to the current values of the
407 -- corresponding temporaries.
410 Make_Object_Declaration
(Loc
,
411 Defining_Identifier
=> Ent
,
412 Object_Definition
=> New_Occurrence_Of
(Etype
(Var
), Loc
),
413 Constant_Present
=> True,
414 Expression
=> New_Occurrence_Of
(Var
, Loc
));
417 Prepend
(Decl
, Declarations
(N
));
419 Insert_After
(Last
, Decl
);
427 -- Loop through calls
429 Call
:= First_Elmt
(Call_List
);
430 while Present
(Call
) loop
432 -- Build a predicate expression of the form
435 -- and then global1 = temp1
436 -- and then global2 = temp2
439 -- This predicate determines if any of the global values
440 -- referenced by the procedure have changed since the
441 -- current call, if not an infinite recursion is assured.
443 Test
:= New_Occurrence_Of
(Standard_True
, Loc
);
445 Elm1
:= First_Elmt
(Var_List
);
446 Elm2
:= First_Elmt
(Shad_List
);
447 while Present
(Elm1
) loop
453 Left_Opnd
=> New_Occurrence_Of
(Node
(Elm1
), Loc
),
454 Right_Opnd
=> New_Occurrence_Of
(Node
(Elm2
), Loc
)));
460 -- Now we replace the call with the sequence
462 -- if no-changes (see above) then
463 -- raise Storage_Error;
468 Rewrite
(Node
(Call
),
469 Make_If_Statement
(Loc
,
471 Then_Statements
=> New_List
(
472 Make_Raise_Storage_Error
(Loc
,
473 Reason
=> SE_Infinite_Recursion
)),
475 Else_Statements
=> New_List
(
476 Relocate_Node
(Node
(Call
)))));
478 Analyze
(Node
(Call
));
483 -- Remove temporary scope stack entry used for analysis
486 end Detect_Infinite_Recursion
;
492 procedure Expand_Actuals
(N
: Node_Id
; Subp
: Entity_Id
) is
493 Loc
: constant Source_Ptr
:= Sloc
(N
);
498 E_Formal
: Entity_Id
;
500 procedure Add_Call_By_Copy_Code
;
501 -- For cases where the parameter must be passed by copy, this routine
502 -- generates a temporary variable into which the actual is copied and
503 -- then passes this as the parameter. For an OUT or IN OUT parameter,
504 -- an assignment is also generated to copy the result back. The call
505 -- also takes care of any constraint checks required for the type
506 -- conversion case (on both the way in and the way out).
508 procedure Add_Simple_Call_By_Copy_Code
;
509 -- This is similar to the above, but is used in cases where we know
510 -- that all that is needed is to simply create a temporary and copy
511 -- the value in and out of the temporary.
513 procedure Check_Fortran_Logical
;
514 -- A value of type Logical that is passed through a formal parameter
515 -- must be normalized because .TRUE. usually does not have the same
516 -- representation as True. We assume that .FALSE. = False = 0.
517 -- What about functions that return a logical type ???
519 function Is_Legal_Copy
return Boolean;
520 -- Check that an actual can be copied before generating the temporary
521 -- to be used in the call. If the actual is of a by_reference type then
522 -- the program is illegal (this can only happen in the presence of
523 -- rep. clauses that force an incorrect alignment). If the formal is
524 -- a by_reference parameter imposed by a DEC pragma, emit a warning to
525 -- the effect that this might lead to unaligned arguments.
527 function Make_Var
(Actual
: Node_Id
) return Entity_Id
;
528 -- Returns an entity that refers to the given actual parameter,
529 -- Actual (not including any type conversion). If Actual is an
530 -- entity name, then this entity is returned unchanged, otherwise
531 -- a renaming is created to provide an entity for the actual.
533 procedure Reset_Packed_Prefix
;
534 -- The expansion of a packed array component reference is delayed in
535 -- the context of a call. Now we need to complete the expansion, so we
536 -- unmark the analyzed bits in all prefixes.
538 ---------------------------
539 -- Add_Call_By_Copy_Code --
540 ---------------------------
542 procedure Add_Call_By_Copy_Code
is
548 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
553 if not Is_Legal_Copy
then
557 Temp
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
559 -- Use formal type for temp, unless formal type is an unconstrained
560 -- array, in which case we don't have to worry about bounds checks,
561 -- and we use the actual type, since that has appropriate bounds.
563 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
564 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
566 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
569 if Nkind
(Actual
) = N_Type_Conversion
then
570 V_Typ
:= Etype
(Expression
(Actual
));
572 -- If the formal is an (in-)out parameter, capture the name
573 -- of the variable in order to build the post-call assignment.
575 Var
:= Make_Var
(Expression
(Actual
));
577 Crep
:= not Same_Representation
578 (F_Typ
, Etype
(Expression
(Actual
)));
581 V_Typ
:= Etype
(Actual
);
582 Var
:= Make_Var
(Actual
);
586 -- Setup initialization for case of in out parameter, or an out
587 -- parameter where the formal is an unconstrained array (in the
588 -- latter case, we have to pass in an object with bounds).
590 -- If this is an out parameter, the initial copy is wasteful, so as
591 -- an optimization for the one-dimensional case we extract the
592 -- bounds of the actual and build an uninitialized temporary of the
595 if Ekind
(Formal
) = E_In_Out_Parameter
596 or else (Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
))
598 if Nkind
(Actual
) = N_Type_Conversion
then
599 if Conversion_OK
(Actual
) then
600 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
602 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
605 elsif Ekind
(Formal
) = E_Out_Parameter
606 and then Is_Array_Type
(F_Typ
)
607 and then Number_Dimensions
(F_Typ
) = 1
608 and then not Has_Non_Null_Base_Init_Proc
(F_Typ
)
610 -- Actual is a one-dimensional array or slice, and the type
611 -- requires no initialization. Create a temporary of the
612 -- right size, but do not copy actual into it (optimization).
616 Make_Subtype_Indication
(Loc
,
618 New_Occurrence_Of
(F_Typ
, Loc
),
620 Make_Index_Or_Discriminant_Constraint
(Loc
,
621 Constraints
=> New_List
(
624 Make_Attribute_Reference
(Loc
,
625 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
626 Attribute_name
=> Name_First
),
628 Make_Attribute_Reference
(Loc
,
629 Prefix
=> New_Occurrence_Of
(Var
, Loc
),
630 Attribute_Name
=> Name_Last
)))));
633 Init
:= New_Occurrence_Of
(Var
, Loc
);
636 -- An initialization is created for packed conversions as
637 -- actuals for out parameters to enable Make_Object_Declaration
638 -- to determine the proper subtype for N_Node. Note that this
639 -- is wasteful because the extra copying on the call side is
640 -- not required for such out parameters. ???
642 elsif Ekind
(Formal
) = E_Out_Parameter
643 and then Nkind
(Actual
) = N_Type_Conversion
644 and then (Is_Bit_Packed_Array
(F_Typ
)
646 Is_Bit_Packed_Array
(Etype
(Expression
(Actual
))))
648 if Conversion_OK
(Actual
) then
649 Init
:= OK_Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
651 Init
:= Convert_To
(F_Typ
, New_Occurrence_Of
(Var
, Loc
));
654 elsif Ekind
(Formal
) = E_In_Parameter
then
655 Init
:= New_Occurrence_Of
(Var
, Loc
);
662 Make_Object_Declaration
(Loc
,
663 Defining_Identifier
=> Temp
,
664 Object_Definition
=> Indic
,
666 Set_Assignment_OK
(N_Node
);
667 Insert_Action
(N
, N_Node
);
669 -- Now, normally the deal here is that we use the defining
670 -- identifier created by that object declaration. There is
671 -- one exception to this. In the change of representation case
672 -- the above declaration will end up looking like:
674 -- temp : type := identifier;
676 -- And in this case we might as well use the identifier directly
677 -- and eliminate the temporary. Note that the analysis of the
678 -- declaration was not a waste of time in that case, since it is
679 -- what generated the necessary change of representation code. If
680 -- the change of representation introduced additional code, as in
681 -- a fixed-integer conversion, the expression is not an identifier
685 and then Present
(Expression
(N_Node
))
686 and then Is_Entity_Name
(Expression
(N_Node
))
688 Temp
:= Entity
(Expression
(N_Node
));
689 Rewrite
(N_Node
, Make_Null_Statement
(Loc
));
692 -- For IN parameter, all we do is to replace the actual
694 if Ekind
(Formal
) = E_In_Parameter
then
695 Rewrite
(Actual
, New_Reference_To
(Temp
, Loc
));
698 -- Processing for OUT or IN OUT parameter
701 -- Kill current value indications for the temporary variable we
702 -- created, since we just passed it as an OUT parameter.
704 Kill_Current_Values
(Temp
);
706 -- If type conversion, use reverse conversion on exit
708 if Nkind
(Actual
) = N_Type_Conversion
then
709 if Conversion_OK
(Actual
) then
710 Expr
:= OK_Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
712 Expr
:= Convert_To
(V_Typ
, New_Occurrence_Of
(Temp
, Loc
));
715 Expr
:= New_Occurrence_Of
(Temp
, Loc
);
718 Rewrite
(Actual
, New_Reference_To
(Temp
, Loc
));
721 Append_To
(Post_Call
,
722 Make_Assignment_Statement
(Loc
,
723 Name
=> New_Occurrence_Of
(Var
, Loc
),
724 Expression
=> Expr
));
726 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
728 end Add_Call_By_Copy_Code
;
730 ----------------------------------
731 -- Add_Simple_Call_By_Copy_Code --
732 ----------------------------------
734 procedure Add_Simple_Call_By_Copy_Code
is
742 F_Typ
: constant Entity_Id
:= Etype
(Formal
);
745 if not Is_Legal_Copy
then
749 -- Use formal type for temp, unless formal type is an unconstrained
750 -- array, in which case we don't have to worry about bounds checks,
751 -- and we use the actual type, since that has appropriate bounds.
753 if Is_Array_Type
(F_Typ
) and then not Is_Constrained
(F_Typ
) then
754 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
756 Indic
:= New_Occurrence_Of
(Etype
(Formal
), Loc
);
759 -- Prepare to generate code
763 Temp
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
764 Incod
:= Relocate_Node
(Actual
);
765 Outcod
:= New_Copy_Tree
(Incod
);
767 -- Generate declaration of temporary variable, initializing it
768 -- with the input parameter unless we have an OUT formal or
769 -- this is an initialization call.
771 -- If the formal is an out parameter with discriminants, the
772 -- discriminants must be captured even if the rest of the object
773 -- is in principle uninitialized, because the discriminants may
774 -- be read by the called subprogram.
776 if Ekind
(Formal
) = E_Out_Parameter
then
779 if Has_Discriminants
(Etype
(Formal
)) then
780 Indic
:= New_Occurrence_Of
(Etype
(Actual
), Loc
);
783 elsif Inside_Init_Proc
then
785 -- Could use a comment here to match comment below ???
787 if Nkind
(Actual
) /= N_Selected_Component
789 not Has_Discriminant_Dependent_Constraint
790 (Entity
(Selector_Name
(Actual
)))
794 -- Otherwise, keep the component in order to generate the proper
795 -- actual subtype, that depends on enclosing discriminants.
803 Make_Object_Declaration
(Loc
,
804 Defining_Identifier
=> Temp
,
805 Object_Definition
=> Indic
,
806 Expression
=> Incod
);
811 -- If the call is to initialize a component of a composite type,
812 -- and the component does not depend on discriminants, use the
813 -- actual type of the component. This is required in case the
814 -- component is constrained, because in general the formal of the
815 -- initialization procedure will be unconstrained. Note that if
816 -- the component being initialized is constrained by an enclosing
817 -- discriminant, the presence of the initialization in the
818 -- declaration will generate an expression for the actual subtype.
820 Set_No_Initialization
(Decl
);
821 Set_Object_Definition
(Decl
,
822 New_Occurrence_Of
(Etype
(Actual
), Loc
));
825 Insert_Action
(N
, Decl
);
827 -- The actual is simply a reference to the temporary
829 Rewrite
(Actual
, New_Occurrence_Of
(Temp
, Loc
));
831 -- Generate copy out if OUT or IN OUT parameter
833 if Ekind
(Formal
) /= E_In_Parameter
then
835 Rhs
:= New_Occurrence_Of
(Temp
, Loc
);
837 -- Deal with conversion
839 if Nkind
(Lhs
) = N_Type_Conversion
then
840 Lhs
:= Expression
(Lhs
);
841 Rhs
:= Convert_To
(Etype
(Actual
), Rhs
);
844 Append_To
(Post_Call
,
845 Make_Assignment_Statement
(Loc
,
848 Set_Assignment_OK
(Name
(Last
(Post_Call
)));
850 end Add_Simple_Call_By_Copy_Code
;
852 ---------------------------
853 -- Check_Fortran_Logical --
854 ---------------------------
856 procedure Check_Fortran_Logical
is
857 Logical
: constant Entity_Id
:= Etype
(Formal
);
860 -- Note: this is very incomplete, e.g. it does not handle arrays
861 -- of logical values. This is really not the right approach at all???)
864 if Convention
(Subp
) = Convention_Fortran
865 and then Root_Type
(Etype
(Formal
)) = Standard_Boolean
866 and then Ekind
(Formal
) /= E_In_Parameter
868 Var
:= Make_Var
(Actual
);
869 Append_To
(Post_Call
,
870 Make_Assignment_Statement
(Loc
,
871 Name
=> New_Occurrence_Of
(Var
, Loc
),
873 Unchecked_Convert_To
(
876 Left_Opnd
=> New_Occurrence_Of
(Var
, Loc
),
878 Unchecked_Convert_To
(
880 New_Occurrence_Of
(Standard_False
, Loc
))))));
882 end Check_Fortran_Logical
;
888 function Is_Legal_Copy
return Boolean is
890 -- An attempt to copy a value of such a type can only occur if
891 -- representation clauses give the actual a misaligned address.
893 if Is_By_Reference_Type
(Etype
(Formal
)) then
895 ("misaligned actual cannot be passed by reference", Actual
);
898 -- For users of Starlet, we assume that the specification of by-
899 -- reference mechanism is mandatory. This may lead to unligned
900 -- objects but at least for DEC legacy code it is known to work.
901 -- The warning will alert users of this code that a problem may
904 elsif Mechanism
(Formal
) = By_Reference
905 and then Is_Valued_Procedure
(Scope
(Formal
))
908 ("by_reference actual may be misaligned?", Actual
);
920 function Make_Var
(Actual
: Node_Id
) return Entity_Id
is
924 if Is_Entity_Name
(Actual
) then
925 return Entity
(Actual
);
928 Var
:= Make_Defining_Identifier
(Loc
, New_Internal_Name
('T'));
931 Make_Object_Renaming_Declaration
(Loc
,
932 Defining_Identifier
=> Var
,
934 New_Occurrence_Of
(Etype
(Actual
), Loc
),
935 Name
=> Relocate_Node
(Actual
));
937 Insert_Action
(N
, N_Node
);
942 -------------------------
943 -- Reset_Packed_Prefix --
944 -------------------------
946 procedure Reset_Packed_Prefix
is
947 Pfx
: Node_Id
:= Actual
;
950 Set_Analyzed
(Pfx
, False);
951 exit when Nkind
(Pfx
) /= N_Selected_Component
952 and then Nkind
(Pfx
) /= N_Indexed_Component
;
955 end Reset_Packed_Prefix
;
957 -- Start of processing for Expand_Actuals
960 Post_Call
:= New_List
;
962 Formal
:= First_Formal
(Subp
);
963 Actual
:= First_Actual
(N
);
964 while Present
(Formal
) loop
965 E_Formal
:= Etype
(Formal
);
967 if Is_Scalar_Type
(E_Formal
)
968 or else Nkind
(Actual
) = N_Slice
970 Check_Fortran_Logical
;
974 elsif Ekind
(Formal
) /= E_Out_Parameter
then
976 -- The unusual case of the current instance of a protected type
977 -- requires special handling. This can only occur in the context
978 -- of a call within the body of a protected operation.
980 if Is_Entity_Name
(Actual
)
981 and then Ekind
(Entity
(Actual
)) = E_Protected_Type
982 and then In_Open_Scopes
(Entity
(Actual
))
984 if Scope
(Subp
) /= Entity
(Actual
) then
985 Error_Msg_N
("operation outside protected type may not "
986 & "call back its protected operations?", Actual
);
990 Expand_Protected_Object_Reference
(N
, Entity
(Actual
)));
993 Apply_Constraint_Check
(Actual
, E_Formal
);
995 -- Out parameter case. No constraint checks on access type
998 elsif Is_Access_Type
(E_Formal
) then
1003 elsif Has_Discriminants
(Base_Type
(E_Formal
))
1004 or else Has_Non_Null_Base_Init_Proc
(E_Formal
)
1006 Apply_Constraint_Check
(Actual
, E_Formal
);
1011 Apply_Constraint_Check
(Actual
, Base_Type
(E_Formal
));
1014 -- Processing for IN-OUT and OUT parameters
1016 if Ekind
(Formal
) /= E_In_Parameter
then
1018 -- For type conversions of arrays, apply length/range checks
1020 if Is_Array_Type
(E_Formal
)
1021 and then Nkind
(Actual
) = N_Type_Conversion
1023 if Is_Constrained
(E_Formal
) then
1024 Apply_Length_Check
(Expression
(Actual
), E_Formal
);
1026 Apply_Range_Check
(Expression
(Actual
), E_Formal
);
1030 -- If argument is a type conversion for a type that is passed
1031 -- by copy, then we must pass the parameter by copy.
1033 if Nkind
(Actual
) = N_Type_Conversion
1035 (Is_Numeric_Type
(E_Formal
)
1036 or else Is_Access_Type
(E_Formal
)
1037 or else Is_Enumeration_Type
(E_Formal
)
1038 or else Is_Bit_Packed_Array
(Etype
(Formal
))
1039 or else Is_Bit_Packed_Array
(Etype
(Expression
(Actual
)))
1041 -- Also pass by copy if change of representation
1043 or else not Same_Representation
1045 Etype
(Expression
(Actual
))))
1047 Add_Call_By_Copy_Code
;
1049 -- References to components of bit packed arrays are expanded
1050 -- at this point, rather than at the point of analysis of the
1051 -- actuals, to handle the expansion of the assignment to
1052 -- [in] out parameters.
1054 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1055 Add_Simple_Call_By_Copy_Code
;
1057 -- If a non-scalar actual is possibly unaligned, we need a copy
1059 elsif Is_Possibly_Unaligned_Object
(Actual
)
1060 and then not Represented_As_Scalar
(Etype
(Formal
))
1062 Add_Simple_Call_By_Copy_Code
;
1064 -- References to slices of bit packed arrays are expanded
1066 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1067 Add_Call_By_Copy_Code
;
1069 -- References to possibly unaligned slices of arrays are expanded
1071 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1072 Add_Call_By_Copy_Code
;
1074 -- Deal with access types where the actual subtpe and the
1075 -- formal subtype are not the same, requiring a check.
1077 -- It is necessary to exclude tagged types because of "downward
1078 -- conversion" errors and a strange assertion error in namet
1079 -- from gnatf in bug 1215-001 ???
1081 elsif Is_Access_Type
(E_Formal
)
1082 and then not Same_Type
(E_Formal
, Etype
(Actual
))
1083 and then not Is_Tagged_Type
(Designated_Type
(E_Formal
))
1085 Add_Call_By_Copy_Code
;
1087 -- If the actual is not a scalar and is marked for volatile
1088 -- treatment, whereas the formal is not volatile, then pass
1089 -- by copy unless it is a by-reference type.
1091 elsif Is_Entity_Name
(Actual
)
1092 and then Treat_As_Volatile
(Entity
(Actual
))
1093 and then not Is_By_Reference_Type
(Etype
(Actual
))
1094 and then not Is_Scalar_Type
(Etype
(Entity
(Actual
)))
1095 and then not Treat_As_Volatile
(E_Formal
)
1097 Add_Call_By_Copy_Code
;
1099 elsif Nkind
(Actual
) = N_Indexed_Component
1100 and then Is_Entity_Name
(Prefix
(Actual
))
1101 and then Has_Volatile_Components
(Entity
(Prefix
(Actual
)))
1103 Add_Call_By_Copy_Code
;
1106 -- Processing for IN parameters
1109 -- For IN parameters is in the packed array case, we expand an
1110 -- indexed component (the circuit in Exp_Ch4 deliberately left
1111 -- indexed components appearing as actuals untouched, so that
1112 -- the special processing above for the OUT and IN OUT cases
1113 -- could be performed. We could make the test in Exp_Ch4 more
1114 -- complex and have it detect the parameter mode, but it is
1115 -- easier simply to handle all cases here.)
1117 if Nkind
(Actual
) = N_Indexed_Component
1118 and then Is_Packed
(Etype
(Prefix
(Actual
)))
1120 Reset_Packed_Prefix
;
1121 Expand_Packed_Element_Reference
(Actual
);
1123 -- If we have a reference to a bit packed array, we copy it,
1124 -- since the actual must be byte aligned.
1126 -- Is this really necessary in all cases???
1128 elsif Is_Ref_To_Bit_Packed_Array
(Actual
) then
1129 Add_Simple_Call_By_Copy_Code
;
1131 -- If a non-scalar actual is possibly unaligned, we need a copy
1133 elsif Is_Possibly_Unaligned_Object
(Actual
)
1134 and then not Represented_As_Scalar
(Etype
(Formal
))
1136 Add_Simple_Call_By_Copy_Code
;
1138 -- Similarly, we have to expand slices of packed arrays here
1139 -- because the result must be byte aligned.
1141 elsif Is_Ref_To_Bit_Packed_Slice
(Actual
) then
1142 Add_Call_By_Copy_Code
;
1144 -- Only processing remaining is to pass by copy if this is a
1145 -- reference to a possibly unaligned slice, since the caller
1146 -- expects an appropriately aligned argument.
1148 elsif Is_Possibly_Unaligned_Slice
(Actual
) then
1149 Add_Call_By_Copy_Code
;
1153 Next_Formal
(Formal
);
1154 Next_Actual
(Actual
);
1157 -- Find right place to put post call stuff if it is present
1159 if not Is_Empty_List
(Post_Call
) then
1161 -- If call is not a list member, it must be the triggering statement
1162 -- of a triggering alternative or an entry call alternative, and we
1163 -- can add the post call stuff to the corresponding statement list.
1165 if not Is_List_Member
(N
) then
1167 P
: constant Node_Id
:= Parent
(N
);
1170 pragma Assert
(Nkind
(P
) = N_Triggering_Alternative
1171 or else Nkind
(P
) = N_Entry_Call_Alternative
);
1173 if Is_Non_Empty_List
(Statements
(P
)) then
1174 Insert_List_Before_And_Analyze
1175 (First
(Statements
(P
)), Post_Call
);
1177 Set_Statements
(P
, Post_Call
);
1181 -- Otherwise, normal case where N is in a statement sequence,
1182 -- just put the post-call stuff after the call statement.
1185 Insert_Actions_After
(N
, Post_Call
);
1189 -- The call node itself is re-analyzed in Expand_Call
1197 -- This procedure handles expansion of function calls and procedure call
1198 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1199 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1201 -- Replace call to Raise_Exception by Raise_Exception always if possible
1202 -- Provide values of actuals for all formals in Extra_Formals list
1203 -- Replace "call" to enumeration literal function by literal itself
1204 -- Rewrite call to predefined operator as operator
1205 -- Replace actuals to in-out parameters that are numeric conversions,
1206 -- with explicit assignment to temporaries before and after the call.
1207 -- Remove optional actuals if First_Optional_Parameter specified.
1209 -- Note that the list of actuals has been filled with default expressions
1210 -- during semantic analysis of the call. Only the extra actuals required
1211 -- for the 'Constrained attribute and for accessibility checks are added
1214 procedure Expand_Call
(N
: Node_Id
) is
1215 Loc
: constant Source_Ptr
:= Sloc
(N
);
1216 Remote
: constant Boolean := Is_Remote_Call
(N
);
1218 Orig_Subp
: Entity_Id
:= Empty
;
1219 Parent_Subp
: Entity_Id
;
1220 Parent_Formal
: Entity_Id
;
1223 Prev
: Node_Id
:= Empty
;
1225 Prev_Orig
: Node_Id
;
1226 -- Original node for an actual, which may have been rewritten. If the
1227 -- actual is a function call that has been transformed from a selected
1228 -- component, the original node is unanalyzed. Otherwise, it carries
1229 -- semantic information used to generate additional actuals.
1232 Extra_Actuals
: List_Id
:= No_List
;
1234 CW_Interface_Formals_Present
: Boolean := False;
1236 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
);
1237 -- Adds one entry to the end of the actual parameter list. Used for
1238 -- default parameters and for extra actuals (for Extra_Formals). The
1239 -- argument is an N_Parameter_Association node.
1241 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
);
1242 -- Adds an extra actual to the list of extra actuals. Expr is the
1243 -- expression for the value of the actual, EF is the entity for the
1246 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
;
1247 -- Within an instance, a type derived from a non-tagged formal derived
1248 -- type inherits from the original parent, not from the actual. This is
1249 -- tested in 4723-003. The current derivation mechanism has the derived
1250 -- type inherit from the actual, which is only correct outside of the
1251 -- instance. If the subprogram is inherited, we test for this particular
1252 -- case through a convoluted tree traversal before setting the proper
1253 -- subprogram to be called.
1255 --------------------------
1256 -- Add_Actual_Parameter --
1257 --------------------------
1259 procedure Add_Actual_Parameter
(Insert_Param
: Node_Id
) is
1260 Actual_Expr
: constant Node_Id
:=
1261 Explicit_Actual_Parameter
(Insert_Param
);
1264 -- Case of insertion is first named actual
1266 if No
(Prev
) or else
1267 Nkind
(Parent
(Prev
)) /= N_Parameter_Association
1269 Set_Next_Named_Actual
(Insert_Param
, First_Named_Actual
(N
));
1270 Set_First_Named_Actual
(N
, Actual_Expr
);
1273 if No
(Parameter_Associations
(N
)) then
1274 Set_Parameter_Associations
(N
, New_List
);
1275 Append
(Insert_Param
, Parameter_Associations
(N
));
1278 Insert_After
(Prev
, Insert_Param
);
1281 -- Case of insertion is not first named actual
1284 Set_Next_Named_Actual
1285 (Insert_Param
, Next_Named_Actual
(Parent
(Prev
)));
1286 Set_Next_Named_Actual
(Parent
(Prev
), Actual_Expr
);
1287 Append
(Insert_Param
, Parameter_Associations
(N
));
1290 Prev
:= Actual_Expr
;
1291 end Add_Actual_Parameter
;
1293 ----------------------
1294 -- Add_Extra_Actual --
1295 ----------------------
1297 procedure Add_Extra_Actual
(Expr
: Node_Id
; EF
: Entity_Id
) is
1298 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
1301 if Extra_Actuals
= No_List
then
1302 Extra_Actuals
:= New_List
;
1303 Set_Parent
(Extra_Actuals
, N
);
1306 Append_To
(Extra_Actuals
,
1307 Make_Parameter_Association
(Loc
,
1308 Explicit_Actual_Parameter
=> Expr
,
1310 Make_Identifier
(Loc
, Chars
(EF
))));
1312 Analyze_And_Resolve
(Expr
, Etype
(EF
));
1313 end Add_Extra_Actual
;
1315 ---------------------------
1316 -- Inherited_From_Formal --
1317 ---------------------------
1319 function Inherited_From_Formal
(S
: Entity_Id
) return Entity_Id
is
1321 Gen_Par
: Entity_Id
;
1322 Gen_Prim
: Elist_Id
;
1327 -- If the operation is inherited, it is attached to the corresponding
1328 -- type derivation. If the parent in the derivation is a generic
1329 -- actual, it is a subtype of the actual, and we have to recover the
1330 -- original derived type declaration to find the proper parent.
1332 if Nkind
(Parent
(S
)) /= N_Full_Type_Declaration
1333 or else not Is_Derived_Type
(Defining_Identifier
(Parent
(S
)))
1334 or else Nkind
(Type_Definition
(Original_Node
(Parent
(S
)))) /=
1335 N_Derived_Type_Definition
1336 or else not In_Instance
1343 (Type_Definition
(Original_Node
(Parent
(S
)))));
1345 if Nkind
(Indic
) = N_Subtype_Indication
then
1346 Par
:= Entity
(Subtype_Mark
(Indic
));
1348 Par
:= Entity
(Indic
);
1352 if not Is_Generic_Actual_Type
(Par
)
1353 or else Is_Tagged_Type
(Par
)
1354 or else Nkind
(Parent
(Par
)) /= N_Subtype_Declaration
1355 or else not In_Open_Scopes
(Scope
(Par
))
1360 Gen_Par
:= Generic_Parent_Type
(Parent
(Par
));
1363 -- If the generic parent type is still the generic type, this is a
1364 -- private formal, not a derived formal, and there are no operations
1365 -- inherited from the formal.
1367 if Nkind
(Parent
(Gen_Par
)) = N_Formal_Type_Declaration
then
1371 Gen_Prim
:= Collect_Primitive_Operations
(Gen_Par
);
1373 Elmt
:= First_Elmt
(Gen_Prim
);
1374 while Present
(Elmt
) loop
1375 if Chars
(Node
(Elmt
)) = Chars
(S
) then
1381 F1
:= First_Formal
(S
);
1382 F2
:= First_Formal
(Node
(Elmt
));
1384 and then Present
(F2
)
1386 if Etype
(F1
) = Etype
(F2
)
1387 or else Etype
(F2
) = Gen_Par
1393 exit; -- not the right subprogram
1405 raise Program_Error
;
1406 end Inherited_From_Formal
;
1408 -- Start of processing for Expand_Call
1411 -- Ignore if previous error
1413 if Nkind
(N
) in N_Has_Etype
and then Etype
(N
) = Any_Type
then
1417 -- Call using access to subprogram with explicit dereference
1419 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
1420 Subp
:= Etype
(Name
(N
));
1421 Parent_Subp
:= Empty
;
1423 -- Case of call to simple entry, where the Name is a selected component
1424 -- whose prefix is the task, and whose selector name is the entry name
1426 elsif Nkind
(Name
(N
)) = N_Selected_Component
then
1427 Subp
:= Entity
(Selector_Name
(Name
(N
)));
1428 Parent_Subp
:= Empty
;
1430 -- Case of call to member of entry family, where Name is an indexed
1431 -- component, with the prefix being a selected component giving the
1432 -- task and entry family name, and the index being the entry index.
1434 elsif Nkind
(Name
(N
)) = N_Indexed_Component
then
1435 Subp
:= Entity
(Selector_Name
(Prefix
(Name
(N
))));
1436 Parent_Subp
:= Empty
;
1441 Subp
:= Entity
(Name
(N
));
1442 Parent_Subp
:= Alias
(Subp
);
1444 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1445 -- if we can tell that the first parameter cannot possibly be null.
1446 -- This helps optimization and also generation of warnings.
1448 if not Restriction_Active
(No_Exception_Handlers
)
1449 and then Is_RTE
(Subp
, RE_Raise_Exception
)
1452 FA
: constant Node_Id
:= Original_Node
(First_Actual
(N
));
1455 -- The case we catch is where the first argument is obtained
1456 -- using the Identity attribute (which must always be
1459 if Nkind
(FA
) = N_Attribute_Reference
1460 and then Attribute_Name
(FA
) = Name_Identity
1462 Subp
:= RTE
(RE_Raise_Exception_Always
);
1463 Set_Entity
(Name
(N
), Subp
);
1468 if Ekind
(Subp
) = E_Entry
then
1469 Parent_Subp
:= Empty
;
1473 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1474 -- alternative in an asynchronous select or as an entry call in
1475 -- a conditional or timed select. Check whether the procedure call
1476 -- is a renaming of an entry and rewrite it as an entry call.
1478 if Ada_Version
>= Ada_05
1479 and then Nkind
(N
) = N_Procedure_Call_Statement
1481 ((Nkind
(Parent
(N
)) = N_Triggering_Alternative
1482 and then Triggering_Statement
(Parent
(N
)) = N
)
1484 (Nkind
(Parent
(N
)) = N_Entry_Call_Alternative
1485 and then Entry_Call_Statement
(Parent
(N
)) = N
))
1489 Ren_Root
: Entity_Id
:= Subp
;
1492 -- This may be a chain of renamings, find the root
1494 if Present
(Alias
(Ren_Root
)) then
1495 Ren_Root
:= Alias
(Ren_Root
);
1498 if Present
(Original_Node
(Parent
(Parent
(Ren_Root
)))) then
1499 Ren_Decl
:= Original_Node
(Parent
(Parent
(Ren_Root
)));
1501 if Nkind
(Ren_Decl
) = N_Subprogram_Renaming_Declaration
then
1503 Make_Entry_Call_Statement
(Loc
,
1505 New_Copy_Tree
(Name
(Ren_Decl
)),
1506 Parameter_Associations
=>
1507 New_Copy_List_Tree
(Parameter_Associations
(N
))));
1515 -- First step, compute extra actuals, corresponding to any
1516 -- Extra_Formals present. Note that we do not access Extra_Formals
1517 -- directly, instead we simply note the presence of the extra
1518 -- formals as we process the regular formals and collect the
1519 -- corresponding actuals in Extra_Actuals.
1521 -- We also generate any required range checks for actuals as we go
1522 -- through the loop, since this is a convenient place to do this.
1524 Formal
:= First_Formal
(Subp
);
1525 Actual
:= First_Actual
(N
);
1526 while Present
(Formal
) loop
1528 -- Generate range check if required (not activated yet ???)
1530 -- if Do_Range_Check (Actual) then
1531 -- Set_Do_Range_Check (Actual, False);
1532 -- Generate_Range_Check
1533 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
1536 -- Prepare to examine current entry
1539 Prev_Orig
:= Original_Node
(Prev
);
1541 if not Analyzed
(Prev_Orig
)
1542 and then Nkind
(Actual
) = N_Function_Call
1547 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
1548 -- to expand it in a further round.
1550 CW_Interface_Formals_Present
:=
1551 CW_Interface_Formals_Present
1553 (Ekind
(Etype
(Formal
)) = E_Class_Wide_Type
1554 and then Is_Interface
(Etype
(Etype
(Formal
))))
1556 (Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
1557 and then Is_Interface
(Directly_Designated_Type
1558 (Etype
(Etype
(Formal
)))));
1560 -- Create possible extra actual for constrained case. Usually, the
1561 -- extra actual is of the form actual'constrained, but since this
1562 -- attribute is only available for unconstrained records, TRUE is
1563 -- expanded if the type of the formal happens to be constrained (for
1564 -- instance when this procedure is inherited from an unconstrained
1565 -- record to a constrained one) or if the actual has no discriminant
1566 -- (its type is constrained). An exception to this is the case of a
1567 -- private type without discriminants. In this case we pass FALSE
1568 -- because the object has underlying discriminants with defaults.
1570 if Present
(Extra_Constrained
(Formal
)) then
1571 if Ekind
(Etype
(Prev
)) in Private_Kind
1572 and then not Has_Discriminants
(Base_Type
(Etype
(Prev
)))
1575 New_Occurrence_Of
(Standard_False
, Loc
),
1576 Extra_Constrained
(Formal
));
1578 elsif Is_Constrained
(Etype
(Formal
))
1579 or else not Has_Discriminants
(Etype
(Prev
))
1582 New_Occurrence_Of
(Standard_True
, Loc
),
1583 Extra_Constrained
(Formal
));
1585 -- Do not produce extra actuals for Unchecked_Union parameters.
1586 -- Jump directly to the end of the loop.
1588 elsif Is_Unchecked_Union
(Base_Type
(Etype
(Actual
))) then
1589 goto Skip_Extra_Actual_Generation
;
1592 -- If the actual is a type conversion, then the constrained
1593 -- test applies to the actual, not the target type.
1599 -- Test for unchecked conversions as well, which can occur
1600 -- as out parameter actuals on calls to stream procedures.
1603 while Nkind
(Act_Prev
) = N_Type_Conversion
1604 or else Nkind
(Act_Prev
) = N_Unchecked_Type_Conversion
1606 Act_Prev
:= Expression
(Act_Prev
);
1609 -- If the expression is a conversion of a dereference,
1610 -- this is internally generated code that manipulates
1611 -- addresses, e.g. when building interface tables. No
1612 -- check should occur in this case, and the discriminated
1613 -- object is not directly a hand.
1615 if not Comes_From_Source
(Actual
)
1616 and then Nkind
(Actual
) = N_Unchecked_Type_Conversion
1617 and then Nkind
(Act_Prev
) = N_Explicit_Dereference
1620 (New_Occurrence_Of
(Standard_False
, Loc
),
1621 Extra_Constrained
(Formal
));
1625 (Make_Attribute_Reference
(Sloc
(Prev
),
1627 Duplicate_Subexpr_No_Checks
1628 (Act_Prev
, Name_Req
=> True),
1629 Attribute_Name
=> Name_Constrained
),
1630 Extra_Constrained
(Formal
));
1636 -- Create possible extra actual for accessibility level
1638 if Present
(Extra_Accessibility
(Formal
)) then
1639 if Is_Entity_Name
(Prev_Orig
) then
1641 -- When passing an access parameter as the actual to another
1642 -- access parameter we need to pass along the actual's own
1643 -- associated access level parameter. This is done if we are
1644 -- in the scope of the formal access parameter (if this is an
1645 -- inlined body the extra formal is irrelevant).
1647 if Ekind
(Entity
(Prev_Orig
)) in Formal_Kind
1648 and then Ekind
(Etype
(Prev_Orig
)) = E_Anonymous_Access_Type
1649 and then In_Open_Scopes
(Scope
(Entity
(Prev_Orig
)))
1652 Parm_Ent
: constant Entity_Id
:= Param_Entity
(Prev_Orig
);
1655 pragma Assert
(Present
(Parm_Ent
));
1657 if Present
(Extra_Accessibility
(Parm_Ent
)) then
1660 (Extra_Accessibility
(Parm_Ent
), Loc
),
1661 Extra_Accessibility
(Formal
));
1663 -- If the actual access parameter does not have an
1664 -- associated extra formal providing its scope level,
1665 -- then treat the actual as having library-level
1670 (Make_Integer_Literal
(Loc
,
1671 Intval
=> Scope_Depth
(Standard_Standard
)),
1672 Extra_Accessibility
(Formal
));
1676 -- The actual is a normal access value, so just pass the
1677 -- level of the actual's access type.
1681 (Make_Integer_Literal
(Loc
,
1682 Intval
=> Type_Access_Level
(Etype
(Prev_Orig
))),
1683 Extra_Accessibility
(Formal
));
1687 case Nkind
(Prev_Orig
) is
1689 when N_Attribute_Reference
=>
1691 case Get_Attribute_Id
(Attribute_Name
(Prev_Orig
)) is
1693 -- For X'Access, pass on the level of the prefix X
1695 when Attribute_Access
=>
1697 Make_Integer_Literal
(Loc
,
1699 Object_Access_Level
(Prefix
(Prev_Orig
))),
1700 Extra_Accessibility
(Formal
));
1702 -- Treat the unchecked attributes as library-level
1704 when Attribute_Unchecked_Access |
1705 Attribute_Unrestricted_Access
=>
1707 Make_Integer_Literal
(Loc
,
1708 Intval
=> Scope_Depth
(Standard_Standard
)),
1709 Extra_Accessibility
(Formal
));
1711 -- No other cases of attributes returning access
1712 -- values that can be passed to access parameters
1715 raise Program_Error
;
1719 -- For allocators we pass the level of the execution of
1720 -- the called subprogram, which is one greater than the
1721 -- current scope level.
1725 Make_Integer_Literal
(Loc
,
1726 Scope_Depth
(Current_Scope
) + 1),
1727 Extra_Accessibility
(Formal
));
1729 -- For other cases we simply pass the level of the
1730 -- actual's access type.
1734 Make_Integer_Literal
(Loc
,
1735 Intval
=> Type_Access_Level
(Etype
(Prev_Orig
))),
1736 Extra_Accessibility
(Formal
));
1742 -- Perform the check of 4.6(49) that prevents a null value from being
1743 -- passed as an actual to an access parameter. Note that the check is
1744 -- elided in the common cases of passing an access attribute or
1745 -- access parameter as an actual. Also, we currently don't enforce
1746 -- this check for expander-generated actuals and when -gnatdj is set.
1748 if Ada_Version
>= Ada_05
then
1750 -- Ada 2005 (AI-231): Check null-excluding access types
1752 if Is_Access_Type
(Etype
(Formal
))
1753 and then Can_Never_Be_Null
(Etype
(Formal
))
1754 and then Nkind
(Prev
) /= N_Raise_Constraint_Error
1755 and then (Nkind
(Prev
) = N_Null
1756 or else not Can_Never_Be_Null
(Etype
(Prev
)))
1758 Install_Null_Excluding_Check
(Prev
);
1761 -- Ada_Version < Ada_05
1764 if Ekind
(Etype
(Formal
)) /= E_Anonymous_Access_Type
1765 or else Access_Checks_Suppressed
(Subp
)
1769 elsif Debug_Flag_J
then
1772 elsif not Comes_From_Source
(Prev
) then
1775 elsif Is_Entity_Name
(Prev
)
1776 and then Ekind
(Etype
(Prev
)) = E_Anonymous_Access_Type
1780 elsif Nkind
(Prev
) = N_Allocator
1781 or else Nkind
(Prev
) = N_Attribute_Reference
1785 -- Suppress null checks when passing to access parameters of Java
1786 -- subprograms. (Should this be done for other foreign conventions
1789 elsif Convention
(Subp
) = Convention_Java
then
1793 Install_Null_Excluding_Check
(Prev
);
1797 -- Perform appropriate validity checks on parameters that
1800 if Validity_Checks_On
then
1801 if (Ekind
(Formal
) = E_In_Parameter
1802 and then Validity_Check_In_Params
)
1804 (Ekind
(Formal
) = E_In_Out_Parameter
1805 and then Validity_Check_In_Out_Params
)
1807 -- If the actual is an indexed component of a packed
1808 -- type, it has not been expanded yet. It will be
1809 -- copied in the validity code that follows, and has
1810 -- to be expanded appropriately, so reanalyze it.
1812 if Nkind
(Actual
) = N_Indexed_Component
then
1813 Set_Analyzed
(Actual
, False);
1816 Ensure_Valid
(Actual
);
1820 -- For IN OUT and OUT parameters, ensure that subscripts are valid
1821 -- since this is a left side reference. We only do this for calls
1822 -- from the source program since we assume that compiler generated
1823 -- calls explicitly generate any required checks. We also need it
1824 -- only if we are doing standard validity checks, since clearly it
1825 -- is not needed if validity checks are off, and in subscript
1826 -- validity checking mode, all indexed components are checked with
1827 -- a call directly from Expand_N_Indexed_Component.
1829 if Comes_From_Source
(N
)
1830 and then Ekind
(Formal
) /= E_In_Parameter
1831 and then Validity_Checks_On
1832 and then Validity_Check_Default
1833 and then not Validity_Check_Subscripts
1835 Check_Valid_Lvalue_Subscripts
(Actual
);
1838 -- Mark any scalar OUT parameter that is a simple variable as no
1839 -- longer known to be valid (unless the type is always valid). This
1840 -- reflects the fact that if an OUT parameter is never set in a
1841 -- procedure, then it can become invalid on the procedure return.
1843 if Ekind
(Formal
) = E_Out_Parameter
1844 and then Is_Entity_Name
(Actual
)
1845 and then Ekind
(Entity
(Actual
)) = E_Variable
1846 and then not Is_Known_Valid
(Etype
(Actual
))
1848 Set_Is_Known_Valid
(Entity
(Actual
), False);
1851 -- For an OUT or IN OUT parameter, if the actual is an entity, then
1852 -- clear current values, since they can be clobbered. We are probably
1853 -- doing this in more places than we need to, but better safe than
1854 -- sorry when it comes to retaining bad current values!
1856 if Ekind
(Formal
) /= E_In_Parameter
1857 and then Is_Entity_Name
(Actual
)
1859 Kill_Current_Values
(Entity
(Actual
));
1862 -- If the formal is class wide and the actual is an aggregate, force
1863 -- evaluation so that the back end who does not know about class-wide
1864 -- type, does not generate a temporary of the wrong size.
1866 if not Is_Class_Wide_Type
(Etype
(Formal
)) then
1869 elsif Nkind
(Actual
) = N_Aggregate
1870 or else (Nkind
(Actual
) = N_Qualified_Expression
1871 and then Nkind
(Expression
(Actual
)) = N_Aggregate
)
1873 Force_Evaluation
(Actual
);
1876 -- In a remote call, if the formal is of a class-wide type, check
1877 -- that the actual meets the requirements described in E.4(18).
1880 and then Is_Class_Wide_Type
(Etype
(Formal
))
1882 Insert_Action
(Actual
,
1883 Make_Implicit_If_Statement
(N
,
1886 Get_Remotely_Callable
1887 (Duplicate_Subexpr_Move_Checks
(Actual
))),
1888 Then_Statements
=> New_List
(
1889 Make_Raise_Program_Error
(Loc
,
1890 Reason
=> PE_Illegal_RACW_E_4_18
))));
1893 -- This label is required when skipping extra actual generation for
1894 -- Unchecked_Union parameters.
1896 <<Skip_Extra_Actual_Generation
>>
1898 Next_Actual
(Actual
);
1899 Next_Formal
(Formal
);
1902 -- If we are expanding a rhs of an assignment we need to check if tag
1903 -- propagation is needed. You might expect this processing to be in
1904 -- Analyze_Assignment but has to be done earlier (bottom-up) because the
1905 -- assignment might be transformed to a declaration for an unconstrained
1906 -- value if the expression is classwide.
1908 if Nkind
(N
) = N_Function_Call
1909 and then Is_Tag_Indeterminate
(N
)
1910 and then Is_Entity_Name
(Name
(N
))
1913 Ass
: Node_Id
:= Empty
;
1916 if Nkind
(Parent
(N
)) = N_Assignment_Statement
then
1919 elsif Nkind
(Parent
(N
)) = N_Qualified_Expression
1920 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
1922 Ass
:= Parent
(Parent
(N
));
1926 and then Is_Class_Wide_Type
(Etype
(Name
(Ass
)))
1928 if Etype
(N
) /= Root_Type
(Etype
(Name
(Ass
))) then
1930 ("tag-indeterminate expression must have type&"
1931 & "('R'M 5.2 (6))", N
, Root_Type
(Etype
(Name
(Ass
))));
1933 Propagate_Tag
(Name
(Ass
), N
);
1936 -- The call will be rewritten as a dispatching call, and
1937 -- expanded as such.
1944 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
1945 -- it to point to the correct secondary virtual table
1947 if (Nkind
(N
) = N_Function_Call
1948 or else Nkind
(N
) = N_Procedure_Call_Statement
)
1949 and then CW_Interface_Formals_Present
1951 Expand_Interface_Actuals
(N
);
1954 -- Deals with Dispatch_Call if we still have a call, before expanding
1955 -- extra actuals since this will be done on the re-analysis of the
1956 -- dispatching call. Note that we do not try to shorten the actual
1957 -- list for a dispatching call, it would not make sense to do so.
1958 -- Expansion of dispatching calls is suppressed when Java_VM, because
1959 -- the JVM back end directly handles the generation of dispatching
1960 -- calls and would have to undo any expansion to an indirect call.
1962 if (Nkind
(N
) = N_Function_Call
1963 or else Nkind
(N
) = N_Procedure_Call_Statement
)
1964 and then Present
(Controlling_Argument
(N
))
1965 and then not Java_VM
1967 Expand_Dispatching_Call
(N
);
1969 -- The following return is worrisome. Is it really OK to
1970 -- skip all remaining processing in this procedure ???
1974 -- Similarly, expand calls to RCI subprograms on which pragma
1975 -- All_Calls_Remote applies. The rewriting will be reanalyzed
1976 -- later. Do this only when the call comes from source since we do
1977 -- not want such a rewritting to occur in expanded code.
1979 elsif Is_All_Remote_Call
(N
) then
1980 Expand_All_Calls_Remote_Subprogram_Call
(N
);
1982 -- Similarly, do not add extra actuals for an entry call whose entity
1983 -- is a protected procedure, or for an internal protected subprogram
1984 -- call, because it will be rewritten as a protected subprogram call
1985 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
1987 elsif Is_Protected_Type
(Scope
(Subp
))
1988 and then (Ekind
(Subp
) = E_Procedure
1989 or else Ekind
(Subp
) = E_Function
)
1993 -- During that loop we gathered the extra actuals (the ones that
1994 -- correspond to Extra_Formals), so now they can be appended.
1997 while Is_Non_Empty_List
(Extra_Actuals
) loop
1998 Add_Actual_Parameter
(Remove_Head
(Extra_Actuals
));
2002 -- At this point we have all the actuals, so this is the point at
2003 -- which the various expansion activities for actuals is carried out.
2005 Expand_Actuals
(N
, Subp
);
2007 -- If the subprogram is a renaming, or if it is inherited, replace it
2008 -- in the call with the name of the actual subprogram being called.
2009 -- If this is a dispatching call, the run-time decides what to call.
2010 -- The Alias attribute does not apply to entries.
2012 if Nkind
(N
) /= N_Entry_Call_Statement
2013 and then No
(Controlling_Argument
(N
))
2014 and then Present
(Parent_Subp
)
2016 if Present
(Inherited_From_Formal
(Subp
)) then
2017 Parent_Subp
:= Inherited_From_Formal
(Subp
);
2019 while Present
(Alias
(Parent_Subp
)) loop
2020 Parent_Subp
:= Alias
(Parent_Subp
);
2024 -- The below setting of Entity is suspect, see F109-018 discussion???
2026 Set_Entity
(Name
(N
), Parent_Subp
);
2028 if Is_Abstract
(Parent_Subp
)
2029 and then not In_Instance
2032 ("cannot call abstract subprogram &!", Name
(N
), Parent_Subp
);
2035 -- Add an explicit conversion for parameter of the derived type.
2036 -- This is only done for scalar and access in-parameters. Others
2037 -- have been expanded in expand_actuals.
2039 Formal
:= First_Formal
(Subp
);
2040 Parent_Formal
:= First_Formal
(Parent_Subp
);
2041 Actual
:= First_Actual
(N
);
2043 -- It is not clear that conversion is needed for intrinsic
2044 -- subprograms, but it certainly is for those that are user-
2045 -- defined, and that can be inherited on derivation, namely
2046 -- unchecked conversion and deallocation.
2047 -- General case needs study ???
2049 if not Is_Intrinsic_Subprogram
(Parent_Subp
)
2050 or else Is_Generic_Instance
(Parent_Subp
)
2052 while Present
(Formal
) loop
2053 if Etype
(Formal
) /= Etype
(Parent_Formal
)
2054 and then Is_Scalar_Type
(Etype
(Formal
))
2055 and then Ekind
(Formal
) = E_In_Parameter
2056 and then not Raises_Constraint_Error
(Actual
)
2059 OK_Convert_To
(Etype
(Parent_Formal
),
2060 Relocate_Node
(Actual
)));
2063 Resolve
(Actual
, Etype
(Parent_Formal
));
2064 Enable_Range_Check
(Actual
);
2066 elsif Is_Access_Type
(Etype
(Formal
))
2067 and then Base_Type
(Etype
(Parent_Formal
)) /=
2068 Base_Type
(Etype
(Actual
))
2070 if Ekind
(Formal
) /= E_In_Parameter
then
2072 Convert_To
(Etype
(Parent_Formal
),
2073 Relocate_Node
(Actual
)));
2076 Resolve
(Actual
, Etype
(Parent_Formal
));
2079 Ekind
(Etype
(Parent_Formal
)) = E_Anonymous_Access_Type
2080 and then Designated_Type
(Etype
(Parent_Formal
))
2082 Designated_Type
(Etype
(Actual
))
2083 and then not Is_Controlling_Formal
(Formal
)
2085 -- This unchecked conversion is not necessary unless
2086 -- inlining is enabled, because in that case the type
2087 -- mismatch may become visible in the body about to be
2091 Unchecked_Convert_To
(Etype
(Parent_Formal
),
2092 Relocate_Node
(Actual
)));
2095 Resolve
(Actual
, Etype
(Parent_Formal
));
2099 Next_Formal
(Formal
);
2100 Next_Formal
(Parent_Formal
);
2101 Next_Actual
(Actual
);
2106 Subp
:= Parent_Subp
;
2109 -- Check for violation of No_Abort_Statements
2111 if Is_RTE
(Subp
, RE_Abort_Task
) then
2112 Check_Restriction
(No_Abort_Statements
, N
);
2114 -- Check for violation of No_Dynamic_Attachment
2116 elsif RTU_Loaded
(Ada_Interrupts
)
2117 and then (Is_RTE
(Subp
, RE_Is_Reserved
) or else
2118 Is_RTE
(Subp
, RE_Is_Attached
) or else
2119 Is_RTE
(Subp
, RE_Current_Handler
) or else
2120 Is_RTE
(Subp
, RE_Attach_Handler
) or else
2121 Is_RTE
(Subp
, RE_Exchange_Handler
) or else
2122 Is_RTE
(Subp
, RE_Detach_Handler
) or else
2123 Is_RTE
(Subp
, RE_Reference
))
2125 Check_Restriction
(No_Dynamic_Attachment
, N
);
2128 -- Deal with case where call is an explicit dereference
2130 if Nkind
(Name
(N
)) = N_Explicit_Dereference
then
2132 -- Handle case of access to protected subprogram type
2134 if Ekind
(Base_Type
(Etype
(Prefix
(Name
(N
))))) =
2135 E_Access_Protected_Subprogram_Type
2137 -- If this is a call through an access to protected operation,
2138 -- the prefix has the form (object'address, operation'access).
2139 -- Rewrite as a for other protected calls: the object is the
2140 -- first parameter of the list of actuals.
2147 Ptr
: constant Node_Id
:= Prefix
(Name
(N
));
2149 T
: constant Entity_Id
:=
2150 Equivalent_Type
(Base_Type
(Etype
(Ptr
)));
2152 D_T
: constant Entity_Id
:=
2153 Designated_Type
(Base_Type
(Etype
(Ptr
)));
2157 Make_Selected_Component
(Loc
,
2158 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
2160 New_Occurrence_Of
(First_Entity
(T
), Loc
));
2163 Make_Selected_Component
(Loc
,
2164 Prefix
=> Unchecked_Convert_To
(T
, Ptr
),
2166 New_Occurrence_Of
(Next_Entity
(First_Entity
(T
)), Loc
));
2168 Nam
:= Make_Explicit_Dereference
(Loc
, Nam
);
2170 if Present
(Parameter_Associations
(N
)) then
2171 Parm
:= Parameter_Associations
(N
);
2176 Prepend
(Obj
, Parm
);
2178 if Etype
(D_T
) = Standard_Void_Type
then
2179 Call
:= Make_Procedure_Call_Statement
(Loc
,
2181 Parameter_Associations
=> Parm
);
2183 Call
:= Make_Function_Call
(Loc
,
2185 Parameter_Associations
=> Parm
);
2188 Set_First_Named_Actual
(Call
, First_Named_Actual
(N
));
2189 Set_Etype
(Call
, Etype
(D_T
));
2191 -- We do not re-analyze the call to avoid infinite recursion.
2192 -- We analyze separately the prefix and the object, and set
2193 -- the checks on the prefix that would otherwise be emitted
2194 -- when resolving a call.
2198 Apply_Access_Check
(Nam
);
2205 -- If this is a call to an intrinsic subprogram, then perform the
2206 -- appropriate expansion to the corresponding tree node and we
2207 -- are all done (since after that the call is gone!)
2209 -- In the case where the intrinsic is to be processed by the back end,
2210 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2211 -- since the idea in this case is to pass the call unchanged.
2213 if Is_Intrinsic_Subprogram
(Subp
) then
2214 Expand_Intrinsic_Call
(N
, Subp
);
2218 if Ekind
(Subp
) = E_Function
2219 or else Ekind
(Subp
) = E_Procedure
2221 if Is_Inlined
(Subp
) then
2223 Inlined_Subprogram
: declare
2225 Must_Inline
: Boolean := False;
2226 Spec
: constant Node_Id
:= Unit_Declaration_Node
(Subp
);
2227 Scop
: constant Entity_Id
:= Scope
(Subp
);
2229 function In_Unfrozen_Instance
return Boolean;
2230 -- If the subprogram comes from an instance in the same
2231 -- unit, and the instance is not yet frozen, inlining might
2232 -- trigger order-of-elaboration problems in gigi.
2234 --------------------------
2235 -- In_Unfrozen_Instance --
2236 --------------------------
2238 function In_Unfrozen_Instance
return Boolean is
2244 and then S
/= Standard_Standard
2246 if Is_Generic_Instance
(S
)
2247 and then Present
(Freeze_Node
(S
))
2248 and then not Analyzed
(Freeze_Node
(S
))
2257 end In_Unfrozen_Instance
;
2259 -- Start of processing for Inlined_Subprogram
2262 -- Verify that the body to inline has already been seen, and
2263 -- that if the body is in the current unit the inlining does
2264 -- not occur earlier. This avoids order-of-elaboration problems
2267 -- This should be documented in sinfo/einfo ???
2270 or else Nkind
(Spec
) /= N_Subprogram_Declaration
2271 or else No
(Body_To_Inline
(Spec
))
2273 Must_Inline
:= False;
2275 -- If this an inherited function that returns a private
2276 -- type, do not inline if the full view is an unconstrained
2277 -- array, because such calls cannot be inlined.
2279 elsif Present
(Orig_Subp
)
2280 and then Is_Array_Type
(Etype
(Orig_Subp
))
2281 and then not Is_Constrained
(Etype
(Orig_Subp
))
2283 Must_Inline
:= False;
2285 elsif In_Unfrozen_Instance
then
2286 Must_Inline
:= False;
2289 Bod
:= Body_To_Inline
(Spec
);
2291 if (In_Extended_Main_Code_Unit
(N
)
2292 or else In_Extended_Main_Code_Unit
(Parent
(N
))
2293 or else Is_Always_Inlined
(Subp
))
2294 and then (not In_Same_Extended_Unit
(Sloc
(Bod
), Loc
)
2296 Earlier_In_Extended_Unit
(Sloc
(Bod
), Loc
))
2298 Must_Inline
:= True;
2300 -- If we are compiling a package body that is not the main
2301 -- unit, it must be for inlining/instantiation purposes,
2302 -- in which case we inline the call to insure that the same
2303 -- temporaries are generated when compiling the body by
2304 -- itself. Otherwise link errors can occur.
2306 -- If the function being called is itself in the main unit,
2307 -- we cannot inline, because there is a risk of double
2308 -- elaboration and/or circularity: the inlining can make
2309 -- visible a private entity in the body of the main unit,
2310 -- that gigi will see before its sees its proper definition.
2312 elsif not (In_Extended_Main_Code_Unit
(N
))
2313 and then In_Package_Body
2315 Must_Inline
:= not In_Extended_Main_Source_Unit
(Subp
);
2320 Expand_Inlined_Call
(N
, Subp
, Orig_Subp
);
2323 -- Let the back end handle it
2325 Add_Inlined_Body
(Subp
);
2327 if Front_End_Inlining
2328 and then Nkind
(Spec
) = N_Subprogram_Declaration
2329 and then (In_Extended_Main_Code_Unit
(N
))
2330 and then No
(Body_To_Inline
(Spec
))
2331 and then not Has_Completion
(Subp
)
2332 and then In_Same_Extended_Unit
(Sloc
(Spec
), Loc
)
2335 ("cannot inline& (body not seen yet)?",
2339 end Inlined_Subprogram
;
2343 -- Check for a protected subprogram. This is either an intra-object
2344 -- call, or a protected function call. Protected procedure calls are
2345 -- rewritten as entry calls and handled accordingly.
2347 -- In Ada 2005, this may be an indirect call to an access parameter
2348 -- that is an access_to_subprogram. In that case the anonymous type
2349 -- has a scope that is a protected operation, but the call is a
2352 Scop
:= Scope
(Subp
);
2354 if Nkind
(N
) /= N_Entry_Call_Statement
2355 and then Is_Protected_Type
(Scop
)
2356 and then Ekind
(Subp
) /= E_Subprogram_Type
2358 -- If the call is an internal one, it is rewritten as a call to
2359 -- to the corresponding unprotected subprogram.
2361 Expand_Protected_Subprogram_Call
(N
, Subp
, Scop
);
2364 -- Functions returning controlled objects need special attention
2366 if Controlled_Type
(Etype
(Subp
))
2367 and then not Is_Return_By_Reference_Type
(Etype
(Subp
))
2369 Expand_Ctrl_Function_Call
(N
);
2372 -- Test for First_Optional_Parameter, and if so, truncate parameter
2373 -- list if there are optional parameters at the trailing end.
2374 -- Note we never delete procedures for call via a pointer.
2376 if (Ekind
(Subp
) = E_Procedure
or else Ekind
(Subp
) = E_Function
)
2377 and then Present
(First_Optional_Parameter
(Subp
))
2380 Last_Keep_Arg
: Node_Id
;
2383 -- Last_Keep_Arg will hold the last actual that should be
2384 -- retained. If it remains empty at the end, it means that
2385 -- all parameters are optional.
2387 Last_Keep_Arg
:= Empty
;
2389 -- Find first optional parameter, must be present since we
2390 -- checked the validity of the parameter before setting it.
2392 Formal
:= First_Formal
(Subp
);
2393 Actual
:= First_Actual
(N
);
2394 while Formal
/= First_Optional_Parameter
(Subp
) loop
2395 Last_Keep_Arg
:= Actual
;
2396 Next_Formal
(Formal
);
2397 Next_Actual
(Actual
);
2400 -- We have Formal and Actual pointing to the first potentially
2401 -- droppable argument. We can drop all the trailing arguments
2402 -- whose actual matches the default. Note that we know that all
2403 -- remaining formals have defaults, because we checked that this
2404 -- requirement was met before setting First_Optional_Parameter.
2406 -- We use Fully_Conformant_Expressions to check for identity
2407 -- between formals and actuals, which may miss some cases, but
2408 -- on the other hand, this is only an optimization (if we fail
2409 -- to truncate a parameter it does not affect functionality).
2410 -- So if the default is 3 and the actual is 1+2, we consider
2411 -- them unequal, which hardly seems worrisome.
2413 while Present
(Formal
) loop
2414 if not Fully_Conformant_Expressions
2415 (Actual
, Default_Value
(Formal
))
2417 Last_Keep_Arg
:= Actual
;
2420 Next_Formal
(Formal
);
2421 Next_Actual
(Actual
);
2424 -- If no arguments, delete entire list, this is the easy case
2426 if No
(Last_Keep_Arg
) then
2427 while Is_Non_Empty_List
(Parameter_Associations
(N
)) loop
2428 Delete_Tree
(Remove_Head
(Parameter_Associations
(N
)));
2431 Set_Parameter_Associations
(N
, No_List
);
2432 Set_First_Named_Actual
(N
, Empty
);
2434 -- Case where at the last retained argument is positional. This
2435 -- is also an easy case, since the retained arguments are already
2436 -- in the right form, and we don't need to worry about the order
2437 -- of arguments that get eliminated.
2439 elsif Is_List_Member
(Last_Keep_Arg
) then
2440 while Present
(Next
(Last_Keep_Arg
)) loop
2441 Delete_Tree
(Remove_Next
(Last_Keep_Arg
));
2444 Set_First_Named_Actual
(N
, Empty
);
2446 -- This is the annoying case where the last retained argument
2447 -- is a named parameter. Since the original arguments are not
2448 -- in declaration order, we may have to delete some fairly
2449 -- random collection of arguments.
2457 pragma Warnings
(Off
, Discard
);
2460 -- First step, remove all the named parameters from the
2461 -- list (they are still chained using First_Named_Actual
2462 -- and Next_Named_Actual, so we have not lost them!)
2464 Temp
:= First
(Parameter_Associations
(N
));
2466 -- Case of all parameters named, remove them all
2468 if Nkind
(Temp
) = N_Parameter_Association
then
2469 while Is_Non_Empty_List
(Parameter_Associations
(N
)) loop
2470 Temp
:= Remove_Head
(Parameter_Associations
(N
));
2473 -- Case of mixed positional/named, remove named parameters
2476 while Nkind
(Next
(Temp
)) /= N_Parameter_Association
loop
2480 while Present
(Next
(Temp
)) loop
2481 Discard
:= Remove_Next
(Temp
);
2485 -- Now we loop through the named parameters, till we get
2486 -- to the last one to be retained, adding them to the list.
2487 -- Note that the Next_Named_Actual list does not need to be
2488 -- touched since we are only reordering them on the actual
2489 -- parameter association list.
2491 Passoc
:= Parent
(First_Named_Actual
(N
));
2493 Temp
:= Relocate_Node
(Passoc
);
2495 (Parameter_Associations
(N
), Temp
);
2497 Last_Keep_Arg
= Explicit_Actual_Parameter
(Passoc
);
2498 Passoc
:= Parent
(Next_Named_Actual
(Passoc
));
2501 Set_Next_Named_Actual
(Temp
, Empty
);
2504 Temp
:= Next_Named_Actual
(Passoc
);
2505 exit when No
(Temp
);
2506 Set_Next_Named_Actual
2507 (Passoc
, Next_Named_Actual
(Parent
(Temp
)));
2515 -- Special processing for Ada 2005 AI-329, which requires a call to
2516 -- Raise_Exception to raise Constraint_Error if the Exception_Id is
2517 -- null. Note that we never need to do this in GNAT mode, or if the
2518 -- parameter to Raise_Exception is a use of Identity, since in these
2519 -- cases we know that the parameter is never null.
2521 if Ada_Version
>= Ada_05
2522 and then not GNAT_Mode
2523 and then Is_RTE
(Subp
, RE_Raise_Exception
)
2524 and then (Nkind
(First_Actual
(N
)) /= N_Attribute_Reference
2525 or else Attribute_Name
(First_Actual
(N
)) /= Name_Identity
)
2528 RCE
: constant Node_Id
:=
2529 Make_Raise_Constraint_Error
(Loc
,
2530 Reason
=> CE_Null_Exception_Id
);
2532 Insert_After
(N
, RCE
);
2538 --------------------------
2539 -- Expand_Inlined_Call --
2540 --------------------------
2542 procedure Expand_Inlined_Call
2545 Orig_Subp
: Entity_Id
)
2547 Loc
: constant Source_Ptr
:= Sloc
(N
);
2548 Is_Predef
: constant Boolean :=
2549 Is_Predefined_File_Name
2550 (Unit_File_Name
(Get_Source_Unit
(Subp
)));
2551 Orig_Bod
: constant Node_Id
:=
2552 Body_To_Inline
(Unit_Declaration_Node
(Subp
));
2557 Decls
: constant List_Id
:= New_List
;
2558 Exit_Lab
: Entity_Id
:= Empty
;
2565 Ret_Type
: Entity_Id
;
2569 Temp_Typ
: Entity_Id
;
2571 Is_Unc
: constant Boolean :=
2572 Is_Array_Type
(Etype
(Subp
))
2573 and then not Is_Constrained
(Etype
(Subp
));
2574 -- If the type returned by the function is unconstrained and the
2575 -- call can be inlined, special processing is required.
2577 procedure Find_Result
;
2578 -- For a function that returns an unconstrained type, retrieve the
2579 -- name of the single variable that is the expression of a return
2580 -- statement in the body of the function. Build_Body_To_Inline has
2581 -- verified that this variable is unique, even in the presence of
2582 -- multiple return statements.
2584 procedure Make_Exit_Label
;
2585 -- Build declaration for exit label to be used in Return statements
2587 function Process_Formals
(N
: Node_Id
) return Traverse_Result
;
2588 -- Replace occurrence of a formal with the corresponding actual, or
2589 -- the thunk generated for it.
2591 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
;
2592 -- If the call being expanded is that of an internal subprogram,
2593 -- set the sloc of the generated block to that of the call itself,
2594 -- so that the expansion is skipped by the -next- command in gdb.
2595 -- Same processing for a subprogram in a predefined file, e.g.
2596 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2597 -- to simplify our own development.
2599 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
);
2600 -- If the function body is a single expression, replace call with
2601 -- expression, else insert block appropriately.
2603 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
);
2604 -- If procedure body has no local variables, inline body without
2605 -- creating block, otherwise rewrite call with block.
2607 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean;
2608 -- Determine whether a formal parameter is used only once in Orig_Bod
2614 procedure Find_Result
is
2618 function Get_Return
(N
: Node_Id
) return Traverse_Result
;
2619 -- Recursive function to locate return statements in body.
2621 function Get_Return
(N
: Node_Id
) return Traverse_Result
is
2623 if Nkind
(N
) = N_Return_Statement
then
2624 Id
:= Expression
(N
);
2631 procedure Find_It
is new Traverse_Proc
(Get_Return
);
2633 -- Start of processing for Find_Result
2636 Find_It
(Handled_Statement_Sequence
(Orig_Bod
));
2638 -- At this point the body is unanalyzed. Traverse the list of
2639 -- declarations to locate the defining_identifier for it.
2641 Decl
:= First
(Declarations
(Blk
));
2643 while Present
(Decl
) loop
2644 if Chars
(Defining_Identifier
(Decl
)) = Chars
(Id
) then
2645 Targ1
:= Defining_Identifier
(Decl
);
2654 ---------------------
2655 -- Make_Exit_Label --
2656 ---------------------
2658 procedure Make_Exit_Label
is
2660 -- Create exit label for subprogram if one does not exist yet
2662 if No
(Exit_Lab
) then
2663 Lab_Id
:= Make_Identifier
(Loc
, New_Internal_Name
('L'));
2665 Make_Defining_Identifier
(Loc
, Chars
(Lab_Id
)));
2666 Exit_Lab
:= Make_Label
(Loc
, Lab_Id
);
2669 Make_Implicit_Label_Declaration
(Loc
,
2670 Defining_Identifier
=> Entity
(Lab_Id
),
2671 Label_Construct
=> Exit_Lab
);
2673 end Make_Exit_Label
;
2675 ---------------------
2676 -- Process_Formals --
2677 ---------------------
2679 function Process_Formals
(N
: Node_Id
) return Traverse_Result
is
2685 if Is_Entity_Name
(N
)
2686 and then Present
(Entity
(N
))
2691 and then Scope
(E
) = Subp
2693 A
:= Renamed_Object
(E
);
2695 if Is_Entity_Name
(A
) then
2696 Rewrite
(N
, New_Occurrence_Of
(Entity
(A
), Loc
));
2698 elsif Nkind
(A
) = N_Defining_Identifier
then
2699 Rewrite
(N
, New_Occurrence_Of
(A
, Loc
));
2701 else -- numeric literal
2702 Rewrite
(N
, New_Copy
(A
));
2708 elsif Nkind
(N
) = N_Return_Statement
then
2710 if No
(Expression
(N
)) then
2712 Rewrite
(N
, Make_Goto_Statement
(Loc
,
2713 Name
=> New_Copy
(Lab_Id
)));
2716 if Nkind
(Parent
(N
)) = N_Handled_Sequence_Of_Statements
2717 and then Nkind
(Parent
(Parent
(N
))) = N_Subprogram_Body
2719 -- Function body is a single expression. No need for
2725 Num_Ret
:= Num_Ret
+ 1;
2729 -- Because of the presence of private types, the views of the
2730 -- expression and the context may be different, so place an
2731 -- unchecked conversion to the context type to avoid spurious
2732 -- errors, eg. when the expression is a numeric literal and
2733 -- the context is private. If the expression is an aggregate,
2734 -- use a qualified expression, because an aggregate is not a
2735 -- legal argument of a conversion.
2737 if Nkind
(Expression
(N
)) = N_Aggregate
2738 or else Nkind
(Expression
(N
)) = N_Null
2741 Make_Qualified_Expression
(Sloc
(N
),
2742 Subtype_Mark
=> New_Occurrence_Of
(Ret_Type
, Sloc
(N
)),
2743 Expression
=> Relocate_Node
(Expression
(N
)));
2746 Unchecked_Convert_To
2747 (Ret_Type
, Relocate_Node
(Expression
(N
)));
2750 if Nkind
(Targ
) = N_Defining_Identifier
then
2752 Make_Assignment_Statement
(Loc
,
2753 Name
=> New_Occurrence_Of
(Targ
, Loc
),
2754 Expression
=> Ret
));
2757 Make_Assignment_Statement
(Loc
,
2758 Name
=> New_Copy
(Targ
),
2759 Expression
=> Ret
));
2762 Set_Assignment_OK
(Name
(N
));
2764 if Present
(Exit_Lab
) then
2766 Make_Goto_Statement
(Loc
,
2767 Name
=> New_Copy
(Lab_Id
)));
2773 -- Remove pragma Unreferenced since it may refer to formals that
2774 -- are not visible in the inlined body, and in any case we will
2775 -- not be posting warnings on the inlined body so it is unneeded.
2777 elsif Nkind
(N
) = N_Pragma
2778 and then Chars
(N
) = Name_Unreferenced
2780 Rewrite
(N
, Make_Null_Statement
(Sloc
(N
)));
2786 end Process_Formals
;
2788 procedure Replace_Formals
is new Traverse_Proc
(Process_Formals
);
2794 function Process_Sloc
(Nod
: Node_Id
) return Traverse_Result
is
2796 if not Debug_Generated_Code
then
2797 Set_Sloc
(Nod
, Sloc
(N
));
2798 Set_Comes_From_Source
(Nod
, False);
2804 procedure Reset_Slocs
is new Traverse_Proc
(Process_Sloc
);
2806 ---------------------------
2807 -- Rewrite_Function_Call --
2808 ---------------------------
2810 procedure Rewrite_Function_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2811 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2812 Fst
: constant Node_Id
:= First
(Statements
(HSS
));
2815 -- Optimize simple case: function body is a single return statement,
2816 -- which has been expanded into an assignment.
2818 if Is_Empty_List
(Declarations
(Blk
))
2819 and then Nkind
(Fst
) = N_Assignment_Statement
2820 and then No
(Next
(Fst
))
2823 -- The function call may have been rewritten as the temporary
2824 -- that holds the result of the call, in which case remove the
2825 -- now useless declaration.
2827 if Nkind
(N
) = N_Identifier
2828 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2830 Rewrite
(Parent
(Entity
(N
)), Make_Null_Statement
(Loc
));
2833 Rewrite
(N
, Expression
(Fst
));
2835 elsif Nkind
(N
) = N_Identifier
2836 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
2838 -- The block assigns the result of the call to the temporary
2840 Insert_After
(Parent
(Entity
(N
)), Blk
);
2842 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
2844 (Is_Entity_Name
(Name
(Parent
(N
)))
2846 (Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
2847 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))))
2849 -- Replace assignment with the block
2852 Original_Assignment
: constant Node_Id
:= Parent
(N
);
2855 -- Preserve the original assignment node to keep the complete
2856 -- assignment subtree consistent enough for Analyze_Assignment
2857 -- to proceed (specifically, the original Lhs node must still
2858 -- have an assignment statement as its parent).
2860 -- We cannot rely on Original_Node to go back from the block
2861 -- node to the assignment node, because the assignment might
2862 -- already be a rewrite substitution.
2864 Discard_Node
(Relocate_Node
(Original_Assignment
));
2865 Rewrite
(Original_Assignment
, Blk
);
2868 elsif Nkind
(Parent
(N
)) = N_Object_Declaration
then
2869 Set_Expression
(Parent
(N
), Empty
);
2870 Insert_After
(Parent
(N
), Blk
);
2873 Insert_Before
(Parent
(N
), Blk
);
2875 end Rewrite_Function_Call
;
2877 ----------------------------
2878 -- Rewrite_Procedure_Call --
2879 ----------------------------
2881 procedure Rewrite_Procedure_Call
(N
: Node_Id
; Blk
: Node_Id
) is
2882 HSS
: constant Node_Id
:= Handled_Statement_Sequence
(Blk
);
2884 if Is_Empty_List
(Declarations
(Blk
)) then
2885 Insert_List_After
(N
, Statements
(HSS
));
2886 Rewrite
(N
, Make_Null_Statement
(Loc
));
2890 end Rewrite_Procedure_Call
;
2892 -------------------------
2893 -- Formal_Is_Used_Once --
2894 ------------------------
2896 function Formal_Is_Used_Once
(Formal
: Entity_Id
) return Boolean is
2897 Use_Counter
: Int
:= 0;
2899 function Count_Uses
(N
: Node_Id
) return Traverse_Result
;
2900 -- Traverse the tree and count the uses of the formal parameter.
2901 -- In this case, for optimization purposes, we do not need to
2902 -- continue the traversal once more than one use is encountered.
2908 function Count_Uses
(N
: Node_Id
) return Traverse_Result
is
2910 -- The original node is an identifier
2912 if Nkind
(N
) = N_Identifier
2913 and then Present
(Entity
(N
))
2915 -- Original node's entity points to the one in the copied body
2917 and then Nkind
(Entity
(N
)) = N_Identifier
2918 and then Present
(Entity
(Entity
(N
)))
2920 -- The entity of the copied node is the formal parameter
2922 and then Entity
(Entity
(N
)) = Formal
2924 Use_Counter
:= Use_Counter
+ 1;
2926 if Use_Counter
> 1 then
2928 -- Denote more than one use and abandon the traversal
2939 procedure Count_Formal_Uses
is new Traverse_Proc
(Count_Uses
);
2941 -- Start of processing for Formal_Is_Used_Once
2944 Count_Formal_Uses
(Orig_Bod
);
2945 return Use_Counter
= 1;
2946 end Formal_Is_Used_Once
;
2948 -- Start of processing for Expand_Inlined_Call
2951 -- Check for special case of To_Address call, and if so, just do an
2952 -- unchecked conversion instead of expanding the call. Not only is this
2953 -- more efficient, but it also avoids problem with order of elaboration
2954 -- when address clauses are inlined (address expression elaborated at
2957 if Subp
= RTE
(RE_To_Address
) then
2959 Unchecked_Convert_To
2961 Relocate_Node
(First_Actual
(N
))));
2965 -- Check for an illegal attempt to inline a recursive procedure. If the
2966 -- subprogram has parameters this is detected when trying to supply a
2967 -- binding for parameters that already have one. For parameterless
2968 -- subprograms this must be done explicitly.
2970 if In_Open_Scopes
(Subp
) then
2971 Error_Msg_N
("call to recursive subprogram cannot be inlined?", N
);
2972 Set_Is_Inlined
(Subp
, False);
2976 if Nkind
(Orig_Bod
) = N_Defining_Identifier
2977 or else Nkind
(Orig_Bod
) = N_Defining_Operator_Symbol
2979 -- Subprogram is a renaming_as_body. Calls appearing after the
2980 -- renaming can be replaced with calls to the renamed entity
2981 -- directly, because the subprograms are subtype conformant. If
2982 -- the renamed subprogram is an inherited operation, we must redo
2983 -- the expansion because implicit conversions may be needed.
2985 Set_Name
(N
, New_Occurrence_Of
(Orig_Bod
, Loc
));
2987 if Present
(Alias
(Orig_Bod
)) then
2994 -- Use generic machinery to copy body of inlined subprogram, as if it
2995 -- were an instantiation, resetting source locations appropriately, so
2996 -- that nested inlined calls appear in the main unit.
2998 Save_Env
(Subp
, Empty
);
2999 Set_Copied_Sloc_For_Inlined_Body
(N
, Defining_Entity
(Orig_Bod
));
3001 Bod
:= Copy_Generic_Node
(Orig_Bod
, Empty
, Instantiating
=> True);
3003 Make_Block_Statement
(Loc
,
3004 Declarations
=> Declarations
(Bod
),
3005 Handled_Statement_Sequence
=> Handled_Statement_Sequence
(Bod
));
3007 if No
(Declarations
(Bod
)) then
3008 Set_Declarations
(Blk
, New_List
);
3011 -- For the unconstrained case, capture the name of the local
3012 -- variable that holds the result.
3018 -- If this is a derived function, establish the proper return type
3020 if Present
(Orig_Subp
)
3021 and then Orig_Subp
/= Subp
3023 Ret_Type
:= Etype
(Orig_Subp
);
3025 Ret_Type
:= Etype
(Subp
);
3028 -- Create temporaries for the actuals that are expressions, or that
3029 -- are scalars and require copying to preserve semantics.
3031 F
:= First_Formal
(Subp
);
3032 A
:= First_Actual
(N
);
3033 while Present
(F
) loop
3034 if Present
(Renamed_Object
(F
)) then
3035 Error_Msg_N
("cannot inline call to recursive subprogram", N
);
3039 -- If the argument may be a controlling argument in a call within
3040 -- the inlined body, we must preserve its classwide nature to insure
3041 -- that dynamic dispatching take place subsequently. If the formal
3042 -- has a constraint it must be preserved to retain the semantics of
3045 if Is_Class_Wide_Type
(Etype
(F
))
3046 or else (Is_Access_Type
(Etype
(F
))
3048 Is_Class_Wide_Type
(Designated_Type
(Etype
(F
))))
3050 Temp_Typ
:= Etype
(F
);
3052 elsif Base_Type
(Etype
(F
)) = Base_Type
(Etype
(A
))
3053 and then Etype
(F
) /= Base_Type
(Etype
(F
))
3055 Temp_Typ
:= Etype
(F
);
3058 Temp_Typ
:= Etype
(A
);
3061 -- If the actual is a simple name or a literal, no need to
3062 -- create a temporary, object can be used directly.
3064 if (Is_Entity_Name
(A
)
3066 (not Is_Scalar_Type
(Etype
(A
))
3067 or else Ekind
(Entity
(A
)) = E_Enumeration_Literal
))
3069 -- When the actual is an identifier and the corresponding formal
3070 -- is used only once in the original body, the formal can be
3071 -- substituted directly with the actual parameter.
3073 or else (Nkind
(A
) = N_Identifier
3074 and then Formal_Is_Used_Once
(F
))
3076 or else Nkind
(A
) = N_Real_Literal
3077 or else Nkind
(A
) = N_Integer_Literal
3078 or else Nkind
(A
) = N_Character_Literal
3080 if Etype
(F
) /= Etype
(A
) then
3082 (F
, Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
)));
3084 Set_Renamed_Object
(F
, A
);
3089 Make_Defining_Identifier
(Loc
,
3090 Chars
=> New_Internal_Name
('C'));
3092 -- If the actual for an in/in-out parameter is a view conversion,
3093 -- make it into an unchecked conversion, given that an untagged
3094 -- type conversion is not a proper object for a renaming.
3096 -- In-out conversions that involve real conversions have already
3097 -- been transformed in Expand_Actuals.
3099 if Nkind
(A
) = N_Type_Conversion
3100 and then Ekind
(F
) /= E_In_Parameter
3102 New_A
:= Make_Unchecked_Type_Conversion
(Loc
,
3103 Subtype_Mark
=> New_Occurrence_Of
(Etype
(F
), Loc
),
3104 Expression
=> Relocate_Node
(Expression
(A
)));
3106 elsif Etype
(F
) /= Etype
(A
) then
3107 New_A
:= Unchecked_Convert_To
(Etype
(F
), Relocate_Node
(A
));
3108 Temp_Typ
:= Etype
(F
);
3111 New_A
:= Relocate_Node
(A
);
3114 Set_Sloc
(New_A
, Sloc
(N
));
3116 if Ekind
(F
) = E_In_Parameter
3117 and then not Is_Limited_Type
(Etype
(A
))
3120 Make_Object_Declaration
(Loc
,
3121 Defining_Identifier
=> Temp
,
3122 Constant_Present
=> True,
3123 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3124 Expression
=> New_A
);
3127 Make_Object_Renaming_Declaration
(Loc
,
3128 Defining_Identifier
=> Temp
,
3129 Subtype_Mark
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3133 Append
(Decl
, Decls
);
3134 Set_Renamed_Object
(F
, Temp
);
3141 -- Establish target of function call. If context is not assignment or
3142 -- declaration, create a temporary as a target. The declaration for
3143 -- the temporary may be subsequently optimized away if the body is a
3144 -- single expression, or if the left-hand side of the assignment is
3145 -- simple enough, i.e. an entity or an explicit dereference of one.
3147 if Ekind
(Subp
) = E_Function
then
3148 if Nkind
(Parent
(N
)) = N_Assignment_Statement
3149 and then Is_Entity_Name
(Name
(Parent
(N
)))
3151 Targ
:= Name
(Parent
(N
));
3153 elsif Nkind
(Parent
(N
)) = N_Assignment_Statement
3154 and then Nkind
(Name
(Parent
(N
))) = N_Explicit_Dereference
3155 and then Is_Entity_Name
(Prefix
(Name
(Parent
(N
))))
3157 Targ
:= Name
(Parent
(N
));
3160 -- Replace call with temporary and create its declaration
3163 Make_Defining_Identifier
(Loc
, New_Internal_Name
('C'));
3164 Set_Is_Internal
(Temp
);
3166 -- For the unconstrained case. the generated temporary has the
3167 -- same constrained declaration as the result variable.
3168 -- It may eventually be possible to remove that temporary and
3169 -- use the result variable directly.
3173 Make_Object_Declaration
(Loc
,
3174 Defining_Identifier
=> Temp
,
3175 Object_Definition
=>
3176 New_Copy_Tree
(Object_Definition
(Parent
(Targ1
))));
3178 Replace_Formals
(Decl
);
3182 Make_Object_Declaration
(Loc
,
3183 Defining_Identifier
=> Temp
,
3184 Object_Definition
=>
3185 New_Occurrence_Of
(Ret_Type
, Loc
));
3187 Set_Etype
(Temp
, Ret_Type
);
3190 Set_No_Initialization
(Decl
);
3191 Append
(Decl
, Decls
);
3192 Rewrite
(N
, New_Occurrence_Of
(Temp
, Loc
));
3197 Insert_Actions
(N
, Decls
);
3199 -- Traverse the tree and replace formals with actuals or their thunks.
3200 -- Attach block to tree before analysis and rewriting.
3202 Replace_Formals
(Blk
);
3203 Set_Parent
(Blk
, N
);
3205 if not Comes_From_Source
(Subp
)
3211 if Present
(Exit_Lab
) then
3213 -- If the body was a single expression, the single return statement
3214 -- and the corresponding label are useless.
3218 Nkind
(Last
(Statements
(Handled_Statement_Sequence
(Blk
)))) =
3221 Remove
(Last
(Statements
(Handled_Statement_Sequence
(Blk
))));
3223 Append
(Lab_Decl
, (Declarations
(Blk
)));
3224 Append
(Exit_Lab
, Statements
(Handled_Statement_Sequence
(Blk
)));
3228 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
3229 -- conflicting private views that Gigi would ignore. If this is
3230 -- predefined unit, analyze with checks off, as is done in the non-
3231 -- inlined run-time units.
3234 I_Flag
: constant Boolean := In_Inlined_Body
;
3237 In_Inlined_Body
:= True;
3241 Style
: constant Boolean := Style_Check
;
3243 Style_Check
:= False;
3244 Analyze
(Blk
, Suppress
=> All_Checks
);
3245 Style_Check
:= Style
;
3252 In_Inlined_Body
:= I_Flag
;
3255 if Ekind
(Subp
) = E_Procedure
then
3256 Rewrite_Procedure_Call
(N
, Blk
);
3258 Rewrite_Function_Call
(N
, Blk
);
3260 -- For the unconstrained case, the replacement of the call has been
3261 -- made prior to the complete analysis of the generated declarations.
3262 -- Propagate the proper type now.
3265 if Nkind
(N
) = N_Identifier
then
3266 Set_Etype
(N
, Etype
(Entity
(N
)));
3268 Set_Etype
(N
, Etype
(Targ1
));
3275 -- Cleanup mapping between formals and actuals for other expansions
3277 F
:= First_Formal
(Subp
);
3278 while Present
(F
) loop
3279 Set_Renamed_Object
(F
, Empty
);
3282 end Expand_Inlined_Call
;
3284 ----------------------------
3285 -- Expand_N_Function_Call --
3286 ----------------------------
3288 procedure Expand_N_Function_Call
(N
: Node_Id
) is
3289 Typ
: constant Entity_Id
:= Etype
(N
);
3291 function Returned_By_Reference
return Boolean;
3292 -- If the return type is returned through the secondary stack. that is
3293 -- by reference, we don't want to create a temp to force stack checking.
3294 -- Shouldn't this function be moved to exp_util???
3296 function Rhs_Of_Assign_Or_Decl
(N
: Node_Id
) return Boolean;
3297 -- If the call is the right side of an assignment or the expression in
3298 -- an object declaration, we don't need to create a temp as the left
3299 -- side will already trigger stack checking if necessary.
3301 -- If the call is a component in an extension aggregate, it will be
3302 -- expanded into assignments as well, so no temporary is needed. This
3303 -- also solves the problem of functions returning types with unknown
3304 -- discriminants, where it is not possible to declare an object of the
3307 ---------------------------
3308 -- Returned_By_Reference --
3309 ---------------------------
3311 function Returned_By_Reference
return Boolean is
3315 if Is_Return_By_Reference_Type
(Typ
) then
3318 elsif Nkind
(Parent
(N
)) /= N_Return_Statement
then
3321 elsif Requires_Transient_Scope
(Typ
) then
3323 -- Verify that the return type of the enclosing function has the
3324 -- same constrained status as that of the expression.
3327 while Ekind
(S
) /= E_Function
loop
3331 return Is_Constrained
(Typ
) = Is_Constrained
(Etype
(S
));
3335 end Returned_By_Reference
;
3337 ---------------------------
3338 -- Rhs_Of_Assign_Or_Decl --
3339 ---------------------------
3341 function Rhs_Of_Assign_Or_Decl
(N
: Node_Id
) return Boolean is
3343 if (Nkind
(Parent
(N
)) = N_Assignment_Statement
3344 and then Expression
(Parent
(N
)) = N
)
3346 (Nkind
(Parent
(N
)) = N_Qualified_Expression
3347 and then Nkind
(Parent
(Parent
(N
))) = N_Assignment_Statement
3348 and then Expression
(Parent
(Parent
(N
))) = Parent
(N
))
3350 (Nkind
(Parent
(N
)) = N_Object_Declaration
3351 and then Expression
(Parent
(N
)) = N
)
3353 (Nkind
(Parent
(N
)) = N_Component_Association
3354 and then Expression
(Parent
(N
)) = N
3355 and then Nkind
(Parent
(Parent
(N
))) = N_Aggregate
3356 and then Rhs_Of_Assign_Or_Decl
(Parent
(Parent
(N
))))
3358 (Nkind
(Parent
(N
)) = N_Extension_Aggregate
3359 and then Is_Private_Type
(Etype
(Typ
)))
3365 end Rhs_Of_Assign_Or_Decl
;
3367 -- Start of processing for Expand_N_Function_Call
3370 -- A special check. If stack checking is enabled, and the return type
3371 -- might generate a large temporary, and the call is not the right side
3372 -- of an assignment, then generate an explicit temporary. We do this
3373 -- because otherwise gigi may generate a large temporary on the fly and
3374 -- this can cause trouble with stack checking.
3376 -- This is unecessary if the call is the expression in an object
3377 -- declaration, or if it appears outside of any library unit. This can
3378 -- only happen if it appears as an actual in a library-level instance,
3379 -- in which case a temporary will be generated for it once the instance
3380 -- itself is installed.
3382 if May_Generate_Large_Temp
(Typ
)
3383 and then not Rhs_Of_Assign_Or_Decl
(N
)
3384 and then not Returned_By_Reference
3385 and then Current_Scope
/= Standard_Standard
3387 if Stack_Checking_Enabled
then
3389 -- Note: it might be thought that it would be OK to use a call to
3390 -- Force_Evaluation here, but that's not good enough, because
3391 -- that can results in a 'Reference construct that may still need
3395 Loc
: constant Source_Ptr
:= Sloc
(N
);
3396 Temp_Obj
: constant Entity_Id
:=
3397 Make_Defining_Identifier
(Loc
,
3398 Chars
=> New_Internal_Name
('F'));
3399 Temp_Typ
: Entity_Id
:= Typ
;
3406 if Is_Tagged_Type
(Typ
)
3407 and then Present
(Controlling_Argument
(N
))
3409 if Nkind
(Parent
(N
)) /= N_Procedure_Call_Statement
3410 and then Nkind
(Parent
(N
)) /= N_Function_Call
3412 -- If this is a tag-indeterminate call, the object must
3415 if Is_Tag_Indeterminate
(N
) then
3416 Temp_Typ
:= Class_Wide_Type
(Typ
);
3420 -- If this is a dispatching call that is itself the
3421 -- controlling argument of an enclosing call, the
3422 -- nominal subtype of the object that replaces it must
3423 -- be classwide, so that dispatching will take place
3424 -- properly. If it is not a controlling argument, the
3425 -- object is not classwide.
3427 Proc
:= Entity
(Name
(Parent
(N
)));
3429 F
:= First_Formal
(Proc
);
3430 A
:= First_Actual
(Parent
(N
));
3436 if Is_Controlling_Formal
(F
) then
3437 Temp_Typ
:= Class_Wide_Type
(Typ
);
3443 Make_Object_Declaration
(Loc
,
3444 Defining_Identifier
=> Temp_Obj
,
3445 Object_Definition
=> New_Occurrence_Of
(Temp_Typ
, Loc
),
3446 Constant_Present
=> True,
3447 Expression
=> Relocate_Node
(N
));
3448 Set_Assignment_OK
(Decl
);
3450 Insert_Actions
(N
, New_List
(Decl
));
3451 Rewrite
(N
, New_Occurrence_Of
(Temp_Obj
, Loc
));
3455 -- If stack-checking is not enabled, increment serial number
3456 -- for internal names, so that subsequent symbols are consistent
3457 -- with and without stack-checking.
3459 Synchronize_Serial_Number
;
3461 -- Now we can expand the call with consistent symbol names
3466 -- Normal case, expand the call
3471 end Expand_N_Function_Call
;
3473 ---------------------------------------
3474 -- Expand_N_Procedure_Call_Statement --
3475 ---------------------------------------
3477 procedure Expand_N_Procedure_Call_Statement
(N
: Node_Id
) is
3480 end Expand_N_Procedure_Call_Statement
;
3482 ------------------------------
3483 -- Expand_N_Subprogram_Body --
3484 ------------------------------
3486 -- Add poll call if ATC polling is enabled, unless the body will be
3487 -- inlined by the back-end.
3489 -- Add return statement if last statement in body is not a return statement
3490 -- (this makes things easier on Gigi which does not want to have to handle
3491 -- a missing return).
3493 -- Add call to Activate_Tasks if body is a task activator
3495 -- Deal with possible detection of infinite recursion
3497 -- Eliminate body completely if convention stubbed
3499 -- Encode entity names within body, since we will not need to reference
3500 -- these entities any longer in the front end.
3502 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
3504 -- Reset Pure indication if any parameter has root type System.Address
3508 procedure Expand_N_Subprogram_Body
(N
: Node_Id
) is
3509 Loc
: constant Source_Ptr
:= Sloc
(N
);
3510 H
: constant Node_Id
:= Handled_Statement_Sequence
(N
);
3511 Body_Id
: Entity_Id
;
3512 Spec_Id
: Entity_Id
;
3519 procedure Add_Return
(S
: List_Id
);
3520 -- Append a return statement to the statement sequence S if the last
3521 -- statement is not already a return or a goto statement. Note that
3522 -- the latter test is not critical, it does not matter if we add a
3523 -- few extra returns, since they get eliminated anyway later on.
3525 procedure Expand_Thread_Body
;
3526 -- Perform required expansion of a thread body
3532 procedure Add_Return
(S
: List_Id
) is
3534 if not Is_Transfer
(Last
(S
)) then
3536 -- The source location for the return is the end label
3537 -- of the procedure in all cases. This is a bit odd when
3538 -- there are exception handlers, but not much else we can do.
3540 Append_To
(S
, Make_Return_Statement
(Sloc
(End_Label
(H
))));
3544 ------------------------
3545 -- Expand_Thread_Body --
3546 ------------------------
3548 -- The required expansion of a thread body is as follows
3550 -- procedure <thread body procedure name> is
3552 -- _Secondary_Stack : aliased
3553 -- Storage_Elements.Storage_Array
3554 -- (1 .. Storage_Offset (Sec_Stack_Size));
3555 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
3557 -- _Process_ATSD : aliased System.Threads.ATSD;
3560 -- System.Threads.Thread_Body_Enter;
3561 -- (_Secondary_Stack'Address,
3562 -- _Secondary_Stack'Length,
3563 -- _Process_ATSD'Address);
3566 -- <user declarations>
3568 -- <user statements>
3569 -- <user exception handlers>
3572 -- System.Threads.Thread_Body_Leave;
3575 -- when E : others =>
3576 -- System.Threads.Thread_Body_Exceptional_Exit (E);
3579 -- Note the exception handler is omitted if pragma Restriction
3580 -- No_Exception_Handlers is currently active.
3582 procedure Expand_Thread_Body
is
3583 User_Decls
: constant List_Id
:= Declarations
(N
);
3584 Sec_Stack_Len
: Node_Id
;
3586 TB_Pragma
: constant Node_Id
:=
3587 Get_Rep_Pragma
(Spec_Id
, Name_Thread_Body
);
3590 Ent_ATSD
: Entity_Id
;
3594 Decl_ATSD
: Node_Id
;
3596 Excep_Handlers
: List_Id
;
3599 New_Scope
(Spec_Id
);
3601 -- Get proper setting for secondary stack size
3603 if List_Length
(Pragma_Argument_Associations
(TB_Pragma
)) = 2 then
3605 Expression
(Last
(Pragma_Argument_Associations
(TB_Pragma
)));
3608 New_Occurrence_Of
(RTE
(RE_Default_Secondary_Stack_Size
), Loc
);
3611 Sec_Stack_Len
:= Convert_To
(RTE
(RE_Storage_Offset
), Sec_Stack_Len
);
3613 -- Build and set declarations for the wrapped thread body
3615 Ent_SS
:= Make_Defining_Identifier
(Loc
, Name_uSecondary_Stack
);
3616 Ent_ATSD
:= Make_Defining_Identifier
(Loc
, Name_uProcess_ATSD
);
3619 Make_Object_Declaration
(Loc
,
3620 Defining_Identifier
=> Ent_SS
,
3621 Aliased_Present
=> True,
3622 Object_Definition
=>
3623 Make_Subtype_Indication
(Loc
,
3625 New_Occurrence_Of
(RTE
(RE_Storage_Array
), Loc
),
3627 Make_Index_Or_Discriminant_Constraint
(Loc
,
3628 Constraints
=> New_List
(
3630 Low_Bound
=> Make_Integer_Literal
(Loc
, 1),
3631 High_Bound
=> Sec_Stack_Len
)))));
3634 Make_Object_Declaration
(Loc
,
3635 Defining_Identifier
=> Ent_ATSD
,
3636 Aliased_Present
=> True,
3637 Object_Definition
=> New_Occurrence_Of
(RTE
(RE_ATSD
), Loc
));
3639 Set_Declarations
(N
, New_List
(Decl_SS
, Decl_ATSD
));
3641 Analyze
(Decl_ATSD
);
3642 Set_Alignment
(Ent_SS
, UI_From_Int
(Maximum_Alignment
));
3644 -- Create new exception handler
3646 if Restriction_Active
(No_Exception_Handlers
) then
3647 Excep_Handlers
:= No_List
;
3650 Check_Restriction
(No_Exception_Handlers
, N
);
3652 Ent_EO
:= Make_Defining_Identifier
(Loc
, Name_uE
);
3654 Excep_Handlers
:= New_List
(
3655 Make_Exception_Handler
(Loc
,
3656 Choice_Parameter
=> Ent_EO
,
3657 Exception_Choices
=> New_List
(
3658 Make_Others_Choice
(Loc
)),
3659 Statements
=> New_List
(
3660 Make_Procedure_Call_Statement
(Loc
,
3663 (RTE
(RE_Thread_Body_Exceptional_Exit
), Loc
),
3664 Parameter_Associations
=> New_List
(
3665 New_Occurrence_Of
(Ent_EO
, Loc
))))));
3668 -- Now build new handled statement sequence and analyze it
3670 Set_Handled_Statement_Sequence
(N
,
3671 Make_Handled_Sequence_Of_Statements
(Loc
,
3672 Statements
=> New_List
(
3674 Make_Procedure_Call_Statement
(Loc
,
3675 Name
=> New_Occurrence_Of
(RTE
(RE_Thread_Body_Enter
), Loc
),
3676 Parameter_Associations
=> New_List
(
3678 Make_Attribute_Reference
(Loc
,
3679 Prefix
=> New_Occurrence_Of
(Ent_SS
, Loc
),
3680 Attribute_Name
=> Name_Address
),
3682 Make_Attribute_Reference
(Loc
,
3683 Prefix
=> New_Occurrence_Of
(Ent_SS
, Loc
),
3684 Attribute_Name
=> Name_Length
),
3686 Make_Attribute_Reference
(Loc
,
3687 Prefix
=> New_Occurrence_Of
(Ent_ATSD
, Loc
),
3688 Attribute_Name
=> Name_Address
))),
3690 Make_Block_Statement
(Loc
,
3691 Declarations
=> User_Decls
,
3692 Handled_Statement_Sequence
=> H
),
3694 Make_Procedure_Call_Statement
(Loc
,
3695 Name
=> New_Occurrence_Of
(RTE
(RE_Thread_Body_Leave
), Loc
))),
3697 Exception_Handlers
=> Excep_Handlers
));
3699 Analyze
(Handled_Statement_Sequence
(N
));
3701 end Expand_Thread_Body
;
3703 -- Start of processing for Expand_N_Subprogram_Body
3706 -- Set L to either the list of declarations if present, or
3707 -- to the list of statements if no declarations are present.
3708 -- This is used to insert new stuff at the start.
3710 if Is_Non_Empty_List
(Declarations
(N
)) then
3711 L
:= Declarations
(N
);
3713 L
:= Statements
(Handled_Statement_Sequence
(N
));
3716 -- Find entity for subprogram
3718 Body_Id
:= Defining_Entity
(N
);
3720 if Present
(Corresponding_Spec
(N
)) then
3721 Spec_Id
:= Corresponding_Spec
(N
);
3726 -- Need poll on entry to subprogram if polling enabled. We only
3727 -- do this for non-empty subprograms, since it does not seem
3728 -- necessary to poll for a dummy null subprogram. Do not add polling
3729 -- point if calls to this subprogram will be inlined by the back-end,
3730 -- to avoid repeated polling points in nested inlinings.
3732 if Is_Non_Empty_List
(L
) then
3733 if Is_Inlined
(Spec_Id
)
3734 and then Front_End_Inlining
3735 and then Optimization_Level
> 1
3739 Generate_Poll_Call
(First
(L
));
3743 -- If this is a Pure function which has any parameters whose root
3744 -- type is System.Address, reset the Pure indication, since it will
3745 -- likely cause incorrect code to be generated as the parameter is
3746 -- probably a pointer, and the fact that the same pointer is passed
3747 -- does not mean that the same value is being referenced.
3749 -- Note that if the programmer gave an explicit Pure_Function pragma,
3750 -- then we believe the programmer, and leave the subprogram Pure.
3752 -- This code should probably be at the freeze point, so that it
3753 -- happens even on a -gnatc (or more importantly -gnatt) compile
3754 -- so that the semantic tree has Is_Pure set properly ???
3756 if Is_Pure
(Spec_Id
)
3757 and then Is_Subprogram
(Spec_Id
)
3758 and then not Has_Pragma_Pure_Function
(Spec_Id
)
3764 F
:= First_Formal
(Spec_Id
);
3765 while Present
(F
) loop
3766 if Is_Descendent_Of_Address
(Etype
(F
)) then
3767 Set_Is_Pure
(Spec_Id
, False);
3769 if Spec_Id
/= Body_Id
then
3770 Set_Is_Pure
(Body_Id
, False);
3781 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3783 if Init_Or_Norm_Scalars
and then Is_Subprogram
(Spec_Id
) then
3786 V
: constant Boolean := Validity_Checks_On
;
3789 -- We turn off validity checking, since we do not want any
3790 -- check on the initializing value itself (which we know
3791 -- may well be invalid!)
3793 Validity_Checks_On
:= False;
3795 -- Loop through formals
3797 F
:= First_Formal
(Spec_Id
);
3798 while Present
(F
) loop
3799 if Is_Scalar_Type
(Etype
(F
))
3800 and then Ekind
(F
) = E_Out_Parameter
3802 Insert_Before_And_Analyze
(First
(L
),
3803 Make_Assignment_Statement
(Loc
,
3804 Name
=> New_Occurrence_Of
(F
, Loc
),
3805 Expression
=> Get_Simple_Init_Val
(Etype
(F
), Loc
)));
3811 Validity_Checks_On
:= V
;
3815 Scop
:= Scope
(Spec_Id
);
3817 -- Add discriminal renamings to protected subprograms. Install new
3818 -- discriminals for expansion of the next subprogram of this protected
3821 if Is_List_Member
(N
)
3822 and then Present
(Parent
(List_Containing
(N
)))
3823 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
3825 Add_Discriminal_Declarations
3826 (Declarations
(N
), Scop
, Name_uObject
, Loc
);
3827 Add_Private_Declarations
(Declarations
(N
), Scop
, Name_uObject
, Loc
);
3829 -- Associate privals and discriminals with the next protected
3830 -- operation body to be expanded. These are used to expand references
3831 -- to private data objects and discriminants, respectively.
3833 Next_Op
:= Next_Protected_Operation
(N
);
3835 if Present
(Next_Op
) then
3836 Dec
:= Parent
(Base_Type
(Scop
));
3837 Set_Privals
(Dec
, Next_Op
, Loc
);
3838 Set_Discriminals
(Dec
);
3842 -- Clear out statement list for stubbed procedure
3844 if Present
(Corresponding_Spec
(N
)) then
3845 Set_Elaboration_Flag
(N
, Spec_Id
);
3847 if Convention
(Spec_Id
) = Convention_Stubbed
3848 or else Is_Eliminated
(Spec_Id
)
3850 Set_Declarations
(N
, Empty_List
);
3851 Set_Handled_Statement_Sequence
(N
,
3852 Make_Handled_Sequence_Of_Statements
(Loc
,
3853 Statements
=> New_List
(
3854 Make_Null_Statement
(Loc
))));
3859 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3860 -- but subprograms with no specs are not frozen.
3863 Typ
: constant Entity_Id
:= Etype
(Spec_Id
);
3864 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
3867 if not Acts_As_Spec
(N
)
3868 and then Nkind
(Parent
(Parent
(Spec_Id
))) /=
3869 N_Subprogram_Body_Stub
3873 elsif Is_Return_By_Reference_Type
(Typ
) then
3874 Set_Returns_By_Ref
(Spec_Id
);
3876 elsif Present
(Utyp
) and then Controlled_Type
(Utyp
) then
3877 Set_Returns_By_Ref
(Spec_Id
);
3881 -- For a procedure, we add a return for all possible syntactic ends
3882 -- of the subprogram. Note that reanalysis is not necessary in this
3883 -- case since it would require a lot of work and accomplish nothing.
3885 if Ekind
(Spec_Id
) = E_Procedure
3886 or else Ekind
(Spec_Id
) = E_Generic_Procedure
3888 Add_Return
(Statements
(H
));
3890 if Present
(Exception_Handlers
(H
)) then
3891 Except_H
:= First_Non_Pragma
(Exception_Handlers
(H
));
3892 while Present
(Except_H
) loop
3893 Add_Return
(Statements
(Except_H
));
3894 Next_Non_Pragma
(Except_H
);
3898 -- For a function, we must deal with the case where there is at least
3899 -- one missing return. What we do is to wrap the entire body of the
3900 -- function in a block:
3913 -- raise Program_Error;
3916 -- This approach is necessary because the raise must be signalled
3917 -- to the caller, not handled by any local handler (RM 6.4(11)).
3919 -- Note: we do not need to analyze the constructed sequence here,
3920 -- since it has no handler, and an attempt to analyze the handled
3921 -- statement sequence twice is risky in various ways (e.g. the
3922 -- issue of expanding cleanup actions twice).
3924 elsif Has_Missing_Return
(Spec_Id
) then
3926 Hloc
: constant Source_Ptr
:= Sloc
(H
);
3927 Blok
: constant Node_Id
:=
3928 Make_Block_Statement
(Hloc
,
3929 Handled_Statement_Sequence
=> H
);
3930 Rais
: constant Node_Id
:=
3931 Make_Raise_Program_Error
(Hloc
,
3932 Reason
=> PE_Missing_Return
);
3935 Set_Handled_Statement_Sequence
(N
,
3936 Make_Handled_Sequence_Of_Statements
(Hloc
,
3937 Statements
=> New_List
(Blok
, Rais
)));
3939 New_Scope
(Spec_Id
);
3946 -- If subprogram contains a parameterless recursive call, then we may
3947 -- have an infinite recursion, so see if we can generate code to check
3948 -- for this possibility if storage checks are not suppressed.
3950 if Ekind
(Spec_Id
) = E_Procedure
3951 and then Has_Recursive_Call
(Spec_Id
)
3952 and then not Storage_Checks_Suppressed
(Spec_Id
)
3954 Detect_Infinite_Recursion
(N
, Spec_Id
);
3957 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3958 -- parameters must be initialized to the appropriate default value.
3960 if Ekind
(Spec_Id
) = E_Procedure
and then Normalize_Scalars
then
3967 Formal
:= First_Formal
(Spec_Id
);
3968 while Present
(Formal
) loop
3969 Floc
:= Sloc
(Formal
);
3971 if Ekind
(Formal
) = E_Out_Parameter
3972 and then Is_Scalar_Type
(Etype
(Formal
))
3975 Make_Assignment_Statement
(Floc
,
3976 Name
=> New_Occurrence_Of
(Formal
, Floc
),
3978 Get_Simple_Init_Val
(Etype
(Formal
), Floc
));
3979 Prepend
(Stm
, Declarations
(N
));
3983 Next_Formal
(Formal
);
3988 -- Deal with thread body
3990 if Is_Thread_Body
(Spec_Id
) then
3994 -- Set to encode entity names in package body before gigi is called
3996 Qualify_Entity_Names
(N
);
3997 end Expand_N_Subprogram_Body
;
3999 -----------------------------------
4000 -- Expand_N_Subprogram_Body_Stub --
4001 -----------------------------------
4003 procedure Expand_N_Subprogram_Body_Stub
(N
: Node_Id
) is
4005 if Present
(Corresponding_Body
(N
)) then
4006 Expand_N_Subprogram_Body
(
4007 Unit_Declaration_Node
(Corresponding_Body
(N
)));
4009 end Expand_N_Subprogram_Body_Stub
;
4011 -------------------------------------
4012 -- Expand_N_Subprogram_Declaration --
4013 -------------------------------------
4015 -- If the declaration appears within a protected body, it is a private
4016 -- operation of the protected type. We must create the corresponding
4017 -- protected subprogram an associated formals. For a normal protected
4018 -- operation, this is done when expanding the protected type declaration.
4020 -- If the declaration is for a null procedure, emit null body
4022 procedure Expand_N_Subprogram_Declaration
(N
: Node_Id
) is
4023 Loc
: constant Source_Ptr
:= Sloc
(N
);
4024 Subp
: constant Entity_Id
:= Defining_Entity
(N
);
4025 Scop
: constant Entity_Id
:= Scope
(Subp
);
4026 Prot_Decl
: Node_Id
;
4028 Prot_Id
: Entity_Id
;
4031 -- Deal with case of protected subprogram. Do not generate protected
4032 -- operation if operation is flagged as eliminated.
4034 if Is_List_Member
(N
)
4035 and then Present
(Parent
(List_Containing
(N
)))
4036 and then Nkind
(Parent
(List_Containing
(N
))) = N_Protected_Body
4037 and then Is_Protected_Type
(Scop
)
4039 if No
(Protected_Body_Subprogram
(Subp
))
4040 and then not Is_Eliminated
(Subp
)
4043 Make_Subprogram_Declaration
(Loc
,
4045 Build_Protected_Sub_Specification
4046 (N
, Scop
, Unprotected_Mode
));
4048 -- The protected subprogram is declared outside of the protected
4049 -- body. Given that the body has frozen all entities so far, we
4050 -- analyze the subprogram and perform freezing actions explicitly.
4051 -- If the body is a subunit, the insertion point is before the
4052 -- stub in the parent.
4054 Prot_Bod
:= Parent
(List_Containing
(N
));
4056 if Nkind
(Parent
(Prot_Bod
)) = N_Subunit
then
4057 Prot_Bod
:= Corresponding_Stub
(Parent
(Prot_Bod
));
4060 Insert_Before
(Prot_Bod
, Prot_Decl
);
4061 Prot_Id
:= Defining_Unit_Name
(Specification
(Prot_Decl
));
4063 New_Scope
(Scope
(Scop
));
4064 Analyze
(Prot_Decl
);
4065 Create_Extra_Formals
(Prot_Id
);
4066 Set_Protected_Body_Subprogram
(Subp
, Prot_Id
);
4070 elsif Nkind
(Specification
(N
)) = N_Procedure_Specification
4071 and then Null_Present
(Specification
(N
))
4074 Bod
: constant Node_Id
:=
4075 Make_Subprogram_Body
(Loc
,
4077 New_Copy_Tree
(Specification
(N
)),
4078 Declarations
=> New_List
,
4079 Handled_Statement_Sequence
=>
4080 Make_Handled_Sequence_Of_Statements
(Loc
,
4081 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
4083 Set_Body_To_Inline
(N
, Bod
);
4084 Insert_After
(N
, Bod
);
4087 -- Corresponding_Spec isn't being set by Analyze_Subprogram_Body,
4088 -- evidently because Set_Has_Completion is called earlier for null
4089 -- procedures in Analyze_Subprogram_Declaration, so we force its
4090 -- setting here. If the setting of Has_Completion is not set
4091 -- earlier, then it can result in missing body errors if other
4092 -- errors were already reported (since expansion is turned off).
4094 -- Should creation of the empty body be moved to the analyzer???
4096 Set_Corresponding_Spec
(Bod
, Defining_Entity
(Specification
(N
)));
4099 end Expand_N_Subprogram_Declaration
;
4101 ---------------------------------------
4102 -- Expand_Protected_Object_Reference --
4103 ---------------------------------------
4105 function Expand_Protected_Object_Reference
4110 Loc
: constant Source_Ptr
:= Sloc
(N
);
4117 Rec
:= Make_Identifier
(Loc
, Name_uObject
);
4118 Set_Etype
(Rec
, Corresponding_Record_Type
(Scop
));
4120 -- Find enclosing protected operation, and retrieve its first parameter,
4121 -- which denotes the enclosing protected object. If the enclosing
4122 -- operation is an entry, we are immediately within the protected body,
4123 -- and we can retrieve the object from the service entries procedure. A
4124 -- barrier function has has the same signature as an entry. A barrier
4125 -- function is compiled within the protected object, but unlike
4126 -- protected operations its never needs locks, so that its protected
4127 -- body subprogram points to itself.
4129 Proc
:= Current_Scope
;
4130 while Present
(Proc
)
4131 and then Scope
(Proc
) /= Scop
4133 Proc
:= Scope
(Proc
);
4136 Corr
:= Protected_Body_Subprogram
(Proc
);
4140 -- Previous error left expansion incomplete.
4141 -- Nothing to do on this call.
4148 (First
(Parameter_Specifications
(Parent
(Corr
))));
4150 if Is_Subprogram
(Proc
)
4151 and then Proc
/= Corr
4153 -- Protected function or procedure
4155 Set_Entity
(Rec
, Param
);
4157 -- Rec is a reference to an entity which will not be in scope when
4158 -- the call is reanalyzed, and needs no further analysis.
4163 -- Entry or barrier function for entry body. The first parameter of
4164 -- the entry body procedure is pointer to the object. We create a
4165 -- local variable of the proper type, duplicating what is done to
4166 -- define _object later on.
4170 Obj_Ptr
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
,
4172 New_Internal_Name
('T'));
4176 Make_Full_Type_Declaration
(Loc
,
4177 Defining_Identifier
=> Obj_Ptr
,
4179 Make_Access_To_Object_Definition
(Loc
,
4180 Subtype_Indication
=>
4182 (Corresponding_Record_Type
(Scop
), Loc
))));
4184 Insert_Actions
(N
, Decls
);
4185 Insert_Actions
(N
, Freeze_Entity
(Obj_Ptr
, Sloc
(N
)));
4188 Make_Explicit_Dereference
(Loc
,
4189 Unchecked_Convert_To
(Obj_Ptr
,
4190 New_Occurrence_Of
(Param
, Loc
)));
4192 -- Analyze new actual. Other actuals in calls are already analyzed
4193 -- and the list of actuals is not renalyzed after rewriting.
4195 Set_Parent
(Rec
, N
);
4201 end Expand_Protected_Object_Reference
;
4203 --------------------------------------
4204 -- Expand_Protected_Subprogram_Call --
4205 --------------------------------------
4207 procedure Expand_Protected_Subprogram_Call
4215 -- If the protected object is not an enclosing scope, this is
4216 -- an inter-object function call. Inter-object procedure
4217 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4218 -- The call is intra-object only if the subprogram being
4219 -- called is in the protected body being compiled, and if the
4220 -- protected object in the call is statically the enclosing type.
4221 -- The object may be an component of some other data structure,
4222 -- in which case this must be handled as an inter-object call.
4224 if not In_Open_Scopes
(Scop
)
4225 or else not Is_Entity_Name
(Name
(N
))
4227 if Nkind
(Name
(N
)) = N_Selected_Component
then
4228 Rec
:= Prefix
(Name
(N
));
4231 pragma Assert
(Nkind
(Name
(N
)) = N_Indexed_Component
);
4232 Rec
:= Prefix
(Prefix
(Name
(N
)));
4235 Build_Protected_Subprogram_Call
(N
,
4236 Name
=> New_Occurrence_Of
(Subp
, Sloc
(N
)),
4237 Rec
=> Convert_Concurrent
(Rec
, Etype
(Rec
)),
4241 Rec
:= Expand_Protected_Object_Reference
(N
, Scop
);
4247 Build_Protected_Subprogram_Call
(N
,
4256 -- If it is a function call it can appear in elaboration code and
4257 -- the called entity must be frozen here.
4259 if Ekind
(Subp
) = E_Function
then
4260 Freeze_Expression
(Name
(N
));
4262 end Expand_Protected_Subprogram_Call
;
4264 -----------------------
4265 -- Freeze_Subprogram --
4266 -----------------------
4268 procedure Freeze_Subprogram
(N
: Node_Id
) is
4269 Loc
: constant Source_Ptr
:= Sloc
(N
);
4270 E
: constant Entity_Id
:= Entity
(N
);
4272 procedure Check_Overriding_Inherited_Interfaces
(E
: Entity_Id
);
4273 -- (Ada 2005): Check if the primitive E covers some interface already
4274 -- implemented by some ancestor of the tagged-type associated with E.
4276 procedure Register_Interface_DT_Entry
4278 Ancestor_Iface_Prim
: Entity_Id
:= Empty
);
4279 -- (Ada 2005): Register an interface primitive in a secondary dispatch
4280 -- table. If Prim overrides an ancestor primitive of its associated
4281 -- tagged-type then Ancestor_Iface_Prim indicates the entity of that
4282 -- immediate ancestor associated with the interface.
4284 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
);
4285 -- (Ada 2005): Register a predefined primitive in all the secondary
4286 -- dispatch tables of its primitive type.
4288 -------------------------------------------
4289 -- Check_Overriding_Inherited_Interfaces --
4290 -------------------------------------------
4292 procedure Check_Overriding_Inherited_Interfaces
(E
: Entity_Id
) is
4295 Prim_Op
: Entity_Id
;
4296 Overriden_Op
: Entity_Id
:= Empty
;
4299 if Ada_Version
< Ada_05
4300 or else not Is_Overriding_Operation
(E
)
4301 or else Is_Predefined_Dispatching_Operation
(E
)
4302 or else Present
(Alias
(E
))
4307 -- Get the entity associated with this primitive operation
4309 Typ
:= Scope
(DTC_Entity
(E
));
4311 exit when Etype
(Typ
) = Typ
4312 or else (Present
(Full_View
(Etype
(Typ
)))
4313 and then Full_View
(Etype
(Typ
)) = Typ
);
4315 -- Climb to the immediate ancestor handling private types
4317 if Present
(Full_View
(Etype
(Typ
))) then
4318 Typ
:= Full_View
(Etype
(Typ
));
4323 if Present
(Abstract_Interfaces
(Typ
)) then
4325 -- Look for the overriden subprogram in the primary dispatch
4326 -- table of the ancestor.
4328 Overriden_Op
:= Empty
;
4329 Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
4330 while Present
(Elmt
) loop
4331 Prim_Op
:= Node
(Elmt
);
4333 if Chars
(Prim_Op
) = Chars
(E
)
4334 and then Type_Conformant
4337 Skip_Controlling_Formals
=> True)
4338 and then DT_Position
(Prim_Op
) = DT_Position
(E
)
4339 and then Etype
(DTC_Entity
(Prim_Op
)) = RTE
(RE_Tag
)
4340 and then No
(Abstract_Interface_Alias
(Prim_Op
))
4342 if Overriden_Op
= Empty
then
4343 Overriden_Op
:= Prim_Op
;
4345 -- Additional check to ensure that if two candidates have
4346 -- been found then they refer to the same subprogram.
4355 while Present
(Alias
(A1
)) loop
4360 while Present
(Alias
(A2
)) loop
4365 raise Program_Error
;
4374 -- If not found this is the first overriding of some abstract
4377 if Overriden_Op
/= Empty
then
4379 -- Find the entries associated with interfaces that are
4380 -- alias of this primitive operation in the ancestor.
4382 Elmt
:= First_Elmt
(Primitive_Operations
(Typ
));
4383 while Present
(Elmt
) loop
4384 Prim_Op
:= Node
(Elmt
);
4386 if Present
(Abstract_Interface_Alias
(Prim_Op
))
4387 and then Alias
(Prim_Op
) = Overriden_Op
4389 Register_Interface_DT_Entry
(E
, Prim_Op
);
4397 end Check_Overriding_Inherited_Interfaces
;
4399 ---------------------------------
4400 -- Register_Interface_DT_Entry --
4401 ---------------------------------
4403 procedure Register_Interface_DT_Entry
4405 Ancestor_Iface_Prim
: Entity_Id
:= Empty
)
4407 Prim_Typ
: Entity_Id
;
4408 Prim_Op
: Entity_Id
;
4409 Iface_Typ
: Entity_Id
;
4410 Iface_DT_Ptr
: Entity_Id
;
4411 Iface_Tag
: Entity_Id
;
4412 New_Thunk
: Node_Id
;
4413 Thunk_Id
: Entity_Id
;
4416 -- Nothing to do if the run-time does not give support to abstract
4419 if not (RTE_Available
(RE_Interface_Tag
)) then
4423 if No
(Ancestor_Iface_Prim
) then
4424 Prim_Typ
:= Scope
(DTC_Entity
(Alias
(Prim
)));
4425 Iface_Typ
:= Scope
(DTC_Entity
(Abstract_Interface_Alias
(Prim
)));
4427 -- Generate the code of the thunk only when this primitive
4428 -- operation is associated with a secondary dispatch table.
4430 if Is_Interface
(Iface_Typ
) then
4431 Iface_Tag
:= Find_Interface_Tag
4433 Iface
=> Iface_Typ
);
4435 if Etype
(Iface_Tag
) = RTE
(RE_Interface_Tag
) then
4437 Make_Defining_Identifier
(Loc
,
4438 Chars
=> New_Internal_Name
('T'));
4441 Expand_Interface_Thunk
4443 Thunk_Alias
=> Alias
(Prim
),
4444 Thunk_Id
=> Thunk_Id
);
4446 Insert_After
(N
, New_Thunk
);
4451 Iface
=> Iface_Typ
);
4453 Insert_After
(New_Thunk
,
4454 Fill_Secondary_DT_Entry
(Sloc
(Prim
),
4456 Iface_DT_Ptr
=> Iface_DT_Ptr
,
4457 Thunk_Id
=> Thunk_Id
));
4463 Scope
(DTC_Entity
(Abstract_Interface_Alias
4464 (Ancestor_Iface_Prim
)));
4468 (T
=> Scope
(DTC_Entity
(Alias
(Ancestor_Iface_Prim
))),
4469 Iface
=> Iface_Typ
);
4471 -- Generate the thunk only if the associated tag is an interface
4472 -- tag. The case in which the associated tag is the primary tag
4473 -- occurs when a tagged type is a direct derivation of an
4474 -- interface. For example:
4476 -- type I is interface;
4478 -- type T is new I with ...
4480 if Etype
(Iface_Tag
) = RTE
(RE_Interface_Tag
) then
4482 Make_Defining_Identifier
(Loc
,
4483 Chars
=> New_Internal_Name
('T'));
4485 if Present
(Alias
(Prim
)) then
4486 Prim_Op
:= Alias
(Prim
);
4492 Expand_Interface_Thunk
4493 (N
=> Ancestor_Iface_Prim
,
4494 Thunk_Alias
=> Prim_Op
,
4495 Thunk_Id
=> Thunk_Id
);
4497 Insert_After
(N
, New_Thunk
);
4501 (T
=> Scope
(DTC_Entity
(Prim_Op
)),
4502 Iface
=> Iface_Typ
);
4504 Insert_After
(New_Thunk
,
4505 Fill_Secondary_DT_Entry
(Sloc
(Prim
),
4506 Prim
=> Ancestor_Iface_Prim
,
4507 Iface_DT_Ptr
=> Iface_DT_Ptr
,
4508 Thunk_Id
=> Thunk_Id
));
4511 end Register_Interface_DT_Entry
;
4513 ----------------------------------
4514 -- Register_Predefined_DT_Entry --
4515 ----------------------------------
4517 procedure Register_Predefined_DT_Entry
(Prim
: Entity_Id
) is
4518 Iface_DT_Ptr
: Elmt_Id
;
4519 Iface_Tag
: Entity_Id
;
4520 Iface_Typ
: Elmt_Id
;
4521 New_Thunk
: Entity_Id
;
4522 Prim_Typ
: Entity_Id
;
4523 Thunk_Id
: Entity_Id
;
4526 Prim_Typ
:= Scope
(DTC_Entity
(Prim
));
4528 if No
(Access_Disp_Table
(Prim_Typ
))
4529 or else No
(Abstract_Interfaces
(Prim_Typ
))
4530 or else not RTE_Available
(RE_Interface_Tag
)
4535 -- Skip the first acces-to-dispatch-table pointer since it leads
4536 -- to the primary dispatch table. We are only concerned with the
4537 -- secondary dispatch table pointers. Note that the access-to-
4538 -- dispatch-table pointer corresponds to the first implemented
4539 -- interface retrieved below.
4541 Iface_DT_Ptr
:= Next_Elmt
(First_Elmt
(Access_Disp_Table
(Prim_Typ
)));
4542 Iface_Typ
:= First_Elmt
(Abstract_Interfaces
(Prim_Typ
));
4543 while Present
(Iface_DT_Ptr
) and then Present
(Iface_Typ
) loop
4544 Iface_Tag
:= Find_Interface_Tag
(Prim_Typ
, Node
(Iface_Typ
));
4545 pragma Assert
(Present
(Iface_Tag
));
4547 if Etype
(Iface_Tag
) = RTE
(RE_Interface_Tag
) then
4548 Thunk_Id
:= Make_Defining_Identifier
(Loc
,
4549 New_Internal_Name
('T'));
4552 Expand_Interface_Thunk
4554 Thunk_Alias
=> Prim
,
4555 Thunk_Id
=> Thunk_Id
);
4557 Insert_After
(N
, New_Thunk
);
4558 Insert_After
(New_Thunk
,
4559 Make_DT_Access_Action
(Node
(Iface_Typ
),
4560 Action
=> Set_Predefined_Prim_Op_Address
,
4562 Unchecked_Convert_To
(RTE
(RE_Tag
),
4563 New_Reference_To
(Node
(Iface_DT_Ptr
), Loc
)),
4565 Make_Integer_Literal
(Loc
, DT_Position
(Prim
)),
4567 Make_Attribute_Reference
(Loc
,
4568 Prefix
=> New_Reference_To
(Thunk_Id
, Loc
),
4569 Attribute_Name
=> Name_Address
))));
4572 Next_Elmt
(Iface_DT_Ptr
);
4573 Next_Elmt
(Iface_Typ
);
4575 end Register_Predefined_DT_Entry
;
4577 -- Start of processing for Freeze_Subprogram
4580 -- When a primitive is frozen, enter its name in the corresponding
4581 -- dispatch table. If the DTC_Entity field is not set this is an
4582 -- overridden primitive that can be ignored. We suppress the
4583 -- initialization of the dispatch table entry when Java_VM because
4584 -- the dispatching mechanism is handled internally by the JVM.
4586 if Is_Dispatching_Operation
(E
)
4587 and then not Is_Abstract
(E
)
4588 and then Present
(DTC_Entity
(E
))
4589 and then not Java_VM
4590 and then not Is_CPP_Class
(Scope
(DTC_Entity
(E
)))
4592 Check_Overriding_Operation
(E
);
4594 -- Ada 95 case: Register the subprogram in the primary dispatch table
4596 if Ada_Version
< Ada_05
then
4598 -- Do not register the subprogram in the dispatch table if we
4599 -- are compiling with the No_Dispatching_Calls restriction.
4601 if not Restriction_Active
(No_Dispatching_Calls
) then
4603 Fill_DT_Entry
(Sloc
(N
), Prim
=> E
));
4606 -- Ada 2005 case: Register the subprogram in the secondary dispatch
4607 -- tables associated with abstract interfaces.
4611 Typ
: constant Entity_Id
:= Scope
(DTC_Entity
(E
));
4614 -- There is no dispatch table associated with abstract
4615 -- interface types. Each type implementing interfaces will
4616 -- fill the associated secondary DT entries.
4618 if not Is_Interface
(Typ
)
4619 or else Present
(Alias
(E
))
4621 -- Ada 2005 (AI-251): Check if this entry corresponds with
4622 -- a subprogram that covers an abstract interface type.
4624 if Present
(Abstract_Interface_Alias
(E
)) then
4625 Register_Interface_DT_Entry
(E
);
4627 -- Common case: Primitive subprogram
4630 -- Generate thunks for all the predefined operations
4632 if not Restriction_Active
(No_Dispatching_Calls
) then
4633 if Is_Predefined_Dispatching_Operation
(E
) then
4634 Register_Predefined_DT_Entry
(E
);
4638 Fill_DT_Entry
(Sloc
(N
), Prim
=> E
));
4641 Check_Overriding_Inherited_Interfaces
(E
);
4648 -- Mark functions that return by reference. Note that it cannot be
4649 -- part of the normal semantic analysis of the spec since the
4650 -- underlying returned type may not be known yet (for private types).
4653 Typ
: constant Entity_Id
:= Etype
(E
);
4654 Utyp
: constant Entity_Id
:= Underlying_Type
(Typ
);
4657 if Is_Return_By_Reference_Type
(Typ
) then
4658 Set_Returns_By_Ref
(E
);
4660 elsif Present
(Utyp
) and then Controlled_Type
(Utyp
) then
4661 Set_Returns_By_Ref
(E
);
4664 end Freeze_Subprogram
;