static public YType GetYType(this TypeSyntax typeSyntax) { if (typeSyntax.IsKind(SyntaxKind.PredefinedType)) { var predefinedTypeKeywoard = ((PredefinedTypeSyntax)typeSyntax).Keyword; if (predefinedTypeKeywoard.IsKind(SyntaxKind.IntKeyword)) { return(YType.Int); } else if (predefinedTypeKeywoard.IsKind(SyntaxKind.VoidKeyword)) { return(YType.Void); } } else if (typeSyntax.IsKind(SyntaxKind.IdentifierName)) { var identifierToken = (IdentifierNameSyntax)typeSyntax; // other types are references.. for now return(new YRefType(identifierToken.GetName())); } throw new TException("Unsupported return type"); }
private static bool AppendQualifiedSymbolName(StringBuilder builder, ISymbol symbol, TypeSyntax type) { switch (symbol.Kind) { case SymbolKind.ArrayType: var arraySymbol = (IArrayTypeSymbol)symbol; AppendQualifiedSymbolName(builder, arraySymbol.ElementType, GetElementSyntax(type)); builder .Append("[") .Append(',', arraySymbol.Rank - 1) .Append("]"); AppendNullableSuffixIfNeeded(builder, type); return(true); case SymbolKind.Namespace: var namespaceSymbol = (INamespaceSymbol)symbol; if (namespaceSymbol.IsGlobalNamespace) { return(false); } builder.Append(namespaceSymbol.ToDisplayString()); return(true); case SymbolKind.NamedType: var namedTypeSymbol = (INamedTypeSymbol)symbol; if (SpecialTypeHelper.TryGetPredefinedType(namedTypeSymbol.SpecialType, out var specialTypeSyntax) && (type?.IsKind(SyntaxKind.PredefinedType) == true || (type is NullableTypeSyntax nullable && nullable.ElementType.IsKind(SyntaxKind.PredefinedType)))) { // This handles these cases: int, int?, object, object? // But not these cases: System.Int32, System.Int32?, System.Object, System.Object? builder.Append(specialTypeSyntax.ToFullString()); AppendNullableSuffixIfNeeded(builder, type); return(true); } else if (namedTypeSymbol.IsTupleType()) { return(AppendTupleType(builder, namedTypeSymbol, type)); } else if (namedTypeSymbol.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T && type?.IsKind(SyntaxKind.NullableType) == true) { // This handles the case '(int, int)?' but not 'System.Nullable<(int, int)>' AppendQualifiedSymbolName(builder, namedTypeSymbol.TypeArguments[0], GetElementSyntax(type)); builder.Append("?"); return(true); } else { return(AppendNamedType(builder, namedTypeSymbol, type)); }
private static void Validate(SyntaxNodeAnalysisContext context, TypeSyntax syntax) { if (syntax.IsKind(SyntaxKind.TupleType) == false) { return; } var tts = (TupleTypeSyntax)syntax; foreach (var ttse in tts.Elements) { var identifier = ttse.Identifier; if (identifier.IsKind(SyntaxKind.None)) { continue; } var text = identifier.Text; if (string.IsNullOrEmpty(text) || char.IsLower(text[0]) == false) { continue; } ReportDiagnostic(context, syntax); return; } }
private static bool IsViableTupleTypeSyntax(TypeSyntax type) { if (type.IsVar) { // 'var t' can be converted to 'var (x, y, z)' return(true); } if (type.IsKind(SyntaxKind.TupleType)) { // '(int x, int y) t' can be convered to '(int x, int y)'. So all the elements // need names. var tupleType = (TupleTypeSyntax)type; foreach (var element in tupleType.Elements) { if (element.Identifier.IsKind(SyntaxKind.None)) { return(false); } } return(true); } return(false); }
public override void VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node) { TypeSyntax type = node.Type; if (type != null) { if (type.IsKind(SyntaxKind.ArrayType)) { VisitArrayType((ArrayTypeSyntax)type); } else { VisitType(type); } } if (!ShouldVisit) { return; } InitializerExpressionSyntax initializer = node.Initializer; if (initializer != null) { VisitInitializerExpression(initializer); } }
private static TypeSyntax GetNullableType(TypeSyntax type) { if (type.IsKind(SyntaxKind.QualifiedName)) { type = ((QualifiedNameSyntax)type).Right; } return(((GenericNameSyntax)type).TypeArgumentList.Arguments[0]); }
public static TypeSyntax StripRefFromType(this TypeSyntax syntax) { if (syntax.IsKind(SyntaxKindEx.RefType)) { syntax = ((RefTypeSyntaxWrapper)syntax).Type; } return(syntax); }
public static bool CanRefactor(RefactoringContext context, GenericNameSyntax name) { TypeSyntax typeArgument = name.TypeArgumentList?.Arguments.SingleOrDefault(shouldThrow: false); return(typeArgument != null && context.Span.IsBetweenSpans(typeArgument) && !typeArgument.IsKind(SyntaxKind.TupleType) && IsTypeOrReturnType(name)); }
private ExpressionSyntax CreateTupleOrDeclarationExpression(INamedTypeSymbol tupleType, TypeSyntax typeNode) { // If we have an explicit tuple type in code, convert that over to a tuple expression. // i.e. (int x, int y) t = ... will be converted to (int x, int y) = ... // // If we had the "var t" form we'll convert that to the declaration expression "var (x, y)" return(typeNode.IsKind(SyntaxKind.TupleType, out TupleTypeSyntax tupleTypeSyntax) ? (ExpressionSyntax)CreateTupleExpression(tupleTypeSyntax) : CreateDeclarationExpression(tupleType, typeNode)); }
public static bool IsVoid(this TypeSyntax type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } return(type.IsKind(SyntaxKind.PredefinedType) && ((PredefinedTypeSyntax)type).Keyword.IsKind(SyntaxKind.VoidKeyword)); }
private static bool IsValueTuple(SemanticModel semanticModel, TypeSyntax typeSyntax) { if (typeSyntax.IsKind(SyntaxKindEx.TupleType)) { return(false); } var symbolInfo = semanticModel.GetSymbolInfo(typeSyntax); return((symbolInfo.Symbol is ITypeSymbol typeSymbol) && typeSymbol.IsTupleType()); }
static bool IsNullableReferenceType(SyntaxNodeAnalysisContext context, TypeSyntax type) { if (!type.IsKind(SyntaxKind.NullableType)) { return(false); } ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(type, context.CancellationToken); return(!typeSymbol.IsErrorType() && typeSymbol.IsReferenceType); }
protected void FilterType(TypeSyntax syntax) { if (syntax.IsKind(SyntaxKind.PredefinedType)) { var symbolInfo = SemanticModel.GetSymbolInfo(syntax); if ((symbolInfo.Symbol != null) && (symbolInfo.Symbol.Kind == SymbolKind.NamedType)) { var symbol = (ITypeSymbol)symbolInfo.Symbol; FilterTypeSymbol(symbol); } } }
public static bool IsNullableReferenceType( TypeSyntax type, SemanticModel semanticModel, CancellationToken cancellationToken = default) { if (type.IsKind(SyntaxKind.NullableType)) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); return(!typeSymbol.IsKind(SymbolKind.ErrorType) && typeSymbol.IsReferenceType); } return(false); }
internal static void AnalyzeXmlCrefAttribute(SyntaxNodeAnalysisContext context) { var xmlCrefAttribute = (XmlCrefAttributeSyntax)context.Node; CrefSyntax cref = xmlCrefAttribute.Cref; switch (cref?.Kind()) { case SyntaxKind.NameMemberCref: { var nameMemberCref = (NameMemberCrefSyntax)cref; TypeSyntax name = nameMemberCref.Name; if (name?.IsKind(SyntaxKind.PredefinedType) == false && IsFixable(context, name)) { ReportDiagnostic(context, cref); } break; } case SyntaxKind.QualifiedCref: { var qualifiedCref = (QualifiedCrefSyntax)cref; MemberCrefSyntax memberCref = qualifiedCref.Member; if (memberCref?.IsKind(SyntaxKind.NameMemberCref) == true) { var nameMemberCref = (NameMemberCrefSyntax)memberCref; TypeSyntax name = nameMemberCref.Name; if (name != null && IsFixable(context, name)) { ReportDiagnostic(context, cref); } } break; } } }
protected override void VisitType(TypeSyntax node) { if (node.IsKind(SyntaxKind.IdentifierName)) { var identifierName = (IdentifierNameSyntax)node; if (string.Equals(Symbol.Name, identifierName.Identifier.ValueText, StringComparison.Ordinal) && SemanticModel .GetSymbol(identifierName, CancellationToken)? .OriginalDefinition .Equals(Symbol) == true) { CanBeMadeStatic = false; } } else { base.VisitType(node); } }
private static bool IsDefaultOfReferenceOrNullableType(DefaultExpressionSyntax defaultExpression, SemanticModel semanticModel, CancellationToken cancellationToken) { TypeSyntax type = defaultExpression.Type; if (type != null) { if (type.IsKind(SyntaxKind.NullableType)) { return(true); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); if (typeSymbol?.IsErrorType() == false && typeSymbol.IsReferenceType) { return(true); } } return(false); }
private static async Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclarationStatement, ExpressionSyntax value, ITypeSymbol typeSymbol, CancellationToken cancellationToken) { ExpressionSyntax defaultValue = typeSymbol.GetDefaultValueSyntax(document.GetDefaultSyntaxOptions()); LocalDeclarationStatementSyntax newNode = localDeclarationStatement.ReplaceNode( value, defaultValue.WithTriviaFrom(value)); if (defaultValue is LiteralExpressionSyntax) { TypeSyntax oldType = newNode.Declaration.Type; TypeSyntax type = typeSymbol.ToTypeSyntax(); if (!type.IsKind(SyntaxKind.NullableType) && typeSymbol.IsReferenceType) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); if ((semanticModel.GetNullableContext(localDeclarationStatement.SpanStart) & NullableContext.AnnotationsEnabled) != 0) { type = SyntaxFactory.NullableType(type); } } type = type.WithSimplifierAnnotation().WithTriviaFrom(oldType); newNode = newNode.ReplaceNode(oldType, type); } return(await document.ReplaceNodeAsync(localDeclarationStatement, newNode, cancellationToken).ConfigureAwait(false)); }
public static bool IsVoid(this TypeSyntax typeSyntax) { return(typeSyntax.IsKind(SyntaxKind.PredefinedType) && ((PredefinedTypeSyntax)typeSyntax).Keyword.IsKind(SyntaxKind.VoidKeyword)); }
private SyntaxNode Comment(XmlElementSyntax comment, TypeSyntax returnType) => returnType.IsKind(SyntaxKind.GenericName) ? GenericComment(comment, (GenericNameSyntax)returnType) : NonGenericComment(comment, returnType);
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.AddTypeArgument)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); TypeSyntax type = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <TypeSyntax>(); Debug.Assert(type != null, $"{nameof(type)} is null"); if (type == null) { return; } if (!type.IsKind(SyntaxKind.IdentifierName)) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case CompilerDiagnosticIdentifiers.UsingGenericTypeRequiresTypeArguments: { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(type, context.CancellationToken); foreach (ISymbol symbol in symbolInfo.CandidateSymbols) { var namedTypeSymbol = symbol as INamedTypeSymbol; if (namedTypeSymbol != null) { ImmutableArray <ITypeParameterSymbol> typeParameters = namedTypeSymbol.TypeParameters; if (typeParameters.Any()) { CodeAction codeAction = CodeAction.Create( GetTitle(typeParameters), cancellationToken => { SeparatedSyntaxList <TypeSyntax> typeArguments = CreateTypeArguments(typeParameters, type.SpanStart, semanticModel).ToSeparatedSyntaxList(); var identifierName = (IdentifierNameSyntax)type; GenericNameSyntax newNode = SyntaxFactory.GenericName(identifierName.Identifier, SyntaxFactory.TypeArgumentList(typeArguments)); return(context.Document.ReplaceNodeAsync(type, newNode, context.CancellationToken)); }, GetEquivalenceKey(diagnostic, SymbolDisplay.GetString(namedTypeSymbol))); context.RegisterCodeFix(codeAction, diagnostic); } } } break; } } } }
static string GetTypeString(TypeSyntax type) => type.IsKind(SyntaxKind.StructDeclaration) && type.IsKind(SyntaxKind.ReadOnlyKeyword) ? "in " + type.ToString() : type.ToString();
public static bool IsVoid(this TypeSyntax typeSyntax) => typeSyntax.IsKind(SyntaxKind.PredefinedType, out PredefinedTypeSyntax predefinedType) && predefinedType.Keyword.IsKind(SyntaxKind.VoidKeyword);