Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Lattices / AbstractDomainExtensions.cs
1 // 
2 // AbstractDomainExtensions.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2012 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 Mono.CodeContracts.Static.DataStructures;
30
31 namespace Mono.CodeContracts.Static.Lattices {
32         public static class AbstractDomainExtensions {
33                 public static bool IsNormal<T> (this IAbstractDomain<T> domain)
34                 {
35                         return !domain.IsTop && !domain.IsBottom;
36                 }
37
38                 public static string BottomSymbolIfAny<T> (this IAbstractDomain<T> domain)
39                 {
40                         return domain.IsBottom ? "_|_" : string.Empty;
41                 }
42
43                 public static bool TryTrivialLessEqual<T> (this T left, T right, out bool result)
44                         where T : IAbstractDomain<T>
45                 {
46                         if (ReferenceEquals (left, right))
47                                 return true.With (true, out result);
48
49                         if (left.IsBottom)
50                                 return true.With (true, out result);
51
52                         if (left.IsTop)
53                                 return true.With (right.IsTop, out result);
54
55                         if (right.IsBottom)
56                                 return true.With (false, out result);
57
58                         if (right.IsTop)
59                                 return true.With (true, out result);
60
61                         return false.Without (out result);
62                 }
63
64                 public static bool TryTrivialJoin<T> (this T left, T right, out T result)
65                         where T : IAbstractDomain<T>
66                 {
67                         if (ReferenceEquals (left, right))
68                                 return true.With (left, out result);
69
70                         if (left.IsBottom)
71                                 return true.With (right, out result);
72
73                         if (left.IsTop)
74                                 return true.With (left, out result);
75
76                         if (right.IsBottom)
77                                 return true.With (left, out result);
78
79                         if (right.IsTop)
80                                 return true.With (right, out result);
81
82                         return false.Without (out result);
83                 }
84
85                 public static bool TryTrivialMeet<T> (this T left, T right, out T result)
86                         where T : IAbstractDomain<T>
87                 {
88                         if (ReferenceEquals (left, right))
89                                 return true.With (left, out result);
90
91                         if (left.IsBottom)
92                                 return true.With (left, out result);
93
94                         if (left.IsTop)
95                                 return true.With (right, out result);
96
97                         if (right.IsBottom)
98                                 return true.With (right, out result);
99
100                         if (right.IsTop)
101                                 return true.With (left, out result);
102
103                         return false.Without (out result);
104                 }
105         }
106 }