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;
17 using System.Collections;
19 namespace Mono.CSharp.Nullable
21 public class NullableType : TypeExpr
25 public NullableType (TypeExpr underlying, Location l)
27 this.underlying = underlying;
30 eclass = ExprClass.Type;
33 public NullableType (Type type, Location loc)
34 : this (new TypeExpression (type, loc), loc)
37 protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
39 if (TypeManager.generic_nullable_type == null) {
40 TypeManager.generic_nullable_type = TypeManager.CoreLookupType (
41 "System", "Nullable`1", Kind.Struct, true);
44 TypeArguments args = new TypeArguments (underlying);
45 GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc);
46 return ctype.ResolveAsTypeTerminal (ec, false);
49 public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
51 return ResolveAsBaseTerminal (ec, silent);
55 public sealed class NullableInfo
57 public readonly Type Type;
58 public readonly Type UnderlyingType;
59 public MethodInfo HasValue;
60 public MethodInfo Value;
61 public MethodInfo GetValueOrDefault;
62 public ConstructorInfo Constructor;
64 public NullableInfo (Type type)
67 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
69 PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
70 PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
71 GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
73 HasValue = has_value_pi.GetGetMethod (false);
74 Value = value_pi.GetGetMethod (false);
76 if (UnderlyingType.Module == CodeGen.Module.Builder) {
77 Type o_type = TypeManager.DropGenericTypeArguments (type);
78 Constructor = TypeBuilder.GetConstructor (type,
79 TypeManager.GetPredefinedConstructor (o_type, Location.Null, o_type.GetGenericArguments ()));
83 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
87 public class Unwrap : Expression, IMemoryLocation, IAssignMethod
94 protected Unwrap (Expression expr)
97 this.loc = expr.Location;
99 info = new NullableInfo (expr.Type);
100 type = info.UnderlyingType;
101 eclass = expr.eclass;
104 public static Expression Create (Expression expr)
107 // Avoid unwraping and wraping of same type
109 Wrap wrap = expr as Wrap;
113 return Create (expr, null);
116 public static Unwrap Create (Expression expr, EmitContext ec)
118 return new Unwrap (expr);
121 public override Expression CreateExpressionTree (EmitContext ec)
123 return expr.CreateExpressionTree (ec);
126 public override Expression DoResolve (EmitContext ec)
131 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
133 return DoResolve (ec);
136 public override void Emit (EmitContext ec)
139 AddressOf (ec, AddressOp.LoadStore);
140 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
143 public void EmitCheck (EmitContext ec)
146 AddressOf (ec, AddressOp.LoadStore);
147 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
150 public void EmitGetValueOrDefault (EmitContext ec)
153 AddressOf (ec, AddressOp.LoadStore);
154 ec.ig.EmitCall (OpCodes.Call, info.GetValueOrDefault, null);
157 public override bool Equals (object obj)
159 Unwrap uw = obj as Unwrap;
160 return uw != null && expr.Equals (uw.expr);
163 public Expression Original {
169 public override int GetHashCode ()
171 return expr.GetHashCode ();
174 public override bool IsNull {
180 void Store (EmitContext ec)
182 if (expr is VariableReference)
189 LocalVariable.Store (ec);
192 public void Load (EmitContext ec)
194 if (expr is VariableReference)
197 LocalVariable.Emit (ec);
200 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
202 type = storey.MutateType (type);
203 info.Constructor = storey.MutateConstructor (info.Constructor);
204 info.HasValue = storey.MutateGenericMethod (info.HasValue);
205 info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
206 info.Value = storey.MutateGenericMethod (info.Value);
209 public void AddressOf (EmitContext ec, AddressOp mode)
211 IMemoryLocation ml = expr as VariableReference;
213 ml.AddressOf (ec, mode);
215 LocalVariable.AddressOf (ec, mode);
219 // Keeps result of non-variable expression
221 LocalTemporary LocalVariable {
224 temp = new LocalTemporary (info.Type);
229 public void Emit (EmitContext ec, bool leave_copy)
237 public void EmitAssign (EmitContext ec, Expression source,
238 bool leave_copy, bool prepare_for_load)
240 InternalWrap wrap = new InternalWrap (source, info, loc);
241 ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
244 protected class InternalWrap : Expression
246 public Expression expr;
247 public NullableInfo info;
249 public InternalWrap (Expression expr, NullableInfo info, Location loc)
256 eclass = ExprClass.Value;
259 public override Expression CreateExpressionTree (EmitContext ec)
261 throw new NotSupportedException ("ET");
264 public override Expression DoResolve (EmitContext ec)
269 public override void Emit (EmitContext ec)
272 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
277 public class Wrap : TypeCast
279 readonly NullableInfo info;
281 protected Wrap (Expression expr, Type type)
284 info = new NullableInfo (type);
285 eclass = ExprClass.Value;
288 public Expression Child {
289 get { return child; }
292 public override Expression CreateExpressionTree (EmitContext ec)
294 TypeCast child_cast = child as TypeCast;
295 if (child_cast != null) {
297 return child_cast.CreateExpressionTree (ec);
300 return base.CreateExpressionTree (ec);
303 public static Expression Create (Expression expr, Type type)
306 // Avoid unwraping and wraping of the same type
308 Unwrap unwrap = expr as Unwrap;
309 if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.GetTypeArguments (type) [0]))
310 return unwrap.Original;
312 return new Wrap (expr, type);
315 public override void Emit (EmitContext ec)
318 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
323 // Represents null literal lifted to nullable type
325 public class LiftedNull : EmptyConstantCast, IMemoryLocation
327 private LiftedNull (Type nullable_type, Location loc)
328 : base (new NullLiteral (loc), nullable_type)
330 eclass = ExprClass.Value;
333 public static Constant Create (Type nullable, Location loc)
335 return new LiftedNull (nullable, loc);
338 public static Expression CreateFromExpression (Expression e)
340 Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
341 TypeManager.CSharpName (e.Type));
343 return ReducedExpression.Create (Create (e.Type, e.Location), e);
346 public override Expression CreateExpressionTree (EmitContext ec)
348 ArrayList args = new ArrayList (2);
349 args.Add (new Argument (this));
350 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
352 return CreateExpressionFactoryCall ("Constant", args);
355 public override void Emit (EmitContext ec)
357 // TODO: generate less temporary variables
358 LocalTemporary value_target = new LocalTemporary (type);
360 value_target.AddressOf (ec, AddressOp.Store);
361 ec.ig.Emit (OpCodes.Initobj, type);
362 value_target.Emit (ec);
365 public void AddressOf (EmitContext ec, AddressOp Mode)
367 LocalTemporary value_target = new LocalTemporary (type);
369 value_target.AddressOf (ec, AddressOp.Store);
370 ec.ig.Emit (OpCodes.Initobj, type);
371 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
375 public class Lifted : Expression, IMemoryLocation
377 Expression expr, wrap, null_value;
380 public Lifted (Expression expr, Unwrap unwrap, Type type)
383 this.unwrap = unwrap;
384 this.loc = expr.Location;
388 public Lifted (Expression expr, Expression unwrap, Type type)
389 : this (expr, unwrap as Unwrap, type)
393 public override Expression CreateExpressionTree (EmitContext ec)
395 return wrap.CreateExpressionTree (ec);
398 public override Expression DoResolve (EmitContext ec)
400 wrap = Wrap.Create (expr, type);
405 // It's null when lifted conversion is transparent
410 null_value = LiftedNull.Create (type, loc);
412 eclass = ExprClass.Value;
416 public override void Emit (EmitContext ec)
418 ILGenerator ig = ec.ig;
419 Label is_null_label = ig.DefineLabel ();
420 Label end_label = ig.DefineLabel ();
422 unwrap.EmitCheck (ec);
423 ig.Emit (OpCodes.Brfalse, is_null_label);
426 ig.Emit (OpCodes.Br, end_label);
428 ig.MarkLabel (is_null_label);
429 null_value.Emit (ec);
431 ig.MarkLabel (end_label);
434 public void AddressOf (EmitContext ec, AddressOp mode)
436 unwrap.AddressOf (ec, mode);
440 public class LiftedUnaryOperator : Unary, IMemoryLocation
443 Expression user_operator;
445 public LiftedUnaryOperator (Unary.Operator op, Expression expr)
450 public void AddressOf (EmitContext ec, AddressOp mode)
452 unwrap.AddressOf (ec, mode);
455 public override Expression CreateExpressionTree (EmitContext ec)
457 if (user_operator != null)
458 return user_operator.CreateExpressionTree (ec);
460 if (Oper == Operator.UnaryPlus)
461 return Expr.CreateExpressionTree (ec);
463 return base.CreateExpressionTree (ec);
466 public override Expression DoResolve (EmitContext ec)
468 if (eclass != ExprClass.Invalid)
471 unwrap = Unwrap.Create (Expr, ec);
475 Expression res = base.ResolveOperator (ec, unwrap);
477 if (user_operator == null)
480 res = Expr = LiftExpression (ec, Expr);
486 eclass = ExprClass.Value;
491 public override void Emit (EmitContext ec)
493 ILGenerator ig = ec.ig;
494 Label is_null_label = ig.DefineLabel ();
495 Label end_label = ig.DefineLabel ();
497 unwrap.EmitCheck (ec);
498 ig.Emit (OpCodes.Brfalse, is_null_label);
500 NullableInfo ni = new NullableInfo (type);
502 if (user_operator != null) {
503 user_operator.Emit (ec);
505 EmitOperator (ec, ni.UnderlyingType);
508 ig.Emit (OpCodes.Newobj, ni.Constructor);
509 ig.Emit (OpCodes.Br_S, end_label);
511 ig.MarkLabel (is_null_label);
512 LiftedNull.Create (type, loc).Emit (ec);
514 ig.MarkLabel (end_label);
517 Expression LiftExpression (EmitContext ec, Expression expr)
519 TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
520 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
521 if (lifted_type == null)
524 expr.Type = lifted_type.Type;
528 protected override Expression ResolveEnumOperator (EmitContext ec, Expression expr)
530 expr = base.ResolveEnumOperator (ec, expr);
534 Expr = LiftExpression (ec, Expr);
535 return LiftExpression (ec, expr);
538 protected override Expression ResolveUserOperator (EmitContext ec, Expression expr)
540 expr = base.ResolveUserOperator (ec, expr);
545 // When a user operator is of non-nullable type
547 if (Expr is Unwrap) {
548 user_operator = LiftExpression (ec, expr);
549 return user_operator;
556 public class LiftedBinaryOperator : Binary
558 Unwrap left_unwrap, right_unwrap;
559 bool left_null_lifted, right_null_lifted;
560 Expression left_orig, right_orig;
561 Expression user_operator;
562 ConstructorInfo wrap_ctor;
564 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
566 : base (op, left, right)
571 public override Expression CreateExpressionTree (EmitContext ec)
573 if (user_operator != null)
574 return user_operator.CreateExpressionTree (ec);
576 return base.CreateExpressionTree (ec);
580 // CSC 2 has this behavior, it allows structs to be compared
581 // with the null literal *outside* of a generics context and
582 // inlines that as true or false.
584 Expression CreateNullConstant (Expression expr)
586 // FIXME: Handle side effect constants
587 Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
589 if ((Oper & Operator.EqualityMask) != 0) {
590 Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
591 expr.GetSignatureForError (), c.AsString ());
593 Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
594 expr.GetSignatureForError (), c.AsString ());
597 return ReducedExpression.Create (c, this);
600 public override Expression DoResolve (EmitContext ec)
602 if (eclass != ExprClass.Invalid)
605 if ((Oper & Operator.LogicalMask) != 0) {
606 Error_OperatorCannotBeApplied (left, right);
611 if (TypeManager.IsNullableType (left.Type)) {
612 left = left_unwrap = Unwrap.Create (left, ec);
618 if (TypeManager.IsNullableType (right.Type)) {
619 right = right_unwrap = Unwrap.Create (right, ec);
625 // Some details are in 6.4.2, 7.2.7
626 // Arguments can be lifted for equal operators when the return type is bool and both
627 // arguments are of same type
629 if (left is NullLiteral) {
631 left_null_lifted = true;
632 type = TypeManager.bool_type;
635 if (right is NullLiteral) {
637 right_null_lifted = true;
638 type = TypeManager.bool_type;
641 eclass = ExprClass.Value;
642 return DoResolveCore (ec, left_orig, right_orig);
645 void EmitBitwiseBoolean (EmitContext ec)
647 ILGenerator ig = ec.ig;
649 Label load_left = ig.DefineLabel ();
650 Label load_right = ig.DefineLabel ();
651 Label end_label = ig.DefineLabel ();
653 left_unwrap.EmitGetValueOrDefault (ec);
654 ig.Emit (OpCodes.Brtrue_S, load_right);
656 right_unwrap.EmitGetValueOrDefault (ec);
657 ig.Emit (OpCodes.Brtrue_S, load_left);
659 left_unwrap.EmitCheck (ec);
660 ig.Emit (OpCodes.Brfalse_S, load_right);
663 ig.MarkLabel (load_left);
665 if (Oper == Operator.BitwiseAnd) {
666 left_unwrap.Load (ec);
668 right_unwrap.Load (ec);
669 right_unwrap = left_unwrap;
671 ig.Emit (OpCodes.Br_S, end_label);
674 ig.MarkLabel (load_right);
675 right_unwrap.Load (ec);
677 ig.MarkLabel (end_label);
681 // Emits optimized equality or inequality operator when possible
683 bool EmitEquality (EmitContext ec)
685 ILGenerator ig = ec.ig;
688 // Either left or right is null
690 if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
691 left_unwrap.EmitCheck (ec);
692 if (Oper == Binary.Operator.Equality) {
693 ig.Emit (OpCodes.Ldc_I4_0);
694 ig.Emit (OpCodes.Ceq);
699 if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
700 right_unwrap.EmitCheck (ec);
701 if (Oper == Binary.Operator.Equality) {
702 ig.Emit (OpCodes.Ldc_I4_0);
703 ig.Emit (OpCodes.Ceq);
708 if (user_operator != null)
711 if (left is UserCast || right is UserCast)
714 Label dissimilar_label = ig.DefineLabel ();
715 Label end_label = ig.DefineLabel ();
717 if (left_unwrap != null)
718 left_unwrap.EmitGetValueOrDefault (ec);
722 if (right_unwrap != null)
723 right_unwrap.EmitGetValueOrDefault (ec);
727 ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
729 if (left_unwrap != null)
730 left_unwrap.EmitCheck (ec);
731 if (right_unwrap != null)
732 right_unwrap.EmitCheck (ec);
734 if (left_unwrap != null && right_unwrap != null) {
735 if (Oper == Operator.Inequality)
736 ig.Emit (OpCodes.Xor);
738 ig.Emit (OpCodes.Ceq);
740 if (Oper == Operator.Inequality) {
741 ig.Emit (OpCodes.Ldc_I4_0);
742 ig.Emit (OpCodes.Ceq);
746 ig.Emit (OpCodes.Br_S, end_label);
748 ig.MarkLabel (dissimilar_label);
749 if (Oper == Operator.Inequality)
750 ig.Emit (OpCodes.Ldc_I4_1);
752 ig.Emit (OpCodes.Ldc_I4_0);
754 ig.MarkLabel (end_label);
758 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
761 ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
764 public override void Emit (EmitContext ec)
767 // Optimize same expression operation
769 if (right_unwrap != null && right.Equals (left))
770 right_unwrap = left_unwrap;
772 if (user_operator == null && IsBitwiseBoolean) {
773 EmitBitwiseBoolean (ec);
777 if ((Oper & Operator.EqualityMask) != 0) {
778 if (EmitEquality (ec))
782 ILGenerator ig = ec.ig;
784 Label is_null_label = ig.DefineLabel ();
785 Label end_label = ig.DefineLabel ();
787 if (left_unwrap != null) {
788 left_unwrap.EmitCheck (ec);
789 ig.Emit (OpCodes.Brfalse, is_null_label);
793 // Don't emit HasValue check when left and right expressions are same
795 if (right_unwrap != null && !left.Equals (right)) {
796 right_unwrap.EmitCheck (ec);
797 ig.Emit (OpCodes.Brfalse, is_null_label);
800 EmitOperator (ec, left.Type);
802 if (wrap_ctor != null)
803 ig.Emit (OpCodes.Newobj, wrap_ctor);
805 ig.Emit (OpCodes.Br_S, end_label);
806 ig.MarkLabel (is_null_label);
808 if ((Oper & Operator.ComparisonMask) != 0) {
810 // Emit true when equality operator both operands are null
811 // or inequality operator operands has only one null
813 if ((Oper == Operator.Equality && left_unwrap != null && right_unwrap != null) ||
814 (Oper == Operator.Inequality && left_unwrap != right_unwrap))
815 ig.Emit (OpCodes.Ldc_I4_1);
817 ig.Emit (OpCodes.Ldc_I4_0);
819 LiftedNull.Create (type, loc).Emit (ec);
822 ig.MarkLabel (end_label);
825 protected override void EmitOperator (EmitContext ec, Type l)
827 if (user_operator != null) {
828 user_operator.Emit (ec);
832 if (TypeManager.IsNullableType (l))
833 l = TypeManager.GetTypeArguments (l) [0];
835 base.EmitOperator (ec, l);
838 bool IsBitwiseBoolean {
840 return (Oper & Operator.BitwiseMask) != 0 && left_unwrap != null && right_unwrap != null &&
841 left_unwrap.Type == TypeManager.bool_type && right_unwrap.Type == TypeManager.bool_type;
845 Expression LiftResult (EmitContext ec, Expression res_expr)
847 TypeExpr lifted_type;
850 // Avoid double conversion
852 if (left_unwrap == null || left_null_lifted || !TypeManager.IsEqual (left_unwrap.Type, left.Type) || (left_unwrap != null && right_null_lifted)) {
853 lifted_type = new NullableType (left.Type, loc);
854 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
855 if (lifted_type == null)
858 if (left is UserCast || left is TypeCast)
859 left.Type = lifted_type.Type;
861 left = EmptyCast.Create (left, lifted_type.Type);
864 if (right_unwrap == null || right_null_lifted || !TypeManager.IsEqual (right_unwrap.Type, right.Type) || (right_unwrap != null && left_null_lifted)) {
865 lifted_type = new NullableType (right.Type, loc);
866 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
867 if (lifted_type == null)
870 if (right is UserCast || right is TypeCast)
871 right.Type = lifted_type.Type;
873 right = EmptyCast.Create (right, lifted_type.Type);
876 if ((Oper & Operator.ComparisonMask) == 0) {
877 lifted_type = new NullableType (res_expr.Type, loc);
878 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
879 if (lifted_type == null)
882 wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
883 type = res_expr.Type = lifted_type.Type;
886 if (left_null_lifted) {
887 left = LiftedNull.Create (right.Type, left.Location);
889 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
890 return LiftedNull.CreateFromExpression (res_expr);
893 // Value types and null comparison
895 if (right_unwrap == null || (Oper & Operator.RelationalMask) != 0)
896 return CreateNullConstant (right_orig).Resolve (ec);
899 if (right_null_lifted) {
900 right = LiftedNull.Create (left.Type, right.Location);
902 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
903 return LiftedNull.CreateFromExpression (res_expr);
906 // Value types and null comparison
908 if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
909 return CreateNullConstant (left_orig).Resolve (ec);
915 protected override Expression ResolveOperatorPredefined (EmitContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
917 Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
919 if (e == this || enum_type != null)
920 return LiftResult (ec, e);
923 // 7.9.9 Equality operators and null
925 // The == and != operators permit one operand to be a value of a nullable type and
926 // the other to be the null literal, even if no predefined or user-defined operator
927 // (in unlifted or lifted form) exists for the operation.
929 if (e == null && (Oper & Operator.EqualityMask) != 0) {
930 if ((left_null_lifted && right_unwrap != null) || (right_null_lifted && left_unwrap != null))
931 return LiftResult (ec, this);
937 protected override Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
939 Expression expr = base.ResolveUserOperator (ec, l, r);
943 expr = LiftResult (ec, expr);
944 if (expr is Constant)
948 user_operator = expr;
953 public class NullCoalescingOperator : Expression
955 Expression left, right;
958 public NullCoalescingOperator (Expression left, Expression right, Location loc)
965 public override Expression CreateExpressionTree (EmitContext ec)
967 if (left is NullLiteral)
968 Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
970 UserCast uc = left as UserCast;
971 Expression conversion = null;
975 ArrayList c_args = new ArrayList (2);
976 c_args.Add (new Argument (uc.CreateExpressionTree (ec)));
977 c_args.Add (new Argument (left.CreateExpressionTree (ec)));
978 conversion = CreateExpressionFactoryCall ("Lambda", c_args);
981 ArrayList args = new ArrayList (3);
982 args.Add (new Argument (left.CreateExpressionTree (ec)));
983 args.Add (new Argument (right.CreateExpressionTree (ec)));
984 if (conversion != null)
985 args.Add (new Argument (conversion));
987 return CreateExpressionFactoryCall ("Coalesce", args);
990 Expression ConvertExpression (EmitContext ec)
992 // TODO: ImplicitConversionExists should take care of this
993 if (left.eclass == ExprClass.MethodGroup)
996 Type ltype = left.Type;
999 // If left is a nullable type and an implicit conversion exists from right to underlying type of left,
1000 // the result is underlying type of left
1002 if (TypeManager.IsNullableType (ltype)) {
1003 unwrap = Unwrap.Create (left, ec);
1007 if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
1010 right = Convert.ImplicitConversion (ec, right, type, loc);
1013 } else if (TypeManager.IsReferenceType (ltype)) {
1014 if (Convert.ImplicitConversionExists (ec, right, ltype)) {
1016 // Reduce (constant ?? expr) to constant
1018 Constant lc = left as Constant;
1019 if (lc != null && !lc.IsDefaultValue)
1020 return new SideEffectConstant (lc, right, loc).Resolve (ec);
1023 // Reduce (left ?? null) to left OR (null-constant ?? right) to right
1025 if (right.IsNull || lc != null)
1026 return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
1028 right = Convert.ImplicitConversion (ec, right, ltype, loc);
1036 Type rtype = right.Type;
1037 if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
1041 // Reduce (null ?? right) to right
1044 return ReducedExpression.Create (right, this).Resolve (ec);
1046 left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
1051 public override Expression DoResolve (EmitContext ec)
1053 if (eclass != ExprClass.Invalid)
1056 left = left.Resolve (ec);
1057 right = right.Resolve (ec);
1059 if (left == null || right == null)
1062 eclass = ExprClass.Value;
1064 Expression e = ConvertExpression (ec);
1066 Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
1073 public override void Emit (EmitContext ec)
1075 ILGenerator ig = ec.ig;
1077 Label end_label = ig.DefineLabel ();
1079 if (unwrap != null) {
1080 Label is_null_label = ig.DefineLabel ();
1082 unwrap.EmitCheck (ec);
1083 ig.Emit (OpCodes.Brfalse, is_null_label);
1086 ig.Emit (OpCodes.Br, end_label);
1088 ig.MarkLabel (is_null_label);
1091 ig.MarkLabel (end_label);
1097 ig.Emit (OpCodes.Dup);
1098 ig.Emit (OpCodes.Brtrue, end_label);
1100 ig.Emit (OpCodes.Pop);
1103 ig.MarkLabel (end_label);
1106 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1108 left.MutateHoistedGenericType (storey);
1109 right.MutateHoistedGenericType (storey);
1110 type = storey.MutateType (type);
1113 protected override void CloneTo (CloneContext clonectx, Expression t)
1115 NullCoalescingOperator target = (NullCoalescingOperator) t;
1117 target.left = left.Clone (clonectx);
1118 target.right = right.Clone (clonectx);
1122 public class LiftedUnaryMutator : ExpressionStatement
1124 public readonly UnaryMutator.Mode Mode;
1126 UnaryMutator underlying;
1129 public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
1135 eclass = ExprClass.Value;
1138 public override Expression CreateExpressionTree (EmitContext ec)
1140 return new SimpleAssign (this, this).CreateExpressionTree (ec);
1143 public override Expression DoResolve (EmitContext ec)
1145 expr = expr.Resolve (ec);
1149 unwrap = Unwrap.Create (expr, ec);
1153 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap).Resolve (ec);
1154 if (underlying == null)
1161 void DoEmit (EmitContext ec, bool is_expr)
1163 ILGenerator ig = ec.ig;
1164 Label is_null_label = ig.DefineLabel ();
1165 Label end_label = ig.DefineLabel ();
1167 unwrap.EmitCheck (ec);
1168 ig.Emit (OpCodes.Brfalse, is_null_label);
1171 underlying.Emit (ec);
1172 ig.Emit (OpCodes.Br_S, end_label);
1174 underlying.EmitStatement (ec);
1177 ig.MarkLabel (is_null_label);
1179 LiftedNull.Create (type, loc).Emit (ec);
1181 ig.MarkLabel (end_label);
1184 public override void Emit (EmitContext ec)
1189 public override void EmitStatement (EmitContext ec)