**** Merged r37411-r37541 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 public void Error_CannotConvertType (Location loc, Type source, Type target)
33                 {
34                         Report.Error (30, loc, "Cannot convert type '" +
35                                       TypeManager.CSharpName (source) + "' to '" +
36                                       TypeManager.CSharpName (target) + "'");
37                 }
38
39                 static 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
763                                 Expression conv = am.Compatible (ec, target_type, true);
764                                 if (conv != null)
765                                         return true;
766                         }
767                         
768                         return false;
769                 }
770
771                 //
772                 // Used internally by FindMostEncompassedType, this is used
773                 // to avoid creating lots of objects in the tight loop inside
774                 // FindMostEncompassedType
775                 //
776                 static EmptyExpression priv_fmet_param;
777                 
778                 /// <summary>
779                 ///  Finds "most encompassed type" according to the spec (13.4.2)
780                 ///  amongst the methods in the MethodGroupExpr
781                 /// </summary>
782                 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
783                 {
784                         Type best = null;
785
786                         if (priv_fmet_param == null)
787                                 priv_fmet_param = new EmptyExpression ();
788
789                         foreach (Type t in types){
790                                 priv_fmet_param.SetType (t);
791                                 
792                                 if (best == null) {
793                                         best = t;
794                                         continue;
795                                 }
796                                 
797                                 if (ImplicitStandardConversionExists (ec, priv_fmet_param, best))
798                                         best = t;
799                         }
800
801                         return best;
802                 }
803
804                 //
805                 // Used internally by FindMostEncompassingType, this is used
806                 // to avoid creating lots of objects in the tight loop inside
807                 // FindMostEncompassingType
808                 //
809                 static EmptyExpression priv_fmee_ret;
810                 
811                 /// <summary>
812                 ///  Finds "most encompassing type" according to the spec (13.4.2)
813                 ///  amongst the types in the given set
814                 /// </summary>
815                 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
816                 {
817                         Type best = null;
818
819                         if (priv_fmee_ret == null)
820                                 priv_fmee_ret = new EmptyExpression ();
821
822                         foreach (Type t in types){
823                                 priv_fmee_ret.SetType (best);
824
825                                 if (best == null) {
826                                         best = t;
827                                         continue;
828                                 }
829
830                                 if (ImplicitStandardConversionExists (ec, priv_fmee_ret, t))
831                                         best = t;
832                         }
833                         
834                         return best;
835                 }
836
837                 //
838                 // Used to avoid creating too many objects
839                 //
840                 static EmptyExpression priv_fms_expr;
841                 
842                 /// <summary>
843                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
844                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
845                 ///   for explicit and implicit conversion operators.
846                 /// </summary>
847                 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
848                                                            Expression source, bool apply_explicit_conv_rules,
849                                                            Location loc)
850                 {
851                         ArrayList src_types_set = new ArrayList ();
852                         
853                         if (priv_fms_expr == null)
854                                 priv_fms_expr = new EmptyExpression ();
855
856                         //
857                         // If any operator converts from S then Sx = S
858                         //
859                         Type source_type = source.Type;
860                         foreach (MethodBase mb in me.Methods){
861                                 ParameterData pd = Invocation.GetParameterData (mb);
862                                 Type param_type = pd.ParameterType (0);
863
864                                 if (param_type == source_type)
865                                         return param_type;
866
867                                 if (apply_explicit_conv_rules) {
868                                         //
869                                         // From the spec :
870                                         // Find the set of applicable user-defined conversion operators, U.  This set
871                                         // consists of the
872                                         // user-defined implicit or explicit conversion operators declared by
873                                         // the classes or structs in D that convert from a type encompassing
874                                         // or encompassed by S to a type encompassing or encompassed by T
875                                         //
876                                         priv_fms_expr.SetType (param_type);
877                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, source_type))
878                                                 src_types_set.Add (param_type);
879                                         else {
880                                                 if (ImplicitStandardConversionExists (ec, source, param_type))
881                                                         src_types_set.Add (param_type);
882                                         }
883                                 } else {
884                                         //
885                                         // Only if S is encompassed by param_type
886                                         //
887                                         if (ImplicitStandardConversionExists (ec, source, param_type))
888                                                 src_types_set.Add (param_type);
889                                 }
890                         }
891                         
892                         //
893                         // Explicit Conv rules
894                         //
895                         if (apply_explicit_conv_rules) {
896                                 ArrayList candidate_set = new ArrayList ();
897
898                                 foreach (Type param_type in src_types_set){
899                                         if (ImplicitStandardConversionExists (ec, source, param_type))
900                                                 candidate_set.Add (param_type);
901                                 }
902
903                                 if (candidate_set.Count != 0)
904                                         return FindMostEncompassedType (ec, candidate_set);
905                         }
906
907                         //
908                         // Final case
909                         //
910                         if (apply_explicit_conv_rules)
911                                 return FindMostEncompassingType (ec, src_types_set);
912                         else
913                                 return FindMostEncompassedType (ec, src_types_set);
914                 }
915
916                 //
917                 // Useful in avoiding proliferation of objects
918                 //
919                 static EmptyExpression priv_fmt_expr;
920                 
921                 /// <summary>
922                 ///  Finds the most specific target Tx according to section 13.4.4
923                 /// </summary>
924                 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
925                                                            Type target, bool apply_explicit_conv_rules,
926                                                            Location loc)
927                 {
928                         ArrayList tgt_types_set = new ArrayList ();
929                         
930                         if (priv_fmt_expr == null)
931                                 priv_fmt_expr = new EmptyExpression ();
932                         
933                         //
934                         // If any operator converts to T then Tx = T
935                         //
936                         foreach (MethodInfo mi in me.Methods){
937                                 Type ret_type = mi.ReturnType;
938
939                                 if (ret_type == target)
940                                         return ret_type;
941
942                                 if (apply_explicit_conv_rules) {
943                                         //
944                                         // From the spec :
945                                         // Find the set of applicable user-defined conversion operators, U.
946                                         //
947                                         // This set consists of the
948                                         // user-defined implicit or explicit conversion operators declared by
949                                         // the classes or structs in D that convert from a type encompassing
950                                         // or encompassed by S to a type encompassing or encompassed by T
951                                         //
952                                         priv_fms_expr.SetType (ret_type);
953                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
954                                                 tgt_types_set.Add (ret_type);
955                                         else {
956                                                 priv_fms_expr.SetType (target);
957                                                 if (ImplicitStandardConversionExists (ec, priv_fms_expr, ret_type))
958                                                         tgt_types_set.Add (ret_type);
959                                         }
960                                 } else {
961                                         //
962                                         // Only if T is encompassed by param_type
963                                         //
964                                         priv_fms_expr.SetType (ret_type);
965                                         if (ImplicitStandardConversionExists (ec, priv_fms_expr, target))
966                                                 tgt_types_set.Add (ret_type);
967                                 }
968                         }
969
970                         //
971                         // Explicit conv rules
972                         //
973                         if (apply_explicit_conv_rules) {
974                                 ArrayList candidate_set = new ArrayList ();
975
976                                 foreach (Type ret_type in tgt_types_set){
977                                         priv_fmt_expr.SetType (ret_type);
978                                         
979                                         if (ImplicitStandardConversionExists (ec, priv_fmt_expr, target))
980                                                 candidate_set.Add (ret_type);
981                                 }
982
983                                 if (candidate_set.Count != 0)
984                                         return FindMostEncompassingType (ec, candidate_set);
985                         }
986                         
987                         //
988                         // Okay, final case !
989                         //
990                         if (apply_explicit_conv_rules)
991                                 return FindMostEncompassedType (ec, tgt_types_set);
992                         else 
993                                 return FindMostEncompassingType (ec, tgt_types_set);
994                 }
995                 
996                 /// <summary>
997                 ///  User-defined Implicit conversions
998                 /// </summary>
999                 static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
1000                                                                  Type target, Location loc)
1001                 {
1002                         return UserDefinedConversion (ec, source, target, loc, false);
1003                 }
1004
1005                 /// <summary>
1006                 ///  User-defined Explicit conversions
1007                 /// </summary>
1008                 static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
1009                                                                  Type target, Location loc)
1010                 {
1011                         return UserDefinedConversion (ec, source, target, loc, true);
1012                 }
1013
1014                 static DoubleHash explicit_conv = new DoubleHash (100);
1015                 static DoubleHash implicit_conv = new DoubleHash (100);
1016                 /// <summary>
1017                 ///   Computes the MethodGroup for the user-defined conversion
1018                 ///   operators from source_type to target_type.  `look_for_explicit'
1019                 ///   controls whether we should also include the list of explicit
1020                 ///   operators
1021                 /// </summary>
1022                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1023                                                                Type source_type, Type target_type,
1024                                                                Location loc, bool look_for_explicit)
1025                 {
1026                         Expression mg1 = null, mg2 = null;
1027                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1028                         string op_name;
1029
1030                         op_name = "op_Implicit";
1031
1032                         MethodGroupExpr union3;
1033                         object r;
1034                         if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1035                                 return (MethodGroupExpr) r;
1036
1037                         mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1038                         if (source_type.BaseType != null)
1039                                 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1040
1041                         if (mg1 == null)
1042                                 union3 = (MethodGroupExpr) mg2;
1043                         else if (mg2 == null)
1044                                 union3 = (MethodGroupExpr) mg1;
1045                         else
1046                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1047
1048                         mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1049                         if (mg1 != null){
1050                                 if (union3 != null)
1051                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1052                                 else
1053                                         union3 = (MethodGroupExpr) mg1;
1054                         }
1055
1056                         if (target_type.BaseType != null)
1057                                 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1058                         
1059                         if (mg1 != null){
1060                                 if (union3 != null)
1061                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1062                                 else
1063                                         union3 = (MethodGroupExpr) mg1;
1064                         }
1065
1066                         MethodGroupExpr union4 = null;
1067
1068                         if (look_for_explicit) {
1069                                 op_name = "op_Explicit";
1070
1071                                 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1072                                 if (source_type.BaseType != null)
1073                                         mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1074                                 
1075                                 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1076                                 if (target_type.BaseType != null)
1077                                         mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1078                                 
1079                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1080                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1081
1082                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1083                         }
1084                         
1085                         MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1086                         (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1087                         return ret;
1088                 }
1089                 
1090                 /// <summary>
1091                 ///   User-defined conversions
1092                 /// </summary>
1093                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1094                                                                 Type target, Location loc,
1095                                                                 bool look_for_explicit)
1096                 {
1097                         MethodGroupExpr union;
1098                         Type source_type = source.Type;
1099                         MethodBase method = null;
1100
1101                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1102                         if (union == null)
1103                                 return null;
1104                         
1105                         Type most_specific_source, most_specific_target;
1106
1107                         most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1108                         if (most_specific_source == null)
1109                                 return null;
1110
1111                         most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1112                         if (most_specific_target == null) 
1113                                 return null;
1114
1115                         int count = 0;
1116
1117                         
1118                         foreach (MethodBase mb in union.Methods){
1119                                 ParameterData pd = Invocation.GetParameterData (mb);
1120                                 MethodInfo mi = (MethodInfo) mb;
1121                                 
1122                                 if (pd.ParameterType (0) == most_specific_source &&
1123                                     mi.ReturnType == most_specific_target) {
1124                                         method = mb;
1125                                         count++;
1126                                 }
1127                         }
1128                         
1129                         if (method == null || count > 1)
1130                                 return null;
1131                         
1132                         
1133                         //
1134                         // This will do the conversion to the best match that we
1135                         // found.  Now we need to perform an implict standard conversion
1136                         // if the best match was not the type that we were requested
1137                         // by target.
1138                         //
1139                         if (look_for_explicit)
1140                                 source = ExplicitConversionStandard (ec, source, most_specific_source, loc);
1141                         else
1142                                 source = ImplicitConversionStandard (ec, source, most_specific_source, loc);
1143
1144                         if (source == null)
1145                                 return null;
1146
1147                         Expression e;
1148                         e =  new UserCast ((MethodInfo) method, source, loc);
1149                         if (e.Type != target){
1150                                 if (!look_for_explicit)
1151                                         e = ImplicitConversionStandard (ec, e, target, loc);
1152                                 else
1153                                         e = ExplicitConversionStandard (ec, e, target, loc);
1154                         }
1155
1156                         return e;
1157                 }
1158                 
1159                 /// <summary>
1160                 ///   Converts implicitly the resolved expression `expr' into the
1161                 ///   `target_type'.  It returns a new expression that can be used
1162                 ///   in a context that expects a `target_type'. 
1163                 /// </summary>
1164                 static public Expression ImplicitConversion (EmitContext ec, Expression expr,
1165                                                              Type target_type, Location loc)
1166                 {
1167                         Expression e;
1168
1169                         if (target_type == null)
1170                                 throw new Exception ("Target type is null");
1171
1172                         e = ImplicitConversionStandard (ec, expr, target_type, loc);
1173                         if (e != null)
1174                                 return e;
1175
1176                         e = ImplicitUserConversion (ec, expr, target_type, loc);
1177                         if (e != null)
1178                                 return e;
1179
1180                         return null;
1181                 }
1182
1183                 
1184                 /// <summary>
1185                 ///   Attempts to apply the `Standard Implicit
1186                 ///   Conversion' rules to the expression `expr' into
1187                 ///   the `target_type'.  It returns a new expression
1188                 ///   that can be used in a context that expects a
1189                 ///   `target_type'.
1190                 ///
1191                 ///   This is different from `ImplicitConversion' in that the
1192                 ///   user defined implicit conversions are excluded. 
1193                 /// </summary>
1194                 static public Expression ImplicitConversionStandard (EmitContext ec, Expression expr,
1195                                                                      Type target_type, Location loc)
1196                 {
1197                         Type expr_type = expr.Type;
1198                         Expression e;
1199
1200                         if ((expr is NullLiteral) && target_type.IsGenericParameter)
1201                                 return TypeParameter_to_Null (expr, target_type, loc);
1202
1203                         if (expr.eclass == ExprClass.MethodGroup){
1204                                 if (!TypeManager.IsDelegateType (target_type)){
1205                                         return null;
1206                                 }
1207
1208                                 //
1209                                 // Only allow anonymous method conversions on post ISO_1
1210                                 //
1211                                 if (RootContext.Version != LanguageVersion.ISO_1){
1212                                         MethodGroupExpr mg = expr as MethodGroupExpr;
1213                                         if (mg != null)
1214                                                 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1215                                 }
1216                         }
1217
1218                         if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1219                                 return expr;
1220
1221                         e = ImplicitNumericConversion (ec, expr, target_type, loc);
1222                         if (e != null)
1223                                 return e;
1224
1225                         e = ImplicitReferenceConversion (ec, expr, target_type);
1226                         if (e != null)
1227                                 return e;
1228                         
1229                         if ((target_type == TypeManager.enum_type ||
1230                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
1231                             expr is IntLiteral){
1232                                 IntLiteral i = (IntLiteral) expr;
1233
1234                                 if (i.Value == 0)
1235                                         return new EnumConstant ((Constant) expr, target_type);
1236                         }
1237
1238                         if (ec.InUnsafe) {
1239                                 if (expr_type.IsPointer){
1240                                         if (target_type == TypeManager.void_ptr_type)
1241                                                 return new EmptyCast (expr, target_type);
1242
1243                                         //
1244                                         // yep, comparing pointer types cant be done with
1245                                         // t1 == t2, we have to compare their element types.
1246                                         //
1247                                         if (target_type.IsPointer){
1248                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1249                                                         return expr;
1250                                         }
1251                                 }
1252                                 
1253                                 if (target_type.IsPointer) {
1254                                         if (expr_type == TypeManager.null_type)
1255                                                 return new EmptyCast (expr, target_type);
1256
1257                                         if (expr_type == TypeManager.void_ptr_type)
1258                                                 return new EmptyCast (expr, target_type);
1259                                 }
1260                         }
1261
1262                         if (expr_type == TypeManager.anonymous_method_type){
1263                                 if (!TypeManager.IsDelegateType (target_type)){
1264                                         Report.Error (1660, loc,
1265                                                               "Cannot convert anonymous method to `{0}', since it is not a delegate",
1266                                                               TypeManager.CSharpName (target_type));
1267                                         return null;
1268                                 }
1269
1270                                 AnonymousMethod am = (AnonymousMethod) expr;
1271                                 int errors = Report.Errors;
1272
1273                                 Expression conv = am.Compatible (ec, target_type, false);
1274                                 if (conv != null)
1275                                         return conv;
1276                                 
1277                                 //
1278                                 // We return something instead of null, to avoid
1279                                 // the duplicate error, since am.Compatible would have
1280                                 // reported that already
1281                                 //
1282                                 if (errors != Report.Errors)
1283                                         return new EmptyCast (expr, target_type);
1284                         }
1285                         
1286                         return null;
1287                 }
1288
1289                 /// <summary>
1290                 ///   Attemps to perform an implict constant conversion of the IntConstant
1291                 ///   into a different data type using casts (See Implicit Constant
1292                 ///   Expression Conversions)
1293                 /// </summary>
1294                 static public Expression TryImplicitIntConversion (Type target_type, IntConstant ic)
1295                 {
1296                         int value = ic.Value;
1297
1298                         if (target_type == TypeManager.sbyte_type){
1299                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1300                                         return new SByteConstant ((sbyte) value);
1301                         } else if (target_type == TypeManager.byte_type){
1302                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1303                                         return new ByteConstant ((byte) value);
1304                         } else if (target_type == TypeManager.short_type){
1305                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1306                                         return new ShortConstant ((short) value);
1307                         } else if (target_type == TypeManager.ushort_type){
1308                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1309                                         return new UShortConstant ((ushort) value);
1310                         } else if (target_type == TypeManager.uint32_type){
1311                                 if (value >= 0)
1312                                         return new UIntConstant ((uint) value);
1313                         } else if (target_type == TypeManager.uint64_type){
1314                                 //
1315                                 // we can optimize this case: a positive int32
1316                                 // always fits on a uint64.  But we need an opcode
1317                                 // to do it.
1318                                 //
1319                                 if (value >= 0)
1320                                         return new ULongConstant ((ulong) value);
1321                         } else if (target_type == TypeManager.double_type)
1322                                 return new DoubleConstant ((double) value);
1323                         else if (target_type == TypeManager.float_type)
1324                                 return new FloatConstant ((float) value);
1325                         
1326                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1327                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1328                                 Constant e = (Constant) ic;
1329                                 
1330                                 //
1331                                 // Possibly, we need to create a different 0 literal before passing
1332                                 // to EnumConstant
1333                                 //
1334                                 if (underlying == TypeManager.int64_type)
1335                                         e = new LongLiteral (0);
1336                                 else if (underlying == TypeManager.uint64_type)
1337                                         e = new ULongLiteral (0);
1338
1339                                 return new EnumConstant (e, target_type);
1340                         }
1341                         return null;
1342                 }
1343
1344                 static public void Error_CannotImplicitConversion (Location loc, Type source, Type target)
1345                 {
1346                         if (source.Name == target.Name){
1347                                 Report.ExtraInformation (loc,
1348                                          String.Format (
1349                                                 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1350                                                 source.Assembly.FullName, target.Assembly.FullName));
1351                                                          
1352                         }
1353                         Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1354                                       source == TypeManager.anonymous_method_type ?
1355                                       "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1356                                       TypeManager.CSharpName (target));
1357                 }
1358
1359                 /// <summary>
1360                 ///   Attemptes to implicityly convert `target' into `type', using
1361                 ///   ImplicitConversion.  If there is no implicit conversion, then
1362                 ///   an error is signaled
1363                 /// </summary>
1364                 static public Expression ImplicitConversionRequired (EmitContext ec, Expression source,
1365                                                                      Type target_type, Location loc)
1366                 {
1367                         Expression e;
1368
1369                         int errors = Report.Errors;
1370                         e = ImplicitConversion (ec, source, target_type, loc);
1371                         if (Report.Errors > errors)
1372                                 return null;
1373                         if (e != null)
1374                                 return e;
1375
1376                         if (source is DoubleLiteral) {
1377                                 if (target_type == TypeManager.float_type) {
1378                                         Error_664 (loc, "float", "f");
1379                                         return null;
1380                                 }
1381                                 if (target_type == TypeManager.decimal_type) {
1382                                         Error_664 (loc, "decimal", "m");
1383                                         return null;
1384                                 }
1385                         }
1386
1387                         if (source is Constant){
1388                                 Constant c = (Constant) source;
1389
1390                                 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1391                                 return null;
1392                         }
1393                         
1394                         Error_CannotImplicitConversion (loc, source.Type, target_type);
1395
1396                         return null;
1397                 }
1398
1399                 static void Error_664 (Location loc, string type, string suffix) {
1400                         Report.Error (664, loc,
1401                                 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1402                                 type, suffix);
1403                 }
1404
1405                 /// <summary>
1406                 ///   Performs the explicit numeric conversions
1407                 /// </summary>
1408                 static Expression ExplicitNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1409                 {
1410                         Type expr_type = expr.Type;
1411
1412                         //
1413                         // If we have an enumeration, extract the underlying type,
1414                         // use this during the comparison, but wrap around the original
1415                         // target_type
1416                         //
1417                         Type real_target_type = target_type;
1418
1419                         if (TypeManager.IsEnumType (real_target_type))
1420                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1421
1422                         if (ImplicitStandardConversionExists (ec, expr, real_target_type)){
1423                                 Expression ce = ImplicitConversionStandard (ec, expr, real_target_type, loc);
1424
1425                                 if (real_target_type != target_type)
1426                                         return new EmptyCast (ce, target_type);
1427                                 return ce;
1428                         }
1429                         
1430                         if (expr_type == TypeManager.sbyte_type){
1431                                 //
1432                                 // From sbyte to byte, ushort, uint, ulong, char
1433                                 //
1434                                 if (real_target_type == TypeManager.byte_type)
1435                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U1);
1436                                 if (real_target_type == TypeManager.ushort_type)
1437                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U2);
1438                                 if (real_target_type == TypeManager.uint32_type)
1439                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U4);
1440                                 if (real_target_type == TypeManager.uint64_type)
1441                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_U8);
1442                                 if (real_target_type == TypeManager.char_type)
1443                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I1_CH);
1444                         } else if (expr_type == TypeManager.byte_type){
1445                                 //
1446                                 // From byte to sbyte and char
1447                                 //
1448                                 if (real_target_type == TypeManager.sbyte_type)
1449                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_I1);
1450                                 if (real_target_type == TypeManager.char_type)
1451                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U1_CH);
1452                         } else if (expr_type == TypeManager.short_type){
1453                                 //
1454                                 // From short to sbyte, byte, ushort, uint, ulong, char
1455                                 //
1456                                 if (real_target_type == TypeManager.sbyte_type)
1457                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_I1);
1458                                 if (real_target_type == TypeManager.byte_type)
1459                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1460                                 if (real_target_type == TypeManager.ushort_type)
1461                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U2);
1462                                 if (real_target_type == TypeManager.uint32_type)
1463                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U4);
1464                                 if (real_target_type == TypeManager.uint64_type)
1465                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U8);
1466                                 if (real_target_type == TypeManager.char_type)
1467                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_CH);
1468                         } else if (expr_type == TypeManager.ushort_type){
1469                                 //
1470                                 // From ushort to sbyte, byte, short, char
1471                                 //
1472                                 if (real_target_type == TypeManager.sbyte_type)
1473                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I1);
1474                                 if (real_target_type == TypeManager.byte_type)
1475                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_U1);
1476                                 if (real_target_type == TypeManager.short_type)
1477                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_I2);
1478                                 if (real_target_type == TypeManager.char_type)
1479                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U2_CH);
1480                         } else if (expr_type == TypeManager.int32_type){
1481                                 //
1482                                 // From int to sbyte, byte, short, ushort, uint, ulong, char
1483                                 //
1484                                 if (real_target_type == TypeManager.sbyte_type)
1485                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I1);
1486                                 if (real_target_type == TypeManager.byte_type)
1487                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1488                                 if (real_target_type == TypeManager.short_type)
1489                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1490                                 if (real_target_type == TypeManager.ushort_type)
1491                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U2);
1492                                 if (real_target_type == TypeManager.uint32_type)
1493                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U4);
1494                                 if (real_target_type == TypeManager.uint64_type)
1495                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U8);
1496                                 if (real_target_type == TypeManager.char_type)
1497                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_CH);
1498                         } else if (expr_type == TypeManager.uint32_type){
1499                                 //
1500                                 // From uint to sbyte, byte, short, ushort, int, char
1501                                 //
1502                                 if (real_target_type == TypeManager.sbyte_type)
1503                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I1);
1504                                 if (real_target_type == TypeManager.byte_type)
1505                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U1);
1506                                 if (real_target_type == TypeManager.short_type)
1507                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I2);
1508                                 if (real_target_type == TypeManager.ushort_type)
1509                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_U2);
1510                                 if (real_target_type == TypeManager.int32_type)
1511                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_I4);
1512                                 if (real_target_type == TypeManager.char_type)
1513                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U4_CH);
1514                         } else if (expr_type == TypeManager.int64_type){
1515                                 //
1516                                 // From long to sbyte, byte, short, ushort, int, uint, ulong, char
1517                                 //
1518                                 if (real_target_type == TypeManager.sbyte_type)
1519                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I1);
1520                                 if (real_target_type == TypeManager.byte_type)
1521                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1522                                 if (real_target_type == TypeManager.short_type)
1523                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1524                                 if (real_target_type == TypeManager.ushort_type)
1525                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U2);
1526                                 if (real_target_type == TypeManager.int32_type)
1527                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1528                                 if (real_target_type == TypeManager.uint32_type)
1529                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U4);
1530                                 if (real_target_type == TypeManager.uint64_type)
1531                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U8);
1532                                 if (real_target_type == TypeManager.char_type)
1533                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_CH);
1534                         } else if (expr_type == TypeManager.uint64_type){
1535                                 //
1536                                 // From ulong to sbyte, byte, short, ushort, int, uint, long, char
1537                                 //
1538                                 if (real_target_type == TypeManager.sbyte_type)
1539                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I1);
1540                                 if (real_target_type == TypeManager.byte_type)
1541                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U1);
1542                                 if (real_target_type == TypeManager.short_type)
1543                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I2);
1544                                 if (real_target_type == TypeManager.ushort_type)
1545                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U2);
1546                                 if (real_target_type == TypeManager.int32_type)
1547                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I4);
1548                                 if (real_target_type == TypeManager.uint32_type)
1549                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_U4);
1550                                 if (real_target_type == TypeManager.int64_type)
1551                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_I8);
1552                                 if (real_target_type == TypeManager.char_type)
1553                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.U8_CH);
1554                         } else if (expr_type == TypeManager.char_type){
1555                                 //
1556                                 // From char to sbyte, byte, short
1557                                 //
1558                                 if (real_target_type == TypeManager.sbyte_type)
1559                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I1);
1560                                 if (real_target_type == TypeManager.byte_type)
1561                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_U1);
1562                                 if (real_target_type == TypeManager.short_type)
1563                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.CH_I2);
1564                         } else if (expr_type == TypeManager.float_type){
1565                                 //
1566                                 // From float to sbyte, byte, short,
1567                                 // ushort, int, uint, long, ulong, char
1568                                 // or decimal
1569                                 //
1570                                 if (real_target_type == TypeManager.sbyte_type)
1571                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I1);
1572                                 if (real_target_type == TypeManager.byte_type)
1573                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U1);
1574                                 if (real_target_type == TypeManager.short_type)
1575                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I2);
1576                                 if (real_target_type == TypeManager.ushort_type)
1577                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U2);
1578                                 if (real_target_type == TypeManager.int32_type)
1579                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I4);
1580                                 if (real_target_type == TypeManager.uint32_type)
1581                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U4);
1582                                 if (real_target_type == TypeManager.int64_type)
1583                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_I8);
1584                                 if (real_target_type == TypeManager.uint64_type)
1585                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_U8);
1586                                 if (real_target_type == TypeManager.char_type)
1587                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R4_CH);
1588                         } else if (expr_type == TypeManager.double_type){
1589                                 //
1590                                 // From double to byte, byte, short,
1591                                 // ushort, int, uint, long, ulong,
1592                                 // char, float or decimal
1593                                 //
1594                                 if (real_target_type == TypeManager.sbyte_type)
1595                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I1);
1596                                 if (real_target_type == TypeManager.byte_type)
1597                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1598                                 if (real_target_type == TypeManager.short_type)
1599                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1600                                 if (real_target_type == TypeManager.ushort_type)
1601                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U2);
1602                                 if (real_target_type == TypeManager.int32_type)
1603                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1604                                 if (real_target_type == TypeManager.uint32_type)
1605                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U4);
1606                                 if (real_target_type == TypeManager.int64_type)
1607                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1608                                 if (real_target_type == TypeManager.uint64_type)
1609                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_U8);
1610                                 if (real_target_type == TypeManager.char_type)
1611                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_CH);
1612                                 if (real_target_type == TypeManager.float_type)
1613                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1614                         } 
1615
1616                         // decimal is taken care of by the op_Explicit methods.
1617
1618                         return null;
1619                 }
1620
1621                 /// <summary>
1622                 ///  Returns whether an explicit reference conversion can be performed
1623                 ///  from source_type to target_type
1624                 /// </summary>
1625                 public static bool ExplicitReferenceConversionExists (Type source_type, Type target_type)
1626                 {
1627                         bool target_is_type_param = target_type.IsGenericParameter;
1628                         bool target_is_value_type = target_type.IsValueType;
1629                         
1630                         if (source_type == target_type)
1631                                 return true;
1632
1633                         //
1634                         // From object to a generic parameter
1635                         //
1636                         if (source_type == TypeManager.object_type && target_is_type_param)
1637                                 return true;
1638
1639                         //
1640                         // From object to any reference type
1641                         //
1642                         if (source_type == TypeManager.object_type && !target_is_value_type)
1643                                 return true;
1644                                         
1645                         //
1646                         // From any class S to any class-type T, provided S is a base class of T
1647                         //
1648                         if (TypeManager.IsSubclassOf (target_type, source_type))
1649                                 return true;
1650
1651                         //
1652                         // From any interface type S to any interface T provided S is not derived from T
1653                         //
1654                         if (source_type.IsInterface && target_type.IsInterface){
1655                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1656                                         return true;
1657                         }
1658                             
1659                         //
1660                         // From any class type S to any interface T, provided S is not sealed
1661                         // and provided S does not implement T.
1662                         //
1663                         if (target_type.IsInterface && !source_type.IsSealed &&
1664                             !TypeManager.ImplementsInterface (source_type, target_type))
1665                                 return true;
1666
1667                         //
1668                         // From any interface-type S to to any class type T, provided T is not
1669                         // sealed, or provided T implements S.
1670                         //
1671                         if (source_type.IsInterface &&
1672                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1673                                 return true;
1674                         
1675                         
1676                         // From an array type S with an element type Se to an array type T with an 
1677                         // element type Te provided all the following are true:
1678                         //     * S and T differe only in element type, in other words, S and T
1679                         //       have the same number of dimensions.
1680                         //     * Both Se and Te are reference types
1681                         //     * An explicit referenc conversions exist from Se to Te
1682                         //
1683                         if (source_type.IsArray && target_type.IsArray) {
1684                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1685                                         
1686                                         Type source_element_type = TypeManager.GetElementType (source_type);
1687                                         Type target_element_type = TypeManager.GetElementType (target_type);
1688                                         
1689                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1690                                                 if (ExplicitReferenceConversionExists (source_element_type,
1691                                                                                        target_element_type))
1692                                                         return true;
1693                                 }
1694                         }
1695                         
1696
1697                         // From System.Array to any array-type
1698                         if (source_type == TypeManager.array_type &&
1699                             target_type.IsArray){
1700                                 return true;
1701                         }
1702
1703                         //
1704                         // From System delegate to any delegate-type
1705                         //
1706                         if (source_type == TypeManager.delegate_type &&
1707                             TypeManager.IsDelegateType (target_type))
1708                                 return true;
1709
1710                         //
1711                         // From ICloneable to Array or Delegate types
1712                         //
1713                         if (source_type == TypeManager.icloneable_type &&
1714                             (target_type == TypeManager.array_type ||
1715                              target_type == TypeManager.delegate_type))
1716                                 return true;
1717                         
1718                         return false;
1719                 }
1720
1721                 /// <summary>
1722                 ///   Implements Explicit Reference conversions
1723                 /// </summary>
1724                 static Expression ExplicitReferenceConversion (Expression source, Type target_type)
1725                 {
1726                         Type source_type = source.Type;
1727                         bool target_is_type_param = target_type.IsGenericParameter;
1728                         bool target_is_value_type = target_type.IsValueType;
1729
1730                         //
1731                         // From object to a generic parameter
1732                         //
1733                         if (source_type == TypeManager.object_type && target_is_type_param)
1734                                 return new UnboxCast (source, target_type);
1735
1736                         //
1737                         // From object to any reference type
1738                         //
1739                         if (source_type == TypeManager.object_type && !target_is_value_type)
1740                                 return new ClassCast (source, target_type);
1741
1742                         //
1743                         // Unboxing conversion.
1744                         //
1745                         if (((source_type == TypeManager.enum_type &&
1746                                 !(source is EmptyCast)) ||
1747                                 source_type == TypeManager.value_type) && target_is_value_type)
1748                                 return new UnboxCast (source, target_type);
1749
1750                         //
1751                         // From any class S to any class-type T, provided S is a base class of T
1752                         //
1753                         if (TypeManager.IsSubclassOf (target_type, source_type))
1754                                 return new ClassCast (source, target_type);
1755
1756                         //
1757                         // From any interface type S to any interface T provided S is not derived from T
1758                         //
1759                         if (source_type.IsInterface && target_type.IsInterface){
1760                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1761                                         return null;
1762                                 else
1763                                         return new ClassCast (source, target_type);
1764                         }
1765
1766                         //
1767                         // From any class type S to any interface T, provides S is not sealed
1768                         // and provided S does not implement T.
1769                         //
1770                         if (target_type.IsInterface && !source_type.IsSealed) {
1771                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1772                                         return null;
1773                                 else
1774                                         return new ClassCast (source, target_type);
1775                                 
1776                         }
1777
1778                         //
1779                         // From any interface-type S to to any class type T, provided T is not
1780                         // sealed, or provided T implements S.
1781                         //
1782                         if (source_type.IsInterface) {
1783                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1784                                         if (target_type.IsClass)
1785                                                 return new ClassCast (source, target_type);
1786                                         else
1787                                                 return new UnboxCast (source, target_type);
1788                                 }
1789
1790                                 return null;
1791                         }
1792                         
1793                         // From an array type S with an element type Se to an array type T with an 
1794                         // element type Te provided all the following are true:
1795                         //     * S and T differe only in element type, in other words, S and T
1796                         //       have the same number of dimensions.
1797                         //     * Both Se and Te are reference types
1798                         //     * An explicit referenc conversions exist from Se to Te
1799                         //
1800                         if (source_type.IsArray && target_type.IsArray) {
1801                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1802                                         
1803                                         Type source_element_type = TypeManager.GetElementType (source_type);
1804                                         Type target_element_type = TypeManager.GetElementType (target_type);
1805                                         
1806                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1807                                                 if (ExplicitReferenceConversionExists (source_element_type,
1808                                                                                        target_element_type))
1809                                                         return new ClassCast (source, target_type);
1810                                 }
1811                         }
1812                         
1813
1814                         // From System.Array to any array-type
1815                         if (source_type == TypeManager.array_type &&
1816                             target_type.IsArray) {
1817                                 return new ClassCast (source, target_type);
1818                         }
1819
1820                         //
1821                         // From System delegate to any delegate-type
1822                         //
1823                         if (source_type == TypeManager.delegate_type &&
1824                             TypeManager.IsDelegateType (target_type))
1825                                 return new ClassCast (source, target_type);
1826
1827                         //
1828                         // From ICloneable to Array or Delegate types
1829                         //
1830                         if (source_type == TypeManager.icloneable_type &&
1831                             (target_type == TypeManager.array_type ||
1832                              target_type == TypeManager.delegate_type))
1833                                 return new ClassCast (source, target_type);
1834                         
1835                         return null;
1836                 }
1837                 
1838                 /// <summary>
1839                 ///   Performs an explicit conversion of the expression `expr' whose
1840                 ///   type is expr.Type to `target_type'.
1841                 /// </summary>
1842                 static public Expression ExplicitConversion (EmitContext ec, Expression expr,
1843                                                              Type target_type, Location loc)
1844                 {
1845                         Type expr_type = expr.Type;
1846                         Type original_expr_type = expr_type;
1847
1848                         if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1849                                 if (target_type == TypeManager.enum_type ||
1850                                     target_type == TypeManager.object_type) {
1851                                         if (expr is EnumConstant)
1852                                                 expr = ((EnumConstant) expr).Child;
1853                                         // We really need all these casts here .... :-(
1854                                         expr = new BoxedCast (new EmptyCast (expr, expr_type));
1855                                         return new EmptyCast (expr, target_type);
1856                                 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1857                                            target_type.IsSubclassOf (TypeManager.enum_type))
1858                                         return new UnboxCast (expr, target_type);
1859
1860                                 //
1861                                 // Notice that we have kept the expr_type unmodified, which is only
1862                                 // used later on to 
1863                                 if (expr is EnumConstant)
1864                                         expr = ((EnumConstant) expr).Child;
1865                                 else
1866                                         expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1867                                 expr_type = expr.Type;
1868                         }
1869
1870                         int errors = Report.Errors;
1871                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc);
1872                         if (Report.Errors > errors)
1873                                 return null;
1874
1875                         if (ne != null)
1876                                 return ne;
1877
1878                         ne = ExplicitNumericConversion (ec, expr, target_type, loc);
1879                         if (ne != null)
1880                                 return ne;
1881
1882                         //
1883                         // Unboxing conversion.
1884                         //
1885                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1886                                 return new UnboxCast (expr, target_type);
1887
1888                         //
1889                         // Skip the ExplicitReferenceConversion because we can not convert
1890                         // from Null to a ValueType, and ExplicitReference wont check against
1891                         // null literal explicitly
1892                         //
1893                         if (expr_type != TypeManager.null_type){
1894                                 ne = ExplicitReferenceConversion (expr, target_type);
1895                                 if (ne != null)
1896                                         return ne;
1897                         }
1898
1899                 skip_explicit:
1900                         if (ec.InUnsafe){
1901                                 if (target_type.IsPointer){
1902                                         if (expr_type.IsPointer)
1903                                                 return new EmptyCast (expr, target_type);
1904                                         
1905                                         if (expr_type == TypeManager.sbyte_type ||
1906                                             expr_type == TypeManager.byte_type ||
1907                                             expr_type == TypeManager.short_type ||
1908                                             expr_type == TypeManager.ushort_type ||
1909                                             expr_type == TypeManager.int32_type ||
1910                                             expr_type == TypeManager.uint32_type ||
1911                                             expr_type == TypeManager.uint64_type ||
1912                                             expr_type == TypeManager.int64_type)
1913                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1914                                 }
1915                                 if (expr_type.IsPointer){
1916                                         Expression e = null;
1917                                         
1918                                         if (target_type == TypeManager.sbyte_type)
1919                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1920                                         else if (target_type == TypeManager.byte_type)
1921                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1922                                         else if (target_type == TypeManager.short_type)
1923                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1924                                         else if (target_type == TypeManager.ushort_type)
1925                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1926                                         else if (target_type == TypeManager.int32_type)
1927                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1928                                         else if (target_type == TypeManager.uint32_type)
1929                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1930                                         else if (target_type == TypeManager.uint64_type)
1931                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1932                                         else if (target_type == TypeManager.int64_type){
1933                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
1934                                         }
1935
1936                                         if (e != null){
1937                                                 Expression ci, ce;
1938
1939                                                 ci = ImplicitConversionStandard (ec, e, target_type, loc);
1940
1941                                                 if (ci != null)
1942                                                         return ci;
1943
1944                                                 ce = ExplicitNumericConversion (ec, e, target_type, loc);
1945                                                 if (ce != null)
1946                                                         return ce;
1947                                                 //
1948                                                 // We should always be able to go from an uint32
1949                                                 // implicitly or explicitly to the other integral
1950                                                 // types
1951                                                 //
1952                                                 throw new Exception ("Internal compiler error");
1953                                         }
1954                                 }
1955                         }
1956                         
1957                         ne = ExplicitUserConversion (ec, expr, target_type, loc);
1958                         if (ne != null)
1959                                 return ne;
1960
1961                         if (expr is NullLiteral){
1962                                 Report.Error (37, loc, "Cannot convert null to value type `" +
1963                                               TypeManager.CSharpName (target_type) + "'");
1964                                 return null;
1965                         }
1966                                 
1967                         Error_CannotConvertType (loc, original_expr_type, target_type);
1968                         return null;
1969                 }
1970
1971                 /// <summary>
1972                 ///   Same as ExplicitConversion, only it doesn't include user defined conversions
1973                 /// </summary>
1974                 static public Expression ExplicitConversionStandard (EmitContext ec, Expression expr,
1975                                                                      Type target_type, Location l)
1976                 {
1977                         int errors = Report.Errors;
1978                         Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
1979                         if (Report.Errors > errors)
1980                                 return null;
1981
1982                         if (ne != null)
1983                                 return ne;
1984
1985                         ne = ExplicitNumericConversion (ec, expr, target_type, l);
1986                         if (ne != null)
1987                                 return ne;
1988
1989                         ne = ExplicitReferenceConversion (expr, target_type);
1990                         if (ne != null)
1991                                 return ne;
1992
1993                         Error_CannotConvertType (l, expr.Type, target_type);
1994                         return null;
1995                 }
1996         }
1997 }