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