Skip to content

Commit

Permalink
Merge pull request #583 from stakx/dp/refactor/simple-ast
Browse files Browse the repository at this point in the history
Replace `ConstReference` with `Literal...Expression` node types
  • Loading branch information
stakx authored Mar 1, 2021
2 parents 3f62b31 + 2778d33 commit eb58a73
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ protected override void CustomizeGetObjectData(CodeBuilder codebuilder, Argument
new MethodInvocationExpression(
serializationInfo,
SerializationInfoMethods.AddValue_Bool,
new ConstReference("__delegateToBase"),
new ConstReference(delegateToBaseGetObjectData)));
new LiteralStringExpression("__delegateToBase"),
new LiteralBoolExpression(delegateToBaseGetObjectData)));

if (delegateToBaseGetObjectData == false)
{
Expand Down Expand Up @@ -136,7 +136,7 @@ private void EmitCustomGetObjectData(CodeBuilder codebuilder, ArgumentReference
var addValue = new MethodInvocationExpression(
serializationInfo,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__data"),
new LiteralStringExpression("__data"),
data);
codebuilder.AddStatement(addValue);
}
Expand Down Expand Up @@ -179,7 +179,7 @@ private void GenerateSerializationConstructor(ClassEmitter emitter)
{
var getValue = new MethodInvocationExpression(serializationInfo,
SerializationInfoMethods.GetValue,
new ConstReference(field.Reference.Name),
new LiteralStringExpression(field.Reference.Name),
new TypeTokenExpression(field.Reference.FieldType));
ctor.CodeBuilder.AddStatement(new AssignStatement(
field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ protected override void CustomizeGetObjectData(CodeBuilder codebuilder, Argument
new MethodInvocationExpression(
serializationInfo,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__targetFieldType"),
new ConstReference(targetField.Reference.FieldType.AssemblyQualifiedName)));
new LiteralStringExpression("__targetFieldType"),
new LiteralStringExpression(targetField.Reference.FieldType.AssemblyQualifiedName)));

codebuilder.AddStatement(
new MethodInvocationExpression(
serializationInfo,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__theInterface"),
new ConstReference(targetType.AssemblyQualifiedName)));
new LiteralStringExpression("__theInterface"),
new LiteralStringExpression(targetType.AssemblyQualifiedName)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ protected void ImplementGetObjectData(ClassEmitter emitter)
new MethodInvocationExpression(
null,
TypeMethods.StaticGetType,
new ConstReference(typeof(ProxyObjectReference).AssemblyQualifiedName),
new ConstReference(1),
new ConstReference(0))));
new LiteralStringExpression(typeof(ProxyObjectReference).AssemblyQualifiedName),
new LiteralBoolExpression(true),
new LiteralBoolExpression(false))));

getObjectData.CodeBuilder.AddStatement(
new MethodInvocationExpression(
Expand Down Expand Up @@ -93,36 +93,36 @@ protected void ImplementGetObjectData(ClassEmitter emitter)
new AssignArrayStatement(
interfacesLocal,
i,
new ConstReference(interfaces[i].AssemblyQualifiedName)));
new LiteralStringExpression(interfaces[i].AssemblyQualifiedName)));
}

getObjectData.CodeBuilder.AddStatement(
new MethodInvocationExpression(
info,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__interfaces"),
new LiteralStringExpression("__interfaces"),
interfacesLocal));

getObjectData.CodeBuilder.AddStatement(
new MethodInvocationExpression(
info,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__baseType"),
new ConstReference(emitter.BaseType.AssemblyQualifiedName)));
new LiteralStringExpression("__baseType"),
new LiteralStringExpression(emitter.BaseType.AssemblyQualifiedName)));

getObjectData.CodeBuilder.AddStatement(
new MethodInvocationExpression(
info,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__proxyGenerationOptions"),
new LiteralStringExpression("__proxyGenerationOptions"),
emitter.GetField("proxyGenerationOptions")));

getObjectData.CodeBuilder.AddStatement(
new MethodInvocationExpression(
info,
SerializationInfoMethods.AddValue_Object,
new ConstReference("__proxyTypeId"),
new ConstReference(proxyTypeId)));
new LiteralStringExpression("__proxyTypeId"),
new LiteralStringExpression(proxyTypeId)));

CustomizeGetObjectData(getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter);

Expand All @@ -136,7 +136,7 @@ protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo
new MethodInvocationExpression(
serializationInfo,
SerializationInfoMethods.AddValue_Object,
new ConstReference(field.Reference.Name),
new LiteralStringExpression(field.Reference.Name),
field));
return;
}
Expand Down
26 changes: 0 additions & 26 deletions src/Castle.Core/DynamicProxy/Generators/Emitters/OpCodeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,6 @@ public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)
}
}

/// <summary>
/// Emits a load opcode of the appropriate kind for a constant string or
/// primitive value.
/// </summary>
public static void EmitLoadOpCodeForConstantValue(ILGenerator gen, object value)
{
if (value is string)
{
gen.Emit(OpCodes.Ldstr, value.ToString());
}
else if (value is Int32)
{
var code = LdcOpCodesDictionary.Instance[value.GetType()];
gen.Emit(code, (int)value);
}
else if (value is bool)
{
var code = LdcOpCodesDictionary.Instance[value.GetType()];
gen.Emit(code, Convert.ToInt32(value));
}
else
{
throw new NotSupportedException();
}
}

/// <summary>
/// Emits a load opcode of the appropriate kind for the constant default value of a
/// type, such as 0 for value types and null for reference types.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 LiteralBoolExpression : IExpression
{
private readonly bool value;

public LiteralBoolExpression(bool value)
{
this.value = value;
}

public void Emit(ILGenerator gen)
{
gen.Emit(value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 LiteralStringExpression : IExpression
{
private readonly string value;

public LiteralStringExpression(string value)
{
this.value = value;
}

public void Emit(ILGenerator gen)
{
gen.Emit(OpCodes.Ldstr, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
internal class LoadRefArrayElementExpression : IExpression
{
private readonly Reference arrayReference;
private readonly ConstReference index;
private readonly LiteralIntExpression index;

public LoadRefArrayElementExpression(int index, Reference arrayReference)
: this(new ConstReference(index), arrayReference)
{
}

public LoadRefArrayElementExpression(ConstReference index, Reference arrayReference)
{
this.index = index;
this.index = new LiteralIntExpression(index);
this.arrayReference = arrayReference;
}

public void Emit(ILGenerator gen)
{
ArgumentsUtil.EmitLoadOwnerAndReference(arrayReference, gen);
ArgumentsUtil.EmitLoadOwnerAndReference(index, gen);
index.Emit(gen);
gen.Emit(OpCodes.Ldelem_Ref);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ThrowStatement(Type exceptionType, string errorMessage)
public void Emit(ILGenerator gen)
{
var ci = exceptionType.GetConstructor(new[] { typeof(string) });
var constRef = new ConstReference(errorMessage);
var message = new LiteralStringExpression(errorMessage);

var creationStmt = new NewInstanceExpression(ci, constRef);
var creationStmt = new NewInstanceExpression(ci, message);

creationStmt.Emit(gen);

Expand Down

0 comments on commit eb58a73

Please sign in to comment.