Remove TypeManager.LookupType and TypeManager.LookupTypeDirect.
[mono.git] / mcs / bmcs / 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                 //
23                 // This is used to prettify the code: a null argument is allowed
24                 // for ImplicitStandardConversion as long as it is known that
25                 // no anonymous method will play a role.
26                 //
27                 // FIXME: renamed from `const' to `static' to allow bootstraping from older
28                 // versions of the compiler that could not cope with this construct.
29                 //
30                 public static EmitContext ConstantEC = null;
31                 
32                 static public void Error_CannotConvertType (Location loc, Type source, Type target)
33                 {
34                         Report.Error (30, loc, "Cannot convert type '" +
35                                       TypeManager.CSharpName (source) + "' to '" +
36                                       TypeManager.CSharpName (target) + "'");
37                 }
38
39                 static Expression TypeParameter_to_Null (Expression expr, Type target_type,
40                                                          Location loc)
41                 {
42                         if (!TypeParameter_to_Null (target_type)) {
43                                 Report.Error (403, loc, "Cannot convert null to the type " +
44                                               "parameter `{0}' becaues it could be a value " +
45                                               "type.  Consider using `default ({0})' instead.",
46                                               target_type);
47                                 return null;
48                         }
49
50                         return new NullCast (expr, target_type);
51                 }
52
53                 static bool TypeParameter_to_Null (Type target_type)
54                 {
55                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (target_type);
56                         if (gc == null)
57                                 return false;
58
59                         if (gc.HasReferenceTypeConstraint)
60                                 return true;
61                         if (gc.HasClassConstraint && !TypeManager.IsValueType (gc.ClassConstraint))
62                                 return true;
63
64                         return false;
65                 }
66
67                 static Type TypeParam_EffectiveBaseType (EmitContext ec, Type t)
68                 {
69                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (t);
70                         if (gc == null)
71                                 return TypeManager.object_type;
72
73                         return TypeParam_EffectiveBaseType (ec, gc);
74                 }
75
76                 static Type TypeParam_EffectiveBaseType (EmitContext ec, GenericConstraints gc)
77                 {
78                         ArrayList list = new ArrayList ();
79                         list.Add (gc.EffectiveBaseClass);
80                         foreach (Type t in gc.InterfaceConstraints) {
81                                 if (!t.IsGenericParameter)
82                                         continue;
83
84                                 GenericConstraints new_gc = TypeManager.GetTypeParameterConstraints (t);
85                                 if (new_gc != null)
86                                         list.Add (TypeParam_EffectiveBaseType (ec, new_gc));
87                         }
88                         return FindMostEncompassedType (ec, list);
89                 }
90
91                 static Expression TypeParameterConversion (Expression expr, bool is_reference, Type target_type)
92                 {
93                         if (is_reference)
94                                 return new EmptyCast (expr, target_type);
95                         else
96                                 return new BoxedCast (expr, target_type);
97                 }
98
99                 static Expression ImplicitTypeParameterConversion (EmitContext ec, Expression expr,
100                                                                    Type target_type)
101                 {
102                         Type expr_type = expr.Type;
103
104                         GenericConstraints gc = TypeManager.GetTypeParameterConstraints (expr_type);
105
106                         if (gc == null) {
107                                 if (target_type == TypeManager.object_type)
108                                         return new BoxedCast (expr);
109
110                                 return null;
111                         }
112
113                         // We're converting from a type parameter which is known to be a reference type.
114                         bool is_reference = gc.IsReferenceType;
115                         Type base_type = TypeParam_EffectiveBaseType (ec, gc);
116
117                         if (TypeManager.IsSubclassOf (base_type, target_type))
118                                 return TypeParameterConversion (expr, is_reference, target_type);
119
120                         if (target_type.IsInterface) {
121                                 if (TypeManager.ImplementsInterface (base_type, target_type))
122                                         return TypeParameterConversion (expr, is_reference, target_type);
123
124                                 foreach (Type t in gc.InterfaceConstraints) {
125                                         if (TypeManager.IsSubclassOf (t, target_type))
126                                                 return TypeParameterConversion (expr, is_reference, target_type);
127                                 }
128                         }
129
130                         foreach (Type t in gc.InterfaceConstraints) {
131                                 if (!t.IsGenericParameter)
132                                         continue;
133                                 if (TypeManager.IsSubclassOf (t, target_type))
134                                         return TypeParameterConversion (expr, is_reference, target_type);
135                         }
136
137                         return null;
138                 }
139
140                 static EmptyExpression MyEmptyExpr;
141                 static public Expression WideningReferenceConversion (EmitContext ec, Expression expr, Type target_type)
142                 {
143                         Type expr_type = expr.Type;
144
145                         if (expr_type == null && expr.eclass == ExprClass.MethodGroup){
146                                 // if we are a method group, emit a warning
147
148                                 expr.Emit (null);
149                         }
150
151                         if (expr_type == TypeManager.void_type)
152                                 return null;
153
154                         if (expr_type.IsGenericParameter)
155                                 return ImplicitTypeParameterConversion (ec, expr, target_type);
156                                 
157                         //
158                         // notice that it is possible to write "ValueType v = 1", the ValueType here
159                         // is an abstract class, and not really a value type, so we apply the same rules.
160                         //
161                         if (target_type == TypeManager.object_type) {
162                                 //
163                                 // A pointer type cannot be converted to object
164                                 // 
165                                 if (expr_type.IsPointer)
166                                         return null;
167
168                                 if (TypeManager.IsValueType (expr_type))
169                                         return new BoxedCast (expr);
170                                 if (expr_type.IsClass || expr_type.IsInterface || expr_type == TypeManager.enum_type){
171                                         if (expr_type == TypeManager.anonymous_method_type)
172                                                 return null;
173                                         return new EmptyCast (expr, target_type);
174                                 }
175
176                                 return null;
177                         } else if (target_type == TypeManager.value_type) {
178                                 if (TypeManager.IsValueType (expr_type))
179                                         return new BoxedCast (expr);
180                                 if (expr_type == TypeManager.null_type)
181                                         return new NullCast (expr, target_type);
182
183                                 return null;
184                         } else if (TypeManager.IsSubclassOf (expr_type, target_type)) {
185                                 //
186                                 // Special case: enumeration to System.Enum.
187                                 // System.Enum is not a value type, it is a class, so we need
188                                 // a boxing conversion
189                                 //
190                                 if (expr_type.IsEnum || expr_type.IsGenericParameter)
191                                         return new BoxedCast (expr);
192
193                                 return new EmptyCast (expr, target_type);
194                         }
195
196                         // This code is kind of mirrored inside WideningStandardConversionExists
197                         // with the small distinction that we only probe there
198                         //
199                         // Always ensure that the code here and there is in sync
200
201                         // from the null type to any reference-type.
202                         if (expr_type == TypeManager.null_type){
203                                 if (target_type.IsPointer)
204                                         return new EmptyCast (expr, target_type);
205                                         
206                                 if (!target_type.IsValueType)
207                                         return new NullCast (expr, target_type);
208                         }
209
210                         // from any class-type S to any interface-type T.
211                         if (target_type.IsInterface) {
212                                 if (target_type != TypeManager.iconvertible_type &&
213                                     expr_type.IsValueType && (expr is Constant) &&
214                                     !(expr is IntLiteral || expr is BoolLiteral ||
215                                       expr is FloatLiteral || expr is DoubleLiteral ||
216                                       expr is LongLiteral || expr is CharLiteral ||
217                                       expr is StringLiteral || expr is DecimalLiteral ||
218                                       expr is UIntLiteral || expr is ULongLiteral)) {
219                                         return null;
220                                 }
221
222                                 if (TypeManager.ImplementsInterface (expr_type, target_type)){
223                                         if (expr_type.IsGenericParameter || TypeManager.IsValueType (expr_type))
224                                                 return new BoxedCast (expr, target_type);
225                                         else
226                                                 return new EmptyCast (expr, target_type);
227                                 }
228                         }
229
230                         // from any interface type S to interface-type T.
231                         if (expr_type.IsInterface && target_type.IsInterface) {
232                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
233                                         return new EmptyCast (expr, target_type);
234                                 else
235                                         return null;
236                         }
237                                 
238                         // from an array-type S to an array-type of type T
239                         if (expr_type.IsArray && target_type.IsArray) {
240                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
241
242                                         Type expr_element_type = TypeManager.GetElementType (expr_type);
243
244                                         if (MyEmptyExpr == null)
245                                                 MyEmptyExpr = new EmptyExpression ();
246                                                 
247                                         MyEmptyExpr.SetType (expr_element_type);
248                                         Type target_element_type = TypeManager.GetElementType (target_type);
249
250                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
251                                                 if (WideningStandardConversionExists (ConstantEC, MyEmptyExpr,
252                                                                                       target_element_type))
253                                                         return new EmptyCast (expr, target_type);
254                                 }
255                         }
256                                 
257                         // from an array-type to System.Array
258                         if (expr_type.IsArray && target_type == TypeManager.array_type)
259                                 return new EmptyCast (expr, target_type);
260                                 
261                         // from any delegate type to System.Delegate
262                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
263                             target_type == TypeManager.delegate_type)
264                                 return new EmptyCast (expr, target_type);
265                                         
266                         // from any array-type or delegate type into System.ICloneable.
267                         if (expr_type.IsArray ||
268                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
269                                 if (target_type == TypeManager.icloneable_type)
270                                         return new EmptyCast (expr, target_type);
271
272                         // from a generic type definition to a generic instance.
273                         if (TypeManager.IsEqual (expr_type, target_type))
274                                 return new EmptyCast (expr, target_type);
275
276                         return null;
277                 }
278
279                 //
280                 // Tests whether an implicit reference conversion exists between expr_type
281                 // and target_type
282                 //
283                 public static bool WideningReferenceConversionExists (EmitContext ec, Expression expr, Type target_type)
284                 {
285                         Type expr_type = expr.Type;
286
287                         if (expr_type.IsGenericParameter)
288                                 return ImplicitTypeParameterConversion (ec, expr, target_type) != null;
289
290                         //
291                         // This is the boxed case.
292                         //
293                         if (target_type == TypeManager.object_type) {
294                                 if (expr_type.IsClass || TypeManager.IsValueType (expr_type) ||
295                                     expr_type.IsInterface || expr_type == TypeManager.enum_type)
296                                         if (target_type != TypeManager.anonymous_method_type)
297                                         return true;
298
299                                 return false;
300                         } else if (TypeManager.IsSubclassOf (expr_type, target_type))
301                                 return true;
302
303                         // Please remember that all code below actually comes
304                         // from WideningReferenceConversion so make sure code remains in sync
305                                 
306                         // from any class-type S to any interface-type T.
307                         if (target_type.IsInterface) {
308                                 if (target_type != TypeManager.iconvertible_type &&
309                                     expr_type.IsValueType && (expr is Constant) &&
310                                     !(expr is IntLiteral || expr is BoolLiteral ||
311                                       expr is FloatLiteral || expr is DoubleLiteral ||
312                                       expr is LongLiteral || expr is CharLiteral ||
313                                       expr is StringLiteral || expr is DecimalLiteral ||
314                                       expr is UIntLiteral || expr is ULongLiteral)) {
315                                         return false;
316                                 }
317                                 
318                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
319                                         return true;
320                         }
321                                 
322                         // from any interface type S to interface-type T.
323                         if (expr_type.IsInterface && target_type.IsInterface)
324                                 if (TypeManager.ImplementsInterface (expr_type, target_type))
325                                         return true;
326                                 
327                         // from an array-type S to an array-type of type T
328                         if (expr_type.IsArray && target_type.IsArray) {
329                                 if (expr_type.GetArrayRank () == target_type.GetArrayRank ()) {
330                                                 
331                                         Type expr_element_type = expr_type.GetElementType ();
332
333                                         if (MyEmptyExpr == null)
334                                                 MyEmptyExpr = new EmptyExpression ();
335                                                 
336                                         MyEmptyExpr.SetType (expr_element_type);
337                                         Type target_element_type = TypeManager.GetElementType (target_type);
338                                                 
339                                         if (!expr_element_type.IsValueType && !target_element_type.IsValueType)
340                                                 if (WideningStandardConversionExists (ConstantEC, MyEmptyExpr,
341                                                                                       target_element_type))
342                                                         return true;
343                                 }
344                         }
345                                 
346                         // from an array-type to System.Array
347                         if (expr_type.IsArray && (target_type == TypeManager.array_type))
348                                 return true;
349                                 
350                         // from any delegate type to System.Delegate
351                         if ((expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type)) &&
352                             target_type == TypeManager.delegate_type)
353                                 if (target_type.IsAssignableFrom (expr_type))
354                                         return true;
355                                         
356                         // from any array-type or delegate type into System.ICloneable.
357                         if (expr_type.IsArray ||
358                             expr_type == TypeManager.delegate_type || TypeManager.IsDelegateType (expr_type))
359                                 if (target_type == TypeManager.icloneable_type)
360                                         return true;
361                                 
362                         // from the null type to any reference-type.
363                         if (expr_type == TypeManager.null_type){
364                                 if (target_type.IsPointer)
365                                         return true;
366                         
367                                 if (!target_type.IsValueType)
368                                         return true;
369                         }
370
371                         // from a generic type definition to a generic instance.
372                         if (TypeManager.IsEqual (expr_type, target_type))
373                                 return true;
374
375                         return false;
376                 }
377
378                 /// <summary>
379                 ///   Implicit Numeric Conversions.
380                 ///
381                 ///   expr is the expression to convert, returns a new expression of type
382                 ///   target_type or null if an implicit conversion is not possible.
383                 /// </summary>
384
385                 static public Expression WideningNumericConversion (EmitContext ec, Expression expr,
386                                                                     Type target_type, Location loc)
387                 {
388                         Type expr_type = expr.Type;
389
390                         //
391                         // Attempt to do the implicit constant expression conversions
392
393                         if (expr is Constant){
394                                 if (expr is IntConstant){
395                                         Expression e;
396                                         
397                                         e = TryWideningIntConversion (target_type, (IntConstant) expr);
398                                         
399                                         if (e != null)
400                                                 return e;
401                                 } else if (expr is LongConstant && target_type == TypeManager.uint64_type){
402                                         //
403                                         // Try the implicit constant expression conversion
404                                         // from long to ulong, instead of a nice routine,
405                                         // we just inline it
406                                         //
407                                         long v = ((LongConstant) expr).Value;
408                                         if (v >= 0)
409                                                 return new ULongConstant ((ulong) v);
410                                 } 
411                         }
412                         
413                         Type real_target_type = target_type;
414
415                          if (expr_type == TypeManager.byte_type){
416                                 //
417                                 // From byte to short, ushort, int, uint, long, ulong, float, double
418                                 // 
419                                 if ((real_target_type == TypeManager.short_type) ||
420                                     (real_target_type == TypeManager.int32_type))
421                                         return new EmptyCast (expr, target_type);
422
423                                 if (real_target_type == TypeManager.int64_type)
424                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
425                                 if (real_target_type == TypeManager.float_type)
426                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
427                                 if (real_target_type == TypeManager.double_type)
428                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
429                                 if (real_target_type == TypeManager.decimal_type)
430                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);
431                         } else if (expr_type == TypeManager.short_type){
432                                 //
433                                 // From short to int, long, float, double
434                                 // 
435                                 if (real_target_type == TypeManager.int32_type)
436                                         return new EmptyCast (expr, target_type);
437                                 if (real_target_type == TypeManager.int64_type)
438                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
439                                 if (real_target_type == TypeManager.double_type)
440                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
441                                 if (real_target_type == TypeManager.float_type)
442                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
443                                 if (real_target_type == TypeManager.decimal_type)
444                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);
445                         } else if (expr_type == TypeManager.int32_type){
446                                 //
447                                 // From int to long, float, double
448                                 //
449                                 if (real_target_type == TypeManager.int64_type)
450                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
451                                 if (real_target_type == TypeManager.double_type)
452                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
453                                 if (real_target_type == TypeManager.float_type)
454                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
455                                 if (real_target_type == TypeManager.decimal_type)
456                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);
457                         } else if (expr_type == TypeManager.int64_type){
458                                 //
459                                 // From long/ulong to float, double
460                                 //
461                                 if (real_target_type == TypeManager.double_type)
462                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
463                                 if (real_target_type == TypeManager.float_type)
464                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);     
465                                 if (real_target_type == TypeManager.decimal_type)
466                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);    
467                         } else if (expr_type == TypeManager.decimal_type){
468                                 //
469                                 // From decimal to float, double
470                                 //
471                                 if (real_target_type == TypeManager.double_type)
472                                         return new ImplicitInvocation (ec, "System", "Convert", "ToDouble", loc, expr); 
473                                 if (real_target_type == TypeManager.float_type)
474                                         return new ImplicitInvocation (ec, "System", "Convert" ,"ToSingle", loc, expr); 
475                         } else if (expr_type == TypeManager.float_type){
476                                 //
477                                 // float to double
478                                 //
479                                 if (real_target_type == TypeManager.double_type)
480                                         return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
481                         }
482
483                         return null;
484                 }
485
486
487                 /// <summary>
488                 ///  Same as WideningStandardConversionExists except that it also looks at
489                 ///  implicit user defined conversions - needed for overload resolution
490                 /// </summary>
491                 public static bool WideningConversionExists (EmitContext ec, Expression expr, Type target_type)
492                 {
493                         if (expr is NullLiteral) {
494                                 if (target_type.IsGenericParameter)
495                                         return TypeParameter_to_Null (target_type);
496
497                                 if (TypeManager.IsNullableType (target_type))
498                                         return true;
499                         }
500
501                         if (WideningStandardConversionExists (ec, expr, target_type))
502                                 return true;
503
504                         //
505                         // VB.NET has no notion of User defined conversions
506                         //
507
508 //                      Expression dummy = ImplicitUserConversion (ec, expr, target_type, Location.Null);
509
510 //                      if (dummy != null)
511 //                              return true;
512
513                         return false;
514                 }
515
516                 //
517                 // VB.NET has no notion of User defined conversions
518                 //
519
520 //              public static bool ImplicitUserConversionExists (EmitContext ec, Type source, Type target)
521 //              {
522 //                      Expression dummy = ImplicitUserConversion (
523 //                              ec, new EmptyExpression (source), target, Location.Null);
524 //                      return dummy != null;
525 //              }
526
527                 /// <summary>
528                 ///  Determines if a standard implicit conversion exists from
529                 ///  expr_type to target_type
530                 ///
531                 ///  ec should point to a real EmitContext if expr.Type is TypeManager.anonymous_method_type.
532                 /// </summary>
533                 public static bool WideningStandardConversionExists (EmitContext ec, Expression expr, Type target_type)
534                 {
535                         Type expr_type = expr.Type;
536
537                         if (expr_type == TypeManager.void_type)
538                                 return false;
539
540                         //Console.WriteLine ("Expr is {0}", expr);
541                         //Console.WriteLine ("{0} -> {1} ?", expr_type, target_type);
542                         if (expr_type.Equals (target_type))
543                                 return true;
544
545
546                         // First numeric conversions 
547
548                         if (expr_type == TypeManager.sbyte_type){
549                                 //
550                                 // From sbyte to short, int, long, float, double.
551                                 //
552                                 if ((target_type == TypeManager.int32_type) || 
553                                     (target_type == TypeManager.int64_type) ||
554                                     (target_type == TypeManager.double_type) ||
555                                     (target_type == TypeManager.float_type)  ||
556                                     (target_type == TypeManager.short_type) ||
557                                     (target_type == TypeManager.decimal_type))
558                                         return true;
559                                 
560                         } else if (expr_type == TypeManager.byte_type){
561                                 //
562                                 // From byte to short, ushort, int, uint, long, ulong, float, double
563                                 // 
564                                 if ((target_type == TypeManager.short_type) ||
565                                     (target_type == TypeManager.ushort_type) ||
566                                     (target_type == TypeManager.int32_type) ||
567                                     (target_type == TypeManager.uint32_type) ||
568                                     (target_type == TypeManager.uint64_type) ||
569                                     (target_type == TypeManager.int64_type) ||
570                                     (target_type == TypeManager.float_type) ||
571                                     (target_type == TypeManager.double_type) ||
572                                     (target_type == TypeManager.decimal_type))
573                                         return true;
574         
575                         } else if (expr_type == TypeManager.short_type){
576                                 //
577                                 // From short to int, long, float, double
578                                 // 
579                                 if ((target_type == TypeManager.int32_type) ||
580                                     (target_type == TypeManager.int64_type) ||
581                                     (target_type == TypeManager.double_type) ||
582                                     (target_type == TypeManager.float_type) ||
583                                     (target_type == TypeManager.decimal_type))
584                                         return true;
585                                         
586                         } else if (expr_type == TypeManager.ushort_type){
587                                 //
588                                 // From ushort to int, uint, long, ulong, float, double
589                                 //
590                                 if ((target_type == TypeManager.uint32_type) ||
591                                     (target_type == TypeManager.uint64_type) ||
592                                     (target_type == TypeManager.int32_type) ||
593                                     (target_type == TypeManager.int64_type) ||
594                                     (target_type == TypeManager.double_type) ||
595                                     (target_type == TypeManager.float_type) ||
596                                     (target_type == TypeManager.decimal_type))
597                                         return true;
598                                     
599                         } else if (expr_type == TypeManager.int32_type){
600                                 //
601                                 // From int to long, float, double
602                                 //
603                                 if ((target_type == TypeManager.int64_type) ||
604                                     (target_type == TypeManager.double_type) ||
605                                     (target_type == TypeManager.float_type) ||
606                                     (target_type == TypeManager.decimal_type))
607                                         return true;
608                                         
609                         } else if (expr_type == TypeManager.uint32_type){
610                                 //
611                                 // From uint to long, ulong, float, double
612                                 //
613                                 if ((target_type == TypeManager.int64_type) ||
614                                     (target_type == TypeManager.uint64_type) ||
615                                     (target_type == TypeManager.double_type) ||
616                                     (target_type == TypeManager.float_type) ||
617                                     (target_type == TypeManager.decimal_type))
618                                         return true;
619                                         
620                         } else if ((expr_type == TypeManager.uint64_type) ||
621                                    (expr_type == TypeManager.int64_type)) {
622                                 //
623                                 // From long/ulong to float, double
624                                 //
625                                 if ((target_type == TypeManager.double_type) ||
626                                     (target_type == TypeManager.float_type) ||
627                                     (target_type == TypeManager.decimal_type))
628                                         return true;
629                                     
630                         } else if (expr_type == TypeManager.char_type){
631                                 //
632                                 // From char to ushort, int, uint, long, ulong, float, double
633                                 // 
634                                 if ((target_type == TypeManager.ushort_type) ||
635                                     (target_type == TypeManager.int32_type) ||
636                                     (target_type == TypeManager.uint32_type) ||
637                                     (target_type == TypeManager.uint64_type) ||
638                                     (target_type == TypeManager.int64_type) ||
639                                     (target_type == TypeManager.float_type) ||
640                                     (target_type == TypeManager.double_type) ||
641                                     (target_type == TypeManager.decimal_type))
642                                         return true;
643
644                         } else if (expr_type == TypeManager.float_type){
645                                 //
646                                 // float to double
647                                 //
648                                 if (target_type == TypeManager.double_type)
649                                         return true;
650                         }       
651                         
652                         if (expr.eclass == ExprClass.MethodGroup){
653                                 if (TypeManager.IsDelegateType (target_type) && RootContext.Version != LanguageVersion.ISO_1){
654                                         MethodGroupExpr mg = expr as MethodGroupExpr;
655                                         if (mg != null){
656                                                 //
657                                                 // This should not happen frequently, so we can create an object
658                                                 // to test compatibility
659                                                 //
660                                                 Expression c = ImplicitDelegateCreation.Create (ec, mg, target_type, Location.Null);
661                                                 return c != null;
662                                         }
663                                 }
664                         }
665                         
666                         if (WideningReferenceConversionExists (ec, expr, target_type))
667                                 return true;
668
669                         //
670                         // Implicit Constant Expression Conversions
671                         //
672                         if (expr is IntConstant){
673                                 int value = ((IntConstant) expr).Value;
674
675                                 if (target_type == TypeManager.sbyte_type){
676                                         if (value >= SByte.MinValue && value <= SByte.MaxValue)
677                                                 return true;
678                                 } else if (target_type == TypeManager.byte_type){
679                                         if (Byte.MinValue >= 0 && value <= Byte.MaxValue)
680                                                 return true;
681                                 } else if (target_type == TypeManager.short_type){
682                                         if (value >= Int16.MinValue && value <= Int16.MaxValue)
683                                                 return true;
684                                 } else if (target_type == TypeManager.ushort_type){
685                                         if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
686                                                 return true;
687                                 } else if (target_type == TypeManager.uint32_type){
688                                         if (value >= 0)
689                                                 return true;
690                                 } else if (target_type == TypeManager.uint64_type){
691                                          //
692                                          // we can optimize this case: a positive int32
693                                          // always fits on a uint64.  But we need an opcode
694                                          // to do it.
695                                          //
696                                         if (value >= 0)
697                                                 return true;
698                                 }
699                                 
700                                 if (value == 0 && expr is IntLiteral && TypeManager.IsEnumType (target_type))
701                                         return true;
702                         }
703
704                         if (expr is LongConstant && target_type == TypeManager.uint64_type){
705                                 //
706                                 // Try the implicit constant expression conversion
707                                 // from long to ulong, instead of a nice routine,
708                                 // we just inline it
709                                 //
710                                 long v = ((LongConstant) expr).Value;
711                                 if (v > 0)
712                                         return true;
713                         }
714                         
715                         if ((target_type == TypeManager.enum_type ||
716                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
717                              expr is IntLiteral){
718                                 IntLiteral i = (IntLiteral) expr;
719
720                                 if (i.Value == 0)
721                                         return true;
722                         }
723
724                         //
725                         // If `expr_type' implements `target_type' (which is an iface)
726                         // see TryWideningIntConversion
727                         // 
728                         if (target_type.IsInterface && target_type.IsAssignableFrom (expr_type))
729                                 return true;
730
731                         if (target_type == TypeManager.void_ptr_type && expr_type.IsPointer)
732                                 return true;
733
734                         if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
735                                 return true;
736
737                         if (expr_type == TypeManager.anonymous_method_type){
738                                 if (!TypeManager.IsDelegateType (target_type))
739                                         return false;
740
741                                 AnonymousMethod am = (AnonymousMethod) expr;
742
743                                 Expression conv = am.Compatible (ec, target_type, true);
744                                 if (conv != null)
745                                         return true;
746                         }
747
748                         return false;
749                 }
750
751                 //
752                 // Used internally by FindMostEncompassedType, this is used
753                 // to avoid creating lots of objects in the tight loop inside
754                 // FindMostEncompassedType
755                 //
756                 static EmptyExpression priv_fmet_param;
757                 
758                 /// <summary>
759                 ///  Finds "most encompassed type" according to the spec (13.4.2)
760                 ///  amongst the methods in the MethodGroupExpr
761                 /// </summary>
762                 static Type FindMostEncompassedType (EmitContext ec, ArrayList types)
763                 {
764                         Type best = null;
765
766                         if (priv_fmet_param == null)
767                                 priv_fmet_param = new EmptyExpression ();
768
769                         foreach (Type t in types){
770                                 priv_fmet_param.SetType (t);
771                                 
772                                 if (best == null) {
773                                         best = t;
774                                         continue;
775                                 }
776                                 
777                                 if (WideningStandardConversionExists (ec, priv_fmet_param, best))
778                                         best = t;
779                         }
780
781                         return best;
782                 }
783
784                 //
785                 // Used internally by FindMostEncompassingType, this is used
786                 // to avoid creating lots of objects in the tight loop inside
787                 // FindMostEncompassingType
788                 //
789                 static EmptyExpression priv_fmee_ret;
790                 
791                 /// <summary>
792                 ///  Finds "most encompassing type" according to the spec (13.4.2)
793                 ///  amongst the types in the given set
794                 /// </summary>
795                 static Type FindMostEncompassingType (EmitContext ec, ArrayList types)
796                 {
797                         Type best = null;
798
799                         if (priv_fmee_ret == null)
800                                 priv_fmee_ret = new EmptyExpression ();
801
802                         foreach (Type t in types){
803                                 priv_fmee_ret.SetType (best);
804
805                                 if (best == null) {
806                                         best = t;
807                                         continue;
808                                 }
809
810                                 if (WideningStandardConversionExists (ec, priv_fmee_ret, t))
811                                         best = t;
812                         }
813                         
814                         return best;
815                 }
816
817                 //
818                 // Used to avoid creating too many objects
819                 //
820                 static EmptyExpression priv_fms_expr;
821                 
822                 /// <summary>
823                 ///   Finds the most specific source Sx according to the rules of the spec (13.4.4)
824                 ///   by making use of FindMostEncomp* methods. Applies the correct rules separately
825                 ///   for explicit and implicit conversion operators.
826                 /// </summary>
827                 static public Type FindMostSpecificSource (EmitContext ec, MethodGroupExpr me,
828                                                            Expression source, bool apply_explicit_conv_rules,
829                                                            Location loc)
830                 {
831                         ArrayList src_types_set = new ArrayList ();
832                         
833                         if (priv_fms_expr == null)
834                                 priv_fms_expr = new EmptyExpression ();
835
836                         //
837                         // If any operator converts from S then Sx = S
838                         //
839                         Type source_type = source.Type;
840                         foreach (MethodBase mb in me.Methods){
841                                 ParameterData pd = Invocation.GetParameterData (mb);
842                                 Type param_type = pd.ParameterType (0);
843
844                                 if (param_type == source_type)
845                                         return param_type;
846
847                                 if (apply_explicit_conv_rules) {
848                                         //
849                                         // From the spec :
850                                         // Find the set of applicable user-defined conversion operators, U.  This set
851                                         // consists of the
852                                         // user-defined implicit or explicit conversion operators declared by
853                                         // the classes or structs in D that convert from a type encompassing
854                                         // or encompassed by S to a type encompassing or encompassed by T
855                                         //
856                                         priv_fms_expr.SetType (param_type);
857                                         if (WideningStandardConversionExists (ec, priv_fms_expr, source_type))
858                                                 src_types_set.Add (param_type);
859                                         else {
860                                                 if (WideningStandardConversionExists (ec, source, param_type))
861                                                         src_types_set.Add (param_type);
862                                         }
863                                 } else {
864                                         //
865                                         // Only if S is encompassed by param_type
866                                         //
867                                         if (WideningStandardConversionExists (ec, source, param_type))
868                                                 src_types_set.Add (param_type);
869                                 }
870                         }
871                         
872                         //
873                         // Explicit Conv rules
874                         //
875                         if (apply_explicit_conv_rules) {
876                                 ArrayList candidate_set = new ArrayList ();
877
878                                 foreach (Type param_type in src_types_set){
879                                         if (WideningStandardConversionExists (ec, source, param_type))
880                                                 candidate_set.Add (param_type);
881                                 }
882
883                                 if (candidate_set.Count != 0)
884                                         return FindMostEncompassedType (ec, candidate_set);
885                         }
886
887                         //
888                         // Final case
889                         //
890                         if (apply_explicit_conv_rules)
891                                 return FindMostEncompassingType (ec, src_types_set);
892                         else
893                                 return FindMostEncompassedType (ec, src_types_set);
894                 }
895
896                 //
897                 // Useful in avoiding proliferation of objects
898                 //
899                 static EmptyExpression priv_fmt_expr;
900                 
901                 /// <summary>
902                 ///  Finds the most specific target Tx according to section 13.4.4
903                 /// </summary>
904                 static public Type FindMostSpecificTarget (EmitContext ec, MethodGroupExpr me,
905                                                            Type target, bool apply_explicit_conv_rules,
906                                                            Location loc)
907                 {
908                         ArrayList tgt_types_set = new ArrayList ();
909                         
910                         if (priv_fmt_expr == null)
911                                 priv_fmt_expr = new EmptyExpression ();
912                         
913                         //
914                         // If any operator converts to T then Tx = T
915                         //
916                         foreach (MethodInfo mi in me.Methods){
917                                 Type ret_type = mi.ReturnType;
918
919                                 if (ret_type == target)
920                                         return ret_type;
921
922                                 if (apply_explicit_conv_rules) {
923                                         //
924                                         // From the spec :
925                                         // Find the set of applicable user-defined conversion operators, U.
926                                         //
927                                         // This set consists of the
928                                         // user-defined implicit or explicit conversion operators declared by
929                                         // the classes or structs in D that convert from a type encompassing
930                                         // or encompassed by S to a type encompassing or encompassed by T
931                                         //
932                                         priv_fms_expr.SetType (ret_type);
933                                         if (WideningStandardConversionExists (ec, priv_fms_expr, target))
934                                                 tgt_types_set.Add (ret_type);
935                                         else {
936                                                 priv_fms_expr.SetType (target);
937                                                 if (WideningStandardConversionExists (ec, priv_fms_expr, ret_type))
938                                                         tgt_types_set.Add (ret_type);
939                                         }
940                                 } else {
941                                         //
942                                         // Only if T is encompassed by param_type
943                                         //
944                                         priv_fms_expr.SetType (ret_type);
945                                         if (WideningStandardConversionExists (ec, priv_fms_expr, target))
946                                                 tgt_types_set.Add (ret_type);
947                                 }
948                         }
949
950                         //
951                         // Explicit conv rules
952                         //
953                         if (apply_explicit_conv_rules) {
954                                 ArrayList candidate_set = new ArrayList ();
955
956                                 foreach (Type ret_type in tgt_types_set){
957                                         priv_fmt_expr.SetType (ret_type);
958                                         
959                                         if (WideningStandardConversionExists (ec, priv_fmt_expr, target))
960                                                 candidate_set.Add (ret_type);
961                                 }
962
963                                 if (candidate_set.Count != 0)
964                                         return FindMostEncompassingType (ec, candidate_set);
965                         }
966                         
967                         //
968                         // Okay, final case !
969                         //
970                         if (apply_explicit_conv_rules)
971                                 return FindMostEncompassedType (ec, tgt_types_set);
972                         else 
973                                 return FindMostEncompassingType (ec, tgt_types_set);
974                 }
975                 
976                 /// <summary>
977                 ///  User-defined Implicit conversions
978                 /// </summary>
979
980                 //
981                 // VB.NET has no notion of User defined conversions
982                 //
983
984 //              static public Expression ImplicitUserConversion (EmitContext ec, Expression source,
985 //                                                               Type target, Location loc)
986 //              {
987 //                      return UserDefinedConversion (ec, source, target, loc, false);
988 //              }
989
990                 /// <summary>
991                 ///  User-defined Explicit conversions
992                 /// </summary>
993
994                 //
995                 // VB.NET has no notion of User defined conversions
996                 //
997
998 //              static public Expression ExplicitUserConversion (EmitContext ec, Expression source,
999 //                                                               Type target, Location loc)
1000 //              {
1001 //                      return UserDefinedConversion (ec, source, target, loc, true);
1002 //              }
1003
1004                 static DoubleHash explicit_conv = new DoubleHash (100);
1005                 static DoubleHash implicit_conv = new DoubleHash (100);
1006                 /// <summary>
1007                 ///   Computes the MethodGroup for the user-defined conversion
1008                 ///   operators from source_type to target_type.  `look_for_explicit'
1009                 ///   controls whether we should also include the list of explicit
1010                 ///   operators
1011                 /// </summary>
1012                 static MethodGroupExpr GetConversionOperators (EmitContext ec,
1013                                                                Type source_type, Type target_type,
1014                                                                Location loc, bool look_for_explicit)
1015                 {
1016                         Expression mg1 = null, mg2 = null;
1017                         Expression mg5 = null, mg6 = null, mg7 = null, mg8 = null;
1018                         string op_name;
1019
1020                         op_name = "op_Implicit";
1021
1022                         MethodGroupExpr union3;
1023                         object r;
1024                         if ((look_for_explicit ? explicit_conv : implicit_conv).Lookup (source_type, target_type, out r))
1025                                 return (MethodGroupExpr) r;
1026
1027                         mg1 = Expression.MethodLookup (ec, source_type, op_name, loc);
1028                         if (source_type.BaseType != null)
1029                                 mg2 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1030
1031                         if (mg1 == null)
1032                                 union3 = (MethodGroupExpr) mg2;
1033                         else if (mg2 == null)
1034                                 union3 = (MethodGroupExpr) mg1;
1035                         else
1036                                 union3 = Invocation.MakeUnionSet (mg1, mg2, loc);
1037
1038                         mg1 = Expression.MethodLookup (ec, target_type, op_name, loc);
1039                         if (mg1 != null){
1040                                 if (union3 != null)
1041                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1042                                 else
1043                                         union3 = (MethodGroupExpr) mg1;
1044                         }
1045
1046                         if (target_type.BaseType != null)
1047                                 mg1 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1048                         
1049                         if (mg1 != null){
1050                                 if (union3 != null)
1051                                         union3 = Invocation.MakeUnionSet (union3, mg1, loc);
1052                                 else
1053                                         union3 = (MethodGroupExpr) mg1;
1054                         }
1055
1056                         MethodGroupExpr union4 = null;
1057
1058                         if (look_for_explicit) {
1059                                 op_name = "op_Explicit";
1060
1061                                 mg5 = Expression.MemberLookup (ec, source_type, op_name, loc);
1062                                 if (source_type.BaseType != null)
1063                                         mg6 = Expression.MethodLookup (ec, source_type.BaseType, op_name, loc);
1064                                 
1065                                 mg7 = Expression.MemberLookup (ec, target_type, op_name, loc);
1066                                 if (target_type.BaseType != null)
1067                                         mg8 = Expression.MethodLookup (ec, target_type.BaseType, op_name, loc);
1068                                 
1069                                 MethodGroupExpr union5 = Invocation.MakeUnionSet (mg5, mg6, loc);
1070                                 MethodGroupExpr union6 = Invocation.MakeUnionSet (mg7, mg8, loc);
1071
1072                                 union4 = Invocation.MakeUnionSet (union5, union6, loc);
1073                         }
1074                         
1075                         MethodGroupExpr ret = Invocation.MakeUnionSet (union3, union4, loc);
1076                         (look_for_explicit ? explicit_conv : implicit_conv).Insert (source_type, target_type, ret);
1077                         return ret;
1078                 }
1079                 
1080                 /// <summary>
1081                 ///   User-defined conversions
1082                 /// </summary>
1083
1084                 //
1085                 // VB.NET has no notion of User defined conversions. This method is not used.
1086                 //
1087                 static public Expression UserDefinedConversion (EmitContext ec, Expression source,
1088                                                                 Type target, Location loc,
1089                                                                 bool look_for_explicit)
1090                 {
1091                         MethodGroupExpr union;
1092                         Type source_type = source.Type;
1093                         MethodBase method = null;
1094
1095                         union = GetConversionOperators (ec, source_type, target, loc, look_for_explicit);
1096                         if (union == null)
1097                                 return null;
1098                         
1099                         Type most_specific_source, most_specific_target;
1100
1101                         most_specific_source = FindMostSpecificSource (ec, union, source, look_for_explicit, loc);
1102                         if (most_specific_source == null)
1103                                 return null;
1104
1105                         most_specific_target = FindMostSpecificTarget (ec, union, target, look_for_explicit, loc);
1106                         if (most_specific_target == null) 
1107                                 return null;
1108
1109                         int count = 0;
1110
1111                         
1112                         foreach (MethodBase mb in union.Methods){
1113                                 ParameterData pd = Invocation.GetParameterData (mb);
1114                                 MethodInfo mi = (MethodInfo) mb;
1115                                 
1116                                 if (pd.ParameterType (0) == most_specific_source &&
1117                                     mi.ReturnType == most_specific_target) {
1118                                         method = mb;
1119                                         count++;
1120                                 }
1121                         }
1122                         
1123                         if (method == null || count > 1)
1124                                 return null;
1125                         
1126                         
1127                         //
1128                         // This will do the conversion to the best match that we
1129                         // found.  Now we need to perform an implict standard conversion
1130                         // if the best match was not the type that we were requested
1131                         // by target.
1132                         //
1133                         if (look_for_explicit)
1134                                 source = WideningAndNarrowingConversionStandard (ec, source, most_specific_source, loc);
1135                         else
1136                                 source = WideningConversionStandard (ec, source, most_specific_source, loc);
1137
1138                         if (source == null)
1139                                 return null;
1140
1141                         Expression e;
1142                         e =  new UserCast ((MethodInfo) method, source, loc);
1143                         if (e.Type != target){
1144                                 if (!look_for_explicit)
1145                                         e = WideningConversionStandard (ec, e, target, loc);
1146                                 else
1147                                         e = WideningAndNarrowingConversionStandard (ec, e, target, loc);
1148                         }
1149
1150                         return e;
1151                 }
1152                 
1153                 /// <summary>
1154                 ///   Converts implicitly the resolved expression `expr' into the
1155                 ///   `target_type'.  It returns a new expression that can be used
1156                 ///   in a context that expects a `target_type'. 
1157                 /// </summary>
1158                 static public Expression WideningConversion (EmitContext ec, Expression expr,
1159                                                              Type target_type, Location loc)
1160                 {
1161                         Expression e;
1162
1163                         if (target_type == null)
1164                                 throw new Exception ("Target type is null");
1165
1166                         e = WideningConversionStandard (ec, expr, target_type, loc);
1167                         if (e != null)
1168                                 return e;
1169
1170                         //
1171                         // VB.NET has no notion of User defined conversions
1172                         //
1173
1174 //                      e = ImplicitUserConversion (ec, expr, target_type, loc);
1175 //                      if (e != null)
1176 //                              return e;
1177
1178                         return null;
1179                 }
1180
1181                 
1182                 /// <summary>
1183                 ///   Attempts to apply the `Standard Implicit
1184                 ///   Conversion' rules to the expression `expr' into
1185                 ///   the `target_type'.  It returns a new expression
1186                 ///   that can be used in a context that expects a
1187                 ///   `target_type'.
1188                 ///
1189                 ///   This is different from `WideningConversion' in that the
1190                 ///   user defined implicit conversions are excluded. 
1191                 /// </summary>
1192                 static public Expression WideningConversionStandard (EmitContext ec, Expression expr,
1193                                                                      Type target_type, Location loc)
1194                 {
1195                         Type expr_type = expr.Type;
1196                         Expression e;
1197
1198                         if (expr is NullLiteral) {
1199                                 if (target_type.IsGenericParameter)
1200                                         return TypeParameter_to_Null (expr, target_type, loc);
1201
1202                                 if (TypeManager.IsNullableType (target_type))
1203                                         return new Nullable.NullLiteral (target_type, loc);
1204                         }
1205
1206                         if (TypeManager.IsNullableType (expr_type) && TypeManager.IsNullableType (target_type))
1207                                 return Nullable.LiftedConversion.Create (ec, expr, target_type, false, loc);
1208
1209                         if (expr.eclass == ExprClass.MethodGroup){
1210                                 if (!TypeManager.IsDelegateType (target_type)){
1211                                         return null;
1212                                 }
1213
1214                                 //
1215                                 // Only allow anonymous method conversions on post ISO_1
1216                                 //
1217                                 if (RootContext.Version != LanguageVersion.ISO_1){
1218                                         MethodGroupExpr mg = expr as MethodGroupExpr;
1219                                         if (mg != null)
1220                                                 return ImplicitDelegateCreation.Create (ec, mg, target_type, loc);
1221                                 }
1222                         }
1223
1224                         if (expr_type.Equals (target_type) && !TypeManager.IsNullType (expr_type))
1225                                 return expr;
1226
1227                         e = WideningNumericConversion (ec, expr, target_type, loc);
1228                         if (e != null)
1229                                 return e;
1230
1231                         e = WideningReferenceConversion (ec, expr, target_type);
1232                         if (e != null)
1233                                 return e;
1234                         
1235                         if ((target_type == TypeManager.enum_type ||
1236                              target_type.IsSubclassOf (TypeManager.enum_type)) &&
1237                             expr is IntLiteral){
1238                                 IntLiteral i = (IntLiteral) expr;
1239
1240                                 if (i.Value == 0)
1241                                         return new EnumConstant ((Constant) expr, target_type);
1242                         }
1243
1244                         if (ec.InUnsafe) {
1245                                 if (expr_type.IsPointer){
1246                                         if (target_type == TypeManager.void_ptr_type)
1247                                                 return new EmptyCast (expr, target_type);
1248
1249                                         //
1250                                         // yep, comparing pointer types cant be done with
1251                                         // t1 == t2, we have to compare their element types.
1252                                         //
1253                                         if (target_type.IsPointer){
1254                                                 if (TypeManager.GetElementType(target_type) == TypeManager.GetElementType(expr_type))
1255                                                         return expr;
1256                                         }
1257                                 }
1258                                 
1259                                 if (target_type.IsPointer) {
1260                                         if (expr_type == TypeManager.null_type)
1261                                                 return new EmptyCast (expr, target_type);
1262
1263                                         if (expr_type == TypeManager.void_ptr_type)
1264                                                 return new EmptyCast (expr, target_type);
1265                                 }
1266                         }
1267
1268                         if (expr_type == TypeManager.anonymous_method_type){
1269                                 if (!TypeManager.IsDelegateType (target_type)){
1270                                         Report.Error (1660, loc,
1271                                                               "Cannot convert anonymous method to `{0}', since it is not a delegate",
1272                                                               TypeManager.CSharpName (target_type));
1273                                         return null;
1274                                 }
1275
1276                                 AnonymousMethod am = (AnonymousMethod) expr;
1277                                 int errors = Report.Errors;
1278
1279                                 Expression conv = am.Compatible (ec, target_type, false);
1280                                 if (conv != null)
1281                                         return conv;
1282                                 
1283                                 //
1284                                 // We return something instead of null, to avoid
1285                                 // the duplicate error, since am.Compatible would have
1286                                 // reported that already
1287                                 //
1288                                 if (errors != Report.Errors)
1289                                         return new EmptyCast (expr, target_type);
1290                         }
1291
1292                         //
1293                         // VB.NET specific conversions
1294                         //
1295
1296                         e = WideningStringConversions (ec, expr, target_type, loc);
1297                         if (e != null)
1298                                 return e;
1299
1300                         
1301                         return null;
1302                 }
1303
1304                 /// <summary>
1305                 ///   Attempts to perform an implicit constant conversion of the IntConstant
1306                 ///   into a different data type using casts (See Implicit Constant
1307                 ///   Expression Conversions)
1308                 /// </summary>
1309                 static public Expression TryWideningIntConversion (Type target_type, IntConstant ic)
1310                 {
1311                         int value = ic.Value;
1312
1313                         if (target_type == TypeManager.sbyte_type){
1314                                 if (value >= SByte.MinValue && value <= SByte.MaxValue)
1315                                         return new SByteConstant ((sbyte) value);
1316                         } else if (target_type == TypeManager.byte_type){
1317                                 if (value >= Byte.MinValue && value <= Byte.MaxValue)
1318                                         return new ByteConstant ((byte) value);
1319                         } else if (target_type == TypeManager.short_type){
1320                                 if (value >= Int16.MinValue && value <= Int16.MaxValue)
1321                                         return new ShortConstant ((short) value);
1322                         } else if (target_type == TypeManager.ushort_type){
1323                                 if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
1324                                         return new UShortConstant ((ushort) value);
1325                         } else if (target_type == TypeManager.uint32_type){
1326                                 if (value >= 0)
1327                                         return new UIntConstant ((uint) value);
1328                         } else if (target_type == TypeManager.uint64_type){
1329                                 //
1330                                 // we can optimize this case: a positive int32
1331                                 // always fits on a uint64.  But we need an opcode
1332                                 // to do it.
1333                                 //
1334                                 if (value >= 0)
1335                                         return new ULongConstant ((ulong) value);
1336                         } else if (target_type == TypeManager.double_type)
1337                                 return new DoubleConstant ((double) value);
1338                         else if (target_type == TypeManager.float_type)
1339                                 return new FloatConstant ((float) value);
1340                         
1341                         if (value == 0 && ic is IntLiteral && TypeManager.IsEnumType (target_type)){
1342                                 Type underlying = TypeManager.EnumToUnderlying (target_type);
1343                                 Constant e = (Constant) ic;
1344                                 
1345                                 //
1346                                 // Possibly, we need to create a different 0 literal before passing
1347                                 // to EnumConstant
1348                                 //
1349                                 if (underlying == TypeManager.int64_type)
1350                                         e = new LongLiteral (0);
1351                                 else if (underlying == TypeManager.uint64_type)
1352                                         e = new ULongLiteral (0);
1353
1354                                 return new EnumConstant (e, target_type);
1355                         }
1356
1357                         //
1358                         // If `target_type' is an interface and the type of `ic' implements the interface
1359                         // e.g. target_type is IComparable, IConvertible, IFormattable
1360                         //
1361                         if (target_type.IsInterface && target_type.IsAssignableFrom (ic.Type))
1362                                 return new BoxedCast (ic);
1363
1364                         return null;
1365                 }
1366
1367                 static public void Error_CannotWideningConversion (Location loc, Type source, Type target)
1368                 {
1369                         if (source.Name == target.Name){
1370                                 Report.ExtraInformation (loc,
1371                                          String.Format (
1372                                                 "The type {0} has two conflicting definitons, one comes from {0} and the other from {1}",
1373                                                 source.Assembly.FullName, target.Assembly.FullName));
1374                                                          
1375                         }
1376                         Report.Error (29, loc, "Cannot convert implicitly from {0} to `{1}'",
1377                                       source == TypeManager.anonymous_method_type ?
1378                                       "anonymous method" : "`" + TypeManager.CSharpName (source) + "'",
1379                                       TypeManager.CSharpName (target));
1380                 }
1381
1382                 /// <summary>
1383                 ///   Attempts to implicitly convert `source' into `target_type', using
1384                 ///   WideningConversion.  If there is no implicit conversion, then
1385                 ///   an error is signaled
1386                 /// </summary>
1387                 static public Expression WideningConversionRequired (EmitContext ec, Expression source,
1388                                                                      Type target_type, Location loc)
1389                 {
1390                         Expression e;
1391
1392                         int errors = Report.Errors;
1393                         e = WideningConversion (ec, source, target_type, loc);
1394                         if (Report.Errors > errors)
1395                                 return null;
1396                         if (e != null)
1397                                 return e;
1398
1399                         if (source is DoubleLiteral) {
1400                                 if (target_type == TypeManager.float_type) {
1401                                         Error_664 (loc, "float", "f");
1402                                         return null;
1403                                 }
1404                                 if (target_type == TypeManager.decimal_type) {
1405                                         Error_664 (loc, "decimal", "m");
1406                                         return null;
1407                                 }
1408                         }
1409
1410                         if (source is Constant){
1411                                 Constant c = (Constant) source;
1412
1413                                 Expression.Error_ConstantValueCannotBeConverted (loc, c.AsString (), target_type);
1414                                 return null;
1415                         }
1416                         
1417                         Error_CannotWideningConversion (loc, source.Type, target_type);
1418
1419                         return null;
1420                 }
1421
1422                 static void Error_664 (Location loc, string type, string suffix) {
1423                         Report.Error (664, loc,
1424                                 "Literal of type double cannot be implicitly converted to type '{0}'. Add suffix '{1}' to create a literal of this type",
1425                                 type, suffix);
1426                 }
1427
1428                 /// <summary>
1429                 ///   Performs the explicit numeric conversions
1430                 /// </summary>
1431
1432                 /// <summary>
1433                 ///   Performs the explicit numeric conversions
1434                 /// </summary>
1435                 static Expression NarrowingNumericConversion (EmitContext ec, Expression expr, Type target_type, Location loc)
1436                 {
1437                         Type expr_type = expr.Type;
1438
1439                         //
1440                         // If we have an enumeration, extract the underlying type,
1441                         // use this during the comparison, but wrap around the original
1442                         // target_type
1443                         //
1444                         Type real_target_type = target_type;
1445
1446                         if (TypeManager.IsEnumType (real_target_type))
1447                                 real_target_type = TypeManager.EnumToUnderlying (real_target_type);
1448
1449                         if (WideningStandardConversionExists (ec, expr, real_target_type)){
1450                                 Expression ce = WideningConversionStandard (ec, expr, real_target_type, loc);
1451
1452                                 if (real_target_type != target_type)
1453                                         return new EmptyCast (ce, target_type);
1454                                 return ce;
1455                         }
1456                         
1457                         if (expr_type == TypeManager.short_type){
1458                                 //
1459                                 // From short to byte
1460                                 //
1461                                 if (real_target_type == TypeManager.byte_type)
1462                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I2_U1);
1463                         } else if (expr_type == TypeManager.int32_type){
1464                                 //
1465                                 // From int to byte, short
1466                                 //
1467                                 if (real_target_type == TypeManager.byte_type)
1468                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_U1);
1469                                 if (real_target_type == TypeManager.short_type)
1470                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I4_I2);
1471                         } else if (expr_type == TypeManager.int64_type){
1472                                 //
1473                                 // From long to byte, short, int
1474                                 //
1475                                 if (real_target_type == TypeManager.byte_type)
1476                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_U1);
1477                                 if (real_target_type == TypeManager.short_type)
1478                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I2);
1479                                 if (real_target_type == TypeManager.int32_type)
1480                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.I8_I4);
1481                         } else if (expr_type == TypeManager.decimal_type){
1482                                 //
1483                                 // From decimal to byte, short, int, long
1484                                 //
1485                                 if (real_target_type == TypeManager.byte_type)
1486                                         return new ImplicitInvocation (ec, "System", "Convert" , "ToByte", loc, expr);  
1487                                 if (real_target_type == TypeManager.short_type)
1488                                         return new ImplicitInvocation (ec, "System", "Convert", "ToInt16", loc, expr);  
1489                                 if (real_target_type == TypeManager.int32_type)
1490                                         return new ImplicitInvocation (ec, "System", "Convert", "ToInt32", loc, expr);  
1491                                 if (real_target_type == TypeManager.int64_type)
1492                                         return new ImplicitInvocation (ec, "System", "Convert", "ToInt64", loc, expr);  
1493                         } else if (expr_type == TypeManager.float_type){
1494                                 //
1495                                 // From float to byte, short, int, long, decimal
1496                                 //
1497                                 if (real_target_type == TypeManager.byte_type)
1498                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1499                                 if (real_target_type == TypeManager.short_type)
1500                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1501                                 if (real_target_type == TypeManager.int32_type)
1502                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1503                                 if (real_target_type == TypeManager.int64_type)
1504                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1505                                 if (real_target_type == TypeManager.decimal_type)
1506                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);    
1507                         } else if (expr_type == TypeManager.double_type){
1508                                 //
1509                                 // From double to byte, short, int, long, float, decimal
1510                                 //
1511                                 if (real_target_type == TypeManager.byte_type)
1512                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_U1);
1513                                 if (real_target_type == TypeManager.short_type)
1514                                         return new FloatingToFixedCast (ec, expr, target_type, ConvCast.Mode.R8_I2);
1515                                 if (real_target_type == TypeManager.int32_type)
1516                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I4);
1517                                 if (real_target_type == TypeManager.int64_type)
1518                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_I8);
1519                                 if (real_target_type == TypeManager.float_type)
1520                                         return new ConvCast (ec, expr, target_type, ConvCast.Mode.R8_R4);
1521                                 if (real_target_type == TypeManager.decimal_type)
1522                                         return new ImplicitNew (ec, "System", "Decimal", loc, expr);
1523                         } 
1524
1525                         return null;
1526                 }
1527
1528                 /// <summary> 
1529                 /// VB.NET specific: Convert to and from boolean
1530                 /// </summary>
1531
1532                 static public Expression BooleanConversions (EmitContext ec, Expression expr,
1533                                                                     Type target_type, Location loc)
1534                 {
1535                         Type expr_type = expr.Type;
1536                         Type real_target_type = target_type;
1537
1538                         if (expr_type == TypeManager.bool_type) {
1539
1540                                 //
1541                                 // From boolean to byte, short, int,
1542                                 // long, float, double, decimal
1543                                 //
1544
1545                                 if (real_target_type == TypeManager.byte_type)
1546                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_U1);
1547                                 if (real_target_type == TypeManager.short_type)
1548                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I2);
1549                                 if (real_target_type == TypeManager.int32_type)
1550                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I4);
1551                                 if (real_target_type == TypeManager.int64_type)
1552                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_I8);
1553                                 if (real_target_type == TypeManager.float_type)
1554                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R4);
1555                                 if (real_target_type == TypeManager.double_type)
1556                                         return new BooleanToNumericCast (expr, target_type, OpCodes.Conv_R8);
1557                                 if (real_target_type == TypeManager.decimal_type) {
1558                                         return new ImplicitInvocation (ec, "DecimalType", "FromBoolean", loc, expr);
1559                                 }
1560                         } if (real_target_type == TypeManager.bool_type) {
1561
1562                                 //
1563                                 // From byte, short, int, long, float,
1564                                 // double, decimal to boolean
1565                                 //
1566
1567                                 if (expr_type == TypeManager.byte_type ||
1568                                         expr_type == TypeManager.short_type ||
1569                                         expr_type == TypeManager.int32_type ||
1570                                         expr_type == TypeManager.int64_type || 
1571                                         expr_type == TypeManager.float_type || 
1572                                         expr_type == TypeManager.double_type)
1573                                                 return new NumericToBooleanCast (expr, expr_type);
1574                                 if (expr_type == TypeManager.decimal_type) {
1575                                         return new ImplicitInvocation (ec, "System", "Convert", "ToBoolean", loc, expr);
1576                                 }
1577                         }
1578
1579                         return null;
1580                 }
1581
1582                 /// <summary> 
1583                 /// VB.NET specific: Widening conversions to string
1584                 /// </summary>
1585
1586                 static public Expression WideningStringConversions (EmitContext ec, Expression expr,
1587                                                                     Type target_type, Location loc)
1588
1589                 {
1590                         Type expr_type = expr.Type;
1591                         Type real_target_type = target_type;
1592
1593                         if (real_target_type == TypeManager.string_type) {
1594                                 //
1595                                 // From char to string
1596                                 //
1597                                 if (expr_type == TypeManager.char_type)
1598                                         return new ImplicitInvocation (ec, "StringType", "FromChar", loc, expr);
1599                         }
1600
1601                         if(expr_type.IsArray && (expr_type.GetElementType() == TypeManager.char_type)) {
1602                                 //
1603                                 // From char array to string
1604                                 //
1605                                 return new ImplicitNew (ec, "System", "String", loc, expr);
1606                         }
1607
1608                         return null;
1609                 }
1610                 
1611                 /// <summary> 
1612                 /// VB.NET specific: Narrowing conversions involving strings
1613                 /// </summary>
1614
1615                 static public Expression NarrowingStringConversions (EmitContext ec, Expression expr,
1616                                                                     Type target_type, Location loc)
1617                 {
1618                         Type expr_type = expr.Type;
1619                         Type real_target_type = target_type;
1620
1621                         // FIXME: Need to take care of Constants
1622
1623                         if (expr_type == TypeManager.string_type) {
1624
1625                                 //
1626                                 // From string to chararray, bool,
1627                                 // byte, short, char, int, long,
1628                                 // float, double, decimal and date 
1629                                 //
1630
1631                                 if (real_target_type.IsArray && (real_target_type.GetElementType() == TypeManager.char_type))
1632                                         return new ImplicitInvocation (ec, "CharArrayType", "FromString", loc, expr);
1633                                 if (real_target_type == TypeManager.bool_type)
1634                                         return new ImplicitInvocation (ec, "BooleanType", "FromString", loc, expr);
1635                                 if (real_target_type == TypeManager.byte_type)
1636                                         return new ImplicitInvocation (ec, "ByteType", "FromString", loc, expr);
1637                                 if (real_target_type == TypeManager.short_type)
1638                                         return new ImplicitInvocation (ec, "ShortType", "FromString", loc, expr);
1639                                 if (real_target_type == TypeManager.char_type)
1640                                         return new ImplicitInvocation (ec, "CharType", "FromString", loc, expr);
1641                                 if (real_target_type == TypeManager.int32_type)
1642                                         return new ImplicitInvocation (ec, "IntegerType", "FromString", loc, expr);
1643                                 if (real_target_type == TypeManager.int64_type)
1644                                         return new ImplicitInvocation (ec, "LongType", "FromString", loc, expr);
1645                                 if (real_target_type == TypeManager.float_type)
1646                                         return new ImplicitInvocation (ec, "SingleType", "FromString", loc, expr);
1647                                 if (real_target_type == TypeManager.double_type)
1648                                         return new ImplicitInvocation (ec, "DoubleType", "FromString", loc, expr);
1649                                 if (real_target_type == TypeManager.decimal_type)
1650                                         return new ImplicitInvocation (ec, "DecimalType", "FromString", loc, expr);
1651                                 if (real_target_type == TypeManager.date_type)
1652                                         return new ImplicitInvocation (ec, "DateType", "FromString", loc, expr);
1653                         } if (real_target_type == TypeManager.string_type) {
1654
1655                                 //
1656                                 // From bool, byte, short, char, int,
1657                                 // long, float, double, decimal and
1658                                 // date to string
1659                                 //
1660
1661                                 if (expr_type == TypeManager.bool_type)
1662                                         return new ImplicitInvocation (ec, "StringType", "FromBoolean", loc, expr);
1663                                 if (expr_type == TypeManager.byte_type)
1664                                         return new ImplicitInvocation (ec, "StringType", "FromByte", loc, expr);
1665                                 if (expr_type == TypeManager.short_type)
1666                                         return new ImplicitInvocation (ec, "StringType", "FromShort", loc, expr);
1667                                 if (expr_type == TypeManager.int32_type)
1668                                         return new ImplicitInvocation (ec, "StringType", "FromInteger", loc, expr);
1669                                 if (expr_type == TypeManager.int64_type)
1670                                         return new ImplicitInvocation (ec, "StringType", "FromLong", loc, expr);
1671                                 if (expr_type == TypeManager.float_type)
1672                                         return new ImplicitInvocation (ec, "StringType", "FromSingle", loc, expr);
1673                                 if (expr_type == TypeManager.double_type)
1674                                         return new ImplicitInvocation (ec, "StringType", "FromDouble", loc, expr);
1675                                 if (expr_type == TypeManager.decimal_type)
1676                                         return new ImplicitInvocation (ec, "StringType", "FromDecimal", loc, expr);
1677                                 if (expr_type == TypeManager.date_type)
1678                                         return new ImplicitInvocation (ec, "StringType", "FromDate", loc, expr);
1679                         }
1680
1681                         return null;
1682                 }
1683
1684                 /// <summary>
1685                 ///  Returns whether an explicit reference conversion can be performed
1686                 ///  from source_type to target_type
1687                 /// </summary>
1688                 public static bool NarrowingReferenceConversionExists (Type source_type, Type target_type)
1689                 {
1690                         bool target_is_type_param = target_type.IsGenericParameter;
1691                         bool target_is_value_type = target_type.IsValueType;
1692                         
1693                         if (source_type == target_type)
1694                                 return true;
1695
1696                         //
1697                         // From object to a generic parameter
1698                         //
1699                         if (source_type == TypeManager.object_type && target_is_type_param)
1700                                 return true;
1701
1702                         //
1703                         // From object to any reference type
1704                         //
1705                         if (source_type == TypeManager.object_type && !target_is_value_type)
1706                                 return true;
1707                                         
1708                         //
1709                         // From any class S to any class-type T, provided S is a base class of T
1710                         //
1711                         if (TypeManager.IsSubclassOf (target_type, source_type))
1712                                 return true;
1713
1714                         //
1715                         // From any interface type S to any interface T provided S is not derived from T
1716                         //
1717                         if (source_type.IsInterface && target_type.IsInterface){
1718                                 if (!TypeManager.IsSubclassOf (target_type, source_type))
1719                                         return true;
1720                         }
1721                             
1722                         //
1723                         // From any class type S to any interface T, provided S is not sealed
1724                         // and provided S does not implement T.
1725                         //
1726                         if (target_type.IsInterface && !source_type.IsSealed &&
1727                             !TypeManager.ImplementsInterface (source_type, target_type))
1728                                 return true;
1729
1730                         //
1731                         // From any interface-type S to to any class type T, provided T is not
1732                         // sealed, or provided T implements S.
1733                         //
1734                         if (source_type.IsInterface &&
1735                             (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)))
1736                                 return true;
1737                         
1738                         
1739                         // From an array type S with an element type Se to an array type T with an 
1740                         // element type Te provided all the following are true:
1741                         //     * S and T differe only in element type, in other words, S and T
1742                         //       have the same number of dimensions.
1743                         //     * Both Se and Te are reference types
1744                         //     * An explicit referenc conversions exist from Se to Te
1745                         //
1746                         if (source_type.IsArray && target_type.IsArray) {
1747                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1748                                         
1749                                         Type source_element_type = TypeManager.GetElementType (source_type);
1750                                         Type target_element_type = TypeManager.GetElementType (target_type);
1751                                         
1752                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1753                                                 if (NarrowingReferenceConversionExists (source_element_type,
1754                                                                                        target_element_type))
1755                                                         return true;
1756                                 }
1757                         }
1758                         
1759
1760                         // From System.Array to any array-type
1761                         if (source_type == TypeManager.array_type &&
1762                             target_type.IsArray){
1763                                 return true;
1764                         }
1765
1766                         //
1767                         // From System delegate to any delegate-type
1768                         //
1769                         if (source_type == TypeManager.delegate_type &&
1770                             TypeManager.IsDelegateType (target_type))
1771                                 return true;
1772
1773                         //
1774                         // From ICloneable to Array or Delegate types
1775                         //
1776                         if (source_type == TypeManager.icloneable_type &&
1777                             (target_type == TypeManager.array_type ||
1778                              target_type == TypeManager.delegate_type))
1779                                 return true;
1780                         
1781                         return false;
1782                 }
1783
1784                 /// <summary>
1785                 ///   Implements Explicit Reference conversions
1786                 /// </summary>
1787                 static Expression NarrowingReferenceConversion (Expression source, Type target_type)
1788                 {
1789                         Type source_type = source.Type;
1790                         bool target_is_type_param = target_type.IsGenericParameter;
1791                         bool target_is_value_type = target_type.IsValueType;
1792
1793                         //
1794                         // From object to a generic parameter
1795                         //
1796                         if (source_type == TypeManager.object_type && target_is_type_param)
1797                                 return new UnboxCast (source, target_type);
1798
1799                         //
1800                         // From object to any reference type
1801                         //
1802                         if (source_type == TypeManager.object_type && !target_is_value_type)
1803                                 return new ClassCast (source, target_type);
1804
1805                         //
1806                         // Unboxing conversion.
1807                         //
1808                         if (((source_type == TypeManager.enum_type &&
1809                                 !(source is EmptyCast)) ||
1810                                 source_type == TypeManager.value_type) && target_is_value_type)
1811                                 return new UnboxCast (source, target_type);
1812
1813                         //
1814                         // From any class S to any class-type T, provided S is a base class of T
1815                         //
1816                         if (TypeManager.IsSubclassOf (target_type, source_type))
1817                                 return new ClassCast (source, target_type);
1818
1819                         //
1820                         // From any interface type S to any interface T provided S is not derived from T
1821                         //
1822                         if (source_type.IsInterface && target_type.IsInterface){
1823                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1824                                         return null;
1825                                 else
1826                                         return new ClassCast (source, target_type);
1827                         }
1828
1829                         //
1830                         // From any class type S to any interface T, provides S is not sealed
1831                         // and provided S does not implement T.
1832                         //
1833                         if (target_type.IsInterface && !source_type.IsSealed) {
1834                                 if (TypeManager.ImplementsInterface (source_type, target_type))
1835                                         return null;
1836                                 else
1837                                         return new ClassCast (source, target_type);
1838                                 
1839                         }
1840
1841                         //
1842                         // From any interface-type S to to any class type T, provided T is not
1843                         // sealed, or provided T implements S.
1844                         //
1845                         if (source_type.IsInterface) {
1846                                 if (!target_type.IsSealed || TypeManager.ImplementsInterface (target_type, source_type)) {
1847                                         if (target_type.IsClass)
1848                                                 return new ClassCast (source, target_type);
1849                                         else
1850                                                 return new UnboxCast (source, target_type);
1851                                 }
1852
1853                                 return null;
1854                         }
1855                         
1856                         // From an array type S with an element type Se to an array type T with an 
1857                         // element type Te provided all the following are true:
1858                         //     * S and T differe only in element type, in other words, S and T
1859                         //       have the same number of dimensions.
1860                         //     * Both Se and Te are reference types
1861                         //     * An explicit referenc conversions exist from Se to Te
1862                         //
1863                         if (source_type.IsArray && target_type.IsArray) {
1864                                 if (source_type.GetArrayRank () == target_type.GetArrayRank ()) {
1865                                         
1866                                         Type source_element_type = TypeManager.GetElementType (source_type);
1867                                         Type target_element_type = TypeManager.GetElementType (target_type);
1868                                         
1869                                         if (!source_element_type.IsValueType && !target_element_type.IsValueType)
1870                                                 if (NarrowingReferenceConversionExists (source_element_type,
1871                                                                                        target_element_type))
1872                                                         return new ClassCast (source, target_type);
1873                                 }
1874                         }
1875                         
1876
1877                         // From System.Array to any array-type
1878                         if (source_type == TypeManager.array_type &&
1879                             target_type.IsArray) {
1880                                 return new ClassCast (source, target_type);
1881                         }
1882
1883                         //
1884                         // From System delegate to any delegate-type
1885                         //
1886                         if (source_type == TypeManager.delegate_type &&
1887                             TypeManager.IsDelegateType (target_type))
1888                                 return new ClassCast (source, target_type);
1889
1890                         //
1891                         // From ICloneable to Array or Delegate types
1892                         //
1893                         if (source_type == TypeManager.icloneable_type &&
1894                             (target_type == TypeManager.array_type ||
1895                              target_type == TypeManager.delegate_type))
1896                                 return new ClassCast (source, target_type);
1897                         
1898                         return null;
1899                 }
1900                 
1901                 /// <summary>
1902                 ///   Performs an explicit conversion of the expression `expr' whose
1903                 ///   type is expr.Type to `target_type'.
1904                 /// </summary>
1905                 static public Expression WideningAndNarrowingConversion (EmitContext ec, Expression expr,
1906                                                              Type target_type, Location loc)
1907                 {
1908                         Type expr_type = expr.Type;
1909                         Type original_expr_type = expr_type;
1910
1911                         if (expr_type.IsSubclassOf (TypeManager.enum_type)){
1912                                 if (target_type == TypeManager.enum_type ||
1913                                     target_type == TypeManager.object_type) {
1914                                         if (expr is EnumConstant)
1915                                                 expr = ((EnumConstant) expr).Child;
1916                                         // We really need all these casts here .... :-(
1917                                         expr = new BoxedCast (new EmptyCast (expr, expr_type));
1918                                         return new EmptyCast (expr, target_type);
1919                                 } else if ((expr_type == TypeManager.enum_type) && target_type.IsValueType &&
1920                                            target_type.IsSubclassOf (TypeManager.enum_type))
1921                                         return new UnboxCast (expr, target_type);
1922
1923                                 //
1924                                 // Notice that we have kept the expr_type unmodified, which is only
1925                                 // used later on to 
1926                                 if (expr is EnumConstant)
1927                                         expr = ((EnumConstant) expr).Child;
1928                                 else
1929                                         expr = new EmptyCast (expr, TypeManager.EnumToUnderlying (expr_type));
1930                                 expr_type = expr.Type;
1931                         }
1932
1933                         int errors = Report.Errors;
1934                         Expression ne = WideningConversionStandard (ec, expr, target_type, loc);
1935                         if (Report.Errors > errors)
1936                                 return null;
1937
1938                         if (ne != null)
1939                                 return ne;
1940
1941                         if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
1942                                 return Nullable.LiftedConversion.Create (ec, expr, target_type, true, loc);
1943
1944                         ne = NarrowingNumericConversion (ec, expr, target_type, loc);
1945                         if (ne != null)
1946                                 return ne;
1947
1948                         //
1949                         // Unboxing conversion.
1950                         //
1951                         if (expr_type == TypeManager.object_type && target_type.IsValueType)
1952                                 return new UnboxCast (expr, target_type);
1953
1954                         //
1955                         // Skip the NarrowingReferenceConversion because we can not convert
1956                         // from Null to a ValueType, and ExplicitReference wont check against
1957                         // null literal explicitly
1958                         //
1959                         if (expr_type != TypeManager.null_type){
1960                                 ne = NarrowingReferenceConversion (expr, target_type);
1961                                 if (ne != null)
1962                                         return ne;
1963                         }
1964
1965                 skip_explicit:
1966                         if (ec.InUnsafe){
1967                                 if (target_type.IsPointer){
1968                                         if (expr_type.IsPointer)
1969                                                 return new EmptyCast (expr, target_type);
1970                                         
1971                                         if (expr_type == TypeManager.sbyte_type ||
1972                                             expr_type == TypeManager.byte_type ||
1973                                             expr_type == TypeManager.short_type ||
1974                                             expr_type == TypeManager.ushort_type ||
1975                                             expr_type == TypeManager.int32_type ||
1976                                             expr_type == TypeManager.uint32_type ||
1977                                             expr_type == TypeManager.uint64_type ||
1978                                             expr_type == TypeManager.int64_type)
1979                                                 return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
1980                                 }
1981                                 if (expr_type.IsPointer){
1982                                         Expression e = null;
1983                                         
1984                                         if (target_type == TypeManager.sbyte_type)
1985                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
1986                                         else if (target_type == TypeManager.byte_type)
1987                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
1988                                         else if (target_type == TypeManager.short_type)
1989                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
1990                                         else if (target_type == TypeManager.ushort_type)
1991                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
1992                                         else if (target_type == TypeManager.int32_type)
1993                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
1994                                         else if (target_type == TypeManager.uint32_type)
1995                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
1996                                         else if (target_type == TypeManager.uint64_type)
1997                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
1998                                         else if (target_type == TypeManager.int64_type){
1999                                                 e = new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
2000                                         }
2001
2002                                         if (e != null){
2003                                                 Expression ci, ce;
2004
2005                                                 ci = WideningConversionStandard (ec, e, target_type, loc);
2006
2007                                                 if (ci != null)
2008                                                         return ci;
2009
2010                                                 ce = NarrowingNumericConversion (ec, e, target_type, loc);
2011                                                 if (ce != null)
2012                                                         return ce;
2013                                                 //
2014                                                 // We should always be able to go from an uint32
2015                                                 // implicitly or explicitly to the other integral
2016                                                 // types
2017                                                 //
2018                                                 throw new Exception ("Internal compiler error");
2019                                         }
2020                                 }
2021                         }
2022
2023                         //
2024                         // VB.NET specific conversions
2025                         //
2026
2027                         ne = BooleanConversions (ec, expr, target_type, loc);
2028                         if (ne != null)
2029                                 return ne;
2030
2031                         ne = NarrowingStringConversions (ec, expr, target_type, loc);
2032                         if (ne != null)
2033                                 return ne;
2034                         
2035                         
2036                         //
2037                         // VB.NET has no notion of User defined conversions
2038                         //
2039
2040 //                      ne = ExplicitUserConversion (ec, expr, target_type, loc);
2041 //                      if (ne != null)
2042 //                              return ne;
2043
2044                         if (expr is NullLiteral){
2045                                 Report.Error (37, loc, "Cannot convert null to value type `" +
2046                                               TypeManager.CSharpName (target_type) + "'");
2047                                 return null;
2048                         }
2049                                 
2050                         Error_CannotConvertType (loc, original_expr_type, target_type);
2051                         return null;
2052                 }
2053
2054                 /// <summary>
2055                 ///   Same as WideningAndNarrowingConversion, only it doesn't include user defined conversions
2056                 /// </summary>
2057                 static public Expression WideningAndNarrowingConversionStandard (EmitContext ec, Expression expr,
2058                                                                      Type target_type, Location l)
2059                 {
2060                         int errors = Report.Errors;
2061                         Expression ne = WideningConversionStandard (ec, expr, target_type, l);
2062                         if (Report.Errors > errors)
2063                                 return null;
2064
2065                         if (ne != null)
2066                                 return ne;
2067
2068                         if (TypeManager.IsNullableType (expr.Type) && TypeManager.IsNullableType (target_type))
2069                                 return Nullable.LiftedConversion.Create (ec, expr, target_type, true, l);
2070
2071                         ne = NarrowingNumericConversion (ec, expr, target_type, l);
2072                         if (ne != null)
2073                                 return ne;
2074
2075                         ne = NarrowingReferenceConversion (expr, target_type);
2076                         if (ne != null)
2077                                 return ne;
2078
2079                         Error_CannotConvertType (l, expr.Type, target_type);
2080                         return null;
2081                 }
2082         }
2083 }