提交 d32c8be3 编写于 作者: C Cyrus Najmabadi

Fix generation of negative literals.

上级 1418a3cf
......@@ -79,17 +79,17 @@ private static ExpressionSyntax GenerateNullLiteral()
case bool val: return GenerateBooleanLiteralExpression(val);
case string val: return GenerateStringLiteralExpression(val);
case char val: return GenerateCharLiteralExpression(val);
case sbyte val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.SByteSpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case short val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.Int16SpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case int val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.Int32SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case long val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.Int64SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case byte val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.ByteSpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case ushort val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.UInt16SpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, (uint)v));
case uint val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.UInt32SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case ulong val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.UInt64SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case sbyte val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.SByteSpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case short val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.Int16SpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case int val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.Int32SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal, x => x < 0, x => -x);
case long val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.Int64SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal, x => x < 0, x => -x);
case byte val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.ByteSpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, v));
case ushort val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.UInt16SpecialValues, null, canUseFieldReference, (s, v) => SyntaxFactory.Literal(s, (uint)v));
case uint val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.UInt32SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case ulong val: return GenerateNonNegativeLiteralExpression(type, val, LiteralSpecialValues.UInt64SpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case float val: return GenerateSingleLiteralExpression(type, val, canUseFieldReference);
case double val: return GenerateDoubleLiteralExpression(type, val, canUseFieldReference);
case decimal val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.DecimalSpecialValues, null, canUseFieldReference, SyntaxFactory.Literal);
case decimal val: return GenerateLiteralExpression(type, val, LiteralSpecialValues.DecimalSpecialValues, null, canUseFieldReference, SyntaxFactory.Literal, x => x < 0, x => -x);
}
return type == null || type.IsReferenceType || type.IsPointerType() || type.IsNullable()
......@@ -196,7 +196,9 @@ private static ExpressionSyntax GenerateDoubleLiteralExpression(ITypeSymbol type
}
}
return GenerateLiteralExpression(type, value, LiteralSpecialValues.DoubleSpecialValues, "R", canUseFieldReference, SyntaxFactory.Literal);
return GenerateLiteralExpression(
type, value, LiteralSpecialValues.DoubleSpecialValues, "R", canUseFieldReference,
SyntaxFactory.Literal, x => x < 0, x => -x);
}
private static ExpressionSyntax GenerateSingleLiteralExpression(ITypeSymbol type, float value, bool canUseFieldReference)
......@@ -223,11 +225,26 @@ private static ExpressionSyntax GenerateSingleLiteralExpression(ITypeSymbol type
}
}
return GenerateLiteralExpression(type, value, LiteralSpecialValues.SingleSpecialValues, "R", canUseFieldReference, SyntaxFactory.Literal);
return GenerateLiteralExpression(
type, value, LiteralSpecialValues.SingleSpecialValues, "R", canUseFieldReference,
SyntaxFactory.Literal, x => x < 0, x => -x);
}
private static ExpressionSyntax GenerateNonNegativeLiteralExpression<T>(
ITypeSymbol type, T value, IEnumerable<KeyValuePair<T, string>> constants,
string formatString, bool canUseFieldReference,
Func<string, T, SyntaxToken> tokenFactory)
{
return GenerateLiteralExpression(
type, value, constants, formatString, canUseFieldReference,
tokenFactory, isNegative: x => false, negate: t => throw new InvalidOperationException());
}
private static ExpressionSyntax GenerateLiteralExpression<T>(
ITypeSymbol type, T value, IEnumerable<KeyValuePair<T, string>> constants, string formatString, bool canUseFieldReference, Func<string, T, SyntaxToken> tokenFactory)
ITypeSymbol type, T value, IEnumerable<KeyValuePair<T, string>> constants,
string formatString, bool canUseFieldReference,
Func<string, T, SyntaxToken> tokenFactory,
Func<T, bool> isNegative, Func<T, T> negate)
{
if (canUseFieldReference)
{
......@@ -238,10 +255,21 @@ private static ExpressionSyntax GenerateSingleLiteralExpression(ITypeSymbol type
}
}
var negative = isNegative(value);
if (negative)
{
value = negate(value);
}
var suffix = DetermineSuffix(type, value);
var stringValue = ((IFormattable)value).ToString(formatString, CultureInfo.InvariantCulture) + suffix;
return SyntaxFactory.LiteralExpression(
var literal = SyntaxFactory.LiteralExpression(
SyntaxKind.NumericLiteralExpression, tokenFactory(stringValue, value));
return negative
? SyntaxFactory.PrefixUnaryExpression(SyntaxKind.UnaryMinusExpression, literal)
: (ExpressionSyntax)literal;
}
private static ExpressionSyntax GenerateFieldReference<T>(ITypeSymbol type, T value, IEnumerable<KeyValuePair<T, string>> constants)
......
......@@ -61,13 +61,13 @@ public void TestLiteralExpressions()
{
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0), "0");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1), "1");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(-1), "-1");
VerifySyntax<PrefixUnaryExpressionSyntax>(Generator.LiteralExpression(-1), "-1");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(int.MinValue), "global::System.Int32.MinValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(int.MaxValue), "global::System.Int32.MaxValue");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0L), "0L");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1L), "1L");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(-1L), "-1L");
VerifySyntax<PrefixUnaryExpressionSyntax>(Generator.LiteralExpression(-1L), "-1L");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(long.MinValue), "global::System.Int64.MinValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(long.MaxValue), "global::System.Int64.MaxValue");
......@@ -78,7 +78,7 @@ public void TestLiteralExpressions()
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0.0f), "0F");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1.0f), "1F");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(-1.0f), "-1F");
VerifySyntax<PrefixUnaryExpressionSyntax>(Generator.LiteralExpression(-1.0f), "-1F");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(float.MinValue), "global::System.Single.MinValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(float.MaxValue), "global::System.Single.MaxValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(float.Epsilon), "global::System.Single.Epsilon");
......@@ -88,7 +88,7 @@ public void TestLiteralExpressions()
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0.0), "0D");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1.0), "1D");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(-1.0), "-1D");
VerifySyntax<PrefixUnaryExpressionSyntax>(Generator.LiteralExpression(-1.0), "-1D");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(double.MinValue), "global::System.Double.MinValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(double.MaxValue), "global::System.Double.MaxValue");
VerifySyntax<MemberAccessExpressionSyntax>(Generator.LiteralExpression(double.Epsilon), "global::System.Double.Epsilon");
......@@ -99,7 +99,7 @@ public void TestLiteralExpressions()
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0m), "0M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0.00m), "0.00M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1.00m), "1.00M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(-1.00m), "-1.00M");
VerifySyntax<PrefixUnaryExpressionSyntax>(Generator.LiteralExpression(-1.00m), "-1.00M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(1.0000000000m), "1.0000000000M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0.000000m), "0.000000M");
VerifySyntax<LiteralExpressionSyntax>(Generator.LiteralExpression(0.0000000m), "0.0000000M");
......
......@@ -15,67 +15,67 @@ internal static class LiteralSpecialValues
public static readonly IEnumerable<KeyValuePair<sbyte, string>> SByteSpecialValues = new Dictionary<sbyte, string>()
{
{ sbyte.MinValue, "MinValue" },
{ sbyte.MaxValue, "MaxValue" },
{ sbyte.MinValue, nameof(sbyte.MinValue) },
{ sbyte.MaxValue, nameof(sbyte.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<short, string>> Int16SpecialValues = new Dictionary<short, string>()
{
{ short.MinValue, "MinValue" },
{ short.MaxValue, "MaxValue" },
{ short.MinValue, nameof(short.MinValue) },
{ short.MaxValue, nameof(short.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<ushort, string>> UInt16SpecialValues = new Dictionary<ushort, string>()
{
{ ushort.MaxValue, "MaxValue" },
{ ushort.MaxValue, nameof(ushort.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<int, string>> Int32SpecialValues = new Dictionary<int, string>()
{
{ int.MinValue, "MinValue" },
{ int.MaxValue, "MaxValue" },
{ int.MinValue, nameof(int.MinValue) },
{ int.MaxValue, nameof(int.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<uint, string>> UInt32SpecialValues = new Dictionary<uint, string>()
{
{ uint.MaxValue, "MaxValue" },
{ uint.MaxValue, nameof(uint.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<long, string>> Int64SpecialValues = new Dictionary<long, string>()
{
{ long.MinValue, "MinValue" },
{ long.MaxValue, "MaxValue" },
{ long.MinValue, nameof(long.MinValue) },
{ long.MaxValue, nameof(long.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<ulong, string>> UInt64SpecialValues = new Dictionary<ulong, string>()
{
{ ulong.MaxValue, "MaxValue" },
{ ulong.MaxValue, nameof(ulong.MaxValue) },
};
public static readonly IEnumerable<KeyValuePair<float, string>> SingleSpecialValues = new Dictionary<float, string>()
{
{ float.MinValue, "MinValue" },
{ float.MaxValue, "MaxValue" },
{ float.Epsilon, "Epsilon" },
{ float.NaN, "NaN" },
{ float.NegativeInfinity, "NegativeInfinity" },
{ float.PositiveInfinity, "PositiveInfinity" },
{ float.MinValue, nameof(float.MinValue) },
{ float.MaxValue, nameof(float.MaxValue) },
{ float.Epsilon, nameof(float.Epsilon) },
{ float.NaN, nameof(float.NaN) },
{ float.NegativeInfinity, nameof(float.NegativeInfinity) },
{ float.PositiveInfinity, nameof(float.PositiveInfinity) },
};
public static readonly IEnumerable<KeyValuePair<double, string>> DoubleSpecialValues = new Dictionary<double, string>()
{
{ double.MinValue, "MinValue" },
{ double.MaxValue, "MaxValue" },
{ double.Epsilon, "Epsilon" },
{ double.NaN, "NaN" },
{ double.NegativeInfinity, "NegativeInfinity" },
{ double.PositiveInfinity, "PositiveInfinity" },
{ double.MinValue, nameof(double.MinValue) },
{ double.MaxValue, nameof(double.MaxValue) },
{ double.Epsilon, nameof(double.Epsilon) },
{ double.NaN, nameof(double.NaN) },
{ double.NegativeInfinity, nameof(double.NegativeInfinity) },
{ double.PositiveInfinity, nameof(double.PositiveInfinity) },
};
public static readonly IEnumerable<KeyValuePair<decimal, string>> DecimalSpecialValues = new Dictionary<decimal, string>()
{
{ decimal.MinValue, "MinValue" },
{ decimal.MaxValue, "MaxValue" },
{ decimal.MinValue, nameof(decimal.MinValue) },
{ decimal.MaxValue, nameof(decimal.MaxValue) },
};
}
}
......@@ -65,21 +65,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
ElseIf TypeOf value Is Char Then
Return GenerateCharLiteralExpression(DirectCast(value, Char))
ElseIf TypeOf value Is SByte Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_SByte, value, canUseFieldReference, LiteralSpecialValues.SByteSpecialValues)
Return GenerateIntegralLiteralExpression(type, SpecialType.System_SByte, DirectCast(value, SByte), canUseFieldReference, LiteralSpecialValues.SByteSpecialValues, Function(t) t < 0, Function(t) -t)
ElseIf TypeOf value Is Short Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int16, value, canUseFieldReference, LiteralSpecialValues.Int16SpecialValues)
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int16, DirectCast(value, Short), canUseFieldReference, LiteralSpecialValues.Int16SpecialValues, Function(t) t < 0, Function(t) -t)
ElseIf TypeOf value Is Integer Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int32, value, canUseFieldReference, LiteralSpecialValues.Int32SpecialValues)
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int32, DirectCast(value, Integer), canUseFieldReference, LiteralSpecialValues.Int32SpecialValues, Function(t) t < 0, Function(t) -t)
ElseIf TypeOf value Is Long Then
Return GenerateLongLiteralExpression(type, DirectCast(value, Long), canUseFieldReference)
ElseIf TypeOf value Is Byte Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Byte, value, canUseFieldReference, LiteralSpecialValues.ByteSpecialValues)
Return GenerateNonNegativeIntegralLiteralExpression(type, SpecialType.System_Byte, DirectCast(value, Byte), canUseFieldReference, LiteralSpecialValues.ByteSpecialValues)
ElseIf TypeOf value Is UShort Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_UInt16, value, canUseFieldReference, LiteralSpecialValues.UInt16SpecialValues)
Return GenerateNonNegativeIntegralLiteralExpression(type, SpecialType.System_UInt16, DirectCast(value, UShort), canUseFieldReference, LiteralSpecialValues.UInt16SpecialValues)
ElseIf TypeOf value Is UInteger Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_UInt32, value, canUseFieldReference, LiteralSpecialValues.UInt32SpecialValues)
Return GenerateNonNegativeIntegralLiteralExpression(type, SpecialType.System_UInt32, DirectCast(value, UInteger), canUseFieldReference, LiteralSpecialValues.UInt32SpecialValues)
ElseIf TypeOf value Is ULong Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_UInt64, value, canUseFieldReference, LiteralSpecialValues.UInt64SpecialValues)
Return GenerateNonNegativeIntegralLiteralExpression(type, SpecialType.System_UInt64, DirectCast(value, ULong), canUseFieldReference, LiteralSpecialValues.UInt64SpecialValues)
ElseIf TypeOf value Is Single Then
Return GenerateSingleLiteralExpression(type, DirectCast(value, Single), canUseFieldReference)
ElseIf TypeOf value Is Double Then
......@@ -155,11 +155,28 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Return invocation.WithAdditionalAnnotations(Simplifier.Annotation)
End Function
Private Function GenerateIntegralLiteralExpression(Of TStructure)(type As ITypeSymbol,
specialType As SpecialType,
value As Object,
canUseFieldReference As Boolean,
specialValues As IEnumerable(Of KeyValuePair(Of TStructure, String))) As ExpressionSyntax
Private Function GenerateNonNegativeIntegralLiteralExpression(Of TStructure)(
type As ITypeSymbol,
specialType As SpecialType,
value As TStructure,
canUseFieldReference As Boolean,
specialValues As IEnumerable(Of KeyValuePair(Of TStructure, String))) As ExpressionSyntax
Return GenerateIntegralLiteralExpression(
type, specialType, value, canUseFieldReference, specialValues,
Function(v) False, Function(v)
Throw New InvalidOperationException()
End Function)
End Function
Private Function GenerateIntegralLiteralExpression(Of TStructure)(
type As ITypeSymbol,
specialType As SpecialType,
value As TStructure,
canUseFieldReference As Boolean,
specialValues As IEnumerable(Of KeyValuePair(Of TStructure, String)),
isNegative As Func(Of TStructure, Boolean),
negate As Func(Of TStructure, TStructure)) As ExpressionSyntax
If canUseFieldReference Then
Dim field = GenerateFieldReference(specialType, value, specialValues)
......@@ -168,15 +185,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End If
End If
Dim negative = isNegative(value)
If negative Then
value = negate(value)
End If
Dim typeSuffix As TypeCharacter = TypeCharacter.None
Dim suffix As String = String.Empty
DetermineSuffix(type, value, typeSuffix, suffix)
Dim literal = DirectCast(value, IFormattable).ToString(Nothing, CultureInfo.InvariantCulture) & suffix
Dim expression = SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(
Dim expression As ExpressionSyntax = SyntaxFactory.NumericLiteralExpression(SyntaxFactory.IntegerLiteralToken(
literal, LiteralBase.Decimal, typeSuffix,
IntegerUtilities.ToUInt64(value)))
If negative Then
expression = SyntaxFactory.UnaryMinusExpression(expression)
End If
If TypeOf value Is Byte AndAlso Not IsSpecialType(type, SpecialType.System_Byte) Then
Return SyntaxFactory.PredefinedCastExpression(SyntaxFactory.Token(SyntaxKind.CByteKeyword), expression)
ElseIf TypeOf value Is SByte AndAlso Not IsSpecialType(type, SpecialType.System_SByte) Then
......@@ -190,7 +216,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
value As Long,
canUseFieldReference As Boolean) As ExpressionSyntax
If canUseFieldReference OrElse value > Long.MinValue Then
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int64, value, canUseFieldReference, LiteralSpecialValues.Int64SpecialValues)
Return GenerateIntegralLiteralExpression(type, SpecialType.System_Int64, value, canUseFieldReference, LiteralSpecialValues.Int64SpecialValues, Function(t) t < 0, Function(t) -t)
End If
' We have to special case how Long.MinValue is printed when we can't refer to the
......@@ -250,8 +276,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End Sub
Private Function GenerateDoubleLiteralExpression(type As ITypeSymbol,
value As Double,
canUseFieldReference As Boolean) As ExpressionSyntax
value As Double,
canUseFieldReference As Boolean) As ExpressionSyntax
If Not canUseFieldReference Then
If Double.IsNaN(value) Then
Return SyntaxFactory.DivideExpression(
......@@ -268,12 +294,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End If
End If
Return GenerateFloatLiteralExpression(type, SpecialType.System_Double, value, canUseFieldReference, LiteralSpecialValues.DoubleSpecialValues)
Return GenerateFloatLiteralExpression(
type, SpecialType.System_Double, value, canUseFieldReference,
LiteralSpecialValues.DoubleSpecialValues, Function(t) t < 0, Function(t) -t)
End Function
Private Function GenerateSingleLiteralExpression(type As ITypeSymbol,
value As Single,
canUseFieldReference As Boolean) As ExpressionSyntax
Private Function GenerateSingleLiteralExpression(
type As ITypeSymbol,
value As Single,
canUseFieldReference As Boolean) As ExpressionSyntax
If Not canUseFieldReference Then
If Double.IsNaN(value) Then
Return SyntaxFactory.DivideExpression(
......@@ -290,14 +319,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End If
End If
Return GenerateFloatLiteralExpression(type, SpecialType.System_Single, value, canUseFieldReference, LiteralSpecialValues.SingleSpecialValues)
Return GenerateFloatLiteralExpression(
type, SpecialType.System_Single, value, canUseFieldReference,
LiteralSpecialValues.SingleSpecialValues, Function(t) t < 0, Function(t) -t)
End Function
Private Function GenerateFloatLiteralExpression(Of TStructure)(type As ITypeSymbol,
specialType As SpecialType,
value As Object,
canUseFieldReference As Boolean,
specialValues As IEnumerable(Of KeyValuePair(Of TStructure, String))) As ExpressionSyntax
Private Function GenerateFloatLiteralExpression(Of TStructure)(
type As ITypeSymbol,
specialType As SpecialType,
value As TStructure,
canUseFieldReference As Boolean,
specialValues As IEnumerable(Of KeyValuePair(Of TStructure, String)),
isNegative As Func(Of TStructure, Boolean),
negate As Func(Of TStructure, TStructure)) As ExpressionSyntax
If canUseFieldReference Then
Dim field = GenerateFieldReference(specialType, value, specialValues)
If field IsNot Nothing Then
......@@ -305,12 +339,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End If
End If
Dim negative = isNegative(value)
If negative Then
value = negate(value)
End If
Dim typeSuffix As TypeCharacter = TypeCharacter.None
Dim suffix As String = String.Empty
DetermineSuffix(type, value, typeSuffix, suffix)
Dim literal = DirectCast(value, IFormattable).ToString("R", CultureInfo.InvariantCulture) & suffix
Return GenerateFloatLiteral(Convert.ToDouble(value), literal, typeSuffix)
Dim literalSyntax As ExpressionSyntax = GenerateFloatLiteral(Convert.ToDouble(value), literal, typeSuffix)
Return If(negative, SyntaxFactory.UnaryMinusExpression(literalSyntax), literalSyntax)
End Function
Private Function GenerateFloatLiteral(value As Double,
......
......@@ -57,13 +57,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Editting
Public Sub TestLiteralExpressions()
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(0), "0")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(1), "1")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(-1), "-1")
VerifySyntax(Of UnaryExpressionSyntax)(Generator.LiteralExpression(-1), "-1")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Integer.MinValue), "Global.System.Int32.MinValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Integer.MaxValue), "Global.System.Int32.MaxValue")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(0L), "0L")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(1L), "1L")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(-1L), "-1L")
VerifySyntax(Of UnaryExpressionSyntax)(Generator.LiteralExpression(-1L), "-1L")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Long.MinValue), "Global.System.Int64.MinValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Long.MaxValue), "Global.System.Int64.MaxValue")
......@@ -74,7 +74,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Editting
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(0.0F), "0F")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(1.0F), "1F")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(-1.0F), "-1F")
VerifySyntax(Of UnaryExpressionSyntax)(Generator.LiteralExpression(-1.0F), "-1F")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Single.MinValue), "Global.System.Single.MinValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Single.MaxValue), "Global.System.Single.MaxValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Single.Epsilon), "Global.System.Single.Epsilon")
......@@ -84,7 +84,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Editting
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(0.0), "0R")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(1.0), "1R")
VerifySyntax(Of LiteralExpressionSyntax)(Generator.LiteralExpression(-1.0), "-1R")
VerifySyntax(Of UnaryExpressionSyntax)(Generator.LiteralExpression(-1.0), "-1R")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Double.MinValue), "Global.System.Double.MinValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Double.MaxValue), "Global.System.Double.MaxValue")
VerifySyntax(Of MemberAccessExpressionSyntax)(Generator.LiteralExpression(Double.Epsilon), "Global.System.Double.Epsilon")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册