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
23 Expression underlying;
25 public NullableType (Expression 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 TypeArguments args = new TypeArguments (loc);
40 args.Add (underlying);
42 if (TypeManager.generic_nullable_type == null) {
43 TypeManager.generic_nullable_type = TypeManager.CoreLookupType (
44 "System", "Nullable`1", Kind.Struct, true);
47 ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
48 return ctype.ResolveAsTypeTerminal (ec, false);
52 public sealed class NullableInfo
54 public readonly Type Type;
55 public readonly Type UnderlyingType;
56 public readonly MethodInfo HasValue;
57 public readonly MethodInfo Value;
58 public readonly MethodInfo GetValueOrDefault;
59 public readonly ConstructorInfo Constructor;
61 public NullableInfo (Type type)
64 UnderlyingType = TypeManager.GetTypeArguments (type) [0];
66 PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null);
67 PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null);
68 GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
70 HasValue = has_value_pi.GetGetMethod (false);
71 Value = value_pi.GetGetMethod (false);
73 if (UnderlyingType.Module == CodeGen.Module.Builder) {
74 Type o_type = TypeManager.DropGenericTypeArguments (type);
75 Constructor = TypeBuilder.GetConstructor (type,
76 TypeManager.GetPredefinedConstructor (o_type, Location.Null, o_type.GetGenericArguments ()));
80 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
84 public class HasValue : Expression
89 private HasValue (Expression expr)
94 public static Expression Create (Expression expr, EmitContext ec)
96 return new HasValue (expr).Resolve (ec);
99 public override void Emit (EmitContext ec)
101 IMemoryLocation memory_loc = expr as IMemoryLocation;
102 if (memory_loc == null) {
103 LocalTemporary temp = new LocalTemporary (expr.Type);
108 memory_loc.AddressOf (ec, AddressOp.LoadStore);
109 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
112 public override Expression DoResolve (EmitContext ec)
114 this.info = new NullableInfo (expr.Type);
116 type = TypeManager.bool_type;
117 eclass = expr.eclass;
122 public class Unwrap : Expression, IMemoryLocation, IAssignMethod
130 protected Unwrap (Expression expr)
133 this.loc = expr.Location;
136 public static Unwrap Create (Expression expr, EmitContext ec)
138 return new Unwrap (expr).Resolve (ec) as Unwrap;
141 public override Expression CreateExpressionTree (EmitContext ec)
143 return expr.CreateExpressionTree (ec);
146 public override Expression DoResolve (EmitContext ec)
151 temp = new LocalTemporary (expr.Type);
153 info = new NullableInfo (expr.Type);
154 type = info.UnderlyingType;
155 eclass = expr.eclass;
159 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
161 return DoResolve (ec);
164 public override void Emit (EmitContext ec)
166 AddressOf (ec, AddressOp.LoadStore);
167 ec.ig.EmitCall (OpCodes.Call, info.Value, null);
170 public void EmitCheck (EmitContext ec)
172 AddressOf (ec, AddressOp.LoadStore);
173 ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
176 public void EmitGetValueOrDefault (EmitContext ec)
178 AddressOf (ec, AddressOp.LoadStore);
179 ec.ig.EmitCall (OpCodes.Call, info.GetValueOrDefault, null);
182 public override bool Equals (object obj)
184 Unwrap uw = obj as Unwrap;
185 return uw != null && expr.Equals (uw.expr);
188 public Expression Original {
194 public override int GetHashCode ()
196 return expr.GetHashCode ();
199 public override bool IsNull {
205 public void Store (EmitContext ec)
210 void create_temp (EmitContext ec)
212 if ((temp != null) && !has_temp) {
219 public void LoadTemporary (EmitContext ec)
224 public void AddressOf (EmitContext ec, AddressOp mode)
228 temp.AddressOf (ec, AddressOp.LoadStore);
230 ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
233 public void Emit (EmitContext ec, bool leave_copy)
246 public void EmitAssign (EmitContext ec, Expression source,
247 bool leave_copy, bool prepare_for_load)
249 InternalWrap wrap = new InternalWrap (source, info, loc);
250 ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
253 protected class InternalWrap : Expression
255 public Expression expr;
256 public NullableInfo info;
258 public InternalWrap (Expression expr, NullableInfo info, Location loc)
265 eclass = ExprClass.Value;
268 public override Expression DoResolve (EmitContext ec)
273 public override void Emit (EmitContext ec)
276 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
281 public class Wrap : TypeCast
283 readonly NullableInfo info;
285 protected Wrap (Expression expr, Type type)
288 info = new NullableInfo (type);
289 eclass = ExprClass.Value;
292 public static Expression Create (Expression expr, Type type)
294 return new Wrap (expr, type);
297 public override void Emit (EmitContext ec)
300 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
305 // Represents null literal lifted to nullable type
307 public class LiftedNull : EmptyConstantCast, IMemoryLocation
309 private LiftedNull (Type nullable_type, Location loc)
310 : base (new NullLiteral (loc), nullable_type)
312 eclass = ExprClass.Value;
315 public static Constant Create (Type nullable, Location loc)
317 return new LiftedNull (nullable, loc);
320 public static Expression CreateFromExpression (Expression e)
322 Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
323 TypeManager.CSharpName (e.Type));
325 return ReducedExpression.Create (Create (e.Type, e.Location), e);
328 public override Expression CreateExpressionTree (EmitContext ec)
330 ArrayList args = new ArrayList (2);
331 args.Add (new Argument (this));
332 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
334 return CreateExpressionFactoryCall ("Constant", args);
337 public override void Emit (EmitContext ec)
339 // TODO: generate less temporary variables
340 LocalTemporary value_target = new LocalTemporary (type);
342 value_target.AddressOf (ec, AddressOp.Store);
343 ec.ig.Emit (OpCodes.Initobj, type);
344 value_target.Emit (ec);
347 public void AddressOf (EmitContext ec, AddressOp Mode)
349 LocalTemporary value_target = new LocalTemporary (type);
351 value_target.AddressOf (ec, AddressOp.Store);
352 ec.ig.Emit (OpCodes.Initobj, type);
353 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
357 public abstract class Lifted : Expression, IMemoryLocation
359 Expression expr, underlying, wrap, null_value;
362 protected Lifted (Expression expr, Location loc)
368 public override Expression CreateExpressionTree (EmitContext ec)
370 ArrayList args = new ArrayList (2);
371 args.Add (new Argument (expr.CreateExpressionTree (ec)));
372 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
373 return CreateExpressionFactoryCall ("Convert", args);
376 public override Expression DoResolve (EmitContext ec)
378 expr = expr.Resolve (ec);
382 unwrap = Unwrap.Create (expr, ec);
386 underlying = ResolveUnderlying (unwrap, ec);
387 if (underlying == null)
390 TypeExpr target_type = new NullableType (underlying.Type, loc);
391 target_type = target_type.ResolveAsTypeTerminal (ec, false);
392 if (target_type == null)
395 wrap = Wrap.Create (underlying, target_type.Type);
399 null_value = LiftedNull.Create (wrap.Type, loc);
402 eclass = ExprClass.Value;
406 protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
408 public override void Emit (EmitContext ec)
410 ILGenerator ig = ec.ig;
411 Label is_null_label = ig.DefineLabel ();
412 Label end_label = ig.DefineLabel ();
414 unwrap.EmitCheck (ec);
415 ig.Emit (OpCodes.Brfalse, is_null_label);
418 ig.Emit (OpCodes.Br, end_label);
420 ig.MarkLabel (is_null_label);
421 null_value.Emit (ec);
423 ig.MarkLabel (end_label);
426 public void AddressOf (EmitContext ec, AddressOp mode)
428 unwrap.AddressOf (ec, mode);
432 public class LiftedConversion : Lifted
434 public readonly bool IsUser;
435 public readonly bool IsExplicit;
436 public readonly Type TargetType;
438 public LiftedConversion (Expression expr, Type target_type, bool is_user,
439 bool is_explicit, Location loc)
442 this.IsUser = is_user;
443 this.IsExplicit = is_explicit;
444 this.TargetType = target_type;
447 protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
449 Type type = TypeManager.GetTypeArguments (TargetType) [0];
453 return Convert.ExplicitUserConversion (ec, unwrap, type, loc);
455 return Convert.ImplicitUserConversion (ec, unwrap, type, loc);
458 return Convert.ExplicitConversion (ec, unwrap, type, loc);
460 return Convert.ImplicitConversion (ec, unwrap, type, loc);
465 public class LiftedUnaryOperator : Unary, IMemoryLocation
468 Expression user_operator;
470 public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
471 : base (op, expr, loc)
475 public void AddressOf (EmitContext ec, AddressOp mode)
477 unwrap.AddressOf (ec, mode);
480 public override Expression CreateExpressionTree (EmitContext ec)
482 if (user_operator != null)
483 return user_operator.CreateExpressionTree (ec);
485 if (Oper == Operator.UnaryPlus)
486 return Expr.CreateExpressionTree (ec);
488 return base.CreateExpressionTree (ec);
491 public override Expression DoResolve (EmitContext ec)
493 if (eclass != ExprClass.Invalid)
496 unwrap = Unwrap.Create (Expr, ec);
500 Expression res = base.ResolveOperator (ec, unwrap);
502 res = Expr = LiftExpression (ec, Expr);
507 eclass = ExprClass.Value;
512 public override void Emit (EmitContext ec)
514 ILGenerator ig = ec.ig;
515 Label is_null_label = ig.DefineLabel ();
516 Label end_label = ig.DefineLabel ();
518 unwrap.EmitCheck (ec);
519 ig.Emit (OpCodes.Brfalse, is_null_label);
521 NullableInfo ni = new NullableInfo (type);
523 if (user_operator != null) {
524 user_operator.Emit (ec);
526 EmitOperator (ec, ni.UnderlyingType);
529 ig.Emit (OpCodes.Newobj, ni.Constructor);
530 ig.Emit (OpCodes.Br_S, end_label);
532 ig.MarkLabel (is_null_label);
533 LiftedNull.Create (type, loc).Emit (ec);
535 ig.MarkLabel (end_label);
538 Expression LiftExpression (EmitContext ec, Expression expr)
540 TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
541 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
542 if (lifted_type == null)
545 expr.Type = lifted_type.Type;
549 protected override Expression ResolveUserOperator (EmitContext ec, Expression expr)
551 expr = base.ResolveUserOperator (ec, expr);
555 user_operator = LiftExpression (ec, expr);
556 return user_operator;
560 public class LiftedBinaryOperator : Binary
562 Unwrap left_unwrap, right_unwrap;
563 bool left_null_lifted, right_null_lifted;
564 Expression left_orig, right_orig;
565 Expression user_operator;
566 ConstructorInfo wrap_ctor;
568 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
570 : base (op, left, right)
575 public override Expression CreateExpressionTree (EmitContext ec)
577 if (user_operator != null)
578 return user_operator.CreateExpressionTree (ec);
580 return base.CreateExpressionTree (ec);
584 // CSC 2 has this behavior, it allows structs to be compared
585 // with the null literal *outside* of a generics context and
586 // inlines that as true or false.
588 Expression CreateNullConstant (Expression expr)
590 // FIXME: Handle side effect constants
591 Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
593 if ((Oper & Operator.EqualityMask) != 0) {
594 Report.Warning (472, 2, loc, "The result of comparing `{0}' against null is always `{1}'. " +
595 "This operation is undocumented and it is temporary supported for compatibility reasons only",
596 expr.GetSignatureForError (), c.AsString ());
598 Report.Warning (464, 2, loc, "The result of comparing type `{0}' against null is always `{1}'",
599 expr.GetSignatureForError (), c.AsString ());
602 return ReducedExpression.Create (c, this);
605 public override Expression DoResolve (EmitContext ec)
607 if (eclass != ExprClass.Invalid)
610 if ((Oper & Operator.LogicalMask) != 0) {
611 Error_OperatorCannotBeApplied (left, right);
616 if (TypeManager.IsNullableType (left.Type)) {
617 left = left_unwrap = Unwrap.Create (left, ec);
623 if (TypeManager.IsNullableType (right.Type)) {
624 right = right_unwrap = Unwrap.Create (right, ec);
630 // Some details are in 6.4.2, 7.2.7
631 // Arguments can be lifted for equal operators when the return type is bool and both
632 // arguments are of same type
634 if (left is NullLiteral) {
636 left_null_lifted = true;
637 type = TypeManager.bool_type;
640 if (right is NullLiteral) {
642 right_null_lifted = true;
643 type = TypeManager.bool_type;
646 eclass = ExprClass.Value;
647 return DoResolveCore (ec, left_orig, right_orig);
650 void EmitBitwiseBoolean (EmitContext ec)
652 ILGenerator ig = ec.ig;
654 Label load_left = ig.DefineLabel ();
655 Label load_right = ig.DefineLabel ();
656 Label end_label = ig.DefineLabel ();
658 left_unwrap.EmitGetValueOrDefault (ec);
659 ig.Emit (OpCodes.Brtrue_S, load_right);
661 right_unwrap.EmitGetValueOrDefault (ec);
662 ig.Emit (OpCodes.Brtrue_S, load_left);
664 left_unwrap.EmitCheck (ec);
665 ig.Emit (OpCodes.Brfalse_S, load_right);
668 ig.MarkLabel (load_left);
670 if (Oper == Operator.BitwiseAnd) {
671 left_unwrap.LoadTemporary (ec);
673 right_unwrap.LoadTemporary (ec);
674 right_unwrap = left_unwrap;
676 ig.Emit (OpCodes.Br_S, end_label);
679 ig.MarkLabel (load_right);
680 right_unwrap.LoadTemporary (ec);
682 ig.MarkLabel (end_label);
686 // Emits optimized equality or inequality operator when possible
688 bool EmitEquality (EmitContext ec)
690 ILGenerator ig = ec.ig;
693 // Either left or right is null
695 if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
696 left_unwrap.EmitCheck (ec);
697 if (Oper == Binary.Operator.Equality) {
698 ig.Emit (OpCodes.Ldc_I4_0);
699 ig.Emit (OpCodes.Ceq);
704 if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
705 right_unwrap.EmitCheck (ec);
706 if (Oper == Binary.Operator.Equality) {
707 ig.Emit (OpCodes.Ldc_I4_0);
708 ig.Emit (OpCodes.Ceq);
713 if (user_operator != null)
716 Label dissimilar_label = ig.DefineLabel ();
717 Label end_label = ig.DefineLabel ();
719 if (left_unwrap != null)
720 left_unwrap.EmitGetValueOrDefault (ec);
724 if (right_unwrap != null)
725 right_unwrap.EmitGetValueOrDefault (ec);
729 ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
731 if (left_unwrap != null)
732 left_unwrap.EmitCheck (ec);
733 if (right_unwrap != null)
734 right_unwrap.EmitCheck (ec);
736 if (left_unwrap != null && right_unwrap != null) {
737 if (Oper == Operator.Inequality)
738 ig.Emit (OpCodes.Xor);
740 ig.Emit (OpCodes.Ceq);
742 if (Oper == Operator.Inequality) {
743 ig.Emit (OpCodes.Ldc_I4_0);
744 ig.Emit (OpCodes.Ceq);
748 ig.Emit (OpCodes.Br_S, end_label);
750 ig.MarkLabel (dissimilar_label);
751 if (Oper == Operator.Inequality)
752 ig.Emit (OpCodes.Ldc_I4_1);
754 ig.Emit (OpCodes.Ldc_I4_0);
756 ig.MarkLabel (end_label);
760 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
763 ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
766 public override void Emit (EmitContext ec)
768 if (left_unwrap != null)
769 left_unwrap.Store (ec);
771 if (right_unwrap != null) {
772 if (right.Equals (left))
773 right_unwrap = left_unwrap;
775 right_unwrap.Store (ec);
778 if (user_operator == null && IsBitwiseBoolean) {
779 EmitBitwiseBoolean (ec);
783 if ((Oper & Operator.EqualityMask) != 0) {
784 if (EmitEquality (ec))
788 ILGenerator ig = ec.ig;
790 Label is_null_label = ig.DefineLabel ();
791 Label end_label = ig.DefineLabel ();
793 if (left_unwrap != null) {
794 left_unwrap.EmitCheck (ec);
795 ig.Emit (OpCodes.Brfalse, is_null_label);
799 // Don't emit HasValue check when left and right expressions are same
801 if (right_unwrap != null && !left.Equals (right)) {
802 right_unwrap.EmitCheck (ec);
803 ig.Emit (OpCodes.Brfalse, is_null_label);
806 EmitOperator (ec, left.Type);
808 if (wrap_ctor != null)
809 ig.Emit (OpCodes.Newobj, wrap_ctor);
811 ig.Emit (OpCodes.Br_S, end_label);
812 ig.MarkLabel (is_null_label);
814 if ((Oper & Operator.ComparisonMask) != 0) {
815 if (Oper == Operator.Equality)
816 ig.Emit (OpCodes.Ldc_I4_1);
818 ig.Emit (OpCodes.Ldc_I4_0);
820 LiftedNull.Create (type, loc).Emit (ec);
823 ig.MarkLabel (end_label);
826 protected override void EmitOperator (EmitContext ec, Type l)
828 if (user_operator != null) {
829 user_operator.Emit (ec);
833 if (TypeManager.IsNullableType (l))
834 l = TypeManager.GetTypeArguments (l) [0];
836 base.EmitOperator (ec, l);
839 bool IsBitwiseBoolean {
841 return (Oper & Operator.BitwiseMask) != 0 && left_unwrap != null && right_unwrap != null &&
842 left_unwrap.Type == TypeManager.bool_type && right_unwrap.Type == TypeManager.bool_type;
846 Expression LiftResult (EmitContext ec, Expression res_expr)
848 TypeExpr lifted_type;
851 // Avoid double conversion
853 if (left_unwrap == null || left_null_lifted || !TypeManager.IsEqual (left_unwrap.Type, left.Type)) {
854 lifted_type = new NullableType (left.Type, loc);
855 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
856 if (lifted_type == null)
859 if (left is UserCast || left is TypeCast)
860 left.Type = lifted_type.Type;
862 left = EmptyCast.Create (left, lifted_type.Type);
865 if (right_unwrap == null || right_null_lifted || !TypeManager.IsEqual (right_unwrap.Type, right.Type)) {
866 lifted_type = new NullableType (right.Type, loc);
867 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
868 if (lifted_type == null)
871 if (right is UserCast || right is TypeCast)
872 right.Type = lifted_type.Type;
874 right = EmptyCast.Create (right, lifted_type.Type);
877 if ((Oper & Operator.ComparisonMask) == 0) {
878 lifted_type = new NullableType (res_expr.Type, loc);
879 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
880 if (lifted_type == null)
883 wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
884 type = res_expr.Type = lifted_type.Type;
887 if (left_null_lifted) {
888 left = LiftedNull.Create (right.Type, left.Location);
890 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
891 return LiftedNull.CreateFromExpression (res_expr);
894 // Value types and null comparison
896 if (right_unwrap == null || (Oper & Operator.RelationalMask) != 0)
897 return CreateNullConstant (right_orig).Resolve (ec);
900 if (right_null_lifted) {
901 right = LiftedNull.Create (left.Type, right.Location);
903 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
904 return LiftedNull.CreateFromExpression (res_expr);
907 // Value types and null comparison
909 if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
910 return CreateNullConstant (left_orig).Resolve (ec);
916 protected override Expression ResolveOperatorPredefined (EmitContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
918 Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
920 if (e == this || enum_type != null)
921 return LiftResult (ec, e);
924 // 7.9.9 Equality operators and null
926 // The == and != operators permit one operand to be a value of a nullable type and
927 // the other to be the null literal, even if no predefined or user-defined operator
928 // (in unlifted or lifted form) exists for the operation.
930 if (e == null && (Oper & Operator.EqualityMask) != 0) {
931 if ((left_null_lifted && right_unwrap != null) || (right_null_lifted && left_unwrap != null))
932 return LiftResult (ec, this);
938 protected override Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
940 Expression expr = base.ResolveUserOperator (ec, l, r);
944 expr = LiftResult (ec, expr);
945 if (expr is Constant)
949 user_operator = expr;
954 public class NullCoalescingOperator : Expression
956 Expression left, right;
959 public NullCoalescingOperator (Expression left, Expression right, Location loc)
966 public override Expression CreateExpressionTree (EmitContext ec)
968 UserCast uc = left as UserCast;
969 Expression conversion = null;
973 ArrayList c_args = new ArrayList (2);
974 c_args.Add (new Argument (uc.CreateExpressionTree (ec)));
975 c_args.Add (new Argument (left.CreateExpressionTree (ec)));
976 conversion = CreateExpressionFactoryCall ("Lambda", c_args);
979 ArrayList args = new ArrayList (3);
980 args.Add (new Argument (left.CreateExpressionTree (ec)));
981 args.Add (new Argument (right.CreateExpressionTree (ec)));
982 if (conversion != null)
983 args.Add (new Argument (conversion));
985 return CreateExpressionFactoryCall ("Coalesce", args);
988 public override Expression DoResolve (EmitContext ec)
993 left = left.Resolve (ec);
994 right = right.Resolve (ec);
996 if (left == null || right == null)
999 eclass = ExprClass.Value;
1000 Type ltype = left.Type, rtype = right.Type;
1003 if (TypeManager.IsNullableType (ltype)) {
1004 NullableInfo info = new NullableInfo (ltype);
1006 unwrap = Unwrap.Create (left, ec);
1010 expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
1017 } else if (!TypeManager.IsReferenceType (ltype)) {
1018 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
1022 expr = Convert.ImplicitConversion (ec, right, ltype, loc);
1029 Expression left_null = unwrap != null ? unwrap : left;
1030 expr = Convert.ImplicitConversion (ec, left_null, rtype, loc);
1037 Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
1041 public override void Emit (EmitContext ec)
1043 ILGenerator ig = ec.ig;
1045 Label is_null_label = ig.DefineLabel ();
1046 Label end_label = ig.DefineLabel ();
1048 if (unwrap != null) {
1049 unwrap.EmitCheck (ec);
1050 ig.Emit (OpCodes.Brfalse, is_null_label);
1053 ig.Emit (OpCodes.Br, end_label);
1055 ig.MarkLabel (is_null_label);
1058 ig.MarkLabel (end_label);
1061 ig.Emit (OpCodes.Dup);
1062 ig.Emit (OpCodes.Brtrue, end_label);
1064 ig.MarkLabel (is_null_label);
1066 ig.Emit (OpCodes.Pop);
1069 ig.MarkLabel (end_label);
1072 protected override void CloneTo (CloneContext clonectx, Expression t)
1074 NullCoalescingOperator target = (NullCoalescingOperator) t;
1076 target.left = left.Clone (clonectx);
1077 target.right = right.Clone (clonectx);
1081 public class LiftedUnaryMutator : ExpressionStatement
1083 public readonly UnaryMutator.Mode Mode;
1084 Expression expr, null_value;
1085 UnaryMutator underlying;
1088 public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
1094 eclass = ExprClass.Value;
1097 public override Expression DoResolve (EmitContext ec)
1099 expr = expr.Resolve (ec);
1103 unwrap = Unwrap.Create (expr, ec);
1107 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
1108 if (underlying == null)
1112 null_value = LiftedNull.Create (type, loc);
1116 void DoEmit (EmitContext ec, bool is_expr)
1118 ILGenerator ig = ec.ig;
1119 Label is_null_label = ig.DefineLabel ();
1120 Label end_label = ig.DefineLabel ();
1122 unwrap.EmitCheck (ec);
1123 ig.Emit (OpCodes.Brfalse, is_null_label);
1126 underlying.Emit (ec);
1128 underlying.EmitStatement (ec);
1129 ig.Emit (OpCodes.Br, end_label);
1131 ig.MarkLabel (is_null_label);
1133 null_value.Emit (ec);
1135 ig.MarkLabel (end_label);
1138 public override void Emit (EmitContext ec)
1143 public override void EmitStatement (EmitContext ec)