This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
forked from dotnet/dotNext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UsingExpression.cs
164 lines (147 loc) · 6.9 KB
/
UsingExpression.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
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
namespace DotNext.Linq.Expressions
{
using static Reflection.DisposableType;
using Seq = Collections.Generic.Sequence;
/// <summary>
/// Represents <c>using</c> or <c>await using</c> expression.
/// </summary>
/// <seealso href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement">USING statement</seealso>
public sealed class UsingExpression : CustomExpression
{
/// <summary>
/// Represents constructor of <c>using</c> expression.
/// </summary>
/// <param name="resource">The variable representing disposable resource.</param>
/// <returns>Body of <c>using</c> expression.</returns>
public delegate Expression Statement(ParameterExpression resource);
private readonly MethodInfo disposeMethod;
private readonly BinaryExpression? assignment;
private readonly bool? configureAwait; // null for synchronous expression
private Expression? body;
internal UsingExpression(Expression resource)
{
disposeMethod = resource.Type.GetDisposeMethod() ?? throw new ArgumentException(ExceptionMessages.DisposePatternExpected(resource.Type), nameof(resource));
if (resource is ParameterExpression param)
{
assignment = null;
Resource = param;
}
else
{
assignment = Assign(Resource = Variable(resource.Type, "resource"), resource);
}
}
internal UsingExpression(Expression resource, bool configureAwait)
{
disposeMethod = resource.Type.GetDisposeAsyncMethod() ?? throw new ArgumentException(ExceptionMessages.DisposePatternExpected(resource.Type), nameof(resource));
if (resource is ParameterExpression param)
{
assignment = null;
Resource = param;
}
else
{
assignment = Assign(Resource = Variable(resource.Type, "resource"), resource);
}
this.configureAwait = configureAwait;
}
/// <summary>
/// Creates a block of code associated with disposable resource.
/// </summary>
/// <param name="resource">The disposable resource.</param>
/// <param name="body">The delegate used to construct the block of code.</param>
/// <returns>The constructed expression.</returns>
public static UsingExpression Create(Expression resource, Statement body)
{
var result = new UsingExpression(resource);
result.Body = body(result.Resource);
return result;
}
/// <summary>
/// Creates a block of code associated with asynchronously disposable resource.
/// </summary>
/// <param name="resource">The disposable resource.</param>
/// <param name="configureAwait"><see langword="true"/> to call <see cref="ValueTask.ConfigureAwait(bool)"/> with <see langword="false"/> argument when awaiting <see cref="IAsyncDisposable.DisposeAsync"/> method.</param>
/// <param name="body">The delegate used to construct the block of code.</param>
/// <returns>The constructed expression.</returns>
/// <seealso cref="IsAwaitable"/>
public static UsingExpression Create(Expression resource, bool configureAwait, Statement body)
{
var result = new UsingExpression(resource, configureAwait);
result.Body = body(result.Resource);
return result;
}
/// <summary>
/// Creates a block of code associated with disposable resource.
/// </summary>
/// <param name="resource">The disposable resource.</param>
/// <param name="body">The body of the statement.</param>
/// <returns>The constructed expression.</returns>
public static UsingExpression Create(Expression resource, Expression body)
=> new(resource) { Body = body };
/// <summary>
/// Creates a block of code associated with asynchronously disposable resource.
/// </summary>
/// <param name="resource">The disposable resource.</param>
/// <param name="configureAwait"><see langword="true"/> to call <see cref="ValueTask.ConfigureAwait(bool)"/> with <see langword="false"/> argument when awaiting <see cref="IAsyncDisposable.DisposeAsync"/> method.</param>
/// <param name="body">The body of the statement.</param>
/// <returns>The constructed expression.</returns>
/// <seealso cref="IsAwaitable"/>
public static UsingExpression Create(Expression resource, bool configureAwait, Expression body)
=> new(resource, configureAwait) { Body = body };
/// <summary>
/// Indicates that this <c>using</c> block is asynchronous.
/// </summary>
public bool IsAwaitable => configureAwait.HasValue;
/// <summary>
/// Gets body of <c>using</c> expression.
/// </summary>
public Expression Body
{
get => body ?? Empty();
internal set => body = value;
}
/// <summary>
/// Gets the variable holding the disposable resource.
/// </summary>
public ParameterExpression Resource { get; }
/// <summary>
/// Gets the type of this expression.
/// </summary>
public override Type Type => configureAwait is null ? Body.Type : typeof(void);
/// <summary>
/// Reconstructs expression with a new body.
/// </summary>
/// <param name="body">The new body of this expression.</param>
/// <returns>Updated expression.</returns>
public UsingExpression Update(Expression body)
{
var resource = assignment is null ? Resource : assignment.Right;
var result = this.configureAwait.TryGetValue(out var configureAwait) ?
new UsingExpression(resource, configureAwait) :
new UsingExpression(resource);
result.Body = body;
return result;
}
/// <summary>
/// Translates this expression into predefined set of expressions
/// using Lowering technique.
/// </summary>
/// <returns>Translated expression.</returns>
public override Expression Reduce()
{
Expression disposeCall = Call(Resource, disposeMethod);
if (this.configureAwait.TryGetValue(out var configureAwait))
{
disposeCall = disposeCall.Await(configureAwait);
}
return assignment is null ?
MakeTry(Type, Body, Block(typeof(void), disposeCall, Assign(Resource, Default(Resource.Type))), null, null) :
Block(Type, Seq.Singleton(Resource), assignment, TryFinally(Body, disposeCall));
}
}
}