using System;
using System.Reflection;
using System.Reflection.Emit;
-using System.Collections;
namespace Mono.CSharp.Nullable
{
eclass = ExprClass.Type;
}
- public NullableType (Type type, Location loc)
+ public NullableType (TypeSpec type, Location loc)
: this (new TypeExpression (type, loc), loc)
{ }
- protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+ protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
{
if (TypeManager.generic_nullable_type == null) {
- TypeManager.generic_nullable_type = TypeManager.CoreLookupType (
- "System", "Nullable`1", Kind.Struct, true);
+ TypeManager.generic_nullable_type = TypeManager.CoreLookupType (ec.Compiler,
+ "System", "Nullable", 1, MemberKind.Struct, true);
}
TypeArguments args = new TypeArguments (underlying);
GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc);
return ctype.ResolveAsTypeTerminal (ec, false);
}
-
- public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
- {
- return ResolveAsBaseTerminal (ec, silent);
- }
}
- public sealed class NullableInfo
+ static class NullableInfo
{
- public readonly Type Type;
- public readonly Type UnderlyingType;
- public MethodInfo HasValue;
- public MethodInfo Value;
- public MethodInfo GetValueOrDefault;
- public ConstructorInfo Constructor;
-
- public NullableInfo (Type type)
- {
- Type = type;
- UnderlyingType = TypeManager.GetTypeArguments (type) [0];
-
- PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
- PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null, Type.EmptyTypes);
- GetValueOrDefault = TypeManager.GetPredefinedMethod (type, "GetValueOrDefault", Location.Null, Type.EmptyTypes);
-
- HasValue = has_value_pi.GetGetMethod (false);
- Value = value_pi.GetGetMethod (false);
-#if MS_COMPATIBLE
- if (UnderlyingType.Module == CodeGen.Module.Builder) {
- Type o_type = TypeManager.DropGenericTypeArguments (type);
- Constructor = TypeBuilder.GetConstructor (type,
- TypeManager.GetPredefinedConstructor (o_type, Location.Null, o_type.GetGenericArguments ()));
- return;
- }
-#endif
- Constructor = type.GetConstructor (new Type[] { UnderlyingType });
+ public static MethodSpec GetConstructor (TypeSpec nullableType)
+ {
+ return TypeManager.GetPredefinedConstructor (nullableType, Location.Null, GetUnderlyingType (nullableType));
+ }
+
+ public static MethodSpec GetHasValue (TypeSpec nullableType)
+ {
+ return (MethodSpec) MemberCache.FindMember (nullableType,
+ MemberFilter.Method ("get_HasValue", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+ }
+
+ public static MethodSpec GetGetValueOrDefault (TypeSpec nullableType)
+ {
+ return (MethodSpec) MemberCache.FindMember (nullableType,
+ MemberFilter.Method ("GetValueOrDefault", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+ }
+
+ public static MethodSpec GetValue (TypeSpec nullableType)
+ {
+ return (MethodSpec) MemberCache.FindMember (nullableType,
+ MemberFilter.Method ("get_Value", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None);
+ }
+
+ public static TypeSpec GetUnderlyingType (TypeSpec nullableType)
+ {
+ return ((InflatedTypeSpec) nullableType).TypeArguments[0];
+ }
+
+ public static bool IsNullableType (TypeSpec type)
+ {
+ throw new NotImplementedException ("net");
}
}
public class Unwrap : Expression, IMemoryLocation, IAssignMethod
{
Expression expr;
- NullableInfo info;
LocalTemporary temp;
+ readonly bool useDefaultValue;
- protected Unwrap (Expression expr)
+ Unwrap (Expression expr, bool useDefaultValue)
{
this.expr = expr;
this.loc = expr.Location;
+ this.useDefaultValue = useDefaultValue;
- info = new NullableInfo (expr.Type);
- type = info.UnderlyingType;
+ type = NullableInfo.GetUnderlyingType (expr.Type);
eclass = expr.eclass;
}
if (wrap != null)
return wrap.Child;
- return Create (expr, null);
+ return Create (expr, false);
}
- public static Unwrap Create (Expression expr, EmitContext ec)
+ public static Unwrap Create (Expression expr, bool useDefaultValue)
{
- return new Unwrap (expr);
+ return new Unwrap (expr, useDefaultValue);
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
return expr.CreateExpressionTree (ec);
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
return this;
}
- public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
{
- return DoResolve (ec);
+ expr = expr.DoResolveLValue (ec, right_side);
+ return this;
}
public override void Emit (EmitContext ec)
{
Store (ec);
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.Value, null);
+ if (useDefaultValue)
+ Invocation.EmitCall (ec, this, NullableInfo.GetGetValueOrDefault (expr.Type), null, loc);
+ else
+ Invocation.EmitCall (ec, this, NullableInfo.GetValue (expr.Type), null, loc);
}
public void EmitCheck (EmitContext ec)
{
Store (ec);
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
- }
-
- public void EmitGetValueOrDefault (EmitContext ec)
- {
- Store (ec);
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.GetValueOrDefault, null);
+ Invocation.EmitCall (ec, this, NullableInfo.GetHasValue (expr.Type), null, loc);
}
public override bool Equals (object obj)
LocalVariable.Emit (ec);
}
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+ public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
{
- type = storey.MutateType (type);
- info.Constructor = storey.MutateConstructor (info.Constructor);
- info.HasValue = storey.MutateGenericMethod (info.HasValue);
- info.GetValueOrDefault = storey.MutateGenericMethod (info.GetValueOrDefault);
- info.Value = storey.MutateGenericMethod (info.Value);
+ return expr.MakeExpression (ctx);
}
public void AddressOf (EmitContext ec, AddressOp mode)
{
IMemoryLocation ml = expr as VariableReference;
- if (ml != null)
+ if (ml != null)
ml.AddressOf (ec, mode);
else
LocalVariable.AddressOf (ec, mode);
LocalTemporary LocalVariable {
get {
if (temp == null)
- temp = new LocalTemporary (info.Type);
+ temp = new LocalTemporary (expr.Type);
return temp;
}
}
public void EmitAssign (EmitContext ec, Expression source,
bool leave_copy, bool prepare_for_load)
{
- InternalWrap wrap = new InternalWrap (source, info, loc);
+ InternalWrap wrap = new InternalWrap (source, expr.Type, loc);
((IAssignMethod) expr).EmitAssign (ec, wrap, leave_copy, false);
}
- protected class InternalWrap : Expression
+ class InternalWrap : Expression
{
public Expression expr;
- public NullableInfo info;
- public InternalWrap (Expression expr, NullableInfo info, Location loc)
+ public InternalWrap (Expression expr, TypeSpec type, Location loc)
{
this.expr = expr;
- this.info = info;
this.loc = loc;
+ this.type = type;
- type = info.Type;
eclass = ExprClass.Value;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
throw new NotSupportedException ("ET");
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
return this;
}
public override void Emit (EmitContext ec)
{
expr.Emit (ec);
- ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+ ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
}
}
}
- public class Wrap : TypeCast
+ //
+ // Calls get_Value method on nullable expression
+ //
+ public class UnwrapCall : CompositeExpression
{
- readonly NullableInfo info;
+ public UnwrapCall (Expression expr)
+ : base (expr)
+ {
+ }
- protected Wrap (Expression expr, Type type)
- : base (expr, type)
+ protected override Expression DoResolve (ResolveContext rc)
{
- info = new NullableInfo (type);
- eclass = ExprClass.Value;
+ base.DoResolve (rc);
+
+ if (type != null)
+ type = NullableInfo.GetUnderlyingType (type);
+
+ return this;
}
- public Expression Child {
- get { return child; }
+ public override void Emit (EmitContext ec)
+ {
+ Invocation.EmitCall (ec, Child, NullableInfo.GetValue (Child.Type), null, loc);
+ }
+ }
+
+ public class Wrap : TypeCast
+ {
+ private Wrap (Expression expr, TypeSpec type)
+ : base (expr, type)
+ {
+ eclass = ExprClass.Value;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
TypeCast child_cast = child as TypeCast;
if (child_cast != null) {
return base.CreateExpressionTree (ec);
}
- public static Expression Create (Expression expr, Type type)
+ public static Expression Create (Expression expr, TypeSpec type)
{
//
// Avoid unwraping and wraping of the same type
//
Unwrap unwrap = expr as Unwrap;
- if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.GetTypeArguments (type) [0]))
+ if (unwrap != null && expr.Type == NullableInfo.GetUnderlyingType (type))
return unwrap.Original;
return new Wrap (expr, type);
public override void Emit (EmitContext ec)
{
child.Emit (ec);
- ec.ig.Emit (OpCodes.Newobj, info.Constructor);
+ ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
}
}
//
// Represents null literal lifted to nullable type
//
- public class LiftedNull : EmptyConstantCast, IMemoryLocation
+ public class LiftedNull : NullConstant, IMemoryLocation
{
- private LiftedNull (Type nullable_type, Location loc)
- : base (new NullLiteral (loc), nullable_type)
+ private LiftedNull (TypeSpec nullable_type, Location loc)
+ : base (nullable_type, loc)
{
eclass = ExprClass.Value;
}
- public static Constant Create (Type nullable, Location loc)
+ public static Constant Create (TypeSpec nullable, Location loc)
{
return new LiftedNull (nullable, loc);
}
- public static Expression CreateFromExpression (Expression e)
+ public static Constant CreateFromExpression (ResolveContext ec, Expression e)
{
- Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
+ ec.Report.Warning (458, 2, e.Location, "The result of the expression is always `null' of type `{0}'",
TypeManager.CSharpName (e.Type));
return ReducedExpression.Create (Create (e.Type, e.Location), e);
}
- public override Expression CreateExpressionTree (EmitContext ec)
- {
- ArrayList args = new ArrayList (2);
- args.Add (new Argument (this));
- args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-
- return CreateExpressionFactoryCall ("Constant", args);
- }
-
public override void Emit (EmitContext ec)
{
// TODO: generate less temporary variables
LocalTemporary value_target = new LocalTemporary (type);
value_target.AddressOf (ec, AddressOp.Store);
- ec.ig.Emit (OpCodes.Initobj, type);
+ ec.Emit (OpCodes.Initobj, type);
value_target.Emit (ec);
}
LocalTemporary value_target = new LocalTemporary (type);
value_target.AddressOf (ec, AddressOp.Store);
- ec.ig.Emit (OpCodes.Initobj, type);
+ ec.Emit (OpCodes.Initobj, type);
((IMemoryLocation) value_target).AddressOf (ec, Mode);
}
}
+ //
+ // Generic lifting expression, supports all S/S? -> T/T? cases
+ //
public class Lifted : Expression, IMemoryLocation
{
- Expression expr, wrap, null_value;
+ Expression expr, null_value;
Unwrap unwrap;
- public Lifted (Expression expr, Unwrap unwrap, Type type)
+ public Lifted (Expression expr, Unwrap unwrap, TypeSpec type)
{
this.expr = expr;
this.unwrap = unwrap;
this.type = type;
}
- public Lifted (Expression expr, Expression unwrap, Type type)
+ public Lifted (Expression expr, Expression unwrap, TypeSpec type)
: this (expr, unwrap as Unwrap, type)
{
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
- return wrap.CreateExpressionTree (ec);
+ return expr.CreateExpressionTree (ec);
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
- wrap = Wrap.Create (expr, type);
- if (wrap == null)
- return null;
-
//
- // It's null when lifted conversion is transparent
+ // It's null when lifting non-nullable type
//
- if (unwrap == null)
- return wrap;
+ if (unwrap == null) {
+ // S -> T? is wrap only
+ if (TypeManager.IsNullableType (type))
+ return Wrap.Create (expr, type);
- null_value = LiftedNull.Create (type, loc);
+ // S -> T can be simplified
+ return expr;
+ }
+
+ // Wrap target for T?
+ if (TypeManager.IsNullableType (type)) {
+ expr = Wrap.Create (expr, type);
+ if (expr == null)
+ return null;
+
+ null_value = LiftedNull.Create (type, loc);
+ } else if (TypeManager.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)
{
- ILGenerator ig = ec.ig;
- Label is_null_label = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ Label is_null_label = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
+
+ expr.Emit (ec);
- wrap.Emit (ec);
- ig.Emit (OpCodes.Br, end_label);
+ ec.Emit (OpCodes.Br, end_label);
+ ec.MarkLabel (is_null_label);
- ig.MarkLabel (is_null_label);
null_value.Emit (ec);
-
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
}
public void AddressOf (EmitContext ec, AddressOp mode)
Unwrap unwrap;
Expression user_operator;
- public LiftedUnaryOperator (Unary.Operator op, Expression expr)
- : base (op, expr)
+ public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
+ : base (op, expr, loc)
{
}
unwrap.AddressOf (ec, mode);
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
if (user_operator != null)
return user_operator.CreateExpressionTree (ec);
return base.CreateExpressionTree (ec);
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
- if (eclass != ExprClass.Invalid)
- return this;
-
- unwrap = Unwrap.Create (Expr, ec);
+ unwrap = Unwrap.Create (Expr, false);
if (unwrap == null)
return null;
public override void Emit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
- Label is_null_label = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ Label is_null_label = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
-
- NullableInfo ni = new NullableInfo (type);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
if (user_operator != null) {
user_operator.Emit (ec);
} else {
- EmitOperator (ec, ni.UnderlyingType);
+ EmitOperator (ec, NullableInfo.GetUnderlyingType (type));
}
- ig.Emit (OpCodes.Newobj, ni.Constructor);
- ig.Emit (OpCodes.Br_S, end_label);
+ ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
+ ec.Emit (OpCodes.Br_S, end_label);
- ig.MarkLabel (is_null_label);
+ ec.MarkLabel (is_null_label);
LiftedNull.Create (type, loc).Emit (ec);
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
}
- Expression LiftExpression (EmitContext ec, Expression expr)
+ Expression LiftExpression (ResolveContext ec, Expression expr)
{
TypeExpr lifted_type = new NullableType (expr.Type, expr.Location);
lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
return expr;
}
- protected override Expression ResolveEnumOperator (EmitContext ec, Expression expr)
+ protected override Expression ResolveEnumOperator (ResolveContext ec, Expression expr)
{
expr = base.ResolveEnumOperator (ec, expr);
if (expr == null)
return LiftExpression (ec, expr);
}
- protected override Expression ResolveUserOperator (EmitContext ec, Expression expr)
+ protected override Expression ResolveUserOperator (ResolveContext ec, Expression expr)
{
expr = base.ResolveUserOperator (ec, expr);
if (expr == null)
public class LiftedBinaryOperator : Binary
{
Unwrap left_unwrap, right_unwrap;
- bool left_null_lifted, right_null_lifted;
Expression left_orig, right_orig;
Expression user_operator;
- ConstructorInfo wrap_ctor;
+ MethodSpec wrap_ctor;
- public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right,
- Location loc)
- : base (op, left, right)
+ public LiftedBinaryOperator (Binary.Operator op, Expression left, Expression right, Location loc)
+ : base (op, left, right, loc)
{
- this.loc = loc;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ bool IsBitwiseBoolean {
+ get {
+ return (Oper & Operator.BitwiseMask) != 0 &&
+ ((left_unwrap != null && left_unwrap.Type == TypeManager.bool_type) ||
+ (right_unwrap != null && right_unwrap.Type == TypeManager.bool_type));
+ }
+ }
+
+ bool IsLeftNullLifted {
+ get {
+ return (state & State.LeftNullLifted) != 0;
+ }
+ }
+
+ bool IsRightNullLifted {
+ get {
+ return (state & State.RightNullLifted) != 0;
+ }
+ }
+
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
if (user_operator != null)
return user_operator.CreateExpressionTree (ec);
// with the null literal *outside* of a generics context and
// inlines that as true or false.
//
- Expression CreateNullConstant (Expression expr)
+ Constant CreateNullConstant (ResolveContext ec, Expression expr)
{
// FIXME: Handle side effect constants
- Constant c = new BoolConstant (Oper == Operator.Inequality, loc);
+ Constant c = new BoolConstant (Oper == Operator.Inequality, loc).Resolve (ec);
if ((Oper & Operator.EqualityMask) != 0) {
- Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
- expr.GetSignatureForError (), c.AsString ());
+ ec.Report.Warning (472, 2, loc, "The result of comparing value type `{0}' with null is `{1}'",
+ TypeManager.CSharpName (expr.Type), c.AsString ());
} else {
- Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
- expr.GetSignatureForError (), c.AsString ());
+ ec.Report.Warning (464, 2, loc, "The result of comparing type `{0}' with null is always `{1}'",
+ TypeManager.CSharpName (expr.Type), c.AsString ());
}
return ReducedExpression.Create (c, this);
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
- if (eclass != ExprClass.Invalid)
- return this;
-
if ((Oper & Operator.LogicalMask) != 0) {
- Error_OperatorCannotBeApplied (left, right);
+ Error_OperatorCannotBeApplied (ec, left, right);
return null;
}
+ bool use_default_call = (Oper & (Operator.BitwiseMask | Operator.EqualityMask)) != 0;
left_orig = left;
if (TypeManager.IsNullableType (left.Type)) {
- left = left_unwrap = Unwrap.Create (left, ec);
+ left = left_unwrap = Unwrap.Create (left, use_default_call);
if (left == null)
return null;
}
right_orig = right;
if (TypeManager.IsNullableType (right.Type)) {
- right = right_unwrap = Unwrap.Create (right, ec);
+ right = right_unwrap = Unwrap.Create (right, use_default_call);
if (right == null)
return null;
}
// Arguments can be lifted for equal operators when the return type is bool and both
// arguments are of same type
//
- if (left is NullLiteral) {
+ if (left_orig.IsNull) {
left = right;
- left_null_lifted = true;
+ state |= State.LeftNullLifted;
type = TypeManager.bool_type;
}
- if (right is NullLiteral) {
+ if (right_orig.IsNull) {
right = left;
- right_null_lifted = true;
+ state |= State.RightNullLifted;
type = TypeManager.bool_type;
}
void EmitBitwiseBoolean (EmitContext ec)
{
- ILGenerator ig = ec.ig;
+ Label load_left = ec.DefineLabel ();
+ Label load_right = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
- Label load_left = ig.DefineLabel ();
- Label load_right = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ // null & value, null | value
+ if (left_unwrap == null) {
+ left_unwrap = right_unwrap;
+ right_unwrap = null;
+ right = left;
+ }
- left_unwrap.EmitGetValueOrDefault (ec);
- ig.Emit (OpCodes.Brtrue_S, load_right);
+ left_unwrap.Emit (ec);
+ ec.Emit (OpCodes.Brtrue_S, load_right);
- right_unwrap.EmitGetValueOrDefault (ec);
- ig.Emit (OpCodes.Brtrue_S, load_left);
+ // value & null, value | null
+ if (right_unwrap != null) {
+ right_unwrap.Emit (ec);
+ ec.Emit (OpCodes.Brtrue_S, load_left);
+ }
left_unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse_S, load_right);
+ ec.Emit (OpCodes.Brfalse_S, load_right);
// load left
- ig.MarkLabel (load_left);
+ ec.MarkLabel (load_left);
if (Oper == Operator.BitwiseAnd) {
left_unwrap.Load (ec);
} else {
- right_unwrap.Load (ec);
- right_unwrap = left_unwrap;
+ if (right_unwrap == null) {
+ right.Emit (ec);
+ if (right is EmptyConstantCast)
+ ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
+ } else {
+ right_unwrap.Load (ec);
+ right_unwrap = left_unwrap;
+ }
}
- ig.Emit (OpCodes.Br_S, end_label);
+ ec.Emit (OpCodes.Br_S, end_label);
// load right
- ig.MarkLabel (load_right);
- right_unwrap.Load (ec);
+ ec.MarkLabel (load_right);
+ if (right_unwrap == null) {
+ if (Oper == Operator.BitwiseAnd) {
+ right.Emit (ec);
+ if (right is EmptyConstantCast)
+ ec.Emit (OpCodes.Newobj, NullableInfo.GetConstructor (type));
+ } else {
+ left_unwrap.Load (ec);
+ }
+ } else {
+ right_unwrap.Load (ec);
+ }
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
}
//
// Emits optimized equality or inequality operator when possible
//
- bool EmitEquality (EmitContext ec)
+ void EmitEquality (EmitContext ec)
{
- ILGenerator ig = ec.ig;
-
//
// Either left or right is null
//
- if (left_unwrap != null && (right_null_lifted || right.IsNull)) {
+ if (left_unwrap != null && (IsRightNullLifted || right.IsNull)) {
left_unwrap.EmitCheck (ec);
if (Oper == Binary.Operator.Equality) {
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
+ ec.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ceq);
}
- return true;
+ return;
}
- if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
+ if (right_unwrap != null && (IsLeftNullLifted || left.IsNull)) {
right_unwrap.EmitCheck (ec);
if (Oper == Binary.Operator.Equality) {
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
+ ec.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ceq);
}
- return true;
+ return;
}
- if (user_operator != null)
- return false;
-
- if (left is UserCast || right is UserCast)
- return false;
-
- Label dissimilar_label = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ Label dissimilar_label = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
- if (left_unwrap != null)
- left_unwrap.EmitGetValueOrDefault (ec);
- else
+ if (user_operator != null) {
+ user_operator.Emit (ec);
+ ec.Emit (Oper == Operator.Equality ? OpCodes.Brfalse_S : OpCodes.Brtrue_S, dissimilar_label);
+ } else {
left.Emit (ec);
-
- if (right_unwrap != null)
- right_unwrap.EmitGetValueOrDefault (ec);
- else
right.Emit (ec);
- ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
+ ec.Emit (OpCodes.Bne_Un_S, dissimilar_label);
+ }
if (left_unwrap != null)
left_unwrap.EmitCheck (ec);
+
if (right_unwrap != null)
right_unwrap.EmitCheck (ec);
if (left_unwrap != null && right_unwrap != null) {
if (Oper == Operator.Inequality)
- ig.Emit (OpCodes.Xor);
+ ec.Emit (OpCodes.Xor);
else
- ig.Emit (OpCodes.Ceq);
+ ec.Emit (OpCodes.Ceq);
} else {
if (Oper == Operator.Inequality) {
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Ceq);
+ ec.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ceq);
}
}
- ig.Emit (OpCodes.Br_S, end_label);
+ ec.Emit (OpCodes.Br_S, end_label);
- ig.MarkLabel (dissimilar_label);
+ ec.MarkLabel (dissimilar_label);
if (Oper == Operator.Inequality)
- ig.Emit (OpCodes.Ldc_I4_1);
+ ec.Emit (OpCodes.Ldc_I4_1);
else
- ig.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ldc_I4_0);
- ig.MarkLabel (end_label);
- return true;
+ ec.MarkLabel (end_label);
}
public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
{
Emit (ec);
- ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
+ ec.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
}
public override void Emit (EmitContext ec)
}
if ((Oper & Operator.EqualityMask) != 0) {
- if (EmitEquality (ec))
- return;
+ EmitEquality (ec);
+ return;
}
- ILGenerator ig = ec.ig;
-
- Label is_null_label = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ Label is_null_label = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
if (left_unwrap != null) {
left_unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
}
//
//
if (right_unwrap != null && !left.Equals (right)) {
right_unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
}
EmitOperator (ec, left.Type);
if (wrap_ctor != null)
- ig.Emit (OpCodes.Newobj, wrap_ctor);
+ ec.Emit (OpCodes.Newobj, wrap_ctor);
- ig.Emit (OpCodes.Br_S, end_label);
- ig.MarkLabel (is_null_label);
+ ec.Emit (OpCodes.Br_S, end_label);
+ ec.MarkLabel (is_null_label);
if ((Oper & Operator.ComparisonMask) != 0) {
- //
- // Emit true when equality operator both operands are null
- // or inequality operator operands has only one null
- //
- if ((Oper == Operator.Equality && left_unwrap != null && right_unwrap != null) ||
- (Oper == Operator.Inequality && left_unwrap != right_unwrap))
- ig.Emit (OpCodes.Ldc_I4_1);
- else
- ig.Emit (OpCodes.Ldc_I4_0);
+ ec.Emit (OpCodes.Ldc_I4_0);
} else {
LiftedNull.Create (type, loc).Emit (ec);
}
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
}
- protected override void EmitOperator (EmitContext ec, Type l)
+ protected override void EmitOperator (EmitContext ec, TypeSpec l)
{
if (user_operator != null) {
user_operator.Emit (ec);
base.EmitOperator (ec, l);
}
- bool IsBitwiseBoolean {
- get {
- return (Oper & Operator.BitwiseMask) != 0 && left_unwrap != null && right_unwrap != null &&
- left_unwrap.Type == TypeManager.bool_type && right_unwrap.Type == TypeManager.bool_type;
- }
- }
-
- Expression LiftResult (EmitContext ec, Expression res_expr)
+ Expression LiftResult (ResolveContext ec, Expression res_expr)
{
TypeExpr lifted_type;
//
// Avoid double conversion
//
- if (left_unwrap == null || left_null_lifted || !TypeManager.IsEqual (left_unwrap.Type, left.Type) || (left_unwrap != null && right_null_lifted)) {
+ if (left_unwrap == null || IsLeftNullLifted || left_unwrap.Type != left.Type || (left_unwrap != null && IsRightNullLifted)) {
lifted_type = new NullableType (left.Type, loc);
lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
if (lifted_type == null)
left = EmptyCast.Create (left, lifted_type.Type);
}
- if (right_unwrap == null || right_null_lifted || !TypeManager.IsEqual (right_unwrap.Type, right.Type) || (right_unwrap != null && left_null_lifted)) {
+ if (left != right && (right_unwrap == null || IsRightNullLifted || right_unwrap.Type != right.Type || (right_unwrap != null && IsLeftNullLifted))) {
lifted_type = new NullableType (right.Type, loc);
lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
if (lifted_type == null)
if (lifted_type == null)
return null;
- wrap_ctor = new NullableInfo (lifted_type.Type).Constructor;
+ wrap_ctor = NullableInfo.GetConstructor (lifted_type.Type);
type = res_expr.Type = lifted_type.Type;
}
- if (left_null_lifted) {
+ if (IsLeftNullLifted) {
left = LiftedNull.Create (right.Type, left.Location);
- if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
- return LiftedNull.CreateFromExpression (res_expr);
+ //
+ // 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) == TypeManager.bool_type) {
+ 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 (right_orig).Resolve (ec);
+ return CreateNullConstant (ec, right_orig).Resolve (ec);
}
- if (right_null_lifted) {
+ if (IsRightNullLifted) {
right = LiftedNull.Create (left.Type, right.Location);
- if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
- return LiftedNull.CreateFromExpression (res_expr);
+ //
+ // 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) == TypeManager.bool_type) {
+ 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 (left_orig).Resolve (ec);
+ return CreateNullConstant (ec, left_orig);
}
return res_expr;
}
- protected override Expression ResolveOperatorPredefined (EmitContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, Type enum_type)
+ protected override Expression ResolveOperatorPredefined (ResolveContext ec, Binary.PredefinedOperator [] operators, bool primitives_only, TypeSpec enum_type)
{
Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
// (in unlifted or lifted form) exists for the operation.
//
if (e == null && (Oper & Operator.EqualityMask) != 0) {
- if ((left_null_lifted && right_unwrap != null) || (right_null_lifted && left_unwrap != null))
+ if ((IsLeftNullLifted && right_unwrap != null) || (IsRightNullLifted && left_unwrap != null))
return LiftResult (ec, this);
}
return e;
}
- protected override Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
+ protected override Expression ResolveUserOperator (ResolveContext ec, Expression left, Expression right)
{
- Expression expr = base.ResolveUserOperator (ec, l, r);
+ State orig_state = state;
+
+ // Try original types first
+ Expression expr = base.ResolveUserOperator (ec, left_orig, right_orig);
+ if (expr == null) {
+ if (left_unwrap != null || right_unwrap != null) {
+ // One side is nullable type, try underlying types
+ expr = base.ResolveUserOperator (ec, left, right);
+ } else if ((state & (State.RightNullLifted | State.LeftNullLifted)) != 0) {
+ expr = base.ResolveUserOperator (ec, left, right);
+ }
+ }
+
if (expr == null)
return null;
+ if (state != orig_state)
+ return expr;
+
expr = LiftResult (ec, expr);
if (expr is Constant)
return expr;
this.loc = loc;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
if (left is NullLiteral)
- Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
+ ec.Report.Error (845, loc, "An expression tree cannot contain a coalescing operator with null left side");
UserCast uc = left as UserCast;
Expression conversion = null;
if (uc != null) {
left = uc.Source;
- ArrayList c_args = new ArrayList (2);
+ Arguments c_args = new Arguments (2);
c_args.Add (new Argument (uc.CreateExpressionTree (ec)));
c_args.Add (new Argument (left.CreateExpressionTree (ec)));
- conversion = CreateExpressionFactoryCall ("Lambda", c_args);
+ conversion = CreateExpressionFactoryCall (ec, "Lambda", c_args);
}
- ArrayList args = new ArrayList (3);
+ Arguments args = new Arguments (3);
args.Add (new Argument (left.CreateExpressionTree (ec)));
args.Add (new Argument (right.CreateExpressionTree (ec)));
if (conversion != null)
args.Add (new Argument (conversion));
- return CreateExpressionFactoryCall ("Coalesce", args);
+ return CreateExpressionFactoryCall (ec, "Coalesce", args);
}
- Expression ConvertExpression (EmitContext ec)
+ Expression ConvertExpression (ResolveContext ec)
{
// TODO: ImplicitConversionExists should take care of this
if (left.eclass == ExprClass.MethodGroup)
return null;
- Type ltype = left.Type;
+ TypeSpec ltype = left.Type;
//
// If left is a nullable type and an implicit conversion exists from right to underlying type of left,
// the result is underlying type of left
//
if (TypeManager.IsNullableType (ltype)) {
- unwrap = Unwrap.Create (left, ec);
+ unwrap = Unwrap.Create (left, false);
if (unwrap == null)
return null;
// Reduce (left ?? null) to left OR (null-constant ?? right) to right
//
if (right.IsNull || lc != null)
- return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
+ return ReducedExpression.Create (lc != null ? right : left, this);
right = Convert.ImplicitConversion (ec, right, ltype, loc);
type = left.Type;
return null;
}
- Type rtype = right.Type;
+ TypeSpec rtype = right.Type;
if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
return null;
// Reduce (null ?? right) to right
//
if (left.IsNull)
- return ReducedExpression.Create (right, this).Resolve (ec);
+ return ReducedExpression.Create (right, this);
left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
type = rtype;
return this;
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
- if (eclass != ExprClass.Invalid)
- return this;
-
left = left.Resolve (ec);
right = right.Resolve (ec);
Expression e = ConvertExpression (ec);
if (e == null) {
- Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
+ Binary.Error_OperatorCannotBeApplied (ec, left, right, "??", loc);
return null;
}
public override void Emit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
-
- Label end_label = ig.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
if (unwrap != null) {
- Label is_null_label = ig.DefineLabel ();
+ Label is_null_label = ec.DefineLabel ();
unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
left.Emit (ec);
- ig.Emit (OpCodes.Br, end_label);
+ ec.Emit (OpCodes.Br, end_label);
- ig.MarkLabel (is_null_label);
+ ec.MarkLabel (is_null_label);
right.Emit (ec);
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
return;
}
left.Emit (ec);
+ ec.Emit (OpCodes.Dup);
- ig.Emit (OpCodes.Dup);
- ig.Emit (OpCodes.Brtrue, end_label);
+ // Only to make verifier happy
+ if (left.Type.IsGenericParameter)
+ ec.Emit (OpCodes.Box, left.Type);
- ig.Emit (OpCodes.Pop);
- right.Emit (ec);
+ ec.Emit (OpCodes.Brtrue, end_label);
- ig.MarkLabel (end_label);
- }
+ ec.Emit (OpCodes.Pop);
+ right.Emit (ec);
- public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
- {
- left.MutateHoistedGenericType (storey);
- right.MutateHoistedGenericType (storey);
- type = storey.MutateType (type);
+ ec.MarkLabel (end_label);
}
protected override void CloneTo (CloneContext clonectx, Expression t)
this.expr = expr;
this.Mode = mode;
this.loc = loc;
-
- eclass = ExprClass.Value;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
return new SimpleAssign (this, this).CreateExpressionTree (ec);
}
- public override Expression DoResolve (EmitContext ec)
+ protected override Expression DoResolve (ResolveContext ec)
{
expr = expr.Resolve (ec);
if (expr == null)
return null;
- unwrap = Unwrap.Create (expr, ec);
+ unwrap = Unwrap.Create (expr, false);
if (unwrap == null)
return null;
- underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap).Resolve (ec);
+ underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap, loc).Resolve (ec);
if (underlying == null)
return null;
+
+ eclass = ExprClass.Value;
type = expr.Type;
return this;
}
void DoEmit (EmitContext ec, bool is_expr)
{
- ILGenerator ig = ec.ig;
- Label is_null_label = ig.DefineLabel ();
- Label end_label = ig.DefineLabel ();
+ Label is_null_label = ec.DefineLabel ();
+ Label end_label = ec.DefineLabel ();
unwrap.EmitCheck (ec);
- ig.Emit (OpCodes.Brfalse, is_null_label);
+ ec.Emit (OpCodes.Brfalse, is_null_label);
if (is_expr) {
underlying.Emit (ec);
- ig.Emit (OpCodes.Br_S, end_label);
+ ec.Emit (OpCodes.Br_S, end_label);
} else {
underlying.EmitStatement (ec);
}
- ig.MarkLabel (is_null_label);
+ ec.MarkLabel (is_null_label);
if (is_expr)
LiftedNull.Create (type, loc).Emit (ec);
- ig.MarkLabel (end_label);
+ ec.MarkLabel (end_label);
}
public override void Emit (EmitContext ec)