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