/
literal.cs
333 lines (277 loc) · 6.91 KB
/
literal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// literal.cs: Literal representation for the IL tree.
//
// Author:
// Miguel de Icaza (miguel@ximian.com)
// Marek Safar (marek.safar@seznam.cz)
//
// Copyright 2001 Ximian, Inc.
// Copyright 2011 Xamarin Inc
//
//
// Notice that during parsing we create objects of type Literal, but the
// types are not loaded (thats why the Resolve method has to assign the
// type at that point).
//
// Literals differ from the constants in that we know we encountered them
// as a literal in the source code (and some extra rules apply there) and
// they have to be resolved (since during parsing we have not loaded the
// types yet) while constants are created only after types have been loaded
// and are fully resolved when born.
//
#if STATIC
using IKVM.Reflection.Emit;
#else
using System.Reflection.Emit;
#endif
namespace Mono.CSharp
{
public interface ILiteralConstant
{
#if FULL_AST
char[] ParsedValue { get; set; }
#endif
}
//
// The null literal
//
// Note: C# specification null-literal is NullLiteral of NullType type
//
public class NullLiteral : NullConstant
{
//
// Default type of null is an object
//
public NullLiteral (Location loc)
: base (InternalType.NullLiteral, loc)
{
}
public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec t, bool expl)
{
if (t.IsGenericParameter) {
ec.Report.Error(403, loc,
"Cannot convert null to the type parameter `{0}' because it could be a value " +
"type. Consider using `default ({0})' instead", t.Name);
return;
}
if (TypeSpec.IsValueType (t)) {
ec.Report.Error(37, loc, "Cannot convert null to `{0}' because it is a value type",
t.GetSignatureForError ());
return;
}
base.Error_ValueCannotBeConverted (ec, t, expl);
}
public override string GetValueAsLiteral ()
{
return "null";
}
public override bool IsLiteral {
get { return true; }
}
public override System.Linq.Expressions.Expression MakeExpression (BuilderContext ctx)
{
return System.Linq.Expressions.Expression.Constant (null);
}
}
public class BoolLiteral : BoolConstant, ILiteralConstant
{
public BoolLiteral (BuiltinTypes types, bool val, Location loc)
: base (types, val, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class CharLiteral : CharConstant, ILiteralConstant
{
public CharLiteral (BuiltinTypes types, char c, Location loc)
: base (types, c, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class IntLiteral : IntConstant, ILiteralConstant
{
public IntLiteral (BuiltinTypes types, int l, Location loc)
: base (types, l, loc)
{
}
public override Constant ConvertImplicitly (TypeSpec type)
{
//
// The 0 literal can be converted to an enum value
//
if (Value == 0 && type.IsEnum) {
Constant c = ConvertImplicitly (EnumSpec.GetUnderlyingType (type));
if (c == null)
return null;
return new EnumConstant (c, type);
}
return base.ConvertImplicitly (type);
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class UIntLiteral : UIntConstant, ILiteralConstant
{
public UIntLiteral (BuiltinTypes types, uint l, Location loc)
: base (types, l, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class LongLiteral : LongConstant, ILiteralConstant
{
public LongLiteral (BuiltinTypes types, long l, Location loc)
: base (types, l, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class ULongLiteral : ULongConstant, ILiteralConstant
{
public ULongLiteral (BuiltinTypes types, ulong l, Location loc)
: base (types, l, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class FloatLiteral : FloatConstant, ILiteralConstant
{
public FloatLiteral (BuiltinTypes types, float f, Location loc)
: base (types, f, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class DoubleLiteral : DoubleConstant, ILiteralConstant
{
public DoubleLiteral (BuiltinTypes types, double d, Location loc)
: base (types, d, loc)
{
}
public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
{
if (target.BuiltinType == BuiltinTypeSpec.Type.Float) {
Error_664 (ec, loc, "float", "f");
return;
}
if (target.BuiltinType == BuiltinTypeSpec.Type.Decimal) {
Error_664 (ec, loc, "decimal", "m");
return;
}
base.Error_ValueCannotBeConverted (ec, target, expl);
}
static void Error_664 (ResolveContext ec, Location loc, string type, string suffix)
{
ec.Report.Error (664, loc,
"Literal of type double cannot be implicitly converted to type `{0}'. Add suffix `{1}' to create a literal of this type",
type, suffix);
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class DecimalLiteral : DecimalConstant, ILiteralConstant
{
public DecimalLiteral (BuiltinTypes types, decimal d, Location loc)
: base (types, d, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
public class StringLiteral : StringConstant, ILiteralConstant
{
public StringLiteral (BuiltinTypes types, string s, Location loc)
: base (types, s, loc)
{
}
public override bool IsLiteral {
get { return true; }
}
#if FULL_AST
public char[] ParsedValue { get; set; }
#endif
public override object Accept (StructuralVisitor visitor)
{
return visitor.Visit (this);
}
}
}