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