Пример #1
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                var newBinder = new ImplicitlyTypedLocalBinder(this.binder, this);
                var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, _initializer, _initializer);
                if (initializerOpt != null)
                {
                    return initializerOpt.Type;
                }

                return null;
            }
Пример #2
0
        private TypeSymbol GetTypeSymbol()
        {
            var diagnostics = DiagnosticBag.GetInstance();

            Binder typeBinder = this.binder;

            bool       isVar;
            TypeSymbol declType = typeBinder.BindType(typeSyntax, diagnostics, out isVar);

            if (isVar)
            {
                TypeSymbol inferredType = null;

                if (this.DeclarationKind == LocalDeclarationKind.ForEach)
                {
                    // Normally, it would not be safe to cast to a specific binder type.  However, we verified the type
                    // in the factory method call for this symbol.
                    inferredType = ((ForEachLoopBinder)typeBinder).InferCollectionElementType(diagnostics, collection);
                }
                else if (initializer != null)
                {
                    var newBinder      = new ImplicitlyTypedLocalBinder(typeBinder, this);
                    var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, initializer, initializer);
                    if (initializerOpt != null)
                    {
                        inferredType = initializerOpt.Type;
                    }
                }

                // If we got a valid result that was not void then use the inferred type
                // else create an error type.

                if ((object)inferredType != null &&
                    inferredType.SpecialType != SpecialType.System_Void)
                {
                    declType = inferredType;
                }
                else
                {
                    declType = typeBinder.CreateErrorType("var");
                }
            }

            Debug.Assert((object)declType != null);

            diagnostics.Free();
            return(declType);
        }
Пример #3
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                // Since initializer might use Out Variable Declarations and Pattern Variable Declarations, we need to find
                // the right binder to use for the initializer.
                // Climb up the syntax tree looking for a first binder that we can find, but stop at the first statement syntax.
                CSharpSyntaxNode currentNode = _initializer;
                Binder           initializerBinder;

                do
                {
                    initializerBinder = this.binder.GetBinder(currentNode);

                    if (initializerBinder != null || currentNode is StatementSyntax)
                    {
                        break;
                    }

                    currentNode = currentNode.Parent;
                }while (currentNode != null);

#if DEBUG
                Binder parentBinder = initializerBinder;

                while (parentBinder != null)
                {
                    if (parentBinder == this.binder)
                    {
                        break;
                    }

                    parentBinder = parentBinder.Next;
                }

                Debug.Assert(parentBinder != null);
#endif

                var newBinder      = new ImplicitlyTypedLocalBinder(initializerBinder ?? this.binder, this);
                var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, RefKind, _initializer, _initializer);
                if (initializerOpt != null)
                {
                    return(initializerOpt.Type);
                }

                return(null);
            }
Пример #4
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                // Try binding enclosing deconstruction-declaration (the top-level VariableDeclaration), this should force the inference.
                SyntaxNode topLevelVariableDeclaration;
                bool       isDeconstruction = SyntaxFacts.IsDeconstructionIdentifier(IdentifierToken, out topLevelVariableDeclaration);

                Debug.Assert(isDeconstruction);
                Debug.Assert(((VariableDeclarationSyntax)topLevelVariableDeclaration).IsDeconstructionDeclaration);

                var statement = topLevelVariableDeclaration.Parent;

                switch (statement.Kind())
                {
                case SyntaxKind.LocalDeclarationStatement:
                    var localDecl      = (LocalDeclarationStatementSyntax)statement;
                    var localBinder    = this.binder.GetBinder(localDecl);
                    var newLocalBinder = new ImplicitlyTypedLocalBinder(localBinder, this);
                    newLocalBinder.BindDeconstructionDeclaration(localDecl, localDecl.Declaration, diagnostics);
                    break;

                case SyntaxKind.ForStatement:
                    var forStatement = (ForStatementSyntax)statement;
                    var forBinder    = this.binder.GetBinder(forStatement);
                    var newForBinder = new ImplicitlyTypedLocalBinder(forBinder, this);
                    newForBinder.BindDeconstructionDeclaration(forStatement.Declaration, forStatement.Declaration, diagnostics);
                    break;

                case SyntaxKind.ForEachStatement:
                    var foreachBinder = this.binder.GetBinder((ForEachStatementSyntax)statement);
                    foreachBinder.BindForEachDeconstruction(diagnostics, foreachBinder);
                    break;

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

                TypeSymbol result = this._type;

                Debug.Assert((object)result != null);
                return(result);
            }
Пример #5
0
        private TypeSymbol GetTypeSymbol()
        {
            var diagnostics = DiagnosticBag.GetInstance();

            Binder typeBinder = this.binder;

            bool isVar;
            TypeSymbol declType = typeBinder.BindType(typeSyntax, diagnostics, out isVar);

            if (isVar)
            {
                TypeSymbol inferredType = null;

                if (this.DeclarationKind == LocalDeclarationKind.ForEach)
                {
                    // Normally, it would not be safe to cast to a specific binder type.  However, we verified the type
                    // in the factory method call for this symbol.
                    inferredType = ((ForEachLoopBinder)typeBinder).InferCollectionElementType(diagnostics, collection);
                }
                else if (initializer != null)
                {
                    var newBinder = new ImplicitlyTypedLocalBinder(typeBinder, this);
                    var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, initializer, initializer);
                    if (initializerOpt != null)
                    {
                        inferredType = initializerOpt.Type;
                    }
                }

                // If we got a valid result that was not void then use the inferred type
                // else create an error type.

                if ((object)inferredType != null &&
                    inferredType.SpecialType != SpecialType.System_Void)
                {
                    declType = inferredType;
                }
                else
                {
                    declType = typeBinder.CreateErrorType("var");
                }
            }

            Debug.Assert((object)declType != null);

            diagnostics.Free();
            return declType;
        }
Пример #6
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                // Since initializer might use Out Variable Declarations and Pattern Variable Declarations, we need to find 
                // the right binder to use for the initializer.
                // Climb up the syntax tree looking for a first binder that we can find, but stop at the first statement syntax.
                CSharpSyntaxNode currentNode = _initializer;
                Binder initializerBinder;

                do
                {
                    initializerBinder = this.binder.GetBinder(currentNode);

                    if (initializerBinder != null || currentNode is StatementSyntax)
                    {
                        break;
                    }

                    currentNode = currentNode.Parent;   
                }
                while (currentNode != null);

#if DEBUG
                Binder parentBinder = initializerBinder;

                while (parentBinder != null)
                {
                    if (parentBinder == this.binder)
                    {
                        break;
                    }

                    parentBinder = parentBinder.Next;
                }

                Debug.Assert(parentBinder != null);
#endif 

                var newBinder = new ImplicitlyTypedLocalBinder(initializerBinder ?? this.binder, this);
                var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, RefKind, _initializer, _initializer);
                if (initializerOpt != null)
                {
                    return initializerOpt.Type;
                }

                return null;
            }
Пример #7
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                // Try binding enclosing deconstruction-declaration (the top-level VariableDeclaration), this should force the inference.
                SyntaxNode statement;
                bool isDeconstruction = SyntaxFacts.IsDeconstructionIdentifier(IdentifierToken, out statement);
                Debug.Assert(isDeconstruction);

                switch (statement.Kind())
                {
                    case SyntaxKind.DeconstructionDeclarationStatement:
                        var localDecl = (DeconstructionDeclarationStatementSyntax)statement;
                        var localBinder = this.binder.GetBinder(localDecl);
                        var newLocalBinder = new ImplicitlyTypedLocalBinder(localBinder, this);
                        newLocalBinder.BindDeconstructionDeclaration(localDecl, localDecl.Assignment.VariableComponent, localDecl.Assignment.Value, diagnostics);
                        break;

                    case SyntaxKind.ForStatement:
                        var forStatement = (ForStatementSyntax)statement;
                        var forBinder = this.binder.GetBinder(forStatement);
                        var newForBinder = new ImplicitlyTypedLocalBinder(forBinder, this);
                        newForBinder.BindDeconstructionDeclaration(forStatement, forStatement.Deconstruction.VariableComponent, forStatement.Deconstruction.Value, diagnostics);
                        break;

                    case SyntaxKind.ForEachComponentStatement:
                        var foreachBinder = this.binder.GetBinder((ForEachComponentStatementSyntax)statement);
                        foreachBinder.BindForEachDeconstruction(diagnostics, foreachBinder);
                        break;

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

                TypeSymbol result = this._type;
                Debug.Assert((object)result != null);
                return result;
            }
Пример #8
0
            protected override TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
            {
                var newBinder = new ImplicitlyTypedLocalBinder(this.binder, this);
                var initializerOpt = newBinder.BindInferredVariableInitializer(diagnostics, initializer, initializer);
                if (initializerOpt != null)
                {
                    return initializerOpt.Type;
        }

                return null;
            }