Exemplo n.º 1
0
		public static Expression ImplicitTypeParameterConversion (Expression expr, TypeParameterSpec expr_type, TypeSpec target_type)
		{
			//
			// From T to a type parameter U, provided T depends on U
			//
			if (target_type.IsGenericParameter) {
				if (expr_type.TypeArguments != null && expr_type.HasDependencyOn (target_type)) {
					if (expr == null)
						return EmptyExpression.Null;

					if (expr_type.IsReferenceType && !((TypeParameterSpec) target_type).IsReferenceType)
						return new BoxedCast (expr, target_type);

					return new ClassCast (expr, target_type);
				}

				return null;
			}

			//
			// LAMESPEC: From T to dynamic type because it's like T to object
			//
			if (target_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
				if (expr == null)
					return EmptyExpression.Null;

				if (expr_type.IsReferenceType)
					return new ClassCast (expr, target_type);

				return new BoxedCast (expr, target_type);
			}

			//
			// From T to its effective base class C
			// From T to any base class of C (it cannot contain dynamic or be of dynamic type)
			// From T to any interface implemented by C
			//
			var base_type = expr_type.GetEffectiveBase ();
			if (base_type == target_type || TypeSpec.IsBaseClass (base_type, target_type, false) || base_type.ImplementsInterface (target_type, true)) {
				if (expr == null)
					return EmptyExpression.Null;

				if (expr_type.IsReferenceType)
					return new ClassCast (expr, target_type);

				return new BoxedCast (expr, target_type);
			}

			if (target_type.IsInterface && expr_type.IsConvertibleToInterface (target_type)) {
				if (expr == null)
					return EmptyExpression.Null;

				if (expr_type.IsReferenceType)
					return new ClassCast (expr, target_type);

				return new BoxedCast (expr, target_type);
			}

			return null;
		}
Exemplo n.º 2
0
		public override bool Resolve (BlockContext ec)
		{
			expr = expr.Resolve (ec);
			if (expr == null)
				return false;

			Report.Debug (64, "RESOLVE YIELD #1", this, ec, expr, expr.GetType (),
				      ec.CurrentAnonymousMethod, ec.CurrentIterator);

			if (!CheckContext (ec, loc))
				return false;

			iterator = ec.CurrentIterator;
			if (expr.Type != iterator.OriginalIteratorType) {
				expr = Convert.ImplicitConversionRequired (
					ec, expr, iterator.OriginalIteratorType, loc);
				if (expr == null)
					return false;
			}

			if (!ec.CurrentBranching.CurrentUsageVector.IsUnreachable)
				unwind_protect = ec.CurrentBranching.AddResumePoint (this, loc, out resume_pc);

			return true;
		}
		public CSharpBinder (DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion)
		{
			this.binder = binder;
			this.expr = expr;
			this.restrictions = BindingRestrictions.Empty;
			this.errorSuggestion = errorSuggestion;
		}
		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);
		}
Exemplo n.º 5
0
 public CSharpBinder(DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion)
 {
     this.binder          = binder;
     this.expr            = expr;
     this.restrictions    = BindingRestrictions.Empty;
     this.errorSuggestion = errorSuggestion;
 }
Exemplo n.º 6
0
		public Argument (Expression expr)
		{
			if (expr == null)
				throw new ArgumentNullException ();

			this.Expr = expr;
		}
Exemplo n.º 7
0
        /// <summary>
        ///   Performs an explicit conversion of the expression `expr' whose
        ///   type is expr.Type to `target_type'.
        /// </summary>
        public static Expression ExplicitConversion(ResolveContext ec, Expression expr,
			TypeSpec target_type, Location loc)
        {
            Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
            if (e != null) {
                //
                // Don't eliminate explicit precission casts
                //
                if (e == expr) {
                    if (target_type.BuiltinType == BuiltinTypeSpec.Type.Float)
                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);

                    if (target_type.BuiltinType == BuiltinTypeSpec.Type.Double)
                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                }

                return e;
            }

            TypeSpec expr_type = expr.Type;
            if (target_type.IsNullableType) {
                TypeSpec target;

                if (expr_type.IsNullableType) {
                    target = Nullable.NullableInfo.GetUnderlyingType (target_type);
                    Expression unwrap = Nullable.Unwrap.Create (expr);
                    e = ExplicitConversion (ec, unwrap, target, expr.Location);
                    if (e == null)
                        return null;

                    return new Nullable.LiftedConversion (e, unwrap, target_type).Resolve (ec);
                }
                if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Object) {
                    return new UnboxCast (expr, target_type);
                }

                target = TypeManager.GetTypeArguments (target_type) [0];
                e = ExplicitConversionCore (ec, expr, target, loc);
                if (e != null)
                    return TypeSpec.IsReferenceType (expr.Type) ? new UnboxCast (expr, target_type) : Nullable.Wrap.Create (e, target_type);
            } else if (expr_type.IsNullableType) {
                e = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
                if (e != null)
                    return e;

                e = Nullable.Unwrap.Create (expr, false);
                e = ExplicitConversionCore (ec, e, target_type, loc);
                if (e != null)
                    return EmptyCast.Create (e, target_type);
            }

            e = ExplicitUserConversion (ec, expr, target_type, loc);

            if (e != null)
                return e;

            expr.Error_ValueCannotBeConverted (ec, target_type, true);
            return null;
        }
Exemplo n.º 8
0
		public Const (DeclSpace parent, FullNamedExpression type, string name,
			      Expression expr, int mod_flags, Attributes attrs, Location loc)
			: base (parent, type, mod_flags, AllowedModifiers,
				new MemberName (name, loc), attrs)
		{
			initializer = expr;
			ModFlags |= Modifiers.STATIC;
		}
Exemplo n.º 9
0
		public EnumMember (Enum parent, EnumMember prev_member, string name, Expression expr,
				   Attributes attrs, Location loc)
			: base (parent, new EnumTypeExpr (parent), name, expr, Modifiers.PUBLIC,
				attrs, loc)
		{
			this.ParentEnum = parent;
			this.ValueExpr = expr;
			this.prev_member = prev_member;
		}
Exemplo n.º 10
0
        /// <summary>
        ///   Performs an explicit conversion of the expression `expr' whose
        ///   type is expr.Type to `target_type'.
        /// </summary>
        public static Expression ExplicitConversion(ResolveContext ec, Expression expr,
            TypeSpec target_type, Location loc)
        {
            Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
            if (e != null) {
                //
                // Don't eliminate explicit precission casts
                //
                if (e == expr) {
                    if (target_type == TypeManager.float_type)
                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);

                    if (target_type == TypeManager.double_type)
                        return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
                }

                return e;
            }

            TypeSpec expr_type = expr.Type;
            if (TypeManager.IsNullableType (target_type)) {
                if (TypeManager.IsNullableType (expr_type)) {
                    TypeSpec target = Nullable.NullableInfo.GetUnderlyingType (target_type);
                    Expression unwrap = Nullable.Unwrap.Create (expr);
                    e = ExplicitConversion (ec, unwrap, target, expr.Location);
                    if (e == null)
                        return null;

                    return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
                } else if (expr_type == TypeManager.object_type) {
                    return new UnboxCast (expr, target_type);
                } else {
                    TypeSpec target = TypeManager.GetTypeArguments (target_type) [0];

                    e = ExplicitConversionCore (ec, expr, target, loc);
                    if (e != null)
                        return Nullable.Wrap.Create (e, target_type);
                }
            } else if (TypeManager.IsNullableType (expr_type)) {
                bool use_class_cast;
                if (ImplicitBoxingConversionExists (Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type, out use_class_cast))
                    return new BoxedCast (expr, target_type);

                e = Nullable.Unwrap.Create (expr, false);
                e = ExplicitConversionCore (ec, e, target_type, loc);
                if (e != null)
                    return EmptyCast.Create (e, target_type);
            }

            e = ExplicitUserConversion (ec, expr, target_type, loc);
            if (e != null)
                return e;

            expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);
            return null;
        }
Exemplo n.º 11
0
        public Const(DeclSpace parent, FullNamedExpression type, string name,
            Expression expr, Modifiers mod_flags, Attributes attrs, Location loc)
            : base(parent, type, mod_flags, AllowedModifiers,
				new MemberName (name, loc), attrs)
        {
            if (expr != null)
                initializer = new ConstInitializer (this, expr);

            ModFlags |= Modifiers.STATIC;
        }
Exemplo n.º 12
0
        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));
        }
Exemplo n.º 13
0
		static public Expression MakeSimpleCall (EmitContext ec, MethodGroupExpr mg,
							 Expression e, Location loc)
		{
			ArrayList args;
			MethodBase method;
			
			args = new ArrayList (1);
			args.Add (new Argument (e, Argument.AType.Expression));
			method = Invocation.OverloadResolve (ec, (MethodGroupExpr) mg, args, loc);

			if (method == null)
				return null;

			return new StaticCallExpr ((MethodInfo) method, args, loc);
		}
Exemplo n.º 14
0
		CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
		{
			var resolveResult = context.Resolve(expression);
			if (resolveResult.IsError)
				return null;
			
			return new CodeAction(context.TranslateString("Extract method"), script => {
				string methodName = "NewMethod";
				var method = new MethodDeclaration {
					ReturnType = context.CreateShortType(resolveResult.Type),
					Name = methodName,
					Body = new BlockStatement {
						new ReturnStatement(expression.Clone())
					}
				};
				if (!StaticVisitor.UsesNotStaticMember(context, expression))
					method.Modifiers |= Modifiers.Static;

				var usedVariables = VariableLookupVisitor.Analyze(context, expression);
				
				var inExtractedRegion = new VariableUsageAnalyzation (context, usedVariables);

				usedVariables.Sort ((l, r) => l.Region.Begin.CompareTo (r.Region.Begin));
				var target = new IdentifierExpression(methodName);
				var invocation = new InvocationExpression(target);
				foreach (var variable in usedVariables) {
					Expression argumentExpression = new IdentifierExpression(variable.Name); 
					
					var mod = ParameterModifier.None;
					if (inExtractedRegion.GetStatus (variable) == VariableState.Changed) {
						mod = ParameterModifier.Ref;
						argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression);
					}
					
					method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(variable.Type), variable.Name, mod));
					invocation.Arguments.Add(argumentExpression);
				}

				script
					.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method)
					.ContinueScript (delegate {
						script.Replace(expression, invocation);
						script.Link(target, method.NameToken);
					});
			}, expression);
		}
Exemplo n.º 15
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));
        }
Exemplo n.º 16
0
		public OptionalAssign (Expression s, Location loc)
			: base (null, s, loc)
		{
		}
Exemplo n.º 17
0
			public override object Visit (Expression expression)
			{
				Console.WriteLine ("Visit unknown expression:" + expression);
				return null;
			}
Exemplo n.º 18
0
Arquivo: assign.cs Projeto: dyxu/vimrc
		protected override void CloneTo (CloneContext clonectx, Expression t)
		{
			CompoundAssign ctarget = (CompoundAssign) t;

			ctarget.right = ctarget.source = source.Clone (clonectx);
			ctarget.target = target.Clone (clonectx);
		}
Exemplo n.º 19
0
Arquivo: assign.cs Projeto: dyxu/vimrc
		protected override Expression DoResolve (ResolveContext ec)
		{
			right = right.Resolve (ec);
			if (right == null)
				return null;

			MemberAccess ma = target as MemberAccess;
			using (ec.Set (ResolveContext.Options.CompoundAssignmentScope)) {
				target = target.Resolve (ec);
			}
			
			if (target == null)
				return null;

			if (target is MethodGroupExpr){
				ec.Report.Error (1656, loc,
					"Cannot assign to `{0}' because it is a `{1}'",
					((MethodGroupExpr)target).Name, target.ExprClassName);
				return null;
			}

			var event_expr = target as EventExpr;
			if (event_expr != null) {
				source = Convert.ImplicitConversionRequired (ec, right, target.Type, loc);
				if (source == null)
					return null;

				Expression rside;
				if (op == Binary.Operator.Addition)
					rside = EmptyExpression.EventAddition;
				else if (op == Binary.Operator.Subtraction)
					rside = EmptyExpression.EventSubtraction;
				else
					rside = null;

				target = target.ResolveLValue (ec, rside);
				if (target == null)
					return null;

				eclass = ExprClass.Value;
				type = event_expr.Operator.ReturnType;
				return this;
			}

			//
			// Only now we can decouple the original source/target
			// into a tree, to guarantee that we do not have side
			// effects.
			//
			if (left == null)
				left = new TargetExpression (target);

			source = new Binary (op, left, right, true);

			if (target is DynamicMemberAssignable) {
				Arguments targs = ((DynamicMemberAssignable) target).Arguments;
				source = source.Resolve (ec);

				Arguments args = new Arguments (targs.Count + 1);
				args.AddRange (targs);
				args.Add (new Argument (source));

				var binder_flags = CSharpBinderFlags.ValueFromCompoundAssignment;

				//
				// Compound assignment does target conversion using additional method
				// call, set checked context as the binary operation can overflow
				//
				if (ec.HasSet (ResolveContext.Options.CheckedScope))
					binder_flags |= CSharpBinderFlags.CheckedContext;

				if (target is DynamicMemberBinder) {
					source = new DynamicMemberBinder (ma.Name, binder_flags, args, loc).Resolve (ec);

					// Handles possible event addition/subtraction
					if (op == Binary.Operator.Addition || op == Binary.Operator.Subtraction) {
						args = new Arguments (targs.Count + 1);
						args.AddRange (targs);
						args.Add (new Argument (right));
						string method_prefix = op == Binary.Operator.Addition ?
							Event.AEventAccessor.AddPrefix : Event.AEventAccessor.RemovePrefix;

						var invoke = DynamicInvocation.CreateSpecialNameInvoke (
							new MemberAccess (right, method_prefix + ma.Name, loc), args, loc).Resolve (ec);

						args = new Arguments (targs.Count);
						args.AddRange (targs);
						source = new DynamicEventCompoundAssign (ma.Name, args,
							(ExpressionStatement) source, (ExpressionStatement) invoke, loc).Resolve (ec);
					}
				} else {
					source = new DynamicIndexBinder (binder_flags, args, loc).Resolve (ec);
				}

				return source;
			}

			return base.DoResolve (ec);
		}
Exemplo n.º 20
0
 protected override Compiler.Expression DoResolveDynamic(Compiler.ResolveContext ec, Compiler.Expression memberExpr)
 {
     return(new RuntimeDynamicInvocation(this, memberExpr).Resolve(ec));
 }
Exemplo n.º 21
0
		public ContextualReturn (Expression expr)
			: base (expr, expr.StartLocation)
		{
		}
Exemplo n.º 22
0
		protected override void CloneTo (CloneContext clonectx, Expression target)
		{
			throw new NotSupportedException ("should not be reached");
		}
Exemplo n.º 23
0
		public FieldDeclarator (SimpleMemberName name, Expression initializer)
		{
			this.Name = name;
			this.Initializer = initializer;
		}
Exemplo n.º 24
0
 public Invocation(Compiler.Expression expr, Compiler.Arguments arguments, CSharpInvokeMemberBinder invokeBinder)
     : base(expr, arguments)
 {
     this.invokeBinder = invokeBinder;
 }
Exemplo n.º 25
0
		public OptionalAssign (Expression t, Expression s, Location loc)
			: base (t, s, loc)
		{
		}
Exemplo n.º 26
0
		//
		// Initializes all hoisted variables
		//
		public void EmitStoreyInstantiation (EmitContext ec, ExplicitBlock block)
		{
			// There can be only one instance variable for each storey type
			if (Instance != null)
				throw new InternalErrorException ();

			//
			// Create an instance of this storey
			//
			ResolveContext rc = new ResolveContext (ec.MemberContext);
			rc.CurrentBlock = block;

			var storey_type_expr = CreateStoreyTypeExpression (ec);
			var source = new New (storey_type_expr, null, Location).Resolve (rc);

			//
			// When the current context is async (or iterator) lift local storey
			// instantiation to the currect storey
			//
			if (ec.CurrentAnonymousMethod is StateMachineInitializer && (block.HasYield || block.HasAwait)) {
				//
				// Unfortunately, normal capture mechanism could not be used because we are
				// too late in the pipeline and standart assign cannot be used either due to
				// recursive nature of GetStoreyInstanceExpression
				//
				var field = ec.CurrentAnonymousMethod.Storey.AddCompilerGeneratedField (
					LocalVariable.GetCompilerGeneratedName (block), storey_type_expr, true);

				field.Define ();
				field.Emit ();

				var fexpr = new FieldExpr (field, Location);
				fexpr.InstanceExpression = new CompilerGeneratedThis (ec.CurrentType, Location);
				fexpr.EmitAssign (ec, source, false, false);
				Instance = fexpr;
			} else {
				var local = TemporaryVariableReference.Create (source.Type, block, Location);
				if (source.Type.IsStruct) {
					local.LocalInfo.CreateBuilder (ec);
				} else {
					local.EmitAssign (ec, source);
				}

				Instance = local;
			}

			EmitHoistedFieldsInitialization (rc, ec);

			// TODO: Implement properly
			//SymbolWriter.DefineScopeVariable (ID, Instance.Builder);
		}
Exemplo n.º 27
0
		public SideEffectConstant (Constant value, Expression side_effect, Location loc)
			: base (loc)
		{
			this.value = value;
			type = value.Type;
			eclass = ExprClass.Value;

			while (side_effect is SideEffectConstant)
				side_effect = ((SideEffectConstant) side_effect).side_effect;
			this.side_effect = side_effect;
		}
Exemplo n.º 28
0
			public HoistedFieldAssign (Expression target, Expression source)
				: base (target, source, target.Location)
			{
			}
Exemplo n.º 29
0
		protected override void CloneTo (CloneContext clonectx, Expression target)
		{
			// TODO: nothing ??
		}
Exemplo n.º 30
0
Arquivo: assign.cs Projeto: dyxu/vimrc
		public CompoundAssign (Binary.Operator op, Expression target, Expression source, Expression left)
			: this (op, target, source)
		{
			this.left = left;
		}
Exemplo n.º 31
0
		protected override void CloneTo (CloneContext clonectx, Expression t)
		{
			AnonymousMethodExpression target = (AnonymousMethodExpression) t;

			target.Block = (ParametersBlock) clonectx.LookupBlock (Block);
		}
Exemplo n.º 32
0
 public RuntimeDynamicInvocation(Invocation invoke, Compiler.Expression memberExpr)
     : base(memberExpr)
 {
     this.invoke = invoke;
 }
Exemplo n.º 33
0
		public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
		{
			GetFieldExpression (ec).EmitAssign (ec, source, leave_copy, false);
		}
Exemplo n.º 34
0
Arquivo: assign.cs Projeto: dyxu/vimrc
			public TargetExpression (Expression child)
			{
				this.child = child;
				this.loc = child.Location;
			}
Exemplo n.º 35
0
			public Quote (Expression expr)
				: base (expr)
			{
			}
Exemplo n.º 36
0
Arquivo: assign.cs Projeto: dyxu/vimrc
		public CompoundAssign (Binary.Operator op, Expression target, Expression source)
			: base (target, source, target.Location)
		{
			right = source;
			this.op = op;
		}