2005-07-18 Marek Safar <marek.safar@seznam.cz>
[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 //
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         // The NullType just exists to compare type equality, and for
41         // expressions that might have the `null type'
42         //
43         public class NullType {
44         }
45
46         //
47         // The null Literal constant
48         //
49         public class NullLiteral : Constant {
50                 public static readonly NullLiteral Null;
51
52                 static NullLiteral ()
53                 {
54                         Null = new NullLiteral ();
55                 }
56                         
57                 public NullLiteral ()
58                 {
59                         eclass = ExprClass.Value;
60                 }
61                 
62                 override public string AsString ()
63                 {
64                         return "null";
65                 }
66
67                 public override object GetValue ()
68                 {
69                         return null;
70                 }
71
72                 public override Expression DoResolve (EmitContext ec)
73                 {
74                         type = TypeManager.null_type;
75                         return this;
76                 }
77
78                 public override void Emit (EmitContext ec)
79                 {
80                         ec.ig.Emit (OpCodes.Ldnull);
81                 }
82
83                 public override bool IsDefaultValue {
84                         get {
85                                 return true;
86                         }
87                 }
88
89                 public override bool IsNegative {
90                         get {
91                                 return false;
92                         }
93                 }
94
95                 public override bool IsZeroInteger {
96                         get { return true; }
97                 }
98
99                 public override string GetSignatureForError()
100                 {
101                         return "null";
102                 }
103
104         }
105
106         //
107         // A null literal in a pointer context
108         //
109         public class NullPointer : NullLiteral {
110                 public new static readonly NullLiteral Null;
111
112                 static NullPointer ()
113                 {
114                         Null = new NullPointer ();
115                 }
116
117                 private NullPointer ()
118                 {
119                         type = TypeManager.object_type;
120                 }
121
122                 public override void Emit (EmitContext ec)
123                 {
124                         ILGenerator ig = ec.ig;
125                                 
126                         ig.Emit (OpCodes.Ldc_I4_0);
127                         ig.Emit (OpCodes.Conv_U);
128                 }
129         }
130
131         public class BoolLiteral : BoolConstant {
132                 public BoolLiteral (bool val) : base (val)
133                 {
134                 }
135
136                 public override Expression DoResolve (EmitContext ec)
137                 {
138                         type = TypeManager.bool_type;
139                         return this;
140                 }
141         }
142
143         public class CharLiteral : CharConstant {
144                 public CharLiteral (char c) : base (c)
145                 {
146                 }
147
148                 public override Expression DoResolve (EmitContext ec)
149                 {
150                         type = TypeManager.char_type;
151                         return this;
152                 }
153         }
154
155         public class IntLiteral : IntConstant {
156                 public static IntLiteral One, Zero;
157                 
158                 static IntLiteral ()
159                 {
160                         Zero = new IntLiteral (0);
161                         One = new IntLiteral (1);
162                 }
163                 
164                 public IntLiteral (int l) : base (l)
165                 {
166                 }
167
168                 public override Expression DoResolve (EmitContext ec)
169                 {
170                         type = TypeManager.int32_type;
171                         return this;
172                 }
173         }
174
175         public class UIntLiteral : UIntConstant {
176                 public UIntLiteral (uint l) : base (l)
177                 {
178                 }
179
180                 public override Expression DoResolve (EmitContext ec)
181                 {
182                         type = TypeManager.uint32_type;
183                         return this;
184                 }
185         }
186         
187         public class LongLiteral : LongConstant {
188                 public LongLiteral (long l) : base (l)
189                 {
190                 }
191
192                 public override Expression DoResolve (EmitContext ec)
193                 {
194                         type = TypeManager.int64_type;
195
196                         return this;
197                 }
198         }
199
200         public class ULongLiteral : ULongConstant {
201                 public ULongLiteral (ulong l) : base (l)
202                 {
203                 }
204
205                 public override Expression DoResolve (EmitContext ec)
206                 {
207                         type = TypeManager.uint64_type;
208                         return this;
209                 }
210         }
211         
212         public class FloatLiteral : FloatConstant {
213                 
214                 public FloatLiteral (float f) : base (f)
215                 {
216                 }
217
218                 public override Expression DoResolve (EmitContext ec)
219                 {
220                         type = TypeManager.float_type;
221                         return this;
222                 }
223         }
224
225         public class DoubleLiteral : DoubleConstant {
226                 public DoubleLiteral (double d) : base (d)
227                 {
228                 }
229
230                 public override Expression DoResolve (EmitContext ec)
231                 {
232                         type = TypeManager.double_type;
233
234                         return this;
235                 }
236         }
237
238         public class DecimalLiteral : DecimalConstant {
239                 public DecimalLiteral (decimal d) : base (d)
240                 {
241                 }
242
243                 public override Expression DoResolve (EmitContext ec)
244                 {
245                         type = TypeManager.decimal_type;
246                         return this;
247                 }
248         }
249
250         public class StringLiteral : StringConstant {
251                 public StringLiteral (string s) : base (s)
252                 {
253                 }
254
255                 public override Expression DoResolve (EmitContext ec)
256                 {
257                         type = TypeManager.string_type;
258
259                         return this;
260                 }
261         }
262 }