119b893b8a1b8559a6335433994e76956d0577e2
[platform/upstream/coreclr.git] / tests / src / Loader / classloader / DefaultInterfaceMethods / constrainedcall / constrained2_gm.il
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 .assembly extern mscorlib { }
6 .assembly constrained2_gm { }
7
8 .class interface private abstract auto ansi IAdder`1<T>
9 {
10   .method public hidebysig newslot abstract virtual instance int32 Add<U>(int32)
11   {
12   }
13
14   .method public hidebysig newslot virtual instance int32 PlusPlus<U>()
15   {
16     ldstr "IAdder`1<T>:PlusPlus"
17     call void [mscorlib]System.Console::WriteLine(string)
18
19     ldarg.0
20     ldc.i4.1
21     callvirt instance int32 class IAdder`1<!0>::Add<!!0>(int32)
22     ret
23   }
24 }
25
26 .class value Adder`1<T> implements class IAdder`1<!T>, class IAdder`1<class [mscorlib]System.String>
27 {
28   .field private int32 _field
29
30   .method public hidebysig newslot virtual instance int32 Add<U>(int32) cil managed
31   {
32     ldarg.0
33     dup
34     ldfld int32 valuetype Adder`1<!T>::_field
35     ldarg.1
36     add
37     stfld int32 valuetype Adder`1<!T>::_field
38     ldarg.0
39     ldfld int32 valuetype Adder`1<!T>::_field
40     ret
41   }
42
43   .method private hidebysig newslot virtual instance int32  'IAdder<System.String>.PlusPlus'<U>()
44   {
45     .override class IAdder`1<class [mscorlib]System.String>::PlusPlus
46
47     ldstr "Adder`1<T>:'IAdder<System.String>.PlusPlus'"
48     call void [mscorlib]System.Console::WriteLine(string)
49
50     ldarg.0
51     ldc.i4.2
52     call instance int32 valuetype Adder`1<!T>::Add<!!U>(int32)
53     ret
54   }
55 }
56
57 .method public hidebysig static int32 Check<(class IAdder`1<!!U>, class IAdder`1<string>) T,U>(!!T t)
58 {
59   // The constrained calls below may or may not box for a valuetype T
60   // depending on whether IAdder`1<!!U>::PlusPlus() ends up calling
61   // the default implementation of the interface method.
62
63   ldarga.s 0
64   constrained. !!T
65   callvirt instance int32 class IAdder`1<string>::PlusPlus<object>()
66   ldarga.s 0
67   constrained. !!T
68   callvirt instance int32 class IAdder`1<string>::PlusPlus<object>()
69   add
70
71   ldarga.s 0
72   constrained. !!T
73   callvirt instance int32 class IAdder`1<!!U>::PlusPlus<object>()
74   ldarga.s 0
75   constrained. !!T
76   callvirt instance int32 class IAdder`1<!!U>::PlusPlus<object>()
77   add
78
79   add
80
81   ret
82 }
83
84 .method public hidebysig static int32 Main()
85 {
86     .entrypoint
87
88     .locals init (
89       valuetype Adder`1<object>,
90       int32
91     )
92
93     // This will end up calling the implementation of IAdder<string>.PlusPlus
94     // provided by the Adder valuetype.
95     // The sum returned by the Check method will be 2+4+6+8 = 20.
96     ldloc.0
97     call int32 Check<valuetype Adder`1<object>,string>(!!0)
98     ldc.i4 20
99     ceq
100     brtrue String_OK
101
102     ldc.i4 1
103     ret
104
105 String_OK:
106
107     // This will end up calling the implementation of IAdder<object>.PlusPlus
108     // provided by the default interface method.
109     // The sum returned by the Check method will be 1+1+2+4 = 8.
110
111     // This requires a runtime that can generate boxing stubs. We will support
112     // both the case when this is not supported, and when it's implemented.
113     ldc.i4.8
114     stloc.1
115
116     .try
117     {
118       ldloca 0
119       initobj valuetype Adder`1<object>
120       ldloc.0
121       call int32 Check<valuetype Adder`1<object>,object>(!!0)
122       stloc.1
123
124       ldstr "Runtime supports lookups with runtime determined boxing"
125       call void [mscorlib]System.Console::WriteLine(string)
126
127       leave AfterBoxingCall
128     }
129     catch [mscorlib]System.Exception
130     {
131       pop
132       leave AfterFailedBoxingCall
133     }
134
135 AfterFailedBoxingCall:
136     ldstr "Runtime does not support lookups with runtime determined boxing"
137     call void [mscorlib]System.Console::WriteLine(string)
138
139 AfterBoxingCall:
140     ldloc.1
141     ldc.i4 8
142     ceq
143     brtrue Object_OK
144
145     ldc.i4.2
146     ret
147
148 Object_OK:
149
150     ldc.i4 100
151     ret
152 }