Пример #1
0
        /// <summary>
        /// Returns true if the node is the object of an element access expression.
        /// </summary>
        public static bool IsIndexed(ExpressionSyntax node)
        {
            node = (ExpressionSyntax)SyntaxFactory.GetStandaloneExpression(node);
            var indexer = node.Parent as ElementAccessExpressionSyntax;

            return(indexer != null && indexer.Expression == node);
        }
Пример #2
0
        /// <summary>
        /// Returns true if a node is in a tree location that is expected to be either a namespace or type
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool IsInNamespaceOrTypeContext(ExpressionSyntax node)
        {
            if (node != null)
            {
                node = (ExpressionSyntax)SyntaxFactory.GetStandaloneExpression(node);
                var parent = node.Parent;
                if (parent != null)
                {
                    switch (parent.Kind())
                    {
                    case UsingDirective:
                        return(((UsingDirectiveSyntax)parent).Name == node);

                    case QualifiedName:
                        // left of QN is namespace or type.  Note: when you have "a.b.c()", then
                        // "a.b" is not a qualified name, it is a member access expression.
                        // Qualified names are only parsed when the parser knows it's a type only
                        // context.
                        return(((QualifiedNameSyntax)parent).Left == node);

                    default:
                        return(IsInTypeOnlyContext(node));
                    }
                }
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Returns true if the node is the object of an invocation expression.
        /// </summary>
        public static bool IsInvoked(ExpressionSyntax node)
        {
            node = (ExpressionSyntax)SyntaxFactory.GetStandaloneExpression(node);
            var inv = node.Parent as InvocationExpressionSyntax;

            return(inv != null && inv.Expression == node);
        }
Пример #4
0
        /// <summary>
        /// Returns true if the node is in a tree location that is expected to be a type
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool IsInTypeOnlyContext(ExpressionSyntax node)
        {
            node = SyntaxFactory.GetStandaloneExpression(node);
            var parent = node.Parent;

            if (parent != null)
            {
                switch (parent.Kind())
                {
                case Attribute:
                    return(((AttributeSyntax)parent).Name == node);

                case ArrayType:
                    return(((ArrayTypeSyntax)parent).ElementType == node);

                case PointerType:
                    return(((PointerTypeSyntax)parent).ElementType == node);

                case PredefinedType:
                    return(true);

                case NullableType:
                    return(((NullableTypeSyntax)parent).ElementType == node);

                case TypeArgumentList:
                    // all children of GenericNames are type arguments
                    return(true);

                case CastExpression:
                    return(((CastExpressionSyntax)parent).Type == node);

                case ObjectCreationExpression:
                    return(((ObjectCreationExpressionSyntax)parent).Type == node);

                case StackAllocArrayCreationExpression:
                    return(((StackAllocArrayCreationExpressionSyntax)parent).Type == node);

                case FromClause:
                    return(((FromClauseSyntax)parent).Type == node);

                case JoinClause:
                    return(((JoinClauseSyntax)parent).Type == node);

                case VariableDeclaration:
                    return(((VariableDeclarationSyntax)parent).Type == node);

                case ForEachStatement:
                    return(((ForEachStatementSyntax)parent).Type == node);

                case CatchDeclaration:
                    return(((CatchDeclarationSyntax)parent).Type == node);

                case AsExpression:
                case IsExpression:
                    return(((BinaryExpressionSyntax)parent).Right == node);

                case TypeOfExpression:
                    return(((TypeOfExpressionSyntax)parent).Type == node);

                case SizeOfExpression:
                    return(((SizeOfExpressionSyntax)parent).Type == node);

                case DefaultExpression:
                    return(((DefaultExpressionSyntax)parent).Type == node);

                case RefValueExpression:
                    return(((RefValueExpressionSyntax)parent).Type == node);

                case RefType:
                    return(((RefTypeSyntax)parent).Type == node);

                case Parameter:
                    return(((ParameterSyntax)parent).Type == node);

                case TypeConstraint:
                    return(((TypeConstraintSyntax)parent).Type == node);

                case MethodDeclaration:
                    return(((MethodDeclarationSyntax)parent).ReturnType == node);

                case IndexerDeclaration:
                    return(((IndexerDeclarationSyntax)parent).Type == node);

                case OperatorDeclaration:
                    return(((OperatorDeclarationSyntax)parent).ReturnType == node);

                case ConversionOperatorDeclaration:
                    return(((ConversionOperatorDeclarationSyntax)parent).Type == node);

                case PropertyDeclaration:
                    return(((PropertyDeclarationSyntax)parent).Type == node);

                case DelegateDeclaration:
                    return(((DelegateDeclarationSyntax)parent).ReturnType == node);

                case EventDeclaration:
                    return(((EventDeclarationSyntax)parent).Type == node);

                case LocalFunctionStatement:
                    return(((LocalFunctionStatementSyntax)parent).ReturnType == node);

                case SimpleBaseType:
                    return(true);

                case CrefParameter:
                    return(true);

                case ConversionOperatorMemberCref:
                    return(((ConversionOperatorMemberCrefSyntax)parent).Type == node);

                case ExplicitInterfaceSpecifier:
                    // #13.4.1 An explicit member implementation is a method, property, event or indexer
                    // declaration that references a fully qualified interface member name.
                    // A ExplicitInterfaceSpecifier represents the left part (QN) of the member name, so it
                    // should be treated like a QualifiedName.
                    return(((ExplicitInterfaceSpecifierSyntax)parent).Name == node);

                case DeclarationPattern:
                    return(((DeclarationPatternSyntax)parent).Type == node);

                case TupleElement:
                    return(((TupleElementSyntax)parent).Type == node);

                case DeclarationExpression:
                    return(((DeclarationExpressionSyntax)parent).Type == node);

                case IncompleteMember:
                    return(((IncompleteMemberSyntax)parent).Type == node);
                }
            }

            return(false);
        }
Пример #5
0
        /// <summary>
        /// Returns true if the node is in a tree location that is expected to be a type
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool IsInTypeOnlyContext(ExpressionSyntax node)
        {
            node = (ExpressionSyntax)SyntaxFactory.GetStandaloneExpression(node);
            var parent = node.Parent;

            if (parent != null)
            {
                switch (parent.Kind)
                {
                case SyntaxKind.Annotation:
                    return(((AnnotationSyntax)parent).Name == node);

                case SyntaxKind.ArrayType:
                    return(((ArrayTypeSyntax)parent).ElementType == node);

                case SyntaxKind.PredefinedType:
                    return(true);

                //case SyntaxKind.NullableType:
                //	return ((NullableTypeSyntax)parent).ElementType == node;

                case SyntaxKind.TypeArgumentList:
                    // all children of GenericNames are type arguments
                    return(true);

                case SyntaxKind.CastExpression:
                    return(((CastExpressionSyntax)parent).Type == node);

                case SyntaxKind.ObjectCreationExpression:
                    return(((ObjectCreationExpressionSyntax)parent).Type == node);


                case SyntaxKind.VariableDeclaration:
                    return(((VariableDeclarationSyntax)parent).Type == node);


                case SyntaxKind.CatchDeclaration:
                    return(((CatchDeclarationSyntax)parent).Type == node);


                case SyntaxKind.InstanceOfExpression:
                    return(((BinaryExpressionSyntax)parent).Right == node);

                case SyntaxKind.DefaultExpression:
                    return(((DefaultExpressionSyntax)parent).Type == node);

                case SyntaxKind.Parameter:
                    return(((ParameterSyntax)parent).Type == node);

                case SyntaxKind.TypeConstraint:
                    return(((TypeConstraintSyntax)parent).Type == node);

                case SyntaxKind.MethodDeclaration:
                    return(((MethodDeclarationSyntax)parent).ReturnType == node);


                case SyntaxKind.ImplementsListClause:
                    return(true);                             // children of BaseListSyntax are only types

                case SyntaxKind.CrefParameter:
                    return(true);
                }
            }

            return(false);
        }