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 (IMemberContext ec)
39 if (TypeManager.generic_nullable_type == null) {
40 TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
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 (IMemberContext 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.TypeToCoreType (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 // When compiling corlib
77 if (type.Module == RootContext.ToplevelTypes.Builder) {
78 TypeContainer tc = TypeManager.LookupGenericTypeContainer (type);
80 // TODO: check for correct overload
81 Constructor c = ((Constructor) tc.InstanceConstructors [0]);
84 Constructor = TypeBuilder.GetConstructor (type, c.ConstructorBuilder);
90 if (UnderlyingType.Module == RootContext.ToplevelTypes.Builder) {
91 ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
92 Constructor = TypeBuilder.GetConstructor (type, cinfo);
97 Constructor = type.GetConstructor (new Type[] { UnderlyingType });
101 public class Unwrap : Expression, IMemoryLocation, IAssignMethod
107 readonly bool useDefaultValue;
109 Unwrap (Expression expr, bool useDefaultValue)
112 this.loc = expr.Location;
113 this.useDefaultValue = useDefaultValue;
115 info = new NullableInfo (expr.Type);
116 type = info.UnderlyingType;
117 eclass = expr.eclass;
120 public static Expression Create (Expression expr)
123 // Avoid unwraping and wraping of same type
125 Wrap wrap = expr as Wrap;
129 return Create (expr, false);
132 public static Unwrap Create (Expression expr, bool useDefaultValue)
134 return new Unwrap (expr, useDefaultValue);
137 public override Expression CreateExpressionTree (ResolveContext ec)
139 return expr.CreateExpressionTree (ec);
142 public override Expression DoResolve (ResolveContext ec)
147 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
149 return DoResolve (ec);
152 public override void Emit (EmitContext ec)
156 Invocation.EmitCall (ec, false, this, info.GetValueOrDefault, null, loc);
158 Invocation.EmitCall (ec, false, this, info.Value, null, loc);
161 public void EmitCheck (EmitContext ec)
164 Invocation.EmitCall (ec, false, this, info.HasValue, null, loc);
167 public override bool Equals (object obj)
169 Unwrap uw = obj as Unwrap;
170 return uw != null && expr.Equals (uw.expr);
173 public Expression Original {
179 public override int GetHashCode ()
181 return expr.GetHashCode ();
184 public override bool IsNull {
190 void Store (EmitContext ec)
192 if (expr is VariableReference)
199 LocalVariable.Store (ec);
202 public void Load (EmitContext ec)
204 if (expr is VariableReference)
207 LocalVariable.Emit (ec);
210 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
212 type = storey.MutateType (type);
213 info.Constructor = storey.MutateConstructor (info.Constructor);
214 info.HasValue = storey.MutateGenericMethod (info.HasValue);
215 info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
216 info.Value = storey.MutateGenericMethod (info.Value);
219 public void AddressOf (EmitContext ec, AddressOp mode)
221 IMemoryLocation ml = expr as VariableReference;
223 ml.AddressOf (ec, mode);
225 LocalVariable.AddressOf (ec, mode);
229 // Keeps result of non-variable expression
231 LocalTemporary LocalVariable {
234 temp = new LocalTemporary (info.Type);
239 public void Emit (EmitContext ec, bool leave_copy)
247 public void EmitAssign (EmitContext ec, Expression source,
248 bool leave_copy, bool prepare_for_load)
250 InternalWrap wrap = new InternalWrap (source, info, loc);
251 ((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
254 protected class InternalWrap : Expression
256 public Expression expr;
257 public NullableInfo info;
259 public InternalWrap (Expression expr, NullableInfo info, Location loc)
266 eclass = ExprClass.Value;
269 public override Expression CreateExpressionTree (ResolveContext ec)
271 throw new NotSupportedException ("ET");
274 public override Expression DoResolve (ResolveContext ec)
279 public override void Emit (EmitContext ec)
282 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
287 public class Wrap : TypeCast
289 readonly NullableInfo info;
291 protected Wrap (Expression expr, Type type)
294 info = new NullableInfo (type);
295 eclass = ExprClass.Value;
298 public Expression Child {
299 get { return child; }
302 public override Expression CreateExpressionTree (ResolveContext ec)
304 TypeCast child_cast = child as TypeCast;
305 if (child_cast != null) {
307 return child_cast.CreateExpressionTree (ec);
310 return base.CreateExpressionTree (ec);
313 public static Expression Create (Expression expr, Type type)
316 // Avoid unwraping and wraping of the same type
318 Unwrap unwrap = expr as Unwrap;
319 if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0])))
320 return unwrap.Original;
322 return new Wrap (expr, type);
325 public override void Emit (EmitContext ec)
328 ec.ig.Emit (OpCodes.Newobj, info.Constructor);
333 // Represents null literal lifted to nullable type
335 public class LiftedNull : EmptyConstantCast, IMemoryLocation
337 private LiftedNull (Type nullable_type, Location loc)
338 : base (new NullLiteral (loc), nullable_type)
340 eclass = ExprClass.Value;
343 public static Constant Create (Type nullable, Location loc)
345 return new LiftedNull (nullable, loc);
348 public static Expression CreateFromExpression (ResolveContext ec, Expression e)
350 ec.Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
351 TypeManager.CSharpName (e.Type));
353 return ReducedExpression.Create (Create (e.Type, e.Location), e);
356 public override Expression CreateExpressionTree (ResolveContext ec)
358 Arguments args = new Arguments (2);
359 args.Add (new Argument (this));
360 args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
362 return CreateExpressionFactoryCall (ec, "Constant", args);
365 public override void Emit (EmitContext ec)
367 // TODO: generate less temporary variables
368 LocalTemporary value_target = new LocalTemporary (type);
370 value_target.AddressOf (ec, AddressOp.Store);
371 ec.ig.Emit (OpCodes.Initobj, type);
372 value_target.Emit (ec);
375 public void AddressOf (EmitContext ec, AddressOp Mode)
377 LocalTemporary value_target = new LocalTemporary (type);
379 value_target.AddressOf (ec, AddressOp.Store);
380 ec.ig.Emit (OpCodes.Initobj, type);
381 ((IMemoryLocation) value_target).AddressOf (ec, Mode);
385 public class Lifted : Expression, IMemoryLocation
387 Expression expr, wrap, null_value;
390 public Lifted (Expression expr, Unwrap unwrap, Type type)
393 this.unwrap = unwrap;
394 this.loc = expr.Location;
398 public Lifted (Expression expr, Expression unwrap, Type type)
399 : this (expr, unwrap as Unwrap, type)
403 public override Expression CreateExpressionTree (ResolveContext ec)
405 return wrap.CreateExpressionTree (ec);
408 public override Expression DoResolve (ResolveContext ec)
410 wrap = Wrap.Create (expr, type);
415 // It's null when lifted conversion is transparent
420 null_value = LiftedNull.Create (type, loc);
422 eclass = ExprClass.Value;
426 public override void Emit (EmitContext ec)
428 ILGenerator ig = ec.ig;
429 Label is_null_label = ig.DefineLabel ();
430 Label end_label = ig.DefineLabel ();
432 unwrap.EmitCheck (ec);
433 ig.Emit (OpCodes.Brfalse, is_null_label);
436 ig.Emit (OpCodes.Br, end_label);
438 ig.MarkLabel (is_null_label);
439 null_value.Emit (ec);
441 ig.MarkLabel (end_label);
444 public void AddressOf (EmitContext ec, AddressOp mode)
446 unwrap.AddressOf (ec, mode);
450 public class LiftedUnaryOperator : Unary, IMemoryLocation
453 Expression user_operator;
455 public LiftedUnaryOperator (Unary.Operator op, Expression expr)
460 public void AddressOf (EmitContext ec, AddressOp mode)
462 unwrap.AddressOf (ec, mode);
465 public override Expression CreateExpressionTree (ResolveContext ec)
467 if (user_operator != null)
468 return user_operator.CreateExpressionTree (ec);
470 if (Oper == Operator.UnaryPlus)
471 return Expr.CreateExpressionTree (ec);
473 return base.CreateExpressionTree (ec);
476 public override Expression DoResolve (ResolveContext ec)
478 if (eclass != ExprClass.Invalid)
481 unwrap = Unwrap.Create (Expr, false);
485 Expression res = base.ResolveOperator (ec, unwrap);
487 if (user_operator == null)
490 res = Expr = LiftExpression (ec, Expr);
496 eclass = ExprClass.Value;
501 public override void Emit (EmitContext ec)
503 ILGenerator ig = ec.ig;
504 Label is_null_label = ig.DefineLabel ();
505 Label end_label = ig.DefineLabel ();
507 unwrap.EmitCheck (ec);
508 ig.Emit (OpCodes.Brfalse, is_null_label);
510 NullableInfo ni = new NullableInfo (type);
512 if (user_operator != null) {
513 user_operator.Emit (ec);
515 EmitOperator (ec, ni.UnderlyingType);
518 ig.Emit (OpCodes.Newobj, ni.Constructor);
519 ig.Emit (OpCodes.Br_S, end_label);
521 ig.MarkLabel (is_null_label);
522 LiftedNull.Create (type, loc).Emit (ec);
524 ig.MarkLabel (end_label);
527 Expression LiftExpression (ResolveContext ec, Expression expr)
529 TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
530 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
531 if (lifted_type == null)
534 expr.Type = lifted_type.Type;
538 protected override Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
540 expr = base.ResolveEnumOperator (ec, expr);
544 Expr = LiftExpression (ec, Expr);
545 return LiftExpression (ec, expr);
548 protected override Expression ResolveUserOperator (ResolveContext ec, Expression expr)
550 expr = base.ResolveUserOperator (ec, expr);
555 // When a user operator is of non-nullable type
557 if (Expr is Unwrap) {
558 user_operator = LiftExpression (ec, expr);
559 return user_operator;
566 public class LiftedBinaryOperator : Binary
568 Unwrap left_unwrap, right_unwrap;
569 bool left_null_lifted, right_null_lifted;
570 Expression left_orig, right_orig;
571 Expression user_operator;
572 ConstructorInfo wrap_ctor;
574 public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
576 : base (op, left, right)
581 public override Expression CreateExpressionTree (ResolveContext ec)
583 if (user_operator != null)
584 return user_operator.CreateExpressionTree (ec);
586 return base.CreateExpressionTree (ec);
590 // CSC 2 has this behavior, it allows structs to be compared
591 // with the null literal *outside* of a generics context and
592 // inlines that as true or false.
594 Expression CreateNullConstant (ResolveContext ec, Expression expr)
596 // FIXME: Handle side effect constants
597 Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
599 if ((Oper & Operator.EqualityMask) != 0) {
600 ec.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
601 expr.GetSignatureForError (), c.AsString ());
603 ec.Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
604 expr.GetSignatureForError (), c.AsString ());
607 return ReducedExpression.Create (c, this);
610 public override Expression DoResolve (ResolveContext ec)
612 if (eclass != ExprClass.Invalid)
615 if ((Oper & Operator.LogicalMask) != 0) {
616 Error_OperatorCannotBeApplied (ec, left, right);
620 bool use_default_call = (Oper & (Operator.BitwiseMask | Operator.EqualityMask)) != 0;
622 if (TypeManager.IsNullableType (left.Type)) {
623 left = left_unwrap = Unwrap.Create (left, use_default_call);
629 if (TypeManager.IsNullableType (right.Type)) {
630 right = right_unwrap = Unwrap.Create (right, use_default_call);
636 // Some details are in 6.4.2, 7.2.7
637 // Arguments can be lifted for equal operators when the return type is bool and both
638 // arguments are of same type
640 if (left_orig.IsNull) {
642 left_null_lifted = true;
643 type = TypeManager.bool_type;
646 if (right_orig.IsNull) {
648 right_null_lifted = true;
649 type = TypeManager.bool_type;
652 eclass = ExprClass.Value;
653 return DoResolveCore (ec, left_orig, right_orig);
656 void EmitBitwiseBoolean (EmitContext ec)
658 ILGenerator ig = ec.ig;
660 Label load_left = ig.DefineLabel ();
661 Label load_right = ig.DefineLabel ();
662 Label end_label = ig.DefineLabel ();
664 left_unwrap.Emit (ec);
665 ig.Emit (OpCodes.Brtrue_S, load_right);
667 right_unwrap.Emit (ec);
668 ig.Emit (OpCodes.Brtrue_S, load_left);
670 left_unwrap.EmitCheck (ec);
671 ig.Emit (OpCodes.Brfalse_S, load_right);
674 ig.MarkLabel (load_left);
676 if (Oper == Operator.BitwiseAnd) {
677 left_unwrap.Load (ec);
679 right_unwrap.Load (ec);
680 right_unwrap = left_unwrap;
682 ig.Emit (OpCodes.Br_S, end_label);
685 ig.MarkLabel (load_right);
686 right_unwrap.Load (ec);
688 ig.MarkLabel (end_label);
692 // Emits optimized equality or inequality operator when possible
694 void EmitEquality (EmitContext ec)
696 ILGenerator ig = ec.ig;
699 // Either left or right is null
701 if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
702 left_unwrap.EmitCheck (ec);
703 if (Oper == Binary.Operator.Equality) {
704 ig.Emit (OpCodes.Ldc_I4_0);
705 ig.Emit (OpCodes.Ceq);
710 if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
711 right_unwrap.EmitCheck (ec);
712 if (Oper == Binary.Operator.Equality) {
713 ig.Emit (OpCodes.Ldc_I4_0);
714 ig.Emit (OpCodes.Ceq);
719 Label dissimilar_label = ig.DefineLabel ();
720 Label end_label = ig.DefineLabel ();
722 if (user_operator != null) {
723 user_operator.Emit (ec);
724 ig.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
729 ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
732 if (left_unwrap != null)
733 left_unwrap.EmitCheck (ec);
735 if (right_unwrap != null)
736 right_unwrap.EmitCheck (ec);
738 if (left_unwrap != null && right_unwrap != null) {
739 if (Oper == Operator.Inequality)
740 ig.Emit (OpCodes.Xor);
742 ig.Emit (OpCodes.Ceq);
744 if (Oper == Operator.Inequality) {
745 ig.Emit (OpCodes.Ldc_I4_0);
746 ig.Emit (OpCodes.Ceq);
750 ig.Emit (OpCodes.Br_S, end_label);
752 ig.MarkLabel (dissimilar_label);
753 if (Oper == Operator.Inequality)
754 ig.Emit (OpCodes.Ldc_I4_1);
756 ig.Emit (OpCodes.Ldc_I4_0);
758 ig.MarkLabel (end_label);
761 public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
764 ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
767 public override void Emit (EmitContext ec)
770 // Optimize same expression operation
772 if (right_unwrap != null && right.Equals (left))
773 right_unwrap = left_unwrap;
775 if (user_operator == null && IsBitwiseBoolean) {
776 EmitBitwiseBoolean (ec);
780 if ((Oper & Operator.EqualityMask) != 0) {
785 ILGenerator ig = ec.ig;
787 Label is_null_label = ig.DefineLabel ();
788 Label end_label = ig.DefineLabel ();
790 if (left_unwrap != null) {
791 left_unwrap.EmitCheck (ec);
792 ig.Emit (OpCodes.Brfalse, is_null_label);
796 // Don't emit HasValue check when left and right expressions are same
798 if (right_unwrap != null && !left.Equals (right)) {
799 right_unwrap.EmitCheck (ec);
800 ig.Emit (OpCodes.Brfalse, is_null_label);
803 EmitOperator (ec, left.Type);
805 if (wrap_ctor != null)
806 ig.Emit (OpCodes.Newobj, wrap_ctor);
808 ig.Emit (OpCodes.Br_S, end_label);
809 ig.MarkLabel (is_null_label);
811 if ((Oper & Operator.ComparisonMask) != 0) {
812 ig.Emit (OpCodes.Ldc_I4_0);
814 LiftedNull.Create (type, loc).Emit (ec);
817 ig.MarkLabel (end_label);
820 protected override void EmitOperator (EmitContext ec, Type l)
822 if (user_operator != null) {
823 user_operator.Emit (ec);
827 if (TypeManager.IsNullableType (l))
828 l = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (l) [0]);
830 base.EmitOperator (ec, l);
833 bool IsBitwiseBoolean {
835 return (Oper & Operator.BitwiseMask) != 0 && left_unwrap != null && right_unwrap != null &&
836 left_unwrap.Type == TypeManager.bool_type && right_unwrap.Type == TypeManager.bool_type;
840 Expression LiftResult (ResolveContext ec, Expression res_expr)
842 TypeExpr lifted_type;
845 // Avoid double conversion
847 if (left_unwrap == null || left_null_lifted || !TypeManager.IsEqual (left_unwrap.Type, left.Type) || (left_unwrap != null && right_null_lifted)) {
848 lifted_type = new NullableType (left.Type, loc);
849 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
850 if (lifted_type == null)
853 if (left is UserCast || left is TypeCast)
854 left.Type = lifted_type.Type;
856 left = EmptyCast.Create (left, lifted_type.Type);
859 if (right_unwrap == null || right_null_lifted || !TypeManager.IsEqual (right_unwrap.Type, right.Type) || (right_unwrap != null && left_null_lifted)) {
860 lifted_type = new NullableType (right.Type, loc);
861 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
862 if (lifted_type == null)
865 if (right is UserCast || right is TypeCast)
866 right.Type = lifted_type.Type;
868 right = EmptyCast.Create (right, lifted_type.Type);
871 if ((Oper & Operator.ComparisonMask) == 0) {
872 lifted_type = new NullableType (res_expr.Type, loc);
873 lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
874 if (lifted_type == null)
877 wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
878 type = res_expr.Type = lifted_type.Type;
881 if (left_null_lifted) {
882 left = LiftedNull.Create (right.Type, left.Location);
884 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
885 return LiftedNull.CreateFromExpression (ec, res_expr);
888 // Value types and null comparison
890 if (right_unwrap == null || (Oper & Operator.RelationalMask) != 0)
891 return CreateNullConstant (ec, right_orig).Resolve (ec);
894 if (right_null_lifted) {
895 right = LiftedNull.Create (left.Type, right.Location);
897 if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
898 return LiftedNull.CreateFromExpression (ec, res_expr);
901 // Value types and null comparison
903 if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
904 return CreateNullConstant (ec, left_orig).Resolve (ec);
910 protected override Expression ResolveOperatorPredefined (ResolveContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
912 Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
914 if (e == this || enum_type != null)
915 return LiftResult (ec, e);
918 // 7.9.9 Equality operators and null
920 // The == and != operators permit one operand to be a value of a nullable type and
921 // the other to be the null literal, even if no predefined or user-defined operator
922 // (in unlifted or lifted form) exists for the operation.
924 if (e == null && (Oper & Operator.EqualityMask) != 0) {
925 if ((left_null_lifted && right_unwrap != null) || (right_null_lifted && left_unwrap != null))
926 return LiftResult (ec, this);
932 protected override Expression ResolveUserOperator (ResolveContext ec, Type l, Type r)
934 Expression expr = base.ResolveUserOperator (ec, l, r);
938 expr = LiftResult (ec, expr);
939 if (expr is Constant)
943 user_operator = expr;
948 public class NullCoalescingOperator : Expression
950 Expression left, right;
953 public NullCoalescingOperator (Expression left, Expression right, Location loc)
960 public override Expression CreateExpressionTree (ResolveContext ec)
962 if (left.Type == TypeManager.null_type)
963 ec.Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
965 UserCast uc = left as UserCast;
966 Expression conversion = null;
970 Arguments c_args = new Arguments (2);
971 c_args.Add (new Argument (uc.CreateExpressionTree (ec)));
972 c_args.Add (new Argument (left.CreateExpressionTree (ec)));
973 conversion = CreateExpressionFactoryCall (ec, "Lambda", c_args);
976 Arguments args = new Arguments (3);
977 args.Add (new Argument (left.CreateExpressionTree (ec)));
978 args.Add (new Argument (right.CreateExpressionTree (ec)));
979 if (conversion != null)
980 args.Add (new Argument (conversion));
982 return CreateExpressionFactoryCall (ec, "Coalesce", args);
985 Expression ConvertExpression (ResolveContext ec)
987 // TODO: ImplicitConversionExists should take care of this
988 if (left.eclass == ExprClass.MethodGroup)
991 Type ltype = left.Type;
994 // If left is a nullable type and an implicit conversion exists from right to underlying type of left,
995 // the result is underlying type of left
997 if (TypeManager.IsNullableType (ltype)) {
998 unwrap = Unwrap.Create (left, false);
1002 if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
1005 right = Convert.ImplicitConversion (ec, right, type, loc);
1008 } else if (TypeManager.IsReferenceType (ltype)) {
1009 if (Convert.ImplicitConversionExists (ec, right, ltype)) {
1011 // Reduce (constant ?? expr) to constant
1013 Constant lc = left as Constant;
1014 if (lc != null && !lc.IsDefaultValue)
1015 return new SideEffectConstant (lc, right, loc).Resolve (ec);
1018 // Reduce (left ?? null) to left OR (null-constant ?? right) to right
1020 if (right.IsNull || lc != null)
1021 return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
1023 right = Convert.ImplicitConversion (ec, right, ltype, loc);
1031 Type rtype = right.Type;
1032 if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
1036 // Reduce (null ?? right) to right
1039 return ReducedExpression.Create (right, this).Resolve (ec);
1041 left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
1046 public override Expression DoResolve (ResolveContext ec)
1048 if (eclass != ExprClass.Invalid)
1051 left = left.Resolve (ec);
1052 right = right.Resolve (ec);
1054 if (left == null || right == null)
1057 eclass = ExprClass.Value;
1059 Expression e = ConvertExpression (ec);
1061 Binary.Error_OperatorCannotBeApplied (ec, left, right, "??", loc);
1068 public override void Emit (EmitContext ec)
1070 ILGenerator ig = ec.ig;
1072 Label end_label = ig.DefineLabel ();
1074 if (unwrap != null) {
1075 Label is_null_label = ig.DefineLabel ();
1077 unwrap.EmitCheck (ec);
1078 ig.Emit (OpCodes.Brfalse, is_null_label);
1081 ig.Emit (OpCodes.Br, end_label);
1083 ig.MarkLabel (is_null_label);
1086 ig.MarkLabel (end_label);
1092 ig.Emit (OpCodes.Dup);
1093 ig.Emit (OpCodes.Brtrue, end_label);
1095 ig.Emit (OpCodes.Pop);
1098 ig.MarkLabel (end_label);
1101 public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
1103 left.MutateHoistedGenericType (storey);
1104 right.MutateHoistedGenericType (storey);
1105 type = storey.MutateType (type);
1108 protected override void CloneTo (CloneContext clonectx, Expression t)
1110 NullCoalescingOperator target = (NullCoalescingOperator) t;
1112 target.left = left.Clone (clonectx);
1113 target.right = right.Clone (clonectx);
1117 public class LiftedUnaryMutator : ExpressionStatement
1119 public readonly UnaryMutator.Mode Mode;
1121 UnaryMutator underlying;
1124 public LiftedUnaryMutator (UnaryMutator.Mode mode, Expression expr, Location loc)
1130 eclass = ExprClass.Value;
1133 public override Expression CreateExpressionTree (ResolveContext ec)
1135 return new SimpleAssign (this, this).CreateExpressionTree (ec);
1138 public override Expression DoResolve (ResolveContext ec)
1140 expr = expr.Resolve (ec);
1144 unwrap = Unwrap.Create (expr, false);
1148 underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap).Resolve (ec);
1149 if (underlying == null)
1156 void DoEmit (EmitContext ec, bool is_expr)
1158 ILGenerator ig = ec.ig;
1159 Label is_null_label = ig.DefineLabel ();
1160 Label end_label = ig.DefineLabel ();
1162 unwrap.EmitCheck (ec);
1163 ig.Emit (OpCodes.Brfalse, is_null_label);
1166 underlying.Emit (ec);
1167 ig.Emit (OpCodes.Br_S, end_label);
1169 underlying.EmitStatement (ec);
1172 ig.MarkLabel (is_null_label);
1174 LiftedNull.Create (type, loc).Emit (ec);
1176 ig.MarkLabel (end_label);
1179 public override void Emit (EmitContext ec)
1184 public override void EmitStatement (EmitContext ec)