LambdaRewriter.cs 68.9 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5

#if DEBUG
//#define CHECK_LOCALS // define CHECK_LOCALS to help debug some rewriting problems that would otherwise cause code-gen failures

6
#endif
P
Pilchie 已提交
7 8 9 10
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
11
using Microsoft.CodeAnalysis.CodeGen;
P
Pilchie 已提交
12
using Microsoft.CodeAnalysis.CSharp.Symbols;
13
using Microsoft.CodeAnalysis.CSharp.Syntax;
P
Pilchie 已提交
14 15 16 17 18 19 20 21 22
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp
{
    /// <summary>
    /// The rewriter for removing lambda expressions from method bodies and introducing closure classes
    /// as containers for captured variables along the lines of the example in section 6.5.3 of the
    /// C# language specification.
    /// 
T
TomasMatousek 已提交
23
    /// The entry point is the public method <see cref="Rewrite"/>.  It operates as follows:
P
Pilchie 已提交
24 25 26
    /// 
    /// First, an analysis of the whole method body is performed that determines which variables are
    /// captured, what their scopes are, and what the nesting relationship is between scopes that
27
    /// have captured variables.  The result of this analysis is left in <see cref="_analysis"/>.
P
Pilchie 已提交
28 29
    /// 
    /// Then we make a frame, or compiler-generated class, represented by an instance of
T
TomasMatousek 已提交
30
    /// <see cref="LambdaFrame"/> for each scope with captured variables.  The generated frames are kept
31
    /// in <see cref="_frames"/>.  Each frame is given a single field for each captured
T
TomasMatousek 已提交
32
    /// variable in the corresponding scope.  These are are maintained in <see cref="MethodToClassRewriter.proxies"/>.
P
Pilchie 已提交
33 34
    /// 
    /// Finally, we walk and rewrite the input bound tree, keeping track of the following:
35 36 37
    /// (1) The current set of active frame pointers, in <see cref="_framePointers"/>
    /// (2) The current method being processed (this changes within a lambda's body), in <see cref="_currentMethod"/>
    /// (3) The "this" symbol for the current method in <see cref="_currentFrameThis"/>, and
P
Pilchie 已提交
38 39 40 41 42 43 44 45
    /// (4) The symbol that is used to access the innermost frame pointer (it could be a local variable or "this" parameter)
    /// 
    /// There are a few key transformations done in the rewriting.
    /// (1) Lambda expressions are turned into delegate creation expressions, and the body of the lambda is
    ///     moved into a new, compiler-generated method of a selected frame class.
    /// (2) On entry to a scope with captured variables, we create a frame object and store it in a local variable.
    /// (3) References to captured variables are transformed into references to fields of a frame class.
    /// 
T
TomasMatousek 已提交
46
    /// In addition, the rewriting deposits into <see cref="TypeCompilationState.SynthesizedMethods"/> a (<see cref="MethodSymbol"/>, <see cref="BoundStatement"/>)
P
Pilchie 已提交
47 48
    /// pair for each generated method.
    /// 
T
TomasMatousek 已提交
49
    /// <see cref="Rewrite"/> produces its output in two forms.  First, it returns a new bound statement
P
Pilchie 已提交
50
    /// for the caller to use for the body of the original method.  Second, it returns a collection of
T
TomasMatousek 已提交
51
    /// (<see cref="MethodSymbol"/>, <see cref="BoundStatement"/>) pairs for additional methods that the lambda rewriter produced.
P
Pilchie 已提交
52 53 54 55 56
    /// These additional methods contain the bodies of the lambdas moved into ordinary methods of their
    /// respective frame classes, and the caller is responsible for processing them just as it does with
    /// the returned bound node.  For example, the caller will typically perform iterator method and
    /// asynchronous method transformations, and emit IL instructions into an assembly.
    /// </summary>
57
    internal sealed partial class LambdaRewriter : MethodToClassRewriter
P
Pilchie 已提交
58
    {
59 60
        private readonly Analysis _analysis;
        private readonly MethodSymbol _topLevelMethod;
61
        private readonly MethodSymbol _substitutedSourceMethod;
62
        private readonly int _topLevelMethodOrdinal;
P
Pilchie 已提交
63

64 65
        // lambda frame for static lambdas. 
        // initialized lazily and could be null if there are no static lambdas
66
        private LambdaFrame _lazyStaticLambdaFrame;
67

68
        // A mapping from every lambda parameter to its corresponding method's parameter.
69
        private readonly Dictionary<ParameterSymbol, ParameterSymbol> _parameterMap = new Dictionary<ParameterSymbol, ParameterSymbol>();
70

71
        // A mapping from every local function to its lowered method
E
Evan Hauck 已提交
72 73 74 75 76 77 78 79 80 81 82
        private struct MappedLocalFunction
        {
            public readonly MethodSymbol Symbol;
            public readonly ClosureKind ClosureKind;
            public MappedLocalFunction(MethodSymbol symbol, ClosureKind closureKind)
            {
                Symbol = symbol;
                ClosureKind = closureKind;
            }
        }
        private readonly Dictionary<LocalFunctionSymbol, MappedLocalFunction> _localFunctionMap = new Dictionary<LocalFunctionSymbol, MappedLocalFunction>();
E
Evan Hauck 已提交
83

P
Pilchie 已提交
84
        // for each block with lifted (captured) variables, the corresponding frame type
85
        private readonly Dictionary<BoundNode, LambdaFrame> _frames = new Dictionary<BoundNode, LambdaFrame>();
P
Pilchie 已提交
86 87 88

        // the current set of frame pointers in scope.  Each is either a local variable (where introduced),
        // or the "this" parameter when at the top level.  Keys in this map are never constructed types.
89
        private readonly Dictionary<NamedTypeSymbol, Symbol> _framePointers = new Dictionary<NamedTypeSymbol, Symbol>();
P
Pilchie 已提交
90

91 92 93
        // True if the rewritten tree should include assignments of the
        // original locals to the lifted proxies. This is only useful for the
        // expression evaluator where the original locals are left as is.
94
        private readonly bool _assignLocals;
95

P
Pilchie 已提交
96
        // The current method or lambda being processed.
97
        private MethodSymbol _currentMethod;
P
Pilchie 已提交
98 99

        // The "this" symbol for the current method.
100
        private ParameterSymbol _currentFrameThis;
P
Pilchie 已提交
101

102
        private readonly ArrayBuilder<LambdaDebugInfo> _lambdaDebugInfoBuilder;
103

104
        // ID dispenser for field names of frame references
105
        private int _synthesizedFieldNameIdDispenser;
106

P
Pilchie 已提交
107
        // The symbol (field or local) holding the innermost frame
108
        private Symbol _innermostFramePointer;
P
Pilchie 已提交
109 110

        // The mapping of type parameters for the current lambda body
111
        private TypeMap _currentLambdaBodyTypeMap;
P
Pilchie 已提交
112 113

        // The current set of type parameters (mapped from the enclosing method's type parameters)
114
        private ImmutableArray<TypeParameterSymbol> _currentTypeParameters;
P
Pilchie 已提交
115 116 117

        // Initialization for the proxy of the upper frame if it needs to be deferred.
        // Such situation happens when lifting this in a ctor.
118
        private BoundExpression _thisProxyInitDeferred;
P
Pilchie 已提交
119 120

        // Set to true once we've seen the base (or self) constructor invocation in a constructor
121
        private bool _seenBaseCall;
P
Pilchie 已提交
122 123

        // Set to true while translating code inside of an expression lambda.
124
        private bool _inExpressionLambda;
P
Pilchie 已提交
125 126 127 128

        // When a lambda captures only 'this' of the enclosing method, we cache it in a local
        // variable.  This is the set of such local variables that must be added to the enclosing
        // method's top-level block.
129
        private ArrayBuilder<LocalSymbol> _addedLocals;
P
Pilchie 已提交
130 131 132

        // Similarly, this is the set of statements that must be added to the enclosing method's
        // top-level block initializing those variables to null.
133
        private ArrayBuilder<BoundStatement> _addedStatements;
P
Pilchie 已提交
134 135 136 137

        private LambdaRewriter(
            Analysis analysis,
            NamedTypeSymbol thisType,
T
TomasMatousek 已提交
138
            ParameterSymbol thisParameterOpt,
P
Pilchie 已提交
139
            MethodSymbol method,
140
            int methodOrdinal,
141
            MethodSymbol substitutedSourceMethod,
142
            ArrayBuilder<LambdaDebugInfo> lambdaDebugInfoBuilder,
143
            VariableSlotAllocator slotAllocatorOpt,
P
Pilchie 已提交
144 145
            TypeCompilationState compilationState,
            DiagnosticBag diagnostics,
146
            bool assignLocals)
147
            : base(slotAllocatorOpt, compilationState, diagnostics)
P
Pilchie 已提交
148
        {
T
TomasMatousek 已提交
149 150 151 152 153 154
            Debug.Assert(analysis != null);
            Debug.Assert(thisType != null);
            Debug.Assert(method != null);
            Debug.Assert(compilationState != null);
            Debug.Assert(diagnostics != null);

155
            _topLevelMethod = method;
156
            _substitutedSourceMethod = substitutedSourceMethod;
157 158 159 160 161 162 163 164 165 166 167
            _topLevelMethodOrdinal = methodOrdinal;
            _lambdaDebugInfoBuilder = lambdaDebugInfoBuilder;
            _currentMethod = method;
            _analysis = analysis;
            _assignLocals = assignLocals;
            _currentTypeParameters = method.TypeParameters;
            _currentLambdaBodyTypeMap = TypeMap.Empty;
            _innermostFramePointer = _currentFrameThis = thisParameterOpt;
            _framePointers[thisType] = thisParameterOpt;
            _seenBaseCall = method.MethodKind != MethodKind.Constructor; // only used for ctors
            _synthesizedFieldNameIdDispenser = 1;
P
Pilchie 已提交
168 169
        }

170 171
        protected override bool NeedsProxy(Symbol localOrParameter)
        {
E
Evan Hauck 已提交
172 173
            Debug.Assert(localOrParameter is LocalSymbol || localOrParameter is ParameterSymbol ||
                (localOrParameter as MethodSymbol)?.MethodKind == MethodKind.LocalFunction);
E
Evan Hauck 已提交
174
            return _analysis.CapturedVariables.ContainsKey(localOrParameter);
175 176
        }

P
Pilchie 已提交
177 178 179 180 181 182
        /// <summary>
        /// Rewrite the given node to eliminate lambda expressions.  Also returned are the method symbols and their
        /// bound bodies for the extracted lambda bodies. These would typically be emitted by the caller such as
        /// MethodBodyCompiler.  See this class' documentation
        /// for a more thorough explanation of the algorithm and its use by clients.
        /// </summary>
183
        /// <param name="loweredBody">The bound node to be rewritten</param>
P
Pilchie 已提交
184 185 186
        /// <param name="thisType">The type of the top-most frame</param>
        /// <param name="thisParameter">The "this" parameter in the top-most frame, or null if static method</param>
        /// <param name="method">The containing method of the node to be rewritten</param>
187
        /// <param name="methodOrdinal">Index of the method symbol in its containing type member list.</param>
188
        /// <param name="substitutedSourceMethod">If this is non-null, then <paramref name="method"/> will be treated as this for uses of parent symbols. For use in EE.</param>
189 190
        /// <param name="lambdaDebugInfoBuilder">Information on lambdas defined in <paramref name="method"/> needed for debugging.</param>
        /// <param name="closureDebugInfoBuilder">Information on closures defined in <paramref name="method"/> needed for debugging.</param>
191
        /// <param name="slotAllocatorOpt">Slot allocator.</param>
P
Pilchie 已提交
192 193
        /// <param name="compilationState">The caller's buffer into which we produce additional methods to be emitted by the caller</param>
        /// <param name="diagnostics">Diagnostic bag for diagnostics</param>
194
        /// <param name="assignLocals">The rewritten tree should include assignments of the original locals to the lifted proxies</param>
P
Pilchie 已提交
195
        public static BoundStatement Rewrite(
196
            BoundStatement loweredBody,
P
Pilchie 已提交
197 198 199
            NamedTypeSymbol thisType,
            ParameterSymbol thisParameter,
            MethodSymbol method,
200
            int methodOrdinal,
201
            MethodSymbol substitutedSourceMethod,
202 203
            ArrayBuilder<LambdaDebugInfo> lambdaDebugInfoBuilder,
            ArrayBuilder<ClosureDebugInfo> closureDebugInfoBuilder,
204
            VariableSlotAllocator slotAllocatorOpt,
P
Pilchie 已提交
205 206
            TypeCompilationState compilationState,
            DiagnosticBag diagnostics,
207
            bool assignLocals)
P
Pilchie 已提交
208 209 210
        {
            Debug.Assert((object)thisType != null);
            Debug.Assert(((object)thisParameter == null) || (thisParameter.Type == thisType));
211 212 213 214 215 216 217 218 219 220 221 222
            Debug.Assert(compilationState.ModuleBuilderOpt != null);

            var analysis = Analysis.Analyze(loweredBody, method);
            if (!analysis.SeenLambda)
            {
                // Unreachable anonymous functions are ignored by the analyzer.
                // No closures or lambda methods are generated.
                // E.g. 
                //   int y = 0;
                //   var b = false && from z in new X(y) select f(z + y)
                return loweredBody;
            }
P
Pilchie 已提交
223

224
            CheckLocalsDefined(loweredBody);
225 226 227 228 229
            var rewriter = new LambdaRewriter(
                analysis,
                thisType,
                thisParameter,
                method,
230
                methodOrdinal,
231
                substitutedSourceMethod,
232
                lambdaDebugInfoBuilder,
233
                slotAllocatorOpt,
234 235
                compilationState,
                diagnostics,
236
                assignLocals);
237

P
Pilchie 已提交
238
            analysis.ComputeLambdaScopesAndFrameCaptures();
239 240
            rewriter.MakeFrames(closureDebugInfoBuilder);
            var body = rewriter.AddStatementsIfNeeded((BoundStatement)rewriter.Visit(loweredBody));
P
Pilchie 已提交
241
            CheckLocalsDefined(body);
242

P
Pilchie 已提交
243 244 245
            return body;
        }

246
        private BoundStatement AddStatementsIfNeeded(BoundStatement body)
P
Pilchie 已提交
247
        {
248
            if (_addedLocals != null)
P
Pilchie 已提交
249
            {
250
                _addedStatements.Add(body);
E
Evan Hauck 已提交
251
                body = new BoundBlock(body.Syntax, _addedLocals.ToImmutableAndFree(), ImmutableArray<LocalFunctionSymbol>.Empty, _addedStatements.ToImmutableAndFree()) { WasCompilerGenerated = true };
252 253
                _addedLocals = null;
                _addedStatements = null;
P
Pilchie 已提交
254 255 256
            }
            else
            {
257
                Debug.Assert(_addedStatements == null);
P
Pilchie 已提交
258 259 260 261 262 263 264
            }

            return body;
        }

        protected override TypeMap TypeMap
        {
265
            get { return _currentLambdaBodyTypeMap; }
P
Pilchie 已提交
266 267 268 269
        }

        protected override MethodSymbol CurrentMethod
        {
270
            get { return _currentMethod; }
P
Pilchie 已提交
271 272 273 274
        }

        protected override NamedTypeSymbol ContainingType
        {
275
            get { return _topLevelMethod.ContainingType; }
P
Pilchie 已提交
276 277 278 279 280 281 282 283 284 285 286
        }

        /// <summary>
        /// Check that the top-level node is well-defined, in the sense that all
        /// locals that are used are defined in some enclosing scope.
        /// </summary>
        static partial void CheckLocalsDefined(BoundNode node);

        /// <summary>
        /// Create the frame types.
        /// </summary>
287
        private void MakeFrames(ArrayBuilder<ClosureDebugInfo> closureDebugInfo)
P
Pilchie 已提交
288 289 290
        {
            NamedTypeSymbol containingType = this.ContainingType;

E
Evan Hauck 已提交
291
            foreach (var kvp in _analysis.CapturedVariables)
P
Pilchie 已提交
292
            {
P
pgavlin 已提交
293 294
                var captured = kvp.Key;

295
                BoundNode scope;
E
Evan Hauck 已提交
296
                if (!_analysis.VariableScope.TryGetValue(captured, out scope))
P
Pilchie 已提交
297 298 299 300
                {
                    continue;
                }

301
                LambdaFrame frame = GetFrameForScope(scope, closureDebugInfo);
P
Pilchie 已提交
302

E
Evan Hauck 已提交
303
                if (captured.Kind != SymbolKind.Method)
P
Pilchie 已提交
304
                {
E
Evan Hauck 已提交
305 306 307 308 309
                    var hoistedField = LambdaCapturedVariable.Create(frame, captured, ref _synthesizedFieldNameIdDispenser);
                    proxies.Add(captured, new CapturedToFrameSymbolReplacement(hoistedField, isReusable: false));
                    CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(frame, hoistedField);

                    if (hoistedField.Type.IsRestrictedType())
P
Pilchie 已提交
310
                    {
E
Evan Hauck 已提交
311 312 313 314 315
                        foreach (CSharpSyntaxNode syntax in kvp.Value)
                        {
                            // CS4013: Instance of type '{0}' cannot be used inside an anonymous function, query expression, iterator block or async method
                            this.Diagnostics.Add(ErrorCode.ERR_SpecialByRefInLambda, syntax.Location, hoistedField.Type);
                        }
P
Pilchie 已提交
316 317 318 319 320
                    }
                }
            }
        }

321
        private LambdaFrame GetFrameForScope(BoundNode scope, ArrayBuilder<ClosureDebugInfo> closureDebugInfo)
322 323
        {
            LambdaFrame frame;
324
            if (!_frames.TryGetValue(scope, out frame))
325
            {
326 327 328
                var syntax = scope.Syntax;
                Debug.Assert(syntax != null);

329 330
                DebugId methodId = GetTopLevelMethodId();
                DebugId closureId = GetClosureId(syntax, closureDebugInfo);
331

E
Evan Hauck 已提交
332
                var canBeStruct = !_analysis.ScopesThatCantBeStructs.Contains(scope);
333

E
Evan Hauck 已提交
334
                var containingMethod = _analysis.ScopeOwner[scope];
335 336 337 338
                if (_substitutedSourceMethod != null && containingMethod == _topLevelMethod)
                {
                    containingMethod = _substitutedSourceMethod;
                }
E
Evan Hauck 已提交
339
                frame = new LambdaFrame(_topLevelMethod, containingMethod, canBeStruct, syntax, methodId, closureId);
340
                _frames.Add(scope, frame);
341

342
                CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(this.ContainingType, frame);
343 344 345 346 347 348 349
                if (frame.Constructor != null)
                {
                    CompilationState.AddSynthesizedMethod(
                        frame.Constructor,
                        FlowAnalysisPass.AppendImplicitReturn(MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
                        frame.Constructor));
                }
350 351 352 353 354
            }

            return frame;
        }

E
Evan Hauck 已提交
355
        private LambdaFrame GetStaticFrame(DiagnosticBag diagnostics, IBoundLambdaOrFunction lambda)
356
        {
357
            if (_lazyStaticLambdaFrame == null)
358
            {
359
                var isNonGeneric = !_topLevelMethod.IsGenericMethod;
360 361
                if (isNonGeneric)
                {
362
                    _lazyStaticLambdaFrame = CompilationState.staticLambdaFrame;
363 364
                }

365
                if (_lazyStaticLambdaFrame == null)
366
                {
367
                    DebugId methodId;
368 369
                    if (isNonGeneric)
                    {
370
                        methodId = new DebugId(DebugId.UndefinedOrdinal, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
371 372 373
                    }
                    else
                    {
374
                        methodId = GetTopLevelMethodId();
375
                    }
376

E
Evan Hauck 已提交
377
                    DebugId closureId = default(DebugId);
E
Evan Hauck 已提交
378
                    // using _topLevelMethod as containing member because the static frame does not have generic parameters, except for the top level method's
379
                    var containingMethod = isNonGeneric ? null : (_substitutedSourceMethod ?? _topLevelMethod);
E
Evan Hauck 已提交
380
                    _lazyStaticLambdaFrame = new LambdaFrame(_topLevelMethod, containingMethod, isStruct: false, scopeSyntaxOpt: null, methodId: methodId, closureId: closureId);
381 382

                    // nongeneric static lambdas can share the frame
383
                    if (isNonGeneric)
384
                    {
385
                        CompilationState.staticLambdaFrame = _lazyStaticLambdaFrame;
386 387
                    }

388
                    var frame = _lazyStaticLambdaFrame;
389 390 391 392

                    // add frame type
                    CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(this.ContainingType, frame);

393
                    // add its ctor (note Constructor can be null if TypeKind.Struct is passed in to LambdaFrame.ctor, but Class is passed in above)
394 395 396 397 398 399
                    CompilationState.AddSynthesizedMethod(
                        frame.Constructor,
                        FlowAnalysisPass.AppendImplicitReturn(MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
                        frame.Constructor));

                    // associate the frame with the the first lambda that caused it to exist. 
C
Charles Stoner 已提交
400
                    // we need to associate this with some syntax.
401 402 403 404 405 406 407 408 409 410 411 412 413 414
                    // unfortunately either containing method or containing class could be synthetic
                    // therefore could have no syntax.
                    CSharpSyntaxNode syntax = lambda.Syntax;

                    // add cctor
                    // Frame.inst = new Frame()
                    var F = new SyntheticBoundNodeFactory(frame.StaticConstructor, syntax, CompilationState, diagnostics);
                    var body = F.Block(
                            F.Assignment(
                                F.Field(null, frame.SingletonCache),
                                F.New(frame.Constructor)),
                            F.Return());

                    CompilationState.AddSynthesizedMethod(frame.StaticConstructor, body);
415 416 417
                }
            }

418
            return _lazyStaticLambdaFrame;
419 420
        }

P
Pilchie 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        /// <summary>
        /// Produce a bound expression representing a pointer to a frame of a particular frame type.
        /// </summary>
        /// <param name="syntax">The syntax to attach to the bound nodes produced</param>
        /// <param name="frameType">The type of frame to be returned</param>
        /// <returns>A bound node that computes the pointer to the required frame</returns>
        private BoundExpression FrameOfType(CSharpSyntaxNode syntax, NamedTypeSymbol frameType)
        {
            BoundExpression result = FramePointer(syntax, frameType.OriginalDefinition);
            Debug.Assert(result.Type == frameType);
            return result;
        }

        /// <summary>
        /// Produce a bound expression representing a pointer to a frame of a particular frame class.
        /// Note that for generic frames, the frameClass parameter is the generic definition, but
        /// the resulting expression will be constructed with the current type parameters.
        /// </summary>
        /// <param name="syntax">The syntax to attach to the bound nodes produced</param>
        /// <param name="frameClass">The class type of frame to be returned</param>
        /// <returns>A bound node that computes the pointer to the required frame</returns>
        protected override BoundExpression FramePointer(CSharpSyntaxNode syntax, NamedTypeSymbol frameClass)
        {
            Debug.Assert(frameClass.IsDefinition);

            // If in an instance method of the right type, we can just return the "this" pointer.
447
            if ((object)_currentFrameThis != null && _currentFrameThis.Type == frameClass)
P
Pilchie 已提交
448 449 450 451 452
            {
                return new BoundThisReference(syntax, frameClass);
            }

            // Otherwise we need to return the value from a frame pointer local variable...
453
            Symbol framePointer = _framePointers[frameClass];
P
Pilchie 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
            CapturedSymbolReplacement proxyField;
            if (proxies.TryGetValue(framePointer, out proxyField))
            {
                // However, frame pointer local variables themselves can be "captured".  In that case
                // the inner frames contain pointers to the enclosing frames.  That is, nested
                // frame pointers are organized in a linked list.
                return proxyField.Replacement(syntax, frameType => FramePointer(syntax, frameType));
            }

            var localFrame = framePointer as LocalSymbol;
            return new BoundLocal(syntax, localFrame, null, localFrame.Type);
        }

        private static void InsertAndFreePrologue(ArrayBuilder<BoundStatement> result, ArrayBuilder<BoundExpression> prologue)
        {
            foreach (var expr in prologue)
            {
                result.Add(new BoundExpressionStatement(expr.Syntax, expr));
            }

            prologue.Free();
        }

        /// <summary>
        /// Introduce a frame around the translation of the given node.
        /// </summary>
        /// <param name="node">The node whose translation should be translated to contain a frame</param>
        /// <param name="frame">The frame for the translated node</param>
        /// <param name="F">A function that computes the translation of the node.  It receives lists of added statements and added symbols</param>
        /// <returns>The translated statement, as returned from F</returns>
        private T IntroduceFrame<T>(BoundNode node, LambdaFrame frame, Func<ArrayBuilder<BoundExpression>, ArrayBuilder<LocalSymbol>, T> F)
        {
E
Evan Hauck 已提交
486 487
            var frameTypeParameters = ImmutableArray.Create(StaticCast<TypeSymbol>.From(_currentTypeParameters), 0, frame.Arity);
            NamedTypeSymbol frameType = frame.ConstructIfGeneric(frameTypeParameters);
488 489

            Debug.Assert(frame.ScopeSyntaxOpt != null);
490
            LocalSymbol framePointer = new SynthesizedLocal(_topLevelMethod, frameType, SynthesizedLocalKind.LambdaDisplayClass, frame.ScopeSyntaxOpt);
P
Pilchie 已提交
491 492 493 494 495 496 497

            CSharpSyntaxNode syntax = node.Syntax;

            // assign new frame to the frame variable

            var prologue = ArrayBuilder<BoundExpression>.GetInstance();

498 499 500 501 502 503 504 505 506 507 508 509 510 511
            BoundExpression newFrame;
            if (frame.Constructor == null)
            {
                Debug.Assert(frame.TypeKind == TypeKind.Struct);
                newFrame = new BoundDefaultOperator(syntax: syntax, type: frameType);
            }
            else
            {
                MethodSymbol constructor = frame.Constructor.AsMember(frameType);
                Debug.Assert(frameType == constructor.ContainingType);
                newFrame = new BoundObjectCreationExpression(
                    syntax: syntax,
                    constructor: constructor);
            }
P
Pilchie 已提交
512 513 514 515 516 517 518

            prologue.Add(new BoundAssignmentOperator(syntax,
                new BoundLocal(syntax, framePointer, null, frameType),
                newFrame,
                frameType));

            CapturedSymbolReplacement oldInnermostFrameProxy = null;
519
            if ((object)_innermostFramePointer != null)
P
Pilchie 已提交
520
            {
521
                proxies.TryGetValue(_innermostFramePointer, out oldInnermostFrameProxy);
E
Evan Hauck 已提交
522
                if (_analysis.NeedsParentFrame.Contains(node))
P
Pilchie 已提交
523
                {
524
                    var capturedFrame = LambdaCapturedVariable.Create(frame, _innermostFramePointer, ref _synthesizedFieldNameIdDispenser);
P
Pilchie 已提交
525 526 527 528 529
                    FieldSymbol frameParent = capturedFrame.AsMember(frameType);
                    BoundExpression left = new BoundFieldAccess(syntax, new BoundLocal(syntax, framePointer, null, frameType), frameParent, null);
                    BoundExpression right = FrameOfType(syntax, frameParent.Type as NamedTypeSymbol);
                    BoundExpression assignment = new BoundAssignmentOperator(syntax, left, right, left.Type);

530
                    if (_currentMethod.MethodKind == MethodKind.Constructor && capturedFrame.Type == _currentMethod.ContainingType && !_seenBaseCall)
P
Pilchie 已提交
531 532 533 534 535
                    {
                        // Containing method is a constructor 
                        // Initialization statement for the "this" proxy must be inserted
                        // after the constructor initializer statement block
                        // This insertion will be done by the delegate F
536 537
                        Debug.Assert(_thisProxyInitDeferred == null);
                        _thisProxyInitDeferred = assignment;
P
Pilchie 已提交
538 539 540 541 542 543 544 545
                    }
                    else
                    {
                        prologue.Add(assignment);
                    }

                    if (CompilationState.Emitting)
                    {
E
Evan Hauck 已提交
546
                        Debug.Assert(capturedFrame.Type.IsReferenceType); // Make sure we're not accidentally capturing a struct by value
T
TomasMatousek 已提交
547
                        CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(frame, capturedFrame);
P
Pilchie 已提交
548 549
                    }

550
                    proxies[_innermostFramePointer] = new CapturedToFrameSymbolReplacement(capturedFrame, isReusable: false);
P
Pilchie 已提交
551 552 553 554
                }
            }

            // Capture any parameters of this block.  This would typically occur
555
            // at the top level of a method or lambda with captured parameters.
P
Pilchie 已提交
556
            // TODO: speed up the following by computing it in analysis.
E
Evan Hauck 已提交
557
            foreach (var variable in _analysis.CapturedVariables.Keys)
P
Pilchie 已提交
558 559
            {
                BoundNode varNode;
E
Evan Hauck 已提交
560
                if (!_analysis.VariableScope.TryGetValue(variable, out varNode) || varNode != node)
P
Pilchie 已提交
561 562 563 564
                {
                    continue;
                }

565
                InitVariableProxy(syntax, variable, framePointer, prologue);
P
Pilchie 已提交
566 567
            }

568 569
            Symbol oldInnermostFramePointer = _innermostFramePointer;
            _innermostFramePointer = framePointer;
P
Pilchie 已提交
570 571
            var addedLocals = ArrayBuilder<LocalSymbol>.GetInstance();
            addedLocals.Add(framePointer);
572
            _framePointers.Add(frame, framePointer);
P
Pilchie 已提交
573 574 575

            var result = F(prologue, addedLocals);

576 577
            _framePointers.Remove(frame);
            _innermostFramePointer = oldInnermostFramePointer;
P
Pilchie 已提交
578

579
            if ((object)_innermostFramePointer != null)
P
Pilchie 已提交
580 581 582
            {
                if (oldInnermostFrameProxy != null)
                {
583
                    proxies[_innermostFramePointer] = oldInnermostFrameProxy;
P
Pilchie 已提交
584 585 586
                }
                else
                {
587
                    proxies.Remove(_innermostFramePointer);
P
Pilchie 已提交
588 589 590 591 592 593
                }
            }

            return result;
        }

594
        private void InitVariableProxy(CSharpSyntaxNode syntax, Symbol symbol, LocalSymbol framePointer, ArrayBuilder<BoundExpression> prologue)
P
Pilchie 已提交
595 596
        {
            CapturedSymbolReplacement proxy;
597
            if (proxies.TryGetValue(symbol, out proxy))
P
Pilchie 已提交
598
            {
599 600
                BoundExpression value;
                switch (symbol.Kind)
601
                {
602 603 604
                    case SymbolKind.Parameter:
                        var parameter = (ParameterSymbol)symbol;
                        ParameterSymbol parameterToUse;
605
                        if (!_parameterMap.TryGetValue(parameter, out parameterToUse))
606 607
                        {
                            parameterToUse = parameter;
608
                        }
609 610

                        value = new BoundParameter(syntax, parameterToUse);
611
                        break;
612

613
                    case SymbolKind.Local:
614
                        if (!_assignLocals)
615 616 617
                        {
                            return;
                        }
618

619 620 621 622 623 624 625 626
                        var local = (LocalSymbol)symbol;
                        LocalSymbol localToUse;
                        if (!localMap.TryGetValue(local, out localToUse))
                        {
                            localToUse = local;
                        }

                        value = new BoundLocal(syntax, localToUse, null, localToUse.Type);
627
                        break;
E
Evan Hauck 已提交
628

629 630 631
                    default:
                        throw ExceptionUtilities.UnexpectedValue(symbol.Kind);
                }
P
Pilchie 已提交
632

E
Evan Hauck 已提交
633 634 635
                var left = proxy.Replacement(syntax, frameType1 => new BoundLocal(syntax, framePointer, null, framePointer.Type));
                var assignToProxy = new BoundAssignmentOperator(syntax, left, value, value.Type);
                prologue.Add(assignToProxy);
P
Pilchie 已提交
636 637 638 639 640
            }
        }

        #region Visit Methods

641 642 643
        protected override BoundNode VisitUnhoistedParameter(BoundParameter node)
        {
            ParameterSymbol replacementParameter;
644
            if (_parameterMap.TryGetValue(node.ParameterSymbol, out replacementParameter))
645 646 647 648 649 650 651
            {
                return new BoundParameter(node.Syntax, replacementParameter, replacementParameter.Type, node.HasErrors);
            }

            return base.VisitUnhoistedParameter(node);
        }

P
Pilchie 已提交
652 653 654 655 656 657 658 659 660 661 662 663
        public override BoundNode VisitThisReference(BoundThisReference node)
        {
            // "topLevelMethod.ThisParameter == null" can occur in a delegate creation expression because the method group
            // in the argument can have a "this" receiver even when "this"
            // is not captured because a static method is selected.  But we do preserve
            // the method group and its receiver in the bound tree.
            // No need to capture "this" in such case.

            // TODO: Why don't we drop "this" while lowering if method is static? 
            //       Actually, considering that method group expression does not evaluate to a particular value 
            //       why do we have it in the lowered tree at all?

664
            return (_currentMethod == _topLevelMethod || _topLevelMethod.ThisParameter == null ?
665 666
                node :
                FramePointer(node.Syntax, (NamedTypeSymbol)node.Type));
P
Pilchie 已提交
667 668 669 670
        }

        public override BoundNode VisitBaseReference(BoundBaseReference node)
        {
671
            return (_currentMethod.ContainingType == _topLevelMethod.ContainingType)
P
Pilchie 已提交
672
                ? node
673
                : FramePointer(node.Syntax, _topLevelMethod.ContainingType); // technically, not the correct static type
P
Pilchie 已提交
674
        }
675

E
Evan Hauck 已提交
676 677 678 679 680 681 682 683
        private void RemapLambdaOrLocalFunction(
            CSharpSyntaxNode syntax,
            MethodSymbol originalMethod,
            ImmutableArray<TypeSymbol> typeArgumentsOpt,
            ClosureKind closureKind,
            ref MethodSymbol synthesizedMethod,
            out BoundExpression receiver,
            out NamedTypeSymbol constructedFrame)
E
Evan Hauck 已提交
684
        {
E
Evan Hauck 已提交
685 686
            var translatedLambdaContainer = synthesizedMethod.ContainingType;
            var containerAsFrame = translatedLambdaContainer as LambdaFrame;
687

688
            // All of _currentTypeParameters might not be preserved here due to recursively calling upwards in the chain of local functions/lambdas
E
Evan Hauck 已提交
689
            Debug.Assert((typeArgumentsOpt.IsDefault && !originalMethod.IsGenericMethod) || (typeArgumentsOpt.Length == originalMethod.Arity));
690
            var totalTypeArgumentCount = (containerAsFrame?.Arity ?? 0) + synthesizedMethod.Arity;
E
Evan Hauck 已提交
691 692
            var realTypeArguments = ImmutableArray.Create(StaticCast<TypeSymbol>.From(_currentTypeParameters), 0, totalTypeArgumentCount - originalMethod.Arity);
            if (!typeArgumentsOpt.IsDefault)
693
            {
E
Evan Hauck 已提交
694
                realTypeArguments = realTypeArguments.Concat(typeArgumentsOpt);
695 696
            }

E
Evan Hauck 已提交
697 698 699 700 701 702 703 704 705 706
            if (containerAsFrame != null && containerAsFrame.Arity != 0)
            {
                var containerTypeArguments = ImmutableArray.Create(realTypeArguments, 0, containerAsFrame.Arity);
                realTypeArguments = ImmutableArray.Create(realTypeArguments, containerAsFrame.Arity, realTypeArguments.Length - containerAsFrame.Arity);
                constructedFrame = containerAsFrame.Construct(containerTypeArguments);
            }
            else
            {
                constructedFrame = translatedLambdaContainer;
            }
E
Evan Hauck 已提交
707 708 709

            // for instance lambdas, receiver is the frame
            // for static lambdas, get the singleton receiver
710
            if (closureKind == ClosureKind.Singleton)
E
Evan Hauck 已提交
711 712 713 714
            {
                var field = containerAsFrame.SingletonCache.AsMember(constructedFrame);
                receiver = new BoundFieldAccess(syntax, null, field, constantValueOpt: null);
            }
715 716 717 718 719 720 721 722
            else if (closureKind == ClosureKind.Static)
            {
                receiver = null;
            }
            else // ThisOnly and General
            {
                receiver = FrameOfType(syntax, constructedFrame);
            }
723

E
Evan Hauck 已提交
724 725 726 727 728 729
            synthesizedMethod = synthesizedMethod.AsMember(constructedFrame);
            if (synthesizedMethod.IsGenericMethod)
            {
                synthesizedMethod = synthesizedMethod.Construct(StaticCast<TypeSymbol>.From(realTypeArguments));
            }
            else
E
Evan Hauck 已提交
730
            {
E
Evan Hauck 已提交
731
                Debug.Assert(realTypeArguments.Length == 0);
E
Evan Hauck 已提交
732
            }
733
        }
P
Pilchie 已提交
734

E
Evan Hauck 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        private void RemapLocalFunction(
            CSharpSyntaxNode syntax, MethodSymbol symbol,
            out BoundExpression receiver, out MethodSymbol method,
            ImmutableArray<TypeSymbol> typeArguments = default(ImmutableArray<TypeSymbol>))
        {
            Debug.Assert(symbol.MethodKind == MethodKind.LocalFunction);

            var constructed = symbol as ConstructedMethodSymbol;
            if (constructed != null)
            {
                RemapLocalFunction(syntax, constructed.ConstructedFrom, out receiver, out method, this.TypeMap.SubstituteTypes(constructed.TypeArguments));
                return;
            }

            var mappedLocalFunction = _localFunctionMap[(LocalFunctionSymbol)symbol];
            method = mappedLocalFunction.Symbol;

            NamedTypeSymbol constructedFrame;
            RemapLambdaOrLocalFunction(syntax, symbol, typeArguments, mappedLocalFunction.ClosureKind, ref method, out receiver, out constructedFrame);
        }

P
Pilchie 已提交
756 757
        public override BoundNode VisitCall(BoundCall node)
        {
E
Evan Hauck 已提交
758 759 760 761 762 763 764 765
            if (node.Method.MethodKind == MethodKind.LocalFunction)
            {
                BoundExpression receiver;
                MethodSymbol method;
                RemapLocalFunction(node.Syntax, node.Method, out receiver, out method);
                node = node.Update(receiver, method, node.Arguments);
            }
            var visited = base.VisitCall(node);
P
Pilchie 已提交
766 767 768 769 770 771 772 773
            if (visited.Kind != BoundKind.Call)
            {
                return visited;
            }

            var rewritten = (BoundCall)visited;

            // Check if we need to init the 'this' proxy in a ctor call
774
            if (!_seenBaseCall)
P
Pilchie 已提交
775
            {
776 777
                _seenBaseCall = _currentMethod == _topLevelMethod && node.IsConstructorInitializer();
                if (_seenBaseCall && _thisProxyInitDeferred != null)
P
Pilchie 已提交
778 779 780 781 782 783 784
                {
                    // Insert the this proxy assignment after the ctor call.
                    // Create bound sequence: { ctor call, thisProxyInitDeferred }
                    return new BoundSequence(
                        syntax: node.Syntax,
                        locals: ImmutableArray<LocalSymbol>.Empty,
                        sideEffects: ImmutableArray.Create<BoundExpression>(rewritten),
785
                        value: _thisProxyInitDeferred,
P
Pilchie 已提交
786 787 788 789 790 791 792 793 794
                        type: rewritten.Type);
                }
            }

            return rewritten;
        }

        private BoundSequence RewriteSequence(BoundSequence node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
        {
795
            RewriteLocals(node.Locals, newLocals);
P
Pilchie 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812

            foreach (var expr in node.SideEffects)
            {
                var replacement = (BoundExpression)this.Visit(expr);
                if (replacement != null) prologue.Add(replacement);
            }

            var newValue = (BoundExpression)this.Visit(node.Value);
            var newType = this.VisitType(node.Type);

            return node.Update(newLocals.ToImmutableAndFree(), prologue.ToImmutableAndFree(), newValue, newType);
        }

        public override BoundNode VisitBlock(BoundBlock node)
        {
            LambdaFrame frame;
            // Test if this frame has captured variables and requires the introduction of a closure class.
813
            if (_frames.TryGetValue(node, out frame))
P
Pilchie 已提交
814 815 816 817 818 819 820 821 822 823
            {
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
                    RewriteBlock(node, prologue, newLocals));
            }
            else
            {
                return RewriteBlock(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
            }
        }

824
        private BoundBlock RewriteBlock(BoundBlock node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
P
Pilchie 已提交
825
        {
826
            RewriteLocals(node.Locals, newLocals);
P
Pilchie 已提交
827 828 829

            var newStatements = ArrayBuilder<BoundStatement>.GetInstance();

830 831 832 833 834 835 836
            if (prologue.Count > 0)
            {
                newStatements.Add(new BoundSequencePoint(null, null) { WasCompilerGenerated = true });
            }

            InsertAndFreePrologue(newStatements, prologue);

P
Pilchie 已提交
837 838 839 840 841 842 843 844 845 846
            foreach (var statement in node.Statements)
            {
                var replacement = (BoundStatement)this.Visit(statement);
                if (replacement != null)
                {
                    newStatements.Add(replacement);
                }
            }

            // TODO: we may not need to update if there was nothing to rewrite.
E
Evan Hauck 已提交
847
            return node.Update(newLocals.ToImmutableAndFree(), node.LocalFunctions, newStatements.ToImmutableAndFree());
P
Pilchie 已提交
848 849 850 851 852 853
        }

        public override BoundNode VisitCatchBlock(BoundCatchBlock node)
        {
            // Test if this frame has captured variables and requires the introduction of a closure class.
            LambdaFrame frame;
854
            if (_frames.TryGetValue(node, out frame))
P
Pilchie 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868
            {
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
                {
                    return RewriteCatch(node, prologue, newLocals);
                });
            }
            else
            {
                return RewriteCatch(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
            }
        }

        private BoundNode RewriteCatch(BoundCatchBlock node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
        {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
            LocalSymbol newLocal;
            if ((object)node.LocalOpt != null && TryRewriteLocal(node.LocalOpt, out newLocal))
            {
                newLocals.Add(newLocal);
            }

            LocalSymbol rewrittenCatchLocal;

            if (newLocals.Count > 0)
            {
                // If the original LocalOpt was lifted into a closure,
                // the newLocals will contain a frame reference. In this case, 
                // instead of an actual local, catch will own the frame reference.

                Debug.Assert((object)node.LocalOpt != null && newLocals.Count == 1);
                rewrittenCatchLocal = newLocals[0];
            }
            else
            {
                Debug.Assert((object)node.LocalOpt == null);
                rewrittenCatchLocal = null;
            }
P
Pilchie 已提交
891 892 893

            // If exception variable got lifted, IntroduceFrame will give us frame init prologue.
            // It needs to run before the exception variable is accessed.
C
Charles Stoner 已提交
894
            // To ensure that, we will make exception variable a sequence that performs prologue as its its sideeffects.
P
Pilchie 已提交
895
            BoundExpression rewrittenExceptionSource = null;
896
            var rewrittenFilter = (BoundExpression)this.Visit(node.ExceptionFilterOpt);
P
Pilchie 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909
            if (node.ExceptionSourceOpt != null)
            {
                rewrittenExceptionSource = (BoundExpression)Visit(node.ExceptionSourceOpt);
                if (prologue.Count > 0)
                {
                    rewrittenExceptionSource = new BoundSequence(
                        rewrittenExceptionSource.Syntax,
                        ImmutableArray.Create<LocalSymbol>(),
                        prologue.ToImmutable(),
                        rewrittenExceptionSource,
                        rewrittenExceptionSource.Type);
                }
            }
910 911 912 913 914 915 916 917 918 919
            else if (prologue.Count > 0)
            {
                Debug.Assert(rewrittenFilter != null);
                rewrittenFilter = new BoundSequence(
                    rewrittenFilter.Syntax,
                    ImmutableArray.Create<LocalSymbol>(),
                    prologue.ToImmutable(),
                    rewrittenFilter,
                    rewrittenFilter.Type);
            }
P
Pilchie 已提交
920

921
            // done with this.
922
            newLocals.Free();
P
Pilchie 已提交
923 924 925 926 927 928 929 930
            prologue.Free();

            // rewrite filter and body
            // NOTE: this will proxy all accesses to exception local if that got lifted.
            var exceptionTypeOpt = this.VisitType(node.ExceptionTypeOpt);
            var rewrittenBlock = (BoundBlock)this.Visit(node.Body);

            return node.Update(
931
                rewrittenCatchLocal,
P
Pilchie 已提交
932 933 934
                rewrittenExceptionSource,
                exceptionTypeOpt,
                rewrittenFilter,
935 936
                rewrittenBlock,
                node.IsSynthesizedAsyncCatchAll);
P
Pilchie 已提交
937 938 939 940 941 942
        }

        public override BoundNode VisitSequence(BoundSequence node)
        {
            LambdaFrame frame;
            // Test if this frame has captured variables and requires the introduction of a closure class.
943
            if (_frames.TryGetValue(node, out frame))
P
Pilchie 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
            {
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
                {
                    return RewriteSequence(node, prologue, newLocals);
                });
            }
            else
            {
                return RewriteSequence(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
            }
        }

        public override BoundNode VisitStatementList(BoundStatementList node)
        {
            LambdaFrame frame;
            // Test if this frame has captured variables and requires the introduction of a closure class.
            // That can occur for a BoundStatementList if it is the body of a method with captured parameters.
961
            if (_frames.TryGetValue(node, out frame))
P
Pilchie 已提交
962 963 964 965 966 967 968 969 970 971 972
            {
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
                {
                    var newStatements = ArrayBuilder<BoundStatement>.GetInstance();
                    InsertAndFreePrologue(newStatements, prologue);

                    foreach (var s in node.Statements)
                    {
                        newStatements.Add((BoundStatement)this.Visit(s));
                    }

E
Evan Hauck 已提交
973
                    return new BoundBlock(node.Syntax, newLocals.ToImmutableAndFree(), ImmutableArray<LocalFunctionSymbol>.Empty, newStatements.ToImmutableAndFree(), node.HasErrors);
P
Pilchie 已提交
974 975 976 977 978 979 980 981 982 983 984 985
                });
            }
            else
            {
                return base.VisitStatementList(node);
            }
        }

        public override BoundNode VisitSwitchStatement(BoundSwitchStatement node)
        {
            LambdaFrame frame;
            // Test if this frame has captured variables and requires the introduction of a closure class.
986
            if (_frames.TryGetValue(node, out frame))
P
Pilchie 已提交
987 988 989 990 991 992 993
            {
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
                {
                    var newStatements = ArrayBuilder<BoundStatement>.GetInstance();
                    InsertAndFreePrologue(newStatements, prologue);
                    newStatements.Add((BoundStatement)base.VisitSwitchStatement(node));

E
Evan Hauck 已提交
994
                    return new BoundBlock(node.Syntax, newLocals.ToImmutableAndFree(), ImmutableArray<LocalFunctionSymbol>.Empty, newStatements.ToImmutableAndFree(), node.HasErrors);
P
Pilchie 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
                });
            }
            else
            {
                return base.VisitSwitchStatement(node);
            }
        }

        public override BoundNode VisitDelegateCreationExpression(BoundDelegateCreationExpression node)
        {
            // A delegate creation expression of the form "new Action( ()=>{} )" is treated exactly like
            // (Action)(()=>{})
            if (node.Argument.Kind == BoundKind.Lambda)
            {
                return RewriteLambdaConversion((BoundLambda)node.Argument);
            }
            else
            {
E
Evan Hauck 已提交
1013 1014 1015 1016 1017 1018 1019 1020
                if (node.MethodOpt?.MethodKind == MethodKind.LocalFunction)
                {
                    BoundExpression receiver;
                    MethodSymbol method;
                    RemapLocalFunction(node.Syntax, node.MethodOpt, out receiver, out method);
                    var result = new BoundDelegateCreationExpression(node.Syntax, receiver, method, isExtensionMethod: false, type: node.Type);
                    return result;
                }
P
Pilchie 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029
                return base.VisitDelegateCreationExpression(node);
            }
        }

        public override BoundNode VisitConversion(BoundConversion conversion)
        {
            if (conversion.ConversionKind == ConversionKind.AnonymousFunction)
            {
                var result = (BoundExpression)RewriteLambdaConversion((BoundLambda)conversion.Operand);
1030
                return _inExpressionLambda && conversion.ExplicitCastInCode
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
                    ? new BoundConversion(
                        syntax: conversion.Syntax,
                        operand: result,
                        conversionKind: conversion.ConversionKind,
                        resultKind: conversion.ResultKind,
                        isBaseConversion: false,
                        symbolOpt: null,
                        @checked: false,
                        explicitCastInCode: true,
                        isExtensionMethod: false,
                        isArrayIndex: false,
                        constantValueOpt: conversion.ConstantValueOpt,
                        type: conversion.Type)
P
Pilchie 已提交
1044 1045 1046 1047
                    : result;
            }
            else
            {
E
Evan Hauck 已提交
1048
                if (conversion.ConversionKind == ConversionKind.MethodGroup && conversion.SymbolOpt?.MethodKind == MethodKind.LocalFunction)
E
Evan Hauck 已提交
1049 1050 1051 1052 1053 1054 1055
                {
                    BoundExpression receiver;
                    MethodSymbol method;
                    RemapLocalFunction(conversion.Syntax, conversion.SymbolOpt, out receiver, out method);
                    var result = new BoundDelegateCreationExpression(conversion.Syntax, receiver, method, isExtensionMethod: false, type: conversion.Type);
                    return result;
                }
P
Pilchie 已提交
1056 1057 1058 1059
                return base.VisitConversion(conversion);
            }
        }

1060 1061
        public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatement node)
        {
E
Evan Hauck 已提交
1062
            ClosureKind closureKind;
1063 1064
            NamedTypeSymbol translatedLambdaContainer;
            LambdaFrame containerAsFrame;
E
Evan Hauck 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
            BoundNode lambdaScope;
            DebugId topLevelMethodId;
            DebugId lambdaId;
            RewriteLambdaOrLocalFunction(
                node,
                out closureKind,
                out translatedLambdaContainer,
                out containerAsFrame,
                out lambdaScope,
                out topLevelMethodId,
                out lambdaId);
1076 1077 1078 1079

            return new BoundNoOpStatement(node.Syntax, NoOpStatementFlavor.Default);
        }

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        private DebugId GetTopLevelMethodId()
        {
            return slotAllocatorOpt?.MethodId ?? new DebugId(_topLevelMethodOrdinal, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
        }

        private DebugId GetClosureId(SyntaxNode syntax, ArrayBuilder<ClosureDebugInfo> closureDebugInfo)
        {
            Debug.Assert(syntax != null);

            DebugId closureId;
            DebugId previousClosureId;
            if (slotAllocatorOpt != null && slotAllocatorOpt.TryGetPreviousClosure(syntax, out previousClosureId))
            {
                closureId = previousClosureId;
            }
            else
            {
                closureId = new DebugId(closureDebugInfo.Count, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
            }

            int syntaxOffset = _topLevelMethod.CalculateLocalSyntaxOffset(syntax.SpanStart, syntax.SyntaxTree);
T
Tomas Matousek 已提交
1101
            closureDebugInfo.Add(new ClosureDebugInfo(syntaxOffset, closureId));
1102 1103 1104 1105 1106

            return closureId;
        }

        private DebugId GetLambdaId(SyntaxNode syntax, ClosureKind closureKind, int closureOrdinal)
1107 1108 1109 1110 1111
        {
            Debug.Assert(syntax != null);

            SyntaxNode lambdaOrLambdaBodySyntax;
            var anonymousFunction = syntax as AnonymousFunctionExpressionSyntax;
1112
            var localFunction = syntax as LocalFunctionStatementSyntax;
1113 1114
            bool isLambdaBody;

1115 1116 1117
            if (anonymousFunction != null)
            {
                lambdaOrLambdaBodySyntax = anonymousFunction.Body;
1118
                isLambdaBody = true;
1119
            }
1120 1121 1122 1123 1124
            else if (localFunction != null)
            {
                lambdaOrLambdaBodySyntax = (SyntaxNode)localFunction.Body ?? localFunction.ExpressionBody;
                isLambdaBody = true;
            }
1125
            else if (LambdaUtilities.IsQueryPairLambda(syntax))
1126 1127 1128
            {
                // "pair" query lambdas
                lambdaOrLambdaBodySyntax = syntax;
1129
                isLambdaBody = false;
1130
                Debug.Assert(closureKind == ClosureKind.Singleton);
1131 1132 1133 1134 1135
            }
            else
            {
                // query lambdas
                lambdaOrLambdaBodySyntax = syntax;
1136
                isLambdaBody = true;
1137 1138
            }

1139
            Debug.Assert(!isLambdaBody || LambdaUtilities.IsLambdaBody(lambdaOrLambdaBodySyntax));
1140

1141
            // determine lambda ordinal and calculate syntax offset
1142

1143 1144 1145
            DebugId lambdaId;
            DebugId previousLambdaId;
            if (slotAllocatorOpt != null && slotAllocatorOpt.TryGetPreviousLambda(lambdaOrLambdaBodySyntax, isLambdaBody, out previousLambdaId))
1146
            {
1147
                lambdaId = previousLambdaId;
1148 1149 1150
            }
            else
            {
1151
                lambdaId = new DebugId(_lambdaDebugInfoBuilder.Count, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
1152
            }
1153 1154

            int syntaxOffset = _topLevelMethod.CalculateLocalSyntaxOffset(lambdaOrLambdaBodySyntax.SpanStart, lambdaOrLambdaBodySyntax.SyntaxTree);
T
Tomas Matousek 已提交
1155
            _lambdaDebugInfoBuilder.Add(new LambdaDebugInfo(syntaxOffset, lambdaId, closureOrdinal));
1156
            return lambdaId;
1157 1158
        }

E
Evan Hauck 已提交
1159 1160 1161 1162 1163 1164 1165 1166
        private SynthesizedLambdaMethod RewriteLambdaOrLocalFunction(
            IBoundLambdaOrFunction node,
            out ClosureKind closureKind,
            out NamedTypeSymbol translatedLambdaContainer,
            out LambdaFrame containerAsFrame,
            out BoundNode lambdaScope,
            out DebugId topLevelMethodId,
            out DebugId lambdaId)
P
Pilchie 已提交
1167
        {
1168
            int closureOrdinal;
E
Evan Hauck 已提交
1169
            if (_analysis.LambdaScopes.TryGetValue(node.Symbol, out lambdaScope))
P
Pilchie 已提交
1170
            {
1171
                translatedLambdaContainer = containerAsFrame = _frames[lambdaScope];
1172
                closureKind = ClosureKind.General;
1173
                closureOrdinal = containerAsFrame.ClosureOrdinal;
P
Pilchie 已提交
1174
            }
E
Evan Hauck 已提交
1175
            else if (_analysis.CapturedVariablesByLambda[node.Symbol].Count == 0)
1176
            {
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
                if (_analysis.MethodsConvertedToDelegates.Contains(node.Symbol))
                {
                    translatedLambdaContainer = containerAsFrame = GetStaticFrame(Diagnostics, node);
                    closureKind = ClosureKind.Singleton;
                    closureOrdinal = LambdaDebugInfo.StaticClosureOrdinal;
                }
                else
                {
                    containerAsFrame = null;
                    translatedLambdaContainer = _topLevelMethod.ContainingType;
                    closureKind = ClosureKind.Static;
                    closureOrdinal = LambdaDebugInfo.StaticClosureOrdinal;
                }
1190
            }
P
Pilchie 已提交
1191 1192
            else
            {
1193
                containerAsFrame = null;
1194
                translatedLambdaContainer = _topLevelMethod.ContainingType;
1195
                closureKind = ClosureKind.ThisOnly;
1196
                closureOrdinal = LambdaDebugInfo.ThisOnlyClosureOrdinal;
P
Pilchie 已提交
1197 1198 1199
            }

            // Move the body of the lambda to a freshly generated synthetic method on its frame.
E
Evan Hauck 已提交
1200 1201
            topLevelMethodId = GetTopLevelMethodId();
            lambdaId = GetLambdaId(node.Syntax, closureKind, closureOrdinal);
1202

1203
            var synthesizedMethod = new SynthesizedLambdaMethod(translatedLambdaContainer, closureKind, _topLevelMethod, topLevelMethodId, node, lambdaId);
1204
            CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(translatedLambdaContainer, synthesizedMethod);
P
Pilchie 已提交
1205

1206
            foreach (var parameter in node.Symbol.Parameters)
P
Pilchie 已提交
1207
            {
1208
                _parameterMap.Add(parameter, synthesizedMethod.Parameters[parameter.Ordinal]);
P
Pilchie 已提交
1209 1210
            }

E
Evan Hauck 已提交
1211 1212
            if (node is BoundLocalFunctionStatement)
            {
E
Evan Hauck 已提交
1213
                _localFunctionMap[((BoundLocalFunctionStatement)node).Symbol] = new MappedLocalFunction(synthesizedMethod, closureKind);
E
Evan Hauck 已提交
1214 1215
            }

P
Pilchie 已提交
1216
            // rewrite the lambda body as the generated method's body
1217 1218 1219 1220 1221 1222 1223 1224 1225
            var oldMethod = _currentMethod;
            var oldFrameThis = _currentFrameThis;
            var oldTypeParameters = _currentTypeParameters;
            var oldInnermostFramePointer = _innermostFramePointer;
            var oldTypeMap = _currentLambdaBodyTypeMap;
            var oldAddedStatements = _addedStatements;
            var oldAddedLocals = _addedLocals;
            _addedStatements = null;
            _addedLocals = null;
P
Pilchie 已提交
1226 1227 1228

            // switch to the generated method

1229
            _currentMethod = synthesizedMethod;
1230
            if (closureKind == ClosureKind.Static || closureKind == ClosureKind.Singleton)
P
Pilchie 已提交
1231 1232
            {
                // no link from a static lambda to its container
1233
                _innermostFramePointer = _currentFrameThis = null;
P
Pilchie 已提交
1234 1235 1236
            }
            else
            {
1237 1238 1239
                _currentFrameThis = synthesizedMethod.ThisParameter;
                _innermostFramePointer = null;
                _framePointers.TryGetValue(translatedLambdaContainer, out _innermostFramePointer);
P
Pilchie 已提交
1240 1241
            }

E
Evan Hauck 已提交
1242 1243
            _currentTypeParameters = translatedLambdaContainer?.TypeParameters.Concat(synthesizedMethod.TypeParameters) ?? synthesizedMethod.TypeParameters;
            _currentLambdaBodyTypeMap = synthesizedMethod.TypeMap;
P
Pilchie 已提交
1244 1245 1246

            var body = AddStatementsIfNeeded((BoundStatement)VisitBlock(node.Body));
            CheckLocalsDefined(body);
T
TomasMatousek 已提交
1247
            CompilationState.AddSynthesizedMethod(synthesizedMethod, body);
P
Pilchie 已提交
1248 1249 1250

            // return to the old method

1251 1252 1253 1254 1255 1256 1257
            _currentMethod = oldMethod;
            _currentFrameThis = oldFrameThis;
            _currentTypeParameters = oldTypeParameters;
            _innermostFramePointer = oldInnermostFramePointer;
            _currentLambdaBodyTypeMap = oldTypeMap;
            _addedLocals = oldAddedLocals;
            _addedStatements = oldAddedStatements;
P
Pilchie 已提交
1258

E
Evan Hauck 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            return synthesizedMethod;
        }

        private BoundNode RewriteLambdaConversion(BoundLambda node)
        {
            var wasInExpressionLambda = _inExpressionLambda;
            _inExpressionLambda = _inExpressionLambda || node.Type.IsExpressionTree();

            if (_inExpressionLambda)
            {
                var newType = VisitType(node.Type);
                var newBody = (BoundBlock)Visit(node.Body);
                node = node.Update(node.Symbol, newBody, node.Diagnostics, node.Binder, newType);
                var result0 = wasInExpressionLambda ? node : ExpressionLambdaRewriter.RewriteLambda(node, CompilationState, TypeMap, Diagnostics);
                _inExpressionLambda = wasInExpressionLambda;
                return result0;
            }

            ClosureKind closureKind;
            NamedTypeSymbol translatedLambdaContainer;
            LambdaFrame containerAsFrame;
            BoundNode lambdaScope;
            DebugId topLevelMethodId;
            DebugId lambdaId;
            SynthesizedLambdaMethod synthesizedMethod = RewriteLambdaOrLocalFunction(
                node,
                out closureKind,
                out translatedLambdaContainer,
                out containerAsFrame,
                out lambdaScope,
                out topLevelMethodId,
                out lambdaId);

E
Evan Hauck 已提交
1292
            MethodSymbol referencedMethod = synthesizedMethod;
1293
            BoundExpression receiver;
E
Evan Hauck 已提交
1294 1295
            NamedTypeSymbol constructedFrame;
            RemapLambdaOrLocalFunction(node.Syntax, node.Symbol, default(ImmutableArray<TypeSymbol>), closureKind, ref referencedMethod, out receiver, out constructedFrame);
1296

E
Evan Hauck 已提交
1297
            // Rewrite the lambda expression (and the enclosing anonymous method conversion) as a delegate creation expression
1298

P
Pilchie 已提交
1299
            TypeSymbol type = this.VisitType(node.Type);
1300

1301 1302 1303 1304
            // static lambdas are emitted as instance methods on a singleton receiver
            // delegates invoke dispatch is optimized for instance delegates so 
            // it is preferrable to emit lambdas as instance methods even when lambdas 
            // do not capture anything
P
Pilchie 已提交
1305 1306 1307 1308
            BoundExpression result = new BoundDelegateCreationExpression(
                node.Syntax,
                receiver,
                referencedMethod,
1309
                isExtensionMethod: false,
P
Pilchie 已提交
1310 1311 1312 1313 1314
                type: type);

            // if the block containing the lambda is not the innermost block,
            // or the lambda is static, then the lambda object should be cached in its frame.
            // NOTE: we are not caching static lambdas in static ctors - cannot reuse such cache.
1315
            var shouldCacheForStaticMethod = closureKind == ClosureKind.Singleton &&
1316
                _currentMethod.MethodKind != MethodKind.StaticConstructor &&
P
Pilchie 已提交
1317 1318 1319 1320 1321
                !referencedMethod.IsGenericMethod;

            // NOTE: We require "lambdaScope != null". 
            //       We do not want to introduce a field into an actual user's class (not a synthetic frame).
            var shouldCacheInLoop = lambdaScope != null &&
E
Evan Hauck 已提交
1322
                lambdaScope != _analysis.ScopeParent[node.Body] &&
P
Pilchie 已提交
1323 1324 1325 1326
                InLoopOrLambda(node.Syntax, lambdaScope.Syntax);

            if (shouldCacheForStaticMethod || shouldCacheInLoop)
            {
1327
                // replace the expression "new Delegate(frame.M)" with "frame.cache ?? (frame.cache = new Delegate(frame.M));
1328
                var F = new SyntheticBoundNodeFactory(_currentMethod, node.Syntax, CompilationState, Diagnostics);
P
Pilchie 已提交
1329 1330
                try
                {
T
TomasMatousek 已提交
1331
                    BoundExpression cache;
1332
                    if (shouldCacheForStaticMethod || shouldCacheInLoop && (object)containerAsFrame != null)
P
Pilchie 已提交
1333
                    {
1334 1335 1336
                        // Since the cache variable will be in a container with possibly alpha-rewritten generic parameters, we need to
                        // substitute the original type according to the type map for that container. That substituted type may be
                        // different from the local variable `type`, which has the node's type substituted for the current container.
1337
                        var cacheVariableType = containerAsFrame.TypeMap.SubstituteType(node.Type);
1338

1339
                        var cacheVariableName = GeneratedNames.MakeLambdaCacheFieldName(
1340 1341
                            // If we are generating the field into a display class created exclusively for the lambda the lambdaOrdinal itself is unique already, 
                            // no need to include the top-level method ordinal in the field name.
1342 1343
                            (closureKind == ClosureKind.General) ? -1 : topLevelMethodId.Ordinal,
                            topLevelMethodId.Generation,
1344 1345
                            lambdaId.Ordinal,
                            lambdaId.Generation);
1346

1347
                        var cacheField = new SynthesizedLambdaCacheFieldSymbol(translatedLambdaContainer, cacheVariableType, cacheVariableName, _topLevelMethod, isReadOnly: false, isStatic: closureKind == ClosureKind.Singleton);
T
TomasMatousek 已提交
1348
                        CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(translatedLambdaContainer, cacheField);
T
TomasMatousek 已提交
1349
                        cache = F.Field(receiver, cacheField.AsMember(constructedFrame)); //NOTE: the field was added to the unconstructed frame type.
P
Pilchie 已提交
1350 1351 1352 1353
                    }
                    else
                    {
                        // the lambda captures at most the "this" of the enclosing method.  We cache its delegate in a local variable.
1354
                        var cacheLocal = F.SynthesizedLocal(type, kind: SynthesizedLocalKind.CachedAnonymousMethodDelegate);
1355 1356 1357
                        if (_addedLocals == null) _addedLocals = ArrayBuilder<LocalSymbol>.GetInstance();
                        _addedLocals.Add(cacheLocal);
                        if (_addedStatements == null) _addedStatements = ArrayBuilder<BoundStatement>.GetInstance();
T
TomasMatousek 已提交
1358
                        cache = F.Local(cacheLocal);
1359
                        _addedStatements.Add(F.Assignment(cache, F.Null(type)));
P
Pilchie 已提交
1360 1361
                    }

T
TomasMatousek 已提交
1362
                    result = F.Coalesce(cache, F.AssignmentExpression(cache, result));
P
Pilchie 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
                }
                catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
                {
                    Diagnostics.Add(ex.Diagnostic);
                    return new BoundBadExpression(F.Syntax, LookupResultKind.Empty, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundNode>(node), node.Type);
                }
            }

            return result;
        }

        // This helper checks syntactically whether there is a loop or lambda expression
        // between given lambda syntax and the syntax that corresponds to its closure.
        // we use this heuristic as a hint that the lambda delegate may be created 
        // multiple times with same closure.
        // In such cases it makes sense to cache the delegate.
        //
        // Examples:
        //            int x = 123;
        //            for (int i = 1; i< 10; i++)
        //            {
        //                if (i< 2)
        //                {
        //                    arr[i].Execute(arg => arg + x);  // delegate should be cached
        //                }
        //            }

        //            for (int i = 1; i< 10; i++)
        //            {
        //                var val = i;
        //                if (i< 2)
        //                {
        //                    int y = i + i;
        //                    System.Console.WriteLine(y);
        //                    arr[i].Execute(arg => arg + val);  // delegate should NOT be cached (closure created inside the loop)
        //                }
        //            }
        //
        private static bool InLoopOrLambda(SyntaxNode lambdaSyntax, SyntaxNode scopeSyntax)
        {
            var curSyntax = lambdaSyntax.Parent;
            while (curSyntax != null && curSyntax != scopeSyntax)
            {
1406
                switch (curSyntax.Kind())
P
Pilchie 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
                {
                    case SyntaxKind.ForStatement:
                    case SyntaxKind.ForEachStatement:
                    case SyntaxKind.WhileStatement:
                    case SyntaxKind.DoStatement:
                    case SyntaxKind.SimpleLambdaExpression:
                    case SyntaxKind.ParenthesizedLambdaExpression:
                        return true;
                }

                curSyntax = curSyntax.Parent;
            }

            return false;
        }

        public override BoundNode VisitLambda(BoundLambda node)
        {
            // these nodes have been handled in the context of the enclosing anonymous method conversion.
            throw ExceptionUtilities.Unreachable;
        }

E
Evan Hauck 已提交
1429
        #endregion
P
Pilchie 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

#if CHECK_LOCALS
        /// <summary>
        /// Ensure that local variables are always in scope where used in bound trees
        /// </summary>
        /// <param name="node"></param>
        static partial void CheckLocalsDefined(BoundNode node)
        {
            LocalsDefinedScanner.INSTANCE.Visit(node);
        }

        class LocalsDefinedScanner : BoundTreeWalker
        {
            internal static LocalsDefinedScanner INSTANCE = new LocalsDefinedScanner();

            HashSet<Symbol> localsDefined = new HashSet<Symbol>();

            public override BoundNode VisitLocal(BoundLocal node)
            {
                Debug.Assert(node.LocalSymbol.IsConst || localsDefined.Contains(node.LocalSymbol));
                return base.VisitLocal(node);
            }

            public override BoundNode VisitSequence(BoundSequence node)
            {
                try
                {
                    if (!node.Locals.IsNullOrEmpty)
                        foreach (var l in node.Locals)
                            localsDefined.Add(l);
                    return base.VisitSequence(node);
                }
                finally
                {
                    if (!node.Locals.IsNullOrEmpty)
                        foreach (var l in node.Locals)
                            localsDefined.Remove(l);
                }
            }

            public override BoundNode VisitCatchBlock(BoundCatchBlock node)
            {
                try
                {
                    if ((object)node.LocalOpt != null) localsDefined.Add(node.LocalOpt);
                    return base.VisitCatchBlock(node);
                }
                finally
                {
                    if ((object)node.LocalOpt != null) localsDefined.Remove(node.LocalOpt);
                }
            }

            public override BoundNode VisitSwitchStatement(BoundSwitchStatement node)
            {
                try
                {
                    if (!node.LocalsOpt.IsNullOrEmpty)
                        foreach (var l in node.LocalsOpt)
                            localsDefined.Add(l);
                    return base.VisitSwitchStatement(node);
                }
                finally
                {
                    if (!node.LocalsOpt.IsNullOrEmpty)
                        foreach (var l in node.LocalsOpt)
                            localsDefined.Remove(l);
                }
            }

            public override BoundNode VisitBlock(BoundBlock node)
            {
                try
                {
                    if (!node.LocalsOpt.IsNullOrEmpty)
                        foreach (var l in node.LocalsOpt)
                            localsDefined.Add(l);
                    return base.VisitBlock(node);
                }
                finally
                {
                    if (!node.LocalsOpt.IsNullOrEmpty)
                        foreach (var l in node.LocalsOpt)
                            localsDefined.Remove(l);
                }
            }
        }
#endif
    }
}