private void AddConstantValue(ITypeSymbol type, object constantValue, bool preferNumericValueOrExpandedFlagsForEnum = false) { if (constantValue != null) { AddNonNullConstantValue(type, constantValue, preferNumericValueOrExpandedFlagsForEnum); } else if (type.IsReferenceType || type.TypeKind == TypeKind.Pointer || ITypeSymbolHelpers.IsNullableType(type)) { AddKeyword(SyntaxKind.NullKeyword); } else { AddKeyword(SyntaxKind.DefaultKeyword); AddPunctuation(SyntaxKind.OpenParenToken); type.Accept(this.NotFirstVisitor); AddPunctuation(SyntaxKind.CloseParenToken); } }
private bool ShouldAddNullableAnnotation(ITypeSymbol type) { switch (type.NullableAnnotation) { case CodeAnalysis.NullableAnnotation.Annotated: if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier) && !ITypeSymbolHelpers.IsNullableType(type) && !type.IsValueType) { return(true); } break; case CodeAnalysis.NullableAnnotation.NotAnnotated: if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier) && !type.IsValueType && (type as Symbols.PublicModel.TypeSymbol)?.UnderlyingTypeSymbol.IsTypeParameterDisallowingAnnotationInCSharp8() != true) { return(true); } break; } return(false); }
private void VisitNamedTypeWithoutNullability(INamedTypeSymbol symbol) { if (this.IsMinimizing && TryAddAlias(symbol, builder)) { return; } if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.UseSpecialTypes) || (symbol.IsNativeIntegerType && !format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.UseNativeIntegerUnderlyingType))) { if (AddSpecialTypeKeyword(symbol)) { //if we're using special type keywords and this is a special type, then no other work is required return; } } if (!format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.ExpandNullable)) { //if we're expanding nullable, we just visit nullable types normally if (ITypeSymbolHelpers.IsNullableType(symbol) && !symbol.IsDefinition) { // Can't have a type called "int*?". var typeArg = symbol.TypeArguments[0]; if (typeArg.TypeKind != TypeKind.Pointer) { typeArg.Accept(this.NotFirstVisitor); AddCustomModifiersIfRequired(symbol.GetTypeArgumentCustomModifiers(0), leadingSpace: true, trailingSpace: false); AddPunctuation(SyntaxKind.QuestionToken); //visiting the underlying type did all of the work for us return; } } } if (this.IsMinimizing || (symbol.IsTupleType && !ShouldDisplayAsValueTuple(symbol))) { MinimallyQualify(symbol); return; } AddTypeKind(symbol); if (CanShowDelegateSignature(symbol)) { if (format.DelegateStyle == SymbolDisplayDelegateStyle.NameAndSignature) { var invokeMethod = symbol.DelegateInvokeMethod; if (invokeMethod.ReturnsByRef) { AddRefIfRequired(); } else if (invokeMethod.ReturnsByRefReadonly) { AddRefReadonlyIfRequired(); } if (invokeMethod.ReturnsVoid) { AddKeyword(SyntaxKind.VoidKeyword); } else { AddReturnType(symbol.DelegateInvokeMethod); } AddSpace(); } } //only visit the namespace if the style requires it and there isn't an enclosing type var containingSymbol = symbol.ContainingSymbol; if (ShouldVisitNamespace(containingSymbol)) { var namespaceSymbol = (INamespaceSymbol)containingSymbol; var shouldSkip = namespaceSymbol.IsGlobalNamespace && symbol.TypeKind == TypeKind.Error; if (!shouldSkip) { namespaceSymbol.Accept(this.NotFirstVisitor); AddPunctuation(namespaceSymbol.IsGlobalNamespace ? SyntaxKind.ColonColonToken : SyntaxKind.DotToken); } } //visit the enclosing type if the style requires it if (format.TypeQualificationStyle == SymbolDisplayTypeQualificationStyle.NameAndContainingTypes || format.TypeQualificationStyle == SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces) { if (IncludeNamedType(symbol.ContainingType)) { symbol.ContainingType.Accept(this.NotFirstVisitor); AddPunctuation(SyntaxKind.DotToken); } } AddNameAndTypeArgumentsOrParameters(symbol); }
public static bool IsNullableType(this ITypeSymbol typeOpt) { return(ITypeSymbolHelpers.IsNullableType(typeOpt)); }