protected override ITypeSymbol DetermineReturnTypeForSimpleNameOrMemberAccessExpression( ITypeInferenceService typeInferenceService, SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken ) { if ( semanticModel.SyntaxTree.IsNameOfContext( expression.SpanStart, semanticModel, cancellationToken ) ) { return(typeInferenceService.InferType( semanticModel, expression, true, cancellationToken )); } return(null); }
private static ITypeSymbol GetPropertyType( SimpleNameSyntax propertyName, SemanticModel semanticModel, ITypeInferenceService typeInference, CancellationToken cancellationToken ) { if (propertyName.Parent is AssignmentExpressionSyntax parentAssignment) { return(typeInference.InferType( semanticModel, parentAssignment.Left, objectAsDefault: true, cancellationToken: cancellationToken )); } if (propertyName.Parent is IsPatternExpressionSyntax isPatternExpression) { return(typeInference.InferType( semanticModel, isPatternExpression.Expression, objectAsDefault: true, cancellationToken: cancellationToken )); } return(null); }
public MappingPage() : base("Fields mapping") { // This call is required by the Windows Form Designer. InitializeComponent(); naming = ServiceRegistry.Instance[ typeof(INamingService) ] as INamingService; typeInference = ServiceRegistry.Instance[ typeof(ITypeInferenceService) ] as ITypeInferenceService; }
public MappingPage() : base("Fields mapping") { // This call is required by the Windows Form Designer. InitializeComponent(); naming = ServiceRegistry.Instance[typeof(INamingService)] as INamingService; typeInference = ServiceRegistry.Instance[typeof(ITypeInferenceService)] as ITypeInferenceService; }
public void CorrectInference() { Kernel.AddComponent( "typeinf", typeof(ITypeInferenceService), typeof(TypeInferenceService) ); ITypeInferenceService typeInf = Kernel[ typeof(ITypeInferenceService) ] as ITypeInferenceService; Assert.AreEqual( typeof(String), typeInf.ConvertOleType( OleDbType.VarChar ) ); Assert.AreEqual( typeof(int), typeInf.ConvertOleType( OleDbType.Integer ) ); }
private IEnumerable <INamedTypeSymbol>?FindNearestTupleConstructionWithInferrableType( SyntaxNode root, SemanticModel semanticModel, int position, SignatureHelpTriggerInfo triggerInfo, ITypeInferenceService typeInferrer, ISyntaxFactsService syntaxFacts, CancellationToken cancellationToken, out ExpressionSyntax?targetExpression ) { // Walk upward through TupleExpressionSyntax/ParenthsizedExpressionSyntax looking for a // place where we can infer a tuple type. ParenthesizedExpressionSyntax?parenthesizedExpression = null; while ( TryGetTupleExpression( triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out var tupleExpression ) || TryGetParenthesizedExpression( triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out parenthesizedExpression ) ) { targetExpression = (ExpressionSyntax?)tupleExpression ?? parenthesizedExpression; var inferredTypes = typeInferrer.InferTypes( semanticModel, targetExpression !.SpanStart, cancellationToken ); var tupleTypes = inferredTypes .Where(t => t.IsTupleType) .OfType <INamedTypeSymbol>() .ToList(); if (tupleTypes.Any()) { return(tupleTypes); } position = targetExpression.GetFirstToken().SpanStart; } targetExpression = null; return(null); }
private bool IsImplicitArrayCreation(SemanticModel semanticModel, SyntaxToken token, int position, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { if (token.IsKind(SyntaxKind.NewKeyword) && token.Parent.IsKind(SyntaxKind.ObjectCreationExpression)) { var type = typeInferrer.InferType(semanticModel, token.Parent, objectAsDefault: false, cancellationToken: cancellationToken); return type != null && type is IArrayTypeSymbol; } return false; }
public static INamedTypeSymbol InferDelegateType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken) { var type = typeInferenceService.InferType(semanticModel, expression, objectAsDefault: false, cancellationToken: cancellationToken); return(type.GetDelegateType(semanticModel.Compilation)); }
public static INamedTypeSymbol InferDelegateType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, int position, CancellationToken cancellationToken) { var types = typeInferenceService.InferTypes(semanticModel, position, cancellationToken); return(GetFirstDelegateType(semanticModel, types)); }
public static ITypeSymbol?InferType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, SyntaxNode expression, bool objectAsDefault, CancellationToken cancellationToken) { return(InferType( typeInferenceService, semanticModel, expression, objectAsDefault, name: null, cancellationToken: cancellationToken)); }
public static INamedTypeSymbol InferDelegateType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken) { var types = typeInferenceService.InferTypes(semanticModel, expression, cancellationToken); var delegateTypes = types.Select(t => t.GetDelegateType(semanticModel.Compilation)); return(delegateTypes.WhereNotNull().FirstOrDefault()); }
public static ITypeSymbol InferType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, int position, bool objectAsDefault, CancellationToken cancellationToken) { return(InferType( typeInferenceService, semanticModel, position, objectAsDefault, nameOpt: null, cancellationToken: cancellationToken)); }
public static ImmutableArray <TypeInferenceInfo> GetTypeInferenceInfo( this ITypeInferenceService service, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken ) => service.GetTypeInferenceInfo( semanticModel, expression, nameOpt: null, cancellationToken: cancellationToken );
public static ImmutableArray <ITypeSymbol> InferTypes( this ITypeInferenceService service, SemanticModel semanticModel, int position, CancellationToken cancellationToken ) => service.InferTypes( semanticModel, position, nameOpt: null, cancellationToken: cancellationToken );
private ITypeSymbol GetPropertyType( SimpleNameSyntax property, SemanticModel semanticModel, ITypeInferenceService typeInference, CancellationToken cancellationToken) { var parent = property.Parent as AssignmentExpressionSyntax; if (parent != null) { return(typeInference.InferType(semanticModel, parent.Left, true, cancellationToken)); } return(null); }
public static ITypeSymbol InferType(this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, int position, bool objectAsDefault, CancellationToken cancellationToken) { var types = typeInferenceService.InferTypes(semanticModel, position, cancellationToken) .WhereNotNull(); if (!types.Any()) { return(objectAsDefault ? semanticModel.Compilation.ObjectType : null); } return(types.FirstOrDefault()); }
public static ITypeSymbol InferType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, int position, bool objectAsDefault, string nameOpt, CancellationToken cancellationToken) { var types = typeInferenceService.InferTypes(semanticModel, position, nameOpt, cancellationToken); if (types.Length == 0) { return(objectAsDefault ? semanticModel.Compilation.ObjectType : null); } return(types.FirstOrDefault()); }
public static ITypeSymbol?InferType( this ITypeInferenceService typeInferenceService, SemanticModel semanticModel, SyntaxNode expression, bool objectAsDefault, string?name, CancellationToken cancellationToken) { var types = typeInferenceService.InferTypes(semanticModel, expression, name, cancellationToken); if (types.Length == 0) { return(objectAsDefault ? semanticModel.Compilation.ObjectType : null); } return(types.First()); }
internal override bool TryGenerateProperty( SimpleNameSyntax propertyName, SemanticModel semanticModel, ITypeInferenceService typeInference, CancellationToken cancellationToken, out IPropertySymbol property) { property = null; var propertyType = GetPropertyType(propertyName, semanticModel, typeInference, cancellationToken); if (propertyType == null || propertyType is IErrorTypeSymbol) { property = CreatePropertySymbol(propertyName, semanticModel.Compilation.ObjectType); return(true); } property = CreatePropertySymbol(propertyName, propertyType); return(true); }
private static bool IsPossibleOutVariableDeclaration(SyntaxToken token, SemanticModel semanticModel, int position, ITypeInferenceService typeInferenceService, CancellationToken cancellationToken, out NameDeclarationInfo result) { if (!token.IsKind(SyntaxKind.IdentifierToken) || !(token.Parent.IsKind(SyntaxKind.IdentifierName))) { result = default; return(false); } var argument = token.Parent.Parent as ArgumentSyntax // var is child of ArgumentSyntax, eg. Goo(out var $$ ?? token.Parent.Parent.Parent as ArgumentSyntax; // var is child of DeclarationExpression // under ArgumentSyntax, eg. Goo(out var a$$ if (argument == null || !argument.RefOrOutKeyword.IsKind(SyntaxKind.OutKeyword)) { result = default; return(false); } var type = typeInferenceService.InferType(semanticModel, argument.SpanStart, objectAsDefault: false, cancellationToken: cancellationToken); if (type != null) { result = new NameDeclarationInfo( ImmutableArray.Create(SymbolKind.Local), Accessibility.NotApplicable, new DeclarationModifiers(), type, alias: null); return(true); } result = default; return(false); }
protected abstract ITypeSymbol DetermineReturnTypeForSimpleNameOrMemberAccessExpression(ITypeInferenceService typeInferenceService, SemanticModel semanticModel, TExpressionSyntax expression, CancellationToken cancellationToken);
internal abstract bool TryGenerateProperty(TSimpleNameSyntax propertyName, SemanticModel semanticModel, ITypeInferenceService typeInference, CancellationToken cancellationToken, out IPropertySymbol property);
IEnumerable<INamedTypeSymbol> FindNearestTupleConstructionWithInferrableType(SyntaxNode root, SemanticModel semanticModel, int position, SignatureHelpTriggerInfo triggerInfo, ITypeInferenceService typeInferrer, ISyntaxFactsService syntaxFacts, CancellationToken cancellationToken, out ExpressionSyntax targetExpression) { // Walk upward through TupleExpressionSyntax/ParenthsizedExpressionSyntax looking for a // place where we can infer a tuple type. ParenthesizedExpressionSyntax parenthesizedExpression = null; while (TryGetTupleExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out var tupleExpression) || TryGetParenthesizedExpression(triggerInfo.TriggerReason, root, position, syntaxFacts, cancellationToken, out parenthesizedExpression)) { targetExpression = (ExpressionSyntax)tupleExpression ?? parenthesizedExpression; var inferredTypes = typeInferrer.InferTypes(semanticModel, targetExpression.SpanStart, cancellationToken); var tupleTypes = inferredTypes.Where(t => t.IsTupleType).OfType<INamedTypeSymbol>().ToList(); if (tupleTypes.Any()) { return tupleTypes; } position = targetExpression.GetFirstToken().SpanStart; } targetExpression = null; return null; }
internal PythiaTypeInferenceServiceWrapper(ITypeInferenceService underlyingObject) => UnderlyingObject = underlyingObject;
private static bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { // Not after `new` if (token.IsKind(SyntaxKind.NewKeyword) && token.Parent.IsKind(SyntaxKind.ObjectCreationExpression)) { return(false); } // Typing a generic type parameter, the tree might look like a binary expression around the < token. // If we infer a delegate type here (because that's what on the other side of the binop), // ignore it. if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax) { return(false); } // We might be in the arguments to a parenthesized lambda if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken) { if (token.Parent != null && token.Parent is ParameterListSyntax) { return(token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax); } } // A lambda that is being typed may be parsed as a tuple without names // For example, "(a, b" could be the start of either a tuple or lambda // But "(a: b, c" cannot be a lambda if (token.SyntaxTree.IsPossibleTupleContext(token, position) && token.Parent.IsKind(SyntaxKind.TupleExpression, out TupleExpressionSyntax tupleExpression) && !tupleExpression.HasNames()) { position = token.Parent.SpanStart; } // Walk up a single level to allow for typing the beginning of a lambda: // new AssemblyLoadEventHandler(($$ if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.ParenthesizedExpression) { position = token.Parent.SpanStart; } // WorkItem 834609: Automatic brace completion inserts the closing paren, making it // like a cast. if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.CastExpression) { position = token.Parent.SpanStart; } // In the following situation, the type inferrer will infer Task to support target type preselection // Action a = Task.$$ // We need to explicitly exclude invocation/member access from suggestion mode var previousToken = token.GetPreviousTokenIfTouchingWord(position); if (previousToken.IsKind(SyntaxKind.DotToken) && previousToken.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression)) { return(false); } // async lambda: // Goo(async($$ // Goo(async(p1, $$ if (token.IsKind(SyntaxKind.OpenParenToken, SyntaxKind.CommaToken) && token.Parent.IsKind(SyntaxKind.ArgumentList) && token.Parent.Parent is InvocationExpressionSyntax invocation && invocation.Expression is IdentifierNameSyntax identifier) { if (identifier.Identifier.IsKindOrHasMatchingText(SyntaxKind.AsyncKeyword)) { return(true); } } // If we're an argument to a function with multiple overloads, // open the builder if any overload takes a delegate at our argument position var inferredTypeInfo = typeInferrer.GetTypeInferenceInfo(semanticModel, position, cancellationToken: cancellationToken); return(inferredTypeInfo.Any(type => GetDelegateType(type, semanticModel.Compilation).IsDelegateType())); }
private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { // Typing a generic type parameter, the tree might look like a binary expression around the < token. // If we infer a delegate type here (because that's what on the other side of the binop), // ignore it. if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax) { return false; } // We might be in the arguments to a parenthsized lambda if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken) { if (token.Parent != null && token.Parent is ParameterListSyntax) { return token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax; } } // Walk up a single level to allow for typing the beginning of a lambda: // new AssemblyLoadEventHandler(($$ if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.ParenthesizedExpression) { position = token.Parent.SpanStart; } // WorkItem 834609: Automatic brace completion inserts the closing paren, making it // like a cast. if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.CastExpression) { position = token.Parent.SpanStart; } // If we're an argument to a function with multiple overloads, // open the builder if any overload takes a delegate at our argument position var inferredTypes = typeInferrer.InferTypes(semanticModel, position, cancellationToken: cancellationToken); return inferredTypes.Any(type => type.GetDelegateType(semanticModel.Compilation).IsDelegateType()); }
protected abstract ITypeSymbol CanGenerateMethodForSimpleNameOrMemberAccessExpression(ITypeInferenceService typeInferenceService, SemanticModel semanticModel, TExpressionSyntax expression, CancellationToken cancellationToken);
private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { // Typing a generic type parameter, the tree might look like a binary expression around the < token. // If we infer a delegate type here (because that's what on the other side of the binop), // ignore it. if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax) { return(false); } // We might be in the arguments to a parenthesized lambda if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken) { if (token.Parent != null && token.Parent is ParameterListSyntax) { return(token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax); } } // Walk up a single level to allow for typing the beginning of a lambda: // new AssemblyLoadEventHandler(($$ if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.ParenthesizedExpression) { position = token.Parent.SpanStart; } // WorkItem 834609: Automatic brace completion inserts the closing paren, making it // like a cast. if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.CastExpression) { position = token.Parent.SpanStart; } // If we're an argument to a function with multiple overloads, // open the builder if any overload takes a delegate at our argument position var inferredTypes = typeInferrer.InferTypes(semanticModel, position, cancellationToken: cancellationToken); return(inferredTypes.Any(type => type.GetDelegateType(semanticModel.Compilation).IsDelegateType())); }
public PlainFieldInferenceService(INamingService namingService, ITypeInferenceService typeInfService) { _namingService = namingService; _typeInfService = typeInfService; }
private bool IsImplicitArrayCreation(SemanticModel semanticModel, SyntaxToken token, int position, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { if (token.IsKind(SyntaxKind.NewKeyword) && token.Parent.IsKind(SyntaxKind.ObjectCreationExpression)) { var type = typeInferrer.InferType(semanticModel, token.Parent, objectAsDefault: false, cancellationToken: cancellationToken); return(type != null && type is IArrayTypeSymbol); } return(false); }
public PlainFieldInferenceService(INamingService namingService, ITypeInferenceService typeInfService) { _namingService = namingService; _typeInfService = typeInfService; }
private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { // Typing a generic type parameter, the tree might look like a binary expression around the < token. // If we infer a delegate type here (because that's what on the other side of the binop), // ignore it. if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax) { return false; } // We might be in the arguments to a parenthesized lambda if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken) { if (token.Parent != null && token.Parent is ParameterListSyntax) { return token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax; } } // A lambda that is being typed may be parsed as a tuple without names // For example, "(a, b" could be the start of either a tuple or lambda // But "(a: b, c" cannot be a lambda if (token.SyntaxTree.IsPossibleTupleContext(token, position) && token.Parent.IsKind(SyntaxKind.TupleExpression) && !((TupleExpressionSyntax)token.Parent).HasNames()) { position = token.Parent.SpanStart; } // Walk up a single level to allow for typing the beginning of a lambda: // new AssemblyLoadEventHandler(($$ if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.ParenthesizedExpression) { position = token.Parent.SpanStart; } // WorkItem 834609: Automatic brace completion inserts the closing paren, making it // like a cast. if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.CastExpression) { position = token.Parent.SpanStart; } // If we're an argument to a function with multiple overloads, // open the builder if any overload takes a delegate at our argument position var inferredTypeInfo = typeInferrer.GetTypeInferenceInfo(semanticModel, position, cancellationToken: cancellationToken); return inferredTypeInfo.Any(type => GetDelegateType(type, semanticModel.Compilation).IsDelegateType()); }
private bool IsLambdaExpression(SemanticModel semanticModel, int position, SyntaxToken token, ITypeInferenceService typeInferrer, CancellationToken cancellationToken) { // Typing a generic type parameter, the tree might look like a binary expression around the < token. // If we infer a delegate type here (because that's what on the other side of the binop), // ignore it. if (token.Kind() == SyntaxKind.LessThanToken && token.Parent is BinaryExpressionSyntax) { return(false); } // We might be in the arguments to a parenthesized lambda if (token.Kind() == SyntaxKind.OpenParenToken || token.Kind() == SyntaxKind.CommaToken) { if (token.Parent != null && token.Parent is ParameterListSyntax) { return(token.Parent.Parent != null && token.Parent.Parent is ParenthesizedLambdaExpressionSyntax); } } // A lambda that is being typed may be parsed as a tuple without names // For example, "(a, b" could be the start of either a tuple or lambda // But "(a: b, c" cannot be a lambda if (token.IsPossibleTupleElementNameContext(position) && token.Parent.IsKind(SyntaxKind.TupleExpression) && !((TupleExpressionSyntax)token.Parent).HasNames()) { position = token.Parent.SpanStart; } // Walk up a single level to allow for typing the beginning of a lambda: // new AssemblyLoadEventHandler(($$ if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.ParenthesizedExpression) { position = token.Parent.SpanStart; } // WorkItem 834609: Automatic brace completion inserts the closing paren, making it // like a cast. if (token.Kind() == SyntaxKind.OpenParenToken && token.Parent.Kind() == SyntaxKind.CastExpression) { position = token.Parent.SpanStart; } // If we're an argument to a function with multiple overloads, // open the builder if any overload takes a delegate at our argument position var inferredTypeInfo = typeInferrer.GetTypeInferenceInfo(semanticModel, position, cancellationToken: cancellationToken); return(inferredTypeInfo.Any(type => GetDelegateType(type, semanticModel.Compilation).IsDelegateType())); }