private Expression GenDlrForMethod(GenContext context) { Expression target = _target.GenDlr(context); Expression[] args = GenTypedArgs(context, _method.GetParameters(), _args); return(AstUtils.SimpleCallHelper(target, _method, args));; }
MethodBuilder GenerateStaticMethod(GenContext context) { string methodName = GetStaticMethodName(); FnExpr fn = context.FnExpr; TypeBuilder tb = fn.TypeBuilder; List <ParameterExpression> parms = new List <ParameterExpression>(_argLocals.count() + 1); ParameterExpression thisParm = Expression.Parameter(fn.BaseType, "this"); _thisBinding.ParamExpression = thisParm; fn.ThisParam = thisParm; parms.Add(thisParm); try { LabelTarget loopLabel = Expression.Label("top"); Var.pushThreadBindings(RT.map(Compiler.LOOP_LABEL, loopLabel, Compiler.METHODS, this)); for (int i = 0; i < _argLocals.count(); i++) { LocalBinding lb = (LocalBinding)_argLocals.nth(i); ParameterExpression parm = Expression.Parameter(typeof(object), lb.Name); lb.ParamExpression = parm; parms.Add(parm); } Expression body = Expression.Block( Expression.Label(loopLabel), Compiler.MaybeBox(_body.GenDlr(context))); LambdaExpression lambda = Expression.Lambda(body, parms); // TODO: Figure out why the Java code nulls all the local variables here. // TODO: Cache all the CreateObjectTypeArray values MethodBuilder mb = tb.DefineMethod(methodName, MethodAttributes.Static, typeof(object), Compiler.CreateObjectTypeArray(NumParams)); lambda.CompileToMethod(mb); //lambda.CompileToMethod(mb, true); return(mb); } finally { Var.popThreadBindings(); } }
public override Expression GenDlr(GenContext context) { Expression exc = _excExpr.GenDlr(context); Expression exc2 = Expression.Convert(exc, typeof(Exception)); return(Expression.Throw(exc2)); }
public override Expression GenAssignDlr(GenContext context, Expr val) { Expression access = GenDlrUnboxed(context); Expression valExpr = val.GenDlr(context); return(Expression.Assign(access, valExpr)); }
public Expression GenAssignDlr(GenContext context, Expr val) { Expression varExpr = context.FnExpr.GenVar(context, _var); Expression valExpr = val.GenDlr(context); return(Expression.Call(varExpr, Compiler.Method_Var_set, valExpr)); }
public override Expression GenAssignDlr(GenContext context, Expr val) { Expression target = _target.GenDlr(context); Expression valExpr = val.GenDlr(context); if (_targetType != null) { Expression convTarget = Expression.Convert(target, _targetType); Expression access = _fieldInfo != null ? Expression.Field(convTarget, _fieldInfo) : Expression.Property(convTarget, _propertyInfo); return(Expression.Assign(access, valExpr)); } else { // TODO: Shouldn't this cause a reflection warning? Expression call = Expression.Call( target, Compiler.Method_Reflector_SetInstanceFieldOrProperty, Expression.Constant(_fieldName), valExpr); return(call); } }
public override Expression GenDlr(GenContext context) { Expression basicBody = _tryExpr.GenDlr(context); // Wrap the basic body, a Comma, in a return to a label LabelTarget target = Expression.Label(basicBody.Type, "ret_label"); //Expression tryBody = Expression.Return(target, basicBody); Expression tryBody = basicBody; CatchBlock[] catches = new CatchBlock[_catchExprs.count()]; for (int i = 0; i < _catchExprs.count(); i++) { CatchClause clause = (CatchClause)_catchExprs.nth(i); ParameterExpression parmExpr = Expression.Parameter(clause.Type, clause.Lb.Name); clause.Lb.ParamExpression = parmExpr; catches[i] = Expression.Catch(parmExpr, clause.Handler.GenDlr(context)); } TryExpression tryStmt = _finallyExpr == null ? Expression.TryCatch(tryBody, catches) : Expression.TryCatchFinally(tryBody, _finallyExpr.GenDlr(context), catches); Expression defaultValue = Expression.Default(basicBody.Type); Expression whole = Expression.Block(tryStmt, Expression.Label(target, defaultValue)); return(whole); }
internal static Expression GenTypedArg(GenContext context, Type type, Expr arg) { if (Compiler.MaybePrimitiveType(arg) == type) return ((MaybePrimitiveExpr)arg).GenDlrUnboxed(context); else // Java has emitUnboxArg -- should we do something similar? return arg.GenDlr(context); }
internal static Expression GenTypedArg(GenContext context, Type type, Expr arg) { if (Compiler.MaybePrimitiveType(arg) == type) return ((MaybePrimitiveExpr)arg).GenDlrUnboxed(context); else { Expression argExpr = arg.GenDlr(context); return GenMaybeUnboxedArg(type, argExpr); } }
public override Expression GenDlr(GenContext context) { Expression target = _target.GenDlr(context); if (_targetType != null && (_fieldInfo != null || _propertyInfo != null)) { Expression convTarget = Expression.Convert(target, _targetType); Expression access = _fieldInfo != null ? Expression.Field(convTarget, _fieldInfo) : Expression.Property(convTarget, _propertyInfo); return(Compiler.MaybeBox(access)); } else { return(Compiler.MaybeBox(Expression.PropertyOrField(target, _fieldName))); } //Or maybe this should call Reflector.invokeNoArgInstanceMember }
public override Expression GenDlr(GenContext context) { Expression objExpr = _expr.GenDlr(context); Expression iobjExpr = Expression.Convert(objExpr, typeof(IObj)); Expression metaExpr = _meta.GenDlr(context); // Do we need a conversion here? probably not. Expression ret = Expression.Call(iobjExpr, Compiler.Method_IObj_withMeta, metaExpr); return(ret); }
internal static Expression GenTypedArg(GenContext context, Type type, Expr arg) { if (Compiler.MaybePrimitiveType(arg) == type) { return(((MaybePrimitiveExpr)arg).GenDlrUnboxed(context)); } else { // Java has emitUnboxArg -- should we do something similar? return(arg.GenDlr(context)); } }
public override Expression GenDlr(GenContext context) { List <Expression> exprs = new List <Expression>(_exprs.count()); for (int i = 0; i < _exprs.count() - 1; i++) { Expr e = (Expr)_exprs.nth(i); exprs.Add(e.GenDlr(context)); } // In Java version, this is split off because the Context in the calls above is forced to be C.STATEMENT. // TODO: Wrap this into the loop above. No real need to do this way. Expr last = (Expr)_exprs.nth(_exprs.count() - 1); exprs.Add(last.GenDlr(context)); return(Expression.Block(exprs)); }
public override Expression GenDlr(GenContext context) { LabelTarget loopLabel = Expression.Label(); List <ParameterExpression> parms = new List <ParameterExpression>(); List <Expression> forms = new List <Expression>(); for (int i = 0; i < _bindingInits.count(); i++) { BindingInit bi = (BindingInit)_bindingInits.nth(i); Type primType = Compiler.MaybePrimitiveType(bi.Init); ParameterExpression parmExpr = Expression.Parameter(primType ?? typeof(object), bi.Binding.Name); bi.Binding.ParamExpression = parmExpr; parms.Add(parmExpr); //forms.Add(Expression.Assign(parmExpr, Compiler.MaybeBox(bi.Init.GenDlr(context)))); Expression initExpr = primType != null ? ((MaybePrimitiveExpr)bi.Init).GenDlrUnboxed(context) : Compiler.MaybeBox(bi.Init.GenDlr(context)); forms.Add(Expression.Assign(parmExpr, initExpr)); } forms.Add(Expression.Label(loopLabel)); try { if (_isLoop) { Var.pushThreadBindings(PersistentHashMap.create(Compiler.LOOP_LABEL, loopLabel)); } forms.Add(_body.GenDlr(context)); } finally { if (_isLoop) { Var.popThreadBindings(); } } Expression block = Expression.Block(parms, forms); return(block); }
public override Expression GenDlr(GenContext context) { Expression fn = _fexpr.GenDlr(context); fn = Expression.Convert(fn, typeof(IFn)); int argCount = _args.count(); Expression[] args = new Expression[argCount]; for (int i = 0; i < argCount; i++) { args[i] = Compiler.MaybeBox(((Expr)_args.nth(i)).GenDlr(context)); } Expression call = GenerateInvocation(InvocationReturnType, fn, args); return(call); }
public override Expression GenAssignDlr(GenContext context, Expr val) { Expression target = _target.GenDlr(context); Expression valExpr = val.GenDlr(context); if (_targetType != null) { Expression convTarget = Expression.Convert(target, _targetType); Expression access = _fieldInfo != null ? Expression.Field(convTarget, _fieldInfo) : Expression.Property(convTarget, _propertyInfo); return Expression.Assign(access, valExpr); } else { // TODO: Shouldn't this cause a reflection warning? Expression call = Expression.Call( target, Compiler.Method_Reflector_SetInstanceFieldOrProperty, Expression.Constant(_fieldName), valExpr); return call; } }
public override Expression GenDlr(GenContext context) { List <Expression> exprs = new List <Expression>(); ParameterExpression parm = Expression.Parameter(typeof(Var), "v"); Expression varExpr = context.FnExpr.GenVar(context, _var); exprs.Add(Expression.Assign(parm, varExpr)); if (_initProvided) { exprs.Add(Expression.Call(parm, Compiler.Method_Var_BindRoot, Compiler.MaybeBox(_init.GenDlr(context)))); } if (_meta != null) { exprs.Add(Expression.Call(parm, Compiler.Method_Var_setMeta, _meta.GenDlr(context))); } exprs.Add(parm); return(Expression.Block(new ParameterExpression[] { parm }, exprs)); }
internal static Expression GenerateDlrExpression(GenContext context, Expr expr) { return expr.GenDlr(context); }
public override Expression GenDlr(GenContext context) { // Original code made a call to RT.IsTrue. // Now we inline the test. // Not clear if there is much speedup from this. //bool testIsBool = _testExpr is MaybePrimitiveExpr && _testExpr.HasClrType && _testExpr.ClrType == typeof(bool); //Expression testCode = testIsBool // ? ((MaybePrimitiveExpr)_testExpr).GenDlrUnboxed(context) // // TODO: Verify the call to MaybeBox is needed. // // TODO: See if we can write the code more directly than calling RT.IsTrue. // : Expression.Call(Compiler.Method_RT_IsTrue, Compiler.MaybeBox(_testExpr.GenDlr(context))); bool testIsBool = _testExpr is MaybePrimitiveExpr && _testExpr.HasClrType && _testExpr.ClrType == typeof(bool); Expression testCode; if (testIsBool) { testCode = ((MaybePrimitiveExpr)_testExpr).GenDlrUnboxed(context); } else { ParameterExpression testVar = Expression.Parameter(typeof(object), "__test"); Expression assign = Expression.Assign(testVar, Compiler.MaybeBox(_testExpr.GenDlr(context))); Expression boolExpr = Expression.Not( Expression.OrElse( Expression.Equal(testVar, Expression.Constant(null)), Expression.AndAlso(Expression.TypeIs(testVar, typeof(bool)), Expression.IsFalse(Expression.Unbox(testVar, typeof(bool)))))); //Expression.Not(Expression.AndAlso(Expression.TypeIs(testVar, typeof(bool)), Expression.IsFalse(Expression.Convert(testVar,typeof(bool)))))); testCode = Expression.Block(typeof(bool), new ParameterExpression[] { testVar }, assign, boolExpr); } Expression thenCode = _thenExpr.GenDlr(context); Expression elseCode = _elseExpr == null ? Expression.Constant(null, typeof(object)) : _elseExpr.GenDlr(context); Type targetType = typeof(object); if (this.HasClrType && this.ClrType != null) { // In this case, both _thenExpr and _elseExpr have types, and they are the same, or one is null. // TODO: Not sure if this works if one has a null value. targetType = this.ClrType; } if (thenCode.Type == typeof(void) && elseCode.Type != typeof(void)) { thenCode = Expression.Block(thenCode, Expression.Default(elseCode.Type)); } else if (elseCode.Type == typeof(void) && thenCode.Type != typeof(void)) { elseCode = Expression.Block(elseCode, Expression.Default(thenCode.Type)); } else if (!Reflector.AreReferenceAssignable(targetType, thenCode.Type) || !Reflector.AreReferenceAssignable(targetType, elseCode.Type)) // Above: this is the test that Expression.Condition does. { // Try to reconcile if (thenCode.Type.IsAssignableFrom(elseCode.Type) && elseCode.Type != typeof(void)) { elseCode = Expression.Convert(elseCode, thenCode.Type); targetType = thenCode.Type; } else if (elseCode.Type.IsAssignableFrom(thenCode.Type) && thenCode.Type != typeof(void)) { thenCode = Expression.Convert(thenCode, elseCode.Type); targetType = elseCode.Type; } else { //if (thenCode.Type == typeof(void)) //{ // thenCode = Expression.Block(thenCode, Expression.Default(elseCode.Type)); // targetType = elseCode.Type; //} //else if (elseCode.Type == typeof(void)) //{ // elseCode = Expression.Block(elseCode, Expression.Default(thenCode.Type)); // targetType = thenCode.Type; //} //else //{ // TODO: Can we find a common ancestor? probably not. thenCode = Expression.Convert(thenCode, typeof(object)); elseCode = Expression.Convert(elseCode, typeof(object)); targetType = typeof(object); //} } } return(Expression.Condition(testCode, thenCode, elseCode, targetType)); }
public override Expression GenDlr(GenContext context) { return(Expression.Block( Expression.Call(Compiler.Method_Monitor_Exit, _target.GenDlr(context)), Compiler.NIL_EXPR.GenDlr(context))); }
public Expression GenAssignDlr(GenContext context, Expr val) { Expression varExpr = context.FnExpr.GenVar(context, _var); Expression valExpr = val.GenDlr(context); return Expression.Call(varExpr, Compiler.Method_Var_set, valExpr); }
public override Expression GenAssignDlr(GenContext context, Expr val) { Expression access = GenDlrUnboxed(context); Expression valExpr = val.GenDlr(context); return Expression.Assign(access, valExpr); }