2005-06-14 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / bmcs / 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 //
26 // I put System.Null just so we do not have to special case it on 
27 // TypeManager.CSharpName
28 //
29 namespace System {
30         //
31         // Represents the Null Type, just used as a placeholder for the type in NullLiteral
32         //
33         public class Null {
34         }
35 }
36         
37 namespace Mono.CSharp {
38
39         //
40         // Unlike C#, VB.NET doesn't understand "signed byte",
41         // "unsigned short", "unsigned int" and "unsigned long" as
42         // primitive types. The NotDefinedAsPrimitiveType just exists
43         // to accomodate this fact.
44         //
45         public class NotDefinedAsPrimitiveType {
46         }
47
48         //
49         // The NullType just exists to compare type equality, and for
50         // expressions that might have the `null type'
51         //
52         public class NullType {
53         }
54
55         //
56         // The null Literal constant
57         //
58         public class NullLiteral : Constant {
59                 public static readonly NullLiteral Null;
60
61                 static NullLiteral ()
62                 {
63                         Null = new NullLiteral ();
64                 }
65                         
66                 public NullLiteral ()
67                 {
68                         eclass = ExprClass.Value;
69                 }
70                 
71                 override public string AsString ()
72                 {
73                         return "null";
74                 }
75
76                 public override object GetValue ()
77                 {
78                         return null;
79                 }
80
81                 public override Expression DoResolve (EmitContext ec)
82                 {
83                         type = TypeManager.null_type;
84                         return this;
85                 }
86
87                 public override void Emit (EmitContext ec)
88                 {
89                         ec.ig.Emit (OpCodes.Ldnull);
90                 }
91
92                 public override CharConstant ConvertToChar ()
93                 {
94                         return new CharConstant ((char) 0);
95                 }
96                 
97                 public override BoolConstant ConvertToBoolean ()
98                 {
99                         return new BoolConstant (false);
100                 }
101
102                 public override ByteConstant ConvertToByte ()
103                 {
104                         return new ByteConstant ((byte) 0);
105                 }
106
107                 public override ShortConstant ConvertToShort ()
108                 {
109                         return new ShortConstant ((short) 0);
110                 }
111
112                 public override DecimalConstant ConvertToDecimal ()
113                 {
114                         return new DecimalConstant ((decimal) 0);
115                 }
116                 
117                 public override DoubleConstant ConvertToDouble ()
118                 {
119                         return new DoubleConstant ((double) 0);
120                 }
121
122                 public override FloatConstant ConvertToFloat ()
123                 {
124                         return new FloatConstant ((float) 0);
125                 }
126
127                 public override LongConstant ConvertToLong ()
128                 {
129                         return new LongConstant ((long) 0);
130                 }
131
132                 public override IntConstant ConvertToInt ()
133                 {
134                         return new IntConstant (0);
135                 }
136                 
137                 public override bool IsNegative {
138                         get {
139                                 return false;
140                         }
141                 }
142
143                 public override bool IsZeroInteger {
144                         get { return true; }
145                 }
146         }
147
148         //
149         // A null literal in a pointer context
150         //
151         public class NullPointer : NullLiteral {
152                 public new static readonly NullLiteral Null;
153
154                 static NullPointer ()
155                 {
156                         Null = new NullPointer ();
157                 }
158                 
159                 private NullPointer ()
160                 {
161                         type = TypeManager.object_type;
162                 }
163
164                 public override void Emit (EmitContext ec)
165                 {
166                         ILGenerator ig = ec.ig;
167                                 
168                         ig.Emit (OpCodes.Ldc_I4_0);
169                         ig.Emit (OpCodes.Conv_U);
170                 }
171         }
172
173         public class BoolLiteral : BoolConstant {
174                 public BoolLiteral (bool val) : base (val)
175                 {
176                 }
177
178                 public override Expression DoResolve (EmitContext ec)
179                 {
180                         type = TypeManager.bool_type;
181                         return this;
182                 }
183         }
184
185         public class CharLiteral : CharConstant {
186                 public CharLiteral (char c) : base (c)
187                 {
188                 }
189
190                 public override Expression DoResolve (EmitContext ec)
191                 {
192                         type = TypeManager.char_type;
193                         return this;
194                 }
195         }
196
197         public class IntLiteral : IntConstant {
198                 public static IntLiteral One, Zero;
199                 
200                 static IntLiteral ()
201                 {
202                         Zero = new IntLiteral (0);
203                         One = new IntLiteral (1);
204                 }
205                 
206                 public IntLiteral (int l) : base (l)
207                 {
208                 }
209
210                 public override Expression DoResolve (EmitContext ec)
211                 {
212                         type = TypeManager.int32_type;
213                         return this;
214                 }
215         }
216
217         public class UIntLiteral : UIntConstant {
218                 public UIntLiteral (uint l) : base (l)
219                 {
220                 }
221
222                 public override Expression DoResolve (EmitContext ec)
223                 {
224                         type = TypeManager.uint32_type;
225                         return this;
226                 }
227         }
228         
229         public class LongLiteral : LongConstant {
230                 public LongLiteral (long l) : base (l)
231                 {
232                 }
233
234                 public override Expression DoResolve (EmitContext ec)
235                 {
236                         type = TypeManager.int64_type;
237
238                         return this;
239                 }
240         }
241
242         public class ULongLiteral : ULongConstant {
243                 public ULongLiteral (ulong l) : base (l)
244                 {
245                 }
246
247                 public override Expression DoResolve (EmitContext ec)
248                 {
249                         type = TypeManager.uint64_type;
250                         return this;
251                 }
252         }
253         
254         public class FloatLiteral : FloatConstant {
255                 
256                 public FloatLiteral (float f) : base (f)
257                 {
258                 }
259
260                 public override Expression DoResolve (EmitContext ec)
261                 {
262                         type = TypeManager.float_type;
263                         return this;
264                 }
265         }
266
267         public class DoubleLiteral : DoubleConstant {
268                 public DoubleLiteral (double d) : base (d)
269                 {
270                 }
271
272                 public override Expression DoResolve (EmitContext ec)
273                 {
274                         type = TypeManager.double_type;
275
276                         return this;
277                 }
278         }
279
280         public class DecimalLiteral : DecimalConstant {
281                 public DecimalLiteral (decimal d) : base (d)
282                 {
283                 }
284
285                 public override Expression DoResolve (EmitContext ec)
286                 {
287                         type = TypeManager.decimal_type;
288                         return this;
289                 }
290         }
291
292         public class StringLiteral : StringConstant {
293                 public StringLiteral (string s) : base (s)
294                 {
295                 }
296
297                 public override Expression DoResolve (EmitContext ec)
298                 {
299                         type = TypeManager.string_type;
300
301                         return this;
302                 }
303         }
304
305         //
306         // VB.NET specific
307         //
308         public class DateLiteral : DateConstant {
309                 public DateLiteral (DateTime s) : base (s)
310                 {
311                 }
312
313                 public override Expression DoResolve (EmitContext ec)
314                 {
315                         type = TypeManager.date_type;
316
317                         return this;
318                 }
319         }
320 }