/// <summary> /// Perform comparison of boolean values where only the right boolean operand is known. /// </summary> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="operation">Operation to be performed, only comparison gives a result.</param> /// <param name="rightOperand">Right boolean operand to compare.</param> /// <returns>If operation is comparison, it returns boolean result, otherwise <c>null</c>.</returns> public static Value LeftAbstractBooleanCompare(ISnapshotReadWrite snapshot, Operations operation, bool rightOperand) { switch (operation) { case Operations.Equal: return(snapshot.AnyBooleanValue); case Operations.NotEqual: return(snapshot.AnyBooleanValue); case Operations.LessThan: if (rightOperand) { return(snapshot.AnyBooleanValue); } else { return(snapshot.CreateBool(false)); } case Operations.LessThanOrEqual: if (rightOperand) { return(snapshot.CreateBool(true)); } else { return(snapshot.AnyBooleanValue); } case Operations.GreaterThan: if (rightOperand) { return(snapshot.CreateBool(false)); } else { return(snapshot.AnyBooleanValue); } case Operations.GreaterThanOrEqual: if (rightOperand) { return(snapshot.AnyBooleanValue); } else { return(snapshot.CreateBool(true)); } default: return(null); } }
/// <summary> /// Perform logical operation for given boolean operands. /// </summary> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="operation">Operation to be performed, only the logical one gives a result.</param> /// <param name="leftOperand">Left boolean operand of logical operation.</param> /// <param name="rightOperand">Right integer operand of logical operation.</param> /// <returns>If operation is logical, it returns boolean result, otherwise <c>null</c>.</returns> public static BooleanValue Logical(ISnapshotReadWrite snapshot, Operations operation, bool leftOperand, bool rightOperand) { switch (operation) { case Operations.And: return(snapshot.CreateBool(leftOperand && rightOperand)); case Operations.Or: return(snapshot.CreateBool(leftOperand || rightOperand)); case Operations.Xor: return(snapshot.CreateBool(leftOperand != rightOperand)); default: return(null); } }
/// <summary> /// Return result of comparison operation where the left operand is greater than the right operand. /// </summary> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="operation">Operation to be performed, only comparison gives a result.</param> /// <returns>If operation is comparison, it returns boolean result, otherwise <c>null</c>.</returns> /// <seealso cref="RightAlwaysGreater"/> public static BooleanValue LeftAlwaysGreater(ISnapshotReadWrite snapshot, Operations operation) { switch (operation) { case Operations.NotEqual: case Operations.GreaterThan: case Operations.GreaterThanOrEqual: return(snapshot.CreateBool(true)); case Operations.Equal: case Operations.LessThan: case Operations.LessThanOrEqual: return(snapshot.CreateBool(false)); default: return(null); } }
/// <summary> /// Perform logical OR for one given boolean operand. The other is unknown. /// </summary> /// <remarks> /// It does not matter whether the concrete operand is on left or right side, /// result is the same, because logical operation is commutative. /// </remarks> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="concreteOperand">One concrete boolean operand of OR logical operation.</param> /// <returns><c>true</c> whether the operand is <c>true</c>, otherwise abstract boolean.</returns> public static Value AbstractOr(ISnapshotReadWrite snapshot, bool concreteOperand) { if (concreteOperand) { return(snapshot.CreateBool(true)); } else { return(snapshot.AnyBooleanValue); } }
/// <summary> /// Compare whether concrete number is greater than or equal to number interval of the same type. /// </summary> /// <typeparam name="T">Comparable type of the operands.</typeparam> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="leftOperand">Left concrete number operand to compare.</param> /// <param name="rightOperand">Right number interval operand to compare.</param> /// <returns>Boolean value obtained by comparison of all value combinations.</returns> public static Value GreaterThanOrEqual <T>(ISnapshotReadWrite snapshot, T leftOperand, IntervalValue <T> rightOperand) where T : IComparable, IComparable <T>, IEquatable <T> { if (leftOperand.CompareTo(rightOperand.Start) >= 0) { if (leftOperand.CompareTo(rightOperand.End) >= 0) { return(snapshot.CreateBool(true)); } else { return(snapshot.AnyBooleanValue); } } else { return(snapshot.CreateBool(false)); } }
/// <summary> /// Compare concrete number to number interval of the same type for inequality. /// </summary> /// <typeparam name="T">Comparable type of the operands.</typeparam> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="leftOperand">Left concrete number operand to compare.</param> /// <param name="rightOperand">Right number interval operand to compare.</param> /// <returns>Boolean value obtained by comparison of all value combinations.</returns> public static Value NotEqual <T>(ISnapshotReadWrite snapshot, T leftOperand, IntervalValue <T> rightOperand) where T : IComparable, IComparable <T>, IEquatable <T> { if ((leftOperand.CompareTo(rightOperand.Start) < 0) || (leftOperand.CompareTo(rightOperand.End) > 0)) { return(snapshot.CreateBool(true)); } else { if (leftOperand.Equals(rightOperand.Start) && leftOperand.Equals(rightOperand.End)) { return(snapshot.CreateBool(false)); } else { return(snapshot.AnyBooleanValue); } } }
/// <summary> /// Compare values of the same type with the specific operation. /// </summary> /// <remarks> /// Note that the method is generic and can be applied for all types that defines comparing. /// There is one exception. The string values must be compared by the specialized method, /// because default comparing of strings differs from the way the PHP compares them. /// </remarks> /// <typeparam name="T">Comparable type of the operands.</typeparam> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="operation">Operation to be performed, only comparison gives a result.</param> /// <param name="leftOperand">Left operand to compare.</param> /// <param name="rightOperand">Right operand to compare.</param> /// <returns>If operation is comparison, it returns boolean result, otherwise <c>null</c>.</returns> public static BooleanValue Compare <T>(ISnapshotReadWrite snapshot, Operations operation, T leftOperand, T rightOperand) where T : IComparable, IComparable <T>, IEquatable <T> { switch (operation) { case Operations.Equal: return(snapshot.CreateBool(leftOperand.Equals(rightOperand))); case Operations.NotEqual: return(snapshot.CreateBool(!leftOperand.Equals(rightOperand))); case Operations.LessThan: return(snapshot.CreateBool(leftOperand.CompareTo(rightOperand) < 0)); case Operations.LessThanOrEqual: return(snapshot.CreateBool(leftOperand.CompareTo(rightOperand) <= 0)); case Operations.GreaterThan: return(snapshot.CreateBool(leftOperand.CompareTo(rightOperand) > 0)); case Operations.GreaterThanOrEqual: return(snapshot.CreateBool(leftOperand.CompareTo(rightOperand) >= 0)); default: return(null); } }
/// <summary> /// Compare string representations with the specific operation. /// </summary> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="operation">Operation to be performed, only comparison gives a result.</param> /// <param name="leftOperand">Left string operand to compare.</param> /// <param name="rightOperand">Right string operand to compare.</param> /// <returns>If operation is comparison, it returns boolean result, otherwise <c>null</c>.</returns> public static BooleanValue Compare(ISnapshotReadWrite snapshot, Operations operation, string leftOperand, string rightOperand) { switch (operation) { case Operations.Equal: return(snapshot.CreateBool(string.Equals(leftOperand, rightOperand, StringComparison.Ordinal))); case Operations.NotEqual: return(snapshot.CreateBool(!string.Equals(leftOperand, rightOperand, StringComparison.Ordinal))); case Operations.LessThan: return(snapshot.CreateBool(string.Compare(leftOperand, rightOperand, StringComparison.Ordinal) < 0)); case Operations.LessThanOrEqual: return(snapshot.CreateBool(string.Compare(leftOperand, rightOperand, StringComparison.Ordinal) <= 0)); case Operations.GreaterThan: return(snapshot.CreateBool(string.Compare(leftOperand, rightOperand, StringComparison.Ordinal) > 0)); case Operations.GreaterThanOrEqual: return(snapshot.CreateBool(string.Compare(leftOperand, rightOperand, StringComparison.Ordinal) >= 0)); default: return(null); } }
/// <summary> /// Perform logical XOR for given boolean and interval operands. /// </summary> /// <typeparam name="T">Type of values in interval.</typeparam> /// <param name="snapshot">Read-write memory snapshot used for fix-point analysis.</param> /// <param name="leftOperand">Left boolean operand of XOR logical operation.</param> /// <param name="rightOperand">Right interval operand of XOR logical operation.</param> /// <returns><c>true</c> whether operands are not equal, otherwise <c>false</c>.</returns> public static Value Xor <T>(ISnapshotReadWrite snapshot, bool leftOperand, IntervalValue <T> rightOperand) where T : IComparable, IComparable <T>, IEquatable <T> { bool convertedValue; if (TypeConversion.TryConvertToBoolean <T>(rightOperand, out convertedValue)) { return(snapshot.CreateBool(leftOperand != convertedValue)); } else { return(snapshot.AnyBooleanValue); } }