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