Skip to content

Commit 4076c5d

Browse files
authored
Merge pull request #566 from stakx/simple-ast
Simplify SimpleAST by converting `Expression`, `Statement` to interfaces
2 parents de1a3c4 + 4c4ff2b commit 4076c5d

File tree

53 files changed

+252
-402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+252
-402
lines changed

src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,12 @@ protected override void AddAddValueInvocation(ArgumentReference serializationInf
9292
protected override void CustomizeGetObjectData(CodeBuilder codebuilder, ArgumentReference serializationInfo,
9393
ArgumentReference streamingContext, ClassEmitter emitter)
9494
{
95-
codebuilder.AddStatement(new ExpressionStatement(
96-
new MethodInvocationExpression(
97-
serializationInfo,
98-
SerializationInfoMethods.AddValue_Bool,
99-
new ConstReference("__delegateToBase").ToExpression(),
100-
new ConstReference(delegateToBaseGetObjectData).
101-
ToExpression())));
95+
codebuilder.AddStatement(
96+
new MethodInvocationExpression(
97+
serializationInfo,
98+
SerializationInfoMethods.AddValue_Bool,
99+
new ConstReference("__delegateToBase"),
100+
new ConstReference(delegateToBaseGetObjectData)));
102101

103102
if (delegateToBaseGetObjectData == false)
104103
{
@@ -124,22 +123,22 @@ private void EmitCustomGetObjectData(CodeBuilder codebuilder, ArgumentReference
124123
var callSort = new MethodInvocationExpression(
125124
null,
126125
TypeUtilMethods.Sort,
127-
members.ToExpression());
126+
members);
128127
codebuilder.AddStatement(new AssignStatement(members, callSort));
129128

130129
var getObjectData = new MethodInvocationExpression(
131130
null,
132131
FormatterServicesMethods.GetObjectData,
133-
SelfReference.Self.ToExpression(),
134-
members.ToExpression());
132+
SelfReference.Self,
133+
members);
135134
codebuilder.AddStatement(new AssignStatement(data, getObjectData));
136135

137136
var addValue = new MethodInvocationExpression(
138137
serializationInfo,
139138
SerializationInfoMethods.AddValue_Object,
140-
new ConstReference("__data").ToExpression(),
141-
data.ToExpression());
142-
codebuilder.AddStatement(new ExpressionStatement(addValue));
139+
new ConstReference("__data"),
140+
data);
141+
codebuilder.AddStatement(addValue);
143142
}
144143

145144
private void EmitCallToBaseGetObjectData(CodeBuilder codebuilder, ArgumentReference serializationInfo,
@@ -148,10 +147,11 @@ private void EmitCallToBaseGetObjectData(CodeBuilder codebuilder, ArgumentRefere
148147
var baseGetObjectData = targetType.GetMethod("GetObjectData",
149148
new[] { typeof(SerializationInfo), typeof(StreamingContext) });
150149

151-
codebuilder.AddStatement(new ExpressionStatement(
152-
new MethodInvocationExpression(baseGetObjectData,
153-
serializationInfo.ToExpression(),
154-
streamingContext.ToExpression())));
150+
codebuilder.AddStatement(
151+
new MethodInvocationExpression(
152+
baseGetObjectData,
153+
serializationInfo,
154+
streamingContext));
155155
}
156156

157157
private void Constructor(ClassEmitter emitter)
@@ -172,14 +172,14 @@ private void GenerateSerializationConstructor(ClassEmitter emitter)
172172

173173
ctor.CodeBuilder.AddStatement(
174174
new ConstructorInvocationStatement(serializationConstructor,
175-
serializationInfo.ToExpression(),
176-
streamingContext.ToExpression()));
175+
serializationInfo,
176+
streamingContext));
177177

178178
foreach (var field in serializedFields)
179179
{
180180
var getValue = new MethodInvocationExpression(serializationInfo,
181181
SerializationInfoMethods.GetValue,
182-
new ConstReference(field.Reference.Name).ToExpression(),
182+
new ConstReference(field.Reference.Name),
183183
new TypeTokenExpression(field.Reference.FieldType));
184184
ctor.CodeBuilder.AddStatement(new AssignStatement(
185185
field,

src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,13 @@ private MethodBuilder CreateCallbackMethod(ClassEmitter emitter, MethodInfo meth
111111
targetMethod = targetMethod.MakeGenericMethod(callBackMethod.GenericTypeParams.AsTypeArray());
112112
}
113113

114-
var exps = new Expression[callBackMethod.Arguments.Length];
115-
for (var i = 0; i < callBackMethod.Arguments.Length; i++)
116-
{
117-
exps[i] = callBackMethod.Arguments[i].ToExpression();
118-
}
119-
120114
// invocation on base class
121115

122116
callBackMethod.CodeBuilder.AddStatement(
123117
new ReturnStatement(
124118
new MethodInvocationExpression(SelfReference.Self,
125119
targetMethod,
126-
exps)));
120+
callBackMethod.Arguments)));
127121

128122
return callBackMethod.MethodBuilder;
129123
}

src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm
7272
return new MethodWithInvocationGenerator(method,
7373
@class.GetField("__interceptors"),
7474
invocation,
75-
(c, m) => c.GetField("__target").ToExpression(),
75+
(c, m) => c.GetField("__target"),
7676
overrideMethod,
7777
null);
7878
}
@@ -147,7 +147,7 @@ private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, Class
147147
return new MethodWithInvocationGenerator(method,
148148
proxy.GetField("__interceptors"),
149149
invocation,
150-
(c, m) => c.GetField("__target").ToExpression(),
150+
(c, m) => c.GetField("__target"),
151151
overrideMethod,
152152
contributor);
153153
}

src/Castle.Core/DynamicProxy/Contributors/Delegates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Castle.DynamicProxy.Contributors
2222
internal delegate MethodEmitter OverrideMethodDelegate(
2323
string name, MethodAttributes attributes, MethodInfo methodToOverride);
2424

25-
internal delegate Expression GetTargetExpressionDelegate(ClassEmitter @class, MethodInfo method);
25+
internal delegate IExpression GetTargetExpressionDelegate(ClassEmitter @class, MethodInfo method);
2626

2727
internal delegate Reference GetTargetReferenceDelegate(ClassEmitter @class, MethodInfo method);
2828
}

src/Castle.Core/DynamicProxy/Contributors/IInvocationCreationContributor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ internal interface IInvocationCreationContributor
2525

2626
MethodInfo GetCallbackMethod();
2727

28-
MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
28+
MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
2929
Reference targetField, MethodEmitter invokeMethodOnTarget);
3030

31-
Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy);
31+
IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy);
3232
}
3333
}

src/Castle.Core/DynamicProxy/Contributors/InterfaceProxySerializableContributor.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ protected override void CustomizeGetObjectData(CodeBuilder codebuilder, Argument
3434
{
3535
var targetField = emitter.GetField("__target");
3636

37-
codebuilder.AddStatement(new ExpressionStatement(
38-
new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object,
39-
new ConstReference("__targetFieldType").ToExpression(),
40-
new ConstReference(
41-
targetField.Reference.FieldType.AssemblyQualifiedName).
42-
ToExpression())));
43-
44-
codebuilder.AddStatement(new ExpressionStatement(
45-
new MethodInvocationExpression(serializationInfo, SerializationInfoMethods.AddValue_Object,
46-
new ConstReference("__theInterface").ToExpression(),
47-
new ConstReference(targetType.AssemblyQualifiedName).
48-
ToExpression())));
37+
codebuilder.AddStatement(
38+
new MethodInvocationExpression(
39+
serializationInfo,
40+
SerializationInfoMethods.AddValue_Object,
41+
new ConstReference("__targetFieldType"),
42+
new ConstReference(targetField.Reference.FieldType.AssemblyQualifiedName)));
43+
44+
codebuilder.AddStatement(
45+
new MethodInvocationExpression(
46+
serializationInfo,
47+
SerializationInfoMethods.AddValue_Object,
48+
new ConstReference("__theInterface"),
49+
new ConstReference(targetType.AssemblyQualifiedName)));
4950
}
5051
}
5152
}

src/Castle.Core/DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected override MethodGenerator GetMethodGenerator(MetaMethod method, ClassEm
6565
return new MethodWithInvocationGenerator(method,
6666
@class.GetField("__interceptors"),
6767
invocation,
68-
(c, m) => c.GetField("__target").ToExpression(),
68+
(c, m) => c.GetField("__target"),
6969
overrideMethod,
7070
null);
7171
}

src/Castle.Core/DynamicProxy/Contributors/InvocationWithDelegateContributor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ConstructorEmitter CreateConstructor(ArgumentReference[] baseCtorArgument
4646
var constructor = invocation.CreateConstructor(arguments);
4747

4848
var delegateField = invocation.CreateField("delegate", delegateType);
49-
constructor.CodeBuilder.AddStatement(new AssignStatement(delegateField, new ReferenceExpression(arguments[0])));
49+
constructor.CodeBuilder.AddStatement(new AssignStatement(delegateField, arguments[0]));
5050
return constructor;
5151
}
5252

@@ -55,7 +55,7 @@ public MethodInfo GetCallbackMethod()
5555
return delegateType.GetMethod("Invoke");
5656
}
5757

58-
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
58+
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
5959
Reference targetField,
6060
MethodEmitter invokeMethodOnTarget)
6161
{
@@ -65,10 +65,10 @@ public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitte
6565
return new MethodInvocationExpression(@delegate, GetCallbackMethod(), allArgs);
6666
}
6767

68-
public Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy)
68+
public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy)
6969
{
70-
var allArguments = new Expression[arguments.Length + 1];
71-
allArguments[0] = new ReferenceExpression(BuildDelegateToken(proxy));
70+
var allArguments = new IExpression[arguments.Length + 1];
71+
allArguments[0] = BuildDelegateToken(proxy);
7272
Array.Copy(arguments, 0, allArguments, 1, arguments.Length);
7373
return allArguments;
7474
}
@@ -88,11 +88,11 @@ private FieldReference BuildDelegateToken(ClassEmitter proxy)
8888
return callback;
8989
}
9090

91-
private Expression[] GetAllArgs(Expression[] args, Reference targetField)
91+
private IExpression[] GetAllArgs(IExpression[] args, Reference targetField)
9292
{
93-
var allArgs = new Expression[args.Length + 1];
93+
var allArgs = new IExpression[args.Length + 1];
9494
args.CopyTo(allArgs, 1);
95-
allArgs[0] = new ConvertExpression(targetType, targetField.ToExpression());
95+
allArgs[0] = new ConvertExpression(targetType, targetField);
9696
return allArgs;
9797
}
9898

src/Castle.Core/DynamicProxy/Contributors/InvocationWithGenericDelegateContributor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public MethodInfo GetCallbackMethod()
4949
return delegateType.GetMethod("Invoke");
5050
}
5151

52-
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, Expression[] args,
52+
public MethodInvocationExpression GetCallbackMethodInvocation(AbstractTypeEmitter invocation, IExpression[] args,
5353
Reference targetField,
5454
MethodEmitter invokeMethodOnTarget)
5555
{
5656
var @delegate = GetDelegate(invocation, invokeMethodOnTarget);
5757
return new MethodInvocationExpression(@delegate, GetCallbackMethod(), args);
5858
}
5959

60-
public Expression[] GetConstructorInvocationArguments(Expression[] arguments, ClassEmitter proxy)
60+
public IExpression[] GetConstructorInvocationArguments(IExpression[] arguments, ClassEmitter proxy)
6161
{
6262
return arguments;
6363
}
@@ -68,13 +68,12 @@ private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invo
6868
var closedDelegateType = delegateType.MakeGenericType(genericTypeParameters);
6969
var localReference = invokeMethodOnTarget.CodeBuilder.DeclareLocal(closedDelegateType);
7070
var closedMethodOnTarget = method.MethodOnTarget.MakeGenericMethod(genericTypeParameters);
71-
var localTarget = new ReferenceExpression(targetReference);
7271
invokeMethodOnTarget.CodeBuilder.AddStatement(
73-
SetDelegate(localReference, localTarget, closedDelegateType, closedMethodOnTarget));
72+
SetDelegate(localReference, targetReference, closedDelegateType, closedMethodOnTarget));
7473
return localReference;
7574
}
7675

77-
private AssignStatement SetDelegate(LocalReference localDelegate, ReferenceExpression localTarget,
76+
private AssignStatement SetDelegate(LocalReference localDelegate, Reference localTarget,
7877
Type closedDelegateType, MethodInfo closedMethodOnTarget)
7978
{
8079
var delegateCreateDelegate = new MethodInvocationExpression(

src/Castle.Core/DynamicProxy/Contributors/MixinContributor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ private GetTargetExpressionDelegate BuildGetTargetExpression()
110110
{
111111
if (!canChangeTarget)
112112
{
113-
return (c, m) => fields[m.DeclaringType].ToExpression();
113+
return (c, m) => fields[m.DeclaringType];
114114
}
115115

116116
return (c, m) => new NullCoalescingOperatorExpression(
117-
new AsTypeReference(c.GetField("__target"), m.DeclaringType).ToExpression(),
118-
fields[m.DeclaringType].ToExpression());
117+
new AsTypeReference(c.GetField("__target"), m.DeclaringType),
118+
fields[m.DeclaringType]);
119119
}
120120

121121
private FieldReference BuildTargetField(ClassEmitter @class, Type type)

0 commit comments

Comments
 (0)