private static TypeSyntax GenerateTypeSyntax(ITypeSymbol type, OptionSet options) { return(type.ContainsAnonymousType() || (true /*options.GetOption(CSharpCodeStyleOptions.UseVarWhenDeclaringLocals)*/ && type.TypeKind != TypeKind.Delegate) ? SyntaxFactory.IdentifierName("var") : type.GenerateTypeSyntax()); }
protected VariableSymbol(Compilation compilation, ITypeSymbol type) { OriginalTypeHadAnonymousTypeOrDelegate = type.ContainsAnonymousType(); OriginalType = OriginalTypeHadAnonymousTypeOrDelegate ? type.RemoveAnonymousTypes(compilation) : type; }
/// <summary> /// Adds to <paramref name="targetType"/> if it does not contain an anonymous /// type and binds to the same type at the given <paramref name="position"/>. /// </summary> public static ExpressionSyntax CastIfPossible( this ExpressionSyntax expression, ITypeSymbol targetType, int position, SemanticModel semanticModel, CancellationToken cancellationToken) { if (targetType.ContainsAnonymousType()) { return(expression); } if (targetType.IsSystemVoid()) { return(expression); } if (targetType.Kind == SymbolKind.DynamicType) { targetType = semanticModel.Compilation.GetSpecialType(SpecialType.System_Object); } var typeSyntax = targetType.GenerateTypeSyntax(); var type = semanticModel.GetSpeculativeTypeInfo( position, typeSyntax, SpeculativeBindingOption.BindAsTypeOrNamespace).Type; if (!targetType.Equals(type)) { return(expression); } var castExpression = expression.Cast(targetType); // Ensure that inserting the cast doesn't change the semantics. var specAnalyzer = new SpeculationAnalyzer(expression, castExpression, semanticModel, cancellationToken); var speculativeSemanticModel = specAnalyzer.SpeculativeSemanticModel; if (speculativeSemanticModel == null) { return(expression); } var speculatedCastExpression = (CastExpressionSyntax)specAnalyzer.ReplacedExpression; if (!CastSimplifier.IsUnnecessaryCast(speculatedCastExpression, speculativeSemanticModel, cancellationToken)) { return(expression); } return(castExpression); }
/// <summary> /// Adds to <paramref name="targetType"/> if it does not contain an anonymous /// type and binds to the same type at the given <paramref name="position"/>. /// </summary> public static ExpressionSyntax CastIfPossible( this ExpressionSyntax expression, ITypeSymbol targetType, int position, SemanticModel semanticModel) { if (targetType.ContainsAnonymousType()) { return expression; } if (targetType.Kind == SymbolKind.DynamicType) { targetType = semanticModel.Compilation.GetSpecialType(SpecialType.System_Object); } var typeSyntax = targetType.GenerateTypeSyntax(); var type = semanticModel.GetSpeculativeTypeInfo( position, typeSyntax, SpeculativeBindingOption.BindAsTypeOrNamespace).Type; if (!targetType.Equals(type)) { return expression; } var castExpression = expression.Cast(targetType); // Ensure that inserting the cast doesn't change the semantics. var specAnalyzer = new SpeculationAnalyzer(expression, castExpression, semanticModel, CancellationToken.None); var speculativeSemanticModel = specAnalyzer.SpeculativeSemanticModel; if (speculativeSemanticModel == null) { return expression; } var speculatedCastExpression = (CastExpressionSyntax)specAnalyzer.ReplacedExpression; if (!speculatedCastExpression.IsUnnecessaryCast(speculativeSemanticModel, CancellationToken.None)) { return expression; } return castExpression; }
private static TypeSyntax GenerateTypeSyntax(ITypeSymbol type, OptionSet options, ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken) { // if there isn't a semantic model, we cannot perform further analysis. if (semanticModel != null) { if (type.ContainsAnonymousType()) { return(SyntaxFactory.IdentifierName("var")); } if (type.TypeKind != TypeKind.Delegate && TypeStyleHelper.IsImplicitTypePreferred(expression, semanticModel, options, cancellationToken)) { return(SyntaxFactory.IdentifierName("var")); } } return(type.GenerateTypeSyntax()); }
private (ITypeSymbol typeSymbol, bool hasAnonymousType, bool awaitTaskReturn) AdjustReturnType(SemanticModel model, ITypeSymbol returnType) { // check whether return type contains anonymous type and if it does, fix it up by making it object var returnTypeHasAnonymousType = returnType.ContainsAnonymousType(); returnType = returnTypeHasAnonymousType ? returnType.RemoveAnonymousTypes(model.Compilation) : returnType; // if selection contains await which is not under async lambda or anonymous delegate, // change return type to be wrapped in Task var shouldPutAsyncModifier = SelectionResult.ShouldPutAsyncModifier(); if (shouldPutAsyncModifier) { WrapReturnTypeInTask(model, ref returnType, out var awaitTaskReturn); return(returnType, returnTypeHasAnonymousType, awaitTaskReturn); } // unwrap task if needed UnwrapTaskIfNeeded(model, ref returnType); return(returnType, returnTypeHasAnonymousType, false); }
static Task <IEnumerable <ISymbol> > GetPreselectedSymbolsWorker2(CSharpSyntaxContext context, ITypeSymbol type, CancellationToken cancellationToken) { // Unwrap an array type fully. We only want to offer the underlying element type in the // list of completion items. bool isArray = false; while (type is IArrayTypeSymbol) { isArray = true; type = ((IArrayTypeSymbol)type).ElementType; } if (type == null) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } // Unwrap nullable if (type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { type = type.GetTypeArguments().FirstOrDefault(); } if (type.SpecialType == SpecialType.System_Void) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } if (type.ContainsAnonymousType()) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } if (!type.CanBeReferencedByName) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } // Normally the user can't say things like "new IList". Except for "IList[] x = new |". // In this case we do want to allow them to preselect certain types in the completion // list even if they can't new them directly. if (!isArray) { if (type.TypeKind == TypeKind.Interface || type.TypeKind == TypeKind.Pointer || type.TypeKind == TypeKind.Dynamic || type.IsAbstract) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } if (type.TypeKind == TypeKind.TypeParameter && !((ITypeParameterSymbol)type).HasConstructorConstraint) { return(Task.FromResult(Enumerable.Empty <ISymbol> ())); } } // if (!type.IsEditorBrowsable(options.GetOption(RecommendationOptions.HideAdvancedMembers, context.SemanticModel.Language), context.SemanticModel.Compilation)) // { // return SpecializedTasks.EmptyEnumerable<ISymbol>(); // } // return(Task.FromResult(SpecializedCollections.SingletonEnumerable((ISymbol)type))); }
static Task<IEnumerable<ISymbol>> GetPreselectedSymbolsWorker2 (CSharpSyntaxContext context, ITypeSymbol type, CancellationToken cancellationToken) { // Unwrap an array type fully. We only want to offer the underlying element type in the // list of completion items. bool isArray = false; while (type is IArrayTypeSymbol) { isArray = true; type = ((IArrayTypeSymbol)type).ElementType; } if (type == null) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } // Unwrap nullable if (type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { type = type.GetTypeArguments ().FirstOrDefault (); } if (type.SpecialType == SpecialType.System_Void) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } if (type.ContainsAnonymousType ()) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } if (!type.CanBeReferencedByName) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } // Normally the user can't say things like "new IList". Except for "IList[] x = new |". // In this case we do want to allow them to preselect certain types in the completion // list even if they can't new them directly. if (!isArray) { if (type.TypeKind == TypeKind.Interface || type.TypeKind == TypeKind.Pointer || type.TypeKind == TypeKind.Dynamic || type.IsAbstract) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } if (type.TypeKind == TypeKind.TypeParameter && !((ITypeParameterSymbol)type).HasConstructorConstraint) { return Task.FromResult (Enumerable.Empty<ISymbol> ()); } } // if (!type.IsEditorBrowsable(options.GetOption(RecommendationOptions.HideAdvancedMembers, context.SemanticModel.Language), context.SemanticModel.Compilation)) // { // return SpecializedTasks.EmptyEnumerable<ISymbol>(); // } // return Task.FromResult (SpecializedCollections.SingletonEnumerable ((ISymbol)type)); }