-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpr.tt
More file actions
317 lines (262 loc) · 9.89 KB
/
Expr.tt
File metadata and controls
317 lines (262 loc) · 9.89 KB
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
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".generated.cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
//---------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by T4 template.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
#nullable enable
namespace Linq.Expressions.Deconstruct
{
public abstract partial class Expr
{
<#
foreach (var item in new []
{
new { Expression = "MethodCallExpression", Expr = "Call" },
new { Expression = "ConditionalExpression", Expr = "Conditional", },
new { Expression = "InvocationExpression", Expr = "Invoke", },
new { Expression = "LambdaExpression", Expr = "Lambda", },
new { Expression = "ListInitExpression", Expr = "ListInit", },
new { Expression = "MemberExpression", Expr = "Member", },
new { Expression = "MemberInitExpression", Expr = "MemberInit", },
new { Expression = "NewExpression", Expr = "New", },
new { Expression = "NewArrayExpression", Expr = "NewArrayBounds", },
new { Expression = "NewArrayExpression", Expr = "NewArrayInit", },
new { Expression = "TypeBinaryExpression", Expr = "TypeEqual", },
new { Expression = "TypeBinaryExpression", Expr = "TypeIs", },
new { Expression = "BlockExpression", Expr = "Block", },
new { Expression = "DynamicExpression", Expr = "Dynamic", },
new { Expression = "GotoExpression", Expr = "Goto", },
new { Expression = "IndexExpression", Expr = "Index", },
new { Expression = "LabelExpression", Expr = "Label", },
new { Expression = "RuntimeVariablesExpression", Expr = "RuntimeVariables", },
new { Expression = "LoopExpression", Expr = "Loop", },
new { Expression = "SwitchExpression", Expr = "Switch", },
new { Expression = "TryExpression", Expr = "Try", },
new { Expression = "Expression", Expr = "Extension", },
new { Expression = "DebugInfoExpression", Expr = "DebugInfo", },
new { Expression = "ParameterExpression", Expr = "Parameter", },
new { Expression = "ConstantExpression", Expr = "Constant", },
new { Expression = "DefaultExpression", Expr = "Default", },
})
{
#>
#region <#= item.Expr #>
public partial class <#= item.Expr #> : Expr
{
public <#= item.Expression #> Expr { get; }
public <#= item.Expr #>(<#= item.Expression #> expr)
{
Expr = expr;
}
protected override Expression GetExpression() => Expr;
<#
if (item.Expression != "Expression")
{
#>
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator Expression?(<#= item.Expr #>? expr) => expr?.Expr;
<#
}
#>
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator <#= item.Expression #>?(<#= item.Expr #>? expr) => expr?.Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator <#= item.Expr #>?(<#= item.Expression #>? expr) => expr == null ? null : new <#= item.Expr #>(expr);
}
#endregion
<#
}
foreach (var item in new []
{
"Add", "AddChecked", "And", "AndAlso", "ArrayIndex",
"Assign", "Coalesce", "Divide", "Equal", "ExclusiveOr",
"GreaterThan", "GreaterThanOrEqual", "LeftShift", "LessThan", "LessThanOrEqual",
"Modulo", "Multiply", "MultiplyChecked", "NotEqual", "Or",
"OrElse", "Power", "RightShift", "Subtract", "SubtractChecked",
"AddAssign", "AndAssign", "DivideAssign", "ExclusiveOrAssign", "LeftShiftAssign",
"ModuloAssign", "MultiplyAssign", "OrAssign", "PowerAssign", "RightShiftAssign",
"SubtractAssign", "AddAssignChecked", "MultiplyAssignChecked", "SubtractAssignChecked",
})
{
#>
#region <#= item #>
public class <#= item #> : Expr
{
Expr? _left; Expr Left => _left ??= Expr.Left. ToExpr();
Expr? _right; Expr Right => _right ??= Expr.Right.ToExpr();
public BinaryExpression Expr { get; }
public <#= item #>(BinaryExpression expr)
{
Expr = expr;
}
protected override Expression GetExpression() => Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator Expression? (<#= item #>? expr) => expr?.Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator BinaryExpression?(<#= item #>? expr) => expr?.Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator <#= item #>?(BinaryExpression? expr) => expr == null ? null : new <#= item #>(expr);
public void Deconstruct(out Type type, out Expr left, out Expr right)
{
type = Expr.Type;
left = Left;
right = Right;
}
public void Deconstruct(out Expr left, out Expr right)
{
left = Left;
right = Right;
}
}
#endregion
<#
}
foreach (var item in new []
{
"ArrayLength", "Convert", "ConvertChecked", "Negate", "NegateChecked",
"Not", "Quote", "TypeAs", "UnaryPlus", "Decrement",
"Increment", "IsFalse", "IsTrue", "Throw", "Unbox",
"PreIncrementAssign", "PreDecrementAssign", "PostIncrementAssign", "PostDecrementAssign", "OnesComplement",
})
{
#>
#region <#= item #>
public class <#= item #> : Expr
{
Expr? _operand;
Expr? Operand => _operand ??= Expr.Operand.ToExpr();
public UnaryExpression Expr { get; }
public <#= item #>(UnaryExpression expr)
{
Expr = expr;
}
protected override Expression GetExpression() => Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator Expression? (<#= item #>? expr) => expr?.Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator UnaryExpression?(<#= item #>? expr) => expr?.Expr;
[return: NotNullIfNotNull(nameof(expr))]
public static implicit operator <#= item #>?(UnaryExpression? expr) => expr == null ? null : new <#= item #>(expr);
public void Deconstruct(out Type type, out Expr? operand)
{
type = Expr.Type;
operand = Operand;
}
public void Deconstruct(out Expr? operand)
{
operand = Operand;
}
}
#endregion
<#
}
#>
}
public static partial class Extensions
{
<#
foreach (var item in new[]
{
new { TItem = "ElementInit", PItem = "ElementInit", BConvert = "", AConvert = "" },
new { TItem = "CatchBlock", PItem = "CatchBlock", BConvert = "", AConvert = "" },
new { TItem = "SwitchCase", PItem = "SwitchCase", BConvert = "", AConvert = "" },
new { TItem = "MemberBinding", PItem = "MemberBinding", BConvert = "", AConvert = "" },
new { TItem = "MemberInfo", PItem = "MemberInfo", BConvert = "", AConvert = "" },
new { TItem = "Expression", PItem = "Expr", BConvert = "", AConvert = ".ToExpr()" },
new { TItem = "ParameterExpression", PItem = "Expr.Parameter", BConvert = "new Expr.Parameter(", AConvert = ")" },
new { TItem = "Expr", PItem = "Expr", BConvert = "", AConvert = "" },
new { TItem = "Expr.Parameter", PItem = "Expr.Parameter", BConvert = "", AConvert = "" },
})
{
var max = 10;
#>
#region IEnumerable<<#= item.TItem #>>
public static void Deconstruct(this IEnumerable<<#= item.TItem #>> list, out int count)
{
switch (list)
{
case <#= item.TItem #>[] arr : count = arr.Length; break;
case ICollection<<#= item.TItem #>> col : count = col.Count; break;
case null : count = -1; break;
default :
using (var enumerator = list.GetEnumerator())
count = enumerator.MoveNext() ? 1 : 0;
break;
}
}
<#
for (var i = 0; i < max; i++)
{
#>
public static void Deconstruct(this IEnumerable<<#= item.TItem #>> list,
out int count<#
for (var j = 0; j <= i; j++)
{
#>, out <#= item.PItem #>? item<#= j #><#
}
#>)
{
if (list == null) { count = -1; <#
for (var j = 0; j <= i; j++)
{
#>item<#= j #> = <#
}
#>default; return; }
count = 0;
using var enumerator = list.GetEnumerator();
<#
for (var j = 0; j <= i; j++)
{
#>
if (enumerator.MoveNext())
{
count++;
item<#= j #> = <#= item.BConvert #>enumerator.Current<#= item.AConvert #>;
<#
PushIndent("\t");
}
#>
switch (list)
{
case <#= item.TItem #>[] arr : count = arr.Length; break;
case ICollection<<#= item.TItem #>> col : count = col.Count; break;
default : if (enumerator.MoveNext()) count++; break;
}<#
for (var j = 0; j <= i; j++)
{
PopIndent();
#>
}
else
{
<#
for (var k = 0; k <= j; k++)
{
#>item<#= i - k #> = <#
}
#>default;
}<#
}
#>
}
<#
}
#>
#endregion
<#
}
#>
}
}