Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/Castle.Core/DynamicProxy/Generators/Emitters/CodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 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.
Expand All @@ -22,14 +22,12 @@ namespace Castle.DynamicProxy.Generators.Emitters

internal sealed class CodeBuilder
{
private readonly List<LocalReference> locals;
private readonly List<IStatement> statements;
private bool isEmpty;

public CodeBuilder()
{
statements = new List<IStatement>();
locals = new List<LocalReference>();
isEmpty = true;
}

Expand All @@ -47,18 +45,11 @@ public CodeBuilder AddStatement(IStatement statement)

public LocalReference DeclareLocal(Type type)
{
var local = new LocalReference(type);
locals.Add(local);
return local;
return new LocalReference(type);
}

internal void Generate(ILGenerator il)
{
foreach (var local in locals)
{
local.Generate(il);
}

foreach (var statement in statements)
{
statement.Emit(il);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Diagnostics;
using System.Reflection.Emit;

[DebuggerDisplay("{reference} as {type}")]
[DebuggerDisplay("{expression} as {type}")]
internal class AsTypeExpression : IExpression
{
private readonly Reference reference;
private readonly IExpression expression;
private readonly Type type;

public AsTypeExpression(Reference reference, Type type)
public AsTypeExpression(IExpression expression, Type type)
{
this.reference = reference;
this.expression = expression;
this.type = type;
}

public void Emit(ILGenerator gen)
{
reference.Emit(gen);
expression.Emit(gen);
gen.Emit(OpCodes.Isinst, type);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 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.
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Reflection;
using System.Reflection.Emit;

[DebuggerDisplay("{fieldBuilder.Name} ({fieldBuilder.FieldType})")]
[DebuggerDisplay("{field.Name} ({field.FieldType})")]
internal class FieldReference : Reference
{
private readonly FieldInfo field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,9 @@
{
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;
}
private readonly IExpression expression;

public IfNullExpression(IExpression expression, IExpressionOrStatement ifNull, IExpressionOrStatement ifNotNull = null)

Check warning on line 28 in src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 28 in src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 28 in src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs

View workflow job for this annotation

GitHub Actions / Build and test (windows-latest)

Cannot convert null literal to non-nullable reference type.
{
this.expression = expression ?? throw new ArgumentNullException(nameof(expression));
this.ifNull = ifNull;
Expand All @@ -42,15 +34,7 @@

public void Emit(ILGenerator gen)
{
if (reference != null)
{
reference.Emit(gen);
}
else if (expression != null)
{
expression.Emit(gen);
}

expression.Emit(gen);
var notNull = gen.DefineLabel();
gen.Emit(OpCodes.Brtrue_S, notNull);
ifNull.Emit(gen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
using System.Reflection.Emit;

/// <summary>
/// Wraps a reference that is passed
/// ByRef and provides indirect load/store support.
/// Represents the storage location <c>X</c> referenced by a <see cref="Reference"/>
/// holding a managed pointer ("by-ref") <c>&amp;X</c> to it.
/// It essentially has the same function as the pointer indirection / dereferencing operator <c>*</c>.
/// </summary>
[DebuggerDisplay("&{OwnerReference}")]
[DebuggerDisplay("*{byRefReference}")]
internal class IndirectReference : Reference
{
private readonly Reference byRefReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
[DebuggerDisplay("local {Type}")]
internal class LocalReference : Reference
{
private LocalBuilder? localBuilder;
private LocalBuilder? local;

public LocalReference(Type type)
: base(type)
{
}

public override void Generate(ILGenerator gen)
{
localBuilder = gen.DeclareLocal(base.Type);
}

public override void EmitAddress(ILGenerator gen)
{
gen.Emit(OpCodes.Ldloca, localBuilder!);
gen.Emit(OpCodes.Ldloca, GetInitializedLocal(gen));
}

public override void Emit(ILGenerator gen)
{
gen.Emit(OpCodes.Ldloc, localBuilder!);
gen.Emit(OpCodes.Ldloc, GetInitializedLocal(gen));
}

public override void EmitStore(IExpression value, ILGenerator gen)
{
value.Emit(gen);
gen.Emit(OpCodes.Stloc, localBuilder!);
gen.Emit(OpCodes.Stloc, GetInitializedLocal(gen));
}

private LocalBuilder GetInitializedLocal(ILGenerator gen)
{
return local ??= gen.DeclareLocal(Type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 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.
Expand All @@ -19,19 +19,19 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST

internal class NewArrayExpression : IExpression
{
private readonly Type arrayType;
private readonly Type elementType;
private readonly int size;

public NewArrayExpression(int size, Type arrayType)
public NewArrayExpression(int size, Type elementType)
{
this.size = size;
this.arrayType = arrayType;
this.elementType = elementType;
}

public void Emit(ILGenerator gen)
{
gen.Emit(OpCodes.Ldc_I4, size);
gen.Emit(OpCodes.Newarr, arrayType);
gen.Emit(OpCodes.Newarr, elementType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,5 @@ public Type Type
public abstract void Emit(ILGenerator gen);

public abstract void EmitStore(IExpression value, ILGenerator gen);

public virtual void Generate(ILGenerator gen)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,19 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
internal class ReturnStatement : IStatement
{
private readonly IExpression? expression;
private readonly Reference? reference;

public ReturnStatement()
{
}

public ReturnStatement(Reference reference)
{
this.reference = reference;
}

public ReturnStatement(IExpression expression)
{
this.expression = expression;
}

public void Emit(ILGenerator gen)
{
if (reference != null)
{
reference.Emit(gen);
}
else if (expression != null)
{
expression.Emit(gen);
}

expression?.Emit(gen);
gen.Emit(OpCodes.Ret);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 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.
Expand Down Expand Up @@ -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 IndirectReference(
new ArgumentReference(parameter.ParameterType, index + 1)),
new DefaultValueExpression(parameter.ParameterType.GetElementType())));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 IndirectReference(
new ArgumentReference(parameter.ParameterType, index + 1)),
new DefaultValueExpression(parameter.ParameterType.GetElementType())));
}
}
}
Expand Down
Loading