A couple of fixes for TimeZoneInfo.ConvertTime.
[mono.git] / mcs / class / System.Core / System.Linq / QueryableTransformer.cs
1 //
2 // QueryableTransformer.cs
3 //
4 // Authors:
5 //      Roei Erez (roeie@mainsoft.com)
6 //      Jb Evain (jbevain@novell.com)
7 //
8 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
34 using System.Linq;
35 using System.Linq.Expressions;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
38
39 namespace System.Linq {
40
41         class QueryableTransformer : ExpressionTransformer {
42
43                 protected override Expression VisitMethodCall (MethodCallExpression methodCall)
44                 {
45                         if (IsQueryableExtension (methodCall.Method))
46                                 return ReplaceQueryableMethod (methodCall);
47
48                         return base.VisitMethodCall (methodCall);
49                 }
50
51                 protected override Expression VisitLambda (LambdaExpression lambda)
52                 {
53                         return lambda;
54                 }
55
56                 protected override Expression VisitConstant (ConstantExpression constant)
57                 {
58                         var qe = constant.Value as IQueryableEnumerable;
59                         if (qe == null)
60                                 return constant;
61
62                         var enumerable = qe.GetEnumerable ();
63                         if (enumerable != null)
64                                 return Expression.Constant (enumerable);
65
66                         return Visit (qe.Expression);
67                 }
68
69                 static bool IsQueryableExtension (MethodInfo method)
70                 {
71                         return HasExtensionAttribute (method) &&
72                                 method.GetParameters () [0].ParameterType.IsAssignableTo (typeof (IQueryable));
73                 }
74
75                 static bool HasExtensionAttribute (MethodInfo method)
76                 {
77                         return method.GetCustomAttributes (typeof (ExtensionAttribute), false).Length > 0;
78                 }
79
80                 MethodCallExpression ReplaceQueryableMethod (MethodCallExpression old)
81                 {
82                         Expression target = null;
83                         if (old.Object != null)
84                                 target = Visit (old.Object);
85
86                         var method = ReplaceQueryableMethod (old.Method);
87                         var parameters = method.GetParameters ();
88                         var arguments = new Expression [old.Arguments.Count];
89
90                         for (int i = 0; i < arguments.Length; i++) {
91                                 arguments [i] = UnquoteIfNeeded (
92                                         Visit (old.Arguments [i]),
93                                         parameters [i].ParameterType);
94                         }
95
96                         return Expression.Call (target, method, arguments);
97                 }
98
99                 static Expression UnquoteIfNeeded (Expression expression, Type delegateType)
100                 {
101                         if (expression.NodeType != ExpressionType.Quote)
102                                 return expression;
103
104                         var lambda = (LambdaExpression) ((UnaryExpression) expression).Operand;
105                         if (lambda.Type == delegateType)
106                                 return lambda;
107
108                         return expression;
109                 }
110
111                 static Type GetTargetDeclaringType (MethodInfo method)
112                 {
113                         return method.DeclaringType == typeof (Queryable) ? typeof (Enumerable) : method.DeclaringType;
114                 }
115
116                 static MethodInfo ReplaceQueryableMethod (MethodInfo method)
117                 {
118                         var target_type = GetTargetDeclaringType (method);
119                         var result = GetMatchingMethod (method, target_type);
120
121                         if (result != null)
122                                 return result;
123
124                         throw new InvalidOperationException (
125                                 string.Format (
126                                         "There is no method {0} on type {1} that matches the specified arguments",
127                                         method.Name,
128                                         target_type.FullName));
129                 }
130
131                 static MethodInfo GetMatchingMethod (MethodInfo method, Type declaring)
132                 {
133                         foreach (var candidate in declaring.GetMethods ()) {
134                                 if (!MethodMatch (candidate, method))
135                                         continue;
136
137                                 if (method.IsGenericMethod)
138                                         return candidate.MakeGenericMethodFrom (method);
139
140                                 return candidate;
141                         }
142
143                         return null;
144                 }
145
146                 static bool MethodMatch (MethodInfo candidate, MethodInfo method)
147                 {
148                         if (candidate.Name != method.Name)
149                                 return false;
150
151                         if (!HasExtensionAttribute (candidate))
152                                 return false;
153
154                         var parameters = method.GetParameterTypes ();
155
156                         if (parameters.Length != candidate.GetParameters ().Length)
157                                 return false;
158
159                         if (method.IsGenericMethod) {
160                                 if (!candidate.IsGenericMethod)
161                                         return false;
162
163                                 if (candidate.GetGenericArguments ().Length != method.GetGenericArguments ().Length)
164                                         return false;
165
166                                 candidate = candidate.MakeGenericMethodFrom (method);
167                         }
168
169                         if (!TypeMatch (candidate.ReturnType, method.ReturnType))
170                                 return false;
171
172                         var candidate_parameters = candidate.GetParameterTypes ();
173
174                         if (candidate_parameters [0] != GetComparableType (parameters [0]))
175                                 return false;
176
177                         for (int i = 1; i < candidate_parameters.Length; ++i)
178                                 if (!TypeMatch (candidate_parameters [i], parameters [i]))
179                                         return false;
180
181                         return true;
182                 }
183
184                 static bool TypeMatch (Type candidate, Type type)
185                 {
186                         if (candidate == type)
187                                 return true;
188
189                         return candidate == GetComparableType (type);
190                 }
191
192                 static Type GetComparableType (Type type)
193                 {
194                         if (type.IsGenericInstanceOf (typeof (IQueryable<>)))
195                                 type = typeof (IEnumerable<>).MakeGenericTypeFrom (type);
196                         else if (type.IsGenericInstanceOf (typeof (IOrderedQueryable<>)))
197                                 type = typeof (IOrderedEnumerable<>).MakeGenericTypeFrom (type);
198                         else if (type.IsGenericInstanceOf (typeof (Expression<>)))
199                                 type = type.GetFirstGenericArgument ();
200                         else if (type == typeof (IQueryable))
201                                 type = typeof (IEnumerable);
202
203                         return type;
204                 }
205         }
206 }