Reverted everything back until 14:15 this afternoon.
[mono.git] / mcs / gmcs / convert.cs
1 //
2 // conversion.cs: various routines for implementing conversions.
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Ravi Pratap (ravi@ximian.com)
7 //
8 // (C) 2001, 2002, 2003 Ximian, Inc.
9 //
10
11 namespace Mono.CSharp {
12         using System;
13         using System.Collections;
14         using System.Diagnostics;
15         using System.Reflection;
16         using System.Reflection.Emit;
17
18         //
19         // A container class for all the conversion operations
20         //
21         public class Convert {
22                 static void Error_CannotConvertType (Location loc, Type source, Type target)
23                 {
24                         Report.Error (30, loc, "Cannot convert type '" +
25                                       TypeManager.CSharpName (source) + "' to '" +
26                                       TypeManager.CSharpName (target) + "'");
27                 }
28
29                 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
30                                                          Location loc)
31                 {
32                         if (!TypeParameter_to_Null (target_type)) {
33                                 Report.Error (403, loc, "Cannot convert null to the type " +
34                                               "parameter `{0}' becaues it could be a value " +
35                                               "type.  Consider using `default ({0})' instead.",
36                                               target_type);
37                                 return null;
38                         }
39
40                         return new NullCast (expr, target_type);
41                 }
42
43                 static bool TypeParameter_to_Null (Type target_type)
44                 {
45                         if ((target_type.BaseType == null) ||
46                             (target_type.BaseType == TypeManager.value_type) ||
47                             target_type.BaseType.IsValueType)
48                                 return false;
49
50                         return true;
51                 }
52
53                 static EmptyExpression MyEmptyExpr;
54                 static public Expression ImplicitReferenceConversion (Expression expr, Type target_type)
55                 {
56                         Type expr_type = expr.Type;
57
58                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
59                                 // if we are a method group, emit a warning
60
61                                 expr.Emit (null);
62                         }
63
64                         if (expr_type == TypeManager.void_type)
65                                 return null;
66
67                         //
68                         // notice that it is possible to write "ValueType v = 1", the ValueType here
69                         // is an abstract class, and not really a value type, so we apply the same rules.
70                         //
71                         if (target_type == TypeManager.object_type) {
72                                 //
73                                 // A pointer type cannot be converted to object
74                                 // 
75                                 if (expr_type.IsPointer)
76                                         return null;
77
78                                 if (TypeManager.IsValueType (expr_type))
79                                         return new BoxedCast (expr);
80                                 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type)
81                                         return new EmptyCast (expr, target_type);
82                         } else if (target_type == TypeManager.value_type) {
83                                 if (TypeManager.IsValueType (expr_type))
84                                         return new BoxedCast (expr);
85                                 if (expr is NullLiteral)
86                                         return new NullCast (expr, target_type);
87                         } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
88                                 //
89                                 // Special case: enumeration to System.Enum.
90                                 // System.Enum is not a value type, it is a class, so we need
91                                 // a boxing conversion
92                                 //
93                                 if (expr_type.IsEnum || expr_type.IsGenericParameter)
94                                         return new BoxedCast (expr);
95
96                                 return new EmptyCast (expr, target_type);
97                         } else {
98
99                                 // This code is kind of mirrored inside ImplicitStandardConversionExists
100                                 // with the small distinction that we only probe there
101                                 //
102                                 // Always ensure that the code here and there is in sync
103
104                                 // from the null type to any reference-type.
105                                 if (expr is NullLiteral){
106                                         if (target_type.IsPointer)
107                                                 return NullPointer.Null;
108                                         
109                                         if (!target_type.IsValueType)
110                                                 return new NullCast (expr, target_type);
111                                 }
112
113                                 // from any class-type S to any interface-type T.
114                                 if (target_type.IsInterface) {
115                                         if (TypeManager.ImplementsInterface (expr_type, target_type)){
116                                                 if (expr_type.IsGenericParameter)
117                                                         return new BoxedCast (expr, target_type);
118                                                 else if (expr_type.IsClass)
119                                                         return new EmptyCast (expr, target_type);
120                                                 else if (TypeManager.IsValueType (expr_type) ||
121                                                          TypeManager.IsEnumType (expr_type))
122                                                         return new BoxedCast (expr, target_type);
123                                                 else
124                                                         return new EmptyCast (expr, target_type);
125                                         }
126                                 }
127
128                                 // from any interface type S to interface-type T.
129                                 if (expr_type.IsInterface && target_type.IsInterface) {
130                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
131                                                 return new EmptyCast (expr, target_type);
132                                         else
133                                                 return null;
134                                 }
135                                 
136                                 // from an array-type S to an array-type of type T
137                                 if (expr_type.IsArray && target_type.IsArray) {
138                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
139
140                                                 Type expr_element_type = TypeManager.GetElementType (expr_type);
141
142                                                 if (MyEmptyExpr == null)
143                                                         MyEmptyExpr = new EmptyExpression ();
144                                                 
145                                                 MyEmptyExpr.SetType (expr_element_type);
146                                                 Type target_element_type = TypeManager.GetElementType (target_type);
147
148                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
149                                                         if (ImplicitStandardConversionExists (MyEmptyExpr,
150                                                                                       target_element_type))
151                                                                 return new EmptyCast (expr, target_type);
152                                         }
153                                 }
154                                 
155                                 
156                                 // from an array-type to System.Array
157                                 if (expr_type.IsArray && target_type == TypeManager.array_type)
158                                         return new EmptyCast (expr, target_type);
159                                 
160                                 // from any delegate type to System.Delegate
161                                 if ((expr_type == TypeManager.delegate_type || 
162                                      TypeManager.IsDelegateType (expr_type)) &&
163                                     target_type == TypeManager.delegate_type)
164                                         return new EmptyCast (expr, target_type);
165                                         
166                                 // from any array-type or delegate type into System.ICloneable.
167                                 if (expr_type.IsArray ||
168                                     expr_type == TypeManager.delegate_type ||
169                                     TypeManager.IsDelegateType (expr_type))
170                                         if (target_type == TypeManager.icloneable_type)
171                                                 return new EmptyCast (expr, target_type);
172
173                                 // from a generic type definition to a generic instance.
174                                 if (TypeManager.IsEqualGenericType (expr_type, target_type))
175                                         return new EmptyCast (expr, target_type);
176                                 
177                                 return null;
178
179                         }
180                         
181                         return null;
182                 }
183
184                 //
185                 // Tests whether an implicit reference conversion exists between expr_type
186                 // and target_type
187                 //
188                 public static bool ImplicitReferenceConversionExists (Expression expr, Type target_type)
189                 {
190                         Type expr_type = expr.Type;
191
192                         //
193                         // This is the boxed case.
194                         //
195                         if (target_type == TypeManager.object_type) {
196                                 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
197                                     expr_type.IsInterface || expr_type == TypeManager.enum_type)
198                                         return true;
199                         } else if (TypeManager.IsSubclassOf (expr_type, target_type))
200                                 return true;
201                         else {
202                                 // Please remember that all code below actually comes
203                                 // from ImplicitReferenceConversion so make sure code remains in sync
204                                 
205                                 // from any class-type S to any interface-type T.
206                                 if (target_type.IsInterface) {
207                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
208                                                 return true;
209                                 }
210                                 
211                                 // from any interface type S to interface-type T.
212                                 if (expr_type.IsInterface && target_type.IsInterface)
213                                         if (TypeManager.ImplementsInterface (expr_type, target_type))
214                                                 return true;
215                                 
216                                 // from an array-type S to an array-type of type T
217                                 if (expr_type.IsArray && target_type.IsArray) {
218                                         if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
219                                                 
220                                                 Type expr_element_type = expr_type.GetElementType ();
221
222                                                 if (MyEmptyExpr == null)
223                                                         MyEmptyExpr = new EmptyExpression ();
224                                                 
225                                                 MyEmptyExpr.SetType (expr_element_type);
226                                                 Type target_element_type = TypeManager.GetElementType (target_type);
227                                                 
228                                                 if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
229                                                         if (ImplicitStandardConversionExists (MyEmptyExpr,
230                                                                                       target_element_type))
231                                                                 return true;
232                                         }
233                                 }
234                                 
235                                 // from an array-type to System.Array
236                                 if (expr_type.IsArray && (target_type == TypeManager.array_type))
237                                         return true;
238                                 
239                                 // from any delegate type to System.Delegate
240                                 if ((expr_type == TypeManager.delegate_type ||
241                                      TypeManager.IsDelegateType (expr_type)) &&
242                                     target_type == TypeManager.delegate_type)
243                                         if (target_type.IsAssignableFrom (expr_type))
244                                                 return true;
245                                         
246                                 // from any array-type or delegate type into System.ICloneable.
247                                 if (expr_type.IsArray ||
248                                     expr_type == TypeManager.delegate_type ||
249                                     TypeManager.IsDelegateType (expr_type))
250                                         if (target_type == TypeManager.icloneable_type)
251                                                 return true;
252                                 
253                                 // from the null type to any reference-type.
254                                 if (expr is NullLiteral && !target_type.IsValueType &&
255                                     !TypeManager.IsEnumType (target_type))
256                                         return true;
257
258                                 // from a generic type definition to a generic instance.
259                                 if (TypeManager.IsEqualGenericType (expr_type, target_type))
260                                         return true;
261                         }
262                         return false;
263                 }
264
265                 /// <summary>
266                 ///   Implicit Numeric Conversions.
267                 ///
268                 ///   expr is the expression to convert, returns a new expression of type
269                 ///   target_type or null if an implicit conversion is not possible.
270                 /// </summary>
271                 static public Expression ImplicitNumericConversion (EmitContext ec, Expression expr,
272                                                                     Type target_type, Location loc)
273                 {
274                         Type expr_type = expr.Type;
275
276                         //
277                         // Attempt to do the implicit constant expression conversions
278
279                         if (expr is Constant){
280                                 if (expr is IntConstant){
281                                         Expression e;
282                                         
283                                         e = TryImplicitIntConversion (target_type, (IntConstant) expr);
284                                         
285                                         if (e != null)
286                                                 return e;
287                                 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
288                                         //
289                                         // Try the implicit constant expression conversion
290                                         // from long to ulong, instead of a nice routine,
291                                         // we just inline it
292                                         //
293                                         long v = ((LongConstant) expr).Value;
294                                         if (v >= 0)
295                                                 return new ULongConstant ((ulong) v);
296                                 } 
297                         }
298                         
299                         Type real_target_type = target_type;
300
301                         if (expr_type == TypeManager.sbyte_type){
302                                 //
303                                 // From sbyte to short, int, long, float, double.
304                                 //
305                                 if (real_target_type == TypeManager.int32_type)
306                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
307                                 if (real_target_type == TypeManager.int64_type)
308                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
309                                 if (real_target_type == TypeManager.double_type)
310                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
311                                 if (real_target_type == TypeManager.float_type)
312                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
313                                 if (real_target_type == TypeManager.short_type)
314                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
315                         } else if (expr_type == TypeManager.byte_type){
316                                 //
317                                 // From byte to short, ushort, int, uint, long, ulong, float, double
318                                 // 
319                                 if ((real_target_type == TypeManager.short_type) ||
320                                     (real_target_type == TypeManager.ushort_type) ||
321                                     (real_target_type == TypeManager.int32_type) ||
322                                     (real_target_type == TypeManager.uint32_type))
323                                         return new EmptyCast (expr, target_type);
324
325                                 if (real_target_type == TypeManager.uint64_type)
326                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
327                                 if (real_target_type == TypeManager.int64_type)
328                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
329                                 if (real_target_type == TypeManager.float_type)
330                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
331                                 if (real_target_type == TypeManager.double_type)
332                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
333                         } else if (expr_type == TypeManager.short_type){
334                                 //
335                                 // From short to int, long, float, double
336                                 // 
337                                 if (real_target_type == TypeManager.int32_type)
338                                         return new EmptyCast (expr, target_type);
339                                 if (real_target_type == TypeManager.int64_type)
340                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
341                                 if (real_target_type == TypeManager.double_type)
342                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
343                                 if (real_target_type == TypeManager.float_type)
344                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
345                         } else if (expr_type == TypeManager.ushort_type){
346                                 //
347                                 // From ushort to int, uint, long, ulong, float, double
348                                 //
349                                 if (real_target_type == TypeManager.uint32_type)
350                                         return new EmptyCast (expr, target_type);
351
352                                 if (real_target_type == TypeManager.uint64_type)
353                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
354                                 if (real_target_type == TypeManager.int32_type)
355                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
356                                 if (real_target_type == TypeManager.int64_type)
357                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
358                                 if (real_target_type == TypeManager.double_type)
359                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
360                                 if (real_target_type == TypeManager.float_type)
361                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
362                         } else if (expr_type == TypeManager.int32_type){
363                                 //
364                                 // From int to long, float, double
365                                 //
366                                 if (real_target_type == TypeManager.int64_type)
367                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
368                                 if (real_target_type == TypeManager.double_type)
369                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
370                                 if (real_target_type == TypeManager.float_type)
371                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
372                         } else if (expr_type == TypeManager.uint32_type){
373                                 //
374                                 // From uint to long, ulong, float, double
375                                 //
376                                 if (real_target_type == TypeManager.int64_type)
377                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
378                                 if (real_target_type == TypeManager.uint64_type)
379                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
380                                 if (real_target_type == TypeManager.double_type)
381                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
382                                                                OpCodes.Conv_R8);
383                                 if (real_target_type == TypeManager.float_type)
384                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
385                                                                OpCodes.Conv_R4);
386                         } else if (expr_type == TypeManager.int64_type){
387                                 //
388                                 // From long/ulong to float, double
389                                 //
390                                 if (real_target_type == TypeManager.double_type)
391                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
392                                 if (real_target_type == TypeManager.float_type)
393                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
394                         } else if (expr_type == TypeManager.uint64_type){
395                                 //
396                                 // From ulong to float, double
397                                 //
398                                 if (real_target_type == TypeManager.double_type)
399                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
400                                                                OpCodes.Conv_R8);
401                                 if (real_target_type == TypeManager.float_type)
402                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R_Un,
403                                                                OpCodes.Conv_R4);        
404                         } else if (expr_type == TypeManager.char_type){
405                                 //
406                                 // From char to ushort, int, uint, long, ulong, float, double
407                                 // 
408                                 if ((real_target_type == TypeManager.ushort_type) ||
409                                     (real_target_type == TypeManager.int32_type) ||
410                                     (real_target_type == TypeManager.uint32_type))
411                                         return new EmptyCast (expr, target_type);
412                                 if (real_target_type == TypeManager.uint64_type)
413                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
414                                 if (real_target_type == TypeManager.int64_type)
415                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
416                                 if (real_target_type == TypeManager.float_type)
417                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
418                                 if (real_target_type == TypeManager.double_type)
419                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
420                         } else if (expr_type == TypeManager.float_type){
421                                 //
422                                 // float to double
423                                 //
424                                 if (real_target_type == TypeManager.double_type)
425                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
426                         }
427
428                         return null;
429                 }
430
431
432                 /// <summary>
433                 ///  Same as ImplicitStandardConversionExists except that it also looks at
434                 ///  implicit user defined conversions - needed for overload resolution
435                 /// </summary>
436                 public static bool ImplicitConversionExists (EmitContext ec, Expression expr, Type target_type)
437                 {
438                         if ((expr is NullLiteral) && target_type.IsGenericParameter)
439                                 return TypeParameter_to_Null (target_type);
440
441                         if (ImplicitStandardConversionExists (expr, target_type))
442                                 return true;
443
444                         Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
445
446                         if (dummy != null)
447                                 return true;
448
449                         return false;
450                 }
451
452                 public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
453                 {
454                         Expression dummy = ImplicitUserConversion (
455                                 ec, new EmptyExpression (source), target, Location.Null);
456                         return dummy != null;
457                 }
458
459                 /// <summary>
460                 ///  Determines if a standard implicit conversion exists from
461                 ///  expr_type to target_type
462                 /// </summary>
463                 public static bool ImplicitStandardConversionExists (Expression expr, Type target_type)
464                 {
465                         Type expr_type = expr.Type;
466
467                         if (expr_type == TypeManager.void_type)
468                                 return false;
469
470                         //Console.WriteLine ("Expr is {0}", expr);
471                         //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
472                         if (expr_type.Equals (target_type))
473                                 return true;
474
475                         // First numeric conversions 
476
477                         if (expr_type == TypeManager.sbyte_type){
478                                 //
479                                 // From sbyte to short, int, long, float, double.
480                                 //
481                                 if ((target_type == TypeManager.int32_type) || 
482                                     (target_type == TypeManager.int64_type) ||
483                                     (target_type == TypeManager.double_type) ||
484                                     (target_type == TypeManager.float_type)  ||
485                                     (target_type == TypeManager.short_type) ||
486                                     (target_type == TypeManager.decimal_type))
487                                         return true;
488                                 
489                         } else if (expr_type == TypeManager.byte_type){
490                                 //
491                                 // From byte to short, ushort, int, uint, long, ulong, float, double
492                                 // 
493                                 if ((target_type == TypeManager.short_type) ||
494                                     (target_type == TypeManager.ushort_type) ||
495                                     (target_type == TypeManager.int32_type) ||
496                                     (target_type == TypeManager.uint32_type) ||
497                                     (target_type == TypeManager.uint64_type) ||
498                                     (target_type == TypeManager.int64_type) ||
499                                     (target_type == TypeManager.float_type) ||
500                                     (target_type == TypeManager.double_type) ||
501                                     (target_type == TypeManager.decimal_type))
502                                         return true;
503         
504                         } else if (expr_type == TypeManager.short_type){
505                                 //
506                                 // From short to int, long, float, double
507                                 // 
508                                 if ((target_type == TypeManager.int32_type) ||
509                                     (target_type == TypeManager.int64_type) ||
510                                     (target_type == TypeManager.double_type) ||
511                                     (target_type == TypeManager.float_type) ||
512                                     (target_type == TypeManager.decimal_type))
513                                         return true;
514                                         
515                         } else if (expr_type == TypeManager.ushort_type){
516                                 //
517                                 // From ushort to int, uint, long, ulong, float, double
518                                 //
519                                 if ((target_type == TypeManager.uint32_type) ||
520                                     (target_type == TypeManager.uint64_type) ||
521                                     (target_type == TypeManager.int32_type) ||
522                                     (target_type == TypeManager.int64_type) ||
523                                     (target_type == TypeManager.double_type) ||
524                                     (target_type == TypeManager.float_type) ||
525                                     (target_type == TypeManager.decimal_type))
526                                         return true;
527                                     
528                         } else if (expr_type == TypeManager.int32_type){
529                                 //
530                                 // From int to long, float, double
531                                 //
532                                 if ((target_type == TypeManager.int64_type) ||
533                                     (target_type == TypeManager.double_type) ||
534                                     (target_type == TypeManager.float_type) ||
535                                     (target_type == TypeManager.decimal_type))
536                                         return true;
537                                         
538                         } else if (expr_type == TypeManager.uint32_type){
539                                 //
540                                 // From uint to long, ulong, float, double
541                                 //
542                                 if ((target_type == TypeManager.int64_type) ||
543                                     (target_type == TypeManager.uint64_type) ||
544                                     (target_type == TypeManager.double_type) ||
545                                     (target_type == TypeManager.float_type) ||
546                                     (target_type == TypeManager.decimal_type))
547                                         return true;
548                                         
549                         } else if ((expr_type == TypeManager.uint64_type) ||
550                                    (expr_type == TypeManager.int64_type)) {
551                                 //
552                                 // From long/ulong to float, double
553                                 //
554                                 if ((target_type == TypeManager.double_type) ||
555                                     (target_type == TypeManager.float_type) ||
556                                     (target_type == TypeManager.decimal_type))
557                                         return true;
558                                     
559                         } else if (expr_type == TypeManager.char_type){
560                                 //
561                                 // From char to ushort, int, uint, long, ulong, float, double
562                                 // 
563                                 if ((target_type == TypeManager.ushort_type) ||
564                                     (target_type == TypeManager.int32_type) ||
565                                     (target_type == TypeManager.uint32_type) ||
566                                     (target_type == TypeManager.uint64_type) ||
567                                     (target_type == TypeManager.int64_type) ||
568                                     (target_type == TypeManager.float_type) ||
569                                     (target_type == TypeManager.double_type) ||
570                                     (target_type == TypeManager.decimal_type))
571                                         return true;
572
573                         } else if (expr_type == TypeManager.float_type){
574                                 //
575                                 // float to double
576                                 //
577                                 if (target_type == TypeManager.double_type)
578                                         return true;
579                         }       
580                         
581                         if (ImplicitReferenceConversionExists (expr, target_type))
582                                 return true;
583
584                         //
585                         // Implicit Constant Expression Conversions
586                         //
587                         if (expr is IntConstant){
588                                 int value = ((IntConstant) expr).Value;
589
590                                 if (target_type == TypeManager.sbyte_type){
591                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
592                                                 return true;
593                                 } else if (target_type == TypeManager.byte_type){
594                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
595                                                 return true;
596                                 } else if (target_type == TypeManager.short_type){
597                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
598                                                 return true;
599                                 } else if (target_type == TypeManager.ushort_type){
600                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
601                                                 return true;
602                                 } else if (target_type == TypeManager.uint32_type){
603                                         if (value >= 0)
604                                                 return true;
605                                 } else if (target_type == TypeManager.uint64_type){
606                                          //
607                                          // we can optimize this case: a positive int32
608                                          // always fits on a uint64.  But we need an opcode
609                                          // to do it.
610                                          //
611                                         if (value >= 0)
612                                                 return true;
613                                 }
614                                 
615                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
616                                         return true;
617                         }
618
619                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
620                                 //
621                                 // Try the implicit constant expression conversion
622                                 // from long to ulong, instead of a nice routine,
623                                 // we just inline it
624                                 //
625                                 long v = ((LongConstant) expr).Value;
626                                 if (v > 0)
627                                         return true;
628                         }
629                         
630                         if ((target_type == TypeManager.enum_type ||
631                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
632                              expr is IntLiteral){
633                                 IntLiteral i = (IntLiteral) expr;
634
635                                 if (i.Value == 0)
636                                         return true;
637                         }
638
639                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
640                                 return true;
641
642                         return false;
643                 }
644
645                 //
646                 // Used internally by FindMostEncompassedType, this is used
647                 // to avoid creating lots of objects in the tight loop inside
648                 // FindMostEncompassedType
649                 //
650                 static EmptyExpression priv_fmet_param;
651                 
652                 /// <summary>
653                 ///  Finds "most encompassed type" according to the spec (13.4.2)
654                 ///  amongst the methods in the MethodGroupExpr
655                 /// </summary>
656                 static Type FindMostEncompassedType (ArrayList types)
657                 {
658                         Type best = null;
659
660                         if (priv_fmet_param == null)
661                                 priv_fmet_param = new EmptyExpression ();
662
663                         foreach (Type t in types){
664                                 priv_fmet_param.SetType (t);
665                                 
666                                 if (best == null) {
667                                         best = t;
668                                         continue;
669                                 }
670                                 
671                                 if (ImplicitStandardConversionExists (priv_fmet_param, best))
672                                         best = t;
673                         }
674
675                         return best;
676                 }
677
678                 //
679                 // Used internally by FindMostEncompassingType, this is used
680                 // to avoid creating lots of objects in the tight loop inside
681                 // FindMostEncompassingType
682                 //
683                 static EmptyExpression priv_fmee_ret;
684                 
685                 /// <summary>
686                 ///  Finds "most encompassing type" according to the spec (13.4.2)
687                 ///  amongst the types in the given set
688                 /// </summary>
689                 static Type FindMostEncompassingType (ArrayList types)
690                 {
691                         Type best = null;
692
693                         if (priv_fmee_ret == null)
694                                 priv_fmee_ret = new EmptyExpression ();
695
696                         foreach (Type t in types){
697                                 priv_fmee_ret.SetType (best);
698
699                                 if (best == null) {
700                                         best = t;
701                                         continue;
702                                 }
703
704                                 if (ImplicitStandardConversionExists (priv_fmee_ret, t))
705                                         best = t;
706                         }
707                         
708                         return best;
709                 }
710
711                 //
712                 // Used to avoid creating too many objects
713                 //
714                 static EmptyExpression priv_fms_expr;
715                 
716                 /// <summary>
717                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
718                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
719                 ///   for explicit and implicit conversion operators.
720                 /// </summary>
721                 static public Type FindMostSpecificSource (MethodGroupExpr me, Expression source,
722                                                            bool apply_explicit_conv_rules,
723                                                            Location loc)
724                 {
725                         ArrayList src_types_set = new ArrayList ();
726                         
727                         if (priv_fms_expr == null)
728                                 priv_fms_expr = new EmptyExpression ();
729
730                         //
731                         // If any operator converts from S then Sx = S
732                         //
733                         Type source_type = source.Type;
734                         foreach (MethodBase mb in me.Methods){
735                                 ParameterData pd = Invocation.GetParameterData (mb);
736                                 Type param_type = pd.ParameterType (0);
737
738                                 if (param_type == source_type)
739                                         return param_type;
740
741                                 if (apply_explicit_conv_rules) {
742                                         //
743                                         // From the spec :
744                                         // Find the set of applicable user-defined conversion operators, U.  This set
745                                         // consists of the
746                                         // user-defined implicit or explicit conversion operators declared by
747                                         // the classes or structs in D that convert from a type encompassing
748                                         // or encompassed by S to a type encompassing or encompassed by T
749                                         //
750                                         priv_fms_expr.SetType (param_type);
751                                         if (ImplicitStandardConversionExists (priv_fms_expr, source_type))
752                                                 src_types_set.Add (param_type);
753                                         else {
754                                                 if (ImplicitStandardConversionExists (source, param_type))
755                                                         src_types_set.Add (param_type);
756                                         }
757                                 } else {
758                                         //
759                                         // Only if S is encompassed by param_type
760                                         //
761                                         if (ImplicitStandardConversionExists (source, param_type))
762                                                 src_types_set.Add (param_type);
763                                 }
764                         }
765                         
766                         //
767                         // Explicit Conv rules
768                         //
769                         if (apply_explicit_conv_rules) {
770                                 ArrayList candidate_set = new ArrayList ();
771
772                                 foreach (Type param_type in src_types_set){
773                                         if (ImplicitStandardConversionExists (source, param_type))
774                                                 candidate_set.Add (param_type);
775                                 }
776
777                                 if (candidate_set.Count != 0)
778                                         return FindMostEncompassedType (candidate_set);
779                         }
780
781                         //
782                         // Final case
783                         //
784                         if (apply_explicit_conv_rules)
785                                 return FindMostEncompassingType (src_types_set);
786                         else
787                                 return FindMostEncompassedType (src_types_set);
788                 }
789
790                 //
791                 // Useful in avoiding proliferation of objects
792                 //
793                 static EmptyExpression priv_fmt_expr;
794                 
795                 /// <summary>
796                 ///  Finds the most specific target Tx according to section 13.4.4
797                 /// </summary>
798                 static public Type FindMostSpecificTarget (MethodGroupExpr me, Type target,
799                                                            bool apply_explicit_conv_rules,
800                                                            Location loc)
801                 {
802                         ArrayList tgt_types_set = new ArrayList ();
803                         
804                         if (priv_fmt_expr == null)
805                                 priv_fmt_expr = new EmptyExpression ();
806                         
807                         //
808                         // If any operator converts to T then Tx = T
809                         //
810                         foreach (MethodInfo mi in me.Methods){
811                                 Type ret_type = mi.ReturnType;
812
813                                 if (ret_type == target)
814                                         return ret_type;
815
816                                 if (apply_explicit_conv_rules) {
817                                         //
818                                         // From the spec :
819                                         // Find the set of applicable user-defined conversion operators, U.
820                                         //
821                                         // This set consists of the
822                                         // user-defined implicit or explicit conversion operators declared by
823                                         // the classes or structs in D that convert from a type encompassing
824                                         // or encompassed by S to a type encompassing or encompassed by T
825                                         //
826                                         priv_fms_expr.SetType (ret_type);
827                                         if (ImplicitStandardConversionExists (priv_fms_expr, target))
828                                                 tgt_types_set.Add (ret_type);
829                                         else {
830                                                 priv_fms_expr.SetType (target);
831                                                 if (ImplicitStandardConversionExists (priv_fms_expr, ret_type))
832                                                         tgt_types_set.Add (ret_type);
833                                         }
834                                 } else {
835                                         //
836                                         // Only if T is encompassed by param_type
837                                         //
838                                         priv_fms_expr.SetType (ret_type);
839                                         if (ImplicitStandardConversionExists (priv_fms_expr, target))
840                                                 tgt_types_set.Add (ret_type);
841                                 }
842                         }
843
844                         //
845                         // Explicit conv rules
846                         //
847                         if (apply_explicit_conv_rules) {
848                                 ArrayList candidate_set = new ArrayList ();
849
850                                 foreach (Type ret_type in tgt_types_set){
851                                         priv_fmt_expr.SetType (ret_type);
852                                         
853                                         if (ImplicitStandardConversionExists (priv_fmt_expr, target))
854                                                 candidate_set.Add (ret_type);
855                                 }
856
857                                 if (candidate_set.Count != 0)
858                                         return FindMostEncompassingType (candidate_set);
859                         }
860                         
861                         //
862                         // Okay, final case !
863                         //
864                         if (apply_explicit_conv_rules)
865                                 return FindMostEncompassedType (tgt_types_set);
866                         else 
867                                 return FindMostEncompassingType (tgt_types_set);
868                 }
869                 
870                 /// <summary>
871                 ///  User-defined Implicit conversions
872                 /// </summary>
873                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
874                                                                  Type target, Location loc)
875                 {
876                         return UserDefinedConversion (ec, source, target, loc, false);
877                 }
878
879                 /// <summary>
880                 ///  User-defined Explicit conversions
881                 /// </summary>
882                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
883                                                                  Type target, Location loc)
884                 {
885                         return UserDefinedConversion (ec, source, target, loc, true);
886                 }
887
888                 /// <summary>
889                 ///   Computes the MethodGroup for the user-defined conversion
890                 ///   operators from source_type to target_type.  `look_for_explicit'
891                 ///   controls whether we should also include the list of explicit
892                 ///   operators
893                 /// </summary>
894                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
895                                                                Type source_type, Type target_type,
896                                                                Location loc, bool look_for_explicit)
897                 {
898                         Expression mg1 = null, mg2 = null;
899                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
900                         string op_name;
901
902                         op_name = "op_Implicit";
903
904                         MethodGroupExpr union3;
905
906                         mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
907                         if (source_type.BaseType != null)
908                                 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
909
910                         if (mg1 == null)
911                                 union3 = (MethodGroupExpr) mg2;
912                         else if (mg2 == null)
913                                 union3 = (MethodGroupExpr) mg1;
914                         else
915                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
916
917                         mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
918                         if (mg1 != null){
919                                 if (union3 != null)
920                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
921                                 else
922                                         union3 = (MethodGroupExpr) mg1;
923                         }
924
925                         if (target_type.BaseType != null)
926                                 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
927                         
928                         if (mg1 != null){
929                                 if (union3 != null)
930                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
931                                 else
932                                         union3 = (MethodGroupExpr) mg1;
933                         }
934
935                         MethodGroupExpr union4 = null;
936
937                         if (look_for_explicit) {
938                                 op_name = "op_Explicit";
939
940                                 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
941                                 if (source_type.BaseType != null)
942                                         mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
943                                 
944                                 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
945                                 if (target_type.BaseType != null)
946                                         mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
947                                 
948                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
949                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
950
951                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
952                         }
953                         
954                         return Invocation.MakeUnionSet (union3, union4, loc);
955                 }
956                 
957                 /// <summary>
958                 ///   User-defined conversions
959                 /// </summary>
960                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
961                                                                 Type target, Location loc,
962                                                                 bool look_for_explicit)
963                 {
964                         MethodGroupExpr union;
965                         Type source_type = source.Type;
966                         MethodBase method = null;
967
968                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
969                         if (union == null)
970                                 return null;
971                         
972                         Type most_specific_source, most_specific_target;
973
974                         most_specific_source = FindMostSpecificSource (union, source, look_for_explicit, loc);
975                         if (most_specific_source == null)
976                                 return null;
977
978                         most_specific_target = FindMostSpecificTarget (union, target, look_for_explicit, loc);
979                         if (most_specific_target == null) 
980                                 return null;
981
982                         int count = 0;
983
984                         
985                         foreach (MethodBase mb in union.Methods){
986                                 ParameterData pd = Invocation.GetParameterData (mb);
987                                 MethodInfo mi = (MethodInfo) mb;
988                                 
989                                 if (pd.ParameterType (0) == most_specific_source &&
990                                     mi.ReturnType == most_specific_target) {
991                                         method = mb;
992                                         count++;
993                                 }
994                         }
995                         
996                         if (method == null || count > 1)
997                                 return null;
998                         
999                         
1000                         //
1001                         // This will do the conversion to the best match that we
1002                         // found.  Now we need to perform an implict standard conversion
1003                         // if the best match was not the type that we were requested
1004                         // by target.
1005                         //
1006                         if (look_for_explicit)
1007                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1008                         else
1009                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1010
1011                         if (source == null)
1012                                 return null;
1013
1014                         Expression e;
1015                         e =  new UserCast ((MethodInfo) method, source, loc);
1016                         if (e.Type != target){
1017                                 if (!look_for_explicit)
1018                                         e = ImplicitConversionStandard (ec, e, target, loc);
1019                                 else
1020                                         e = ExplicitConversionStandard (ec, e, target, loc);
1021                         }
1022
1023                         return e;
1024                 }
1025                 
1026                 /// <summary>
1027                 ///   Converts implicitly the resolved expression `expr' into the
1028                 ///   `target_type'.  It returns a new expression that can be used
1029                 ///   in a context that expects a `target_type'. 
1030                 /// </summary>
1031                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1032                                                              Type target_type, Location loc)
1033                 {
1034                         Expression e;
1035
1036                         if (target_type == null)
1037                                 throw new Exception ("Target type is null");
1038
1039                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1040                         if (e != null)
1041                                 return e;
1042
1043                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1044                         if (e != null)
1045                                 return e;
1046
1047                         return null;
1048                 }
1049
1050                 
1051                 /// <summary>
1052                 ///   Attempts to apply the `Standard Implicit
1053                 ///   Conversion' rules to the expression `expr' into
1054                 ///   the `target_type'.  It returns a new expression
1055                 ///   that can be used in a context that expects a
1056                 ///   `target_type'.
1057                 ///
1058                 ///   This is different from `ImplicitConversion' in that the
1059                 ///   user defined implicit conversions are excluded. 
1060                 /// </summary>
1061                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1062                                                                      Type target_type, Location loc)
1063                 {
1064                         Type expr_type = expr.Type;
1065                         Expression e;
1066
1067                         if ((expr is NullLiteral) && target_type.IsGenericParameter)
1068                                 return TypeParameter_to_Null (expr, target_type, loc);
1069
1070                         if (expr.eclass == ExprClass.MethodGroup){
1071                                 if (!TypeManager.IsDelegateType (target_type)){
1072                                         Report.Error (428, loc,
1073                                                       String.Format (
1074                                                            "Cannot convert method group to `{0}', since it is not a delegate",
1075                                                            TypeManager.CSharpName (target_type)));
1076                                         return null;
1077                                 }
1078
1079                                 return ImplicitDelegateCreation.Create (ec, (MethodGroupExpr) expr, target_type, loc);
1080                         }
1081
1082                         if (expr_type.Equals (target_type) && !(expr is NullLiteral))
1083                                 return expr;
1084
1085                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1086                         if (e != null)
1087                                 return e;
1088
1089                         e = ImplicitReferenceConversion (expr, target_type);
1090                         if (e != null)
1091                                 return e;
1092                         
1093                         if ((target_type == TypeManager.enum_type ||
1094                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
1095                             expr is IntLiteral){
1096                                 IntLiteral i = (IntLiteral) expr;
1097
1098                                 if (i.Value == 0)
1099                                         return new EnumConstant ((Constant) expr, target_type);
1100                         }
1101
1102                         if (ec.InUnsafe) {
1103                                 if (expr_type.IsPointer){
1104                                         if (target_type == TypeManager.void_ptr_type)
1105                                                 return new EmptyCast (expr, target_type);
1106
1107                                         //
1108                                         // yep, comparing pointer types cant be done with
1109                                         // t1 == t2, we have to compare their element types.
1110                                         //
1111                                         if (target_type.IsPointer){
1112                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1113                                                         return expr;
1114                                         }
1115                                 }
1116                                 
1117                                 if (target_type.IsPointer) {
1118                                         if (expr is NullLiteral)
1119                                                 return new EmptyCast (expr, target_type);
1120
1121                                         if (expr_type == TypeManager.void_ptr_type)
1122                                                 return new EmptyCast (expr, target_type);
1123                                 }
1124                         }
1125
1126                         return null;
1127                 }
1128
1129                 /// <summary>
1130                 ///   Attemps to perform an implict constant conversion of the IntConstant
1131                 ///   into a different data type using casts (See Implicit Constant
1132                 ///   Expression Conversions)
1133                 /// </summary>
1134                 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1135                 {
1136                         int value = ic.Value;
1137
1138                         if (target_type == TypeManager.sbyte_type){
1139                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1140                                         return new SByteConstant ((sbyte) value);
1141                         } else if (target_type == TypeManager.byte_type){
1142                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1143                                         return new ByteConstant ((byte) value);
1144                         } else if (target_type == TypeManager.short_type){
1145                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1146                                         return new ShortConstant ((short) value);
1147                         } else if (target_type == TypeManager.ushort_type){
1148                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1149                                         return new UShortConstant ((ushort) value);
1150                         } else if (target_type == TypeManager.uint32_type){
1151                                 if (value >= 0)
1152                                         return new UIntConstant ((uint) value);
1153                         } else if (target_type == TypeManager.uint64_type){
1154                                 //
1155                                 // we can optimize this case: a positive int32
1156                                 // always fits on a uint64.  But we need an opcode
1157                                 // to do it.
1158                                 //
1159                                 if (value >= 0)
1160                                         return new ULongConstant ((ulong) value);
1161                         } else if (target_type == TypeManager.double_type)
1162                                 return new DoubleConstant ((double) value);
1163                         else if (target_type == TypeManager.float_type)
1164                                 return new FloatConstant ((float) value);
1165                         
1166                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1167                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1168                                 Constant e = (Constant) ic;
1169                                 
1170                                 //
1171                                 // Possibly, we need to create a different 0 literal before passing
1172                                 // to EnumConstant
1173                                 //n
1174                                 if (underlying == TypeManager.int64_type)
1175                                         e = new LongLiteral (0);
1176                                 else if (underlying == TypeManager.uint64_type)
1177                                         e = new ULongLiteral (0);
1178
1179                                 return new EnumConstant (e, target_type);
1180                         }
1181                         return null;
1182                 }
1183
1184                 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1185                 {
1186                         string msg = "Cannot convert implicitly from `"+
1187                                 TypeManager.CSharpName (source) + "' to `" +
1188                                 TypeManager.CSharpName (target) + "'";
1189
1190                         Report.Error (29, loc, msg);
1191                 }
1192
1193                 /// <summary>
1194                 ///   Attemptes to implicityly convert `target' into `type', using
1195                 ///   ImplicitConversion.  If there is no implicit conversion, then
1196                 ///   an error is signaled
1197                 /// </summary>
1198                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1199                                                                      Type target_type, Location loc)
1200                 {
1201                         Expression e;
1202
1203                         int errors = Report.Errors;
1204                         e = ImplicitConversion (ec, source, target_type, loc);
1205                         if (Report.Errors > errors)
1206                                 return null;
1207                         if (e != null)
1208                                 return e;
1209
1210                         if (source is DoubleLiteral && target_type == TypeManager.float_type){
1211                                 Report.Error (664, loc,
1212                                               "Double literal cannot be implicitly converted to " +
1213                                               "float type, use F suffix to create a float literal");
1214                         }
1215
1216                         if (source is Constant){
1217                                 Constant c = (Constant) source;
1218
1219                                 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1220                                 return null;
1221                         }
1222                         
1223                         Error_CannotImplicitConversion (loc, source.Type, target_type);
1224
1225                         return null;
1226                 }
1227
1228                 /// <summary>
1229                 ///   Performs the explicit numeric conversions
1230                 /// </summary>
1231                 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1232                 {
1233                         Type expr_type = expr.Type;
1234
1235                         //
1236                         // If we have an enumeration, extract the underlying type,
1237                         // use this during the comparison, but wrap around the original
1238                         // target_type
1239                         //
1240                         Type real_target_type = target_type;
1241
1242                         if (TypeManager.IsEnumType (real_target_type))
1243                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1244
1245                         if (ImplicitStandardConversionExists (expr, real_target_type)){
1246                                 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1247
1248                                 if (real_target_type != target_type)
1249                                         return new EmptyCast (ce, target_type);
1250                                 return ce;
1251                         }
1252                         
1253                         if (expr_type == TypeManager.sbyte_type){
1254                                 //
1255                                 // From sbyte to byte, ushort, uint, ulong, char
1256                                 //
1257                                 if (real_target_type == TypeManager.byte_type)
1258                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1259                                 if (real_target_type == TypeManager.ushort_type)
1260                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1261                                 if (real_target_type == TypeManager.uint32_type)
1262                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1263                                 if (real_target_type == TypeManager.uint64_type)
1264                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1265                                 if (real_target_type == TypeManager.char_type)
1266                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1267                         } else if (expr_type == TypeManager.byte_type){
1268                                 //
1269                                 // From byte to sbyte and char
1270                                 //
1271                                 if (real_target_type == TypeManager.sbyte_type)
1272                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1273                                 if (real_target_type == TypeManager.char_type)
1274                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1275                         } else if (expr_type == TypeManager.short_type){
1276                                 //
1277                                 // From short to sbyte, byte, ushort, uint, ulong, char
1278                                 //
1279                                 if (real_target_type == TypeManager.sbyte_type)
1280                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1281                                 if (real_target_type == TypeManager.byte_type)
1282                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1283                                 if (real_target_type == TypeManager.ushort_type)
1284                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1285                                 if (real_target_type == TypeManager.uint32_type)
1286                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1287                                 if (real_target_type == TypeManager.uint64_type)
1288                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1289                                 if (real_target_type == TypeManager.char_type)
1290                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1291                         } else if (expr_type == TypeManager.ushort_type){
1292                                 //
1293                                 // From ushort to sbyte, byte, short, char
1294                                 //
1295                                 if (real_target_type == TypeManager.sbyte_type)
1296                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1297                                 if (real_target_type == TypeManager.byte_type)
1298                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1299                                 if (real_target_type == TypeManager.short_type)
1300                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1301                                 if (real_target_type == TypeManager.char_type)
1302                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1303                         } else if (expr_type == TypeManager.int32_type){
1304                                 //
1305                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1306                                 //
1307                                 if (real_target_type == TypeManager.sbyte_type)
1308                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1309                                 if (real_target_type == TypeManager.byte_type)
1310                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1311                                 if (real_target_type == TypeManager.short_type)
1312                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1313                                 if (real_target_type == TypeManager.ushort_type)
1314                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1315                                 if (real_target_type == TypeManager.uint32_type)
1316                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1317                                 if (real_target_type == TypeManager.uint64_type)
1318                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1319                                 if (real_target_type == TypeManager.char_type)
1320                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1321                         } else if (expr_type == TypeManager.uint32_type){
1322                                 //
1323                                 // From uint to sbyte, byte, short, ushort, int, char
1324                                 //
1325                                 if (real_target_type == TypeManager.sbyte_type)
1326                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1327                                 if (real_target_type == TypeManager.byte_type)
1328                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1329                                 if (real_target_type == TypeManager.short_type)
1330                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1331                                 if (real_target_type == TypeManager.ushort_type)
1332                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1333                                 if (real_target_type == TypeManager.int32_type)
1334                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1335                                 if (real_target_type == TypeManager.char_type)
1336                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1337                         } else if (expr_type == TypeManager.int64_type){
1338                                 //
1339                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1340                                 //
1341                                 if (real_target_type == TypeManager.sbyte_type)
1342                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1343                                 if (real_target_type == TypeManager.byte_type)
1344                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1345                                 if (real_target_type == TypeManager.short_type)
1346                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1347                                 if (real_target_type == TypeManager.ushort_type)
1348                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1349                                 if (real_target_type == TypeManager.int32_type)
1350                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1351                                 if (real_target_type == TypeManager.uint32_type)
1352                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1353                                 if (real_target_type == TypeManager.uint64_type)
1354                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1355                                 if (real_target_type == TypeManager.char_type)
1356                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1357                         } else if (expr_type == TypeManager.uint64_type){
1358                                 //
1359                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1360                                 //
1361                                 if (real_target_type == TypeManager.sbyte_type)
1362                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1363                                 if (real_target_type == TypeManager.byte_type)
1364                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1365                                 if (real_target_type == TypeManager.short_type)
1366                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1367                                 if (real_target_type == TypeManager.ushort_type)
1368                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1369                                 if (real_target_type == TypeManager.int32_type)
1370                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1371                                 if (real_target_type == TypeManager.uint32_type)
1372                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1373                                 if (real_target_type == TypeManager.int64_type)
1374                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1375                                 if (real_target_type == TypeManager.char_type)
1376                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1377                         } else if (expr_type == TypeManager.char_type){
1378                                 //
1379                                 // From char to sbyte, byte, short
1380                                 //
1381                                 if (real_target_type == TypeManager.sbyte_type)
1382                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1383                                 if (real_target_type == TypeManager.byte_type)
1384                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1385                                 if (real_target_type == TypeManager.short_type)
1386                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1387                         } else if (expr_type == TypeManager.float_type){
1388                                 //
1389                                 // From float to sbyte, byte, short,
1390                                 // ushort, int, uint, long, ulong, char
1391                                 // or decimal
1392                                 //
1393                                 if (real_target_type == TypeManager.sbyte_type)
1394                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1395                                 if (real_target_type == TypeManager.byte_type)
1396                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1397                                 if (real_target_type == TypeManager.short_type)
1398                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1399                                 if (real_target_type == TypeManager.ushort_type)
1400                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1401                                 if (real_target_type == TypeManager.int32_type)
1402                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1403                                 if (real_target_type == TypeManager.uint32_type)
1404                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1405                                 if (real_target_type == TypeManager.int64_type)
1406                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1407                                 if (real_target_type == TypeManager.uint64_type)
1408                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1409                                 if (real_target_type == TypeManager.char_type)
1410                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1411                         } else if (expr_type == TypeManager.double_type){
1412                                 //
1413                                 // From double to byte, byte, short,
1414                                 // ushort, int, uint, long, ulong,
1415                                 // char, float or decimal
1416                                 //
1417                                 if (real_target_type == TypeManager.sbyte_type)
1418                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1419                                 if (real_target_type == TypeManager.byte_type)
1420                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1421                                 if (real_target_type == TypeManager.short_type)
1422                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1423                                 if (real_target_type == TypeManager.ushort_type)
1424                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1425                                 if (real_target_type == TypeManager.int32_type)
1426                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1427                                 if (real_target_type == TypeManager.uint32_type)
1428                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1429                                 if (real_target_type == TypeManager.int64_type)
1430                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1431                                 if (real_target_type == TypeManager.uint64_type)
1432                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1433                                 if (real_target_type == TypeManager.char_type)
1434                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1435                                 if (real_target_type == TypeManager.float_type)
1436                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1437                         } 
1438
1439                         // decimal is taken care of by the op_Explicit methods.
1440
1441                         return null;
1442                 }
1443
1444                 /// <summary>
1445                 ///  Returns whether an explicit reference conversion can be performed
1446                 ///  from source_type to target_type
1447                 /// </summary>
1448                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1449                 {
1450                         bool target_is_type_param = target_type.IsGenericParameter;
1451                         bool target_is_value_type = target_type.IsValueType;
1452                         
1453                         if (source_type == target_type)
1454                                 return true;
1455
1456                         //
1457                         // From object to a generic parameter
1458                         //
1459                         if (source_type == TypeManager.object_type && target_is_type_param)
1460                                 return true;
1461
1462                         //
1463                         // From object to any reference type
1464                         //
1465                         if (source_type == TypeManager.object_type && !target_is_value_type)
1466                                 return true;
1467                                         
1468                         //
1469                         // From any class S to any class-type T, provided S is a base class of T
1470                         //
1471                         if (TypeManager.IsSubclassOf (target_type, source_type))
1472                                 return true;
1473
1474                         //
1475                         // From any interface type S to any interface T provided S is not derived from T
1476                         //
1477                         if (source_type.IsInterface && target_type.IsInterface){
1478                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1479                                         return true;
1480                         }
1481                             
1482                         //
1483                         // From any class type S to any interface T, provided S is not sealed
1484                         // and provided S does not implement T.
1485                         //
1486                         if (target_type.IsInterface && !source_type.IsSealed &&
1487                             !TypeManager.ImplementsInterface (source_type, target_type))
1488                                 return true;
1489
1490                         //
1491                         // From any interface-type S to to any class type T, provided T is not
1492                         // sealed, or provided T implements S.
1493                         //
1494                         if (source_type.IsInterface &&
1495                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1496                                 return true;
1497                         
1498                         
1499                         // From an array type S with an element type Se to an array type T with an 
1500                         // element type Te provided all the following are true:
1501                         //     * S and T differe only in element type, in other words, S and T
1502                         //       have the same number of dimensions.
1503                         //     * Both Se and Te are reference types
1504                         //     * An explicit referenc conversions exist from Se to Te
1505                         //
1506                         if (source_type.IsArray && target_type.IsArray) {
1507                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1508                                         
1509                                         Type source_element_type = TypeManager.GetElementType (source_type);
1510                                         Type target_element_type = TypeManager.GetElementType (target_type);
1511                                         
1512                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1513                                                 if (ExplicitReferenceConversionExists (source_element_type,
1514                                                                                        target_element_type))
1515                                                         return true;
1516                                 }
1517                         }
1518                         
1519
1520                         // From System.Array to any array-type
1521                         if (source_type == TypeManager.array_type &&
1522                             target_type.IsArray){
1523                                 return true;
1524                         }
1525
1526                         //
1527                         // From System delegate to any delegate-type
1528                         //
1529                         if (source_type == TypeManager.delegate_type &&
1530                             TypeManager.IsDelegateType (target_type))
1531                                 return true;
1532
1533                         //
1534                         // From ICloneable to Array or Delegate types
1535                         //
1536                         if (source_type == TypeManager.icloneable_type &&
1537                             (target_type == TypeManager.array_type ||
1538                              target_type == TypeManager.delegate_type))
1539                                 return true;
1540                         
1541                         return false;
1542                 }
1543
1544                 /// <summary>
1545                 ///   Implements Explicit Reference conversions
1546                 /// </summary>
1547                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1548                 {
1549                         Type source_type = source.Type;
1550                         bool target_is_type_param = target_type.IsGenericParameter;
1551                         bool target_is_value_type = target_type.IsValueType;
1552
1553                         //
1554                         // From object to a generic parameter
1555                         //
1556                         if (source_type == TypeManager.object_type && target_is_type_param)
1557                                 return new UnboxCast (source, target_type);
1558
1559                         //
1560                         // From object to any reference type
1561                         //
1562                         if (source_type == TypeManager.object_type && !target_is_value_type)
1563                                 return new ClassCast (source, target_type);
1564
1565                         //
1566                         // Unboxing conversion.
1567                         //
1568                         if (((source_type == TypeManager.enum_type &&
1569                                 !(source is EmptyCast)) ||
1570                                 source_type == TypeManager.value_type) && target_is_value_type)
1571                                 return new UnboxCast (source, target_type);
1572
1573                         //
1574                         // From any class S to any class-type T, provided S is a base class of T
1575                         //
1576                         if (TypeManager.IsSubclassOf (target_type, source_type))
1577                                 return new ClassCast (source, target_type);
1578
1579                         //
1580                         // From any interface type S to any interface T provided S is not derived from T
1581                         //
1582                         if (source_type.IsInterface && target_type.IsInterface){
1583                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1584                                         return null;
1585                                 else
1586                                         return new ClassCast (source, target_type);
1587                         }
1588
1589                         //
1590                         // From any class type S to any interface T, provides S is not sealed
1591                         // and provided S does not implement T.
1592                         //
1593                         if (target_type.IsInterface && !source_type.IsSealed) {
1594                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1595                                         return null;
1596                                 else
1597                                         return new ClassCast (source, target_type);
1598                                 
1599                         }
1600
1601                         //
1602                         // From any interface-type S to to any class type T, provided T is not
1603                         // sealed, or provided T implements S.
1604                         //
1605                         if (source_type.IsInterface) {
1606                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1607                                         if (target_type.IsClass)
1608                                                 return new ClassCast (source, target_type);
1609                                         else
1610                                                 return new UnboxCast (source, target_type);
1611                                 }
1612
1613                                 return null;
1614                         }
1615                         
1616                         // From an array type S with an element type Se to an array type T with an 
1617                         // element type Te provided all the following are true:
1618                         //     * S and T differe only in element type, in other words, S and T
1619                         //       have the same number of dimensions.
1620                         //     * Both Se and Te are reference types
1621                         //     * An explicit referenc conversions exist from Se to Te
1622                         //
1623                         if (source_type.IsArray && target_type.IsArray) {
1624                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1625                                         
1626                                         Type source_element_type = TypeManager.GetElementType (source_type);
1627                                         Type target_element_type = TypeManager.GetElementType (target_type);
1628                                         
1629                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1630                                                 if (ExplicitReferenceConversionExists (source_element_type,
1631                                                                                        target_element_type))
1632                                                         return new ClassCast (source, target_type);
1633                                 }
1634                         }
1635                         
1636
1637                         // From System.Array to any array-type
1638                         if (source_type == TypeManager.array_type &&
1639                             target_type.IsArray) {
1640                                 return new ClassCast (source, target_type);
1641                         }
1642
1643                         //
1644                         // From System delegate to any delegate-type
1645                         //
1646                         if (source_type == TypeManager.delegate_type &&
1647                             TypeManager.IsDelegateType (target_type))
1648                                 return new ClassCast (source, target_type);
1649
1650                         //
1651                         // From ICloneable to Array or Delegate types
1652                         //
1653                         if (source_type == TypeManager.icloneable_type &&
1654                             (target_type == TypeManager.array_type ||
1655                              target_type == TypeManager.delegate_type))
1656                                 return new ClassCast (source, target_type);
1657                         
1658                         return null;
1659                 }
1660                 
1661                 /// <summary>
1662                 ///   Performs an explicit conversion of the expression `expr' whose
1663                 ///   type is expr.Type to `target_type'.
1664                 /// </summary>
1665                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1666                                                              Type target_type, Location loc)
1667                 {
1668                         Type expr_type = expr.Type;
1669                         Type original_expr_type = expr_type;
1670
1671                         if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1672                                 if (target_type == TypeManager.enum_type ||
1673                                     target_type == TypeManager.object_type) {
1674                                         if (expr is EnumConstant)
1675                                                 expr = ((EnumConstant) expr).Child;
1676                                         // We really need all these casts here .... :-(
1677                                         expr = new BoxedCast (new EmptyCast (expr, expr_type));
1678                                         return new EmptyCast (expr, target_type);
1679                                 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1680                                            target_type.IsSubclassOf (TypeManager.enum_type))
1681                                         return new UnboxCast (expr, target_type);
1682
1683                                 //
1684                                 // Notice that we have kept the expr_type unmodified, which is only
1685                                 // used later on to 
1686                                 if (expr is EnumConstant)
1687                                         expr = ((EnumConstant) expr).Child;
1688                                 else
1689                                         expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1690                                 expr_type = expr.Type;
1691                         }
1692
1693                         int errors = Report.Errors;
1694                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1695                         if (Report.Errors > errors)
1696                                 return null;
1697
1698                         if (ne != null)
1699                                 return ne;
1700
1701                         ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1702                         if (ne != null)
1703                                 return ne;
1704
1705                         //
1706                         // Unboxing conversion.
1707                         //
1708                         if (expr_type == TypeManager.object_type && target_type.IsValueType){
1709                                 if (expr is NullLiteral){
1710                                         //
1711                                         // Skip the ExplicitReferenceConversion because we can not convert
1712                                         // from Null to a ValueType, and ExplicitReference wont check against
1713                                         // null literal explicitly
1714                                         //
1715                                         goto skip_explicit;
1716                                 }
1717                                 return new UnboxCast (expr, target_type);
1718
1719                         }
1720
1721                         ne = ExplicitReferenceConversion (expr, target_type);
1722                         if (ne != null)
1723                                 return ne;
1724
1725                 skip_explicit:
1726                         if (ec.InUnsafe){
1727                                 if (target_type.IsPointer){
1728                                         if (expr_type.IsPointer)
1729                                                 return new EmptyCast (expr, target_type);
1730                                         
1731                                         if (expr_type == TypeManager.sbyte_type ||
1732                                             expr_type == TypeManager.byte_type ||
1733                                             expr_type == TypeManager.short_type ||
1734                                             expr_type == TypeManager.ushort_type ||
1735                                             expr_type == TypeManager.int32_type ||
1736                                             expr_type == TypeManager.uint32_type ||
1737                                             expr_type == TypeManager.uint64_type ||
1738                                             expr_type == TypeManager.int64_type)
1739                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1740                                 }
1741                                 if (expr_type.IsPointer){
1742                                         Expression e = null;
1743                                         
1744                                         if (target_type == TypeManager.sbyte_type)
1745                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1746                                         else if (target_type == TypeManager.byte_type)
1747                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1748                                         else if (target_type == TypeManager.short_type)
1749                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1750                                         else if (target_type == TypeManager.ushort_type)
1751                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1752                                         else if (target_type == TypeManager.int32_type)
1753                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1754                                         else if (target_type == TypeManager.uint32_type)
1755                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1756                                         else if (target_type == TypeManager.uint64_type)
1757                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1758                                         else if (target_type == TypeManager.int64_type){
1759                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1760                                         }
1761
1762                                         if (e != null){
1763                                                 Expression ci, ce;
1764
1765                                                 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1766
1767                                                 if (ci != null)
1768                                                         return ci;
1769
1770                                                 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1771                                                 if (ce != null)
1772                                                         return ce;
1773                                                 //
1774                                                 // We should always be able to go from an uint32
1775                                                 // implicitly or explicitly to the other integral
1776                                                 // types
1777                                                 //
1778                                                 throw new Exception ("Internal compiler error");
1779                                         }
1780                                 }
1781                         }
1782                         
1783                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1784                         if (ne != null)
1785                                 return ne;
1786
1787                         if (expr is NullLiteral){
1788                                 Report.Error (37, loc, "Cannot convert null to value type `" +
1789                                               TypeManager.CSharpName (target_type) + "'");
1790                                 return null;
1791                         }
1792                                 
1793                         Error_CannotConvertType (loc, original_expr_type, target_type);
1794                         return null;
1795                 }
1796
1797                 /// <summary>
1798                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
1799                 /// </summary>
1800                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1801                                                                      Type target_type, Location l)
1802                 {
1803                         int errors = Report.Errors;
1804                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1805                         if (Report.Errors > errors)
1806                                 return null;
1807
1808                         if (ne != null)
1809                                 return ne;
1810
1811                         ne = ExplicitNumericConversion (ec, expr, target_type, l);
1812                         if (ne != null)
1813                                 return ne;
1814
1815                         ne = ExplicitReferenceConversion (expr, target_type);
1816                         if (ne != null)
1817                                 return ne;
1818
1819                         Error_CannotConvertType (l, expr.Type, target_type);
1820                         return null;
1821                 }
1822         }
1823 }