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