The base architecture for code-contracts analysis
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Analysis.StackAnalysis / StackInfo.cs
1 // 
2 // StackInfo.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 namespace Mono.CodeContracts.Static.Analysis.StackAnalysis {
30         struct StackInfo {
31                 private StackInfo<object> stack;
32
33                 public int Depth {
34                         get { return this.stack.Depth; }
35                 }
36
37                 public object this [int offset] {
38                         get { return this.stack [offset]; }
39                 }
40
41                 public StackInfo (int depth, int capacity)
42                 {
43                         this.stack = new StackInfo<object> (depth, capacity);
44                 }
45
46                 private StackInfo (StackInfo<object> copy)
47                 {
48                         this.stack = copy;
49                 }
50
51                 public StackInfo Pop (int slots)
52                 {
53                         return new StackInfo (this.stack.Pop (slots));
54                 }
55
56                 public StackInfo Push ()
57                 {
58                         this.stack.Push (null);
59                         return this;
60                 }
61
62                 public StackInfo PushThis ()
63                 {
64                         this.stack.Push (true);
65                         return this;
66                 }
67
68                 public StackInfo Push<T> (T target)
69                 {
70                         this.stack.Push (target);
71                         return this;
72                 }
73
74                 public void Adjust (int delta)
75                 {
76                         if (delta == 0)
77                                 return;
78                         if (delta < 0)
79                                 this.stack.Pop (-delta);
80                         for (int i = 0; i < delta; ++i)
81                                 Push ();
82                 }
83
84                 public bool IsThis (int offset)
85                 {
86                         return As<bool> (offset);
87                 }
88
89                 public bool TryGet<T> (int offset, out T target)
90                 {
91                         object o = this [offset];
92                         if (o is T) {
93                                 target = (T) o;
94                                 return true;
95                         }
96                         target = default(T);
97                         return false;
98                 }
99
100                 private T As<T> (int offset)
101                 {
102                         T res;
103                         TryGetTarget (offset, out res);
104                         return res;
105                 }
106
107                 public StackInfo Clone ()
108                 {
109                         return new StackInfo (new StackInfo<object> (this.stack));
110                 }
111
112                 public override string ToString ()
113                 {
114                         return this.stack.ToString ();
115                 }
116
117                 public bool TryGetTarget<T> (int offset, out T target)
118                 {
119                         if (this [offset] is T) {
120                                 target = (T) this [offset];
121                                 return true;
122                         }
123                         target = default(T);
124                         return false;
125                 }
126         }
127 }