//
using System;
+
+#if STATIC
+using IKVM.Reflection.Emit;
+#else
using System.Reflection.Emit;
+#endif
namespace Mono.CSharp {
this.loc = loc;
}
- /// <remarks>
- /// 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.
- /// </remarks>
- public abstract string AsString ();
-
override public string ToString ()
{
- return this.GetType ().Name + " (" + AsString () + ")";
+ return this.GetType ().Name + " (" + GetValueAsLiteral () + ")";
}
/// <summary>
/// </summary>
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)
{
(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);
}
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);
}
}
// 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)
}
}
- public abstract class IntegralConstant : Constant {
+ public abstract class IntegralConstant : Constant
+ {
protected IntegralConstant (Location loc) :
base (loc)
{
GetValue ().ToString (), TypeManager.CSharpName (target));
}
}
+
+ public override string GetValueAsLiteral ()
+ {
+ return GetValue ().ToString ();
+ }
public abstract Constant Increment ();
}
Value = val;
}
- override public string AsString ()
- {
- return Value ? "true" : "false";
- }
-
protected override Expression DoResolve (ResolveContext ec)
{
type = TypeManager.bool_type;
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)
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
protected override Expression DoResolve (ResolveContext ec)
{
type = TypeManager.byte_type;
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)
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 {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitInt (unchecked ((int) Value));
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
{
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);
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitLong (Value);
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
ec.EmitLong (unchecked ((long) Value));
}
- public override string AsString ()
- {
- return Value.ToString ();
- }
-
public override object GetValue ()
{
return Value;
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
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 {
public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType)
{
- enc.Stream.Write (Value);
+ enc.Encode (Value);
}
public override void Emit (EmitContext ec)
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 {
Value = d;
}
- override public string AsString ()
- {
- return Value.ToString () + "M";
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
type = TypeManager.decimal_type;
public override object GetValue ()
{
- return (object) Value;
+ return Value;
+ }
+
+ public override string GetValueAsLiteral ()
+ {
+ return Value.ToString () + "M";
}
public override void Emit (EmitContext ec)
Value = s;
}
- // FIXME: Escape the string.
- override public string AsString ()
- {
- return "\"" + Value + "\"";
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
type = TypeManager.string_type;
{
return Value;
}
+
+ public override string GetValueAsLiteral ()
+ {
+ // FIXME: Escape the string.
+ return "\"" + Value + "\"";
+ }
public override void Emit (EmitContext ec)
{
// 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);
this.type = type;
}
- public override string AsString ()
- {
- return GetSignatureForError ();
- }
-
public override Expression CreateExpressionTree (ResolveContext ec)
{
if (type == InternalType.Null || type == TypeManager.object_type) {
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);
}
}
}
}
- public override string GetSignatureForError ()
- {
- return "null";
- }
-
public override Constant ConvertExplicitly (bool inCheckedContext, TypeSpec targetType)
{
if (targetType.IsPointer) {
}
// 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))
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; }
}
this.side_effect = side_effect;
}
- public override string AsString ()
- {
- return value.AsString ();
- }
-
protected override Expression DoResolve (ResolveContext rc)
{
value = value.Resolve (rc);
return value.GetValue ();
}
+ public override string GetValueAsLiteral ()
+ {
+ return value.GetValueAsLiteral ();
+ }
+
public override void Emit (EmitContext ec)
{
side_effect.EmitSideEffect (ec);