~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory/Semantics/Conversion.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 
4
// software and associated documentation files (the "Software"), to deal in the Software
 
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
 
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 
7
// to whom the Software is furnished to do so, subject to the following conditions:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
17
// DEALINGS IN THE SOFTWARE.
 
18
 
 
19
using System;
 
20
using ICSharpCode.NRefactory.TypeSystem;
 
21
 
 
22
namespace ICSharpCode.NRefactory.Semantics
 
23
{
 
24
        /// <summary>
 
25
        /// Holds information about a conversion between two types.
 
26
        /// </summary>
 
27
        public abstract class Conversion : IEquatable<Conversion>
 
28
        {
 
29
                #region Conversion factory methods
 
30
                /// <summary>
 
31
                /// Not a valid conversion.
 
32
                /// </summary>
 
33
                public static readonly Conversion None = new InvalidConversion();
 
34
                
 
35
                /// <summary>
 
36
                /// Identity conversion.
 
37
                /// </summary>
 
38
                public static readonly Conversion IdentityConversion = new BuiltinConversion(true, 0);
 
39
                
 
40
                public static readonly Conversion ImplicitNumericConversion = new NumericOrEnumerationConversion(true, false);
 
41
                public static readonly Conversion ExplicitNumericConversion = new NumericOrEnumerationConversion(false, false);
 
42
                public static readonly Conversion ImplicitLiftedNumericConversion = new NumericOrEnumerationConversion(true, true);
 
43
                public static readonly Conversion ExplicitLiftedNumericConversion = new NumericOrEnumerationConversion(false, true);
 
44
                
 
45
                public static Conversion EnumerationConversion(bool isImplicit, bool isLifted)
 
46
                {
 
47
                        return new NumericOrEnumerationConversion(isImplicit, isLifted, true);
 
48
                }
 
49
                
 
50
                public static readonly Conversion NullLiteralConversion = new BuiltinConversion(true, 1);
 
51
                
 
52
                /// <summary>
 
53
                /// The numeric conversion of a constant expression.
 
54
                /// </summary>
 
55
                public static readonly Conversion ImplicitConstantExpressionConversion = new BuiltinConversion(true, 2);
 
56
                
 
57
                public static readonly Conversion ImplicitReferenceConversion = new BuiltinConversion(true, 3);
 
58
                public static readonly Conversion ExplicitReferenceConversion = new BuiltinConversion(false, 3);
 
59
                
 
60
                public static readonly Conversion ImplicitDynamicConversion = new BuiltinConversion(true, 4);
 
61
                public static readonly Conversion ExplicitDynamicConversion = new BuiltinConversion(false, 4);
 
62
                
 
63
                public static readonly Conversion ImplicitNullableConversion = new BuiltinConversion(true, 5);
 
64
                public static readonly Conversion ExplicitNullableConversion = new BuiltinConversion(false, 5);
 
65
                
 
66
                public static readonly Conversion ImplicitPointerConversion = new BuiltinConversion(true, 6);
 
67
                public static readonly Conversion ExplicitPointerConversion = new BuiltinConversion(false, 6);
 
68
                
 
69
                public static readonly Conversion BoxingConversion = new BuiltinConversion(true, 7);
 
70
                public static readonly Conversion UnboxingConversion = new BuiltinConversion(false, 8);
 
71
                
 
72
                /// <summary>
 
73
                /// C# 'as' cast.
 
74
                /// </summary>
 
75
                public static readonly Conversion TryCast = new BuiltinConversion(false, 9);
 
76
                
 
77
                [Obsolete("Use UserDefinedConversion() instead")]
 
78
                public static Conversion UserDefinedImplicitConversion(IMethod operatorMethod, Conversion conversionBeforeUserDefinedOperator, Conversion conversionAfterUserDefinedOperator, bool isLifted)
 
79
                {
 
80
                        if (operatorMethod == null)
 
81
                                throw new ArgumentNullException("operatorMethod");
 
82
                        return new UserDefinedConv(true, operatorMethod, conversionBeforeUserDefinedOperator, conversionAfterUserDefinedOperator, isLifted, false);
 
83
                }
 
84
                
 
85
                [Obsolete("Use UserDefinedConversion() instead")]
 
86
                public static Conversion UserDefinedExplicitConversion(IMethod operatorMethod, Conversion conversionBeforeUserDefinedOperator, Conversion conversionAfterUserDefinedOperator, bool isLifted)
 
87
                {
 
88
                        if (operatorMethod == null)
 
89
                                throw new ArgumentNullException("operatorMethod");
 
90
                        return new UserDefinedConv(false, operatorMethod, conversionBeforeUserDefinedOperator, conversionAfterUserDefinedOperator, isLifted, false);
 
91
                }
 
92
                
 
93
                public static Conversion UserDefinedConversion(IMethod operatorMethod, bool isImplicit, Conversion conversionBeforeUserDefinedOperator, Conversion conversionAfterUserDefinedOperator, bool isLifted = false, bool isAmbiguous = false)
 
94
                {
 
95
                        if (operatorMethod == null)
 
96
                                throw new ArgumentNullException("operatorMethod");
 
97
                        return new UserDefinedConv(isImplicit, operatorMethod, conversionBeforeUserDefinedOperator, conversionAfterUserDefinedOperator, isLifted, isAmbiguous);
 
98
                }
 
99
                
 
100
                public static Conversion MethodGroupConversion(IMethod chosenMethod, bool isVirtualMethodLookup)
 
101
                {
 
102
                        if (chosenMethod == null)
 
103
                                throw new ArgumentNullException("chosenMethod");
 
104
                        return new MethodGroupConv(chosenMethod, isVirtualMethodLookup, isValid: true);
 
105
                }
 
106
                
 
107
                public static Conversion InvalidMethodGroupConversion(IMethod chosenMethod, bool isVirtualMethodLookup)
 
108
                {
 
109
                        if (chosenMethod == null)
 
110
                                throw new ArgumentNullException("chosenMethod");
 
111
                        return new MethodGroupConv(chosenMethod, isVirtualMethodLookup, isValid: false);
 
112
                }
 
113
                #endregion
 
114
                
 
115
                #region Inner classes
 
116
                sealed class InvalidConversion : Conversion
 
117
                {
 
118
                        public override bool IsValid {
 
119
                                get { return false; }
 
120
                        }
 
121
                        
 
122
                        public override string ToString()
 
123
                        {
 
124
                                return "None";
 
125
                        }
 
126
                }
 
127
                
 
128
                sealed class NumericOrEnumerationConversion : Conversion
 
129
                {
 
130
                        readonly bool isImplicit;
 
131
                        readonly bool isLifted;
 
132
                        readonly bool isEnumeration;
 
133
                        
 
134
                        public NumericOrEnumerationConversion(bool isImplicit, bool isLifted, bool isEnumeration = false)
 
135
                        {
 
136
                                this.isImplicit = isImplicit;
 
137
                                this.isLifted = isLifted;
 
138
                                this.isEnumeration = isEnumeration;
 
139
                        }
 
140
                        
 
141
                        public override bool IsImplicit {
 
142
                                get { return isImplicit; }
 
143
                        }
 
144
                        
 
145
                        public override bool IsExplicit {
 
146
                                get { return !isImplicit; }
 
147
                        }
 
148
                        
 
149
                        public override bool IsNumericConversion {
 
150
                                get { return !isEnumeration; }
 
151
                        }
 
152
                        
 
153
                        public override bool IsEnumerationConversion {
 
154
                                get { return isEnumeration; }
 
155
                        }
 
156
                        
 
157
                        public override bool IsLifted {
 
158
                                get { return isLifted; }
 
159
                        }
 
160
                        
 
161
                        public override string ToString()
 
162
                        {
 
163
                                return (isImplicit ? "implicit" : "explicit")
 
164
                                        + (isLifted ? " lifted" : "")
 
165
                                        + (isEnumeration ? " enumeration" : " numeric")
 
166
                                        + " conversion";
 
167
                        }
 
168
                        
 
169
                        public override bool Equals(Conversion other)
 
170
                        {
 
171
                                NumericOrEnumerationConversion o = other as NumericOrEnumerationConversion;
 
172
                                return o != null && isImplicit == o.isImplicit && isLifted == o.isLifted && isEnumeration == o.isEnumeration;
 
173
                        }
 
174
                        
 
175
                        public override int GetHashCode()
 
176
                        {
 
177
                                return (isImplicit ? 1 : 0) + (isLifted ? 2 : 0) + (isEnumeration ? 4 : 0);
 
178
                        }
 
179
                }
 
180
                
 
181
                sealed class BuiltinConversion : Conversion
 
182
                {
 
183
                        readonly bool isImplicit;
 
184
                        readonly byte type;
 
185
                        
 
186
                        public BuiltinConversion(bool isImplicit, byte type)
 
187
                        {
 
188
                                this.isImplicit = isImplicit;
 
189
                                this.type = type;
 
190
                        }
 
191
                        
 
192
                        public override bool IsImplicit {
 
193
                                get { return isImplicit; }
 
194
                        }
 
195
                        
 
196
                        public override bool IsExplicit {
 
197
                                get { return !isImplicit; }
 
198
                        }
 
199
                        
 
200
                        public override bool IsIdentityConversion {
 
201
                                get { return type == 0; }
 
202
                        }
 
203
                        
 
204
                        public override bool IsNullLiteralConversion {
 
205
                                get { return type == 1; }
 
206
                        }
 
207
                        
 
208
                        public override bool IsConstantExpressionConversion {
 
209
                                get { return type == 2; }
 
210
                        }
 
211
 
 
212
                        public override bool IsReferenceConversion {
 
213
                                get { return type == 3; }
 
214
                        }
 
215
                        
 
216
                        public override bool IsDynamicConversion {
 
217
                                get { return type == 4; }
 
218
                        }
 
219
                        
 
220
                        public override bool IsNullableConversion {
 
221
                                get { return type == 5; }
 
222
                        }
 
223
                        
 
224
                        public override bool IsPointerConversion {
 
225
                                get { return type == 6; }
 
226
                        }
 
227
                        
 
228
                        public override bool IsBoxingConversion {
 
229
                                get { return type == 7; }
 
230
                        }
 
231
                        
 
232
                        public override bool IsUnboxingConversion {
 
233
                                get { return type == 8; }
 
234
                        }
 
235
                        
 
236
                        public override bool IsTryCast {
 
237
                                get { return type == 9; }
 
238
                        }
 
239
                        
 
240
                        public override string ToString()
 
241
                        {
 
242
                                string name = null;
 
243
                                switch (type) {
 
244
                                        case 0:
 
245
                                                return "identity conversion";
 
246
                                        case 1:
 
247
                                                return "null-literal conversion";
 
248
                                        case 2:
 
249
                                                name = "constant-expression";
 
250
                                                break;
 
251
                                        case 3:
 
252
                                                name = "reference";
 
253
                                                break;
 
254
                                        case 4:
 
255
                                                name = "dynamic";
 
256
                                                break;
 
257
                                        case 5:
 
258
                                                name = "nullable";
 
259
                                                break;
 
260
                                        case 6:
 
261
                                                name = "pointer";
 
262
                                                break;
 
263
                                        case 7:
 
264
                                                return "boxing conversion";
 
265
                                        case 8:
 
266
                                                return "unboxing conversion";
 
267
                                        case 9:
 
268
                                                return "try cast";
 
269
                                }
 
270
                                return (isImplicit ? "implicit " : "explicit ") + name + " conversion";
 
271
                        }
 
272
                }
 
273
                
 
274
                sealed class UserDefinedConv : Conversion
 
275
                {
 
276
                        readonly IMethod method;
 
277
                        readonly bool isLifted;
 
278
                        readonly Conversion conversionBeforeUserDefinedOperator;
 
279
                        readonly Conversion conversionAfterUserDefinedOperator;
 
280
                        readonly bool isImplicit;
 
281
                        readonly bool isValid;
 
282
                        
 
283
                        public UserDefinedConv(bool isImplicit, IMethod method, Conversion conversionBeforeUserDefinedOperator, Conversion conversionAfterUserDefinedOperator, bool isLifted, bool isAmbiguous)
 
284
                        {
 
285
                                this.method = method;
 
286
                                this.isLifted = isLifted;
 
287
                                this.conversionBeforeUserDefinedOperator = conversionBeforeUserDefinedOperator;
 
288
                                this.conversionAfterUserDefinedOperator = conversionAfterUserDefinedOperator;
 
289
                                this.isImplicit = isImplicit;
 
290
                                this.isValid = !isAmbiguous;
 
291
                        }
 
292
                        
 
293
                        public override bool IsValid {
 
294
                                get { return isValid; }
 
295
                        }
 
296
                        
 
297
                        public override bool IsImplicit {
 
298
                                get { return isImplicit; }
 
299
                        }
 
300
                        
 
301
                        public override bool IsExplicit {
 
302
                                get { return !isImplicit; }
 
303
                        }
 
304
                        
 
305
                        public override bool IsLifted {
 
306
                                get { return isLifted; }
 
307
                        }
 
308
                        
 
309
                        public override bool IsUserDefined {
 
310
                                get { return true; }
 
311
                        }
 
312
                        
 
313
                        public override Conversion ConversionBeforeUserDefinedOperator {
 
314
                                get { return conversionBeforeUserDefinedOperator; }
 
315
                        }
 
316
                
 
317
                        public override Conversion ConversionAfterUserDefinedOperator {
 
318
                                get { return conversionAfterUserDefinedOperator; }
 
319
                        }
 
320
 
 
321
                        public override IMethod Method {
 
322
                                get { return method; }
 
323
                        }
 
324
                        
 
325
                        public override bool Equals(Conversion other)
 
326
                        {
 
327
                                UserDefinedConv o = other as UserDefinedConv;
 
328
                                return o != null && isLifted == o.isLifted && isImplicit == o.isImplicit && isValid == o.isValid && method.Equals(o.method);
 
329
                        }
 
330
                        
 
331
                        public override int GetHashCode()
 
332
                        {
 
333
                                return unchecked(method.GetHashCode() + (isLifted ? 31 : 27) + (isImplicit ? 71 : 61) + (isValid ? 107 : 109));
 
334
                        }
 
335
                        
 
336
                        public override string ToString()
 
337
                        {
 
338
                                return (isImplicit ? "implicit" : "explicit")
 
339
                                        + (isLifted ? " lifted" : "")
 
340
                                        + (isValid ? "" : " ambiguous")
 
341
                                        + "user-defined conversion (" + method + ")";
 
342
                        }
 
343
                }
 
344
                
 
345
                sealed class MethodGroupConv : Conversion
 
346
                {
 
347
                        readonly IMethod method;
 
348
                        readonly bool isVirtualMethodLookup;
 
349
                        readonly bool isValid;
 
350
                        
 
351
                        public MethodGroupConv(IMethod method, bool isVirtualMethodLookup, bool isValid)
 
352
                        {
 
353
                                this.method = method;
 
354
                                this.isVirtualMethodLookup = isVirtualMethodLookup;
 
355
                                this.isValid = isValid;
 
356
                        }
 
357
                        
 
358
                        public override bool IsValid {
 
359
                                get { return isValid; }
 
360
                        }
 
361
                        
 
362
                        public override bool IsImplicit {
 
363
                                get { return true; }
 
364
                        }
 
365
                        
 
366
                        public override bool IsMethodGroupConversion {
 
367
                                get { return true; }
 
368
                        }
 
369
                        
 
370
                        public override bool IsVirtualMethodLookup {
 
371
                                get { return isVirtualMethodLookup; }
 
372
                        }
 
373
                        
 
374
                        public override IMethod Method {
 
375
                                get { return method; }
 
376
                        }
 
377
                        
 
378
                        public override bool Equals(Conversion other)
 
379
                        {
 
380
                                MethodGroupConv o = other as MethodGroupConv;
 
381
                                return o != null && method.Equals(o.method);
 
382
                        }
 
383
                        
 
384
                        public override int GetHashCode()
 
385
                        {
 
386
                                return method.GetHashCode();
 
387
                        }
 
388
                }
 
389
                #endregion
 
390
                
 
391
                /// <summary>
 
392
                /// Gets whether the conversion is valid.
 
393
                /// </summary>
 
394
                public virtual bool IsValid {
 
395
                        get { return true; }
 
396
                }
 
397
                
 
398
                public virtual bool IsImplicit {
 
399
                        get { return false; }
 
400
                }
 
401
                
 
402
                public virtual bool IsExplicit {
 
403
                        get { return false; }
 
404
                }
 
405
                
 
406
                /// <summary>
 
407
                /// Gets whether the conversion is an '<c>as</c>' cast.
 
408
                /// </summary>
 
409
                public virtual bool IsTryCast {
 
410
                        get { return false; }
 
411
                }
 
412
                
 
413
                public virtual bool IsIdentityConversion {
 
414
                        get { return false; }
 
415
                }
 
416
                
 
417
                public virtual bool IsNullLiteralConversion {
 
418
                        get { return false; }
 
419
                }
 
420
                
 
421
                public virtual bool IsConstantExpressionConversion {
 
422
                        get { return false; }
 
423
                }
 
424
 
 
425
                public virtual bool IsNumericConversion {
 
426
                        get { return false; }
 
427
                }
 
428
                
 
429
                /// <summary>
 
430
                /// Gets whether this conversion is a lifted version of another conversion.
 
431
                /// </summary>
 
432
                public virtual bool IsLifted {
 
433
                        get { return false; }
 
434
                }
 
435
                
 
436
                /// <summary>
 
437
                /// Gets whether the conversion is dynamic.
 
438
                /// </summary>
 
439
                public virtual bool IsDynamicConversion {
 
440
                        get { return false; }
 
441
                }
 
442
                
 
443
                /// <summary>
 
444
                /// Gets whether the conversion is a reference conversion.
 
445
                /// </summary>
 
446
                public virtual bool IsReferenceConversion {
 
447
                        get { return false; }
 
448
                }
 
449
                
 
450
                /// <summary>
 
451
                /// Gets whether the conversion is an enumeration conversion.
 
452
                /// </summary>
 
453
                public virtual bool IsEnumerationConversion {
 
454
                        get { return false; }
 
455
                }
 
456
                
 
457
                /// <summary>
 
458
                /// Gets whether the conversion is a nullable conversion
 
459
                /// (conversion between a nullable type and the regular type).
 
460
                /// </summary>
 
461
                public virtual bool IsNullableConversion {
 
462
                        get { return false; }
 
463
                }
 
464
                
 
465
                /// <summary>
 
466
                /// Gets whether this conversion is user-defined (op_Implicit or op_Explicit).
 
467
                /// </summary>
 
468
                public virtual bool IsUserDefined {
 
469
                        get { return false; }
 
470
                }
 
471
 
 
472
                /// <summary>
 
473
                /// The conversion that is applied to the input before the user-defined conversion operator is invoked.
 
474
                /// </summary>
 
475
                public virtual Conversion ConversionBeforeUserDefinedOperator {
 
476
                        get { return null; }
 
477
                }
 
478
                
 
479
                /// <summary>
 
480
                /// The conversion that is applied to the result of the user-defined conversion operator.
 
481
                /// </summary>
 
482
                public virtual Conversion ConversionAfterUserDefinedOperator {
 
483
                        get { return null; }
 
484
                }
 
485
 
 
486
                /// <summary>
 
487
                /// Gets whether this conversion is a boxing conversion.
 
488
                /// </summary>
 
489
                public virtual bool IsBoxingConversion {
 
490
                        get { return false; }
 
491
                }
 
492
                
 
493
                /// <summary>
 
494
                /// Gets whether this conversion is an unboxing conversion.
 
495
                /// </summary>
 
496
                public virtual bool IsUnboxingConversion {
 
497
                        get { return false; }
 
498
                }
 
499
                
 
500
                /// <summary>
 
501
                /// Gets whether this conversion is a pointer conversion.
 
502
                /// </summary>
 
503
                public virtual bool IsPointerConversion {
 
504
                        get { return false; }
 
505
                }
 
506
                
 
507
                /// <summary>
 
508
                /// Gets whether this conversion is a method group conversion.
 
509
                /// </summary>
 
510
                public virtual bool IsMethodGroupConversion {
 
511
                        get { return false; }
 
512
                }
 
513
                
 
514
                /// <summary>
 
515
                /// For method-group conversions, gets whether to perform a virtual method lookup at runtime.
 
516
                /// </summary>
 
517
                public virtual bool IsVirtualMethodLookup {
 
518
                        get { return false; }
 
519
                }
 
520
                
 
521
                /// <summary>
 
522
                /// Gets whether this conversion is an anonymous function conversion.
 
523
                /// </summary>
 
524
                public virtual bool IsAnonymousFunctionConversion {
 
525
                        get { return false; }
 
526
                }
 
527
                
 
528
                /// <summary>
 
529
                /// Gets the method associated with this conversion.
 
530
                /// For user-defined conversions, this is the method being called.
 
531
                /// For method-group conversions, this is the method that was chosen from the group.
 
532
                /// </summary>
 
533
                public virtual IMethod Method {
 
534
                        get { return null; }
 
535
                }
 
536
                
 
537
                public override sealed bool Equals(object obj)
 
538
                {
 
539
                        return Equals(obj as Conversion);
 
540
                }
 
541
                
 
542
                public override int GetHashCode()
 
543
                {
 
544
                        return base.GetHashCode();
 
545
                }
 
546
                
 
547
                public virtual bool Equals(Conversion other)
 
548
                {
 
549
                        return this == other;
 
550
                }
 
551
        }
 
552
}