Ejemplo n.º 1
0
    /// <summary>
    /// This method is provided as a convenience for testing the SemanticModel.GetDeclaredSymbol implementation.
    /// </summary>
    /// <param name="declaration">This parameter will be type checked, and a NotSupportedException will be thrown if the type is not currently supported by an overload of GetDeclaredSymbol.</param>
    internal static Symbol GetDeclaredSymbolFromSyntaxNode(this CSharpSemanticModel model, Microsoft.CodeAnalysis.SyntaxNode declaration, CancellationToken cancellationToken = default(CancellationToken))
    {
        // NOTE: Do not add types to this condition unless you have verified that there is an overload of SemanticModel.GetDeclaredSymbol
        //       that supports the type you're adding.
        if (!(
                declaration is AnonymousObjectCreationExpressionSyntax ||
                declaration is AnonymousObjectMemberDeclaratorSyntax ||
                declaration is BaseTypeDeclarationSyntax ||
                declaration is CatchDeclarationSyntax ||
                declaration is ExternAliasDirectiveSyntax ||
                declaration is ForEachStatementSyntax ||
                declaration is JoinIntoClauseSyntax ||
                declaration is LabeledStatementSyntax ||
                declaration is MemberDeclarationSyntax ||
                declaration is NamespaceDeclarationSyntax ||
                declaration is ParameterSyntax ||
                declaration is QueryClauseSyntax ||
                declaration is QueryContinuationSyntax ||
                declaration is SwitchLabelSyntax ||
                declaration is TypeParameterSyntax ||
                declaration is UsingDirectiveSyntax ||
                declaration is VariableDeclaratorSyntax))
        {
            throw new NotSupportedException("This node type is not supported.");
        }

        return((Symbol)model.GetDeclaredSymbol(declaration, cancellationToken));
    }
Ejemplo n.º 2
0
        public void NullCheckedDiscard()
        {
            var source = @"
using System;
class C
{
    public void M()
    {
        Func<string, int> func1 = (_!!) => 42;
    }
}";
            var tree   = Parse(source, options: TestOptions.RegularPreview);
            var comp   = CreateCompilation(tree);

            comp.VerifyDiagnostics();

            CSharpSemanticModel model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
            ParenthesizedLambdaExpressionSyntax node = comp.GlobalNamespace.GetTypeMember("C")
                                                       .GetMember <SourceMethodSymbol>("M")
                                                       .GetNonNullSyntaxNode()
                                                       .DescendantNodes()
                                                       .OfType <ParenthesizedLambdaExpressionSyntax>()
                                                       .Single();
            var methodSymbol = (IMethodSymbol)model.GetSymbolInfo(node).Symbol !;

            Assert.True(methodSymbol.Parameters[0].IsNullChecked);
        }
 private SpeculativeSyntaxTreeSemanticModel(CSharpSemanticModel parentSemanticModel, CSharpSyntaxNode root, Binder rootBinder, int position, SpeculativeBindingOption bindingOption)
     : base(parentSemanticModel.Compilation, parentSemanticModel.SyntaxTree, root.SyntaxTree)
 {
     this.parentSemanticModel = parentSemanticModel;
     this.root          = root;
     this.rootBinder    = rootBinder;
     this.position      = position;
     this.bindingOption = bindingOption;
 }
        private static SpeculativeSyntaxTreeSemanticModel CreateCore(CSharpSemanticModel parentSemanticModel, CSharpSyntaxNode root, Binder rootBinder, int position, SpeculativeBindingOption bindingOption)
        {
            Debug.Assert(parentSemanticModel is SyntaxTreeSemanticModel);
            Debug.Assert(root != null);
            Debug.Assert(root is TypeSyntax || root is CrefSyntax);
            Debug.Assert(rootBinder != null);
            Debug.Assert(rootBinder.IsSemanticModelBinder);

            var speculativeModel = new SpeculativeSyntaxTreeSemanticModel(parentSemanticModel, root, rootBinder, position, bindingOption);

            return(speculativeModel);
        }
 public static SpeculativeSyntaxTreeSemanticModel Create(CSharpSemanticModel parentSemanticModel, CrefSyntax root, Binder rootBinder, int position)
 {
     return(CreateCore(parentSemanticModel, root, rootBinder, position, bindingOption: SpeculativeBindingOption.BindAsTypeOrNamespace));
 }
 public static SpeculativeSyntaxTreeSemanticModel Create(CSharpSemanticModel parentSemanticModel, TypeSyntax root, Binder rootBinder, int position, SpeculativeBindingOption bindingOption)
 {
     return(CreateCore(parentSemanticModel, root, rootBinder, position, bindingOption));
 }
Ejemplo n.º 7
0
        public static void GetExpressionSymbols(this BoundExpression node, ArrayBuilder <Symbol> symbols, BoundNode parent, Binder binder)
        {
            switch (node.Kind)
            {
            case BoundKind.MethodGroup:
                // Special case: if we are looking for info on "M" in "new Action(M)" in the context of a parent
                // then we want to get the symbol that overload resolution chose for M, not on the whole method group M.
                var delegateCreation = parent as BoundDelegateCreationExpression;
                if (delegateCreation != null && (object)delegateCreation.MethodOpt != null)
                {
                    symbols.Add(delegateCreation.MethodOpt);
                }
                else
                {
                    symbols.AddRange(CSharpSemanticModel.GetReducedAndFilteredMethodGroupSymbols(binder, (BoundMethodGroup)node));
                }
                break;

            case BoundKind.BadExpression:
                symbols.AddRange(((BoundBadExpression)node).Symbols);
                break;

            case BoundKind.DelegateCreationExpression:
                var expr = (BoundDelegateCreationExpression)node;
                var ctor = expr.Type.GetMembers(WellKnownMemberNames.InstanceConstructorName).FirstOrDefault();
                if ((object)ctor != null)
                {
                    symbols.Add(ctor);
                }
                break;

            case BoundKind.Call:
                // Either overload resolution succeeded for this call or it did not. If it did not
                // succeed then we've stashed the original method symbols from the method group,
                // and we should use those as the symbols displayed for the call. If it did succeed
                // then we did not stash any symbols; just fall through to the default case.

                var originalMethods = ((BoundCall)node).OriginalMethodsOpt;
                if (originalMethods.IsDefault)
                {
                    goto default;
                }
                symbols.AddRange(originalMethods);
                break;

            case BoundKind.IndexerAccess:
                // Same behavior as for a BoundCall: if overload resolution failed, pull out stashed candidates;
                // otherwise use the default behavior.

                var originalIndexers = ((BoundIndexerAccess)node).OriginalIndexersOpt;
                if (originalIndexers.IsDefault)
                {
                    goto default;
                }
                symbols.AddRange(originalIndexers);
                break;

            default:
                var symbol = node.ExpressionSymbol;
                if ((object)symbol != null)
                {
                    symbols.Add(symbol);
                }
                break;
            }
        }