/// <inheritdoc/> public override void GenerateCode(MethodBodyGenerator generator, MethodCompileOption options) { if (Arguments != null) { var conversions = ArgumentConversions; for (int i = 0; i < Arguments.Count; i++) { Arguments[i].GenerateCode(generator); var conversion = conversions[i]; if (conversion != null) { generator.EmitConvert(conversion); } } } generator.NewObject(Constructor); int length = Expressions.Count; if (length > 0) { var variable = generator.DeclareVariable(Type); generator.StoreVariable(variable); generator.LoadVariable(variable); var conversions = ArrayConversions; generator.LoadInt32(length); Type type = ElementType; generator.NewArray(type); for (int i = 0; i < length; i++) { generator.Duplicate(); generator.LoadInt32(i); var expression = Expressions[i]; expression.GenerateCode(generator); if (expression.Type.IsValueType && type.IsValueType == false) { generator.Box(expression.Type); } var convertion = conversions[i]; if (convertion != null) { generator.EmitConvert(convertion); } generator.StoreArrayElement(type); } var m = Type.GetMethod("AddRange", Utils.ReflectionUtils.PublicInstance); generator.Call(m); generator.LoadVariable(variable); } }
public void GenerateGet(Expression target, MethodBodyGenerator generator, MethodCompileOption option = MethodCompileOption.None) { if (target != null && target.Type.IsValueType) { generator.Box(target.Type); } generator.LoadString(Name); generator.Call(typeof(IDynamicInvocable).GetInstanceMethod(nameof(IDynamicInvocable.SafeGetValue), typeof(string))); // since it is a value type load the address if ((option & MethodCompileOption.EmitStartAddress) == MethodCompileOption.EmitStartAddress) { var temp = generator.DeclareVariable(TypeProvider.AnyType); generator.StoreVariable(temp); generator.LoadAddressOfVariable(temp); } }
public override void GenerateCode(MethodBodyGenerator generator, MethodCompileOption option) { if (Method.IsAbstract && Method.DeclaringType == typeof(IDynamicInvocable)) { Target.GenerateCode(generator); if (Target.Type.IsValueType) { generator.Box(Target.Type); } } else { Target.GenerateCode(generator, MethodCompileOption.EmitStartAddress); } if (Target.NodeType == ExpressionType.MemberAccess) { MemberExpression target = (MemberExpression)Target; if (target.Target.Type.IsValueType) { switch (target.Target.NodeType) { case ExpressionType.Indexer: case ExpressionType.MemberAccess: var temp = generator.DeclareVariable(target.Target.Type); generator.StoreVariable(temp); generator.LoadAddressOfVariable(temp); break; } } } else if (Target.NodeType == ExpressionType.Identifier) { // if it an identifier expression this might be local member call var exp = (NameExpression)Target; if (exp.Binder == null && Method.IsStatic == false) { generator.LoadArgument(0); } } generator.EmitArguments(Arguments, Conversions); generator.Call(Method); // if current value must not be returned for assigment if ((option & MethodCompileOption.Return) == 0 && Type is object && Type != TypeProvider.VoidType) { generator.Pop(); } }
public static void MakeObjectArray(MethodBodyGenerator generator, ICollection <Expression> expressions) { int i = 0; generator.LoadInt32(expressions.Count); generator.NewArray(typeof(object)); foreach (var expression in expressions) { generator.Duplicate(); generator.LoadInt32(i++); expression.Accept(generator).GenerateCode(generator); if (expression.Type.IsValueType) { generator.Box(expression.Type); } generator.StoreArrayElement(typeof(object)); } }
public override void GenerateCode(MethodBodyGenerator generator, MethodCompileOption option) { if (Left.NodeType == ExpressionType.Identifier) { var exp = (NameExpression)Left; var binder = exp.Binder; // binder is null create new local variable if (binder is null) { exp.Binder = binder = new Binders.VariableBinder(generator.DeclareVariable(Right.Type, exp.Name)); } if ((binder.Attributes & Binders.BindingAttributes.HasThis) != 0) { generator.LoadArgument(0); } Right.GenerateCode(generator, Option); if (Conversion != null) { generator.EmitConvert(Conversion); } binder.GenerateSet(Right, generator, option); } else if (Left.NodeType == ExpressionType.MemberAccess) { var exp = (MemberExpression)Left; exp.Target.GenerateCode(generator); // member assign for dynamic to be Any if ((exp.Binder.Attributes & Binders.BindingAttributes.Dynamic) == Binders.BindingAttributes.Dynamic) { generator.Box(TypeProvider.AnyType); } Right.GenerateCode(generator, Option); if (Conversion != null) { generator.EmitConvert(Conversion); } exp.Binder.GenerateSet(Right, generator, option); } else if (Left.NodeType == ExpressionType.Indexer) { GenerateIndexer(generator); } }
public void GenerateSet(Expression right, MethodBodyGenerator generator, MethodCompileOption option = MethodCompileOption.None) { // first parameter to be Any if (right.Type != TypeProvider.AnyType) { if (right.Type.IsValueType) { generator.Box(right.Type); } generator.CallStatic(ReflectionHelpers.ImplicitAny); } generator.LoadString(Name); generator.LoadToken(right.Type); generator.Call((Func <RuntimeTypeHandle, Type>)Type.GetTypeFromHandle); generator.Call(typeof(IDynamicInvocable).GetInstanceMethod(nameof(IDynamicInvocable.SafeSetValue), TypeProvider.AnyType, typeof(string), typeof(Type))); if ((option & MethodCompileOption.Return) == 0) { generator.Pop(); return; } }
/// <inheritdoc/> public override void GenerateCode(MethodBodyGenerator generator) { // Generate code for the start of the statement. var statementLocals = new StatementLocals(); GenerateStartOfStatement(generator, statementLocals); if (NodeType == StatementType.Return) { bool lastStatement = true; if (Value != null) { var exp = Value.Accept(generator); exp.GenerateCode(generator, Expression.AssignOption); if (generator.SyntaxTree is BlockStatement block) { if (block.Statements.Count > 0) { lastStatement = block.Statements[block.Statements.Count - 1] == this; } } var dest = generator.Method.ReturnType; if (dest is null) { throw new System.NullReferenceException(nameof(System.Reflection.MethodInfo.ReturnType)); } // void type no return if (dest != TypeProvider.VoidType) { // todo variable name not used if (generator.ReturnVariable == null) { generator.ReturnVariable = generator.DeclareVariable(dest); } System.Type src = exp.Type; if (!dest.IsAssignableFrom(src)) { if (src.TryImplicitConvert(dest, out System.Reflection.MethodInfo method)) { if (src.IsValueType && method.GetParameters()[0].ParameterType.IsValueType == false) { generator.Box(src); } generator.Call(method); src = method.ReturnType; } else { throw new System.Exception(string.Concat("can't cast ", src, " to ", dest)); } } if (src.IsValueType && dest.IsValueType == false) { generator.Box(src); } generator.StoreVariable(generator.ReturnVariable); if (generator.ReturnTarget == null) { generator.ReturnTarget = generator.CreateLabel(); } } } //last statement is not a return if (!lastStatement) { //if iniside try finally block generator.EmitLongJump(generator, generator.ReturnTarget); } } else if (NodeType == StatementType.Throw) { if (Value == null) { throw new System.ArgumentNullException("Value", "throw expression missing argument"); } var exp = Value.Accept(generator); exp.GenerateCode(generator, Expression.AssignOption); generator.Throw(); } GenerateEndOfStatement(generator, statementLocals); }