with Atree; use Atree;
with Checks; use Checks;
with Einfo; use Einfo;
with Exp_Aggr; use Exp_Aggr;
with Exp_Ch7; use Exp_Ch7;
with Exp_Ch11; use Exp_Ch11;
with Exp_Dbug; use Exp_Dbug;
with Exp_Pakd; use Exp_Pakd;
with Exp_Tss; use Exp_Tss;
with Exp_Util; use Exp_Util;
with Hostparm; use Hostparm;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Restrict; use Restrict;
with Rident; use Rident;
with Rtsfind; use Rtsfind;
with Sinfo; use Sinfo;
with Sem; use Sem;
with Sem_Ch3; use Sem_Ch3;
with Sem_Ch8; use Sem_Ch8;
with Sem_Ch13; use Sem_Ch13;
with Sem_Eval; use Sem_Eval;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Snames; use Snames;
with Stand; use Stand;
with Stringt; use Stringt;
with Tbuild; use Tbuild;
with Ttypes; use Ttypes;
with Uintp; use Uintp;
with Validsw; use Validsw;
package body Exp_Ch5 is
function Change_Of_Representation (N : Node_Id) return Boolean;
procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id);
function Expand_Assign_Array_Loop
(N : Node_Id;
Larray : Entity_Id;
Rarray : Entity_Id;
L_Type : Entity_Id;
R_Type : Entity_Id;
Ndim : Pos;
Rev : Boolean) return Node_Id;
procedure Expand_Assign_Record (N : Node_Id);
function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id;
function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean;
function Change_Of_Representation (N : Node_Id) return Boolean is
Rhs : constant Node_Id := Expression (N);
begin
return
Nkind (Rhs) = N_Type_Conversion
and then
not Same_Representation (Etype (Rhs), Etype (Expression (Rhs)));
end Change_Of_Representation;
procedure Expand_Assign_Array (N : Node_Id; Rhs : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Lhs : constant Node_Id := Name (N);
Act_Lhs : constant Node_Id := Get_Referenced_Object (Lhs);
Act_Rhs : Node_Id := Get_Referenced_Object (Rhs);
L_Type : constant Entity_Id :=
Underlying_Type (Get_Actual_Subtype (Act_Lhs));
R_Type : Entity_Id :=
Underlying_Type (Get_Actual_Subtype (Act_Rhs));
L_Slice : constant Boolean := Nkind (Act_Lhs) = N_Slice;
R_Slice : constant Boolean := Nkind (Act_Rhs) = N_Slice;
Crep : constant Boolean := Change_Of_Representation (N);
Larray : Node_Id;
Rarray : Node_Id;
Ndim : constant Pos := Number_Dimensions (L_Type);
Loop_Required : Boolean := False;
procedure Apply_Dereference (Arg : in out Node_Id);
function Has_Address_Clause (Exp : Node_Id) return Boolean;
function Is_Formal_Array (Exp : Node_Id) return Boolean;
function Is_Non_Local_Array (Exp : Node_Id) return Boolean;
procedure Apply_Dereference (Arg : in out Node_Id) is
Typ : constant Entity_Id := Etype (Arg);
begin
if Is_Access_Type (Typ) then
Rewrite (Arg, Make_Explicit_Dereference (Loc,
Prefix => Relocate_Node (Arg)));
Analyze_And_Resolve (Arg, Designated_Type (Typ));
end if;
end Apply_Dereference;
function Has_Address_Clause (Exp : Node_Id) return Boolean is
begin
return
(Is_Entity_Name (Exp) and then
Present (Address_Clause (Entity (Exp))))
or else
(Nkind (Exp) = N_Slice and then Has_Address_Clause (Prefix (Exp)));
end Has_Address_Clause;
function Is_Formal_Array (Exp : Node_Id) return Boolean is
begin
return
(Is_Entity_Name (Exp) and then Is_Formal (Entity (Exp)))
or else
(Nkind (Exp) = N_Slice and then Is_Formal_Array (Prefix (Exp)));
end Is_Formal_Array;
function Is_Non_Local_Array (Exp : Node_Id) return Boolean is
begin
return (Is_Entity_Name (Exp)
and then Scope (Entity (Exp)) /= Current_Scope)
or else (Nkind (Exp) = N_Slice
and then Is_Non_Local_Array (Prefix (Exp)));
end Is_Non_Local_Array;
Lhs_Formal : constant Boolean := Is_Formal_Array (Act_Lhs);
Rhs_Formal : constant Boolean := Is_Formal_Array (Act_Rhs);
Lhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Lhs);
Rhs_Non_Local_Var : constant Boolean := Is_Non_Local_Array (Act_Rhs);
begin
Apply_Length_Check (Rhs, L_Type);
Set_Forwards_OK (N, True);
Set_Backwards_OK (N, True);
if Ndim = 1
and then not Crep
and then
((Lhs_Formal and Rhs_Formal)
or else
(Lhs_Formal and Rhs_Non_Local_Var)
or else
(Rhs_Formal and Lhs_Non_Local_Var))
and then
(not Is_Constrained (Etype (Lhs))
or else not Is_First_Subtype (Etype (Lhs)))
and then not Java_VM
then
Set_Forwards_OK (N, False);
Set_Backwards_OK (N, False);
end if;
if Crep then
Act_Rhs := Get_Referenced_Object (Rhs);
R_Type := Get_Actual_Subtype (Act_Rhs);
Loop_Required := True;
elsif Possible_Bit_Aligned_Component (Lhs)
or else
Possible_Bit_Aligned_Component (Rhs)
then
Loop_Required := True;
elsif Has_Controlled_Component (L_Type) then
Loop_Required := True;
elsif Is_Atomic_Object (Act_Lhs)
or else
Is_Atomic_Object (Act_Rhs)
then
return;
elsif Has_Atomic_Components (L_Type)
or else Has_Atomic_Components (R_Type)
or else Is_Atomic (Component_Type (L_Type))
or else Is_Atomic (Component_Type (R_Type))
then
Loop_Required := True;
elsif not L_Slice and not R_Slice then
Check_Unconstrained_Bit_Packed_Array : declare
function Is_UBPA_Reference (Opnd : Node_Id) return Boolean;
function Is_UBPA_Reference (Opnd : Node_Id) return Boolean is
Typ : constant Entity_Id := Underlying_Type (Etype (Opnd));
P_Type : Entity_Id;
Des_Type : Entity_Id;
begin
if Present (Packed_Array_Type (Typ))
and then Is_Array_Type (Packed_Array_Type (Typ))
and then not Is_Constrained (Packed_Array_Type (Typ))
then
return True;
elsif Nkind (Opnd) = N_Explicit_Dereference then
P_Type := Underlying_Type (Etype (Prefix (Opnd)));
if not Is_Access_Type (P_Type) then
return False;
else
Des_Type := Designated_Type (P_Type);
return
Is_Bit_Packed_Array (Des_Type)
and then not Is_Constrained (Des_Type);
end if;
else
return False;
end if;
end Is_UBPA_Reference;
begin
if Is_UBPA_Reference (Lhs)
or else
Is_UBPA_Reference (Rhs)
then
Loop_Required := True;
elsif Forwards_OK (N) then
return;
end if;
end Check_Unconstrained_Bit_Packed_Array;
elsif Nkind (Rhs) = N_String_Literal then
if String_Length (Strval (Rhs)) = 0
and then Is_Bit_Packed_Array (L_Type)
then
Rewrite (N, Make_Null_Statement (Loc));
Analyze (N);
end if;
return;
elsif Is_Bit_Packed_Array (L_Type)
or else Is_Bit_Packed_Array (R_Type)
or else Is_Possibly_Unaligned_Slice (Lhs)
or else Is_Possibly_Unaligned_Slice (Rhs)
then
Loop_Required := True;
elsif not (L_Slice and R_Slice) then
if Forwards_OK (N) then
return;
end if;
end if;
if Nkind (Rhs) = N_String_Literal then
declare
Temp : constant Entity_Id :=
Make_Defining_Identifier (Loc, Name_T);
Decl : Node_Id;
begin
Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier => Temp,
Object_Definition => New_Occurrence_Of (L_Type, Loc),
Expression => Relocate_Node (Rhs));
Insert_Action (N, Decl);
Rewrite (Rhs, New_Occurrence_Of (Temp, Loc));
R_Type := Etype (Temp);
end;
end if;
declare
L_Index_Typ : constant Node_Id := Etype (First_Index (L_Type));
R_Index_Typ : constant Node_Id := Etype (First_Index (R_Type));
Left_Lo : constant Node_Id := Type_Low_Bound (L_Index_Typ);
Left_Hi : constant Node_Id := Type_High_Bound (L_Index_Typ);
Right_Lo : constant Node_Id := Type_Low_Bound (R_Index_Typ);
Right_Hi : constant Node_Id := Type_High_Bound (R_Index_Typ);
Act_L_Array : Node_Id;
Act_R_Array : Node_Id;
Cleft_Lo : Node_Id;
Cright_Lo : Node_Id;
Condition : Node_Id;
Cresult : Compare_Result;
begin
if Nkind (Act_Lhs) = N_Slice then
Larray := Prefix (Act_Lhs);
else
Larray := Act_Lhs;
if Is_Private_Type (Etype (Larray)) then
Larray :=
Unchecked_Convert_To
(Underlying_Type (Etype (Larray)), Larray);
end if;
end if;
if Nkind (Act_Rhs) = N_Slice then
Rarray := Prefix (Act_Rhs);
else
Rarray := Act_Rhs;
if Is_Private_Type (Etype (Rarray)) then
Rarray :=
Unchecked_Convert_To
(Underlying_Type (Etype (Rarray)), Rarray);
end if;
end if;
if (not Crep) and L_Slice and R_Slice then
Act_L_Array := Get_Referenced_Object (Prefix (Act_Lhs));
Act_R_Array := Get_Referenced_Object (Prefix (Act_Rhs));
if Is_Entity_Name (Act_L_Array)
and then Is_Entity_Name (Act_R_Array)
and then Entity (Act_L_Array) /= Entity (Act_R_Array)
then
null;
else
Cresult := Compile_Time_Compare (Left_Lo, Right_Lo);
if Cresult = Unknown then
Cresult := Compile_Time_Compare (Left_Hi, Right_Hi);
end if;
case Cresult is
when LT | LE | EQ => Set_Backwards_OK (N, False);
when GT | GE => Set_Forwards_OK (N, False);
when NE | Unknown => Set_Backwards_OK (N, False);
Set_Forwards_OK (N, False);
end case;
end if;
end if;
if not Loop_Required then
if Forwards_OK (N) then
return;
else
null;
end if;
end if;
if Forwards_OK (N) or else Backwards_OK (N) then
if Controlled_Type (Component_Type (L_Type))
and then Base_Type (L_Type) = Base_Type (R_Type)
and then Ndim = 1
and then not No_Ctrl_Actions (N)
then
declare
Proc : constant Entity_Id :=
TSS (Base_Type (L_Type), TSS_Slice_Assign);
Actuals : List_Id;
begin
Apply_Dereference (Larray);
Apply_Dereference (Rarray);
Actuals := New_List (
Duplicate_Subexpr (Larray, Name_Req => True),
Duplicate_Subexpr (Rarray, Name_Req => True),
Duplicate_Subexpr (Left_Lo, Name_Req => True),
Duplicate_Subexpr (Left_Hi, Name_Req => True),
Duplicate_Subexpr (Right_Lo, Name_Req => True),
Duplicate_Subexpr (Right_Hi, Name_Req => True));
Append_To (Actuals,
New_Occurrence_Of (
Boolean_Literals (not Forwards_OK (N)), Loc));
Rewrite (N,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations => Actuals));
end;
else
Rewrite (N,
Expand_Assign_Array_Loop
(N, Larray, Rarray, L_Type, R_Type, Ndim,
Rev => not Forwards_OK (N)));
end if;
elsif Restriction_Active (No_Implicit_Conditionals) then
declare
T : constant Entity_Id :=
Make_Defining_Identifier (Loc, Chars => Name_T);
begin
Rewrite (N,
Make_Block_Statement (Loc,
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => T,
Constant_Present => True,
Object_Definition =>
New_Occurrence_Of (Etype (Rhs), Loc),
Expression => Relocate_Node (Rhs))),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (
Make_Assignment_Statement (Loc,
Name => Relocate_Node (Lhs),
Expression => New_Occurrence_Of (T, Loc))))));
end;
else
Ensure_Defined (L_Type, N);
Ensure_Defined (R_Type, N);
if not Is_Bit_Packed_Array (L_Type) and then not Java_VM then
Condition :=
Make_Op_Le (Loc,
Left_Opnd =>
Unchecked_Convert_To (RTE (RE_Integer_Address),
Make_Attribute_Reference (Loc,
Prefix =>
Make_Indexed_Component (Loc,
Prefix =>
Duplicate_Subexpr_Move_Checks (Larray, True),
Expressions => New_List (
Make_Attribute_Reference (Loc,
Prefix =>
New_Reference_To
(L_Index_Typ, Loc),
Attribute_Name => Name_First))),
Attribute_Name => Name_Address)),
Right_Opnd =>
Unchecked_Convert_To (RTE (RE_Integer_Address),
Make_Attribute_Reference (Loc,
Prefix =>
Make_Indexed_Component (Loc,
Prefix =>
Duplicate_Subexpr_Move_Checks (Rarray, True),
Expressions => New_List (
Make_Attribute_Reference (Loc,
Prefix =>
New_Reference_To
(R_Index_Typ, Loc),
Attribute_Name => Name_First))),
Attribute_Name => Name_Address)));
else
Cleft_Lo := New_Copy_Tree (Left_Lo);
Cright_Lo := New_Copy_Tree (Right_Lo);
Set_Analyzed (Cleft_Lo, False);
Set_Analyzed (Cright_Lo, False);
Condition :=
Make_Op_Le (Loc,
Left_Opnd => Cleft_Lo,
Right_Opnd => Cright_Lo);
end if;
if Controlled_Type (Component_Type (L_Type))
and then Base_Type (L_Type) = Base_Type (R_Type)
and then Ndim = 1
and then not No_Ctrl_Actions (N)
then
declare
Proc : constant Node_Id :=
TSS (Base_Type (L_Type), TSS_Slice_Assign);
Actuals : List_Id;
begin
Apply_Dereference (Larray);
Apply_Dereference (Rarray);
Actuals := New_List (
Duplicate_Subexpr (Larray, Name_Req => True),
Duplicate_Subexpr (Rarray, Name_Req => True),
Duplicate_Subexpr (Left_Lo, Name_Req => True),
Duplicate_Subexpr (Left_Hi, Name_Req => True),
Duplicate_Subexpr (Right_Lo, Name_Req => True),
Duplicate_Subexpr (Right_Hi, Name_Req => True));
Append_To (Actuals,
Make_Op_Not (Loc,
Right_Opnd => Condition));
Rewrite (N,
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Proc, Loc),
Parameter_Associations => Actuals));
end;
else
Rewrite (N,
Make_Implicit_If_Statement (N,
Condition => Condition,
Then_Statements => New_List (
Expand_Assign_Array_Loop
(N, Larray, Rarray, L_Type, R_Type, Ndim,
Rev => False)),
Else_Statements => New_List (
Expand_Assign_Array_Loop
(N, Larray, Rarray, L_Type, R_Type, Ndim,
Rev => True))));
end if;
end if;
Analyze (N, Suppress => All_Checks);
end;
exception
when RE_Not_Available =>
return;
end Expand_Assign_Array;
function Expand_Assign_Array_Loop
(N : Node_Id;
Larray : Entity_Id;
Rarray : Entity_Id;
L_Type : Entity_Id;
R_Type : Entity_Id;
Ndim : Pos;
Rev : Boolean) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
Lnn : array (1 .. Ndim) of Entity_Id;
Rnn : array (1 .. Ndim) of Entity_Id;
L_Index_Type : array (1 .. Ndim) of Entity_Id;
R_Index_Type : array (1 .. Ndim) of Entity_Id;
Assign : Node_Id;
F_Or_L : Name_Id;
S_Or_P : Name_Id;
begin
if Rev then
F_Or_L := Name_Last;
S_Or_P := Name_Pred;
else
F_Or_L := Name_First;
S_Or_P := Name_Succ;
end if;
declare
L_Index : Node_Id;
R_Index : Node_Id;
begin
L_Index := First_Index (L_Type);
R_Index := First_Index (R_Type);
for J in 1 .. Ndim loop
Lnn (J) :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('L'));
Rnn (J) :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('R'));
L_Index_Type (J) := Etype (L_Index);
R_Index_Type (J) := Etype (R_Index);
Next_Index (L_Index);
Next_Index (R_Index);
end loop;
end;
declare
ExprL : constant List_Id := New_List;
ExprR : constant List_Id := New_List;
begin
for J in 1 .. Ndim loop
Append_To (ExprL, New_Occurrence_Of (Lnn (J), Loc));
Append_To (ExprR, New_Occurrence_Of (Rnn (J), Loc));
end loop;
Assign :=
Make_Assignment_Statement (Loc,
Name =>
Make_Indexed_Component (Loc,
Prefix => Duplicate_Subexpr (Larray, Name_Req => True),
Expressions => ExprL),
Expression =>
Make_Indexed_Component (Loc,
Prefix => Duplicate_Subexpr (Rarray, Name_Req => True),
Expressions => ExprR));
Set_No_Ctrl_Actions (Assign, No_Ctrl_Actions (N));
end;
for J in reverse 1 .. Ndim loop
Assign :=
Make_Block_Statement (Loc,
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Rnn (J),
Object_Definition =>
New_Occurrence_Of (R_Index_Type (J), Loc),
Expression =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (R_Index_Type (J), Loc),
Attribute_Name => F_Or_L))),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (
Make_Implicit_Loop_Statement (N,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => Lnn (J),
Reverse_Present => Rev,
Discrete_Subtype_Definition =>
New_Reference_To (L_Index_Type (J), Loc))),
Statements => New_List (
Assign,
Make_Assignment_Statement (Loc,
Name => New_Occurrence_Of (Rnn (J), Loc),
Expression =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (R_Index_Type (J), Loc),
Attribute_Name => S_Or_P,
Expressions => New_List (
New_Occurrence_Of (Rnn (J), Loc)))))))));
end loop;
return Assign;
end Expand_Assign_Array_Loop;
procedure Expand_Assign_Record (N : Node_Id) is
Lhs : constant Node_Id := Name (N);
Rhs : Node_Id := Expression (N);
begin
if Change_Of_Representation (N) then
Rhs := Expression (Rhs);
elsif Possible_Bit_Aligned_Component (Lhs)
or
Possible_Bit_Aligned_Component (Rhs)
then
null;
else
return;
end if;
declare
Loc : constant Source_Ptr := Sloc (N);
R_Typ : constant Entity_Id := Base_Type (Etype (Rhs));
L_Typ : constant Entity_Id := Base_Type (Etype (Lhs));
Decl : constant Node_Id := Declaration_Node (R_Typ);
RDef : Node_Id;
F : Entity_Id;
function Find_Component
(Typ : Entity_Id;
Comp : Entity_Id) return Entity_Id;
function Make_Component_List_Assign
(CL : Node_Id;
U_U : Boolean := False) return List_Id;
function Make_Field_Assign
(C : Entity_Id;
U_U : Boolean := False) return Node_Id;
function Make_Field_Assigns (CI : List_Id) return List_Id;
function Find_Component
(Typ : Entity_Id;
Comp : Entity_Id) return Entity_Id
is
Utyp : constant Entity_Id := Underlying_Type (Typ);
C : Entity_Id;
begin
C := First_Entity (Utyp);
while Present (C) loop
if Chars (C) = Chars (Comp) then
return C;
end if;
Next_Entity (C);
end loop;
raise Program_Error;
end Find_Component;
function Make_Component_List_Assign
(CL : Node_Id;
U_U : Boolean := False) return List_Id
is
CI : constant List_Id := Component_Items (CL);
VP : constant Node_Id := Variant_Part (CL);
Alts : List_Id;
DC : Node_Id;
DCH : List_Id;
Expr : Node_Id;
Result : List_Id;
V : Node_Id;
begin
Result := Make_Field_Assigns (CI);
if Present (VP) then
V := First_Non_Pragma (Variants (VP));
Alts := New_List;
while Present (V) loop
DCH := New_List;
DC := First (Discrete_Choices (V));
while Present (DC) loop
Append_To (DCH, New_Copy_Tree (DC));
Next (DC);
end loop;
Append_To (Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => DCH,
Statements =>
Make_Component_List_Assign (Component_List (V))));
Next_Non_Pragma (V);
end loop;
if U_U then
Expr :=
New_Copy (Get_Discriminant_Value (
Entity (Name (VP)),
Etype (Rhs),
Discriminant_Constraint (Etype (Rhs))));
else
Expr :=
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr (Rhs),
Selector_Name =>
Make_Identifier (Loc, Chars (Name (VP))));
end if;
Append_To (Result,
Make_Case_Statement (Loc,
Expression => Expr,
Alternatives => Alts));
end if;
return Result;
end Make_Component_List_Assign;
function Make_Field_Assign
(C : Entity_Id;
U_U : Boolean := False) return Node_Id
is
A : Node_Id;
Expr : Node_Id;
begin
if U_U then
Expr :=
New_Copy (Get_Discriminant_Value (C,
Etype (Rhs),
Discriminant_Constraint (Etype (Rhs))));
else
Expr :=
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr (Rhs),
Selector_Name => New_Occurrence_Of (C, Loc));
end if;
A :=
Make_Assignment_Statement (Loc,
Name =>
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr (Lhs),
Selector_Name =>
New_Occurrence_Of (Find_Component (L_Typ, C), Loc)),
Expression => Expr);
Set_Assignment_OK (Name (A), True);
return A;
end Make_Field_Assign;
function Make_Field_Assigns (CI : List_Id) return List_Id is
Item : Node_Id;
Result : List_Id;
begin
Item := First (CI);
Result := New_List;
while Present (Item) loop
if Nkind (Item) = N_Component_Declaration then
Append_To
(Result, Make_Field_Assign (Defining_Identifier (Item)));
end if;
Next (Item);
end loop;
return Result;
end Make_Field_Assigns;
begin
if Has_Discriminants (L_Typ) then
F := First_Discriminant (R_Typ);
while Present (F) loop
if Is_Unchecked_Union (Base_Type (R_Typ)) then
Insert_Action (N, Make_Field_Assign (F, True));
else
Insert_Action (N, Make_Field_Assign (F));
end if;
Next_Discriminant (F);
end loop;
end if;
if Nkind (Decl) = N_Private_Type_Declaration
and then Present (Full_View (R_Typ))
then
RDef := Type_Definition (Declaration_Node (Full_View (R_Typ)));
else
RDef := Type_Definition (Decl);
end if;
if Nkind (RDef) = N_Record_Definition
and then Present (Component_List (RDef))
then
if Is_Unchecked_Union (R_Typ) then
Insert_Actions (N,
Make_Component_List_Assign (Component_List (RDef), True));
else
Insert_Actions
(N, Make_Component_List_Assign (Component_List (RDef)));
end if;
Rewrite (N, Make_Null_Statement (Loc));
end if;
end;
end Expand_Assign_Record;
procedure Expand_N_Assignment_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Lhs : constant Node_Id := Name (N);
Rhs : constant Node_Id := Expression (N);
Typ : constant Entity_Id := Underlying_Type (Etype (Lhs));
Exp : Node_Id;
begin
if Do_Range_Check (Rhs)
and then Is_Discrete_Type (Typ)
then
Set_Do_Range_Check (Rhs, False);
Generate_Range_Check (Rhs, Typ, CE_Range_Check_Failed);
end if;
if (Nkind (Lhs) = N_Indexed_Component
or else
Nkind (Lhs) = N_Selected_Component)
and then Is_Ref_To_Bit_Packed_Array (Prefix (Lhs))
then
declare
BPAR_Expr : constant Node_Id := Relocate_Node (Prefix (Lhs));
BPAR_Typ : constant Entity_Id := Etype (BPAR_Expr);
Tnn : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Insert_After (N,
Make_Assignment_Statement (Loc,
Name => New_Copy_Tree (BPAR_Expr),
Expression => New_Occurrence_Of (Tnn, Loc)));
Exp := BPAR_Expr;
loop
Set_Analyzed (Exp, False);
if Nkind (Exp) = N_Selected_Component
or else
Nkind (Exp) = N_Indexed_Component
then
Exp := Prefix (Exp);
else
exit;
end if;
end loop;
declare
Uses_Transient_Scope : constant Boolean :=
Scope_Is_Transient and then N = Node_To_Be_Wrapped;
begin
if Uses_Transient_Scope then
New_Scope (Scope (Current_Scope));
end if;
Insert_Before_And_Analyze (N,
Make_Object_Declaration (Loc,
Defining_Identifier => Tnn,
Object_Definition => New_Occurrence_Of (BPAR_Typ, Loc),
Expression => BPAR_Expr));
if Uses_Transient_Scope then
Pop_Scope;
end if;
end;
Rewrite (Prefix (Lhs),
New_Occurrence_Of (Tnn, Loc));
Set_Is_True_Constant (Tnn, False);
end;
end if;
if Is_Delayed_Aggregate (Rhs) then
Convert_Aggr_In_Assignment (N);
Rewrite (N, Make_Null_Statement (Loc));
Analyze (N);
return;
end if;
if Has_Discriminants (Etype (Lhs)) then
if not Change_Of_Representation (N) then
Apply_Discriminant_Check (Rhs, Etype (Lhs), Lhs);
end if;
elsif Is_Private_Type (Etype (Lhs))
and then Has_Discriminants (Typ)
and then Nkind (Lhs) = N_Explicit_Dereference
and then Comes_From_Source (Lhs)
then
declare
Lt : constant Entity_Id := Etype (Lhs);
begin
Set_Etype (Lhs, Typ);
Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
Apply_Discriminant_Check (Rhs, Typ, Lhs);
Set_Etype (Lhs, Lt);
end;
elsif Has_Unknown_Discriminants (Base_Type (Etype (Lhs)))
and then Has_Discriminants (Typ)
then
Rewrite (Rhs, OK_Convert_To (Base_Type (Typ), Rhs));
Apply_Discriminant_Check (Rhs, Typ, Lhs);
elsif Is_Access_Type (Etype (Lhs))
and then Is_Constrained (Designated_Type (Etype (Lhs)))
then
if Has_Discriminants (Designated_Type (Etype (Lhs))) then
if not Change_Of_Representation (N) then
Apply_Discriminant_Check (Rhs, Etype (Lhs));
end if;
elsif Is_Array_Type (Designated_Type (Etype (Lhs))) then
Apply_Range_Check (Rhs, Etype (Lhs));
if Is_Constrained (Etype (Lhs)) then
Apply_Length_Check (Rhs, Etype (Lhs));
end if;
if Nkind (Rhs) = N_Allocator then
declare
Target_Typ : constant Entity_Id := Etype (Expression (Rhs));
C_Es : Check_Result;
begin
C_Es :=
Range_Check
(Lhs,
Target_Typ,
Etype (Designated_Type (Etype (Lhs))));
Insert_Range_Checks
(C_Es,
N,
Target_Typ,
Sloc (Lhs),
Lhs);
end;
end if;
end if;
elsif Is_Access_Type (Etype (Lhs))
and then Nkind (Rhs) = N_Allocator
and then Nkind (Expression (Rhs)) = N_Qualified_Expression
then
Analyze_And_Resolve (Expression (Rhs));
Apply_Range_Check
(Expression (Rhs), Designated_Type (Etype (Lhs)));
end if;
if Is_Access_Type (Typ)
and then
((Is_Entity_Name (Lhs) and then Can_Never_Be_Null (Entity (Lhs)))
or else Can_Never_Be_Null (Etype (Lhs)))
then
Rewrite (Rhs, Convert_To (Etype (Lhs),
Relocate_Node (Rhs)));
Analyze_And_Resolve (Rhs, Etype (Lhs));
end if;
if Is_Access_Type (Typ)
and then Is_Entity_Name (Lhs)
and then Known_Non_Null (Rhs)
and then Safe_To_Capture_Value (N, Entity (Lhs))
then
Set_Is_Known_Non_Null (Entity (Lhs), Known_Non_Null (Rhs));
end if;
if Nkind (Lhs) = N_Indexed_Component
and then Is_Bit_Packed_Array (Etype (Prefix (Lhs)))
then
Expand_Bit_Packed_Element_Set (N);
return;
elsif Is_Tagged_Type (Typ)
or else (Controlled_Type (Typ) and then not Is_Array_Type (Typ))
then
Tagged_Case : declare
L : List_Id := No_List;
Expand_Ctrl_Actions : constant Boolean := not No_Ctrl_Actions (N);
begin
Remove_Side_Effects (Lhs);
Remove_Side_Effects (Rhs);
Set_Analyzed (N);
if Is_Class_Wide_Type (Typ)
or else (Is_Tagged_Type (Typ)
and then Chars (Current_Scope) /= Name_uAssign
and then Expand_Ctrl_Actions
and then not Discriminant_Checks_Suppressed (Empty))
then
declare
Op : constant Entity_Id :=
Find_Prim_Op (Typ, Name_uAssign);
F_Typ : Entity_Id := Etype (First_Formal (Op));
begin
if Is_Class_Wide_Type (Typ) then
F_Typ := Class_Wide_Type (F_Typ);
end if;
L := New_List (
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To (Op, Loc),
Parameter_Associations => New_List (
Unchecked_Convert_To (F_Typ, Duplicate_Subexpr (Lhs)),
Unchecked_Convert_To (F_Typ,
Duplicate_Subexpr (Rhs)))));
end;
else
L := Make_Tag_Ctrl_Assignment (N);
if not Statically_Different (Lhs, Rhs)
and then Expand_Ctrl_Actions
then
L := New_List (
Make_Implicit_If_Statement (N,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr (Lhs),
Attribute_Name => Name_Address),
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr (Rhs),
Attribute_Name => Name_Address)),
Then_Statements => L));
end if;
if Expand_Ctrl_Actions
and then not Restriction_Active (No_Finalization)
then
L := New_List (
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => L,
Exception_Handlers => New_List (
Make_Exception_Handler (Loc,
Exception_Choices =>
New_List (Make_Others_Choice (Loc)),
Statements => New_List (
Make_Raise_Program_Error (Loc,
Reason =>
PE_Finalize_Raised_Exception)
))))));
end if;
end if;
Rewrite (N,
Make_Block_Statement (Loc,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc, Statements => L)));
if Controlled_Type (Typ)
and then Expand_Ctrl_Actions
and then Abort_Allowed
then
declare
Blk : constant Entity_Id :=
New_Internal_Entity (
E_Block, Current_Scope, Sloc (N), 'B');
begin
Set_Scope (Blk, Current_Scope);
Set_Etype (Blk, Standard_Void_Type);
Set_Identifier (N, New_Occurrence_Of (Blk, Sloc (N)));
Prepend_To (L, Build_Runtime_Call (Loc, RE_Abort_Defer));
Set_At_End_Proc (Handled_Statement_Sequence (N),
New_Occurrence_Of (RTE (RE_Abort_Undefer_Direct), Loc));
Expand_At_End_Handler
(Handled_Statement_Sequence (N), Blk);
end;
end if;
Analyze (N);
return;
end Tagged_Case;
elsif Is_Array_Type (Typ) then
declare
Actual_Rhs : Node_Id := Rhs;
begin
while Nkind (Actual_Rhs) = N_Type_Conversion
or else
Nkind (Actual_Rhs) = N_Qualified_Expression
loop
Actual_Rhs := Expression (Actual_Rhs);
end loop;
Expand_Assign_Array (N, Actual_Rhs);
return;
end;
elsif Is_Record_Type (Typ) then
Expand_Assign_Record (N);
return;
elsif Is_Scalar_Type (Typ) then
if Expr_Known_Valid (Rhs) then
if Is_Local_Variable_Reference (Lhs)
and then not Is_Known_Valid (Entity (Lhs))
and then In_Unconditional_Context (N)
then
Set_Is_Known_Valid (Entity (Lhs), True);
end if;
else
if Validity_Checks_On
and then Validity_Check_Copies
then
Ensure_Valid (Rhs);
if Is_Local_Variable_Reference (Lhs)
and then not Is_Known_Valid (Entity (Lhs))
and then In_Unconditional_Context (N)
then
Set_Is_Known_Valid (Entity (Lhs), True);
end if;
elsif Is_Local_Variable_Reference (Lhs) then
Set_Is_Known_Valid (Entity (Lhs), False);
elsif Is_Entity_Name (Lhs)
and then Is_Known_Valid (Entity (Lhs))
then
Ensure_Valid (Rhs);
else
null;
end if;
end if;
end if;
if Validity_Checks_On
and then Validity_Check_Default
and then not Validity_Check_Subscripts
then
Check_Valid_Lvalue_Subscripts (Lhs);
end if;
exception
when RE_Not_Available =>
return;
end Expand_N_Assignment_Statement;
procedure Expand_N_Block_Statement (N : Node_Id) is
begin
Qualify_Entity_Names (N);
end Expand_N_Block_Statement;
procedure Expand_N_Case_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Expr : constant Node_Id := Expression (N);
Alt : Node_Id;
Len : Nat;
Cond : Node_Id;
Choice : Node_Id;
Chlist : List_Id;
begin
if Compile_Time_Known_Value (Expr) then
Alt := Find_Static_Alternative (N);
Insert_List_After (N, Statements (Alt));
Kill_Dead_Code (Expression (N));
Kill_Dead_Code (Alternatives (N));
Rewrite (N, Make_Null_Statement (Loc));
return;
end if;
declare
Last_Alt : constant Node_Id := Last (Alternatives (N));
Others_Present : Boolean;
Others_Node : Node_Id;
Then_Stms : List_Id;
Else_Stms : List_Id;
begin
if Nkind (First (Discrete_Choices (Last_Alt))) = N_Others_Choice then
Others_Present := True;
Others_Node := Last_Alt;
else
Others_Present := False;
end if;
if Validity_Check_Default then
Ensure_Valid (Expr);
end if;
Len := List_Length (Alternatives (N));
if Len = 1 then
Remove_Side_Effects (Expression (N));
Insert_List_After (N, Statements (First (Alternatives (N))));
Kill_Dead_Code (Expression (N));
Rewrite (N, Make_Null_Statement (Loc));
return;
end if;
if Len = 2 then
Chlist := Discrete_Choices (First (Alternatives (N)));
if List_Length (Chlist) = 1 then
Choice := First (Chlist);
Then_Stms := Statements (First (Alternatives (N)));
Else_Stms := Statements (Last (Alternatives (N)));
if Nkind (Choice) = N_Identifier
and then Entity (Choice) = Standard_True
then
Cond := Expression (N);
elsif Nkind (Choice) = N_Identifier
and then Entity (Choice) = Standard_False
then
Cond := Expression (N);
Else_Stms := Statements (First (Alternatives (N)));
Then_Stms := Statements (Last (Alternatives (N)));
elsif Nkind (Choice) = N_Range
or else (Nkind (Choice) = N_Attribute_Reference
and then Attribute_Name (Choice) = Name_Range)
or else (Is_Entity_Name (Choice)
and then Is_Type (Entity (Choice)))
or else Nkind (Choice) = N_Subtype_Indication
then
Cond :=
Make_In (Loc,
Left_Opnd => Expression (N),
Right_Opnd => Relocate_Node (Choice));
else
Cond :=
Make_Op_Eq (Loc,
Left_Opnd => Expression (N),
Right_Opnd => Relocate_Node (Choice));
end if;
Rewrite (N,
Make_If_Statement (Loc,
Condition => Cond,
Then_Statements => Then_Stms,
Else_Statements => Else_Stms));
Analyze (N);
return;
end if;
end if;
if not Others_Present then
Others_Node := Make_Others_Choice (Sloc (Last_Alt));
Set_Others_Discrete_Choices
(Others_Node, Discrete_Choices (Last_Alt));
Set_Discrete_Choices (Last_Alt, New_List (Others_Node));
end if;
end;
end Expand_N_Case_Statement;
procedure Expand_N_Exit_Statement (N : Node_Id) is
begin
Adjust_Condition (Condition (N));
end Expand_N_Exit_Statement;
procedure Expand_N_Goto_Statement (N : Node_Id) is
begin
Generate_Poll_Call (N);
end Expand_N_Goto_Statement;
procedure Expand_N_If_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Hed : Node_Id;
E : Node_Id;
New_If : Node_Id;
begin
Adjust_Condition (Condition (N));
while Compile_Time_Known_Value (Condition (N)) loop
if Is_True (Expr_Value (Condition (N))) then
Kill_Dead_Code (Elsif_Parts (N));
Kill_Dead_Code (Else_Statements (N));
Hed := Remove_Head (Then_Statements (N));
Insert_List_After (N, Then_Statements (N));
Rewrite (N, Hed);
return;
else
if not Constant_Condition_Warnings then
Kill_Dead_Code (Condition (N));
end if;
Kill_Dead_Code (Then_Statements (N));
if No (Elsif_Parts (N)) then
if No (Else_Statements (N))
or else Is_Empty_List (Else_Statements (N))
then
Rewrite (N,
Make_Null_Statement (Sloc (N)));
else
Hed := Remove_Head (Else_Statements (N));
Insert_List_After (N, Else_Statements (N));
Rewrite (N, Hed);
end if;
return;
else
Hed := Remove_Head (Elsif_Parts (N));
Insert_Actions (N, Condition_Actions (Hed));
Set_Condition (N, Condition (Hed));
Set_Then_Statements (N, Then_Statements (Hed));
if Is_Empty_List (Elsif_Parts (N)) then
Set_Elsif_Parts (N, No_List);
end if;
end if;
end if;
end loop;
if Present (Elsif_Parts (N)) then
E := First (Elsif_Parts (N));
while Present (E) loop
Adjust_Condition (Condition (E));
if Present (Condition_Actions (E))
or else Compile_Time_Known_Value (Condition (E))
then
New_If :=
Make_If_Statement (Sloc (E),
Condition => Condition (E),
Then_Statements => Then_Statements (E),
Elsif_Parts => No_List,
Else_Statements => Else_Statements (N));
while Present (Next (E)) loop
if No (Elsif_Parts (New_If)) then
Set_Elsif_Parts (New_If, New_List);
end if;
Append (Remove_Next (E), Elsif_Parts (New_If));
end loop;
Set_Else_Statements (N, New_List (New_If));
if Present (Condition_Actions (E)) then
Insert_List_Before (New_If, Condition_Actions (E));
end if;
Remove (E);
if Is_Empty_List (Elsif_Parts (N)) then
Set_Elsif_Parts (N, No_List);
end if;
Analyze (New_If);
return;
else
Next (E);
end if;
end loop;
end if;
if Nkind (N) /= N_If_Statement then
return;
end if;
if Nkind (N) = N_If_Statement
and then No (Elsif_Parts (N))
and then Present (Else_Statements (N))
and then List_Length (Then_Statements (N)) = 1
and then List_Length (Else_Statements (N)) = 1
then
declare
Then_Stm : constant Node_Id := First (Then_Statements (N));
Else_Stm : constant Node_Id := First (Else_Statements (N));
begin
if Nkind (Then_Stm) = N_Return_Statement
and then
Nkind (Else_Stm) = N_Return_Statement
then
declare
Then_Expr : constant Node_Id := Expression (Then_Stm);
Else_Expr : constant Node_Id := Expression (Else_Stm);
begin
if Nkind (Then_Expr) = N_Identifier
and then
Nkind (Else_Expr) = N_Identifier
then
if Entity (Then_Expr) = Standard_True
and then Entity (Else_Expr) = Standard_False
then
Rewrite (N,
Make_Return_Statement (Loc,
Expression => Relocate_Node (Condition (N))));
Analyze (N);
return;
elsif Entity (Then_Expr) = Standard_False
and then Entity (Else_Expr) = Standard_True
then
Rewrite (N,
Make_Return_Statement (Loc,
Expression =>
Make_Op_Not (Loc,
Right_Opnd => Relocate_Node (Condition (N)))));
Analyze (N);
return;
end if;
end if;
end;
end if;
end;
end if;
end Expand_N_If_Statement;
procedure Expand_N_Loop_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Isc : constant Node_Id := Iteration_Scheme (N);
begin
if Present (Isc) then
Adjust_Condition (Condition (Isc));
end if;
if Is_Non_Empty_List (Statements (N)) then
Generate_Poll_Call (First (Statements (N)));
end if;
if No (Isc) then
return;
end if;
if Present (Loop_Parameter_Specification (Isc)) then
declare
LPS : constant Node_Id := Loop_Parameter_Specification (Isc);
Loop_Id : constant Entity_Id := Defining_Identifier (LPS);
Ltype : constant Entity_Id := Etype (Loop_Id);
Btype : constant Entity_Id := Base_Type (Ltype);
Expr : Node_Id;
New_Id : Entity_Id;
begin
if not Is_Enumeration_Type (Btype)
or else No (Enum_Pos_To_Rep (Btype))
then
return;
end if;
New_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name (Chars (Loop_Id), 'P'));
if Has_Contiguous_Rep (Btype) then
Expr :=
Unchecked_Convert_To (Btype,
Make_Op_Add (Loc,
Left_Opnd =>
Make_Integer_Literal (Loc,
Enumeration_Rep (First_Literal (Btype))),
Right_Opnd => New_Reference_To (New_Id, Loc)));
else
Expr :=
Make_Indexed_Component (Loc,
Prefix => New_Reference_To (Enum_Pos_To_Rep (Btype), Loc),
Expressions => New_List (New_Reference_To (New_Id, Loc)));
end if;
Rewrite (N,
Make_Loop_Statement (Loc,
Identifier => Identifier (N),
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier => New_Id,
Reverse_Present => Reverse_Present (LPS),
Discrete_Subtype_Definition =>
Make_Subtype_Indication (Loc,
Subtype_Mark =>
New_Reference_To (Standard_Natural, Loc),
Constraint =>
Make_Range_Constraint (Loc,
Range_Expression =>
Make_Range (Loc,
Low_Bound =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Reference_To (Btype, Loc),
Attribute_Name => Name_Pos,
Expressions => New_List (
Relocate_Node
(Type_Low_Bound (Ltype)))),
High_Bound =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Reference_To (Btype, Loc),
Attribute_Name => Name_Pos,
Expressions => New_List (
Relocate_Node
(Type_High_Bound (Ltype))))))))),
Statements => New_List (
Make_Block_Statement (Loc,
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Loop_Id,
Constant_Present => True,
Object_Definition => New_Reference_To (Ltype, Loc),
Expression => Expr)),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Statements (N)))),
End_Label => End_Label (N)));
Analyze (N);
end;
elsif Present (Isc)
and then Present (Condition_Actions (Isc))
then
declare
ES : Node_Id;
begin
ES :=
Make_Exit_Statement (Sloc (Condition (Isc)),
Condition =>
Make_Op_Not (Sloc (Condition (Isc)),
Right_Opnd => Condition (Isc)));
Prepend (ES, Statements (N));
Insert_List_Before (ES, Condition_Actions (Isc));
Rewrite (N,
Make_Loop_Statement (Sloc (N),
Identifier => Identifier (N),
Statements => Statements (N),
End_Label => End_Label (N)));
Analyze (N);
end;
end if;
end Expand_N_Loop_Statement;
procedure Expand_N_Return_Statement (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Exp : constant Node_Id := Expression (N);
Exptyp : Entity_Id;
T : Entity_Id;
Utyp : Entity_Id;
Scope_Id : Entity_Id;
Kind : Entity_Kind;
Call : Node_Id;
Acc_Stat : Node_Id;
Goto_Stat : Node_Id;
Lab_Node : Node_Id;
Cur_Idx : Nat;
Return_Type : Entity_Id;
Result_Exp : Node_Id;
Result_Id : Entity_Id;
Result_Obj : Node_Id;
begin
if Present (Exp) then
Exptyp := Etype (Exp);
if Is_Boolean_Type (Exptyp)
and then Nonzero_Is_True (Exptyp)
then
Adjust_Condition (Exp);
Adjust_Result_Type (Exp, Exptyp);
end if;
if Validity_Checks_On
and then Validity_Check_Returns
then
Ensure_Valid (Exp);
end if;
end if;
Cur_Idx := Scope_Stack.Last;
loop
Scope_Id := Scope_Stack.Table (Cur_Idx).Entity;
if Ekind (Scope_Id) /= E_Block
and then Ekind (Scope_Id) /= E_Loop
then
exit;
else
Cur_Idx := Cur_Idx - 1;
pragma Assert (Cur_Idx >= 0);
end if;
end loop;
if No (Exp) then
Kind := Ekind (Scope_Id);
if Kind = E_Procedure or else Kind = E_Generic_Procedure then
return;
end if;
pragma Assert (Is_Entry (Scope_Id));
for J in reverse 0 .. Cur_Idx loop
Scope_Id := Scope_Stack.Table (J).Entity;
exit when Is_Concurrent_Type (Scope_Id);
end loop;
if Is_Task_Type (Scope_Id) then
Call := (Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To
(RTE (RE_Complete_Rendezvous), Loc)));
Insert_Before (N, Call);
Analyze (Call);
Acc_Stat := Parent (N);
while Nkind (Acc_Stat) /= N_Accept_Statement loop
Acc_Stat := Parent (Acc_Stat);
end loop;
Lab_Node := Last (Statements
(Handled_Statement_Sequence (Acc_Stat)));
Goto_Stat := Make_Goto_Statement (Loc,
Name => New_Occurrence_Of
(Entity (Identifier (Lab_Node)), Loc));
Set_Analyzed (Goto_Stat);
Rewrite (N, Goto_Stat);
Analyze (N);
elsif Is_Protected_Type (Scope_Id) then
Call :=
Make_Procedure_Call_Statement (Loc,
Name => New_Reference_To
(RTE (RE_Complete_Entry_Body), Loc),
Parameter_Associations => New_List
(Make_Attribute_Reference (Loc,
Prefix =>
New_Reference_To
(Object_Ref
(Corresponding_Body (Parent (Scope_Id))),
Loc),
Attribute_Name => Name_Unchecked_Access)));
Insert_Before (N, Call);
Analyze (Call);
end if;
return;
end if;
T := Etype (Exp);
Return_Type := Etype (Scope_Id);
Utyp := Underlying_Type (Return_Type);
if Is_Scalar_Type (T) then
Rewrite (Exp, Convert_To (Return_Type, Exp));
Analyze (Exp);
end if;
if Is_Tagged_Type (Utyp)
and then not Is_Class_Wide_Type (Utyp)
and then (Nkind (Exp) = N_Type_Conversion
or else Nkind (Exp) = N_Unchecked_Type_Conversion
or else (Is_Entity_Name (Exp)
and then Ekind (Entity (Exp)) in Formal_Kind))
then
if Is_Limited_Type (Return_Type) then
Insert_Action (Exp,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd =>
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr (Exp),
Selector_Name =>
New_Reference_To (Tag_Component (Utyp), Loc)),
Right_Opnd =>
Unchecked_Convert_To (RTE (RE_Tag),
New_Reference_To
(Access_Disp_Table (Base_Type (Utyp)), Loc))),
Reason => CE_Tag_Check_Failed));
else
Result_Id :=
Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
Result_Exp := New_Reference_To (Result_Id, Loc);
Result_Obj :=
Make_Object_Declaration (Loc,
Defining_Identifier => Result_Id,
Object_Definition => New_Reference_To (Return_Type, Loc),
Constant_Present => True,
Expression => Relocate_Node (Exp));
Set_Assignment_OK (Result_Obj);
Insert_Action (Exp, Result_Obj);
Rewrite (Exp, Result_Exp);
Analyze_And_Resolve (Exp, Return_Type);
end if;
end if;
if Is_Return_By_Reference_Type (T)
or else not Requires_Transient_Scope (Return_Type)
then
null;
elsif Function_Returns_With_DSP (Scope_Id) then
No_Secondary_Stack_Case : declare
Local_Copy_Required : Boolean := False;
Copy_Ent : Entity_Id;
Decl : Node_Id;
procedure Test_Copy_Required (Expr : Node_Id);
procedure Test_Copy_Required (Expr : Node_Id) is
Ent : Entity_Id;
begin
if Nkind (Expr) = N_Indexed_Component
or else
Nkind (Expr) = N_Selected_Component
then
Test_Copy_Required (Prefix (Expr));
return;
elsif Is_Entity_Name (Expr) then
Ent := Entity (Expr);
if Ekind (Ent) = E_Constant then
return;
elsif Ekind (Ent) = E_Variable
and then Scope (Ent) = Current_Subprogram
then
return;
end if;
end if;
Local_Copy_Required := True;
end Test_Copy_Required;
begin
if Requires_Transient_Scope (T)
and then
(not Is_Array_Type (T)
or else Is_Constrained (T) = Is_Constrained (Return_Type)
or else Controlled_Type (T))
and then Nkind (Exp) = N_Function_Call
then
Set_By_Ref (N);
elsif Controlled_Type (Utyp) then
Copy_Ent :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('R'));
Decl :=
Make_Object_Declaration (Loc,
Defining_Identifier => Copy_Ent,
Object_Definition => New_Occurrence_Of (Return_Type, Loc),
Expression => Relocate_Node (Exp));
Set_Assignment_OK (Decl);
Set_Delay_Finalize_Attach (Decl);
Insert_Action (N, Decl);
Rewrite (Exp, New_Occurrence_Of (Copy_Ent, Loc));
Analyze_And_Resolve (Exp, Return_Type);
Set_By_Ref (N);
else
Test_Copy_Required (Exp);
if not Local_Copy_Required then
Set_By_Ref (N);
end if;
end if;
end No_Secondary_Stack_Case;
else
declare
S : Entity_Id := Current_Scope;
begin
while Ekind (S) = E_Block or else Ekind (S) = E_Loop loop
Set_Sec_Stack_Needed_For_Return (S, True);
S := Enclosing_Dynamic_Scope (S);
end loop;
end;
if Requires_Transient_Scope (T)
and then
(not Is_Array_Type (T)
or else Is_Constrained (T) = Is_Constrained (Return_Type)
or else Controlled_Type (T))
and then Nkind (Exp) = N_Function_Call
then
Set_By_Ref (N);
elsif Controlled_Type (Utyp) then
declare
Loc : constant Source_Ptr := Sloc (N);
Temp : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('R'));
Acc_Typ : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('A'));
Alloc_Node : Node_Id;
begin
Set_Ekind (Acc_Typ, E_Access_Type);
Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
Alloc_Node :=
Make_Allocator (Loc,
Expression =>
Make_Qualified_Expression (Loc,
Subtype_Mark => New_Reference_To (Etype (Exp), Loc),
Expression => Relocate_Node (Exp)));
Insert_List_Before_And_Analyze (N, New_List (
Make_Full_Type_Declaration (Loc,
Defining_Identifier => Acc_Typ,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
Subtype_Indication =>
New_Reference_To (Return_Type, Loc))),
Make_Object_Declaration (Loc,
Defining_Identifier => Temp,
Object_Definition => New_Reference_To (Acc_Typ, Loc),
Expression => Alloc_Node)));
Rewrite (Exp,
Make_Explicit_Dereference (Loc,
Prefix => New_Reference_To (Temp, Loc)));
Analyze_And_Resolve (Exp, Return_Type);
end;
else
Set_Storage_Pool (N, RTE (RE_SS_Pool));
if not Java_VM then
Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
end if;
end if;
end if;
exception
when RE_Not_Available =>
return;
end Expand_N_Return_Statement;
function Make_Tag_Ctrl_Assignment (N : Node_Id) return List_Id is
Loc : constant Source_Ptr := Sloc (N);
L : constant Node_Id := Name (N);
T : constant Entity_Id := Underlying_Type (Etype (L));
Ctrl_Act : constant Boolean := Controlled_Type (T)
and then not No_Ctrl_Actions (N);
Save_Tag : constant Boolean := Is_Tagged_Type (T)
and then not No_Ctrl_Actions (N)
and then not Java_VM;
Res : List_Id;
Tag_Tmp : Entity_Id;
begin
Res := New_List;
if not Ctrl_Act then
null;
elsif Nkind (L) = N_Type_Conversion
and then Is_Entity_Name (Expression (L))
and then No_Initialization (Parent (Entity (Expression (L))))
then
null;
else
Append_List_To (Res,
Make_Final_Call (
Ref => Duplicate_Subexpr_No_Checks (L),
Typ => Etype (L),
With_Detach => New_Reference_To (Standard_False, Loc)));
end if;
if Save_Tag then
Tag_Tmp :=
Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
Append_To (Res,
Make_Object_Declaration (Loc,
Defining_Identifier => Tag_Tmp,
Object_Definition => New_Reference_To (RTE (RE_Tag), Loc),
Expression =>
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (L),
Selector_Name => New_Reference_To (Tag_Component (T), Loc))));
else
Tag_Tmp := Empty;
end if;
if Ctrl_Act then
Controlled_Actions : declare
Prev_Ref : Node_Id;
First_After_Root : Node_Id := Empty;
Last_Before_Hole : Node_Id := Empty;
Hole_Length : Node_Id := Empty;
First_After_Hole : Node_Id := Empty;
Expr, Source_Size : Node_Id;
Range_Type : Entity_Id;
Opaque_Type : Entity_Id;
function Build_Slice
(Rec : Entity_Id;
Lo : Node_Id;
Hi : Node_Id) return Node_Id;
function Build_Slice
(Rec : Node_Id;
Lo : Node_Id;
Hi : Node_Id) return Node_Id
is
Lo_Bound : Node_Id;
Hi_Bound : Node_Id;
Opaque : constant Node_Id :=
Unchecked_Convert_To (Opaque_Type,
Make_Attribute_Reference (Loc,
Prefix => Rec,
Attribute_Name => Name_Address));
begin
if No (Lo) then
Lo_Bound := Make_Integer_Literal (Loc, 1);
else
Lo_Bound := Lo;
end if;
if No (Hi) then
Hi_Bound := Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Range_Type, Loc),
Attribute_Name => Name_Last);
else
Hi_Bound := Hi;
end if;
return Make_Slice (Loc,
Prefix =>
Opaque,
Discrete_Range => Make_Range (Loc,
Lo_Bound, Hi_Bound));
end Build_Slice;
begin
Expr := Duplicate_Subexpr_No_Checks (Expression (N));
if Nkind (Expr) = N_Qualified_Expression then
Expr := Expression (Expr);
end if;
Source_Size :=
Make_Op_Add (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix =>
Expr,
Attribute_Name =>
Name_Size),
Right_Opnd =>
Make_Integer_Literal (Loc,
System_Storage_Unit - 1));
Set_Comes_From_Source (Prefix (Left_Opnd (Source_Size)), False);
Source_Size :=
Make_Op_Divide (Loc,
Left_Opnd => Source_Size,
Right_Opnd =>
Make_Integer_Literal (Loc,
Intval => System_Storage_Unit));
Range_Type :=
Make_Defining_Identifier (Loc,
New_Internal_Name ('G'));
Append_To (Res,
Make_Subtype_Declaration (Loc,
Defining_Identifier => Range_Type,
Subtype_Indication =>
Make_Subtype_Indication (Loc,
Subtype_Mark =>
New_Reference_To (RTE (RE_Storage_Offset), Loc),
Constraint => Make_Range_Constraint (Loc,
Range_Expression =>
Make_Range (Loc,
Low_Bound => Make_Integer_Literal (Loc, 1),
High_Bound => Source_Size)))));
Append_To (Res,
Make_Subtype_Declaration (Loc,
Defining_Identifier =>
Make_Defining_Identifier (Loc,
New_Internal_Name ('S')),
Subtype_Indication =>
Make_Subtype_Indication (Loc,
Subtype_Mark =>
New_Reference_To (RTE (RE_Storage_Array), Loc),
Constraint =>
Make_Index_Or_Discriminant_Constraint (Loc,
Constraints =>
New_List (New_Reference_To (Range_Type, Loc))))));
Opaque_Type :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('A'));
Append_To (Res,
Make_Full_Type_Declaration (Loc,
Defining_Identifier => Opaque_Type,
Type_Definition =>
Make_Access_To_Object_Definition (Loc,
Subtype_Indication =>
New_Occurrence_Of (
Defining_Identifier (Last (Res)), Loc))));
First_After_Root := Make_Integer_Literal (Loc, 1);
if Is_Controlled (T) then
First_After_Root :=
Make_Op_Add (Loc,
First_After_Root,
Make_Op_Divide (Loc,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (RTE (RE_Root_Controlled), Loc),
Attribute_Name => Name_Size),
Make_Integer_Literal (Loc, System_Storage_Unit)));
end if;
if Has_Controlled_Component (T) then
Prev_Ref :=
Make_Selected_Component (Loc,
Prefix =>
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (L),
Selector_Name =>
New_Reference_To (Controller_Component (T), Loc)),
Selector_Name => Make_Identifier (Loc, Name_Prev));
Last_Before_Hole :=
Make_Defining_Identifier (Loc,
New_Internal_Name ('L'));
Append_To (Res,
Make_Object_Declaration (Loc,
Defining_Identifier => Last_Before_Hole,
Object_Definition => New_Occurrence_Of (
RTE (RE_Storage_Offset), Loc),
Constant_Present => True,
Expression => Make_Op_Add (Loc,
Make_Attribute_Reference (Loc,
Prefix => Prev_Ref,
Attribute_Name => Name_Position),
Make_Attribute_Reference (Loc,
Prefix => New_Copy_Tree (Prefix (Prev_Ref)),
Attribute_Name => Name_Position))));
Hole_Length :=
Make_Op_Multiply (Loc,
Left_Opnd => Make_Integer_Literal (Loc, Uint_2),
Right_Opnd =>
Make_Op_Divide (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Copy_Tree (Prev_Ref),
Attribute_Name => Name_Size),
Right_Opnd =>
Make_Integer_Literal (Loc,
Intval => System_Storage_Unit)));
First_After_Hole :=
Make_Defining_Identifier (Loc,
New_Internal_Name ('F'));
Append_To (Res,
Make_Object_Declaration (Loc,
Defining_Identifier => First_After_Hole,
Object_Definition => New_Occurrence_Of (
RTE (RE_Storage_Offset), Loc),
Constant_Present => True,
Expression =>
Make_Op_Add (Loc,
Left_Opnd =>
Make_Op_Add (Loc,
Left_Opnd =>
New_Occurrence_Of (Last_Before_Hole, Loc),
Right_Opnd => Hole_Length),
Right_Opnd => Make_Integer_Literal (Loc, 1))));
Last_Before_Hole := New_Occurrence_Of (Last_Before_Hole, Loc);
First_After_Hole := New_Occurrence_Of (First_After_Hole, Loc);
end if;
Append_To (Res, Make_Assignment_Statement (Loc,
Name => Build_Slice (
Rec => Duplicate_Subexpr_No_Checks (L),
Lo => First_After_Root,
Hi => Last_Before_Hole),
Expression => Build_Slice (
Rec => Expression (N),
Lo => First_After_Root,
Hi => New_Copy_Tree (Last_Before_Hole))));
if Present (First_After_Hole) then
Append_To (Res, Make_Assignment_Statement (Loc,
Name => Build_Slice (
Rec => Duplicate_Subexpr_No_Checks (L),
Lo => First_After_Hole,
Hi => Empty),
Expression => Build_Slice (
Rec => Duplicate_Subexpr_No_Checks (Expression (N)),
Lo => New_Copy_Tree (First_After_Hole),
Hi => Empty)));
end if;
end Controlled_Actions;
else
Append_To (Res, Relocate_Node (N));
end if;
if Save_Tag then
Append_To (Res,
Make_Assignment_Statement (Loc,
Name =>
Make_Selected_Component (Loc,
Prefix => Duplicate_Subexpr_No_Checks (L),
Selector_Name => New_Reference_To (Tag_Component (T), Loc)),
Expression => New_Reference_To (Tag_Tmp, Loc)));
end if;
if Ctrl_Act then
Append_List_To (Res,
Make_Adjust_Call (
Ref => Duplicate_Subexpr_Move_Checks (L),
Typ => Etype (L),
Flist_Ref => New_Reference_To (RTE (RE_Global_Final_List), Loc),
With_Attach => Make_Integer_Literal (Loc, 0)));
end if;
return Res;
exception
when RE_Not_Available =>
return Empty_List;
end Make_Tag_Ctrl_Assignment;
function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
begin
case Nkind (N) is
when N_Indexed_Component =>
declare
P : constant Node_Id := Prefix (N);
Ptyp : constant Entity_Id := Etype (P);
begin
if Known_Static_Component_Size (Ptyp)
and then Component_Size (Ptyp) <= 64
then
return False;
else
return Possible_Bit_Aligned_Component (P);
end if;
end;
when N_Selected_Component =>
declare
P : constant Node_Id := Prefix (N);
Comp : constant Entity_Id := Entity (Selector_Name (N));
begin
if Component_May_Be_Bit_Aligned (Comp) then
return True;
else
return Possible_Bit_Aligned_Component (P);
end if;
end;
when others =>
return False;
end case;
end Possible_Bit_Aligned_Component;
end Exp_Ch5;