2 // ExpressionVisitor.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2008 Novell, Inc. (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System
.Collections
.ObjectModel
;
32 namespace System
.Linq
.Expressions
{
34 abstract class ExpressionVisitor
{
36 protected virtual void Visit (Expression expression
)
38 if (expression
== null)
41 switch (expression
.NodeType
) {
42 case ExpressionType
.Negate
:
43 case ExpressionType
.NegateChecked
:
44 case ExpressionType
.Not
:
45 case ExpressionType
.Convert
:
46 case ExpressionType
.ConvertChecked
:
47 case ExpressionType
.ArrayLength
:
48 case ExpressionType
.Quote
:
49 case ExpressionType
.TypeAs
:
50 case ExpressionType
.UnaryPlus
:
51 VisitUnary ((UnaryExpression
) expression
);
53 case ExpressionType
.Add
:
54 case ExpressionType
.AddChecked
:
55 case ExpressionType
.Subtract
:
56 case ExpressionType
.SubtractChecked
:
57 case ExpressionType
.Multiply
:
58 case ExpressionType
.MultiplyChecked
:
59 case ExpressionType
.Divide
:
60 case ExpressionType
.Modulo
:
61 case ExpressionType
.Power
:
62 case ExpressionType
.And
:
63 case ExpressionType
.AndAlso
:
64 case ExpressionType
.Or
:
65 case ExpressionType
.OrElse
:
66 case ExpressionType
.LessThan
:
67 case ExpressionType
.LessThanOrEqual
:
68 case ExpressionType
.GreaterThan
:
69 case ExpressionType
.GreaterThanOrEqual
:
70 case ExpressionType
.Equal
:
71 case ExpressionType
.NotEqual
:
72 case ExpressionType
.Coalesce
:
73 case ExpressionType
.ArrayIndex
:
74 case ExpressionType
.RightShift
:
75 case ExpressionType
.LeftShift
:
76 case ExpressionType
.ExclusiveOr
:
77 VisitBinary ((BinaryExpression
) expression
);
79 case ExpressionType
.TypeIs
:
80 VisitTypeIs ((TypeBinaryExpression
) expression
);
82 case ExpressionType
.Conditional
:
83 VisitConditional ((ConditionalExpression
) expression
);
85 case ExpressionType
.Constant
:
86 VisitConstant ((ConstantExpression
) expression
);
88 case ExpressionType
.Parameter
:
89 VisitParameter ((ParameterExpression
) expression
);
91 case ExpressionType
.MemberAccess
:
92 VisitMemberAccess ((MemberExpression
) expression
);
94 case ExpressionType
.Call
:
95 VisitMethodCall ((MethodCallExpression
) expression
);
97 case ExpressionType
.Lambda
:
98 VisitLambda ((LambdaExpression
) expression
);
100 case ExpressionType
.New
:
101 VisitNew ((NewExpression
) expression
);
103 case ExpressionType
.NewArrayInit
:
104 case ExpressionType
.NewArrayBounds
:
105 VisitNewArray ((NewArrayExpression
) expression
);
107 case ExpressionType
.Invoke
:
108 VisitInvocation ((InvocationExpression
) expression
);
110 case ExpressionType
.MemberInit
:
111 VisitMemberInit ((MemberInitExpression
) expression
);
113 case ExpressionType
.ListInit
:
114 VisitListInit ((ListInitExpression
) expression
);
117 throw new ArgumentException (string.Format ("Unhandled expression type: '{0}'", expression
.NodeType
));
121 protected virtual void VisitBinding (MemberBinding binding
)
123 switch (binding
.BindingType
) {
124 case MemberBindingType
.Assignment
:
125 VisitMemberAssignment ((MemberAssignment
) binding
);
127 case MemberBindingType
.MemberBinding
:
128 VisitMemberMemberBinding ((MemberMemberBinding
) binding
);
130 case MemberBindingType
.ListBinding
:
131 VisitMemberListBinding ((MemberListBinding
) binding
);
134 throw new ArgumentException (string.Format ("Unhandled binding type '{0}'", binding
.BindingType
));
138 protected virtual void VisitElementInitializer (ElementInit initializer
)
140 VisitExpressionList (initializer
.Arguments
);
143 protected virtual void VisitUnary (UnaryExpression unary
)
145 Visit (unary
.Operand
);
148 protected virtual void VisitBinary (BinaryExpression binary
)
151 Visit (binary
.Right
);
152 Visit (binary
.Conversion
);
155 protected virtual void VisitTypeIs (TypeBinaryExpression type
)
157 Visit (type
.Expression
);
160 protected virtual void VisitConstant (ConstantExpression constant
)
164 protected virtual void VisitConditional (ConditionalExpression conditional
)
166 Visit (conditional
.Test
);
167 Visit (conditional
.IfTrue
);
168 Visit (conditional
.IfFalse
);
171 protected virtual void VisitParameter (ParameterExpression parameter
)
175 protected virtual void VisitMemberAccess (MemberExpression member
)
177 Visit (member
.Expression
);
180 protected virtual void VisitMethodCall (MethodCallExpression methodCall
)
182 Visit (methodCall
.Object
);
183 VisitExpressionList (methodCall
.Arguments
);
186 protected virtual void VisitList
<T
> (ReadOnlyCollection
<T
> list
, Action
<T
> visitor
)
188 foreach (T element
in list
) {
193 protected virtual void VisitExpressionList (ReadOnlyCollection
<Expression
> list
)
195 VisitList (list
, Visit
);
198 protected virtual void VisitMemberAssignment (MemberAssignment assignment
)
200 Visit (assignment
.Expression
);
203 protected virtual void VisitMemberMemberBinding (MemberMemberBinding binding
)
205 VisitBindingList (binding
.Bindings
);
208 protected virtual void VisitMemberListBinding (MemberListBinding binding
)
210 VisitElementInitializerList (binding
.Initializers
);
213 protected virtual void VisitBindingList (ReadOnlyCollection
<MemberBinding
> list
)
215 VisitList (list
, VisitBinding
);
218 protected virtual void VisitElementInitializerList (ReadOnlyCollection
<ElementInit
> list
)
220 VisitList (list
, VisitElementInitializer
);
223 protected virtual void VisitLambda (LambdaExpression lambda
)
228 protected virtual void VisitNew (NewExpression nex
)
230 VisitExpressionList (nex
.Arguments
);
233 protected virtual void VisitMemberInit (MemberInitExpression init
)
235 VisitNew (init
.NewExpression
);
236 VisitBindingList (init
.Bindings
);
239 protected virtual void VisitListInit (ListInitExpression init
)
241 VisitNew (init
.NewExpression
);
242 VisitElementInitializerList (init
.Initializers
);
245 protected virtual void VisitNewArray (NewArrayExpression newArray
)
247 VisitExpressionList (newArray
.Expressions
);
250 protected virtual void VisitInvocation (InvocationExpression invocation
)
252 VisitExpressionList (invocation
.Arguments
);
253 Visit (invocation
.Expression
);