Skip to content

Commit e2b6007

Browse files
committed
Complete Reference.StoreReference impls with the missing bits from AssignStatement
1 parent 71af9fa commit e2b6007

File tree

9 files changed

+56
-40
lines changed

9 files changed

+56
-40
lines changed

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

Lines changed: 7 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;
@@ -66,12 +68,15 @@ public override void LoadReference(ILGenerator gen)
6668
}
6769
}
6870

69-
public override void StoreReference(ILGenerator gen)
71+
public override void StoreReference(IExpression value, ILGenerator gen)
7072
{
7173
if (Position == -1)
7274
{
7375
throw new InvalidOperationException("ArgumentReference uninitialized");
7476
}
77+
78+
OwnerReference?.Emit(gen);
79+
value.Emit(gen);
7580
gen.Emit(OpCodes.Starg, Position);
7681
}
7782
}

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

Lines changed: 10 additions & 14 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,14 +28,6 @@ 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;
3933
if (reference == OwnerReference)
@@ -44,8 +38,9 @@ public AsTypeReference(Reference reference, Type type)
4438

4539
public override void LoadAddressOfReference(ILGenerator gen)
4640
{
47-
// NOTE: Or maybe throw new NotSupportedException() ?
48-
reference.LoadAddressOfReference(gen);
41+
// It does not make sense to take the address of a cast expression.
42+
// The C# language would also forbid address-taking of the form `&(x as T)`.
43+
throw new NotSupportedException();
4944
}
5045

5146
public override void LoadReference(ILGenerator gen)
@@ -54,10 +49,11 @@ public override void LoadReference(ILGenerator gen)
5449
gen.Emit(OpCodes.Isinst, type);
5550
}
5651

57-
public override void StoreReference(ILGenerator gen)
52+
public override void StoreReference(IExpression value, ILGenerator gen)
5853
{
59-
// NOTE: Or maybe throw new NotSupportedException() ?
60-
reference.StoreReference(gen);
54+
// It does not make sense to assign a value to the result of a cast expression.
55+
// The C# language would also forbid assignments of the form `(x as T) = y`.
56+
throw new NotSupportedException();
6157
}
6258
}
6359
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
2020

2121
internal class AssignStatement : IStatement
2222
{
23-
private readonly IExpression expression;
2423
private readonly Reference target;
24+
private readonly IExpression value;
2525

26-
public AssignStatement(Reference target, IExpression expression)
26+
public AssignStatement(Reference target, IExpression value)
2727
{
2828
this.target = target;
29-
this.expression = expression;
29+
this.value = value;
3030
}
3131

3232
public void Emit(ILGenerator gen)
3333
{
34-
target.OwnerReference?.Emit(gen);
35-
expression.Emit(gen);
36-
target.StoreReference(gen);
34+
target.StoreReference(value, gen);
3735
}
3836
}
3937
}

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.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;
@@ -40,7 +42,7 @@ public override void LoadReference(ILGenerator gen)
4042
localReference.LoadAddressOfReference(gen);
4143
}
4244

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

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.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.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)
@@ -46,7 +48,7 @@ public FieldReference(FieldBuilder fieldBuilder)
4648
}
4749
}
4850

49-
public FieldBuilder FieldBuilder
51+
public FieldBuilder? FieldBuilder
5052
{
5153
get { return fieldBuilder; }
5254
}
@@ -80,8 +82,10 @@ public override void LoadReference(ILGenerator gen)
8082
}
8183
}
8284

83-
public override void StoreReference(ILGenerator gen)
85+
public override void StoreReference(IExpression value, ILGenerator gen)
8486
{
87+
OwnerReference?.Emit(gen);
88+
value.Emit(gen);
8589
if (isStatic)
8690
{
8791
gen.Emit(OpCodes.Stsfld, Reference);

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

Lines changed: 6 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;
@@ -47,8 +49,10 @@ public override void LoadReference(ILGenerator gen)
4749
OpCodeUtil.EmitLoadIndirectOpCodeForType(gen, Type);
4850
}
4951

50-
public override void StoreReference(ILGenerator gen)
52+
public override void StoreReference(IExpression value, ILGenerator gen)
5153
{
54+
OwnerReference?.Emit(gen);
55+
value.Emit(gen);
5256
OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, Type);
5357
}
5458

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

Lines changed: 10 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;
@@ -21,7 +23,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
2123
[DebuggerDisplay("local {Type}")]
2224
internal class LocalReference : TypeReference
2325
{
24-
private LocalBuilder localBuilder;
26+
private LocalBuilder? localBuilder;
2527

2628
public LocalReference(Type type) : base(type)
2729
{
@@ -34,17 +36,19 @@ public override void Generate(ILGenerator gen)
3436

3537
public override void LoadAddressOfReference(ILGenerator gen)
3638
{
37-
gen.Emit(OpCodes.Ldloca, localBuilder);
39+
gen.Emit(OpCodes.Ldloca, localBuilder!);
3840
}
3941

4042
public override void LoadReference(ILGenerator gen)
4143
{
42-
gen.Emit(OpCodes.Ldloc, localBuilder);
44+
gen.Emit(OpCodes.Ldloc, localBuilder!);
4345
}
4446

45-
public override void StoreReference(ILGenerator gen)
47+
public override void StoreReference(IExpression value, ILGenerator gen)
4648
{
47-
gen.Emit(OpCodes.Stloc, localBuilder);
49+
OwnerReference?.Emit(gen);
50+
value.Emit(gen);
51+
gen.Emit(OpCodes.Stloc, localBuilder!);
4852
}
4953
}
5054
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Reference? OwnerReference
4141

4242
public abstract void LoadReference(ILGenerator gen);
4343

44-
public abstract void StoreReference(ILGenerator gen);
44+
public abstract void StoreReference(IExpression value, ILGenerator gen);
4545

4646
public virtual void Generate(ILGenerator gen)
4747
{

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

Lines changed: 6 additions & 3 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;
@@ -37,9 +39,10 @@ public override void LoadReference(ILGenerator gen)
3739
gen.Emit(OpCodes.Ldarg_0);
3840
}
3941

40-
public override void StoreReference(ILGenerator gen)
42+
public override void StoreReference(IExpression value, ILGenerator gen)
4143
{
42-
gen.Emit(OpCodes.Ldarg_0);
44+
// It does not make sense to assign a value to the this pointer.
45+
throw new NotSupportedException();
4346
}
4447
}
4548
}

0 commit comments

Comments
 (0)