public static VarianceKind VarianceKindFromToken(this SyntaxToken node)
 {
     switch (node.CSharpKind())
     {
     default: return(VarianceKind.None);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Helper for implementing <see cref="DeclaringSyntaxReferences"/> for derived classes that store a location but not a
        /// <see cref="CSharpSyntaxNode"/> or <see cref="SyntaxReference"/>.
        /// </summary>
        internal static ImmutableArray <SyntaxReference> GetDeclaringSyntaxReferenceHelper <TNode>(ImmutableArray <Location> locations)
            where TNode : CSharpSyntaxNode
        {
            if (locations.IsEmpty)
            {
                return(ImmutableArray <SyntaxReference> .Empty);
            }

            ArrayBuilder <SyntaxReference> builder = ArrayBuilder <SyntaxReference> .GetInstance();

            foreach (Location location in locations)
            {
                if (location.IsInSource)
                {
                    SyntaxToken token = (SyntaxToken)location.SourceTree.GetRoot().FindToken(location.SourceSpan.Start);
                    if (token.CSharpKind() != SyntaxKind.None)
                    {
                        CSharpSyntaxNode node = token.Parent.FirstAncestorOrSelf <TNode>();
                        if (node != null)
                        {
                            builder.Add(node.GetReference());
                        }
                    }
                }
            }

            return(builder.ToImmutableAndFree());
        }
        public bool IsOperator(SyntaxToken token)
        {
            var kind = token.CSharpKind();

            return
                ((SyntaxFacts.IsAnyUnaryExpression(kind) &&
                  (token.Parent is PrefixUnaryExpressionSyntax || token.Parent is PostfixUnaryExpressionSyntax)) ||
                 (SyntaxFacts.IsBinaryExpression(kind) && token.Parent is BinaryExpressionSyntax));
        }
            public override void VisitToken(SyntaxToken token)
            {
                // trivia is not considered, only the raw form of the token

                this.hash = Hash.Combine((int)token.CSharpKind(), this.hash);

                switch (token.CSharpKind())
                {
                case SyntaxKind.IdentifierToken:
                    this.hash = Hash.Combine(token.ValueText.GetHashCode(), this.hash);
                    break;

                case SyntaxKind.NumericLiteralToken:
                case SyntaxKind.CharacterLiteralToken:
                case SyntaxKind.StringLiteralToken:
                    this.hash = Hash.Combine(token.ToString().GetHashCode(), this.hash);
                    break;
                }
            }
Esempio n. 5
0
        private BoundExpression BindInterpolatedString(InterpolatedStringSyntax node, DiagnosticBag diagnostics)
        {
            var builder = ArrayBuilder <BoundExpression> .GetInstance();

            SyntaxToken stringStart = node.StringStart;

            Debug.Assert(stringStart.CSharpKind() == SyntaxKind.InterpolatedStringStartToken);
            var stringType = GetSpecialType(SpecialType.System_String, diagnostics, node);
            var objectType = GetSpecialType(SpecialType.System_Object, diagnostics, node);

            builder.Add(new BoundLiteral(node, ConstantValue.Create(stringStart.ValueText, SpecialType.System_String), stringType));
            var inserts = node.InterpolatedInserts.GetWithSeparators();

            for (int i = 0; i < inserts.Count; i++)
            {
                if (i % 2 == 0)
                {
                    // an expression hole
                    var             expr      = (InterpolatedStringInsertSyntax)inserts[i].AsNode();
                    var             bound     = this.GenerateConversionForAssignment(objectType, this.BindExpression(expr.Expression, diagnostics), diagnostics);
                    BoundExpression alignment = expr.Alignment != null?BindExpression(expr.Alignment, diagnostics) : null;

                    BoundExpression format = null;
                    if (expr.Format != default(SyntaxToken))
                    {
                        switch (expr.Format.CSharpKind())
                        {
                        case SyntaxKind.IdentifierToken:
                        case SyntaxKind.StringLiteralToken:
                            format = new BoundLiteral(expr, ConstantValue.Create(expr.Format.ValueText), stringType);
                            break;

                        default:
                            Debug.Assert(expr.HasErrors);
                            break;
                        }
                    }

                    builder.Add(new BoundStringInsert(expr, bound, alignment, format, null));
                }
                else
                {
                    // the separator token, which is part of the string literal
                    var token = inserts[i].AsToken();
                    var bound = new BoundLiteral(node, ConstantValue.Create(token.ValueText, SpecialType.System_String), stringType);
                    builder.Add(bound);
                }
            }

            SyntaxToken stringEnd = node.StringEnd;

            builder.Add(new BoundLiteral(node, ConstantValue.Create(stringEnd.ValueText, SpecialType.System_String), stringType));
            return(new BoundInterpolatedString(node, builder.ToImmutableAndFree(), stringType));
        }
        public bool TryGetCorrespondingOpenBrace(SyntaxToken token, out SyntaxToken openBrace)
        {
            if (token.CSharpKind() == SyntaxKind.CloseBraceToken)
            {
                var tuple = token.Parent.GetBraces();

                openBrace = tuple.Item1;
                return(openBrace.CSharpKind() == SyntaxKind.OpenBraceToken);
            }

            return(false);
        }
        public bool IsLiteral(SyntaxToken token)
        {
            switch (token.CSharpKind())
            {
            case SyntaxKind.NumericLiteralToken:
            case SyntaxKind.CharacterLiteralToken:
            case SyntaxKind.StringLiteralToken:
            case SyntaxKind.NullKeyword:
            case SyntaxKind.TrueKeyword:
            case SyntaxKind.FalseKeyword:
                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        private BoundExpression BindAnonymousObjectCreation(AnonymousObjectCreationExpressionSyntax node, DiagnosticBag diagnostics)
        {
            //  prepare
            var  initializers = node.Initializers;
            int  fieldCount   = initializers.Count;
            bool hasError     = false;

            //  bind field initializers
            BoundExpression[]    boundExpressions = new BoundExpression[fieldCount];
            AnonymousTypeField[] fields           = new AnonymousTypeField[fieldCount];
            CSharpSyntaxNode[]   fieldSyntaxNodes = new CSharpSyntaxNode[fieldCount];

            // WARNING: Note that SemanticModel.GetDeclaredSymbol for field initializer node relies on
            //          the fact that the order of properties in anonymous type template corresponds
            //          1-to-1 to the appropriate filed initializer syntax nodes; This means such
            //          correspondence must be preserved all the time including erroneos scenarios

            // set of names already used
            HashSet <string> uniqueFieldNames = new HashSet <string>();

            for (int i = 0; i < fieldCount; i++)
            {
                AnonymousObjectMemberDeclaratorSyntax fieldInitializer = initializers[i];
                NameEqualsSyntax nameEquals = fieldInitializer.NameEquals;
                ExpressionSyntax expression = fieldInitializer.Expression;

                SyntaxToken nameToken = default(SyntaxToken);
                if (nameEquals != null)
                {
                    nameToken = nameEquals.Name.Identifier;
                }
                else
                {
                    nameToken = expression.ExtractAnonymousTypeMemberName();
                }

                hasError            = hasError || expression.HasErrors;
                boundExpressions[i] = this.BindValue(expression, diagnostics, BindValueKind.RValue);

                //  check the name to be unique
                string fieldName = null;
                if (nameToken.CSharpKind() == SyntaxKind.IdentifierToken)
                {
                    fieldName = nameToken.ValueText;
                    if (uniqueFieldNames.Contains(fieldName))
                    {
                        //  name duplication
                        Error(diagnostics, ErrorCode.ERR_AnonymousTypeDuplicatePropertyName, fieldInitializer);
                        hasError  = true;
                        fieldName = null;
                    }
                    else
                    {
                        uniqueFieldNames.Add(fieldName);
                    }
                }
                else
                {
                    // there is something wrong with field's name
                    hasError = true;
                }

                //  calculate the expression's type and report errors if needed
                TypeSymbol fieldType = GetAnonymousTypeFieldType(boundExpressions[i], fieldInitializer, diagnostics, ref hasError);

                // build anonymous type field descriptor
                fieldSyntaxNodes[i] = (nameToken.CSharpKind() == SyntaxKind.IdentifierToken) ? (CSharpSyntaxNode)nameToken.Parent : fieldInitializer;
                fields[i]           = new AnonymousTypeField(fieldName == null ? '$' + i.ToString() : fieldName, fieldSyntaxNodes[i].Location, fieldType);

                //  NOTE: ERR_InvalidAnonymousTypeMemberDeclarator (CS0746) would be generated by parser if needed
            }

            //  Create anonymous type
            AnonymousTypeManager    manager       = this.Compilation.AnonymousTypeManager;
            AnonymousTypeDescriptor descriptor    = new AnonymousTypeDescriptor(fields.AsImmutableOrNull(), node.NewKeyword.GetLocation());
            NamedTypeSymbol         anonymousType = manager.ConstructAnonymousTypeSymbol(descriptor);

            // declarators - bound nodes created for providing semantic info
            // on anonymous type fields having explicitly specified name
            ArrayBuilder <BoundAnonymousPropertyDeclaration> declarators =
                ArrayBuilder <BoundAnonymousPropertyDeclaration> .GetInstance();

            for (int i = 0; i < fieldCount; i++)
            {
                NameEqualsSyntax explicitName = initializers[i].NameEquals;
                if (explicitName != null)
                {
                    AnonymousTypeField field = fields[i];
                    if (field.Name != null)
                    {
                        //  get property symbol and create a bound property declaration node
                        foreach (var symbol in anonymousType.GetMembers(field.Name))
                        {
                            if (symbol.Kind == SymbolKind.Property)
                            {
                                declarators.Add(new BoundAnonymousPropertyDeclaration(fieldSyntaxNodes[i], (PropertySymbol)symbol, field.Type));
                                break;
                            }
                        }
                    }
                }
            }

            // check if anonymous object creation is allowed in this context
            if (!this.IsAnonymousTypesAllowed())
            {
                Error(diagnostics, ErrorCode.ERR_AnonymousTypeNotAvailable, node.NewKeyword);
                hasError = true;
            }

            //  Finally create a bound node
            return(new BoundAnonymousObjectCreationExpression(
                       node,
                       anonymousType.InstanceConstructors[0],
                       boundExpressions.AsImmutableOrNull(),
                       declarators.ToImmutableAndFree(),
                       anonymousType,
                       hasError));
        }
Esempio n. 9
0
 private static bool IsBeforeToken(int position, SyntaxToken firstExcluded)
 {
     return(firstExcluded.CSharpKind() == SyntaxKind.None || position < firstExcluded.SpanStart);
 }
Esempio n. 10
0
        //public static bool IsContextualKeyword(this SyntaxToken token)
        //{
        //	return SyntaxKindFacts.IsContextualKeyword(token.CSharpKind());
        //}

        public static bool IsReservedKeyword(this SyntaxToken token)
        {
            return(SyntaxKindFacts.IsReservedKeyword(token.CSharpKind()));
        }