Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IntroduceQueryExpressions.cs « ICSharpCode.NRefactory.CSharp - github.com/xamarin/NRefactory.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d80ddb82cd0ca1bdc09d9f028741519ef4591fe8 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//
// IntroduceQueryExpressions.cs
//
// Modified by Luís Reis <luiscubal@gmail.com> (Copyright (C) 2013)
//
// Copyright header of the original version follows:
//
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
// 
// 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 System.Diagnostics;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.PatternMatching;

namespace ICSharpCode.NRefactory.CSharp
{
	static class NRefactoryExtensions
	{
		public static T Detach<T>(this T node) where T : AstNode
		{
			node.Remove();
			return node;
		}

		public static T CopyAnnotationsFrom<T>(this T node, AstNode other) where T : AstNode
		{
			foreach (object annotation in other.Annotations) {
				node.AddAnnotation(annotation);
			}
			return node;
		}

		public static Expression WithName(this Expression node, string patternGroupName)
		{
			return new NamedNode(patternGroupName, node);
		}
	}

	/// <summary>
	/// Decompiles query expressions.
	/// Based on C# 4.0 spec, §7.16.2 Query expression translation
	/// </summary>
	public class IntroduceQueryExpressions
	{
		static readonly InvocationExpression castPattern = new InvocationExpression {
			Target = new MemberReferenceExpression {
				Target = new AnyNode("inExpr"),
				MemberName = "Cast",
				TypeArguments = { new AnyNode("targetType") }
			}};

		public Expression ConvertFluentToQuery(Expression node)
		{
			node = node.Clone();

			var artificialParent = new ExpressionStatement();
			artificialParent.Expression = node;

			DecompileQueries(node);
			// After all queries were decompiled, detect degenerate queries (queries not property terminated with 'select' or 'group')
			// and fix them, either by adding a degenerate select, or by combining them with another query.
			foreach (QueryExpression query in artificialParent.Descendants.OfType<QueryExpression>()) {
				QueryFromClause fromClause = (QueryFromClause)query.Clauses.First();
				if (IsDegenerateQuery(query)) {
					string identifierName = fromClause.Identifier;

					// introduce select for degenerate query
					query.Clauses.Add(new QuerySelectClause { Expression = new IdentifierExpression(identifierName) });
				}

				if (fromClause.Type.IsNull) {
					// See if the data source of this query is a degenerate query,
					// and combine the queries if possible.
					QueryExpression innerQuery = fromClause.Expression as QueryExpression;
					while (IsDegenerateQuery(innerQuery)) {
						QueryFromClause innerFromClause = (QueryFromClause)innerQuery.Clauses.First();
						if (fromClause.Identifier != innerFromClause.Identifier && !innerFromClause.Identifier.StartsWith("<>"))
							break;
						// Replace the fromClause with all clauses from the inner query
						fromClause.Remove();
						foreach (var identifierChild in innerQuery.Descendants.OfType<Identifier>().Where(identifier => identifier.Name == innerFromClause.Identifier)) {
							//When the identifier is "<>X", then replace it with the outer one
							identifierChild.ReplaceWith(fromClause.IdentifierToken.Clone());
						}
						QueryClause insertionPos = null;
						foreach (var clause in innerQuery.Clauses) {
							query.Clauses.InsertAfter(insertionPos, insertionPos = clause.Detach());
						}
						fromClause = innerFromClause;
						innerQuery = fromClause.Expression as QueryExpression;
					}
				}
			}

			return artificialParent.Expression.Clone();
		}

		bool IsDegenerateQuery(QueryExpression query)
		{
			if (query == null)
				return false;
			var lastClause = query.Clauses.LastOrDefault();
			return !(lastClause is QuerySelectClause || lastClause is QueryGroupClause);
		}

		void DecompileQueries(AstNode node)
		{
			QueryExpression query = DecompileQuery(node as InvocationExpression);
			if (query != null)
				node.ReplaceWith(query);
		}

		Expression ExtractQuery(MemberReferenceExpression mre)
		{
			var inExpression = mre.Target.Clone();
			var inContent = DecompileQuery(inExpression as InvocationExpression) ?? inExpression;
			return inContent;
		}

		QueryExpression DecompileQuery(InvocationExpression invocation)
		{
			if (invocation == null)
				return null;
			MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;
			if (mre == null)
				return null;

			switch (mre.MemberName) {
				case "Select":
					{
						if (invocation.Arguments.Count != 1)
							return null;
						string parameterName;
						Expression body;
						if (MatchSimpleLambda(invocation.Arguments.Single(), out parameterName, out body)) {
							QueryExpression query = new QueryExpression();
							query.Clauses.Add(new QueryFromClause { Identifier = parameterName, Expression = ExtractQuery(mre) });
							query.Clauses.Add(new QuerySelectClause { Expression = body.Detach() });
							return query;
						}
						return null;
					}
				case "Cast":
					{
						if (invocation.Arguments.Count == 0 && mre.TypeArguments.Count == 1) {
							var typeArgument = mre.TypeArguments.First();

							QueryExpression query = new QueryExpression();
							string varName = GenerateVariableName();
							query.Clauses.Add(new QueryFromClause {
								Identifier = varName,
								Expression = ExtractQuery(mre),
								Type = typeArgument.Detach()
							});
							return query;

						}
						return null;
					}
				case "GroupBy":
					{
						if (invocation.Arguments.Count == 2) {
							string parameterName1, parameterName2;
							Expression keySelector, elementSelector;
							if (MatchSimpleLambda(invocation.Arguments.ElementAt(0), out parameterName1, out keySelector)
								&& MatchSimpleLambda(invocation.Arguments.ElementAt(1), out parameterName2, out elementSelector)
								&& parameterName1 == parameterName2) {
								QueryExpression query = new QueryExpression();
								query.Clauses.Add(new QueryFromClause { Identifier = parameterName1, Expression = ExtractQuery(mre) });
								query.Clauses.Add(new QueryGroupClause { Projection = elementSelector.Detach(), Key = keySelector.Detach() });
								return query;
							}
						} else if (invocation.Arguments.Count == 1) {
							string parameterName;
							Expression keySelector;
							if (MatchSimpleLambda(invocation.Arguments.Single(), out parameterName, out keySelector)) {
								QueryExpression query = new QueryExpression();
								query.Clauses.Add(new QueryFromClause { Identifier = parameterName, Expression = ExtractQuery(mre) });
								query.Clauses.Add(new QueryGroupClause {
									Projection = new IdentifierExpression(parameterName),
									Key = keySelector.Detach()
								});
								return query;
							}
						}
						return null;
					}
				case "SelectMany":
					{
						if (invocation.Arguments.Count != 2)
							return null;
						string parameterName;
						Expression collectionSelector;
						if (!MatchSimpleLambda(invocation.Arguments.ElementAt(0), out parameterName, out collectionSelector))
							return null;
						LambdaExpression lambda = invocation.Arguments.ElementAt(1) as LambdaExpression;
						if (lambda != null && lambda.Parameters.Count == 2 && lambda.Body is Expression) {
							ParameterDeclaration p1 = lambda.Parameters.ElementAt(0);
							ParameterDeclaration p2 = lambda.Parameters.ElementAt(1);
							QueryExpression query = new QueryExpression();
							query.Clauses.Add(new QueryFromClause { Identifier = p1.Name, Expression = ExtractQuery(mre) });
							query.Clauses.Add(new QueryFromClause { Identifier = p2.Name, Expression = collectionSelector.Detach() });
							query.Clauses.Add(new QuerySelectClause { Expression = ((Expression)lambda.Body).Detach() });
							return query;
						}
						return null;
					}
				case "Where":
					{
						if (invocation.Arguments.Count != 1)
							return null;
						string parameterName;
						Expression body;
						if (MatchSimpleLambda(invocation.Arguments.Single(), out parameterName, out body)) {
							QueryExpression query = new QueryExpression();
							query.Clauses.Add(new QueryFromClause { Identifier = parameterName, Expression = ExtractQuery(mre) });
							query.Clauses.Add(new QueryWhereClause { Condition = body.Detach() });
							return query;
						}
						return null;
					}
				case "OrderBy":
				case "OrderByDescending":
				case "ThenBy":
				case "ThenByDescending":
					{
						if (invocation.Arguments.Count != 1)
							return null;
						string parameterName;
						Expression orderExpression;
						if (MatchSimpleLambda(invocation.Arguments.Single(), out parameterName, out orderExpression)) {
							if (ValidateThenByChain(invocation, parameterName)) {
								QueryOrderClause orderClause = new QueryOrderClause();
								InvocationExpression tmp = invocation;
								while (mre.MemberName == "ThenBy" || mre.MemberName == "ThenByDescending") {
									// insert new ordering at beginning
									orderClause.Orderings.InsertAfter(
										null, new QueryOrdering {
										Expression = orderExpression.Detach(),
										Direction = (mre.MemberName == "ThenBy" ? QueryOrderingDirection.None : QueryOrderingDirection.Descending)
									});

									tmp = (InvocationExpression)mre.Target;
									mre = (MemberReferenceExpression)tmp.Target;
									MatchSimpleLambda(tmp.Arguments.Single(), out parameterName, out orderExpression);
								}
								// insert new ordering at beginning
								orderClause.Orderings.InsertAfter(
									null, new QueryOrdering {
									Expression = orderExpression.Detach(),
									Direction = (mre.MemberName == "OrderBy" ? QueryOrderingDirection.None : QueryOrderingDirection.Descending)
								});

								QueryExpression query = new QueryExpression();
								query.Clauses.Add(new QueryFromClause { Identifier = parameterName, Expression = ExtractQuery(mre) });
								query.Clauses.Add(orderClause);
								return query;
							}
						}
						return null;
					}
				case "Join":
				case "GroupJoin":
					{
						if (invocation.Arguments.Count != 4)
							return null;
						Expression source1 = mre.Target;
						Expression source2 = invocation.Arguments.ElementAt(0);
						string elementName1, elementName2;
						Expression key1, key2;
						if (!MatchSimpleLambda(invocation.Arguments.ElementAt(1), out elementName1, out key1))
							return null;
						if (!MatchSimpleLambda(invocation.Arguments.ElementAt(2), out elementName2, out key2))
							return null;
						LambdaExpression lambda = invocation.Arguments.ElementAt(3) as LambdaExpression;
						if (lambda != null && lambda.Parameters.Count == 2 && lambda.Body is Expression) {
							ParameterDeclaration p1 = lambda.Parameters.ElementAt(0);
							ParameterDeclaration p2 = lambda.Parameters.ElementAt(1);
							QueryExpression query = new QueryExpression();
							query.Clauses.Add(new QueryFromClause { Identifier = elementName1, Expression = source1.Detach() });
							QueryJoinClause joinClause = new QueryJoinClause();
							
							joinClause.JoinIdentifier = elementName2;    // join elementName2
							joinClause.InExpression = source2.Detach();  // in source2

							Match castMatch = castPattern.Match(source2);
							if (castMatch.Success) {
								Expression target = castMatch.Get<Expression>("inExpr").Single().Detach();
								joinClause.Type = castMatch.Get<AstType>("targetType").Single().Detach();
								joinClause.InExpression = target;
							}
							
							joinClause.OnExpression = key1.Detach();     // on key1
							joinClause.EqualsExpression = key2.Detach(); // equals key2
							if (mre.MemberName == "GroupJoin") {
								joinClause.IntoIdentifier = p2.Name; // into p2.Name
							}
							query.Clauses.Add(joinClause);
							Expression resultExpr = ((Expression)lambda.Body).Detach();
							if (p1.Name != elementName1) {
								foreach (var identifier in resultExpr.Descendants.OfType<Identifier>().Where(id => id.Name == p1.Name))
								{
									identifier.Name = elementName1;
								}
							}
							if (p2.Name != elementName2 && mre.MemberName != "GroupJoin") {
								foreach (var identifier in resultExpr.Descendants.OfType<Identifier>().Where(id => id.Name == p2.Name))
								{
									identifier.Name = elementName2;
								}
							}
							query.Clauses.Add(new QuerySelectClause { Expression = resultExpr });
							return query;
						}
						return null;
					}
				default:
					return null;
			}
		}

		int id = 1;
		string GenerateVariableName()
		{
			return "<>" + id++;
		}

		/// <summary>
		/// Ensure that all ThenBy's are correct, and that the list of ThenBy's is terminated by an 'OrderBy' invocation.
		/// </summary>
		bool ValidateThenByChain(InvocationExpression invocation, string expectedParameterName)
		{
			if (invocation == null || invocation.Arguments.Count != 1)
				return false;
			MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;
			if (mre == null)
				return false;
			string parameterName;
			Expression body;
			if (!MatchSimpleLambda(invocation.Arguments.Single(), out parameterName, out body))
				return false;
			if (parameterName != expectedParameterName)
				return false;

			if (mre.MemberName == "OrderBy" || mre.MemberName == "OrderByDescending")
				return true;
			else if (mre.MemberName == "ThenBy" || mre.MemberName == "ThenByDescending")
				return ValidateThenByChain(mre.Target as InvocationExpression, expectedParameterName);
			else
				return false;
		}

		/// <summary>Matches simple lambdas of the form "a => b"</summary>
		bool MatchSimpleLambda(Expression expr, out string parameterName, out Expression body)
		{
			LambdaExpression lambda = expr as LambdaExpression;
			if (lambda != null && lambda.Parameters.Count == 1 && lambda.Body is Expression) {
				ParameterDeclaration p = lambda.Parameters.Single();
				if (p.ParameterModifier == ParameterModifier.None) {
					parameterName = p.Name;
					body = (Expression)lambda.Body;
					return true;
				}
			}
			parameterName = null;
			body = null;
			return false;
		}
	}
}