5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2011 Alexander Chebaturkin
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.ControlFlow;
33 using Mono.CodeContracts.Static.DataStructures;
35 namespace Mono.CodeContracts.Static.Analysis.ExpressionAnalysis.Expressions {
36 class BinaryExpr<TSymbolicValue> : Expr<TSymbolicValue>
37 where TSymbolicValue : IEquatable<TSymbolicValue> {
39 public readonly BinaryOperator Operator;
40 public readonly TSymbolicValue Left;
41 public readonly TSymbolicValue Right;
43 public BinaryExpr (TSymbolicValue left, TSymbolicValue right, BinaryOperator op)
50 #region Overrides of Expression
51 public override IEnumerable<TSymbolicValue> Variables
55 yield return this.Left;
56 yield return this.Right;
60 public override Result Decode<Data, Result, Visitor> (APC pc, TSymbolicValue dest, Visitor visitor, Data data)
62 return visitor.Binary (pc, this.Operator, dest, this.Left, this.Right, data);
65 public override Expr<TSymbolicValue> Substitute (IImmutableMap<TSymbolicValue, LispList<TSymbolicValue>> substitutions)
67 if (substitutions.ContainsKey (this.Left) && substitutions.ContainsKey (this.Right))
68 return new BinaryExpr<TSymbolicValue> (substitutions [this.Left].Head, substitutions [this.Right].Head, this.Operator);
73 public override bool IsContained (IImmutableSet<TSymbolicValue> candidates)
75 return candidates.Contains (this.Left) || candidates.Contains (this.Right);
78 public override bool Contains (TSymbolicValue symbol)
80 return this.Left.Equals (symbol) || this.Right.Equals (symbol);
83 public override string ToString ()
85 return String.Format ("Binary({0} {1} {2})", this.Left, this.Operator, this.Right);
88 public override bool Equals (Expr<TSymbolicValue> other)
90 var binary = other as BinaryExpr<TSymbolicValue>;
91 if (binary == null || binary.Operator != this.Operator)
94 return binary.Left.Equals (this.Left) && binary.Right.Equals (this.Right);