{
throw new Exception ("Should not happen");
}
+
+ public override Location Location
+ {
+ get {
+ return Expr.Location;
+ }
+ }
}
/// <summary>
Expression e = null;
if (expr is IntConstant)
- e = new IntConstant (-((IntConstant) expr).Value);
+ e = new IntConstant (-((IntConstant) expr).Value, expr.Location);
else if (expr is UIntConstant){
uint value = ((UIntConstant) expr).Value;
if (value < 2147483649)
- return new IntConstant (-(int)value);
+ return new IntConstant (-(int)value, expr.Location);
else
- e = new LongConstant (-value);
+ e = new LongConstant (-value, expr.Location);
}
else if (expr is LongConstant)
- e = new LongConstant (-((LongConstant) expr).Value);
+ e = new LongConstant (-((LongConstant) expr).Value, expr.Location);
else if (expr is ULongConstant){
ulong value = ((ULongConstant) expr).Value;
if (value < 9223372036854775809)
- return new LongConstant(-(long)value);
+ return new LongConstant(-(long)value, expr.Location);
}
else if (expr is FloatConstant)
- e = new FloatConstant (-((FloatConstant) expr).Value);
+ e = new FloatConstant (-((FloatConstant) expr).Value, expr.Location);
else if (expr is DoubleConstant)
- e = new DoubleConstant (-((DoubleConstant) expr).Value);
+ e = new DoubleConstant (-((DoubleConstant) expr).Value, expr.Location);
else if (expr is DecimalConstant)
- e = new DecimalConstant (-((DecimalConstant) expr).Value);
+ e = new DecimalConstant (-((DecimalConstant) expr).Value, expr.Location);
else if (expr is ShortConstant)
- e = new IntConstant (-((ShortConstant) expr).Value);
+ e = new IntConstant (-((ShortConstant) expr).Value, expr.Location);
else if (expr is UShortConstant)
- e = new IntConstant (-((UShortConstant) expr).Value);
+ e = new IntConstant (-((UShortConstant) expr).Value, expr.Location);
else if (expr is SByteConstant)
- e = new IntConstant (-((SByteConstant) expr).Value);
+ e = new IntConstant (-((SByteConstant) expr).Value, expr.Location);
else if (expr is ByteConstant)
- e = new IntConstant (-((ByteConstant) expr).Value);
+ e = new IntConstant (-((ByteConstant) expr).Value, expr.Location);
return e;
}
}
BoolConstant b = (BoolConstant) e;
- result = new BoolConstant (!(b.Value));
+ result = new BoolConstant (!(b.Value), b.Location);
return true;
case Operator.OnesComplement:
}
if (expr_type == TypeManager.int32_type){
- result = new IntConstant (~ ((IntConstant) e).Value);
+ result = new IntConstant (~ ((IntConstant) e).Value, e.Location);
} else if (expr_type == TypeManager.uint32_type){
- result = new UIntConstant (~ ((UIntConstant) e).Value);
+ result = new UIntConstant (~ ((UIntConstant) e).Value, e.Location);
} else if (expr_type == TypeManager.int64_type){
- result = new LongConstant (~ ((LongConstant) e).Value);
+ result = new LongConstant (~ ((LongConstant) e).Value, e.Location);
} else if (expr_type == TypeManager.uint64_type){
- result = new ULongConstant (~ ((ULongConstant) e).Value);
+ result = new ULongConstant (~ ((ULongConstant) e).Value, e.Location);
} else {
result = null;
Error23 (expr_type);
public abstract class Probe : Expression {
public Expression ProbeType;
protected Expression expr;
- protected Type probe_type;
+ protected TypeExpr probe_type_expr;
public Probe (Expression expr, Expression probe_type, Location l)
{
public override Expression DoResolve (EmitContext ec)
{
- TypeExpr texpr = ProbeType.ResolveAsTypeTerminal (ec);
- if (texpr == null)
+ probe_type_expr = ProbeType.ResolveAsTypeTerminal (ec);
+ if (probe_type_expr == null)
+ return null;
+ if (probe_type_expr.ResolveType (ec) == null)
return null;
- probe_type = texpr.Type;
-
- CheckObsoleteAttribute (probe_type);
expr = expr.Resolve (ec);
if (expr == null)
ig.Emit (OpCodes.Ceq);
return;
case Action.Probe:
- ig.Emit (OpCodes.Isinst, probe_type);
+ ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
ig.Emit (OpCodes.Ldnull);
ig.Emit (OpCodes.Cgt_Un);
return;
return;
case Action.Probe:
expr.Emit (ec);
- ig.Emit (OpCodes.Isinst, probe_type);
+ ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
return;
}
// First case, if at compile time, there is an implicit conversion
// then e != null (objects) or true (value types)
//
+ Type probe_type = probe_type_expr.Type;
e = Convert.ImplicitConversionStandard (ec, expr, probe_type, loc);
if (e != null && !(e is NullCast)){
expr = e;
}
bool do_isinst = false;
+ Expression resolved_type;
public override void Emit (EmitContext ec)
{
expr.Emit (ec);
if (do_isinst)
- ig.Emit (OpCodes.Isinst, probe_type);
+ ig.Emit (OpCodes.Isinst, probe_type_expr.Type);
}
static void Error_CannotConvertType (Type source, Type target, Location loc)
public override Expression DoResolve (EmitContext ec)
{
- Expression e = base.DoResolve (ec);
+ if (resolved_type == null) {
+ resolved_type = base.DoResolve (ec);
- if (e == null)
- return null;
+ if (resolved_type == null)
+ return null;
+ }
- type = probe_type;
+ type = probe_type_expr.Type;
eclass = ExprClass.Value;
Type etype = expr.Type;
- if (probe_type.IsValueType) {
+ if (type.IsValueType) {
Report.Error (77, loc, "The as operator must be used with a reference type (`" +
- TypeManager.CSharpName (probe_type) + "' is a value type)");
+ TypeManager.CSharpName (type) + "' is a value type)");
return null;
}
+
+ //
+ // If the type is a type parameter, ensure
+ // that it is constrained by a class
+ //
+ TypeParameterExpr tpe = probe_type_expr as TypeParameterExpr;
+ if (tpe != null){
+ Constraints constraints = tpe.TypeParameter.Constraints;
+ bool error = false;
+
+ if (constraints == null)
+ error = true;
+ else {
+ if (!constraints.HasClassConstraint)
+ if ((constraints.Attributes & GenericParameterAttributes.ReferenceTypeConstraint) == 0)
+ error = true;
+ }
+ if (error){
+ Report.Error (413, loc,
+ "The as operator requires that the `{0}' type parameter be constrained by a class",
+ probe_type_expr);
+ return null;
+ }
+ }
- e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
+ Expression e = Convert.ImplicitConversion (ec, expr, type, loc);
if (e != null){
expr = e;
do_isinst = false;
return this;
}
- if (Convert.ExplicitReferenceConversionExists (etype, probe_type)){
+ if (Convert.ExplicitReferenceConversionExists (etype, type)){
if (etype.IsGenericParameter)
expr = new BoxedCast (expr, etype);
return this;
}
- Error_CannotConvertType (etype, probe_type, loc);
+ Error_CannotConvertType (etype, type, loc);
return null;
}
}
expr = value;
}
}
-
- bool CheckRange (EmitContext ec, long value, Type type, long min, long max)
- {
- if (!ec.ConstantCheckState)
- return true;
-
- if ((value < min) || (value > max)) {
- Error (221, "Constant value `" + value + "' cannot be converted " +
- "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
- "syntax to override)");
- return false;
- }
-
- return true;
- }
-
- bool CheckRange (EmitContext ec, ulong value, Type type, ulong max)
- {
- if (!ec.ConstantCheckState)
- return true;
-
- if (value > max) {
- Error (221, "Constant value `" + value + "' cannot be converted " +
- "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
- "syntax to override)");
- return false;
- }
-
- return true;
- }
-
- bool CheckUnsigned (EmitContext ec, long value, Type type)
- {
- if (!ec.ConstantCheckState)
- return true;
-
- if (value < 0) {
- Error (221, "Constant value `" + value + "' cannot be converted " +
- "to a `" + TypeManager.CSharpName (type) + "' (use `unchecked' " +
- "syntax to override)");
- return false;
- }
-
- return true;
- }
-
- // TODO: move to constant
- /// <summary>
- /// Attempts to do a compile-time folding of a constant cast.
- /// </summary>
- Expression TryReduce (EmitContext ec, Type target_type)
- {
- if (expr.Type == target_type)
- return expr;
-
- if (TypeManager.IsEnumType (target_type) && TypeManager.EnumToUnderlying (target_type) == expr.Type)
- return new EnumConstant ((Constant)expr, target_type);
-
- Expression real_expr = expr;
- if (real_expr is EnumConstant)
- real_expr = ((EnumConstant) real_expr).Child;
-
- if (real_expr is ByteConstant){
- byte v = ((ByteConstant) real_expr).Value;
-
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type)
- return new ShortConstant ((short) v);
- if (target_type == TypeManager.ushort_type)
- return new UShortConstant ((ushort) v);
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type)
- return new CharConstant ((char) v);
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is SByteConstant){
- sbyte v = ((SByteConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.short_type)
- return new ShortConstant ((short) v);
- if (target_type == TypeManager.ushort_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new UShortConstant ((ushort) v);
- } if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new UIntConstant ((uint) v);
- } if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new ULongConstant ((ulong) v);
- }
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is ShortConstant){
- short v = ((ShortConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.ushort_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new UShortConstant ((ushort) v);
- }
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new UIntConstant ((uint) v);
- }
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new ULongConstant ((ulong) v);
- }
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is UShortConstant){
- ushort v = ((UShortConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is IntConstant){
- int v = ((IntConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.ushort_type) {
- if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
- return null;
- return new UShortConstant ((ushort) v);
- }
- if (target_type == TypeManager.uint32_type) {
- if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
- return null;
- return new UIntConstant ((uint) v);
- }
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new ULongConstant ((ulong) v);
- }
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is UIntConstant){
- uint v = ((UIntConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.ushort_type) {
- if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
- return null;
- return new UShortConstant ((ushort) v);
- }
- if (target_type == TypeManager.int32_type) {
- if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
- return null;
- return new IntConstant ((int) v);
- }
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is LongConstant){
- long v = ((LongConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.ushort_type) {
- if (!CheckRange (ec, v, target_type, UInt16.MinValue, UInt16.MaxValue))
- return null;
- return new UShortConstant ((ushort) v);
- }
- if (target_type == TypeManager.int32_type) {
- if (!CheckRange (ec, v, target_type, Int32.MinValue, Int32.MaxValue))
- return null;
- return new IntConstant ((int) v);
- }
- if (target_type == TypeManager.uint32_type) {
- if (!CheckRange (ec, v, target_type, UInt32.MinValue, UInt32.MaxValue))
- return null;
- return new UIntConstant ((uint) v);
- }
- if (target_type == TypeManager.uint64_type) {
- if (!CheckUnsigned (ec, v, target_type))
- return null;
- return new ULongConstant ((ulong) v);
- }
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is ULongConstant){
- ulong v = ((ULongConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, (ulong) SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, (ulong) Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.ushort_type) {
- if (!CheckRange (ec, v, target_type, UInt16.MaxValue))
- return null;
- return new UShortConstant ((ushort) v);
- }
- if (target_type == TypeManager.int32_type) {
- if (!CheckRange (ec, v, target_type, Int32.MaxValue))
- return null;
- return new IntConstant ((int) v);
- }
- if (target_type == TypeManager.uint32_type) {
- if (!CheckRange (ec, v, target_type, UInt32.MaxValue))
- return null;
- return new UIntConstant ((uint) v);
- }
- if (target_type == TypeManager.int64_type) {
- if (!CheckRange (ec, v, target_type, (ulong) Int64.MaxValue))
- return null;
- return new LongConstant ((long) v);
- }
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is FloatConstant){
- float v = ((FloatConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type)
- return new ByteConstant ((byte) v);
- if (target_type == TypeManager.sbyte_type)
- return new SByteConstant ((sbyte) v);
- if (target_type == TypeManager.short_type)
- return new ShortConstant ((short) v);
- if (target_type == TypeManager.ushort_type)
- return new UShortConstant ((ushort) v);
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type)
- return new CharConstant ((char) v);
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
- if (real_expr is DoubleConstant){
- double v = ((DoubleConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type){
- return new ByteConstant ((byte) v);
- } if (target_type == TypeManager.sbyte_type)
- return new SByteConstant ((sbyte) v);
- if (target_type == TypeManager.short_type)
- return new ShortConstant ((short) v);
- if (target_type == TypeManager.ushort_type)
- return new UShortConstant ((ushort) v);
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.char_type)
- return new CharConstant ((char) v);
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
-
- if (real_expr is CharConstant){
- char v = ((CharConstant) real_expr).Value;
-
- if (target_type == TypeManager.byte_type) {
- if (!CheckRange (ec, v, target_type, Byte.MinValue, Byte.MaxValue))
- return null;
- return new ByteConstant ((byte) v);
- }
- if (target_type == TypeManager.sbyte_type) {
- if (!CheckRange (ec, v, target_type, SByte.MinValue, SByte.MaxValue))
- return null;
- return new SByteConstant ((sbyte) v);
- }
- if (target_type == TypeManager.short_type) {
- if (!CheckRange (ec, v, target_type, Int16.MinValue, Int16.MaxValue))
- return null;
- return new ShortConstant ((short) v);
- }
- if (target_type == TypeManager.int32_type)
- return new IntConstant ((int) v);
- if (target_type == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
- if (target_type == TypeManager.int64_type)
- return new LongConstant ((long) v);
- if (target_type == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
- if (target_type == TypeManager.float_type)
- return new FloatConstant ((float) v);
- if (target_type == TypeManager.double_type)
- return new DoubleConstant ((double) v);
- if (target_type == TypeManager.char_type) {
- if (!CheckRange (ec, v, target_type, Char.MinValue, Char.MaxValue))
- return null;
- return new CharConstant ((char) v);
- }
- if (target_type == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
- }
-
- return null;
- }
public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
if (target == null)
return null;
- type = target.Type;
-
- CheckObsoleteAttribute (type);
+ type = target.ResolveType (ec);
if (type.IsAbstract && type.IsSealed) {
Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
eclass = ExprClass.Value;
- if (expr is Constant){
- Expression e = TryReduce (ec, type);
-
- if (e != null)
- return e;
+ Constant c = expr as Constant;
+ if (c != null) {
+ c = c.TryReduce (ec, type, loc);
+ if (c != null)
+ return c;
}
if (type.IsPointer && !ec.InUnsafe) {
/// <summary>
/// Returns a stringified representation of the Operator
/// </summary>
- static string OperName (Operator oper)
+ public static string OperName (Operator oper)
{
switch (oper){
case Operator.Multiply:
long ll = ((LongConstant) right).Value;
if (ll >= 0)
- right = new ULongConstant ((ulong) ll);
+ right = new ULongConstant ((ulong) ll, right.Location);
} else {
- e = Convert.ImplicitNumericConversion (ec, right, l, loc);
+ e = Convert.ImplicitNumericConversion (ec, right, l);
if (e != null)
right = e;
}
long ll = ((LongConstant) left).Value;
if (ll > 0)
- left = new ULongConstant ((ulong) ll);
+ left = new ULongConstant ((ulong) ll, right.Location);
} else {
- e = Convert.ImplicitNumericConversion (ec, left, r, loc);
+ e = Convert.ImplicitNumericConversion (ec, left, r);
if (e != null)
left = e;
}
int val = ic.Value;
if (val >= 0){
- right = new UIntConstant ((uint) val);
+ right = new UIntConstant ((uint) val, ic.Location);
type = l;
return true;
int val = ic.Value;
if (val >= 0){
- left = new UIntConstant ((uint) val);
+ left = new UIntConstant ((uint) val, ic.Location);
type = r;
return true;
}
}
static public void Error_OperatorCannotBeApplied (Location loc, string name, Type l, Type r)
+ {
+ Error_OperatorCannotBeApplied (loc, name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
+ }
+
+ public static void Error_OperatorCannotBeApplied (Location loc, string name, string left, string right)
{
Report.Error (19, loc, "Operator `{0}' cannot be applied to operands of type `{1}' and `{2}'",
- name, TypeManager.CSharpName (l), TypeManager.CSharpName (r));
+ name, left, right);
}
void Error_OperatorCannotBeApplied ()
{
- Error_OperatorCannotBeApplied (loc, OperName (oper), left.Type, right.Type);
+ Error_OperatorCannotBeApplied (Location, OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
}
static bool is_unsigned (Type t)
type = e.Type;
if (type == TypeManager.int32_type || type == TypeManager.uint32_type){
- right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (31));
+ right = new Binary (Binary.Operator.BitwiseAnd, right, new IntConstant (31, loc));
right = right.DoResolve (ec);
} else {
- right = new Binary (Binary.Operator.BitwiseAnd, right, new IntLiteral (63));
+ right = new Binary (Binary.Operator.BitwiseAnd, right, new IntConstant (63, loc));
right = right.DoResolve (ec);
}
// Simple constant folding
if (left is StringConstant && right is StringConstant)
- return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value);
+ return new StringConstant (((StringConstant) left).Value + ((StringConstant) right).Value, left.Location);
if (l == TypeManager.string_type || r == TypeManager.string_type) {
return this;
}
+ Constant EnumLiftUp (EmitContext ec, Constant left, Constant right)
+ {
+ switch (oper) {
+ case Operator.BitwiseOr:
+ case Operator.BitwiseAnd:
+ case Operator.ExclusiveOr:
+ case Operator.Equality:
+ case Operator.Inequality:
+ case Operator.LessThan:
+ case Operator.LessThanOrEqual:
+ case Operator.GreaterThan:
+ case Operator.GreaterThanOrEqual:
+ if (left is EnumConstant)
+ return left;
+
+ if (left.IsZeroInteger)
+ return new EnumConstant (left, right.Type);
+
+ break;
+
+ case Operator.Addition:
+ case Operator.Subtraction:
+ return left;
+
+ case Operator.Multiply:
+ case Operator.Division:
+ case Operator.Modulus:
+ case Operator.LeftShift:
+ case Operator.RightShift:
+ if (right is EnumConstant || left is EnumConstant)
+ break;
+ return left;
+ }
+ Error_OperatorCannotBeApplied (loc, Binary.OperName (oper), left.Type, right.Type);
+ return null;
+ }
+
public override Expression DoResolve (EmitContext ec)
{
if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
return null;
if (left.eclass == ExprClass.Type) {
- Error (75, "To cast a negative value, you must enclose the value in parentheses");
+ Report.Error (75, loc, "To cast a negative value, you must enclose the value in parentheses");
return null;
}
} else
return null;
eclass = ExprClass.Value;
-
Constant rc = right as Constant;
+ if (lc != null && rc != null && (TypeManager.IsEnumType (left.Type) || TypeManager.IsEnumType (right.Type))) {
+ left = lc = EnumLiftUp (ec, lc, rc);
+ if (lc == null)
+ return null;
+
+ right = rc = EnumLiftUp (ec, rc, lc);
+ if (rc == null)
+ return null;
+ }
+
if (oper == Operator.BitwiseAnd) {
if (rc != null && rc.IsZeroInteger) {
return lc is EnumConstant ?
lc;
}
}
+ else if (oper == Operator.BitwiseOr) {
+ if (lc is EnumConstant &&
+ rc != null && rc.IsZeroInteger)
+ return lc;
+ if (rc is EnumConstant &&
+ lc != null && lc.IsZeroInteger)
+ return rc;
+ }
if (rc != null && lc != null){
int prev_e = Report.Errors;
if (operand is StringConstant && operands.Count != 0) {
StringConstant last_operand = operands [operands.Count - 1] as StringConstant;
if (last_operand != null) {
- operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value);
+ operands [operands.Count - 1] = new StringConstant (last_operand.Value + ((StringConstant) operand).Value, last_operand.Location);
return;
}
}
protected void Error19 ()
{
- Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", type, type);
+ Binary.Error_OperatorCannotBeApplied (loc, is_and ? "&&" : "||", left.GetSignatureForError (), right.GetSignatureForError ());
}
protected void Error218 ()
Constant right_const = right as Constant;
if (right_const != null && size != 0) {
- Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size), right_const, loc);
+ Expression ex = ConstantFold.BinaryFold (ec, Binary.Operator.Multiply, new IntConstant (size, right.Location), right_const, loc);
if (ex == null)
return;
ex.Emit (ec);
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
- Expression ret = DoResolveBase (ec, right_side);
- if (ret != null)
- CheckObsoleteAttribute (ret.Type);
-
- return ret;
+ return DoResolveBase (ec, right_side);
}
public bool VerifyFixed ()
/// </summary>
public static void EmitArguments (EmitContext ec, MethodBase mb, ArrayList arguments, bool dup_args, LocalTemporary this_arg)
{
- ParameterData pd;
- if (mb != null)
- pd = TypeManager.GetParameterData (mb);
- else
- pd = null;
-
+ ParameterData pd = mb == null ? null : TypeManager.GetParameterData (mb);
+ int top = arguments == null ? 0 : arguments.Count;
LocalTemporary [] temps = null;
- if (dup_args)
- temps = new LocalTemporary [arguments.Count];
-
- //
- // If we are calling a params method with no arguments, special case it
- //
- if (arguments == null){
- if (pd != null && pd.Count > 0 &&
- pd.ParameterModifier (0) == Parameter.Modifier.PARAMS){
- ILGenerator ig = ec.ig;
-
- IntConstant.EmitInt (ig, 0);
- ig.Emit (OpCodes.Newarr, TypeManager.GetElementType (pd.ParameterType (0)));
- }
-
- return;
- }
-
- int top = arguments.Count;
+ if (dup_args && top != 0)
+ temps = new LocalTemporary [top];
for (int i = 0; i < top; i++){
Argument a = (Argument) arguments [i];
}
if (dup_args) {
- this_arg = new LocalTemporary (ec, t);
ig.Emit (OpCodes.Dup);
- this_arg.Store (ec);
+ if (Arguments != null && Arguments.Count != 0) {
+ this_arg = new LocalTemporary (ec, t);
+ this_arg.Store (ec);
+ }
}
}
}
public static Constant Constantify (Type t)
{
if (t == TypeManager.int32_type)
- return new IntConstant (0);
+ return new IntConstant (0, Location.Null);
if (t == TypeManager.uint32_type)
- return new UIntConstant (0);
+ return new UIntConstant (0, Location.Null);
if (t == TypeManager.int64_type)
- return new LongConstant (0);
+ return new LongConstant (0, Location.Null);
if (t == TypeManager.uint64_type)
- return new ULongConstant (0);
+ return new ULongConstant (0, Location.Null);
if (t == TypeManager.float_type)
- return new FloatConstant (0);
+ return new FloatConstant (0, Location.Null);
if (t == TypeManager.double_type)
- return new DoubleConstant (0);
+ return new DoubleConstant (0, Location.Null);
if (t == TypeManager.short_type)
- return new ShortConstant (0);
+ return new ShortConstant (0, Location.Null);
if (t == TypeManager.ushort_type)
- return new UShortConstant (0);
+ return new UShortConstant (0, Location.Null);
if (t == TypeManager.sbyte_type)
- return new SByteConstant (0);
+ return new SByteConstant (0, Location.Null);
if (t == TypeManager.byte_type)
- return new ByteConstant (0);
+ return new ByteConstant (0, Location.Null);
if (t == TypeManager.char_type)
- return new CharConstant ('\0');
+ return new CharConstant ('\0', Location.Null);
if (t == TypeManager.bool_type)
- return new BoolConstant (false);
+ return new BoolConstant (false, Location.Null);
if (t == TypeManager.decimal_type)
- return new DecimalConstant (0);
+ return new DecimalConstant (0, Location.Null);
return null;
}
if (texpr == null)
return null;
+ type = texpr.ResolveType (ec);
+
if (Arguments == null) {
Expression c = Constantify (type);
if (c != null)
return c;
}
- type = texpr.Type;
- if (type == null)
- return null;
-
- CheckObsoleteAttribute (type);
-
- if (TypeManager.IsDelegateType (type)) {
+ if (TypeManager.IsDelegateType (type)) {
RequestedType = (new NewDelegate (type, Arguments, loc)).Resolve (ec);
if (RequestedType != null)
if (!(RequestedType is DelegateCreation))
int i = 0;
for (ArrayList probe = initializers; probe != null;) {
if (probe.Count > 0 && probe [0] is ArrayList) {
- Expression e = new IntConstant (probe.Count);
+ Expression e = new IntConstant (probe.Count, Location.Null);
arguments.Add (new Argument (e, Argument.AType.Expression));
bounds [i++] = probe.Count;
probe = (ArrayList) probe [0];
} else {
- Expression e = new IntConstant (probe.Count);
+ Expression e = new IntConstant (probe.Count, Location.Null);
arguments.Add (new Argument (e, Argument.AType.Expression));
bounds [i++] = probe.Count;
if (array_type_expr == null)
return false;
- type = array_type_expr.Type;
+ type = array_type_expr.ResolveType (ec);
if (!type.IsArray) {
Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
public bool VerifyFixed ()
{
- // Treat 'this' as a value parameter for the purpose of fixed variable determination.
- return true;
+ return !TypeManager.IsValueType (Type);
}
public bool ResolveBase (EmitContext ec)
if (texpr == null)
return null;
- typearg = texpr.Type;
+ typearg = texpr.ResolveType (ec);
if (typearg == TypeManager.void_type) {
Error (673, "System.Void cannot be used from C#. Use typeof (void) to get the void type object");
UnsafeError (loc);
return null;
}
- CheckObsoleteAttribute (typearg);
type = TypeManager.type_type;
// Even though what is returned is a type object, it's treated as a value by the compiler.
return null;
}
- type_queried = texpr.Type;
+ type_queried = texpr.ResolveType (ec);
int size_of = GetTypeSize (type_queried);
if (size_of > 0) {
- return new IntConstant (size_of);
+ return new IntConstant (size_of, loc);
}
if (!ec.InUnsafe) {
return null;
}
- CheckObsoleteAttribute (type_queried);
-
if (!TypeManager.VerifyUnManaged (type_queried, loc)){
return null;
}
/// Implements the member access expression
/// </summary>
public class MemberAccess : Expression {
- public readonly string Identifier; // TODO: LocatedToken
+ public readonly string Identifier;
Expression expr;
TypeArguments args;
+ // TODO: Location can be removed
public MemberAccess (Expression expr, string id, Location l)
{
this.expr = expr;
Identifier = id;
- loc = l;
+ loc = expr.Location;
}
public MemberAccess (Expression expr, string id, TypeArguments args,
if (tnew_expr == null)
return null;
- Type expr_type = tnew_expr.Type;
+ Type expr_type = tnew_expr.ResolveType (ec);
if (expr_type.IsPointer){
Error (23, "The `.' operator can not be applied to pointer operands (" +
if (lexpr == null)
return null;
- Type ltype = lexpr.Type;
+ bool old = ec.TestObsoleteMethodUsage;
+ ec.TestObsoleteMethodUsage = true;
+ Type ltype = lexpr.ResolveType (ec);
+ ec.TestObsoleteMethodUsage = old;
if ((ltype == TypeManager.void_type) && (dim != "*")) {
Report.Error (1547, Location,
}
}
- public class FixedBufferPtr: Expression {
+ public class FixedBufferPtr : Expression {
Expression array;
public FixedBufferPtr (Expression array, Type array_type, Location l)
if (texpr == null)
return null;
- otype = texpr.Type;
+ otype = texpr.ResolveType (ec);
if (!TypeManager.VerifyUnManaged (otype, loc))
return null;