Merge branch 'sgen-android'
[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 (bool val, Location loc) : base (val, loc)
100                 {
101                 }
102
103                 public override bool IsLiteral {
104                         get { return true; }
105                 }
106         }
107
108         public class CharLiteral : CharConstant {
109                 public CharLiteral (char c, Location loc) : base (c, loc)
110                 {
111                 }
112
113                 public override bool IsLiteral {
114                         get { return true; }
115                 }
116         }
117
118         public class IntLiteral : IntConstant {
119                 public IntLiteral (int l, Location loc) : base (l, loc)
120                 {
121                 }
122
123                 public override Constant ConvertImplicitly (ResolveContext rc, TypeSpec type)
124                 {
125                         //
126                         // The 0 literal can be converted to an enum value
127                         //
128                         if (Value == 0 && TypeManager.IsEnumType (type)) {
129                                 Constant c = ConvertImplicitly (rc, EnumSpec.GetUnderlyingType (type));
130                                 if (c == null)
131                                         return null;
132
133                                 return new EnumConstant (c, type).Resolve (rc);
134                         }
135
136                         return base.ConvertImplicitly (rc, type);
137                 }
138
139                 public override bool IsLiteral {
140                         get { return true; }
141                 }
142         }
143
144         public class UIntLiteral : UIntConstant {
145                 public UIntLiteral (uint l, Location loc) : base (l, loc)
146                 {
147                 }
148
149                 public override bool IsLiteral {
150                         get { return true; }
151                 }
152         }
153         
154         public class LongLiteral : LongConstant {
155                 public LongLiteral (long l, Location loc) : base (l, loc)
156                 {
157                 }
158
159                 public override bool IsLiteral {
160                         get { return true; }
161                 }
162         }
163
164         public class ULongLiteral : ULongConstant {
165                 public ULongLiteral (ulong l, Location loc) : base (l, loc)
166                 {
167                 }
168
169                 public override bool IsLiteral {
170                         get { return true; }
171                 }
172         }
173         
174         public class FloatLiteral : FloatConstant {
175                 
176                 public FloatLiteral (float f, Location loc) : base (f, loc)
177                 {
178                 }
179
180                 public override bool IsLiteral {
181                         get { return true; }
182                 }
183
184         }
185
186         public class DoubleLiteral : DoubleConstant {
187                 public DoubleLiteral (double d, Location loc) : base (d, loc)
188                 {
189                 }
190
191                 public override void Error_ValueCannotBeConverted (ResolveContext ec, Location loc, TypeSpec target, bool expl)
192                 {
193                         if (target == TypeManager.float_type) {
194                                 Error_664 (ec, loc, "float", "f");
195                                 return;
196                         }
197
198                         if (target == TypeManager.decimal_type) {
199                                 Error_664 (ec, loc, "decimal", "m");
200                                 return;
201                         }
202
203                         base.Error_ValueCannotBeConverted (ec, loc, target, expl);
204                 }
205
206                 static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
207                 {
208                         ec.Report.Error (664, loc,
209                                 "Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
210                                 type, suffix);
211                 }
212
213                 public override bool IsLiteral {
214                         get { return true; }
215                 }
216
217         }
218
219         public class DecimalLiteral : DecimalConstant {
220                 public DecimalLiteral (decimal d, Location loc) : base (d, loc)
221                 {
222                 }
223
224                 public override bool IsLiteral {
225                         get { return true; }
226                 }
227         }
228
229         public class StringLiteral : StringConstant {
230                 public StringLiteral (string s, Location loc) : base (s, loc)
231                 {
232                 }
233
234                 public override bool IsLiteral {
235                         get { return true; }
236                 }
237
238         }
239 }