/// <summary> /// Returns true if the specified node can represent a closure scope -- that is a scope of a captured variable. /// Doesn't validate whether or not the node actually declares any captured variable. /// </summary> internal static bool IsClosureScope(SyntaxNode node) { switch (node.Kind()) { case SyntaxKind.Block: case SyntaxKind.SwitchStatement: case SyntaxKind.ArrowExpressionClause: // expression-bodied member case SyntaxKind.CatchClause: case SyntaxKind.ForStatement: case SyntaxKind.ForEachStatement: case SyntaxKind.UsingStatement: // ctor parameter captured by a lambda in a ctor initializer case SyntaxKind.ConstructorDeclaration: return(true); default: if (SyntaxFacts.IsLambdaBody(node)) { return(true); } // TODO: EE expression if (node is ExpressionSyntax && node.Parent != null && node.Parent.Parent == null) { return(true); } return(false); } }
public BoundLambda(CSharpSyntaxNode syntax, BoundBlock body, ImmutableArray <Diagnostic> diagnostics, Binder binder, TypeSymbol type, bool inferReturnType) : this(syntax, (LambdaSymbol)binder.ContainingMemberOrLambda, body, diagnostics, binder, type) { if (inferReturnType) { _inferredReturnType = InferReturnType(this.Body, this.Binder, this.Symbol.IsAsync, ref _inferredReturnTypeUseSiteDiagnostics, out _inferredFromSingleType); #if DEBUG _hasInferredReturnType = true; #endif } Debug.Assert( syntax.IsAnonymousFunction() || // lambda expressions syntax is ExpressionSyntax && SyntaxFacts.IsLambdaBody(syntax) || // query lambdas SyntaxFacts.IsQueryPairLambda(syntax) // "pair" lambdas in queries ); }
private static void AssertIsLambdaScopeSyntax(CSharpSyntaxNode syntax) { // See C# specification, chapter 3.7 Scopes. // static lambdas technically have the class scope so the scope syntax is null if (syntax == null) { return; } // block: if (syntax.IsKind(SyntaxKind.Block)) { return; } // switch block: if (syntax.IsKind(SyntaxKind.SwitchStatement)) { return; } // expression-bodied member: if (syntax.IsKind(SyntaxKind.ArrowExpressionClause)) { return; } // catch clause (including filter): if (syntax.IsKind(SyntaxKind.CatchClause)) { return; } // class/struct containing a field/property with a declaration expression if (syntax.IsKind(SyntaxKind.ClassDeclaration) || syntax.IsKind(SyntaxKind.StructDeclaration)) { return; } // lambda in a let clause, // e.g. from item in array let a = new Func<int>(() => item) if (syntax.IsKind(SyntaxKind.LetClause)) { return; } if (IsStatementWithEmbeddedStatementBody(syntax.Kind())) { return; } // lambda bodies: if (SyntaxFacts.IsLambdaBody(syntax)) { return; } // TODO: EE expression if (syntax is ExpressionSyntax && syntax.Parent.Parent == null) { return; } throw ExceptionUtilities.UnexpectedValue(syntax.Kind()); }