Skip to content

Commit 61eed01

Browse files
authored
Merge pull request #708 from stakx/refactor/simple-ast-reference-methods
Refactor `SimpleAST`'s `Reference` base class
2 parents 93e20a0 + 3d3bf4a commit 61eed01

18 files changed

+131
-136
lines changed

src/Castle.Core/DynamicProxy/Generators/Emitters/ArgumentsUtil.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -58,18 +58,6 @@ public static IExpression[] ConvertToArgumentReferenceExpression(ParameterInfo[]
5858
return arguments;
5959
}
6060

61-
public static void EmitLoadOwnerAndReference(Reference reference, ILGenerator il)
62-
{
63-
if (reference == null)
64-
{
65-
return;
66-
}
67-
68-
EmitLoadOwnerAndReference(reference.OwnerReference, il);
69-
70-
reference.LoadReference(il);
71-
}
72-
7361
public static Type[] GetTypes(ParameterInfo[] parameters)
7462
{
7563
var types = new Type[parameters.Length];

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System;
@@ -35,12 +37,12 @@ public ArgumentReference(Type argumentType, int position)
3537

3638
internal int Position { get; set; }
3739

38-
public override void LoadAddressOfReference(ILGenerator gen)
40+
public override void EmitAddress(ILGenerator gen)
3941
{
4042
throw new NotSupportedException();
4143
}
4244

43-
public override void LoadReference(ILGenerator gen)
45+
public override void Emit(ILGenerator gen)
4446
{
4547
if (Position == -1)
4648
{
@@ -66,12 +68,14 @@ public override void LoadReference(ILGenerator gen)
6668
}
6769
}
6870

69-
public override void StoreReference(ILGenerator gen)
71+
public override void EmitStore(IExpression value, ILGenerator gen)
7072
{
7173
if (Position == -1)
7274
{
7375
throw new InvalidOperationException("ArgumentReference uninitialized");
7476
}
77+
78+
value.Emit(gen);
7579
gen.Emit(OpCodes.Starg, Position);
7680
}
7781
}
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System;
@@ -26,38 +28,28 @@ internal class AsTypeReference : Reference
2628

2729
public AsTypeReference(Reference reference, Type type)
2830
{
29-
if (reference == null)
30-
{
31-
throw new ArgumentNullException(nameof(reference));
32-
}
33-
if (type == null)
34-
{
35-
throw new ArgumentNullException(nameof(type));
36-
}
3731
this.reference = reference;
3832
this.type = type;
39-
if (reference == OwnerReference)
40-
{
41-
OwnerReference = null;
42-
}
4333
}
4434

45-
public override void LoadAddressOfReference(ILGenerator gen)
35+
public override void EmitAddress(ILGenerator gen)
4636
{
47-
// NOTE: Or maybe throw new NotSupportedException() ?
48-
reference.LoadAddressOfReference(gen);
37+
// It does not make sense to take the address of a cast expression.
38+
// The C# language would also forbid address-taking of the form `&(x as T)`.
39+
throw new NotSupportedException();
4940
}
5041

51-
public override void LoadReference(ILGenerator gen)
42+
public override void Emit(ILGenerator gen)
5243
{
53-
reference.LoadReference(gen);
44+
reference.Emit(gen);
5445
gen.Emit(OpCodes.Isinst, type);
5546
}
5647

57-
public override void StoreReference(ILGenerator gen)
48+
public override void EmitStore(IExpression value, ILGenerator gen)
5849
{
59-
// NOTE: Or maybe throw new NotSupportedException() ?
60-
reference.StoreReference(gen);
50+
// It does not make sense to assign a value to the result of a cast expression.
51+
// The C# language would also forbid assignments of the form `(x as T) = y`.
52+
throw new NotSupportedException();
6153
}
6254
}
6355
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArgumentStatement.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System.Reflection.Emit;
@@ -29,7 +31,7 @@ public AssignArgumentStatement(ArgumentReference argument, IExpression expressio
2931

3032
public void Emit(ILGenerator gen)
3133
{
32-
ArgumentsUtil.EmitLoadOwnerAndReference(argument, gen);
34+
argument.Emit(gen);
3335
expression.Emit(gen);
3436
}
3537
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArrayStatement.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System.Reflection.Emit;
@@ -31,7 +33,7 @@ public AssignArrayStatement(Reference targetArray, int targetPosition, IExpressi
3133

3234
public void Emit(ILGenerator il)
3335
{
34-
ArgumentsUtil.EmitLoadOwnerAndReference(targetArray, il);
36+
targetArray.Emit(il);
3537

3638
il.Emit(OpCodes.Ldc_I4, targetPosition);
3739

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,26 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System.Reflection.Emit;
1820

1921
internal class AssignStatement : IStatement
2022
{
21-
private readonly IExpression expression;
2223
private readonly Reference target;
24+
private readonly IExpression value;
2325

24-
public AssignStatement(Reference target, IExpression expression)
26+
public AssignStatement(Reference target, IExpression value)
2527
{
2628
this.target = target;
27-
this.expression = expression;
29+
this.value = value;
2830
}
2931

3032
public void Emit(ILGenerator gen)
3133
{
32-
ArgumentsUtil.EmitLoadOwnerAndReference(target.OwnerReference, gen);
33-
expression.Emit(gen);
34-
target.StoreReference(gen);
34+
target.EmitStore(value, gen);
3535
}
3636
}
3737
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System;
@@ -30,17 +32,17 @@ public ByRefReference(LocalReference localReference)
3032
this.localReference = localReference;
3133
}
3234

33-
public override void LoadAddressOfReference(ILGenerator gen)
35+
public override void EmitAddress(ILGenerator gen)
3436
{
35-
localReference.LoadAddressOfReference(gen);
37+
localReference.EmitAddress(gen);
3638
}
3739

38-
public override void LoadReference(ILGenerator gen)
40+
public override void Emit(ILGenerator gen)
3941
{
40-
localReference.LoadAddressOfReference(gen);
42+
localReference.EmitAddress(gen);
4143
}
4244

43-
public override void StoreReference(ILGenerator gen)
45+
public override void EmitStore(IExpression value, ILGenerator gen)
4446
{
4547
throw new NotImplementedException();
4648
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System.Diagnostics;
@@ -22,7 +24,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
2224
internal class FieldReference : Reference
2325
{
2426
private readonly FieldInfo field;
25-
private readonly FieldBuilder fieldBuilder;
27+
private readonly FieldBuilder? fieldBuilder;
2628
private readonly bool isStatic;
2729

2830
public FieldReference(FieldInfo field)
@@ -31,7 +33,6 @@ public FieldReference(FieldInfo field)
3133
if ((field.Attributes & FieldAttributes.Static) != 0)
3234
{
3335
isStatic = true;
34-
owner = null;
3536
}
3637
}
3738

@@ -42,11 +43,10 @@ public FieldReference(FieldBuilder fieldBuilder)
4243
if ((fieldBuilder.Attributes & FieldAttributes.Static) != 0)
4344
{
4445
isStatic = true;
45-
owner = null;
4646
}
4747
}
4848

49-
public FieldBuilder FieldBuilder
49+
public FieldBuilder? FieldBuilder
5050
{
5151
get { return fieldBuilder; }
5252
}
@@ -56,38 +56,43 @@ public FieldInfo Reference
5656
get { return @field; }
5757
}
5858

59-
public override void LoadAddressOfReference(ILGenerator gen)
59+
public override void EmitAddress(ILGenerator gen)
6060
{
6161
if (isStatic)
6262
{
6363
gen.Emit(OpCodes.Ldsflda, Reference);
6464
}
6565
else
6666
{
67+
SelfReference.Self.Emit(gen);
6768
gen.Emit(OpCodes.Ldflda, Reference);
6869
}
6970
}
7071

71-
public override void LoadReference(ILGenerator gen)
72+
public override void Emit(ILGenerator gen)
7273
{
7374
if (isStatic)
7475
{
7576
gen.Emit(OpCodes.Ldsfld, Reference);
7677
}
7778
else
7879
{
80+
SelfReference.Self.Emit(gen);
7981
gen.Emit(OpCodes.Ldfld, Reference);
8082
}
8183
}
8284

83-
public override void StoreReference(ILGenerator gen)
85+
public override void EmitStore(IExpression value, ILGenerator gen)
8486
{
8587
if (isStatic)
8688
{
89+
value.Emit(gen);
8790
gen.Emit(OpCodes.Stsfld, Reference);
8891
}
8992
else
9093
{
94+
SelfReference.Self.Emit(gen);
95+
value.Emit(gen);
9196
gen.Emit(OpCodes.Stfld, Reference);
9297
}
9398
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#nullable enable
16+
1517
namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1618
{
1719
using System;
@@ -21,8 +23,8 @@ internal class IfNullExpression : IExpression, IStatement
2123
{
2224
private readonly IExpressionOrStatement ifNotNull;
2325
private readonly IExpressionOrStatement ifNull;
24-
private readonly Reference reference;
25-
private readonly IExpression expression;
26+
private readonly Reference? reference;
27+
private readonly IExpression? expression;
2628

2729
public IfNullExpression(Reference reference, IExpressionOrStatement ifNull, IExpressionOrStatement ifNotNull = null)
2830
{
@@ -42,7 +44,7 @@ public void Emit(ILGenerator gen)
4244
{
4345
if (reference != null)
4446
{
45-
ArgumentsUtil.EmitLoadOwnerAndReference(reference, gen);
47+
reference.Emit(gen);
4648
}
4749
else if (expression != null)
4850
{

0 commit comments

Comments
 (0)