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; }
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); }
public CSharpBinder(DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion) { this.binder = binder; this.expr = expr; this.restrictions = BindingRestrictions.Empty; this.errorSuggestion = errorSuggestion; }
public Argument (Expression expr) { if (expr == null) throw new ArgumentNullException (); this.Expr = expr; }
/// <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; }
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; }
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; }
/// <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; }
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; }
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)); }
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); }
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); }
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 OptionalAssign (Expression s, Location loc) : base (null, s, loc) { }
public override object Visit (Expression expression) { Console.WriteLine ("Visit unknown expression:" + expression); return null; }
protected override void CloneTo (CloneContext clonectx, Expression t) { CompoundAssign ctarget = (CompoundAssign) t; ctarget.right = ctarget.source = source.Clone (clonectx); ctarget.target = target.Clone (clonectx); }
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); }
protected override Compiler.Expression DoResolveDynamic(Compiler.ResolveContext ec, Compiler.Expression memberExpr) { return(new RuntimeDynamicInvocation(this, memberExpr).Resolve(ec)); }
public ContextualReturn (Expression expr) : base (expr, expr.StartLocation) { }
protected override void CloneTo (CloneContext clonectx, Expression target) { throw new NotSupportedException ("should not be reached"); }
public FieldDeclarator (SimpleMemberName name, Expression initializer) { this.Name = name; this.Initializer = initializer; }
public Invocation(Compiler.Expression expr, Compiler.Arguments arguments, CSharpInvokeMemberBinder invokeBinder) : base(expr, arguments) { this.invokeBinder = invokeBinder; }
public OptionalAssign (Expression t, Expression s, Location loc) : base (t, s, loc) { }
// // 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); }
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; }
public HoistedFieldAssign (Expression target, Expression source) : base (target, source, target.Location) { }
protected override void CloneTo (CloneContext clonectx, Expression target) { // TODO: nothing ?? }
public CompoundAssign (Binary.Operator op, Expression target, Expression source, Expression left) : this (op, target, source) { this.left = left; }
protected override void CloneTo (CloneContext clonectx, Expression t) { AnonymousMethodExpression target = (AnonymousMethodExpression) t; target.Block = (ParametersBlock) clonectx.LookupBlock (Block); }
public RuntimeDynamicInvocation(Invocation invoke, Compiler.Expression memberExpr) : base(memberExpr) { this.invoke = invoke; }
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound) { GetFieldExpression (ec).EmitAssign (ec, source, leave_copy, false); }
public TargetExpression (Expression child) { this.child = child; this.loc = child.Location; }
public Quote (Expression expr) : base (expr) { }
public CompoundAssign (Binary.Operator op, Expression target, Expression source) : base (target, source, target.Location) { right = source; this.op = op; }