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