Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Analysis.ExpressionAnalysis.Expressions / BinaryExpr.cs
1 // 
2 // BinaryExpr.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.ControlFlow;
33 using Mono.CodeContracts.Static.DataStructures;
34
35 namespace Mono.CodeContracts.Static.Analysis.ExpressionAnalysis.Expressions {
36         class BinaryExpr<TSymbolicValue> : Expr<TSymbolicValue>
37                 where TSymbolicValue : IEquatable<TSymbolicValue> {
38                 
39                 public readonly BinaryOperator Operator;
40                 public readonly TSymbolicValue Left;
41                 public readonly TSymbolicValue Right;
42
43                 public BinaryExpr (TSymbolicValue left, TSymbolicValue right, BinaryOperator op)
44                 {
45                         this.Left = left;
46                         this.Right = right;
47                         this.Operator = op;
48                 }
49
50                 #region Overrides of Expression
51                 public override IEnumerable<TSymbolicValue> Variables
52                 {
53                         get
54                         {
55                                 yield return this.Left;
56                                 yield return this.Right;
57                         }
58                 }
59
60                 public override Result Decode<Data, Result, Visitor> (APC pc, TSymbolicValue dest, Visitor visitor, Data data)
61                 {
62                         return visitor.Binary (pc, this.Operator, dest, this.Left, this.Right, data);
63                 }
64
65                 public override Expr<TSymbolicValue> Substitute (IImmutableMap<TSymbolicValue, Sequence<TSymbolicValue>> substitutions)
66                 {
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);
69
70                         return null;
71                 }
72
73                 public override bool IsContained (IImmutableSet<TSymbolicValue> candidates)
74                 {
75                         return candidates.Contains (this.Left) || candidates.Contains (this.Right);
76                 }
77
78                 public override bool Contains (TSymbolicValue symbol)
79                 {
80                         return this.Left.Equals (symbol) || this.Right.Equals (symbol);
81                 }
82
83                 public override string ToString ()
84                 {
85                         return String.Format ("Binary({0} {1} {2})", this.Left, this.Operator, this.Right);
86                 }
87
88                 public override bool Equals (Expr<TSymbolicValue> other)
89                 {
90                         var binary = other as BinaryExpr<TSymbolicValue>;
91                         if (binary == null || binary.Operator != this.Operator)
92                                 return false;
93
94                         return binary.Left.Equals (this.Left) && binary.Right.Equals (this.Right);
95                 }
96                 #endregion
97         }
98 }