Merge branch 'cecil-light'
[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.Emit;
24
25 namespace Mono.CSharp {
26
27         //
28         // The null literal
29         //
30         // Note: C# specification null-literal is NullLiteral of NullType type
31         //
32         public class NullLiteral : NullConstant
33         {
34                 //
35                 // Default type of null is an object
36                 //
37                 public NullLiteral (Location loc)
38                         : base (InternalType.Null, loc)
39                 {
40                 }
41
42                 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec t, bool expl)
43                 {
44                         if (t.IsGenericParameter) {
45                                 ec.Report.Error(403, loc,
46                                         "Cannot convert null to the type parameter `{0}' because it could be a value " +
47                                         "type. Consider using `default ({0})' instead", t.Name);
48                                 return;
49                         }
50
51                         if (TypeManager.IsValueType (t)) {
52                                 ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
53                                         TypeManager.CSharpName(t));
54                                 return;
55                         }
56
57                         base.Error_ValueCannotBeConverted (ec, loc, t, expl);
58                 }
59
60                 public override bool IsLiteral {
61                         get { return true; }
62                 }
63
64                 public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
65                 {
66                         return System.Linq.Expressions.Expression.Constant (null);
67                 }
68         }
69
70         //
71         // A null literal in a pointer context
72         //
73         class NullPointer : NullLiteral {
74                 public NullPointer (Location loc):
75                         base (loc)
76                 {
77                         type = TypeManager.object_type;
78                 }
79
80                 public override void Emit (EmitContext ec)
81                 {
82                         //
83                         // Emits null pointer
84                         //
85                         ec.Emit (OpCodes.Ldc_I4_0);
86                         ec.Emit (OpCodes.Conv_U);
87                 }
88         }
89
90         public class BoolLiteral : BoolConstant {
91                 public BoolLiteral (bool val, Location loc) : base (val, loc)
92                 {
93                 }
94
95                 public override bool IsLiteral {
96                         get { return true; }
97                 }
98         }
99
100         public class CharLiteral : CharConstant {
101                 public CharLiteral (char c, Location loc) : base (c, loc)
102                 {
103                 }
104
105                 public override bool IsLiteral {
106                         get { return true; }
107                 }
108         }
109
110         public class IntLiteral : IntConstant {
111                 public IntLiteral (int l, Location loc) : base (l, loc)
112                 {
113                 }
114
115                 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
116                 {
117                         //
118                         // The 0 literal can be converted to an enum value
119                         //
120                         if (Value == 0 && TypeManager.IsEnumType (type)) {
121                                 Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
122                                 if (c == null)
123                                         return null;
124
125                                 return new EnumConstant (c, type).Resolve (rc);
126                         }
127
128                         return base.ConvertImplicitly (rc, type);
129                 }
130
131                 public override bool IsLiteral {
132                         get { return true; }
133                 }
134         }
135
136         public class UIntLiteral : UIntConstant {
137                 public UIntLiteral (uint l, Location loc) : base (l, loc)
138                 {
139                 }
140
141                 public override bool IsLiteral {
142                         get { return true; }
143                 }
144         }
145         
146         public class LongLiteral : LongConstant {
147                 public LongLiteral (long l, Location loc) : base (l, loc)
148                 {
149                 }
150
151                 public override bool IsLiteral {
152                         get { return true; }
153                 }
154         }
155
156         public class ULongLiteral : ULongConstant {
157                 public ULongLiteral (ulong l, Location loc) : base (l, loc)
158                 {
159                 }
160
161                 public override bool IsLiteral {
162                         get { return true; }
163                 }
164         }
165         
166         public class FloatLiteral : FloatConstant {
167                 
168                 public FloatLiteral (float f, Location loc) : base (f, loc)
169                 {
170                 }
171
172                 public override bool IsLiteral {
173                         get { return true; }
174                 }
175
176         }
177
178         public class DoubleLiteral : DoubleConstant {
179                 public DoubleLiteral (double d, Location loc) : base (d, loc)
180                 {
181                 }
182
183                 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
184                 {
185                         if (target == TypeManager.float_type) {
186                                 Error_664 (ec, loc, "float", "f");
187                                 return;
188                         }
189
190                         if (target == TypeManager.decimal_type) {
191                                 Error_664 (ec, loc, "decimal", "m");
192                                 return;
193                         }
194
195                         base.Error_ValueCannotBeConverted (ec, loc, target, expl);
196                 }
197
198                 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
199                 {
200                         ec.Report.Error (664, loc,
201                                 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
202                                 type, suffix);
203                 }
204
205                 public override bool IsLiteral {
206                         get { return true; }
207                 }
208
209         }
210
211         public class DecimalLiteral : DecimalConstant {
212                 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
213                 {
214                 }
215
216                 public override bool IsLiteral {
217                         get { return true; }
218                 }
219         }
220
221         public class StringLiteral : StringConstant {
222                 public StringLiteral (string s, Location loc) : base (s, loc)
223                 {
224                 }
225
226                 public override bool IsLiteral {
227                         get { return true; }
228                 }
229
230         }
231 }