Create() public static method

public static Create ( TypeSpec nullable, Location loc ) : Constant
nullable TypeSpec
loc Location
return Constant
Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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)
            {
                if (!expr.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);
        }
Example #4
0
        void EmitBitwiseBoolean(EmitContext ec)
        {
            Label load_left     = ec.DefineLabel();
            Label load_right    = ec.DefineLabel();
            Label end_label     = ec.DefineLabel();
            Label is_null_label = ec.DefineLabel();

            bool or = Binary.Oper == Binary.Operator.BitwiseOr;

            //
            // Both operands are bool? types
            //
            if (UnwrapLeft != null && UnwrapRight != null)
            {
                if (ec.HasSet(BuilderContext.Options.AsyncBody) && Binary.Right.ContainsEmitWithAwait())
                {
                    Left  = Left.EmitToField(ec);
                    Right = Right.EmitToField(ec);
                }
                else
                {
                    UnwrapLeft.Store(ec);
                    UnwrapRight.Store(ec);
                }

                Left.Emit(ec);
                ec.Emit(OpCodes.Brtrue_S, load_right);

                Right.Emit(ec);
                ec.Emit(OpCodes.Brtrue_S, load_left);

                UnwrapLeft.EmitCheck(ec);
                ec.Emit(OpCodes.Brfalse_S, load_right);

                // load left
                ec.MarkLabel(load_left);
                if (or)
                {
                    UnwrapRight.Load(ec);
                }
                else
                {
                    UnwrapLeft.Load(ec);
                }

                ec.Emit(OpCodes.Br_S, end_label);

                // load right
                ec.MarkLabel(load_right);
                if (or)
                {
                    UnwrapLeft.Load(ec);
                }
                else
                {
                    UnwrapRight.Load(ec);
                }

                ec.MarkLabel(end_label);
                return;
            }

            //
            // Faster version when one operand is bool
            //
            if (UnwrapLeft == null)
            {
                //
                // (bool, bool?)
                //
                // Optimizes remaining (false & bool?), (true | bool?) which are not easy to handle
                // in binary expression reduction
                //
                var c = Left as BoolConstant;
                if (c != null)
                {
                    // Keep evaluation order
                    UnwrapRight.Store(ec);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                }
                else if (Left.IsNull)
                {
                    UnwrapRight.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

                    UnwrapRight.Load(ec);
                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(is_null_label);
                    LiftedNull.Create(type, loc).Emit(ec);
                }
                else
                {
                    Left.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));

                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(load_right);
                    UnwrapRight.Original.Emit(ec);
                }
            }
            else
            {
                //
                // (bool?, bool)
                //
                // Keep left-right evaluation order
                UnwrapLeft.Store(ec);

                //
                // Optimizes remaining (bool? & false), (bool? | true) which are not easy to handle
                // in binary expression reduction
                //
                var c = Right as BoolConstant;
                if (c != null)
                {
                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
                }
                else if (Right.IsNull)
                {
                    UnwrapLeft.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, is_null_label);

                    UnwrapLeft.Load(ec);
                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(is_null_label);
                    LiftedNull.Create(type, loc).Emit(ec);
                }
                else
                {
                    Right.Emit(ec);
                    ec.Emit(or ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, load_right);

                    ec.EmitInt(or ? 1 : 0);
                    ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));

                    ec.Emit(OpCodes.Br_S, end_label);

                    ec.MarkLabel(load_right);

                    UnwrapLeft.Load(ec);
                }
            }

            ec.MarkLabel(end_label);
        }
Example #5
0
        public override void Emit(EmitContext ec)
        {
            if (IsBitwiseBoolean && UserOperator == null)
            {
                EmitBitwiseBoolean(ec);
                return;
            }

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

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

            if (ec.HasSet(BuilderContext.Options.AsyncBody) && Right.ContainsEmitWithAwait())
            {
                Left  = Left.EmitToField(ec);
                Right = Right.EmitToField(ec);
            }

            if (UnwrapLeft != null)
            {
                UnwrapLeft.EmitCheck(ec);
            }

            //
            // Don't emit HasValue check when left and right expressions are same
            //
            if (UnwrapRight != null && !Binary.Left.Equals(Binary.Right))
            {
                UnwrapRight.EmitCheck(ec);
                if (UnwrapLeft != null)
                {
                    ec.Emit(OpCodes.And);
                }
            }

            ec.Emit(OpCodes.Brfalse, is_null_label);

            if (UserOperator != null)
            {
                var args = new Arguments(2);
                args.Add(new Argument(Left));
                args.Add(new Argument(Right));

                var call = new CallEmitter();
                call.EmitPredefined(ec, UserOperator, args);
            }
            else
            {
                Binary.EmitOperator(ec, Left, Right);
            }

            //
            // Wrap the result when the operator return type is nullable type
            //
            if (type.IsNullableType)
            {
                ec.Emit(OpCodes.Newobj, NullableInfo.GetConstructor(type));
            }

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

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

            ec.MarkLabel(end_label);
        }