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