//
// Author:
// Miguel de Icaza (miguel@ximian.com)
+// Marek Safar (marek.safar@seznam.cz)
//
// (C) 2001, 2002, 2003 Ximian, Inc.
// (C) 2003, 2004 Novell, Inc.
{
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);
return null;
probe_type = texpr.ResolveType (ec);
- CheckObsoleteAttribute (probe_type);
-
expr = expr.Resolve (ec);
if (expr == null)
return null;
}
bool do_isinst = false;
+ Expression resolved_type;
public override void Emit (EmitContext ec)
{
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;
eclass = ExprClass.Value;
}
- e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
+ Expression e = Convert.ImplicitConversion (ec, expr, probe_type, loc);
if (e != null){
expr = e;
do_isinst = false;
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))
- 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)
{
type = target.ResolveType (ec);
- CheckObsoleteAttribute (type);
-
if (type.IsAbstract && type.IsSealed) {
Report.Error (716, loc, "Cannot convert to static type `{0}'", TypeManager.CSharpName (type));
return null;
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 (left == null)
+ return null;
+
if ((oper == Operator.Subtraction) && (left is ParenthesizedExpression)) {
left = ((ParenthesizedExpression) left).Expr;
left = left.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.Type);
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;
+ // The conversion rules are ignored in enum context but why
+ if (!ec.InEnumContext && 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;
+ } else if (oper == Operator.LogicalAnd) {
+ if (rc != null && rc.IsDefaultValue && rc.Type == TypeManager.bool_type)
+ return rc;
+ if (lc != null && lc.IsDefaultValue && lc.Type == TypeManager.bool_type)
+ return lc;
+ }
if (rc != null && lc != null){
int prev_e = Report.Errors;
return e;
}
- // Check CS0652 warning here (before resolving operator).
- if (oper == Operator.Equality ||
- oper == Operator.Inequality ||
- oper == Operator.LessThanOrEqual ||
- oper == Operator.LessThan ||
- oper == Operator.GreaterThanOrEqual ||
- oper == Operator.GreaterThan){
- CheckUselessComparison (left as Constant, right.Type);
- CheckUselessComparison (right as Constant, left.Type);
+ // Comparison warnings
+ if (oper == Operator.Equality || oper == Operator.Inequality ||
+ oper == Operator.LessThanOrEqual || oper == Operator.LessThan ||
+ oper == Operator.GreaterThanOrEqual || oper == Operator.GreaterThan){
+ if (left.Equals (right)) {
+ Report.Warning (1718, 3, loc, "Comparison made to same variable; did you mean to compare something else?");
+ }
+ CheckUselessComparison (lc, right.Type);
+ CheckUselessComparison (rc, left.Type);
}
return ResolveOperator (ec);
if (c is ULongConstant) {
ulong uvalue = ((ULongConstant) c).Value;
- if (uvalue > long.MaxValue && (
- type == TypeManager.byte_type ||
- type == TypeManager.sbyte_type ||
- type == TypeManager.short_type ||
- type == TypeManager.ushort_type ||
- type == TypeManager.int32_type ||
- type == TypeManager.uint32_type ||
- type == TypeManager.int64_type)) {
- WarnUselessComparison (type);
+ if (uvalue > long.MaxValue) {
+ if (type == TypeManager.byte_type ||
+ type == TypeManager.sbyte_type ||
+ type == TypeManager.short_type ||
+ type == TypeManager.ushort_type ||
+ type == TypeManager.int32_type ||
+ type == TypeManager.uint32_type ||
+ type == TypeManager.int64_type)
+ WarnUselessComparison (type);
return;
}
value = (long) uvalue;
private void WarnUselessComparison (Type type)
{
- Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type '{0}'", type);
+ Report.Warning (652, 2, loc, "Comparison to integral constant is useless; the constant is outside the range of type `{0}'",
+ TypeManager.CSharpName (type));
}
/// <remarks>
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);
return null;
}
+ Assign ass = expr as Assign;
+ if (ass != null && ass.Source is Constant) {
+ Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+ }
+
trueExpr = trueExpr.Resolve (ec);
falseExpr = falseExpr.Resolve (ec);
}
}
+ public bool VerifyAssigned (EmitContext ec)
+ {
+ VariableInfo variable_info = local_info.VariableInfo;
+ return variable_info == null || variable_info.IsAssigned (ec, loc);
+ }
+
protected Expression DoResolveBase (EmitContext ec, Expression lvalue_right_side)
{
if (local_info == null) {
return e.Resolve (ec);
}
- if ((variable_info != null) && !variable_info.IsAssigned (ec, loc))
+ if (!VerifyAssigned (ec))
return null;
if (lvalue_right_side == null)
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 ()
/// representation.
/// </summary>
public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
- Parameters pars;
- String name;
+ Parameter par;
+ string name;
int idx;
Block block;
VariableInfo vi;
- public Parameter.Modifier mod;
public bool is_ref, is_out, prepared;
public bool IsOut {
LocalTemporary temp;
- public ParameterReference (Parameters pars, Block block, int idx, string name, Location loc)
+ public ParameterReference (Parameter par, Block block, int idx, Location loc)
{
- this.pars = pars;
+ this.par = par;
+ this.name = par.Name;
this.block = block;
this.idx = idx;
- this.name = name;
this.loc = loc;
eclass = ExprClass.Variable;
}
- public ParameterReference (InternalParameters pars, Block block, int idx, Location loc)
- : this (pars.Parameters, block, idx, pars.ParameterName (idx), loc)
- { }
-
public VariableInfo VariableInfo {
get { return vi; }
}
public bool VerifyFixed ()
{
// A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
- return mod == Parameter.Modifier.NONE;
+ return par.ModFlags == Parameter.Modifier.NONE;
}
public bool IsAssigned (EmitContext ec, Location loc)
return true;
Report.Error (269, loc,
- "Use of unassigned out parameter `{0}'", name);
+ "Use of unassigned out parameter `{0}'", par.Name);
return false;
}
protected void DoResolveBase (EmitContext ec)
{
- type = pars.GetParameterInfo (ec, idx, out mod);
+ if (!par.Resolve (ec)) {
+ //TODO:
+ }
+
+ type = par.ParameterType;
+ Parameter.Modifier mod = par.ModFlags;
is_ref = (mod & Parameter.Modifier.ISBYREF) != 0;
- is_out = (mod & Parameter.Modifier.OUT) != 0;
+ is_out = (mod & Parameter.Modifier.OUT) == Parameter.Modifier.OUT;
eclass = ExprClass.Variable;
if (is_out)
if (ec.CurrentAnonymousMethod != null){
if (is_ref){
Report.Error (1628, Location, "Cannot use ref or out parameter `{0}' inside an anonymous method block",
- name);
+ par.Name);
return;
}
ILGenerator ig = ec.ig;
int arg_idx = idx;
- if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
- if (leave_copy)
- throw new InternalErrorException ();
-
- ec.EmitParameter (name);
+ if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
+ ec.EmitParameter (name, leave_copy, prepared, ref temp);
return;
}
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
{
+ prepared = prepare_for_load;
if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
- ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load);
+ ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load, ref temp);
return;
}
ILGenerator ig = ec.ig;
int arg_idx = idx;
- prepared = prepare_for_load;
+
if (!ec.MethodIsStatic)
arg_idx++;
}
}
+ public override string ToString ()
+ {
+ return "ParameterReference[" + name + "]";
+ }
}
/// <summary>
get {
switch (ArgType) {
case AType.Out:
- return Parameter.Modifier.OUT | Parameter.Modifier.ISBYREF;
+ return Parameter.Modifier.OUT;
case AType.Ref:
- return Parameter.Modifier.REF | Parameter.Modifier.ISBYREF;
+ return Parameter.Modifier.REF;
default:
return Parameter.Modifier.NONE;
return !candidate_params && best_params;
}
- static bool IsOverride (MethodBase cand_method, MethodBase base_method)
+ internal static bool IsOverride (MethodBase cand_method, MethodBase base_method)
{
if (!IsAncestralType (base_method.DeclaringType, cand_method.DeclaringType))
return false;
return union;
}
- public static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
+ public static bool IsParamsMethodApplicable (EmitContext ec,
ArrayList arguments, int arg_count,
- ref MethodBase candidate)
+ MethodBase candidate)
{
return IsParamsMethodApplicable (
- ec, me, arguments, arg_count, false, ref candidate) ||
+ ec, arguments, arg_count, candidate, false) ||
IsParamsMethodApplicable (
- ec, me, arguments, arg_count, true, ref candidate);
-
+ ec, arguments, arg_count, candidate, true);
- }
- static bool IsParamsMethodApplicable (EmitContext ec, MethodGroupExpr me,
- ArrayList arguments, int arg_count,
- bool do_varargs, ref MethodBase candidate)
- {
- return IsParamsMethodApplicable (
- ec, arguments, arg_count, candidate, do_varargs);
}
/// <summary>
if (pd_count != arg_count)
return false;
} else {
- if (pd.ParameterModifier (count) != Parameter.Modifier.PARAMS)
+ if (!pd.HasParams)
return false;
}
Argument a = (Argument) arguments [i];
Parameter.Modifier a_mod = a.Modifier &
- (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+ (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
Parameter.Modifier p_mod = pd.ParameterModifier (i) &
- (unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF)));
+ (unchecked (~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK)));
if (a_mod == p_mod) {
if (!Convert.ImplicitConversionExists (ec,
a.Expr,
pd.ParameterType (i)))
- return false;
-
+ return false;
+
if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
Type pt = pd.ParameterType (i);
return true;
}
- public static bool IsApplicable (EmitContext ec, MethodGroupExpr me,
- ArrayList arguments, int arg_count,
- ref MethodBase candidate)
- {
- return IsApplicable (ec, arguments, arg_count, candidate);
- }
-
/// <summary>
/// Determines if the candidate method is applicable (section 14.4.2.1)
/// to the given set of arguments
/// </summary>
- static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
- MethodBase candidate)
+ public static bool IsApplicable (EmitContext ec, ArrayList arguments, int arg_count,
+ MethodBase candidate)
{
ParameterData pd = TypeManager.GetParameterData (candidate);
Argument a = (Argument) arguments [i];
Parameter.Modifier a_mod = a.Modifier &
- unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+ ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
+
Parameter.Modifier p_mod = pd.ParameterModifier (i) &
- unchecked (~(Parameter.Modifier.OUT | Parameter.Modifier.REF));
+ ~(Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK | Parameter.Modifier.PARAMS);
- if (a_mod == p_mod ||
- (a_mod == Parameter.Modifier.NONE && p_mod == Parameter.Modifier.PARAMS)) {
- if (a_mod == Parameter.Modifier.NONE) {
- if (!Convert.ImplicitConversionExists (ec,
- a.Expr,
- pd.ParameterType (i)))
- return false;
- }
-
- if ((a_mod & Parameter.Modifier.ISBYREF) != 0) {
- Type pt = pd.ParameterType (i);
+ if (a_mod == p_mod) {
+ Type pt = pd.ParameterType (i);
- if (!pt.IsByRef)
- pt = TypeManager.GetReferenceType (pt);
-
- if (pt != a.Type)
+ if (a_mod == Parameter.Modifier.NONE) {
+ if (!Convert.ImplicitConversionExists (ec, a.Expr, pt))
return false;
+ continue;
}
+
+ if (pt != a.Type)
+ return false;
} else
return false;
}
return true;
}
- static private bool IsAncestralType (Type first_type, Type second_type)
+ static internal bool IsAncestralType (Type first_type, Type second_type)
{
return first_type != second_type &&
(second_type.IsSubclassOf (first_type) ||
// Is candidate applicable in normal form?
//
bool is_applicable = IsApplicable (
- ec, me, Arguments, arg_count, ref methods [i]);
+ ec, Arguments, arg_count, methods [i]);
if (!is_applicable &&
(IsParamsMethodApplicable (
- ec, me, Arguments, arg_count, ref methods [i]))) {
+ ec, Arguments, arg_count, methods [i]))) {
MethodBase candidate = methods [i];
if (candidate_to_form == null)
candidate_to_form = new PtrHashtable ();
VerifyArgumentsCompat (ec, Arguments, arg_count,
c, false, null, may_fail, loc);
+
+ if (!may_fail && errors == Report.Errors)
+ throw new InternalErrorException (
+ "VerifyArgumentsCompat and IsApplicable do not agree; " +
+ "likely reason: ImplicitConversion and ImplicitConversionExists have gone out of sync");
+
break;
}
public static void Error_WrongNumArguments (Location loc, String name, int arg_count)
{
Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
- name, arg_count);
+ name, arg_count.ToString ());
}
static void Error_InvokeOnDelegate (Location loc)
Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
TypeManager.CSharpName (delegate_type));
- string par_desc = expected_par.ParameterDesc (idx);
+ Parameter.Modifier mod = expected_par.ParameterModifier (idx);
- if (a.Modifier != expected_par.ParameterModifier (idx)) {
- if ((expected_par.ParameterModifier (idx) & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
+ string index = (idx + 1).ToString ();
+ if (mod != Parameter.Modifier.ARGLIST && mod != a.Modifier) {
+ if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) == 0)
Report.Error (1615, loc, "Argument `{0}' should not be passed with the `{1}' keyword",
- idx + 1, Parameter.GetModifierSignature (a.Modifier));
+ index, Parameter.GetModifierSignature (a.Modifier));
else
Report.Error (1620, loc, "Argument `{0}' must be passed with the `{1}' keyword",
- idx + 1, Parameter.GetModifierSignature (expected_par.ParameterModifier (idx)));
- return;
+ index, Parameter.GetModifierSignature (mod));
+ } else {
+ Report.Error (1503, loc, "Argument {0}: Cannot convert from `{1}' to `{2}'",
+ index, Argument.FullDesc (a), expected_par.ParameterDesc (idx));
}
-
- Report.Error (1503, loc,
- String.Format ("Argument {0}: Cannot convert from `{1}' to `{2}'",
- idx + 1, Argument.FullDesc (a), par_desc));
}
public static bool VerifyArgumentsCompat (EmitContext ec, ArrayList Arguments,
if (pm == Parameter.Modifier.PARAMS){
if ((pm & ~Parameter.Modifier.PARAMS) != a.Modifier) {
if (!may_fail)
- Error_InvalidArguments (
- loc, j, method, delegate_type,
- a, pd);
+ Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
return false;
}
if (chose_params_expanded)
parameter_type = TypeManager.GetElementType (parameter_type);
- } else if (pm == Parameter.Modifier.ARGLIST){
+ } else if (pm == Parameter.Modifier.ARGLIST) {
+ if (!(a.Expr is Arglist)) {
+ if (!may_fail)
+ Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
+ return false;
+ }
continue;
} else {
//
//
if (pd.ParameterModifier (j) != a.Modifier){
if (!may_fail)
- Error_InvalidArguments (
- loc, j, method, delegate_type,
- a, pd);
+ Error_InvalidArguments (loc, j, method, delegate_type, a, pd);
return false;
}
}
return null;
}
- if ((method.Attributes & MethodAttributes.SpecialName) != 0) {
- if (TypeManager.LookupDeclSpace (method.DeclaringType) != null || TypeManager.IsSpecialMethod (method)) {
- Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
- TypeManager.CSharpSignature (method, true));
- return null;
- }
+ if ((method.Attributes & MethodAttributes.SpecialName) != 0 && IsSpecialMethodInvocation (method)) {
+ return null;
}
if (mg.InstanceExpression != null)
return this;
}
+ bool IsSpecialMethodInvocation (MethodBase method)
+ {
+ IMethodData md = TypeManager.GetMethod (method);
+ if (md != null) {
+ if (!(md is AbstractPropertyEventMethod) && !(md is Operator))
+ return false;
+ } else {
+ if (!TypeManager.IsSpecialMethod (method))
+ return false;
+
+ int args = TypeManager.GetParameterData (method).Count;
+ if (method.Name.StartsWith ("get_") && args > 0)
+ return false;
+ else if (method.Name.StartsWith ("set_") && args > 2)
+ return false;
+
+ // TODO: check operators and events as well ?
+ }
+
+ Report.SymbolRelatedToPreviousError (method);
+ Report.Error (571, loc, "`{0}': cannot explicitly call operator or accessor",
+ TypeManager.CSharpSignature (method, true));
+
+ return true;
+ }
+
// <summary>
// Emits the list of arguments as an array
// </summary>
/// </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 (IsMethodExcluded (method, ec))
- return;
+ return;
if (!is_static){
if (instance_expr == EmptyExpression.Null) {
}
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;
}
+ //
+ // Checks whether the type is an interface that has the
+ // [ComImport, CoClass] attributes and must be treated
+ // specially
+ //
+ public Expression CheckComImport (EmitContext ec)
+ {
+ if (!type.IsInterface)
+ return null;
+
+ //
+ // Turn the call into:
+ // (the-interface-stated) (new class-referenced-in-coclassattribute ())
+ //
+ Type real_class = AttributeTester.GetCoClassAttribute (type);
+ if (real_class == null)
+ return null;
+
+ New proxy = new New (new TypeExpression (real_class, loc), Arguments, loc);
+ Cast cast = new Cast (new TypeExpression (type, loc), proxy, loc);
+ return cast.Resolve (ec);
+ }
+
public override Expression DoResolve (EmitContext ec)
{
//
return c;
}
- 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))
}
if (type.IsAbstract && type.IsSealed) {
+ Report.SymbolRelatedToPreviousError (type);
Report.Error (712, loc, "Cannot create an instance of the static class `{0}'", TypeManager.CSharpName (type));
return null;
}
if (type.IsInterface || type.IsAbstract){
+ RequestedType = CheckComImport (ec);
+ if (RequestedType != null)
+ return RequestedType;
+
+ Report.SymbolRelatedToPreviousError (type);
Report.Error (144, loc, "Cannot create an instance of the abstract class or interface `{0}'", TypeManager.CSharpName (type));
return null;
}
{
if (specified_dims) {
Argument a = (Argument) arguments [idx];
-
+
if (!a.Resolve (ec, loc))
return false;
-
- if (!(a.Expr is Constant)) {
- Error (150, "A constant value is expected");
+
+ Constant c = a.Expr as Constant;
+ if (c != null) {
+ c = c.ToType (TypeManager.int32_type, a.Expr.Location);
+ }
+
+ if (c == null) {
+ Report.Error (150, a.Expr.Location, "A constant value is expected");
return false;
}
-
- int value = (int) ((Constant) a.Expr).GetValue ();
+
+ int value = (int) c.GetValue ();
if (value != probe.Count) {
Error_IncorrectArrayInitializer ();
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.ResolveType (ec);
-
- if (!type.IsArray) {
- Error (622, "Can only use array initializer expressions to assign to array types. Try using a new expression instead.");
- return false;
- }
+ type = array_type_expr.ResolveType (ec);
underlying_type = TypeManager.GetElementType (type);
dimensions = type.GetArrayRank ();
if (e is StringConstant || e is DecimalConstant || !(e is Constant) ||
num_automatic_initializers <= max_automatic_initializers) {
Type etype = e.Type;
-
+
ig.Emit (OpCodes.Dup);
for (int idx = 0; idx < dims; idx++)
// address of it, so we can store it.
//
if ((dims == 1) &&
- etype.IsSubclassOf (TypeManager.value_type) &&
+ TypeManager.IsValueType (etype) &&
(!TypeManager.IsBuiltinOrEnum (etype) ||
etype == TypeManager.decimal_type)) {
if (e is New){
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)
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.
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;
}
public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
{
if (alias == "global")
- return new MemberAccess (Namespace.Root, identifier, loc).ResolveAsTypeStep (ec, silent);
+ return new MemberAccess (RootNamespace.Global, identifier, loc).ResolveAsTypeStep (ec, silent);
int errors = Report.Errors;
FullNamedExpression fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
{
FullNamedExpression fne;
if (alias == "global") {
- fne = Namespace.Root;
+ fne = RootNamespace.Global;
} else {
int errors = Report.Errors;
fne = ec.DeclSpace.NamespaceEntry.LookupAlias (alias);
/// Implements the member access expression
/// </summary>
public class MemberAccess : Expression {
- public readonly string Identifier; // TODO: LocatedToken
+ public readonly string Identifier;
Expression expr;
+ // TODO: Location can be removed
public MemberAccess (Expression expr, string id, Location l)
{
this.expr = expr;
Identifier = id;
- loc = l;
+ loc = expr.Location;
}
public Expression Expr {
if (member_lookup == null)
return null;
+ if (original != null && !TypeManager.IsValueType (expr_type)) {
+ me = member_lookup as MemberExpr;
+ if (me != null && me.IsInstance) {
+ LocalVariableReference var = new_expr as LocalVariableReference;
+ if (var != null && !var.VerifyAssigned (ec))
+ return null;
+ }
+ }
+
// The following DoResolve/DoResolveLValue will do the definite assignment
// check.
if (right_side != null)
- member_lookup = member_lookup.DoResolveLValue (ec, right_side);
+ return member_lookup.DoResolveLValue (ec, right_side);
else
- member_lookup = member_lookup.DoResolve (ec);
-
- return member_lookup;
+ return member_lookup.DoResolve (ec);
}
public override Expression DoResolve (EmitContext ec)
{
FullNamedExpression new_expr = expr.ResolveAsTypeStep (ec, silent);
- if (new_expr == null) {
- Report.Error (234, "No such name or typespace {0}", expr);
+ if (new_expr == null)
return null;
- }
if (new_expr is Namespace) {
Namespace ns = (Namespace) new_expr;
Report.Error (1708, loc, "Fixed size buffers can only be accessed through locals or fields");
return null;
}
-// TODO: not sure whether it is correct
-// if (!ec.InFixedInitializer) {
-// Error (1666, "You cannot use fixed sized buffers contained in unfixed expressions. Try using the fixed statement");
-// return null;
-// }
+ if (!ec.InFixedInitializer && ec.ContainerType.IsValueType) {
+ Error (1666, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
+ return null;
+ }
return MakePointerAccess (ec, ff.ElementType);
}
}
Type t = ea.Expr.Type;
if (t.GetArrayRank () != ea.Arguments.Count){
Report.Error (22, ea.Location, "Wrong number of indexes `{0}' inside [], expected `{1}'",
- ea.Arguments.Count, t.GetArrayRank ());
+ ea.Arguments.Count.ToString (), t.GetArrayRank ().ToString ());
return null;
}
} else if (type.IsValueType){
ig.Emit (OpCodes.Ldelema, type);
ig.Emit (OpCodes.Ldobj, type);
- } else
+ } else if (type.IsPointer)
+ ig.Emit (OpCodes.Ldelem_I);
+ else
ig.Emit (OpCodes.Ldelem_Ref);
}
} else if (t.IsValueType) {
is_stobj = true;
return OpCodes.Stobj;
- } else
+ } else if (t.IsPointer)
+ return OpCodes.Stelem_I;
+ else
return OpCodes.Stelem_Ref;
}
if (lexpr == null)
return null;
+ bool old = ec.TestObsoleteMethodUsage;
+ ec.TestObsoleteMethodUsage = false;
Type ltype = lexpr.ResolveType (ec);
+ ec.TestObsoleteMethodUsage = old;
if ((ltype == TypeManager.void_type) && (dim != "*")) {
Report.Error (1547, Location,
}
public override string Name {
- get {
- return left + dim;
- }
+ get { return left + dim; }
}
public override string FullName {
- get {
- return type.FullName;
- }
+ get { return type.FullName; }
+ }
+
+ public override string GetSignatureForError ()
+ {
+ return left.GetSignatureForError () + dim;
}
}
- public class FixedBufferPtr: Expression {
+ public class FixedBufferPtr : Expression {
Expression array;
public FixedBufferPtr (Expression array, Type array_type, Location l)