public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            var ctx  = DynamicContext.Create();
            var expr = ctx.CreateCompilerExpression(null, target);

            if (Explicit)
            {
                expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(Type), Compiler.Location.Null), expr, Compiler.Location.Null);
            }
            else
            {
                expr = new Compiler.ImplicitCast(expr, ctx.ImportType(Type), (flags & CSharpBinderFlags.ConvertArrayIndex) != 0);
            }

            if ((flags & CSharpBinderFlags.CheckedContext) != 0)
            {
                expr = new Compiler.CheckedExpr(expr, Compiler.Location.Null);
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);

            return(binder.Bind(ctx, context));
        }
		public DynamicMetaObject Bind (DynamicContext ctx, Type callingType)
		{
			Expression res;
			try {
				var rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType), ResolveOptions);

				// Static typemanager and internal caches are not thread-safe
				lock (resolver) {
					expr = expr.Resolve (rc, Compiler.ResolveFlags.VariableOrValue);
				}

				if (expr == null)
					throw new RuntimeBinderInternalCompilerException ("Expression resolved to null");

				res = expr.MakeExpression (new Compiler.BuilderContext ());
			} catch (RuntimeBinderException e) {
				if (errorSuggestion != null)
					return errorSuggestion;

				res = CreateBinderException (e.Message);
			} catch (Exception) {
				if (errorSuggestion != null)
					return errorSuggestion;

				throw;
			}

			return new DynamicMetaObject (res, restrictions);
		}
        public override DynamicMetaObject FallbackGetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject errorSuggestion)
        {
            if (argumentInfo.Count != indexes.Length + 1)
            {
                if (errorSuggestion == null)
                {
                    throw new NotImplementedException();
                }

                return(errorSuggestion);
            }

            var ctx  = DynamicContext.Create();
            var expr = ctx.CreateCompilerExpression(argumentInfo [0], target);
            var args = ctx.CreateCompilerArguments(argumentInfo.Skip(1), indexes);

            expr = new Compiler.ElementAccess(expr, args, Compiler.Location.Null);
            expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);
            binder.AddRestrictions(indexes);

            return(binder.Bind(ctx, callingContext));
        }
        public static DynamicContext Create()
        {
            if (dc != null)
            {
                return(dc);
            }

            lock (compiler_initializer) {
                if (dc != null)
                {
                    return(dc);
                }

                var settings = new Compiler.CompilerSettings()
                {
                    WarningLevel = 0
                };

                var cc = new Compiler.CompilerContext(settings, ErrorPrinter.Instance)
                {
                    IsRuntimeBinder = true
                };

                //
                // Any later loaded assemblies are handled internally by GetAssemblyDefinition
                // domain.AssemblyLoad cannot be used as that would be too destructive as we
                // would hold all loaded assemblies even if they can be never visited
                //
                // TODO: Remove this code and rely on GetAssemblyDefinition only
                //
                var module = new Compiler.ModuleContainer(cc);
                module.HasTypesFullyDefined = true;

                // Setup fake assembly, it's used mostly to simplify checks like friend-access
                var temp = new Compiler.AssemblyDefinitionDynamic(module, "dynamic");
                module.SetDeclaringAssembly(temp);

                var importer = new Compiler.ReflectionImporter(module, cc.BuiltinTypes)
                {
                    IgnorePrivateMembers = false
                };

                // Import all currently loaded assemblies
                // TODO: Rewrite this to populate type cache on-demand, that should greatly
                // reduce our start-up cost
                var domain = AppDomain.CurrentDomain;
                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    importer.ImportAssembly(a, module.GlobalRootNamespace);
                }

                cc.BuiltinTypes.CheckDefinitions(module);
                module.InitializePredefinedTypes();

                dc = new DynamicContext(module, importer);
            }

            return(dc);
        }
Esempio n. 5
0
        public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
        {
            var ctx    = DynamicContext.Create();
            var c_args = ctx.CreateCompilerArguments(argumentInfo.Skip(1), args);
            var t_args = typeArguments == null ?
                         null :
                         new Compiler.TypeArguments(typeArguments.Select(l => new Compiler.TypeExpression(ctx.ImportType(l), Compiler.Location.Null)).ToArray());

            var expr = ctx.CreateCompilerExpression(argumentInfo[0], target);

            //
            // Simple name invocation is actually member access invocation
            // to capture original this argument. This  brings problem when
            // simple name is resolved as a static invocation and member access
            // has to be reduced back to simple name without reporting an error
            //
            if ((flags & CSharpBinderFlags.InvokeSimpleName) != 0)
            {
                var value = expr as Compiler.RuntimeValueExpression;
                if (value != null)
                {
                    value.IsSuggestionOnly = true;
                }
            }

            expr = new Compiler.MemberAccess(expr, Name, t_args, Compiler.Location.Null);
            expr = new Invocation(expr, c_args, this);

            if ((flags & CSharpBinderFlags.ResultDiscarded) == 0)
            {
                expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
            }
            else
            {
                expr = new Compiler.DynamicResultCast(ctx.ImportType(ReturnType), expr);
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);
            binder.AddRestrictions(args);

            if ((flags & CSharpBinderFlags.InvokeSpecialName) != 0)
            {
                binder.ResolveOptions |= Compiler.ResolveContext.Options.InvokeSpecialName;
            }

            return(binder.Bind(ctx, callingContext));
        }
        public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            var ctx = DynamicContext.Create();

            var expr = ctx.CreateCompilerExpression(argumentInfo [0], target);

            expr = new Compiler.MemberAccess(expr, Name);
            expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);

            return(binder.Bind(ctx, callingContext));
        }
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            var ctx          = DynamicContext.Create();
            var context_type = ctx.ImportType(callingContext);
            var queried_type = ctx.ImportType(target.LimitType);
            var rc           = new Compiler.ResolveContext(new RuntimeBinderContext(ctx, context_type), 0);

            var expr = Compiler.Expression.MemberLookup(rc, false, queried_type,
                                                        name, 0, Compiler.Expression.MemberLookupRestrictions.ExactArity, Compiler.Location.Null);

            var binder = new CSharpBinder(
                this, new Compiler.BoolConstant(ctx.CompilerContext.BuiltinTypes, expr is Compiler.EventExpr, Compiler.Location.Null), null);

            binder.AddRestrictions(target);
            return(binder.Bind(ctx, callingContext));
        }
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            var ctx = DynamicContext.Create();

            var type = ctx.CreateCompilerExpression(argumentInfo [0], target);

            target_return_type = type.Type.GetMetaInfo();

            var c_args = ctx.CreateCompilerArguments(argumentInfo.Skip(1), args);

            var binder = new CSharpBinder(
                this, new Compiler.New(type, c_args, Compiler.Location.Null), null);

            binder.AddRestrictions(target);
            binder.AddRestrictions(args);

            return(binder.Bind(ctx, callingContext));
        }
        public override DynamicMetaObject FallbackSetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            if (argumentInfo.Count != indexes.Length + 2)
            {
                if (errorSuggestion == null)
                {
                    throw new NotImplementedException();
                }

                return(errorSuggestion);
            }

            var ctx  = DynamicContext.Create();
            var expr = ctx.CreateCompilerExpression(argumentInfo [0], target);
            var args = ctx.CreateCompilerArguments(argumentInfo.Skip(1), indexes);

            expr = new Compiler.ElementAccess(expr, args, Compiler.Location.Null);

            var source = ctx.CreateCompilerExpression(argumentInfo [indexes.Length + 1], value);

            // Same conversion as in SetMemberBinder
            if ((flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0)
            {
                expr = new Compiler.RuntimeExplicitAssign(expr, source);
            }
            else
            {
                expr = new Compiler.SimpleAssign(expr, source);
            }
            expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);

            if ((flags & CSharpBinderFlags.CheckedContext) != 0)
            {
                expr = new Compiler.CheckedExpr(expr, Compiler.Location.Null);
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);
            binder.AddRestrictions(value);
            binder.AddRestrictions(indexes);

            return(binder.Bind(ctx, callingContext));
        }
        public override DynamicMetaObject FallbackUnaryOperation(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            var ctx  = DynamicContext.Create();
            var expr = ctx.CreateCompilerExpression(argumentInfo [0], target);

            if (Operation == ExpressionType.IsTrue)
            {
                expr = new Compiler.BooleanExpression(expr);
            }
            else if (Operation == ExpressionType.IsFalse)
            {
                expr = new Compiler.BooleanExpressionFalse(expr);
            }
            else
            {
                if (Operation == ExpressionType.Increment)
                {
                    expr = new Compiler.UnaryMutator(Compiler.UnaryMutator.Mode.PreIncrement, expr, Compiler.Location.Null);
                }
                else if (Operation == ExpressionType.Decrement)
                {
                    expr = new Compiler.UnaryMutator(Compiler.UnaryMutator.Mode.PreDecrement, expr, Compiler.Location.Null);
                }
                else
                {
                    expr = new Compiler.Unary(GetOperator(), expr, Compiler.Location.Null);
                }

                expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);

                if ((flags & CSharpBinderFlags.CheckedContext) != 0)
                {
                    expr = new Compiler.CheckedExpr(expr, Compiler.Location.Null);
                }
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);

            return(binder.Bind(ctx, context));
        }
Esempio n. 11
0
        public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            var ctx    = DynamicContext.Create();
            var source = ctx.CreateCompilerExpression(argumentInfo [1], value);
            var expr   = ctx.CreateCompilerExpression(argumentInfo [0], target);

            // Field assignment
            expr = new Compiler.MemberAccess(expr, Name);

            // Compound assignment under dynamic context does not convert result
            // expression but when setting member type we need to do explicit
            // conversion to ensure type match between member type and dynamic
            // expression type
            if ((flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0)
            {
                expr = new Compiler.RuntimeExplicitAssign(expr, source);
            }
            else
            {
                expr = new Compiler.SimpleAssign(expr, source);
            }

            expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);

            if ((flags & CSharpBinderFlags.CheckedContext) != 0)
            {
                expr = new Compiler.CheckedExpr(expr, Compiler.Location.Null);
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);
            binder.AddRestrictions(value);

            return(binder.Bind(ctx, callingContext));
        }
Esempio n. 12
0
        public override DynamicMetaObject FallbackInvoke(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
        {
            var ctx    = DynamicContext.Create();
            var expr   = ctx.CreateCompilerExpression(argumentInfo [0], target);
            var c_args = ctx.CreateCompilerArguments(argumentInfo.Skip(1), args);

            expr = new Compiler.Invocation(expr, c_args);

            if ((flags & CSharpBinderFlags.ResultDiscarded) == 0)
            {
                expr = new Compiler.Cast(new Compiler.TypeExpression(ctx.ImportType(ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
            }
            else
            {
                expr = new Compiler.DynamicResultCast(ctx.ImportType(ReturnType), expr);
            }

            var binder = new CSharpBinder(this, expr, errorSuggestion);

            binder.AddRestrictions(target);
            binder.AddRestrictions(args);

            return(binder.Bind(ctx, callingContext));
        }
Esempio n. 13
0
        public DynamicMetaObject Bind(DynamicContext ctx, Type callingType)
        {
            Expression res;

            try {
                var rc = new Compiler.ResolveContext(new RuntimeBinderContext(ctx, callingType), ResolveOptions);

                // Static typemanager and internal caches are not thread-safe
                lock (resolver) {
                    expr = expr.Resolve(rc, Compiler.ResolveFlags.VariableOrValue);
                }

                if (expr == null)
                {
                    throw new RuntimeBinderInternalCompilerException("Expression resolved to null");
                }

                res = expr.MakeExpression(new Compiler.BuilderContext());
            } catch (RuntimeBinderException e) {
                if (errorSuggestion != null)
                {
                    return(errorSuggestion);
                }

                res = CreateBinderException(e.Message);
            } catch (Exception) {
                if (errorSuggestion != null)
                {
                    return(errorSuggestion);
                }

                throw;
            }

            return(new DynamicMetaObject(res, restrictions));
        }
Esempio n. 14
0
		public static DynamicContext Create ()
		{
			if (dc != null)
				return dc;

			lock (compiler_initializer) {
				if (dc != null)
					return dc;

				var settings = new Compiler.CompilerSettings () {
					WarningLevel = 0
				};

				var cc = new Compiler.CompilerContext (settings, ErrorPrinter.Instance) {
					IsRuntimeBinder = true
				};

				//
				// Any later loaded assemblies are handled internally by GetAssemblyDefinition
				// domain.AssemblyLoad cannot be used as that would be too destructive as we
				// would hold all loaded assemblies even if they can be never visited
				//
				// TODO: Remove this code and rely on GetAssemblyDefinition only
				//
				var module = new Compiler.ModuleContainer (cc);
				module.HasTypesFullyDefined = true;
				
				// Setup fake assembly, it's used mostly to simplify checks like friend-access
				var temp = new Compiler.AssemblyDefinitionDynamic (module, "dynamic");
				module.SetDeclaringAssembly (temp);

				var importer = new Compiler.ReflectionImporter (module, cc.BuiltinTypes) {
					IgnorePrivateMembers = false
				};

				// Import all currently loaded assemblies
				// TODO: Rewrite this to populate type cache on-demand, that should greatly
				// reduce our start-up cost
				var domain = AppDomain.CurrentDomain;
				foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
					importer.ImportAssembly (a, module.GlobalRootNamespace);
				}

				cc.BuiltinTypes.CheckDefinitions (module);
				module.InitializePredefinedTypes ();

				dc = new DynamicContext (module, importer);
			}

			return dc;
		}
		public RuntimeBinderContext (DynamicContext ctx, Compiler.TypeSpec callingType)
		{
			this.ctx = ctx;
			this.module = ctx.Module;
			this.callingTypeImported = callingType;
		}
 public RuntimeBinderContext(DynamicContext ctx, Type callingType)
 {
     this.ctx         = ctx;
     this.module      = ctx.Module;
     this.callingType = callingType;
 }
 public RuntimeBinderContext(DynamicContext ctx, Compiler.TypeSpec callingType)
 {
     this.ctx    = ctx;
     this.module = ctx.Module;
     this.callingTypeImported = callingType;
 }
		public RuntimeBinderContext (DynamicContext ctx, Type callingType)
		{
			this.ctx = ctx;
			this.module = ctx.Module;
			this.callingType = callingType;
		}