-
Notifications
You must be signed in to change notification settings - Fork 4k
/
LocalRewriter_LockStatement.cs
240 lines (208 loc) · 11.1 KB
/
LocalRewriter_LockStatement.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp
{
internal sealed partial class LocalRewriter
{
/// <summary>
/// Lowers a lock statement to a try-finally block that calls (before and after the body, respectively):
/// <list type="bullet">
/// <item>Lock.EnterScope and Lock+Scope.Dispose if the argument is of type Lock, or</item>
/// <item>Monitor.Enter and Monitor.Exit.</item>
/// </list>
/// </summary>
public override BoundNode VisitLockStatement(BoundLockStatement node)
{
LockStatementSyntax lockSyntax = (LockStatementSyntax)node.Syntax;
BoundExpression rewrittenArgument = VisitExpression(node.Argument);
BoundStatement? rewrittenBody = VisitStatement(node.Body);
Debug.Assert(rewrittenBody is { });
TypeSymbol? argumentType = rewrittenArgument.Type;
if (argumentType is null)
{
// This isn't particularly elegant, but hopefully locking on null is
// not very common.
Debug.Assert(rewrittenArgument.ConstantValueOpt == ConstantValue.Null);
argumentType = _compilation.GetSpecialType(SpecialType.System_Object);
rewrittenArgument = MakeLiteral(
rewrittenArgument.Syntax,
rewrittenArgument.ConstantValueOpt,
argumentType); //need to have a non-null type here for TempHelpers.StoreToTemp.
}
if (argumentType.IsWellKnownTypeLock())
{
if (LockBinder.TryFindLockTypeInfo(argumentType, _diagnostics, rewrittenArgument.Syntax) is not { } lockTypeInfo)
{
Debug.Fail("We should have reported an error during binding if lock type info cannot be found.");
return node.Update(rewrittenArgument, rewrittenBody).WithHasErrors();
}
// lock (x) { body } -> using (x.EnterScope()) { body }
var tryBlock = rewrittenBody is BoundBlock block ? block : BoundBlock.SynthesizedNoLocals(lockSyntax, rewrittenBody);
var enterScopeCall = BoundCall.Synthesized(
rewrittenArgument.Syntax,
rewrittenArgument,
initialBindingReceiverIsSubjectToCloning: ThreeState.Unknown,
lockTypeInfo.EnterScopeMethod);
// the temp must be associated with the lock statement to support EnC slot mapping:
BoundLocal boundTemp = _factory.StoreToTemp(enterScopeCall,
out BoundAssignmentOperator tempAssignment,
syntaxOpt: lockSyntax,
kind: SynthesizedLocalKind.Using);
var expressionStatement = new BoundExpressionStatement(rewrittenArgument.Syntax, tempAssignment);
BoundStatement tryFinally = RewriteUsingStatementTryFinally(
rewrittenArgument.Syntax,
rewrittenArgument.Syntax,
tryBlock,
boundTemp,
awaitKeywordOpt: default,
awaitOpt: null,
patternDisposeInfo: MethodArgumentInfo.CreateParameterlessMethod(lockTypeInfo.ScopeDisposeMethod));
return new BoundBlock(
lockSyntax,
locals: ImmutableArray.Create(boundTemp.LocalSymbol),
statements: ImmutableArray.Create(expressionStatement, tryFinally));
}
if (argumentType.Kind == SymbolKind.TypeParameter)
{
// If the argument has a type parameter type, then we'll box it right away
// so that the same object is passed to both Monitor.Enter and Monitor.Exit.
argumentType = _compilation.GetSpecialType(SpecialType.System_Object);
rewrittenArgument = MakeConversionNode(
rewrittenArgument.Syntax,
rewrittenArgument,
Conversion.Boxing,
argumentType,
@checked: false,
constantValueOpt: rewrittenArgument.ConstantValueOpt);
}
BoundAssignmentOperator assignmentToLockTemp;
BoundLocal boundLockTemp = _factory.StoreToTemp(rewrittenArgument, out assignmentToLockTemp, syntaxOpt: lockSyntax, kind: SynthesizedLocalKind.Lock);
BoundStatement boundLockTempInit = new BoundExpressionStatement(lockSyntax, assignmentToLockTemp);
BoundExpression exitCallExpr;
MethodSymbol? exitMethod;
if (TryGetWellKnownTypeMember(lockSyntax, WellKnownMember.System_Threading_Monitor__Exit, out exitMethod))
{
exitCallExpr = BoundCall.Synthesized(
lockSyntax,
receiverOpt: null,
initialBindingReceiverIsSubjectToCloning: ThreeState.Unknown,
exitMethod,
boundLockTemp);
}
else
{
exitCallExpr = new BoundBadExpression(lockSyntax, LookupResultKind.NotInvocable, ImmutableArray<Symbol?>.Empty, ImmutableArray.Create<BoundExpression>(boundLockTemp), ErrorTypeSymbol.UnknownResultType);
}
BoundStatement exitCall = new BoundExpressionStatement(lockSyntax, exitCallExpr);
MethodSymbol? enterMethod;
if ((TryGetWellKnownTypeMember(lockSyntax, WellKnownMember.System_Threading_Monitor__Enter2, out enterMethod, isOptional: true) ||
TryGetWellKnownTypeMember(lockSyntax, WellKnownMember.System_Threading_Monitor__Enter, out enterMethod)) && // If we didn't find the overload introduced in .NET 4.0, then use the older one.
enterMethod.ParameterCount == 2)
{
// C# 4.0+ version
// L $lock = `argument`; // sequence point
// bool $lockTaken = false;
// try
// {
// Monitor.Enter($lock, ref $lockTaken);
// `body` // sequence point
// }
// finally
// { // hidden sequence point
// if ($lockTaken) Monitor.Exit($lock);
// }
TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);
BoundAssignmentOperator assignmentToLockTakenTemp;
BoundLocal boundLockTakenTemp = _factory.StoreToTemp(
MakeLiteral(rewrittenArgument.Syntax, ConstantValue.False, boolType),
store: out assignmentToLockTakenTemp,
syntaxOpt: lockSyntax,
kind: SynthesizedLocalKind.LockTaken);
BoundStatement boundLockTakenTempInit = new BoundExpressionStatement(lockSyntax, assignmentToLockTakenTemp);
BoundStatement enterCall = new BoundExpressionStatement(
lockSyntax,
BoundCall.Synthesized(
lockSyntax,
receiverOpt: null,
initialBindingReceiverIsSubjectToCloning: ThreeState.Unknown,
enterMethod,
boundLockTemp,
boundLockTakenTemp));
exitCall = RewriteIfStatement(
lockSyntax,
boundLockTakenTemp,
exitCall,
node.HasErrors);
return new BoundBlock(
lockSyntax,
ImmutableArray.Create(boundLockTemp.LocalSymbol, boundLockTakenTemp.LocalSymbol),
ImmutableArray.Create(
InstrumentLockTargetCapture(node, boundLockTempInit),
boundLockTakenTempInit,
new BoundTryStatement(
lockSyntax,
BoundBlock.SynthesizedNoLocals(lockSyntax, ImmutableArray.Create<BoundStatement>(
enterCall,
rewrittenBody)),
ImmutableArray<BoundCatchBlock>.Empty,
BoundBlock.SynthesizedNoLocals(lockSyntax,
exitCall))));
}
else
{
// Pre-4.0 version
// L $lock = `argument`; // sequence point
// Monitor.Enter($lock); // NB: before try-finally so we don't Exit if an exception prevents us from acquiring the lock.
// try
// {
// `body` // sequence point
// }
// finally
// {
// Monitor.Exit($lock); // hidden sequence point
// }
BoundExpression enterCallExpr;
if ((object?)enterMethod != null)
{
Debug.Assert(enterMethod.ParameterCount == 1);
enterCallExpr = BoundCall.Synthesized(
lockSyntax,
receiverOpt: null,
initialBindingReceiverIsSubjectToCloning: ThreeState.Unknown,
enterMethod,
boundLockTemp);
}
else
{
enterCallExpr = new BoundBadExpression(lockSyntax, LookupResultKind.NotInvocable, ImmutableArray<Symbol?>.Empty, ImmutableArray.Create<BoundExpression>(boundLockTemp), ErrorTypeSymbol.UnknownResultType);
}
BoundStatement enterCall = new BoundExpressionStatement(
lockSyntax,
enterCallExpr);
return new BoundBlock(
lockSyntax,
ImmutableArray.Create(boundLockTemp.LocalSymbol),
ImmutableArray.Create(
InstrumentLockTargetCapture(node, boundLockTempInit),
enterCall,
new BoundTryStatement(
lockSyntax,
BoundBlock.SynthesizedNoLocals(lockSyntax, rewrittenBody),
ImmutableArray<BoundCatchBlock>.Empty,
BoundBlock.SynthesizedNoLocals(lockSyntax, exitCall))));
}
}
private BoundStatement InstrumentLockTargetCapture(BoundLockStatement original, BoundStatement lockTargetCapture)
{
return this.Instrument ?
Instrumenter.InstrumentLockTargetCapture(original, lockTargetCapture) :
lockTargetCapture;
}
}
}