2004-12-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / literal.cs
old mode 100755 (executable)
new mode 100644 (file)
index 826f229..a998bb2
@@ -6,61 +6,57 @@
 //
 // (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 {
-               // <summary>
-               //   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.
-               // </summary>
-               public abstract string AsString ();
-
-               override public string ToString ()
-               {
-                       return AsString ();
-               }
+//
+// I put System.Null just so we do not have to special case it on 
+// TypeManager.CSharpName
+//
+namespace System {
+       //
+       // Represents the Null Type, just used as a placeholder for the type in NullLiteral
+       //
+       public class Null {
+       }
+}
+       
+namespace Mono.CSharp {
+
+       //
+       // The NullType just exists to compare type equality, and for
+       // expressions that might have the `null type'
+       //
+       public class NullType {
+       }
 
-               static public string descape (char c)
+       //
+       // The null Literal constant
+       //
+       public class NullLiteral : Constant {
+               public static readonly NullLiteral Null;
+
+               static NullLiteral ()
                {
-                       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 ();
+                       Null = new NullLiteral ();
                }
-       }
-
-       public class NullLiteral : Literal {
+                       
                public NullLiteral ()
                {
+                       eclass = ExprClass.Value;
                }
                
                override public string AsString ()
@@ -68,201 +64,187 @@ namespace CIR {
                        return "null";
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override object GetValue ()
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.object_type;
+                       return null;
+               }
 
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       type = TypeManager.null_type;
                        return this;
                }
 
                public override void Emit (EmitContext ec)
                {
+                       ec.ig.Emit (OpCodes.Ldnull);
                }
-       }
-
-       public class BoolLiteral : Literal {
-               static Type bool_type = Type.GetType ("System.Bool");
-               bool val;
                
-               public BoolLiteral (bool val)
-               {
-                       this.val = val;
+               public override bool IsNegative {
+                       get {
+                               return false;
+                       }
                }
 
-               override public string AsString ()
-               {
-                       return val ? "true" : "false";
+               public override bool IsZeroInteger {
+                       get { return true; }
                }
+       }
+
+       //
+       // A null literal in a pointer context
+       //
+       public class NullPointer : NullLiteral {
+               public new static readonly NullLiteral Null;
 
-               public override Expression Resolve (TypeContainer tc)
+               static NullPointer ()
                {
-                       eclass = ExprClass.Value;
-                       type = bool_type;
+                       Null = new NullPointer ();
+               }
 
-                       return this;
+               private NullPointer ()
+               {
+                       type = TypeManager.object_type;
                }
 
                public override void Emit (EmitContext ec)
                {
+                       ILGenerator ig = ec.ig;
+                               
+                       ig.Emit (OpCodes.Ldc_I4_0);
+                       ig.Emit (OpCodes.Conv_U);
                }
        }
 
-       public class CharLiteral : Literal {
-               char c;
-               
-               public CharLiteral (char c)
+       public class BoolLiteral : BoolConstant {
+               public BoolLiteral (bool val) : base (val)
                {
-                       this.c = c;
                }
 
-               override public string AsString ()
+               public override Expression DoResolve (EmitContext ec)
                {
-                       return "\"" + descape (c) + "\"";
+                       type = TypeManager.bool_type;
+                       return this;
                }
+       }
 
-               public override Expression Resolve (TypeContainer tc)
+       public class CharLiteral : CharConstant {
+               public CharLiteral (char c) : base (c)
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.char_type;
-
-                       return this;
                }
 
-               public override void Emit (EmitContext ec)
+               public override Expression DoResolve (EmitContext ec)
                {
+                       type = TypeManager.char_type;
+                       return this;
                }
        }
 
-       public class IntLiteral : Literal {
-               int i;
-
-               public IntLiteral (int l)
+       public class IntLiteral : IntConstant {
+               public static IntLiteral One, Zero;
+               
+               static IntLiteral ()
                {
-                       i = l;
+                       Zero = new IntLiteral (0);
+                       One = new IntLiteral (1);
                }
-
-               override public string AsString ()
+               
+               public IntLiteral (int l) : base (l)
                {
-                       return i.ToString ();
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Value;
                        type = TypeManager.int32_type;
-
                        return this;
                }
+       }
 
-               public override void Emit (EmitContext ec)
+       public class UIntLiteral : UIntConstant {
+               public UIntLiteral (uint l) : base (l)
                {
                }
-       }
 
-       public class FloatLiteral : Literal {
-               float f;
-
-               public FloatLiteral (float f)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       this.f = f;
+                       type = TypeManager.uint32_type;
+                       return this;
                }
-
-               override public string AsString ()
+       }
+       
+       public class LongLiteral : LongConstant {
+               public LongLiteral (long l) : base (l)
                {
-                       return f.ToString ();
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.float_type;
+                       type = TypeManager.int64_type;
 
                        return this;
                }
-
-               public override void Emit (EmitContext ec)
-               {
-               }
        }
 
-       public class DoubleLiteral : Literal {
-               double d;
-
-               public DoubleLiteral (double d)
-               {
-                       this.d = d;
-               }
-
-               override public string AsString ()
+       public class ULongLiteral : ULongConstant {
+               public ULongLiteral (ulong l) : base (l)
                {
-                       return d.ToString ();
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.double_type;
-
+                       type = TypeManager.uint64_type;
                        return this;
                }
-
-               public override void Emit (EmitContext ec)
+       }
+       
+       public class FloatLiteral : FloatConstant {
+               
+               public FloatLiteral (float f) : base (f)
                {
                }
-       }
 
-       public class DecimalLiteral : Literal {
-               decimal d;
-
-               public DecimalLiteral (decimal d)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       this.d = d;
+                       type = TypeManager.float_type;
+                       return this;
                }
+       }
 
-               override public string AsString ()
+       public class DoubleLiteral : DoubleConstant {
+               public DoubleLiteral (double d) : base (d)
                {
-                       return d.ToString ();
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Value;
-                       type = TypeManager.decimal_type;
+                       type = TypeManager.double_type;
 
                        return this;
                }
+       }
 
-               public override void Emit (EmitContext ec)
+       public class DecimalLiteral : DecimalConstant {
+               public DecimalLiteral (decimal d) : base (d)
                {
                }
-       }
-
-       public class StringLiteral : Literal {
-               string s;
 
-               public StringLiteral (string s)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       this.s = s;
+                       type = TypeManager.decimal_type;
+                       return this;
                }
+       }
 
-               // FIXME: Escape the string.
-               override public string AsString ()
+       public class StringLiteral : StringConstant {
+               public StringLiteral (string s) : base (s)
                {
-                       return "\"" + s + "\"";
                }
 
-               public override Expression Resolve (TypeContainer tc)
+               public override Expression DoResolve (EmitContext ec)
                {
-                       eclass = ExprClass.Value;
                        type = TypeManager.string_type;
 
                        return this;
                }
-
-               public override void Emit (EmitContext ec)
-               {
-                       ec.ig.Emit (OpCodes.Ldstr, s);
-               }
        }
 }