Browse Source

Fix #1309: Decompilation of dynamic casts.

pull/1317/head
Daniel Grunwald 7 years ago
parent
commit
35a0ee2263
  1. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs
  2. 90
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.il
  3. 74
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.opt.il
  4. 68
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.opt.roslyn.il
  5. 84
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.roslyn.il
  6. 2
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs
  7. 12
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  8. 17
      ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs
  9. 44
      ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

10
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.cs

@ -435,5 +435,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -435,5 +435,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
Console.WriteLine(GetBool(1) || GetDynamic(2));
Console.WriteLine(i == 1 || d == null);
}
private static int ImplicitCast(object o)
{
return (dynamic)o;
}
private static int ExplicitCast(object o)
{
return (int)(dynamic)o;
}
}
}

90
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.il

@ -20,10 +20,10 @@ @@ -20,10 +20,10 @@
}
.assembly DynamicTests
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 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.
.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.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
@ -112,8 +112,8 @@ @@ -112,8 +112,8 @@
.field public object Field
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private object '<Property>k__BackingField'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig specialname
instance object get_GetOnlyProperty() cil managed
{
@ -802,12 +802,26 @@ @@ -802,12 +802,26 @@
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`4<class [System.Core]System.Runtime.CompilerServices.CallSite,object,object,object>> '<>p__Site17b'
} // end of class '<LogicOrExtended>o__SiteContainer170'
.class abstract auto ansi sealed nested private beforefieldinit '<ImplicitCast>o__SiteContainer17c'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__Site17d'
} // end of class '<ImplicitCast>o__SiteContainer17c'
.class abstract auto ansi sealed nested private beforefieldinit '<ExplicitCast>o__SiteContainer17e'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__Site17f'
} // end of class '<ExplicitCast>o__SiteContainer17e'
.field private static object 'field'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private static object objectField
.field private object '<Property>k__BackingField'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig specialname instance object
get_Property() cil managed
{
@ -15600,6 +15614,76 @@ @@ -15600,6 +15614,76 @@
IL_043f: ret
} // end of method DynamicTests::LogicOrExtended
.method private hidebysig static int32
ImplicitCast(object o) cil managed
{
// Code size 72 (0x48)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_0006: brtrue.s IL_002e
IL_0008: ldc.i4.0
IL_0009: ldtoken [mscorlib]System.Int32
IL_000e: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0013: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0018: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001d: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0022: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0027: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_002c: br.s IL_002e
IL_002e: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_0033: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0038: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_003d: ldarg.0
IL_003e: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0043: stloc.0
IL_0044: br.s IL_0046
IL_0046: ldloc.0
IL_0047: ret
} // end of method DynamicTests::ImplicitCast
.method private hidebysig static int32
ExplicitCast(object o) cil managed
{
// Code size 73 (0x49)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_0006: brtrue.s IL_002f
IL_0008: ldc.i4.s 16
IL_000a: ldtoken [mscorlib]System.Int32
IL_000f: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0014: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0019: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001e: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0023: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0028: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_002d: br.s IL_002f
IL_002f: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_0034: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0039: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_003e: ldarg.0
IL_003f: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0044: stloc.0
IL_0045: br.s IL_0047
IL_0047: ldloc.0
IL_0048: ret
} // end of method DynamicTests::ExplicitCast
.property instance object Property()
{
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )

74
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.opt.il

@ -20,10 +20,10 @@ @@ -20,10 +20,10 @@
}
.assembly DynamicTests.opt
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 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.
.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.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
@ -105,8 +105,8 @@ @@ -105,8 +105,8 @@
.field public object Field
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private object '<Property>k__BackingField'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig specialname
instance object get_GetOnlyProperty() cil managed
{
@ -783,12 +783,26 @@ @@ -783,12 +783,26 @@
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`4<class [System.Core]System.Runtime.CompilerServices.CallSite,object,object,object>> '<>p__Site17b'
} // end of class '<LogicOrExtended>o__SiteContainer170'
.class abstract auto ansi sealed nested private beforefieldinit '<ImplicitCast>o__SiteContainer17c'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__Site17d'
} // end of class '<ImplicitCast>o__SiteContainer17c'
.class abstract auto ansi sealed nested private beforefieldinit '<ExplicitCast>o__SiteContainer17e'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__Site17f'
} // end of class '<ExplicitCast>o__SiteContainer17e'
.field private static object 'field'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private static object objectField
.field private object '<Property>k__BackingField'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.method public hidebysig specialname instance object
get_Property() cil managed
{
@ -14889,6 +14903,60 @@ @@ -14889,6 +14903,60 @@
IL_044b: ret
} // end of method DynamicTests::LogicOrExtended
.method private hidebysig static int32
ImplicitCast(object o) cil managed
{
// Code size 65 (0x41)
.maxstack 3
IL_0000: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_0005: brtrue.s IL_002b
IL_0007: ldc.i4.0
IL_0008: ldtoken [mscorlib]System.Int32
IL_000d: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0012: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0017: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001c: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0021: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0026: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_002b: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_0030: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0035: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ImplicitCast>o__SiteContainer17c'::'<>p__Site17d'
IL_003a: ldarg.0
IL_003b: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0040: ret
} // end of method DynamicTests::ImplicitCast
.method private hidebysig static int32
ExplicitCast(object o) cil managed
{
// Code size 66 (0x42)
.maxstack 3
IL_0000: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_0005: brtrue.s IL_002c
IL_0007: ldc.i4.s 16
IL_0009: ldtoken [mscorlib]System.Int32
IL_000e: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0013: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0018: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001d: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0022: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0027: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_002c: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_0031: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0036: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<ExplicitCast>o__SiteContainer17e'::'<>p__Site17f'
IL_003b: ldarg.0
IL_003c: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0041: ret
} // end of method DynamicTests::ExplicitCast
.property instance object Property()
{
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )

68
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.opt.roslyn.il

@ -785,6 +785,20 @@ @@ -785,6 +785,20 @@
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> '<>p__10'
} // end of class '<>o__55'
.class abstract auto ansi sealed nested private beforefieldinit '<>o__56'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__0'
} // end of class '<>o__56'
.class abstract auto ansi sealed nested private beforefieldinit '<>o__57'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__0'
} // end of class '<>o__57'
.field private static object 'field'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private static object objectField
@ -13951,6 +13965,60 @@ @@ -13951,6 +13965,60 @@
IL_040a: ret
} // end of method DynamicTests::LogicOrExtended
.method private hidebysig static int32
ImplicitCast(object o) cil managed
{
// Code size 65 (0x41)
.maxstack 3
IL_0000: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_0005: brtrue.s IL_002b
IL_0007: ldc.i4.0
IL_0008: ldtoken [mscorlib]System.Int32
IL_000d: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0012: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0017: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001c: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0021: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0026: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_002b: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_0030: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0035: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_003a: ldarg.0
IL_003b: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0040: ret
} // end of method DynamicTests::ImplicitCast
.method private hidebysig static int32
ExplicitCast(object o) cil managed
{
// Code size 66 (0x42)
.maxstack 3
IL_0000: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_0005: brtrue.s IL_002c
IL_0007: ldc.i4.s 16
IL_0009: ldtoken [mscorlib]System.Int32
IL_000e: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0013: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_0018: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001d: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0022: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0027: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_002c: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_0031: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0036: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_003b: ldarg.0
IL_003c: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0041: ret
} // end of method DynamicTests::ExplicitCast
.property instance object Property()
{
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )

84
ICSharpCode.Decompiler.Tests/TestCases/Pretty/DynamicTests.roslyn.il

@ -792,6 +792,20 @@ @@ -792,6 +792,20 @@
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> '<>p__10'
} // end of class '<>o__55'
.class abstract auto ansi sealed nested private beforefieldinit '<>o__56'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__0'
} // end of class '<>o__56'
.class abstract auto ansi sealed nested private beforefieldinit '<>o__57'
extends [mscorlib]System.Object
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public static class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> '<>p__0'
} // end of class '<>o__57'
.field private static object 'field'
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )
.field private static object objectField
@ -14904,6 +14918,76 @@ @@ -14904,6 +14918,76 @@
IL_0425: ret
} // end of method DynamicTests::LogicOrExtended
.method private hidebysig static int32
ImplicitCast(object o) cil managed
{
// Code size 72 (0x48)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_0006: brfalse.s IL_000a
IL_0008: br.s IL_002e
IL_000a: ldc.i4.0
IL_000b: ldtoken [mscorlib]System.Int32
IL_0010: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0015: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_001a: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_001f: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0024: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_0029: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_002e: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_0033: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0038: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__56'::'<>p__0'
IL_003d: ldarg.0
IL_003e: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0043: stloc.0
IL_0044: br.s IL_0046
IL_0046: ldloc.0
IL_0047: ret
} // end of method DynamicTests::ImplicitCast
.method private hidebysig static int32
ExplicitCast(object o) cil managed
{
// Code size 73 (0x49)
.maxstack 3
.locals init (int32 V_0)
IL_0000: nop
IL_0001: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_0006: brfalse.s IL_000a
IL_0008: br.s IL_002f
IL_000a: ldc.i4.s 16
IL_000c: ldtoken [mscorlib]System.Int32
IL_0011: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0016: ldtoken ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests
IL_001b: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0020: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
class [mscorlib]System.Type,
class [mscorlib]System.Type)
IL_0025: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_002a: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_002f: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_0034: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>>::Target
IL_0039: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>> ICSharpCode.Decompiler.Tests.TestCases.Pretty.DynamicTests/'<>o__57'::'<>p__0'
IL_003e: ldarg.0
IL_003f: callvirt instance !2 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite,object,int32>::Invoke(!0,
!1)
IL_0044: stloc.0
IL_0045: br.s IL_0047
IL_0047: ldloc.0
IL_0048: ret
} // end of method DynamicTests::ExplicitCast
.property instance object Property()
{
.custom instance void [System.Core]System.Runtime.CompilerServices.DynamicAttribute::.ctor() = ( 01 00 00 00 )

2
ICSharpCode.Decompiler.Tests/TestCases/Pretty/ExpressionTrees.cs

@ -533,7 +533,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -533,7 +533,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public void MembersBuiltin()
{
ToCode(X(), () => 1.23m.ToString());
ToCode(X(), () => AttributeTargets.All.HasFlag((Enum)AttributeTargets.Assembly));
ToCode(X(), () => AttributeTargets.All.HasFlag(AttributeTargets.Assembly));
ToCode(X(), () => "abc".Length == 3);
ToCode(X(), () => 'a'.CompareTo('b') < 0);
}

12
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -2425,9 +2425,15 @@ namespace ICSharpCode.Decompiler.CSharp @@ -2425,9 +2425,15 @@ namespace ICSharpCode.Decompiler.CSharp
protected internal override TranslatedExpression VisitDynamicConvertInstruction(DynamicConvertInstruction inst, TranslationContext context)
{
// TODO : make conversions implicit, if !inst.IsExplicit
// currently this leads to stack type mismatch assertions, if the expected type is not O
return Translate(inst.Argument).ConvertTo(inst.Type, this, inst.IsChecked, allowImplicitConversion: false);
var operand = Translate(inst.Argument).ConvertTo(SpecialType.Dynamic, this);
var result = new CastExpression(ConvertType(inst.Type), operand)
.WithILInstruction(inst)
.WithRR(new ConversionResolveResult(
inst.Type, operand.ResolveResult,
inst.IsExplicit ? Conversion.ExplicitDynamicConversion : Conversion.ImplicitDynamicConversion
));
result.Expression.AddAnnotation(inst.IsChecked ? AddCheckedBlocks.CheckedAnnotation : AddCheckedBlocks.UncheckedAnnotation);
return result;
}
protected internal override TranslatedExpression VisitDynamicGetIndexInstruction(DynamicGetIndexInstruction inst, TranslationContext context)

17
ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs

@ -650,9 +650,9 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -650,9 +650,9 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
|| kind == TypeKind.Delegate || kind == TypeKind.Anonymous;
}
#endregion
#region Boxing Conversions
public bool IsBoxingConversion(IType fromType, IType toType)
bool IsBoxingConversion(IType fromType, IType toType)
{
// C# 4.0 spec: §6.1.7
fromType = NullableType.GetUnderlyingType(fromType);
@ -661,7 +661,18 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -661,7 +661,18 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
else
return false;
}
/// <summary>
/// Gets whether the conversion from fromType to toType is a boxing conversion,
/// or an implicit conversion involving a type parameter that might be
/// a boxing conversion when instantiated with a value type.
/// </summary>
public bool IsBoxingConversionOrInvolvingTypeParameter(IType fromType, IType toType)
{
return IsBoxingConversion(fromType, toType)
|| ImplicitTypeParameterConversion(fromType, toType);
}
bool UnboxingConversion(IType fromType, IType toType)
{
// C# 4.0 spec: §6.2.5

44
ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

@ -185,11 +185,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -185,11 +185,12 @@ namespace ICSharpCode.Decompiler.CSharp
if (allowImplicitConversion) {
switch (ResolveResult) {
case ConversionResolveResult conversion: {
if (Expression is CastExpression cast
&& ((type.IsKnownType(KnownTypeCode.Object) && conversion.Conversion.IsBoxingConversion
|| conversion.Conversion.IsAnonymousFunctionConversion
|| (conversion.Conversion.IsImplicit && (conversion.Conversion.IsUserDefined || targetType.IsKnownType(KnownTypeCode.Decimal)))
) || (conversion.Conversion.IsInterpolatedStringConversion))) {
if (Expression is CastExpression cast && CastCanBeMadeImplicit(
Resolver.CSharpConversions.Get(expressionBuilder.compilation),
conversion.Conversion,
conversion.Input.Type,
type, targetType
)) {
return this.UnwrapChild(cast.Expression);
} else if (Expression is ObjectCreateExpression oce && conversion.Conversion.IsMethodGroupConversion
&& oce.Arguments.Count == 1 && expressionBuilder.settings.UseImplicitMethodGroupConversion) {
@ -228,7 +229,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -228,7 +229,7 @@ namespace ICSharpCode.Decompiler.CSharp
var compilation = expressionBuilder.compilation;
var conversions = Resolver.CSharpConversions.Get(compilation);
if (ResolveResult is ConversionResolveResult conv && Expression is CastExpression cast2 &&
IsBoxingOrInterpolatedStringConversion(conversions, conv.Conversion, conv.Input.Type, targetType))
CastCanBeMadeImplicit(conversions, conv.Conversion, conv.Input.Type, type, targetType))
{
var unwrapped = this.UnwrapChild(cast2.Expression);
if (allowImplicitConversion)
@ -393,14 +394,31 @@ namespace ICSharpCode.Decompiler.CSharp @@ -393,14 +394,31 @@ namespace ICSharpCode.Decompiler.CSharp
}
return castExpr.WithoutILInstruction().WithRR(rr);
}
bool IsBoxingOrInterpolatedStringConversion(Resolver.CSharpConversions conversions, Conversion conversion, IType inputType, IType targetType)
/// <summary>
/// Gets whether an implicit conversion from 'inputType' to 'newTargetType'
/// would have the same semantics as the existing cast from 'inputType' to 'oldTargetType'.
/// The existing cast is classified in 'conversion'.
/// </summary>
bool CastCanBeMadeImplicit(Resolver.CSharpConversions conversions, Conversion conversion, IType inputType, IType oldTargetType, IType newTargetType)
{
if (conversion.IsBoxingConversion && conversions.IsBoxingConversion(inputType, targetType))
return true;
if (conversion.IsInterpolatedStringConversion && (targetType.IsKnownType(KnownTypeCode.FormattableString)
|| targetType.IsKnownType(KnownTypeCode.IFormattable)))
return true;
if (!conversion.IsImplicit) {
// If the cast was required for the old conversion, avoid making it implicit.
return false;
}
if (conversion.IsBoxingConversion) {
return conversions.IsBoxingConversionOrInvolvingTypeParameter(inputType, newTargetType);
}
if (conversion.IsInterpolatedStringConversion) {
return newTargetType.IsKnownType(KnownTypeCode.FormattableString)
|| newTargetType.IsKnownType(KnownTypeCode.IFormattable);
}
if (conversion.IsAnonymousFunctionConversion) {
return oldTargetType.Equals(newTargetType);
}
if (conversion.IsUserDefined || newTargetType.IsKnownType(KnownTypeCode.Decimal) || conversion.IsDynamicConversion) {
return oldTargetType.Equals(newTargetType);
}
return false;
}

Loading…
Cancel
Save