public bool HasConstraint(SymbolicValueConstraint constraint, ProgramState programState) { SymbolicValueConstraints constraints; return(programState.Constraints.TryGetValue(this, out constraints) && constraints.HasConstraint(constraint)); }
public virtual IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { if (constraint == null) { return(new[] { currentProgramState }); } SymbolicValueConstraint oldConstraint; if (!currentProgramState.Constraints.TryGetValue(this, out oldConstraint)) { return(new[] { SetConstraint(constraint, currentProgramState) }); } var boolConstraint = constraint as BoolConstraint; if (boolConstraint != null) { return(TrySetConstraint(boolConstraint, oldConstraint, currentProgramState)); } var objectConstraint = constraint as ObjectConstraint; if (objectConstraint != null) { return(TrySetConstraint(objectConstraint, oldConstraint, currentProgramState)); } throw new NotSupportedException($"Neither {nameof(BoolConstraint)}, nor {nameof(ObjectConstraint)}"); }
internal SymbolicValueConstraints WithConstraint(SymbolicValueConstraint constraint) { var constraintsCopy = new Dictionary <Type, SymbolicValueConstraint>(constraints); SetConstraint(constraint, constraintsCopy); return(new SymbolicValueConstraints(constraintsCopy)); }
internal T GetConstraintOrDefault <T>() where T : SymbolicValueConstraint { SymbolicValueConstraint constraint = null; return(constraints.TryGetValue(typeof(T), out constraint) ? (T)constraint : null); }
internal SymbolicValueConstraints WithoutConstraint(SymbolicValueConstraint constraint) { var constraintsCopy = new Dictionary <Type, SymbolicValueConstraint>(constraints); if (constraintsCopy.Remove(constraint.GetType())) { return(new SymbolicValueConstraints(constraintsCopy)); } return(this); }
public override IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { var boolConstraint = constraint as BoolConstraint; if (boolConstraint == null) { return(new[] { currentProgramState }); } return(Operand.TrySetConstraint(boolConstraint.OppositeForLogicalNot, currentProgramState)); }
public override IEnumerable <ProgramState> TrySetOppositeConstraint(SymbolicValueConstraint constraint, ProgramState programState) { var negateConstraint = constraint?.OppositeForLogicalNot; if (constraint is BoolConstraint) { return(TrySetConstraint(negateConstraint, programState) .Union(TrySetConstraint(NullableValueConstraint.NoValue, programState))); } return(TrySetConstraint(negateConstraint, programState)); }
internal override IEnumerable <ProgramState> SetConstraint(BoolConstraint boolConstraint, SymbolicValueConstraint leftConstraint, SymbolicValueConstraint rightConstraint, ProgramState programState) { if (boolConstraint == BoolConstraint.True) { return(RightOperand.TrySetConstraint(leftConstraint, programState) .SelectMany(ps => LeftOperand.TrySetConstraint(rightConstraint, ps))); } return(RightOperand.TrySetConstraint(leftConstraint?.OppositeForLogicalNot, programState) .SelectMany(ps => LeftOperand.TrySetConstraint(rightConstraint?.OppositeForLogicalNot, ps))); }
private static void SetConstraint(SymbolicValueConstraint constraint, Dictionary <Type, SymbolicValueConstraint> constraints) { constraints[constraint.GetType()] = constraint; if (constraint is BoolConstraint || constraint is DisposableConstraint) { constraints[typeof(ObjectConstraint)] = ObjectConstraint.NotNull; if (constraints.ContainsKey(typeof(NullableValueConstraint))) { constraints[typeof(NullableValueConstraint)] = NullableValueConstraint.HasValue; } } }
internal virtual ProgramState RemoveConstraint(SymbolicValueConstraint constraint, ProgramState programState) { if (constraint == null) { return(programState); } var updatedConstraintsMap = RemoveConstraintForSymbolicValue(this, constraint, programState.Constraints); return(new ProgramState( programState.Values, updatedConstraintsMap, programState.ProgramPointVisitCounts, programState.ExpressionStack, programState.Relationships)); }
private ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> AddConstraintTo <TRelationship>( SymbolicValueConstraint constraint, ProgramState programState, ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> constraintsMap) where TRelationship : BinaryRelationship { var newConstraintsMap = constraintsMap; var equalSymbols = programState.Relationships .OfType <TRelationship>() .Select(r => GetOtherOperandFromMatchingRelationship(r)) .Where(e => e != null); foreach (var equalSymbol in equalSymbols.Where(e => !e.HasConstraint(constraint, programState))) { newConstraintsMap = AddConstraintForSymbolicValue(equalSymbol, constraint, newConstraintsMap); } return(newConstraintsMap); }
public override IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { var boolConstraint = constraint as BoolConstraint; if (boolConstraint == null) { return(new[] { currentProgramState }); } SymbolicValueConstraints oldConstraints; BoolConstraint oldBoolConstraint = null; if (TryGetConstraints(currentProgramState, out oldConstraints)) { oldBoolConstraint = oldConstraints.GetConstraintOrDefault <BoolConstraint>(); } if (oldBoolConstraint != null /* could also be ObjectConstraint.NotNull, which can be overridden */ && oldBoolConstraint != boolConstraint) { return(Enumerable.Empty <ProgramState>()); } SymbolicValueConstraints leftConstraints; var leftHasConstraint = LeftOperand.TryGetConstraints(currentProgramState, out leftConstraints); SymbolicValueConstraints rightConstraints; var rightHasConstraint = RightOperand.TryGetConstraints(currentProgramState, out rightConstraints); var relationship = GetRelationship(boolConstraint); var newProgramState = currentProgramState.TrySetRelationship(relationship); if (newProgramState == null) { return(Enumerable.Empty <ProgramState>()); } if (!rightHasConstraint && !leftHasConstraint) { return(new[] { newProgramState }); } return(SetConstraint(boolConstraint, leftConstraints, rightConstraints, newProgramState)); }
public override IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { var boolConstraint = constraint as BoolConstraint; if (boolConstraint == null) { return(new[] { currentProgramState }); } var relationship = GetRelationship(boolConstraint); var newProgramState = currentProgramState.TrySetRelationship(relationship); if (newProgramState == null) { return(Enumerable.Empty <ProgramState>()); } return(new[] { newProgramState }); }
public virtual IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { if (constraint == null) { return(new[] { currentProgramState }); } SymbolicValueConstraints oldConstraints; if (!currentProgramState.Constraints.TryGetValue(this, out oldConstraints)) { return(new[] { SetConstraint(constraint, currentProgramState) }); } var boolConstraint = constraint as BoolConstraint; if (boolConstraint != null) { return(TrySetConstraint(boolConstraint, oldConstraints, currentProgramState)); } var objectConstraint = constraint as ObjectConstraint; if (objectConstraint != null) { return(TrySetConstraint(objectConstraint, oldConstraints, currentProgramState)); } if (constraint is NullableValueConstraint || constraint is DisposableConstraint || constraint is CollectionCapacityConstraint) { return(new[] { currentProgramState }); } throw new NotSupportedException($"Neither one of {nameof(BoolConstraint)}, {nameof(ObjectConstraint)}, " + $"{nameof(ObjectConstraint)}, {nameof(DisposableConstraint)}, {nameof(CollectionCapacityConstraint)}."); }
public override IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { var boolConstraint = constraint as BoolConstraint; if (boolConstraint == null) { return(new[] { currentProgramState }); } if (boolConstraint == BoolConstraint.False) { return(LeftOperand.TrySetConstraint(BoolConstraint.False, currentProgramState) .SelectMany(ps => RightOperand.TrySetConstraint(BoolConstraint.False, ps))); } return(LeftOperand.TrySetConstraint(BoolConstraint.True, currentProgramState) .SelectMany(ps => RightOperand.TrySetConstraint(BoolConstraint.False, ps)) .Union(LeftOperand.TrySetConstraint(BoolConstraint.False, currentProgramState) .SelectMany(ps => RightOperand.TrySetConstraint(BoolConstraint.True, ps))) .Union(LeftOperand.TrySetConstraint(BoolConstraint.True, currentProgramState) .SelectMany(ps => RightOperand.TrySetConstraint(BoolConstraint.True, ps)))); }
internal ProgramState SetConstraint(SymbolicValueConstraint constraint, ProgramState programState) { if (constraint == null) { return(programState); } var newConstraints = programState.Constraints.SetItem(this, constraint); newConstraints = AddConstraintTo <EqualsRelationship>(constraint, programState, newConstraints); if (constraint is BoolConstraint) { newConstraints = AddConstraintTo <NotEqualsRelationship>(constraint.OppositeForLogicalNot, programState, newConstraints); } return(new ProgramState( programState.Values, newConstraints, programState.ProgramPointVisitCounts, programState.ExpressionStack, programState.Relationships)); }
internal virtual ProgramState SetConstraint(SymbolicValueConstraint constraint, ProgramState programState) { if (constraint == null) { return(programState); } var updatedConstraintsMap = AddConstraintForSymbolicValue(this, constraint, programState.Constraints); updatedConstraintsMap = AddConstraintTo <EqualsRelationship>(constraint, programState, updatedConstraintsMap); if (constraint is BoolConstraint) { updatedConstraintsMap = AddConstraintTo <NotEqualsRelationship>(constraint.OppositeForLogicalNot, programState, updatedConstraintsMap); } return(new ProgramState( programState.Values, updatedConstraintsMap, programState.ProgramPointVisitCounts, programState.ExpressionStack, programState.Relationships)); }
public override IEnumerable <ProgramState> TrySetConstraint(SymbolicValueConstraint constraint, ProgramState currentProgramState) { if (constraint == null) { return(new[] { currentProgramState }); } if (constraint is ObjectConstraint) { var optionalConstraint = constraint == ObjectConstraint.Null ? NullableValueConstraint.NoValue : NullableValueConstraint.HasValue; return(TrySetConstraint(optionalConstraint, currentProgramState)); } var oldConstraint = currentProgramState.Constraints.GetValueOrDefault(this)?.GetConstraintOrDefault <NullableValueConstraint>(); if (constraint is NullableValueConstraint) { if (oldConstraint == null) { return(new[] { SetConstraint(constraint, currentProgramState) }); } if (oldConstraint != constraint) { return(Enumerable.Empty <ProgramState>()); } return(new[] { currentProgramState }); } return(TrySetConstraint(NullableValueConstraint.HasValue, currentProgramState) .SelectMany(ps => WrappedValue.TrySetConstraint(constraint, ps))); }
private ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> AddConstraintForSymbolicValue(SymbolicValue symbolicValue, SymbolicValueConstraint constraint, ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> constraintMap) { var constraints = constraintMap.GetValueOrDefault(symbolicValue); var updatedConstraints = constraints != null ? constraints.WithConstraint(constraint) : SymbolicValueConstraints.Create(constraint); return(constraintMap.SetItem(symbolicValue, updatedConstraints)); }
public bool HasConstraint(SymbolicValueConstraint constraint, ProgramState programState) { return(programState.Constraints.ContainsKey(this) && programState.Constraints[this].Implies(constraint)); }
public bool TryGetConstraint(ProgramState programState, out SymbolicValueConstraint constraint) { return(programState.Constraints.TryGetValue(this, out constraint)); }
private SymbolicValueConstraints(SymbolicValueConstraint constraint) { SetConstraint(constraint, this.constraints); hashCode = ComputeHashcode(); }
public static SymbolicValueConstraints Create(SymbolicValueConstraint constraint) { return(new SymbolicValueConstraints(constraint)); }
internal abstract IEnumerable <ProgramState> SetConstraint(BoolConstraint boolConstraint, SymbolicValueConstraint leftConstraint, SymbolicValueConstraint rightConstraint, ProgramState programState);
private IEnumerable <ProgramState> TrySetConstraint(BoolConstraint boolConstraint, SymbolicValueConstraint oldConstraint, ProgramState currentProgramState) { if (oldConstraint == ObjectConstraint.Null) { // It was null, and now it should be true or false return(Enumerable.Empty <ProgramState>()); } var oldBoolConstraint = oldConstraint as BoolConstraint; if (oldBoolConstraint != null && oldBoolConstraint != boolConstraint) { return(Enumerable.Empty <ProgramState>()); } // Either same bool constraint, or previously not null, and now a bool constraint return(new[] { SetConstraint(boolConstraint, currentProgramState) }); }
public virtual IEnumerable <ProgramState> TrySetOppositeConstraint(SymbolicValueConstraint constraint, ProgramState programState) { return(TrySetConstraint(constraint?.OppositeForLogicalNot, programState)); }
private IEnumerable <ProgramState> TrySetConstraint(ObjectConstraint objectConstraint, SymbolicValueConstraint oldConstraint, ProgramState currentProgramState) { var oldBoolConstraint = oldConstraint as BoolConstraint; if (oldBoolConstraint != null) { if (objectConstraint == ObjectConstraint.Null) { return(Enumerable.Empty <ProgramState>()); } return(new[] { currentProgramState }); } var oldObjectConstraint = oldConstraint as ObjectConstraint; if (oldObjectConstraint != null) { if (oldObjectConstraint != objectConstraint) { return(Enumerable.Empty <ProgramState>()); } return(new[] { SetConstraint(objectConstraint, currentProgramState) }); } throw new NotSupportedException($"Neither {nameof(BoolConstraint)}, nor {nameof(ObjectConstraint)}"); }
private ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> RemoveConstraintForSymbolicValue(SymbolicValue symbolicValue, SymbolicValueConstraint constraint, ImmutableDictionary <SymbolicValue, SymbolicValueConstraints> constraintMap) { var constraints = constraintMap.GetValueOrDefault(symbolicValue); if (constraints == null) { return(constraintMap); } var updatedConstraints = constraints.WithoutConstraint(constraint); return(constraintMap.SetItem(symbolicValue, updatedConstraints)); }