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