Skip to content

Commit 0630f51

Browse files
committed
Clean up CSharpCompilerFix code
1 parent 151e01c commit 0630f51

File tree

1 file changed

+93
-68
lines changed

1 file changed

+93
-68
lines changed

core/RoslynCompilerFix/CSharpCompilerFix.cs

Lines changed: 93 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ class CSharpCompilerFix
1010
{
1111
public static void Process(AssemblyDefinition assemblyDef)
1212
{
13-
foreach (var moduleDef in assemblyDef.Modules)
13+
foreach (var module in assemblyDef.Modules)
1414
{
15-
foreach (var type in moduleDef.Types)
15+
var generatedNames = module.GetType("Microsoft.CodeAnalysis.CSharp.Symbols.GeneratedNames");
16+
if (generatedNames != null)
1617
{
17-
if (type.FullName == "Microsoft.CodeAnalysis.CSharp.Symbols.GeneratedNames")
18+
foreach (var method in generatedNames.Methods)
1819
{
19-
foreach (var method in type.Methods)
20-
{
21-
if (method.Name == "MakeHoistedLocalFieldName")
22-
Fix_GeneratedNames_MakeHoistedLocalFieldName(method);
23-
else if (method.Name == "MakeMethodScopedSynthesizedName")
24-
Fix_GeneratedNames_MakeMethodScopedSynthesizedName(method);
25-
else if (method.Name == "ThisProxyFieldName")
26-
Fix_GeneratedNames_ThisProxyFieldName(method);
27-
else if (method.Name == "MakeLambdaMethodName")
28-
Fix_GeneratedNames_MakeLambdaMethodName(method);
29-
}
20+
if (method.Name == "MakeHoistedLocalFieldName")
21+
Fix_GeneratedNames_MakeHoistedLocalFieldName(method);
22+
else if (method.Name == "MakeMethodScopedSynthesizedName")
23+
Fix_GeneratedNames_MakeMethodScopedSynthesizedName(method);
24+
else if (method.Name == "ThisProxyFieldName")
25+
Fix_GeneratedNames_ThisProxyFieldName(method);
26+
else if (method.Name == "MakeLambdaMethodName")
27+
Fix_GeneratedNames_MakeLambdaMethodName(method);
3028
}
31-
else if (type.FullName == "Microsoft.CodeAnalysis.CSharp.LambdaFrame")
29+
}
30+
31+
var lambdaFrame = module.GetType("Microsoft.CodeAnalysis.CSharp.LambdaFrame");
32+
if (lambdaFrame != null)
33+
{
34+
foreach (var method in lambdaFrame.Methods)
3235
{
33-
foreach (var method in type.Methods)
34-
{
35-
if (method.Name == ".ctor")
36-
Fix_LambdaFrame_Constructor(method);
37-
}
36+
if (method.Name == ".ctor")
37+
Fix_LambdaFrame_Constructor(method);
3838
}
3939
}
4040
}
@@ -44,8 +44,13 @@ static void Fix_GeneratedNames_MakeHoistedLocalFieldName(MethodDefinition method
4444
{
4545
// Goal:
4646
// Rename <v>4__1 to <v>__1 for local variables in iterator
47-
// How:
48-
// Remove following code from body of GeneratedNames.MakeHoistedLocalFieldName
47+
//
48+
// How for Roslyn:
49+
// Remove following code from GeneratedNames.MakeHoistedLocalFieldName
50+
// builder.Append((char)GeneratedNameKind.HoistedLocalField);
51+
//
52+
// How for IL:
53+
// Remove following code from body of method
4954
// IL_003c: ldloc.0
5055
// IL_003d: ldc.i4.s 53
5156
// IL_003f: callvirt System.Text.StringBuilder System.Text.StringBuilder::Append(System.Char)
@@ -68,45 +73,52 @@ static void Fix_GeneratedNames_MakeMethodScopedSynthesizedName(MethodDefinition
6873
{
6974
// Goal:
7075
// Rename <TestCoroutine>d__1 to <TestCoroutine>c__Iterator0 for iterator class name
71-
// How:
72-
// Append following code from MakeMethodScopedSynthesizedName
73-
// IL_0033: ldloc.0
74-
// IL_0034: ldc.i4.s 62
75-
// IL_0036: callvirt instance class [System.Runtime]System.Text.StringBuilder [System.Runtime]System.Text.StringBuilder::Append(char)
76-
// IL_003b: pop
77-
// IL_003c: ldloc.0
78-
// IL_003d: ldarg.0
79-
// IL_003e: conv.u2
80-
// IL_003f: callvirt instance class [System.Runtime]System.Text.StringBuilder [System.Runtime]System.Text.StringBuilder::Append(char)
81-
// IL_0044: pop
82-
// >> IL_?: ldarg.0
83-
// >> IL_?: ldc.i4.s 100
84-
// >> IL_?: bne.un.s OUT
85-
// >> IL_?: ldloc.0
86-
// >> IL_?: ldstr "__Iterator"
87-
// >> IL_?: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
88-
// >> IL_?: pop
76+
//
77+
// How for Roslyn:
78+
// Append code following code at GeneratedNames.MakeMethodScopedSynthesizedName
79+
// builder.Append('>');
80+
// builder.Append((char)kind);
81+
// >> if (kind == GeneratedNameKind.StateMachineType) {
82+
// >> builder.Append("__Iterator")
83+
// >> }
84+
//
85+
// How for IL:
86+
// IL_0033: ldloc.0
87+
// IL_0034: ldc.i4.s 62
88+
// IL_0036: callvirt instance class [System.Runtime]System.Text.StringBuilder [System.Runtime]System.Text.StringBuilder::Append(char)
89+
// IL_003b: pop
90+
// IL_003c: ldloc.0
91+
// IL_003d: ldarg.0
92+
// IL_003e: conv.u2
93+
// IL_003f: callvirt instance class [System.Runtime]System.Text.StringBuilder [System.Runtime]System.Text.StringBuilder::Append(char)
94+
// IL_0044: pop
95+
// >> IL_?: ldarg.0
96+
// >> IL_?: ldc.i4.s IL_BASE
97+
// >> IL_?: bne.un.s OUT
98+
// >> IL_?: ldloc.0
99+
// >> IL_?: ldstr "__Iterator"
100+
// >> IL_?: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
101+
// >> IL_?: pop
102+
// IL_BASE:
89103

90104
var il = method.Body.GetILProcessor();
91105
for (var i = 0; i < il.Body.Instructions.Count; i++)
92106
{
93107
var inst = il.Body.Instructions[i];
94108
if (inst.OpCode.Code == Code.Ldc_I4_S && (sbyte)inst.Operand == 62)
95109
{
96-
var baseInst = il.Body.Instructions[i + 7];
97-
var baseInst2 = il.Body.Instructions[i + 8];
98-
var appendCharRef = ((MethodReference)il.Body.Instructions[i + 1].Operand);
110+
var baseInst = il.Body.Instructions[i + 8];
99111

100112
var appendStringMethod = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) });
101-
var appendString = appendCharRef.DeclaringType.Module.Import(appendStringMethod);
102-
103-
il.InsertAfter(baseInst, il.Create(OpCodes.Pop));
104-
il.InsertAfter(baseInst, il.Create(OpCodes.Callvirt, appendString));
105-
il.InsertAfter(baseInst, il.Create(OpCodes.Ldstr, "__Iterator"));
106-
il.InsertAfter(baseInst, il.Create(OpCodes.Ldloc_0));
107-
il.InsertAfter(baseInst, il.Create(OpCodes.Bne_Un_S, baseInst2));
108-
il.InsertAfter(baseInst, il.Create(OpCodes.Ldc_I4_S, (sbyte)100));
109-
il.InsertAfter(baseInst, il.Create(OpCodes.Ldarg_0));
113+
var appendString = method.Module.Import(appendStringMethod);
114+
115+
il.InsertBefore(baseInst, il.Create(OpCodes.Ldarg_0));
116+
il.InsertBefore(baseInst, il.Create(OpCodes.Ldc_I4_S, (sbyte)100));
117+
il.InsertBefore(baseInst, il.Create(OpCodes.Bne_Un_S, baseInst));
118+
il.InsertBefore(baseInst, il.Create(OpCodes.Ldloc_0));
119+
il.InsertBefore(baseInst, il.Create(OpCodes.Ldstr, "__Iterator"));
120+
il.InsertBefore(baseInst, il.Create(OpCodes.Callvirt, appendString));
121+
il.InsertBefore(baseInst, il.Create(OpCodes.Pop));
110122
break;
111123
}
112124
}
@@ -116,7 +128,14 @@ static void Fix_GeneratedNames_ThisProxyFieldName(MethodDefinition method)
116128
{
117129
// Goal:
118130
// Rename <>4__this to <>f__this for this in iterator class
119-
// How:
131+
//
132+
// How for Roslyn:
133+
// Modify return statement of GeneratedNames.ThisProxyFieldNam from
134+
// return "<>4__this";
135+
// to
136+
// return "<>f__this";
137+
//
138+
// How for IL:
120139
// Change literal "<>4__this" to "<>f__this" at ThisProxyFieldName()
121140
// IL_0000: ldstr "<>4__this"
122141
// IL_0005: ret
@@ -142,24 +161,23 @@ static void Fix_LambdaFrame_Constructor(MethodDefinition method)
142161
// Modify constructor of LambdaFrame from
143162
// LambdaFrame(MethodSymbol topLevelMethod, ...)
144163
// : base(MakeName(...
145-
// To
164+
// to
146165
// LambdaFrame(MethodSymbol topLevelMethod, ...)
147166
// : base("<" + topLevelMethod.Name + ">c__AnonStorey" + MakeName(...
148167
//
149168
// How for IL:
150-
// Insert codes like:
151-
// IL_0000: ldarg.0
152-
// >> IL_?: ldstr "<"
153-
// >> IL_?: ldarg.1
154-
// >> IL_?: callvirt instance string Microsoft.CodeAnalysis.CSharp.Symbol::get_Name()
155-
// >> IL_?: ldstr ">c__AnonStorey"
156-
// IL_0001: ldarg.2
157-
// IL_0002: ldarg.3
158-
// IL_0003: ldarg.s closureId
159-
// IL_0005: call string Microsoft.CodeAnalysis.CSharp.LambdaFrame::MakeName(class Microsoft.CodeAnalysis.SyntaxNode, valuetype Microsoft.CodeAnalysis.CodeGen.DebugId, valuetype Microsoft.CodeAnalysis.CodeGen.DebugId)
160-
// >> IL_?: call string [mscorlib]System.String::Concat(string, string, string, string)
161-
// IL_000a: ldarg.1
162-
// IL_000b: call instance void Microsoft.CodeAnalysis.CSharp.Symbols.SynthesizedContainer::.ctor(string, class Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol)
169+
// IL_0000: ldarg.0
170+
// >> IL_?: ldstr "<"
171+
// >> IL_?: ldarg.1
172+
// >> IL_?: callvirt instance string Microsoft.CodeAnalysis.CSharp.Symbol::get_Name()
173+
// >> IL_?: ldstr ">c__AnonStorey"
174+
// IL_0001: ldarg.2
175+
// IL_0002: ldarg.3
176+
// IL_0003: ldarg.s closureId
177+
// IL_0005: call string Microsoft.CodeAnalysis.CSharp.LambdaFrame::MakeName(class Microsoft.CodeAnalysis.SyntaxNode, valuetype Microsoft.CodeAnalysis.CodeGen.DebugId, valuetype Microsoft.CodeAnalysis.CodeGen.DebugId)
178+
// >> IL_?: call string [mscorlib]System.String::Concat(string, string, string, string)
179+
// IL_000a: ldarg.1
180+
// IL_000b: call instance void Microsoft.CodeAnalysis.CSharp.Symbols.SynthesizedContainer::.ctor(string, class Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol)
163181

164182
var il = method.Body.GetILProcessor();
165183
var baseInst = il.Body.Instructions[1];
@@ -188,7 +206,14 @@ static void Fix_GeneratedNames_MakeLambdaMethodName(MethodDefinition method)
188206
{
189207
// Goal:
190208
// Rename <Start>b__0 to <>m__<Start>b__0 for method name in lambda class
191-
// How:
209+
//
210+
// How for Roslyn:
211+
// Modify return statement of GeneratedNames.MakeLambdaMethodName from
212+
// return MakeMethodScopedSynthesizedName(GeneratedNameKind.LambdaMethod, ...
213+
// to
214+
// return "<>m__" + MakeMethodScopedSynthesizedName(GeneratedNameKind.LambdaMethod, ...
215+
//
216+
// How for IL:
192217
// >> IL_?: ldstr "<>m__"
193218
// IL_0000: ldc.i4.s 57
194219
// IL_0002: ldarg.0

0 commit comments

Comments
 (0)