X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fliteral.cs;h=a307030db6a687916a72976177cf307914acdd8b;hb=f282c09a4f474c0994bf739e0b300043fd342b80;hp=8aea831e68eec60b75303923d77ac62e09b44302;hpb=c07b3a44d91fb7e3e4872ec3b72b8ee2c3e7c399;p=mono.git diff --git a/mcs/mcs/literal.cs b/mcs/mcs/literal.cs index 8aea831e68e..a307030db6a 100755 --- a/mcs/mcs/literal.cs +++ b/mcs/mcs/literal.cs @@ -6,66 +6,37 @@ // // (C) 2001 Ximian, Inc. // +// +// Notice that during parsing we create objects of type Literal, but the +// types are not loaded (thats why the Resolve method has to assign the +// type at that point). +// +// Literals differ from the constants in that we know we encountered them +// as a literal in the source code (and some extra rules apply there) and +// they have to be resolved (since during parsing we have not loaded the +// types yet) while constants are created only after types have been loaded +// and are fully resolved when born. +// using System; using System.Reflection; using System.Reflection.Emit; -namespace CIR { - public abstract class Literal : Expression { - // - // This is different from ToString in that ToString - // is supposed to be there for debugging purposes, - // and is not guarantee 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 AsString (); - } - - static public string descape (char c) - { - switch (c){ - case '\a': - return "\\a"; - case '\b': - return "\\b"; - case '\n': - return "\\n"; - case '\t': - return "\\t"; - case '\v': - return "\\v"; - case '\r': - return "\\r"; - case '\\': - return "\\\\"; - case '\f': - return "\\f"; - case '\0': - return "\\0"; - case '"': - return "\\\""; - case '\'': - return "\\\'"; - } - return c.ToString (); - } +namespace Mono.CSharp { - protected Literal () + public class NullLiteral : Constant { + public static readonly NullLiteral Null; + + static NullLiteral () { - eclass = ExprClass.Value; + Null = new NullLiteral (); } - } - - public class NullLiteral : Literal { + public NullLiteral () { + if (Null != null) + throw new Exception ("More than one null has been created!"); + eclass = ExprClass.Value; } override public string AsString () @@ -73,10 +44,14 @@ namespace CIR { return "null"; } + public override object GetValue () + { + return null; + } + public override Expression DoResolve (EmitContext ec) { type = TypeManager.object_type; - eclass = ExprClass.Value; return this; } @@ -86,181 +61,57 @@ namespace CIR { } } - public class BoolLiteral : Literal { - bool val; - - public BoolLiteral (bool val) - { - this.val = val; - } - - override public string AsString () + public class BoolLiteral : BoolConstant { + public BoolLiteral (bool val) : base (val) { - return val ? "true" : "false"; } public override Expression DoResolve (EmitContext ec) { type = TypeManager.bool_type; - return this; } - - public override void Emit (EmitContext ec) - { - if (val) - ec.ig.Emit (OpCodes.Ldc_I4_1); - else - ec.ig.Emit (OpCodes.Ldc_I4_0); - } } - public class CharLiteral : Literal { - char c; - - public CharLiteral (char c) - { - this.c = c; - } - - override public string AsString () + public class CharLiteral : CharConstant { + public CharLiteral (char c) : base (c) { - return "\"" + descape (c) + "\""; } public override Expression DoResolve (EmitContext ec) { type = TypeManager.char_type; - return this; } - - public override void Emit (EmitContext ec) - { - IntLiteral.EmitInt (ec.ig, c); - } } - public class IntLiteral : Literal { - public readonly int Value; - - public IntLiteral (int l) - { - Value = l; - } - - override public string AsString () + public class IntLiteral : IntConstant { + public IntLiteral (int l) : base (l) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) { type = TypeManager.int32_type; - return this; } - - public override void Emit (EmitContext ec) - { - ILGenerator ig = ec.ig; - - EmitInt (ig, Value); - } - - static public void EmitInt (ILGenerator ig, int i) - { - switch (i){ - case -1: - ig.Emit (OpCodes.Ldc_I4_M1); - break; - - case 0: - ig.Emit (OpCodes.Ldc_I4_0); - break; - - case 1: - ig.Emit (OpCodes.Ldc_I4_1); - break; - - case 2: - ig.Emit (OpCodes.Ldc_I4_2); - break; - - case 3: - ig.Emit (OpCodes.Ldc_I4_3); - break; - - case 4: - ig.Emit (OpCodes.Ldc_I4_4); - break; - - case 5: - ig.Emit (OpCodes.Ldc_I4_5); - break; - - case 6: - ig.Emit (OpCodes.Ldc_I4_6); - break; - - case 7: - ig.Emit (OpCodes.Ldc_I4_7); - break; - - case 8: - ig.Emit (OpCodes.Ldc_I4_8); - break; - - default: - if (i > 0 && i < 127){ - ig.Emit (OpCodes.Ldc_I4_S, (sbyte) i); - } else - ig.Emit (OpCodes.Ldc_I4, i); - break; - } - } } - public class UIntLiteral : Literal { - public readonly uint Value; - - public UIntLiteral (uint l) - { - Value = l; - } - - override public string AsString () + public class UIntLiteral : UIntConstant { + public UIntLiteral (uint l) : base (l) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) { type = TypeManager.uint32_type; - return this; } - - public override void Emit (EmitContext ec) - { - ILGenerator ig = ec.ig; - - IntLiteral.EmitInt (ig, unchecked ((int) Value)); - } - } - public class LongLiteral : Literal { - public readonly long Value; - - public LongLiteral (long l) - { - Value = l; - } - - override public string AsString () + public class LongLiteral : LongConstant { + public LongLiteral (long l) : base (l) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) @@ -269,85 +120,36 @@ namespace CIR { return this; } - - public override void Emit (EmitContext ec) - { - ILGenerator ig = ec.ig; - - EmitLong (ig, Value); - } - - static public void EmitLong (ILGenerator ig, long l) - { - ig.Emit (OpCodes.Ldc_I8, l); - } } - public class ULongLiteral : Literal { - public readonly ulong Value; - - public ULongLiteral (ulong l) - { - Value = l; - } - - override public string AsString () + public class ULongLiteral : ULongConstant { + public ULongLiteral (ulong l) : base (l) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) { type = TypeManager.uint64_type; - return this; } - - public override void Emit (EmitContext ec) - { - ILGenerator ig = ec.ig; - - LongLiteral.EmitLong (ig, unchecked ((long) Value)); - } } - public class FloatLiteral : Literal { - public readonly float Value; - - public FloatLiteral (float f) - { - Value = f; - } - - override public string AsString () + public class FloatLiteral : FloatConstant { + + public FloatLiteral (float f) : base (f) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) { type = TypeManager.float_type; - return this; } - - public override void Emit (EmitContext ec) - { - ec.ig.Emit (OpCodes.Ldc_R4, Value); - } } - public class DoubleLiteral : Literal { - public readonly double Value; - - public DoubleLiteral (double d) - { - Value = d; - } - - override public string AsString () + public class DoubleLiteral : DoubleConstant { + public DoubleLiteral (double d) : base (d) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) @@ -356,51 +158,23 @@ namespace CIR { return this; } - - public override void Emit (EmitContext ec) - { - ec.ig.Emit (OpCodes.Ldc_R8, Value); - } } - public class DecimalLiteral : Literal { - public readonly decimal Value; - - public DecimalLiteral (decimal d) - { - Value = d; - } - - override public string AsString () + public class DecimalLiteral : DecimalConstant { + public DecimalLiteral (decimal d) : base (d) { - return Value.ToString (); } public override Expression DoResolve (EmitContext ec) { type = TypeManager.decimal_type; - return this; } - - public override void Emit (EmitContext ec) - { - throw new Exception ("Implement me"); - } } - public class StringLiteral : Literal { - string s; - - public StringLiteral (string s) + public class StringLiteral : StringConstant { + public StringLiteral (string s) : base (s) { - this.s = s; - } - - // FIXME: Escape the string. - override public string AsString () - { - return "\"" + s + "\""; } public override Expression DoResolve (EmitContext ec) @@ -409,10 +183,5 @@ namespace CIR { return this; } - - public override void Emit (EmitContext ec) - { - ec.ig.Emit (OpCodes.Ldstr, s); - } } }