//[SuppressMessage("Microsoft.Contracts", "Ensures-22-42", Justification="Depends on overriding")] public sealed override bool Visit(Expression exp, Void data) { Contract.Ensures(!Contract.Result <bool>() || result != null); result = default(NormalizedExpression <Variable>); return(base.Visit(exp, data)); }
public TryConvertRead(IExpressionDecoder <Variable, Expression> decoder) : base(decoder) { Contract.Requires(decoder != null); result = default(NormalizedExpression <Variable>); }
public SegmentLimit <Variable> Add(NormalizedExpression <Variable> element) { Contract.Ensures(Contract.Result <SegmentLimit <Variable> >() != null); var newSet = new Set <NormalizedExpression <Variable> >(); newSet.AddRange(expressions); newSet.Add(element); return(new SegmentLimit <Variable>(newSet, this.IsConditional)); }
public override bool VisitConstant(Expression left, Void data) { Int32 value; if (this.Decoder.TryValueOf <Int32>(left, ExpressionType.Int32, out value)) { result = NormalizedExpression <Variable> .For(value); return(true); } return(false); }
public override bool VisitAddition(Expression left, Expression right, Expression original, Void data) { NormalizedExpression <Variable> leftNorm, rightNorm; if (this.TryConvert(left, out leftNorm)) { Contract.Assert(leftNorm != null); if (this.TryConvert(right, out rightNorm)) { Contract.Assert(rightNorm != null); // All the cases. We declare all the variables here, // so to leverage the definite assignment analysis of // the compiler to check we are doing everything right int leftConst, rightCont; Variable leftVar, rightVar; if (leftNorm.IsConstant(out leftConst)) { // K + K if (rightNorm.IsConstant(out rightCont)) { result = NormalizedExpression <Variable> .For(leftConst + rightCont); // Can overflow, we do not care has it may be the concrete semantics return(true); } // K + V if (rightNorm.IsVariable(out rightVar)) { result = NormalizedExpression <Variable> .For(rightVar, leftConst); return(true); } } else if (leftNorm.IsVariable(out leftVar)) { // V + K if (rightNorm.IsConstant(out rightCont)) { result = NormalizedExpression <Variable> .For(leftVar, rightCont); return(true); } } } } return(Default(data)); }
public override void AssignInParallel(Dictionary <Variable, FList <Variable> > sourcesToTargets, Converter <Variable, Expression> convert) { var newMaps = this.Factory(); foreach (var pair in this.Elements) { FList <Variable> newNames; if (sourcesToTargets.TryGetValue(pair.Key, out newNames)) { Contract.Assume(pair.Value != null); ArraySegmentation <AbstractDomain, Variable, Expression> renamedElement; if (pair.Value.TryAssignInParallelFunctional(sourcesToTargets, convert, out renamedElement)) { renamedElement = renamedElement.Simplify(); foreach (var name in newNames.GetEnumerable()) { newMaps.AddElement(name, renamedElement); } } } // else, it is abstracted away... } // We need constants.... // So we want to add all the knowledge const == var the possible var decoder = this.ExpressionManager.Decoder; foreach (var pair in sourcesToTargets) { int value; var sourceExp = convert(pair.Key); if (decoder.TryValueOf(sourceExp, ExpressionType.Int32, out value)) { var k = NormalizedExpression <Variable> .For(value); var eq = new Set <NormalizedExpression <Variable> >(); foreach (var v in pair.Value.GetEnumerable()) { eq.Add(NormalizedExpression <Variable> .For(v)); } newMaps = newMaps.TestTrueEqualAsymmetric(eq, k); } } this.CopyAndTransferOwnership(newMaps); }
TestTrueEqualAsymmetric(NormalizedExpression <Variable> v, NormalizedExpression <Variable> normExpression) { Contract.Requires(v != null); Contract.Ensures(Contract.Result <ArraySegmentationEnvironment <AbstractDomain, Variable, Expression> >() != null); var result = Factory(); foreach (var pair in this.Elements) { Contract.Assume(pair.Value != null); result[pair.Key] = pair.Value.TestTrueEqualAsymmetric(v, normExpression); } return(result); }
static public bool TryConvertFrom <Expression>( Expression exp, IExpressionDecoder <Variable, Expression> decoder, out NormalizedExpression <Variable> result) { Contract.Requires(exp != null); if (decoder == null) { result = default(NormalizedExpression <Variable>); return(false); } var reader = new TryConvertRead <Expression>(decoder); return(reader.TryConvert(exp, out result)); }
public bool TryConvert(Expression exp, out NormalizedExpression <Variable> result) { Contract.Requires(exp != null); Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out result) != null); Contract.Ensures(this.result == Contract.ValueAtReturn(out result)); if (this.Visit(exp, Void.Value)) { Contract.Assert(this.result != null); result = this.result; return(true); } this.result = result = default(NormalizedExpression <Variable>); return(false); }
public static string PrettyPrint <Variable>(this NormalizedExpression <Variable> exp, Func <Variable, string> variableNamePrettyPrinter) { Contract.Requires(exp != null); Contract.Requires(variableNamePrettyPrinter != null); Variable x; int k; if (exp.IsConstant(out k)) { return(k.ToString()); } else if (exp.IsVariable(out x)) { return(variableNamePrettyPrinter(x)); } else if (exp.IsAddition(out x, out k)) { return(variableNamePrettyPrinter(x) + " + " + k.ToString()); } return(null); }
static public bool TryConvertFrom <Expression>( Expression exp, ExpressionManagerWithEncoder <Variable, Expression> expManager, out NormalizedExpression <Variable> result) { Contract.Requires(exp != null); Contract.Requires(expManager != null); if (TryConvertFrom(exp, expManager.Decoder, out result)) { return(true); } //if (expManager.Encoder != null) { Polynomial <Variable, Expression> normalized; if (Polynomial <Variable, Expression> .TryToPolynomialForm(exp, expManager.Decoder, out normalized)) { return(TryConvertFrom(normalized.ToPureExpression(expManager.Encoder), expManager.Decoder, out result)); } } result = default(NormalizedExpression <Variable>); return(false); }
public bool Contains(NormalizedExpression <Variable> exp) { return(expressions.Contains(exp)); }
public SegmentLimit(NormalizedExpression <Variable> expression, bool isConditional) : this(new Set <NormalizedExpression <Variable> >() { expression }, isConditional) { Contract.Requires(expression != null); }
public SegmentLimit <Variable> AssignInParallel <Expression>(Dictionary <Variable, FList <Variable> > sourceToTargets, Converter <Variable, Expression> convert, IExpressionDecoder <Variable, Expression> decoder) { #region Contracts Contract.Requires(sourceToTargets != null); Contract.Requires(convert != null); Contract.Requires(decoder != null); Contract.Ensures(Contract.Result <SegmentLimit <Variable> >() != null); #endregion var newSet = new Set <NormalizedExpression <Variable> >(); foreach (var x in expressions) { Contract.Assume(x != null); FList <Variable> targets; int value; Variable var; if (x.IsConstant(out value)) { newSet.Add(x); } else if (x.IsVariable(out var)) { if (sourceToTargets.TryGetValue(var, out targets)) { Contract.Assume(targets != null); foreach (var newName in targets.GetEnumerable()) { newSet.Add(NormalizedExpression <Variable> .For(newName)); } } } else if (x.IsAddition(out var, out value)) { if (sourceToTargets.TryGetValue(var, out targets)) { Contract.Assume(targets != null); foreach (var newName in targets.GetEnumerable()) { newSet.Add(NormalizedExpression <Variable> .For(newName, value)); } } // This is a special case to handle renaming for a[p++] = ... // We have (var + value) --> var Variable source; if (IsATarget(var, sourceToTargets, out source)) { Variable v; int k; if (decoder.TryMatchVarPlusConst(convert(source), out v, out k) && v.Equals(var) && k == value) { newSet.Add(NormalizedExpression <Variable> .For(var)); } } } } return(new SegmentLimit <Variable>(newSet, this.IsConditional)); }
protected override bool Default(Void data) { result = default(NormalizedExpression <Variable>); return(false); }
public override bool VisitVariable(Variable variable, Expression original, Void data) { result = NormalizedExpression <Variable> .For(variable); return(true); }