-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncBlockExpression.cs
More file actions
170 lines (134 loc) · 6.22 KB
/
AsyncBlockExpression.cs
File metadata and controls
170 lines (134 loc) · 6.22 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
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq.Expressions;
using Hyperbee.Collections;
using Hyperbee.Expressions.CompilerServices;
using Hyperbee.Expressions.CompilerServices.Lowering;
namespace Hyperbee.Expressions;
[DebuggerTypeProxy( typeof( AsyncBlockExpressionDebuggerProxy ) )]
public class AsyncBlockExpression : Expression
{
private Expression _stateMachine;
public ReadOnlyCollection<Expression> Expressions { get; }
public ReadOnlyCollection<ParameterExpression> Variables { get; }
public ExpressionRuntimeOptions RuntimeOptions { get; }
internal LinkedDictionary<ParameterExpression, ParameterExpression> ScopedVariables { get; set; }
public Expression Result => Expressions[^1];
internal AsyncBlockExpression( ReadOnlyCollection<ParameterExpression> variables, ReadOnlyCollection<Expression> expressions, ExpressionRuntimeOptions options = null )
: this( variables, expressions, null, options )
{
}
internal AsyncBlockExpression(
ReadOnlyCollection<ParameterExpression> variables,
ReadOnlyCollection<Expression> expressions,
LinkedDictionary<ParameterExpression, ParameterExpression> scopedVariables,
ExpressionRuntimeOptions options = null
)
{
if ( expressions == null || expressions.Count == 0 )
throw new ArgumentException( $"{nameof( AsyncBlockExpression )} must contain at least one expression.", nameof( expressions ) );
Variables = variables;
Expressions = expressions;
ScopedVariables = scopedVariables;
RuntimeOptions = options;
Type = GetTaskType( Result.Type );
}
public override bool CanReduce => true;
public override ExpressionType NodeType => ExpressionType.Extension;
public override Type Type { get; }
public override Expression Reduce()
{
return _stateMachine ??= AsyncStateMachineBuilder.Create( Result.Type, LoweringTransformer, RuntimeOptions );
}
private AsyncLoweringInfo LoweringTransformer()
{
try
{
var visitor = new AsyncLoweringVisitor { Optimize = RuntimeOptions?.Optimize ?? true };
return visitor.Transform(
Result.Type,
[.. Variables],
[.. Expressions],
ScopedVariables ?? []
);
}
catch ( LoweringException ex )
{
throw new InvalidOperationException( $"Unable to lower {nameof( AsyncBlockExpression )}.", ex );
}
}
protected override Expression VisitChildren( ExpressionVisitor visitor )
{
var newVariables = visitor.VisitAndConvert( Variables, nameof( VisitChildren ) );
var newExpressions = visitor.Visit( Expressions );
if ( Compare( newVariables, Variables ) && Compare( newExpressions, Expressions ) )
return this;
return new AsyncBlockExpression( newVariables, newExpressions, ScopedVariables, RuntimeOptions );
}
internal static bool Compare<T>( ICollection<T> compare, IReadOnlyList<T> current )
where T : class
{
if ( ReferenceEquals( compare, current ) )
return true;
if ( compare == null )
return current.Count == 0;
if ( compare.Count != current.Count )
return false;
using var comparand = compare.GetEnumerator();
for ( var i = 0; i < current.Count; i++ )
{
comparand.MoveNext();
if ( !ReferenceEquals( comparand.Current, current[i] ) )
return false;
}
return true;
}
private static Type GetTaskType( Type resultType )
{
return resultType == typeof( void )
? typeof( Task )
: typeof( Task<> ).MakeGenericType( resultType );
}
private class AsyncBlockExpressionDebuggerProxy( AsyncBlockExpression node )
{
public Expression StateMachine => node._stateMachine;
public ReadOnlyCollection<Expression> Expressions => node.Expressions;
public ReadOnlyCollection<ParameterExpression> Variables => node.Variables;
public Expression Result => node.Result;
}
}
public static partial class ExpressionExtensions
{
public static AsyncBlockExpression BlockAsync( params Expression[] expressions )
{
return new AsyncBlockExpression( ReadOnlyCollection<ParameterExpression>.Empty, new ReadOnlyCollection<Expression>( expressions ) );
}
public static AsyncBlockExpression BlockAsync( ParameterExpression[] variables, params Expression[] expressions )
{
return new AsyncBlockExpression( new ReadOnlyCollection<ParameterExpression>( variables ), new ReadOnlyCollection<Expression>( expressions ) );
}
public static AsyncBlockExpression BlockAsync( ReadOnlyCollection<Expression> expressions )
{
return new AsyncBlockExpression( ReadOnlyCollection<ParameterExpression>.Empty, expressions );
}
public static AsyncBlockExpression BlockAsync( ReadOnlyCollection<ParameterExpression> variables, ReadOnlyCollection<Expression> expressions )
{
return new AsyncBlockExpression( variables, expressions );
}
public static AsyncBlockExpression BlockAsync( Expression[] expressions, ExpressionRuntimeOptions options )
{
return new AsyncBlockExpression( ReadOnlyCollection<ParameterExpression>.Empty, new ReadOnlyCollection<Expression>( expressions ), options );
}
public static AsyncBlockExpression BlockAsync( ParameterExpression[] variables, Expression[] expressions, ExpressionRuntimeOptions options )
{
return new AsyncBlockExpression( new ReadOnlyCollection<ParameterExpression>( variables ), new ReadOnlyCollection<Expression>( expressions ), options );
}
public static AsyncBlockExpression BlockAsync( ReadOnlyCollection<Expression> expressions, ExpressionRuntimeOptions options )
{
return new AsyncBlockExpression( ReadOnlyCollection<ParameterExpression>.Empty, expressions, options );
}
public static AsyncBlockExpression BlockAsync( ReadOnlyCollection<ParameterExpression> variables, ReadOnlyCollection<Expression> expressions, ExpressionRuntimeOptions options )
{
return new AsyncBlockExpression( variables, expressions, options );
}
}