Merge pull request #557 from jack-pappas/gacutil-patch
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Rewrite.AstVisitors / ExprVisitor.cs
1 //
2 // ExprVisitor.cs
3 //
4 // Authors:
5 //      Chris Bacon (chrisbacon76@gmail.com)
6 //
7 // Copyright (C) 2010 Chris Bacon
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 System.Linq;
32 using System.Text;
33 using Mono.CodeContracts.Rewrite.Ast;
34
35 namespace Mono.CodeContracts.Rewrite.AstVisitors {
36         abstract class ExprVisitor {
37
38                 public virtual Expr Visit (Expr e)
39                 {
40                         switch (e.ExprType) {
41                         case ExprType.Block:
42                                 return this.VisitBlock ((ExprBlock) e);
43                         case ExprType.Nop:
44                                 return this.VisitNop ((ExprNop) e);
45                         case ExprType.Call:
46                                 return this.VisitCall ((ExprCall) e);
47                         case ExprType.CompareEqual:
48                                 return this.VisitCompareEqual ((ExprCompareEqual) e);
49                         case ExprType.CompareLessThan:
50                                 return this.VisitCompareLessThan ((ExprCompareLessThan) e);
51                         case ExprType.CompareGreaterThan:
52                                 return this.VisitCompareGreaterThan ((ExprCompareGreaterThan) e);
53                         case ExprType.Add:
54                                 return this.VisitAdd ((ExprAdd) e);
55                         case ExprType.Sub:
56                                 return this.VisitSub ((ExprSub) e);
57                         case ExprType.LoadArg:
58                                 return this.VisitLoadArg ((ExprLoadArg) e);
59                         case ExprType.LoadConstant:
60                                 return this.VisitLoadConstant ((ExprLoadConstant) e);
61                         case ExprType.Return:
62                                 return this.VisitReturn ((ExprReturn) e);
63                         case ExprType.Box:
64                                 return this.VisitBox ((ExprBox) e);
65                         case ExprType.Conv:
66                                 return this.VisitConv ((ExprConv) e);
67                         default:
68                                 throw new NotSupportedException ("Cannot handle: " + e.ExprType);
69                         }
70                 }
71
72                 protected virtual Expr VisitCollection (IEnumerable<Expr> collection, Expr e, Func<IEnumerable<Expr>, Expr> fnCreateNew)
73                 {
74                         int index = 0;
75                         List<Expr> exprs = null;
76                         foreach (var expr in collection) {
77                                 Expr exprVisited = this.Visit (expr);
78                                 if (exprs != null || exprVisited != expr) {
79                                         if (exprs == null) {
80                                                 exprs = new List<Expr> (collection.Take (index));
81                                         }
82                                         exprs.Add (exprVisited);
83                                 }
84                                 index++;
85                         }
86                         if (exprs == null) {
87                                 return e;
88                         } else {
89                                 return fnCreateNew(exprs);
90                         }
91                 }
92
93                 protected Expr VisitCollection (Expr e, Func<IEnumerable<Expr>, Expr> fnCreateNew, params Expr [] collection)
94                 {
95                         return this.VisitCollection (collection, e, fnCreateNew);
96                 }
97
98                 protected virtual Expr VisitBlock (ExprBlock e)
99                 {
100                         return this.VisitCollection (e.Exprs, e, exprs => new ExprBlock (e.MethodInfo, exprs));
101                 }
102
103                 protected virtual Expr VisitNop (ExprNop e)
104                 {
105                         return e;
106                 }
107
108                 protected virtual Expr VisitLoadArg (ExprLoadArg e)
109                 {
110                         return e;
111                 }
112
113                 protected virtual Expr VisitLoadConstant (ExprLoadConstant e)
114                 {
115                         return e;
116                 }
117
118                 protected virtual Expr VisitCompareLessThan (ExprCompareLessThan e)
119                 {
120                         return this.VisitCollection (e, exprs => new ExprCompareLessThan (e.MethodInfo, exprs.First (), exprs.ElementAt (1), e.Signage), e.Left, e.Right);
121                 }
122
123                 protected virtual Expr VisitCompareGreaterThan (ExprCompareGreaterThan e)
124                 {
125                         return this.VisitCollection (e, exprs => new ExprCompareGreaterThan (e.MethodInfo, exprs.First (), exprs.ElementAt (1), e.Signage), e.Left, e.Right);
126                 }
127
128                 protected virtual Expr VisitCompareEqual (ExprCompareEqual e)
129                 {
130                         return this.VisitCollection (e, exprs => new ExprCompareEqual (e.MethodInfo, exprs.First (), exprs.ElementAt (1)), e.Left, e.Right);
131                 }
132
133                 protected virtual Expr VisitAdd (ExprAdd e)
134                 {
135                         return this.VisitCollection (e, exprs => new ExprAdd (e.MethodInfo, exprs.First (), exprs.ElementAt (1), e.Signage, e.Overflow), e.Left, e.Right);
136                 }
137
138                 protected virtual Expr VisitSub (ExprSub e)
139                 {
140                         return this.VisitCollection (e, exprs => new ExprSub (e.MethodInfo, exprs.First (), exprs.ElementAt (1), e.Signage, e.Overflow), e.Left, e.Right);
141                 }
142
143                 protected virtual Expr VisitCall (ExprCall e)
144                 {
145                         return this.VisitCollection (e.Parameters, e, exprs => new ExprCall (e.MethodInfo, e.Method, exprs));
146                 }
147
148                 protected virtual Expr VisitReturn (ExprReturn e)
149                 {
150                         return e;
151                 }
152
153                 protected virtual Expr VisitBox (ExprBox e)
154                 {
155                         return this.VisitCollection (e, exprs => new ExprBox (e.MethodInfo, exprs.First ()), e.ExprToBox);
156                 }
157
158                 protected virtual Expr VisitConv (ExprConv e)
159                 {
160                         return this.VisitCollection (e, exprs => new ExprConv (e.MethodInfo, exprs.First (), e.ConvToType), e.ExprToConvert);
161                 }
162         }
163 }