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