Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider constructor type when lifting decimal constants #2953

Merged
merged 3 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -36,7 +36,31 @@ public static int Main()
Console.WriteLine(field2);
Console.WriteLine(field3);
Console.WriteLine(field4);
Console.WriteLine(IntToDecimal());
Console.WriteLine(UIntToDecimal());
Console.WriteLine(LongToDecimal());
Console.WriteLine(ULongToDecimal());
return 0;
}

public static decimal IntToDecimal()
{
return (decimal)int.MaxValue;
}

public static decimal UIntToDecimal()
{
return (decimal)uint.MaxValue;
}

public static decimal LongToDecimal()
{
return (decimal)long.MaxValue;
}

public static decimal ULongToDecimal()
{
return (decimal)ulong.MaxValue;
}
}
}
17 changes: 12 additions & 5 deletions ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2017 Daniel Grunwald
// Copyright (c) 2014-2017 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
Expand Down Expand Up @@ -428,11 +428,18 @@ bool TransformDecimalCtorToConstant(NewObj inst, out LdcDecimal result)
var args = inst.Arguments;
if (args.Count == 1)
{
int val;
if (args[0].MatchLdcI4(out val))
long val;
if (args[0].MatchLdcI(out val))
{
result = new LdcDecimal(val);
return true;
var paramType = inst.Method.Parameters[0].Type.GetDefinition()?.KnownTypeCode;
result = paramType switch {
KnownTypeCode.Int32 => new LdcDecimal(new decimal(unchecked((int)val))),
KnownTypeCode.UInt32 => new LdcDecimal(new decimal(unchecked((uint)val))),
KnownTypeCode.Int64 => new LdcDecimal(new decimal(val)),
KnownTypeCode.UInt64 => new LdcDecimal(new decimal(unchecked((ulong)val))),
_ => null
};
return result is not null;
}
}
else if (args.Count == 5)
Expand Down