using System;
using System.Reflection;
using System.Reflection.Emit;
-using System.Collections;
namespace Mono.CSharp.Nullable
{
public class NullableType : TypeExpr
{
- Expression underlying;
+ TypeExpr underlying;
- public NullableType (Expression underlying, Location l)
+ public NullableType (TypeExpr underlying, Location l)
{
this.underlying = underlying;
loc = l;
: this (new TypeExpression (type, loc), loc)
{ }
- protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+ protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
{
- TypeArguments args = new TypeArguments (loc);
- args.Add (underlying);
-
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);
}
- ConstructedType ctype = new ConstructedType (TypeManager.generic_nullable_type, args, loc);
+ TypeArguments args = new TypeArguments (underlying);
+ GenericTypeExpr ctype = new GenericTypeExpr (TypeManager.generic_nullable_type, args, loc);
return ctype.ResolveAsTypeTerminal (ec, false);
}
+
+ public override TypeExpr ResolveAsTypeTerminal (IMemberContext ec, bool silent)
+ {
+ return ResolveAsBaseTerminal (ec, silent);
+ }
}
public sealed class NullableInfo
{
public readonly Type Type;
public readonly Type UnderlyingType;
- public readonly MethodInfo HasValue;
- public readonly MethodInfo Value;
- public readonly MethodInfo GetValueOrDefault;
- public readonly ConstructorInfo Constructor;
+ public MethodSpec HasValue;
+ public MethodSpec Value;
+ public MethodSpec GetValueOrDefault;
+ public MethodSpec Constructor;
public NullableInfo (Type type)
{
Type = type;
- UnderlyingType = TypeManager.GetTypeArguments (type) [0];
+ UnderlyingType = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0]);
- PropertyInfo has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null);
- PropertyInfo value_pi = TypeManager.GetPredefinedProperty (type, "Value", Location.Null);
+ var has_value_pi = TypeManager.GetPredefinedProperty (type, "HasValue", Location.Null, Type.EmptyTypes);
+ var 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 ()));
+ HasValue = Import.CreateMethod (has_value_pi.MetaInfo.GetGetMethod (false));
+ Value = Import.CreateMethod (value_pi.MetaInfo.GetGetMethod (false));
+
+ // When compiling corlib
+ if (TypeManager.IsBeingCompiled (type)) {
+ TypeContainer tc = TypeManager.LookupGenericTypeContainer (type);
+
+ // TODO: check for correct overload
+ Constructor c = ((Constructor) tc.InstanceConstructors [0]);
+
+ Constructor = Import.CreateMethod (TypeBuilder.GetConstructor (type, c.ConstructorBuilder));
return;
}
+
+#if MS_COMPATIBLE
+// if (TypeManager.IsBeingCompiled (UnderlyingType)) {
+// ConstructorInfo cinfo = TypeManager.DropGenericTypeArguments (type).GetConstructors ()[0];
+// Constructor = TypeBuilder.GetConstructor (type, cinfo);
+// return;
+// }
#endif
- Constructor = type.GetConstructor (new Type[] { UnderlyingType });
+
+ Constructor = Import.CreateMethod (type.GetConstructor (new Type[] { UnderlyingType }));
}
}
NullableInfo info;
LocalTemporary temp;
- bool has_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;
+ eclass = expr.eclass;
}
- public static Unwrap Create (Expression expr, EmitContext ec)
+ public static Expression Create (Expression expr)
{
- return new Unwrap (expr).Resolve (ec) as Unwrap;
+ //
+ // Avoid unwraping and wraping of same type
+ //
+ Wrap wrap = expr as Wrap;
+ if (wrap != null)
+ return wrap.Child;
+
+ return Create (expr, false);
+ }
+
+ public static Unwrap Create (Expression expr, bool useDefaultValue)
+ {
+ 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)
{
- if (expr == null)
- return null;
-
- temp = new LocalTemporary (expr.Type);
-
- info = new NullableInfo (expr.Type);
- type = info.UnderlyingType;
- eclass = expr.eclass;
return this;
}
-
- public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+
+ public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
{
return DoResolve (ec);
- }
+ }
public override void Emit (EmitContext ec)
{
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.Value, null);
+ Store (ec);
+ if (useDefaultValue)
+ Invocation.EmitCall (ec, false, this, info.GetValueOrDefault, null, loc);
+ else
+ Invocation.EmitCall (ec, false, this, info.Value, null, loc);
}
public void EmitCheck (EmitContext ec)
{
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.HasValue, null);
- }
-
- public void EmitGetValueOrDefault (EmitContext ec)
- {
- AddressOf (ec, AddressOp.LoadStore);
- ec.ig.EmitCall (OpCodes.Call, info.GetValueOrDefault, null);
+ Store (ec);
+ Invocation.EmitCall (ec, false, this, info.HasValue, null, loc);
}
public override bool Equals (object obj)
}
}
- public void Store (EmitContext ec)
+ void Store (EmitContext ec)
{
- create_temp (ec);
+ if (expr is VariableReference)
+ return;
+
+ if (temp != null)
+ return;
+
+ expr.Emit (ec);
+ LocalVariable.Store (ec);
}
- void create_temp (EmitContext ec)
+ public void Load (EmitContext ec)
{
- if ((temp != null) && !has_temp) {
+ if (expr is VariableReference)
expr.Emit (ec);
- temp.Store (ec);
- has_temp = true;
- }
+ else
+ LocalVariable.Emit (ec);
}
- public void LoadTemporary (EmitContext ec)
+ public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
{
- temp.Emit (ec);
+ return expr.MakeExpression (ctx);
+ }
+
+ public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+ {
+ type = storey.MutateType (type);
+ storey.MutateConstructor (info.Constructor);
+ storey.MutateGenericMethod (info.HasValue);
+ storey.MutateGenericMethod (info.GetValueOrDefault);
+ storey.MutateGenericMethod (info.Value);
}
public void AddressOf (EmitContext ec, AddressOp mode)
{
- create_temp (ec);
- if (temp != null)
- temp.AddressOf (ec, AddressOp.LoadStore);
+ IMemoryLocation ml = expr as VariableReference;
+ if (ml != null)
+ ml.AddressOf (ec, mode);
else
- ((IMemoryLocation) expr).AddressOf (ec, AddressOp.LoadStore);
+ LocalVariable.AddressOf (ec, mode);
+ }
+
+ //
+ // Keeps result of non-variable expression
+ //
+ LocalTemporary LocalVariable {
+ get {
+ if (temp == null)
+ temp = new LocalTemporary (info.Type);
+ return temp;
+ }
}
public void Emit (EmitContext ec, bool leave_copy)
{
- create_temp (ec);
- if (leave_copy) {
- if (temp != null)
- temp.Emit (ec);
- else
- expr.Emit (ec);
- }
+ if (leave_copy)
+ Load (ec);
Emit (ec);
}
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.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
}
}
}
eclass = ExprClass.Value;
}
+ public Expression Child {
+ get { return child; }
+ }
+
+ public override Expression CreateExpressionTree (ResolveContext ec)
+ {
+ TypeCast child_cast = child as TypeCast;
+ if (child_cast != null) {
+ child.Type = type;
+ return child_cast.CreateExpressionTree (ec);
+ }
+
+ return base.CreateExpressionTree (ec);
+ }
+
public static Expression Create (Expression expr, Type type)
{
+ //
+ // Avoid unwraping and wraping of the same type
+ //
+ Unwrap unwrap = expr as Unwrap;
+ if (unwrap != null && TypeManager.IsEqual (expr.Type, TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (type) [0])))
+ 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.ig.Emit (OpCodes.Newobj, (ConstructorInfo) info.Constructor.MetaInfo);
}
}
//
// 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)
+ : base (nullable_type, loc)
{
eclass = ExprClass.Value;
}
return new LiftedNull (nullable, loc);
}
- public static Expression CreateFromExpression (Expression e)
+ public static Expression 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
}
}
- public abstract class Lifted : Expression, IMemoryLocation
+ //
+ // Generic lifting expression, supports all S/S? -> T/T? cases
+ //
+ public class Lifted : Expression, IMemoryLocation
{
- Expression expr, underlying, wrap, null_value;
+ Expression expr, null_value;
Unwrap unwrap;
- protected Lifted (Expression expr, Location loc)
+ public Lifted (Expression expr, Unwrap unwrap, Type type)
{
this.expr = expr;
- this.loc = loc;
+ this.unwrap = unwrap;
+ this.loc = expr.Location;
+ this.type = type;
+ }
+
+ public Lifted (Expression expr, Expression unwrap, Type type)
+ : this (expr, unwrap as Unwrap, type)
+ {
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
- ArrayList args = new ArrayList (2);
- args.Add (new Argument (expr.CreateExpressionTree (ec)));
- args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
- return CreateExpressionFactoryCall ("Convert", args);
+ return expr.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);
- if (unwrap == null)
- return null;
-
- underlying = ResolveUnderlying (unwrap, ec);
- if (underlying == null)
- return null;
+ //
+ // It's null when lifting non-nullable type
+ //
+ if (unwrap == null) {
+ // S -> T? is wrap only
+ if (TypeManager.IsNullableType (type))
+ return Wrap.Create (expr, type);
- TypeExpr target_type = new NullableType (underlying.Type, loc);
- target_type = target_type.ResolveAsTypeTerminal (ec, false);
- if (target_type == null)
- return null;
+ // S -> T can be simplified
+ return expr;
+ }
- wrap = Wrap.Create (underlying, target_type.Type);
- if (wrap == null)
- return null;
+ // Wrap target for T?
+ if (TypeManager.IsNullableType (type)) {
+ expr = Wrap.Create (expr, type);
+ if (expr == null)
+ return null;
- null_value = LiftedNull.Create (wrap.Type, loc);
+ null_value = LiftedNull.Create (type, loc);
+ } else {
+ null_value = new NullConstant (type, loc);
+ }
- type = wrap.Type;
eclass = ExprClass.Value;
return this;
}
- protected abstract Expression ResolveUnderlying (Expression unwrap, EmitContext ec);
-
public override void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
unwrap.EmitCheck (ec);
ig.Emit (OpCodes.Brfalse, is_null_label);
- wrap.Emit (ec);
- ig.Emit (OpCodes.Br, end_label);
+ expr.Emit (ec);
+ ig.Emit (OpCodes.Br, end_label);
ig.MarkLabel (is_null_label);
- null_value.Emit (ec);
+ null_value.Emit (ec);
ig.MarkLabel (end_label);
}
}
}
- public class LiftedConversion : Lifted
- {
- public readonly bool IsUser;
- public readonly bool IsExplicit;
- public readonly Type TargetType;
-
- public LiftedConversion (Expression expr, Type target_type, bool is_user,
- bool is_explicit, Location loc)
- : base (expr, loc)
- {
- this.IsUser = is_user;
- this.IsExplicit = is_explicit;
- this.TargetType = target_type;
- }
-
- protected override Expression ResolveUnderlying (Expression unwrap, EmitContext ec)
- {
- Type type = TypeManager.GetTypeArguments (TargetType) [0];
-
- if (IsUser) {
- if (IsExplicit)
- return Convert.ExplicitUserConversion (ec, unwrap, type, loc);
- else
- return Convert.ImplicitUserConversion (ec, unwrap, type, loc);
- } else {
- if (IsExplicit)
- return Convert.ExplicitConversion (ec, unwrap, type, loc);
- else
- return Convert.ImplicitConversion (ec, unwrap, type, loc);
- }
- }
- }
-
public class LiftedUnaryOperator : Unary, IMemoryLocation
{
Unwrap unwrap;
Expression user_operator;
- public LiftedUnaryOperator (Unary.Operator op, Expression expr, Location loc)
- : base (op, expr, loc)
+ public LiftedUnaryOperator (Unary.Operator op, Expression expr)
+ : base (op, expr)
{
}
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;
EmitOperator (ec, ni.UnderlyingType);
}
- ig.Emit (OpCodes.Newobj, ni.Constructor);
+ ig.Emit (OpCodes.Newobj, (ConstructorInfo) ni.Constructor.MetaInfo);
ig.Emit (OpCodes.Br_S, end_label);
ig.MarkLabel (is_null_label);
ig.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)
return null;
- user_operator = LiftExpression (ec, expr);
- return user_operator;
+ //
+ // When a user operator is of non-nullable type
+ //
+ if (Expr is Unwrap) {
+ user_operator = LiftExpression (ec, expr);
+ return user_operator;
+ }
+
+ return expr;
}
}
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)
this.loc = loc;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ 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)
+ Expression 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 `{0}' against null is always `{1}'. " +
- "This operation is undocumented and it is temporary supported for compatibility reasons only",
- 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}' against 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;
type = TypeManager.bool_type;
}
- if (right is NullLiteral) {
+ if (right_orig.IsNull) {
right = left;
right_null_lifted = true;
type = TypeManager.bool_type;
Label load_right = ig.DefineLabel ();
Label end_label = ig.DefineLabel ();
- left_unwrap.EmitGetValueOrDefault (ec);
+ left_unwrap.Emit (ec);
ig.Emit (OpCodes.Brtrue_S, load_right);
- right_unwrap.EmitGetValueOrDefault (ec);
+ right_unwrap.Emit (ec);
ig.Emit (OpCodes.Brtrue_S, load_left);
left_unwrap.EmitCheck (ec);
ig.MarkLabel (load_left);
if (Oper == Operator.BitwiseAnd) {
- left_unwrap.LoadTemporary (ec);
+ left_unwrap.Load (ec);
} else {
- right_unwrap.LoadTemporary (ec);
+ right_unwrap.Load (ec);
right_unwrap = left_unwrap;
}
ig.Emit (OpCodes.Br_S, end_label);
// load right
ig.MarkLabel (load_right);
- right_unwrap.LoadTemporary (ec);
+ right_unwrap.Load (ec);
ig.MarkLabel (end_label);
}
//
// Emits optimized equality or inequality operator when possible
//
- bool EmitEquality (EmitContext ec)
+ void EmitEquality (EmitContext ec)
{
ILGenerator ig = ec.ig;
ig.Emit (OpCodes.Ldc_I4_0);
ig.Emit (OpCodes.Ceq);
}
- return true;
+ return;
}
if (right_unwrap != null && (left_null_lifted || left.IsNull)) {
ig.Emit (OpCodes.Ldc_I4_0);
ig.Emit (OpCodes.Ceq);
}
- return true;
+ return;
}
- if (user_operator != null)
- return false;
-
Label dissimilar_label = ig.DefineLabel ();
Label end_label = ig.DefineLabel ();
- if (left_unwrap != null)
- left_unwrap.EmitGetValueOrDefault (ec);
- else
+ if (user_operator != null) {
+ user_operator.Emit (ec);
+ ig.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);
+ ig.Emit (OpCodes.Bne_Un_S, dissimilar_label);
+ }
if (left_unwrap != null)
left_unwrap.EmitCheck (ec);
+
if (right_unwrap != null)
right_unwrap.EmitCheck (ec);
ig.Emit (OpCodes.Ldc_I4_0);
ig.MarkLabel (end_label);
- return true;
}
public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
public override void Emit (EmitContext ec)
{
- if (left_unwrap != null)
- left_unwrap.Store (ec);
-
- if (right_unwrap != null) {
- if (right.Equals (left))
- right_unwrap = left_unwrap;
- else
- right_unwrap.Store (ec);
- }
+ //
+ // Optimize same expression operation
+ //
+ if (right_unwrap != null && right.Equals (left))
+ right_unwrap = left_unwrap;
if (user_operator == null && IsBitwiseBoolean) {
EmitBitwiseBoolean (ec);
}
if ((Oper & Operator.EqualityMask) != 0) {
- if (EmitEquality (ec))
- return;
+ EmitEquality (ec);
+ return;
}
ILGenerator ig = ec.ig;
EmitOperator (ec, left.Type);
if (wrap_ctor != null)
- ig.Emit (OpCodes.Newobj, wrap_ctor);
+ ig.Emit (OpCodes.Newobj, (ConstructorInfo) wrap_ctor.MetaInfo);
ig.Emit (OpCodes.Br_S, end_label);
ig.MarkLabel (is_null_label);
if ((Oper & Operator.ComparisonMask) != 0) {
- if (Oper == Operator.Equality)
- ig.Emit (OpCodes.Ldc_I4_1);
- else
- ig.Emit (OpCodes.Ldc_I4_0);
+ ig.Emit (OpCodes.Ldc_I4_0);
} else {
LiftedNull.Create (type, loc).Emit (ec);
}
}
if (TypeManager.IsNullableType (l))
- l = TypeManager.GetTypeArguments (l) [0];
+ l = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (l) [0]);
base.EmitOperator (ec, l);
}
}
}
- 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)) {
+ if (left_unwrap == null || left_null_lifted || !TypeManager.IsEqual (left_unwrap.Type, left.Type) || (left_unwrap != null && right_null_lifted)) {
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)) {
+ if (right_unwrap == null || right_null_lifted || !TypeManager.IsEqual (right_unwrap.Type, right.Type) || (right_unwrap != null && left_null_lifted)) {
lifted_type = new NullableType (right.Type, loc);
lifted_type = lifted_type.ResolveAsTypeTerminal (ec, false);
if (lifted_type == null)
if (left_null_lifted) {
left = LiftedNull.Create (right.Type, left.Location);
- if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
- return LiftedNull.CreateFromExpression (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) {
right = LiftedNull.Create (left.Type, right.Location);
- if ((Oper & (Operator.ArithmeticMask | Operator.ShiftMask)) != 0)
- return LiftedNull.CreateFromExpression (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, Type enum_type)
{
Expression e = base.ResolveOperatorPredefined (ec, operators, primitives_only, enum_type);
return e;
}
- protected override Expression ResolveUserOperator (EmitContext ec, Type l, Type r)
+ protected override Expression ResolveUserOperator (ResolveContext ec, Type l, Type r)
{
Expression expr = base.ResolveUserOperator (ec, l, r);
if (expr == null)
this.loc = loc;
}
- public override Expression CreateExpressionTree (EmitContext ec)
+ public override Expression CreateExpressionTree (ResolveContext ec)
{
+ if (left.Type == TypeManager.null_type)
+ 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);
+ }
- public override Expression DoResolve (EmitContext ec)
+ Expression ConvertExpression (ResolveContext ec)
{
- if (type != null)
- return this;
-
- left = left.Resolve (ec);
- right = right.Resolve (ec);
-
- if (left == null || right == null)
+ // TODO: ImplicitConversionExists should take care of this
+ if (left.eclass == ExprClass.MethodGroup)
return null;
- eclass = ExprClass.Value;
- Type ltype = left.Type, rtype = right.Type;
- Expression expr;
+ Type 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)) {
- NullableInfo info = new NullableInfo (ltype);
-
- unwrap = Unwrap.Create (left, ec);
+ unwrap = Unwrap.Create (left, false);
if (unwrap == null)
return null;
- expr = Convert.ImplicitConversion (ec, right, info.UnderlyingType, loc);
- if (expr != null) {
+ if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
left = unwrap;
- right = expr;
- type = expr.Type;
+ type = left.Type;
+ right = Convert.ImplicitConversion (ec, right, type, loc);
+ return this;
+ }
+ } else if (TypeManager.IsReferenceType (ltype)) {
+ if (Convert.ImplicitConversionExists (ec, right, ltype)) {
+ //
+ // Reduce (constant ?? expr) to constant
+ //
+ Constant lc = left as Constant;
+ if (lc != null && !lc.IsDefaultValue)
+ return new SideEffectConstant (lc, right, loc).Resolve (ec);
+
+ //
+ // Reduce (left ?? null) to left OR (null-constant ?? right) to right
+ //
+ if (right.IsNull || lc != null)
+ return ReducedExpression.Create (lc != null ? right : left, this);
+
+ right = Convert.ImplicitConversion (ec, right, ltype, loc);
+ type = left.Type;
return this;
}
- } else if (!TypeManager.IsReferenceType (ltype)) {
- Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
+ } else {
return null;
}
- expr = Convert.ImplicitConversion (ec, right, ltype, loc);
- if (expr != null) {
- type = expr.Type;
- right = expr;
- return this;
- }
+ Type rtype = right.Type;
+ if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype) || right.eclass == ExprClass.MethodGroup)
+ return null;
- Expression left_null = unwrap != null ? unwrap : left;
- expr = Convert.ImplicitConversion (ec, left_null, rtype, loc);
- if (expr != null) {
- left = expr;
- type = rtype;
- return this;
+ //
+ // Reduce (null ?? right) to right
+ //
+ if (left.IsNull)
+ return ReducedExpression.Create (right, this);
+
+ left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
+ type = rtype;
+ return this;
+ }
+
+ protected override Expression DoResolve (ResolveContext ec)
+ {
+ left = left.Resolve (ec);
+ right = right.Resolve (ec);
+
+ if (left == null || right == null)
+ return null;
+
+ eclass = ExprClass.Value;
+
+ Expression e = ConvertExpression (ec);
+ if (e == null) {
+ Binary.Error_OperatorCannotBeApplied (ec, left, right, "??", loc);
+ return null;
}
- Binary.Error_OperatorCannotBeApplied (loc, "??", ltype, rtype);
- return null;
+ return e;
}
public override void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
- Label is_null_label = ig.DefineLabel ();
Label end_label = ig.DefineLabel ();
if (unwrap != null) {
+ Label is_null_label = ig.DefineLabel ();
+
unwrap.EmitCheck (ec);
ig.Emit (OpCodes.Brfalse, is_null_label);
right.Emit (ec);
ig.MarkLabel (end_label);
- } else {
- left.Emit (ec);
- ig.Emit (OpCodes.Dup);
- ig.Emit (OpCodes.Brtrue, end_label);
+ return;
+ }
- ig.MarkLabel (is_null_label);
+ left.Emit (ec);
- ig.Emit (OpCodes.Pop);
- right.Emit (ec);
+ ig.Emit (OpCodes.Dup);
+ ig.Emit (OpCodes.Brtrue, end_label);
- ig.MarkLabel (end_label);
- }
+ ig.Emit (OpCodes.Pop);
+ right.Emit (ec);
+
+ ig.MarkLabel (end_label);
+ }
+
+ public override void MutateHoistedGenericType (AnonymousMethodStorey storey)
+ {
+ left.MutateHoistedGenericType (storey);
+ right.MutateHoistedGenericType (storey);
+ type = storey.MutateType (type);
}
+
protected override void CloneTo (CloneContext clonectx, Expression t)
{
NullCoalescingOperator target = (NullCoalescingOperator) t;
public class LiftedUnaryMutator : ExpressionStatement
{
public readonly UnaryMutator.Mode Mode;
- Expression expr, null_value;
+ Expression expr;
UnaryMutator underlying;
Unwrap unwrap;
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, loc).Resolve (ec);
+ underlying = (UnaryMutator) new UnaryMutator (Mode, unwrap).Resolve (ec);
if (underlying == null)
return null;
+
+ eclass = ExprClass.Value;
type = expr.Type;
- null_value = LiftedNull.Create (type, loc);
return this;
}
unwrap.EmitCheck (ec);
ig.Emit (OpCodes.Brfalse, is_null_label);
- if (is_expr)
+ if (is_expr) {
underlying.Emit (ec);
- else
+ ig.Emit (OpCodes.Br_S, end_label);
+ } else {
underlying.EmitStatement (ec);
- ig.Emit (OpCodes.Br, end_label);
+ }
ig.MarkLabel (is_null_label);
if (is_expr)
- null_value.Emit (ec);
+ LiftedNull.Create (type, loc).Emit (ec);
ig.MarkLabel (end_label);
}