with Atree; use Atree;
with Debug; use Debug;
with Einfo; use Einfo;
with Errout; use Errout;
with Exp_Ch2; use Exp_Ch2;
with Exp_Pakd; use Exp_Pakd;
with Exp_Util; use Exp_Util;
with Elists; use Elists;
with Eval_Fat; use Eval_Fat;
with Freeze; use Freeze;
with Lib; use Lib;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Output; use Output;
with Restrict; use Restrict;
with Rident; use Rident;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Eval; use Sem_Eval;
with Sem_Ch3; use Sem_Ch3;
with Sem_Ch8; use Sem_Ch8;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Sem_Warn; use Sem_Warn;
with Sinfo; use Sinfo;
with Sinput; use Sinput;
with Snames; use Snames;
with Sprint; use Sprint;
with Stand; use Stand;
with Targparm; use Targparm;
with Tbuild; use Tbuild;
with Ttypes; use Ttypes;
with Urealp; use Urealp;
with Validsw; use Validsw;
package body Checks is
type Saved_Check is record
Killed : Boolean;
Entity : Entity_Id;
Offset : Uint;
Check_Type : Character;
Target_Type : Entity_Id;
end record;
Saved_Checks : array (Int range 1 .. 200) of Saved_Check;
Num_Saved_Checks : Nat := 0;
Saved_Checks_Stack : array (Int range 1 .. 100) of Nat;
Saved_Checks_TOS : Nat := 0;
procedure Apply_Float_Conversion_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id);
procedure Apply_Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean);
procedure Apply_Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean);
type Check_Type is (Access_Check, Division_Check);
function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean;
procedure Find_Check
(Expr : Node_Id;
Check_Type : Character;
Target_Type : Entity_Id;
Entry_OK : out Boolean;
Check_Num : out Nat;
Ent : out Entity_Id;
Ofs : out Uint);
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
function Guard_Access
(Cond : Node_Id;
Loc : Source_Ptr;
Ck_Node : Node_Id) return Node_Id;
procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
function Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id) return Check_Result;
function Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id) return Check_Result;
function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Access_Check);
else
return Scope_Suppress (Access_Check);
end if;
end Access_Checks_Suppressed;
function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Accessibility_Check);
else
return Scope_Suppress (Accessibility_Check);
end if;
end Accessibility_Checks_Suppressed;
procedure Append_Range_Checks
(Checks : Check_Result;
Stmts : List_Id;
Suppress_Typ : Entity_Id;
Static_Sloc : Source_Ptr;
Flag_Node : Node_Id)
is
Internal_Flag_Node : constant Node_Id := Flag_Node;
Internal_Static_Sloc : constant Source_Ptr := Static_Sloc;
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Suppress_Typ))
or else
(not Range_Checks_Suppressed (Suppress_Typ));
begin
if not Checks_On then
return;
end if;
for J in 1 .. 2 loop
exit when No (Checks (J));
if Nkind (Checks (J)) = N_Raise_Constraint_Error
and then Present (Condition (Checks (J)))
then
if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
Append_To (Stmts, Checks (J));
Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
end if;
else
Append_To
(Stmts,
Make_Raise_Constraint_Error (Internal_Static_Sloc,
Reason => CE_Range_Check_Failed));
end if;
end loop;
end Append_Range_Checks;
procedure Apply_Access_Check (N : Node_Id) is
P : constant Node_Id := Prefix (N);
begin
if not Expander_Active then
return;
end if;
if not Check_Needed (P, Access_Check) then
return;
end if;
Install_Null_Excluding_Check (P);
end Apply_Access_Check;
procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id) is
Loc : constant Source_Ptr := Sloc (N);
Param_Ent : constant Entity_Id := Param_Entity (N);
Param_Level : Node_Id;
Type_Level : Node_Id;
begin
if Inside_A_Generic then
return;
elsif Present (Param_Ent)
and then Present (Extra_Accessibility (Param_Ent))
and then UI_Gt (Object_Access_Level (N),
Type_Access_Level (Typ))
and then not Accessibility_Checks_Suppressed (Param_Ent)
and then not Accessibility_Checks_Suppressed (Typ)
then
Param_Level :=
New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
Type_Level :=
Make_Integer_Literal (Loc, Type_Access_Level (Typ));
Insert_Action (N,
Make_Raise_Program_Error (Loc,
Condition =>
Make_Op_Gt (Loc,
Left_Opnd => Param_Level,
Right_Opnd => Type_Level),
Reason => PE_Accessibility_Check_Failed));
Analyze_And_Resolve (N);
end if;
end Apply_Accessibility_Check;
procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id) is
AC : constant Node_Id := Address_Clause (E);
Typ : constant Entity_Id := Etype (E);
Expr : Node_Id;
Loc : Source_Ptr;
Alignment_Required : constant Boolean := Maximum_Alignment > 1;
begin
if No (AC)
or else not Check_Address_Alignment (AC)
or else not Alignment_Required
then
return;
end if;
Loc := Sloc (AC);
Expr := Expression (AC);
if Nkind (Expr) = N_Unchecked_Type_Conversion then
Expr := Expression (Expr);
elsif Nkind (Expr) = N_Function_Call
and then Is_Entity_Name (Name (Expr))
and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
then
Expr := First (Parameter_Associations (Expr));
if Nkind (Expr) = N_Parameter_Association then
Expr := Explicit_Actual_Parameter (Expr);
end if;
end if;
if Compile_Time_Known_Value (Expr)
and then (Known_Alignment (E) or else Known_Alignment (Typ))
then
declare
AL : Uint := Alignment (Typ);
begin
if Known_Alignment (E) then
AL := Alignment (E);
end if;
if Expr_Value (Expr) mod AL /= 0 then
Insert_Action (N,
Make_Raise_Program_Error (Loc,
Reason => PE_Misaligned_Address_Value));
Error_Msg_NE
("?specified address for& not " &
"consistent with alignment ('R'M 13.3(27))", Expr, E);
end if;
end;
else
if not Restriction_Active (No_Elaboration_Code) then
Insert_After_And_Analyze (N,
Make_Raise_Program_Error (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd =>
Make_Op_Mod (Loc,
Left_Opnd =>
Unchecked_Convert_To
(RTE (RE_Integer_Address),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (E, Loc),
Attribute_Name => Name_Alignment)),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Reason => PE_Misaligned_Address_Value),
Suppress => All_Checks);
end if;
end if;
return;
exception
when RE_Not_Available =>
return;
end Apply_Alignment_Check;
procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
Rtyp : constant Entity_Id := Root_Type (Typ);
Siz : constant Int := UI_To_Int (Esize (Rtyp));
Dsiz : constant Int := Siz * 2;
Opnod : Node_Id;
Ctyp : Entity_Id;
Opnd : Node_Id;
Cent : RE_Id;
begin
if Backend_Overflow_Checks_On_Target
or else not Do_Overflow_Check (N)
or else not Expander_Active
then
return;
end if;
if Dsiz <= Standard_Integer_Size then
Ctyp := Standard_Integer;
elsif Dsiz <= Standard_Long_Long_Integer_Size then
Ctyp := Standard_Long_Long_Integer;
else
if Nkind (N) = N_Op_Add then
Cent := RE_Add_With_Ovflo_Check;
elsif Nkind (N) = N_Op_Multiply then
Cent := RE_Multiply_With_Ovflo_Check;
else
pragma Assert (Nkind (N) = N_Op_Subtract);
Cent := RE_Subtract_With_Ovflo_Check;
end if;
Rewrite (N,
OK_Convert_To (Typ,
Make_Function_Call (Loc,
Name => New_Reference_To (RTE (Cent), Loc),
Parameter_Associations => New_List (
OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
Analyze_And_Resolve (N, Typ);
return;
end if;
Opnod := Relocate_Node (N);
Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
Analyze (Opnd);
Set_Etype (Opnd, Ctyp);
Set_Analyzed (Opnd, True);
Set_Left_Opnd (Opnod, Opnd);
Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
Analyze (Opnd);
Set_Etype (Opnd, Ctyp);
Set_Analyzed (Opnd, True);
Set_Right_Opnd (Opnod, Opnd);
Set_Etype (Opnod, Base_Type (Ctyp));
Set_Do_Overflow_Check (Opnod, False);
Set_Analyzed (Opnod, True);
Opnd := OK_Convert_To (Typ, Opnod);
Analyze (Opnd);
Set_Etype (Opnd, Typ);
if Is_Discrete_Type (Typ) then
Rewrite (N, Opnd);
Generate_Range_Check (Expression (N), Typ, CE_Overflow_Check_Failed);
else
Set_Analyzed (Opnd, True);
Enable_Overflow_Check (Opnd);
Rewrite (N, Opnd);
end if;
exception
when RE_Not_Available =>
return;
end Apply_Arithmetic_Overflow_Check;
procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ctyp : constant Entity_Id := Component_Type (Typ);
Ent : constant Entity_Id := Defining_Identifier (N);
Decl : Node_Id;
Lo : Node_Id;
Hi : Node_Id;
Lob : Uint;
Hib : Uint;
Siz : Uint;
Xtyp : Entity_Id;
Indx : Node_Id;
Sizx : Node_Id;
Code : Node_Id;
Static : Boolean := True;
Umark : constant Uintp.Save_Mark := Uintp.Mark;
Check_Siz : Uint;
function Is_Address_Or_Import (Decl : Node_Id) return Boolean;
function Is_Address_Or_Import (Decl : Node_Id) return Boolean is
begin
if Nkind (Decl) = N_At_Clause then
return Chars (Identifier (Decl)) = Chars (Ent);
elsif Nkind (Decl) = N_Attribute_Definition_Clause then
return
Chars (Decl) = Name_Address
and then
Nkind (Name (Decl)) = N_Identifier
and then
Chars (Name (Decl)) = Chars (Ent);
elsif Nkind (Decl) = N_Pragma then
if (Chars (Decl) = Name_Import
or else
Chars (Decl) = Name_Interface)
and then Present (Pragma_Argument_Associations (Decl))
then
declare
F : constant Node_Id :=
First (Pragma_Argument_Associations (Decl));
begin
return
Present (F)
and then
Present (Next (F))
and then
Nkind (Expression (Next (F))) = N_Identifier
and then
Chars (Expression (Next (F))) = Chars (Ent);
end;
else
return False;
end if;
else
return False;
end if;
end Is_Address_Or_Import;
begin
if Opt.GCC_Version >= 3 then
return;
end if;
if not Expander_Active then
return;
end if;
if Storage_Checks_Suppressed (Typ) then
return;
end if;
if Inside_Init_Proc then
return;
end if;
Decl := N;
for Ctr in 1 .. 20 loop
Next (Decl);
exit when No (Decl);
if Is_Address_Or_Import (Decl) then
return;
end if;
end loop;
Siz := Uint_1;
Indx := First_Index (Typ);
while Present (Indx) loop
Xtyp := Etype (Indx);
Lo := Type_Low_Bound (Xtyp);
Hi := Type_High_Bound (Xtyp);
if Raises_Constraint_Error (Lo)
or else
Raises_Constraint_Error (Hi)
then
Uintp.Release (Umark);
return;
end if;
if Is_Static_Expression (Lo) then
Lob := Expr_Value (Lo);
else
Lob := Expr_Value (Type_Low_Bound (Base_Type (Xtyp)));
Static := False;
end if;
if Is_Static_Expression (Hi) then
Hib := Expr_Value (Hi);
else
Hib := Expr_Value (Type_High_Bound (Base_Type (Xtyp)));
Static := False;
end if;
Siz := Siz * UI_Max (Hib - Lob + 1, Uint_0);
Next_Index (Indx);
end loop;
if Is_Subprogram (Scope (Ent)) then
Check_Siz := Uint_2 ** 27;
else
Check_Siz := Uint_2 ** 31;
end if;
if Static and then Siz >= Check_Siz then
Insert_Action (N,
Make_Raise_Storage_Error (Loc,
Reason => SE_Object_Too_Large));
Error_Msg_N ("?Storage_Error will be raised at run-time", N);
Uintp.Release (Umark);
return;
end if;
if Known_Esize (Ctyp)
and then Siz * Esize (Ctyp) < Check_Siz
then
Uintp.Release (Umark);
return;
end if;
Uintp.Release (Umark);
Sizx :=
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Ctyp, Loc),
Attribute_Name => Name_Size);
Indx := First_Index (Typ);
for J in 1 .. Number_Dimensions (Typ) loop
if Sloc (Etype (Indx)) = Sloc (N) then
Ensure_Defined (Etype (Indx), N);
end if;
Sizx :=
Make_Op_Multiply (Loc,
Left_Opnd => Sizx,
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Length,
Expressions => New_List (
Make_Integer_Literal (Loc, J))));
Next_Index (Indx);
end loop;
Code :=
Make_Raise_Storage_Error (Loc,
Condition =>
Make_Op_Ge (Loc,
Left_Opnd => Sizx,
Right_Opnd =>
Make_Integer_Literal (Loc,
Intval => Check_Siz)),
Reason => SE_Object_Too_Large);
Set_Size_Check_Code (Defining_Identifier (N), Code);
Insert_Action (N, Code, Suppress => All_Checks);
end Apply_Array_Size_Check;
procedure Apply_Constraint_Check
(N : Node_Id;
Typ : Entity_Id;
No_Sliding : Boolean := False)
is
Desig_Typ : Entity_Id;
begin
if Inside_A_Generic then
return;
elsif Is_Scalar_Type (Typ) then
Apply_Scalar_Range_Check (N, Typ);
elsif Is_Array_Type (Typ) then
if Nkind (N) = N_Aggregate
and then No (Expressions (N))
and then Nkind
(First (Choices (First (Component_Associations (N)))))
= N_Others_Choice
then
return;
end if;
if Is_Constrained (Typ) then
Apply_Length_Check (N, Typ);
if No_Sliding then
Apply_Range_Check (N, Typ);
end if;
else
Apply_Range_Check (N, Typ);
end if;
elsif (Is_Record_Type (Typ)
or else Is_Private_Type (Typ))
and then Has_Discriminants (Base_Type (Typ))
and then Is_Constrained (Typ)
then
Apply_Discriminant_Check (N, Typ);
elsif Is_Access_Type (Typ) then
Desig_Typ := Designated_Type (Typ);
if Nkind (N) = N_Null then
null;
elsif Is_Array_Type (Desig_Typ) then
if Is_Constrained (Desig_Typ) then
Apply_Length_Check (N, Typ);
end if;
Apply_Range_Check (N, Typ);
elsif Has_Discriminants (Base_Type (Desig_Typ))
and then Is_Constrained (Desig_Typ)
then
Apply_Discriminant_Check (N, Typ);
end if;
if Can_Never_Be_Null (Typ)
and then not Can_Never_Be_Null (Etype (N))
then
Install_Null_Excluding_Check (N);
end if;
end if;
end Apply_Constraint_Check;
procedure Apply_Discriminant_Check
(N : Node_Id;
Typ : Entity_Id;
Lhs : Node_Id := Empty)
is
Loc : constant Source_Ptr := Sloc (N);
Do_Access : constant Boolean := Is_Access_Type (Typ);
S_Typ : Entity_Id := Etype (N);
Cond : Node_Id;
T_Typ : Entity_Id;
function Is_Aliased_Unconstrained_Component return Boolean;
function Is_Aliased_Unconstrained_Component return Boolean is
Comp : Entity_Id;
Pref : Node_Id;
begin
if Nkind (Lhs) /= N_Selected_Component then
return False;
else
Comp := Entity (Selector_Name (Lhs));
Pref := Prefix (Lhs);
end if;
if Ekind (Comp) /= E_Component
or else not Is_Aliased (Comp)
then
return False;
end if;
return not Comes_From_Source (Pref)
and then In_Instance
and then not Is_Constrained (Etype (Comp));
end Is_Aliased_Unconstrained_Component;
begin
if Do_Access then
T_Typ := Designated_Type (Typ);
else
T_Typ := Typ;
end if;
if not Expander_Active
or else Discriminant_Checks_Suppressed (T_Typ)
then
return;
end if;
if Nkind (N) = N_Null then
return;
elsif Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
if Ekind (S_Typ) = E_Incomplete_Type then
return;
end if;
end if;
if Present (Lhs)
and then (Present (Param_Entity (Lhs))
or else (not Is_Constrained (T_Typ)
and then Is_Aliased_View (Lhs)
and then not Is_Aliased_Unconstrained_Component))
then
T_Typ := Get_Actual_Subtype (Lhs);
end if;
if not Is_Constrained (T_Typ) then
return;
elsif Ada_Version >= Ada_05
and then Has_Constrained_Partial_View (Base_Type (T_Typ))
then
return;
end if;
if Is_Unchecked_Union (Base_Type (T_Typ)) then
return;
end if;
if Nkind (Original_Node (N)) /= N_Allocator
and then (No (Lhs)
or else not Is_Entity_Name (Lhs)
or else No (Param_Entity (Lhs)))
then
if (Etype (N) = Typ
or else (Do_Access and then Designated_Type (Typ) = S_Typ))
and then not Is_Aliased_View (Lhs)
then
return;
end if;
elsif Nkind (Original_Node (N)) = N_Allocator
and then Is_Entity_Name (Expression (Original_Node (N)))
then
declare
Alloc_Typ : constant Entity_Id :=
Entity (Expression (Original_Node (N)));
begin
if Alloc_Typ = T_Typ
or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
and then Is_Entity_Name (
Subtype_Indication (Parent (T_Typ)))
and then Alloc_Typ = Base_Type (T_Typ))
then
return;
end if;
end;
end if;
if Is_Constrained (S_Typ)
and then Nkind (Original_Node (N)) /= N_Allocator
then
declare
DconT : Elmt_Id;
Discr : Entity_Id;
DconS : Elmt_Id;
ItemS : Node_Id;
ItemT : Node_Id;
begin
if Has_Discriminants (S_Typ) then
Discr := First_Discriminant (S_Typ);
DconS := First_Elmt (Discriminant_Constraint (S_Typ));
else
Discr := First_Discriminant (Underlying_Type (S_Typ));
DconS :=
First_Elmt
(Discriminant_Constraint (Underlying_Type (S_Typ)));
if No (DconS) then
return;
end if;
if Nkind (Original_Node (Parent (T_Typ))) =
N_Full_Type_Declaration
then
declare
Type_Def : constant Node_Id :=
Type_Definition
(Original_Node (Parent (T_Typ)));
begin
if Nkind (Type_Def) = N_Derived_Type_Definition
and then Is_Entity_Name (Subtype_Indication (Type_Def))
and then Entity (Subtype_Indication (Type_Def)) = S_Typ
then
return;
end if;
end;
end if;
end if;
DconT := First_Elmt (Discriminant_Constraint (T_Typ));
while Present (Discr) loop
ItemS := Node (DconS);
ItemT := Node (DconT);
exit when
not Is_OK_Static_Expression (ItemS)
or else
not Is_OK_Static_Expression (ItemT);
if Expr_Value (ItemS) /= Expr_Value (ItemT) then
if Do_Access then exit;
else
Apply_Compile_Time_Constraint_Error
(N, "incorrect value for discriminant&?",
CE_Discriminant_Check_Failed, Ent => Discr);
return;
end if;
end if;
Next_Elmt (DconS);
Next_Elmt (DconT);
Next_Discriminant (Discr);
end loop;
if No (Discr) then
return;
end if;
end;
end if;
Cond := Build_Discriminant_Checks (N, T_Typ);
if Present (Param_Entity (Lhs)) then
Cond :=
Make_And_Then (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
Attribute_Name => Name_Constrained),
Right_Opnd => Cond);
end if;
if Do_Access then
Cond := Guard_Access (Cond, Loc, N);
end if;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Discriminant_Check_Failed));
end Apply_Discriminant_Check;
procedure Apply_Divide_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
Left : constant Node_Id := Left_Opnd (N);
Right : constant Node_Id := Right_Opnd (N);
LLB : Uint;
Llo : Uint;
Lhi : Uint;
LOK : Boolean;
Rlo : Uint;
Rhi : Uint;
ROK : Boolean;
begin
if Expander_Active
and then not Backend_Divide_Checks_On_Target
and then Check_Needed (Right, Division_Check)
then
Determine_Range (Right, ROK, Rlo, Rhi);
if Do_Division_Check (N) then
if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Eq (Loc,
Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
Right_Opnd => Make_Integer_Literal (Loc, 0)),
Reason => CE_Divide_By_Zero));
end if;
end if;
if Do_Overflow_Check (N) then
if Nkind (N) = N_Op_Divide
and then Is_Signed_Integer_Type (Typ)
then
Determine_Range (Left, LOK, Llo, Lhi);
LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
and then
((not LOK) or else (Llo = LLB))
then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_And_Then (Loc,
Make_Op_Eq (Loc,
Left_Opnd =>
Duplicate_Subexpr_Move_Checks (Left),
Right_Opnd => Make_Integer_Literal (Loc, LLB)),
Make_Op_Eq (Loc,
Left_Opnd =>
Duplicate_Subexpr (Right),
Right_Opnd =>
Make_Integer_Literal (Loc, -1))),
Reason => CE_Overflow_Check_Failed));
end if;
end if;
end if;
end if;
end Apply_Divide_Check;
procedure Apply_Float_Conversion_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id)
is
LB : constant Node_Id := Type_Low_Bound (Target_Typ);
HB : constant Node_Id := Type_High_Bound (Target_Typ);
Loc : constant Source_Ptr := Sloc (Ck_Node);
Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node));
Target_Base : constant Entity_Id := Implementation_Base_Type
(Target_Typ);
Max_Bound : constant Uint := UI_Expon
(Machine_Radix (Expr_Type),
Machine_Mantissa (Expr_Type) - 1) - 1;
Ifirst,
Ilast : Uint; Lo, Hi : Ureal; Lo_OK,
Hi_OK : Boolean;
Lo_Chk,
Hi_Chk : Node_Id;
Reason : RT_Exception_Code;
begin
if not Compile_Time_Known_Value (LB)
or not Compile_Time_Known_Value (HB)
then
declare
Par : constant Node_Id := Parent (Ck_Node);
pragma Assert (Target_Base /= Target_Typ);
pragma Assert (Nkind (Par) = N_Type_Conversion);
Temp : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Apply_Float_Conversion_Check (Ck_Node, Target_Base);
Set_Etype (Temp, Target_Base);
Insert_Action (Parent (Par),
Make_Object_Declaration (Loc,
Defining_Identifier => Temp,
Object_Definition => New_Occurrence_Of (Target_Typ, Loc),
Expression => New_Copy_Tree (Par)),
Suppress => All_Checks);
Insert_Action (Par,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => New_Occurrence_Of (Temp, Loc),
Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)),
Reason => CE_Range_Check_Failed));
Rewrite (Par, New_Occurrence_Of (Temp, Loc));
return;
end;
end if;
Ifirst := Expr_Value (LB);
Ilast := Expr_Value (HB);
if abs (Ifirst) < Max_Bound then
Lo := UR_From_Uint (Ifirst) - Ureal_Half;
Lo_OK := (Ifirst > 0);
else
Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node);
Lo_OK := (Lo >= UR_From_Uint (Ifirst));
end if;
if Lo_OK then
Lo_Chk := Make_Op_Ge (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Real_Literal (Loc, Lo));
else
Lo_Chk := Make_Op_Gt (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Real_Literal (Loc, Lo));
end if;
if abs (Ilast) < Max_Bound then
Hi := UR_From_Uint (Ilast) + Ureal_Half;
Hi_OK := (Ilast < 0);
else
Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node);
Hi_OK := (Hi <= UR_From_Uint (Ilast));
end if;
if Hi_OK then
Hi_Chk := Make_Op_Le (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Real_Literal (Loc, Hi));
else
Hi_Chk := Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Real_Literal (Loc, Hi));
end if;
if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst
and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast
then
Reason := CE_Overflow_Check_Failed;
else
Reason := CE_Range_Check_Failed;
end if;
Insert_Action (Ck_Node,
Make_Raise_Constraint_Error (Loc,
Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)),
Reason => Reason));
end Apply_Float_Conversion_Check;
procedure Apply_Length_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Length_Checks
(Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
end Apply_Length_Check;
procedure Apply_Range_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Range_Checks
(Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
end Apply_Range_Check;
procedure Apply_Scalar_Range_Check
(Expr : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty;
Fixed_Int : Boolean := False)
is
Parnt : constant Node_Id := Parent (Expr);
S_Typ : Entity_Id;
Arr : Node_Id := Empty; Arr_Typ : Entity_Id := Empty; OK : Boolean;
Is_Subscr_Ref : Boolean;
Is_Unconstrained_Subscr_Ref : Boolean;
Int_Real : Boolean;
procedure Bad_Value;
procedure Bad_Value is
begin
Apply_Compile_Time_Constraint_Error
(Expr, "value not in range of}?", CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
end Bad_Value;
begin
if Inside_A_Generic then
return;
elsif Target_Typ = Any_Type
or else not Is_Scalar_Type (Target_Typ)
or else Raises_Constraint_Error (Expr)
then
return;
end if;
Is_Subscr_Ref :=
Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
if Is_Subscr_Ref then
Arr := Prefix (Parnt);
Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
end if;
if not Do_Range_Check (Expr) then
if Is_Subscr_Ref then
if Index_Checks_Suppressed (Arr_Typ)
or else Index_Checks_Suppressed (Base_Type (Arr_Typ))
then
return;
elsif Is_Entity_Name (Arr)
and then Index_Checks_Suppressed (Entity (Arr))
then
return;
elsif Is_Entity_Name (Expr)
and then Index_Checks_Suppressed (Entity (Expr))
then
return;
end if;
else
if Range_Checks_Suppressed (Target_Typ)
or else Range_Checks_Suppressed (Base_Type (Target_Typ))
then
return;
elsif Is_Entity_Name (Expr)
and then Range_Checks_Suppressed (Entity (Expr))
then
return;
elsif Nkind (Parnt) = N_Assignment_Statement
and then Is_Entity_Name (Name (Parnt))
and then Range_Checks_Suppressed (Entity (Name (Parnt)))
then
return;
end if;
end if;
end if;
if Nkind (Expr) = N_Unchecked_Type_Conversion
and then Kill_Range_Check (Expr)
then
return;
end if;
if Is_Entity_Name (Expr)
and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values)
then
return;
end if;
if No (Source_Typ) then
S_Typ := Etype (Expr);
else
S_Typ := Source_Typ;
end if;
if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
return;
end if;
Is_Unconstrained_Subscr_Ref :=
Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
if Is_Floating_Point_Type (S_Typ)
and then Has_Infinities (S_Typ)
and then not Has_Infinities (Target_Typ)
then
Enable_Range_Check (Expr);
end if;
if Is_Discrete_Type (Target_Typ)
and then Is_Discrete_Type (Etype (Expr))
and then not Is_Unconstrained_Subscr_Ref
and then No (Source_Typ)
then
declare
Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
Thi : constant Node_Id := Type_High_Bound (Target_Typ);
Lo : Uint;
Hi : Uint;
begin
if Compile_Time_Known_Value (Tlo)
and then Compile_Time_Known_Value (Thi)
then
declare
Lov : constant Uint := Expr_Value (Tlo);
Hiv : constant Uint := Expr_Value (Thi);
begin
if Lov > Hiv then
Bad_Value;
return;
end if;
Determine_Range (Expr, OK, Lo, Hi);
if OK then
if Lo >= Lov and then Hi <= Hiv then
return;
elsif Lov > Hi or else Hiv < Lo then
Bad_Value;
return;
else
null;
end if;
end if;
end;
end if;
end;
end if;
Int_Real :=
Is_Floating_Point_Type (S_Typ)
or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
if not Is_Unconstrained_Subscr_Ref
and then
Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
and then
(In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
or else
Is_In_Range (Expr, Target_Typ, Fixed_Int, Int_Real))
then
return;
elsif Is_Out_Of_Range (Expr, Target_Typ, Fixed_Int, Int_Real) then
Bad_Value;
return;
elsif Is_Floating_Point_Type (S_Typ) then
if Is_Constrained (S_Typ) then
Enable_Range_Check (Expr);
end if;
else
Enable_Range_Check (Expr);
return;
end if;
end Apply_Scalar_Range_Check;
procedure Apply_Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean)
is
Cond : Node_Id;
R_Result : Check_Result;
R_Cno : Node_Id;
Loc : constant Source_Ptr := Sloc (Ck_Node);
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Target_Typ))
or else
(not Length_Checks_Suppressed (Target_Typ));
begin
if not Expander_Active then
return;
end if;
R_Result :=
Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
for J in 1 .. 2 loop
R_Cno := R_Result (J);
exit when No (R_Cno);
if Ekind (Current_Scope) = E_Package
and then Is_Compilation_Unit (Current_Scope)
then
Ensure_Defined (Target_Typ, Ck_Node);
if Present (Source_Typ) then
Ensure_Defined (Source_Typ, Ck_Node);
elsif Is_Itype (Etype (Ck_Node)) then
Ensure_Defined (Etype (Ck_Node), Ck_Node);
end if;
end if;
if Nkind (R_Cno) = N_Raise_Constraint_Error
and then Present (Condition (R_Cno))
then
Cond := Condition (R_Cno);
if not Has_Dynamic_Length_Check (Ck_Node)
and then Checks_On
then
Insert_Action (Ck_Node, R_Cno);
if not Do_Static then
Set_Has_Dynamic_Length_Check (Ck_Node);
end if;
end if;
if Is_Entity_Name (Cond)
and then Entity (Cond) = Standard_True
then
Apply_Compile_Time_Constraint_Error
(Ck_Node, "wrong length for array of}?",
CE_Length_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
elsif Do_Static or else not Checks_On then
Rewrite (R_Cno, Make_Null_Statement (Loc));
end if;
else
Install_Static_Check (R_Cno, Loc);
end if;
end loop;
end Apply_Selected_Length_Checks;
procedure Apply_Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean)
is
Cond : Node_Id;
R_Result : Check_Result;
R_Cno : Node_Id;
Loc : constant Source_Ptr := Sloc (Ck_Node);
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Target_Typ))
or else
(not Range_Checks_Suppressed (Target_Typ));
begin
if not Expander_Active or else not Checks_On then
return;
end if;
R_Result :=
Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
for J in 1 .. 2 loop
R_Cno := R_Result (J);
exit when No (R_Cno);
if Nkind (R_Cno) = N_Raise_Constraint_Error
and then Present (Condition (R_Cno))
then
Cond := Condition (R_Cno);
if not Has_Dynamic_Range_Check (Ck_Node) then
Insert_Action (Ck_Node, R_Cno);
if not Do_Static then
Set_Has_Dynamic_Range_Check (Ck_Node);
end if;
end if;
if Is_Entity_Name (Cond)
and then Entity (Cond) = Standard_True
then
if Nkind (Ck_Node) = N_Range then
Apply_Compile_Time_Constraint_Error
(Low_Bound (Ck_Node), "static range out of bounds of}?",
CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
Set_Raises_Constraint_Error (Ck_Node);
else
Apply_Compile_Time_Constraint_Error
(Ck_Node, "static value out of range of}?",
CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
end if;
elsif Do_Static or else not Checks_On then
Rewrite (R_Cno, Make_Null_Statement (Loc));
end if;
else
Install_Static_Check (R_Cno, Loc);
end if;
end loop;
end Apply_Selected_Range_Checks;
procedure Apply_Static_Length_Check
(Expr : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Length_Checks
(Expr, Target_Typ, Source_Typ, Do_Static => True);
end Apply_Static_Length_Check;
procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
Sub : Node_Id;
begin
pragma Assert (Nkind (Expr) = N_Indexed_Component);
Sub := First (Expressions (Expr));
while Present (Sub) loop
Ensure_Valid (Sub, Holes_OK => True);
Sub := Next (Sub);
end loop;
end Apply_Subscript_Validity_Checks;
procedure Apply_Type_Conversion_Checks (N : Node_Id) is
Target_Type : constant Entity_Id := Etype (N);
Target_Base : constant Entity_Id := Base_Type (Target_Type);
Expr : constant Node_Id := Expression (N);
Expr_Type : constant Entity_Id := Etype (Expr);
begin
if Inside_A_Generic then
return;
elsif Serious_Errors_Detected > 0 then
return;
elsif Is_Scalar_Type (Target_Type) then
declare
Conv_OK : constant Boolean := Conversion_OK (N);
Float_To_Int : constant Boolean :=
Is_Floating_Point_Type (Expr_Type)
and then Is_Integer_Type (Target_Type);
begin
if not Overflow_Checks_Suppressed (Target_Base)
and then not In_Subrange_Of (Expr_Type, Target_Base, Conv_OK)
and then not Float_To_Int
then
Set_Do_Overflow_Check (N);
end if;
if not Range_Checks_Suppressed (Target_Type)
and then not Range_Checks_Suppressed (Expr_Type)
then
if Float_To_Int then
Apply_Float_Conversion_Check (Expr, Target_Type);
else
Apply_Scalar_Range_Check
(Expr, Target_Type, Fixed_Int => Conv_OK);
end if;
end if;
end;
elsif Comes_From_Source (N)
and then Is_Record_Type (Target_Type)
and then Is_Derived_Type (Target_Type)
and then not Is_Tagged_Type (Target_Type)
and then not Is_Constrained (Target_Type)
and then Present (Stored_Constraint (Target_Type))
then
declare
Loc : constant Source_Ptr := Sloc (N);
Cond : Node_Id;
Constraint : Elmt_Id;
Discr_Value : Node_Id;
Discr : Entity_Id;
New_Constraints : constant Elist_Id := New_Elmt_List;
Old_Constraints : constant Elist_Id :=
Discriminant_Constraint (Expr_Type);
begin
Constraint := First_Elmt (Stored_Constraint (Target_Type));
while Present (Constraint) loop
Discr_Value := Node (Constraint);
if Is_Entity_Name (Discr_Value)
and then Ekind (Entity (Discr_Value)) = E_Discriminant
then
Discr := Corresponding_Discriminant (Entity (Discr_Value));
if Present (Discr)
and then Scope (Discr) = Base_Type (Expr_Type)
then
Append_Elmt (
Make_Selected_Component (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks
(Expr, Name_Req => True),
Selector_Name =>
Make_Identifier (Loc, Chars (Discr))),
New_Constraints);
else
return;
end if;
else
Append_Elmt
(Duplicate_Subexpr_No_Checks (Discr_Value),
New_Constraints);
end if;
Next_Elmt (Constraint);
end loop;
Set_Discriminant_Constraint (Expr_Type, New_Constraints);
Cond := Build_Discriminant_Checks (Expr, Expr_Type);
Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Discriminant_Check_Failed));
end;
else
null;
end if;
end Apply_Type_Conversion_Checks;
procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
begin
if Inside_A_Generic then
return;
elsif Range_Checks_Suppressed (Typ)
and then Overflow_Checks_Suppressed (Typ)
then
return;
elsif not Comes_From_Source (N) then
return;
elsif Nkind (Prefix (N)) = N_Selected_Component
and then Ekind (Typ) = E_Signed_Integer_Subtype
and then Depends_On_Discriminant (Scalar_Range (Typ))
then
Set_Etype (N, Base_Type (Typ));
else
Set_Etype (N, Universal_Integer);
Set_Analyzed (N, True);
Rewrite (N,
Make_Type_Conversion (Loc,
Subtype_Mark => New_Occurrence_Of (Typ, Loc),
Expression => Relocate_Node (N)));
Analyze_And_Resolve (N, Typ);
return;
end if;
end Apply_Universal_Integer_Attribute_Checks;
function Build_Discriminant_Checks
(N : Node_Id;
T_Typ : Entity_Id) return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
Cond : Node_Id;
Disc : Elmt_Id;
Disc_Ent : Entity_Id;
Dref : Node_Id;
Dval : Node_Id;
function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id;
function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is
Assoc : Node_Id;
begin
Assoc := First (Component_Associations (N));
while Present (Assoc) loop
if Chars (First (Choices (Assoc))) = Chars (Disc) then
return Expression (Assoc);
else
Next (Assoc);
end if;
end loop;
raise Program_Error;
end Aggregate_Discriminant_Val;
begin
Cond := Empty;
Disc := First_Elmt (Discriminant_Constraint (T_Typ));
if Is_Private_Type (T_Typ)
and then No (Full_View (T_Typ))
then
Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
else
Disc_Ent := First_Discriminant (T_Typ);
end if;
while Present (Disc) loop
Dval := Node (Disc);
if Nkind (Dval) = N_Identifier
and then Ekind (Entity (Dval)) = E_Discriminant
then
Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
else
Dval := Duplicate_Subexpr_No_Checks (Dval);
end if;
if Is_Unchecked_Union (Base_Type (T_Typ)) then
Dref := New_Copy (
Get_Discriminant_Value (
First_Discriminant (T_Typ),
T_Typ,
Stored_Constraint (T_Typ)));
elsif Nkind (N) = N_Aggregate then
Dref :=
Duplicate_Subexpr_No_Checks
(Aggregate_Discriminant_Val (Disc_Ent));
else
Dref :=
Make_Selected_Component (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Selector_Name =>
Make_Identifier (Loc, Chars (Disc_Ent)));
Set_Is_In_Discriminant_Check (Dref);
end if;
Evolve_Or_Else (Cond,
Make_Op_Ne (Loc,
Left_Opnd => Dref,
Right_Opnd => Dval));
Next_Elmt (Disc);
Next_Discriminant (Disc_Ent);
end loop;
return Cond;
end Build_Discriminant_Checks;
function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is
N : Node_Id;
P : Node_Id;
K : Node_Kind;
L : Node_Id;
R : Node_Id;
begin
if Nkind (Nod) not in N_Has_Entity
or else not Comes_From_Source (Nod)
then
return True;
end if;
N := Nod;
loop
P := Parent (N);
K := Nkind (P);
if K not in N_Subexpr then
return True;
elsif K = N_Op_Or or else K = N_Or_Else then
exit when N = Right_Opnd (P)
and then Nkind (Left_Opnd (P)) = N_Op_Eq;
elsif K = N_Op_And or else K = N_And_Then then
exit when N = Right_Opnd (P)
and then Nkind (Left_Opnd (P)) = N_Op_Ne;
end if;
N := P;
end loop;
L := Left_Opnd (P);
if Nkind (L) = N_Op_Not then
L := Right_Opnd (L);
end if;
R := Right_Opnd (L);
L := Left_Opnd (L);
if Nkind (L) not in N_Has_Entity
or else Entity (L) /= Entity (Nod)
then
return True;
end if;
case Check is
when Access_Check =>
if Nkind (R) /= N_Null then
return True;
end if;
when Division_Check =>
if not Compile_Time_Known_Value (R)
or else Expr_Value (R) /= Uint_0
then
return True;
end if;
end case;
if K = N_Op_And or else K = N_Op_Or then
case Check is
when Access_Check =>
Error_Msg_N
("Constraint_Error may be raised (access check)?",
Parent (Nod));
when Division_Check =>
Error_Msg_N
("Constraint_Error may be raised (zero divide)?",
Parent (Nod));
end case;
if K = N_Op_And then
Error_Msg_N ("use `AND THEN` instead of AND?", P);
else
Error_Msg_N ("use `OR ELSE` instead of OR?", P);
end if;
return True;
else
return False;
end if;
end Check_Needed;
procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
begin
if Range_Checks_Suppressed (Etype (Expr)) then
return;
elsif not Comes_From_Source (Expr) then
return;
elsif Nkind (Expr) = N_Selected_Component then
Check_Valid_Lvalue_Subscripts (Prefix (Expr));
return;
elsif Nkind (Expr) = N_Indexed_Component then
Apply_Subscript_Validity_Checks (Expr);
Check_Valid_Lvalue_Subscripts (Prefix (Expr));
end if;
end Check_Valid_Lvalue_Subscripts;
procedure Null_Exclusion_Static_Checks (N : Node_Id) is
K : constant Node_Kind := Nkind (N);
Typ : Entity_Id;
Related_Nod : Node_Id;
Has_Null_Exclusion : Boolean := False;
begin
pragma Assert (K = N_Parameter_Specification
or else K = N_Object_Declaration
or else K = N_Discriminant_Specification
or else K = N_Component_Declaration);
Typ := Etype (Defining_Identifier (N));
pragma Assert (Is_Access_Type (Typ)
or else (K = N_Object_Declaration and then Is_Array_Type (Typ)));
case K is
when N_Parameter_Specification =>
Related_Nod := Parameter_Type (N);
Has_Null_Exclusion := Null_Exclusion_Present (N);
when N_Object_Declaration =>
Related_Nod := Object_Definition (N);
Has_Null_Exclusion := Null_Exclusion_Present (N);
when N_Discriminant_Specification =>
Related_Nod := Discriminant_Type (N);
Has_Null_Exclusion := Null_Exclusion_Present (N);
when N_Component_Declaration =>
if Present (Access_Definition (Component_Definition (N))) then
Related_Nod := Component_Definition (N);
Has_Null_Exclusion :=
Null_Exclusion_Present
(Access_Definition (Component_Definition (N)));
else
Related_Nod :=
Subtype_Indication (Component_Definition (N));
Has_Null_Exclusion :=
Null_Exclusion_Present (Component_Definition (N));
end if;
when others =>
raise Program_Error;
end case;
if Has_Null_Exclusion
and then Can_Never_Be_Null (Typ)
and then not Is_Itype (Typ)
then
Error_Msg_N
("(Ada 2005) already a null-excluding type", Related_Nod);
end if;
if K = N_Object_Declaration
and then No (Expression (N))
then
Set_Expression (N, Make_Null (Sloc (N)));
Set_Etype (Expression (N), Etype (Defining_Identifier (N)));
Apply_Compile_Time_Constraint_Error
(N => Expression (N),
Msg => "(Ada 2005) null-excluding objects must be initialized?",
Reason => CE_Null_Not_Allowed);
end if;
declare
Expr : constant Node_Id := Expression (N);
begin
if Present (Expr)
and then Nkind (Expr) = N_Null
then
case K is
when N_Discriminant_Specification |
N_Component_Declaration =>
Apply_Compile_Time_Constraint_Error
(N => Expr,
Msg => "(Ada 2005) NULL not allowed in"
& " null-excluding components?",
Reason => CE_Null_Not_Allowed);
when N_Parameter_Specification =>
Apply_Compile_Time_Constraint_Error
(N => Expr,
Msg => "(Ada 2005) NULL not allowed in"
& " null-excluding formals?",
Reason => CE_Null_Not_Allowed);
when N_Object_Declaration =>
Apply_Compile_Time_Constraint_Error
(N => Expr,
Msg => "(Ada 2005) NULL not allowed in"
& " null-excluding objects?",
Reason => CE_Null_Not_Allowed);
when others =>
null;
end case;
end if;
end;
end Null_Exclusion_Static_Checks;
procedure Conditional_Statements_Begin is
begin
Saved_Checks_TOS := Saved_Checks_TOS + 1;
if Saved_Checks_TOS > Saved_Checks_Stack'Last then
Kill_All_Checks;
else
Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks;
if Debug_Flag_CC then
w ("Conditional_Statements_Begin: Num_Saved_Checks = ",
Num_Saved_Checks);
end if;
end if;
end Conditional_Statements_Begin;
procedure Conditional_Statements_End is
begin
pragma Assert (Saved_Checks_TOS > 0);
if Saved_Checks_TOS > Saved_Checks_Stack'Last then
Num_Saved_Checks := 0;
else
Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS);
if Debug_Flag_CC then
w ("Conditional_Statements_End: Num_Saved_Checks = ",
Num_Saved_Checks);
end if;
end if;
Saved_Checks_TOS := Saved_Checks_TOS - 1;
end Conditional_Statements_End;
Cache_Size : constant := 2 ** 10;
type Cache_Index is range 0 .. Cache_Size - 1;
Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
procedure Determine_Range
(N : Node_Id;
OK : out Boolean;
Lo : out Uint;
Hi : out Uint)
is
Typ : constant Entity_Id := Etype (N);
Lo_Left : Uint;
Hi_Left : Uint;
Lo_Right : Uint;
Hi_Right : Uint;
Bound : Node_Id;
Hbound : Uint;
Lor : Uint;
Hir : Uint;
OK1 : Boolean;
Cindex : Cache_Index;
function OK_Operands return Boolean;
function OK_Operands return Boolean is
begin
Determine_Range (Left_Opnd (N), OK1, Lo_Left, Hi_Left);
if not OK1 then
return False;
end if;
Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
return OK1;
end OK_Operands;
begin
Lo := No_Uint;
Hi := No_Uint;
Lor := No_Uint;
Hir := No_Uint;
if No (Typ) or else not Is_Discrete_Type (Typ)
or else Error_Posted (N)
then
OK := False;
return;
end if;
OK := True;
if Compile_Time_Known_Value (N) then
Lo := Expr_Value (N);
Hi := Lo;
return;
end if;
Cindex := Cache_Index (N mod Cache_Size);
if Determine_Range_Cache_N (Cindex) = N then
Lo := Determine_Range_Cache_Lo (Cindex);
Hi := Determine_Range_Cache_Hi (Cindex);
return;
end if;
Bound := Type_Low_Bound (Typ);
if Compile_Time_Known_Value (Bound) then
Lo := Expr_Value (Bound);
elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then
Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
else
OK := False;
return;
end if;
Bound := Type_High_Bound (Typ);
if Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then
Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ)));
Hi := Hbound;
else
OK := False;
return;
end if;
if Compile_Time_Known_Value (Bound) then
Hi := Expr_Value (Bound);
end if;
case Nkind (N) is
when N_Op_Plus =>
Determine_Range (Right_Opnd (N), OK1, Lor, Hir);
when N_Op_Minus =>
Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
if OK1 then
Lor := -Hi_Right;
Hir := -Lo_Right;
end if;
when N_Op_Add =>
if OK_Operands then
Lor := Lo_Left + Lo_Right;
Hir := Hi_Left + Hi_Right;
end if;
when N_Op_Divide =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right > 0
then
Lor := Lo_Left / Lo_Right;
Hir := Hi_Left / Lo_Right;
else
OK1 := False;
end if;
end if;
when N_Op_Subtract =>
if OK_Operands then
Lor := Lo_Left - Hi_Right;
Hir := Hi_Left - Lo_Right;
end if;
when N_Op_Mod =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right /= 0
then
if Lo_Right > 0 then
Lor := Uint_0;
Hir := Lo_Right - 1;
else Lor := Lo_Right + 1;
Hir := Uint_0;
end if;
else
OK1 := False;
end if;
end if;
when N_Op_Rem =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right /= 0
then
declare
Dval : constant Uint := (abs Lo_Right) - 1;
begin
if Lo_Left < 0 then
Lor := -Dval;
else
Lor := Uint_0;
end if;
if Hi_Left < 0 then
Hir := Uint_0;
else
Hir := Dval;
end if;
end;
else
OK1 := False;
end if;
end if;
when N_Attribute_Reference =>
case Attribute_Name (N) is
when Name_Pos | Name_Val =>
Determine_Range (First (Expressions (N)), OK1, Lor, Hir);
when Name_Length =>
declare
Atyp : Entity_Id := Etype (Prefix (N));
Inum : Nat;
Indx : Node_Id;
LL, LU : Uint;
UL, UU : Uint;
begin
if Is_Access_Type (Atyp) then
Atyp := Designated_Type (Atyp);
end if;
if Ekind (Atyp) = E_String_Literal_Subtype then
OK := True;
Lo := String_Literal_Length (Atyp);
Hi := String_Literal_Length (Atyp);
return;
end if;
if No (Expressions (N)) then
Inum := 1;
else
Inum :=
UI_To_Int (Expr_Value (First (Expressions (N))));
end if;
Indx := First_Index (Atyp);
for J in 2 .. Inum loop
Indx := Next_Index (Indx);
end loop;
Determine_Range
(Type_Low_Bound (Etype (Indx)), OK1, LL, LU);
if OK1 then
Determine_Range
(Type_High_Bound (Etype (Indx)), OK1, UL, UU);
if OK1 then
Hir := UI_Max (Uint_0, UU - LL);
if Is_Constrained (Atyp) then
Lor := UI_Max (Uint_0, UL - LU);
else
Lor := Uint_0;
end if;
end if;
end if;
end;
when others =>
OK1 := False;
end case;
when N_Type_Conversion =>
Determine_Range (Expression (N), OK1, Lor, Hir);
when others =>
OK1 := False;
Lor := No_Uint;
Hir := No_Uint;
end case;
if OK1 then
if Lor > Lo
and then not (Is_Modular_Integer_Type (Typ)
and then Hir > Hbound)
then
Lo := Lor;
end if;
if Hir < Hi
and then not (Is_Modular_Integer_Type (Typ)
and then Lor < Uint_0)
then
Hi := Hir;
end if;
end if;
Determine_Range_Cache_N (Cindex) := N;
Determine_Range_Cache_Lo (Cindex) := Lo;
Determine_Range_Cache_Hi (Cindex) := Hi;
return;
exception
when others =>
if Debug_Flag_K then
raise;
else
OK := False;
Lo := No_Uint;
Hi := No_Uint;
return;
end if;
end Determine_Range;
function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Is_Unchecked_Union (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Discriminant_Check);
end if;
end if;
return Scope_Suppress (Discriminant_Check);
end Discriminant_Checks_Suppressed;
function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Division_Check);
else
return Scope_Suppress (Division_Check);
end if;
end Division_Checks_Suppressed;
function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Kill_Elaboration_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
if Is_Check_Suppressed (E, Elaboration_Check) then
return True;
elsif Dynamic_Elaboration_Checks then
return Is_Check_Suppressed (E, All_Checks);
else
return False;
end if;
end if;
end if;
if Scope_Suppress (Elaboration_Check) then
return True;
elsif Dynamic_Elaboration_Checks then
return Scope_Suppress (All_Checks);
else
return False;
end if;
end Elaboration_Checks_Suppressed;
procedure Enable_Overflow_Check (N : Node_Id) is
Typ : constant Entity_Id := Base_Type (Etype (N));
Chk : Nat;
OK : Boolean;
Ent : Entity_Id;
Ofs : Uint;
Lo : Uint;
Hi : Uint;
begin
if Debug_Flag_CC then
w ("Enable_Overflow_Check for node ", Int (N));
Write_Str (" Source location = ");
wl (Sloc (N));
pg (N);
end if;
if Nkind (N) /= N_Type_Conversion then
Determine_Range (N, OK, Lo, Hi);
if OK
and then Lo > Expr_Value (Type_Low_Bound (Typ))
and then Hi < Expr_Value (Type_High_Bound (Typ))
then
if Debug_Flag_CC then
w ("No overflow check required");
end if;
return;
end if;
end if;
if Optimization_Level = 0
or else not Is_Discrete_Type (Etype (N))
or else Num_Saved_Checks = Saved_Checks'Last
then
Set_Do_Overflow_Check (N, True);
if Debug_Flag_CC then
w ("Optimization off");
end if;
return;
end if;
Find_Check
(Expr => N,
Check_Type => 'O',
Target_Type => Empty,
Entry_OK => OK,
Check_Num => Chk,
Ent => Ent,
Ofs => Ofs);
if Debug_Flag_CC then
w ("Called Find_Check");
w (" OK = ", OK);
if OK then
w (" Check_Num = ", Chk);
w (" Ent = ", Int (Ent));
Write_Str (" Ofs = ");
pid (Ofs);
end if;
end if;
if not OK then
Set_Do_Overflow_Check (N, True);
return;
end if;
if Chk /= 0 then
if Debug_Flag_CC then
w ("Check suppressed!");
end if;
return;
end if;
Set_Do_Overflow_Check (N, True);
Num_Saved_Checks := Num_Saved_Checks + 1;
Saved_Checks (Num_Saved_Checks) :=
(Killed => False,
Entity => Ent,
Offset => Ofs,
Check_Type => 'O',
Target_Type => Empty);
if Debug_Flag_CC then
w ("Make new entry, check number = ", Num_Saved_Checks);
w (" Entity = ", Int (Ent));
Write_Str (" Offset = ");
pid (Ofs);
w (" Check_Type = O");
w (" Target_Type = Empty");
end if;
exception
when others =>
Set_Do_Overflow_Check (N, True);
if Debug_Flag_CC then
w (" exception occurred, overflow flag set");
end if;
return;
end Enable_Overflow_Check;
procedure Enable_Range_Check (N : Node_Id) is
Chk : Nat;
OK : Boolean;
Ent : Entity_Id;
Ofs : Uint;
Ttyp : Entity_Id;
P : Node_Id;
begin
if Nkind (N) = N_Unchecked_Type_Conversion
and then Kill_Range_Check (N)
then
return;
end if;
if Debug_Flag_CC then
w ("Enable_Range_Check for node ", Int (N));
Write_Str (" Source location = ");
wl (Sloc (N));
pg (N);
end if;
if Optimization_Level = 0
or else No (Etype (N))
or else not Is_Discrete_Type (Etype (N))
or else Num_Saved_Checks = Saved_Checks'Last
then
Set_Do_Range_Check (N, True);
if Debug_Flag_CC then
w ("Optimization off");
end if;
return;
end if;
P := Parent (N);
if Nkind (P) = N_Assignment_Statement
and then Expression (P) = N
then
Ttyp := Etype (Name (P));
elsif Nkind (P) = N_Indexed_Component then
declare
Atyp : Entity_Id;
Indx : Node_Id;
Subs : Node_Id;
begin
Atyp := Etype (Prefix (P));
if Is_Access_Type (Atyp) then
Atyp := Designated_Type (Atyp);
if not Is_Constrained (Atyp) then
Set_Do_Range_Check (N, True);
return;
end if;
elsif Nkind (Prefix (P)) = N_Explicit_Dereference
and then not Is_Constrained (Atyp)
then
Set_Do_Range_Check (N, True);
return;
end if;
Indx := First_Index (Atyp);
Subs := First (Expressions (P));
loop
if Subs = N then
Ttyp := Etype (Indx);
exit;
end if;
Next_Index (Indx);
Next (Subs);
end loop;
end;
else
if Debug_Flag_CC then
w (" target type not found, flag set");
end if;
Set_Do_Range_Check (N, True);
return;
end if;
Find_Check
(Expr => N,
Check_Type => 'R',
Target_Type => Ttyp,
Entry_OK => OK,
Check_Num => Chk,
Ent => Ent,
Ofs => Ofs);
if Debug_Flag_CC then
w ("Called Find_Check");
w ("Target_Typ = ", Int (Ttyp));
w (" OK = ", OK);
if OK then
w (" Check_Num = ", Chk);
w (" Ent = ", Int (Ent));
Write_Str (" Ofs = ");
pid (Ofs);
end if;
end if;
if not OK then
if Debug_Flag_CC then
w (" expression not of optimizable type, flag set");
end if;
Set_Do_Range_Check (N, True);
return;
end if;
if Chk /= 0 then
if Debug_Flag_CC then
w ("Check suppressed!");
end if;
return;
end if;
Set_Do_Range_Check (N, True);
Num_Saved_Checks := Num_Saved_Checks + 1;
Saved_Checks (Num_Saved_Checks) :=
(Killed => False,
Entity => Ent,
Offset => Ofs,
Check_Type => 'R',
Target_Type => Ttyp);
if Debug_Flag_CC then
w ("Make new entry, check number = ", Num_Saved_Checks);
w (" Entity = ", Int (Ent));
Write_Str (" Offset = ");
pid (Ofs);
w (" Check_Type = R");
w (" Target_Type = ", Int (Ttyp));
pg (Ttyp);
end if;
exception
when others =>
Set_Do_Range_Check (N, True);
if Debug_Flag_CC then
w (" exception occurred, range flag set");
end if;
return;
end Enable_Range_Check;
procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is
Typ : constant Entity_Id := Etype (Expr);
begin
if not Validity_Checks_On then
return;
elsif Is_Entity_Name (Expr)
and then Range_Checks_Suppressed (Entity (Expr))
then
return;
elsif not Comes_From_Source (Expr)
and then not Force_Validity_Checks
and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
or else Kill_Range_Check (Expr))
then
return;
elsif Expr_Known_Valid (Expr) then
return;
elsif Range_Checks_Suppressed (Typ) then
return;
elsif Is_Enumeration_Type (Typ)
and then Has_Non_Standard_Rep (Typ)
and then Holes_OK
then
return;
elsif Nkind (Parent (Expr)) = N_Assignment_Statement
and then Expr = Name (Parent (Expr))
then
return;
elsif Nkind (Expr) = N_Real_Literal
and then Etype (Expr) = Universal_Real
then
return;
else
if Is_Scalar_Type (Typ) then
declare
P : Node_Id;
N : Node_Id;
E : Entity_Id;
F : Entity_Id;
A : Node_Id;
L : List_Id;
begin
N := Expr;
P := Parent (Expr);
if Nkind (P) = N_Parameter_Association then
N := P;
P := Parent (N);
end if;
if Nkind (P) = N_Procedure_Call_Statement then
L := Parameter_Associations (P);
if Is_Entity_Name (Name (P)) then
E := Entity (Name (P));
else
pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference);
E := Etype (Name (P));
end if;
if Is_Non_Empty_List (L)
and then Is_Subprogram (E)
then
F := First_Formal (E);
A := First (L);
while Present (F) loop
if Ekind (F) = E_Out_Parameter and then A = N then
return;
end if;
Next_Formal (F);
Next (A);
end loop;
end if;
end if;
end;
end if;
end if;
Insert_Valid_Check (Expr);
end Ensure_Valid;
function Expr_Known_Valid (Expr : Node_Id) return Boolean is
Typ : constant Entity_Id := Etype (Expr);
begin
if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then
return True;
elsif not Validity_Checks_On then
return True;
elsif Is_Floating_Point_Type (Typ)
and then not Validity_Check_Floating_Point
then
return True;
elsif Is_Entity_Name (Expr)
and then Is_Known_Valid (Entity (Expr))
then
return True;
elsif Is_Known_Valid (Typ) then
if Is_Entity_Name (Expr)
and then Ekind (Entity (Expr)) = E_Variable
and then Esize (Entity (Expr)) > Esize (Typ)
then
return False;
else
return True;
end if;
elsif Nkind (Expr) = N_Integer_Literal
or else
Nkind (Expr) = N_Character_Literal
then
return True;
elsif Nkind (Expr) = N_Type_Conversion
or else
Nkind (Expr) = N_Qualified_Expression
then
return Expr_Known_Valid (Expression (Expr));
elsif
Nkind (Expr) in N_Binary_Op or else Nkind (Expr) in N_Unary_Op
then
if Is_Floating_Point_Type (Typ)
and then Validity_Check_Floating_Point
and then
(Nkind (Parent (Expr)) = N_Assignment_Statement
or else Nkind (Parent (Expr)) = N_Function_Call
or else Nkind (Parent (Expr)) = N_Parameter_Association)
then
return False;
else
return True;
end if;
else
return False;
end if;
end Expr_Known_Valid;
procedure Find_Check
(Expr : Node_Id;
Check_Type : Character;
Target_Type : Entity_Id;
Entry_OK : out Boolean;
Check_Num : out Nat;
Ent : out Entity_Id;
Ofs : out Uint)
is
function Within_Range_Of
(Target_Type : Entity_Id;
Check_Type : Entity_Id) return Boolean;
function Within_Range_Of
(Target_Type : Entity_Id;
Check_Type : Entity_Id) return Boolean
is
begin
if Target_Type = Check_Type then
return True;
else
declare
Tlo : constant Node_Id := Type_Low_Bound (Target_Type);
Thi : constant Node_Id := Type_High_Bound (Target_Type);
Clo : constant Node_Id := Type_Low_Bound (Check_Type);
Chi : constant Node_Id := Type_High_Bound (Check_Type);
begin
if (Tlo = Clo
or else (Compile_Time_Known_Value (Tlo)
and then
Compile_Time_Known_Value (Clo)
and then
Expr_Value (Clo) >= Expr_Value (Tlo)))
and then
(Thi = Chi
or else (Compile_Time_Known_Value (Thi)
and then
Compile_Time_Known_Value (Chi)
and then
Expr_Value (Chi) <= Expr_Value (Clo)))
then
return True;
else
return False;
end if;
end;
end if;
end Within_Range_Of;
begin
Check_Num := 0;
if Is_Entity_Name (Expr) then
Ent := Entity (Expr);
Ofs := Uint_0;
elsif Nkind (Expr) = N_Op_Add
and then Compile_Time_Known_Value (Right_Opnd (Expr))
and then Is_Entity_Name (Left_Opnd (Expr))
then
Ent := Entity (Left_Opnd (Expr));
Ofs := Expr_Value (Right_Opnd (Expr));
elsif Nkind (Expr) = N_Op_Subtract
and then Compile_Time_Known_Value (Right_Opnd (Expr))
and then Is_Entity_Name (Left_Opnd (Expr))
then
Ent := Entity (Left_Opnd (Expr));
Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr)));
else
Ent := Empty;
Ofs := Uint_0;
Entry_OK := False;
return;
end if;
if (Ekind (Ent) = E_Variable
or else
Ekind (Ent) = E_Constant
or else
Ekind (Ent) = E_Loop_Parameter
or else
Ekind (Ent) = E_In_Parameter)
and then not Is_Library_Level_Entity (Ent)
then
Entry_OK := True;
else
Entry_OK := False;
return;
end if;
for J in reverse 1 .. Num_Saved_Checks loop
declare
SC : Saved_Check renames Saved_Checks (J);
begin
if SC.Killed = False
and then SC.Entity = Ent
and then SC.Offset = Ofs
and then SC.Check_Type = Check_Type
and then Within_Range_Of (Target_Type, SC.Target_Type)
then
Check_Num := J;
return;
end if;
end;
end loop;
Check_Num := 0;
return;
end Find_Check;
procedure Generate_Discriminant_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Pref : constant Node_Id := Prefix (N);
Sel : constant Node_Id := Selector_Name (N);
Orig_Comp : constant Entity_Id :=
Original_Record_Component (Entity (Sel));
Discr_Fct : constant Entity_Id :=
Discriminant_Checking_Func (Orig_Comp);
Discr : Entity_Id;
Real_Discr : Entity_Id;
Pref_Type : Entity_Id;
Args : List_Id;
Formal : Entity_Id;
Scomp : Node_Id;
begin
Pref_Type := Etype (Pref);
if Is_Entity_Name (Pref) then
if Treat_As_Volatile (Entity (Pref)) then
Force_Evaluation (Pref, Name_Req => True);
end if;
elsif Treat_As_Volatile (Etype (Pref)) then
Force_Evaluation (Pref, Name_Req => True);
elsif Nkind (Pref) = N_Selected_Component
and then Is_Entity_Name (Prefix (Pref))
then
null;
else
Force_Evaluation (Pref, Name_Req => True);
end if;
if Is_Tagged_Type (Scope (Orig_Comp)) then
Pref_Type := Scope (Orig_Comp);
else
if Is_Derived_Type (Pref_Type)
and then Number_Discriminants (Pref_Type) /=
Number_Discriminants (Etype (Base_Type (Pref_Type)))
then
Pref_Type := Etype (Base_Type (Pref_Type));
end if;
end if;
pragma Assert (Present (Discr_Fct));
Args := New_List;
Formal := First_Formal (Discr_Fct);
Discr := First_Discriminant (Pref_Type);
while Present (Discr) loop
if Present (Corresponding_Discriminant (Discr))
and then Ekind (Pref_Type) = E_Record_Type
and then Present (Parent_Subtype (Pref_Type))
then
Real_Discr := Corresponding_Discriminant (Discr);
else
Real_Discr := Discr;
end if;
Scomp :=
Make_Selected_Component (Loc,
Prefix =>
Unchecked_Convert_To (Pref_Type,
Duplicate_Subexpr (Pref)),
Selector_Name => New_Occurrence_Of (Real_Discr, Loc));
Set_Etype (Scomp, Etype (Real_Discr));
Set_Analyzed (Scomp, True);
Append_To (Args, Convert_To (Etype (Formal), Scomp));
Next_Formal_With_Extras (Formal);
Next_Discriminant (Discr);
end loop;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Function_Call (Loc,
Name => New_Occurrence_Of (Discr_Fct, Loc),
Parameter_Associations => Args),
Reason => CE_Discriminant_Check_Failed));
end Generate_Discriminant_Check;
procedure Generate_Index_Checks (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
A : constant Node_Id := Prefix (N);
Sub : Node_Id;
Ind : Nat;
Num : List_Id;
begin
Sub := First (Expressions (N));
Ind := 1;
while Present (Sub) loop
if Do_Range_Check (Sub) then
Set_Do_Range_Check (Sub, False);
if not Is_Entity_Name (Sub)
or else Treat_As_Volatile (Entity (Sub))
then
Force_Evaluation (Sub);
end if;
if Ind = 1 then
Num := No_List;
else
Num := New_List (Make_Integer_Literal (Loc, Ind));
end if;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd =>
Convert_To (Base_Type (Etype (Sub)),
Duplicate_Subexpr_Move_Checks (Sub)),
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr_Move_Checks (A),
Attribute_Name => Name_Range,
Expressions => Num)),
Reason => CE_Index_Check_Failed));
end if;
Ind := Ind + 1;
Next (Sub);
end loop;
end Generate_Index_Checks;
procedure Generate_Range_Check
(N : Node_Id;
Target_Type : Entity_Id;
Reason : RT_Exception_Code)
is
Loc : constant Source_Ptr := Sloc (N);
Source_Type : constant Entity_Id := Etype (N);
Source_Base_Type : constant Entity_Id := Base_Type (Source_Type);
Target_Base_Type : constant Entity_Id := Base_Type (Target_Type);
begin
if In_Subrange_Of (Source_Type, Target_Type)
and then not
(Nkind (N) = N_Integer_Literal
or else
Nkind (N) = N_Real_Literal
or else
Nkind (N) = N_Character_Literal
or else
(Is_Entity_Name (N)
and then Ekind (Entity (N)) = E_Enumeration_Literal))
then
return;
end if;
if not Is_Entity_Name (N)
or else Treat_As_Volatile (Entity (N))
then
Force_Evaluation (N);
end if;
if Source_Base_Type = Target_Base_Type then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
Reason => Reason));
elsif In_Subrange_Of (Target_Type, Source_Base_Type) then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Make_Range (Loc,
Low_Bound =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First)),
High_Bound =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last)))),
Reason => Reason));
elsif In_Subrange_Of (Source_Type, Target_Base_Type) then
declare
Tnn : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Tnn,
Object_Definition =>
New_Occurrence_Of (Target_Base_Type, Loc),
Constant_Present => True,
Expression =>
Make_Type_Conversion (Loc,
Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc),
Expression => Duplicate_Subexpr (N))),
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => New_Occurrence_Of (Tnn, Loc),
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
Reason => Reason)));
Rewrite (N, New_Occurrence_Of (Tnn, Loc));
end;
else
if Is_Unsigned_Type (Source_Base_Type)
and then not Is_Unsigned_Type (Target_Base_Type)
then
pragma Assert
(Esize (Source_Base_Type) >= Esize (Target_Base_Type));
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Or_Else (Loc,
Make_Or_Else (Loc,
Left_Opnd =>
Make_And_Then (Loc,
Left_Opnd => Make_Op_Ge (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Right_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First)))),
Right_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0))),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last)))),
Reason => Reason),
Suppress => All_Checks);
else
pragma Assert (not Is_Unsigned_Type (Source_Base_Type)
and then Is_Unsigned_Type (Target_Base_Type));
declare
Tnn : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Tnn,
Object_Definition =>
New_Occurrence_Of (Target_Base_Type, Loc),
Constant_Present => True,
Expression =>
Make_Type_Conversion (Loc,
Subtype_Mark =>
New_Occurrence_Of (Target_Base_Type, Loc),
Expression => Duplicate_Subexpr (N))),
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Right_Opnd =>
Make_Not_In (Loc,
Left_Opnd => New_Occurrence_Of (Tnn, Loc),
Right_Opnd =>
New_Occurrence_Of (Target_Type, Loc))),
Reason => Reason)),
Suppress => All_Checks);
Set_Etype (Tnn, Target_Base_Type);
Rewrite (N, New_Occurrence_Of (Tnn, Loc));
end;
end if;
end if;
end Generate_Range_Check;
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
Loc : constant Source_Ptr := Sloc (E);
D : Entity_Id;
Sc : Entity_Id;
begin
Sc := Scope (E);
if Ekind (Sc) /= E_Protected_Type then
Sc := Scope (Sc);
if Ekind (Sc) /= E_Protected_Type then
return Bound;
end if;
end if;
D := First_Discriminant (Sc);
while Present (D)
and then Chars (D) /= Chars (Bound)
loop
Next_Discriminant (D);
end loop;
return New_Occurrence_Of (Discriminal (D), Loc);
end Get_Discriminal;
function Guard_Access
(Cond : Node_Id;
Loc : Source_Ptr;
Ck_Node : Node_Id) return Node_Id
is
begin
if Nkind (Cond) = N_Or_Else then
Set_Paren_Count (Cond, 1);
end if;
if Nkind (Ck_Node) = N_Allocator then
return Cond;
else
return
Make_And_Then (Loc,
Left_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Null (Loc)),
Right_Opnd => Cond);
end if;
end Guard_Access;
function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Index_Check);
else
return Scope_Suppress (Index_Check);
end if;
end Index_Checks_Suppressed;
procedure Initialize is
begin
for J in Determine_Range_Cache_N'Range loop
Determine_Range_Cache_N (J) := Empty;
end loop;
end Initialize;
procedure Insert_Range_Checks
(Checks : Check_Result;
Node : Node_Id;
Suppress_Typ : Entity_Id;
Static_Sloc : Source_Ptr := No_Location;
Flag_Node : Node_Id := Empty;
Do_Before : Boolean := False)
is
Internal_Flag_Node : Node_Id := Flag_Node;
Internal_Static_Sloc : Source_Ptr := Static_Sloc;
Check_Node : Node_Id;
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Suppress_Typ))
or else
(not Range_Checks_Suppressed (Suppress_Typ));
begin
if not Expander_Active or else not Checks_On then
return;
end if;
if Static_Sloc = No_Location then
Internal_Static_Sloc := Sloc (Node);
end if;
if No (Flag_Node) then
Internal_Flag_Node := Node;
end if;
for J in 1 .. 2 loop
exit when No (Checks (J));
if Nkind (Checks (J)) = N_Raise_Constraint_Error
and then Present (Condition (Checks (J)))
then
if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
Check_Node := Checks (J);
Mark_Rewrite_Insertion (Check_Node);
if Do_Before then
Insert_Before_And_Analyze (Node, Check_Node);
else
Insert_After_And_Analyze (Node, Check_Node);
end if;
Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
end if;
else
Check_Node :=
Make_Raise_Constraint_Error (Internal_Static_Sloc,
Reason => CE_Range_Check_Failed);
Mark_Rewrite_Insertion (Check_Node);
if Do_Before then
Insert_Before_And_Analyze (Node, Check_Node);
else
Insert_After_And_Analyze (Node, Check_Node);
end if;
end if;
end loop;
end Insert_Range_Checks;
procedure Insert_Valid_Check (Expr : Node_Id) is
Loc : constant Source_Ptr := Sloc (Expr);
Exp : Node_Id;
begin
if Range_Checks_Suppressed (Etype (Expr))
or else (not Validity_Checks_On)
then
return;
end if;
Exp := Expr;
while Nkind (Exp) = N_Type_Conversion loop
Exp := Expression (Exp);
end loop;
Validity_Checks_On := False;
Insert_Action
(Expr,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Not (Loc,
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks (Exp, Name_Req => True),
Attribute_Name => Name_Valid)),
Reason => CE_Invalid_Data),
Suppress => All_Checks);
if Is_Entity_Name (Exp)
and then Nkind (Parent (Entity (Exp))) = N_Object_Renaming_Declaration
then
declare
Old_Exp : constant Node_Id := Name (Parent (Entity (Exp)));
begin
if Nkind (Old_Exp) = N_Indexed_Component
and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp)))
then
Expand_Packed_Element_Reference (Old_Exp);
end if;
end;
end if;
Validity_Checks_On := True;
end Insert_Valid_Check;
procedure Install_Null_Excluding_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
procedure Mark_Non_Null;
procedure Mark_Non_Null is
begin
if Is_Entity_Name (N) then
Set_Is_Known_Null (Entity (N), False);
if Safe_To_Capture_Value (N, Entity (N)) then
Set_Is_Known_Non_Null (Entity (N), True);
end if;
end if;
end Mark_Non_Null;
begin
pragma Assert (Is_Access_Type (Typ));
if Inside_A_Generic then
return;
end if;
if Known_Non_Null (N) then
return;
end if;
if Known_Null (N) then
Apply_Compile_Time_Constraint_Error
(N,
"null value not allowed here?",
CE_Access_Check_Failed);
Mark_Non_Null;
return;
end if;
if Is_Entity_Name (N) then
Check_Unset_Reference (N);
end if;
if Access_Checks_Suppressed (Typ) then
return;
end if;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Eq (Loc,
Left_Opnd => Duplicate_Subexpr_Move_Checks (N),
Right_Opnd => Make_Null (Loc)),
Reason => CE_Access_Check_Failed));
Mark_Non_Null;
end Install_Null_Excluding_Check;
procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
Stat : constant Boolean := Is_Static_Expression (R_Cno);
Typ : constant Entity_Id := Etype (R_Cno);
begin
Rewrite (R_Cno,
Make_Raise_Constraint_Error (Loc,
Reason => CE_Range_Check_Failed));
Set_Analyzed (R_Cno);
Set_Etype (R_Cno, Typ);
Set_Raises_Constraint_Error (R_Cno);
Set_Is_Static_Expression (R_Cno, Stat);
end Install_Static_Check;
procedure Kill_All_Checks is
begin
if Debug_Flag_CC then
w ("Kill_All_Checks");
end if;
Num_Saved_Checks := 0;
for J in 1 .. Saved_Checks_TOS loop
Saved_Checks_Stack (J) := 0;
end loop;
end Kill_All_Checks;
procedure Kill_Checks (V : Entity_Id) is
begin
if Debug_Flag_CC then
w ("Kill_Checks for entity", Int (V));
end if;
for J in 1 .. Num_Saved_Checks loop
if Saved_Checks (J).Entity = V then
if Debug_Flag_CC then
w (" Checks killed for saved check ", J);
end if;
Saved_Checks (J).Killed := True;
end if;
end loop;
end Kill_Checks;
function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Length_Check);
else
return Scope_Suppress (Length_Check);
end if;
end Length_Checks_Suppressed;
function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Overflow_Check);
else
return Scope_Suppress (Overflow_Check);
end if;
end Overflow_Checks_Suppressed;
function Range_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty;
Warn_Node : Node_Id := Empty) return Check_Result
is
begin
return Selected_Range_Checks
(Ck_Node, Target_Typ, Source_Typ, Warn_Node);
end Range_Check;
function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Vax_Float (E) then
return True;
elsif Kill_Range_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Range_Check);
end if;
end if;
return Scope_Suppress (Range_Check);
end Range_Checks_Suppressed;
procedure Remove_Checks (Expr : Node_Id) is
Discard : Traverse_Result;
pragma Warnings (Off, Discard);
function Process (N : Node_Id) return Traverse_Result;
function Traverse is new Traverse_Func (Process);
function Process (N : Node_Id) return Traverse_Result is
begin
if Nkind (N) not in N_Subexpr then
return Skip;
end if;
Set_Do_Range_Check (N, False);
case Nkind (N) is
when N_And_Then =>
Discard := Traverse (Left_Opnd (N));
return Skip;
when N_Attribute_Reference =>
Set_Do_Overflow_Check (N, False);
when N_Function_Call =>
Set_Do_Tag_Check (N, False);
when N_Op =>
Set_Do_Overflow_Check (N, False);
case Nkind (N) is
when N_Op_Divide =>
Set_Do_Division_Check (N, False);
when N_Op_And =>
Set_Do_Length_Check (N, False);
when N_Op_Mod =>
Set_Do_Division_Check (N, False);
when N_Op_Or =>
Set_Do_Length_Check (N, False);
when N_Op_Rem =>
Set_Do_Division_Check (N, False);
when N_Op_Xor =>
Set_Do_Length_Check (N, False);
when others =>
null;
end case;
when N_Or_Else =>
Discard := Traverse (Left_Opnd (N));
return Skip;
when N_Selected_Component =>
Set_Do_Discriminant_Check (N, False);
when N_Type_Conversion =>
Set_Do_Length_Check (N, False);
Set_Do_Tag_Check (N, False);
Set_Do_Overflow_Check (N, False);
when others =>
null;
end case;
return OK;
end Process;
begin
Discard := Traverse (Expr);
end Remove_Checks;
function Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id) return Check_Result
is
Loc : constant Source_Ptr := Sloc (Ck_Node);
S_Typ : Entity_Id;
T_Typ : Entity_Id;
Expr_Actual : Node_Id;
Exptyp : Entity_Id;
Cond : Node_Id := Empty;
Do_Access : Boolean := False;
Wnode : Node_Id := Warn_Node;
Ret_Result : Check_Result := (Empty, Empty);
Num_Checks : Natural := 0;
procedure Add_Check (N : Node_Id);
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
function Length_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id;
function Length_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id;
procedure Add_Check (N : Node_Id) is
begin
if Present (N) then
if Num_Checks = 2 then
return;
end if;
pragma Assert (Num_Checks <= 1);
Num_Checks := Num_Checks + 1;
Ret_Result (Num_Checks) := N;
end if;
end Add_Check;
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
Pt : constant Entity_Id := Scope (Scope (E));
N : Node_Id;
E1 : Entity_Id := E;
begin
if Ekind (Scope (E)) = E_Record_Type
and then Has_Discriminants (Scope (E))
then
N := Build_Discriminal_Subtype_Of_Component (E);
if Present (N) then
Insert_Action (Ck_Node, N);
E1 := Defining_Identifier (N);
end if;
end if;
if Ekind (E1) = E_String_Literal_Subtype then
return
Make_Integer_Literal (Loc,
Intval => String_Literal_Length (E1));
elsif Ekind (Pt) = E_Protected_Type
and then Has_Discriminants (Pt)
and then Has_Completion (Pt)
and then not Inside_Init_Proc
then
declare
Indx_Type : Node_Id;
Lo : Node_Id;
Hi : Node_Id;
Do_Expand : Boolean := False;
begin
Indx_Type := First_Index (E);
for J in 1 .. Indx - 1 loop
Next_Index (Indx_Type);
end loop;
Get_Index_Bounds (Indx_Type, Lo, Hi);
if Nkind (Lo) = N_Identifier
and then Ekind (Entity (Lo)) = E_In_Parameter
then
Lo := Get_Discriminal (E, Lo);
Do_Expand := True;
end if;
if Nkind (Hi) = N_Identifier
and then Ekind (Entity (Hi)) = E_In_Parameter
then
Hi := Get_Discriminal (E, Hi);
Do_Expand := True;
end if;
if Do_Expand then
if not Is_Entity_Name (Lo) then
Lo := Duplicate_Subexpr_No_Checks (Lo);
end if;
if not Is_Entity_Name (Hi) then
Lo := Duplicate_Subexpr_No_Checks (Hi);
end if;
N :=
Make_Op_Add (Loc,
Left_Opnd =>
Make_Op_Subtract (Loc,
Left_Opnd => Hi,
Right_Opnd => Lo),
Right_Opnd => Make_Integer_Literal (Loc, 1));
return N;
else
N :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
New_Occurrence_Of (E1, Loc));
if Indx > 1 then
Set_Expressions (N, New_List (
Make_Integer_Literal (Loc, Indx)));
end if;
return N;
end if;
end;
else
N :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
New_Occurrence_Of (E1, Loc));
if Indx > 1 then
Set_Expressions (N, New_List (
Make_Integer_Literal (Loc, Indx)));
end if;
return N;
end if;
end Get_E_Length;
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_Length;
function Length_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id
is
begin
return
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (Typ, Indx),
Right_Opnd => Get_E_Length (Exptyp, Indx));
end Length_E_Cond;
function Length_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id
is
begin
return
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (Typ, Indx),
Right_Opnd => Get_N_Length (Expr, Indx));
end Length_N_Cond;
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
begin
return
(Nkind (L) = N_Integer_Literal
and then Nkind (R) = N_Integer_Literal
and then Intval (L) = Intval (R))
or else
(Is_Entity_Name (L)
and then Ekind (Entity (L)) = E_Constant
and then ((Is_Entity_Name (R)
and then Entity (L) = Entity (R))
or else
(Nkind (R) = N_Type_Conversion
and then Is_Entity_Name (Expression (R))
and then Entity (L) = Entity (Expression (R)))))
or else
(Is_Entity_Name (R)
and then Ekind (Entity (R)) = E_Constant
and then Nkind (L) = N_Type_Conversion
and then Is_Entity_Name (Expression (L))
and then Entity (R) = Entity (Expression (L)))
or else
(Is_Entity_Name (L)
and then Is_Entity_Name (R)
and then Entity (L) = Entity (R)
and then Ekind (Entity (L)) = E_In_Parameter
and then Inside_Init_Proc);
end Same_Bounds;
begin
if not Expander_Active then
return Ret_Result;
end if;
if Target_Typ = Any_Type
or else Target_Typ = Any_Composite
or else Raises_Constraint_Error (Ck_Node)
then
return Ret_Result;
end if;
if No (Wnode) then
Wnode := Ck_Node;
end if;
T_Typ := Target_Typ;
if No (Source_Typ) then
S_Typ := Etype (Ck_Node);
else
S_Typ := Source_Typ;
end if;
if S_Typ = Any_Type or else S_Typ = Any_Composite then
return Ret_Result;
end if;
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
T_Typ := Designated_Type (T_Typ);
Do_Access := True;
if Nkind (Ck_Node) = N_Null then
return Ret_Result;
end if;
end if;
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
if Is_Constrained (T_Typ) then
Freeze_Before (Ck_Node, T_Typ);
Expr_Actual := Get_Referenced_Object (Ck_Node);
Exptyp := Get_Actual_Subtype (Ck_Node);
if Is_Access_Type (Exptyp) then
Exptyp := Designated_Type (Exptyp);
end if;
if Nkind (Expr_Actual) = N_String_Literal
and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype
then
Cond :=
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (T_Typ, 1),
Right_Opnd =>
Make_Integer_Literal (Loc,
Intval =>
String_Literal_Length (Etype (Expr_Actual))));
elsif Is_Constrained (Exptyp) then
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
L_Index : Node_Id;
R_Index : Node_Id;
L_Low : Node_Id;
L_High : Node_Id;
R_Low : Node_Id;
R_High : Node_Id;
L_Length : Uint;
R_Length : Uint;
Ref_Node : Node_Id;
begin
if Is_Itype (Exptyp)
and then
Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package
and then
not In_Package_Body (Cunit_Entity (Current_Sem_Unit))
and then In_Open_Scopes (Scope (Exptyp))
then
Ref_Node := Make_Itype_Reference (Sloc (Ck_Node));
Set_Itype (Ref_Node, Exptyp);
Insert_Action (Ck_Node, Ref_Node);
end if;
L_Index := First_Index (T_Typ);
R_Index := First_Index (Exptyp);
for Indx in 1 .. Ndims loop
if not (Nkind (L_Index) = N_Raise_Constraint_Error
or else
Nkind (R_Index) = N_Raise_Constraint_Error)
then
Get_Index_Bounds (L_Index, L_Low, L_High);
Get_Index_Bounds (R_Index, R_Low, R_High);
if not Do_Access
and then Compile_Time_Known_Value (L_Low)
and then Compile_Time_Known_Value (L_High)
and then Compile_Time_Known_Value (R_Low)
and then Compile_Time_Known_Value (R_High)
then
if Expr_Value (L_High) >= Expr_Value (L_Low) then
L_Length := Expr_Value (L_High) -
Expr_Value (L_Low) + 1;
else
L_Length := UI_From_Int (0);
end if;
if Expr_Value (R_High) >= Expr_Value (R_Low) then
R_Length := Expr_Value (R_High) -
Expr_Value (R_Low) + 1;
else
R_Length := UI_From_Int (0);
end if;
if L_Length > R_Length then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "too few elements for}?", T_Typ));
elsif L_Length < R_Length then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "too many elements for}?", T_Typ));
end if;
elsif not
Subtypes_Statically_Match
(Etype (L_Index), Etype (R_Index))
and then not
(Same_Bounds (L_Low, R_Low)
and then Same_Bounds (L_High, R_High))
then
Evolve_Or_Else
(Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
end if;
Next (L_Index);
Next (R_Index);
end if;
end loop;
end;
else
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
begin
for Indx in 1 .. Ndims loop
Evolve_Or_Else
(Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
end loop;
end;
end if;
end if;
end if;
if Present (Cond) then
if Do_Access then
Cond := Guard_Access (Cond, Loc, Ck_Node);
end if;
Add_Check
(Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Length_Check_Failed));
end if;
return Ret_Result;
end Selected_Length_Checks;
function Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id) return Check_Result
is
Loc : constant Source_Ptr := Sloc (Ck_Node);
S_Typ : Entity_Id;
T_Typ : Entity_Id;
Expr_Actual : Node_Id;
Exptyp : Entity_Id;
Cond : Node_Id := Empty;
Do_Access : Boolean := False;
Wnode : Node_Id := Warn_Node;
Ret_Result : Check_Result := (Empty, Empty);
Num_Checks : Integer := 0;
procedure Add_Check (N : Node_Id);
function Discrete_Range_Cond
(Expr : Node_Id;
Typ : Entity_Id) return Node_Id;
function Discrete_Expr_Cond
(Expr : Node_Id;
Typ : Entity_Id) return Node_Id;
function Get_E_First_Or_Last
(E : Entity_Id;
Indx : Nat;
Nam : Name_Id) return Node_Id;
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
function Range_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
function Range_Equal_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id;
function Range_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id;
procedure Add_Check (N : Node_Id) is
begin
if Present (N) then
if Num_Checks = 2 then
return;
end if;
pragma Assert (Num_Checks <= 1);
Num_Checks := Num_Checks + 1;
Ret_Result (Num_Checks) := N;
end if;
end Add_Check;
function Discrete_Expr_Cond
(Expr : Node_Id;
Typ : Entity_Id) return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd =>
Convert_To (Base_Type (Typ),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Convert_To (Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_First))),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd =>
Convert_To (Base_Type (Typ),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Convert_To
(Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_Last))));
end Discrete_Expr_Cond;
function Discrete_Range_Cond
(Expr : Node_Id;
Typ : Entity_Id) return Node_Id
is
LB : Node_Id := Low_Bound (Expr);
HB : Node_Id := High_Bound (Expr);
Left_Opnd : Node_Id;
Right_Opnd : Node_Id;
begin
if Nkind (LB) = N_Identifier
and then Ekind (Entity (LB)) = E_Discriminant then
LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
end if;
if Nkind (HB) = N_Identifier
and then Ekind (Entity (HB)) = E_Discriminant then
HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
end if;
Left_Opnd :=
Make_Op_Lt (Loc,
Left_Opnd =>
Convert_To
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)),
Right_Opnd =>
Convert_To
(Base_Type (Typ), Get_E_First_Or_Last (Typ, 0, Name_First)));
if Base_Type (Typ) = Typ then
return Left_Opnd;
elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ)))
and then
Compile_Time_Known_Value (High_Bound (Scalar_Range
(Base_Type (Typ))))
then
if Is_Floating_Point_Type (Typ) then
if Expr_Value_R (High_Bound (Scalar_Range (Typ))) =
Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ))))
then
return Left_Opnd;
end if;
else
if Expr_Value (High_Bound (Scalar_Range (Typ))) =
Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ))))
then
return Left_Opnd;
end if;
end if;
end if;
Right_Opnd :=
Make_Op_Gt (Loc,
Left_Opnd =>
Convert_To
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)),
Right_Opnd =>
Convert_To
(Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_Last)));
return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
end Discrete_Range_Cond;
function Get_E_First_Or_Last
(E : Entity_Id;
Indx : Nat;
Nam : Name_Id) return Node_Id
is
N : Node_Id;
LB : Node_Id;
HB : Node_Id;
Bound : Node_Id;
begin
if Is_Array_Type (E) then
N := First_Index (E);
for J in 2 .. Indx loop
Next_Index (N);
end loop;
else
N := Scalar_Range (E);
end if;
if Nkind (N) = N_Subtype_Indication then
LB := Low_Bound (Range_Expression (Constraint (N)));
HB := High_Bound (Range_Expression (Constraint (N)));
elsif Is_Entity_Name (N) then
LB := Type_Low_Bound (Etype (N));
HB := Type_High_Bound (Etype (N));
else
LB := Low_Bound (N);
HB := High_Bound (N);
end if;
if Nam = Name_First then
Bound := LB;
else
Bound := HB;
end if;
if Nkind (Bound) = N_Identifier
and then Ekind (Entity (Bound)) = E_Discriminant
then
if Is_Concurrent_Record_Type (Scope (Entity (Bound))) then
declare
Tsk : constant Entity_Id :=
Corresponding_Concurrent_Type
(Scope (Entity (Bound)));
Disc : Entity_Id;
begin
if In_Open_Scopes (Tsk)
and then Has_Completion (Tsk)
then
Disc := First_Discriminant (Tsk);
while Present (Disc) loop
if Chars (Disc) = Chars (Entity (Bound)) then
Set_Scope (Discriminal (Disc), Tsk);
return New_Occurrence_Of (Discriminal (Disc), Loc);
end if;
Next_Discriminant (Disc);
end loop;
raise Program_Error;
else
return
New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
end if;
end;
else
return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
end if;
elsif Nkind (Bound) = N_Identifier
and then Ekind (Entity (Bound)) = E_In_Parameter
and then not Inside_Init_Proc
then
return Get_Discriminal (E, Bound);
elsif Nkind (Bound) = N_Integer_Literal then
return Make_Integer_Literal (Loc, Intval (Bound));
elsif Nkind (Bound) = N_Raise_Constraint_Error then
return New_Copy_Tree (Bound);
else
return Duplicate_Subexpr_No_Checks (Bound);
end if;
end Get_E_First_Or_Last;
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_First,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_First;
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Last,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_Last;
function Range_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_E_Cond;
function Range_Equal_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_Equal_E_Cond;
function Range_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat) return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Get_N_First (Expr, Indx),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Get_N_Last (Expr, Indx),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_N_Cond;
begin
if not Expander_Active then
return Ret_Result;
end if;
if Target_Typ = Any_Type
or else Target_Typ = Any_Composite
or else Raises_Constraint_Error (Ck_Node)
then
return Ret_Result;
end if;
if No (Wnode) then
Wnode := Ck_Node;
end if;
T_Typ := Target_Typ;
if No (Source_Typ) then
S_Typ := Etype (Ck_Node);
else
S_Typ := Source_Typ;
end if;
if S_Typ = Any_Type or else S_Typ = Any_Composite then
return Ret_Result;
end if;
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
T_Typ := Designated_Type (T_Typ);
Do_Access := True;
if Nkind (Ck_Node) = N_Null then
return Ret_Result;
end if;
end if;
if Nkind (Ck_Node) = N_Range then
if Ck_Node = Scalar_Range (T_Typ) then
return Ret_Result;
end if;
declare
T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
T_HB : constant Node_Id := Type_High_Bound (T_Typ);
LB : constant Node_Id := Low_Bound (Ck_Node);
HB : constant Node_Id := High_Bound (Ck_Node);
Null_Range : Boolean;
Out_Of_Range_L : Boolean;
Out_Of_Range_H : Boolean;
begin
if Compile_Time_Known_Value (LB)
and then Compile_Time_Known_Value (HB)
and then Compile_Time_Known_Value (T_LB)
and then Compile_Time_Known_Value (T_HB)
and then not Do_Access
then
if Is_Floating_Point_Type (S_Typ) then
Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
Out_Of_Range_L :=
(Expr_Value_R (LB) < Expr_Value_R (T_LB))
or else
(Expr_Value_R (LB) > Expr_Value_R (T_HB));
Out_Of_Range_H :=
(Expr_Value_R (HB) > Expr_Value_R (T_HB))
or else
(Expr_Value_R (HB) < Expr_Value_R (T_LB));
else
Null_Range := Expr_Value (HB) < Expr_Value (LB);
Out_Of_Range_L :=
(Expr_Value (LB) < Expr_Value (T_LB))
or else
(Expr_Value (LB) > Expr_Value (T_HB));
Out_Of_Range_H :=
(Expr_Value (HB) > Expr_Value (T_HB))
or else
(Expr_Value (HB) < Expr_Value (T_LB));
end if;
if not Null_Range then
if Out_Of_Range_L then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(Low_Bound (Ck_Node),
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static range out of bounds of}?", T_Typ));
end if;
end if;
if Out_Of_Range_H then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(High_Bound (Ck_Node),
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static range out of bounds of}?", T_Typ));
end if;
end if;
end if;
else
declare
LB : Node_Id := Low_Bound (Ck_Node);
HB : Node_Id := High_Bound (Ck_Node);
begin
if Nkind (LB) = N_Identifier
and then Ekind (Entity (LB)) = E_Discriminant
then
if Current_Scope = Scope (Entity (LB)) then
return Ret_Result;
else
LB :=
New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
end if;
end if;
if Nkind (HB) = N_Identifier
and then Ekind (Entity (HB)) = E_Discriminant
then
if Current_Scope = Scope (Entity (HB)) then
return Ret_Result;
else
HB :=
New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
end if;
end if;
Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
Set_Paren_Count (Cond, 1);
Cond :=
Make_And_Then (Loc,
Left_Opnd =>
Make_Op_Ge (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (HB),
Right_Opnd => Duplicate_Subexpr_No_Checks (LB)),
Right_Opnd => Cond);
end;
end if;
end;
elsif Is_Scalar_Type (S_Typ) then
if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
elsif Compile_Time_Known_Value (Ck_Node)
and then not Do_Access
then
declare
LB : constant Node_Id := Type_Low_Bound (T_Typ);
UB : constant Node_Id := Type_High_Bound (T_Typ);
Out_Of_Range : Boolean;
Static_Bounds : constant Boolean :=
Compile_Time_Known_Value (LB)
and Compile_Time_Known_Value (UB);
begin
if Static_Bounds then
if Is_Floating_Point_Type (S_Typ) then
Out_Of_Range :=
(Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
or else
(Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
else Out_Of_Range :=
Expr_Value (Ck_Node) < Expr_Value (LB)
or else
Expr_Value (Ck_Node) > Expr_Value (UB);
end if;
if Out_Of_Range then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(Ck_Node,
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static value out of range of}?", T_Typ));
end if;
end if;
else
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
end if;
end;
else
if not In_Subrange_Of (S_Typ, T_Typ) then
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
end if;
end if;
end if;
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
if Is_Constrained (T_Typ) then
Expr_Actual := Get_Referenced_Object (Ck_Node);
Exptyp := Get_Actual_Subtype (Expr_Actual);
if Is_Access_Type (Exptyp) then
Exptyp := Designated_Type (Exptyp);
end if;
if Nkind (Expr_Actual) = N_String_Literal then
null;
elsif Is_Constrained (Exptyp) then
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
L_Index : Node_Id;
R_Index : Node_Id;
L_Low : Node_Id;
L_High : Node_Id;
R_Low : Node_Id;
R_High : Node_Id;
begin
L_Index := First_Index (T_Typ);
R_Index := First_Index (Exptyp);
for Indx in 1 .. Ndims loop
if not (Nkind (L_Index) = N_Raise_Constraint_Error
or else
Nkind (R_Index) = N_Raise_Constraint_Error)
then
Get_Index_Bounds (L_Index, L_Low, L_High);
Get_Index_Bounds (R_Index, R_Low, R_High);
if not
Subtypes_Statically_Match
(Etype (L_Index), Etype (R_Index))
then
if Is_Constrained (T_Typ) then
Evolve_Or_Else
(Cond,
Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
else
Evolve_Or_Else
(Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
end if;
end if;
Next (L_Index);
Next (R_Index);
end if;
end loop;
end;
else
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
begin
for Indx in 1 .. Ndims loop
Evolve_Or_Else
(Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
end loop;
end;
end if;
else
if Nkind (Parent (Ck_Node)) = N_Type_Conversion then
declare
Opnd_Index : Node_Id;
Targ_Index : Node_Id;
begin
Opnd_Index
:= First_Index (Get_Actual_Subtype (Ck_Node));
Targ_Index := First_Index (T_Typ);
while Opnd_Index /= Empty loop
if Nkind (Opnd_Index) = N_Range then
if Is_In_Range
(Low_Bound (Opnd_Index), Etype (Targ_Index))
and then
Is_In_Range
(High_Bound (Opnd_Index), Etype (Targ_Index))
then
null;
elsif
Compile_Time_Known_Value (High_Bound (Opnd_Index))
and then
Compile_Time_Known_Value (Low_Bound (Opnd_Index))
and then
Expr_Value (High_Bound (Opnd_Index)) <
Expr_Value (Low_Bound (Opnd_Index))
then
null;
elsif Is_Out_Of_Range
(Low_Bound (Opnd_Index), Etype (Targ_Index))
or else
Is_Out_Of_Range
(High_Bound (Opnd_Index), Etype (Targ_Index))
then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "value out of range of}?", T_Typ));
else
Evolve_Or_Else
(Cond,
Discrete_Range_Cond
(Opnd_Index, Etype (Targ_Index)));
end if;
end if;
Next_Index (Opnd_Index);
Next_Index (Targ_Index);
end loop;
end;
end if;
end if;
end if;
if Present (Cond) then
if Do_Access then
Cond := Guard_Access (Cond, Loc, Ck_Node);
end if;
Add_Check
(Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Range_Check_Failed));
end if;
return Ret_Result;
end Selected_Range_Checks;
function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Storage_Check);
else
return Scope_Suppress (Storage_Check);
end if;
end Storage_Checks_Suppressed;
function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Kill_Tag_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Tag_Check);
end if;
end if;
return Scope_Suppress (Tag_Check);
end Tag_Checks_Suppressed;
end Checks;