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