Пример #1
0
        // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
        private static ExpressionSyntax GetDefaultValueSyntaxImpl(ITypeSymbol typeSymbol, TypeSyntax type, SemanticModel semanticModel, int position, SymbolDisplayFormat format = null)
        {
            switch (typeSymbol.SpecialType)
            {
            case SpecialType.System_Boolean:
                return(FalseLiteralExpression());

            case SpecialType.System_Char:
                return(CharacterLiteralExpression('\0'));

            case SpecialType.System_SByte:
            case SpecialType.System_Byte:
            case SpecialType.System_Int16:
            case SpecialType.System_UInt16:
            case SpecialType.System_Int32:
            case SpecialType.System_UInt32:
            case SpecialType.System_Int64:
            case SpecialType.System_UInt64:
            case SpecialType.System_Decimal:
            case SpecialType.System_Single:
            case SpecialType.System_Double:
                return(NumericLiteralExpression(0));
            }

            if (typeSymbol.IsNullableType())
            {
                return(NullLiteralExpression());
            }

            if (typeSymbol.BaseType?.SpecialType == SpecialType.System_Enum)
            {
                IFieldSymbol fieldSymbol = typeSymbol.FindFieldWithConstantValue(0);

                if (fieldSymbol != null)
                {
                    type = type ?? (typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format));

                    Debug.Assert(type != null);

                    return(SimpleMemberAccessExpression(type, IdentifierName(fieldSymbol.Name)));
                }
                else
                {
                    return(NumericLiteralExpression(0));
                }
            }

            if (typeSymbol.IsReferenceType)
            {
                return(NullLiteralExpression());
            }

            type = type ?? (typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format));

            Debug.Assert(type != null);

            return(DefaultExpression(type));
        }
Пример #2
0
        internal static ExpressionSyntax GetDefaultValueMinimalSyntax(this IParameterSymbol parameterSymbol, SemanticModel semanticModel, int position, SymbolDisplayFormat format = null)
        {
            if (parameterSymbol == null)
            {
                throw new ArgumentNullException(nameof(parameterSymbol));
            }

            if (!parameterSymbol.HasExplicitDefaultValue)
            {
                throw new ArgumentException("Parameter does not specify default value.", nameof(parameterSymbol));
            }

            object value = parameterSymbol.ExplicitDefaultValue;

            ITypeSymbol typeSymbol = parameterSymbol.Type;

            if (typeSymbol.TypeKind == TypeKind.Enum)
            {
                if (value == null)
                {
                    return(NullLiteralExpression());
                }

                IFieldSymbol fieldSymbol = typeSymbol.FindFieldWithConstantValue(value);

                TypeSyntax type = typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format);

                if (fieldSymbol != null)
                {
                    return(SimpleMemberAccessExpression(type, IdentifierName(fieldSymbol.Name)));
                }
                else
                {
                    return(CastExpression(type, LiteralExpression(value)));
                }
            }

            if (value == null &&
                !typeSymbol.IsReferenceTypeOrNullableType())
            {
                return(DefaultExpression(typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format)));
            }

            return(LiteralExpression(value));
        }
Пример #3
0
        private static ExpressionSyntax ToDefaultExpression(ITypeSymbol typeSymbol, TypeSyntax type, SemanticModel semanticModel, int position, SymbolDisplayFormat format = null)
        {
            if (typeSymbol.IsErrorType())
            {
                return(null);
            }

            switch (typeSymbol.SpecialType)
            {
            case SpecialType.System_Boolean:
                return(FalseLiteralExpression());

            case SpecialType.System_Char:
                return(CharacterLiteralExpression('\0'));

            case SpecialType.System_SByte:
            case SpecialType.System_Byte:
            case SpecialType.System_Int16:
            case SpecialType.System_UInt16:
            case SpecialType.System_Int32:
            case SpecialType.System_UInt32:
            case SpecialType.System_Int64:
            case SpecialType.System_UInt64:
            case SpecialType.System_Decimal:
            case SpecialType.System_Single:
            case SpecialType.System_Double:
                return(ZeroLiteralExpression());
            }

            if (typeSymbol.IsConstructedFrom(SpecialType.System_Nullable_T))
            {
                return(NullLiteralExpression());
            }

            if (typeSymbol.BaseType?.SpecialType == SpecialType.System_Enum)
            {
                IFieldSymbol fieldSymbol = typeSymbol.FindFieldWithConstantValue(0);

                if (fieldSymbol != null)
                {
                    if (type == null)
                    {
                        type = (semanticModel != null)
                            ? typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format)
                            : typeSymbol.ToTypeSyntax().WithSimplifierAnnotation();
                    }

                    Debug.Assert(type != null);

                    return(SimpleMemberAccessExpression(type, IdentifierName(fieldSymbol.Name)));
                }
                else
                {
                    return(ZeroLiteralExpression());
                }
            }

            if (typeSymbol.IsReferenceType)
            {
                return(NullLiteralExpression());
            }

            if (type == null)
            {
                type = (semanticModel != null)
                    ? typeSymbol.ToMinimalTypeSyntax(semanticModel, position, format)
                    : typeSymbol.ToTypeSyntax().WithSimplifierAnnotation();
            }

            Debug.Assert(type != null);

            return(DefaultExpression(type));
        }