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