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