2008-05-01 Rodrigo Kumpera <rkumpera@novell.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 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // Copyright 2001 Ximian, Inc.
9 //
10 //
11 // Notice that during parsing we create objects of type Literal, but the
12 // types are not loaded (thats why the Resolve method has to assign the
13 // type at that point).
14 //
15 // Literals differ from the constants in that we know we encountered them
16 // as a literal in the source code (and some extra rules apply there) and
17 // they have to be resolved (since during parsing we have not loaded the
18 // types yet) while constants are created only after types have been loaded
19 // and are fully resolved when born.
20 //
21
22 using System;
23 using System.Reflection;
24 using System.Reflection.Emit;
25
26 namespace Mono.CSharp {
27
28         //
29         // The null literal
30         //
31         public class NullLiteral : Constant {
32                 public NullLiteral (Location loc):
33                         base (loc)
34                 {
35                         eclass = ExprClass.Value;
36                         type = typeof (NullLiteral);
37                 }
38
39                 override public string AsString ()
40                 {
41                         return GetSignatureForError ();
42                 }
43                 
44                 public override Expression CreateExpressionTree (EmitContext ec)
45                 {
46                         type = TypeManager.object_type;
47                         return base.CreateExpressionTree (ec);
48                 }               
49
50                 public override Expression DoResolve (EmitContext ec)
51                 {
52                         return this;
53                 }
54
55                 public override void Emit (EmitContext ec)
56                 {
57                         ec.ig.Emit (OpCodes.Ldnull);
58                 }
59
60                 public override string ExprClassName {
61                         get {
62                                 return GetSignatureForError ();
63                         }
64                 }
65
66                 public override string GetSignatureForError ()
67                 {
68                         return "null";
69                 }
70
71                 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type t, bool expl)
72                 {
73                         if (TypeManager.IsGenericParameter (t)) {
74                                 Report.Error(403, loc,
75                                         "Cannot convert null to the type parameter `{0}' because it could be a value " +
76                                         "type. Consider using `default ({0})' instead", t.Name);
77                         } else {
78                                 Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
79                                         TypeManager.CSharpName(t));
80                         }
81                 }
82
83                 public override Constant ConvertImplicitly (Type targetType)
84                 {
85                         if (targetType.IsPointer)
86                                 return new EmptyConstantCast (NullPointer.Null, targetType);
87
88                         if (TypeManager.IsReferenceType (targetType))
89                                 return new EmptyConstantCast (this, targetType);
90
91                         return null;
92                 }
93
94                 public override object GetValue ()
95                 {
96                         return null;
97                 }
98
99                 public override Constant Increment ()
100                 {
101                         throw new NotSupportedException ();
102                 }
103
104                 public override bool IsDefaultValue 
105                 {
106                         get { return true; }
107                 }
108
109                 public override bool IsLiteral {
110                         get { return true; }
111                 }
112
113                 public override bool IsNegative {
114                         get { return false; }
115                 }
116
117                 public override bool IsNull {
118                         get { return true; }
119                 }
120
121                 public override bool IsZeroInteger {
122                         get { return true; }
123                 }
124
125                 public override Constant ConvertExplicitly(bool inCheckedContext, Type target_type)
126                 {
127                         if (!TypeManager.IsValueType (target_type))
128                                 return new EmptyConstantCast (this, target_type);
129
130                         return null;
131                 }
132         }
133
134         //
135         // A null literal in a pointer context
136         //
137         public class NullPointer : NullLiteral {
138                 public static readonly NullLiteral Null = new NullPointer ();
139
140                 private NullPointer ():
141                         base (Location.Null)
142                 {
143                         type = TypeManager.object_type;
144                 }
145
146                 public override void Emit (EmitContext ec)
147                 {
148                         ILGenerator ig = ec.ig;
149                                 
150                         // TODO: why not use Ldnull instead ?
151                         ig.Emit (OpCodes.Ldc_I4_0);
152                         ig.Emit (OpCodes.Conv_U);
153                 }
154         }
155
156         public class BoolLiteral : BoolConstant {
157                 public BoolLiteral (bool val, Location loc) : base (val, loc)
158                 {
159                 }
160
161                 public override Expression DoResolve (EmitContext ec)
162                 {
163                         type = TypeManager.bool_type;
164                         return this;
165                 }
166
167                 public override bool IsLiteral {
168                         get { return true; }
169                 }
170         }
171
172         public class CharLiteral : CharConstant {
173                 public CharLiteral (char c, Location loc) : base (c, loc)
174                 {
175                 }
176
177                 public override Expression DoResolve (EmitContext ec)
178                 {
179                         type = TypeManager.char_type;
180                         return this;
181                 }
182
183                 public override bool IsLiteral {
184                         get { return true; }
185                 }
186         }
187
188         public class IntLiteral : IntConstant {
189                 public IntLiteral (int l, Location loc) : base (l, loc)
190                 {
191                 }
192
193                 public override Expression DoResolve (EmitContext ec)
194                 {
195                         type = TypeManager.int32_type;
196                         return this;
197                 }
198
199                 public override Constant ConvertImplicitly (Type type)
200                 {
201                         ///
202                         /// The 0 literal can be converted to an enum value,
203                         ///
204                         if (Value == 0 && TypeManager.IsEnumType (type)) {
205                                 Constant c = ConvertImplicitly (TypeManager.GetEnumUnderlyingType (type));
206                                 if (c == null)
207                                         return null;
208
209                                 return new EnumConstant (c, type);
210                         }
211                         return base.ConvertImplicitly (type);
212                 }
213
214                 public override bool IsLiteral {
215                         get { return true; }
216                 }
217         }
218
219         public class UIntLiteral : UIntConstant {
220                 public UIntLiteral (uint l, Location loc) : base (l, loc)
221                 {
222                 }
223
224                 public override Expression DoResolve (EmitContext ec)
225                 {
226                         type = TypeManager.uint32_type;
227                         return this;
228                 }
229
230                 public override bool IsLiteral {
231                         get { return true; }
232                 }
233         }
234         
235         public class LongLiteral : LongConstant {
236                 public LongLiteral (long l, Location loc) : base (l, loc)
237                 {
238                 }
239
240                 public override Expression DoResolve (EmitContext ec)
241                 {
242                         type = TypeManager.int64_type;
243                         return this;
244                 }
245
246                 public override bool IsLiteral {
247                         get { return true; }
248                 }
249         }
250
251         public class ULongLiteral : ULongConstant {
252                 public ULongLiteral (ulong l, Location loc) : base (l, loc)
253                 {
254                 }
255
256                 public override Expression DoResolve (EmitContext ec)
257                 {
258                         type = TypeManager.uint64_type;
259                         return this;
260                 }
261
262                 public override bool IsLiteral {
263                         get { return true; }
264                 }
265         }
266         
267         public class FloatLiteral : FloatConstant {
268                 
269                 public FloatLiteral (float f, Location loc) : base (f, loc)
270                 {
271                 }
272
273                 public override Expression DoResolve (EmitContext ec)
274                 {
275                         type = TypeManager.float_type;
276                         return this;
277                 }
278
279                 public override bool IsLiteral {
280                         get { return true; }
281                 }
282
283         }
284
285         public class DoubleLiteral : DoubleConstant {
286                 public DoubleLiteral (double d, Location loc) : base (d, loc)
287                 {
288                 }
289
290                 public override Expression DoResolve (EmitContext ec)
291                 {
292                         type = TypeManager.double_type;
293
294                         return this;
295                 }
296
297                 public override void Error_ValueCannotBeConverted (EmitContext ec, Location loc, Type target, bool expl)
298                 {
299                         if (target == TypeManager.float_type) {
300                                 Error_664 (loc, "float", "f");
301                                 return;
302                         }
303
304                         if (target == TypeManager.decimal_type) {
305                                 Error_664 (loc, "decimal", "m");
306                                 return;
307                         }
308
309                         base.Error_ValueCannotBeConverted (ec, loc, target, expl);
310                 }
311
312                 static void Error_664 (Location loc, string type, string suffix)
313                 {
314                         Report.Error (664, loc,
315                                 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
316                                 type, suffix);
317                 }
318
319                 public override bool IsLiteral {
320                         get { return true; }
321                 }
322
323         }
324
325         public class DecimalLiteral : DecimalConstant {
326                 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
327                 {
328                 }
329
330                 public override Expression DoResolve (EmitContext ec)
331                 {
332                         type = TypeManager.decimal_type;
333                         return this;
334                 }
335
336                 public override bool IsLiteral {
337                         get { return true; }
338                 }
339         }
340
341         public class StringLiteral : StringConstant {
342                 public StringLiteral (string s, Location loc) : base (s, loc)
343                 {
344                 }
345
346                 public override Expression DoResolve (EmitContext ec)
347                 {
348                         type = TypeManager.string_type;
349
350                         return this;
351                 }
352
353                 public override bool IsLiteral {
354                         get { return true; }
355                 }
356
357         }
358 }