// // BasicFacts.cs // // Authors: // Alexander Chebaturkin (chebaturkin@gmail.com) // // Copyright (C) 2011 Alexander Chebaturkin // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using Mono.CodeContracts.Static.Analysis; using Mono.CodeContracts.Static.ControlFlow; using Mono.CodeContracts.Static.DataStructures; using Mono.CodeContracts.Static.Lattices; namespace Mono.CodeContracts.Static.Proving { class BasicFacts : IFactQuery { protected IExpressionContextProvider ContextProvider; protected IFactBase FactBase; protected Predicate isUnreachable; public BasicFacts (IExpressionContextProvider contextProvider, IFactBase factBase, Predicate isUnreachable) { this.ContextProvider = contextProvider; this.FactBase = factBase; this.isUnreachable = isUnreachable; } #region Implementation of IFactBase public FlatDomain IsNull(APC pc, Variable variable) { return this.FactBase.IsNull (pc, variable); } public FlatDomain IsNonNull(APC pc, Variable variable) { return this.FactBase.IsNonNull (pc, variable); } public bool IsUnreachable (APC pc) { if (this.isUnreachable != null && this.isUnreachable (pc)) return true; return this.FactBase.IsUnreachable (pc); } protected static bool TryVariable (BoxedExpression e, out Variable v) { object underlyingVariable = e.UnderlyingVariable; if (underlyingVariable is Variable) { v = (Variable) underlyingVariable; return true; } v = default(Variable); return false; } #endregion #region Implementation of IFactQuery public virtual FlatDomain IsNull(APC pc, BoxedExpression expr) { Variable v; if (TryVariable (expr, out v)) { FlatDomain outcome = this.FactBase.IsNull(pc, v); if (!outcome.IsTop) return outcome; } return ProofOutcome.Top; } public virtual FlatDomain IsNonNull(APC pc, BoxedExpression expr) { Variable v; if (TryVariable (expr, out v)) { FlatDomain outcome = this.FactBase.IsNonNull(pc, v); if (!outcome.IsTop) return outcome; } return ProofOutcome.Top; } public FlatDomain IsTrue(APC pc, BoxedExpression expr) { return IsNonZero (pc, expr); } public FlatDomain IsTrueImply(APC pc, Sequence positiveAssumptions, Sequence negativeAssumptions, BoxedExpression goal) { FlatDomain outcome = IsTrue(pc, goal); if (outcome.IsTrue() || outcome.IsBottom) return outcome; return ProofOutcome.Top; } public FlatDomain IsGreaterEqualToZero(APC pc, BoxedExpression expr) { return ProofOutcome.Top; } public FlatDomain IsLessThan(APC pc, BoxedExpression expr, BoxedExpression right) { return ProofOutcome.Top; } public FlatDomain IsNonZero(APC pc, BoxedExpression expr) { return IsNonNull (pc, expr); } #endregion } }