diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs index d5ba7a8bd9..011a115565 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs @@ -33,7 +33,7 @@ internal class ClassProxySerializableContributor : SerializableContributor { private bool delegateToBaseGetObjectData; private ConstructorInfo serializationConstructor; - private readonly IList serializedFields = new List(); + private readonly IList serializedFields = new List(); public ClassProxySerializableContributor(Type targetType, Type[] interfaces, string typeId) : base(targetType, interfaces, typeId) @@ -82,15 +82,15 @@ public override void Generate(ClassEmitter @class) Constructor(@class); } - protected override void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData, - FieldReference field) + protected override void AddAddValueInvocation(ArgumentLocation serializationInfo, MethodEmitter getObjectData, + FieldLocation field) { serializedFields.Add(field); base.AddAddValueInvocation(serializationInfo, getObjectData, field); } - protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter) + protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo, + ArgumentLocation streamingContext, ClassEmitter emitter) { codeBuilder.AddStatement( new MethodInvocationExpression( @@ -108,7 +108,7 @@ protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, Argument EmitCallToBaseGetObjectData(codeBuilder, serializationInfo, streamingContext); } - private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo) + private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo) { var members = codeBuilder.DeclareLocal(typeof(MemberInfo[])); var data = codeBuilder.DeclareLocal(typeof(object[])); @@ -129,7 +129,7 @@ private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference var getObjectData = new MethodInvocationExpression( null, FormatterServicesMethods.GetObjectData, - SelfReference.Self, + ThisExpression.Instance, members); codeBuilder.AddStatement(new AssignStatement(data, getObjectData)); @@ -141,14 +141,15 @@ private void EmitCustomGetObjectData(CodeBuilder codeBuilder, ArgumentReference codeBuilder.AddStatement(addValue); } - private void EmitCallToBaseGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext) + private void EmitCallToBaseGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo, + ArgumentLocation streamingContext) { var baseGetObjectData = targetType.GetMethod("GetObjectData", new[] { typeof(SerializationInfo), typeof(StreamingContext) }); codeBuilder.AddStatement( new MethodInvocationExpression( + ThisExpression.Instance, baseGetObjectData, serializationInfo, streamingContext)); @@ -165,8 +166,8 @@ private void Constructor(ClassEmitter emitter) private void GenerateSerializationConstructor(ClassEmitter emitter) { - var serializationInfo = new ArgumentReference(typeof(SerializationInfo)); - var streamingContext = new ArgumentReference(typeof(StreamingContext)); + var serializationInfo = new ArgumentLocation(typeof(SerializationInfo)); + var streamingContext = new ArgumentLocation(typeof(StreamingContext)); var ctor = emitter.CreateConstructor(serializationInfo, streamingContext); diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index e60b4d08d2..cd3142dba5 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -78,13 +78,13 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm var invocation = GetInvocationType(method, @class); - GetTargetExpressionDelegate getTargetTypeExpression = (c, m) => new TypeTokenExpression(targetType); + GetTargetExpressionDelegate getTargetType = (c, m) => new TypeTokenExpression(targetType); return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, - getTargetTypeExpression, - getTargetTypeExpression, + getTargetType, + getTargetType, overrideMethod, null); } @@ -122,7 +122,7 @@ private MethodBuilder CreateCallbackMethod(ClassEmitter emitter, MethodInfo meth callBackMethod.CodeBuilder.AddStatement( new ReturnStatement( - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, targetMethod, callBackMethod.Arguments))); @@ -158,7 +158,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod } return new InvocationWithGenericDelegateContributor(@delegate, method, - new FieldReference(InvocationMethods.ProxyObject)); + new FieldLocation(InvocationMethods.ProxyObject)); } private Type GetDelegateType(MetaMethod method, ClassEmitter @class) diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs index d834373c3b..8e0acbc9ab 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs @@ -109,7 +109,7 @@ private IInvocationCreationContributor GetContributor(Type @delegate, MetaMethod } return new InvocationWithGenericDelegateContributor(@delegate, method, - new FieldReference(InvocationMethods.CompositionInvocationTarget)); + new FieldLocation(InvocationMethods.CompositionInvocationTarget)); } private Type GetDelegateType(MetaMethod method, ClassEmitter @class) diff --git a/src/Castle.Core/DynamicProxy/Contributors/Delegates.cs b/src/Castle.Core/DynamicProxy/Contributors/Delegates.cs index 60a64d6173..ffab3c2e37 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/Delegates.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/Delegates.cs @@ -23,6 +23,4 @@ internal delegate MethodEmitter OverrideMethodDelegate( string name, MethodAttributes attributes, MethodInfo methodToOverride); internal delegate IExpression GetTargetExpressionDelegate(ClassEmitter @class, MethodInfo method); - - internal delegate Reference GetTargetReferenceDelegate(ClassEmitter @class, MethodInfo method); } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs index 87b94f848b..2c1be5bdd6 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs @@ -21,12 +21,12 @@ namespace Castle.DynamicProxy.Contributors internal interface IInvocationCreationContributor { - ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation); + ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation); MethodInfo GetCallbackMethod(); MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args, - Reference targetField, MethodEmitter invokeMethodOnTarget); + Location targetField, MethodEmitter invokeMethodOnTarget); IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxySerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxySerializableContributor.cs index a9b85d86ea..cc11fd4231 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxySerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxySerializableContributor.cs @@ -29,8 +29,8 @@ public InterfaceProxySerializableContributor(Type targetType, string proxyGenera { } - protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter) + protected override void CustomizeGetObjectData(CodeBuilder codeBuilder, ArgumentLocation serializationInfo, + ArgumentLocation streamingContext, ClassEmitter emitter) { var targetField = emitter.GetField("__target"); diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs index 858a6fb205..ccf88663c1 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithOptionalTargetContributor.cs @@ -19,13 +19,9 @@ namespace Castle.DynamicProxy.Contributors internal class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWithoutTargetContributor { - private readonly GetTargetReferenceDelegate getTargetReference; - - public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget, - GetTargetReferenceDelegate getTargetReference) + public InterfaceProxyWithOptionalTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget) : base(namingScope, getTarget) { - this.getTargetReference = getTargetReference; canChangeTarget = true; } @@ -34,7 +30,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm { if (!method.Proxyable) { - return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTargetReference); + return new OptionallyForwardingMethodGenerator(method, overrideMethod, getTarget); } return base.GetMethodGenerator(method, @class, overrideMethod); diff --git a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs index d450b39c4b..50bda083e0 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyWithoutTargetContributor.cs @@ -26,13 +26,13 @@ namespace Castle.DynamicProxy.Contributors internal class InterfaceProxyWithoutTargetContributor : CompositeTypeContributor { - private readonly GetTargetExpressionDelegate getTargetExpression; + protected readonly GetTargetExpressionDelegate getTarget; protected bool canChangeTarget = false; public InterfaceProxyWithoutTargetContributor(INamingScope namingScope, GetTargetExpressionDelegate getTarget) : base(namingScope) { - getTargetExpression = getTarget; + this.getTarget = getTarget; } protected override IEnumerable GetCollectors() @@ -56,7 +56,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, - getTargetExpression, + getTarget, overrideMethod, null); } @@ -119,7 +119,7 @@ private MethodInfo CreateCallbackMethod(ClassEmitter emitter, MethodInfo methodI callBackMethod.CodeBuilder.AddStatement( new ReturnStatement( - new MethodInvocationExpression(SelfReference.Self, targetMethod, callBackMethod.Arguments))); + new MethodInvocationExpression(ThisExpression.Instance, targetMethod, callBackMethod.Arguments))); return callBackMethod.MethodBuilder; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs index 48f7d29691..74cf85c273 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs @@ -40,7 +40,7 @@ public InvocationWithDelegateContributor(Type delegateType, Type targetType, Met this.namingScope = namingScope; } - public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation) + public ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation) { var arguments = GetArguments(baseCtorArguments); var constructor = invocation.CreateConstructor(arguments); @@ -56,11 +56,11 @@ public MethodInfo GetCallbackMethod() } public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args, - Reference targetField, + Location targetField, MethodEmitter invokeMethodOnTarget) { var allArgs = GetAllArgs(args, targetField); - var @delegate = (Reference)invocation.GetField("delegate"); + var @delegate = (Location)invocation.GetField("delegate"); return new MethodInvocationExpression(@delegate, GetCallbackMethod(), allArgs); } @@ -73,7 +73,7 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, return allArguments; } - private FieldReference BuildDelegateToken(ClassEmitter proxy) + private FieldLocation BuildDelegateToken(ClassEmitter proxy) { var callback = proxy.CreateStaticField(namingScope.GetUniqueName("callback_" + method.Method.Name), delegateType); var createDelegate = new MethodInvocationExpression( @@ -88,7 +88,7 @@ private FieldReference BuildDelegateToken(ClassEmitter proxy) return callback; } - private IExpression[] GetAllArgs(IExpression[] args, Reference targetField) + private IExpression[] GetAllArgs(IExpression[] args, Location targetField) { var allArgs = new IExpression[args.Length + 1]; args.CopyTo(allArgs, 1); @@ -96,10 +96,10 @@ private IExpression[] GetAllArgs(IExpression[] args, Reference targetField) return allArgs; } - private ArgumentReference[] GetArguments(ArgumentReference[] baseCtorArguments) + private ArgumentLocation[] GetArguments(ArgumentLocation[] baseCtorArguments) { - var arguments = new ArgumentReference[baseCtorArguments.Length + 1]; - arguments[0] = new ArgumentReference(delegateType); + var arguments = new ArgumentLocation[baseCtorArguments.Length + 1]; + arguments[0] = new ArgumentLocation(delegateType); baseCtorArguments.CopyTo(arguments, 1); return arguments; } diff --git a/src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs index 3dbeaf5fe7..a37b1b4550 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs @@ -29,17 +29,17 @@ internal class InvocationWithGenericDelegateContributor : IInvocationCreationCon { private readonly Type delegateType; private readonly MetaMethod method; - private readonly Reference targetReference; + private readonly IExpression target; - public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, Reference targetReference) + public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, IExpression target) { Debug.Assert(delegateType.IsGenericType, "delegateType.IsGenericType"); this.delegateType = delegateType; this.method = method; - this.targetReference = targetReference; + this.target = target; } - public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArguments, AbstractTypeEmitter invocation) + public ConstructorEmitter CreateConstructor(ArgumentLocation[] baseCtorArguments, AbstractTypeEmitter invocation) { return invocation.CreateConstructor(baseCtorArguments); } @@ -50,7 +50,7 @@ public MethodInfo GetCallbackMethod() } public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args, - Reference targetField, + Location targetField, MethodEmitter invokeMethodOnTarget) { var @delegate = GetDelegate(invocation, invokeMethodOnTarget); @@ -62,25 +62,25 @@ public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, return arguments; } - private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget) + private Location GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invokeMethodOnTarget) { var genericTypeParameters = invocation.GenericTypeParams.AsTypeArray(); var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters); var localReference = invokeMethodOnTarget.CodeBuilder.DeclareLocal(closedDelegateType); var closedMethodOnTarget = method.MethodOnTarget.MakeGenericMethod(genericTypeParameters); invokeMethodOnTarget.CodeBuilder.AddStatement( - SetDelegate(localReference, targetReference, closedDelegateType, closedMethodOnTarget)); + SetDelegate(localReference, target, closedDelegateType, closedMethodOnTarget)); return localReference; } - private AssignStatement SetDelegate(LocalReference localDelegate, Reference localTarget, + private AssignStatement SetDelegate(LocalLocation localDelegate, IExpression target, Type closedDelegateType, MethodInfo closedMethodOnTarget) { var delegateCreateDelegate = new MethodInvocationExpression( null, DelegateMethods.CreateDelegate, new TypeTokenExpression(closedDelegateType), - localTarget, + target, new MethodTokenExpression(closedMethodOnTarget)); return new AssignStatement(localDelegate, new ConvertExpression(closedDelegateType, delegateCreateDelegate)); } diff --git a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs index e6070994ef..8d1c251fdb 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs @@ -28,17 +28,17 @@ internal class MixinContributor : CompositeTypeContributor { private readonly bool canChangeTarget; private readonly IList empty = new List(); - private readonly IDictionary fields = new SortedDictionary(new FieldReferenceComparer()); - private readonly GetTargetExpressionDelegate getTargetExpression; + private readonly IDictionary fields = new SortedDictionary(new FieldReferenceComparer()); + private readonly GetTargetExpressionDelegate getTarget; public MixinContributor(INamingScope namingScope, bool canChangeTarget) : base(namingScope) { this.canChangeTarget = canChangeTarget; - getTargetExpression = BuildGetTargetExpression(); + getTarget = BuildGetTargetExpression(); } - public IEnumerable Fields + public IEnumerable Fields { get { return fields.Values; } } @@ -101,7 +101,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm return new MethodWithInvocationGenerator(method, @class.GetField("__interceptors"), invocation, - getTargetExpression, + getTarget, overrideMethod, null); } @@ -114,11 +114,11 @@ private GetTargetExpressionDelegate BuildGetTargetExpression() } return (c, m) => new NullCoalescingOperatorExpression( - new AsTypeReference(c.GetField("__target"), m.DeclaringType), + new AsTypeExpression(c.GetField("__target"), m.DeclaringType), fields[m.DeclaringType]); } - private FieldReference BuildTargetField(ClassEmitter @class, Type type) + private FieldLocation BuildTargetField(ClassEmitter @class, Type type) { var name = "__mixin_" + type.FullName.Replace(".", "_"); return @class.CreateField(namingScope.GetUniqueName(name), type); diff --git a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs index fbdfbc0b58..45a28fdea9 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs @@ -25,12 +25,12 @@ namespace Castle.DynamicProxy.Contributors /// internal sealed class ProxyTargetAccessorContributor : ITypeContributor { - private readonly Func getTargetReference; + private readonly Func getTarget; private readonly Type targetType; - public ProxyTargetAccessorContributor(Func getTargetReference, Type targetType) + public ProxyTargetAccessorContributor(Func getTarget, Type targetType) { - this.getTargetReference = getTargetReference; + this.getTarget = getTarget; this.targetType = targetType; } @@ -41,17 +41,17 @@ public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) public void Generate(ClassEmitter emitter) { var interceptorsField = emitter.GetField("__interceptors"); - var targetReference = getTargetReference(); + var target = getTarget(); var dynProxyGetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxyGetTarget), typeof(object)); dynProxyGetTarget.CodeBuilder.AddStatement( - new ReturnStatement(new ConvertExpression(typeof(object), targetType, targetReference))); + new ReturnStatement(new ConvertExpression(typeof(object), targetType, target))); var dynProxySetTarget = emitter.CreateMethod(nameof(IProxyTargetAccessor.DynProxySetTarget), typeof(void), typeof(object)); // we can only change the target of the interface proxy - if (targetReference is FieldReference targetField) + if (target is FieldLocation targetField) { dynProxySetTarget.CodeBuilder.AddStatement( new AssignStatement(targetField, diff --git a/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs index c4ff33945c..9acf3bffb4 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs @@ -90,9 +90,8 @@ protected void ImplementGetObjectData(ClassEmitter emitter) for (var i = 0; i < interfaces.Length; i++) { getObjectData.CodeBuilder.AddStatement( - new AssignArrayStatement( - interfacesLocal, - i, + new AssignStatement( + new ArrayElementLocation(interfacesLocal, i), new LiteralStringExpression(interfaces[i].AssemblyQualifiedName))); } @@ -129,8 +128,8 @@ protected void ImplementGetObjectData(ClassEmitter emitter) getObjectData.CodeBuilder.AddStatement(new ReturnStatement()); } - protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData, - FieldReference field) + protected virtual void AddAddValueInvocation(ArgumentLocation serializationInfo, MethodEmitter getObjectData, + FieldLocation field) { getObjectData.CodeBuilder.AddStatement( new MethodInvocationExpression( @@ -141,8 +140,8 @@ protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo return; } - protected abstract void CustomizeGetObjectData(CodeBuilder builder, ArgumentReference serializationInfo, - ArgumentReference streamingContext, ClassEmitter emitter); + protected abstract void CustomizeGetObjectData(CodeBuilder builder, ArgumentLocation serializationInfo, + ArgumentLocation streamingContext, ClassEmitter emitter); public virtual void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model) { diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs index 02f6012380..794b34a165 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseClassProxyGenerator.cs @@ -31,7 +31,7 @@ protected BaseClassProxyGenerator(ModuleScope scope, Type targetType, Type[] int EnsureDoesNotImplementIProxyTargetAccessor(targetType, nameof(targetType)); } - protected abstract FieldReference TargetField { get; } + protected abstract FieldLocation TargetField { get; } #if FEATURE_SERIALIZATION protected abstract SerializableContributor GetSerializableContributor(); @@ -62,7 +62,7 @@ protected sealed override Type GenerateType(string name, INamingScope namingScop // Constructor var cctor = GenerateStaticConstructor(emitter); - var constructorArguments = new List(); + var constructorArguments = new List(); if (TargetField is { } targetField) { diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs index 84e6b1186b..45a25589e8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseInterfaceProxyGenerator.cs @@ -31,7 +31,7 @@ internal abstract class BaseInterfaceProxyGenerator : BaseProxyGenerator { protected readonly Type proxyTargetType; - protected FieldReference targetField; + protected FieldLocation targetField; protected BaseInterfaceProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces, Type proxyTargetType, ProxyGenerationOptions options) @@ -99,13 +99,13 @@ protected override Type GenerateType(string typeName, INamingScope namingScope) ProxyGenerationOptions.Hook.MethodsInspected(); ClassEmitter emitter; - FieldReference interceptorsField; + FieldLocation interceptorsField; var baseType = Init(typeName, out emitter, proxyTargetType, out interceptorsField, allInterfaces); // Constructor var cctor = GenerateStaticConstructor(emitter); - var ctorArguments = new List(); + var ctorArguments = new List(); foreach (var contributor in contributors) { @@ -237,7 +237,7 @@ protected virtual IEnumerable GetTypeImplementerMapping(Type proxyTargetTy } protected virtual Type Init(string typeName, out ClassEmitter emitter, Type proxyTargetType, - out FieldReference interceptorsField, IEnumerable allInterfaces) + out FieldLocation interceptorsField, IEnumerable allInterfaces) { var baseType = ProxyGenerationOptions.BaseTypeForInterfaceProxy; diff --git a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs index 2d790409d6..22afe73ee7 100644 --- a/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs @@ -175,7 +175,7 @@ protected void CreateInterceptorsField(ClassEmitter emitter) #endif } - protected FieldReference CreateOptionsField(ClassEmitter emitter) + protected FieldLocation CreateOptionsField(ClassEmitter emitter) { return emitter.CreateStaticField("proxyGenerationOptions", typeof(ProxyGenerationOptions)); } @@ -215,9 +215,9 @@ protected void EnsureOptionsOverrideEqualsAndGetHashCode() } protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseConstructor, - params FieldReference[] fields) + params FieldLocation[] fields) { - ArgumentReference[] args; + ArgumentLocation[] args; ParameterInfo[] baseConstructorParams = null; if (baseConstructor != null) @@ -227,23 +227,23 @@ protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseCon if (baseConstructorParams != null && baseConstructorParams.Length != 0) { - args = new ArgumentReference[fields.Length + baseConstructorParams.Length]; + args = new ArgumentLocation[fields.Length + baseConstructorParams.Length]; var offset = fields.Length; for (var i = offset; i < offset + baseConstructorParams.Length; i++) { var paramInfo = baseConstructorParams[i - offset]; - args[i] = new ArgumentReference(paramInfo.ParameterType); + args[i] = new ArgumentLocation(paramInfo.ParameterType); } } else { - args = new ArgumentReference[fields.Length]; + args = new ArgumentLocation[fields.Length]; } for (var i = 0; i < fields.Length; i++) { - args[i] = new ArgumentReference(fields[i].Reference.FieldType); + args[i] = new ArgumentLocation(fields[i].Reference.FieldType); } var constructor = emitter.CreateConstructor(args); @@ -271,7 +271,7 @@ protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseCon { Debug.Assert(baseConstructorParams != null); - var slice = new ArgumentReference[baseConstructorParams.Length]; + var slice = new ArgumentLocation[baseConstructorParams.Length]; Array.Copy(args, fields.Length, slice, 0, baseConstructorParams.Length); constructor.CodeBuilder.AddStatement(new ConstructorInvocationStatement(baseConstructor, slice)); @@ -284,7 +284,7 @@ protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseCon constructor.CodeBuilder.AddStatement(new ReturnStatement()); } - protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params FieldReference[] fields) + protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params FieldLocation[] fields) { var constructors = baseType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); @@ -307,7 +307,7 @@ protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params /// This constructor is important to allow proxies to be XML serializable /// /// - protected void GenerateParameterlessConstructor(ClassEmitter emitter, Type baseClass, FieldReference interceptorField) + protected void GenerateParameterlessConstructor(ClassEmitter emitter, Type baseClass, FieldLocation interceptorField) { // Check if the type actually has a default constructor var defaultConstructor = baseClass.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, @@ -331,7 +331,9 @@ protected void GenerateParameterlessConstructor(ClassEmitter emitter, Type baseC constructor.CodeBuilder.AddStatement(new AssignStatement(interceptorField, new NewArrayExpression(1, typeof(IInterceptor)))); constructor.CodeBuilder.AddStatement( - new AssignArrayStatement(interceptorField, 0, new NewInstanceExpression(typeof(StandardInterceptor)))); + new AssignStatement( + new ArrayElementLocation(interceptorField, 0), + new NewInstanceExpression(typeof(StandardInterceptor)))); // Invoke base constructor diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs index 7d713f15dc..12b3383d69 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs @@ -29,7 +29,7 @@ public ClassProxyGenerator(ModuleScope scope, Type targetType, Type[] interfaces { } - protected override FieldReference TargetField => null; + protected override FieldLocation TargetField => null; protected override CacheKey GetCacheKey() { @@ -51,7 +51,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => SelfReference.Self, + getTarget: () => ThisExpression.Instance, targetType); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs index c7f6fe6ad5..ba5c55f2e2 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ClassProxyWithTargetGenerator.cs @@ -29,7 +29,7 @@ namespace Castle.DynamicProxy.Generators internal sealed class ClassProxyWithTargetGenerator : BaseClassProxyGenerator { - private FieldReference targetField; + private FieldLocation targetField; public ClassProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] interfaces, ProxyGenerationOptions options) @@ -37,7 +37,7 @@ public ClassProxyWithTargetGenerator(ModuleScope scope, Type targetType, Type[] { } - protected override FieldReference TargetField => targetField; + protected override FieldLocation TargetField => targetField; protected override CacheKey GetCacheKey() { @@ -65,7 +65,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(INamingSco protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, targetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs index 7222725f31..316efe0625 100644 --- a/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/CompositionInvocationTypeGenerator.cs @@ -33,17 +33,17 @@ public CompositionInvocationTypeGenerator(Type target, MetaMethod method, Method { } - protected override ArgumentReference[] GetBaseCtorArguments(Type targetFieldType, + protected override ArgumentLocation[] GetBaseCtorArguments(Type targetFieldType, out ConstructorInfo baseConstructor) { baseConstructor = InvocationMethods.CompositionInvocationConstructor; return new[] { - new ArgumentReference(targetFieldType), - new ArgumentReference(typeof(object)), - new ArgumentReference(typeof(IInterceptor[])), - new ArgumentReference(typeof(MethodInfo)), - new ArgumentReference(typeof(object[])), + new ArgumentLocation(targetFieldType), + new ArgumentLocation(typeof(object)), + new ArgumentLocation(typeof(IInterceptor[])), + new ArgumentLocation(typeof(MethodInfo)), + new ArgumentLocation(typeof(object[])), }; } @@ -52,17 +52,17 @@ protected override Type GetBaseType() return BaseType; } - protected override FieldReference GetTargetReference() + protected override FieldLocation GetTargetField() { - return new FieldReference(InvocationMethods.CompositionInvocationTarget); + return new FieldLocation(InvocationMethods.CompositionInvocationTarget); } protected override void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, - MethodEmitter invokeMethodOnTarget, Reference targetField) + MethodEmitter invokeMethodOnTarget, Location targetField) { invokeMethodOnTarget.CodeBuilder.AddStatement( new MethodInvocationExpression( - SelfReference.Self, + ThisExpression.Instance, InvocationMethods.CompositionInvocationEnsureValidTarget)); base.ImplementInvokeMethodOnTarget(invocation, parameters, invokeMethodOnTarget, targetField); diff --git a/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs index 8efd6d40cf..7c356dfae0 100644 --- a/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/DelegateTypeGenerator.cs @@ -48,8 +48,8 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScop private void BuildConstructor(AbstractTypeEmitter emitter) { - var constructor = emitter.CreateConstructor(new ArgumentReference(typeof(object)), - new ArgumentReference(typeof(IntPtr))); + var constructor = emitter.CreateConstructor(new ArgumentLocation(typeof(object)), + new ArgumentLocation(typeof(IntPtr))); constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed); } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs index 361a6bdf8e..6878e3f7b8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/AbstractTypeEmitter.cs @@ -31,8 +31,8 @@ internal abstract class AbstractTypeEmitter private readonly List constructors; private readonly List events; - private readonly IDictionary fields = - new Dictionary(StringComparer.OrdinalIgnoreCase); + private readonly IDictionary fields = + new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly List methods; @@ -114,7 +114,7 @@ public void CopyGenericParametersFromMethod(MethodInfo methodToCopyGenericsFrom) SetGenericTypeParameters(GenericUtil.CopyGenericArguments(methodToCopyGenericsFrom, typeBuilder)); } - public ConstructorEmitter CreateConstructor(params ArgumentReference[] arguments) + public ConstructorEmitter CreateConstructor(params ArgumentLocation[] arguments) { if (TypeBuilder.IsInterface) { @@ -143,12 +143,12 @@ public EventEmitter CreateEvent(string name, EventAttributes atts, Type type) return eventEmitter; } - public FieldReference CreateField(string name, Type fieldType) + public FieldLocation CreateField(string name, Type fieldType) { return CreateField(name, fieldType, true); } - public FieldReference CreateField(string name, Type fieldType, bool serializable) + public FieldLocation CreateField(string name, Type fieldType, bool serializable) { var atts = FieldAttributes.Private; @@ -160,10 +160,10 @@ public FieldReference CreateField(string name, Type fieldType, bool serializable return CreateField(name, fieldType, atts); } - public FieldReference CreateField(string name, Type fieldType, FieldAttributes atts) + public FieldLocation CreateField(string name, Type fieldType, FieldAttributes atts) { var fieldBuilder = typeBuilder.DefineField(name, fieldType, atts); - var reference = new FieldReference(fieldBuilder); + var reference = new FieldLocation(fieldBuilder); fields[name] = reference; return reference; } @@ -199,12 +199,12 @@ public PropertyEmitter CreateProperty(string name, PropertyAttributes attributes return propEmitter; } - public FieldReference CreateStaticField(string name, Type fieldType) + public FieldLocation CreateStaticField(string name, Type fieldType) { return CreateStaticField(name, fieldType, FieldAttributes.Private); } - public FieldReference CreateStaticField(string name, Type fieldType, FieldAttributes atts) + public FieldLocation CreateStaticField(string name, Type fieldType, FieldAttributes atts) { atts |= FieldAttributes.Static; return CreateField(name, fieldType, atts); @@ -235,7 +235,7 @@ public void DefineCustomAttribute(object[] constructorArguments) whe typeBuilder.SetCustomAttribute(customAttributeInfo.Builder); } - public void DefineCustomAttributeFor(FieldReference field) where TAttribute : Attribute, new() + public void DefineCustomAttributeFor(FieldLocation field) where TAttribute : Attribute, new() { var customAttributeInfo = AttributeUtil.CreateInfo(); var fieldBuilder = field.FieldBuilder; @@ -247,19 +247,19 @@ public void DefineCustomAttribute(object[] constructorArguments) whe fieldBuilder.SetCustomAttribute(customAttributeInfo.Builder); } - public IEnumerable GetAllFields() + public IEnumerable GetAllFields() { return fields.Values; } - public FieldReference GetField(string name) + public FieldLocation GetField(string name) { if (string.IsNullOrEmpty(name)) { return null; } - FieldReference value; + FieldLocation value; fields.TryGetValue(name, out value); return value; } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/ArgumentsUtil.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/ArgumentsUtil.cs index 2e0396df26..64fe44f314 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/ArgumentsUtil.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/ArgumentsUtil.cs @@ -22,25 +22,25 @@ namespace Castle.DynamicProxy.Generators.Emitters internal abstract class ArgumentsUtil { - public static ArgumentReference[] ConvertToArgumentReference(Type[] args) + public static ArgumentLocation[] ConvertToArgumentReference(Type[] args) { - var arguments = new ArgumentReference[args.Length]; + var arguments = new ArgumentLocation[args.Length]; for (var i = 0; i < args.Length; ++i) { - arguments[i] = new ArgumentReference(args[i]); + arguments[i] = new ArgumentLocation(args[i]); } return arguments; } - public static ArgumentReference[] ConvertToArgumentReference(ParameterInfo[] args) + public static ArgumentLocation[] ConvertToArgumentReference(ParameterInfo[] args) { - var arguments = new ArgumentReference[args.Length]; + var arguments = new ArgumentLocation[args.Length]; for (var i = 0; i < args.Length; ++i) { - arguments[i] = new ArgumentReference(args[i].ParameterType); + arguments[i] = new ArgumentLocation(args[i].ParameterType); } return arguments; @@ -52,24 +52,12 @@ public static IExpression[] ConvertToArgumentReferenceExpression(ParameterInfo[] for (var i = 0; i < args.Length; ++i) { - arguments[i] = new ArgumentReference(args[i].ParameterType, i + 1); + arguments[i] = new ArgumentLocation(args[i].ParameterType, i + 1); } return arguments; } - public static void EmitLoadOwnerAndReference(Reference reference, ILGenerator il) - { - if (reference == null) - { - return; - } - - EmitLoadOwnerAndReference(reference.OwnerReference, il); - - reference.LoadReference(il); - } - public static Type[] GetTypes(ParameterInfo[] parameters) { var types = new Type[parameters.Length]; @@ -80,7 +68,7 @@ public static Type[] GetTypes(ParameterInfo[] parameters) return types; } - public static Type[] InitializeAndConvert(ArgumentReference[] args) + public static Type[] InitializeAndConvert(ArgumentLocation[] args) { var types = new Type[args.Length]; @@ -93,7 +81,7 @@ public static Type[] InitializeAndConvert(ArgumentReference[] args) return types; } - public static void InitializeArgumentsByPosition(ArgumentReference[] args, bool isStatic) + public static void InitializeArgumentsByPosition(ArgumentLocation[] args, bool isStatic) { var offset = isStatic ? 0 : 1; for (var i = 0; i < args.Length; ++i) diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/CodeBuilder.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/CodeBuilder.cs index 606e975431..1344f16ea8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/CodeBuilder.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/CodeBuilder.cs @@ -22,14 +22,14 @@ namespace Castle.DynamicProxy.Generators.Emitters internal sealed class CodeBuilder { - private readonly List locals; + private readonly List locals; private readonly List statements; private bool isEmpty; public CodeBuilder() { statements = new List(); - locals = new List(); + locals = new List(); isEmpty = true; } @@ -45,9 +45,9 @@ public CodeBuilder AddStatement(IStatement statement) return this; } - public LocalReference DeclareLocal(Type type) + public LocalLocation DeclareLocal(Type type) { - var local = new LocalReference(type); + var local = new LocalLocation(type); locals.Add(local); return local; } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/ConstructorEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/ConstructorEmitter.cs index 74b2edac37..dcfe1762d7 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/ConstructorEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/ConstructorEmitter.cs @@ -33,7 +33,7 @@ protected internal ConstructorEmitter(AbstractTypeEmitter mainType, ConstructorB codeBuilder = new CodeBuilder(); } - internal ConstructorEmitter(AbstractTypeEmitter mainType, params ArgumentReference[] arguments) + internal ConstructorEmitter(AbstractTypeEmitter mainType, params ArgumentLocation[] arguments) { this.mainType = mainType; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs index 7d2579f10f..85f95bc81f 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs @@ -32,7 +32,7 @@ internal class MethodEmitter : IMemberEmitter private readonly CodeBuilder codeBuilder; private readonly GenericTypeParameterBuilder[] genericTypeParams; - private ArgumentReference[] arguments; + private ArgumentLocation[] arguments; protected internal MethodEmitter(MethodBuilder builder) { @@ -73,7 +73,7 @@ internal MethodEmitter(AbstractTypeEmitter owner, string name, DefineParameters(baseMethodParameters); } - public ArgumentReference[] Arguments + public ArgumentLocation[] Arguments { get { return arguments; } } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentLocation.cs similarity index 80% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentLocation.cs index 19be72db47..17635d3893 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentLocation.cs @@ -19,28 +19,28 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection.Emit; [DebuggerDisplay("argument {Type}")] - internal class ArgumentReference : TypeReference + internal class ArgumentLocation : Location { - public ArgumentReference(Type argumentType) - : base(argumentType) + public ArgumentLocation(Type type) + : base(type) { Position = -1; } - public ArgumentReference(Type argumentType, int position) - : base(argumentType) + public ArgumentLocation(Type type, int position) + : base(type) { Position = position; } internal int Position { get; set; } - public override void LoadAddressOfReference(ILGenerator gen) + public override void EmitLoadAddress(ILGenerator gen) { throw new NotSupportedException(); } - public override void LoadReference(ILGenerator gen) + public override void EmitLoad(ILGenerator gen) { if (Position == -1) { @@ -66,12 +66,14 @@ public override void LoadReference(ILGenerator gen) } } - public override void StoreReference(ILGenerator gen) + public override void EmitStore(IExpression expression, ILGenerator gen) { if (Position == -1) { throw new InvalidOperationException("ArgumentReference uninitialized"); } + + expression.Emit(gen); gen.Emit(OpCodes.Starg, Position); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentsToObjectArrayExpression.cs similarity index 75% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentsToObjectArrayExpression.cs index a611fd54f4..487d52c41b 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReferencesToObjectArrayExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArgumentsToObjectArrayExpression.cs @@ -15,16 +15,15 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { using System; - using System.Reflection; using System.Reflection.Emit; using Castle.DynamicProxy.Internal; - internal class ReferencesToObjectArrayExpression : IExpression + internal sealed class ArgumentsToObjectArrayExpression : IExpression { - private readonly TypeReference[] args; + private readonly Location[] args; - public ReferencesToObjectArrayExpression(params TypeReference[] args) + public ArgumentsToObjectArrayExpression(params Location[] args) { this.args = args; } @@ -42,10 +41,10 @@ public void Emit(ILGenerator gen) gen.Emit(OpCodes.Ldloc, local); gen.Emit(OpCodes.Ldc_I4, i); - var reference = args[i]; + var arg = args[i]; #if FEATURE_BYREFLIKE - if (reference.Type.IsByRefLikeSafe()) + if (arg.Type.IsByRefLikeSafe()) { // The by-ref-like argument value cannot be put into the `object[]` array, // because it cannot be boxed. We need to replace it with some other value. @@ -58,20 +57,20 @@ public void Emit(ILGenerator gen) } #endif - ArgumentsUtil.EmitLoadOwnerAndReference(reference, gen); + arg.Emit(gen); - if (reference.Type.IsByRef) + if (arg.Type.IsByRef) { throw new NotSupportedException(); } - if (reference.Type.IsValueType) + if (arg.Type.IsValueType) { - gen.Emit(OpCodes.Box, reference.Type); + gen.Emit(OpCodes.Box, arg.Type); } - else if (reference.Type.IsGenericParameter) + else if (arg.Type.IsGenericParameter) { - gen.Emit(OpCodes.Box, reference.Type); + gen.Emit(OpCodes.Box, arg.Type); } gen.Emit(OpCodes.Stelem_Ref); diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArrayElementLocation.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArrayElementLocation.cs new file mode 100644 index 0000000000..acb4d0725e --- /dev/null +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ArrayElementLocation.cs @@ -0,0 +1,81 @@ +// Copyright 2004-2025 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST +{ + using System.Diagnostics; + using System.Reflection.Emit; + + internal sealed class ArrayElementLocation : Location + { + private readonly Location array; + private readonly int index; + + public ArrayElementLocation(Location array, int index) + : base(array.Type.GetElementType()) + { + Debug.Assert(array.Type.IsArray); + Debug.Assert(array.Type.GetElementType().IsClass || array.Type.GetElementType().IsInterface); + + this.array = array; + this.index = index; + } + + public override void EmitLoadAddress(ILGenerator gen) + { + array.Emit(gen); + LiteralIntExpression.Emit(index, gen); + gen.Emit(OpCodes.Ldelema); + } + + public override void EmitLoad(ILGenerator gen) + { + array.Emit(gen); + LiteralIntExpression.Emit(index, gen); + gen.Emit(OpCodes.Ldelem_Ref); + + // NOTE: The opcode isedabove is only valid for reference types. + // Here's the opcode mapping for all array element types: + // + // | element type | opcode + // |----------------------------------------------|------------------- + // | signed primitive types and enums | ldelem.i{1,2,4,8} + // | unsigned primitive types (including `bool`) | ldelem.u{1,2,4} + // | `nint` and `nuint` | ldelem.i + // | floating point primitive types | ldelem.r{4,8} + // | generic type param/args and structs | ldelem + // | classes and interfaces | ldelem.ref + } + + public override void EmitStore(IExpression expression, ILGenerator gen) + { + array.Emit(gen); + LiteralIntExpression.Emit(index, gen); + expression.Emit(gen); + gen.Emit(OpCodes.Stelem_Ref); + + // NOTE: The opcode used above is only valid for reference types. + // Here's the opcode mapping for all array element types: + // + // | element type | opcode + // |----------------------------------------------|------------------- + // | signed primitive types and enums | stelem.i{1,2,4,8} + // | unsigned primitive types (including `bool`) | stelem.u{1,2,4} + // | `nint` and `nuint` | stelem.i + // | floating point primitive types | stelem.r{4,8} + // | generic type param/args and structs | stelem + // | classes and interfaces | stelem.ref + } + } +} diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArgumentStatement.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeExpression.cs similarity index 60% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArgumentStatement.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeExpression.cs index 5c4b2a70f8..7ea4081d72 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArgumentStatement.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeExpression.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,23 +14,34 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { + using System; + using System.Diagnostics; using System.Reflection.Emit; - internal class AssignArgumentStatement : IStatement + [DebuggerDisplay("{expression} as {type}")] + internal class AsTypeExpression : IExpression { - private readonly ArgumentReference argument; private readonly IExpression expression; + private readonly Type type; - public AssignArgumentStatement(ArgumentReference argument, IExpression expression) + public AsTypeExpression(IExpression expression, Type type) { - this.argument = argument; + if (expression == null) + { + throw new ArgumentNullException(nameof(AsTypeExpression.expression)); + } + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } this.expression = expression; + this.type = type; } public void Emit(ILGenerator gen) { - ArgumentsUtil.EmitLoadOwnerAndReference(argument, gen); expression.Emit(gen); + gen.Emit(OpCodes.Isinst, type); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs deleted file mode 100644 index 6f0ce6c0ca..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AsTypeReference.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST -{ - using System; - using System.Diagnostics; - using System.Reflection.Emit; - - [DebuggerDisplay("{reference} as {type}")] - internal class AsTypeReference : Reference - { - private readonly Reference reference; - private readonly Type type; - - public AsTypeReference(Reference reference, Type type) - { - if (reference == null) - { - throw new ArgumentNullException(nameof(reference)); - } - if (type == null) - { - throw new ArgumentNullException(nameof(type)); - } - this.reference = reference; - this.type = type; - if (reference == OwnerReference) - { - OwnerReference = null; - } - } - - public override void LoadAddressOfReference(ILGenerator gen) - { - // NOTE: Or maybe throw new NotSupportedException() ? - reference.LoadAddressOfReference(gen); - } - - public override void LoadReference(ILGenerator gen) - { - reference.LoadReference(gen); - gen.Emit(OpCodes.Isinst, type); - } - - public override void StoreReference(ILGenerator gen) - { - // NOTE: Or maybe throw new NotSupportedException() ? - reference.StoreReference(gen); - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArrayStatement.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArrayStatement.cs deleted file mode 100644 index b8ea4d599f..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignArrayStatement.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST -{ - using System.Reflection.Emit; - - internal class AssignArrayStatement : IStatement - { - private readonly Reference targetArray; - private readonly int targetPosition; - private readonly IExpression value; - - public AssignArrayStatement(Reference targetArray, int targetPosition, IExpression value) - { - this.targetArray = targetArray; - this.targetPosition = targetPosition; - this.value = value; - } - - public void Emit(ILGenerator il) - { - ArgumentsUtil.EmitLoadOwnerAndReference(targetArray, il); - - il.Emit(OpCodes.Ldc_I4, targetPosition); - - value.Emit(il); - - il.Emit(OpCodes.Stelem_Ref); - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignStatement.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignStatement.cs index 4d33b78ac9..379879aadf 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignStatement.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/AssignStatement.cs @@ -19,9 +19,9 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST internal class AssignStatement : IStatement { private readonly IExpression expression; - private readonly Reference target; + private readonly Location target; - public AssignStatement(Reference target, IExpression expression) + public AssignStatement(Location target, IExpression expression) { this.target = target; this.expression = expression; @@ -29,9 +29,7 @@ public AssignStatement(Reference target, IExpression expression) public void Emit(ILGenerator gen) { - ArgumentsUtil.EmitLoadOwnerAndReference(target.OwnerReference, gen); - expression.Emit(gen); - target.StoreReference(gen); + target.EmitStore(expression, gen); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LoadRefArrayElementExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefExpression.cs similarity index 64% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LoadRefArrayElementExpression.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefExpression.cs index 311a46c432..69c56460ea 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LoadRefArrayElementExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefExpression.cs @@ -14,24 +14,22 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { + using System.Diagnostics; using System.Reflection.Emit; - internal class LoadRefArrayElementExpression : IExpression + [DebuggerDisplay("&{reference}")] + internal class ByRefExpression : IExpression { - private readonly Reference arrayReference; - private readonly LiteralIntExpression index; + private readonly Location location; - public LoadRefArrayElementExpression(int index, Reference arrayReference) + public ByRefExpression(Location location) { - this.index = new LiteralIntExpression(index); - this.arrayReference = arrayReference; + this.location = location; } public void Emit(ILGenerator gen) { - ArgumentsUtil.EmitLoadOwnerAndReference(arrayReference, gen); - index.Emit(gen); - gen.Emit(OpCodes.Ldelem_Ref); + location.EmitLoadAddress(gen); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs deleted file mode 100644 index f6fc69ad5b..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ByRefReference.cs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST -{ - using System; - using System.Diagnostics; - using System.Reflection.Emit; - - - [DebuggerDisplay("&{localReference}")] - internal class ByRefReference : TypeReference - { - private readonly LocalReference localReference; - - public ByRefReference(LocalReference localReference) - : base(localReference.Type) - { - this.localReference = localReference; - } - - public override void LoadAddressOfReference(ILGenerator gen) - { - localReference.LoadAddressOfReference(gen); - } - - public override void LoadReference(ILGenerator gen) - { - localReference.LoadAddressOfReference(gen); - } - - public override void StoreReference(ILGenerator gen) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/DefaultValueExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/DefaultValueExpression.cs index c4668cf208..fe2d3c66a3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/DefaultValueExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/DefaultValueExpression.cs @@ -42,34 +42,12 @@ public void Emit(ILGenerator gen) gen.Emit(OpCodes.Initobj, type); gen.Emit(OpCodes.Ldloc, local); } - else if (type.IsByRef) - { - EmitByRef(gen); - } else { throw new NotImplementedException("Can't emit default value for type " + type); } } - private void EmitByRef(ILGenerator gen) - { - var elementType = type.GetElementType(); - if (IsPrimitiveOrClass(elementType)) - { - OpCodeUtil.EmitLoadOpCodeForDefaultValueOfType(gen, elementType); - OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, elementType); - } - else if (elementType.IsGenericParameter || elementType.IsValueType) - { - gen.Emit(OpCodes.Initobj, elementType); - } - else - { - throw new NotImplementedException("Can't emit default value for reference of type " + elementType); - } - } - private bool IsPrimitiveOrClass(Type type) { if (type.IsPrimitive && type != typeof(IntPtr) && type != typeof(UIntPtr)) diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldLocation.cs similarity index 74% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldLocation.cs index ebd0112df1..9f2089abf2 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FieldLocation.cs @@ -18,31 +18,31 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection; using System.Reflection.Emit; - [DebuggerDisplay("{fieldBuilder.Name} ({fieldBuilder.FieldType})")] - internal class FieldReference : Reference + [DebuggerDisplay("{field.Name} ({field.FieldType})")] + internal class FieldLocation : Location { private readonly FieldInfo field; private readonly FieldBuilder fieldBuilder; private readonly bool isStatic; - public FieldReference(FieldInfo field) + public FieldLocation(FieldInfo field) + : base(field.FieldType) { this.field = field; if ((field.Attributes & FieldAttributes.Static) != 0) { isStatic = true; - owner = null; } } - public FieldReference(FieldBuilder fieldBuilder) + public FieldLocation(FieldBuilder fieldBuilder) + : base(fieldBuilder.FieldType) { this.fieldBuilder = fieldBuilder; field = fieldBuilder; if ((fieldBuilder.Attributes & FieldAttributes.Static) != 0) { isStatic = true; - owner = null; } } @@ -56,7 +56,7 @@ public FieldInfo Reference get { return @field; } } - public override void LoadAddressOfReference(ILGenerator gen) + public override void EmitLoadAddress(ILGenerator gen) { if (isStatic) { @@ -68,8 +68,10 @@ public override void LoadAddressOfReference(ILGenerator gen) } } - public override void LoadReference(ILGenerator gen) + public override void EmitLoad(ILGenerator gen) { + var owner = isStatic ? null : ThisExpression.Instance; + owner?.Emit(gen); if (isStatic) { gen.Emit(OpCodes.Ldsfld, Reference); @@ -80,8 +82,11 @@ public override void LoadReference(ILGenerator gen) } } - public override void StoreReference(ILGenerator gen) + public override void EmitStore(IExpression expression, ILGenerator gen) { + var owner = isStatic ? null : ThisExpression.Instance; + owner?.Emit(gen); + expression.Emit(gen); if (isStatic) { gen.Emit(OpCodes.Stsfld, Reference); diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs index 333a732ac2..f7d206c20f 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs @@ -21,16 +21,8 @@ internal class IfNullExpression : IExpression, IStatement { private readonly IExpressionOrStatement ifNotNull; private readonly IExpressionOrStatement ifNull; - private readonly Reference reference; private readonly IExpression expression; - public IfNullExpression(Reference reference, IExpressionOrStatement ifNull, IExpressionOrStatement ifNotNull = null) - { - this.reference = reference ?? throw new ArgumentNullException(nameof(reference)); - this.ifNull = ifNull; - this.ifNotNull = ifNotNull; - } - public IfNullExpression(IExpression expression, IExpressionOrStatement ifNull, IExpressionOrStatement ifNotNull = null) { this.expression = expression ?? throw new ArgumentNullException(nameof(expression)); @@ -40,15 +32,7 @@ public IfNullExpression(IExpression expression, IExpressionOrStatement ifNull, I public void Emit(ILGenerator gen) { - if (reference != null) - { - ArgumentsUtil.EmitLoadOwnerAndReference(reference, gen); - } - else if (expression != null) - { - expression.Emit(gen); - } - + expression.Emit(gen); var notNull = gen.DefineLabel(); gen.Emit(OpCodes.Brtrue_S, notNull); ifNull.Emit(gen); diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectLocation.cs similarity index 66% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectLocation.cs index 321d43954d..5bde7d05b9 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IndirectLocation.cs @@ -23,44 +23,51 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST /// Wraps a reference that is passed /// ByRef and provides indirect load/store support. /// - [DebuggerDisplay("&{OwnerReference}")] - internal class IndirectReference : TypeReference + [DebuggerDisplay("*{owner}")] + internal class IndirectLocation : Location { - public IndirectReference(TypeReference byRefReference) : - base(byRefReference, byRefReference.Type.GetElementType()) + private readonly Location owner; + + public IndirectLocation(Location byRefReference) : + base(byRefReference.Type.GetElementType()) { if (!byRefReference.Type.IsByRef) { throw new ArgumentException("Expected an IsByRef reference", nameof(byRefReference)); } + + owner = byRefReference; } - public override void LoadAddressOfReference(ILGenerator gen) + public override void EmitLoadAddress(ILGenerator gen) { // Load of owner reference takes care of this. } // TODO: Better name - public override void LoadReference(ILGenerator gen) + public override void EmitLoad(ILGenerator gen) { + owner.Emit(gen); OpCodeUtil.EmitLoadIndirectOpCodeForType(gen, Type); } - public override void StoreReference(ILGenerator gen) + public override void EmitStore(IExpression expression, ILGenerator gen) { + owner.Emit(gen); + expression.Emit(gen); OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, Type); } - public static TypeReference WrapIfByRef(TypeReference reference) + public static Location WrapIfByRef(Location reference) { - return reference.Type.IsByRef ? new IndirectReference(reference) : reference; + return reference.Type.IsByRef ? new IndirectLocation(reference) : reference; } // TODO: Better name - public static TypeReference[] WrapIfByRef(TypeReference[] references) + public static Location[] WrapIfByRef(Location[] references) { - var result = new TypeReference[references.Length]; + var result = new Location[references.Length]; for (var i = 0; i < references.Length; i++) { diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LiteralIntExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LiteralIntExpression.cs index 3e07dd435c..15a4f9a9a5 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LiteralIntExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LiteralIntExpression.cs @@ -25,7 +25,9 @@ public LiteralIntExpression(int value) this.value = value; } - public void Emit(ILGenerator gen) + public void Emit(ILGenerator gen) => Emit(value, gen); + + internal static void Emit(int value, ILGenerator gen) { switch (value) { diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalLocation.cs similarity index 79% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalLocation.cs index 768bac77f3..480e3139c8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/LocalLocation.cs @@ -19,11 +19,12 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST using System.Reflection.Emit; [DebuggerDisplay("local {Type}")] - internal class LocalReference : TypeReference + internal class LocalLocation : Location { private LocalBuilder localBuilder; - public LocalReference(Type type) : base(type) + public LocalLocation(Type type) + : base(type) { } @@ -32,18 +33,19 @@ public override void Generate(ILGenerator gen) localBuilder = gen.DeclareLocal(base.Type); } - public override void LoadAddressOfReference(ILGenerator gen) + public override void EmitLoadAddress(ILGenerator gen) { gen.Emit(OpCodes.Ldloca, localBuilder); } - public override void LoadReference(ILGenerator gen) + public override void EmitLoad(ILGenerator gen) { gen.Emit(OpCodes.Ldloc, localBuilder); } - public override void StoreReference(ILGenerator gen) + public override void EmitStore(IExpression expression, ILGenerator gen) { + expression.Emit(gen); gen.Emit(OpCodes.Stloc, localBuilder); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Location.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Location.cs new file mode 100644 index 0000000000..1a68db7dfc --- /dev/null +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Location.cs @@ -0,0 +1,66 @@ +// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#nullable enable + +namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST +{ + using System; + using System.Reflection.Emit; + + // Locations as defined by ECMA-335 section I.8.3. + // They are typed and can each hold one value of that type. + // Managed pointers (&, or "by-refs") may refer to them. + // For that purpose, their address can be taken. + // + // There are four principal types of locations: + // + // 1. method arguments (see `ArgumentLocation`) + // 2. static and instance fields (see `FieldLocation`) + // 3. local variables (see `LocalLocation`) + // 4. array elements (see `ArrayElementLocation`) + // + // One additional helper location type, `IndirectLocation`, + // acts as a stand-in for any and all of the above + // to facilitate code generation involving by-refs. + internal abstract class Location : IExpression + { + protected readonly Type type; + + protected Location(Type type) + { + this.type = type; + } + + public Type Type + { + get { return type; } + } + + public abstract void EmitLoadAddress(ILGenerator gen); + + public abstract void EmitLoad(ILGenerator gen); + + public abstract void EmitStore(IExpression expression, ILGenerator gen); + + public virtual void Generate(ILGenerator gen) + { + } + + public void Emit(ILGenerator gen) + { + EmitLoad(gen); + } + } +} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs index 4261758eea..2641bd0305 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/MethodInvocationExpression.cs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#nullable enable + namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { using System.Reflection; @@ -21,24 +23,9 @@ internal class MethodInvocationExpression : IExpression, IStatement { protected readonly IExpression[] args; protected readonly MethodInfo method; - protected readonly Reference owner; - - public MethodInvocationExpression(MethodInfo method, params IExpression[] args) : - this(SelfReference.Self, method, args) - { - } - - public MethodInvocationExpression(MethodEmitter method, params IExpression[] args) : - this(SelfReference.Self, method.MethodBuilder, args) - { - } - - public MethodInvocationExpression(Reference owner, MethodEmitter method, params IExpression[] args) : - this(owner, method.MethodBuilder, args) - { - } + protected readonly IExpression? owner; - public MethodInvocationExpression(Reference owner, MethodInfo method, params IExpression[] args) + public MethodInvocationExpression(IExpression? owner, MethodInfo method, params IExpression[] args) { this.owner = owner; this.method = method; @@ -49,7 +36,7 @@ public MethodInvocationExpression(Reference owner, MethodInfo method, params IEx public void Emit(ILGenerator gen) { - ArgumentsUtil.EmitLoadOwnerAndReference(owner, gen); + owner?.Emit(gen); foreach (var exp in args) { diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/NullCoalescingOperatorExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/NullCoalescingOperatorExpression.cs index 7c492a6ae8..c1cb306ad9 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/NullCoalescingOperatorExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/NullCoalescingOperatorExpression.cs @@ -15,8 +15,10 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { using System; + using System.Diagnostics; using System.Reflection.Emit; + [DebuggerDisplay("{expression} ?? {default}")] internal class NullCoalescingOperatorExpression : IExpression { private readonly IExpression @default; diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs deleted file mode 100644 index 6064317125..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/Reference.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST -{ - using System.Reflection.Emit; - - internal abstract class Reference : IExpression - { - protected Reference owner = SelfReference.Self; - - protected Reference() - { - } - - protected Reference(Reference owner) - { - this.owner = owner; - } - - public Reference OwnerReference - { - get { return owner; } - set { owner = value; } - } - - public abstract void LoadAddressOfReference(ILGenerator gen); - - public abstract void LoadReference(ILGenerator gen); - - public abstract void StoreReference(ILGenerator gen); - - public virtual void Generate(ILGenerator gen) - { - } - - public void Emit(ILGenerator gen) - { - ArgumentsUtil.EmitLoadOwnerAndReference(this, gen); - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReturnStatement.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReturnStatement.cs index 35bd01daf2..55ad034d23 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReturnStatement.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReturnStatement.cs @@ -18,34 +18,20 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST internal class ReturnStatement : IStatement { - private readonly IExpression expression; - private readonly Reference reference; + private readonly IExpression value; public ReturnStatement() { } - public ReturnStatement(Reference reference) + public ReturnStatement(IExpression value) { - this.reference = reference; - } - - public ReturnStatement(IExpression expression) - { - this.expression = expression; + this.value = value; } public void Emit(ILGenerator gen) { - if (reference != null) - { - ArgumentsUtil.EmitLoadOwnerAndReference(reference, gen); - } - else if (expression != null) - { - expression.Emit(gen); - } - + value?.Emit(gen); gen.Emit(OpCodes.Ret); } } diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs deleted file mode 100644 index 085d964da1..0000000000 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/SelfReference.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2004-2021 Castle Project - http://www.castleproject.org/ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST -{ - using System; - using System.Diagnostics; - using System.Reflection.Emit; - - [DebuggerDisplay("this")] - internal class SelfReference : Reference - { - public static readonly SelfReference Self = new SelfReference(); - - protected SelfReference() : base(null) - { - } - - public override void LoadAddressOfReference(ILGenerator gen) - { - throw new NotSupportedException(); - } - - public override void LoadReference(ILGenerator gen) - { - gen.Emit(OpCodes.Ldarg_0); - } - - public override void StoreReference(ILGenerator gen) - { - gen.Emit(OpCodes.Ldarg_0); - } - } -} \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs similarity index 69% rename from src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs rename to src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs index fa551e64fc..a9b3046912 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeReference.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ThisExpression.cs @@ -14,24 +14,21 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { - using System; + using System.Diagnostics; + using System.Reflection.Emit; - internal abstract class TypeReference : Reference + [DebuggerDisplay("this")] + internal class ThisExpression : IExpression { - private readonly Type type; + public static readonly ThisExpression Instance = new ThisExpression(); - protected TypeReference(Type argumentType) : this(null, argumentType) + protected ThisExpression() { } - protected TypeReference(Reference owner, Type type) : base(owner) + public void Emit(ILGenerator gen) { - this.type = type; - } - - public Type Type - { - get { return type; } + gen.Emit(OpCodes.Ldarg_0); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeTokenExpression.cs b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeTokenExpression.cs index 2a390c286c..287448f4ad 100644 --- a/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeTokenExpression.cs +++ b/src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TypeTokenExpression.cs @@ -15,10 +15,12 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { using System; + using System.Diagnostics; using System.Reflection.Emit; using Castle.DynamicProxy.Tokens; + [DebuggerDisplay("typeof {type}")] internal class TypeTokenExpression : IExpression { private readonly Type type; diff --git a/src/Castle.Core/DynamicProxy/Generators/ForwardingMethodGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/ForwardingMethodGenerator.cs index 6b7364f6e5..8af47db319 100644 --- a/src/Castle.Core/DynamicProxy/Generators/ForwardingMethodGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/ForwardingMethodGenerator.cs @@ -20,24 +20,24 @@ namespace Castle.DynamicProxy.Generators internal class ForwardingMethodGenerator : MethodGenerator { - private readonly GetTargetReferenceDelegate getTargetReference; + private readonly GetTargetExpressionDelegate getTarget; public ForwardingMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod, - GetTargetReferenceDelegate getTargetReference) + GetTargetExpressionDelegate getTarget) : base(method, overrideMethod) { - this.getTargetReference = getTargetReference; + this.getTarget = getTarget; } protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class, INamingScope namingScope) { - var targetReference = getTargetReference(@class, MethodToOverride); + var target = getTarget(@class, MethodToOverride); var arguments = ArgumentsUtil.ConvertToArgumentReferenceExpression(MethodToOverride.GetParameters()); emitter.CodeBuilder.AddStatement(new ReturnStatement( new MethodInvocationExpression( - targetReference, + target, MethodToOverride, arguments) { VirtualCall = true })); return emitter; diff --git a/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs b/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs index 97a221a6a0..013c01102e 100644 --- a/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs +++ b/src/Castle.Core/DynamicProxy/Generators/GeneratorUtil.cs @@ -25,13 +25,13 @@ namespace Castle.DynamicProxy.Generators internal static class GeneratorUtil { - public static void CopyOutAndRefParameters(TypeReference[] dereferencedArguments, LocalReference invocation, + public static void CopyOutAndRefParameters(Location[] dereferencedArguments, LocalLocation invocation, MethodInfo method, MethodEmitter emitter) { var parameters = method.GetParameters(); // Create it only if there are byref writable arguments. - LocalReference arguments = null; + LocalLocation arguments = null; for (var i = 0; i < parameters.Length; i++) { @@ -131,23 +131,23 @@ bool IsReadOnly(ParameterInfo parameter) } } - private static ConvertExpression Argument(int i, LocalReference invocationArgs, TypeReference[] arguments) + private static ConvertExpression Argument(int i, LocalLocation invocationArgs, Location[] arguments) { - return new ConvertExpression(arguments[i].Type, new LoadRefArrayElementExpression(i, invocationArgs)); + return new ConvertExpression(arguments[i].Type, new ArrayElementLocation(invocationArgs, i)); } - private static AssignStatement AssignArgument(TypeReference[] dereferencedArguments, int i, - LocalReference invocationArgs) + private static AssignStatement AssignArgument(Location[] dereferencedArguments, int i, + LocalLocation invocationArgs) { return new AssignStatement(dereferencedArguments[i], Argument(i, invocationArgs, dereferencedArguments)); } - private static AssignStatement GetArguments(LocalReference invocationArgs, LocalReference invocation) + private static AssignStatement GetArguments(LocalLocation invocationArgs, LocalLocation invocation) { return new AssignStatement(invocationArgs, new MethodInvocationExpression(invocation, InvocationMethods.GetArguments)); } - private static LocalReference StoreInvocationArgumentsInLocal(MethodEmitter emitter, LocalReference invocation) + private static LocalLocation StoreInvocationArgumentsInLocal(MethodEmitter emitter, LocalLocation invocation) { var invocationArgs = emitter.CodeBuilder.DeclareLocal(typeof(object[])); emitter.CodeBuilder.AddStatement(GetArguments(invocationArgs, invocation)); diff --git a/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs index b02d5876e9..c465770699 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InheritanceInvocationTypeGenerator.cs @@ -32,17 +32,17 @@ public InheritanceInvocationTypeGenerator(Type targetType, MetaMethod method, Me { } - protected override ArgumentReference[] GetBaseCtorArguments(Type targetFieldType, + protected override ArgumentLocation[] GetBaseCtorArguments(Type targetFieldType, out ConstructorInfo baseConstructor) { baseConstructor = InvocationMethods.InheritanceInvocationConstructor; return new[] { - new ArgumentReference(typeof(Type)), - new ArgumentReference(typeof(object)), - new ArgumentReference(typeof(IInterceptor[])), - new ArgumentReference(typeof(MethodInfo)), - new ArgumentReference(typeof(object[])) + new ArgumentLocation(typeof(Type)), + new ArgumentLocation(typeof(object)), + new ArgumentLocation(typeof(IInterceptor[])), + new ArgumentLocation(typeof(MethodInfo)), + new ArgumentLocation(typeof(object[])) }; } @@ -51,9 +51,9 @@ protected override Type GetBaseType() return BaseType; } - protected override FieldReference GetTargetReference() + protected override FieldLocation GetTargetField() { - return new FieldReference(InvocationMethods.ProxyObject); + return new FieldLocation(InvocationMethods.ProxyObject); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs index dc4ac91c41..083994281c 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs @@ -40,7 +40,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs index 9ae7b102c9..472aa48c98 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithTargetInterfaceGenerator.cs @@ -44,7 +44,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } @@ -57,18 +57,13 @@ protected override void AddMappingForAdditionalInterfaces(CompositeTypeContribut protected override InterfaceProxyWithoutTargetContributor GetContributorForAdditionalInterfaces( INamingScope namingScope) { - return new InterfaceProxyWithOptionalTargetContributor(namingScope, GetTargetExpression, GetTarget) + return new InterfaceProxyWithOptionalTargetContributor(namingScope, GetTarget) { Logger = Logger }; } - private Reference GetTarget(ClassEmitter @class, MethodInfo method) + private IExpression GetTarget(ClassEmitter @class, MethodInfo method) { - return new AsTypeReference(@class.GetField("__target"), method.DeclaringType); - } - - private IExpression GetTargetExpression(ClassEmitter @class, MethodInfo method) - { - return GetTarget(@class, method); + return new AsTypeExpression(@class.GetField("__target"), method.DeclaringType); } } } \ No newline at end of file diff --git a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs index d916e4dc62..845d1a81b3 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs @@ -41,7 +41,7 @@ protected override CompositeTypeContributor GetProxyTargetContributor(Type proxy protected override ProxyTargetAccessorContributor GetProxyTargetAccessorContributor() { return new ProxyTargetAccessorContributor( - getTargetReference: () => targetField, + getTarget: () => targetField, proxyTargetType); } diff --git a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs index 0065584b7f..e1de93a982 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs @@ -46,12 +46,12 @@ protected InvocationTypeGenerator(Type targetType, MetaMethod method, MethodInfo /// Generates the constructor for the class that extends /// /// - protected abstract ArgumentReference[] GetBaseCtorArguments(Type targetFieldType, + protected abstract ArgumentLocation[] GetBaseCtorArguments(Type targetFieldType, out ConstructorInfo baseConstructor); protected abstract Type GetBaseType(); - protected abstract FieldReference GetTargetReference(); + protected abstract FieldLocation GetTargetField(); public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScope) { @@ -71,7 +71,7 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScop CreateConstructor(invocation); - var targetField = GetTargetReference(); + var targetField = GetTargetField(); if (canChangeTarget) { ImplementChangeProxyTargetInterface(@class, invocation, targetField); @@ -88,7 +88,7 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScop protected virtual MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args, MethodInfo callbackMethod, - Reference targetField, + Location targetField, MethodEmitter invokeMethodOnTarget) { if (contributor != null) @@ -96,7 +96,7 @@ protected virtual MethodInvocationExpression GetCallbackMethodInvocation(Abstrac return contributor.GetCallbackMethodInvocation(invocation, args, targetField, invokeMethodOnTarget); } var methodOnTargetInvocationExpression = new MethodInvocationExpression( - new AsTypeReference(targetField, callbackMethod.DeclaringType), + new AsTypeExpression(targetField, callbackMethod.DeclaringType), callbackMethod, args) { VirtualCall = true }; return methodOnTargetInvocationExpression; @@ -104,7 +104,7 @@ protected virtual MethodInvocationExpression GetCallbackMethodInvocation(Abstrac protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, MethodEmitter invokeMethodOnTarget, - Reference targetField) + Location targetField) { var callbackMethod = GetCallbackMethod(invocation); if (callbackMethod == null) @@ -117,7 +117,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat // Idea: instead of grab parameters one by one // we should grab an array - var byRefArguments = new Dictionary(); + var byRefArguments = new Dictionary(); for (var i = 0; i < parameters.Length; i++) { @@ -143,13 +143,13 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat #endif { localValue = new ConvertExpression(paramType.GetElementType(), - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.GetArgumentValue, new LiteralIntExpression(i))); } invokeMethodOnTarget.CodeBuilder.AddStatement(new AssignStatement(localReference, localValue)); - var byRefReference = new ByRefReference(localReference); + var byRefReference = new ByRefExpression(localReference); args[i] = byRefReference; byRefArguments[i] = localReference; } @@ -170,7 +170,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat { args[i] = new ConvertExpression(paramType, - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.GetArgumentValue, new LiteralIntExpression(i))); } @@ -184,7 +184,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat var methodOnTargetInvocationExpression = GetCallbackMethodInvocation(invocation, args, callbackMethod, targetField, invokeMethodOnTarget); - LocalReference returnValue = null; + LocalLocation returnValue = null; if (callbackMethod.ReturnType != typeof(void)) { var returnType = invocation.GetClosedParameterType(callbackMethod.ReturnType); @@ -218,7 +218,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat } var setRetVal = - new MethodInvocationExpression(SelfReference.Self, + new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.SetReturnValue, retVal); @@ -228,7 +228,7 @@ protected virtual void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocat invokeMethodOnTarget.CodeBuilder.AddStatement(new ReturnStatement()); } - private void AssignBackByRefArguments(MethodEmitter invokeMethodOnTarget, Dictionary byRefArguments) + private void AssignBackByRefArguments(MethodEmitter invokeMethodOnTarget, Dictionary byRefArguments) { if (byRefArguments.Count == 0) { @@ -260,7 +260,7 @@ private void AssignBackByRefArguments(MethodEmitter invokeMethodOnTarget, Dictio invokeMethodOnTarget.CodeBuilder.AddStatement( new MethodInvocationExpression( - SelfReference.Self, + ThisExpression.Instance, InvocationMethods.SetArgumentValue, new LiteralIntExpression(index), localValue)); @@ -278,7 +278,7 @@ private void CreateConstructor(AbstractTypeEmitter invocation) constructor.CodeBuilder.AddStatement(new ReturnStatement()); } - private ConstructorEmitter CreateConstructor(AbstractTypeEmitter invocation, ArgumentReference[] baseCtorArguments) + private ConstructorEmitter CreateConstructor(AbstractTypeEmitter invocation, ArgumentLocation[] baseCtorArguments) { if (contributor == null) { @@ -289,7 +289,7 @@ private ConstructorEmitter CreateConstructor(AbstractTypeEmitter invocation, Arg private void EmitCallThrowOnNoTarget(MethodEmitter invokeMethodOnTarget) { - var throwOnNoTarget = new MethodInvocationExpression(InvocationMethods.ThrowOnNoTarget); + var throwOnNoTarget = new MethodInvocationExpression(ThisExpression.Instance, InvocationMethods.ThrowOnNoTarget); invokeMethodOnTarget.CodeBuilder.AddStatement(throwOnNoTarget); invokeMethodOnTarget.CodeBuilder.AddStatement(new ReturnStatement()); @@ -325,13 +325,13 @@ private AbstractTypeEmitter GetEmitter(ClassEmitter @class, Type[] interfaces, I } private void ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, - FieldReference targetField, MethodInfo callbackMethod) + FieldLocation targetField, MethodInfo callbackMethod) { var invokeMethodOnTarget = invocation.CreateMethod("InvokeMethodOnTarget", typeof(void)); ImplementInvokeMethodOnTarget(invocation, parameters, invokeMethodOnTarget, targetField); } - private void ImplementChangeInvocationTarget(AbstractTypeEmitter invocation, FieldReference targetField) + private void ImplementChangeInvocationTarget(AbstractTypeEmitter invocation, FieldLocation targetField) { var changeInvocationTarget = invocation.CreateMethod("ChangeInvocationTarget", typeof(void), new[] { typeof(object) }); changeInvocationTarget.CodeBuilder.AddStatement( @@ -344,7 +344,7 @@ private void ImplementChangeProxyTarget(AbstractTypeEmitter invocation, ClassEmi { var changeProxyTarget = invocation.CreateMethod("ChangeProxyTarget", typeof(void), new[] { typeof(object) }); - var proxyObject = new FieldReference(InvocationMethods.ProxyObject); + var proxyObject = new FieldLocation(InvocationMethods.ProxyObject); var localProxy = changeProxyTarget.CodeBuilder.DeclareLocal(typeof(IProxyTargetAccessor)); changeProxyTarget.CodeBuilder.AddStatement( new AssignStatement(localProxy, @@ -362,7 +362,7 @@ private void ImplementChangeProxyTarget(AbstractTypeEmitter invocation, ClassEmi } private void ImplementChangeProxyTargetInterface(ClassEmitter @class, AbstractTypeEmitter invocation, - FieldReference targetField) + FieldLocation targetField) { ImplementChangeInvocationTarget(invocation, targetField); diff --git a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs index 184c0ce484..9c53850da8 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MethodWithInvocationGenerator.cs @@ -32,32 +32,32 @@ namespace Castle.DynamicProxy.Generators internal class MethodWithInvocationGenerator : MethodGenerator { private readonly IInvocationCreationContributor contributor; - private readonly GetTargetExpressionDelegate getTargetExpression; - private readonly GetTargetExpressionDelegate getTargetTypeExpression; + private readonly GetTargetExpressionDelegate getTarget; + private readonly GetTargetExpressionDelegate getTargetType; private readonly IExpression interceptors; private readonly Type invocation; public MethodWithInvocationGenerator(MetaMethod method, IExpression interceptors, Type invocation, - GetTargetExpressionDelegate getTargetExpression, + GetTargetExpressionDelegate getTarget, OverrideMethodDelegate createMethod, IInvocationCreationContributor contributor) - : this(method, interceptors, invocation, getTargetExpression, null, createMethod, contributor) + : this(method, interceptors, invocation, getTarget, null, createMethod, contributor) { } public MethodWithInvocationGenerator(MetaMethod method, IExpression interceptors, Type invocation, - GetTargetExpressionDelegate getTargetExpression, - GetTargetExpressionDelegate getTargetTypeExpression, + GetTargetExpressionDelegate getTarget, + GetTargetExpressionDelegate getTargetType, OverrideMethodDelegate createMethod, IInvocationCreationContributor contributor) : base(method, createMethod) { this.invocation = invocation; - this.getTargetExpression = getTargetExpression; - this.getTargetTypeExpression = getTargetTypeExpression; + this.getTarget = getTarget; + this.getTargetType = getTargetType; this.interceptors = interceptors; this.contributor = contributor; } - protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope) + protected FieldLocation BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope) { var methodInterceptors = @class.CreateField( namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)), @@ -101,7 +101,7 @@ protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, C var methodInterceptors = SetMethodInterceptors(@class, namingScope, emitter, proxiedMethodTokenExpression); - var dereferencedArguments = IndirectReference.WrapIfByRef(emitter.Arguments); + var dereferencedArguments = IndirectLocation.WrapIfByRef(emitter.Arguments); var hasByRefArguments = HasByRefArguments(emitter.Arguments); var arguments = GetCtorArguments(@class, proxiedMethodTokenExpression, dereferencedArguments, methodInterceptors); @@ -158,7 +158,7 @@ protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, C // Emit code to ensure a value type return type is not null, otherwise the cast will cause a null-deref if (emitter.ReturnType.IsValueType && !emitter.ReturnType.IsNullableType()) { - LocalReference returnValue = emitter.CodeBuilder.DeclareLocal(typeof(object)); + LocalLocation returnValue = emitter.CodeBuilder.DeclareLocal(typeof(object)); emitter.CodeBuilder.AddStatement(new AssignStatement(returnValue, retVal)); emitter.CodeBuilder.AddStatement(new IfNullExpression(returnValue, new ThrowStatement(typeof(InvalidOperationException), @@ -189,13 +189,13 @@ private IExpression SetMethodInterceptors(ClassEmitter @class, INamingScope nami var methodInterceptorsField = BuildMethodInterceptorsField(@class, MethodToOverride, namingScope); IExpression targetTypeExpression; - if (getTargetTypeExpression != null) + if (getTargetType != null) { - targetTypeExpression = getTargetTypeExpression(@class, MethodToOverride); + targetTypeExpression = getTargetType(@class, MethodToOverride); } else { - targetTypeExpression = new MethodInvocationExpression(null, TypeUtilMethods.GetTypeOrNull, getTargetExpression(@class, MethodToOverride)); + targetTypeExpression = new MethodInvocationExpression(null, TypeUtilMethods.GetTypeOrNull, getTarget(@class, MethodToOverride)); } var emptyInterceptors = new NewArrayExpression(0, typeof(IInterceptor)); @@ -212,7 +212,7 @@ private IExpression SetMethodInterceptors(ClassEmitter @class, INamingScope nami return methodInterceptorsField; } - private void EmitLoadGenericMethodArguments(MethodEmitter methodEmitter, MethodInfo method, Reference invocationLocal) + private void EmitLoadGenericMethodArguments(MethodEmitter methodEmitter, MethodInfo method, Location invocationLocal) { var genericParameters = Array.FindAll(method.GetGenericArguments(), t => t.IsGenericParameter); var genericParamsArrayLocal = methodEmitter.CodeBuilder.DeclareLocal(typeof(Type[])); @@ -222,7 +222,9 @@ private void EmitLoadGenericMethodArguments(MethodEmitter methodEmitter, MethodI for (var i = 0; i < genericParameters.Length; ++i) { methodEmitter.CodeBuilder.AddStatement( - new AssignArrayStatement(genericParamsArrayLocal, i, new TypeTokenExpression(genericParameters[i]))); + new AssignStatement( + new ArrayElementLocation(genericParamsArrayLocal, i), + new TypeTokenExpression(genericParameters[i]))); } methodEmitter.CodeBuilder.AddStatement( new MethodInvocationExpression(invocationLocal, @@ -230,15 +232,15 @@ private void EmitLoadGenericMethodArguments(MethodEmitter methodEmitter, MethodI genericParamsArrayLocal)); } - private IExpression[] GetCtorArguments(ClassEmitter @class, IExpression proxiedMethodTokenExpression, TypeReference[] dereferencedArguments, IExpression methodInterceptors) + private IExpression[] GetCtorArguments(ClassEmitter @class, IExpression proxiedMethodTokenExpression, Location[] dereferencedArguments, IExpression methodInterceptors) { return new[] { - getTargetExpression(@class, MethodToOverride), - SelfReference.Self, + getTarget(@class, MethodToOverride), + ThisExpression.Instance, methodInterceptors ?? interceptors, proxiedMethodTokenExpression, - new ReferencesToObjectArrayExpression(dereferencedArguments) + new ArgumentsToObjectArrayExpression(dereferencedArguments) }; } @@ -252,7 +254,7 @@ private IExpression[] ModifyArguments(ClassEmitter @class, IExpression[] argumen return contributor.GetConstructorInvocationArguments(arguments, @class); } - private bool HasByRefArguments(ArgumentReference[] arguments) + private bool HasByRefArguments(ArgumentLocation[] arguments) { for (int i = 0; i < arguments.Length; i++ ) { diff --git a/src/Castle.Core/DynamicProxy/Generators/MinimalisticMethodGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/MinimalisticMethodGenerator.cs index 965ea8e78e..6935e21d1d 100644 --- a/src/Castle.Core/DynamicProxy/Generators/MinimalisticMethodGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/MinimalisticMethodGenerator.cs @@ -52,8 +52,10 @@ private void InitOutParameters(MethodEmitter emitter, ParameterInfo[] parameters if (parameter.IsOut) { emitter.CodeBuilder.AddStatement( - new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1), - new DefaultValueExpression(parameter.ParameterType))); + new AssignStatement( + new IndirectLocation( + new ArgumentLocation(parameter.ParameterType, index + 1)), + new DefaultValueExpression(parameter.ParameterType.GetElementType()))); } } } diff --git a/src/Castle.Core/DynamicProxy/Generators/OptionallyForwardingMethodGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/OptionallyForwardingMethodGenerator.cs index 51a36f2f73..c54d582ca0 100644 --- a/src/Castle.Core/DynamicProxy/Generators/OptionallyForwardingMethodGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/OptionallyForwardingMethodGenerator.cs @@ -24,37 +24,37 @@ namespace Castle.DynamicProxy.Generators internal class OptionallyForwardingMethodGenerator : MethodGenerator { // TODO: This class largely duplicates code from Forwarding and Minimalistic generators. Should be refactored to change that - private readonly GetTargetReferenceDelegate getTargetReference; + private readonly GetTargetExpressionDelegate getTarget; public OptionallyForwardingMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod, - GetTargetReferenceDelegate getTargetReference) + GetTargetExpressionDelegate getTarget) : base(method, overrideMethod) { - this.getTargetReference = getTargetReference; + this.getTarget = getTarget; } protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class, INamingScope namingScope) { - var targetReference = getTargetReference(@class, MethodToOverride); + var target = getTarget(@class, MethodToOverride); emitter.CodeBuilder.AddStatement( new IfNullExpression( - targetReference, + target, IfNull(emitter.ReturnType), - IfNotNull(targetReference))); + IfNotNull(target))); return emitter; } - private IStatement IfNotNull(Reference targetReference) + private IStatement IfNotNull(IExpression target) { var statements = new BlockStatement(); var arguments = ArgumentsUtil.ConvertToArgumentReferenceExpression(MethodToOverride.GetParameters()); statements.AddStatement(new ReturnStatement( new MethodInvocationExpression( - targetReference, + target, MethodToOverride, arguments) { VirtualCall = true })); return statements; @@ -84,8 +84,10 @@ private void InitOutParameters(BlockStatement statements, ParameterInfo[] parame if (parameter.IsOut) { statements.AddStatement( - new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1), - new DefaultValueExpression(parameter.ParameterType))); + new AssignStatement( + new IndirectLocation( + new ArgumentLocation(parameter.ParameterType, index + 1)), + new DefaultValueExpression(parameter.ParameterType.GetElementType()))); } } }