LambdaRewriter.cs 78.6 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
A
Andy Gocke 已提交
7

P
Pilchie 已提交
8 9 10 11
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
12
using Microsoft.CodeAnalysis.CodeGen;
P
Pilchie 已提交
13
using Microsoft.CodeAnalysis.CSharp.Symbols;
14
using Microsoft.CodeAnalysis.CSharp.Syntax;
T
Tomas Matousek 已提交
15
using Microsoft.CodeAnalysis.PooledObjects;
P
Pilchie 已提交
16 17 18 19 20 21 22 23 24
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 已提交
25
    /// The entry point is the public method <see cref="Rewrite"/>.  It operates as follows:
P
Pilchie 已提交
26 27 28
    /// 
    /// 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
29
    /// have captured variables.  The result of this analysis is left in <see cref="_analysis"/>.
P
Pilchie 已提交
30 31
    /// 
    /// Then we make a frame, or compiler-generated class, represented by an instance of
32
    /// <see cref="SynthesizedClosureEnvironment"/> for each scope with captured variables.  The generated frames are kept
33
    /// in <see cref="_frames"/>.  Each frame is given a single field for each captured
34
    /// variable in the corresponding scope.  These are maintained in <see cref="MethodToClassRewriter.proxies"/>.
P
Pilchie 已提交
35
    /// 
36
    /// Next, we walk and rewrite the input bound tree, keeping track of the following:
37 38 39
    /// (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 已提交
40
    /// (4) The symbol that is used to access the innermost frame pointer (it could be a local variable or "this" parameter)
41 42 43 44 45 46 47
    ///
    /// Lastly, we visit the top-level method and each of the lowered methods
    /// to rewrite references (e.g., calls and delegate conversions) to local
    /// functions. We visit references to local functions separately from
    /// lambdas because we may see the reference before we lower the target
    /// local function. Lambdas, on the other hand, are always convertible as
    /// they are being lowered.
P
Pilchie 已提交
48 49 50 51 52 53 54
    /// 
    /// 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.
    /// 
55 56
    /// In addition, the rewriting deposits into <see cref="TypeCompilationState.SynthesizedMethods"/>
    /// a (<see cref="MethodSymbol"/>, <see cref="BoundStatement"/>) pair for each generated method.
P
Pilchie 已提交
57
    /// 
T
TomasMatousek 已提交
58
    /// <see cref="Rewrite"/> produces its output in two forms.  First, it returns a new bound statement
P
Pilchie 已提交
59
    /// for the caller to use for the body of the original method.  Second, it returns a collection of
T
TomasMatousek 已提交
60
    /// (<see cref="MethodSymbol"/>, <see cref="BoundStatement"/>) pairs for additional methods that the lambda rewriter produced.
P
Pilchie 已提交
61 62 63 64 65
    /// 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>
66
    internal sealed partial class LambdaRewriter : MethodToClassRewriter
P
Pilchie 已提交
67
    {
68 69
        private readonly Analysis _analysis;
        private readonly MethodSymbol _topLevelMethod;
70
        private readonly MethodSymbol _substitutedSourceMethod;
71
        private readonly int _topLevelMethodOrdinal;
P
Pilchie 已提交
72

73 74
        // lambda frame for static lambdas. 
        // initialized lazily and could be null if there are no static lambdas
75
        private SynthesizedClosureEnvironment _lazyStaticLambdaFrame;
76

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

P
Pilchie 已提交
80
        // for each block with lifted (captured) variables, the corresponding frame type
81
        private readonly Dictionary<BoundNode, Analysis.ClosureEnvironment> _frames = new Dictionary<BoundNode, Analysis.ClosureEnvironment>();
P
Pilchie 已提交
82 83 84

        // 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.
85
        private readonly Dictionary<NamedTypeSymbol, Symbol> _framePointers = new Dictionary<NamedTypeSymbol, Symbol>();
P
Pilchie 已提交
86

87 88 89 90
        // The set of original locals that should be assigned to proxies
        // if lifted. This is useful for the expression evaluator where
        // the original locals are left as is.
        private readonly HashSet<LocalSymbol> _assignLocals;
91

P
Pilchie 已提交
92
        // The current method or lambda being processed.
93
        private MethodSymbol _currentMethod;
P
Pilchie 已提交
94 95

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

98
        private readonly ArrayBuilder<LambdaDebugInfo> _lambdaDebugInfoBuilder;
99

100
        // ID dispenser for field names of frame references
101
        private int _synthesizedFieldNameIdDispenser;
102

P
Pilchie 已提交
103
        // The symbol (field or local) holding the innermost frame
104
        private Symbol _innermostFramePointer;
P
Pilchie 已提交
105 106

        // The mapping of type parameters for the current lambda body
107
        private TypeMap _currentLambdaBodyTypeMap;
P
Pilchie 已提交
108 109

        // The current set of type parameters (mapped from the enclosing method's type parameters)
110
        private ImmutableArray<TypeParameterSymbol> _currentTypeParameters;
P
Pilchie 已提交
111 112 113

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

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

        // Set to true while translating code inside of an expression lambda.
120
        private bool _inExpressionLambda;
P
Pilchie 已提交
121 122 123 124

        // 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.
125
        private ArrayBuilder<LocalSymbol> _addedLocals;
P
Pilchie 已提交
126 127 128

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

131 132 133 134 135 136
        /// <summary>
        /// Temporary bag for methods synthesized by the rewriting. Added to
        /// <see cref="TypeCompilationState.SynthesizedMethods"/> at the end of rewriting.
        /// </summary>
        private ArrayBuilder<TypeCompilationState.MethodWithBody> _synthesizedMethods;

A
Andy Gocke 已提交
137 138 139 140 141 142 143 144 145
        /// <summary>
        /// TODO(https://github.com/dotnet/roslyn/projects/26): Delete this.
        /// This should only be used by <see cref="NeedsProxy(Symbol)"/> which
        /// hasn't had logic to move the proxy analysis into <see cref="Analysis"/>,
        /// where the <see cref="Analysis.ScopeTree"/> could be walked to build
        /// the proxy list.
        /// </summary>
        private readonly ImmutableHashSet<Symbol> _allCapturedVariables;

P
Pilchie 已提交
146 147 148
        private LambdaRewriter(
            Analysis analysis,
            NamedTypeSymbol thisType,
T
TomasMatousek 已提交
149
            ParameterSymbol thisParameterOpt,
P
Pilchie 已提交
150
            MethodSymbol method,
151
            int methodOrdinal,
152
            MethodSymbol substitutedSourceMethod,
153
            ArrayBuilder<LambdaDebugInfo> lambdaDebugInfoBuilder,
154
            VariableSlotAllocator slotAllocatorOpt,
P
Pilchie 已提交
155 156
            TypeCompilationState compilationState,
            DiagnosticBag diagnostics,
157
            HashSet<LocalSymbol> assignLocals)
158
            : base(slotAllocatorOpt, compilationState, diagnostics)
P
Pilchie 已提交
159
        {
T
TomasMatousek 已提交
160
            Debug.Assert(analysis != null);
161
            Debug.Assert((object)thisType != null);
T
TomasMatousek 已提交
162 163 164 165
            Debug.Assert(method != null);
            Debug.Assert(compilationState != null);
            Debug.Assert(diagnostics != null);

166
            _topLevelMethod = method;
167
            _substitutedSourceMethod = substitutedSourceMethod;
168 169 170 171 172 173 174
            _topLevelMethodOrdinal = methodOrdinal;
            _lambdaDebugInfoBuilder = lambdaDebugInfoBuilder;
            _currentMethod = method;
            _analysis = analysis;
            _assignLocals = assignLocals;
            _currentTypeParameters = method.TypeParameters;
            _currentLambdaBodyTypeMap = TypeMap.Empty;
175
            _innermostFramePointer = _currentFrameThis = thisParameterOpt;
176 177 178
            _framePointers[thisType] = thisParameterOpt;
            _seenBaseCall = method.MethodKind != MethodKind.Constructor; // only used for ctors
            _synthesizedFieldNameIdDispenser = 1;
A
Andy Gocke 已提交
179 180 181 182 183 184 185

            var allCapturedVars = ImmutableHashSet.CreateBuilder<Symbol>();
            Analysis.VisitClosures(analysis.ScopeTree, (scope, closure) =>
            {
                allCapturedVars.UnionWith(closure.CapturedVariables);
            });
            _allCapturedVariables = allCapturedVars.ToImmutable();
P
Pilchie 已提交
186 187
        }

188 189
        protected override bool NeedsProxy(Symbol localOrParameter)
        {
E
Evan Hauck 已提交
190 191
            Debug.Assert(localOrParameter is LocalSymbol || localOrParameter is ParameterSymbol ||
                (localOrParameter as MethodSymbol)?.MethodKind == MethodKind.LocalFunction);
A
Andy Gocke 已提交
192
            return _allCapturedVariables.Contains(localOrParameter);
193 194
        }

P
Pilchie 已提交
195 196 197 198 199 200
        /// <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>
201
        /// <param name="loweredBody">The bound node to be rewritten</param>
P
Pilchie 已提交
202 203 204
        /// <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>
205
        /// <param name="methodOrdinal">Index of the method symbol in its containing type member list.</param>
206
        /// <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>
207 208
        /// <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>
209
        /// <param name="slotAllocatorOpt">Slot allocator.</param>
P
Pilchie 已提交
210 211
        /// <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>
212
        /// <param name="assignLocals">The set of original locals that should be assigned to proxies if lifted</param>
P
Pilchie 已提交
213
        public static BoundStatement Rewrite(
214
            BoundStatement loweredBody,
P
Pilchie 已提交
215 216 217
            NamedTypeSymbol thisType,
            ParameterSymbol thisParameter,
            MethodSymbol method,
218
            int methodOrdinal,
219
            MethodSymbol substitutedSourceMethod,
220 221
            ArrayBuilder<LambdaDebugInfo> lambdaDebugInfoBuilder,
            ArrayBuilder<ClosureDebugInfo> closureDebugInfoBuilder,
222
            VariableSlotAllocator slotAllocatorOpt,
P
Pilchie 已提交
223 224
            TypeCompilationState compilationState,
            DiagnosticBag diagnostics,
225
            HashSet<LocalSymbol> assignLocals)
P
Pilchie 已提交
226 227
        {
            Debug.Assert((object)thisType != null);
228
            Debug.Assert(((object)thisParameter == null) || (TypeSymbol.Equals(thisParameter.Type, thisType, TypeCompareKind.ConsiderEverything2)));
229 230
            Debug.Assert(compilationState.ModuleBuilderOpt != null);

A
Andy Gocke 已提交
231 232 233 234 235 236 237 238 239
            var analysis = Analysis.Analyze(
                loweredBody,
                method,
                methodOrdinal,
                substitutedSourceMethod,
                slotAllocatorOpt,
                compilationState,
                closureDebugInfoBuilder,
                diagnostics);
P
Pilchie 已提交
240

241
            CheckLocalsDefined(loweredBody);
242 243 244 245 246
            var rewriter = new LambdaRewriter(
                analysis,
                thisType,
                thisParameter,
                method,
247
                methodOrdinal,
248
                substitutedSourceMethod,
249
                lambdaDebugInfoBuilder,
250
                slotAllocatorOpt,
251 252
                compilationState,
                diagnostics,
253
                assignLocals);
254

255
            rewriter.SynthesizeClosureEnvironments(closureDebugInfoBuilder);
256
            rewriter.SynthesizeLoweredFunctionMethods();
257 258 259 260

            var body = rewriter.AddStatementsIfNeeded(
                (BoundStatement)rewriter.Visit(loweredBody));

261 262 263 264 265 266 267 268 269 270 271 272 273 274
            // Add the completed methods to the compilation state
            if (rewriter._synthesizedMethods != null)
            {
                if (compilationState.SynthesizedMethods == null)
                {
                    compilationState.SynthesizedMethods = rewriter._synthesizedMethods;
                }
                else
                {
                    compilationState.SynthesizedMethods.AddRange(rewriter._synthesizedMethods);
                    rewriter._synthesizedMethods.Free();
                }
            }

P
Pilchie 已提交
275
            CheckLocalsDefined(body);
276

277 278
            analysis.Free();

P
Pilchie 已提交
279 280 281
            return body;
        }

282
        private BoundStatement AddStatementsIfNeeded(BoundStatement body)
P
Pilchie 已提交
283
        {
284
            if (_addedLocals != null)
P
Pilchie 已提交
285
            {
286
                _addedStatements.Add(body);
287
                body = new BoundBlock(body.Syntax, _addedLocals.ToImmutableAndFree(), _addedStatements.ToImmutableAndFree()) { WasCompilerGenerated = true };
288 289
                _addedLocals = null;
                _addedStatements = null;
P
Pilchie 已提交
290 291 292
            }
            else
            {
293
                Debug.Assert(_addedStatements == null);
P
Pilchie 已提交
294 295 296 297 298 299 300
            }

            return body;
        }

        protected override TypeMap TypeMap
        {
301
            get { return _currentLambdaBodyTypeMap; }
P
Pilchie 已提交
302 303 304 305
        }

        protected override MethodSymbol CurrentMethod
        {
306
            get { return _currentMethod; }
P
Pilchie 已提交
307 308 309 310
        }

        protected override NamedTypeSymbol ContainingType
        {
311
            get { return _topLevelMethod.ContainingType; }
P
Pilchie 已提交
312 313 314 315 316 317 318 319 320
        }

        /// <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>
321
        /// Adds <see cref="SynthesizedClosureEnvironment"/> synthesized types to the compilation state
A
Andy Gocke 已提交
322
        /// and creates hoisted fields for all locals captured by the environments.
P
Pilchie 已提交
323
        /// </summary>
324
        private void SynthesizeClosureEnvironments(ArrayBuilder<ClosureDebugInfo> closureDebugInfo)
P
Pilchie 已提交
325
        {
A
Andy Gocke 已提交
326
            Analysis.VisitScopeTree(_analysis.ScopeTree, scope =>
P
Pilchie 已提交
327
            {
A
Andy Gocke 已提交
328
                if (scope.DeclaredEnvironments.Count > 0)
P
Pilchie 已提交
329
                {
A
Andy Gocke 已提交
330 331 332 333
                    Debug.Assert(!_frames.ContainsKey(scope.BoundNode));
                    // At the moment, all variables declared in the same
                    // scope always get assigned to the same environment
                    Debug.Assert(scope.DeclaredEnvironments.Count == 1);
P
Pilchie 已提交
334

A
Andy Gocke 已提交
335
                    var env = scope.DeclaredEnvironments[0];
336
                    var frame = MakeFrame(scope, env);
337
                    env.SynthesizedEnvironment = frame;
338

339 340
                    CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(ContainingType, frame);
                    if (frame.Constructor != null)
341
                    {
A
Andy Gocke 已提交
342
                        AddSynthesizedMethod(
343
                            frame.Constructor,
A
Andy Gocke 已提交
344
                            FlowAnalysisPass.AppendImplicitReturn(
345 346
                                MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
                                frame.Constructor));
347 348
                    }

A
Andy Gocke 已提交
349
                    _frames.Add(scope.BoundNode, env);
350
                }
A
Andy Gocke 已提交
351
            });
352

353
            SynthesizedClosureEnvironment MakeFrame(Analysis.Scope scope, Analysis.ClosureEnvironment env)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
            {
                var scopeBoundNode = scope.BoundNode;

                var syntax = scopeBoundNode.Syntax;
                Debug.Assert(syntax != null);

                DebugId methodId = _analysis.GetTopLevelMethodId();
                DebugId closureId = _analysis.GetClosureId(syntax, closureDebugInfo);

                var containingMethod = scope.ContainingClosureOpt?.OriginalMethodSymbol ?? _topLevelMethod;
                if ((object)_substitutedSourceMethod != null && containingMethod == _topLevelMethod)
                {
                    containingMethod = _substitutedSourceMethod;
                }

369
                var synthesizedEnv = new SynthesizedClosureEnvironment(
370 371
                    _topLevelMethod,
                    containingMethod,
372
                    env.IsStruct,
373 374 375
                    syntax,
                    methodId,
                    closureId);
376 377 378 379 380 381 382 383 384 385 386 387

                foreach (var captured in env.CapturedVariables)
                {
                    Debug.Assert(!proxies.ContainsKey(captured));

                    var hoistedField = LambdaCapturedVariable.Create(synthesizedEnv, captured, ref _synthesizedFieldNameIdDispenser);
                    proxies.Add(captured, new CapturedToFrameSymbolReplacement(hoistedField, isReusable: false));
                    synthesizedEnv.AddHoistedField(hoistedField);
                    CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(synthesizedEnv, hoistedField);
                }

                return synthesizedEnv;
388
            }
389 390
        }

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        /// <summary>
        /// Synthesize the final signature for all closures.
        /// </summary>
        private void SynthesizeLoweredFunctionMethods()
        {
            Analysis.VisitClosures(_analysis.ScopeTree, (scope, closure) =>
            {
                var originalMethod = closure.OriginalMethodSymbol;
                var syntax = originalMethod.DeclaringSyntaxReferences[0].GetSyntax();

                int closureOrdinal;
                ClosureKind closureKind;
                NamedTypeSymbol translatedLambdaContainer;
                SynthesizedClosureEnvironment containerAsFrame;
                DebugId topLevelMethodId;
                DebugId lambdaId;
A
Andy Gocke 已提交
407

408 409
                if (closure.ContainingEnvironmentOpt != null)
                {
C
Charles Stoner 已提交
410
                    containerAsFrame = closure.ContainingEnvironmentOpt.SynthesizedEnvironment;
411 412 413 414 415 416 417 418 419 420 421 422

                    closureKind = ClosureKind.General;
                    translatedLambdaContainer = containerAsFrame;
                    closureOrdinal = containerAsFrame.ClosureOrdinal;
                }
                else if (closure.CapturesThis)
                {
                    containerAsFrame = null;
                    translatedLambdaContainer = _topLevelMethod.ContainingType;
                    closureKind = ClosureKind.ThisOnly;
                    closureOrdinal = LambdaDebugInfo.ThisOnlyClosureOrdinal;
                }
A
Andy Gocke 已提交
423 424
                else if (closure.CapturedEnvironments.Count == 0 &&
                         _analysis.MethodsConvertedToDelegates.Contains(originalMethod))
425
                {
A
Andy Gocke 已提交
426 427 428
                    translatedLambdaContainer = containerAsFrame = GetStaticFrame(Diagnostics, syntax);
                    closureKind = ClosureKind.Singleton;
                    closureOrdinal = LambdaDebugInfo.StaticClosureOrdinal;
429 430 431 432 433
                }
                else
                {
                    // Lower directly onto the containing type
                    translatedLambdaContainer = _topLevelMethod.ContainingType;
A
Andy Gocke 已提交
434 435
                    containerAsFrame = null;
                    closureKind = ClosureKind.Static;
436 437 438 439 440 441 442 443 444
                    closureOrdinal = LambdaDebugInfo.StaticClosureOrdinal;
                }

                // Move the body of the lambda to a freshly generated synthetic method on its frame.
                topLevelMethodId = _analysis.GetTopLevelMethodId();
                lambdaId = GetLambdaId(syntax, closureKind, closureOrdinal);

                var synthesizedMethod = new SynthesizedClosureMethod(
                    translatedLambdaContainer,
A
Andy Gocke 已提交
445
                    GetStructClosures(closure),
446 447 448 449 450
                    closureKind,
                    _topLevelMethod,
                    topLevelMethodId,
                    originalMethod,
                    closure.BlockSyntax,
451 452
                    lambdaId,
                    Diagnostics);
453 454
                closure.SynthesizedLoweredMethod = synthesizedMethod;
            });
A
Andy Gocke 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

            ImmutableArray<SynthesizedClosureEnvironment> GetStructClosures(Analysis.Closure closure)
            {
                var closuresBuilder = ArrayBuilder<SynthesizedClosureEnvironment>.GetInstance();

                foreach (var env in closure.CapturedEnvironments)
                {
                    if (env.IsStruct)
                    {
                        closuresBuilder.Add(env.SynthesizedEnvironment);
                    }
                }

                return closuresBuilder.ToImmutableAndFree();
            }
470 471 472 473 474 475 476 477 478 479 480 481
        }

        /// <summary>
        /// Get the static container for closures or create one if one doesn't already exist.
        /// </summary>
        /// <param name="syntax">
        /// associate the frame with the first lambda that caused it to exist. 
        /// we need to associate this with some syntax.
        /// unfortunately either containing method or containing class could be synthetic
        /// therefore could have no syntax.
        /// </param>
        private SynthesizedClosureEnvironment GetStaticFrame(DiagnosticBag diagnostics, SyntaxNode syntax)
482
        {
483
            if ((object)_lazyStaticLambdaFrame == null)
484
            {
485
                var isNonGeneric = !_topLevelMethod.IsGenericMethod;
486 487
                if (isNonGeneric)
                {
488
                    _lazyStaticLambdaFrame = CompilationState.StaticLambdaFrame;
489 490
                }

491
                if ((object)_lazyStaticLambdaFrame == null)
492
                {
493
                    DebugId methodId;
494 495
                    if (isNonGeneric)
                    {
496
                        methodId = new DebugId(DebugId.UndefinedOrdinal, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
497 498 499
                    }
                    else
                    {
A
Andy Gocke 已提交
500
                        methodId = _analysis.GetTopLevelMethodId();
501
                    }
502

E
Evan Hauck 已提交
503
                    DebugId closureId = default(DebugId);
E
Evan Hauck 已提交
504
                    // using _topLevelMethod as containing member because the static frame does not have generic parameters, except for the top level method's
505
                    var containingMethod = isNonGeneric ? null : (_substitutedSourceMethod ?? _topLevelMethod);
506
                    _lazyStaticLambdaFrame = new SynthesizedClosureEnvironment(
A
Andy Gocke 已提交
507 508 509 510 511 512
                        _topLevelMethod,
                        containingMethod,
                        isStruct: false,
                        scopeSyntaxOpt: null,
                        methodId: methodId,
                        closureId: closureId);
513

514
                    // non-generic static lambdas can share the frame
515
                    if (isNonGeneric)
516
                    {
517
                        CompilationState.StaticLambdaFrame = _lazyStaticLambdaFrame;
518 519
                    }

520
                    var frame = _lazyStaticLambdaFrame;
521

522
                    // add frame type and cache field
523 524
                    CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(this.ContainingType, frame);

525
                    // add its ctor (note Constructor can be null if TypeKind.Struct is passed in to LambdaFrame.ctor, but Class is passed in above)
526
                    AddSynthesizedMethod(
527
                        frame.Constructor,
528 529 530
                        FlowAnalysisPass.AppendImplicitReturn(
                            MethodCompiler.BindMethodBody(frame.Constructor, CompilationState, null),
                            frame.Constructor));
531 532 533 534 535 536 537 538

                    // 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)),
539
                            new BoundReturnStatement(syntax, RefKind.None, null));
540

541
                    AddSynthesizedMethod(frame.StaticConstructor, body);
542 543 544
                }
            }

545
            return _lazyStaticLambdaFrame;
546 547
        }

P
Pilchie 已提交
548 549 550 551 552 553
        /// <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>
554
        private BoundExpression FrameOfType(SyntaxNode syntax, NamedTypeSymbol frameType)
P
Pilchie 已提交
555 556
        {
            BoundExpression result = FramePointer(syntax, frameType.OriginalDefinition);
557
            Debug.Assert(TypeSymbol.Equals(result.Type, frameType, TypeCompareKind.ConsiderEverything2));
P
Pilchie 已提交
558 559 560 561 562 563 564 565 566 567 568
            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>
569
        protected override BoundExpression FramePointer(SyntaxNode syntax, NamedTypeSymbol frameClass)
P
Pilchie 已提交
570 571 572 573
        {
            Debug.Assert(frameClass.IsDefinition);

            // If in an instance method of the right type, we can just return the "this" pointer.
574
            if ((object)_currentFrameThis != null && TypeSymbol.Equals(_currentFrameThis.Type, frameClass, TypeCompareKind.ConsiderEverything2))
P
Pilchie 已提交
575 576 577 578
            {
                return new BoundThisReference(syntax, frameClass);
            }

579
            // If the current method has by-ref struct closure parameters, and one of them is correct, use it.
580
            var lambda = _currentMethod as SynthesizedClosureMethod;
581 582 583 584 585 586
            if (lambda != null)
            {
                var start = lambda.ParameterCount - lambda.ExtraSynthesizedParameterCount;
                for (var i = start; i < lambda.ParameterCount; i++)
                {
                    var potentialParameter = lambda.Parameters[i];
587
                    if (TypeSymbol.Equals(potentialParameter.Type.OriginalDefinition, frameClass, TypeCompareKind.ConsiderEverything2))
588 589 590 591 592 593
                    {
                        return new BoundParameter(syntax, potentialParameter);
                    }
                }
            }

P
Pilchie 已提交
594
            // Otherwise we need to return the value from a frame pointer local variable...
595
            Symbol framePointer = _framePointers[frameClass];
P
Pilchie 已提交
596 597 598 599 600 601 602 603 604
            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));
            }

605
            var localFrame = (LocalSymbol)framePointer;
606
            return new BoundLocal(syntax, localFrame, null, localFrame.Type);
P
Pilchie 已提交
607 608
        }

609
        private static void InsertAndFreePrologue<T>(ArrayBuilder<BoundStatement> result, ArrayBuilder<T> prologue) where T : BoundNode
P
Pilchie 已提交
610
        {
611
            foreach (var node in prologue)
P
Pilchie 已提交
612
            {
613 614 615 616 617 618 619 620
                if (node is BoundStatement stmt)
                {
                    result.Add(stmt);
                }
                else
                {
                    result.Add(new BoundExpressionStatement(node.Syntax, (BoundExpression)(BoundNode)node));
                }
P
Pilchie 已提交
621 622 623 624 625 626 627 628 629
            }

            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>
630
        /// <param name="env">The environment for the translated node</param>
P
Pilchie 已提交
631 632
        /// <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>
633
        private BoundNode IntroduceFrame(BoundNode node, Analysis.ClosureEnvironment env, Func<ArrayBuilder<BoundExpression>, ArrayBuilder<LocalSymbol>, BoundNode> F)
P
Pilchie 已提交
634
        {
635
            var frame = env.SynthesizedEnvironment;
636
            var frameTypeParameters = ImmutableArray.Create(_currentTypeParameters.SelectAsArray(t => TypeWithAnnotations.Create(t)), 0, frame.Arity);
E
Evan Hauck 已提交
637
            NamedTypeSymbol frameType = frame.ConstructIfGeneric(frameTypeParameters);
638 639

            Debug.Assert(frame.ScopeSyntaxOpt != null);
640
            LocalSymbol framePointer = new SynthesizedLocal(_topLevelMethod, TypeWithAnnotations.Create(frameType), SynthesizedLocalKind.LambdaDisplayClass, frame.ScopeSyntaxOpt);
P
Pilchie 已提交
641

642
            SyntaxNode syntax = node.Syntax;
P
Pilchie 已提交
643 644 645

            // assign new frame to the frame variable

646
            var prologue = ArrayBuilder<BoundExpression>.GetInstance();
P
Pilchie 已提交
647

A
Andy Gocke 已提交
648
            if ((object)frame.Constructor != null)
649
            {
650
                MethodSymbol constructor = frame.Constructor.AsMember(frameType);
651
                Debug.Assert(TypeSymbol.Equals(frameType, constructor.ContainingType, TypeCompareKind.ConsiderEverything2));
P
Pilchie 已提交
652

V
vsadov 已提交
653 654
                prologue.Add(new BoundAssignmentOperator(syntax,
                    new BoundLocal(syntax, framePointer, null, frameType),
G
Gen Lu 已提交
655
                    new BoundObjectCreationExpression(syntax: syntax, constructor: constructor, binderOpt: null),
V
vsadov 已提交
656 657
                    frameType));
            }
P
Pilchie 已提交
658 659

            CapturedSymbolReplacement oldInnermostFrameProxy = null;
660
            if ((object)_innermostFramePointer != null)
P
Pilchie 已提交
661
            {
662
                proxies.TryGetValue(_innermostFramePointer, out oldInnermostFrameProxy);
663
                if (env.CapturesParent)
P
Pilchie 已提交
664
                {
665
                    var capturedFrame = LambdaCapturedVariable.Create(frame, _innermostFramePointer, ref _synthesizedFieldNameIdDispenser);
P
Pilchie 已提交
666 667
                    FieldSymbol frameParent = capturedFrame.AsMember(frameType);
                    BoundExpression left = new BoundFieldAccess(syntax, new BoundLocal(syntax, framePointer, null, frameType), frameParent, null);
668
                    BoundExpression right = FrameOfType(syntax, frameParent.Type as NamedTypeSymbol);
P
Pilchie 已提交
669
                    BoundExpression assignment = new BoundAssignmentOperator(syntax, left, right, left.Type);
A
Andy Gocke 已提交
670
                    prologue.Add(assignment);
P
Pilchie 已提交
671 672 673

                    if (CompilationState.Emitting)
                    {
674
                        Debug.Assert(capturedFrame.Type.IsReferenceType); // Make sure we're not accidentally capturing a struct by value
675
                        frame.AddHoistedField(capturedFrame);
T
TomasMatousek 已提交
676
                        CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(frame, capturedFrame);
P
Pilchie 已提交
677 678
                    }

679
                    proxies[_innermostFramePointer] = new CapturedToFrameSymbolReplacement(capturedFrame, isReusable: false);
P
Pilchie 已提交
680 681 682 683
                }
            }

            // Capture any parameters of this block.  This would typically occur
684
            // at the top level of a method or lambda with captured parameters.
685
            foreach (var variable in env.CapturedVariables)
686
            {
687
                InitVariableProxy(syntax, variable, framePointer, prologue);
P
Pilchie 已提交
688 689
            }

690
            Symbol oldInnermostFramePointer = _innermostFramePointer;
691
            if (!framePointer.Type.IsValueType)
A
Andy Gocke 已提交
692 693 694
            {
                _innermostFramePointer = framePointer;
            }
P
Pilchie 已提交
695 696
            var addedLocals = ArrayBuilder<LocalSymbol>.GetInstance();
            addedLocals.Add(framePointer);
697
            _framePointers.Add(frame, framePointer);
P
Pilchie 已提交
698 699 700

            var result = F(prologue, addedLocals);

701
            _innermostFramePointer = oldInnermostFramePointer;
P
Pilchie 已提交
702

703
            if ((object)_innermostFramePointer != null)
P
Pilchie 已提交
704 705 706
            {
                if (oldInnermostFrameProxy != null)
                {
707
                    proxies[_innermostFramePointer] = oldInnermostFrameProxy;
P
Pilchie 已提交
708 709 710
                }
                else
                {
711
                    proxies.Remove(_innermostFramePointer);
P
Pilchie 已提交
712 713 714 715 716 717
                }
            }

            return result;
        }

718
        private void InitVariableProxy(SyntaxNode syntax, Symbol symbol, LocalSymbol framePointer, ArrayBuilder<BoundExpression> prologue)
P
Pilchie 已提交
719 720
        {
            CapturedSymbolReplacement proxy;
721
            if (proxies.TryGetValue(symbol, out proxy))
P
Pilchie 已提交
722
            {
723 724
                BoundExpression value;
                switch (symbol.Kind)
725
                {
726 727 728
                    case SymbolKind.Parameter:
                        var parameter = (ParameterSymbol)symbol;
                        ParameterSymbol parameterToUse;
729
                        if (!_parameterMap.TryGetValue(parameter, out parameterToUse))
730 731
                        {
                            parameterToUse = parameter;
732
                        }
733 734

                        value = new BoundParameter(syntax, parameterToUse);
735
                        break;
736

737
                    case SymbolKind.Local:
738 739
                        var local = (LocalSymbol)symbol;
                        if (_assignLocals == null || !_assignLocals.Contains(local))
740 741 742
                        {
                            return;
                        }
743

744 745 746 747 748 749
                        LocalSymbol localToUse;
                        if (!localMap.TryGetValue(local, out localToUse))
                        {
                            localToUse = local;
                        }

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

753 754 755
                    default:
                        throw ExceptionUtilities.UnexpectedValue(symbol.Kind);
                }
P
Pilchie 已提交
756

757
                var left = proxy.Replacement(syntax, frameType1 => new BoundLocal(syntax, framePointer, null, framePointer.Type));
E
Evan Hauck 已提交
758
                var assignToProxy = new BoundAssignmentOperator(syntax, left, value, value.Type);
A
Andy Gocke 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772
                if (_currentMethod.MethodKind == MethodKind.Constructor &&
                    symbol == _currentMethod.ThisParameter &&
                    !_seenBaseCall)
                {
                    // Containing method is a constructor 
                    // Initialization statement for the "this" proxy must be inserted
                    // after the constructor initializer statement block
                    Debug.Assert(_thisProxyInitDeferred == null);
                    _thisProxyInitDeferred = assignToProxy;
                }
                else
                {
                    prologue.Add(assignToProxy);
                }
P
Pilchie 已提交
773 774 775 776 777
            }
        }

        #region Visit Methods

778 779 780
        protected override BoundNode VisitUnhoistedParameter(BoundParameter node)
        {
            ParameterSymbol replacementParameter;
781
            if (_parameterMap.TryGetValue(node.ParameterSymbol, out replacementParameter))
782
            {
783
                return new BoundParameter(node.Syntax, replacementParameter, node.HasErrors);
784 785 786 787 788
            }

            return base.VisitUnhoistedParameter(node);
        }

P
Pilchie 已提交
789 790 791 792 793 794 795 796 797 798 799 800
        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?

801
            return (_currentMethod == _topLevelMethod || _topLevelMethod.ThisParameter == null ?
802 803
                node :
                FramePointer(node.Syntax, (NamedTypeSymbol)node.Type));
P
Pilchie 已提交
804 805 806 807
        }

        public override BoundNode VisitBaseReference(BoundBaseReference node)
        {
808
            return (!_currentMethod.IsStatic && TypeSymbol.Equals(_currentMethod.ContainingType, _topLevelMethod.ContainingType, TypeCompareKind.ConsiderEverything2))
P
Pilchie 已提交
809
                ? node
810
                : FramePointer(node.Syntax, _topLevelMethod.ContainingType); // technically, not the correct static type
P
Pilchie 已提交
811
        }
812

813 814 815 816 817 818 819 820 821
        /// <summary>
        /// Rewrites a reference to an unlowered local function to the newly
        /// lowered local function.
        /// </summary>
        private void RemapLocalFunction(
            SyntaxNode syntax,
            MethodSymbol localFunc,
            out BoundExpression receiver,
            out MethodSymbol method,
822 823
            ref ImmutableArray<BoundExpression> arguments,
            ref ImmutableArray<RefKind> argRefKinds)
824 825 826 827 828 829 830 831 832 833 834
        {
            Debug.Assert(localFunc.MethodKind == MethodKind.LocalFunction);

            var closure = Analysis.GetClosureInTree(_analysis.ScopeTree, localFunc.OriginalDefinition);
            var loweredSymbol = closure.SynthesizedLoweredMethod;

            // If the local function captured variables then they will be stored
            // in frames and the frames need to be passed as extra parameters.
            var frameCount = loweredSymbol.ExtraSynthesizedParameterCount;
            if (frameCount != 0)
            {
835
                Debug.Assert(!arguments.IsDefault);
836

837
                // Build a new list of arguments to pass to the local function
838
                // call that includes any necessary capture frames
V
vsadov 已提交
839 840
                var argumentsBuilder = ArrayBuilder<BoundExpression>.GetInstance(loweredSymbol.ParameterCount);
                argumentsBuilder.AddRange(arguments);
841 842 843 844 845

                var start = loweredSymbol.ParameterCount - frameCount;
                for (int i = start; i < loweredSymbol.ParameterCount; i++)
                {
                    // will always be a LambdaFrame, it's always a capture frame
846
                    var frameType = (NamedTypeSymbol)loweredSymbol.Parameters[i].Type.OriginalDefinition;
847 848 849 850 851 852 853 854 855 856 857 858

                    Debug.Assert(frameType is SynthesizedClosureEnvironment);

                    if (frameType.Arity > 0)
                    {
                        var typeParameters = ((SynthesizedClosureEnvironment)frameType).ConstructedFromTypeParameters;
                        Debug.Assert(typeParameters.Length == frameType.Arity);
                        var subst = this.TypeMap.SubstituteTypeParameters(typeParameters);
                        frameType = frameType.Construct(subst);
                    }

                    var frame = FrameOfType(syntax, frameType);
V
vsadov 已提交
859
                    argumentsBuilder.Add(frame);
860
                }
861 862 863

                // frame arguments are passed by ref
                // add corresponding refkinds
V
vsadov 已提交
864
                var refkindsBuilder = ArrayBuilder<RefKind>.GetInstance(argumentsBuilder.Count);
865 866 867 868 869 870 871 872 873 874 875
                if (!argRefKinds.IsDefault)
                {
                    refkindsBuilder.AddRange(argRefKinds);
                }
                else
                {
                    refkindsBuilder.AddMany(RefKind.None, arguments.Length);
                }

                refkindsBuilder.AddMany(RefKind.Ref, frameCount);

V
vsadov 已提交
876
                arguments = argumentsBuilder.ToImmutableAndFree();
877
                argRefKinds = refkindsBuilder.ToImmutableAndFree();
878 879 880 881 882 883 884
            }

            method = loweredSymbol;
            NamedTypeSymbol constructedFrame;

            RemapLambdaOrLocalFunction(syntax,
                                       localFunc,
885
                                       SubstituteTypeArguments(localFunc.TypeArgumentsWithAnnotations),
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
                                       loweredSymbol.ClosureKind,
                                       ref method,
                                       out receiver,
                                       out constructedFrame);
        }

        /// <summary>
        /// Substitutes references from old type arguments to new type arguments
        /// in the lowered methods.
        /// </summary>
        /// <example>
        /// Consider the following method:
        ///     void M() {
        ///         void L&lt;T&gt;(T t) => Console.Write(t);
        ///         L("A");
        ///     }
        ///     
        /// In this example, L&lt;T&gt; is a local function that will be
        /// lowered into its own method and the type parameter T will be
        /// alpha renamed to something else (let's call it T'). In this case,
        /// all references to the original type parameter T in L must be
        /// rewritten to the renamed parameter, T'.
        /// </example>
909
        private ImmutableArray<TypeWithAnnotations> SubstituteTypeArguments(ImmutableArray<TypeWithAnnotations> typeArguments)
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
        {
            Debug.Assert(!typeArguments.IsDefault);

            if (typeArguments.IsEmpty)
            {
                return typeArguments;
            }

            // We must perform this process repeatedly as local
            // functions may nest inside one another and capture type
            // parameters from the enclosing local functions. Each
            // iteration of nesting will cause alpha-renaming of the captured
            // parameters, meaning that we must replace until there are no
            // more alpha-rename mappings.
            //
            // The method symbol references are different from all other
            // substituted types in this context because the method symbol in
            // local function references is not rewritten until all local
            // functions have already been lowered. Everything else is rewritten
            // by the visitors as the definition is lowered. This means that
C
Charles Stoner 已提交
930
            // only one substitution happens per lowering, but we need to do
931 932
            // N substitutions all at once, where N is the number of lowerings.

933
            var builder = ArrayBuilder<TypeWithAnnotations>.GetInstance(typeArguments.Length);
934 935
            foreach (var typeArg in typeArguments)
            {
936 937
                TypeWithAnnotations oldTypeArg;
                TypeWithAnnotations newTypeArg = typeArg;
938 939 940
                do
                {
                    oldTypeArg = newTypeArg;
941
                    newTypeArg = this.TypeMap.SubstituteType(typeArg);
942 943 944

                    // When type substitution does not change the type, it is expected to return the very same object.
                    // Therefore the loop is terminated when that type (as an object) does not change.
945
                }
946
                while ((object)oldTypeArg.Type != newTypeArg.Type);
947

948 949 950
                // The types are the same, so the last pass performed no substitutions.
                // Therefore the annotations ought to be the same too.
                Debug.Assert(oldTypeArg.NullableAnnotation == newTypeArg.NullableAnnotation);
951 952 953 954 955 956 957

                builder.Add(newTypeArg);
            }

            return builder.ToImmutableAndFree();
        }

E
Evan Hauck 已提交
958
        private void RemapLambdaOrLocalFunction(
959
            SyntaxNode syntax,
E
Evan Hauck 已提交
960
            MethodSymbol originalMethod,
961
            ImmutableArray<TypeWithAnnotations> typeArgumentsOpt,
E
Evan Hauck 已提交
962 963 964 965
            ClosureKind closureKind,
            ref MethodSymbol synthesizedMethod,
            out BoundExpression receiver,
            out NamedTypeSymbol constructedFrame)
E
Evan Hauck 已提交
966
        {
E
Evan Hauck 已提交
967
            var translatedLambdaContainer = synthesizedMethod.ContainingType;
968
            var containerAsFrame = translatedLambdaContainer as SynthesizedClosureEnvironment;
969

970
            // All of _currentTypeParameters might not be preserved here due to recursively calling upwards in the chain of local functions/lambdas
E
Evan Hauck 已提交
971
            Debug.Assert((typeArgumentsOpt.IsDefault && !originalMethod.IsGenericMethod) || (typeArgumentsOpt.Length == originalMethod.Arity));
972
            var totalTypeArgumentCount = (containerAsFrame?.Arity ?? 0) + synthesizedMethod.Arity;
973
            var realTypeArguments = ImmutableArray.Create(_currentTypeParameters.SelectAsArray(t => TypeWithAnnotations.Create(t)), 0, totalTypeArgumentCount - originalMethod.Arity);
E
Evan Hauck 已提交
974
            if (!typeArgumentsOpt.IsDefault)
975
            {
E
Evan Hauck 已提交
976
                realTypeArguments = realTypeArguments.Concat(typeArgumentsOpt);
977 978
            }

979
            if ((object)containerAsFrame != null && containerAsFrame.Arity != 0)
E
Evan Hauck 已提交
980 981 982 983 984 985 986 987 988
            {
                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 已提交
989 990 991

            // for instance lambdas, receiver is the frame
            // for static lambdas, get the singleton receiver
992
            if (closureKind == ClosureKind.Singleton)
E
Evan Hauck 已提交
993 994 995 996
            {
                var field = containerAsFrame.SingletonCache.AsMember(constructedFrame);
                receiver = new BoundFieldAccess(syntax, null, field, constantValueOpt: null);
            }
997 998 999 1000 1001 1002 1003 1004
            else if (closureKind == ClosureKind.Static)
            {
                receiver = null;
            }
            else // ThisOnly and General
            {
                receiver = FrameOfType(syntax, constructedFrame);
            }
1005

E
Evan Hauck 已提交
1006 1007 1008
            synthesizedMethod = synthesizedMethod.AsMember(constructedFrame);
            if (synthesizedMethod.IsGenericMethod)
            {
1009
                synthesizedMethod = synthesizedMethod.Construct(realTypeArguments);
E
Evan Hauck 已提交
1010 1011
            }
            else
E
Evan Hauck 已提交
1012
            {
E
Evan Hauck 已提交
1013
                Debug.Assert(realTypeArguments.Length == 0);
E
Evan Hauck 已提交
1014
            }
1015
        }
P
Pilchie 已提交
1016 1017 1018

        public override BoundNode VisitCall(BoundCall node)
        {
E
Evan Hauck 已提交
1019 1020
            if (node.Method.MethodKind == MethodKind.LocalFunction)
            {
1021
                var args = VisitList(node.Arguments);
1022
                var argRefKinds = node.ArgumentRefKindsOpt;
1023 1024
                var type = VisitType(node.Type);

1025 1026
                Debug.Assert(node.ArgsToParamsOpt.IsDefault, "should be done with argument reordering by now");

1027 1028
                RemapLocalFunction(
                    node.Syntax,
1029
                    node.Method,
1030 1031
                    out var receiver,
                    out var method,
1032 1033
                    ref args,
                    ref argRefKinds);
1034 1035 1036 1037 1038

                return node.Update(
                    receiver,
                    method,
                    args,
1039
                    node.ArgumentNamesOpt,
1040
                    argRefKinds,
1041 1042 1043 1044 1045
                    node.IsDelegateCall,
                    node.Expanded,
                    node.InvokedAsExtensionMethod,
                    node.ArgsToParamsOpt,
                    node.ResultKind,
1046
                    node.BinderOpt,
1047
                    type);
E
Evan Hauck 已提交
1048
            }
1049

E
Evan Hauck 已提交
1050
            var visited = base.VisitCall(node);
P
Pilchie 已提交
1051 1052 1053 1054 1055 1056 1057 1058
            if (visited.Kind != BoundKind.Call)
            {
                return visited;
            }

            var rewritten = (BoundCall)visited;

            // Check if we need to init the 'this' proxy in a ctor call
1059
            if (!_seenBaseCall)
P
Pilchie 已提交
1060
            {
A
Andy Gocke 已提交
1061
                if (_currentMethod == _topLevelMethod && node.IsConstructorInitializer())
P
Pilchie 已提交
1062
                {
A
Andy Gocke 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
                    _seenBaseCall = true;
                    if (_thisProxyInitDeferred != null)
                    {
                        // 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),
                            value: _thisProxyInitDeferred,
                            type: rewritten.Type);
                    }
P
Pilchie 已提交
1075 1076 1077 1078 1079 1080
                }
            }

            return rewritten;
        }

1081
        private BoundSequence RewriteSequence(BoundSequence node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
P
Pilchie 已提交
1082
        {
1083
            RewriteLocals(node.Locals, newLocals);
P
Pilchie 已提交
1084

1085
            foreach (var effect in node.SideEffects)
P
Pilchie 已提交
1086
            {
1087
                var replacement = (BoundExpression)this.Visit(effect);
P
Pilchie 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
                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)
        {
            // Test if this frame has captured variables and requires the introduction of a closure class.
1100
            if (_frames.TryGetValue(node, out var frame))
P
Pilchie 已提交
1101
            {
1102
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
P
Pilchie 已提交
1103 1104 1105 1106
                    RewriteBlock(node, prologue, newLocals));
            }
            else
            {
1107
                return RewriteBlock(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
P
Pilchie 已提交
1108 1109 1110
            }
        }

1111
        private BoundBlock RewriteBlock(BoundBlock node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
P
Pilchie 已提交
1112
        {
1113
            RewriteLocals(node.Locals, newLocals);
P
Pilchie 已提交
1114 1115 1116

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

1117 1118 1119 1120 1121 1122 1123
            if (prologue.Count > 0)
            {
                newStatements.Add(new BoundSequencePoint(null, null) { WasCompilerGenerated = true });
            }

            InsertAndFreePrologue(newStatements, prologue);

P
Pilchie 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
            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 已提交
1134
            return node.Update(newLocals.ToImmutableAndFree(), node.LocalFunctions, newStatements.ToImmutableAndFree());
P
Pilchie 已提交
1135 1136
        }

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        public override BoundNode VisitScope(BoundScope node)
        {
            Debug.Assert(!node.Locals.IsEmpty);
            var newLocals = ArrayBuilder<LocalSymbol>.GetInstance();
            RewriteLocals(node.Locals, newLocals);

            var statements = VisitList(node.Statements);
            if (newLocals.Count == 0)
            {
                newLocals.Free();
                return new BoundStatementList(node.Syntax, statements);
            }

            return node.Update(newLocals.ToImmutableAndFree(), statements);
        }

P
Pilchie 已提交
1153 1154 1155
        public override BoundNode VisitCatchBlock(BoundCatchBlock node)
        {
            // Test if this frame has captured variables and requires the introduction of a closure class.
1156
            if (_frames.TryGetValue(node, out var frame))
P
Pilchie 已提交
1157
            {
1158
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
P
Pilchie 已提交
1159 1160 1161 1162 1163 1164
                {
                    return RewriteCatch(node, prologue, newLocals);
                });
            }
            else
            {
1165
                return RewriteCatch(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
P
Pilchie 已提交
1166 1167 1168
            }
        }

1169
        private BoundNode RewriteCatch(BoundCatchBlock node, ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals)
P
Pilchie 已提交
1170
        {
1171 1172
            RewriteLocals(node.Locals, newLocals);
            var rewrittenCatchLocals = newLocals.ToImmutableAndFree();
P
Pilchie 已提交
1173 1174 1175

            // If exception variable got lifted, IntroduceFrame will give us frame init prologue.
            // It needs to run before the exception variable is accessed.
1176
            // To ensure that, we will make exception variable a sequence that performs prologue as its side-effects.
P
Pilchie 已提交
1177
            BoundExpression rewrittenExceptionSource = null;
1178
            var rewrittenFilter = (BoundExpression)this.Visit(node.ExceptionFilterOpt);
P
Pilchie 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
            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);
                }
            }
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
            else if (prologue.Count > 0)
            {
                Debug.Assert(rewrittenFilter != null);
                rewrittenFilter = new BoundSequence(
                    rewrittenFilter.Syntax,
                    ImmutableArray.Create<LocalSymbol>(),
                    prologue.ToImmutable(),
                    rewrittenFilter,
                    rewrittenFilter.Type);
            }
P
Pilchie 已提交
1202

1203
            // done with this.
P
Pilchie 已提交
1204 1205 1206 1207 1208 1209 1210 1211
            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(
1212
                rewrittenCatchLocals,
P
Pilchie 已提交
1213 1214 1215
                rewrittenExceptionSource,
                exceptionTypeOpt,
                rewrittenFilter,
1216 1217
                rewrittenBlock,
                node.IsSynthesizedAsyncCatchAll);
P
Pilchie 已提交
1218 1219 1220 1221 1222
        }

        public override BoundNode VisitSequence(BoundSequence node)
        {
            // Test if this frame has captured variables and requires the introduction of a closure class.
1223
            if (_frames.TryGetValue(node, out var frame))
P
Pilchie 已提交
1224
            {
1225
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
P
Pilchie 已提交
1226 1227 1228 1229 1230 1231
                {
                    return RewriteSequence(node, prologue, newLocals);
                });
            }
            else
            {
1232
                return RewriteSequence(node, ArrayBuilder<BoundExpression>.GetInstance(), ArrayBuilder<LocalSymbol>.GetInstance());
P
Pilchie 已提交
1233 1234 1235 1236 1237 1238 1239
            }
        }

        public override BoundNode VisitStatementList(BoundStatementList node)
        {
            // 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.
1240
            if (_frames.TryGetValue(node, out var frame))
P
Pilchie 已提交
1241
            {
1242
                return IntroduceFrame(node, frame, (ArrayBuilder<BoundExpression> prologue, ArrayBuilder<LocalSymbol> newLocals) =>
P
Pilchie 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251
                {
                    var newStatements = ArrayBuilder<BoundStatement>.GetInstance();
                    InsertAndFreePrologue(newStatements, prologue);

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

1252
                    return new BoundBlock(node.Syntax, newLocals.ToImmutableAndFree(), newStatements.ToImmutableAndFree(), node.HasErrors);
P
Pilchie 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
                });
            }
            else
            {
                return base.VisitStatementList(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);
            }
1269 1270

            if (node.MethodOpt?.MethodKind == MethodKind.LocalFunction)
P
Pilchie 已提交
1271
            {
1272
                var arguments = default(ImmutableArray<BoundExpression>);
1273 1274
                var argRefKinds = default(ImmutableArray<RefKind>);

1275 1276
                RemapLocalFunction(
                    node.Syntax,
1277
                    node.MethodOpt,
1278 1279
                    out var receiver,
                    out var method,
1280 1281
                    ref arguments,
                    ref argRefKinds);
1282

1283 1284 1285 1286 1287 1288
                return new BoundDelegateCreationExpression(
                    node.Syntax,
                    receiver,
                    method,
                    node.IsExtensionMethod,
                    VisitType(node.Type));
P
Pilchie 已提交
1289
            }
1290
            return base.VisitDelegateCreationExpression(node);
P
Pilchie 已提交
1291 1292 1293 1294
        }

        public override BoundNode VisitConversion(BoundConversion conversion)
        {
1295
            Debug.Assert(conversion.ConversionKind != ConversionKind.MethodGroup);
P
Pilchie 已提交
1296 1297 1298
            if (conversion.ConversionKind == ConversionKind.AnonymousFunction)
            {
                var result = (BoundExpression)RewriteLambdaConversion((BoundLambda)conversion.Operand);
1299 1300 1301 1302

                if (_inExpressionLambda && conversion.ExplicitCastInCode)
                {
                    result = new BoundConversion(
1303 1304
                        syntax: conversion.Syntax,
                        operand: result,
V
VSadov 已提交
1305
                        conversion: conversion.Conversion,
1306 1307 1308
                        isBaseConversion: false,
                        @checked: false,
                        explicitCastInCode: true,
1309
                        conversionGroupOpt: conversion.ConversionGroupOpt,
1310
                        constantValueOpt: conversion.ConstantValueOpt,
1311 1312 1313 1314
                        type: conversion.Type);
                }

                return result;
P
Pilchie 已提交
1315
            }
1316 1317

            return base.VisitConversion(conversion);
P
Pilchie 已提交
1318 1319
        }

1320 1321
        public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatement node)
        {
E
Evan Hauck 已提交
1322
            ClosureKind closureKind;
1323
            NamedTypeSymbol translatedLambdaContainer;
1324
            SynthesizedClosureEnvironment containerAsFrame;
E
Evan Hauck 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
            BoundNode lambdaScope;
            DebugId topLevelMethodId;
            DebugId lambdaId;
            RewriteLambdaOrLocalFunction(
                node,
                out closureKind,
                out translatedLambdaContainer,
                out containerAsFrame,
                out lambdaScope,
                out topLevelMethodId,
                out lambdaId);
1336 1337 1338 1339

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

1340
        private DebugId GetLambdaId(SyntaxNode syntax, ClosureKind closureKind, int closureOrdinal)
1341 1342 1343 1344 1345
        {
            Debug.Assert(syntax != null);

            SyntaxNode lambdaOrLambdaBodySyntax;
            var anonymousFunction = syntax as AnonymousFunctionExpressionSyntax;
1346
            var localFunction = syntax as LocalFunctionStatementSyntax;
1347 1348
            bool isLambdaBody;

1349 1350 1351
            if (anonymousFunction != null)
            {
                lambdaOrLambdaBodySyntax = anonymousFunction.Body;
1352
                isLambdaBody = true;
1353
            }
1354 1355
            else if (localFunction != null)
            {
1356
                lambdaOrLambdaBodySyntax = (SyntaxNode)localFunction.Body ?? localFunction.ExpressionBody?.Expression;
1357 1358
                isLambdaBody = true;
            }
1359
            else if (LambdaUtilities.IsQueryPairLambda(syntax))
1360 1361 1362
            {
                // "pair" query lambdas
                lambdaOrLambdaBodySyntax = syntax;
1363
                isLambdaBody = false;
1364
                Debug.Assert(closureKind == ClosureKind.Singleton);
1365 1366 1367 1368 1369
            }
            else
            {
                // query lambdas
                lambdaOrLambdaBodySyntax = syntax;
1370
                isLambdaBody = true;
1371 1372
            }

1373
            Debug.Assert(!isLambdaBody || LambdaUtilities.IsLambdaBody(lambdaOrLambdaBodySyntax));
1374

1375
            // determine lambda ordinal and calculate syntax offset
1376

1377 1378 1379
            DebugId lambdaId;
            DebugId previousLambdaId;
            if (slotAllocatorOpt != null && slotAllocatorOpt.TryGetPreviousLambda(lambdaOrLambdaBodySyntax, isLambdaBody, out previousLambdaId))
1380
            {
1381
                lambdaId = previousLambdaId;
1382 1383 1384
            }
            else
            {
1385
                lambdaId = new DebugId(_lambdaDebugInfoBuilder.Count, CompilationState.ModuleBuilderOpt.CurrentGenerationOrdinal);
1386
            }
1387 1388

            int syntaxOffset = _topLevelMethod.CalculateLocalSyntaxOffset(lambdaOrLambdaBodySyntax.SpanStart, lambdaOrLambdaBodySyntax.SyntaxTree);
T
Tomas Matousek 已提交
1389
            _lambdaDebugInfoBuilder.Add(new LambdaDebugInfo(syntaxOffset, lambdaId, closureOrdinal));
1390
            return lambdaId;
1391 1392
        }

1393
        private SynthesizedClosureMethod RewriteLambdaOrLocalFunction(
E
Evan Hauck 已提交
1394 1395 1396
            IBoundLambdaOrFunction node,
            out ClosureKind closureKind,
            out NamedTypeSymbol translatedLambdaContainer,
1397
            out SynthesizedClosureEnvironment containerAsFrame,
E
Evan Hauck 已提交
1398 1399 1400
            out BoundNode lambdaScope,
            out DebugId topLevelMethodId,
            out DebugId lambdaId)
P
Pilchie 已提交
1401
        {
A
Andy Gocke 已提交
1402
            Analysis.Closure closure = Analysis.GetClosureInTree(_analysis.ScopeTree, node.Symbol);
1403 1404
            var synthesizedMethod = closure.SynthesizedLoweredMethod;
            Debug.Assert(synthesizedMethod != null);
A
Andy Gocke 已提交
1405

1406 1407 1408 1409 1410
            closureKind = synthesizedMethod.ClosureKind;
            translatedLambdaContainer = synthesizedMethod.ContainingType;
            containerAsFrame = translatedLambdaContainer as SynthesizedClosureEnvironment;
            topLevelMethodId = _analysis.GetTopLevelMethodId();
            lambdaId = synthesizedMethod.LambdaId;
1411

A
Andy Gocke 已提交
1412
            if (closure.ContainingEnvironmentOpt != null)
P
Pilchie 已提交
1413
            {
1414 1415 1416
                // Find the scope of the containing environment
                BoundNode tmpScope = null;
                Analysis.VisitScopeTree(_analysis.ScopeTree, scope =>
1417
                {
1418
                    if (scope.DeclaredEnvironments.Contains(closure.ContainingEnvironmentOpt))
A
Andy Gocke 已提交
1419
                    {
1420 1421 1422 1423 1424
                        tmpScope = scope.BoundNode;
                    }
                });
                Debug.Assert(tmpScope != null);
                lambdaScope = tmpScope;
P
Pilchie 已提交
1425 1426 1427
            }
            else
            {
1428
                lambdaScope = null;
P
Pilchie 已提交
1429
            }
D
dotnet-bot 已提交
1430

1431
            CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(translatedLambdaContainer, synthesizedMethod);
P
Pilchie 已提交
1432

1433
            foreach (var parameter in node.Symbol.Parameters)
P
Pilchie 已提交
1434
            {
1435
                _parameterMap.Add(parameter, synthesizedMethod.Parameters[parameter.Ordinal]);
P
Pilchie 已提交
1436 1437 1438
            }

            // rewrite the lambda body as the generated method's body
1439 1440 1441 1442 1443 1444 1445 1446 1447
            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 已提交
1448 1449 1450

            // switch to the generated method

1451
            _currentMethod = synthesizedMethod;
1452
            if (closureKind == ClosureKind.Static || closureKind == ClosureKind.Singleton)
P
Pilchie 已提交
1453 1454
            {
                // no link from a static lambda to its container
1455
                _innermostFramePointer = _currentFrameThis = null;
P
Pilchie 已提交
1456 1457 1458
            }
            else
            {
1459 1460
                _currentFrameThis = synthesizedMethod.ThisParameter;
                _framePointers.TryGetValue(translatedLambdaContainer, out _innermostFramePointer);
P
Pilchie 已提交
1461 1462
            }

E
Evan Hauck 已提交
1463
            _currentTypeParameters = containerAsFrame?.TypeParameters.Concat(synthesizedMethod.TypeParameters) ?? synthesizedMethod.TypeParameters;
E
Evan Hauck 已提交
1464
            _currentLambdaBodyTypeMap = synthesizedMethod.TypeMap;
P
Pilchie 已提交
1465 1466 1467

            var body = AddStatementsIfNeeded((BoundStatement)VisitBlock(node.Body));
            CheckLocalsDefined(body);
1468
            AddSynthesizedMethod(synthesizedMethod, body);
P
Pilchie 已提交
1469 1470 1471

            // return to the old method

1472 1473 1474 1475 1476 1477 1478
            _currentMethod = oldMethod;
            _currentFrameThis = oldFrameThis;
            _currentTypeParameters = oldTypeParameters;
            _innermostFramePointer = oldInnermostFramePointer;
            _currentLambdaBodyTypeMap = oldTypeMap;
            _addedLocals = oldAddedLocals;
            _addedStatements = oldAddedStatements;
P
Pilchie 已提交
1479

E
Evan Hauck 已提交
1480
            return synthesizedMethod;
1481
        }
E
Evan Hauck 已提交
1482

1483 1484 1485
        private void AddSynthesizedMethod(MethodSymbol method, BoundStatement body)
        {
            if (_synthesizedMethods == null)
1486
            {
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
                _synthesizedMethods = ArrayBuilder<TypeCompilationState.MethodWithBody>.GetInstance();
            }

            _synthesizedMethods.Add(
                new TypeCompilationState.MethodWithBody(
                    method,
                    body,
                    CompilationState.CurrentImportChain));
        }

        private BoundNode RewriteLambdaConversion(BoundLambda node)
        {
E
Evan Hauck 已提交
1499 1500 1501 1502 1503 1504 1505
            var wasInExpressionLambda = _inExpressionLambda;
            _inExpressionLambda = _inExpressionLambda || node.Type.IsExpressionTree();

            if (_inExpressionLambda)
            {
                var newType = VisitType(node.Type);
                var newBody = (BoundBlock)Visit(node.Body);
1506
                node = node.Update(node.UnboundLambda, node.Symbol, newBody, node.Diagnostics, node.Binder, newType);
1507
                var result0 = wasInExpressionLambda ? node : ExpressionLambdaRewriter.RewriteLambda(node, CompilationState, TypeMap, RecursionDepth, Diagnostics);
E
Evan Hauck 已提交
1508 1509 1510 1511 1512 1513
                _inExpressionLambda = wasInExpressionLambda;
                return result0;
            }

            ClosureKind closureKind;
            NamedTypeSymbol translatedLambdaContainer;
1514
            SynthesizedClosureEnvironment containerAsFrame;
E
Evan Hauck 已提交
1515 1516 1517
            BoundNode lambdaScope;
            DebugId topLevelMethodId;
            DebugId lambdaId;
1518
            SynthesizedClosureMethod synthesizedMethod = RewriteLambdaOrLocalFunction(
E
Evan Hauck 已提交
1519 1520 1521 1522 1523 1524 1525 1526
                node,
                out closureKind,
                out translatedLambdaContainer,
                out containerAsFrame,
                out lambdaScope,
                out topLevelMethodId,
                out lambdaId);

E
Evan Hauck 已提交
1527
            MethodSymbol referencedMethod = synthesizedMethod;
1528
            BoundExpression receiver;
E
Evan Hauck 已提交
1529
            NamedTypeSymbol constructedFrame;
1530
            RemapLambdaOrLocalFunction(node.Syntax, node.Symbol, default(ImmutableArray<TypeWithAnnotations>), closureKind, ref referencedMethod, out receiver, out constructedFrame);
1531

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

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

1536 1537
            // static lambdas are emitted as instance methods on a singleton receiver
            // delegates invoke dispatch is optimized for instance delegates so 
1538
            // it is preferable to emit lambdas as instance methods even when lambdas 
1539
            // do not capture anything
P
Pilchie 已提交
1540 1541 1542 1543
            BoundExpression result = new BoundDelegateCreationExpression(
                node.Syntax,
                receiver,
                referencedMethod,
1544
                isExtensionMethod: false,
P
Pilchie 已提交
1545 1546 1547 1548 1549
                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.
1550
            var shouldCacheForStaticMethod = closureKind == ClosureKind.Singleton &&
1551
                _currentMethod.MethodKind != MethodKind.StaticConstructor &&
P
Pilchie 已提交
1552 1553 1554 1555 1556
                !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 &&
1557
                lambdaScope != Analysis.GetScopeParent(_analysis.ScopeTree, node.Body).BoundNode &&
P
Pilchie 已提交
1558 1559 1560 1561
                InLoopOrLambda(node.Syntax, lambdaScope.Syntax);

            if (shouldCacheForStaticMethod || shouldCacheInLoop)
            {
1562
                // replace the expression "new Delegate(frame.M)" with "frame.cache ?? (frame.cache = new Delegate(frame.M));
1563
                var F = new SyntheticBoundNodeFactory(_currentMethod, node.Syntax, CompilationState, Diagnostics);
P
Pilchie 已提交
1564 1565
                try
                {
T
TomasMatousek 已提交
1566
                    BoundExpression cache;
1567
                    if (shouldCacheForStaticMethod || shouldCacheInLoop && (object)containerAsFrame != null)
P
Pilchie 已提交
1568
                    {
1569 1570 1571
                        // 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.
1572
                        var cacheVariableType = containerAsFrame.TypeMap.SubstituteType(node.Type).Type;
1573

1574
                        var cacheVariableName = GeneratedNames.MakeLambdaCacheFieldName(
1575 1576
                            // 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.
1577 1578
                            (closureKind == ClosureKind.General) ? -1 : topLevelMethodId.Ordinal,
                            topLevelMethodId.Generation,
1579 1580
                            lambdaId.Ordinal,
                            lambdaId.Generation);
1581

1582
                        var cacheField = new SynthesizedLambdaCacheFieldSymbol(translatedLambdaContainer, cacheVariableType, cacheVariableName, _topLevelMethod, isReadOnly: false, isStatic: closureKind == ClosureKind.Singleton);
T
TomasMatousek 已提交
1583
                        CompilationState.ModuleBuilderOpt.AddSynthesizedDefinition(translatedLambdaContainer, cacheField);
T
TomasMatousek 已提交
1584
                        cache = F.Field(receiver, cacheField.AsMember(constructedFrame)); //NOTE: the field was added to the unconstructed frame type.
P
Pilchie 已提交
1585 1586 1587 1588
                    }
                    else
                    {
                        // the lambda captures at most the "this" of the enclosing method.  We cache its delegate in a local variable.
1589
                        var cacheLocal = F.SynthesizedLocal(type, kind: SynthesizedLocalKind.CachedAnonymousMethodDelegate);
1590 1591 1592
                        if (_addedLocals == null) _addedLocals = ArrayBuilder<LocalSymbol>.GetInstance();
                        _addedLocals.Add(cacheLocal);
                        if (_addedStatements == null) _addedStatements = ArrayBuilder<BoundStatement>.GetInstance();
T
TomasMatousek 已提交
1593
                        cache = F.Local(cacheLocal);
1594
                        _addedStatements.Add(F.Assignment(cache, F.Null(type)));
P
Pilchie 已提交
1595 1596
                    }

T
TomasMatousek 已提交
1597
                    result = F.Coalesce(cache, F.AssignmentExpression(cache, result));
P
Pilchie 已提交
1598 1599 1600 1601
                }
                catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
                {
                    Diagnostics.Add(ex.Diagnostic);
1602
                    return new BoundBadExpression(F.Syntax, LookupResultKind.Empty, ImmutableArray<Symbol>.Empty, ImmutableArray.Create<BoundExpression>(node), node.Type);
P
Pilchie 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
                }
            }

            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)
            {
1641
                switch (curSyntax.Kind())
P
Pilchie 已提交
1642 1643 1644
                {
                    case SyntaxKind.ForStatement:
                    case SyntaxKind.ForEachStatement:
1645
                    case SyntaxKind.ForEachVariableStatement:
P
Pilchie 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
                    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 已提交
1665
        #endregion
P
Pilchie 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

#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
    }
}