2 // nullable.cs: Nullable types support
4 // Authors: Martin Baulig (martin@ximian.com)
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
15 using System.Reflection;
16 using System.Reflection.Emit;
18 namespace Mono.CSharp.Nullable
20 public class NullableType : TypeExpr
24 public NullableType (TypeExpr underlying, Location l)
26 this.underlying = underlying;
29 eclass = ExprClass.Type;
32 public NullableType (TypeSpec type, Location loc)
33 : this (new TypeExpression (type, loc), loc)
36 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
38 if (TypeManager.generic_nullable_type == null) {
39 TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
40 "System", "Nullable", 1, MemberKind.Struct, true);
43 TypeArguments args = new TypeArguments (underlying);
44 GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc);
45 return ctype.ResolveAsTypeTerminal (ec, false);
49 static class NullableInfo
51 public static MethodSpec GetConstructor (TypeSpec nullableType)
53 return TypeManager.GetPredefinedConstructor (nullableType, Location.Null, GetUnderlyingType (nullableType));
56 public static MethodSpec GetHasValue (TypeSpec nullableType)
58 return (MethodSpec) MemberCache.FindMember (nullableType,
59 MemberFilter.Method ("get_HasValue", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
62 public static MethodSpec GetGetValueOrDefault (TypeSpec nullableType)
64 return (MethodSpec) MemberCache.FindMember (nullableType,
65 MemberFilter.Method ("GetValueOrDefault", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
68 public static MethodSpec GetValue (TypeSpec nullableType)
70 return (MethodSpec) MemberCache.FindMember (nullableType,
71 MemberFilter.Method ("get_Value", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
74 public static TypeSpec GetUnderlyingType (TypeSpec nullableType)
76 return ((InflatedTypeSpec) nullableType).TypeArguments[0];
79 public static bool IsNullableType (TypeSpec type)
81 throw new NotImplementedException ("net");
85 public class Unwrap : Expression, IMemoryLocation, IAssignMethod
90 readonly bool useDefaultValue;
92 Unwrap (Expression expr, bool useDefaultValue)
95 this.loc = expr.Location;
96 this.useDefaultValue = useDefaultValue;
98 type = NullableInfo.GetUnderlyingType (expr.Type);
102 public static Expression Create (Expression expr)
105 // Avoid unwraping and wraping of same type
107 Wrap wrap = expr as Wrap;
111 return Create (expr, false);
114 public static Unwrap Create (Expression expr, bool useDefaultValue)
116 return new Unwrap (expr, useDefaultValue);
119 public override Expression CreateExpressionTree (ResolveContext ec)
121 return expr.CreateExpressionTree (ec);
124 protected override Expression DoResolve (ResolveContext ec)
129 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
131 expr = expr.DoResolveLValue (ec, right_side);
135 public override void Emit (EmitContext ec)
139 Invocation.EmitCall (ec, this, NullableInfo.GetGetValueOrDefault (expr.Type), null, loc);
141 Invocation.EmitCall (ec, this, NullableInfo.GetValue (expr.Type), null, loc);
144 public void EmitCheck (EmitContext ec)
147 Invocation.EmitCall (ec, this, NullableInfo.GetHasValue (expr.Type), null, loc);
150 public override bool Equals (object obj)
152 Unwrap uw = obj as Unwrap;
153 return uw != null && expr.Equals (uw.expr);
156 public Expression Original {
162 public override int GetHashCode ()
164 return expr.GetHashCode ();
167 public override bool IsNull {
173 void Store (EmitContext ec)
175 if (expr is VariableReference)
182 LocalVariable.Store (ec);
185 public void Load (EmitContext ec)
187 if (expr is VariableReference)
190 LocalVariable.Emit (ec);
193 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
195 return expr.MakeExpression (ctx);
198 public void AddressOf (EmitContext ec, AddressOp mode)
200 IMemoryLocation ml = expr as VariableReference;
202 ml.AddressOf (ec, mode);
204 LocalVariable.AddressOf (ec, mode);
208 // Keeps result of non-variable expression
210 LocalTemporary LocalVariable {
213 temp = new LocalTemporary (expr.Type);
218 public void Emit (EmitContext ec, bool leave_copy)
226 public void EmitAssign (EmitContext ec, Expression source,
227 bool leave_copy, bool prepare_for_load)
229 InternalWrap wrap = new InternalWrap (source, expr.Type, loc);
230 ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
233 class InternalWrap : Expression
235 public Expression expr;
237 public InternalWrap (Expression expr, TypeSpec type, Location loc)
243 eclass = ExprClass.Value;
246 public override Expression CreateExpressionTree (ResolveContext ec)
248 throw new NotSupportedException ("ET");
251 protected override Expression DoResolve (ResolveContext ec)
256 public override void Emit (EmitContext ec)
259 ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
265 // Calls get_Value method on nullable expression
267 public class UnwrapCall : CompositeExpression
269 public UnwrapCall (Expression expr)
274 protected override Expression DoResolve (ResolveContext rc)
279 type = NullableInfo.GetUnderlyingType (type);
284 public override void Emit (EmitContext ec)
286 Invocation.EmitCall (ec, Child, NullableInfo.GetValue (Child.Type), null, loc);
290 public class Wrap : TypeCast
292 private Wrap (Expression expr, TypeSpec type)
295 eclass = ExprClass.Value;
298 public override Expression CreateExpressionTree (ResolveContext ec)
300 TypeCast child_cast = child as TypeCast;
301 if (child_cast != null) {
303 return child_cast.CreateExpressionTree (ec);
306 return base.CreateExpressionTree (ec);
309 public static Expression Create (Expression expr, TypeSpec type)
312 // Avoid unwraping and wraping of the same type
314 Unwrap unwrap = expr as Unwrap;
315 if (unwrap != null && expr.Type == NullableInfo.GetUnderlyingType (type))
316 return unwrap.Original;
318 return new Wrap (expr, type);
321 public override void Emit (EmitContext ec)
324 ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
329 // Represents null literal lifted to nullable type
331 public class LiftedNull : NullConstant, IMemoryLocation
333 private LiftedNull (TypeSpec nullable_type, Location loc)
334 : base (nullable_type, loc)
336 eclass = ExprClass.Value;
339 public static Constant Create (TypeSpec nullable, Location loc)
341 return new LiftedNull (nullable, loc);
344 public static Constant CreateFromExpression (ResolveContext ec, Expression e)
346 ec.Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
347 TypeManager.CSharpName (e.Type));
349 return ReducedExpression.Create (Create (e.Type, e.Location), e);
352 public override void Emit (EmitContext ec)
354 // TODO: generate less temporary variables
355 LocalTemporary value_target = new LocalTemporary (type);
357 value_target.AddressOf (ec, AddressOp.Store);
358 ec.Emit (OpCodes.Initobj, type);
359 value_target.Emit (ec);
362 public void AddressOf (EmitContext ec, AddressOp Mode)
364 LocalTemporary value_target = new LocalTemporary (type);
366 value_target.AddressOf (ec, AddressOp.Store);
367 ec.Emit (OpCodes.Initobj, type);
368 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
373 // Generic lifting expression, supports all S/S? -> T/T? cases
375 public class Lifted : Expression, IMemoryLocation
377 Expression expr, null_value;
380 public Lifted (Expression expr, Unwrap unwrap, TypeSpec type)
383 this.unwrap = unwrap;
384 this.loc = expr.Location;
388 public Lifted (Expression expr, Expression unwrap, TypeSpec type)
389 : this (expr, unwrap as Unwrap, type)
393 public override Expression CreateExpressionTree (ResolveContext ec)
395 return expr.CreateExpressionTree (ec);
398 protected override Expression DoResolve (ResolveContext ec)
401 // It's null when lifting non-nullable type
403 if (unwrap == null) {
404 // S -> T? is wrap only
405 if (TypeManager.IsNullableType (type))
406 return Wrap.Create (expr, type);
408 // S -> T can be simplified
412 // Wrap target for T?
413 if (TypeManager.IsNullableType (type)) {
414 expr = Wrap.Create (expr, type);
418 null_value = LiftedNull.Create (type, loc);
419 } else if (TypeManager.IsValueType (type)) {
420 null_value = LiftedNull.Create (type, loc);
422 null_value = new NullConstant (type, loc);
425 eclass = ExprClass.Value;
429 public override void Emit (EmitContext ec)
431 Label is_null_label = ec.DefineLabel ();
432 Label end_label = ec.DefineLabel ();
434 unwrap.EmitCheck (ec);
435 ec.Emit (OpCodes.Brfalse, is_null_label);
439 ec.Emit (OpCodes.Br, end_label);
440 ec.MarkLabel (is_null_label);
442 null_value.Emit (ec);
443 ec.MarkLabel (end_label);
446 public void AddressOf (EmitContext ec, AddressOp mode)
448 unwrap.AddressOf (ec, mode);
452 public class LiftedUnaryOperator : Unary, IMemoryLocation
455 Expression user_operator;
457 public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
458 : base (op, expr, loc)
462 public void AddressOf (EmitContext ec, AddressOp mode)
464 unwrap.AddressOf (ec, mode);
467 public override Expression CreateExpressionTree (ResolveContext ec)
469 if (user_operator != null)
470 return user_operator.CreateExpressionTree (ec);
472 if (Oper == Operator.UnaryPlus)
473 return Expr.CreateExpressionTree (ec);
475 return base.CreateExpressionTree (ec);
478 protected override Expression DoResolve (ResolveContext ec)
480 unwrap = Unwrap.Create (Expr, false);
484 Expression res = base.ResolveOperator (ec, unwrap);
486 if (user_operator == null)
489 res = Expr = LiftExpression (ec, Expr);
495 eclass = ExprClass.Value;
500 public override void Emit (EmitContext ec)
502 Label is_null_label = ec.DefineLabel ();
503 Label end_label = ec.DefineLabel ();
505 unwrap.EmitCheck (ec);
506 ec.Emit (OpCodes.Brfalse, is_null_label);
508 if (user_operator != null) {
509 user_operator.Emit (ec);
511 EmitOperator (ec, NullableInfo.GetUnderlyingType (type));
514 ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
515 ec.Emit (OpCodes.Br_S, end_label);
517 ec.MarkLabel (is_null_label);
518 LiftedNull.Create (type, loc).Emit (ec);
520 ec.MarkLabel (end_label);
523 Expression LiftExpression (ResolveContext ec, Expression expr)
525 TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
526 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
527 if (lifted_type == null)
530 expr.Type = lifted_type.Type;
534 protected override Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
536 expr = base.ResolveEnumOperator (ec, expr);
540 Expr = LiftExpression (ec, Expr);
541 return LiftExpression (ec, expr);
544 protected override Expression ResolveUserOperator (ResolveContext ec, Expression expr)
546 expr = base.ResolveUserOperator (ec, expr);
551 // When a user operator is of non-nullable type
553 if (Expr is Unwrap) {
554 user_operator = LiftExpression (ec, expr);
555 return user_operator;
562 public class LiftedBinaryOperator : Binary
564 Unwrap left_unwrap, right_unwrap;
565 Expression left_orig, right_orig;
566 Expression user_operator;
567 MethodSpec wrap_ctor;
569 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right, Location loc)
570 : base (op, left, right, loc)
574 bool IsBitwiseBoolean {
576 return (Oper & Operator.BitwiseMask) != 0 &&
577 ((left_unwrap != null && left_unwrap.Type == TypeManager.bool_type) ||
578 (right_unwrap != null && right_unwrap.Type == TypeManager.bool_type));
582 bool IsLeftNullLifted {
584 return (state & State.LeftNullLifted) != 0;
588 bool IsRightNullLifted {
590 return (state & State.RightNullLifted) != 0;
594 public override Expression CreateExpressionTree (ResolveContext ec)
596 if (user_operator != null)
597 return user_operator.CreateExpressionTree (ec);
599 return base.CreateExpressionTree (ec);
603 // CSC 2 has this behavior, it allows structs to be compared
604 // with the null literal *outside* of a generics context and
605 // inlines that as true or false.
607 Constant CreateNullConstant (ResolveContext ec, Expression expr)
609 // FIXME: Handle side effect constants
610 Constant c = new BoolConstant (Oper == Operator.Inequality, loc).Resolve (ec);
612 if ((Oper & Operator.EqualityMask) != 0) {
613 ec.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
614 TypeManager.CSharpName (expr.Type), c.AsString ());
616 ec.Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
617 TypeManager.CSharpName (expr.Type), c.AsString ());
620 return ReducedExpression.Create (c, this);
623 protected override Expression DoResolve (ResolveContext ec)
625 if ((Oper & Operator.LogicalMask) != 0) {
626 Error_OperatorCannotBeApplied (ec, left, right);
630 bool use_default_call = (Oper & (Operator.BitwiseMask | Operator.EqualityMask)) != 0;
632 if (TypeManager.IsNullableType (left.Type)) {
633 left = left_unwrap = Unwrap.Create (left, use_default_call);
639 if (TypeManager.IsNullableType (right.Type)) {
640 right = right_unwrap = Unwrap.Create (right, use_default_call);
646 // Some details are in 6.4.2, 7.2.7
647 // Arguments can be lifted for equal operators when the return type is bool and both
648 // arguments are of same type
650 if (left_orig.IsNull) {
652 state |= State.LeftNullLifted;
653 type = TypeManager.bool_type;
656 if (right_orig.IsNull) {
658 state |= State.RightNullLifted;
659 type = TypeManager.bool_type;
662 eclass = ExprClass.Value;
663 return DoResolveCore (ec, left_orig, right_orig);
666 void EmitBitwiseBoolean (EmitContext ec)
668 Label load_left = ec.DefineLabel ();
669 Label load_right = ec.DefineLabel ();
670 Label end_label = ec.DefineLabel ();
672 // null & value, null | value
673 if (left_unwrap == null) {
674 left_unwrap = right_unwrap;
679 left_unwrap.Emit (ec);
680 ec.Emit (OpCodes.Brtrue_S, load_right);
682 // value & null, value | null
683 if (right_unwrap != null) {
684 right_unwrap.Emit (ec);
685 ec.Emit (OpCodes.Brtrue_S, load_left);
688 left_unwrap.EmitCheck (ec);
689 ec.Emit (OpCodes.Brfalse_S, load_right);
692 ec.MarkLabel (load_left);
694 if (Oper == Operator.BitwiseAnd) {
695 left_unwrap.Load (ec);
697 if (right_unwrap == null) {
699 if (right is EmptyConstantCast)
700 ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
702 right_unwrap.Load (ec);
703 right_unwrap = left_unwrap;
706 ec.Emit (OpCodes.Br_S, end_label);
709 ec.MarkLabel (load_right);
710 if (right_unwrap == null) {
711 if (Oper == Operator.BitwiseAnd) {
713 if (right is EmptyConstantCast)
714 ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
716 left_unwrap.Load (ec);
719 right_unwrap.Load (ec);
722 ec.MarkLabel (end_label);
726 // Emits optimized equality or inequality operator when possible
728 void EmitEquality (EmitContext ec)
731 // Either left or right is null
733 if (left_unwrap != null && (IsRightNullLifted || right.IsNull)) {
734 left_unwrap.EmitCheck (ec);
735 if (Oper == Binary.Operator.Equality) {
736 ec.Emit (OpCodes.Ldc_I4_0);
737 ec.Emit (OpCodes.Ceq);
742 if (right_unwrap != null && (IsLeftNullLifted || left.IsNull)) {
743 right_unwrap.EmitCheck (ec);
744 if (Oper == Binary.Operator.Equality) {
745 ec.Emit (OpCodes.Ldc_I4_0);
746 ec.Emit (OpCodes.Ceq);
751 Label dissimilar_label = ec.DefineLabel ();
752 Label end_label = ec.DefineLabel ();
754 if (user_operator != null) {
755 user_operator.Emit (ec);
756 ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
761 ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);
764 if (left_unwrap != null)
765 left_unwrap.EmitCheck (ec);
767 if (right_unwrap != null)
768 right_unwrap.EmitCheck (ec);
770 if (left_unwrap != null && right_unwrap != null) {
771 if (Oper == Operator.Inequality)
772 ec.Emit (OpCodes.Xor);
774 ec.Emit (OpCodes.Ceq);
776 if (Oper == Operator.Inequality) {
777 ec.Emit (OpCodes.Ldc_I4_0);
778 ec.Emit (OpCodes.Ceq);
782 ec.Emit (OpCodes.Br_S, end_label);
784 ec.MarkLabel (dissimilar_label);
785 if (Oper == Operator.Inequality)
786 ec.Emit (OpCodes.Ldc_I4_1);
788 ec.Emit (OpCodes.Ldc_I4_0);
790 ec.MarkLabel (end_label);
793 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
796 ec.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
799 public override void Emit (EmitContext ec)
802 // Optimize same expression operation
804 if (right_unwrap != null && right.Equals (left))
805 right_unwrap = left_unwrap;
807 if (user_operator == null && IsBitwiseBoolean) {
808 EmitBitwiseBoolean (ec);
812 if ((Oper & Operator.EqualityMask) != 0) {
817 Label is_null_label = ec.DefineLabel ();
818 Label end_label = ec.DefineLabel ();
820 if (left_unwrap != null) {
821 left_unwrap.EmitCheck (ec);
822 ec.Emit (OpCodes.Brfalse, is_null_label);
826 // Don't emit HasValue check when left and right expressions are same
828 if (right_unwrap != null && !left.Equals (right)) {
829 right_unwrap.EmitCheck (ec);
830 ec.Emit (OpCodes.Brfalse, is_null_label);
833 EmitOperator (ec, left.Type);
835 if (wrap_ctor != null)
836 ec.Emit (OpCodes.Newobj, wrap_ctor);
838 ec.Emit (OpCodes.Br_S, end_label);
839 ec.MarkLabel (is_null_label);
841 if ((Oper & Operator.ComparisonMask) != 0) {
842 ec.Emit (OpCodes.Ldc_I4_0);
844 LiftedNull.Create (type, loc).Emit (ec);
847 ec.MarkLabel (end_label);
850 protected override void EmitOperator (EmitContext ec, TypeSpec l)
852 if (user_operator != null) {
853 user_operator.Emit (ec);
857 if (TypeManager.IsNullableType (l))
858 l = TypeManager.GetTypeArguments (l) [0];
860 base.EmitOperator (ec, l);
863 Expression LiftResult (ResolveContext ec, Expression res_expr)
865 TypeExpr lifted_type;
868 // Avoid double conversion
870 if (left_unwrap == null || IsLeftNullLifted || left_unwrap.Type != left.Type || (left_unwrap != null && IsRightNullLifted)) {
871 lifted_type = new NullableType (left.Type, loc);
872 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
873 if (lifted_type == null)
876 if (left is UserCast || left is TypeCast)
877 left.Type = lifted_type.Type;
879 left = EmptyCast.Create (left, lifted_type.Type);
882 if (left != right && (right_unwrap == null || IsRightNullLifted || right_unwrap.Type != right.Type || (right_unwrap != null && IsLeftNullLifted))) {
883 lifted_type = new NullableType (right.Type, loc);
884 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
885 if (lifted_type == null)
888 if (right is UserCast || right is TypeCast)
889 right.Type = lifted_type.Type;
891 right = EmptyCast.Create (right, lifted_type.Type);
894 if ((Oper & Operator.ComparisonMask) == 0) {
895 lifted_type = new NullableType (res_expr.Type, loc);
896 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
897 if (lifted_type == null)
900 wrap_ctor = NullableInfo.GetConstructor (lifted_type.Type);
901 type = res_expr.Type = lifted_type.Type;
904 if (IsLeftNullLifted) {
905 left = LiftedNull.Create (right.Type, left.Location);
908 // Special case for bool?, the result depends on both null right side and left side value
910 if ((Oper == Operator.BitwiseAnd || Oper == Operator.BitwiseOr) && NullableInfo.GetUnderlyingType (type) == TypeManager.bool_type) {
914 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
915 return LiftedNull.CreateFromExpression (ec, res_expr);
918 // Value types and null comparison
920 if (right_unwrap == null || (Oper & Operator.RelationalMask) != 0)
921 return CreateNullConstant (ec, right_orig).Resolve (ec);
924 if (IsRightNullLifted) {
925 right = LiftedNull.Create (left.Type, right.Location);
928 // Special case for bool?, the result depends on both null right side and left side value
930 if ((Oper == Operator.BitwiseAnd || Oper == Operator.BitwiseOr) && NullableInfo.GetUnderlyingType (type) == TypeManager.bool_type) {
934 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
935 return LiftedNull.CreateFromExpression (ec, res_expr);
938 // Value types and null comparison
940 if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
941 return CreateNullConstant (ec, left_orig);
947 protected override Expression ResolveOperatorPredefined (ResolveContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, TypeSpec enum_type)
949 Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
951 if (e == this || enum_type != null)
952 return LiftResult (ec, e);
955 // 7.9.9 Equality operators and null
957 // The == and != operators permit one operand to be a value of a nullable type and
958 // the other to be the null literal, even if no predefined or user-defined operator
959 // (in unlifted or lifted form) exists for the operation.
961 if (e == null && (Oper & Operator.EqualityMask) != 0) {
962 if ((IsLeftNullLifted && right_unwrap != null) || (IsRightNullLifted && left_unwrap != null))
963 return LiftResult (ec, this);
969 protected override Expression ResolveUserOperator (ResolveContext ec, TypeSpec l, TypeSpec r)
971 Expression expr = base.ResolveUserOperator (ec, l, r);
975 expr = LiftResult (ec, expr);
976 if (expr is Constant)
980 user_operator = expr;
985 public class NullCoalescingOperator : Expression
987 Expression left, right;
990 public NullCoalescingOperator (Expression left, Expression right, Location loc)
997 public override Expression CreateExpressionTree (ResolveContext ec)
999 if (left is NullLiteral)
1000 ec.Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
1002 UserCast uc = left as UserCast;
1003 Expression conversion = null;
1007 Arguments c_args = new Arguments (2);
1008 c_args.Add (new Argument (uc.CreateExpressionTree (ec)));
1009 c_args.Add (new Argument (left.CreateExpressionTree (ec)));
1010 conversion = CreateExpressionFactoryCall (ec, "Lambda", c_args);
1013 Arguments args = new Arguments (3);
1014 args.Add (new Argument (left.CreateExpressionTree (ec)));
1015 args.Add (new Argument (right.CreateExpressionTree (ec)));
1016 if (conversion != null)
1017 args.Add (new Argument (conversion));
1019 return CreateExpressionFactoryCall (ec, "Coalesce", args);
1022 Expression ConvertExpression (ResolveContext ec)
1024 // TODO: ImplicitConversionExists should take care of this
1025 if (left.eclass == ExprClass.MethodGroup)
1028 TypeSpec ltype = left.Type;
1031 // If left is a nullable type and an implicit conversion exists from right to underlying type of left,
1032 // the result is underlying type of left
1034 if (TypeManager.IsNullableType (ltype)) {
1035 unwrap = Unwrap.Create (left, false);
1039 if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
1042 right = Convert.ImplicitConversion (ec, right, type, loc);
1045 } else if (TypeManager.IsReferenceType (ltype)) {
1046 if (Convert.ImplicitConversionExists (ec, right, ltype)) {
1048 // Reduce (constant ?? expr) to constant
1050 Constant lc = left as Constant;
1051 if (lc != null && !lc.IsDefaultValue)
1052 return new SideEffectConstant (lc, right, loc).Resolve (ec);
1055 // Reduce (left ?? null) to left OR (null-constant ?? right) to right
1057 if (right.IsNull || lc != null)
1058 return ReducedExpression.Create (lc != null ? right : left, this);
1060 right = Convert.ImplicitConversion (ec, right, ltype, loc);
1068 TypeSpec rtype = right.Type;
1069 if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
1073 // Reduce (null ?? right) to right
1076 return ReducedExpression.Create (right, this);
1078 left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
1083 protected override Expression DoResolve (ResolveContext ec)
1085 left = left.Resolve (ec);
1086 right = right.Resolve (ec);
1088 if (left == null || right == null)
1091 eclass = ExprClass.Value;
1093 Expression e = ConvertExpression (ec);
1095 Binary.Error_OperatorCannotBeApplied (ec, left, right, "??", loc);
1102 public override void Emit (EmitContext ec)
1104 Label end_label = ec.DefineLabel ();
1106 if (unwrap != null) {
1107 Label is_null_label = ec.DefineLabel ();
1109 unwrap.EmitCheck (ec);
1110 ec.Emit (OpCodes.Brfalse, is_null_label);
1113 ec.Emit (OpCodes.Br, end_label);
1115 ec.MarkLabel (is_null_label);
1118 ec.MarkLabel (end_label);
1123 ec.Emit (OpCodes.Dup);
1125 // Only to make verifier happy
1126 if (left.Type.IsGenericParameter)
1127 ec.Emit (OpCodes.Box, left.Type);
1129 ec.Emit (OpCodes.Brtrue, end_label);
1131 ec.Emit (OpCodes.Pop);
1134 ec.MarkLabel (end_label);
1137 protected override void CloneTo (CloneContext clonectx, Expression t)
1139 NullCoalescingOperator target = (NullCoalescingOperator) t;
1141 target.left = left.Clone (clonectx);
1142 target.right = right.Clone (clonectx);
1146 public class LiftedUnaryMutator : ExpressionStatement
1148 public readonly UnaryMutator.Mode Mode;
1150 UnaryMutator underlying;
1153 public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
1160 public override Expression CreateExpressionTree (ResolveContext ec)
1162 return new SimpleAssign (this, this).CreateExpressionTree (ec);
1165 protected override Expression DoResolve (ResolveContext ec)
1167 expr = expr.Resolve (ec);
1171 unwrap = Unwrap.Create (expr, false);
1175 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
1176 if (underlying == null)
1180 eclass = ExprClass.Value;
1185 void DoEmit (EmitContext ec, bool is_expr)
1187 Label is_null_label = ec.DefineLabel ();
1188 Label end_label = ec.DefineLabel ();
1190 unwrap.EmitCheck (ec);
1191 ec.Emit (OpCodes.Brfalse, is_null_label);
1194 underlying.Emit (ec);
1195 ec.Emit (OpCodes.Br_S, end_label);
1197 underlying.EmitStatement (ec);
1200 ec.MarkLabel (is_null_label);
1202 LiftedNull.Create (type, loc).Emit (ec);
1204 ec.MarkLabel (end_label);
1207 public override void Emit (EmitContext ec)
1212 public override void EmitStatement (EmitContext ec)