void EmitBitwiseBoolean (EmitContext ec) { Label load_left = ec.DefineLabel (); Label load_right = ec.DefineLabel (); Label end_label = ec.DefineLabel (); // null & value, null | value if (left_unwrap == null) { left_unwrap = right_unwrap; right_unwrap = null; right = left; } left_unwrap.Emit (ec); ec.Emit (OpCodes.Brtrue, load_right); // value & null, value | null if (right_unwrap != null) { right_unwrap.Emit (ec); ec.Emit (OpCodes.Brtrue_S, load_left); } left_unwrap.EmitCheck (ec); ec.Emit (OpCodes.Brfalse_S, load_right); // load left ec.MarkLabel (load_left); if (Oper == Operator.BitwiseAnd) { left_unwrap.Load (ec); } else { if (right_unwrap == null) { right.Emit (ec); if (right is EmptyConstantCast || right is EmptyCast) ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type)); } else { right_unwrap.Load (ec); right_unwrap = left_unwrap; } } ec.Emit (OpCodes.Br_S, end_label); // load right ec.MarkLabel (load_right); if (right_unwrap == null) { if (Oper == Operator.BitwiseAnd) { right.Emit (ec); if (right is EmptyConstantCast || right is EmptyCast) ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type)); } else { left_unwrap.Load (ec); } } else { right_unwrap.Load (ec); } ec.MarkLabel (end_label); }
protected override Expression DoResolve (ResolveContext ec) { unwrap = Unwrap.Create (Expr, false); if (unwrap == null) return null; Expression res = base.ResolveOperator (ec, unwrap); if (res != this) { if (user_operator == null) return res; } else { res = Expr = LiftExpression (ec, Expr); } if (res == null) return null; eclass = ExprClass.Value; type = res.Type; return this; }
protected override Expression DoResolve (ResolveContext ec) { if ((Oper & Operator.LogicalMask) != 0) { Error_OperatorCannotBeApplied (ec, left, right); return null; } bool use_default_call = (Oper & (Operator.BitwiseMask | Operator.EqualityMask)) != 0; left_orig = left; if (left.Type.IsNullableType) { left = left_unwrap = Unwrap.Create (left, use_default_call); if (left == null) return null; } right_orig = right; if (right.Type.IsNullableType) { right = right_unwrap = Unwrap.Create (right, use_default_call); if (right == null) return null; } // // Some details are in 6.4.2, 7.2.7 // Arguments can be lifted for equal operators when the return type is bool and both // arguments are of same type // if (left_orig is NullLiteral) { left = right; state |= State.LeftNullLifted; type = ec.BuiltinTypes.Bool; } if (right_orig.IsNull) { if ((Oper & Operator.ShiftMask) != 0) right = new EmptyExpression (ec.BuiltinTypes.Int); else right = left; state |= State.RightNullLifted; type = ec.BuiltinTypes.Bool; } eclass = ExprClass.Value; return DoResolveCore (ec, left_orig, right_orig); }
public Lifted (Expression expr, Unwrap unwrap, TypeSpec type) { this.expr = expr; this.unwrap = unwrap; this.loc = expr.Location; this.type = type; }
Expression ConvertExpression (ResolveContext ec) { // TODO: ImplicitConversionExists should take care of this if (left.eclass == ExprClass.MethodGroup) return null; TypeSpec ltype = left.Type; // // If left is a nullable type and an implicit conversion exists from right to underlying type of left, // the result is underlying type of left // if (ltype.IsNullableType) { unwrap = Unwrap.Create (left, false); if (unwrap == null) return null; // // Reduce (left ?? null) to left // if (right.IsNull) return ReducedExpression.Create (left, this); if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) { left = unwrap; ltype = left.Type; // // If right is a dynamic expression, the result type is dynamic // if (right.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { type = right.Type; // Need to box underlying value type left = Convert.ImplicitBoxingConversion (left, ltype, type); return this; } right = Convert.ImplicitConversion (ec, right, ltype, loc); type = ltype; return this; } } else if (TypeSpec.IsReferenceType (ltype)) { if (Convert.ImplicitConversionExists (ec, right, ltype)) { // // If right is a dynamic expression, the result type is dynamic // if (right.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { type = right.Type; return this; } // // Reduce ("foo" ?? expr) to expression // Constant lc = left as Constant; if (lc != null && !lc.IsDefaultValue) return ReducedExpression.Create (lc, this); // // Reduce (left ?? null) to left OR (null-constant ?? right) to right // if (right.IsNull || lc != null) return ReducedExpression.Create (lc != null ? right : left, this); right = Convert.ImplicitConversion (ec, right, ltype, loc); type = ltype; return this; } // // Special case null ?? null // if (ltype == right.Type) { type = ltype; return this; } } else { return null; } TypeSpec rtype = right.Type; if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup) return null; // // Reduce (null ?? right) to right // if (left.IsNull) return ReducedExpression.Create (right, this).Resolve (ec); left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc); type = rtype; return this; }
void EmitBitwiseBoolean(EmitContext ec) { Label load_left = ec.DefineLabel(); Label load_right = ec.DefineLabel(); Label end_label = ec.DefineLabel(); // null & value, null | value if (left_unwrap == null) { left_unwrap = right_unwrap; right_unwrap = null; right = left; } left_unwrap.Emit(ec); ec.Emit(OpCodes.Brtrue, load_right); // value & null, value | null if (right_unwrap != null) { right_unwrap.Emit(ec); ec.Emit(OpCodes.Brtrue_S, load_left); } left_unwrap.EmitCheck(ec); ec.Emit(OpCodes.Brfalse_S, load_right); // load left ec.MarkLabel(load_left); if (Oper == Operator.BitwiseAnd) { left_unwrap.Load(ec); } else { if (right_unwrap == null) { right.Emit(ec); if (right is EmptyConstantCast || right is EmptyCast) { ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type)); } } else { right_unwrap.Load(ec); right_unwrap = left_unwrap; } } ec.Emit(OpCodes.Br_S, end_label); // load right ec.MarkLabel(load_right); if (right_unwrap == null) { if (Oper == Operator.BitwiseAnd) { right.Emit(ec); if (right is EmptyConstantCast || right is EmptyCast) { ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type)); } } else { left_unwrap.Load(ec); } } else { right_unwrap.Load(ec); } ec.MarkLabel(end_label); }
public override bool Equals(object obj) { Unwrap uw = obj as Unwrap; return(uw != null && expr.Equals(uw.expr)); }
Expression ConvertExpression(ResolveContext ec) { // TODO: ImplicitConversionExists should take care of this if (left.eclass == ExprClass.MethodGroup) { return(null); } TypeSpec ltype = left.Type; // // If left is a nullable type and an implicit conversion exists from right to underlying type of left, // the result is underlying type of left // if (ltype.IsNullableType) { unwrap = Unwrap.Create(left, false); if (unwrap == null) { return(null); } // // Reduce (left ?? null) to left // if (right.IsNull) { return(ReducedExpression.Create(left, this)); } if (Convert.ImplicitConversionExists(ec, right, unwrap.Type)) { left = unwrap; ltype = left.Type; // // If right is a dynamic expression, the result type is dynamic // if (right.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { type = right.Type; // Need to box underlying value type left = Convert.ImplicitBoxingConversion(left, ltype, type); return(this); } right = Convert.ImplicitConversion(ec, right, ltype, loc); type = ltype; return(this); } } else if (TypeSpec.IsReferenceType(ltype)) { if (Convert.ImplicitConversionExists(ec, right, ltype)) { // // If right is a dynamic expression, the result type is dynamic // if (right.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) { type = right.Type; return(this); } // // Reduce ("foo" ?? expr) to expression // Constant lc = left as Constant; if (lc != null && !lc.IsDefaultValue) { return(ReducedExpression.Create(lc, this)); } // // Reduce (left ?? null) to left OR (null-constant ?? right) to right // if (right.IsNull || lc != null) { return(ReducedExpression.Create(lc != null ? right : left, this)); } right = Convert.ImplicitConversion(ec, right, ltype, loc); type = ltype; return(this); } // // Special case null ?? null // if (ltype == right.Type) { type = ltype; return(this); } } else { return(null); } TypeSpec rtype = right.Type; if (!Convert.ImplicitConversionExists(ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup) { return(null); } // // Reduce (null ?? right) to right // if (left.IsNull) { return(ReducedExpression.Create(right, this).Resolve(ec)); } left = Convert.ImplicitConversion(ec, unwrap != null ? unwrap : left, rtype, loc); type = rtype; return(this); }