public override void Emit(EmitContext ec)
        {
            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            unwrap.EmitCheck(ec);
            ec.Emit(OpCodes.Brfalse, is_null_label);

            if (user_operator != null)
            {
                user_operator.Emit(ec);
            }
            else
            {
                EmitOperator(ec, NullableInfo.GetUnderlyingType(type));
            }

            ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
            ec.Emit(OpCodes.Br_S, end_label);

            ec.MarkLabel(is_null_label);
            LiftedNull.Create(type, loc).Emit(ec);

            ec.MarkLabel(end_label);
        }
        protected override void EmitOperation(EmitContext ec)
        {
            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            LocalTemporary lt = new LocalTemporary(type);

            // Value is on the stack
            lt.Store(ec);

            var call = new CallEmitter();

            call.InstanceExpression = lt;
            call.EmitPredefined(ec, NullableInfo.GetHasValue(expr.Type), null);

            ec.Emit(OpCodes.Brfalse, is_null_label);

            call = new CallEmitter();
            call.InstanceExpression = lt;
            call.EmitPredefined(ec, NullableInfo.GetGetValueOrDefault(expr.Type), null);

            lt.Release(ec);

            base.EmitOperation(ec);

            ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
            ec.Emit(OpCodes.Br_S, end_label);

            ec.MarkLabel(is_null_label);
            LiftedNull.Create(type, loc).Emit(ec);

            ec.MarkLabel(end_label);
        }
        protected override Expression DoResolve(ResolveContext ec)
        {
            //
            // It's null when lifting non-nullable type
            //
            if (unwrap == null)
            {
                // S -> T? is wrap only
                if (type.IsNullableType)
                {
                    return(Wrap.Create(expr, type));
                }

                // S -> T can be simplified
                return(expr);
            }

            // Wrap target for T?
            if (type.IsNullableType)
            {
                expr = Wrap.Create(expr, type);
                if (expr == null)
                {
                    return(null);
                }

                null_value = LiftedNull.Create(type, loc);
            }
            else if (TypeSpec.IsValueType(type))
            {
                null_value = LiftedNull.Create(type, loc);
            }
            else
            {
                null_value = new NullConstant(type, loc);
            }

            eclass = ExprClass.Value;
            return(this);
        }
        public override void Emit(EmitContext ec)
        {
            //
            // Optimize same expression operation
            //
            if (right_unwrap != null && right.Equals(left))
            {
                right_unwrap = left_unwrap;
            }

            if (user_operator == null && IsBitwiseBoolean)
            {
                EmitBitwiseBoolean(ec);
                return;
            }

            if ((Oper & Operator.EqualityMask) != 0)
            {
                EmitEquality(ec);
                return;
            }

            Label is_null_label = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();

            if (left_unwrap != null)
            {
                left_unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);
            }

            //
            // Don't emit HasValue check when left and right expressions are same
            //
            if (right_unwrap != null && !left.Equals(right))
            {
                right_unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse, is_null_label);
            }

            EmitOperator(ec, left.Type);

            if (wrap_ctor != null)
            {
                ec.Emit(OpCodes.Newobj, wrap_ctor);
            }

            ec.Emit(OpCodes.Br_S, end_label);
            ec.MarkLabel(is_null_label);

            if ((Oper & Operator.ComparisonMask) != 0)
            {
                ec.EmitInt(0);
            }
            else
            {
                LiftedNull.Create(type, loc).Emit(ec);
            }

            ec.MarkLabel(end_label);
        }
        Expression LiftResult(ResolveContext ec, Expression res_expr)
        {
            TypeSpec lifted_type;

            //
            // Avoid double conversion
            //
            if (left_unwrap == null || IsLeftNullLifted || left_unwrap.Type != left.Type || (left_unwrap != null && IsRightNullLifted))
            {
                lifted_type = new NullableType(left.Type, loc).ResolveAsType(ec);
                if (lifted_type == null)
                {
                    return(null);
                }

                if (left is UserCast || left is EmptyCast || left is OpcodeCast)
                {
                    left.Type = lifted_type;
                }
                else
                {
                    left = EmptyCast.Create(left, lifted_type);
                }
            }

            if (left != right && (right_unwrap == null || IsRightNullLifted || right_unwrap.Type != right.Type || (right_unwrap != null && IsLeftNullLifted)))
            {
                lifted_type = new NullableType(right.Type, loc).ResolveAsType(ec);
                if (lifted_type == null)
                {
                    return(null);
                }

                var r = right;
                if (r is ReducedExpression)
                {
                    r = ((ReducedExpression)r).OriginalExpression;
                }

                if (r is UserCast || r is EmptyCast || r is OpcodeCast)
                {
                    r.Type = lifted_type;
                }
                else
                {
                    right = EmptyCast.Create(right, lifted_type);
                }
            }

            if ((Oper & Operator.ComparisonMask) == 0)
            {
                lifted_type = new NullableType(res_expr.Type, loc).ResolveAsType(ec);
                if (lifted_type == null)
                {
                    return(null);
                }

                wrap_ctor = NullableInfo.GetConstructor(lifted_type);
                type      = res_expr.Type = lifted_type;
            }

            if (IsLeftNullLifted)
            {
                left = LiftedNull.Create(right.Type, left.Location);

                //
                // Special case for bool?, the result depends on both null right side and left side value
                //
                if ((Oper == Operator.BitwiseAnd || Oper == Operator.BitwiseOr) && NullableInfo.GetUnderlyingType(type).BuiltinType == BuiltinTypeSpec.Type.Bool)
                {
                    return(res_expr);
                }

                if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
                {
                    return(LiftedNull.CreateFromExpression(ec, res_expr));
                }

                //
                // Value types and null comparison
                //
                if (right_unwrap == null || (Oper & Operator.RelationalMask) != 0)
                {
                    return(CreateNullConstant(ec, right_orig));
                }
            }

            if (IsRightNullLifted)
            {
                right = LiftedNull.Create(left.Type, right.Location);

                //
                // Special case for bool?, the result depends on both null right side and left side value
                //
                if ((Oper == Operator.BitwiseAnd || Oper == Operator.BitwiseOr) && NullableInfo.GetUnderlyingType(type).BuiltinType == BuiltinTypeSpec.Type.Bool)
                {
                    return(res_expr);
                }

                if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask | Operator.BitwiseMask)) != 0)
                {
                    return(LiftedNull.CreateFromExpression(ec, res_expr));
                }

                //
                // Value types and null comparison
                //
                if (left_unwrap == null || (Oper & Operator.RelationalMask) != 0)
                {
                    return(CreateNullConstant(ec, left_orig));
                }
            }

            return(res_expr);
        }