with Atree; use Atree;
with Einfo; use Einfo;
with Elists; use Elists;
with Errout; use Errout;
with Exp_Ch4; use Exp_Ch4;
with Exp_Ch7; use Exp_Ch7;
with Exp_Ch11; use Exp_Ch11;
with Exp_Code; use Exp_Code;
with Exp_Disp; use Exp_Disp;
with Exp_Fixd; use Exp_Fixd;
with Exp_Util; use Exp_Util;
with Freeze; use Freeze;
with Namet; use Namet;
with Nmake; use Nmake;
with Nlists; use Nlists;
with Restrict; use Restrict;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Eval; use Sem_Eval;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Sinfo; use Sinfo;
with Sinput; use Sinput;
with Snames; use Snames;
with Stand; use Stand;
with Stringt; use Stringt;
with Tbuild; use Tbuild;
with Uintp; use Uintp;
with Urealp; use Urealp;
package body Exp_Intr is
procedure Expand_Is_Negative (N : Node_Id);
procedure Expand_Dispatching_Constructor_Call (N : Node_Id);
procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id);
procedure Expand_Import_Call (N : Node_Id);
procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind);
procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id);
procedure Expand_Unc_Deallocation (N : Node_Id);
procedure Expand_To_Address (N : Node_Id);
procedure Expand_To_Pointer (N : Node_Id);
procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id);
procedure Expand_Dispatching_Constructor_Call (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Tag_Arg : constant Node_Id := First_Actual (N);
Param_Arg : constant Node_Id := Next_Actual (Tag_Arg);
Subp_Decl : constant Node_Id := Parent (Parent (Entity (Name (N))));
Inst_Pkg : constant Node_Id := Parent (Subp_Decl);
Act_Rename : Node_Id;
Act_Constr : Entity_Id;
Result_Typ : Entity_Id;
Cnstr_Call : Node_Id;
begin
Act_Rename := First (Visible_Declarations (Inst_Pkg));
while Nkind (Act_Rename) /= N_Subprogram_Renaming_Declaration loop
Next (Act_Rename);
end loop;
Act_Constr := Entity (Name (Act_Rename));
Result_Typ := Class_Wide_Type (Etype (Act_Constr));
Cnstr_Call :=
Make_Function_Call (Loc,
Name => New_Occurrence_Of (Act_Constr, Loc),
Parameter_Associations => New_List (Relocate_Node (Param_Arg)));
Set_Controlling_Argument (Cnstr_Call, Relocate_Node (Tag_Arg));
Rewrite (N, Convert_To (Result_Typ, Cnstr_Call));
Analyze_And_Resolve (N, Etype (Act_Constr));
Insert_Action (N,
Make_Implicit_If_Statement (N,
Condition =>
Make_Op_Not (Loc,
Make_DT_Access_Action (Result_Typ,
Action => CW_Membership,
Args => New_List (
Duplicate_Subexpr (Tag_Arg),
New_Reference_To (
Node (First_Elmt (Access_Disp_Table (
Root_Type (Result_Typ)))), Loc)))),
Then_Statements =>
New_List (Make_Raise_Statement (Loc,
New_Occurrence_Of (RTE (RE_Tag_Error), Loc)))));
end Expand_Dispatching_Constructor_Call;
procedure Expand_Exception_Call (N : Node_Id; Ent : RE_Id) is
Loc : constant Source_Ptr := Sloc (N);
P : Node_Id;
E : Entity_Id;
begin
P := Parent (N);
loop
if No (P) then
Rewrite (N,
Make_String_Literal (Loc,
Strval => ""));
exit;
elsif Nkind (P) = N_Exception_Handler then
if No (Choice_Parameter (P)) then
E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
Set_Choice_Parameter (P, E);
Set_Ekind (E, E_Variable);
Set_Etype (E, RTE (RE_Exception_Occurrence));
Set_Scope (E, Current_Scope);
end if;
Rewrite (N,
Make_Function_Call (Loc,
Name => New_Occurrence_Of (RTE (Ent), Loc),
Parameter_Associations => New_List (
New_Occurrence_Of (Choice_Parameter (P), Loc))));
exit;
else
P := Parent (P);
end if;
end loop;
Analyze_And_Resolve (N, Standard_String);
end Expand_Exception_Call;
procedure Expand_Import_Call (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ent : constant Entity_Id := Entity (Name (N));
Str : constant Node_Id := First_Actual (N);
Dum : Entity_Id;
begin
Dum := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Dum,
Object_Definition =>
New_Occurrence_Of (Standard_Character, Loc)),
Make_Pragma (Loc,
Chars => Name_Import,
Pragma_Argument_Associations => New_List (
Make_Pragma_Argument_Association (Loc,
Expression => Make_Identifier (Loc, Name_Ada)),
Make_Pragma_Argument_Association (Loc,
Expression => Make_Identifier (Loc, Chars (Dum))),
Make_Pragma_Argument_Association (Loc,
Chars => Name_Link_Name,
Expression => Relocate_Node (Str))))));
Rewrite (N,
Unchecked_Convert_To (Etype (Ent),
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Address,
Prefix => Make_Identifier (Loc, Chars (Dum)))));
Analyze_And_Resolve (N, Etype (Ent));
end Expand_Import_Call;
procedure Expand_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
Nam : Name_Id;
begin
if Present (Parent (E))
and then Present (Generic_Parent (Parent (E)))
then
Nam := Chars (Generic_Parent (Parent (E)));
else
Nam := Chars (E);
end if;
if Nam = Name_Asm then
Expand_Asm_Call (N);
elsif Nam = Name_Divide then
Expand_Decimal_Divide_Call (N);
elsif Nam = Name_Exception_Information then
Expand_Exception_Call (N, RE_Exception_Information);
elsif Nam = Name_Exception_Message then
Expand_Exception_Call (N, RE_Exception_Message);
elsif Nam = Name_Exception_Name then
Expand_Exception_Call (N, RE_Exception_Name_Simple);
elsif Nam = Name_Generic_Dispatching_Constructor then
Expand_Dispatching_Constructor_Call (N);
elsif Nam = Name_Import_Address
or else
Nam = Name_Import_Largest_Value
or else
Nam = Name_Import_Value
then
Expand_Import_Call (N);
elsif Nam = Name_Is_Negative then
Expand_Is_Negative (N);
elsif Nam = Name_Rotate_Left then
Expand_Shift (N, E, N_Op_Rotate_Left);
elsif Nam = Name_Rotate_Right then
Expand_Shift (N, E, N_Op_Rotate_Right);
elsif Nam = Name_Shift_Left then
Expand_Shift (N, E, N_Op_Shift_Left);
elsif Nam = Name_Shift_Right then
Expand_Shift (N, E, N_Op_Shift_Right);
elsif Nam = Name_Shift_Right_Arithmetic then
Expand_Shift (N, E, N_Op_Shift_Right_Arithmetic);
elsif Nam = Name_Unchecked_Conversion then
Expand_Unc_Conversion (N, E);
elsif Nam = Name_Unchecked_Deallocation then
Expand_Unc_Deallocation (N);
elsif Nam = Name_To_Address then
Expand_To_Address (N);
elsif Nam = Name_To_Pointer then
Expand_To_Pointer (N);
elsif Nam = Name_File
or else Nam = Name_Line
or else Nam = Name_Source_Location
or else Nam = Name_Enclosing_Entity
then
Expand_Source_Info (N, Nam);
elsif Present (Alias (E)) then
Expand_Intrinsic_Call (N, Alias (E));
else
null;
end if;
end Expand_Intrinsic_Call;
procedure Expand_Is_Negative (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Opnd : constant Node_Id := Relocate_Node (First_Actual (N));
begin
Rewrite (N,
Make_Conditional_Expression (Loc,
Expressions => New_List (
Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr (Opnd),
Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
New_Occurrence_Of (Standard_True, Loc),
Make_Conditional_Expression (Loc,
Expressions => New_List (
Make_Op_Gt (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Opnd),
Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
New_Occurrence_Of (Standard_False, Loc),
Make_Op_Ne (Loc,
Left_Opnd =>
Unchecked_Convert_To
(RTE (RE_Float_Unsigned),
Convert_To
(Standard_Float,
Duplicate_Subexpr_No_Checks (Opnd))),
Right_Opnd =>
Make_Integer_Literal (Loc, 0)))))));
Analyze_And_Resolve (N, Standard_Boolean);
end Expand_Is_Negative;
procedure Expand_Shift (N : Node_Id; E : Entity_Id; K : Node_Kind) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
Left : constant Node_Id := First_Actual (N);
Right : constant Node_Id := Next_Actual (Left);
Ltyp : constant Node_Id := Etype (Left);
Rtyp : constant Node_Id := Etype (Right);
Snode : Node_Id;
begin
Snode := New_Node (K, Loc);
Set_Left_Opnd (Snode, Relocate_Node (Left));
Set_Right_Opnd (Snode, Relocate_Node (Right));
Set_Chars (Snode, Chars (E));
Set_Etype (Snode, Base_Type (Typ));
Set_Entity (Snode, E);
if Compile_Time_Known_Value (Type_High_Bound (Rtyp))
and then Expr_Value (Type_High_Bound (Rtyp)) < Esize (Ltyp)
then
Set_Shift_Count_OK (Snode, True);
end if;
Rewrite (N, Snode);
Set_Analyzed (N);
end Expand_Shift;
procedure Expand_Source_Info (N : Node_Id; Nam : Name_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ent : Entity_Id;
procedure Write_Entity_Name (E : Entity_Id);
procedure Write_Entity_Name (E : Entity_Id) is
SDef : Source_Ptr;
TDef : constant Source_Buffer_Ptr :=
Source_Text (Get_Source_File_Index (Sloc (E)));
begin
if Scope (E) = Standard_Standard then
null;
elsif Comes_From_Source (Scope (E)) then
Write_Entity_Name (Scope (E));
Add_Char_To_Name_Buffer ('.');
elsif Is_Wrapper_Package (Scope (E)) then
Write_Entity_Name (Scope (Scope (E)));
Add_Char_To_Name_Buffer ('.');
else
null;
end if;
SDef := Sloc (E);
while TDef (SDef) in '0' .. '9'
or else TDef (SDef) >= 'A'
or else TDef (SDef) = ASCII.ESC
loop
Add_Char_To_Name_Buffer (TDef (SDef));
SDef := SDef + 1;
end loop;
end Write_Entity_Name;
begin
if Nam = Name_Line then
Rewrite (N,
Make_Integer_Literal (Loc,
Intval => UI_From_Int (Int (Get_Logical_Line_Number (Loc)))));
Analyze_And_Resolve (N, Standard_Positive);
else
case Nam is
when Name_File =>
Get_Decoded_Name_String
(Reference_Name (Get_Source_File_Index (Loc)));
when Name_Source_Location =>
Build_Location_String (Loc);
when Name_Enclosing_Entity =>
Name_Len := 0;
Ent := Current_Scope;
while Present (Ent) loop
exit when Ekind (Ent) /= E_Block
and then Ekind (Ent) /= E_Loop;
Ent := Scope (Ent);
end loop;
Name_Len := 0;
Write_Entity_Name (Ent);
when others =>
raise Program_Error;
end case;
Rewrite (N,
Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
Analyze_And_Resolve (N, Standard_String);
end if;
Set_Is_Static_Expression (N);
end Expand_Source_Info;
procedure Expand_Unc_Conversion (N : Node_Id; E : Entity_Id) is
Func : constant Entity_Id := Entity (Name (N));
Conv : Node_Id;
Ftyp : Entity_Id;
Ttyp : Entity_Id;
begin
Conv := Relocate_Node (First_Actual (N));
Ftyp := Etype (First_Formal (Func));
if Is_Scalar_Type (Ftyp) then
Conv := Convert_To (Ftyp, Conv);
Set_Parent (Conv, N);
Analyze_And_Resolve (Conv);
end if;
Ttyp := Etype (E);
if Is_Entity_Name (Conv) then
null;
elsif Nkind (Parent (Ttyp)) = N_Subtype_Declaration then
Ttyp := Entity (Subtype_Indication (Parent (Etype (E))));
elsif Is_Itype (Ttyp) then
Ttyp :=
Entity (Subtype_Indication (Associated_Node_For_Itype (Ttyp)));
else
raise Program_Error;
end if;
Rewrite (N, Unchecked_Convert_To (Ttyp, Conv));
Set_Etype (N, Ttyp);
Set_Analyzed (N);
if Nkind (N) = N_Unchecked_Type_Conversion then
Expand_N_Unchecked_Type_Conversion (N);
end if;
end Expand_Unc_Conversion;
procedure Expand_Unc_Deallocation (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Arg : constant Node_Id := First_Actual (N);
Typ : constant Entity_Id := Etype (Arg);
Stmts : constant List_Id := New_List;
Rtyp : constant Entity_Id := Underlying_Type (Root_Type (Typ));
Pool : constant Entity_Id := Associated_Storage_Pool (Rtyp);
Desig_T : constant Entity_Id := Designated_Type (Typ);
Gen_Code : Node_Id;
Free_Node : Node_Id;
Deref : Node_Id;
Free_Arg : Node_Id;
Free_Cod : List_Id;
Blk : Node_Id;
Arg_Known_Non_Null : constant Boolean := Known_Non_Null (N);
begin
if No_Pool_Assigned (Rtyp) then
Error_Msg_N ("?deallocation from empty storage pool", N);
end if;
if Known_Null (N) then
return;
end if;
if Controlled_Type (Desig_T) then
Deref :=
Make_Explicit_Dereference (Loc,
Prefix => Duplicate_Subexpr_No_Checks (Arg));
if Is_Tagged_Type (Desig_T)
and then not Is_Class_Wide_Type (Desig_T)
then
Deref := Unchecked_Convert_To (Class_Wide_Type (Desig_T), Deref);
elsif not Is_Tagged_Type (Desig_T) then
Set_Etype (Deref, Desig_T);
end if;
Free_Cod :=
Make_Final_Call
(Ref => Deref,
Typ => Desig_T,
With_Detach => New_Reference_To (Standard_True, Loc));
if Abort_Allowed then
Prepend_To (Free_Cod,
Build_Runtime_Call (Loc, RE_Abort_Defer));
Blk :=
Make_Block_Statement (Loc, Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Free_Cod,
At_End_Proc =>
New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc)));
Set_Parent (Blk, N);
Analyze (Blk);
Expand_At_End_Handler
(Handled_Statement_Sequence (Blk), Entity (Identifier (Blk)));
Append (Blk, Stmts);
Kill_Current_Values;
else
Append_List_To (Stmts, Free_Cod);
end if;
end if;
if Is_Task_Type (Desig_T) then
declare
Stat : Node_Id := Prev (N);
Nam1 : Node_Id;
Nam2 : Node_Id;
begin
while Present (Stat)
and then not Comes_From_Source (Original_Node (Stat))
loop
Prev (Stat);
end loop;
if Present (Stat)
and then Nkind (Original_Node (Stat)) = N_Abort_Statement
then
Stat := Original_Node (Stat);
Nam1 := First (Names (Stat));
Nam2 := Original_Node (First (Parameter_Associations (N)));
if Nkind (Nam1) = N_Explicit_Dereference
and then Is_Entity_Name (Prefix (Nam1))
and then Is_Entity_Name (Nam2)
and then Entity (Prefix (Nam1)) = Entity (Nam2)
then
Error_Msg_N ("abort may take time to complete?", N);
Error_Msg_N ("\deallocation might have no effect?", N);
Error_Msg_N ("\safer to wait for termination.?", N);
end if;
end if;
end;
Append_To
(Stmts, Cleanup_Task (N, Duplicate_Subexpr_No_Checks (Arg)));
elsif Has_Task (Desig_T) then
if Is_Record_Type (Desig_T) then
Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
elsif Is_Array_Type (Desig_T) then
Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
end if;
end if;
if Is_Simple_Protected_Type (Desig_T) then
Append_To (Stmts,
Cleanup_Protected_Object (N, Duplicate_Subexpr_No_Checks (Arg)));
elsif Has_Simple_Protected_Object (Desig_T) then
if Is_Record_Type (Desig_T) then
Append_List_To (Stmts, Cleanup_Record (N, Arg, Desig_T));
elsif Is_Array_Type (Desig_T) then
Append_List_To (Stmts, Cleanup_Array (N, Arg, Desig_T));
end if;
end if;
Free_Arg := Duplicate_Subexpr_No_Checks (Arg);
Free_Node := Make_Free_Statement (Loc, Empty);
Append_To (Stmts, Free_Node);
Set_Storage_Pool (Free_Node, Pool);
if Present (Pool) then
if Is_RTE (Pool, RE_SS_Pool) then
null;
elsif Is_Class_Wide_Type (Etype (Pool)) then
Set_Procedure_To_Call (Free_Node,
RTE (RE_Deallocate_Any));
else
Set_Procedure_To_Call (Free_Node,
Find_Prim_Op (Etype (Pool), Name_Deallocate));
end if;
end if;
if Present (Procedure_To_Call (Free_Node)) then
if Is_Class_Wide_Type (Desig_T)
or else
(Is_Array_Type (Desig_T)
and then not Is_Constrained (Desig_T)
and then Is_Packed (Desig_T))
then
declare
Deref : constant Node_Id :=
Make_Explicit_Dereference (Loc,
Duplicate_Subexpr_No_Checks (Arg));
D_Subtyp : Node_Id;
D_Type : Entity_Id;
begin
Set_Etype (Deref, Typ);
Set_Parent (Deref, Free_Node);
D_Subtyp := Make_Subtype_From_Expr (Deref, Desig_T);
if Nkind (D_Subtyp) in N_Has_Entity then
D_Type := Entity (D_Subtyp);
else
D_Type := Make_Defining_Identifier (Loc,
New_Internal_Name ('A'));
Insert_Action (N,
Make_Subtype_Declaration (Loc,
Defining_Identifier => D_Type,
Subtype_Indication => D_Subtyp));
Freeze_Itype (D_Type, N);
end if;
Set_Actual_Designated_Subtype (Free_Node, D_Type);
end;
end if;
end if;
Set_Expression (Free_Node, Free_Arg);
if Can_Never_Be_Null (Etype (Arg)) then
Append_To (Stmts,
Make_Raise_Constraint_Error (Loc,
Reason => CE_Access_Check_Failed));
else
declare
Lhs : constant Node_Id := Duplicate_Subexpr_No_Checks (Arg);
begin
Set_Assignment_OK (Lhs);
Append_To (Stmts,
Make_Assignment_Statement (Loc,
Name => Lhs,
Expression => Make_Null (Loc)));
end;
end if;
if Arg_Known_Non_Null then
Gen_Code :=
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stmts));
else
Gen_Code :=
Make_Implicit_If_Statement (N,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd => Duplicate_Subexpr (Arg),
Right_Opnd => Make_Null (Loc)),
Then_Statements => Stmts);
end if;
Rewrite (N, Gen_Code);
Analyze (N);
end Expand_Unc_Deallocation;
procedure Expand_To_Address (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Arg : constant Node_Id := First_Actual (N);
Obj : Node_Id;
begin
Remove_Side_Effects (Arg);
Obj := Make_Explicit_Dereference (Loc, Relocate_Node (Arg));
Rewrite (N,
Make_Conditional_Expression (Loc,
Expressions => New_List (
Make_Op_Eq (Loc,
Left_Opnd => New_Copy_Tree (Arg),
Right_Opnd => Make_Null (Loc)),
New_Occurrence_Of (RTE (RE_Null_Address), Loc),
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Address,
Prefix => Obj))));
Analyze_And_Resolve (N, RTE (RE_Address));
end Expand_To_Address;
procedure Expand_To_Pointer (N : Node_Id) is
Arg : constant Node_Id := First_Actual (N);
begin
Rewrite (N, Unchecked_Convert_To (Etype (N), Arg));
Analyze (N);
end Expand_To_Pointer;
end Exp_Intr;