Esempio n. 1
0
            private static Binder MakeNameBinder(bool isParameter, bool isTypeParameterRef, Symbol memberSymbol, CSharpCompilation compilation, SyntaxTree syntaxTree)
            {
                Binder binder = new BuckStopsHereBinder(compilation, syntaxTree);

                // All binders should have a containing symbol.
                Symbol containingSymbol = memberSymbol.ContainingSymbol;

                Debug.Assert((object)containingSymbol != null);
                binder = binder.WithContainingMemberOrLambda(containingSymbol);

                if (isParameter)
                {
                    ImmutableArray <ParameterSymbol> parameters = ImmutableArray <ParameterSymbol> .Empty;

                    switch (memberSymbol.Kind)
                    {
                    case SymbolKind.Method:
                        parameters = ((MethodSymbol)memberSymbol).Parameters;
                        break;

                    case SymbolKind.Property:
                        parameters = ((PropertySymbol)memberSymbol).Parameters;
                        break;

                    case SymbolKind.NamedType:
                    case SymbolKind.ErrorType:
                        NamedTypeSymbol typeSymbol = (NamedTypeSymbol)memberSymbol;
                        if (typeSymbol.IsDelegateType())
                        {
                            parameters = typeSymbol.DelegateInvokeMethod.Parameters;
                        }
                        break;
                    }

                    if (parameters.Length > 0)
                    {
                        binder = new WithParametersBinder(parameters, binder);
                    }
                }
                else
                {
                    Symbol currentSymbol = memberSymbol;
                    do
                    {
                        switch (currentSymbol.Kind)
                        {
                        case SymbolKind.NamedType:     // Includes delegates.
                        case SymbolKind.ErrorType:
                            NamedTypeSymbol typeSymbol = (NamedTypeSymbol)currentSymbol;
                            if (typeSymbol.Arity > 0)
                            {
                                binder = new WithClassTypeParametersBinder(typeSymbol, binder);
                            }
                            break;

                        case SymbolKind.Method:
                            MethodSymbol methodSymbol = (MethodSymbol)currentSymbol;
                            if (methodSymbol.Arity > 0)
                            {
                                binder = new WithMethodTypeParametersBinder(methodSymbol, binder);
                            }
                            break;
                        }
                        currentSymbol = currentSymbol.ContainingSymbol;
                    } while (isTypeParameterRef && !(currentSymbol is null));
                }

                return(binder);
            }
Esempio n. 2
0
        public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
        {
            var body = (CSharpSyntaxNode)node.Body ?? node.ExpressionBody;
            LocalFunctionSymbol match = null;
            // Don't use LookupLocalFunction because it recurses up the tree, as it
            // should be defined in the directly enclosing block (see note below)

            Binder possibleScopeBinder = _enclosing;
            while (possibleScopeBinder != null && !possibleScopeBinder.IsLocalFunctionsScopeBinder)
            {
                possibleScopeBinder = possibleScopeBinder.Next;
            }

            if (possibleScopeBinder != null)
            {
                foreach (var candidate in possibleScopeBinder.LocalFunctions)
                {
                    if (candidate.Locations[0] == node.Identifier.GetLocation())
                    {
                        match = candidate;
                    }
                }
            }

            bool oldSawYield = _sawYield;
            _sawYield = false;

            if (match != null)
            {
                var oldMethod = _containingMemberOrLambda;
                _containingMemberOrLambda = match;
                Binder addToMap;
                if (match.IsGenericMethod)
                {
                    addToMap = new WithMethodTypeParametersBinder(match, _enclosing);
                }
                else
                {
                    addToMap = _enclosing;
                }

                AddToMap(node, addToMap);

                if (body != null)
                {
                    Visit(body, new InMethodBinder(match, addToMap));
                }

                _containingMemberOrLambda = oldMethod;
            }
            else
            {
                // The enclosing block should have found this node and created a LocalFunctionMethodSymbol
                // The code that does so is in LocalScopeBinder.BuildLocalFunctions

                if (body != null)
                {
                    // do our best to attempt to bind
                    Visit(body);
                }
            }

            if (_sawYield)
            {
                _methodsWithYields.Add(body);
            }
            _sawYield = oldSawYield;
        }
            // NOTE: We're not sharing code with the BinderFactory visitor, because we already have the
            // member symbol in hand, which makes things much easier.
            private static Binder MakeNameBinder(bool isParameter, Symbol memberSymbol, CSharpCompilation compilation)
            {
                Binder binder = new BuckStopsHereBinder(compilation);

                // All binders should have a containing symbol.
                Symbol containingSymbol = memberSymbol.ContainingSymbol;
                Debug.Assert((object)containingSymbol != null);
                binder = binder.WithContainingMemberOrLambda(containingSymbol);

                if (isParameter)
                {
                    ImmutableArray<ParameterSymbol> parameters = ImmutableArray<ParameterSymbol>.Empty;

                    switch (memberSymbol.Kind)
                    {
                        case SymbolKind.Method:
                            parameters = ((MethodSymbol)memberSymbol).Parameters;
                            break;
                        case SymbolKind.Property:
                            parameters = ((PropertySymbol)memberSymbol).Parameters;
                            break;
                        case SymbolKind.NamedType:
                        case SymbolKind.ErrorType:
                            NamedTypeSymbol typeSymbol = (NamedTypeSymbol)memberSymbol;
                            if (typeSymbol.IsDelegateType())
                            {
                                parameters = typeSymbol.DelegateInvokeMethod.Parameters;
                            }
                            break;
                    }

                    if (parameters.Length > 0)
                    {
                        binder = new WithParametersBinder(parameters, binder);
                    }
                }
                else
                {
                    switch (memberSymbol.Kind)
                    {
                        case SymbolKind.NamedType: // Includes delegates.
                        case SymbolKind.ErrorType:
                            NamedTypeSymbol typeSymbol = (NamedTypeSymbol)memberSymbol;
                            if (typeSymbol.Arity > 0)
                            {
                                binder = new WithClassTypeParametersBinder(typeSymbol, binder);
                            }
                            break;
                        case SymbolKind.Method:
                            MethodSymbol methodSymbol = (MethodSymbol)memberSymbol;
                            if (methodSymbol.Arity > 0)
                            {
                                binder = new WithMethodTypeParametersBinder(methodSymbol, binder);
                            }
                            break;
                    }
                }

                return binder;
            }
Esempio n. 4
0
        public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
        {
            var body = (CSharpSyntaxNode)node.Body ?? node.ExpressionBody;
            LocalFunctionSymbol match = null;
            // Don't use LookupLocalFunction because it recurses up the tree, as it
            // should be defined in the directly enclosing block (see note below)

            Binder possibleScopeBinder = _enclosing;

            while (possibleScopeBinder != null && !possibleScopeBinder.IsLocalFunctionsScopeBinder)
            {
                possibleScopeBinder = possibleScopeBinder.Next;
            }

            if (possibleScopeBinder != null)
            {
                foreach (var candidate in possibleScopeBinder.LocalFunctions)
                {
                    if (candidate.Locations[0] == node.Identifier.GetLocation())
                    {
                        match = candidate;
                    }
                }
            }

            bool oldSawYield = _sawYield;

            _sawYield = false;

            if (match != null)
            {
                var oldMethod = _containingMemberOrLambda;
                _containingMemberOrLambda = match;
                Binder addToMap;
                if (match.IsGenericMethod)
                {
                    addToMap = new WithMethodTypeParametersBinder(match, _enclosing);
                }
                else
                {
                    addToMap = _enclosing;
                }

                AddToMap(node, addToMap);

                if (body != null)
                {
                    Visit(body, new InMethodBinder(match, addToMap));
                }

                _containingMemberOrLambda = oldMethod;
            }
            else
            {
                // The enclosing block should have found this node and created a LocalFunctionMethodSymbol
                // The code that does so is in LocalScopeBinder.BuildLocalFunctions

                if (body != null)
                {
                    // do our best to attempt to bind
                    Visit(body);
                }
            }

            if (_sawYield)
            {
                _methodsWithYields.Add(body);
            }
            _sawYield = oldSawYield;
        }