示例#1
0
        public bool HasConstraint(SymbolicValueConstraint constraint, ProgramState programState)
        {
            SymbolicValueConstraints constraints;

            return(programState.Constraints.TryGetValue(this, out constraints) &&
                   constraints.HasConstraint(constraint));
        }
示例#2
0
        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)}");
        }
示例#3
0
        internal SymbolicValueConstraints WithConstraint(SymbolicValueConstraint constraint)
        {
            var constraintsCopy = new Dictionary <Type, SymbolicValueConstraint>(constraints);

            SetConstraint(constraint, constraintsCopy);

            return(new SymbolicValueConstraints(constraintsCopy));
        }
示例#4
0
        internal T GetConstraintOrDefault <T>()
            where T : SymbolicValueConstraint
        {
            SymbolicValueConstraint constraint = null;

            return(constraints.TryGetValue(typeof(T), out constraint)
                ? (T)constraint
                : null);
        }
示例#5
0
        internal SymbolicValueConstraints WithoutConstraint(SymbolicValueConstraint constraint)
        {
            var constraintsCopy = new Dictionary <Type, SymbolicValueConstraint>(constraints);

            if (constraintsCopy.Remove(constraint.GetType()))
            {
                return(new SymbolicValueConstraints(constraintsCopy));
            }
            return(this);
        }
示例#6
0
        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)));
        }
示例#9
0
        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;
                }
            }
        }
示例#10
0
        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));
        }
示例#11
0
        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);
        }
示例#12
0
        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));
        }
示例#13
0
        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 });
        }
示例#14
0
        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)}.");
        }
示例#15
0
        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))));
        }
示例#16
0
        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));
        }
示例#17
0
        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)));
        }
示例#19
0
        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));
        }
示例#20
0
 public bool HasConstraint(SymbolicValueConstraint constraint, ProgramState programState)
 {
     return(programState.Constraints.ContainsKey(this) &&
            programState.Constraints[this].Implies(constraint));
 }
示例#21
0
 public bool TryGetConstraint(ProgramState programState, out SymbolicValueConstraint constraint)
 {
     return(programState.Constraints.TryGetValue(this, out constraint));
 }
示例#22
0
 private SymbolicValueConstraints(SymbolicValueConstraint constraint)
 {
     SetConstraint(constraint, this.constraints);
     hashCode = ComputeHashcode();
 }
示例#23
0
 public static SymbolicValueConstraints Create(SymbolicValueConstraint constraint)
 {
     return(new SymbolicValueConstraints(constraint));
 }
示例#24
0
 internal abstract IEnumerable <ProgramState> SetConstraint(BoolConstraint boolConstraint,
                                                            SymbolicValueConstraint leftConstraint, SymbolicValueConstraint rightConstraint,
                                                            ProgramState programState);
示例#25
0
        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) });
        }
示例#26
0
 public virtual IEnumerable <ProgramState> TrySetOppositeConstraint(SymbolicValueConstraint constraint,
                                                                    ProgramState programState)
 {
     return(TrySetConstraint(constraint?.OppositeForLogicalNot, programState));
 }
示例#27
0
        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)}");
        }
示例#28
0
        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));
        }