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