~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/CSharpBinding/MonoDevelop.CSharp.Parser/mcs/convert.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
//   Marek Safar (marek.safar@gmail.com)
 
8
//
 
9
// Copyright 2001, 2002, 2003 Ximian, Inc.
 
10
// Copyright 2003-2008 Novell, Inc.
 
11
//
 
12
 
 
13
using System;
 
14
using System.Collections.Generic;
 
15
 
 
16
#if STATIC
 
17
using IKVM.Reflection.Emit;
 
18
#else
 
19
using System.Reflection.Emit;
 
20
#endif
 
21
 
 
22
namespace Mono.CSharp {
 
23
 
 
24
        //
 
25
        // A container class for all the conversion operations
 
26
        //
 
27
        static class Convert
 
28
        {
 
29
                //
 
30
                // From a one-dimensional array-type S[] to System.Collections.IList<T> and base
 
31
                // interfaces of this interface, provided there is an implicit reference conversion
 
32
                // from S to T.
 
33
                //
 
34
                static bool ArrayToIList (ArrayContainer array, TypeSpec list, bool isExplicit)
 
35
                {
 
36
                        if (array.Rank != 1 || !list.IsGenericIterateInterface)
 
37
                                return false;
 
38
 
 
39
                        var arg_type = list.TypeArguments[0];
 
40
                        if (array.Element == arg_type)
 
41
                                return true;
 
42
 
 
43
                        //
 
44
                        // Reject conversion from T[] to IList<U> even if T has U dependency
 
45
                        //
 
46
                        if (arg_type.IsGenericParameter)
 
47
                                return false;
 
48
 
 
49
                        if (isExplicit)
 
50
                                return ExplicitReferenceConversionExists (array.Element, arg_type);
 
51
 
 
52
                        return ImplicitReferenceConversionExists (array.Element, arg_type);
 
53
                }
 
54
                
 
55
                static bool IList_To_Array(TypeSpec list, ArrayContainer array)
 
56
                {
 
57
                        if (array.Rank != 1 || !list.IsGenericIterateInterface)
 
58
                                return false;
 
59
 
 
60
                        var arg_type = list.TypeArguments[0];
 
61
                        if (array.Element == arg_type)
 
62
                                return true;
 
63
                        
 
64
                        return ImplicitReferenceConversionExists (array.Element, arg_type) || ExplicitReferenceConversionExists (array.Element, arg_type);
 
65
                }
 
66
 
 
67
                public static Expression ImplicitTypeParameterConversion (Expression expr, TypeParameterSpec expr_type, TypeSpec target_type)
 
68
                {
 
69
                        //
 
70
                        // From T to a type parameter U, provided T depends on U
 
71
                        //
 
72
                        if (target_type.IsGenericParameter) {
 
73
                                if (expr_type.TypeArguments != null) {
 
74
                                        foreach (var targ in expr_type.TypeArguments) {
 
75
                                                if (!TypeSpecComparer.Override.IsEqual (target_type, targ))
 
76
                                                        continue;
 
77
 
 
78
                                                if (expr == null)
 
79
                                                        return EmptyExpression.Null;
 
80
 
 
81
                                                if (expr_type.IsReferenceType && !((TypeParameterSpec)target_type).IsReferenceType)
 
82
                                                        return new BoxedCast (expr, target_type);
 
83
 
 
84
                                                return new ClassCast (expr, target_type);
 
85
                                        }
 
86
                                }
 
87
 
 
88
                                return null;
 
89
                        }
 
90
 
 
91
                        //
 
92
                        // LAMESPEC: From T to dynamic type because it's like T to object
 
93
                        //
 
94
                        if (target_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
 
95
                                if (expr == null)
 
96
                                        return EmptyExpression.Null;
 
97
 
 
98
                                if (expr_type.IsReferenceType)
 
99
                                        return new ClassCast (expr, target_type);
 
100
 
 
101
                                return new BoxedCast (expr, target_type);
 
102
                        }
 
103
 
 
104
                        //
 
105
                        // From T to its effective base class C
 
106
                        // From T to any base class of C (it cannot contain dynamic or be of dynamic type)
 
107
                        // From T to any interface implemented by C
 
108
                        //
 
109
                        var base_type = expr_type.GetEffectiveBase ();
 
110
                        if (base_type == target_type || TypeSpec.IsBaseClass (base_type, target_type, false) || base_type.ImplementsInterface (target_type, true)) {
 
111
                                if (expr == null)
 
112
                                        return EmptyExpression.Null;
 
113
 
 
114
                                if (expr_type.IsReferenceType)
 
115
                                        return new ClassCast (expr, target_type);
 
116
 
 
117
                                return new BoxedCast (expr, target_type);
 
118
                        }
 
119
 
 
120
                        if (target_type.IsInterface && expr_type.IsConvertibleToInterface (target_type)) {
 
121
                                if (expr == null)
 
122
                                        return EmptyExpression.Null;
 
123
 
 
124
                                if (expr_type.IsReferenceType)
 
125
                                        return new ClassCast (expr, target_type);
 
126
 
 
127
                                return new BoxedCast (expr, target_type);
 
128
                        }
 
129
 
 
130
                        return null;
 
131
                }
 
132
 
 
133
                static Expression ExplicitTypeParameterConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
 
134
                {
 
135
                        var target_tp = target_type as TypeParameterSpec;
 
136
                        if (target_tp != null) {
 
137
                                if (target_tp.TypeArguments != null) {
 
138
                                        foreach (var targ in target_tp.TypeArguments) {
 
139
                                                if (!TypeSpecComparer.Override.IsEqual (source_type, targ))
 
140
                                                        continue;
 
141
 
 
142
                                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
143
                                        }
 
144
                                }
 
145
/*
 
146
                                if (target_tp.Interfaces != null) {
 
147
                                        foreach (TypeSpec iface in target_tp.Interfaces) {
 
148
                                                if (!TypeManager.IsGenericParameter (iface))
 
149
                                                        continue;
 
150
 
 
151
                                                if (TypeManager.IsSubclassOf (source_type, iface))
 
152
                                                        return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
 
153
                                        }
 
154
                                }
 
155
*/
 
156
                                return null;
 
157
                        }
 
158
 
 
159
                        if (target_type.IsInterface)
 
160
                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type, true);
 
161
 
 
162
                        return null;
 
163
                }
 
164
 
 
165
                public static Expression ImplicitReferenceConversion (Expression expr, TypeSpec target_type, bool explicit_cast)
 
166
                {
 
167
                        TypeSpec expr_type = expr.Type;
 
168
 
 
169
                        if (expr_type.Kind == MemberKind.TypeParameter)
 
170
                                return ImplicitTypeParameterConversion (expr, (TypeParameterSpec) expr.Type, target_type);
 
171
 
 
172
                        //
 
173
                        // from the null type to any reference-type.
 
174
                        //
 
175
                        NullLiteral nl = expr as NullLiteral;
 
176
                        if (nl != null) {
 
177
                                return nl.ConvertImplicitly (target_type);
 
178
                        }
 
179
 
 
180
                        if (ImplicitReferenceConversionExists (expr_type, target_type)) {
 
181
                                // 
 
182
                                // Avoid wrapping implicitly convertible reference type
 
183
                                //
 
184
                                if (!explicit_cast)
 
185
                                        return expr;
 
186
 
 
187
                                return EmptyCast.Create (expr, target_type);
 
188
                        }
 
189
 
 
190
                        return null;
 
191
                }
 
192
 
 
193
                //
 
194
                // Implicit reference conversions
 
195
                //
 
196
                public static bool ImplicitReferenceConversionExists (TypeSpec expr_type, TypeSpec target_type)
 
197
                {
 
198
                        // It's here only to speed things up
 
199
                        if (target_type.IsStruct)
 
200
                                return false;
 
201
 
 
202
                        switch (expr_type.Kind) {
 
203
                        case MemberKind.TypeParameter:
 
204
                                return ImplicitTypeParameterConversion (null, (TypeParameterSpec) expr_type, target_type) != null;
 
205
 
 
206
                        case MemberKind.Class:
 
207
                                //
 
208
                                // From any class-type to dynamic (+object to speed up common path)
 
209
                                //
 
210
                                if (target_type.BuiltinType == BuiltinTypeSpec.Type.Object || target_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
 
211
                                        return true;
 
212
 
 
213
                                if (target_type.IsClass) {
 
214
                                        //
 
215
                                        // Identity conversion, including dynamic erasure
 
216
                                        //
 
217
                                        if (TypeSpecComparer.IsEqual (expr_type, target_type))
 
218
                                                return true;
 
219
 
 
220
                                        //
 
221
                                        // From any class-type S to any class-type T, provided S is derived from T
 
222
                                        //
 
223
                                        return TypeSpec.IsBaseClass (expr_type, target_type, true);
 
224
                                }
 
225
 
 
226
                                //
 
227
                                // From any class-type S to any interface-type T, provided S implements T
 
228
                                //
 
229
                                if (target_type.IsInterface)
 
230
                                        return expr_type.ImplementsInterface (target_type, true);
 
231
 
 
232
                                return false;
 
233
 
 
234
                        case MemberKind.ArrayType:
 
235
                                //
 
236
                                // Identity array conversion
 
237
                                //
 
238
                                if (expr_type == target_type)
 
239
                                        return true;
 
240
 
 
241
                                //
 
242
                                // From any array-type to System.Array
 
243
                                //
 
244
                                switch (target_type.BuiltinType) {
 
245
                                case BuiltinTypeSpec.Type.Array:
 
246
                                case BuiltinTypeSpec.Type.Object:
 
247
                                case BuiltinTypeSpec.Type.Dynamic:
 
248
                                        return true;
 
249
                                }
 
250
 
 
251
                                var expr_type_array = (ArrayContainer) expr_type;
 
252
                                var target_type_array = target_type as ArrayContainer;
 
253
 
 
254
                                //
 
255
                                // From an array-type S to an array-type of type T
 
256
                                //
 
257
                                if (target_type_array != null && expr_type_array.Rank == target_type_array.Rank) {
 
258
 
 
259
                                        //
 
260
                                        // Both SE and TE are reference-types. TE check is defered
 
261
                                        // to ImplicitReferenceConversionExists
 
262
                                        //
 
263
                                        TypeSpec expr_element_type = expr_type_array.Element;
 
264
                                        if (!TypeSpec.IsReferenceType (expr_element_type))
 
265
                                                return false;
 
266
 
 
267
                                        //
 
268
                                        // An implicit reference conversion exists from SE to TE
 
269
                                        //
 
270
                                        return ImplicitReferenceConversionExists (expr_element_type, target_type_array.Element);
 
271
                                }
 
272
 
 
273
                                //
 
274
                                // From any array-type to the interfaces it implements
 
275
                                //
 
276
                                if (target_type.IsInterface) {
 
277
                                        if (expr_type.ImplementsInterface (target_type, false))
 
278
                                                return true;
 
279
 
 
280
                                        // from an array-type of type T to IList<T>
 
281
                                        if (ArrayToIList (expr_type_array, target_type, false))
 
282
                                                return true;
 
283
                                }
 
284
 
 
285
                                return false;
 
286
 
 
287
                        case MemberKind.Delegate:
 
288
                                //
 
289
                                // From any delegate-type to System.Delegate (and its base types)
 
290
                                //
 
291
                                switch (target_type.BuiltinType) {
 
292
                                case BuiltinTypeSpec.Type.Delegate:
 
293
                                case BuiltinTypeSpec.Type.MulticastDelegate:
 
294
                                case BuiltinTypeSpec.Type.Object:
 
295
                                case BuiltinTypeSpec.Type.Dynamic:
 
296
                                        return true;
 
297
                                }
 
298
 
 
299
                                //
 
300
                                // Identity conversion, including dynamic erasure
 
301
                                //
 
302
                                if (TypeSpecComparer.IsEqual (expr_type, target_type))
 
303
                                        return true;
 
304
 
 
305
                                //
 
306
                                // From any delegate-type to the interfaces it implements
 
307
                                // From any reference-type to an delegate type if is variance-convertible
 
308
                                //
 
309
                                return expr_type.ImplementsInterface (target_type, false) || TypeSpecComparer.Variant.IsEqual (expr_type, target_type);
 
310
 
 
311
                        case MemberKind.Interface:
 
312
                                //
 
313
                                // Identity conversion, including dynamic erasure
 
314
                                //
 
315
                                if (TypeSpecComparer.IsEqual (expr_type, target_type))
 
316
                                        return true;
 
317
 
 
318
                                //
 
319
                                // From any interface type S to interface-type T
 
320
                                // From any reference-type to an interface if is variance-convertible
 
321
                                //
 
322
                                if (target_type.IsInterface)
 
323
                                        return TypeSpecComparer.Variant.IsEqual (expr_type, target_type) || expr_type.ImplementsInterface (target_type, true);
 
324
 
 
325
                                return target_type.BuiltinType == BuiltinTypeSpec.Type.Object || target_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
 
326
                        }
 
327
 
 
328
                        //
 
329
                        // from the null literal to any reference-type.
 
330
                        //
 
331
                        if (expr_type == InternalType.NullLiteral) {
 
332
                                // Exlude internal compiler types
 
333
                                if (target_type.Kind == MemberKind.InternalCompilerType)
 
334
                                        return target_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
 
335
 
 
336
                                return TypeSpec.IsReferenceType (target_type);
 
337
                        }
 
338
 
 
339
                        return false;
 
340
                }
 
341
 
 
342
                public static Expression ImplicitBoxingConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
 
343
                {
 
344
                        switch (target_type.BuiltinType) {
 
345
                        //
 
346
                        // From any non-nullable-value-type to the type object and dynamic
 
347
                        //
 
348
                        case BuiltinTypeSpec.Type.Object:
 
349
                        case BuiltinTypeSpec.Type.Dynamic:
 
350
                        //
 
351
                        // From any non-nullable-value-type to the type System.ValueType
 
352
                        //
 
353
                        case BuiltinTypeSpec.Type.ValueType:
 
354
                                //
 
355
                                // No ned to check for nullable type as underlying type is always convertible
 
356
                                //
 
357
                                if (!TypeSpec.IsValueType (expr_type))
 
358
                                        return null;
 
359
 
 
360
                                return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
 
361
 
 
362
                        case BuiltinTypeSpec.Type.Enum:
 
363
                                //
 
364
                                // From any enum-type to the type System.Enum.
 
365
                                //
 
366
                                if (expr_type.IsEnum)
 
367
                                        return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
 
368
 
 
369
                                break;
 
370
                        }
 
371
 
 
372
                        //
 
373
                        // From a nullable-type to a reference type, if a boxing conversion exists from
 
374
                        // the underlying type to the reference type
 
375
                        //
 
376
                        if (expr_type.IsNullableType) {
 
377
                                if (!TypeSpec.IsReferenceType (target_type))
 
378
                                        return null;
 
379
 
 
380
                                var res = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
 
381
 
 
382
                                // "cast" underlying type to target type to emit correct InvalidCastException when
 
383
                                // underlying hierarchy changes without recompilation
 
384
                                if (res != null && expr != null)
 
385
                                        res = new UnboxCast (res, target_type);
 
386
 
 
387
                                return res;
 
388
                        }
 
389
 
 
390
                        //
 
391
                        // A value type has a boxing conversion to an interface type I if it has a boxing conversion
 
392
                        // to an interface or delegate type I0 and I0 is variance-convertible to I
 
393
                        //
 
394
                        if (target_type.IsInterface && TypeSpec.IsValueType (expr_type) && expr_type.ImplementsInterface (target_type, true)) {
 
395
                                return expr == null ? EmptyExpression.Null : new BoxedCast (expr, target_type);
 
396
                        }
 
397
 
 
398
                        return null;
 
399
                }
 
400
 
 
401
                public static Expression ImplicitNulableConversion (ResolveContext ec, Expression expr, TypeSpec target_type)
 
402
                {
 
403
                        TypeSpec expr_type = expr.Type;
 
404
 
 
405
                        //
 
406
                        // From null to any nullable type
 
407
                        //
 
408
                        if (expr_type == InternalType.NullLiteral)
 
409
                                return ec == null ? EmptyExpression.Null : Nullable.LiftedNull.Create (target_type, expr.Location);
 
410
 
 
411
                        // S -> T?
 
412
                        TypeSpec t_el = Nullable.NullableInfo.GetUnderlyingType (target_type);
 
413
 
 
414
                        // S? -> T?
 
415
                        if (expr_type.IsNullableType)
 
416
                                expr_type = Nullable.NullableInfo.GetUnderlyingType (expr_type);
 
417
 
 
418
                        //
 
419
                        // Predefined implicit identity or implicit numeric conversion
 
420
                        // has to exist between underlying type S and underlying type T
 
421
                        //
 
422
 
 
423
                        // conversion exists only mode
 
424
                        if (ec == null) {
 
425
                                if (TypeSpecComparer.IsEqual (expr_type, t_el))
 
426
                                        return EmptyExpression.Null;
 
427
 
 
428
                                if (expr is Constant)
 
429
                                        return ((Constant) expr).ConvertImplicitly (t_el);
 
430
 
 
431
                                return ImplicitNumericConversion (null, expr_type, t_el);
 
432
                        }
 
433
 
 
434
                        Expression unwrap;
 
435
                        if (expr_type != expr.Type)
 
436
                                unwrap = Nullable.Unwrap.Create (expr);
 
437
                        else
 
438
                                unwrap = expr;
 
439
 
 
440
                        Expression conv = unwrap;
 
441
                        if (!TypeSpecComparer.IsEqual (expr_type, t_el)) {
 
442
                                if (conv is Constant)
 
443
                                        conv = ((Constant)conv).ConvertImplicitly (t_el);
 
444
                                else
 
445
                                        conv = ImplicitNumericConversion (conv, expr_type, t_el);
 
446
 
 
447
                                if (conv == null)
 
448
                                        return null;
 
449
                        }
 
450
                        
 
451
                        if (expr_type != expr.Type)
 
452
                                return new Nullable.Lifted (conv, unwrap, target_type).Resolve (ec);
 
453
 
 
454
                        return Nullable.Wrap.Create (conv, target_type);
 
455
                }
 
456
 
 
457
                /// <summary>
 
458
                ///   Implicit Numeric Conversions.
 
459
                ///
 
460
                ///   expr is the expression to convert, returns a new expression of type
 
461
                ///   target_type or null if an implicit conversion is not possible.
 
462
                /// </summary>
 
463
                public static Expression ImplicitNumericConversion (Expression expr, TypeSpec target_type)
 
464
                {
 
465
                        return ImplicitNumericConversion (expr, expr.Type, target_type);
 
466
                }
 
467
 
 
468
                static Expression ImplicitNumericConversion (Expression expr, TypeSpec expr_type, TypeSpec target_type)
 
469
                {
 
470
                        switch (expr_type.BuiltinType) {
 
471
                        case BuiltinTypeSpec.Type.SByte:
 
472
                                //
 
473
                                // From sbyte to short, int, long, float, double, decimal
 
474
                                //
 
475
                                switch (target_type.BuiltinType) {
 
476
                                case BuiltinTypeSpec.Type.Int:
 
477
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
 
478
                                case BuiltinTypeSpec.Type.Long:
 
479
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
480
                                case BuiltinTypeSpec.Type.Double:
 
481
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
482
                                case BuiltinTypeSpec.Type.Float:
 
483
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
484
                                case BuiltinTypeSpec.Type.Short:
 
485
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
 
486
                                case BuiltinTypeSpec.Type.Decimal:
 
487
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
488
 
 
489
                                }
 
490
 
 
491
                                break;
 
492
                        case BuiltinTypeSpec.Type.Byte:
 
493
                                //
 
494
                                // From byte to short, ushort, int, uint, long, ulong, float, double, decimal
 
495
                                //
 
496
                                switch (target_type.BuiltinType) {
 
497
                                case BuiltinTypeSpec.Type.Int:
 
498
                                case BuiltinTypeSpec.Type.UInt:
 
499
                                case BuiltinTypeSpec.Type.Short:
 
500
                                case BuiltinTypeSpec.Type.UShort:
 
501
                                        return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
 
502
                                case BuiltinTypeSpec.Type.ULong:
 
503
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
504
                                case BuiltinTypeSpec.Type.Long:
 
505
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
506
                                case BuiltinTypeSpec.Type.Float:
 
507
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
508
                                case BuiltinTypeSpec.Type.Double:
 
509
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
510
                                case BuiltinTypeSpec.Type.Decimal:
 
511
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
512
                                }
 
513
                                break;
 
514
                        case BuiltinTypeSpec.Type.Short:
 
515
                                //
 
516
                                // From short to int, long, float, double, decimal
 
517
                                //
 
518
                                switch (target_type.BuiltinType) {
 
519
                                case BuiltinTypeSpec.Type.Int:
 
520
                                        return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
 
521
                                case BuiltinTypeSpec.Type.Long:
 
522
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
523
                                case BuiltinTypeSpec.Type.Double:
 
524
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
525
                                case BuiltinTypeSpec.Type.Float:
 
526
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
527
                                case BuiltinTypeSpec.Type.Decimal:
 
528
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
529
                                }
 
530
                                break;
 
531
                        case BuiltinTypeSpec.Type.UShort:
 
532
                                //
 
533
                                // From ushort to int, uint, long, ulong, float, double, decimal
 
534
                                //
 
535
                                switch (target_type.BuiltinType) {
 
536
                                case BuiltinTypeSpec.Type.Int:
 
537
                                case BuiltinTypeSpec.Type.UInt:
 
538
                                        return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
 
539
                                case BuiltinTypeSpec.Type.ULong:
 
540
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
541
                                case BuiltinTypeSpec.Type.Long:
 
542
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
543
                                case BuiltinTypeSpec.Type.Double:
 
544
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
545
                                case BuiltinTypeSpec.Type.Float:
 
546
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
547
                                case BuiltinTypeSpec.Type.Decimal:
 
548
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
549
                                }
 
550
                                break;
 
551
                        case BuiltinTypeSpec.Type.Int:
 
552
                                //
 
553
                                // From int to long, float, double, decimal
 
554
                                //
 
555
                                switch (target_type.BuiltinType) {
 
556
                                case BuiltinTypeSpec.Type.Long:
 
557
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
558
                                case BuiltinTypeSpec.Type.Double:
 
559
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
560
                                case BuiltinTypeSpec.Type.Float:
 
561
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
562
                                case BuiltinTypeSpec.Type.Decimal:
 
563
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
564
                                }
 
565
                                break;
 
566
                        case BuiltinTypeSpec.Type.UInt:
 
567
                                //
 
568
                                // From uint to long, ulong, float, double, decimal
 
569
                                //
 
570
                                switch (target_type.BuiltinType) {
 
571
                                case BuiltinTypeSpec.Type.Long:
 
572
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
573
                                case BuiltinTypeSpec.Type.ULong:
 
574
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
575
                                case BuiltinTypeSpec.Type.Double:
 
576
                                        return expr == null ? EmptyExpression.Null : new OpcodeCastDuplex (expr, target_type, OpCodes.Conv_R_Un, OpCodes.Conv_R8);
 
577
                                case BuiltinTypeSpec.Type.Float:
 
578
                                        return expr == null ? EmptyExpression.Null : new OpcodeCastDuplex (expr, target_type, OpCodes.Conv_R_Un, OpCodes.Conv_R4);
 
579
                                case BuiltinTypeSpec.Type.Decimal:
 
580
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
581
                                }
 
582
                                break;
 
583
                        case BuiltinTypeSpec.Type.Long:
 
584
                                //
 
585
                                // From long to float, double, decimal
 
586
                                //
 
587
                                switch (target_type.BuiltinType) {
 
588
                                case BuiltinTypeSpec.Type.Double:
 
589
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
590
                                case BuiltinTypeSpec.Type.Float:
 
591
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
592
                                case BuiltinTypeSpec.Type.Decimal:
 
593
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
594
                                }
 
595
                                break;
 
596
                        case BuiltinTypeSpec.Type.ULong:
 
597
                                //
 
598
                                // From ulong to float, double, decimal
 
599
                                //
 
600
                                switch (target_type.BuiltinType) {
 
601
                                case BuiltinTypeSpec.Type.Double:
 
602
                                        return expr == null ? EmptyExpression.Null : new OpcodeCastDuplex (expr, target_type, OpCodes.Conv_R_Un, OpCodes.Conv_R8);
 
603
                                case BuiltinTypeSpec.Type.Float:
 
604
                                        return expr == null ? EmptyExpression.Null : new OpcodeCastDuplex (expr, target_type, OpCodes.Conv_R_Un, OpCodes.Conv_R4);
 
605
                                case BuiltinTypeSpec.Type.Decimal:
 
606
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
607
                                }
 
608
                                break;
 
609
                        case BuiltinTypeSpec.Type.Char:
 
610
                                //
 
611
                                // From char to ushort, int, uint, long, ulong, float, double, decimal
 
612
                                //
 
613
                                switch (target_type.BuiltinType) {
 
614
                                case BuiltinTypeSpec.Type.UShort:
 
615
                                case BuiltinTypeSpec.Type.Int:
 
616
                                case BuiltinTypeSpec.Type.UInt:
 
617
                                        return expr == null ? EmptyExpression.Null : EmptyCast.Create (expr, target_type);
 
618
                                case BuiltinTypeSpec.Type.ULong:
 
619
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
620
                                case BuiltinTypeSpec.Type.Long:
 
621
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_I8);
 
622
                                case BuiltinTypeSpec.Type.Float:
 
623
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
624
                                case BuiltinTypeSpec.Type.Double:
 
625
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
626
                                case BuiltinTypeSpec.Type.Decimal:
 
627
                                        return expr == null ? EmptyExpression.Null : new OperatorCast (expr, target_type);
 
628
                                }
 
629
                                break;
 
630
                        case BuiltinTypeSpec.Type.Float:
 
631
                                //
 
632
                                // float to double
 
633
                                //
 
634
                                if (target_type.BuiltinType == BuiltinTypeSpec.Type.Double)
 
635
                                        return expr == null ? EmptyExpression.Null : new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
636
                                break;
 
637
                        }
 
638
 
 
639
                        return null;
 
640
                }
 
641
 
 
642
                //
 
643
                // Full version of implicit conversion
 
644
                //
 
645
                public static bool ImplicitConversionExists (ResolveContext ec, Expression expr, TypeSpec target_type)
 
646
                {
 
647
                        if (ImplicitStandardConversionExists (expr, target_type))
 
648
                                return true;
 
649
 
 
650
                        if (expr.Type == InternalType.AnonymousMethod) {
 
651
                                if (!target_type.IsDelegate && !target_type.IsExpressionTreeType)
 
652
                                        return false;
 
653
 
 
654
                                AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
 
655
                                return ame.ImplicitStandardConversionExists (ec, target_type);
 
656
                        }
 
657
                        
 
658
                        if (expr.eclass == ExprClass.MethodGroup) {
 
659
                                if (target_type.IsDelegate && ec.Module.Compiler.Settings.Version != LanguageVersion.ISO_1) {
 
660
                                        MethodGroupExpr mg = expr as MethodGroupExpr;
 
661
                                        if (mg != null)
 
662
                                                return DelegateCreation.ImplicitStandardConversionExists (ec, mg, target_type);
 
663
                                }
 
664
 
 
665
                                return false;
 
666
                        }
 
667
 
 
668
                        // Conversion from __arglist to System.ArgIterator
 
669
                        if (expr.Type == InternalType.Arglist)
 
670
                                return target_type == ec.Module.PredefinedTypes.ArgIterator.TypeSpec;
 
671
 
 
672
                        return ImplicitUserConversion (ec, expr, target_type, Location.Null) != null;
 
673
                }
 
674
 
 
675
                //
 
676
                // Implicit standard conversion (only core conversions are used here)
 
677
                //
 
678
                public static bool ImplicitStandardConversionExists (Expression expr, TypeSpec target_type)
 
679
                {
 
680
                        //
 
681
                        // Identity conversions
 
682
                        // Implicit numeric conversions
 
683
                        // Implicit nullable conversions
 
684
                        // Implicit reference conversions
 
685
                        // Boxing conversions
 
686
                        // Implicit constant expression conversions
 
687
                        // Implicit conversions involving type parameters
 
688
                        //
 
689
 
 
690
                        TypeSpec expr_type = expr.Type;
 
691
 
 
692
                        if (expr_type == target_type)
 
693
                                return true;
 
694
 
 
695
                        if (target_type.IsNullableType)
 
696
                                return ImplicitNulableConversion (null, expr, target_type) != null;
 
697
 
 
698
                        if (ImplicitNumericConversion (null, expr_type, target_type) != null)
 
699
                                return true;
 
700
 
 
701
                        if (ImplicitReferenceConversionExists (expr_type, target_type))
 
702
                                return true;
 
703
 
 
704
                        if (ImplicitBoxingConversion (null, expr_type, target_type) != null)
 
705
                                return true;
 
706
                        
 
707
                        //
 
708
                        // Implicit Constant Expression Conversions
 
709
                        //
 
710
                        if (expr is IntConstant){
 
711
                                int value = ((IntConstant) expr).Value;
 
712
                                switch (target_type.BuiltinType) {
 
713
                                case BuiltinTypeSpec.Type.SByte:
 
714
                                        if (value >= SByte.MinValue && value <= SByte.MaxValue)
 
715
                                                return true;
 
716
                                        break;
 
717
                                case BuiltinTypeSpec.Type.Byte:
 
718
                                        if (value >= 0 && value <= Byte.MaxValue)
 
719
                                                return true;
 
720
                                        break;
 
721
                                case BuiltinTypeSpec.Type.Short:
 
722
                                        if (value >= Int16.MinValue && value <= Int16.MaxValue)
 
723
                                                return true;
 
724
                                        break;
 
725
                                case BuiltinTypeSpec.Type.UShort:
 
726
                                        if (value >= UInt16.MinValue && value <= UInt16.MaxValue)
 
727
                                                return true;
 
728
                                        break;
 
729
                                case BuiltinTypeSpec.Type.UInt:
 
730
                                        if (value >= 0)
 
731
                                                return true;
 
732
                                        break;
 
733
                                case BuiltinTypeSpec.Type.ULong:
 
734
                                         //
 
735
                                         // we can optimize this case: a positive int32
 
736
                                         // always fits on a uint64.  But we need an opcode
 
737
                                         // to do it.
 
738
                                         //
 
739
                                        if (value >= 0)
 
740
                                                return true;
 
741
 
 
742
                                        break;
 
743
                                }
 
744
                        }
 
745
 
 
746
                        if (expr is LongConstant && target_type.BuiltinType == BuiltinTypeSpec.Type.ULong){
 
747
                                //
 
748
                                // Try the implicit constant expression conversion
 
749
                                // from long to ulong, instead of a nice routine,
 
750
                                // we just inline it
 
751
                                //
 
752
                                long v = ((LongConstant) expr).Value;
 
753
                                if (v >= 0)
 
754
                                        return true;
 
755
                        }
 
756
 
 
757
                        if (expr is IntegralConstant && target_type.IsEnum) {
 
758
                                var i = (IntegralConstant) expr;
 
759
                                //
 
760
                                // LAMESPEC: csc allows any constant like 0 values to be converted, including const float f = 0.0
 
761
                                //
 
762
                                // An implicit enumeration conversion permits the decimal-integer-literal 0
 
763
                                // to be converted to any enum-type and to any nullable-type whose underlying
 
764
                                // type is an enum-type
 
765
                                //
 
766
                                return i.IsZeroInteger;
 
767
                        }
 
768
 
 
769
                        // Implicit dynamic conversion
 
770
                        if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
 
771
                                switch (target_type.Kind) {
 
772
                                case MemberKind.ArrayType:
 
773
                                case MemberKind.Class:
 
774
                                case MemberKind.Struct:
 
775
                                case MemberKind.Delegate:
 
776
                                case MemberKind.Enum:
 
777
                                case MemberKind.Interface:
 
778
                                case MemberKind.TypeParameter:
 
779
                                        return true;
 
780
                                }
 
781
 
 
782
                                // dynamic to __arglist
 
783
                                if (target_type == InternalType.Arglist)
 
784
                                        return true;
 
785
 
 
786
                                return false;
 
787
                        }
 
788
 
 
789
                        //
 
790
                        // In an unsafe context implicit conversions is extended to include
 
791
                        //
 
792
                        // From any pointer-type to the type void*
 
793
                        // From the null literal to any pointer-type.
 
794
                        //
 
795
                        // LAMESPEC: The specification claims this conversion is allowed in implicit conversion but
 
796
                        // in reality implicit standard conversion uses it
 
797
                        //
 
798
                        if (target_type.IsPointer && expr.Type.IsPointer && ((PointerContainer) target_type).Element.Kind == MemberKind.Void)
 
799
                                return true;
 
800
 
 
801
                        //
 
802
                        // Struct identity conversion, including dynamic erasure
 
803
                        //
 
804
                        if (expr_type.IsStruct && TypeSpecComparer.IsEqual (expr_type, target_type))
 
805
                                return true;
 
806
 
 
807
                        return false;
 
808
                }
 
809
 
 
810
                /// <summary>
 
811
                ///  Finds "most encompassed type" according to the spec (13.4.2)
 
812
                ///  amongst the methods in the MethodGroupExpr
 
813
                /// </summary>
 
814
                public static TypeSpec FindMostEncompassedType (IEnumerable<TypeSpec> types)
 
815
                {
 
816
                        TypeSpec best = null;
 
817
                        EmptyExpression expr;
 
818
 
 
819
                        foreach (TypeSpec t in types) {
 
820
                                if (best == null) {
 
821
                                        best = t;
 
822
                                        continue;
 
823
                                }
 
824
 
 
825
                                expr = new EmptyExpression (t);
 
826
                                if (ImplicitStandardConversionExists (expr, best))
 
827
                                        best = t;
 
828
                        }
 
829
 
 
830
                        expr = new EmptyExpression (best);
 
831
                        foreach (TypeSpec t in types) {
 
832
                                if (best == t)
 
833
                                        continue;
 
834
                                if (!ImplicitStandardConversionExists (expr, t)) {
 
835
                                        best = null;
 
836
                                        break;
 
837
                                }
 
838
                        }
 
839
 
 
840
                        return best;
 
841
                }
 
842
 
 
843
                //
 
844
                // Finds the most encompassing type (type into which all other
 
845
                // types can convert to) amongst the types in the given set
 
846
                //
 
847
                static TypeSpec FindMostEncompassingType (IList<TypeSpec> types)
 
848
                {
 
849
                        if (types.Count == 0)
 
850
                                return null;
 
851
 
 
852
                        if (types.Count == 1)
 
853
                                return types [0];
 
854
 
 
855
                        TypeSpec best = null;
 
856
                        for (int i = 0; i < types.Count; ++i) {
 
857
                                int ii = 0;
 
858
                                for (; ii < types.Count; ++ii) {
 
859
                                        if (ii == i)
 
860
                                                continue;
 
861
 
 
862
                                        var expr = new EmptyExpression (types[ii]);
 
863
                                        if (!ImplicitStandardConversionExists (expr, types [i])) {
 
864
                                                ii = 0;
 
865
                                                break;
 
866
                                        }
 
867
                                }
 
868
 
 
869
                                if (ii == 0)
 
870
                                        continue;
 
871
 
 
872
                                if (best == null) {
 
873
                                        best = types[i];
 
874
                                        continue;
 
875
                                }
 
876
 
 
877
                                // Indicates multiple best types
 
878
                                return InternalType.FakeInternalType;
 
879
                        }
 
880
 
 
881
                        return best;
 
882
                }
 
883
 
 
884
                //
 
885
                // Finds the most specific source Sx according to the rules of the spec (13.4.4)
 
886
                // by making use of FindMostEncomp* methods. Applies the correct rules separately
 
887
                // for explicit and implicit conversion operators.
 
888
                //
 
889
                static TypeSpec FindMostSpecificSource (List<MethodSpec> list, TypeSpec sourceType, Expression source, bool apply_explicit_conv_rules)
 
890
                {
 
891
                        var src_types_set = new TypeSpec [list.Count];
 
892
 
 
893
                        //
 
894
                        // Try exact match first, if any operator converts from S then Sx = S
 
895
                        //
 
896
                        for (int i = 0; i < src_types_set.Length; ++i) {
 
897
                                TypeSpec param_type = list [i].Parameters.Types [0];
 
898
 
 
899
                                if (param_type == sourceType)
 
900
                                        return param_type;
 
901
 
 
902
                                src_types_set [i] = param_type;
 
903
                        }
 
904
 
 
905
                        //
 
906
                        // Explicit Conv rules
 
907
                        //
 
908
                        if (apply_explicit_conv_rules) {
 
909
                                var candidate_set = new List<TypeSpec> ();
 
910
 
 
911
                                foreach (TypeSpec param_type in src_types_set){
 
912
                                        if (ImplicitStandardConversionExists (source, param_type))
 
913
                                                candidate_set.Add (param_type);
 
914
                                }
 
915
 
 
916
                                if (candidate_set.Count != 0)
 
917
                                        return FindMostEncompassedType (candidate_set);
 
918
                        }
 
919
 
 
920
                        //
 
921
                        // Final case
 
922
                        //
 
923
                        if (apply_explicit_conv_rules)
 
924
                                return FindMostEncompassingType (src_types_set);
 
925
                        else
 
926
                                return FindMostEncompassedType (src_types_set);
 
927
                }
 
928
 
 
929
                /// <summary>
 
930
                ///  Finds the most specific target Tx according to section 13.4.4
 
931
                /// </summary>
 
932
                static public TypeSpec FindMostSpecificTarget (IList<MethodSpec> list,
 
933
                                                           TypeSpec target, bool apply_explicit_conv_rules)
 
934
                {
 
935
                        var tgt_types_set = new List<TypeSpec> ();
 
936
 
 
937
                        //
 
938
                        // If any operator converts to T then Tx = T
 
939
                        //
 
940
                        foreach (var mi in list){
 
941
                                TypeSpec ret_type = mi.ReturnType;
 
942
                                if (ret_type == target)
 
943
                                        return ret_type;
 
944
 
 
945
                                tgt_types_set.Add (ret_type);
 
946
                        }
 
947
 
 
948
                        //
 
949
                        // Explicit conv rules
 
950
                        //
 
951
                        if (apply_explicit_conv_rules) {
 
952
                                var candidate_set = new List<TypeSpec> ();
 
953
 
 
954
                                foreach (TypeSpec ret_type in tgt_types_set) {
 
955
                                        var expr = new EmptyExpression (ret_type);
 
956
 
 
957
                                        if (ImplicitStandardConversionExists (expr, target))
 
958
                                                candidate_set.Add (ret_type);
 
959
                                }
 
960
 
 
961
                                if (candidate_set.Count != 0)
 
962
                                        return FindMostEncompassingType (candidate_set);
 
963
                        }
 
964
 
 
965
                        //
 
966
                        // Okay, final case !
 
967
                        //
 
968
                        if (apply_explicit_conv_rules)
 
969
                                return FindMostEncompassedType (tgt_types_set);
 
970
                        else
 
971
                                return FindMostEncompassingType (tgt_types_set);
 
972
                }
 
973
 
 
974
                /// <summary>
 
975
                ///  User-defined Implicit conversions
 
976
                /// </summary>
 
977
                static public Expression ImplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
 
978
                {
 
979
                        return UserDefinedConversion (ec, source, target, true, loc);
 
980
                }
 
981
 
 
982
                /// <summary>
 
983
                ///  User-defined Explicit conversions
 
984
                /// </summary>
 
985
                static Expression ExplicitUserConversion (ResolveContext ec, Expression source, TypeSpec target, Location loc)
 
986
                {
 
987
                        return UserDefinedConversion (ec, source, target, false, loc);
 
988
                }
 
989
 
 
990
                static void FindApplicableUserDefinedConversionOperators (IList<MemberSpec> operators, Expression source, TypeSpec target, bool implicitOnly, ref List<MethodSpec> candidates)
 
991
                {
 
992
                        if (source.Type.IsInterface) {
 
993
                                // Neither A nor B are interface-types
 
994
                                return;
 
995
                        }
 
996
 
 
997
                        // For a conversion operator to be applicable, it must be possible
 
998
                        // to perform a standard conversion from the source type to
 
999
                        // the operand type of the operator, and it must be possible
 
1000
                        // to perform a standard conversion from the result type of
 
1001
                        // the operator to the target type.
 
1002
 
 
1003
                        Expression texpr = null;
 
1004
 
 
1005
                        foreach (MethodSpec op in operators) {
 
1006
                                
 
1007
                                // Can be null because MemberCache.GetUserOperator does not resize the array
 
1008
                                if (op == null)
 
1009
                                        continue;
 
1010
 
 
1011
                                var t = op.Parameters.Types[0];
 
1012
                                if (source.Type != t && !ImplicitStandardConversionExists (source, t)) {
 
1013
                                        if (implicitOnly)
 
1014
                                                continue;
 
1015
 
 
1016
                                        if (!ImplicitStandardConversionExists (new EmptyExpression (t), source.Type))
 
1017
                                                continue;
 
1018
                                }
 
1019
 
 
1020
                                t = op.ReturnType;
 
1021
 
 
1022
                                if (t.IsInterface)
 
1023
                                        continue;
 
1024
 
 
1025
                                if (target != t) {
 
1026
                                        if (t.IsNullableType)
 
1027
                                                t = Nullable.NullableInfo.GetUnderlyingType (t);
 
1028
 
 
1029
                                        if (!ImplicitStandardConversionExists (new EmptyExpression (t), target)) {
 
1030
                                                if (implicitOnly)
 
1031
                                                        continue;
 
1032
 
 
1033
                                                if (texpr == null)
 
1034
                                                        texpr = new EmptyExpression (target);
 
1035
 
 
1036
                                                if (!ImplicitStandardConversionExists (texpr, t))
 
1037
                                                        continue;
 
1038
                                        }
 
1039
                                }
 
1040
 
 
1041
                                if (candidates == null)
 
1042
                                        candidates = new List<MethodSpec> ();
 
1043
 
 
1044
                                candidates.Add (op);
 
1045
                        }
 
1046
                }
 
1047
 
 
1048
                //
 
1049
                // User-defined conversions
 
1050
                //
 
1051
                static Expression UserDefinedConversion (ResolveContext ec, Expression source, TypeSpec target, bool implicitOnly, Location loc)
 
1052
                {
 
1053
                        List<MethodSpec> candidates = null;
 
1054
 
 
1055
                        //
 
1056
                        // If S or T are nullable types, source_type and target_type are their underlying types
 
1057
                        // otherwise source_type and target_type are equal to S and T respectively.
 
1058
                        //
 
1059
                        TypeSpec source_type = source.Type;
 
1060
                        TypeSpec target_type = target;
 
1061
                        Expression source_type_expr;
 
1062
 
 
1063
                        if (source_type.IsNullableType) {
 
1064
                                // No implicit conversion S? -> T for non-reference types
 
1065
                                if (implicitOnly && !TypeSpec.IsReferenceType (target_type) && !target_type.IsNullableType)
 
1066
                                        return null;
 
1067
 
 
1068
                                source_type_expr = Nullable.Unwrap.Create (source);
 
1069
                                source_type = source_type_expr.Type;
 
1070
                        } else {
 
1071
                                source_type_expr = source;
 
1072
                        }
 
1073
 
 
1074
                        if (target_type.IsNullableType)
 
1075
                                target_type = Nullable.NullableInfo.GetUnderlyingType (target_type);
 
1076
 
 
1077
                        // Only these containers can contain a user defined implicit or explicit operators
 
1078
                        const MemberKind user_conversion_kinds = MemberKind.Class | MemberKind.Struct | MemberKind.TypeParameter;
 
1079
 
 
1080
                        if ((source_type.Kind & user_conversion_kinds) != 0 && source_type.BuiltinType != BuiltinTypeSpec.Type.Decimal) {
 
1081
                                bool declared_only = source_type.IsStruct;
 
1082
 
 
1083
                                var operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Implicit, declared_only);
 
1084
                                if (operators != null) {
 
1085
                                        FindApplicableUserDefinedConversionOperators (operators, source_type_expr, target_type, implicitOnly, ref candidates);
 
1086
                                }
 
1087
 
 
1088
                                if (!implicitOnly) {
 
1089
                                        operators = MemberCache.GetUserOperator (source_type, Operator.OpType.Explicit, declared_only);
 
1090
                                        if (operators != null) {
 
1091
                                                FindApplicableUserDefinedConversionOperators (operators, source_type_expr, target_type, false, ref candidates);
 
1092
                                        }
 
1093
                                }
 
1094
                        }
 
1095
 
 
1096
                        if ((target.Kind & user_conversion_kinds) != 0 && target_type.BuiltinType != BuiltinTypeSpec.Type.Decimal) {
 
1097
                                bool declared_only = target.IsStruct || implicitOnly;
 
1098
 
 
1099
                                var operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Implicit, declared_only);
 
1100
                                if (operators != null) {
 
1101
                                        FindApplicableUserDefinedConversionOperators (operators, source_type_expr, target_type, implicitOnly, ref candidates);
 
1102
                                }
 
1103
 
 
1104
                                if (!implicitOnly) {
 
1105
                                        operators = MemberCache.GetUserOperator (target_type, Operator.OpType.Explicit, declared_only);
 
1106
                                        if (operators != null) {
 
1107
                                                FindApplicableUserDefinedConversionOperators (operators, source_type_expr, target_type, false, ref candidates);
 
1108
                                        }
 
1109
                                }
 
1110
                        }
 
1111
 
 
1112
                        if (candidates == null)
 
1113
                                return null;
 
1114
 
 
1115
                        //
 
1116
                        // Find the most specific conversion operator
 
1117
                        //
 
1118
                        MethodSpec most_specific_operator;
 
1119
                        TypeSpec s_x, t_x;
 
1120
                        if (candidates.Count == 1) {
 
1121
                                most_specific_operator = candidates[0];
 
1122
                                s_x = most_specific_operator.Parameters.Types[0];
 
1123
                                t_x = most_specific_operator.ReturnType;
 
1124
                        } else {
 
1125
                                //
 
1126
                                // Pass original source type to find the best match against input type and
 
1127
                                // not the unwrapped expression
 
1128
                                //
 
1129
                                s_x = FindMostSpecificSource (candidates, source.Type, source_type_expr, !implicitOnly);
 
1130
                                if (s_x == null)
 
1131
                                        return null;
 
1132
 
 
1133
                                t_x = FindMostSpecificTarget (candidates, target, !implicitOnly);
 
1134
                                if (t_x == null)
 
1135
                                        return null;
 
1136
 
 
1137
                                most_specific_operator = null;
 
1138
                                for (int i = 0; i < candidates.Count; ++i) {
 
1139
                                        if (candidates[i].ReturnType == t_x && candidates[i].Parameters.Types[0] == s_x) {
 
1140
                                                most_specific_operator = candidates[i];
 
1141
                                                break;
 
1142
                                        }
 
1143
                                }
 
1144
 
 
1145
                                if (most_specific_operator == null) {
 
1146
                                        MethodSpec ambig_arg = null;
 
1147
                                        foreach (var candidate in candidates) {
 
1148
                                                if (candidate.ReturnType == t_x)
 
1149
                                                        most_specific_operator = candidate;
 
1150
                                                else if (candidate.Parameters.Types[0] == s_x)
 
1151
                                                        ambig_arg = candidate;
 
1152
                                        }
 
1153
 
 
1154
                                        ec.Report.Error (457, loc,
 
1155
                                                "Ambiguous user defined operators `{0}' and `{1}' when converting from `{2}' to `{3}'",
 
1156
                                                ambig_arg.GetSignatureForError (), most_specific_operator.GetSignatureForError (),
 
1157
                                                source.Type.GetSignatureForError (), target.GetSignatureForError ());
 
1158
 
 
1159
                                        return ErrorExpression.Instance;
 
1160
                                }
 
1161
                        }
 
1162
 
 
1163
                        //
 
1164
                        // Convert input type when it's different to best operator argument
 
1165
                        //
 
1166
                        if (s_x != source_type) {
 
1167
                                var c = source as Constant;
 
1168
                                if (c != null) {
 
1169
                                        source = c.TryReduce (ec, s_x, loc);
 
1170
                                } else {
 
1171
                                        source = implicitOnly ?
 
1172
                                                ImplicitConversionStandard (ec, source_type_expr, s_x, loc) :
 
1173
                                                ExplicitConversionStandard (ec, source_type_expr, s_x, loc);
 
1174
                                }
 
1175
                        } else {
 
1176
                                source = source_type_expr;
 
1177
                        }
 
1178
 
 
1179
                        source = new UserCast (most_specific_operator, source, loc).Resolve (ec);
 
1180
 
 
1181
                        //
 
1182
                        // Convert result type when it's different to best operator return type
 
1183
                        //
 
1184
                        if (t_x != target_type) {
 
1185
                                //
 
1186
                                // User operator is of T?, no need to lift it
 
1187
                                //
 
1188
                                if (t_x == target && t_x.IsNullableType)
 
1189
                                        return source;
 
1190
 
 
1191
                                source = implicitOnly ?
 
1192
                                        ImplicitConversionStandard (ec, source, target_type, loc) :
 
1193
                                        ExplicitConversionStandard (ec, source, target_type, loc);
 
1194
 
 
1195
                                if (source == null)
 
1196
                                        return null;
 
1197
                        }
 
1198
 
 
1199
                        //
 
1200
                        // Source expression is of nullable type, lift the result in the case it's null and
 
1201
                        // not nullable/lifted user operator is used
 
1202
                        //
 
1203
                        if (source_type_expr is Nullable.Unwrap && !s_x.IsNullableType && (TypeSpec.IsReferenceType (target) || target_type != target))
 
1204
                                source = new Nullable.Lifted (source, source_type_expr, target).Resolve (ec);
 
1205
                        else if (target_type != target)
 
1206
                                source = Nullable.Wrap.Create (source, target);
 
1207
 
 
1208
                        return source;
 
1209
                }
 
1210
 
 
1211
                /// <summary>
 
1212
                ///   Converts implicitly the resolved expression `expr' into the
 
1213
                ///   `target_type'.  It returns a new expression that can be used
 
1214
                ///   in a context that expects a `target_type'.
 
1215
                /// </summary>
 
1216
                static public Expression ImplicitConversion (ResolveContext ec, Expression expr,
 
1217
                                                             TypeSpec target_type, Location loc)
 
1218
                {
 
1219
                        Expression e;
 
1220
 
 
1221
                        if (target_type == null)
 
1222
                                throw new Exception ("Target type is null");
 
1223
 
 
1224
                        e = ImplicitConversionStandard (ec, expr, target_type, loc);
 
1225
                        if (e != null)
 
1226
                                return e;
 
1227
 
 
1228
                        e = ImplicitUserConversion (ec, expr, target_type, loc);
 
1229
                        if (e != null)
 
1230
                                return e;
 
1231
 
 
1232
                        return null;
 
1233
                }
 
1234
 
 
1235
 
 
1236
                /// <summary>
 
1237
                ///   Attempts to apply the `Standard Implicit
 
1238
                ///   Conversion' rules to the expression `expr' into
 
1239
                ///   the `target_type'.  It returns a new expression
 
1240
                ///   that can be used in a context that expects a
 
1241
                ///   `target_type'.
 
1242
                ///
 
1243
                ///   This is different from `ImplicitConversion' in that the
 
1244
                ///   user defined implicit conversions are excluded.
 
1245
                /// </summary>
 
1246
                static public Expression ImplicitConversionStandard (ResolveContext ec, Expression expr,
 
1247
                                                                     TypeSpec target_type, Location loc)
 
1248
                {
 
1249
                        return ImplicitConversionStandard (ec, expr, target_type, loc, false);
 
1250
                }
 
1251
 
 
1252
                static Expression ImplicitConversionStandard (ResolveContext ec, Expression expr, TypeSpec target_type, Location loc, bool explicit_cast)
 
1253
                {
 
1254
                        if (expr.eclass == ExprClass.MethodGroup){
 
1255
                                if (!TypeManager.IsDelegateType (target_type)){
 
1256
                                        return null;
 
1257
                                }
 
1258
 
 
1259
                                //
 
1260
                                // Only allow anonymous method conversions on post ISO_1
 
1261
                                //
 
1262
                                if (ec.Module.Compiler.Settings.Version != LanguageVersion.ISO_1){
 
1263
                                        MethodGroupExpr mg = expr as MethodGroupExpr;
 
1264
                                        if (mg != null)
 
1265
                                                return ImplicitDelegateCreation.Create (
 
1266
                                                        ec, mg, target_type, loc);
 
1267
                                }
 
1268
                        }
 
1269
 
 
1270
                        TypeSpec expr_type = expr.Type;
 
1271
                        Expression e;
 
1272
 
 
1273
                        if (expr_type == target_type) {
 
1274
                                if (expr_type != InternalType.NullLiteral && expr_type != InternalType.AnonymousMethod)
 
1275
                                        return expr;
 
1276
                                return null;
 
1277
                        }
 
1278
 
 
1279
                        if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
 
1280
                                switch (target_type.Kind) {
 
1281
                                case MemberKind.ArrayType:
 
1282
                                case MemberKind.Class:
 
1283
                                        if (target_type.BuiltinType == BuiltinTypeSpec.Type.Object)
 
1284
                                                return EmptyCast.Create (expr, target_type);
 
1285
 
 
1286
                                        goto case MemberKind.Struct;
 
1287
                                case MemberKind.Struct:
 
1288
                                case MemberKind.Delegate:
 
1289
                                case MemberKind.Enum:
 
1290
                                case MemberKind.Interface:
 
1291
                                case MemberKind.TypeParameter:
 
1292
                                        Arguments args = new Arguments (1);
 
1293
                                        args.Add (new Argument (expr));
 
1294
                                        return new DynamicConversion (target_type, explicit_cast ? CSharpBinderFlags.ConvertExplicit : 0, args, loc).Resolve (ec);
 
1295
                                }
 
1296
 
 
1297
                                return null;
 
1298
                        }
 
1299
 
 
1300
                        if (target_type.IsNullableType)
 
1301
                                return ImplicitNulableConversion (ec, expr, target_type);
 
1302
 
 
1303
                        //
 
1304
                        // Attempt to do the implicit constant expression conversions
 
1305
                        //
 
1306
                        Constant c = expr as Constant;
 
1307
                        if (c != null) {
 
1308
                                try {
 
1309
                                        c = c.ConvertImplicitly (target_type);
 
1310
                                } catch {
 
1311
                                        Console.WriteLine ("Conversion error happened in line {0}", loc);
 
1312
                                        throw;
 
1313
                                }
 
1314
                                if (c != null)
 
1315
                                        return c;
 
1316
                        }
 
1317
 
 
1318
                        e = ImplicitNumericConversion (expr, expr_type, target_type);
 
1319
                        if (e != null)
 
1320
                                return e;
 
1321
 
 
1322
                        e = ImplicitReferenceConversion (expr, target_type, explicit_cast);
 
1323
                        if (e != null)
 
1324
                                return e;
 
1325
 
 
1326
                        e = ImplicitBoxingConversion (expr, expr_type, target_type);
 
1327
                        if (e != null)
 
1328
                                return e;
 
1329
 
 
1330
                        if (expr is IntegralConstant && TypeManager.IsEnumType (target_type)){
 
1331
                                var i = (IntegralConstant) expr;
 
1332
                                //
 
1333
                                // LAMESPEC: csc allows any constant like 0 values to be converted, including const float f = 0.0
 
1334
                                //
 
1335
                                // An implicit enumeration conversion permits the decimal-integer-literal 0
 
1336
                                // to be converted to any enum-type and to any nullable-type whose underlying
 
1337
                                // type is an enum-type
 
1338
                                //
 
1339
                                if (i.IsZeroInteger) {
 
1340
                                        // Recreate 0 literal to remove any collected conversions
 
1341
                                        return new EnumConstant (new IntLiteral (ec.BuiltinTypes, 0, i.Location), target_type);
 
1342
                                }
 
1343
                        }
 
1344
 
 
1345
                        if (ec.IsUnsafe) {
 
1346
                                var target_pc = target_type as PointerContainer;
 
1347
                                if (target_pc != null) {
 
1348
                                        if (expr_type.IsPointer) {
 
1349
                                                //
 
1350
                                                // Pointer types are same when they have same element types
 
1351
                                                //
 
1352
                                                if (expr_type == target_pc)
 
1353
                                                        return expr;
 
1354
 
 
1355
                                                if (target_pc.Element.Kind == MemberKind.Void)
 
1356
                                                        return EmptyCast.Create (expr, target_type);
 
1357
 
 
1358
                                                //return null;
 
1359
                                        }
 
1360
 
 
1361
                                        if (expr_type == InternalType.NullLiteral)
 
1362
                                                return new NullPointer (target_type, loc);
 
1363
                                }
 
1364
                        }
 
1365
 
 
1366
                        if (expr_type == InternalType.AnonymousMethod){
 
1367
                                AnonymousMethodExpression ame = (AnonymousMethodExpression) expr;
 
1368
                                Expression am = ame.Compatible (ec, target_type);
 
1369
                                if (am != null)
 
1370
                                        return am.Resolve (ec);
 
1371
                        }
 
1372
 
 
1373
                        if (expr_type == InternalType.Arglist && target_type == ec.Module.PredefinedTypes.ArgIterator.TypeSpec)
 
1374
                                return expr;
 
1375
 
 
1376
                        //
 
1377
                        // dynamic erasure conversion on value types
 
1378
                        //
 
1379
                        if (expr_type.IsStruct && TypeSpecComparer.IsEqual (expr_type, target_type))
 
1380
                                return expr_type == target_type ? expr : EmptyCast.Create (expr, target_type);
 
1381
 
 
1382
                        return null;
 
1383
                }
 
1384
 
 
1385
                /// <summary>
 
1386
                ///   Attempts to implicitly convert `source' into `target_type', using
 
1387
                ///   ImplicitConversion.  If there is no implicit conversion, then
 
1388
                ///   an error is signaled
 
1389
                /// </summary>
 
1390
                static public Expression ImplicitConversionRequired (ResolveContext ec, Expression source,
 
1391
                                                                     TypeSpec target_type, Location loc)
 
1392
                {
 
1393
                        Expression e = ImplicitConversion (ec, source, target_type, loc);
 
1394
                        if (e != null)
 
1395
                                return e;
 
1396
 
 
1397
                        source.Error_ValueCannotBeConverted (ec, loc, target_type, false);
 
1398
                        return null;
 
1399
                }
 
1400
 
 
1401
                /// <summary>
 
1402
                ///   Performs the explicit numeric conversions
 
1403
                ///
 
1404
                /// There are a few conversions that are not part of the C# standard,
 
1405
                /// they were interim hacks in the C# compiler that were supposed to
 
1406
                /// become explicit operators in the UIntPtr class and IntPtr class,
 
1407
                /// but for historical reasons it did not happen, so the C# compiler
 
1408
                /// ended up with these special hacks.
 
1409
                ///
 
1410
                /// See bug 59800 for details.
 
1411
                ///
 
1412
                /// The conversion are:
 
1413
                ///   UIntPtr->SByte
 
1414
                ///   UIntPtr->Int16
 
1415
                ///   UIntPtr->Int32
 
1416
                ///   IntPtr->UInt64
 
1417
                ///   UInt64->IntPtr
 
1418
                ///   SByte->UIntPtr
 
1419
                ///   Int16->UIntPtr
 
1420
                ///
 
1421
                /// </summary>
 
1422
                public static Expression ExplicitNumericConversion (ResolveContext rc, Expression expr, TypeSpec target_type)
 
1423
                {
 
1424
                        // Not all predefined explicit numeric conversion are
 
1425
                        // defined here, for some of them (mostly IntPtr/UIntPtr) we
 
1426
                        // defer to user-operator handling which is now perfect but
 
1427
                        // works for now
 
1428
                        //
 
1429
                        // LAMESPEC: Undocumented IntPtr/UIntPtr conversions
 
1430
                        // IntPtr -> uint uses int
 
1431
                        // UIntPtr -> long uses ulong
 
1432
                        //
 
1433
 
 
1434
                        switch (expr.Type.BuiltinType) {
 
1435
                        case BuiltinTypeSpec.Type.SByte:
 
1436
                                //
 
1437
                                // From sbyte to byte, ushort, uint, ulong, char, uintptr
 
1438
                                //
 
1439
                                switch (target_type.BuiltinType) {
 
1440
                                case BuiltinTypeSpec.Type.Byte:
 
1441
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I1_U1);
 
1442
                                case BuiltinTypeSpec.Type.UShort:
 
1443
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I1_U2);
 
1444
                                case BuiltinTypeSpec.Type.UInt:
 
1445
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I1_U4);
 
1446
                                case BuiltinTypeSpec.Type.ULong:
 
1447
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I1_U8);
 
1448
                                case BuiltinTypeSpec.Type.Char:
 
1449
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I1_CH);
 
1450
 
 
1451
                                // One of the built-in conversions that belonged in the class library
 
1452
                                case BuiltinTypeSpec.Type.UIntPtr:
 
1453
                                        return new OperatorCast (new ConvCast (expr, rc.BuiltinTypes.ULong, ConvCast.Mode.I1_U8), target_type, target_type, true);
 
1454
                                }
 
1455
                                break;
 
1456
                        case BuiltinTypeSpec.Type.Byte:
 
1457
                                //
 
1458
                                // From byte to sbyte and char
 
1459
                                //
 
1460
                                switch (target_type.BuiltinType) {
 
1461
                                case BuiltinTypeSpec.Type.SByte:
 
1462
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U1_I1);
 
1463
                                case BuiltinTypeSpec.Type.Char:
 
1464
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U1_CH);
 
1465
                                }
 
1466
                                break;
 
1467
                        case BuiltinTypeSpec.Type.Short:
 
1468
                                //
 
1469
                                // From short to sbyte, byte, ushort, uint, ulong, char, uintptr
 
1470
                                //
 
1471
                                switch (target_type.BuiltinType) {
 
1472
                                case BuiltinTypeSpec.Type.SByte:
 
1473
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_I1);
 
1474
                                case BuiltinTypeSpec.Type.Byte:
 
1475
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_U1);
 
1476
                                case BuiltinTypeSpec.Type.UShort:
 
1477
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_U2);
 
1478
                                case BuiltinTypeSpec.Type.UInt:
 
1479
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_U4);
 
1480
                                case BuiltinTypeSpec.Type.ULong:
 
1481
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_U8);
 
1482
                                case BuiltinTypeSpec.Type.Char:
 
1483
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I2_CH);
 
1484
 
 
1485
                                // One of the built-in conversions that belonged in the class library
 
1486
                                case BuiltinTypeSpec.Type.UIntPtr:
 
1487
                                        return new OperatorCast (new ConvCast (expr, rc.BuiltinTypes.ULong, ConvCast.Mode.I2_U8), target_type, target_type, true);
 
1488
                                }
 
1489
                                break;
 
1490
                        case BuiltinTypeSpec.Type.UShort:
 
1491
                                //
 
1492
                                // From ushort to sbyte, byte, short, char
 
1493
                                //
 
1494
                                switch (target_type.BuiltinType) {
 
1495
                                case BuiltinTypeSpec.Type.SByte:
 
1496
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U2_I1);
 
1497
                                case BuiltinTypeSpec.Type.Byte:
 
1498
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U2_U1);
 
1499
                                case BuiltinTypeSpec.Type.Short:
 
1500
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U2_I2);
 
1501
                                case BuiltinTypeSpec.Type.Char:
 
1502
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U2_CH);
 
1503
                                }
 
1504
                                break;
 
1505
                        case BuiltinTypeSpec.Type.Int:
 
1506
                                //
 
1507
                                // From int to sbyte, byte, short, ushort, uint, ulong, char, uintptr
 
1508
                                //
 
1509
                                switch (target_type.BuiltinType) {
 
1510
                                case BuiltinTypeSpec.Type.SByte:
 
1511
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_I1);
 
1512
                                case BuiltinTypeSpec.Type.Byte:
 
1513
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_U1);
 
1514
                                case BuiltinTypeSpec.Type.Short:
 
1515
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_I2);
 
1516
                                case BuiltinTypeSpec.Type.UShort:
 
1517
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_U2);
 
1518
                                case BuiltinTypeSpec.Type.UInt:
 
1519
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_U4);
 
1520
                                case BuiltinTypeSpec.Type.ULong:
 
1521
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_U8);
 
1522
                                case BuiltinTypeSpec.Type.Char:
 
1523
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I4_CH);
 
1524
 
 
1525
                                // One of the built-in conversions that belonged in the class library
 
1526
                                case BuiltinTypeSpec.Type.UIntPtr:
 
1527
                                        return new OperatorCast (new ConvCast (expr, rc.BuiltinTypes.ULong, ConvCast.Mode.I2_U8), target_type, target_type, true);
 
1528
                                }
 
1529
                                break;
 
1530
                        case BuiltinTypeSpec.Type.UInt:
 
1531
                                //
 
1532
                                // From uint to sbyte, byte, short, ushort, int, char
 
1533
                                //
 
1534
                                switch (target_type.BuiltinType) {
 
1535
                                case BuiltinTypeSpec.Type.SByte:
 
1536
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_I1);
 
1537
                                case BuiltinTypeSpec.Type.Byte:
 
1538
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_U1);
 
1539
                                case BuiltinTypeSpec.Type.Short:
 
1540
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_I2);
 
1541
                                case BuiltinTypeSpec.Type.UShort:
 
1542
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_U2);
 
1543
                                case BuiltinTypeSpec.Type.Int:
 
1544
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_I4);
 
1545
                                case BuiltinTypeSpec.Type.Char:
 
1546
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U4_CH);
 
1547
                                }
 
1548
                                break;
 
1549
                        case BuiltinTypeSpec.Type.Long:
 
1550
                                //
 
1551
                                // From long to sbyte, byte, short, ushort, int, uint, ulong, char
 
1552
                                //
 
1553
                                switch (target_type.BuiltinType) {
 
1554
                                case BuiltinTypeSpec.Type.SByte:
 
1555
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_I1);
 
1556
                                case BuiltinTypeSpec.Type.Byte:
 
1557
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_U1);
 
1558
                                case BuiltinTypeSpec.Type.Short:
 
1559
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_I2);
 
1560
                                case BuiltinTypeSpec.Type.UShort:
 
1561
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_U2);
 
1562
                                case BuiltinTypeSpec.Type.Int:
 
1563
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_I4);
 
1564
                                case BuiltinTypeSpec.Type.UInt:
 
1565
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_U4);
 
1566
                                case BuiltinTypeSpec.Type.ULong:
 
1567
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_U8);
 
1568
                                case BuiltinTypeSpec.Type.Char:
 
1569
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_CH);
 
1570
                                }
 
1571
                                break;
 
1572
                        case BuiltinTypeSpec.Type.ULong:
 
1573
                                //
 
1574
                                // From ulong to sbyte, byte, short, ushort, int, uint, long, char
 
1575
                                //
 
1576
                                switch (target_type.BuiltinType) {
 
1577
                                case BuiltinTypeSpec.Type.SByte:
 
1578
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_I1);
 
1579
                                case BuiltinTypeSpec.Type.Byte:
 
1580
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_U1);
 
1581
                                case BuiltinTypeSpec.Type.Short:
 
1582
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_I2);
 
1583
                                case BuiltinTypeSpec.Type.UShort:
 
1584
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_U2);
 
1585
                                case BuiltinTypeSpec.Type.Int:
 
1586
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_I4);
 
1587
                                case BuiltinTypeSpec.Type.UInt:
 
1588
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_U4);
 
1589
                                case BuiltinTypeSpec.Type.Long:
 
1590
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_I8);
 
1591
                                case BuiltinTypeSpec.Type.Char:
 
1592
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_CH);
 
1593
 
 
1594
                                // One of the built-in conversions that belonged in the class library
 
1595
                                case BuiltinTypeSpec.Type.IntPtr:
 
1596
                                        return new OperatorCast (EmptyCast.Create (expr, rc.BuiltinTypes.Long), target_type, true);
 
1597
                                }
 
1598
                                break;
 
1599
                        case BuiltinTypeSpec.Type.Char:
 
1600
                                //
 
1601
                                // From char to sbyte, byte, short
 
1602
                                //
 
1603
                                switch (target_type.BuiltinType) {
 
1604
                                case BuiltinTypeSpec.Type.SByte:
 
1605
                                        return new ConvCast (expr, target_type, ConvCast.Mode.CH_I1);
 
1606
                                case BuiltinTypeSpec.Type.Byte:
 
1607
                                        return new ConvCast (expr, target_type, ConvCast.Mode.CH_U1);
 
1608
                                case BuiltinTypeSpec.Type.Short:
 
1609
                                        return new ConvCast (expr, target_type, ConvCast.Mode.CH_I2);
 
1610
                                }
 
1611
                                break;
 
1612
                        case BuiltinTypeSpec.Type.Float:
 
1613
                                //
 
1614
                                // From float to sbyte, byte, short,
 
1615
                                // ushort, int, uint, long, ulong, char
 
1616
                                // or decimal
 
1617
                                //
 
1618
                                switch (target_type.BuiltinType) {
 
1619
                                case BuiltinTypeSpec.Type.SByte:
 
1620
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_I1);
 
1621
                                case BuiltinTypeSpec.Type.Byte:
 
1622
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_U1);
 
1623
                                case BuiltinTypeSpec.Type.Short:
 
1624
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_I2);
 
1625
                                case BuiltinTypeSpec.Type.UShort:
 
1626
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_U2);
 
1627
                                case BuiltinTypeSpec.Type.Int:
 
1628
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_I4);
 
1629
                                case BuiltinTypeSpec.Type.UInt:
 
1630
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_U4);
 
1631
                                case BuiltinTypeSpec.Type.Long:
 
1632
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_I8);
 
1633
                                case BuiltinTypeSpec.Type.ULong:
 
1634
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_U8);
 
1635
                                case BuiltinTypeSpec.Type.Char:
 
1636
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R4_CH);
 
1637
                                case BuiltinTypeSpec.Type.Decimal:
 
1638
                                        return new OperatorCast (expr, target_type, true);
 
1639
                                }
 
1640
                                break;
 
1641
                        case BuiltinTypeSpec.Type.Double:
 
1642
                                //
 
1643
                                // From double to sbyte, byte, short,
 
1644
                                // ushort, int, uint, long, ulong,
 
1645
                                // char, float or decimal
 
1646
                                //
 
1647
                                switch (target_type.BuiltinType) {
 
1648
                                case BuiltinTypeSpec.Type.SByte:
 
1649
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_I1);
 
1650
                                case BuiltinTypeSpec.Type.Byte:
 
1651
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_U1);
 
1652
                                case BuiltinTypeSpec.Type.Short:
 
1653
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_I2);
 
1654
                                case BuiltinTypeSpec.Type.UShort:
 
1655
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_U2);
 
1656
                                case BuiltinTypeSpec.Type.Int:
 
1657
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_I4);
 
1658
                                case BuiltinTypeSpec.Type.UInt:
 
1659
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_U4);
 
1660
                                case BuiltinTypeSpec.Type.Long:
 
1661
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_I8);
 
1662
                                case BuiltinTypeSpec.Type.ULong:
 
1663
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_U8);
 
1664
                                case BuiltinTypeSpec.Type.Char:
 
1665
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_CH);
 
1666
                                case BuiltinTypeSpec.Type.Float:
 
1667
                                        return new ConvCast (expr, target_type, ConvCast.Mode.R8_R4);
 
1668
                                case BuiltinTypeSpec.Type.Decimal:
 
1669
                                        return new OperatorCast (expr, target_type, true);
 
1670
                                }
 
1671
                                break;
 
1672
                        case BuiltinTypeSpec.Type.UIntPtr:
 
1673
                                //
 
1674
                                // Various built-in conversions that belonged in the class library
 
1675
                                //
 
1676
                                // from uintptr to sbyte, short, int32
 
1677
                                //
 
1678
                                switch (target_type.BuiltinType) {
 
1679
                                case BuiltinTypeSpec.Type.SByte:
 
1680
                                        return new ConvCast (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.UInt, true), target_type, ConvCast.Mode.U4_I1);
 
1681
                                case BuiltinTypeSpec.Type.Short:
 
1682
                                        return new ConvCast (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.UInt, true), target_type, ConvCast.Mode.U4_I2);
 
1683
                                case BuiltinTypeSpec.Type.Int:
 
1684
                                        return EmptyCast.Create (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.UInt, true), target_type);
 
1685
                                case BuiltinTypeSpec.Type.UInt:
 
1686
                                        return new OperatorCast (expr, expr.Type, target_type, true);
 
1687
                                case BuiltinTypeSpec.Type.Long:
 
1688
                                        return EmptyCast.Create (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.ULong, true), target_type);
 
1689
                                }
 
1690
                                break;
 
1691
                        case BuiltinTypeSpec.Type.IntPtr:
 
1692
                                if (target_type.BuiltinType == BuiltinTypeSpec.Type.UInt)
 
1693
                                        return EmptyCast.Create (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.Int, true), target_type);
 
1694
                                if (target_type.BuiltinType == BuiltinTypeSpec.Type.ULong)
 
1695
                                        return EmptyCast.Create (new OperatorCast (expr, expr.Type, rc.BuiltinTypes.Long, true), target_type);
 
1696
                                
 
1697
                                break;
 
1698
                        case BuiltinTypeSpec.Type.Decimal:
 
1699
                                // From decimal to sbyte, byte, short,
 
1700
                                // ushort, int, uint, long, ulong, char,
 
1701
                                // float, or double
 
1702
                                switch (target_type.BuiltinType) {
 
1703
                                case BuiltinTypeSpec.Type.SByte:
 
1704
                                case BuiltinTypeSpec.Type.Byte:
 
1705
                                case BuiltinTypeSpec.Type.Short:
 
1706
                                case BuiltinTypeSpec.Type.UShort:
 
1707
                                case BuiltinTypeSpec.Type.Int:
 
1708
                                case BuiltinTypeSpec.Type.UInt:
 
1709
                                case BuiltinTypeSpec.Type.Long:
 
1710
                                case BuiltinTypeSpec.Type.ULong:
 
1711
                                case BuiltinTypeSpec.Type.Char:
 
1712
                                case BuiltinTypeSpec.Type.Float:
 
1713
                                case BuiltinTypeSpec.Type.Double:
 
1714
                                        return new OperatorCast (expr, expr.Type, target_type, true);
 
1715
                                }
 
1716
 
 
1717
                                break;
 
1718
                        }
 
1719
 
 
1720
                        return null;
 
1721
                }
 
1722
 
 
1723
                /// <summary>
 
1724
                ///  Returns whether an explicit reference conversion can be performed
 
1725
                ///  from source_type to target_type
 
1726
                /// </summary>
 
1727
                public static bool ExplicitReferenceConversionExists (TypeSpec source_type, TypeSpec target_type)
 
1728
                {
 
1729
                        Expression e = ExplicitReferenceConversion (null, source_type, target_type);
 
1730
                        if (e == null)
 
1731
                                return false;
 
1732
 
 
1733
                        if (e == EmptyExpression.Null)
 
1734
                                return true;
 
1735
 
 
1736
                        throw new InternalErrorException ("Invalid probing conversion result");
 
1737
                }
 
1738
 
 
1739
                /// <summary>
 
1740
                ///   Implements Explicit Reference conversions
 
1741
                /// </summary>
 
1742
                static Expression ExplicitReferenceConversion (Expression source, TypeSpec source_type, TypeSpec target_type)
 
1743
                {
 
1744
                        //
 
1745
                        // From object to a generic parameter
 
1746
                        //
 
1747
                        if (source_type.BuiltinType == BuiltinTypeSpec.Type.Object && TypeManager.IsGenericParameter (target_type))
 
1748
                                return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
 
1749
 
 
1750
                        //
 
1751
                        // Explicit type parameter conversion.
 
1752
                        //
 
1753
                        if (source_type.Kind == MemberKind.TypeParameter)
 
1754
                                return ExplicitTypeParameterConversion (source, source_type, target_type);
 
1755
 
 
1756
                        bool target_is_value_type = target_type.Kind == MemberKind.Struct || target_type.Kind == MemberKind.Enum;
 
1757
 
 
1758
                        //
 
1759
                        // Unboxing conversion from System.ValueType to any non-nullable-value-type
 
1760
                        //
 
1761
                        if (source_type.BuiltinType == BuiltinTypeSpec.Type.ValueType && target_is_value_type)
 
1762
                                return source == null ? EmptyExpression.Null : new UnboxCast (source, target_type);
 
1763
 
 
1764
                        //
 
1765
                        // From object or dynamic to any reference type or value type (unboxing)
 
1766
                        //
 
1767
                        if (source_type.BuiltinType == BuiltinTypeSpec.Type.Object || source_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
 
1768
                                if (target_type.IsPointer)
 
1769
                                        return null;
 
1770
 
 
1771
                                return
 
1772
                                        source == null ? EmptyExpression.Null :
 
1773
                                        target_is_value_type ? new UnboxCast (source, target_type) :
 
1774
                                        source is Constant ? (Expression) new EmptyConstantCast ((Constant) source, target_type) :
 
1775
                                        new ClassCast (source, target_type);
 
1776
                        }
 
1777
 
 
1778
                        //
 
1779
                        // From any class S to any class-type T, provided S is a base class of T
 
1780
                        //
 
1781
                        if (source_type.Kind == MemberKind.Class && TypeSpec.IsBaseClass (target_type, source_type, true))
 
1782
                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1783
 
 
1784
                        //
 
1785
                        // From any interface-type S to to any class type T, provided T is not
 
1786
                        // sealed, or provided T implements S.
 
1787
                        //
 
1788
                        if (source_type.Kind == MemberKind.Interface) {
 
1789
                                if (!target_type.IsSealed || target_type.ImplementsInterface (source_type, true)) {
 
1790
                                        if (source == null)
 
1791
                                                return EmptyExpression.Null;
 
1792
 
 
1793
                                        //
 
1794
                                        // Unboxing conversion from any interface-type to any non-nullable-value-type that
 
1795
                                        // implements the interface-type
 
1796
                                        //
 
1797
                                        return target_is_value_type ? new UnboxCast (source, target_type) : (Expression) new ClassCast (source, target_type);
 
1798
                                }
 
1799
 
 
1800
                                //
 
1801
                                // From System.Collections.Generic.IList<T> and its base interfaces to a one-dimensional
 
1802
                                // array type S[], provided there is an implicit or explicit reference conversion from S to T.
 
1803
                                //
 
1804
                                var target_array = target_type as ArrayContainer;
 
1805
                                if (target_array != null && IList_To_Array (source_type, target_array))
 
1806
                                        return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1807
 
 
1808
                                return null;
 
1809
                        }
 
1810
 
 
1811
                        var source_array = source_type as ArrayContainer;
 
1812
                        if (source_array != null) {
 
1813
                                var target_array = target_type as ArrayContainer;
 
1814
                                if (target_array != null) {
 
1815
                                        //
 
1816
                                        // From System.Array to any array-type
 
1817
                                        //
 
1818
                                        if (source_type.BuiltinType == BuiltinTypeSpec.Type.Array)
 
1819
                                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1820
 
 
1821
                                        //
 
1822
                                        // From an array type S with an element type Se to an array type T with an
 
1823
                                        // element type Te provided all the following are true:
 
1824
                                        //     * S and T differe only in element type, in other words, S and T
 
1825
                                        //       have the same number of dimensions.
 
1826
                                        //     * Both Se and Te are reference types
 
1827
                                        //     * An explicit reference conversions exist from Se to Te
 
1828
                                        //
 
1829
                                        if (source_array.Rank == target_array.Rank) {
 
1830
 
 
1831
                                                source_type = source_array.Element;
 
1832
                                                if (!TypeSpec.IsReferenceType (source_type))
 
1833
                                                        return null;
 
1834
 
 
1835
                                                var target_element = target_array.Element;
 
1836
                                                if (!TypeSpec.IsReferenceType (target_element))
 
1837
                                                        return null;
 
1838
 
 
1839
                                                if (ExplicitReferenceConversionExists (source_type, target_element))
 
1840
                                                        return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1841
                                                        
 
1842
                                                return null;
 
1843
                                        }
 
1844
                                }
 
1845
 
 
1846
                                //
 
1847
                                // From a single-dimensional array type S[] to System.Collections.Generic.IList<T> and its base interfaces, 
 
1848
                                // provided that there is an explicit reference conversion from S to T
 
1849
                                //
 
1850
                                if (ArrayToIList (source_array, target_type, true))
 
1851
                                        return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1852
 
 
1853
                                return null;
 
1854
                        }
 
1855
 
 
1856
                        //
 
1857
                        // From any class type S to any interface T, provides S is not sealed
 
1858
                        // and provided S does not implement T.
 
1859
                        //
 
1860
                        if (target_type.IsInterface && !source_type.IsSealed && !source_type.ImplementsInterface (target_type, true)) {
 
1861
                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1862
                        }
 
1863
 
 
1864
                        //
 
1865
                        // From System delegate to any delegate-type
 
1866
                        //
 
1867
                        if (source_type.BuiltinType == BuiltinTypeSpec.Type.Delegate && TypeManager.IsDelegateType (target_type))
 
1868
                                return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1869
 
 
1870
                        //
 
1871
                        // From variant generic delegate to same variant generic delegate type
 
1872
                        //
 
1873
                        if (source_type.IsDelegate && target_type.IsDelegate && source_type.MemberDefinition == target_type.MemberDefinition) {
 
1874
                                var tparams = source_type.MemberDefinition.TypeParameters;
 
1875
                                var targs_src = source_type.TypeArguments;
 
1876
                                var targs_dst = target_type.TypeArguments;
 
1877
                                int i;
 
1878
                                for (i = 0; i < tparams.Length; ++i) {
 
1879
                                        //
 
1880
                                        // If TP is invariant, types have to be identical
 
1881
                                        //
 
1882
                                        if (TypeSpecComparer.IsEqual (targs_src[i], targs_dst[i]))
 
1883
                                                continue;
 
1884
 
 
1885
                                        if (tparams[i].Variance == Variance.Covariant) {
 
1886
                                                //
 
1887
                                                //If TP is covariant, an implicit or explicit identity or reference conversion is required
 
1888
                                                //
 
1889
                                                if (ImplicitReferenceConversionExists (targs_src[i], targs_dst[i]))
 
1890
                                                        continue;
 
1891
 
 
1892
                                                if (ExplicitReferenceConversionExists (targs_src[i], targs_dst[i]))
 
1893
                                                        continue;
 
1894
 
 
1895
                                        } else if (tparams[i].Variance == Variance.Contravariant) {
 
1896
                                                //
 
1897
                                                //If TP is contravariant, both are either identical or reference types
 
1898
                                                //
 
1899
                                                if (TypeSpec.IsReferenceType (targs_src[i]) && TypeSpec.IsReferenceType (targs_dst[i]))
 
1900
                                                        continue;
 
1901
                                        }
 
1902
 
 
1903
                                        break;
 
1904
                                }
 
1905
 
 
1906
                                if (i == tparams.Length)
 
1907
                                        return source == null ? EmptyExpression.Null : new ClassCast (source, target_type);
 
1908
                        }
 
1909
 
 
1910
                        return null;
 
1911
                }
 
1912
 
 
1913
                /// <summary>
 
1914
                ///   Performs an explicit conversion of the expression `expr' whose
 
1915
                ///   type is expr.Type to `target_type'.
 
1916
                /// </summary>
 
1917
                static public Expression ExplicitConversionCore (ResolveContext ec, Expression expr,
 
1918
                                                                 TypeSpec target_type, Location loc)
 
1919
                {
 
1920
                        TypeSpec expr_type = expr.Type;
 
1921
 
 
1922
                        // Explicit conversion includes implicit conversion and it used for enum underlying types too
 
1923
                        Expression ne = ImplicitConversionStandard (ec, expr, target_type, loc, true);
 
1924
                        if (ne != null)
 
1925
                                return ne;
 
1926
 
 
1927
                        if (TypeManager.IsEnumType (expr_type)) {
 
1928
                                TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
 
1929
                                Expression underlying = EmptyCast.Create (expr, EnumSpec.GetUnderlyingType (expr_type));
 
1930
                                if (underlying.Type == real_target)
 
1931
                                        ne = underlying;
 
1932
 
 
1933
                                if (ne == null)
 
1934
                                        ne = ImplicitNumericConversion (underlying, real_target);
 
1935
 
 
1936
                                if (ne == null)
 
1937
                                        ne = ExplicitNumericConversion (ec, underlying, real_target);
 
1938
 
 
1939
                                //
 
1940
                                // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
 
1941
                                //
 
1942
                                if (ne == null && (real_target.BuiltinType == BuiltinTypeSpec.Type.IntPtr || real_target.BuiltinType == BuiltinTypeSpec.Type.UIntPtr))
 
1943
                                        ne = ExplicitUserConversion (ec, underlying, real_target, loc);
 
1944
 
 
1945
                                return ne != null ? EmptyCast.Create (ne, target_type) : null;
 
1946
                        }
 
1947
 
 
1948
                        if (TypeManager.IsEnumType (target_type)) {
 
1949
                                //
 
1950
                                // System.Enum can be unboxed to any enum-type
 
1951
                                //
 
1952
                                if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Enum)
 
1953
                                        return new UnboxCast (expr, target_type);
 
1954
 
 
1955
                                TypeSpec real_target = TypeManager.IsEnumType (target_type) ? EnumSpec.GetUnderlyingType (target_type) : target_type;
 
1956
 
 
1957
                                if (expr_type == real_target)
 
1958
                                        return EmptyCast.Create (expr, target_type);
 
1959
 
 
1960
                                ne = ImplicitNumericConversion (expr, real_target);
 
1961
                                if (ne != null)
 
1962
                                        return EmptyCast.Create (ne, target_type);
 
1963
 
 
1964
                                ne = ExplicitNumericConversion (ec, expr, real_target);
 
1965
                                if (ne != null)
 
1966
                                        return EmptyCast.Create (ne, target_type);
 
1967
 
 
1968
                                //
 
1969
                                // LAMESPEC: IntPtr and UIntPtr conversion to any Enum is allowed
 
1970
                                //
 
1971
                                if (expr_type.BuiltinType == BuiltinTypeSpec.Type.IntPtr || expr_type.BuiltinType == BuiltinTypeSpec.Type.UIntPtr) {
 
1972
                                        ne = ExplicitUserConversion (ec, expr, real_target, loc);
 
1973
                                        if (ne != null)
 
1974
                                                return ExplicitConversionCore (ec, ne, target_type, loc);
 
1975
                                }
 
1976
                        } else {
 
1977
                                ne = ExplicitNumericConversion (ec, expr, target_type);
 
1978
                                if (ne != null)
 
1979
                                        return ne;
 
1980
                        }
 
1981
 
 
1982
                        //
 
1983
                        // Skip the ExplicitReferenceConversion because we can not convert
 
1984
                        // from Null to a ValueType, and ExplicitReference wont check against
 
1985
                        // null literal explicitly
 
1986
                        //
 
1987
                        if (expr_type != InternalType.NullLiteral) {
 
1988
                                ne = ExplicitReferenceConversion (expr, expr_type, target_type);
 
1989
                                if (ne != null)
 
1990
                                        return ne;
 
1991
                        }
 
1992
 
 
1993
                        if (ec.IsUnsafe){
 
1994
                                ne = ExplicitUnsafe (expr, target_type);
 
1995
                                if (ne != null)
 
1996
                                        return ne;
 
1997
                        }
 
1998
                        
 
1999
                        return null;
 
2000
                }
 
2001
 
 
2002
                public static Expression ExplicitUnsafe (Expression expr, TypeSpec target_type)
 
2003
                {
 
2004
                        TypeSpec expr_type = expr.Type;
 
2005
 
 
2006
                        if (target_type.IsPointer){
 
2007
                                if (expr_type.IsPointer)
 
2008
                                        return EmptyCast.Create (expr, target_type);
 
2009
 
 
2010
                                switch (expr_type.BuiltinType) {
 
2011
                                case BuiltinTypeSpec.Type.SByte:
 
2012
                                case BuiltinTypeSpec.Type.Short:
 
2013
                                case BuiltinTypeSpec.Type.Int:
 
2014
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I);
 
2015
 
 
2016
                                case BuiltinTypeSpec.Type.UShort:
 
2017
                                case BuiltinTypeSpec.Type.UInt:
 
2018
                                case BuiltinTypeSpec.Type.Byte:
 
2019
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U);
 
2020
 
 
2021
                                case BuiltinTypeSpec.Type.Long:
 
2022
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I8_I);
 
2023
 
 
2024
                                case BuiltinTypeSpec.Type.ULong:
 
2025
                                        return new ConvCast (expr, target_type, ConvCast.Mode.U8_I);
 
2026
                                }
 
2027
                        }
 
2028
 
 
2029
                        if (expr_type.IsPointer){
 
2030
                                switch (target_type.BuiltinType) {
 
2031
                                case BuiltinTypeSpec.Type.SByte:
 
2032
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I1);
 
2033
                                case BuiltinTypeSpec.Type.Byte:
 
2034
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U1);
 
2035
                                case BuiltinTypeSpec.Type.Short:
 
2036
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I2);
 
2037
                                case BuiltinTypeSpec.Type.UShort:
 
2038
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U2);
 
2039
                                case BuiltinTypeSpec.Type.Int:
 
2040
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_I4);
 
2041
                                case BuiltinTypeSpec.Type.UInt:
 
2042
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U4);
 
2043
                                case BuiltinTypeSpec.Type.Long:
 
2044
                                        return new ConvCast (expr, target_type, ConvCast.Mode.I_I8);
 
2045
                                case BuiltinTypeSpec.Type.ULong:
 
2046
                                        return new OpcodeCast (expr, target_type, OpCodes.Conv_U8);
 
2047
                                }
 
2048
                        }
 
2049
                        return null;
 
2050
                }
 
2051
 
 
2052
                /// <summary>
 
2053
                ///   Same as ExplicitConversion, only it doesn't include user defined conversions
 
2054
                /// </summary>
 
2055
                static public Expression ExplicitConversionStandard (ResolveContext ec, Expression expr,
 
2056
                                                                     TypeSpec target_type, Location l)
 
2057
                {
 
2058
                        int errors = ec.Report.Errors;
 
2059
                        Expression ne = ImplicitConversionStandard (ec, expr, target_type, l);
 
2060
                        if (ec.Report.Errors > errors)
 
2061
                                return null;
 
2062
 
 
2063
                        if (ne != null)
 
2064
                                return ne;
 
2065
 
 
2066
                        ne = ExplicitNumericConversion (ec, expr, target_type);
 
2067
                        if (ne != null)
 
2068
                                return ne;
 
2069
 
 
2070
                        ne = ExplicitReferenceConversion (expr, expr.Type, target_type);
 
2071
                        if (ne != null)
 
2072
                                return ne;
 
2073
 
 
2074
                        if (ec.IsUnsafe && expr.Type.IsPointer && target_type.IsPointer && ((PointerContainer)expr.Type).Element.Kind == MemberKind.Void)
 
2075
                                return EmptyCast.Create (expr, target_type);
 
2076
 
 
2077
                        expr.Error_ValueCannotBeConverted (ec, l, target_type, true);
 
2078
                        return null;
 
2079
                }
 
2080
 
 
2081
                /// <summary>
 
2082
                ///   Performs an explicit conversion of the expression `expr' whose
 
2083
                ///   type is expr.Type to `target_type'.
 
2084
                /// </summary>
 
2085
                static public Expression ExplicitConversion (ResolveContext ec, Expression expr,
 
2086
                        TypeSpec target_type, Location loc)
 
2087
                {
 
2088
                        Expression e = ExplicitConversionCore (ec, expr, target_type, loc);
 
2089
                        if (e != null) {
 
2090
                                //
 
2091
                                // Don't eliminate explicit precission casts
 
2092
                                //
 
2093
                                if (e == expr) {
 
2094
                                        if (target_type.BuiltinType == BuiltinTypeSpec.Type.Float)
 
2095
                                                return new OpcodeCast (expr, target_type, OpCodes.Conv_R4);
 
2096
                                        
 
2097
                                        if (target_type.BuiltinType == BuiltinTypeSpec.Type.Double)
 
2098
                                                return new OpcodeCast (expr, target_type, OpCodes.Conv_R8);
 
2099
                                }
 
2100
                                        
 
2101
                                return e;
 
2102
                        }
 
2103
 
 
2104
                        TypeSpec expr_type = expr.Type;
 
2105
                        if (target_type.IsNullableType) {
 
2106
                                TypeSpec target;
 
2107
 
 
2108
                                if (expr_type.IsNullableType) {
 
2109
                                        target = Nullable.NullableInfo.GetUnderlyingType (target_type);
 
2110
                                        Expression unwrap = Nullable.Unwrap.Create (expr);
 
2111
                                        e = ExplicitConversion (ec, unwrap, target, expr.Location);
 
2112
                                        if (e == null)
 
2113
                                                return null;
 
2114
 
 
2115
                                        return new Nullable.Lifted (e, unwrap, target_type).Resolve (ec);
 
2116
                                }
 
2117
                                if (expr_type.BuiltinType == BuiltinTypeSpec.Type.Object) {
 
2118
                                        return new UnboxCast (expr, target_type);
 
2119
                                }
 
2120
 
 
2121
                                target = TypeManager.GetTypeArguments (target_type) [0];
 
2122
                                e = ExplicitConversionCore (ec, expr, target, loc);
 
2123
                                if (e != null)
 
2124
                                        return Nullable.Wrap.Create (e, target_type);
 
2125
                        } else if (expr_type.IsNullableType) {
 
2126
                                e = ImplicitBoxingConversion (expr, Nullable.NullableInfo.GetUnderlyingType (expr_type), target_type);
 
2127
                                if (e != null)
 
2128
                                        return e;
 
2129
 
 
2130
                                e = Nullable.Unwrap.Create (expr, false);                       
 
2131
                                e = ExplicitConversionCore (ec, e, target_type, loc);
 
2132
                                if (e != null)
 
2133
                                        return EmptyCast.Create (e, target_type);
 
2134
                        }
 
2135
                        
 
2136
                        e = ExplicitUserConversion (ec, expr, target_type, loc);
 
2137
                        if (e != null)
 
2138
                                return e;                       
 
2139
 
 
2140
                        expr.Error_ValueCannotBeConverted (ec, loc, target_type, true);
 
2141
                        return null;
 
2142
                }
 
2143
        }
 
2144
}