using System.Linq.Expressions;
using System;
using System.Reflection;
using Microsoft.Scripting.Interpreter;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
namespace IronPython.Compiler {
internal class ClosureExpression : Expression, IPythonVariableExpression {
private readonly Expression _closureCell;
private readonly ParameterExpression _parameter;
private readonly Ast.PythonVariable _variable;
internal static readonly FieldInfo _cellField = typeof(ClosureCell).GetField("Value");
public ClosureExpression(Ast.PythonVariable variable, Expression closureCell, ParameterExpression parameter) {
Assert.NotNull(closureCell);
_variable = variable;
_closureCell = closureCell;
_parameter = parameter;
}
#region ClosureExpression Public API
public Expression ClosureCell {
get {
return _closureCell;
}
}
public ParameterExpression OriginalParameter {
get {
return _parameter;
}
}
public Ast.PythonVariable PythonVariable {
get {
return _variable;
}
}
public Expression Create() {
if (OriginalParameter != null) {
return Expression.Assign(_closureCell, Expression.Call(Ast.AstMethods.MakeClosureCellWithValue, OriginalParameter));
}
return Expression.Assign(_closureCell, MakeClosureCellExpression.Instance);
}
private class MakeClosureCellExpression : Expression, IInstructionProvider {
private static readonly Expression _call = Expression.Call(Ast.AstMethods.MakeClosureCell);
public static readonly MakeClosureCellExpression Instance = new MakeClosureCellExpression();
public override bool CanReduce {
get {
return true;
}
}
public override ExpressionType NodeType {
get {
return ExpressionType.Extension;
}
}
public override Type Type {
get {
return typeof(ClosureCell);
}
}
public override Expression Reduce() {
return _call;
}
#region IInstructionProvider Members
public void AddInstructions(LightCompiler compiler) {
compiler.Instructions.Emit(MakeClosureCellInstruction.Instance);
}
#endregion
private class MakeClosureCellInstruction : Instruction {
public static readonly MakeClosureCellInstruction Instance = new MakeClosureCellInstruction();
public override int ProducedStack {
get {
return 1;
}
}
public override int ConsumedStack {
get {
return 0;
}
}
public override int Run(InterpretedFrame frame) {
frame.Push(PythonOps.MakeClosureCell());
return +1;
}
}
}
#endregion
#region Expression overrides
public sealed override ExpressionType NodeType {
get { return ExpressionType.Extension; }
}
public sealed override Type Type {
get { return typeof(object); }
}
public override bool CanReduce {
get {
return true;
}
}
public string Name {
get {
return _variable.Name;
}
}
public override Expression Reduce() {
return Expression.Field(
_closureCell,
_cellField
);
}
#endregion
#region IPythonVariableExpression implementation
public Expression Assign(Expression value) {
return Expression.Assign(
Expression.Field(_closureCell, _cellField),
value
);
}
public Expression Delete() {
return Expression.Assign(
Expression.Field(_closureCell, _cellField),
Expression.Field(null, typeof(Uninitialized).GetDeclaredField("Instance"))
);
}
#endregion
}
}