File: GenericExpressionVisitor.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (338 lines) | stat: -rw-r--r-- 16,979 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// GenericExpressionVisitor.cs
// 
// Authors:
// 	Alexander Chebaturkin (chebaturkin@gmail.com)
// 
// Copyright (C) 2012 Alexander Chebaturkin
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 

using System;

using Mono.CodeContracts.Static.DataStructures;

using Op = Mono.CodeContracts.Static.Analysis.Numerical.ExpressionOperator;

namespace Mono.CodeContracts.Static.Analysis.Numerical {
        abstract class GenericExpressionVisitor<In, Out, Var, Expr> {
                protected readonly IExpressionDecoder<Var, Expr> Decoder;

                protected GenericExpressionVisitor (IExpressionDecoder<Var, Expr> decoder)
                {
                        Decoder = decoder;
                }

                public virtual Out Visit (Expr expr, In data)
                {
                        var op = Decoder.OperatorFor (expr);
                        switch (op) {
                        case Op.Constant:
                                return VisitConstant (expr, data);
                        case Op.Variable:
                                return VisitVariable (Decoder.UnderlyingVariable (expr), expr, data);
                        case Op.Not:
                                return DispatchVisitNot (Decoder.LeftExpressionFor (expr), data);
                        case Op.And:
                                return VisitAnd (Decoder.LeftExpressionFor (expr), Decoder.RightExpressionFor (expr),
                                                 expr, data);
                        case Op.Or:
                                return VisitOr (Decoder.LeftExpressionFor (expr), Decoder.RightExpressionFor (expr),
                                                expr, data);
                        case Op.Xor:
                                return VisitXor (Decoder.LeftExpressionFor (expr), Decoder.RightExpressionFor (expr),
                                                 expr, data);
                        case Op.LogicalAnd:
                                return VisitLogicalAnd (Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);
                        case Op.LogicalOr:
                                return VisitLogicalOr (Decoder.LeftExpressionFor (expr),
                                                       Decoder.RightExpressionFor (expr), expr, data);
                        case Op.NotEqual:
                                return VisitNotEqual (Decoder.LeftExpressionFor (expr),
                                                      Decoder.RightExpressionFor (expr), expr, data);

                        case Op.Equal:
                        case Op.Equal_Obj:
                                return DispatchVisitEqual (op, Decoder.LeftExpressionFor (expr),
                                                           Decoder.RightExpressionFor (expr), expr, data);

                        case Op.LessThan:
                                return DispatchCompare (VisitLessThan, Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);
                        case Op.LessEqualThan:
                                return DispatchCompare (VisitLessEqualThan, Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);
                        case Op.GreaterThan:
                                return DispatchCompare (VisitGreaterThan, Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);
                        case Op.GreaterEqualThan:
                                return DispatchCompare (VisitGreaterEqualThan, Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);

                        case Op.Add:
                                return VisitAddition (Decoder.LeftExpressionFor (expr),
                                                      Decoder.RightExpressionFor (expr), expr, data);
                        case Op.Div:
                                return VisitDivision (Decoder.LeftExpressionFor (expr),
                                                      Decoder.RightExpressionFor (expr), expr, data);
                        case Op.Sub:
                                return VisitSubtraction (Decoder.LeftExpressionFor (expr),
                                                         Decoder.RightExpressionFor (expr), expr, data);
                        case Op.Mod:
                                return VisitModulus (Decoder.LeftExpressionFor (expr), Decoder.RightExpressionFor (expr),
                                                     expr, data);
                        case Op.Mult:
                                return VisitMultiply (Decoder.LeftExpressionFor (expr),
                                                      Decoder.RightExpressionFor (expr), expr, data);

                        case Op.SizeOf:
                                return VisitSizeOf (Decoder.LeftExpressionFor (expr), data);
                        case Op.UnaryMinus:
                                return VisitUnaryMinus (Decoder.LeftExpressionFor (expr), expr, data);
                        case Op.LogicalNot:
                                return VisitLogicalNot (Decoder.LeftExpressionFor (expr), expr, data);
                        case Op.Unknown:
                                return VisitUnknown (expr, data);
                        default:
                                throw new ArgumentOutOfRangeException ();
                        }
                }

                public virtual Out VisitVariable (Var var, Expr expr, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitConstant (Expr expr, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitLogicalNot (Expr left, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitUnaryMinus (Expr left, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitNot (Expr expr, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitSizeOf (Expr expr, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitUnknown (Expr expr, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitLessThan (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitLessEqualThan (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitGreaterThan (Expr left, Expr right, Expr original, In data)
                {
                        return DispatchCompare (VisitLessEqualThan, right, left, original, data);
                }

                public virtual Out VisitGreaterEqualThan (Expr left, Expr right, Expr original, In data)
                {
                        return DispatchCompare (VisitLessThan, right, left, original, data);
                }

                public virtual Out VisitNotEqual (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitAddition (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitSubtraction (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitMultiply (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitModulus (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitDivision (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitEqual (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitLogicalOr (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitLogicalAnd (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitXor (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitOr (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                public virtual Out VisitAnd (Expr left, Expr right, Expr original, In data)
                {
                        return Default (data);
                }

                protected virtual bool TryPolarity (Expr expr, In data, out bool shouldNegate)
                {
                        if (Decoder.IsConstant (expr)) {
                                int intValue;
                                if (Decoder.TryValueOf (expr, ExpressionType.Int32, out intValue))
                                        return true.With (intValue == 0, out shouldNegate);

                                bool boolValue;
                                if (Decoder.TryValueOf (expr, ExpressionType.Bool, out boolValue))
                                        return true.With (boolValue, out shouldNegate);
                        }

                        return false.Without (out shouldNegate);
                }

                protected delegate Out CompareVisitor (Expr left, Expr right, Expr original, In data);

                protected virtual Out DispatchCompare (CompareVisitor cmp, Expr left, Expr right, Expr original, In data)
                {
                        if (Decoder.IsConstant (left) && Decoder.OperatorFor (right) == ExpressionOperator.Sub)
                                // const OP (a - b)
                        {
                                int num;
                                if (Decoder.TryValueOf (left, ExpressionType.Int32, out num) && num == 0)
                                        // 0 OP (a-b) ==> b OP a
                                        return cmp (Decoder.RightExpressionFor (right),
                                                    Decoder.LeftExpressionFor (right), right, data);
                        }
                        else if (Decoder.IsConstant (right) && Decoder.OperatorFor (left) == ExpressionOperator.Sub)
                                // (a-b) OP const
                        {
                                int num;
                                if (Decoder.TryValueOf (right, ExpressionType.Int32, out num) && num == 0)
                                        // (a-b) OP 0 ==> a OP b
                                        return cmp (Decoder.LeftExpressionFor (left),
                                                    Decoder.RightExpressionFor (left), left, data);
                        }

                        return cmp (left, right, original, data);
                }

                protected abstract Out Default (In data);

                Out DispatchVisitNot (Expr expr, In data)
                {
                        switch (Decoder.OperatorFor (expr)) {
                        case ExpressionOperator.Equal:
                        case ExpressionOperator.Equal_Obj:
                                return VisitNotEqual (Decoder.LeftExpressionFor (expr),
                                                      Decoder.RightExpressionFor (expr), expr, data);
                        case ExpressionOperator.LessThan: // a < b ==>  b <= a
                                return DispatchCompare (VisitLessEqualThan, Decoder.RightExpressionFor (expr),
                                                        Decoder.LeftExpressionFor (expr), expr, data);
                        case ExpressionOperator.LessEqualThan: // a <= b ==> b < a
                                return DispatchCompare (VisitLessThan, Decoder.LeftExpressionFor (expr),
                                                        Decoder.RightExpressionFor (expr), expr, data);
                        case ExpressionOperator.GreaterThan: // a > b ==> b < a
                                return DispatchCompare (VisitLessThan, Decoder.RightExpressionFor (expr),
                                                        Decoder.LeftExpressionFor (expr), expr, data);
                        case ExpressionOperator.GreaterEqualThan: // a >= b ==> b <= a
                                return DispatchCompare (VisitLessEqualThan, Decoder.RightExpressionFor (expr),
                                                        Decoder.LeftExpressionFor (expr), expr, data);
                        default:
                                return VisitNot (expr, data);
                        }
                }

                Out DispatchVisitEqual (ExpressionOperator eqKind, Expr left, Expr right, Expr original, In data)
                {
                        // { left :eq: right }
                        switch (Decoder.OperatorFor (left)) {
                        case Op.GreaterEqualThan:
                        case Op.GreaterThan:
                        case Op.LessThan:
                        case Op.LessEqualThan:
                        case Op.Equal:
                        case Op.Equal_Obj:
                                // { ( a ?= b ) :eq: right } 
                                bool shouldNegate;
                                if (TryPolarity (right, data, out shouldNegate))
                                        // { (a ?= b) :eq: true => (a ?= b) }; (a ?= b) :eq: false => !(a ?= b) }
                                        return shouldNegate ? DispatchVisitNot (left, data) : Visit (left, data);
                                break;
                        }

                        switch (Decoder.OperatorFor (right)) {
                        case Op.GreaterEqualThan:
                        case Op.GreaterThan:
                        case Op.LessThan:
                        case Op.LessEqualThan:
                        case Op.Equal:
                        case Op.Equal_Obj:
                                // { left :eq: (a ?= b) }
                                bool shouldNegate;
                                if (TryPolarity (left, data, out shouldNegate))
                                        // { true :eq: (a ?= b) => (a ?= b) }; false :eq: (a ?= b) => !(a ?= b) }
                                        return shouldNegate ? DispatchVisitNot (right, data) : Visit (right, data);
                                break;
                        }

                        return VisitEqual (left, right, original, data);
                }
        }
}