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