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