/// <summary> /// Initializes a new instance of the <see cref="DapperRowMetaObject"/> class. /// </summary> /// <param name="expression">The expression representing this <see cref="T:System.Dynamic.DynamicMetaObject" /> during the dynamic binding process.</param> /// <param name="restrictions">The set of binding restrictions under which the binding is valid.</param> public DapperRowMetaObject( System.Linq.Expressions.Expression expression, System.Dynamic.BindingRestrictions restrictions ) : base(expression, restrictions) { }
public MetaObjectBuilder(SilverMetaObject target, DynamicMetaObject[] args) { _restrictions = null; if (target.Restrictions != BindingRestrictions.Empty) { _restrictions = target.Restrictions; } }
public TableMetaObject( Expression expression, BindingRestrictions restrictions, Table value) : base(expression, restrictions, value) { }
public CSharpBinder (DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion) { this.binder = binder; this.expr = expr; this.restrictions = BindingRestrictions.Empty; this.errorSuggestion = errorSuggestion; }
public SmalltalkDynamicMetaObject(Expression expression, BindingRestrictions restrictions, SmalltalkClass cls, object value) : base(expression, restrictions, value) { if (cls == null) throw new ArgumentNullException("cls"); this.Class = cls; }
/// <summary> /// Initializes a new instance of the <see cref="DynamicMetaObject"/> class. /// </summary> /// <param name="expression">The expression representing this <see cref="DynamicMetaObject"/> during the dynamic binding process.</param> /// <param name="restrictions">The set of binding restrictions under which the binding is valid.</param> public DynamicMetaObject(Expression expression, BindingRestrictions restrictions) { ContractUtils.RequiresNotNull(expression, "expression"); ContractUtils.RequiresNotNull(restrictions, "restrictions"); _expression = expression; _restrictions = restrictions; }
public DynamicMetaObject(System.Linq.Expressions.Expression expression, BindingRestrictions restrictions) { ContractUtils.RequiresNotNull(expression, "expression"); ContractUtils.RequiresNotNull(restrictions, "restrictions"); this._expression = expression; this._restrictions = restrictions; }
internal ComInvokeBinder( CallInfo callInfo, DynamicMetaObject[] args, bool[] isByRef, BindingRestrictions restrictions, Expression method, Expression dispatch, ComMethodDesc methodDesc ) { Debug.Assert(callInfo != null, "arguments"); Debug.Assert(args != null, "args"); Debug.Assert(isByRef != null, "isByRef"); Debug.Assert(method != null, "method"); Debug.Assert(dispatch != null, "dispatch"); Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method"); Debug.Assert(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch"); _method = method; _dispatch = dispatch; _methodDesc = methodDesc; _callInfo = callInfo; _args = args; _isByRef = isByRef; _restrictions = restrictions; // Set Instance to some value so that CallBinderHelper has the right number of parameters to work with _instance = dispatch; }
private static DynamicMetaObject ThrowArgumentException(string message, BindingRestrictions restrictions) { var ctorInfo = typeof(InvalidOperationException).GetConstructor(new[] { typeof(string) }); return new DynamicMetaObject( Expression.Throw(Expression.New(ctorInfo, Expression.Constant(message)), typeof(object)), restrictions); }
public override DYN.DynamicMetaObject FallbackGetMember(DYN.DynamicMetaObject target, DYN.DynamicMetaObject errorSuggestion) { DYN.BindingRestrictions restriction = DYN.BindingRestrictions.GetTypeRestriction(target.Expression, target.RuntimeType); string exceptionString = String.Format("GetMemberBinder: cannot bind member '{0}' on object '{1}'", this.Name, target.Value.ToString()); DLR.Expression throwBlock = DLR.Expression.Throw( DLR.Expression.New( typeof(Error.Value).GetConstructor(new Type[] { typeof(string) }), DLR.Expression.Constant(exceptionString) ), typeof(Error) ); DYN.DynamicMetaObject dynobj = new DYN.DynamicMetaObject( throwBlock, restriction ); return(dynobj); }
public FapRowDataMetaObject( System.Linq.Expressions.Expression expression, System.Dynamic.BindingRestrictions restrictions, object value ) : base(expression, restrictions, value) { }
private void Clear() { _condition = null; _restrictions = BindingRestrictions.Empty; _result = null; _initializations = null; _error = false; _treatRestrictionsAsConditions = false; }
internal MetaObjectBuilder(DynamicMetaObject target, params DynamicMetaObject/*!*/[]/*!*/ arguments) { var restrictions = BindingRestrictions.Combine(arguments); if (target != null) { restrictions = target.Restrictions.Merge(restrictions); } _restrictions = restrictions; }
/// <summary> /// Performs binding against a set of overloaded methods using the specified arguments. The arguments are /// consumed as specified by the CallSignature object. /// </summary> /// <param name="resolver">Overload resolver.</param> /// <param name="targets">The methods to be called</param> /// <param name="restrictions">Additional restrictions which should be applied to the resulting MetaObject.</param> /// <returns>A meta object which results from the call.</returns> public DynamicMetaObject CallMethod(DefaultOverloadResolver resolver, IList<MethodBase> targets, BindingRestrictions restrictions) { return CallMethod( resolver, targets, restrictions, null ); }
public InferenceResult(Type type, BindingRestrictions restrictions) { ContractUtils.RequiresNotNull(type, "type"); ContractUtils.RequiresNotNull(restrictions, "restrictions"); _type = type; _restrictions = restrictions; }
/// <summary> /// Initializes a new instance of the <see cref="DynamicMetaObject"/> class. /// </summary> /// <param name="expression">The expression representing this <see cref="DynamicMetaObject"/> during the dynamic binding process.</param> /// <param name="restrictions">The set of binding restrictions under which the binding is valid.</param> public DynamicMetaObject(Expression expression, BindingRestrictions restrictions) { ContractUtils.RequiresNotNull(expression, nameof(expression)); ContractUtils.RequiresNotNull(restrictions, nameof(restrictions)); Expression = expression; Restrictions = restrictions; }
private MetaObjectBuilder(RubyContext siteContext, DynamicMetaObject target, DynamicMetaObject/*!*/[]/*!*/ arguments) { var restrictions = BindingRestrictions.Combine(arguments); if (target != null) { restrictions = target.Restrictions.Merge(restrictions); } _restrictions = restrictions; _siteContext = siteContext; }
/// <summary> /// Checks if any conversions are available and if so builds the target for that conversion. /// </summary> private DynamicMetaObject TryAllConversions(Type toType, ConversionResultKind kind, Type knownType, BindingRestrictions restrictions, DynamicMetaObject arg) { return TryAssignableConversion(toType, knownType, restrictions, arg) ?? // known type -> known type TryExtensibleConversion(toType, knownType, restrictions, arg) ?? // Extensible<T> -> Extensible<T>.Value TryUserDefinedConversion(kind, toType, knownType, restrictions, arg) ?? // op_Implicit TryImplicitNumericConversion(toType, knownType, restrictions, arg) ?? // op_Implicit TryNullableConversion(toType, kind, knownType, restrictions, arg) ?? // null -> Nullable<T> or T -> Nullable<T> TryNullConversion(toType, knownType, restrictions); // null -> reference type }
public void AddRestriction(BindingRestrictions restriction) { if (_restrictions == null) { _restrictions = restriction; } else { _restrictions.Merge(restriction); } }
/// <summary> /// Prepares arguments for argfull overload. /// </summary> /// <param name="routine">Routine for which arguments should be prepared</param> /// <param name="arguments">Arguments to be prepared for the routine</param> /// <param name="genericArguments">Amount of generic arguments provided by call site.</param> /// <param name="regularArguments">Amount of value arguments provided by call site.</param> /// <param name="restrictions">Type restrictions for the arguments</param> /// <returns>Array of prepared arguments to be called with routine</returns> /// <remarks> /// This is basically substitute for everything important that was done in argless overload (except it doesn't use PhpStack but evaluation stack). /// It adopts the arguments according to routine. e.g. dereference reference if value is needed, supplies default argument, etc. /// </remarks> public static Expression[] PrepareArguments(this PhpRoutine routine, DynamicMetaObject[] arguments, int genericArguments, int regularArguments, out BindingRestrictions restrictions) { const int scriptContextIndex = 0; DynamicMetaObject arg; int result_offset = 0; int argument_offset = 0; Expression[] result = new Expression[1 + routine.Signature.GenericParamCount + routine.Signature.ParamCount];//ScriptContext + all arguments = actual arguments to be passed to argfull overload restrictions = BindingRestrictions.Empty; result[scriptContextIndex] = arguments[scriptContextIndex].Expression; ++result_offset; ++argument_offset; // peek pseudo-generic arguments: for (int i = 0; i < routine.Signature.GenericParamCount; ++i) { if (i < genericArguments) { arg = arguments[argument_offset + i]; } else { arg = null; } result[result_offset + i] = GeneratePeekPseudoGenericArgument(routine, arguments[scriptContextIndex], arg, i); // it isn't necessary to add restriction for type argument, it is always DTypeDesc } result_offset += routine.Signature.GenericParamCount; argument_offset += genericArguments; // peek regular arguments: // skip first one ScriptContext and generic parameters for (int i = 0; i < routine.Signature.ParamCount; ++i) { if (i < regularArguments) { arg = arguments[argument_offset + i]; if (arg.RuntimeType != null) restrictions = restrictions.Merge(BindingRestrictions.GetTypeRestriction(arguments[argument_offset + i].Expression, arguments[argument_offset + i].LimitType)); else restrictions = restrictions.Merge(BindingRestrictions.GetInstanceRestriction(arguments[argument_offset + i].Expression, null));//(MB) is it necessary? } else { arg = null; } result[result_offset + i] = GeneratePeekArgument(routine, arguments[scriptContextIndex], arg, i); } return result; }
/// <summary> /// Checks if the conversion can be handled by a simple cast. /// </summary> private static DynamicMetaObject TryAssignableConversion(Type toType, Type type, BindingRestrictions restrictions, DynamicMetaObject arg) { if (toType.IsAssignableFrom(type) || (type == typeof(DynamicNull) && (toType.IsClass || toType.IsInterface))) { // MakeSimpleConversionTarget handles the ConversionResultKind check return MakeSimpleConversionTarget(toType, restrictions, arg); } return null; }
public BoundaryAspectGenerator(object instance, DynamicMetaObject metaObj, IEnumerable<OnMethodBoundaryAspect> aspects, MethodInfo method, IEnumerable<DynamicMetaObject> args, IEnumerable<Type> argsTypes) { _rule = metaObj.Restrictions; var methExecArgs = new MethodExecutionArgs(instance, method, new Arguments(args.Select(x => x.Value).ToArray())); _aspectCalls = new AspectCalls(metaObj.Expression, aspects, args, methExecArgs, argsTypes.Any(t => t.IsByRef)); }
/// <summary> /// Merges the set of binding restrictions with the current binding restrictions. /// </summary> /// <param name="restrictions">The set of restrictions with which to merge the current binding restrictions.</param> /// <returns>The new set of binding restrictions.</returns> public BindingRestrictions Merge(BindingRestrictions restrictions) { ContractUtils.RequiresNotNull(restrictions, "restrictions"); if (this == Empty) { return restrictions; } if (restrictions == Empty) { return this; } return new MergedRestriction(this, restrictions); }
/// <summary> /// Checks if the conversion is to object and produces a target if it is. /// </summary> private static DynamicMetaObject TryConvertToObject(Type toType, Type knownType, DynamicMetaObject arg, BindingRestrictions restrictions) { if (toType == typeof(object)) { if (knownType.IsValueType) { return MakeBoxingTarget(arg, restrictions); } else { return new DynamicMetaObject(arg.Expression, restrictions); } } return null; }
public static void TargetAsObject(DynamicMetaObject target, out Expression target_expr, out object target_value, ref BindingRestrictions restrictions) { target_expr = target.Expression; target_value = target.Value; if (target_value == null) { throw new NotImplementedException(); // TODO: call on NULL } for (;;) { if (target_expr.Type == typeof(PhpValue)) { // Template: target.Object // target.IsObject var value = (PhpValue)target_value; if (value.IsNull) { throw new NotImplementedException(); // TODO: call on NULL } else if (value.IsObject) { restrictions = restrictions.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Property(target_expr, "IsObject"))); target_value = value.Object; target_expr = Expression.Property(target_expr, "Object"); break; } else if (value.IsAlias) { restrictions = restrictions.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Property(target_expr, "IsAlias"))); target_value = value.Alias; target_expr = Expression.Property(target_expr, "Alias"); continue; } else { throw new NotImplementedException(); // TODO: scalar } } else if (target_expr.Type == typeof(PhpAlias)) { // dereference target_value = (PhpAlias)target_value; target_expr = Expression.PropertyOrField(target_expr, "Value"); continue; } // break; } }
/// <summary> /// Performs binding against a set of overloaded methods using the specified arguments. The arguments are /// consumed as specified by the CallSignature object. /// </summary> /// <param name="resolver">Overload resolver.</param> /// <param name="targets">The methods to be called</param> /// <param name="restrictions">Additional restrictions which should be applied to the resulting MetaObject.</param> /// <param name="name">The name of the method or null to use the name from targets.</param> /// <returns>A meta object which results from the call.</returns> public DynamicMetaObject CallMethod(DefaultOverloadResolver resolver, IList<MethodBase> targets, BindingRestrictions restrictions, string name) { BindingTarget target; return CallMethod( resolver, targets, restrictions, name, NarrowingLevel.None, NarrowingLevel.All, out target ); }
string ResolveName(DynamicMetaObject[] args, ref BindingRestrictions restrictions) { if (_name != null) { return _name; } else { Debug.Assert(args.Length >= 1 && args[0].LimitType == typeof(string)); restrictions = restrictions.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Equal(args[0].Expression, Expression.Constant(args[0].Value)))); // args[0] == "VALUE" return (string)args[0].Value; } }
public static DynamicMetaObject MakeError(ErrorInfo error, BindingRestrictions restrictions, Type type) { switch (error.Kind) { case ErrorInfoKind.Error: return new ErrorMetaObject(AstUtils.Convert(error.Expression, type), restrictions); case ErrorInfoKind.Exception: return new ErrorMetaObject(AstUtils.Convert(Expression.Throw(error.Expression), type), restrictions); case ErrorInfoKind.Success: return new ErrorMetaObject(AstUtils.Convert(error.Expression, type), restrictions); default: throw new InvalidOperationException(); } }
void ResolveArgs(DynamicMetaObject[] args, ref BindingRestrictions restrictions, out string fieldName, out Expression valueExpr) { if (_name != null) { fieldName = _name; valueExpr = (args.Length > 0) ? args[0].Expression : null; } else { Debug.Assert(args.Length >= 1 && args[0].LimitType == typeof(string)); restrictions = restrictions.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Equal(args[0].Expression, Expression.Constant(args[0].Value)))); // args[0] == "VALUE" fieldName = (string)args[0].Value; valueExpr = (args.Length > 1) ? args[1].Expression : null; } }
public ForwardingMetaObject( Expression expression, BindingRestrictions restrictions, object forwarder, IDynamicMetaObjectProvider forwardee, Func<Expression, Expression> forwardeeGetter ) : base(expression, restrictions, forwarder) { // We'll use forwardee's meta-object to bind dynamic operations. metaForwardee = forwardee.GetMetaObject( forwardeeGetter( Expression.Convert(expression, forwarder.GetType()) // [1] ) ); }
public override DYN.DynamicMetaObject FallbackConvert(DYN.DynamicMetaObject target, DYN.DynamicMetaObject errorSuggestion) { DYN.BindingRestrictions restriction = DYN.BindingRestrictions.GetTypeRestriction( target.Expression, target.RuntimeType ); // Found an AType, simply return the expression // We check if it implements the AType interface if (target.RuntimeType.GetInterface("AType") != null) { DYN.DynamicMetaObject result = new DYN.DynamicMetaObject( DLR.Expression.Convert(target.Expression, typeof(AType)), restriction ); return(result); } else if (target.RuntimeType.Name == "Int32") { DYN.DynamicMetaObject result = new DYN.DynamicMetaObject( // Create a new AInteger from the input value (need to convert to int first) DLR.Expression.Call( typeof(AInteger).GetMethod("Create", new Type[] { typeof(int) }), DLR.Expression.Convert(target.Expression, typeof(int)) ), restriction ); // .New AplusCore.Types.AInteger((System.Int32)$$arg0) return(result); } else if (target.RuntimeType.Name == "String") { DYN.DynamicMetaObject result = new DYN.DynamicMetaObject( DLR.Expression.Convert(target.Expression, typeof(AType), typeof(Helpers).GetMethod("BuildString") ), restriction ); return(result); } // TODO:: throw new NotImplementedException(); }
internal ComInvokeBinder(IList<ArgumentInfo> arguments, DynamicMetaObject[] args, BindingRestrictions restrictions, Expression method, Expression dispatch, ComMethodDesc methodDesc) { ContractUtils.RequiresNotNull(arguments, "arguments"); ContractUtils.RequiresNotNull(args, "args"); ContractUtils.RequiresNotNull(method, "method"); ContractUtils.RequiresNotNull(dispatch, "dispatch"); ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(ComMethodDesc), method.Type), "method"); ContractUtils.Requires(TypeUtils.AreReferenceAssignable(typeof(IDispatch), dispatch.Type), "dispatch"); _method = method; _dispatch = dispatch; _methodDesc = methodDesc; _arguments = arguments; _args = args; _restrictions = restrictions; // Set Instance to some value so that CallBinderHelper has the right number of parameters to work with _instance = dispatch; }
public static DynamicMetaObject Bind (DynamicMetaObject target, Compiler.Expression expr, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion) { var report = new Compiler.Report (ErrorPrinter.Instance) { WarningLevel = 0 }; var ctx = new Compiler.CompilerContext (report); Compiler.RootContext.ToplevelTypes = new Compiler.ModuleContainer (ctx, true); InitializeCompiler (ctx); Expression res; try { // TODO: ResolveOptions Compiler.ResolveContext rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx)); // Static typemanager and internal caches are not thread-safe lock (resolver) { expr = expr.Resolve (rc); } if (expr == null) throw new RuntimeBinderInternalCompilerException ("Expression resolved to null"); res = expr.MakeExpression (new Compiler.BuilderContext ()); } catch (RuntimeBinderException e) { if (errorSuggestion != null) return errorSuggestion; if (binder_exception_ctor == null) binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) }); // // Uses target type to keep expressions composition working // res = Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (e.Message)), target.LimitType); } catch (Exception) { if (errorSuggestion != null) return errorSuggestion; throw; } return new DynamicMetaObject (res, restrictions); }
private static BindingRestrictions AddRemoteObjectRestrictions(BindingRestrictions restrictions, object[] args, ReadOnlyCollection<ParameterExpression> parameters) { for (int i = 0; i < parameters.Count; i++) { ParameterExpression left = parameters[i]; MarshalByRefObject obj2 = args[i] as MarshalByRefObject; if ((obj2 != null) && !IsComObject(obj2)) { BindingRestrictions expressionRestriction; if (RemotingServices.IsObjectOutOfAppDomain(obj2)) { expressionRestriction = BindingRestrictions.GetExpressionRestriction(Expression.AndAlso(Expression.NotEqual(left, Expression.Constant(null)), Expression.Call(typeof(RemotingServices).GetMethod("IsObjectOutOfAppDomain"), left))); } else { expressionRestriction = BindingRestrictions.GetExpressionRestriction(Expression.AndAlso(Expression.NotEqual(left, Expression.Constant(null)), Expression.Not(Expression.Call(typeof(RemotingServices).GetMethod("IsObjectOutOfAppDomain"), left)))); } restrictions = restrictions.Merge(expressionRestriction); } } return restrictions; }
internal MergedRestriction(BindingRestrictions left, BindingRestrictions right) { Left = left; Right = right; }
/// <summary> /// Returns a Restrictions object which includes our current restrictions merged /// with a restriction limiting our type /// </summary> private BindingRestrictions GetRestrictions() { Debug.Assert(Restrictions == BindingRestrictions.Empty, "We don't merge, restrictions are always empty"); return(BindingRestrictions.GetTypeRestriction(this)); }
// // Summary: // Initializes a new instance of the System.Dynamic.DynamicMetaObject class. // // Parameters: // expression: // The expression representing this System.Dynamic.DynamicMetaObject during // the dynamic binding process. // // restrictions: // The set of binding restrictions under which the binding is valid. // // value: // The runtime value represented by the System.Dynamic.DynamicMetaObject. public DynamicMetaObject(Expression expression, BindingRestrictions restrictions, object value) { Contract.Requires(expression != null); Contract.Requires(restrictions != null); }
/// <summary> /// /// </summary> /// <param name="target">The <see cref="CallbackItem"/> to use.</param> /// <param name="args"></param> /// <remarks> /// Order of items in the <see cref="args"/> array: /// <list type="number"> /// <item><see cref="Aplus"/></item> /// <item>New value for the global variable</item> /// </list> /// </remarks> /// <returns></returns> public override DYN.DynamicMetaObject Bind(DYN.DynamicMetaObject target, DYN.DynamicMetaObject[] args) { ContractUtils.RequiresNotNull(target, "target"); ContractUtils.RequiresNotNullItems(args, "args"); if (target.HasValue && target.Value is CallbackItem) { CallbackItem callbackItem = (CallbackItem)target.Value; AFunc callbackAFunction = (AFunc)callbackItem.CallbackFunction.Data; DLR.Expression callbackItemExpression = DLR.Expression.Convert( target.Expression, typeof(CallbackItem) ); DYN.BindingRestrictions baseRestriction = DYN.BindingRestrictions.GetTypeRestriction( target.Expression, target.LimitType ); DLR.Expression callbackAFunctionExpression = DLR.Expression.Convert( DLR.Expression.Convert(target.Expression, typeof(CallbackItem)) .Property("CallbackFunction").Property("Data"), typeof(AFunc) ); DYN.BindingRestrictions valenceRestriction = DYN.BindingRestrictions.GetExpressionRestriction( DLR.Expression.Equal( callbackAFunctionExpression.Property("Valence"), DLR.Expression.Constant(callbackAFunction.Valence) ) ); // TODO: refactor the argument generation, something similar is in the InvokeBinder IEnumerable <DLR.Expression> callbackBaseArguments = new DLR.Expression[] { callbackItemExpression.Property("StaticData"), // static data DLR.Expression.Convert(args[1].Expression, typeof(AType)), // new value DLR.Expression.Convert(args[2].Expression, typeof(AType)), // index/set of indices DLR.Expression.Convert(args[3].Expression, typeof(AType)), // path (left argument of pick) callbackItemExpression.Property("Context"), // context of the global variable callbackItemExpression.Property("UnqualifiedName") // name of the global variable }.Where((item, i) => i < callbackAFunction.Valence - 1).Reverse(); List <DLR.Expression> callbackArguments = new List <DLR.Expression>(); // Aplus callbackArguments.Add(DLR.Expression.Convert(args[0].Expression, args[0].RuntimeType)); callbackArguments.AddRange(callbackBaseArguments); Type[] callTypes = new Type[callbackAFunction.Valence + 1]; callTypes[0] = typeof(Aplus); for (int i = 1; i < callbackAFunction.Valence; i++) { callTypes[i] = typeof(AType); } // return type callTypes[callbackAFunction.Valence] = typeof(AType); DLR.Expression codeBlock = DLR.Expression.Invoke( DLR.Expression.Convert( callbackAFunctionExpression.Property("Method"), DLR.Expression.GetFuncType(callTypes) ), callbackArguments ); DYN.DynamicMetaObject dynobj = new DYN.DynamicMetaObject(codeBlock, baseRestriction.Merge(valenceRestriction)); return(dynobj); } // Throw a non-function error return(new DYN.DynamicMetaObject( DLR.Expression.Throw( DLR.Expression.New( typeof(Error.NonFunction).GetConstructor(new Type[] { typeof(string) }), DLR.Expression.Call( target.Expression, typeof(object).GetMethod("ToString") ) ), typeof(Error.NonFunction) ), DYN.BindingRestrictions.GetTypeRestriction(target.Expression, target.RuntimeType) )); }
/// <inheritdoc /> /// <summary> /// Initializes a new instance of the <see cref="T:System.Dynamic.DynamicMetaObject" /> class. /// </summary> /// <param name="expression"> /// The expression representing this <see cref="T:System.Dynamic.DynamicMetaObject" /> during the dynamic binding /// process. /// </param> /// <param name="restrictions">The set of binding restrictions under which the binding is valid.</param> /// <param name="value">The runtime value represented by the <see cref="T:System.Dynamic.DynamicMetaObject" />.</param> public DynamicMetaObject(Expression expression, BindingRestrictions restrictions, object value) : this(expression, restrictions) { _value = value; }
public override DYN.DynamicMetaObject FallbackInvoke(DYN.DynamicMetaObject target, DYN.DynamicMetaObject[] args, DYN.DynamicMetaObject errorSuggestion) { if (target.HasValue && target.Value is AReference && ((AReference)target.Value).Data is AFunc) { DLR.Expression dataExpression = DLR.Expression.PropertyOrField( DLR.Expression.Convert(target.Expression, typeof(AType)), "Data" ); DYN.BindingRestrictions baseRestriction = DYN.BindingRestrictions.GetTypeRestriction( target.Expression, target.LimitType ); DYN.BindingRestrictions builtinRestriction; DYN.BindingRestrictions restriction; DLR.Expression targetExpression = DLR.Expression.Convert(dataExpression, typeof(AFunc)); // Func<..> types (argument types + return type) Type[] callTypes; // Convert every function parameters to AType DLR.Expression[] callArguments; DLR.Expression codeBlock = null; if (((AFunc)((AReference)target.Value).Data).IsBuiltin) { switch (this.CallInfo.ArgumentCount) { case 2: callTypes = new Type[args.Length + 2]; callArguments = new DLR.Expression[args.Length + 1]; BuildArguments(args, callTypes, callArguments); // Add the missing infos: callTypes[2] = typeof(AType); callArguments[2] = DLR.Expression.Constant(null, typeof(AType)); // Build the codeblock for the method invoke codeBlock = BuildInvoke(targetExpression, callTypes, callArguments); break; case 3: callTypes = new Type[args.Length + 1]; callArguments = new DLR.Expression[args.Length]; BuildArguments(args, callTypes, callArguments); // Build the codeblock for the method invoke codeBlock = BuildInvoke(targetExpression, callTypes, callArguments); break; default: // Every other case is invalid... codeBlock = DLR.Expression.Block( DLR.Expression.Throw( DLR.Expression.New( typeof(Error.Valence).GetConstructor(new Type[] { typeof(string) }), DLR.Expression.Property(targetExpression, "Name") ) ), DLR.Expression.Default(typeof(AType)) ); break; } builtinRestriction = DYN.BindingRestrictions.GetExpressionRestriction( DLR.Expression.IsTrue(DLR.Expression.Property(targetExpression, "IsBuiltin")) ); } else { callTypes = new Type[args.Length + 1]; callArguments = new DLR.Expression[args.Length]; BuildArguments(args, callTypes, callArguments); codeBlock = DLR.Expression.Block( // Check if the function we are calling have the correct valence DLR.Expression.IfThen( DLR.Expression.NotEqual( DLR.Expression.Property(targetExpression, "Valence"), DLR.Expression.Constant(this.CallInfo.ArgumentCount) ), // Function's valence is incorrect throw error DLR.Expression.Throw( DLR.Expression.New( typeof(Error.Valence).GetConstructor(new Type[] { typeof(string) }), DLR.Expression.Property(targetExpression, "Name") ) ) ), // Function's valence is OK // Call the function BuildInvoke(targetExpression, callTypes, callArguments) ); builtinRestriction = DYN.BindingRestrictions.GetExpressionRestriction( DLR.Expression.IsFalse(DLR.Expression.Property(targetExpression, "IsBuiltin")) ); } restriction = baseRestriction.Merge(builtinRestriction); DYN.DynamicMetaObject dynobj = new DYN.DynamicMetaObject(codeBlock, restriction); return(dynobj); } // Throw a non-function error return(new DYN.DynamicMetaObject( DLR.Expression.Throw( DLR.Expression.New( typeof(Error.NonFunction).GetConstructor(new Type[] { typeof(string) }), DLR.Expression.Call( target.Expression, typeof(object).GetMethod("ToString") ) ), typeof(Error.NonFunction) ), DYN.BindingRestrictions.GetTypeRestriction(target.Expression, target.RuntimeType) )); }
public sealed override Expression Bind(object[] args, ReadOnlyCollection <ParameterExpression> parameters, LabelTarget returnLabel) { Type returnType; ContractUtils.RequiresNotNull(args, "args"); ContractUtils.RequiresNotNull(parameters, "parameters"); ContractUtils.RequiresNotNull(returnLabel, "returnLabel"); if (args.Length == 0) { throw Error.OutOfRange("args.Length", 1); } if (parameters.Count == 0) { throw Error.OutOfRange("parameters.Count", 1); } if (args.Length != parameters.Count) { throw new ArgumentOutOfRangeException("args"); } if (this.IsStandardBinder) { returnType = this.ReturnType; if ((returnLabel.Type != typeof(void)) && !TypeUtils.AreReferenceAssignable(returnLabel.Type, returnType)) { throw Error.BinderNotCompatibleWithCallSite(returnType, this, returnLabel.Type); } } else { returnType = returnLabel.Type; } DynamicMetaObject target = DynamicMetaObject.Create(args[0], parameters[0]); DynamicMetaObject[] objArray = CreateArgumentMetaObjects(args, parameters); DynamicMetaObject obj3 = this.Bind(target, objArray); if (obj3 == null) { throw Error.BindingCannotBeNull(); } Expression expression = obj3.Expression; BindingRestrictions restrictions = obj3.Restrictions; if ((returnType != typeof(void)) && !TypeUtils.AreReferenceAssignable(returnType, expression.Type)) { if (target.Value is IDynamicMetaObjectProvider) { throw Error.DynamicObjectResultNotAssignable(expression.Type, target.Value.GetType(), this, returnType); } throw Error.DynamicBinderResultNotAssignable(expression.Type, this, returnType); } if ((this.IsStandardBinder && (args[0] is IDynamicMetaObjectProvider)) && (restrictions == BindingRestrictions.Empty)) { throw Error.DynamicBindingNeedsRestrictions(target.Value.GetType(), this); } restrictions = AddRemoteObjectRestrictions(restrictions, args, parameters); if (expression.NodeType != ExpressionType.Goto) { expression = Expression.Return(returnLabel, expression); } if (restrictions != BindingRestrictions.Empty) { expression = Expression.IfThen(restrictions.ToExpression(), expression); } return(expression); }
public DynamicMetaObject Defer(params DynamicMetaObject[] args) { return(this.MakeDeferred(BindingRestrictions.Combine(args), args)); }
public DynamicMetaObject(System.Linq.Expressions.Expression expression, BindingRestrictions restrictions) { }
/// <summary> /// Performs the runtime binding of the dynamic operation on a set of arguments. /// </summary> /// <param name="args">An array of arguments to the dynamic operation.</param> /// <param name="parameters">The array of <see cref="ParameterExpression"/> instances that represent the parameters of the call site in the binding process.</param> /// <param name="returnLabel">A LabelTarget used to return the result of the dynamic binding.</param> /// <returns> /// An Expression that performs tests on the dynamic operation arguments, and /// performs the dynamic operation if the tests are valid. If the tests fail on /// subsequent occurrences of the dynamic operation, Bind will be called again /// to produce a new <see cref="Expression"/> for the new argument types. /// </returns> public sealed override Expression Bind(object[] args, ReadOnlyCollection <ParameterExpression> parameters, LabelTarget returnLabel) { ContractUtils.RequiresNotNull(args, "args"); ContractUtils.RequiresNotNull(parameters, "parameters"); ContractUtils.RequiresNotNull(returnLabel, "returnLabel"); if (args.Length == 0) { throw Error.OutOfRange("args.Length", 1); } if (parameters.Count == 0) { throw Error.OutOfRange("parameters.Count", 1); } if (args.Length != parameters.Count) { throw new ArgumentOutOfRangeException(nameof(args)); } // Ensure that the binder's ReturnType matches CallSite's return // type. We do this so meta objects and language binders can // compose trees together without needing to insert converts. Type expectedResult; if (IsStandardBinder) { expectedResult = ReturnType; if (returnLabel.Type != typeof(void) && !TypeUtils.AreReferenceAssignable(returnLabel.Type, expectedResult)) { throw Error.BinderNotCompatibleWithCallSite(expectedResult, this, returnLabel.Type); } } else { // Even for non-standard binders, we have to at least make sure // it works with the CallSite's type to build the return. expectedResult = returnLabel.Type; } DynamicMetaObject target = DynamicMetaObject.Create(args[0], parameters[0]); DynamicMetaObject[] metaArgs = CreateArgumentMetaObjects(args, parameters); DynamicMetaObject binding = Bind(target, metaArgs); if (binding == null) { throw Error.BindingCannotBeNull(); } Expression body = binding.Expression; BindingRestrictions restrictions = binding.Restrictions; // Ensure the result matches the expected result type. if (expectedResult != typeof(void) && !TypeUtils.AreReferenceAssignable(expectedResult, body.Type)) { // // Blame the last person that handled the result: assume it's // the dynamic object (if any), otherwise blame the language. // if (target.Value is IDynamicMetaObjectProvider) { throw Error.DynamicObjectResultNotAssignable(body.Type, target.Value.GetType(), this, expectedResult); } else { throw Error.DynamicBinderResultNotAssignable(body.Type, this, expectedResult); } } // if the target is IDO, standard binders ask it to bind the rule so we may have a target-specific binding. // it makes sense to restrict on the target's type in such cases. // ideally IDO metaobjects should do this, but they often miss that type of "this" is significant. if (IsStandardBinder && args[0] as IDynamicMetaObjectProvider != null) { if (restrictions == BindingRestrictions.Empty) { throw Error.DynamicBindingNeedsRestrictions(target.Value.GetType(), this); } } // Add the return if (body.NodeType != ExpressionType.Goto) { body = Expression.Return(returnLabel, body); } // Finally, add restrictions if (restrictions != BindingRestrictions.Empty) { body = Expression.IfThen(restrictions.ToExpression(), body); } return(body); }
public BindingRestrictionsProxy(BindingRestrictions node) { ContractUtils.RequiresNotNull(node, nameof(node)); _node = node; }
public BindingRestrictionsProxy(BindingRestrictions node) { _node = node; }