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