1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, 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 -- If type conversion, use reverse conversion on exit
703 if Nkind (Actual) = N_Type_Conversion then
704 if Conversion_OK (Actual) then
705 Expr := OK_Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
707 Expr := Convert_To (V_Typ, New_Occurrence_Of (Temp, Loc));
710 Expr := New_Occurrence_Of (Temp, Loc);
713 Rewrite (Actual, New_Reference_To (Temp, Loc));
716 Append_To (Post_Call,
717 Make_Assignment_Statement (Loc,
718 Name => New_Occurrence_Of (Var, Loc),
719 Expression => Expr));
721 Set_Assignment_OK (Name (Last (Post_Call)));
723 end Add_Call_By_Copy_Code;
725 ----------------------------------
726 -- Add_Simple_Call_By_Copy_Code --
727 ----------------------------------
729 procedure Add_Simple_Call_By_Copy_Code is
737 F_Typ : constant Entity_Id := Etype (Formal);
740 if not Is_Legal_Copy then
744 -- Use formal type for temp, unless formal type is an unconstrained
745 -- array, in which case we don't have to worry about bounds checks,
746 -- and we use the actual type, since that has appropriate bounds.
748 if Is_Array_Type (F_Typ) and then not Is_Constrained (F_Typ) then
749 Indic := New_Occurrence_Of (Etype (Actual), Loc);
751 Indic := New_Occurrence_Of (Etype (Formal), Loc);
754 -- Prepare to generate code
758 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
759 Incod := Relocate_Node (Actual);
760 Outcod := New_Copy_Tree (Incod);
762 -- Generate declaration of temporary variable, initializing it
763 -- with the input parameter unless we have an OUT formal or
764 -- this is an initialization call.
766 -- If the formal is an out parameter with discriminants, the
767 -- discriminants must be captured even if the rest of the object
768 -- is in principle uninitialized, because the discriminants may
769 -- be read by the called subprogram.
771 if Ekind (Formal) = E_Out_Parameter then
774 if Has_Discriminants (Etype (Formal)) then
775 Indic := New_Occurrence_Of (Etype (Actual), Loc);
778 elsif Inside_Init_Proc then
780 -- Could use a comment here to match comment below ???
782 if Nkind (Actual) /= N_Selected_Component
784 not Has_Discriminant_Dependent_Constraint
785 (Entity (Selector_Name (Actual)))
789 -- Otherwise, keep the component in order to generate the proper
790 -- actual subtype, that depends on enclosing discriminants.
798 Make_Object_Declaration (Loc,
799 Defining_Identifier => Temp,
800 Object_Definition => Indic,
801 Expression => Incod);
806 -- If the call is to initialize a component of a composite type,
807 -- and the component does not depend on discriminants, use the
808 -- actual type of the component. This is required in case the
809 -- component is constrained, because in general the formal of the
810 -- initialization procedure will be unconstrained. Note that if
811 -- the component being initialized is constrained by an enclosing
812 -- discriminant, the presence of the initialization in the
813 -- declaration will generate an expression for the actual subtype.
815 Set_No_Initialization (Decl);
816 Set_Object_Definition (Decl,
817 New_Occurrence_Of (Etype (Actual), Loc));
820 Insert_Action (N, Decl);
822 -- The actual is simply a reference to the temporary
824 Rewrite (Actual, New_Occurrence_Of (Temp, Loc));
826 -- Generate copy out if OUT or IN OUT parameter
828 if Ekind (Formal) /= E_In_Parameter then
830 Rhs := New_Occurrence_Of (Temp, Loc);
832 -- Deal with conversion
834 if Nkind (Lhs) = N_Type_Conversion then
835 Lhs := Expression (Lhs);
836 Rhs := Convert_To (Etype (Actual), Rhs);
839 Append_To (Post_Call,
840 Make_Assignment_Statement (Loc,
843 Set_Assignment_OK (Name (Last (Post_Call)));
845 end Add_Simple_Call_By_Copy_Code;
847 ---------------------------
848 -- Check_Fortran_Logical --
849 ---------------------------
851 procedure Check_Fortran_Logical is
852 Logical : constant Entity_Id := Etype (Formal);
855 -- Note: this is very incomplete, e.g. it does not handle arrays
856 -- of logical values. This is really not the right approach at all???)
859 if Convention (Subp) = Convention_Fortran
860 and then Root_Type (Etype (Formal)) = Standard_Boolean
861 and then Ekind (Formal) /= E_In_Parameter
863 Var := Make_Var (Actual);
864 Append_To (Post_Call,
865 Make_Assignment_Statement (Loc,
866 Name => New_Occurrence_Of (Var, Loc),
868 Unchecked_Convert_To (
871 Left_Opnd => New_Occurrence_Of (Var, Loc),
873 Unchecked_Convert_To (
875 New_Occurrence_Of (Standard_False, Loc))))));
877 end Check_Fortran_Logical;
883 function Is_Legal_Copy return Boolean is
885 -- An attempt to copy a value of such a type can only occur if
886 -- representation clauses give the actual a misaligned address.
888 if Is_By_Reference_Type (Etype (Formal)) then
890 ("misaligned actual cannot be passed by reference", Actual);
893 -- For users of Starlet, we assume that the specification of by-
894 -- reference mechanism is mandatory. This may lead to unligned
895 -- objects but at least for DEC legacy code it is known to work.
896 -- The warning will alert users of this code that a problem may
899 elsif Mechanism (Formal) = By_Reference
900 and then Is_Valued_Procedure (Scope (Formal))
903 ("by_reference actual may be misaligned?", Actual);
915 function Make_Var (Actual : Node_Id) return Entity_Id is
919 if Is_Entity_Name (Actual) then
920 return Entity (Actual);
923 Var := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
926 Make_Object_Renaming_Declaration (Loc,
927 Defining_Identifier => Var,
929 New_Occurrence_Of (Etype (Actual), Loc),
930 Name => Relocate_Node (Actual));
932 Insert_Action (N, N_Node);
937 -------------------------
938 -- Reset_Packed_Prefix --
939 -------------------------
941 procedure Reset_Packed_Prefix is
942 Pfx : Node_Id := Actual;
945 Set_Analyzed (Pfx, False);
946 exit when Nkind (Pfx) /= N_Selected_Component
947 and then Nkind (Pfx) /= N_Indexed_Component;
950 end Reset_Packed_Prefix;
952 -- Start of processing for Expand_Actuals
955 Post_Call := New_List;
957 Formal := First_Formal (Subp);
958 Actual := First_Actual (N);
959 while Present (Formal) loop
960 E_Formal := Etype (Formal);
962 if Is_Scalar_Type (E_Formal)
963 or else Nkind (Actual) = N_Slice
965 Check_Fortran_Logical;
969 elsif Ekind (Formal) /= E_Out_Parameter then
971 -- The unusual case of the current instance of a protected type
972 -- requires special handling. This can only occur in the context
973 -- of a call within the body of a protected operation.
975 if Is_Entity_Name (Actual)
976 and then Ekind (Entity (Actual)) = E_Protected_Type
977 and then In_Open_Scopes (Entity (Actual))
979 if Scope (Subp) /= Entity (Actual) then
980 Error_Msg_N ("operation outside protected type may not "
981 & "call back its protected operations?", Actual);
985 Expand_Protected_Object_Reference (N, Entity (Actual)));
988 Apply_Constraint_Check (Actual, E_Formal);
990 -- Out parameter case. No constraint checks on access type
993 elsif Is_Access_Type (E_Formal) then
998 elsif Has_Discriminants (Base_Type (E_Formal))
999 or else Has_Non_Null_Base_Init_Proc (E_Formal)
1001 Apply_Constraint_Check (Actual, E_Formal);
1006 Apply_Constraint_Check (Actual, Base_Type (E_Formal));
1009 -- Processing for IN-OUT and OUT parameters
1011 if Ekind (Formal) /= E_In_Parameter then
1013 -- For type conversions of arrays, apply length/range checks
1015 if Is_Array_Type (E_Formal)
1016 and then Nkind (Actual) = N_Type_Conversion
1018 if Is_Constrained (E_Formal) then
1019 Apply_Length_Check (Expression (Actual), E_Formal);
1021 Apply_Range_Check (Expression (Actual), E_Formal);
1025 -- If argument is a type conversion for a type that is passed
1026 -- by copy, then we must pass the parameter by copy.
1028 if Nkind (Actual) = N_Type_Conversion
1030 (Is_Numeric_Type (E_Formal)
1031 or else Is_Access_Type (E_Formal)
1032 or else Is_Enumeration_Type (E_Formal)
1033 or else Is_Bit_Packed_Array (Etype (Formal))
1034 or else Is_Bit_Packed_Array (Etype (Expression (Actual)))
1036 -- Also pass by copy if change of representation
1038 or else not Same_Representation
1040 Etype (Expression (Actual))))
1042 Add_Call_By_Copy_Code;
1044 -- References to components of bit packed arrays are expanded
1045 -- at this point, rather than at the point of analysis of the
1046 -- actuals, to handle the expansion of the assignment to
1047 -- [in] out parameters.
1049 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1050 Add_Simple_Call_By_Copy_Code;
1052 -- If a non-scalar actual is possibly unaligned, we need a copy
1054 elsif Is_Possibly_Unaligned_Object (Actual)
1055 and then not Represented_As_Scalar (Etype (Formal))
1057 Add_Simple_Call_By_Copy_Code;
1059 -- References to slices of bit packed arrays are expanded
1061 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1062 Add_Call_By_Copy_Code;
1064 -- References to possibly unaligned slices of arrays are expanded
1066 elsif Is_Possibly_Unaligned_Slice (Actual) then
1067 Add_Call_By_Copy_Code;
1069 -- Deal with access types where the actual subtpe and the
1070 -- formal subtype are not the same, requiring a check.
1072 -- It is necessary to exclude tagged types because of "downward
1073 -- conversion" errors and a strange assertion error in namet
1074 -- from gnatf in bug 1215-001 ???
1076 elsif Is_Access_Type (E_Formal)
1077 and then not Same_Type (E_Formal, Etype (Actual))
1078 and then not Is_Tagged_Type (Designated_Type (E_Formal))
1080 Add_Call_By_Copy_Code;
1082 -- If the actual is not a scalar and is marked for volatile
1083 -- treatment, whereas the formal is not volatile, then pass
1084 -- by copy unless it is a by-reference type.
1086 elsif Is_Entity_Name (Actual)
1087 and then Treat_As_Volatile (Entity (Actual))
1088 and then not Is_By_Reference_Type (Etype (Actual))
1089 and then not Is_Scalar_Type (Etype (Entity (Actual)))
1090 and then not Treat_As_Volatile (E_Formal)
1092 Add_Call_By_Copy_Code;
1094 elsif Nkind (Actual) = N_Indexed_Component
1095 and then Is_Entity_Name (Prefix (Actual))
1096 and then Has_Volatile_Components (Entity (Prefix (Actual)))
1098 Add_Call_By_Copy_Code;
1101 -- Processing for IN parameters
1104 -- For IN parameters is in the packed array case, we expand an
1105 -- indexed component (the circuit in Exp_Ch4 deliberately left
1106 -- indexed components appearing as actuals untouched, so that
1107 -- the special processing above for the OUT and IN OUT cases
1108 -- could be performed. We could make the test in Exp_Ch4 more
1109 -- complex and have it detect the parameter mode, but it is
1110 -- easier simply to handle all cases here.)
1112 if Nkind (Actual) = N_Indexed_Component
1113 and then Is_Packed (Etype (Prefix (Actual)))
1115 Reset_Packed_Prefix;
1116 Expand_Packed_Element_Reference (Actual);
1118 -- If we have a reference to a bit packed array, we copy it,
1119 -- since the actual must be byte aligned.
1121 -- Is this really necessary in all cases???
1123 elsif Is_Ref_To_Bit_Packed_Array (Actual) then
1124 Add_Simple_Call_By_Copy_Code;
1126 -- If a non-scalar actual is possibly unaligned, we need a copy
1128 elsif Is_Possibly_Unaligned_Object (Actual)
1129 and then not Represented_As_Scalar (Etype (Formal))
1131 Add_Simple_Call_By_Copy_Code;
1133 -- Similarly, we have to expand slices of packed arrays here
1134 -- because the result must be byte aligned.
1136 elsif Is_Ref_To_Bit_Packed_Slice (Actual) then
1137 Add_Call_By_Copy_Code;
1139 -- Only processing remaining is to pass by copy if this is a
1140 -- reference to a possibly unaligned slice, since the caller
1141 -- expects an appropriately aligned argument.
1143 elsif Is_Possibly_Unaligned_Slice (Actual) then
1144 Add_Call_By_Copy_Code;
1148 Next_Formal (Formal);
1149 Next_Actual (Actual);
1152 -- Find right place to put post call stuff if it is present
1154 if not Is_Empty_List (Post_Call) then
1156 -- If call is not a list member, it must be the triggering statement
1157 -- of a triggering alternative or an entry call alternative, and we
1158 -- can add the post call stuff to the corresponding statement list.
1160 if not Is_List_Member (N) then
1162 P : constant Node_Id := Parent (N);
1165 pragma Assert (Nkind (P) = N_Triggering_Alternative
1166 or else Nkind (P) = N_Entry_Call_Alternative);
1168 if Is_Non_Empty_List (Statements (P)) then
1169 Insert_List_Before_And_Analyze
1170 (First (Statements (P)), Post_Call);
1172 Set_Statements (P, Post_Call);
1176 -- Otherwise, normal case where N is in a statement sequence,
1177 -- just put the post-call stuff after the call statement.
1180 Insert_Actions_After (N, Post_Call);
1184 -- The call node itself is re-analyzed in Expand_Call
1192 -- This procedure handles expansion of function calls and procedure call
1193 -- statements (i.e. it serves as the body for Expand_N_Function_Call and
1194 -- Expand_N_Procedure_Call_Statement. Processing for calls includes:
1196 -- Replace call to Raise_Exception by Raise_Exception always if possible
1197 -- Provide values of actuals for all formals in Extra_Formals list
1198 -- Replace "call" to enumeration literal function by literal itself
1199 -- Rewrite call to predefined operator as operator
1200 -- Replace actuals to in-out parameters that are numeric conversions,
1201 -- with explicit assignment to temporaries before and after the call.
1202 -- Remove optional actuals if First_Optional_Parameter specified.
1204 -- Note that the list of actuals has been filled with default expressions
1205 -- during semantic analysis of the call. Only the extra actuals required
1206 -- for the 'Constrained attribute and for accessibility checks are added
1209 procedure Expand_Call (N : Node_Id) is
1210 Loc : constant Source_Ptr := Sloc (N);
1211 Remote : constant Boolean := Is_Remote_Call (N);
1213 Orig_Subp : Entity_Id := Empty;
1214 Parent_Subp : Entity_Id;
1215 Parent_Formal : Entity_Id;
1218 Prev : Node_Id := Empty;
1220 Prev_Orig : Node_Id;
1221 -- Original node for an actual, which may have been rewritten. If the
1222 -- actual is a function call that has been transformed from a selected
1223 -- component, the original node is unanalyzed. Otherwise, it carries
1224 -- semantic information used to generate additional actuals.
1227 Extra_Actuals : List_Id := No_List;
1229 CW_Interface_Formals_Present : Boolean := False;
1231 procedure Add_Actual_Parameter (Insert_Param : Node_Id);
1232 -- Adds one entry to the end of the actual parameter list. Used for
1233 -- default parameters and for extra actuals (for Extra_Formals). The
1234 -- argument is an N_Parameter_Association node.
1236 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id);
1237 -- Adds an extra actual to the list of extra actuals. Expr is the
1238 -- expression for the value of the actual, EF is the entity for the
1241 function Inherited_From_Formal (S : Entity_Id) return Entity_Id;
1242 -- Within an instance, a type derived from a non-tagged formal derived
1243 -- type inherits from the original parent, not from the actual. This is
1244 -- tested in 4723-003. The current derivation mechanism has the derived
1245 -- type inherit from the actual, which is only correct outside of the
1246 -- instance. If the subprogram is inherited, we test for this particular
1247 -- case through a convoluted tree traversal before setting the proper
1248 -- subprogram to be called.
1250 --------------------------
1251 -- Add_Actual_Parameter --
1252 --------------------------
1254 procedure Add_Actual_Parameter (Insert_Param : Node_Id) is
1255 Actual_Expr : constant Node_Id :=
1256 Explicit_Actual_Parameter (Insert_Param);
1259 -- Case of insertion is first named actual
1261 if No (Prev) or else
1262 Nkind (Parent (Prev)) /= N_Parameter_Association
1264 Set_Next_Named_Actual (Insert_Param, First_Named_Actual (N));
1265 Set_First_Named_Actual (N, Actual_Expr);
1268 if not Present (Parameter_Associations (N)) then
1269 Set_Parameter_Associations (N, New_List);
1270 Append (Insert_Param, Parameter_Associations (N));
1273 Insert_After (Prev, Insert_Param);
1276 -- Case of insertion is not first named actual
1279 Set_Next_Named_Actual
1280 (Insert_Param, Next_Named_Actual (Parent (Prev)));
1281 Set_Next_Named_Actual (Parent (Prev), Actual_Expr);
1282 Append (Insert_Param, Parameter_Associations (N));
1285 Prev := Actual_Expr;
1286 end Add_Actual_Parameter;
1288 ----------------------
1289 -- Add_Extra_Actual --
1290 ----------------------
1292 procedure Add_Extra_Actual (Expr : Node_Id; EF : Entity_Id) is
1293 Loc : constant Source_Ptr := Sloc (Expr);
1296 if Extra_Actuals = No_List then
1297 Extra_Actuals := New_List;
1298 Set_Parent (Extra_Actuals, N);
1301 Append_To (Extra_Actuals,
1302 Make_Parameter_Association (Loc,
1303 Explicit_Actual_Parameter => Expr,
1305 Make_Identifier (Loc, Chars (EF))));
1307 Analyze_And_Resolve (Expr, Etype (EF));
1308 end Add_Extra_Actual;
1310 ---------------------------
1311 -- Inherited_From_Formal --
1312 ---------------------------
1314 function Inherited_From_Formal (S : Entity_Id) return Entity_Id is
1316 Gen_Par : Entity_Id;
1317 Gen_Prim : Elist_Id;
1322 -- If the operation is inherited, it is attached to the corresponding
1323 -- type derivation. If the parent in the derivation is a generic
1324 -- actual, it is a subtype of the actual, and we have to recover the
1325 -- original derived type declaration to find the proper parent.
1327 if Nkind (Parent (S)) /= N_Full_Type_Declaration
1328 or else not Is_Derived_Type (Defining_Identifier (Parent (S)))
1329 or else Nkind (Type_Definition (Original_Node (Parent (S)))) /=
1330 N_Derived_Type_Definition
1331 or else not In_Instance
1338 (Type_Definition (Original_Node (Parent (S)))));
1340 if Nkind (Indic) = N_Subtype_Indication then
1341 Par := Entity (Subtype_Mark (Indic));
1343 Par := Entity (Indic);
1347 if not Is_Generic_Actual_Type (Par)
1348 or else Is_Tagged_Type (Par)
1349 or else Nkind (Parent (Par)) /= N_Subtype_Declaration
1350 or else not In_Open_Scopes (Scope (Par))
1355 Gen_Par := Generic_Parent_Type (Parent (Par));
1358 -- If the generic parent type is still the generic type, this is a
1359 -- private formal, not a derived formal, and there are no operations
1360 -- inherited from the formal.
1362 if Nkind (Parent (Gen_Par)) = N_Formal_Type_Declaration then
1366 Gen_Prim := Collect_Primitive_Operations (Gen_Par);
1368 Elmt := First_Elmt (Gen_Prim);
1369 while Present (Elmt) loop
1370 if Chars (Node (Elmt)) = Chars (S) then
1376 F1 := First_Formal (S);
1377 F2 := First_Formal (Node (Elmt));
1379 and then Present (F2)
1381 if Etype (F1) = Etype (F2)
1382 or else Etype (F2) = Gen_Par
1388 exit; -- not the right subprogram
1400 raise Program_Error;
1401 end Inherited_From_Formal;
1403 -- Start of processing for Expand_Call
1406 -- Ignore if previous error
1408 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1412 -- Call using access to subprogram with explicit dereference
1414 if Nkind (Name (N)) = N_Explicit_Dereference then
1415 Subp := Etype (Name (N));
1416 Parent_Subp := Empty;
1418 -- Case of call to simple entry, where the Name is a selected component
1419 -- whose prefix is the task, and whose selector name is the entry name
1421 elsif Nkind (Name (N)) = N_Selected_Component then
1422 Subp := Entity (Selector_Name (Name (N)));
1423 Parent_Subp := Empty;
1425 -- Case of call to member of entry family, where Name is an indexed
1426 -- component, with the prefix being a selected component giving the
1427 -- task and entry family name, and the index being the entry index.
1429 elsif Nkind (Name (N)) = N_Indexed_Component then
1430 Subp := Entity (Selector_Name (Prefix (Name (N))));
1431 Parent_Subp := Empty;
1436 Subp := Entity (Name (N));
1437 Parent_Subp := Alias (Subp);
1439 -- Replace call to Raise_Exception by call to Raise_Exception_Always
1440 -- if we can tell that the first parameter cannot possibly be null.
1441 -- This helps optimization and also generation of warnings.
1443 if not Restriction_Active (No_Exception_Handlers)
1444 and then Is_RTE (Subp, RE_Raise_Exception)
1447 FA : constant Node_Id := Original_Node (First_Actual (N));
1450 -- The case we catch is where the first argument is obtained
1451 -- using the Identity attribute (which must always be
1454 if Nkind (FA) = N_Attribute_Reference
1455 and then Attribute_Name (FA) = Name_Identity
1457 Subp := RTE (RE_Raise_Exception_Always);
1458 Set_Entity (Name (N), Subp);
1463 if Ekind (Subp) = E_Entry then
1464 Parent_Subp := Empty;
1468 -- Ada 2005 (AI-345): We have a procedure call as a triggering
1469 -- alternative in an asynchronous select or as an entry call in
1470 -- a conditional or timed select. Check whether the procedure call
1471 -- is a renaming of an entry and rewrite it as an entry call.
1473 if Ada_Version >= Ada_05
1474 and then Nkind (N) = N_Procedure_Call_Statement
1476 ((Nkind (Parent (N)) = N_Triggering_Alternative
1477 and then Triggering_Statement (Parent (N)) = N)
1479 (Nkind (Parent (N)) = N_Entry_Call_Alternative
1480 and then Entry_Call_Statement (Parent (N)) = N))
1484 Ren_Root : Entity_Id := Subp;
1487 -- This may be a chain of renamings, find the root
1489 if Present (Alias (Ren_Root)) then
1490 Ren_Root := Alias (Ren_Root);
1493 if Present (Original_Node (Parent (Parent (Ren_Root)))) then
1494 Ren_Decl := Original_Node (Parent (Parent (Ren_Root)));
1496 if Nkind (Ren_Decl) = N_Subprogram_Renaming_Declaration then
1498 Make_Entry_Call_Statement (Loc,
1500 New_Copy_Tree (Name (Ren_Decl)),
1501 Parameter_Associations =>
1502 New_Copy_List_Tree (Parameter_Associations (N))));
1510 -- First step, compute extra actuals, corresponding to any
1511 -- Extra_Formals present. Note that we do not access Extra_Formals
1512 -- directly, instead we simply note the presence of the extra
1513 -- formals as we process the regular formals and collect the
1514 -- corresponding actuals in Extra_Actuals.
1516 -- We also generate any required range checks for actuals as we go
1517 -- through the loop, since this is a convenient place to do this.
1519 Formal := First_Formal (Subp);
1520 Actual := First_Actual (N);
1521 while Present (Formal) loop
1523 -- Generate range check if required (not activated yet ???)
1525 -- if Do_Range_Check (Actual) then
1526 -- Set_Do_Range_Check (Actual, False);
1527 -- Generate_Range_Check
1528 -- (Actual, Etype (Formal), CE_Range_Check_Failed);
1531 -- Prepare to examine current entry
1534 Prev_Orig := Original_Node (Prev);
1536 if not Analyzed (Prev_Orig)
1537 and then Nkind (Actual) = N_Function_Call
1542 -- Ada 2005 (AI-251): Check if any formal is a class-wide interface
1543 -- to expand it in a further round.
1545 CW_Interface_Formals_Present :=
1546 CW_Interface_Formals_Present
1548 (Ekind (Etype (Formal)) = E_Class_Wide_Type
1549 and then Is_Interface (Etype (Etype (Formal))))
1551 (Ekind (Etype (Formal)) = E_Anonymous_Access_Type
1552 and then Is_Interface (Directly_Designated_Type
1553 (Etype (Etype (Formal)))));
1555 -- Create possible extra actual for constrained case. Usually, the
1556 -- extra actual is of the form actual'constrained, but since this
1557 -- attribute is only available for unconstrained records, TRUE is
1558 -- expanded if the type of the formal happens to be constrained (for
1559 -- instance when this procedure is inherited from an unconstrained
1560 -- record to a constrained one) or if the actual has no discriminant
1561 -- (its type is constrained). An exception to this is the case of a
1562 -- private type without discriminants. In this case we pass FALSE
1563 -- because the object has underlying discriminants with defaults.
1565 if Present (Extra_Constrained (Formal)) then
1566 if Ekind (Etype (Prev)) in Private_Kind
1567 and then not Has_Discriminants (Base_Type (Etype (Prev)))
1570 New_Occurrence_Of (Standard_False, Loc),
1571 Extra_Constrained (Formal));
1573 elsif Is_Constrained (Etype (Formal))
1574 or else not Has_Discriminants (Etype (Prev))
1577 New_Occurrence_Of (Standard_True, Loc),
1578 Extra_Constrained (Formal));
1580 -- Do not produce extra actuals for Unchecked_Union parameters.
1581 -- Jump directly to the end of the loop.
1583 elsif Is_Unchecked_Union (Base_Type (Etype (Actual))) then
1584 goto Skip_Extra_Actual_Generation;
1587 -- If the actual is a type conversion, then the constrained
1588 -- test applies to the actual, not the target type.
1594 -- Test for unchecked conversions as well, which can occur
1595 -- as out parameter actuals on calls to stream procedures.
1598 while Nkind (Act_Prev) = N_Type_Conversion
1599 or else Nkind (Act_Prev) = N_Unchecked_Type_Conversion
1601 Act_Prev := Expression (Act_Prev);
1604 -- If the expression is a conversion of a dereference,
1605 -- this is internally generated code that manipulates
1606 -- addresses, e.g. when building interface tables. No
1607 -- check should occur in this case, and the discriminated
1608 -- object is not directly a hand.
1610 if not Comes_From_Source (Actual)
1611 and then Nkind (Actual) = N_Unchecked_Type_Conversion
1612 and then Nkind (Act_Prev) = N_Explicit_Dereference
1615 (New_Occurrence_Of (Standard_False, Loc),
1616 Extra_Constrained (Formal));
1620 (Make_Attribute_Reference (Sloc (Prev),
1622 Duplicate_Subexpr_No_Checks
1623 (Act_Prev, Name_Req => True),
1624 Attribute_Name => Name_Constrained),
1625 Extra_Constrained (Formal));
1631 -- Create possible extra actual for accessibility level
1633 if Present (Extra_Accessibility (Formal)) then
1634 if Is_Entity_Name (Prev_Orig) then
1636 -- When passing an access parameter as the actual to another
1637 -- access parameter we need to pass along the actual's own
1638 -- associated access level parameter. This is done if we are
1639 -- in the scope of the formal access parameter (if this is an
1640 -- inlined body the extra formal is irrelevant).
1642 if Ekind (Entity (Prev_Orig)) in Formal_Kind
1643 and then Ekind (Etype (Prev_Orig)) = E_Anonymous_Access_Type
1644 and then In_Open_Scopes (Scope (Entity (Prev_Orig)))
1647 Parm_Ent : constant Entity_Id := Param_Entity (Prev_Orig);
1650 pragma Assert (Present (Parm_Ent));
1652 if Present (Extra_Accessibility (Parm_Ent)) then
1655 (Extra_Accessibility (Parm_Ent), Loc),
1656 Extra_Accessibility (Formal));
1658 -- If the actual access parameter does not have an
1659 -- associated extra formal providing its scope level,
1660 -- then treat the actual as having library-level
1665 (Make_Integer_Literal (Loc,
1666 Intval => Scope_Depth (Standard_Standard)),
1667 Extra_Accessibility (Formal));
1671 -- The actual is a normal access value, so just pass the
1672 -- level of the actual's access type.
1676 (Make_Integer_Literal (Loc,
1677 Intval => Type_Access_Level (Etype (Prev_Orig))),
1678 Extra_Accessibility (Formal));
1682 case Nkind (Prev_Orig) is
1684 when N_Attribute_Reference =>
1686 case Get_Attribute_Id (Attribute_Name (Prev_Orig)) is
1688 -- For X'Access, pass on the level of the prefix X
1690 when Attribute_Access =>
1692 Make_Integer_Literal (Loc,
1694 Object_Access_Level (Prefix (Prev_Orig))),
1695 Extra_Accessibility (Formal));
1697 -- Treat the unchecked attributes as library-level
1699 when Attribute_Unchecked_Access |
1700 Attribute_Unrestricted_Access =>
1702 Make_Integer_Literal (Loc,
1703 Intval => Scope_Depth (Standard_Standard)),
1704 Extra_Accessibility (Formal));
1706 -- No other cases of attributes returning access
1707 -- values that can be passed to access parameters
1710 raise Program_Error;
1714 -- For allocators we pass the level of the execution of
1715 -- the called subprogram, which is one greater than the
1716 -- current scope level.
1720 Make_Integer_Literal (Loc,
1721 Scope_Depth (Current_Scope) + 1),
1722 Extra_Accessibility (Formal));
1724 -- For other cases we simply pass the level of the
1725 -- actual's access type.
1729 Make_Integer_Literal (Loc,
1730 Intval => Type_Access_Level (Etype (Prev_Orig))),
1731 Extra_Accessibility (Formal));
1737 -- Perform the check of 4.6(49) that prevents a null value from being
1738 -- passed as an actual to an access parameter. Note that the check is
1739 -- elided in the common cases of passing an access attribute or
1740 -- access parameter as an actual. Also, we currently don't enforce
1741 -- this check for expander-generated actuals and when -gnatdj is set.
1743 if Ada_Version >= Ada_05 then
1745 -- Ada 2005 (AI-231): Check null-excluding access types
1747 if Is_Access_Type (Etype (Formal))
1748 and then Can_Never_Be_Null (Etype (Formal))
1749 and then Nkind (Prev) /= N_Raise_Constraint_Error
1750 and then (Nkind (Prev) = N_Null
1751 or else not Can_Never_Be_Null (Etype (Prev)))
1753 Install_Null_Excluding_Check (Prev);
1756 -- Ada_Version < Ada_05
1759 if Ekind (Etype (Formal)) /= E_Anonymous_Access_Type
1760 or else Access_Checks_Suppressed (Subp)
1764 elsif Debug_Flag_J then
1767 elsif not Comes_From_Source (Prev) then
1770 elsif Is_Entity_Name (Prev)
1771 and then Ekind (Etype (Prev)) = E_Anonymous_Access_Type
1775 elsif Nkind (Prev) = N_Allocator
1776 or else Nkind (Prev) = N_Attribute_Reference
1780 -- Suppress null checks when passing to access parameters of Java
1781 -- subprograms. (Should this be done for other foreign conventions
1784 elsif Convention (Subp) = Convention_Java then
1788 Install_Null_Excluding_Check (Prev);
1792 -- Perform appropriate validity checks on parameters that
1795 if Validity_Checks_On then
1796 if (Ekind (Formal) = E_In_Parameter
1797 and then Validity_Check_In_Params)
1799 (Ekind (Formal) = E_In_Out_Parameter
1800 and then Validity_Check_In_Out_Params)
1802 -- If the actual is an indexed component of a packed
1803 -- type, it has not been expanded yet. It will be
1804 -- copied in the validity code that follows, and has
1805 -- to be expanded appropriately, so reanalyze it.
1807 if Nkind (Actual) = N_Indexed_Component then
1808 Set_Analyzed (Actual, False);
1811 Ensure_Valid (Actual);
1815 -- For IN OUT and OUT parameters, ensure that subscripts are valid
1816 -- since this is a left side reference. We only do this for calls
1817 -- from the source program since we assume that compiler generated
1818 -- calls explicitly generate any required checks. We also need it
1819 -- only if we are doing standard validity checks, since clearly it
1820 -- is not needed if validity checks are off, and in subscript
1821 -- validity checking mode, all indexed components are checked with
1822 -- a call directly from Expand_N_Indexed_Component.
1824 if Comes_From_Source (N)
1825 and then Ekind (Formal) /= E_In_Parameter
1826 and then Validity_Checks_On
1827 and then Validity_Check_Default
1828 and then not Validity_Check_Subscripts
1830 Check_Valid_Lvalue_Subscripts (Actual);
1833 -- Mark any scalar OUT parameter that is a simple variable
1834 -- as no longer known to be valid (unless the type is always
1835 -- valid). This reflects the fact that if an OUT parameter
1836 -- is never set in a procedure, then it can become invalid
1837 -- on return from the procedure.
1839 if Ekind (Formal) = E_Out_Parameter
1840 and then Is_Entity_Name (Actual)
1841 and then Ekind (Entity (Actual)) = E_Variable
1842 and then not Is_Known_Valid (Etype (Actual))
1844 Set_Is_Known_Valid (Entity (Actual), False);
1847 -- For an OUT or IN OUT parameter of an access type, if the
1848 -- actual is an entity, then it is no longer known to be non-null.
1850 if Ekind (Formal) /= E_In_Parameter
1851 and then Is_Entity_Name (Actual)
1852 and then Is_Access_Type (Etype (Actual))
1854 Set_Is_Known_Non_Null (Entity (Actual), False);
1857 -- If the formal is class wide and the actual is an aggregate, force
1858 -- evaluation so that the back end who does not know about class-wide
1859 -- type, does not generate a temporary of the wrong size.
1861 if not Is_Class_Wide_Type (Etype (Formal)) then
1864 elsif Nkind (Actual) = N_Aggregate
1865 or else (Nkind (Actual) = N_Qualified_Expression
1866 and then Nkind (Expression (Actual)) = N_Aggregate)
1868 Force_Evaluation (Actual);
1871 -- In a remote call, if the formal is of a class-wide type, check
1872 -- that the actual meets the requirements described in E.4(18).
1875 and then Is_Class_Wide_Type (Etype (Formal))
1877 Insert_Action (Actual,
1878 Make_Implicit_If_Statement (N,
1881 Get_Remotely_Callable
1882 (Duplicate_Subexpr_Move_Checks (Actual))),
1883 Then_Statements => New_List (
1884 Make_Raise_Program_Error (Loc,
1885 Reason => PE_Illegal_RACW_E_4_18))));
1888 -- This label is required when skipping extra actual generation for
1889 -- Unchecked_Union parameters.
1891 <<Skip_Extra_Actual_Generation>>
1893 Next_Actual (Actual);
1894 Next_Formal (Formal);
1897 -- If we are expanding a rhs of an assignement we need to check if
1898 -- tag propagation is needed. This code belongs theorically in Analyze
1899 -- Assignment but has to be done earlier (bottom-up) because the
1900 -- assignment might be transformed into a declaration for an uncons-
1901 -- trained value, if the expression is classwide.
1903 if Nkind (N) = N_Function_Call
1904 and then Is_Tag_Indeterminate (N)
1905 and then Is_Entity_Name (Name (N))
1908 Ass : Node_Id := Empty;
1911 if Nkind (Parent (N)) = N_Assignment_Statement then
1914 elsif Nkind (Parent (N)) = N_Qualified_Expression
1915 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
1917 Ass := Parent (Parent (N));
1921 and then Is_Class_Wide_Type (Etype (Name (Ass)))
1923 if Etype (N) /= Root_Type (Etype (Name (Ass))) then
1925 ("tag-indeterminate expression must have type&"
1926 & "('R'M 5.2 (6))", N, Root_Type (Etype (Name (Ass))));
1928 Propagate_Tag (Name (Ass), N);
1931 -- The call will be rewritten as a dispatching call, and
1932 -- expanded as such.
1939 -- Ada 2005 (AI-251): If some formal is a class-wide interface, expand
1940 -- it to point to the correct secondary virtual table
1942 if (Nkind (N) = N_Function_Call
1943 or else Nkind (N) = N_Procedure_Call_Statement)
1944 and then CW_Interface_Formals_Present
1946 Expand_Interface_Actuals (N);
1949 -- Deals with Dispatch_Call if we still have a call, before expanding
1950 -- extra actuals since this will be done on the re-analysis of the
1951 -- dispatching call. Note that we do not try to shorten the actual
1952 -- list for a dispatching call, it would not make sense to do so.
1953 -- Expansion of dispatching calls is suppressed when Java_VM, because
1954 -- the JVM back end directly handles the generation of dispatching
1955 -- calls and would have to undo any expansion to an indirect call.
1957 if (Nkind (N) = N_Function_Call
1958 or else Nkind (N) = N_Procedure_Call_Statement)
1959 and then Present (Controlling_Argument (N))
1960 and then not Java_VM
1962 Expand_Dispatching_Call (N);
1964 -- The following return is worrisome. Is it really OK to
1965 -- skip all remaining processing in this procedure ???
1969 -- Similarly, expand calls to RCI subprograms on which pragma
1970 -- All_Calls_Remote applies. The rewriting will be reanalyzed
1971 -- later. Do this only when the call comes from source since we do
1972 -- not want such a rewritting to occur in expanded code.
1974 elsif Is_All_Remote_Call (N) then
1975 Expand_All_Calls_Remote_Subprogram_Call (N);
1977 -- Similarly, do not add extra actuals for an entry call whose entity
1978 -- is a protected procedure, or for an internal protected subprogram
1979 -- call, because it will be rewritten as a protected subprogram call
1980 -- and reanalyzed (see Expand_Protected_Subprogram_Call).
1982 elsif Is_Protected_Type (Scope (Subp))
1983 and then (Ekind (Subp) = E_Procedure
1984 or else Ekind (Subp) = E_Function)
1988 -- During that loop we gathered the extra actuals (the ones that
1989 -- correspond to Extra_Formals), so now they can be appended.
1992 while Is_Non_Empty_List (Extra_Actuals) loop
1993 Add_Actual_Parameter (Remove_Head (Extra_Actuals));
1997 -- At this point we have all the actuals, so this is the point at
1998 -- which the various expansion activities for actuals is carried out.
2000 Expand_Actuals (N, Subp);
2002 -- If the subprogram is a renaming, or if it is inherited, replace it
2003 -- in the call with the name of the actual subprogram being called.
2004 -- If this is a dispatching call, the run-time decides what to call.
2005 -- The Alias attribute does not apply to entries.
2007 if Nkind (N) /= N_Entry_Call_Statement
2008 and then No (Controlling_Argument (N))
2009 and then Present (Parent_Subp)
2011 if Present (Inherited_From_Formal (Subp)) then
2012 Parent_Subp := Inherited_From_Formal (Subp);
2014 while Present (Alias (Parent_Subp)) loop
2015 Parent_Subp := Alias (Parent_Subp);
2019 Set_Entity (Name (N), Parent_Subp);
2021 if Is_Abstract (Parent_Subp)
2022 and then not In_Instance
2025 ("cannot call abstract subprogram &!", Name (N), Parent_Subp);
2028 -- Add an explicit conversion for parameter of the derived type.
2029 -- This is only done for scalar and access in-parameters. Others
2030 -- have been expanded in expand_actuals.
2032 Formal := First_Formal (Subp);
2033 Parent_Formal := First_Formal (Parent_Subp);
2034 Actual := First_Actual (N);
2036 -- It is not clear that conversion is needed for intrinsic
2037 -- subprograms, but it certainly is for those that are user-
2038 -- defined, and that can be inherited on derivation, namely
2039 -- unchecked conversion and deallocation.
2040 -- General case needs study ???
2042 if not Is_Intrinsic_Subprogram (Parent_Subp)
2043 or else Is_Generic_Instance (Parent_Subp)
2045 while Present (Formal) loop
2046 if Etype (Formal) /= Etype (Parent_Formal)
2047 and then Is_Scalar_Type (Etype (Formal))
2048 and then Ekind (Formal) = E_In_Parameter
2049 and then not Raises_Constraint_Error (Actual)
2052 OK_Convert_To (Etype (Parent_Formal),
2053 Relocate_Node (Actual)));
2056 Resolve (Actual, Etype (Parent_Formal));
2057 Enable_Range_Check (Actual);
2059 elsif Is_Access_Type (Etype (Formal))
2060 and then Base_Type (Etype (Parent_Formal)) /=
2061 Base_Type (Etype (Actual))
2063 if Ekind (Formal) /= E_In_Parameter then
2065 Convert_To (Etype (Parent_Formal),
2066 Relocate_Node (Actual)));
2069 Resolve (Actual, Etype (Parent_Formal));
2072 Ekind (Etype (Parent_Formal)) = E_Anonymous_Access_Type
2073 and then Designated_Type (Etype (Parent_Formal))
2075 Designated_Type (Etype (Actual))
2076 and then not Is_Controlling_Formal (Formal)
2078 -- This unchecked conversion is not necessary unless
2079 -- inlining is enabled, because in that case the type
2080 -- mismatch may become visible in the body about to be
2084 Unchecked_Convert_To (Etype (Parent_Formal),
2085 Relocate_Node (Actual)));
2088 Resolve (Actual, Etype (Parent_Formal));
2092 Next_Formal (Formal);
2093 Next_Formal (Parent_Formal);
2094 Next_Actual (Actual);
2099 Subp := Parent_Subp;
2102 -- Check for violation of No_Abort_Statements
2104 if Is_RTE (Subp, RE_Abort_Task) then
2105 Check_Restriction (No_Abort_Statements, N);
2107 -- Check for violation of No_Dynamic_Attachment
2109 elsif RTU_Loaded (Ada_Interrupts)
2110 and then (Is_RTE (Subp, RE_Is_Reserved) or else
2111 Is_RTE (Subp, RE_Is_Attached) or else
2112 Is_RTE (Subp, RE_Current_Handler) or else
2113 Is_RTE (Subp, RE_Attach_Handler) or else
2114 Is_RTE (Subp, RE_Exchange_Handler) or else
2115 Is_RTE (Subp, RE_Detach_Handler) or else
2116 Is_RTE (Subp, RE_Reference))
2118 Check_Restriction (No_Dynamic_Attachment, N);
2121 -- Deal with case where call is an explicit dereference
2123 if Nkind (Name (N)) = N_Explicit_Dereference then
2125 -- Handle case of access to protected subprogram type
2127 if Ekind (Base_Type (Etype (Prefix (Name (N))))) =
2128 E_Access_Protected_Subprogram_Type
2130 -- If this is a call through an access to protected operation,
2131 -- the prefix has the form (object'address, operation'access).
2132 -- Rewrite as a for other protected calls: the object is the
2133 -- first parameter of the list of actuals.
2140 Ptr : constant Node_Id := Prefix (Name (N));
2142 T : constant Entity_Id :=
2143 Equivalent_Type (Base_Type (Etype (Ptr)));
2145 D_T : constant Entity_Id :=
2146 Designated_Type (Base_Type (Etype (Ptr)));
2150 Make_Selected_Component (Loc,
2151 Prefix => Unchecked_Convert_To (T, Ptr),
2153 New_Occurrence_Of (First_Entity (T), Loc));
2156 Make_Selected_Component (Loc,
2157 Prefix => Unchecked_Convert_To (T, Ptr),
2159 New_Occurrence_Of (Next_Entity (First_Entity (T)), Loc));
2161 Nam := Make_Explicit_Dereference (Loc, Nam);
2163 if Present (Parameter_Associations (N)) then
2164 Parm := Parameter_Associations (N);
2169 Prepend (Obj, Parm);
2171 if Etype (D_T) = Standard_Void_Type then
2172 Call := Make_Procedure_Call_Statement (Loc,
2174 Parameter_Associations => Parm);
2176 Call := Make_Function_Call (Loc,
2178 Parameter_Associations => Parm);
2181 Set_First_Named_Actual (Call, First_Named_Actual (N));
2182 Set_Etype (Call, Etype (D_T));
2184 -- We do not re-analyze the call to avoid infinite recursion.
2185 -- We analyze separately the prefix and the object, and set
2186 -- the checks on the prefix that would otherwise be emitted
2187 -- when resolving a call.
2191 Apply_Access_Check (Nam);
2198 -- If this is a call to an intrinsic subprogram, then perform the
2199 -- appropriate expansion to the corresponding tree node and we
2200 -- are all done (since after that the call is gone!)
2202 -- In the case where the intrinsic is to be processed by the back end,
2203 -- the call to Expand_Intrinsic_Call will do nothing, which is fine,
2204 -- since the idea in this case is to pass the call unchanged.
2206 if Is_Intrinsic_Subprogram (Subp) then
2207 Expand_Intrinsic_Call (N, Subp);
2211 if Ekind (Subp) = E_Function
2212 or else Ekind (Subp) = E_Procedure
2214 if Is_Inlined (Subp) then
2216 Inlined_Subprogram : declare
2218 Must_Inline : Boolean := False;
2219 Spec : constant Node_Id := Unit_Declaration_Node (Subp);
2220 Scop : constant Entity_Id := Scope (Subp);
2222 function In_Unfrozen_Instance return Boolean;
2223 -- If the subprogram comes from an instance in the same
2224 -- unit, and the instance is not yet frozen, inlining might
2225 -- trigger order-of-elaboration problems in gigi.
2227 --------------------------
2228 -- In_Unfrozen_Instance --
2229 --------------------------
2231 function In_Unfrozen_Instance return Boolean is
2237 and then S /= Standard_Standard
2239 if Is_Generic_Instance (S)
2240 and then Present (Freeze_Node (S))
2241 and then not Analyzed (Freeze_Node (S))
2250 end In_Unfrozen_Instance;
2252 -- Start of processing for Inlined_Subprogram
2255 -- Verify that the body to inline has already been seen, and
2256 -- that if the body is in the current unit the inlining does
2257 -- not occur earlier. This avoids order-of-elaboration problems
2260 -- This should be documented in sinfo/einfo ???
2263 or else Nkind (Spec) /= N_Subprogram_Declaration
2264 or else No (Body_To_Inline (Spec))
2266 Must_Inline := False;
2268 -- If this an inherited function that returns a private
2269 -- type, do not inline if the full view is an unconstrained
2270 -- array, because such calls cannot be inlined.
2272 elsif Present (Orig_Subp)
2273 and then Is_Array_Type (Etype (Orig_Subp))
2274 and then not Is_Constrained (Etype (Orig_Subp))
2276 Must_Inline := False;
2278 elsif In_Unfrozen_Instance then
2279 Must_Inline := False;
2282 Bod := Body_To_Inline (Spec);
2284 if (In_Extended_Main_Code_Unit (N)
2285 or else In_Extended_Main_Code_Unit (Parent (N))
2286 or else Is_Always_Inlined (Subp))
2287 and then (not In_Same_Extended_Unit (Sloc (Bod), Loc)
2289 Earlier_In_Extended_Unit (Sloc (Bod), Loc))
2291 Must_Inline := True;
2293 -- If we are compiling a package body that is not the main
2294 -- unit, it must be for inlining/instantiation purposes,
2295 -- in which case we inline the call to insure that the same
2296 -- temporaries are generated when compiling the body by
2297 -- itself. Otherwise link errors can occur.
2299 -- If the function being called is itself in the main unit,
2300 -- we cannot inline, because there is a risk of double
2301 -- elaboration and/or circularity: the inlining can make
2302 -- visible a private entity in the body of the main unit,
2303 -- that gigi will see before its sees its proper definition.
2305 elsif not (In_Extended_Main_Code_Unit (N))
2306 and then In_Package_Body
2308 Must_Inline := not In_Extended_Main_Source_Unit (Subp);
2313 Expand_Inlined_Call (N, Subp, Orig_Subp);
2316 -- Let the back end handle it
2318 Add_Inlined_Body (Subp);
2320 if Front_End_Inlining
2321 and then Nkind (Spec) = N_Subprogram_Declaration
2322 and then (In_Extended_Main_Code_Unit (N))
2323 and then No (Body_To_Inline (Spec))
2324 and then not Has_Completion (Subp)
2325 and then In_Same_Extended_Unit (Sloc (Spec), Loc)
2328 ("cannot inline& (body not seen yet)?",
2332 end Inlined_Subprogram;
2336 -- Check for a protected subprogram. This is either an intra-object
2337 -- call, or a protected function call. Protected procedure calls are
2338 -- rewritten as entry calls and handled accordingly.
2340 Scop := Scope (Subp);
2342 if Nkind (N) /= N_Entry_Call_Statement
2343 and then Is_Protected_Type (Scop)
2345 -- If the call is an internal one, it is rewritten as a call to
2346 -- to the corresponding unprotected subprogram.
2348 Expand_Protected_Subprogram_Call (N, Subp, Scop);
2351 -- Functions returning controlled objects need special attention
2353 if Controlled_Type (Etype (Subp))
2354 and then not Is_Return_By_Reference_Type (Etype (Subp))
2356 Expand_Ctrl_Function_Call (N);
2359 -- Test for First_Optional_Parameter, and if so, truncate parameter
2360 -- list if there are optional parameters at the trailing end.
2361 -- Note we never delete procedures for call via a pointer.
2363 if (Ekind (Subp) = E_Procedure or else Ekind (Subp) = E_Function)
2364 and then Present (First_Optional_Parameter (Subp))
2367 Last_Keep_Arg : Node_Id;
2370 -- Last_Keep_Arg will hold the last actual that should be
2371 -- retained. If it remains empty at the end, it means that
2372 -- all parameters are optional.
2374 Last_Keep_Arg := Empty;
2376 -- Find first optional parameter, must be present since we
2377 -- checked the validity of the parameter before setting it.
2379 Formal := First_Formal (Subp);
2380 Actual := First_Actual (N);
2381 while Formal /= First_Optional_Parameter (Subp) loop
2382 Last_Keep_Arg := Actual;
2383 Next_Formal (Formal);
2384 Next_Actual (Actual);
2387 -- We have Formal and Actual pointing to the first potentially
2388 -- droppable argument. We can drop all the trailing arguments
2389 -- whose actual matches the default. Note that we know that all
2390 -- remaining formals have defaults, because we checked that this
2391 -- requirement was met before setting First_Optional_Parameter.
2393 -- We use Fully_Conformant_Expressions to check for identity
2394 -- between formals and actuals, which may miss some cases, but
2395 -- on the other hand, this is only an optimization (if we fail
2396 -- to truncate a parameter it does not affect functionality).
2397 -- So if the default is 3 and the actual is 1+2, we consider
2398 -- them unequal, which hardly seems worrisome.
2400 while Present (Formal) loop
2401 if not Fully_Conformant_Expressions
2402 (Actual, Default_Value (Formal))
2404 Last_Keep_Arg := Actual;
2407 Next_Formal (Formal);
2408 Next_Actual (Actual);
2411 -- If no arguments, delete entire list, this is the easy case
2413 if No (Last_Keep_Arg) then
2414 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2415 Delete_Tree (Remove_Head (Parameter_Associations (N)));
2418 Set_Parameter_Associations (N, No_List);
2419 Set_First_Named_Actual (N, Empty);
2421 -- Case where at the last retained argument is positional. This
2422 -- is also an easy case, since the retained arguments are already
2423 -- in the right form, and we don't need to worry about the order
2424 -- of arguments that get eliminated.
2426 elsif Is_List_Member (Last_Keep_Arg) then
2427 while Present (Next (Last_Keep_Arg)) loop
2428 Delete_Tree (Remove_Next (Last_Keep_Arg));
2431 Set_First_Named_Actual (N, Empty);
2433 -- This is the annoying case where the last retained argument
2434 -- is a named parameter. Since the original arguments are not
2435 -- in declaration order, we may have to delete some fairly
2436 -- random collection of arguments.
2444 pragma Warnings (Off, Discard);
2447 -- First step, remove all the named parameters from the
2448 -- list (they are still chained using First_Named_Actual
2449 -- and Next_Named_Actual, so we have not lost them!)
2451 Temp := First (Parameter_Associations (N));
2453 -- Case of all parameters named, remove them all
2455 if Nkind (Temp) = N_Parameter_Association then
2456 while Is_Non_Empty_List (Parameter_Associations (N)) loop
2457 Temp := Remove_Head (Parameter_Associations (N));
2460 -- Case of mixed positional/named, remove named parameters
2463 while Nkind (Next (Temp)) /= N_Parameter_Association loop
2467 while Present (Next (Temp)) loop
2468 Discard := Remove_Next (Temp);
2472 -- Now we loop through the named parameters, till we get
2473 -- to the last one to be retained, adding them to the list.
2474 -- Note that the Next_Named_Actual list does not need to be
2475 -- touched since we are only reordering them on the actual
2476 -- parameter association list.
2478 Passoc := Parent (First_Named_Actual (N));
2480 Temp := Relocate_Node (Passoc);
2482 (Parameter_Associations (N), Temp);
2484 Last_Keep_Arg = Explicit_Actual_Parameter (Passoc);
2485 Passoc := Parent (Next_Named_Actual (Passoc));
2488 Set_Next_Named_Actual (Temp, Empty);
2491 Temp := Next_Named_Actual (Passoc);
2492 exit when No (Temp);
2493 Set_Next_Named_Actual
2494 (Passoc, Next_Named_Actual (Parent (Temp)));
2503 --------------------------
2504 -- Expand_Inlined_Call --
2505 --------------------------
2507 procedure Expand_Inlined_Call
2510 Orig_Subp : Entity_Id)
2512 Loc : constant Source_Ptr := Sloc (N);
2513 Is_Predef : constant Boolean :=
2514 Is_Predefined_File_Name
2515 (Unit_File_Name (Get_Source_Unit (Subp)));
2516 Orig_Bod : constant Node_Id :=
2517 Body_To_Inline (Unit_Declaration_Node (Subp));
2522 Exit_Lab : Entity_Id := Empty;
2529 Ret_Type : Entity_Id;
2532 Temp_Typ : Entity_Id;
2534 procedure Make_Exit_Label;
2535 -- Build declaration for exit label to be used in Return statements
2537 function Process_Formals (N : Node_Id) return Traverse_Result;
2538 -- Replace occurrence of a formal with the corresponding actual, or
2539 -- the thunk generated for it.
2541 function Process_Sloc (Nod : Node_Id) return Traverse_Result;
2542 -- If the call being expanded is that of an internal subprogram,
2543 -- set the sloc of the generated block to that of the call itself,
2544 -- so that the expansion is skipped by the -next- command in gdb.
2545 -- Same processing for a subprogram in a predefined file, e.g.
2546 -- Ada.Tags. If Debug_Generated_Code is true, suppress this change
2547 -- to simplify our own development.
2549 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id);
2550 -- If the function body is a single expression, replace call with
2551 -- expression, else insert block appropriately.
2553 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id);
2554 -- If procedure body has no local variables, inline body without
2555 -- creating block, otherwise rewrite call with block.
2557 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean;
2558 -- Determine whether a formal parameter is used only once in Orig_Bod
2560 ---------------------
2561 -- Make_Exit_Label --
2562 ---------------------
2564 procedure Make_Exit_Label is
2566 -- Create exit label for subprogram if one does not exist yet
2568 if No (Exit_Lab) then
2569 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
2571 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
2572 Exit_Lab := Make_Label (Loc, Lab_Id);
2575 Make_Implicit_Label_Declaration (Loc,
2576 Defining_Identifier => Entity (Lab_Id),
2577 Label_Construct => Exit_Lab);
2579 end Make_Exit_Label;
2581 ---------------------
2582 -- Process_Formals --
2583 ---------------------
2585 function Process_Formals (N : Node_Id) return Traverse_Result is
2591 if Is_Entity_Name (N)
2592 and then Present (Entity (N))
2597 and then Scope (E) = Subp
2599 A := Renamed_Object (E);
2601 if Is_Entity_Name (A) then
2602 Rewrite (N, New_Occurrence_Of (Entity (A), Loc));
2604 elsif Nkind (A) = N_Defining_Identifier then
2605 Rewrite (N, New_Occurrence_Of (A, Loc));
2607 else -- numeric literal
2608 Rewrite (N, New_Copy (A));
2614 elsif Nkind (N) = N_Return_Statement then
2616 if No (Expression (N)) then
2618 Rewrite (N, Make_Goto_Statement (Loc,
2619 Name => New_Copy (Lab_Id)));
2622 if Nkind (Parent (N)) = N_Handled_Sequence_Of_Statements
2623 and then Nkind (Parent (Parent (N))) = N_Subprogram_Body
2625 -- Function body is a single expression. No need for
2631 Num_Ret := Num_Ret + 1;
2635 -- Because of the presence of private types, the views of the
2636 -- expression and the context may be different, so place an
2637 -- unchecked conversion to the context type to avoid spurious
2638 -- errors, eg. when the expression is a numeric literal and
2639 -- the context is private. If the expression is an aggregate,
2640 -- use a qualified expression, because an aggregate is not a
2641 -- legal argument of a conversion.
2643 if Nkind (Expression (N)) = N_Aggregate
2644 or else Nkind (Expression (N)) = N_Null
2647 Make_Qualified_Expression (Sloc (N),
2648 Subtype_Mark => New_Occurrence_Of (Ret_Type, Sloc (N)),
2649 Expression => Relocate_Node (Expression (N)));
2652 Unchecked_Convert_To
2653 (Ret_Type, Relocate_Node (Expression (N)));
2656 if Nkind (Targ) = N_Defining_Identifier then
2658 Make_Assignment_Statement (Loc,
2659 Name => New_Occurrence_Of (Targ, Loc),
2660 Expression => Ret));
2663 Make_Assignment_Statement (Loc,
2664 Name => New_Copy (Targ),
2665 Expression => Ret));
2668 Set_Assignment_OK (Name (N));
2670 if Present (Exit_Lab) then
2672 Make_Goto_Statement (Loc,
2673 Name => New_Copy (Lab_Id)));
2679 -- Remove pragma Unreferenced since it may refer to formals that
2680 -- are not visible in the inlined body, and in any case we will
2681 -- not be posting warnings on the inlined body so it is unneeded.
2683 elsif Nkind (N) = N_Pragma
2684 and then Chars (N) = Name_Unreferenced
2686 Rewrite (N, Make_Null_Statement (Sloc (N)));
2692 end Process_Formals;
2694 procedure Replace_Formals is new Traverse_Proc (Process_Formals);
2700 function Process_Sloc (Nod : Node_Id) return Traverse_Result is
2702 if not Debug_Generated_Code then
2703 Set_Sloc (Nod, Sloc (N));
2704 Set_Comes_From_Source (Nod, False);
2710 procedure Reset_Slocs is new Traverse_Proc (Process_Sloc);
2712 ---------------------------
2713 -- Rewrite_Function_Call --
2714 ---------------------------
2716 procedure Rewrite_Function_Call (N : Node_Id; Blk : Node_Id) is
2717 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2718 Fst : constant Node_Id := First (Statements (HSS));
2721 -- Optimize simple case: function body is a single return statement,
2722 -- which has been expanded into an assignment.
2724 if Is_Empty_List (Declarations (Blk))
2725 and then Nkind (Fst) = N_Assignment_Statement
2726 and then No (Next (Fst))
2729 -- The function call may have been rewritten as the temporary
2730 -- that holds the result of the call, in which case remove the
2731 -- now useless declaration.
2733 if Nkind (N) = N_Identifier
2734 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2736 Rewrite (Parent (Entity (N)), Make_Null_Statement (Loc));
2739 Rewrite (N, Expression (Fst));
2741 elsif Nkind (N) = N_Identifier
2742 and then Nkind (Parent (Entity (N))) = N_Object_Declaration
2744 -- The block assigns the result of the call to the temporary
2746 Insert_After (Parent (Entity (N)), Blk);
2748 elsif Nkind (Parent (N)) = N_Assignment_Statement
2749 and then Is_Entity_Name (Name (Parent (N)))
2751 -- Replace assignment with the block
2754 Original_Assignment : constant Node_Id := Parent (N);
2757 -- Preserve the original assignment node to keep the complete
2758 -- assignment subtree consistent enough for Analyze_Assignment
2759 -- to proceed (specifically, the original Lhs node must still
2760 -- have an assignment statement as its parent).
2762 -- We cannot rely on Original_Node to go back from the block
2763 -- node to the assignment node, because the assignment might
2764 -- already be a rewrite substitution.
2766 Discard_Node (Relocate_Node (Original_Assignment));
2767 Rewrite (Original_Assignment, Blk);
2770 elsif Nkind (Parent (N)) = N_Object_Declaration then
2771 Set_Expression (Parent (N), Empty);
2772 Insert_After (Parent (N), Blk);
2774 end Rewrite_Function_Call;
2776 ----------------------------
2777 -- Rewrite_Procedure_Call --
2778 ----------------------------
2780 procedure Rewrite_Procedure_Call (N : Node_Id; Blk : Node_Id) is
2781 HSS : constant Node_Id := Handled_Statement_Sequence (Blk);
2783 if Is_Empty_List (Declarations (Blk)) then
2784 Insert_List_After (N, Statements (HSS));
2785 Rewrite (N, Make_Null_Statement (Loc));
2789 end Rewrite_Procedure_Call;
2791 -------------------------
2792 -- Formal_Is_Used_Once --
2793 ------------------------
2795 function Formal_Is_Used_Once (Formal : Entity_Id) return Boolean is
2796 Use_Counter : Int := 0;
2798 function Count_Uses (N : Node_Id) return Traverse_Result;
2799 -- Traverse the tree and count the uses of the formal parameter.
2800 -- In this case, for optimization purposes, we do not need to
2801 -- continue the traversal once more than one use is encountered.
2807 function Count_Uses (N : Node_Id) return Traverse_Result is
2809 -- The original node is an identifier
2811 if Nkind (N) = N_Identifier
2812 and then Present (Entity (N))
2814 -- Original node's entity points to the one in the copied body
2816 and then Nkind (Entity (N)) = N_Identifier
2817 and then Present (Entity (Entity (N)))
2819 -- The entity of the copied node is the formal parameter
2821 and then Entity (Entity (N)) = Formal
2823 Use_Counter := Use_Counter + 1;
2825 if Use_Counter > 1 then
2827 -- Denote more than one use and abandon the traversal
2838 procedure Count_Formal_Uses is new Traverse_Proc (Count_Uses);
2840 -- Start of processing for Formal_Is_Used_Once
2843 Count_Formal_Uses (Orig_Bod);
2844 return Use_Counter = 1;
2845 end Formal_Is_Used_Once;
2847 -- Start of processing for Expand_Inlined_Call
2850 -- Check for special case of To_Address call, and if so, just do an
2851 -- unchecked conversion instead of expanding the call. Not only is this
2852 -- more efficient, but it also avoids problem with order of elaboration
2853 -- when address clauses are inlined (address expression elaborated at
2856 if Subp = RTE (RE_To_Address) then
2858 Unchecked_Convert_To
2860 Relocate_Node (First_Actual (N))));
2864 -- Check for an illegal attempt to inline a recursive procedure. If the
2865 -- subprogram has parameters this is detected when trying to supply a
2866 -- binding for parameters that already have one. For parameterless
2867 -- subprograms this must be done explicitly.
2869 if In_Open_Scopes (Subp) then
2870 Error_Msg_N ("call to recursive subprogram cannot be inlined?", N);
2871 Set_Is_Inlined (Subp, False);
2875 if Nkind (Orig_Bod) = N_Defining_Identifier
2876 or else Nkind (Orig_Bod) = N_Defining_Operator_Symbol
2878 -- Subprogram is a renaming_as_body. Calls appearing after the
2879 -- renaming can be replaced with calls to the renamed entity
2880 -- directly, because the subprograms are subtype conformant. If
2881 -- the renamed subprogram is an inherited operation, we must redo
2882 -- the expansion because implicit conversions may be needed.
2884 Set_Name (N, New_Occurrence_Of (Orig_Bod, Loc));
2886 if Present (Alias (Orig_Bod)) then
2893 -- Use generic machinery to copy body of inlined subprogram, as if it
2894 -- were an instantiation, resetting source locations appropriately, so
2895 -- that nested inlined calls appear in the main unit.
2897 Save_Env (Subp, Empty);
2898 Set_Copied_Sloc_For_Inlined_Body (N, Defining_Entity (Orig_Bod));
2900 Bod := Copy_Generic_Node (Orig_Bod, Empty, Instantiating => True);
2902 Make_Block_Statement (Loc,
2903 Declarations => Declarations (Bod),
2904 Handled_Statement_Sequence => Handled_Statement_Sequence (Bod));
2906 if No (Declarations (Bod)) then
2907 Set_Declarations (Blk, New_List);
2910 -- If this is a derived function, establish the proper return type
2912 if Present (Orig_Subp)
2913 and then Orig_Subp /= Subp
2915 Ret_Type := Etype (Orig_Subp);
2917 Ret_Type := Etype (Subp);
2920 -- Create temporaries for the actuals that are expressions, or that
2921 -- are scalars and require copying to preserve semantics.
2923 F := First_Formal (Subp);
2924 A := First_Actual (N);
2925 while Present (F) loop
2926 if Present (Renamed_Object (F)) then
2927 Error_Msg_N ("cannot inline call to recursive subprogram", N);
2931 -- If the argument may be a controlling argument in a call within
2932 -- the inlined body, we must preserve its classwide nature to insure
2933 -- that dynamic dispatching take place subsequently. If the formal
2934 -- has a constraint it must be preserved to retain the semantics of
2937 if Is_Class_Wide_Type (Etype (F))
2938 or else (Is_Access_Type (Etype (F))
2940 Is_Class_Wide_Type (Designated_Type (Etype (F))))
2942 Temp_Typ := Etype (F);
2944 elsif Base_Type (Etype (F)) = Base_Type (Etype (A))
2945 and then Etype (F) /= Base_Type (Etype (F))
2947 Temp_Typ := Etype (F);
2950 Temp_Typ := Etype (A);
2953 -- If the actual is a simple name or a literal, no need to
2954 -- create a temporary, object can be used directly.
2956 if (Is_Entity_Name (A)
2958 (not Is_Scalar_Type (Etype (A))
2959 or else Ekind (Entity (A)) = E_Enumeration_Literal))
2961 -- When the actual is an identifier and the corresponding formal
2962 -- is used only once in the original body, the formal can be
2963 -- substituted directly with the actual parameter.
2965 or else (Nkind (A) = N_Identifier
2966 and then Formal_Is_Used_Once (F))
2968 or else Nkind (A) = N_Real_Literal
2969 or else Nkind (A) = N_Integer_Literal
2970 or else Nkind (A) = N_Character_Literal
2972 if Etype (F) /= Etype (A) then
2974 (F, Unchecked_Convert_To (Etype (F), Relocate_Node (A)));
2976 Set_Renamed_Object (F, A);
2981 Make_Defining_Identifier (Loc,
2982 Chars => New_Internal_Name ('C'));
2984 -- If the actual for an in/in-out parameter is a view conversion,
2985 -- make it into an unchecked conversion, given that an untagged
2986 -- type conversion is not a proper object for a renaming.
2988 -- In-out conversions that involve real conversions have already
2989 -- been transformed in Expand_Actuals.
2991 if Nkind (A) = N_Type_Conversion
2992 and then Ekind (F) /= E_In_Parameter
2994 New_A := Make_Unchecked_Type_Conversion (Loc,
2995 Subtype_Mark => New_Occurrence_Of (Etype (F), Loc),
2996 Expression => Relocate_Node (Expression (A)));
2998 elsif Etype (F) /= Etype (A) then
2999 New_A := Unchecked_Convert_To (Etype (F), Relocate_Node (A));
3000 Temp_Typ := Etype (F);
3003 New_A := Relocate_Node (A);
3006 Set_Sloc (New_A, Sloc (N));
3008 if Ekind (F) = E_In_Parameter
3009 and then not Is_Limited_Type (Etype (A))
3012 Make_Object_Declaration (Loc,
3013 Defining_Identifier => Temp,
3014 Constant_Present => True,
3015 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3016 Expression => New_A);
3019 Make_Object_Renaming_Declaration (Loc,
3020 Defining_Identifier => Temp,
3021 Subtype_Mark => New_Occurrence_Of (Temp_Typ, Loc),
3025 Prepend (Decl, Declarations (Blk));
3026 Set_Renamed_Object (F, Temp);
3033 -- Establish target of function call. If context is not assignment or
3034 -- declaration, create a temporary as a target. The declaration for
3035 -- the temporary may be subsequently optimized away if the body is a
3036 -- single expression, or if the left-hand side of the assignment is
3039 if Ekind (Subp) = E_Function then
3040 if Nkind (Parent (N)) = N_Assignment_Statement
3041 and then Is_Entity_Name (Name (Parent (N)))
3043 Targ := Name (Parent (N));
3046 -- Replace call with temporary and create its declaration
3049 Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
3050 Set_Is_Internal (Temp);
3053 Make_Object_Declaration (Loc,
3054 Defining_Identifier => Temp,
3055 Object_Definition =>
3056 New_Occurrence_Of (Ret_Type, Loc));
3058 Set_No_Initialization (Decl);
3059 Insert_Action (N, Decl);
3060 Rewrite (N, New_Occurrence_Of (Temp, Loc));
3065 -- Traverse the tree and replace formals with actuals or their thunks.
3066 -- Attach block to tree before analysis and rewriting.
3068 Replace_Formals (Blk);
3069 Set_Parent (Blk, N);
3071 if not Comes_From_Source (Subp)
3077 if Present (Exit_Lab) then
3079 -- If the body was a single expression, the single return statement
3080 -- and the corresponding label are useless.
3084 Nkind (Last (Statements (Handled_Statement_Sequence (Blk)))) =
3087 Remove (Last (Statements (Handled_Statement_Sequence (Blk))));
3089 Append (Lab_Decl, (Declarations (Blk)));
3090 Append (Exit_Lab, Statements (Handled_Statement_Sequence (Blk)));
3094 -- Analyze Blk with In_Inlined_Body set, to avoid spurious errors on
3095 -- conflicting private views that Gigi would ignore. If this is
3096 -- predefined unit, analyze with checks off, as is done in the non-
3097 -- inlined run-time units.
3100 I_Flag : constant Boolean := In_Inlined_Body;
3103 In_Inlined_Body := True;
3107 Style : constant Boolean := Style_Check;
3109 Style_Check := False;
3110 Analyze (Blk, Suppress => All_Checks);
3111 Style_Check := Style;
3118 In_Inlined_Body := I_Flag;
3121 if Ekind (Subp) = E_Procedure then
3122 Rewrite_Procedure_Call (N, Blk);
3124 Rewrite_Function_Call (N, Blk);
3129 -- Cleanup mapping between formals and actuals for other expansions
3131 F := First_Formal (Subp);
3132 while Present (F) loop
3133 Set_Renamed_Object (F, Empty);
3136 end Expand_Inlined_Call;
3138 ----------------------------
3139 -- Expand_N_Function_Call --
3140 ----------------------------
3142 procedure Expand_N_Function_Call (N : Node_Id) is
3143 Typ : constant Entity_Id := Etype (N);
3145 function Returned_By_Reference return Boolean;
3146 -- If the return type is returned through the secondary stack. that is
3147 -- by reference, we don't want to create a temp to force stack checking.
3148 -- Shouldn't this function be moved to exp_util???
3150 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean;
3151 -- If the call is the right side of an assignment or the expression in
3152 -- an object declaration, we don't need to create a temp as the left
3153 -- side will already trigger stack checking if necessary.
3155 -- If the call is a component in an extension aggregate, it will be
3156 -- expanded into assignments as well, so no temporary is needed. This
3157 -- also solves the problem of functions returning types with unknown
3158 -- discriminants, where it is not possible to declare an object of the
3161 ---------------------------
3162 -- Returned_By_Reference --
3163 ---------------------------
3165 function Returned_By_Reference return Boolean is
3169 if Is_Return_By_Reference_Type (Typ) then
3172 elsif Nkind (Parent (N)) /= N_Return_Statement then
3175 elsif Requires_Transient_Scope (Typ) then
3177 -- Verify that the return type of the enclosing function has the
3178 -- same constrained status as that of the expression.
3181 while Ekind (S) /= E_Function loop
3185 return Is_Constrained (Typ) = Is_Constrained (Etype (S));
3189 end Returned_By_Reference;
3191 ---------------------------
3192 -- Rhs_Of_Assign_Or_Decl --
3193 ---------------------------
3195 function Rhs_Of_Assign_Or_Decl (N : Node_Id) return Boolean is
3197 if (Nkind (Parent (N)) = N_Assignment_Statement
3198 and then Expression (Parent (N)) = N)
3200 (Nkind (Parent (N)) = N_Qualified_Expression
3201 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
3202 and then Expression (Parent (Parent (N))) = Parent (N))
3204 (Nkind (Parent (N)) = N_Object_Declaration
3205 and then Expression (Parent (N)) = N)
3207 (Nkind (Parent (N)) = N_Component_Association
3208 and then Expression (Parent (N)) = N
3209 and then Nkind (Parent (Parent (N))) = N_Aggregate
3210 and then Rhs_Of_Assign_Or_Decl (Parent (Parent (N))))
3212 (Nkind (Parent (N)) = N_Extension_Aggregate
3213 and then Is_Private_Type (Etype (Typ)))
3219 end Rhs_Of_Assign_Or_Decl;
3221 -- Start of processing for Expand_N_Function_Call
3224 -- A special check. If stack checking is enabled, and the return type
3225 -- might generate a large temporary, and the call is not the right side
3226 -- of an assignment, then generate an explicit temporary. We do this
3227 -- because otherwise gigi may generate a large temporary on the fly and
3228 -- this can cause trouble with stack checking.
3230 -- This is unecessary if the call is the expression in an object
3231 -- declaration, or if it appears outside of any library unit. This can
3232 -- only happen if it appears as an actual in a library-level instance,
3233 -- in which case a temporary will be generated for it once the instance
3234 -- itself is installed.
3236 if May_Generate_Large_Temp (Typ)
3237 and then not Rhs_Of_Assign_Or_Decl (N)
3238 and then not Returned_By_Reference
3239 and then Current_Scope /= Standard_Standard
3241 if Stack_Checking_Enabled then
3243 -- Note: it might be thought that it would be OK to use a call to
3244 -- Force_Evaluation here, but that's not good enough, because
3245 -- that can results in a 'Reference construct that may still need
3249 Loc : constant Source_Ptr := Sloc (N);
3250 Temp_Obj : constant Entity_Id :=
3251 Make_Defining_Identifier (Loc,
3252 Chars => New_Internal_Name ('F'));
3253 Temp_Typ : Entity_Id := Typ;
3260 if Is_Tagged_Type (Typ)
3261 and then Present (Controlling_Argument (N))
3263 if Nkind (Parent (N)) /= N_Procedure_Call_Statement
3264 and then Nkind (Parent (N)) /= N_Function_Call
3266 -- If this is a tag-indeterminate call, the object must
3269 if Is_Tag_Indeterminate (N) then
3270 Temp_Typ := Class_Wide_Type (Typ);
3274 -- If this is a dispatching call that is itself the
3275 -- controlling argument of an enclosing call, the
3276 -- nominal subtype of the object that replaces it must
3277 -- be classwide, so that dispatching will take place
3278 -- properly. If it is not a controlling argument, the
3279 -- object is not classwide.
3281 Proc := Entity (Name (Parent (N)));
3283 F := First_Formal (Proc);
3284 A := First_Actual (Parent (N));
3290 if Is_Controlling_Formal (F) then
3291 Temp_Typ := Class_Wide_Type (Typ);
3297 Make_Object_Declaration (Loc,
3298 Defining_Identifier => Temp_Obj,
3299 Object_Definition => New_Occurrence_Of (Temp_Typ, Loc),
3300 Constant_Present => True,
3301 Expression => Relocate_Node (N));
3302 Set_Assignment_OK (Decl);
3304 Insert_Actions (N, New_List (Decl));
3305 Rewrite (N, New_Occurrence_Of (Temp_Obj, Loc));
3309 -- If stack-checking is not enabled, increment serial number
3310 -- for internal names, so that subsequent symbols are consistent
3311 -- with and without stack-checking.
3313 Synchronize_Serial_Number;
3315 -- Now we can expand the call with consistent symbol names
3320 -- Normal case, expand the call
3325 end Expand_N_Function_Call;
3327 ---------------------------------------
3328 -- Expand_N_Procedure_Call_Statement --
3329 ---------------------------------------
3331 procedure Expand_N_Procedure_Call_Statement (N : Node_Id) is
3334 end Expand_N_Procedure_Call_Statement;
3336 ------------------------------
3337 -- Expand_N_Subprogram_Body --
3338 ------------------------------
3340 -- Add poll call if ATC polling is enabled, unless the body will be
3341 -- inlined by the back-end.
3343 -- Add return statement if last statement in body is not a return statement
3344 -- (this makes things easier on Gigi which does not want to have to handle
3345 -- a missing return).
3347 -- Add call to Activate_Tasks if body is a task activator
3349 -- Deal with possible detection of infinite recursion
3351 -- Eliminate body completely if convention stubbed
3353 -- Encode entity names within body, since we will not need to reference
3354 -- these entities any longer in the front end.
3356 -- Initialize scalar out parameters if Initialize/Normalize_Scalars
3358 -- Reset Pure indication if any parameter has root type System.Address
3362 procedure Expand_N_Subprogram_Body (N : Node_Id) is
3363 Loc : constant Source_Ptr := Sloc (N);
3364 H : constant Node_Id := Handled_Statement_Sequence (N);
3365 Body_Id : Entity_Id;
3366 Spec_Id : Entity_Id;
3373 procedure Add_Return (S : List_Id);
3374 -- Append a return statement to the statement sequence S if the last
3375 -- statement is not already a return or a goto statement. Note that
3376 -- the latter test is not critical, it does not matter if we add a
3377 -- few extra returns, since they get eliminated anyway later on.
3379 procedure Expand_Thread_Body;
3380 -- Perform required expansion of a thread body
3386 procedure Add_Return (S : List_Id) is
3388 if not Is_Transfer (Last (S)) then
3390 -- The source location for the return is the end label
3391 -- of the procedure in all cases. This is a bit odd when
3392 -- there are exception handlers, but not much else we can do.
3394 Append_To (S, Make_Return_Statement (Sloc (End_Label (H))));
3398 ------------------------
3399 -- Expand_Thread_Body --
3400 ------------------------
3402 -- The required expansion of a thread body is as follows
3404 -- procedure <thread body procedure name> is
3406 -- _Secondary_Stack : aliased
3407 -- Storage_Elements.Storage_Array
3408 -- (1 .. Storage_Offset (Sec_Stack_Size));
3409 -- for _Secondary_Stack'Alignment use Standard'Maximum_Alignment;
3411 -- _Process_ATSD : aliased System.Threads.ATSD;
3414 -- System.Threads.Thread_Body_Enter;
3415 -- (_Secondary_Stack'Address,
3416 -- _Secondary_Stack'Length,
3417 -- _Process_ATSD'Address);
3420 -- <user declarations>
3422 -- <user statements>
3423 -- <user exception handlers>
3426 -- System.Threads.Thread_Body_Leave;
3429 -- when E : others =>
3430 -- System.Threads.Thread_Body_Exceptional_Exit (E);
3433 -- Note the exception handler is omitted if pragma Restriction
3434 -- No_Exception_Handlers is currently active.
3436 procedure Expand_Thread_Body is
3437 User_Decls : constant List_Id := Declarations (N);
3438 Sec_Stack_Len : Node_Id;
3440 TB_Pragma : constant Node_Id :=
3441 Get_Rep_Pragma (Spec_Id, Name_Thread_Body);
3444 Ent_ATSD : Entity_Id;
3448 Decl_ATSD : Node_Id;
3450 Excep_Handlers : List_Id;
3453 New_Scope (Spec_Id);
3455 -- Get proper setting for secondary stack size
3457 if List_Length (Pragma_Argument_Associations (TB_Pragma)) = 2 then
3459 Expression (Last (Pragma_Argument_Associations (TB_Pragma)));
3462 New_Occurrence_Of (RTE (RE_Default_Secondary_Stack_Size), Loc);
3465 Sec_Stack_Len := Convert_To (RTE (RE_Storage_Offset), Sec_Stack_Len);
3467 -- Build and set declarations for the wrapped thread body
3469 Ent_SS := Make_Defining_Identifier (Loc, Name_uSecondary_Stack);
3470 Ent_ATSD := Make_Defining_Identifier (Loc, Name_uProcess_ATSD);
3473 Make_Object_Declaration (Loc,
3474 Defining_Identifier => Ent_SS,
3475 Aliased_Present => True,
3476 Object_Definition =>
3477 Make_Subtype_Indication (Loc,
3479 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
3481 Make_Index_Or_Discriminant_Constraint (Loc,
3482 Constraints => New_List (
3484 Low_Bound => Make_Integer_Literal (Loc, 1),
3485 High_Bound => Sec_Stack_Len)))));
3488 Make_Object_Declaration (Loc,
3489 Defining_Identifier => Ent_ATSD,
3490 Aliased_Present => True,
3491 Object_Definition => New_Occurrence_Of (RTE (RE_ATSD), Loc));
3493 Set_Declarations (N, New_List (Decl_SS, Decl_ATSD));
3495 Analyze (Decl_ATSD);
3496 Set_Alignment (Ent_SS, UI_From_Int (Maximum_Alignment));
3498 -- Create new exception handler
3500 if Restriction_Active (No_Exception_Handlers) then
3501 Excep_Handlers := No_List;
3504 Check_Restriction (No_Exception_Handlers, N);
3506 Ent_EO := Make_Defining_Identifier (Loc, Name_uE);
3508 Excep_Handlers := New_List (
3509 Make_Exception_Handler (Loc,
3510 Choice_Parameter => Ent_EO,
3511 Exception_Choices => New_List (
3512 Make_Others_Choice (Loc)),
3513 Statements => New_List (
3514 Make_Procedure_Call_Statement (Loc,
3517 (RTE (RE_Thread_Body_Exceptional_Exit), Loc),
3518 Parameter_Associations => New_List (
3519 New_Occurrence_Of (Ent_EO, Loc))))));
3522 -- Now build new handled statement sequence and analyze it
3524 Set_Handled_Statement_Sequence (N,
3525 Make_Handled_Sequence_Of_Statements (Loc,
3526 Statements => New_List (
3528 Make_Procedure_Call_Statement (Loc,
3529 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Enter), Loc),
3530 Parameter_Associations => New_List (
3532 Make_Attribute_Reference (Loc,
3533 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3534 Attribute_Name => Name_Address),
3536 Make_Attribute_Reference (Loc,
3537 Prefix => New_Occurrence_Of (Ent_SS, Loc),
3538 Attribute_Name => Name_Length),
3540 Make_Attribute_Reference (Loc,
3541 Prefix => New_Occurrence_Of (Ent_ATSD, Loc),
3542 Attribute_Name => Name_Address))),
3544 Make_Block_Statement (Loc,
3545 Declarations => User_Decls,
3546 Handled_Statement_Sequence => H),
3548 Make_Procedure_Call_Statement (Loc,
3549 Name => New_Occurrence_Of (RTE (RE_Thread_Body_Leave), Loc))),
3551 Exception_Handlers => Excep_Handlers));
3553 Analyze (Handled_Statement_Sequence (N));
3555 end Expand_Thread_Body;
3557 -- Start of processing for Expand_N_Subprogram_Body
3560 -- Set L to either the list of declarations if present, or
3561 -- to the list of statements if no declarations are present.
3562 -- This is used to insert new stuff at the start.
3564 if Is_Non_Empty_List (Declarations (N)) then
3565 L := Declarations (N);
3567 L := Statements (Handled_Statement_Sequence (N));
3570 -- Find entity for subprogram
3572 Body_Id := Defining_Entity (N);
3574 if Present (Corresponding_Spec (N)) then
3575 Spec_Id := Corresponding_Spec (N);
3580 -- Need poll on entry to subprogram if polling enabled. We only
3581 -- do this for non-empty subprograms, since it does not seem
3582 -- necessary to poll for a dummy null subprogram. Do not add polling
3583 -- point if calls to this subprogram will be inlined by the back-end,
3584 -- to avoid repeated polling points in nested inlinings.
3586 if Is_Non_Empty_List (L) then
3587 if Is_Inlined (Spec_Id)
3588 and then Front_End_Inlining
3589 and then Optimization_Level > 1
3593 Generate_Poll_Call (First (L));
3597 -- If this is a Pure function which has any parameters whose root
3598 -- type is System.Address, reset the Pure indication, since it will
3599 -- likely cause incorrect code to be generated as the parameter is
3600 -- probably a pointer, and the fact that the same pointer is passed
3601 -- does not mean that the same value is being referenced.
3603 -- Note that if the programmer gave an explicit Pure_Function pragma,
3604 -- then we believe the programmer, and leave the subprogram Pure.
3606 -- This code should probably be at the freeze point, so that it
3607 -- happens even on a -gnatc (or more importantly -gnatt) compile
3608 -- so that the semantic tree has Is_Pure set properly ???
3610 if Is_Pure (Spec_Id)
3611 and then Is_Subprogram (Spec_Id)
3612 and then not Has_Pragma_Pure_Function (Spec_Id)
3618 F := First_Formal (Spec_Id);
3619 while Present (F) loop
3620 if Is_Descendent_Of_Address (Etype (F)) then
3621 Set_Is_Pure (Spec_Id, False);
3623 if Spec_Id /= Body_Id then
3624 Set_Is_Pure (Body_Id, False);
3635 -- Initialize any scalar OUT args if Initialize/Normalize_Scalars
3637 if Init_Or_Norm_Scalars and then Is_Subprogram (Spec_Id) then
3640 V : constant Boolean := Validity_Checks_On;
3643 -- We turn off validity checking, since we do not want any
3644 -- check on the initializing value itself (which we know
3645 -- may well be invalid!)
3647 Validity_Checks_On := False;
3649 -- Loop through formals
3651 F := First_Formal (Spec_Id);
3652 while Present (F) loop
3653 if Is_Scalar_Type (Etype (F))
3654 and then Ekind (F) = E_Out_Parameter
3656 Insert_Before_And_Analyze (First (L),
3657 Make_Assignment_Statement (Loc,
3658 Name => New_Occurrence_Of (F, Loc),
3659 Expression => Get_Simple_Init_Val (Etype (F), Loc)));
3665 Validity_Checks_On := V;
3669 Scop := Scope (Spec_Id);
3671 -- Add discriminal renamings to protected subprograms. Install new
3672 -- discriminals for expansion of the next subprogram of this protected
3675 if Is_List_Member (N)
3676 and then Present (Parent (List_Containing (N)))
3677 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3679 Add_Discriminal_Declarations
3680 (Declarations (N), Scop, Name_uObject, Loc);
3681 Add_Private_Declarations (Declarations (N), Scop, Name_uObject, Loc);
3683 -- Associate privals and discriminals with the next protected
3684 -- operation body to be expanded. These are used to expand references
3685 -- to private data objects and discriminants, respectively.
3687 Next_Op := Next_Protected_Operation (N);
3689 if Present (Next_Op) then
3690 Dec := Parent (Base_Type (Scop));
3691 Set_Privals (Dec, Next_Op, Loc);
3692 Set_Discriminals (Dec);
3696 -- Clear out statement list for stubbed procedure
3698 if Present (Corresponding_Spec (N)) then
3699 Set_Elaboration_Flag (N, Spec_Id);
3701 if Convention (Spec_Id) = Convention_Stubbed
3702 or else Is_Eliminated (Spec_Id)
3704 Set_Declarations (N, Empty_List);
3705 Set_Handled_Statement_Sequence (N,
3706 Make_Handled_Sequence_Of_Statements (Loc,
3707 Statements => New_List (
3708 Make_Null_Statement (Loc))));
3713 -- Returns_By_Ref flag is normally set when the subprogram is frozen
3714 -- but subprograms with no specs are not frozen.
3717 Typ : constant Entity_Id := Etype (Spec_Id);
3718 Utyp : constant Entity_Id := Underlying_Type (Typ);
3721 if not Acts_As_Spec (N)
3722 and then Nkind (Parent (Parent (Spec_Id))) /=
3723 N_Subprogram_Body_Stub
3727 elsif Is_Return_By_Reference_Type (Typ) then
3728 Set_Returns_By_Ref (Spec_Id);
3730 elsif Present (Utyp) and then Controlled_Type (Utyp) then
3731 Set_Returns_By_Ref (Spec_Id);
3735 -- For a procedure, we add a return for all possible syntactic ends
3736 -- of the subprogram. Note that reanalysis is not necessary in this
3737 -- case since it would require a lot of work and accomplish nothing.
3739 if Ekind (Spec_Id) = E_Procedure
3740 or else Ekind (Spec_Id) = E_Generic_Procedure
3742 Add_Return (Statements (H));
3744 if Present (Exception_Handlers (H)) then
3745 Except_H := First_Non_Pragma (Exception_Handlers (H));
3746 while Present (Except_H) loop
3747 Add_Return (Statements (Except_H));
3748 Next_Non_Pragma (Except_H);
3752 -- For a function, we must deal with the case where there is at least
3753 -- one missing return. What we do is to wrap the entire body of the
3754 -- function in a block:
3767 -- raise Program_Error;
3770 -- This approach is necessary because the raise must be signalled
3771 -- to the caller, not handled by any local handler (RM 6.4(11)).
3773 -- Note: we do not need to analyze the constructed sequence here,
3774 -- since it has no handler, and an attempt to analyze the handled
3775 -- statement sequence twice is risky in various ways (e.g. the
3776 -- issue of expanding cleanup actions twice).
3778 elsif Has_Missing_Return (Spec_Id) then
3780 Hloc : constant Source_Ptr := Sloc (H);
3781 Blok : constant Node_Id :=
3782 Make_Block_Statement (Hloc,
3783 Handled_Statement_Sequence => H);
3784 Rais : constant Node_Id :=
3785 Make_Raise_Program_Error (Hloc,
3786 Reason => PE_Missing_Return);
3789 Set_Handled_Statement_Sequence (N,
3790 Make_Handled_Sequence_Of_Statements (Hloc,
3791 Statements => New_List (Blok, Rais)));
3793 New_Scope (Spec_Id);
3800 -- If subprogram contains a parameterless recursive call, then we may
3801 -- have an infinite recursion, so see if we can generate code to check
3802 -- for this possibility if storage checks are not suppressed.
3804 if Ekind (Spec_Id) = E_Procedure
3805 and then Has_Recursive_Call (Spec_Id)
3806 and then not Storage_Checks_Suppressed (Spec_Id)
3808 Detect_Infinite_Recursion (N, Spec_Id);
3811 -- Finally, if we are in Normalize_Scalars mode, then any scalar out
3812 -- parameters must be initialized to the appropriate default value.
3814 if Ekind (Spec_Id) = E_Procedure and then Normalize_Scalars then
3821 Formal := First_Formal (Spec_Id);
3822 while Present (Formal) loop
3823 Floc := Sloc (Formal);
3825 if Ekind (Formal) = E_Out_Parameter
3826 and then Is_Scalar_Type (Etype (Formal))
3829 Make_Assignment_Statement (Floc,
3830 Name => New_Occurrence_Of (Formal, Floc),
3832 Get_Simple_Init_Val (Etype (Formal), Floc));
3833 Prepend (Stm, Declarations (N));
3837 Next_Formal (Formal);
3842 -- Deal with thread body
3844 if Is_Thread_Body (Spec_Id) then
3848 -- Set to encode entity names in package body before gigi is called
3850 Qualify_Entity_Names (N);
3851 end Expand_N_Subprogram_Body;
3853 -----------------------------------
3854 -- Expand_N_Subprogram_Body_Stub --
3855 -----------------------------------
3857 procedure Expand_N_Subprogram_Body_Stub (N : Node_Id) is
3859 if Present (Corresponding_Body (N)) then
3860 Expand_N_Subprogram_Body (
3861 Unit_Declaration_Node (Corresponding_Body (N)));
3863 end Expand_N_Subprogram_Body_Stub;
3865 -------------------------------------
3866 -- Expand_N_Subprogram_Declaration --
3867 -------------------------------------
3869 -- If the declaration appears within a protected body, it is a private
3870 -- operation of the protected type. We must create the corresponding
3871 -- protected subprogram an associated formals. For a normal protected
3872 -- operation, this is done when expanding the protected type declaration.
3874 -- If the declaration is for a null procedure, emit null body
3876 procedure Expand_N_Subprogram_Declaration (N : Node_Id) is
3877 Loc : constant Source_Ptr := Sloc (N);
3878 Subp : constant Entity_Id := Defining_Entity (N);
3879 Scop : constant Entity_Id := Scope (Subp);
3880 Prot_Decl : Node_Id;
3882 Prot_Id : Entity_Id;
3885 -- Deal with case of protected subprogram. Do not generate protected
3886 -- operation if operation is flagged as eliminated.
3888 if Is_List_Member (N)
3889 and then Present (Parent (List_Containing (N)))
3890 and then Nkind (Parent (List_Containing (N))) = N_Protected_Body
3891 and then Is_Protected_Type (Scop)
3893 if No (Protected_Body_Subprogram (Subp))
3894 and then not Is_Eliminated (Subp)
3897 Make_Subprogram_Declaration (Loc,
3899 Build_Protected_Sub_Specification
3900 (N, Scop, Unprotected_Mode));
3902 -- The protected subprogram is declared outside of the protected
3903 -- body. Given that the body has frozen all entities so far, we
3904 -- analyze the subprogram and perform freezing actions explicitly.
3905 -- If the body is a subunit, the insertion point is before the
3906 -- stub in the parent.
3908 Prot_Bod := Parent (List_Containing (N));
3910 if Nkind (Parent (Prot_Bod)) = N_Subunit then
3911 Prot_Bod := Corresponding_Stub (Parent (Prot_Bod));
3914 Insert_Before (Prot_Bod, Prot_Decl);
3915 Prot_Id := Defining_Unit_Name (Specification (Prot_Decl));
3917 New_Scope (Scope (Scop));
3918 Analyze (Prot_Decl);
3919 Create_Extra_Formals (Prot_Id);
3920 Set_Protected_Body_Subprogram (Subp, Prot_Id);
3924 elsif Nkind (Specification (N)) = N_Procedure_Specification
3925 and then Null_Present (Specification (N))
3928 Bod : constant Node_Id :=
3929 Make_Subprogram_Body (Loc,
3931 New_Copy_Tree (Specification (N)),
3932 Declarations => New_List,
3933 Handled_Statement_Sequence =>
3934 Make_Handled_Sequence_Of_Statements (Loc,
3935 Statements => New_List (Make_Null_Statement (Loc))));
3937 Set_Body_To_Inline (N, Bod);
3938 Insert_After (N, Bod);
3941 -- Corresponding_Spec isn't being set by Analyze_Subprogram_Body,
3942 -- evidently because Set_Has_Completion is called earlier for null
3943 -- procedures in Analyze_Subprogram_Declaration, so we force its
3944 -- setting here. If the setting of Has_Completion is not set
3945 -- earlier, then it can result in missing body errors if other
3946 -- errors were already reported (since expansion is turned off).
3948 -- Should creation of the empty body be moved to the analyzer???
3950 Set_Corresponding_Spec (Bod, Defining_Entity (Specification (N)));
3953 end Expand_N_Subprogram_Declaration;
3955 ---------------------------------------
3956 -- Expand_Protected_Object_Reference --
3957 ---------------------------------------
3959 function Expand_Protected_Object_Reference
3964 Loc : constant Source_Ptr := Sloc (N);
3971 Rec := Make_Identifier (Loc, Name_uObject);
3972 Set_Etype (Rec, Corresponding_Record_Type (Scop));
3974 -- Find enclosing protected operation, and retrieve its first parameter,
3975 -- which denotes the enclosing protected object. If the enclosing
3976 -- operation is an entry, we are immediately within the protected body,
3977 -- and we can retrieve the object from the service entries procedure. A
3978 -- barrier function has has the same signature as an entry. A barrier
3979 -- function is compiled within the protected object, but unlike
3980 -- protected operations its never needs locks, so that its protected
3981 -- body subprogram points to itself.
3983 Proc := Current_Scope;
3984 while Present (Proc)
3985 and then Scope (Proc) /= Scop
3987 Proc := Scope (Proc);
3990 Corr := Protected_Body_Subprogram (Proc);
3994 -- Previous error left expansion incomplete.
3995 -- Nothing to do on this call.
4002 (First (Parameter_Specifications (Parent (Corr))));
4004 if Is_Subprogram (Proc)
4005 and then Proc /= Corr
4007 -- Protected function or procedure
4009 Set_Entity (Rec, Param);
4011 -- Rec is a reference to an entity which will not be in scope when
4012 -- the call is reanalyzed, and needs no further analysis.
4017 -- Entry or barrier function for entry body. The first parameter of
4018 -- the entry body procedure is pointer to the object. We create a
4019 -- local variable of the proper type, duplicating what is done to
4020 -- define _object later on.
4024 Obj_Ptr : constant Entity_Id := Make_Defining_Identifier (Loc,
4026 New_Internal_Name ('T'));
4030 Make_Full_Type_Declaration (Loc,
4031 Defining_Identifier => Obj_Ptr,
4033 Make_Access_To_Object_Definition (Loc,
4034 Subtype_Indication =>
4036 (Corresponding_Record_Type (Scop), Loc))));
4038 Insert_Actions (N, Decls);
4039 Insert_Actions (N, Freeze_Entity (Obj_Ptr, Sloc (N)));
4042 Make_Explicit_Dereference (Loc,
4043 Unchecked_Convert_To (Obj_Ptr,
4044 New_Occurrence_Of (Param, Loc)));
4046 -- Analyze new actual. Other actuals in calls are already analyzed
4047 -- and the list of actuals is not renalyzed after rewriting.
4049 Set_Parent (Rec, N);
4055 end Expand_Protected_Object_Reference;
4057 --------------------------------------
4058 -- Expand_Protected_Subprogram_Call --
4059 --------------------------------------
4061 procedure Expand_Protected_Subprogram_Call
4069 -- If the protected object is not an enclosing scope, this is
4070 -- an inter-object function call. Inter-object procedure
4071 -- calls are expanded by Exp_Ch9.Build_Simple_Entry_Call.
4072 -- The call is intra-object only if the subprogram being
4073 -- called is in the protected body being compiled, and if the
4074 -- protected object in the call is statically the enclosing type.
4075 -- The object may be an component of some other data structure,
4076 -- in which case this must be handled as an inter-object call.
4078 if not In_Open_Scopes (Scop)
4079 or else not Is_Entity_Name (Name (N))
4081 if Nkind (Name (N)) = N_Selected_Component then
4082 Rec := Prefix (Name (N));
4085 pragma Assert (Nkind (Name (N)) = N_Indexed_Component);
4086 Rec := Prefix (Prefix (Name (N)));
4089 Build_Protected_Subprogram_Call (N,
4090 Name => New_Occurrence_Of (Subp, Sloc (N)),
4091 Rec => Convert_Concurrent (Rec, Etype (Rec)),
4095 Rec := Expand_Protected_Object_Reference (N, Scop);
4101 Build_Protected_Subprogram_Call (N,
4110 -- If it is a function call it can appear in elaboration code and
4111 -- the called entity must be frozen here.
4113 if Ekind (Subp) = E_Function then
4114 Freeze_Expression (Name (N));
4116 end Expand_Protected_Subprogram_Call;
4118 -----------------------
4119 -- Freeze_Subprogram --
4120 -----------------------
4122 procedure Freeze_Subprogram (N : Node_Id) is
4123 Loc : constant Source_Ptr := Sloc (N);
4124 E : constant Entity_Id := Entity (N);
4126 procedure Check_Overriding_Inherited_Interfaces (E : Entity_Id);
4127 -- (Ada 2005): Check if the primitive E covers some interface already
4128 -- implemented by some ancestor of the tagged-type associated with E.
4130 procedure Register_Interface_DT_Entry
4132 Ancestor_Iface_Prim : Entity_Id := Empty);
4133 -- (Ada 2005): Register an interface primitive in a secondary dispatch
4134 -- table. If Prim overrides an ancestor primitive of its associated
4135 -- tagged-type then Ancestor_Iface_Prim indicates the entity of that
4136 -- immediate ancestor associated with the interface; otherwise Prim and
4137 -- Ancestor_Iface_Prim have the same info.
4139 procedure Register_Predefined_DT_Entry (Prim : Entity_Id);
4140 -- (Ada 2005): Register a predefined primitive in all the secondary
4141 -- dispatch tables of its primitive type.
4143 -------------------------------------------
4144 -- Check_Overriding_Inherited_Interfaces --
4145 -------------------------------------------
4147 procedure Check_Overriding_Inherited_Interfaces (E : Entity_Id) is
4150 Prim_Op : Entity_Id;
4151 Overriden_Op : Entity_Id := Empty;
4154 if Ada_Version < Ada_05
4155 or else not Is_Overriding_Operation (E)
4156 or else Is_Predefined_Dispatching_Operation (E)
4157 or else Present (Alias (E))
4162 -- Get the entity associated with this primitive operation
4164 Typ := Scope (DTC_Entity (E));
4166 exit when Etype (Typ) = Typ
4167 or else (Present (Full_View (Etype (Typ)))
4168 and then Full_View (Etype (Typ)) = Typ);
4170 -- Climb to the immediate ancestor handling private types
4172 if Present (Full_View (Etype (Typ))) then
4173 Typ := Full_View (Etype (Typ));
4178 if Present (Abstract_Interfaces (Typ)) then
4180 -- Look for the overriden subprogram in the primary dispatch
4181 -- table of the ancestor.
4183 Overriden_Op := Empty;
4184 Elmt := First_Elmt (Primitive_Operations (Typ));
4185 while Present (Elmt) loop
4186 Prim_Op := Node (Elmt);
4188 if Chars (Prim_Op) = Chars (E)
4189 and then Type_Conformant
4192 Skip_Controlling_Formals => True)
4193 and then DT_Position (Prim_Op) = DT_Position (E)
4194 and then Etype (DTC_Entity (Prim_Op)) = RTE (RE_Tag)
4195 and then not Present (Abstract_Interface_Alias (Prim_Op))
4197 if Overriden_Op = Empty then
4198 Overriden_Op := Prim_Op;
4200 -- Additional check to ensure that if two candidates have
4201 -- been found then they refer to the same subprogram.
4210 while Present (Alias (A1)) loop
4215 while Present (Alias (A2)) loop
4220 raise Program_Error;
4229 -- If not found this is the first overriding of some abstract
4232 if Overriden_Op /= Empty then
4234 -- Find the entries associated with interfaces that are
4235 -- alias of this primitive operation in the ancestor.
4237 Elmt := First_Elmt (Primitive_Operations (Typ));
4238 while Present (Elmt) loop
4239 Prim_Op := Node (Elmt);
4241 if Present (Abstract_Interface_Alias (Prim_Op))
4242 and then Alias (Prim_Op) = Overriden_Op
4244 Register_Interface_DT_Entry (E, Prim_Op);
4252 end Check_Overriding_Inherited_Interfaces;
4254 ---------------------------------
4255 -- Register_Interface_DT_Entry --
4256 ---------------------------------
4258 procedure Register_Interface_DT_Entry
4260 Ancestor_Iface_Prim : Entity_Id := Empty)
4262 Prim_Typ : Entity_Id;
4263 Prim_Op : Entity_Id;
4264 Iface_Typ : Entity_Id;
4265 Iface_DT_Ptr : Entity_Id;
4266 Iface_Tag : Entity_Id;
4267 New_Thunk : Node_Id;
4268 Thunk_Id : Entity_Id;
4271 if not Present (Ancestor_Iface_Prim) then
4272 Prim_Typ := Scope (DTC_Entity (Alias (Prim)));
4273 Iface_Typ := Scope (DTC_Entity (Abstract_Interface_Alias (Prim)));
4275 -- Generate the code of the thunk only when this primitive
4276 -- operation is associated with a secondary dispatch table.
4278 if Is_Interface (Iface_Typ) then
4279 Iface_Tag := Find_Interface_Tag
4281 Iface => Iface_Typ);
4283 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4285 Make_Defining_Identifier (Loc,
4286 Chars => New_Internal_Name ('T'));
4289 Expand_Interface_Thunk
4291 Thunk_Alias => Alias (Prim),
4292 Thunk_Id => Thunk_Id,
4293 Thunk_Tag => Iface_Tag);
4295 Insert_After (N, New_Thunk);
4300 Iface => Iface_Typ);
4302 Insert_After (New_Thunk,
4303 Fill_Secondary_DT_Entry (Sloc (Prim),
4305 Iface_DT_Ptr => Iface_DT_Ptr,
4306 Thunk_Id => Thunk_Id));
4312 Scope (DTC_Entity (Abstract_Interface_Alias
4313 (Ancestor_Iface_Prim)));
4317 (T => Scope (DTC_Entity (Alias (Ancestor_Iface_Prim))),
4318 Iface => Iface_Typ);
4320 -- Generate the thunk only if the associated tag is an interface
4321 -- tag. The case in which the associated tag is the primary tag
4322 -- occurs when a tagged type is a direct derivation of an
4323 -- interface. For example:
4325 -- type I is interface;
4327 -- type T is new I with ...
4329 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4331 Make_Defining_Identifier (Loc,
4332 Chars => New_Internal_Name ('T'));
4334 if Present (Alias (Prim)) then
4335 Prim_Op := Alias (Prim);
4341 Expand_Interface_Thunk
4342 (N => Ancestor_Iface_Prim,
4343 Thunk_Alias => Prim_Op,
4344 Thunk_Id => Thunk_Id,
4345 Thunk_Tag => Iface_Tag);
4347 Insert_After (N, New_Thunk);
4351 (T => Scope (DTC_Entity (Prim_Op)),
4352 Iface => Iface_Typ);
4354 Insert_After (New_Thunk,
4355 Fill_Secondary_DT_Entry (Sloc (Prim),
4356 Prim => Ancestor_Iface_Prim,
4357 Iface_DT_Ptr => Iface_DT_Ptr,
4358 Thunk_Id => Thunk_Id));
4361 end Register_Interface_DT_Entry;
4363 ----------------------------------
4364 -- Register_Predefined_DT_Entry --
4365 ----------------------------------
4367 procedure Register_Predefined_DT_Entry (Prim : Entity_Id) is
4368 Iface_DT_Ptr : Elmt_Id;
4369 Iface_Tag : Entity_Id;
4370 Iface_Typ : Elmt_Id;
4371 New_Thunk : Entity_Id;
4372 Prim_Typ : Entity_Id;
4373 Thunk_Id : Entity_Id;
4376 Prim_Typ := Scope (DTC_Entity (Prim));
4378 if not Present (Access_Disp_Table (Prim_Typ))
4379 or else not Present (Abstract_Interfaces (Prim_Typ))
4384 -- Skip the first acces-to-dispatch-table pointer since it leads
4385 -- to the primary dispatch table. We are only concerned with the
4386 -- secondary dispatch table pointers. Note that the access-to-
4387 -- dispatch-table pointer corresponds to the first implemented
4388 -- interface retrieved below.
4390 Iface_DT_Ptr := Next_Elmt (First_Elmt (Access_Disp_Table (Prim_Typ)));
4391 Iface_Typ := First_Elmt (Abstract_Interfaces (Prim_Typ));
4392 while Present (Iface_DT_Ptr) and then Present (Iface_Typ) loop
4393 Iface_Tag := Find_Interface_Tag (Prim_Typ, Node (Iface_Typ));
4394 pragma Assert (Present (Iface_Tag));
4396 if Etype (Iface_Tag) = RTE (RE_Interface_Tag) then
4397 Thunk_Id := Make_Defining_Identifier (Loc,
4398 New_Internal_Name ('T'));
4401 Expand_Interface_Thunk
4403 Thunk_Alias => Prim,
4404 Thunk_Id => Thunk_Id,
4405 Thunk_Tag => Iface_Tag);
4407 Insert_After (N, New_Thunk);
4408 Insert_After (New_Thunk,
4409 Make_DT_Access_Action (Node (Iface_Typ),
4410 Action => Set_Prim_Op_Address,
4412 Unchecked_Convert_To (RTE (RE_Tag),
4413 New_Reference_To (Node (Iface_DT_Ptr), Loc)),
4415 Make_Integer_Literal (Loc, DT_Position (Prim)),
4417 Make_Attribute_Reference (Loc,
4418 Prefix => New_Reference_To (Thunk_Id, Loc),
4419 Attribute_Name => Name_Address))));
4422 Next_Elmt (Iface_DT_Ptr);
4423 Next_Elmt (Iface_Typ);
4425 end Register_Predefined_DT_Entry;
4427 -- Start of processing for Freeze_Subprogram
4430 -- When a primitive is frozen, enter its name in the corresponding
4431 -- dispatch table. If the DTC_Entity field is not set this is an
4432 -- overridden primitive that can be ignored. We suppress the
4433 -- initialization of the dispatch table entry when Java_VM because
4434 -- the dispatching mechanism is handled internally by the JVM.
4436 if Is_Dispatching_Operation (E)
4437 and then not Is_Abstract (E)
4438 and then Present (DTC_Entity (E))
4439 and then not Java_VM
4440 and then not Is_CPP_Class (Scope (DTC_Entity (E)))
4442 Check_Overriding_Operation (E);
4444 if Ada_Version < Ada_05 then
4446 Fill_DT_Entry (Sloc (N), Prim => E));
4450 Typ : constant Entity_Id := Scope (DTC_Entity (E));
4453 -- There is no dispatch table associated with abstract
4454 -- interface types; each type implementing interfaces
4455 -- will fill the associated secondary DT entries.
4457 if not Is_Interface (Typ)
4458 or else Present (Alias (E))
4460 -- Ada 2005 (AI-251): Check if this entry corresponds with
4461 -- a subprogram that covers an abstract interface type.
4463 if Present (Abstract_Interface_Alias (E)) then
4464 Register_Interface_DT_Entry (E);
4466 -- Common case: Primitive subprogram
4469 -- Generate thunks for all the predefined operations
4471 if Is_Predefined_Dispatching_Operation (E) then
4472 Register_Predefined_DT_Entry (E);
4476 Fill_DT_Entry (Sloc (N), Prim => E));
4477 Check_Overriding_Inherited_Interfaces (E);
4484 -- Mark functions that return by reference. Note that it cannot be
4485 -- part of the normal semantic analysis of the spec since the
4486 -- underlying returned type may not be known yet (for private types).
4489 Typ : constant Entity_Id := Etype (E);
4490 Utyp : constant Entity_Id := Underlying_Type (Typ);
4493 if Is_Return_By_Reference_Type (Typ) then
4494 Set_Returns_By_Ref (E);
4496 elsif Present (Utyp) and then Controlled_Type (Utyp) then
4497 Set_Returns_By_Ref (E);
4500 end Freeze_Subprogram;