Ejemplo n.º 1
0
 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);
     }
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        private static bool CanBeInControlFlowGraph(IOperation n)
        {
            switch (n.Kind)
            {
            case OperationKind.Block:
            case OperationKind.Switch:
            case OperationKind.Loop:
            case OperationKind.Branch:
            case OperationKind.Lock:
            case OperationKind.Try:
            case OperationKind.Using:
            case OperationKind.Conditional:
            case OperationKind.Coalesce:
            case OperationKind.ConditionalAccess:
            case OperationKind.ConditionalAccessInstance:
            case OperationKind.MemberInitializer:
            case OperationKind.FieldInitializer:
            case OperationKind.PropertyInitializer:
            case OperationKind.ParameterInitializer:
            case OperationKind.CatchClause:
            case OperationKind.SwitchCase:
            case OperationKind.CaseClause:
            case OperationKind.VariableDeclarationGroup:
            case OperationKind.VariableDeclaration:
            case OperationKind.VariableDeclarator:
            case OperationKind.VariableInitializer:
            case OperationKind.Return:
            case OperationKind.YieldBreak:
            case OperationKind.Labeled:
            case OperationKind.Throw:
            case OperationKind.End:
            case OperationKind.Empty:
            case OperationKind.NameOf:
            case OperationKind.AnonymousFunction:
            case OperationKind.ObjectOrCollectionInitializer:
            case OperationKind.LocalFunction:
                return(false);

            case OperationKind.BinaryOperator:
                var binary = (IBinaryOperation)n;
                return((binary.OperatorKind != Operations.BinaryOperatorKind.ConditionalAnd && binary.OperatorKind != Operations.BinaryOperatorKind.ConditionalOr) ||
                       (binary.OperatorMethod == null &&
                        !ITypeSymbolHelpers.IsBooleanType(binary.Type) &&
                        !ITypeSymbolHelpers.IsNullableOfBoolean(binary.Type) &&
                        !ITypeSymbolHelpers.IsObjectType(binary.Type) &&
                        !ITypeSymbolHelpers.IsDynamicType(binary.Type)));

            case OperationKind.InstanceReference:
                // Implicit instance receivers are expected to have been removed when dealing with creations.
                return(((IInstanceReferenceOperation)n).ReferenceKind == InstanceReferenceKind.ContainingTypeInstance);

            case OperationKind.None:
                return(!(n is IPlaceholderOperation));

            case OperationKind.Invalid:
            case OperationKind.YieldReturn:
            case OperationKind.ExpressionStatement:
            case OperationKind.Stop:
            case OperationKind.RaiseEvent:
            case OperationKind.Literal:
            case OperationKind.Conversion:
            case OperationKind.Invocation:
            case OperationKind.ArrayElementReference:
            case OperationKind.LocalReference:
            case OperationKind.ParameterReference:
            case OperationKind.FieldReference:
            case OperationKind.MethodReference:
            case OperationKind.PropertyReference:
            case OperationKind.EventReference:
            case OperationKind.FlowAnonymousFunction:
            case OperationKind.ObjectCreation:
            case OperationKind.TypeParameterObjectCreation:
            case OperationKind.ArrayCreation:
            case OperationKind.ArrayInitializer:
            case OperationKind.IsType:
            case OperationKind.Await:
            case OperationKind.SimpleAssignment:
            case OperationKind.CompoundAssignment:
            case OperationKind.Parenthesized:
            case OperationKind.EventAssignment:
            case OperationKind.InterpolatedString:
            case OperationKind.AnonymousObjectCreation:
            case OperationKind.Tuple:
            case OperationKind.TupleBinaryOperator:
            case OperationKind.DynamicObjectCreation:
            case OperationKind.DynamicMemberReference:
            case OperationKind.DynamicInvocation:
            case OperationKind.DynamicIndexerAccess:
            case OperationKind.TranslatedQuery:
            case OperationKind.DelegateCreation:
            case OperationKind.DefaultValue:
            case OperationKind.TypeOf:
            case OperationKind.SizeOf:
            case OperationKind.AddressOf:
            case OperationKind.IsPattern:
            case OperationKind.Increment:
            case OperationKind.Decrement:
            case OperationKind.DeconstructionAssignment:
            case OperationKind.DeclarationExpression:
            case OperationKind.OmittedArgument:
            case OperationKind.Argument:
            case OperationKind.InterpolatedStringText:
            case OperationKind.Interpolation:
            case OperationKind.ConstantPattern:
            case OperationKind.DeclarationPattern:
            case OperationKind.UnaryOperator:
            case OperationKind.FlowCapture:
            case OperationKind.FlowCaptureReference:
            case OperationKind.IsNull:
            case OperationKind.CaughtException:
            case OperationKind.StaticLocalInitializationSemaphore:
            case OperationKind.Discard:
                return(true);
            }

            Assert.True(false, $"Unhandled node kind OperationKind.{n.Kind}");
            return(false);
        }
Ejemplo n.º 5
0
 public static ITypeSymbol GetNullableUnderlyingType(this ITypeSymbol type)
 {
     return(ITypeSymbolHelpers.GetNullableUnderlyingType(type));
 }
Ejemplo n.º 6
0
 public static bool IsNullableType(this ITypeSymbol typeOpt)
 {
     return(ITypeSymbolHelpers.IsNullableType(typeOpt));
 }