NullableInfo info;
LocalTemporary temp;
- bool has_temp;
protected Unwrap (Expression expr)
{
if (expr == null)
return null;
- temp = new LocalTemporary (expr.Type);
-
info = new NullableInfo (expr.Type);
type = info.UnderlyingType;
eclass = expr.eclass;
public override void Emit (EmitContext ec)
{
+ Store (ec);
AddressOf (ec, AddressOp.LoadStore);
ec.ig.EmitCall (OpCodes.Call, info.Value, null);
}
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);
}
}
}
- public void Store (EmitContext ec)
+ void Store (EmitContext ec)
{
- create_temp (ec);
- }
+ if (expr is VariableReference)
+ return;
- void create_temp (EmitContext ec)
- {
- if ((temp != null) && !has_temp) {
- expr.Emit (ec);
- temp.Store (ec);
- has_temp = true;
- }
+ if (temp != null)
+ return;
+
+ expr.Emit (ec);
+ LocalVariable.Store (ec);
}
- public void LoadTemporary (EmitContext ec)
+ public void Load (EmitContext ec)
{
- temp.Emit (ec);
+ if (expr is VariableReference)
+ expr.Emit (ec);
+ else
+ LocalVariable.Emit (ec);
}
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);
}
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.GetTypeArguments (type) [0]))
+ return unwrap.Original;
+
return new Wrap (expr, type);
}
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;
}
}
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);
}
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);
Type ltype = left.Type, rtype = right.Type;
//
- // If left is a nullable type and an implicit conversion exists from right to left,
- // the result type is left
+ // 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) && left.eclass != ExprClass.MethodGroup) {
unwrap = Unwrap.Create (left, ec);
if (unwrap == null)
return null;
- if (Convert.ImplicitConversionExists (ec, right, ltype)) {
+ if (Convert.ImplicitConversionExists (ec, right, unwrap.Type)) {
left = unwrap;
- right = Convert.ImplicitConversion (ec, right, ltype, loc);
type = left.Type;
+ right = Convert.ImplicitConversion (ec, right, type, loc);
return this;
- }
+ }
} else if (TypeManager.IsReferenceType (ltype) && right.eclass != ExprClass.MethodGroup) {
if (Convert.ImplicitConversionExists (ec, right, ltype)) {
//
return ReducedExpression.Create (lc != null ? right : left, this).Resolve (ec);
right = Convert.ImplicitConversion (ec, right, ltype, loc);
- type = right.Type;
+ type = left.Type;
return this;
}
} else {
return null;
}
- if (!Convert.ImplicitConversionExists (ec, left, rtype)) {
+ if (!Convert.ImplicitConversionExists (ec, unwrap != null ? unwrap : left, rtype)) {
Binary.Error_OperatorCannotBeApplied (left, right, "??", loc);
return null;
}
if (left.IsNull)
return ReducedExpression.Create (right, this).Resolve (ec);
- left = Convert.ImplicitConversion (ec, left, rtype, loc);
+ left = Convert.ImplicitConversion (ec, unwrap != null ? unwrap : left, rtype, loc);
type = rtype;
return this;
}
public class LiftedUnaryMutator : ExpressionStatement
{
public readonly UnaryMutator.Mode Mode;
- Expression expr, null_value;
+ Expression expr;
UnaryMutator underlying;
Unwrap unwrap;
return null;
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);
}