Update helper errors to use the -pkg syntax when needed, patch from iain@mccoy.id...
[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 IsNegative {
84                         get {
85                                 return false;
86                         }
87                 }
88
89                 public override bool IsZeroInteger {
90                         get { return true; }
91                 }
92         }
93
94         //
95         // A null literal in a pointer context
96         //
97         public class NullPointer : NullLiteral {
98                 public new static readonly NullLiteral Null;
99
100                 static NullPointer ()
101                 {
102                         Null = new NullPointer ();
103                 }
104
105                 private NullPointer ()
106                 {
107                         type = TypeManager.object_type;
108                 }
109
110                 public override void Emit (EmitContext ec)
111                 {
112                         ILGenerator ig = ec.ig;
113                                 
114                         ig.Emit (OpCodes.Ldc_I4_0);
115                         ig.Emit (OpCodes.Conv_U);
116                 }
117         }
118
119         public class BoolLiteral : BoolConstant {
120                 public BoolLiteral (bool val) : base (val)
121                 {
122                 }
123
124                 public override Expression DoResolve (EmitContext ec)
125                 {
126                         type = TypeManager.bool_type;
127                         return this;
128                 }
129         }
130
131         public class CharLiteral : CharConstant {
132                 public CharLiteral (char c) : base (c)
133                 {
134                 }
135
136                 public override Expression DoResolve (EmitContext ec)
137                 {
138                         type = TypeManager.char_type;
139                         return this;
140                 }
141         }
142
143         public class IntLiteral : IntConstant {
144                 public static IntLiteral One, Zero;
145                 
146                 static IntLiteral ()
147                 {
148                         Zero = new IntLiteral (0);
149                         One = new IntLiteral (1);
150                 }
151                 
152                 public IntLiteral (int l) : base (l)
153                 {
154                 }
155
156                 public override Expression DoResolve (EmitContext ec)
157                 {
158                         type = TypeManager.int32_type;
159                         return this;
160                 }
161         }
162
163         public class UIntLiteral : UIntConstant {
164                 public UIntLiteral (uint l) : base (l)
165                 {
166                 }
167
168                 public override Expression DoResolve (EmitContext ec)
169                 {
170                         type = TypeManager.uint32_type;
171                         return this;
172                 }
173         }
174         
175         public class LongLiteral : LongConstant {
176                 public LongLiteral (long l) : base (l)
177                 {
178                 }
179
180                 public override Expression DoResolve (EmitContext ec)
181                 {
182                         type = TypeManager.int64_type;
183
184                         return this;
185                 }
186         }
187
188         public class ULongLiteral : ULongConstant {
189                 public ULongLiteral (ulong l) : base (l)
190                 {
191                 }
192
193                 public override Expression DoResolve (EmitContext ec)
194                 {
195                         type = TypeManager.uint64_type;
196                         return this;
197                 }
198         }
199         
200         public class FloatLiteral : FloatConstant {
201                 
202                 public FloatLiteral (float f) : base (f)
203                 {
204                 }
205
206                 public override Expression DoResolve (EmitContext ec)
207                 {
208                         type = TypeManager.float_type;
209                         return this;
210                 }
211         }
212
213         public class DoubleLiteral : DoubleConstant {
214                 public DoubleLiteral (double d) : base (d)
215                 {
216                 }
217
218                 public override Expression DoResolve (EmitContext ec)
219                 {
220                         type = TypeManager.double_type;
221
222                         return this;
223                 }
224         }
225
226         public class DecimalLiteral : DecimalConstant {
227                 public DecimalLiteral (decimal d) : base (d)
228                 {
229                 }
230
231                 public override Expression DoResolve (EmitContext ec)
232                 {
233                         type = TypeManager.decimal_type;
234                         return this;
235                 }
236         }
237
238         public class StringLiteral : StringConstant {
239                 public StringLiteral (string s) : base (s)
240                 {
241                 }
242
243                 public override Expression DoResolve (EmitContext ec)
244                 {
245                         type = TypeManager.string_type;
246
247                         return this;
248                 }
249         }
250 }