示例#1
0
        internal static bool IsInLocalFunctionTypeParameterScope(int position, LocalFunctionStatementSyntax localFunction)
        {
            Debug.Assert(localFunction != null);

            if (localFunction.TypeParameterList == null)
            {
                // no type parameters => they are not in scope
                return(false);
            }

            // optimization for a common case - when position is in the ReturnType, we can see type parameters
            if (localFunction.ReturnType.FullSpan.Contains(position))
            {
                return(true);
            }

            // Must be in the local function, but not in an attribute on the method.
            if (IsInAttributeSpecification(position, localFunction.AttributeLists))
            {
                return(false);
            }

            var firstNameToken     = localFunction.Identifier;
            var firstPostNameToken = localFunction.TypeParameterList.LessThanToken;

            // Scope does not include local function name.
            return(!IsBetweenTokens(position, firstNameToken, firstPostNameToken));
        }
示例#2
0
        private static SyntaxToken GetFirstExcludedToken(StatementSyntax statement)
        {
            Debug.Assert(statement != null);
            switch (statement.Kind())
            {
            case SyntaxKind.Block:
                return(((BlockSyntax)statement).CloseBraceToken);

            case SyntaxKind.BreakStatement:
                return(((BreakStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
                return(((CheckedStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.ContinueStatement:
                return(((ContinueStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalDeclarationStatement:
                return(((LocalDeclarationStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.DoStatement:
                return(((DoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.EmptyStatement:
                return(((EmptyStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.ExpressionStatement:
                return(((ExpressionStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.FixedStatement:
                return(GetFirstExcludedToken(((FixedStatementSyntax)statement).Statement));

            case SyntaxKind.ForEachStatement:
            case SyntaxKind.ForEachVariableStatement:
                return(GetFirstExcludedToken(((CommonForEachStatementSyntax)statement).Statement));

            case SyntaxKind.ForStatement:
                return(GetFirstExcludedToken(((ForStatementSyntax)statement).Statement));

            case SyntaxKind.GotoDefaultStatement:
            case SyntaxKind.GotoCaseStatement:
            case SyntaxKind.GotoStatement:
                return(((GotoStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.IfStatement:
                IfStatementSyntax ifStmt  = (IfStatementSyntax)statement;
                ElseClauseSyntax  elseOpt = ifStmt.Else;
                return(GetFirstExcludedToken(elseOpt == null ? ifStmt.Statement : elseOpt.Statement));

            case SyntaxKind.LabeledStatement:
                return(GetFirstExcludedToken(((LabeledStatementSyntax)statement).Statement));

            case SyntaxKind.LockStatement:
                return(GetFirstExcludedToken(((LockStatementSyntax)statement).Statement));

            case SyntaxKind.ReturnStatement:
                return(((ReturnStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.SwitchStatement:
                return(((SwitchStatementSyntax)statement).CloseBraceToken);

            case SyntaxKind.ThrowStatement:
                return(((ThrowStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.TryStatement:
                TryStatementSyntax tryStmt = (TryStatementSyntax)statement;

                FinallyClauseSyntax finallyClause = tryStmt.Finally;
                if (finallyClause != null)
                {
                    return(finallyClause.Block.CloseBraceToken);
                }

                CatchClauseSyntax lastCatch = tryStmt.Catches.LastOrDefault();
                if (lastCatch != null)
                {
                    return(lastCatch.Block.CloseBraceToken);
                }
                return(tryStmt.Block.CloseBraceToken);

            case SyntaxKind.UnsafeStatement:
                return(((UnsafeStatementSyntax)statement).Block.CloseBraceToken);

            case SyntaxKind.UsingStatement:
                return(GetFirstExcludedToken(((UsingStatementSyntax)statement).Statement));

            case SyntaxKind.WhileStatement:
                return(GetFirstExcludedToken(((WhileStatementSyntax)statement).Statement));

            case SyntaxKind.YieldReturnStatement:
            case SyntaxKind.YieldBreakStatement:
                return(((YieldStatementSyntax)statement).SemicolonToken);

            case SyntaxKind.LocalFunctionStatement:
                LocalFunctionStatementSyntax localFunctionStmt = (LocalFunctionStatementSyntax)statement;
                if (localFunctionStmt.Body != null)
                {
                    return(GetFirstExcludedToken(localFunctionStmt.Body));
                }
                if (localFunctionStmt.SemicolonToken != default(SyntaxToken))
                {
                    return(localFunctionStmt.SemicolonToken);
                }
                return(localFunctionStmt.ParameterList.GetLastToken());

            default:
                throw ExceptionUtilities.UnexpectedValue(statement.Kind());
            }
        }