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