X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fconstant.cs;h=81d2c9604c33482226971554f6c8f736937bc64d;hb=3204e13673602e695cfaf5520ae45440dcb006ed;hp=c04ad0246df1417ee4d111d3245a9f5e4e2370c1;hpb=175cf27dbe0eee4bd9ff2de5618e8ed3465dbf32;p=mono.git
diff --git a/mcs/mcs/constant.cs b/mcs/mcs/constant.cs
index c04ad0246df..81d2c9604c3 100644
--- a/mcs/mcs/constant.cs
+++ b/mcs/mcs/constant.cs
@@ -10,7 +10,12 @@
//
using System;
+
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
using System.Reflection.Emit;
+#endif
namespace Mono.CSharp {
@@ -24,19 +29,9 @@ namespace Mono.CSharp {
this.loc = loc;
}
- ///
- /// This is different from ToString in that ToString
- /// is supposed to be there for debugging purposes,
- /// and is not guaranteed to be useful for anything else,
- /// AsString() will provide something that can be used
- /// for round-tripping C# code. Maybe it can be used
- /// for IL assembly as well.
- ///
- public abstract string AsString ();
-
override public string ToString ()
{
- return this.GetType ().Name + " (" + AsString () + ")";
+ return this.GetType ().Name + " (" + GetValueAsLiteral () + ")";
}
///
@@ -45,10 +40,17 @@ namespace Mono.CSharp {
///
public abstract object GetValue ();
+ public abstract string GetValueAsLiteral ();
+
+#if !STATIC
+ //
+ // Returns an object value which is typed to contant type
+ //
public virtual object GetTypedValue ()
{
return GetValue ();
}
+#endif
public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
{
@@ -56,7 +58,7 @@ namespace Mono.CSharp {
(TypeManager.IsPrimitiveType (target) || type == TypeManager.decimal_type) &&
(TypeManager.IsPrimitiveType (type) || type == TypeManager.decimal_type)) {
ec.Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
- AsString (), TypeManager.CSharpName (target));
+ GetValueAsLiteral (), TypeManager.CSharpName (target));
} else {
base.Error_ValueCannotBeConverted (ec, loc, target, expl);
}
@@ -173,14 +175,15 @@ namespace Mono.CSharp {
return TryReduce (ec, target_type);
}
catch (OverflowException) {
- if (ec.ConstantCheckState) {
- ec.Report.Error (221, loc, "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
- GetValue ().ToString (), TypeManager.CSharpName (target_type));
+ if (ec.ConstantCheckState && Type.BuildinType != BuildinTypeSpec.Type.Decimal) {
+ ec.Report.Error (221, loc,
+ "Constant value `{0}' cannot be converted to a `{1}' (use `unchecked' syntax to override)",
+ GetValueAsLiteral (), target_type.GetSignatureForError ());
} else {
Error_ValueCannotBeConverted (ec, loc, target_type, false);
}
- return New.Constantify (target_type).Resolve (ec);
+ return New.Constantify (target_type, loc).Resolve (ec);
}
}
@@ -249,14 +252,24 @@ namespace Mono.CSharp {
// do nothing
}
+ public sealed override Expression Clone (CloneContext clonectx)
+ {
+ // No cloning is not needed for constants
+ return this;
+ }
+
protected override void CloneTo (CloneContext clonectx, Expression target)
{
- // CloneTo: Nothing, we do not keep any state on this expression
+ throw new NotSupportedException ("should not be reached");
}
public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
{
+#if STATIC
+ return base.MakeExpression (ctx);
+#else
return System.Linq.Expressions.Expression.Constant (GetTypedValue (), type.GetMetaInfo ());
+#endif
}
public new Constant Resolve (ResolveContext rc)
@@ -281,7 +294,8 @@ namespace Mono.CSharp {
}
}
- public abstract class IntegralConstant : Constant {
+ public abstract class IntegralConstant : Constant
+ {
protected IntegralConstant (Location loc) :
base (loc)
{
@@ -299,6 +313,11 @@ namespace Mono.CSharp {
GetValue ().ToString (), TypeManager.CSharpName (target));
}
}
+
+ public override string GetValueAsLiteral ()
+ {
+ return GetValue ().ToString ();
+ }
public abstract Constant Increment ();
}
@@ -312,11 +331,6 @@ namespace Mono.CSharp {
Value = val;
}
- override public string AsString ()
- {
- return Value ? "true" : "false";
- }
-
protected override Expression DoResolve (ResolveContext ec)
{
type = TypeManager.bool_type;
@@ -329,9 +343,14 @@ namespace Mono.CSharp {
return (object) Value;
}
+ public override string GetValueAsLiteral ()
+ {
+ return Value ? "true" : "false";
+ }
+
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -376,7 +395,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -384,11 +403,6 @@ namespace Mono.CSharp {
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
protected override Expression DoResolve (ResolveContext ec)
{
type = TypeManager.byte_type;
@@ -481,7 +495,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write ((ushort) Value);
+ enc.Encode ((ushort) Value);
}
public override void Emit (EmitContext ec)
@@ -518,14 +532,14 @@ namespace Mono.CSharp {
return c.ToString ();
}
- public override string AsString ()
+ public override object GetValue ()
{
- return "\"" + descape (Value) + "\"";
+ return Value;
}
- public override object GetValue ()
+ public override string GetValueAsLiteral ()
{
- return Value;
+ return "\"" + descape (Value) + "\"";
}
public override bool IsDefaultValue {
@@ -605,7 +619,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -613,11 +627,6 @@ namespace Mono.CSharp {
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -711,7 +720,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -719,11 +728,6 @@ namespace Mono.CSharp {
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -829,7 +833,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -837,11 +841,6 @@ namespace Mono.CSharp {
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -941,7 +940,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -949,11 +948,6 @@ namespace Mono.CSharp {
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -1122,7 +1116,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -1130,11 +1124,6 @@ namespace Mono.CSharp {
ec.EmitInt (unchecked ((int) Value));
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -1171,7 +1160,7 @@ namespace Mono.CSharp {
{
if (target_type == TypeManager.byte_type) {
if (in_checked_context){
- if (Value < Char.MinValue || Value > Char.MaxValue)
+ if (Value < 0 || Value > byte.MaxValue)
throw new OverflowException ();
}
return new ByteConstant ((byte) Value, Location);
@@ -1245,7 +1234,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -1253,11 +1242,6 @@ namespace Mono.CSharp {
ec.EmitLong (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -1384,7 +1368,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -1392,11 +1376,6 @@ namespace Mono.CSharp {
ec.EmitLong (unchecked ((long) Value));
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
@@ -1501,7 +1480,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -1509,14 +1488,14 @@ namespace Mono.CSharp {
ec.Emit (OpCodes.Ldc_R4, Value);
}
- public override string AsString ()
+ public override object GetValue ()
{
- return Value.ToString ();
+ return Value;
}
- public override object GetValue ()
+ public override string GetValueAsLiteral ()
{
- return Value;
+ return Value.ToString ();
}
public override bool IsDefaultValue {
@@ -1624,7 +1603,7 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
@@ -1632,14 +1611,14 @@ namespace Mono.CSharp {
ec.Emit (OpCodes.Ldc_R8, Value);
}
- public override string AsString ()
+ public override object GetValue ()
{
- return Value.ToString ();
+ return Value;
}
- public override object GetValue ()
+ public override string GetValueAsLiteral ()
{
- return Value;
+ return Value.ToString ();
}
public override bool IsDefaultValue {
@@ -1738,11 +1717,6 @@ namespace Mono.CSharp {
Value = d;
}
- override public string AsString ()
- {
- return Value.ToString () + "M";
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
type = TypeManager.decimal_type;
@@ -1752,7 +1726,12 @@ namespace Mono.CSharp {
public override object GetValue ()
{
- return (object) Value;
+ return Value;
+ }
+
+ public override string GetValueAsLiteral ()
+ {
+ return Value.ToString () + "M";
}
public override void Emit (EmitContext ec)
@@ -1863,12 +1842,6 @@ namespace Mono.CSharp {
Value = s;
}
- // FIXME: Escape the string.
- override public string AsString ()
- {
- return "\"" + Value + "\"";
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
type = TypeManager.string_type;
@@ -1880,6 +1853,12 @@ namespace Mono.CSharp {
{
return Value;
}
+
+ public override string GetValueAsLiteral ()
+ {
+ // FIXME: Escape the string.
+ return "\"" + Value + "\"";
+ }
public override void Emit (EmitContext ec)
{
@@ -1892,7 +1871,7 @@ namespace Mono.CSharp {
// Use string.Empty for both literals and constants even if
// it's not allowed at language level
//
- if (Value.Length == 0 && RootContext.Optimize && !TypeManager.IsEqual (ec.CurrentType, TypeManager.string_type)) {
+ if (Value.Length == 0 && RootContext.Optimize && ec.CurrentType != TypeManager.string_type) {
if (TypeManager.string_empty == null)
TypeManager.string_empty = TypeManager.GetPredefinedField (TypeManager.string_type, "Empty", loc, TypeManager.string_type);
@@ -1950,11 +1929,6 @@ namespace Mono.CSharp {
this.type = type;
}
- public override string AsString ()
- {
- return GetSignatureForError ();
- }
-
public override Expression CreateExpressionTree (ResolveContext ec)
{
if (type == InternalType.Null || type == TypeManager.object_type) {
@@ -1975,17 +1949,17 @@ namespace Mono.CSharp {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
// Type it as string cast
- if (targetType == TypeManager.object_type || targetType == TypeManager.null_type)
+ if (targetType == TypeManager.object_type || targetType == InternalType.Null)
enc.Encode (TypeManager.string_type);
var ac = targetType as ArrayContainer;
if (ac != null) {
- if (ac.Rank != 1)
+ if (ac.Rank != 1 || ac.Element.IsArray)
base.EncodeAttributeValue (rc, enc, targetType);
else
- enc.Stream.Write (uint.MaxValue);
+ enc.Encode (uint.MaxValue);
} else {
- enc.Stream.Write (byte.MaxValue);
+ enc.Encode (byte.MaxValue);
}
}
@@ -2004,11 +1978,6 @@ namespace Mono.CSharp {
}
}
- public override string GetSignatureForError ()
- {
- return "null";
- }
-
public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
{
if (targetType.IsPointer) {
@@ -2019,7 +1988,7 @@ namespace Mono.CSharp {
}
// Exlude internal compiler types
- if (targetType == InternalType.AnonymousMethod)
+ if (targetType.Kind == MemberKind.InternalCompilerType && targetType != InternalType.Dynamic && targetType != InternalType.Null)
return null;
if (!IsLiteral && !Convert.ImplicitStandardConversionExists (this, targetType))
@@ -2039,11 +2008,21 @@ namespace Mono.CSharp {
return ConvertExplicitly (false, targetType);
}
+ public override string GetSignatureForError ()
+ {
+ return "null";
+ }
+
public override object GetValue ()
{
return null;
}
+ public override string GetValueAsLiteral ()
+ {
+ return GetSignatureForError ();
+ }
+
public override bool IsDefaultValue {
get { return true; }
}
@@ -2078,11 +2057,6 @@ namespace Mono.CSharp {
this.side_effect = side_effect;
}
- public override string AsString ()
- {
- return value.AsString ();
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
value = value.Resolve (rc);
@@ -2097,6 +2071,11 @@ namespace Mono.CSharp {
return value.GetValue ();
}
+ public override string GetValueAsLiteral ()
+ {
+ return value.GetValueAsLiteral ();
+ }
+
public override void Emit (EmitContext ec)
{
side_effect.EmitSideEffect (ec);