mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
Added option to uglify presentation of decimal constants (show `[DecimalConstant(...)]`).pull/1236/head
26 changed files with 566 additions and 73 deletions
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
using System.Runtime.CompilerServices; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly |
||||
{ |
||||
public class NoDecimalConstants |
||||
{ |
||||
[DecimalConstant(1, 0, 0u, 0u, 10u)] |
||||
private static readonly decimal constant = 1.0m; |
||||
private void MethodWithOptionalParameter([Optional] [DecimalConstant(1, 0, 0u, 0u, 10u)] decimal parameter) |
||||
{ |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 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
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly |
||||
{ |
||||
public class NoDecimalConstants |
||||
{ |
||||
private const decimal constant = 1.0m; |
||||
|
||||
private void MethodWithOptionalParameter(decimal parameter = 1.0m) |
||||
{ |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,95 @@
@@ -0,0 +1,95 @@
|
||||
|
||||
|
||||
|
||||
|
||||
// Metadata version: v4.0.30319 |
||||
.assembly extern mscorlib |
||||
{ |
||||
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. |
||||
.ver 4:0:0:0 |
||||
} |
||||
.assembly NoDecimalConstants |
||||
{ |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx |
||||
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. |
||||
|
||||
// --- The following custom attribute is added automatically, do not uncomment ------- |
||||
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 ) |
||||
|
||||
.permissionset reqmin |
||||
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} |
||||
.hash algorithm 0x00008004 |
||||
.ver 0:0:0:0 |
||||
} |
||||
.module NoDecimalConstants.dll |
||||
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) |
||||
.imagebase 0x10000000 |
||||
.file alignment 0x00000200 |
||||
.stackreserve 0x00100000 |
||||
.subsystem 0x0003 // WINDOWS_CUI |
||||
.corflags 0x00000001 // ILONLY |
||||
|
||||
|
||||
// =============== CLASS MEMBERS DECLARATION =================== |
||||
|
||||
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants |
||||
extends [mscorlib]System.Object |
||||
{ |
||||
.field private static initonly valuetype [mscorlib]System.Decimal constant |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8, |
||||
uint8, |
||||
uint32, |
||||
uint32, |
||||
uint32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 0A 00 00 00 |
||||
00 00 ) |
||||
.method private hidebysig instance void |
||||
MethodWithOptionalParameter([opt] valuetype [mscorlib]System.Decimal parameter) cil managed |
||||
{ |
||||
.param [1] |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8, |
||||
uint8, |
||||
uint32, |
||||
uint32, |
||||
uint32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 0A 00 00 00 |
||||
00 00 ) |
||||
// Code size 1 (0x1) |
||||
.maxstack 8 |
||||
IL_0000: ret |
||||
} // end of method NoDecimalConstants::MethodWithOptionalParameter |
||||
|
||||
.method public hidebysig specialname rtspecialname |
||||
instance void .ctor() cil managed |
||||
{ |
||||
// Code size 7 (0x7) |
||||
.maxstack 8 |
||||
IL_0000: ldarg.0 |
||||
IL_0001: call instance void [mscorlib]System.Object::.ctor() |
||||
IL_0006: ret |
||||
} // end of method NoDecimalConstants::.ctor |
||||
|
||||
.method private hidebysig specialname rtspecialname static |
||||
void .cctor() cil managed |
||||
{ |
||||
// Code size 17 (0x11) |
||||
.maxstack 8 |
||||
IL_0000: ldc.i4.s 10 |
||||
IL_0002: ldc.i4.0 |
||||
IL_0003: ldc.i4.0 |
||||
IL_0004: ldc.i4.0 |
||||
IL_0005: ldc.i4.1 |
||||
IL_0006: newobj instance void [mscorlib]System.Decimal::.ctor(int32, |
||||
int32, |
||||
int32, |
||||
bool, |
||||
uint8) |
||||
IL_000b: stsfld valuetype [mscorlib]System.Decimal ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants::constant |
||||
IL_0010: ret |
||||
} // end of method NoDecimalConstants::.cctor |
||||
|
||||
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants |
||||
|
||||
|
||||
// ============================================================= |
||||
|
||||
// *********** DISASSEMBLY COMPLETE *********************** |
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
|
||||
|
||||
|
||||
|
||||
// Metadata version: v4.0.30319 |
||||
.assembly extern mscorlib |
||||
{ |
||||
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. |
||||
.ver 4:0:0:0 |
||||
} |
||||
.assembly NoDecimalConstants |
||||
{ |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx |
||||
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. |
||||
|
||||
// --- The following custom attribute is added automatically, do not uncomment ------- |
||||
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) |
||||
|
||||
.permissionset reqmin |
||||
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} |
||||
.hash algorithm 0x00008004 |
||||
.ver 0:0:0:0 |
||||
} |
||||
.module NoDecimalConstants.dll |
||||
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) |
||||
.imagebase 0x10000000 |
||||
.file alignment 0x00000200 |
||||
.stackreserve 0x00100000 |
||||
.subsystem 0x0003 // WINDOWS_CUI |
||||
.corflags 0x00000001 // ILONLY |
||||
|
||||
|
||||
// =============== CLASS MEMBERS DECLARATION =================== |
||||
|
||||
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants |
||||
extends [mscorlib]System.Object |
||||
{ |
||||
.field private static initonly valuetype [mscorlib]System.Decimal constant |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8, |
||||
uint8, |
||||
uint32, |
||||
uint32, |
||||
uint32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 0A 00 00 00 |
||||
00 00 ) |
||||
.method private hidebysig instance void |
||||
MethodWithOptionalParameter([opt] valuetype [mscorlib]System.Decimal parameter) cil managed |
||||
{ |
||||
.param [1] |
||||
.custom instance void [mscorlib]System.Runtime.CompilerServices.DecimalConstantAttribute::.ctor(uint8, |
||||
uint8, |
||||
uint32, |
||||
uint32, |
||||
uint32) = ( 01 00 01 00 00 00 00 00 00 00 00 00 0A 00 00 00 |
||||
00 00 ) |
||||
// Code size 2 (0x2) |
||||
.maxstack 8 |
||||
IL_0000: nop |
||||
IL_0001: ret |
||||
} // end of method NoDecimalConstants::MethodWithOptionalParameter |
||||
|
||||
.method public hidebysig specialname rtspecialname |
||||
instance void .ctor() cil managed |
||||
{ |
||||
// Code size 8 (0x8) |
||||
.maxstack 8 |
||||
IL_0000: ldarg.0 |
||||
IL_0001: call instance void [mscorlib]System.Object::.ctor() |
||||
IL_0006: nop |
||||
IL_0007: ret |
||||
} // end of method NoDecimalConstants::.ctor |
||||
|
||||
.method private hidebysig specialname rtspecialname static |
||||
void .cctor() cil managed |
||||
{ |
||||
// Code size 17 (0x11) |
||||
.maxstack 8 |
||||
IL_0000: ldc.i4.s 10 |
||||
IL_0002: ldc.i4.0 |
||||
IL_0003: ldc.i4.0 |
||||
IL_0004: ldc.i4.0 |
||||
IL_0005: ldc.i4.1 |
||||
IL_0006: newobj instance void [mscorlib]System.Decimal::.ctor(int32, |
||||
int32, |
||||
int32, |
||||
bool, |
||||
uint8) |
||||
IL_000b: stsfld valuetype [mscorlib]System.Decimal ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants::constant |
||||
IL_0010: ret |
||||
} // end of method NoDecimalConstants::.cctor |
||||
|
||||
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoDecimalConstants |
||||
|
||||
|
||||
// ============================================================= |
||||
|
||||
// *********** DISASSEMBLY COMPLETE *********************** |
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2018 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
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System.Reflection.Metadata; |
||||
|
||||
namespace ICSharpCode.Decompiler.TypeSystem.Implementation |
||||
{ |
||||
static class DecimalConstantHelper |
||||
{ |
||||
public static bool AllowsDecimalConstants(MetadataModule module) |
||||
{ |
||||
return ((module.TypeSystemOptions & TypeSystemOptions.DecimalConstants) == TypeSystemOptions.DecimalConstants); |
||||
} |
||||
|
||||
public static bool IsDecimalConstant(MetadataModule module, CustomAttributeHandleCollection attributeHandles) |
||||
{ |
||||
return attributeHandles.HasKnownAttribute(module.metadata, KnownAttribute.DecimalConstant); |
||||
} |
||||
|
||||
public static object GetDecimalConstantValue(MetadataModule module, CustomAttributeHandleCollection attributeHandles) |
||||
{ |
||||
var metadata = module.metadata; |
||||
foreach (var attributeHandle in attributeHandles) { |
||||
var attribute = metadata.GetCustomAttribute(attributeHandle); |
||||
if (attribute.IsKnownAttribute(metadata, KnownAttribute.DecimalConstant)) |
||||
return TryDecodeDecimalConstantAttribute(module, attribute); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
static decimal? TryDecodeDecimalConstantAttribute(MetadataModule module, System.Reflection.Metadata.CustomAttribute attribute) |
||||
{ |
||||
var attrValue = attribute.DecodeValue(module.TypeProvider); |
||||
if (attrValue.FixedArguments.Length != 5) |
||||
return null; |
||||
// DecimalConstantAttribute has the arguments (byte scale, byte sign, uint hi, uint mid, uint low) or (byte scale, byte sign, int hi, int mid, int low)
|
||||
// Both of these invoke the Decimal constructor (int lo, int mid, int hi, bool isNegative, byte scale) with explicit argument conversions if required.
|
||||
if (!(attrValue.FixedArguments[0].Value is byte scale && attrValue.FixedArguments[1].Value is byte sign)) |
||||
return null; |
||||
unchecked { |
||||
if (attrValue.FixedArguments[2].Value is uint hi |
||||
&& attrValue.FixedArguments[3].Value is uint mid |
||||
&& attrValue.FixedArguments[4].Value is uint lo) { |
||||
return new decimal((int)lo, (int)mid, (int)hi, sign != 0, scale); |
||||
} |
||||
} |
||||
{ |
||||
if (attrValue.FixedArguments[2].Value is int hi |
||||
&& attrValue.FixedArguments[3].Value is int mid |
||||
&& attrValue.FixedArguments[4].Value is int lo) { |
||||
return new decimal(lo, mid, hi, sign != 0, scale); |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue