-
Notifications
You must be signed in to change notification settings - Fork 17
/
LinqExpressionCompiler.cs
377 lines (318 loc) · 17.1 KB
/
LinqExpressionCompiler.cs
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
// Copyright © Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Serilog.Events;
using Serilog.Expressions.Ast;
using Serilog.Expressions.Compilation.Transformations;
using Serilog.Templates.Compilation;
using Serilog.Templates.Themes;
using ConstantExpression = Serilog.Expressions.Ast.ConstantExpression;
using Expression = Serilog.Expressions.Ast.Expression;
using ParameterExpression = System.Linq.Expressions.ParameterExpression;
using LX = System.Linq.Expressions.Expression;
using ExpressionBody = System.Linq.Expressions.Expression;
// ReSharper disable UseIndexFromEndExpression
namespace Serilog.Expressions.Compilation.Linq;
class LinqExpressionCompiler : SerilogExpressionTransformer<ExpressionBody>
{
readonly NameResolver _nameResolver;
readonly IFormatProvider? _formatProvider;
static readonly MethodInfo CollectSequenceElementsMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.CollectSequenceElements), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ExtendSequenceValueWithSpreadMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ExtendSequenceValueWithSpread), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ExtendSequenceValueWithItemMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ExtendSequenceValueWithItem), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ConstructSequenceValueMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ConstructSequenceValue), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo CollectStructurePropertiesMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.CollectStructureProperties), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ConstructStructureValueMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ConstructStructureValue), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo CompleteStructureValueMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.CompleteStructureValue), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ExtendStructureValueWithSpreadMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ExtendStructureValueWithSpread), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo ExtendStructureValueWithPropertyMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.ExtendStructureValueWithProperty), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo CoerceToScalarBooleanMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.CoerceToScalarBoolean), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo IndexOfMatchMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.IndexOfMatch), BindingFlags.Static | BindingFlags.Public)!;
static readonly MethodInfo TryGetStructurePropertyValueMethod = typeof(Intrinsics)
.GetMethod(nameof(Intrinsics.TryGetStructurePropertyValue), BindingFlags.Static | BindingFlags.Public)!;
static readonly PropertyInfo EvaluationContextLogEventProperty = typeof(EvaluationContext)
.GetProperty(nameof(EvaluationContext.LogEvent), BindingFlags.Instance | BindingFlags.Public)!;
ParameterExpression Context { get; } = LX.Variable(typeof(EvaluationContext), "ctx");
LinqExpressionCompiler(IFormatProvider? formatProvider, NameResolver nameResolver)
{
_nameResolver = nameResolver;
_formatProvider = formatProvider;
}
public static Evaluatable Compile(Expression expression, IFormatProvider? formatProvider,
NameResolver nameResolver)
{
if (expression == null) throw new ArgumentNullException(nameof(expression));
var compiler = new LinqExpressionCompiler(formatProvider, nameResolver);
var body = compiler.Transform(expression);
return LX.Lambda<Evaluatable>(body, compiler.Context).Compile();
}
ExpressionBody Splice(Expression<Evaluatable> lambda)
{
return ParameterReplacementVisitor.ReplaceParameters(lambda, Context);
}
protected override ExpressionBody Transform(CallExpression call)
{
if (!_nameResolver.TryResolveFunctionName(call.OperatorName, out var m))
throw new ArgumentException($"The function name `{call.OperatorName}` was not recognized.");
var methodParameters = m.GetParameters()
.Select(info => (pi: info, optional: info.GetCustomAttribute<OptionalAttribute>() != null))
.ToList();
var allowedParameters = methodParameters.Where(info => info.pi.ParameterType == typeof(LogEventPropertyValue)).ToList();
var requiredParameterCount = allowedParameters.Count(info => !info.optional);
if (call.Operands.Length < requiredParameterCount || call.Operands.Length > allowedParameters.Count)
{
var requirements = DescribeRequirements(allowedParameters.Select(info => (info.pi.Name!, info.optional)).ToList());
throw new ArgumentException($"The function `{call.OperatorName}` {requirements}.");
}
var operands = new Queue<LX>(call.Operands.Select(Transform));
// `and` and `or` short-circuit to save execution time; unlike the earlier Serilog.Filters.Expressions, nothing else does.
if (Operators.SameOperator(call.OperatorName, Operators.RuntimeOpAnd))
return CompileLogical(LX.AndAlso, operands.Dequeue(), operands.Dequeue());
if (Operators.SameOperator(call.OperatorName, Operators.RuntimeOpOr))
return CompileLogical(LX.OrElse, operands.Dequeue(), operands.Dequeue());
var boundParameters = new List<LX>(methodParameters.Count);
foreach (var (pi, optional) in methodParameters)
{
if (pi.ParameterType == typeof(LogEventPropertyValue))
{
boundParameters.Add(operands.Count > 0
? operands.Dequeue()
: LX.Constant(null, typeof(LogEventPropertyValue)));
}
else if (pi.ParameterType == typeof(StringComparison))
boundParameters.Add(LX.Constant(call.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
else if (pi.ParameterType == typeof(IFormatProvider))
boundParameters.Add(LX.Constant(_formatProvider, typeof(IFormatProvider)));
else if (pi.ParameterType == typeof(LogEvent))
boundParameters.Add(LX.Property(Context, EvaluationContextLogEventProperty));
else if (_nameResolver.TryBindFunctionParameter(pi, out var binding))
boundParameters.Add(LX.Constant(binding, pi.ParameterType));
else if (optional)
boundParameters.Add(LX.Constant(
pi.GetCustomAttribute<DefaultParameterValueAttribute>()?.Value, pi.ParameterType));
else
throw new ArgumentException($"The method `{m.Name}` implementing function `{call.OperatorName}` has argument `{pi.Name}` which could not be bound.");
}
return LX.Call(m, boundParameters);
}
static string DescribeRequirements(IReadOnlyList<(string name, bool optional)> parameters)
{
static string DescribeArgument((string name, bool optional) p) =>
$"`{p.name}`" + (p.optional ? " (optional)" : "");
if (parameters.Count == 0)
return "accepts no arguments";
if (parameters.Count == 1)
return $"accepts one argument, {DescribeArgument(parameters[0])}";
if (parameters.Count == 2)
return $"accepts two arguments, {DescribeArgument(parameters[0])} and {DescribeArgument(parameters[1])}";
var result = new StringBuilder("accepts arguments");
for (var i = 0; i < parameters.Count - 1; ++i)
result.Append($" {DescribeArgument(parameters[i])},");
result.Append($" and {DescribeArgument(parameters[parameters.Count - 1])}");
return result.ToString();
}
static ExpressionBody CompileLogical(Func<ExpressionBody, ExpressionBody, ExpressionBody> apply, ExpressionBody lhs, ExpressionBody rhs)
{
return LX.Convert(
LX.New(
typeof(ScalarValue).GetConstructor(new[]{typeof(object)})!,
LX.Convert(apply(
LX.Call(CoerceToScalarBooleanMethod, lhs),
LX.Call(CoerceToScalarBooleanMethod, rhs)), typeof(object))),
typeof(LogEventPropertyValue));
}
protected override ExpressionBody Transform(AccessorExpression spx)
{
var receiver = Transform(spx.Receiver);
return LX.Call(TryGetStructurePropertyValueMethod, LX.Constant(StringComparison.Ordinal), receiver, LX.Constant(spx.MemberName, typeof(string)));
}
protected override ExpressionBody Transform(ConstantExpression cx)
{
return LX.Constant(cx.Constant);
}
protected override ExpressionBody Transform(AmbientNameExpression px)
{
if (px.IsBuiltIn)
{
var formatter = new CompiledMessageToken(_formatProvider, null, TemplateTheme.None);
var formatProvider = _formatProvider;
return px.PropertyName switch
{
BuiltInProperty.Level => Splice(context => new ScalarValue(context.LogEvent.Level)),
BuiltInProperty.Message => Splice(context => new ScalarValue(Intrinsics.RenderMessage(formatter, context))),
BuiltInProperty.Exception => Splice(context =>
context.LogEvent.Exception == null ? null : new ScalarValue(context.LogEvent.Exception)),
BuiltInProperty.TraceId => Splice(context =>
context.LogEvent.TraceId == null ? null : new ScalarValue(context.LogEvent.TraceId.Value)),
BuiltInProperty.SpanId => Splice(context =>
context.LogEvent.SpanId == null ? null : new ScalarValue(context.LogEvent.SpanId.Value)),
BuiltInProperty.Timestamp => Splice(context => new ScalarValue(context.LogEvent.Timestamp)),
BuiltInProperty.MessageTemplate => Splice(context => new ScalarValue(context.LogEvent.MessageTemplate.Text)),
BuiltInProperty.Properties => Splice(context =>
new StructureValue(context.LogEvent.Properties.Select(kvp => new LogEventProperty(kvp.Key, kvp.Value)),
null)),
BuiltInProperty.Renderings => Splice(context => Intrinsics.GetRenderings(context.LogEvent, formatProvider)),
BuiltInProperty.EventId => Splice(context =>
new ScalarValue(EventIdHash.Compute(context.LogEvent.MessageTemplate.Text))),
var alias when _nameResolver.TryResolveBuiltInPropertyName(alias, out var target) =>
Transform(new AmbientNameExpression(target, true)),
_ => LX.Constant(null, typeof(LogEventPropertyValue))
};
}
// Don't close over the AST node.
var propertyName = px.PropertyName;
return Splice(context => Intrinsics.GetPropertyValue(context, propertyName));
}
protected override ExpressionBody Transform(LocalNameExpression nlx)
{
// Don't close over the AST node.
var name = nlx.Name;
return Splice(context => Intrinsics.GetLocalValue(context, name));
}
protected override ExpressionBody Transform(Ast.LambdaExpression lmx)
{
var parameters = lmx.Parameters.Select(px => Tuple.Create(px, LX.Parameter(typeof(LogEventPropertyValue), px.ParameterName))).ToList();
var paramSwitcher = new ExpressionConstantMapper(parameters.ToDictionary(px => (object)px.Item1, px => (System.Linq.Expressions.Expression)px.Item2));
var rewritten = paramSwitcher.Visit(Transform(lmx.Body));
Type delegateType;
if (lmx.Parameters.Length == 1)
delegateType = typeof(Func<LogEventPropertyValue, LogEventPropertyValue>);
else if (lmx.Parameters.Length == 2)
delegateType = typeof(Func<LogEventPropertyValue, LogEventPropertyValue, LogEventPropertyValue>);
else
throw new NotSupportedException("Unsupported lambda signature.");
var lambda = LX.Lambda(delegateType, rewritten!, parameters.Select(px => px.Item2).ToArray());
// Unfortunately, right now, functions need to be threaded through in constant scalar values :-D
return LX.New(typeof(ScalarValue).GetConstructor(new[] {typeof(object)})!,
LX.Convert(lambda, typeof(object)));
}
protected override ExpressionBody Transform(Ast.ParameterExpression prx)
{
// Will be within a lambda, which will subsequently sub-in the actual value.
// The `prx` placeholder needs to be wrapped in a `ScalarValue` so that eager
// typechecking doesn't fail before we've substituted the real value in.
return LX.Constant(new ScalarValue(prx), typeof(LogEventPropertyValue));
}
protected override ExpressionBody Transform(IndexerWildcardExpression wx)
{
return LX.Constant(null, typeof(LogEventPropertyValue));
}
protected override ExpressionBody Transform(ArrayExpression ax)
{
var elements = new List<ExpressionBody>(ax.Elements.Length);
var i = 0;
for (; i < ax.Elements.Length; ++i)
{
var element = ax.Elements[i];
if (element is ItemElement item)
elements.Add(Transform(item.Value));
else
break;
}
var arr = LX.NewArrayInit(typeof(LogEventPropertyValue), elements.ToArray());
var collected = LX.Call(CollectSequenceElementsMethod, arr);
for (; i < ax.Elements.Length; ++i)
{
var element = ax.Elements[i];
if (element is ItemElement item)
collected = LX.Call(ExtendSequenceValueWithItemMethod, collected, Transform(item.Value));
else
{
var spread = (SpreadElement) element;
collected = LX.Call(ExtendSequenceValueWithSpreadMethod, collected, Transform(spread.Content));
}
}
return LX.Call(ConstructSequenceValueMethod, collected);
}
protected override ExpressionBody Transform(ObjectExpression ox)
{
var names = new List<string>();
var values = new List<ExpressionBody>();
var i = 0;
for (; i < ox.Members.Length; ++i)
{
var member = ox.Members[i];
if (member is PropertyMember property)
{
if (names.Contains(property.Name))
{
var oldPos = names.IndexOf(property.Name);
values[oldPos] = Transform(property.Value);
}
else
{
names.Add(property.Name);
values.Add(Transform(property.Value));
}
}
else
{
break;
}
}
var namesConstant = LX.Constant(names.ToArray(), typeof(string[]));
var valuesArr = LX.NewArrayInit(typeof(LogEventPropertyValue), values.ToArray());
var properties = LX.Call(CollectStructurePropertiesMethod, namesConstant, valuesArr);
if (i == ox.Members.Length)
{
// No spreads; more efficient than `Complete*` because erasure is not required.
return LX.Call(ConstructStructureValueMethod, properties);
}
for (; i < ox.Members.Length; ++i)
{
var member = ox.Members[i];
if (member is PropertyMember property)
{
properties = LX.Call(
ExtendStructureValueWithPropertyMethod,
properties,
LX.Constant(property.Name),
Transform(property.Value));
}
else
{
var spread = (SpreadMember) member;
properties = LX.Call(
ExtendStructureValueWithSpreadMethod,
properties,
Transform(spread.Content));
}
}
return LX.Call(CompleteStructureValueMethod, properties);
}
protected override ExpressionBody Transform(IndexerExpression ix)
{
return Transform(new CallExpression(false, Operators.OpElementAt, ix.Receiver, ix.Index));
}
protected override ExpressionBody Transform(IndexOfMatchExpression mx)
{
var rx = LX.Constant(mx.Regex);
var target = Transform(mx.Corpus);
return LX.Call(IndexOfMatchMethod, target, rx);
}
}