Skip to content

Commit de69e83

Browse files
buyaa-nAaronRobinsonMSFTjkotas
authored andcommitted
Abstract out LocalBuilder, emit Locals with new ILGenerator (dotnet#93809)
* Abstract LocalBuilder, emit LocalBuilder in ILGenerator * Apply suggestions from code review Co-authored-by: Aaron Robinson <arobins@microsoft.com> * Remove public LocalBuilder.Method, and apply other feedback * Avoid invalid cast exception. Co-authored-by: Jan Kotas <jkotas@microsoft.com> --------- Co-authored-by: Aaron Robinson <arobins@microsoft.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com>
1 parent 0a4abb6 commit de69e83

19 files changed

Lines changed: 326 additions & 77 deletions

File tree

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@
162162
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\CustomAttributeBuilder.cs" />
163163
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicILGenerator.cs" />
164164
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\DynamicMethod.CoreCLR.cs" />
165-
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\LocalBuilder.cs" />
166165
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeAssemblyBuilder.cs" />
167166
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeConstructorBuilder.cs" />
168167
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEnumBuilder.cs" />
169168
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeEventBuilder.cs" />
170169
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeFieldBuilder.cs" />
171170
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeGenericTypeParameterBuilder.cs" />
172171
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeILGenerator.cs" />
172+
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeLocalBuilder.cs" />
173173
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeMethodBuilder.cs" />
174174
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeModuleBuilder.cs" />
175175
<Compile Include="$(BclSourcesRoot)\System\Reflection\Emit\RuntimeParameterBuilder.cs" />

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public override LocalBuilder DeclareLocal(Type localType, bool pinned)
3636
{
3737
ArgumentNullException.ThrowIfNull(localType);
3838

39-
LocalBuilder localBuilder;
39+
RuntimeLocalBuilder localBuilder;
4040

4141
RuntimeType? rtType = localType as RuntimeType;
4242

4343
if (rtType == null)
4444
throw new ArgumentException(SR.Argument_MustBeRuntimeType);
4545

46-
localBuilder = new LocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
46+
localBuilder = new RuntimeLocalBuilder(m_localCount, localType, m_methodBuilder, pinned);
4747
// add the localType to local signature
4848
m_localSignature.AddArgument(localType, pinned);
4949
m_localCount++;

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeILGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,8 @@ public override void Emit(OpCode opcode, LocalBuilder local)
819819
ArgumentNullException.ThrowIfNull(local);
820820

821821
// Puts the opcode onto the IL stream followed by the information for local variable local.
822-
int tempVal = local.GetLocalIndex();
823-
if (local.GetMethodBuilder() != m_methodBuilder)
822+
int tempVal = local.LocalIndex;
823+
if (local is not RuntimeLocalBuilder localBuilder || localBuilder.GetMethodBuilder() != m_methodBuilder)
824824
{
825825
throw new ArgumentException(SR.Argument_UnmatchedMethodForLocal, nameof(local));
826826
}
@@ -1185,7 +1185,7 @@ public override LocalBuilder DeclareLocal(Type localType, bool pinned)
11851185
// add the localType to local signature
11861186
m_localSignature.AddArgument(localType, pinned);
11871187

1188-
return new LocalBuilder(m_localCount++, localType, methodBuilder, pinned);
1188+
return new RuntimeLocalBuilder(m_localCount++, localType, methodBuilder, pinned);
11891189
}
11901190

11911191
public override void UsingNamespace(string usingNamespace)

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs renamed to src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeLocalBuilder.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace System.Reflection.Emit
55
{
6-
public sealed class LocalBuilder : LocalVariableInfo
6+
internal sealed class RuntimeLocalBuilder : LocalBuilder
77
{
88
#region Private Data Members
99
private readonly int m_localIndex;
@@ -13,9 +13,9 @@ public sealed class LocalBuilder : LocalVariableInfo
1313
#endregion
1414

1515
#region Constructor
16-
internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder)
16+
internal RuntimeLocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder)
1717
: this(localIndex, localType, methodBuilder, false) { }
18-
internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder, bool isPinned)
18+
internal RuntimeLocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder, bool isPinned)
1919
{
2020
m_isPinned = isPinned;
2121
m_localIndex = localIndex;
@@ -25,14 +25,7 @@ internal LocalBuilder(int localIndex, Type localType, MethodInfo methodBuilder,
2525
#endregion
2626

2727
#region Internal Members
28-
internal int GetLocalIndex()
29-
{
30-
return m_localIndex;
31-
}
32-
internal MethodInfo GetMethodBuilder()
33-
{
34-
return m_methodBuilder;
35-
}
28+
internal MethodInfo GetMethodBuilder() => m_methodBuilder;
3629
#endregion
3730

3831
#region LocalVariableInfo Override

src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@
144144
<Compile Include="System\Reflection\Emit\CustomAttributeBuilder.cs" />
145145
<Compile Include="System\Reflection\Emit\DynamicILInfo.cs" />
146146
<Compile Include="System\Reflection\Emit\DynamicMethod.cs" />
147-
<Compile Include="System\Reflection\Emit\LocalBuilder.cs" />
148147
<Compile Include="System\Reflection\Emit\ReflectionEmitThrower.cs" />
149148
<Compile Include="System\Reflection\Emit\SignatureHelper.cs" />
150149
<Compile Include="System\Reflection\EnumInfo.cs" />

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Emit/LocalBuilder.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@
418418
<Compile Include="$(MSBuildThisFileDirectory)System\IFormatProvider.cs" />
419419
<Compile Include="$(MSBuildThisFileDirectory)System\IFormattable.cs" />
420420
<Compile Include="$(MSBuildThisFileDirectory)System\Index.cs" />
421-
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ILGenerator.cs" />
422421
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\BitVector256.cs" />
423422
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\ProbabilisticWithAsciiCharSearchValues.cs" />
424423
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\SingleCharSearchValues.cs" />
@@ -674,7 +673,9 @@
674673
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FieldOnTypeBuilderInstantiation.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
675674
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\FlowControl.cs" />
676675
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\GenericTypeParameterBuilder.cs" />
676+
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\ILGenerator.cs" />
677677
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\Label.cs" />
678+
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\LocalBuilder.cs" />
678679
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodBuilder.cs" />
679680
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodBuilderInstantiation.cs" />
680681
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\MethodOnTypeBuilderInstantiation.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
4+
namespace System.Reflection.Emit
5+
{
6+
public abstract class LocalBuilder : LocalVariableInfo
7+
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="LocalBuilder"/> class.
10+
/// </summary>
11+
/// <remarks>
12+
/// This constructor is invoked by derived classes.
13+
/// </remarks>
14+
protected LocalBuilder() { }
15+
}
16+
}

src/libraries/System.Reflection.Emit.ILGeneration/ref/System.Reflection.Emit.ILGeneration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public virtual void ThrowException([System.Diagnostics.CodeAnalysis.DynamicallyA
6767
public static bool operator ==(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
6868
public static bool operator !=(System.Reflection.Emit.Label a, System.Reflection.Emit.Label b) { throw null; }
6969
}
70-
public sealed partial class LocalBuilder : System.Reflection.LocalVariableInfo
70+
public abstract class LocalBuilder : System.Reflection.LocalVariableInfo
7171
{
72-
internal LocalBuilder() { }
72+
protected LocalBuilder() { }
7373
public override bool IsPinned { get { throw null; } }
7474
public override int LocalIndex { get { throw null; } }
7575
public override System.Type LocalType { get { throw null; } }

src/libraries/System.Reflection.Emit/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@
186186
<data name="InvalidOperation_ShouldNotHaveMethodBody" xml:space="preserve">
187187
<value>Method body should not exist.</value>
188188
</data>
189+
<data name="Argument_UnmatchedMethodForLocal" xml:space="preserve">
190+
<value>Local passed in does not belong to this ILGenerator.</value>
191+
</data>
189192
<data name="Argument_InvalidLabel" xml:space="preserve">
190193
<value>Invalid Label.</value>
191194
</data>

0 commit comments

Comments
 (0)