Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Analysis.HeapAnalysis / AbstractType.cs
1 // 
2 // AbstractType.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.IO;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.Lattices;
33
34 namespace Mono.CodeContracts.Static.Analysis.HeapAnalysis {
35         struct AbstractType : IAbstractDomainForEGraph<AbstractType>, IEquatable<AbstractType> {
36                 public static AbstractType TopValue = new AbstractType (FlatDomain<TypeNode>.TopValue, false);
37                 public static AbstractType BottomValue = new AbstractType (FlatDomain<TypeNode>.BottomValue, true);
38
39                 private FlatDomain<TypeNode> value;
40
41                 public AbstractType (FlatDomain<TypeNode> value, bool isZero) : this ()
42                 {
43                         IsZero = isZero;
44                         this.value = value;
45                 }
46
47                 public bool IsZero { get; private set; }
48
49                 public FlatDomain<TypeNode> Type
50                 {
51                         get { return this.value; }
52                 }
53
54                 public TypeNode ConcreteType
55                 {
56                         get { return this.value.Value; }
57                 }
58
59                 private static AbstractType ForManifestedFieldValue
60                 {
61                         get { return TopValue; }
62                 }
63
64                 public AbstractType ButZero
65                 {
66                         get { return new AbstractType (this.value, true); }
67                 }
68                 public AbstractType With (FlatDomain<TypeNode> type)
69                 {
70                         return new AbstractType (type, this.IsZero);
71                 }
72
73                 #region IAbstractDomainForEGraph<AbstractType> Members
74                 public AbstractType Top
75                 {
76                         get { return new AbstractType (FlatDomain<TypeNode>.TopValue, false); }
77                 }
78
79                 public AbstractType Bottom
80                 {
81                         get { return new AbstractType (FlatDomain<TypeNode>.BottomValue, true); }
82                 }
83
84                 public bool IsTop
85                 {
86                         get { return !IsZero && this.value.IsTop; }
87                 }
88
89                 public bool IsBottom
90                 {
91                         get { return IsZero && this.value.IsBottom; }
92                 }
93
94             public AbstractType Join(AbstractType that)
95             {
96                 throw new NotImplementedException();
97             }
98
99             public AbstractType Join (AbstractType that, bool widening, out bool weaker)
100                 {
101                         if (that.IsZero) {
102                                 weaker = false;
103                                 if (this.value.IsBottom)
104                                         return new AbstractType (that.value, IsZero);
105                                 return this;
106                         }
107                         if (IsZero) {
108                                 weaker = true;
109                                 if (that.value.IsBottom)
110                                         return new AbstractType (this.value, that.IsZero);
111                                 return that;
112                         }
113
114                         FlatDomain<TypeNode> resultType = this.value.Join (that.value, widening, out weaker);
115                         return new AbstractType (resultType, false);
116                 }
117
118             public AbstractType Widen(AbstractType that)
119             {
120                 throw new NotImplementedException();
121             }
122
123             public AbstractType Meet (AbstractType that)
124                 {
125                         return new AbstractType (this.value.Meet (that.value), IsZero || that.IsZero);
126                 }
127
128                 public bool LessEqual (AbstractType that)
129                 {
130                         if (IsZero)
131                                 return true;
132                         if (that.IsZero)
133                                 return false;
134
135                         return this.value.LessEqual (that.value);
136                 }
137
138                 public AbstractType ImmutableVersion ()
139                 {
140                         return this;
141                 }
142
143                 public AbstractType Clone ()
144                 {
145                         return this;
146                 }
147
148                 public void Dump (TextWriter tw)
149                 {
150                         if (IsZero)
151                                 tw.Write ("(Zero) ");
152
153                         this.value.Dump (tw);
154                 }
155
156                 public bool HasAllBottomFields
157                 {
158                         get { return IsZero; }
159                 }
160
161                 public AbstractType ForManifestedField ()
162                 {
163                         return ForManifestedFieldValue;
164                 }
165                 #endregion
166
167                 #region IEquatable<AbstractType> Members
168                 public bool Equals (AbstractType that)
169                 {
170                         return this.IsZero == that.IsZero && this.value.Equals (that.value);
171                 }
172                 #endregion
173
174                 public override string ToString ()
175                 {
176                         return (IsZero ? "(Zero) " : "") + this.value;
177                 }
178         }
179 }