2002-01-31 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / mcs / literal.cs
1 //
2 // literal.cs: Literal representation for the IL tree.
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.
8 //
9 //
10 // Notice that during parsing we create objects of type Literal, but the
11 // types are not loaded (thats why the Resolve method has to assign the
12 // type at that point).
13 //
14 // Literals differ from the constants in that we know we encountered them
15 // as a literal in the source code (and some extra rules apply there) and
16 // they have to be resolved (since during parsing we have not loaded the
17 // types yet) while constants are created only after types have been loaded
18 // and are fully resolved when born.
19 //
20
21 using System;
22 using System.Reflection;
23 using System.Reflection.Emit;
24
25 namespace Mono.CSharp {
26
27         public class NullLiteral : Constant {
28                 public static readonly NullLiteral Null;
29                 
30                 static NullLiteral ()
31                 {
32                         Null = new NullLiteral ();
33                 }
34                         
35                 public NullLiteral ()
36                 {
37                         if (Null != null)
38                                 throw new Exception ("More than one null has been created!");
39                 }
40                 
41                 override public string AsString ()
42                 {
43                         return "null";
44                 }
45
46                 public override object GetValue ()
47                 {
48                         return null;
49                 }
50
51                 public override Expression DoResolve (EmitContext ec)
52                 {
53                         type = TypeManager.object_type;
54                         return this;
55                 }
56
57                 public override void Emit (EmitContext ec)
58                 {
59                         ec.ig.Emit (OpCodes.Ldnull);
60                 }
61         }
62
63         public class BoolLiteral : BoolConstant {
64                 public BoolLiteral (bool val) : base (val)
65                 {
66                 }
67
68                 public override Expression DoResolve (EmitContext ec)
69                 {
70                         type = TypeManager.bool_type;
71                         return this;
72                 }
73         }
74
75         public class CharLiteral : CharConstant {
76                 public CharLiteral (char c) : base (c)
77                 {
78                 }
79
80                 public override Expression DoResolve (EmitContext ec)
81                 {
82                         type = TypeManager.char_type;
83                         return this;
84                 }
85         }
86
87         public class IntLiteral : IntConstant {
88                 public IntLiteral (int l) : base (l)
89                 {
90                 }
91
92                 public override Expression DoResolve (EmitContext ec)
93                 {
94                         type = TypeManager.int32_type;
95                         return this;
96                 }
97         }
98
99         public class UIntLiteral : UIntConstant {
100                 public UIntLiteral (uint l) : base (l)
101                 {
102                 }
103
104                 public override Expression DoResolve (EmitContext ec)
105                 {
106                         type = TypeManager.uint32_type;
107                         return this;
108                 }
109         }
110         
111         public class LongLiteral : LongConstant {
112                 public LongLiteral (long l) : base (l)
113                 {
114                 }
115
116                 public override Expression DoResolve (EmitContext ec)
117                 {
118                         type = TypeManager.int64_type;
119
120                         return this;
121                 }
122         }
123
124         public class ULongLiteral : ULongConstant {
125                 public ULongLiteral (ulong l) : base (l)
126                 {
127                 }
128
129                 public override Expression DoResolve (EmitContext ec)
130                 {
131                         type = TypeManager.uint64_type;
132                         return this;
133                 }
134         }
135         
136         public class FloatLiteral : FloatConstant {
137                 
138                 public FloatLiteral (float f) : base (f)
139                 {
140                 }
141
142                 public override Expression DoResolve (EmitContext ec)
143                 {
144                         type = TypeManager.float_type;
145                         return this;
146                 }
147         }
148
149         public class DoubleLiteral : DoubleConstant {
150                 public DoubleLiteral (double d) : base (d)
151                 {
152                 }
153
154                 public override Expression DoResolve (EmitContext ec)
155                 {
156                         type = TypeManager.double_type;
157
158                         return this;
159                 }
160         }
161
162         public class DecimalLiteral : DecimalConstant {
163                 public DecimalLiteral (decimal d) : base (d)
164                 {
165                 }
166
167                 public override Expression DoResolve (EmitContext ec)
168                 {
169                         type = TypeManager.decimal_type;
170                         return this;
171                 }
172
173                 public override void Emit (EmitContext ec)
174                 {
175                         throw new Exception ("Implement me");
176                 }
177         }
178
179         public class StringLiteral : StringConstant {
180                 public StringLiteral (string s) : base (s)
181                 {
182                 }
183
184                 public override Expression DoResolve (EmitContext ec)
185                 {
186                         type = TypeManager.string_type;
187
188                         return this;
189                 }
190         }
191 }