Example #1
0
        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));
        }
Example #2
0
        public override DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion)
        {
            var ctx   = DynamicContext.Create();
            var left  = ctx.CreateCompilerExpression(argumentInfo [0], target);
            var right = ctx.CreateCompilerExpression(argumentInfo [1], arg);

            bool is_compound;
            var  oper = GetOperator(out is_compound);

            Compiler.Expression expr;

            if (is_compound)
            {
                var target_expr = new Compiler.RuntimeValueExpression(target, ctx.ImportType(target.LimitType));
                expr = new Compiler.CompoundAssign(oper, target_expr, right, left);
            }
            else
            {
                expr = new Compiler.Binary(oper, left, right);
            }

            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(arg);

            return(binder.Bind(ctx, context));
        }
Example #3
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,
                    IgnoreCompilerGeneratedField = 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);
        }
Example #4
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);
		}
        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 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));
        }
Example #8
0
        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, context_type, queried_type,
                                                        name, 0, Compiler.Expression.MemberLookupRestrictions.ExactArity, Compiler.Location.Null);

            var binder = new CSharpBinder(
                this, new Compiler.BoolConstant(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));
        }
Example #10
0
        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));
        }
Example #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));
        }
Example #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));
        }
        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));
        }
Example #15
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, 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;
		}
		public RuntimeBinderContext (DynamicContext ctx, Compiler.TypeSpec callingType)
		{
			this.ctx = ctx;
			this.module = ctx.Module;
			this.callingTypeImported = callingType;
		}
Example #20
0
		public static DynamicContext Create ()
		{
			if (dc != null)
				return dc;

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

				var reporter = new Compiler.Report (ErrorPrinter.Instance) {
					WarningLevel = 0
				};

				var cc = new Compiler.CompilerContext (reporter) {
					IsRuntimeBinder = true
				};

				//IList<Compiler.PredefinedTypeSpec> core_types = null;
				//// HACK: To avoid re-initializing static TypeManager types, like string_type
				//if (!Compiler.RootContext.EvalMode) {
				//    core_types = Compiler.TypeManager.InitCoreTypes ();
				//}

				//
				// 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);
				var temp = new Compiler.AssemblyDefinitionDynamic (module, "dynamic");
				module.SetDeclaringAssembly (temp);

				// Import all currently loaded assemblies
				var domain = AppDomain.CurrentDomain;

				temp.Create (domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
				var importer = new Compiler.ReflectionImporter (cc.BuildinTypes) {
					IgnorePrivateMembers = false
				};

				Compiler.RootContext.ToplevelTypes = module;

				foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
					importer.ImportAssembly (a, module.GlobalRootNamespace);
				}

				if (!Compiler.RootContext.EvalMode) {
					cc.BuildinTypes.CheckDefinitions (module);
					module.InitializePredefinedTypes ();
				}

				dc = new DynamicContext (module, importer);
			}

			return dc;
		}
Example #21
0
        public static DynamicContext Create()
        {
            if (dc != null)
            {
                return(dc);
            }

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

                var reporter = new Compiler.Report(ErrorPrinter.Instance)
                {
                    WarningLevel = 0
                };

                var settings = new Compiler.CompilerSettings();

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

                //IList<Compiler.PredefinedTypeSpec> core_types = null;
                //// HACK: To avoid re-initializing static TypeManager types, like string_type
                //if (!Compiler.RootContext.EvalMode) {
                //    core_types = Compiler.TypeManager.InitCoreTypes ();
                //}

                //
                // 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;
                var temp = new Compiler.AssemblyDefinitionDynamic(module, "dynamic");
                module.SetDeclaringAssembly(temp);

                // Import all currently loaded assemblies
                var domain = AppDomain.CurrentDomain;

                temp.Create(domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
                var importer = new Compiler.ReflectionImporter(module, cc.BuiltinTypes)
                {
                    IgnorePrivateMembers = false
                };

                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);
        }
Example #22
0
		public RuntimeBinderContext (DynamicContext ctx, Compiler.TypeSpec currentType)
		{
			this.ctx = ctx.CompilerContext;
			this.currentType = currentType;
		}
Example #23
0
		public static DynamicContext Create ()
		{
			if (dc != null)
				return dc;

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

				var importer = new Compiler.ReflectionMetaImporter () {
					IgnorePrivateMembers = false
				};

				var core_types = Compiler.TypeManager.InitCoreTypes ();
				importer.Initialize ();

				// I don't think dynamically loaded assemblies can be used as dynamic
				// expression without static type to be loaded first
				// AppDomain.CurrentDomain.AssemblyLoad += (sender, e) => { throw new NotImplementedException (); };

				// Import all currently loaded assemblies
				var ns = Compiler.GlobalRootNamespace.Instance;
				foreach (System.Reflection.Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
					ns.AddAssemblyReference (a);
					importer.ImportAssembly (a, ns);
				}

				var reporter = new Compiler.Report (ErrorPrinter.Instance) {
					WarningLevel = 0
				};

				var cc = new Compiler.CompilerContext (importer, reporter) {
					IsRuntimeBinder = true
				};

				Compiler.TypeManager.InitCoreTypes (cc, core_types);
				Compiler.TypeManager.InitOptionalCoreTypes (cc);

				dc = new DynamicContext (cc);
			}

			return dc;
		}