-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Conversion.cs
1007 lines (905 loc) · 39.6 KB
/
Conversion.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// Summarizes whether a conversion is allowed, and if so, which kind of conversion (and in some cases, the
/// associated symbol).
/// </summary>
public struct Conversion : IEquatable<Conversion>, IConvertibleConversion
{
private readonly ConversionKind _kind;
private readonly UncommonData _uncommonData;
// most conversions are trivial and do not require additional data besides Kind
// in uncommon cases an instance of this class is attached to the conversion.
private class UncommonData
{
public UncommonData(
bool isExtensionMethod,
bool isArrayIndex,
UserDefinedConversionResult conversionResult,
MethodSymbol conversionMethod,
ImmutableArray<Conversion> nestedConversions)
{
_conversionMethod = conversionMethod;
_conversionResult = conversionResult;
_nestedConversionsOpt = nestedConversions;
_flags = isExtensionMethod ? IsExtensionMethodMask : (byte)0;
if (isArrayIndex)
{
_flags |= IsArrayIndexMask;
}
}
internal readonly MethodSymbol _conversionMethod;
internal readonly ImmutableArray<Conversion> _nestedConversionsOpt;
//no effect on Equals/GetHashCode
internal readonly UserDefinedConversionResult _conversionResult;
private const byte IsExtensionMethodMask = 1 << 0;
private const byte IsArrayIndexMask = 1 << 1;
private readonly byte _flags;
internal bool IsExtensionMethod
{
get
{
return (_flags & IsExtensionMethodMask) != 0;
}
}
// used by dynamic, which needs to know if a conversion is an array index conversion.
internal bool IsArrayIndex
{
get
{
return (_flags & IsArrayIndexMask) != 0;
}
}
}
private class DeconstructionUncommonData : UncommonData
{
internal DeconstructionUncommonData(DeconstructMethodInfo deconstructMethodInfoOpt, ImmutableArray<Conversion> nestedConversions)
: base(isExtensionMethod: false, isArrayIndex: false, conversionResult: default, conversionMethod: null, nestedConversions)
{
Debug.Assert(!nestedConversions.IsDefaultOrEmpty);
DeconstructMethodInfo = deconstructMethodInfoOpt;
}
readonly internal DeconstructMethodInfo DeconstructMethodInfo;
}
private Conversion(
ConversionKind kind,
UncommonData uncommonData)
{
_kind = kind;
_uncommonData = uncommonData;
}
private Conversion(ConversionKind kind)
: this(kind, null)
{
}
internal Conversion(UserDefinedConversionResult conversionResult, bool isImplicit)
{
_kind = conversionResult.Kind == UserDefinedConversionResultKind.NoApplicableOperators
? ConversionKind.NoConversion
: isImplicit ? ConversionKind.ImplicitUserDefined : ConversionKind.ExplicitUserDefined;
_uncommonData = new UncommonData(
isExtensionMethod: false,
isArrayIndex: false,
conversionResult: conversionResult,
conversionMethod: null,
nestedConversions: default(ImmutableArray<Conversion>));
}
// For the method group, lambda and anonymous method conversions
internal Conversion(ConversionKind kind, MethodSymbol conversionMethod, bool isExtensionMethod)
{
this._kind = kind;
_uncommonData = new UncommonData(
isExtensionMethod: isExtensionMethod,
isArrayIndex: false,
conversionResult: default(UserDefinedConversionResult),
conversionMethod: conversionMethod,
nestedConversions: default(ImmutableArray<Conversion>));
}
internal Conversion(ConversionKind kind, ImmutableArray<Conversion> nestedConversions)
{
this._kind = kind;
_uncommonData = new UncommonData(
isExtensionMethod: false,
isArrayIndex: false,
conversionResult: default(UserDefinedConversionResult),
conversionMethod: null,
nestedConversions: nestedConversions);
}
internal Conversion(ConversionKind kind, DeconstructMethodInfo deconstructMethodInfo, ImmutableArray<Conversion> nestedConversions)
{
Debug.Assert(kind == ConversionKind.Deconstruction);
this._kind = kind;
_uncommonData = new DeconstructionUncommonData(deconstructMethodInfo, nestedConversions);
}
internal Conversion SetConversionMethod(MethodSymbol conversionMethod)
{
// we use this method to patch up the conversion method only in two cases -
// 1) when rewriting MethodGroup conversions and the method gets substituted.
// 2) when lowering IntPtr conversion (a compat-related conversion which becomes a kind of a user-defined conversion)
// in those cases it is ok to ignore existing _uncommonData.
Debug.Assert(_kind == ConversionKind.MethodGroup || _kind == ConversionKind.IntPtr);
return new Conversion(this.Kind, conversionMethod, isExtensionMethod: IsExtensionMethod);
}
internal Conversion SetArrayIndexConversionForDynamic()
{
Debug.Assert(_kind.IsDynamic());
Debug.Assert(_uncommonData == null);
return new Conversion(
_kind,
new UncommonData(
isExtensionMethod: false,
isArrayIndex: true,
conversionResult: default(UserDefinedConversionResult),
conversionMethod: null,
nestedConversions: default(ImmutableArray<Conversion>)));
}
[Conditional("DEBUG")]
private static void AssertTrivialConversion(ConversionKind kind)
{
bool isTrivial;
switch (kind)
{
case ConversionKind.NoConversion:
case ConversionKind.Identity:
case ConversionKind.ImplicitConstant:
case ConversionKind.ImplicitNumeric:
case ConversionKind.ImplicitReference:
case ConversionKind.ImplicitEnumeration:
case ConversionKind.ImplicitThrow:
case ConversionKind.AnonymousFunction:
case ConversionKind.Boxing:
case ConversionKind.DefaultOrNullLiteral:
case ConversionKind.NullToPointer:
case ConversionKind.PointerToVoid:
case ConversionKind.PointerToPointer:
case ConversionKind.PointerToInteger:
case ConversionKind.IntegerToPointer:
case ConversionKind.Unboxing:
case ConversionKind.ExplicitReference:
case ConversionKind.IntPtr:
case ConversionKind.ExplicitEnumeration:
case ConversionKind.ExplicitNumeric:
case ConversionKind.ImplicitDynamic:
case ConversionKind.ExplicitDynamic:
case ConversionKind.InterpolatedString:
isTrivial = true;
break;
default:
isTrivial = false;
break;
}
Debug.Assert(isTrivial, "this conversion needs additional data: " + kind);
}
internal static Conversion GetTrivialConversion(ConversionKind kind)
{
AssertTrivialConversion(kind);
return new Conversion(kind);
}
internal static Conversion UnsetConversion => new Conversion(ConversionKind.UnsetConversionKind);
internal static Conversion NoConversion => new Conversion(ConversionKind.NoConversion);
internal static Conversion Identity => new Conversion(ConversionKind.Identity);
internal static Conversion ImplicitConstant => new Conversion(ConversionKind.ImplicitConstant);
internal static Conversion ImplicitNumeric => new Conversion(ConversionKind.ImplicitNumeric);
internal static Conversion ImplicitReference => new Conversion(ConversionKind.ImplicitReference);
internal static Conversion ImplicitEnumeration => new Conversion(ConversionKind.ImplicitEnumeration);
internal static Conversion ImplicitThrow => new Conversion(ConversionKind.ImplicitThrow);
internal static Conversion AnonymousFunction => new Conversion(ConversionKind.AnonymousFunction);
internal static Conversion Boxing => new Conversion(ConversionKind.Boxing);
internal static Conversion DefaultOrNullLiteral => new Conversion(ConversionKind.DefaultOrNullLiteral);
internal static Conversion NullToPointer => new Conversion(ConversionKind.NullToPointer);
internal static Conversion PointerToVoid => new Conversion(ConversionKind.PointerToVoid);
internal static Conversion PointerToPointer => new Conversion(ConversionKind.PointerToPointer);
internal static Conversion PointerToInteger => new Conversion(ConversionKind.PointerToInteger);
internal static Conversion IntegerToPointer => new Conversion(ConversionKind.IntegerToPointer);
internal static Conversion Unboxing => new Conversion(ConversionKind.Unboxing);
internal static Conversion ExplicitReference => new Conversion(ConversionKind.ExplicitReference);
internal static Conversion IntPtr => new Conversion(ConversionKind.IntPtr);
internal static Conversion ExplicitEnumeration => new Conversion(ConversionKind.ExplicitEnumeration);
internal static Conversion ExplicitNumeric => new Conversion(ConversionKind.ExplicitNumeric);
internal static Conversion ImplicitDynamic => new Conversion(ConversionKind.ImplicitDynamic);
internal static Conversion ExplicitDynamic => new Conversion(ConversionKind.ExplicitDynamic);
internal static Conversion InterpolatedString => new Conversion(ConversionKind.InterpolatedString);
internal static Conversion SwitchExpression => new Conversion(ConversionKind.SwitchExpression);
internal static Conversion Deconstruction => new Conversion(ConversionKind.Deconstruction);
internal static Conversion PinnedObjectToPointer => new Conversion(ConversionKind.PinnedObjectToPointer);
// trivial conversions that could be underlying in nullable conversion
// NOTE: tuple conversions can be underlying as well, but they are not trivial
internal static ImmutableArray<Conversion> IdentityUnderlying => ConversionSingletons.IdentityUnderlying;
internal static ImmutableArray<Conversion> ImplicitConstantUnderlying => ConversionSingletons.ImplicitConstantUnderlying;
internal static ImmutableArray<Conversion> ImplicitNumericUnderlying => ConversionSingletons.ImplicitNumericUnderlying;
internal static ImmutableArray<Conversion> ExplicitNumericUnderlying => ConversionSingletons.ExplicitNumericUnderlying;
internal static ImmutableArray<Conversion> ExplicitEnumerationUnderlying => ConversionSingletons.ExplicitEnumerationUnderlying;
internal static ImmutableArray<Conversion> PointerToIntegerUnderlying => ConversionSingletons.PointerToIntegerUnderlying;
// these static fields are not directly inside the Conversion
// because that causes CLR loader failure.
private static class ConversionSingletons
{
internal static ImmutableArray<Conversion> IdentityUnderlying = ImmutableArray.Create(Identity);
internal static ImmutableArray<Conversion> ImplicitConstantUnderlying = ImmutableArray.Create(ImplicitConstant);
internal static ImmutableArray<Conversion> ImplicitNumericUnderlying = ImmutableArray.Create(ImplicitNumeric);
internal static ImmutableArray<Conversion> ExplicitNumericUnderlying = ImmutableArray.Create(ExplicitNumeric);
internal static ImmutableArray<Conversion> ExplicitEnumerationUnderlying = ImmutableArray.Create(ExplicitEnumeration);
internal static ImmutableArray<Conversion> PointerToIntegerUnderlying = ImmutableArray.Create(PointerToInteger);
}
internal static Conversion MakeStackAllocToPointerType(Conversion underlyingConversion)
{
return new Conversion(ConversionKind.StackAllocToPointerType, ImmutableArray.Create(underlyingConversion));
}
internal static Conversion MakeStackAllocToSpanType(Conversion underlyingConversion)
{
return new Conversion(ConversionKind.StackAllocToSpanType, ImmutableArray.Create(underlyingConversion));
}
internal static Conversion MakeNullableConversion(ConversionKind kind, Conversion nestedConversion)
{
Debug.Assert(kind == ConversionKind.ImplicitNullable || kind == ConversionKind.ExplicitNullable);
ImmutableArray<Conversion> nested;
switch (nestedConversion.Kind)
{
case ConversionKind.Identity:
nested = IdentityUnderlying;
break;
case ConversionKind.ImplicitConstant:
nested = ImplicitConstantUnderlying;
break;
case ConversionKind.ImplicitNumeric:
nested = ImplicitNumericUnderlying;
break;
case ConversionKind.ExplicitNumeric:
nested = ExplicitNumericUnderlying;
break;
case ConversionKind.ExplicitEnumeration:
nested = ExplicitEnumerationUnderlying;
break;
case ConversionKind.PointerToInteger:
nested = PointerToIntegerUnderlying;
break;
default:
nested = ImmutableArray.Create(nestedConversion);
break;
}
return new Conversion(kind, nested);
}
internal ConversionKind Kind
{
get
{
return _kind;
}
}
internal bool IsExtensionMethod
{
get
{
return _uncommonData?.IsExtensionMethod == true;
}
}
internal bool IsArrayIndex
{
get
{
return _uncommonData?.IsArrayIndex == true;
}
}
internal ImmutableArray<Conversion> UnderlyingConversions
{
get
{
return _uncommonData?._nestedConversionsOpt ?? default(ImmutableArray<Conversion>);
}
}
internal MethodSymbol Method
{
get
{
var uncommonData = _uncommonData;
if (uncommonData != null)
{
if ((object)uncommonData._conversionMethod != null)
{
return uncommonData._conversionMethod;
}
var conversionResult = uncommonData._conversionResult;
if (conversionResult.Kind == UserDefinedConversionResultKind.Valid)
{
UserDefinedConversionAnalysis analysis = conversionResult.Results[conversionResult.Best];
return analysis.Operator;
}
if (uncommonData is DeconstructionUncommonData deconstruction
&& deconstruction.DeconstructMethodInfo.Invocation is BoundCall call)
{
return call.Method;
}
}
return null;
}
}
internal DeconstructMethodInfo DeconstructionInfo
{
get
{
var uncommonData = (DeconstructionUncommonData)_uncommonData;
return uncommonData == null ? default(DeconstructMethodInfo) : uncommonData.DeconstructMethodInfo;
}
}
// CONSIDER: public?
internal bool IsValid
{
get
{
if (!this.Exists)
{
return false;
}
var nestedConversionsOpt = _uncommonData?._nestedConversionsOpt;
if (nestedConversionsOpt != null)
{
foreach (var conv in nestedConversionsOpt)
{
if (!conv.IsValid)
{
return false;
}
}
Debug.Assert(!this.IsUserDefined);
return true;
}
return !this.IsUserDefined ||
(object)this.Method != null ||
_uncommonData?._conversionResult.Kind == UserDefinedConversionResultKind.Valid;
}
}
/// <summary>
/// Returns true if the conversion exists, either as an implicit or explicit conversion.
/// </summary>
/// <remarks>
/// The existence of a conversion does not necessarily imply that the conversion is valid.
/// For example, an ambiguous user-defined conversion may exist but may not be valid.
/// </remarks>
public bool Exists
{
get
{
return Kind != ConversionKind.NoConversion;
}
}
/// <summary>
/// Returns true if the conversion is implicit.
/// </summary>
/// <remarks>
/// Implicit conversions are described in section 6.1 of the C# language specification.
/// </remarks>
public bool IsImplicit
{
get
{
return Kind.IsImplicitConversion();
}
}
/// <summary>
/// Returns true if the conversion is explicit.
/// </summary>
/// <remarks>
/// Explicit conversions are described in section 6.2 of the C# language specification.
/// </remarks>
public bool IsExplicit
{
get
{
// All conversions are either implicit or explicit.
return Exists && !IsImplicit;
}
}
/// <summary>
/// Returns true if the conversion is an identity conversion.
/// </summary>
/// <remarks>
/// Identity conversions are described in section 6.1.1 of the C# language specification.
/// </remarks>
public bool IsIdentity
{
get
{
return Kind == ConversionKind.Identity;
}
}
/// <summary>
/// Returns true if the conversion is a stackalloc conversion.
/// </summary>
public bool IsStackAlloc
{
get
{
return Kind == ConversionKind.StackAllocToPointerType || Kind == ConversionKind.StackAllocToSpanType;
}
}
/// <summary>
/// Returns true if the conversion is an implicit numeric conversion or explicit numeric conversion.
/// </summary>
/// <remarks>
/// Implicit and explicit numeric conversions are described in sections 6.1.2 and 6.2.1 of the C# language specification.
/// </remarks>
public bool IsNumeric
{
get
{
return Kind == ConversionKind.ImplicitNumeric || Kind == ConversionKind.ExplicitNumeric;
}
}
/// <summary>
/// Returns true if the conversion is an implicit enumeration conversion or explicit enumeration conversion.
/// </summary>
/// <remarks>
/// Implicit and explicit enumeration conversions are described in sections 6.1.3 and 6.2.2 of the C# language specification.
/// </remarks>
public bool IsEnumeration
{
get
{
return Kind == ConversionKind.ImplicitEnumeration || Kind == ConversionKind.ExplicitEnumeration;
}
}
/// <summary>
/// Returns true if the conversion is an implicit throw conversion.
/// </summary>
public bool IsThrow
{
get
{
return Kind == ConversionKind.ImplicitThrow;
}
}
/// <summary>
/// Returns true if the conversion is an implicit switch expression conversion.
/// </summary>
public bool IsSwitchExpression
{
get
{
return Kind == ConversionKind.SwitchExpression;
}
}
// TODO: update the language reference section number below.
/// <summary>
/// Returns true if the conversion is an interpolated string conversion.
/// </summary>
/// <remarks>
/// The interpolated string conversion described in section 6.1.N of the C# language specification.
/// </remarks>
public bool IsInterpolatedString
{
get
{
return Kind == ConversionKind.InterpolatedString;
}
}
/// <summary>
/// Returns true if the conversion is an implicit nullable conversion or explicit nullable conversion.
/// </summary>
/// <remarks>
/// Implicit and explicit nullable conversions are described in sections 6.1.4 and 6.2.3 of the C# language specification.
/// </remarks>
public bool IsNullable
{
get
{
return Kind == ConversionKind.ImplicitNullable || Kind == ConversionKind.ExplicitNullable;
}
}
/// <summary>
/// Returns true if the conversion is an implicit tuple literal conversion or explicit tuple literal conversion.
/// </summary>
public bool IsTupleLiteralConversion
{
get
{
return Kind == ConversionKind.ImplicitTupleLiteral || Kind == ConversionKind.ExplicitTupleLiteral;
}
}
/// <summary>
/// Returns true if the conversion is an implicit tuple conversion or explicit tuple conversion.
/// </summary>
public bool IsTupleConversion
{
get
{
return Kind == ConversionKind.ImplicitTuple || Kind == ConversionKind.ExplicitTuple;
}
}
/// <summary>
/// Returns true if the conversion is an implicit reference conversion or explicit reference conversion.
/// </summary>
/// <remarks>
/// Implicit and explicit reference conversions are described in sections 6.1.6 and 6.2.4 of the C# language specification.
/// </remarks>
public bool IsReference
{
get
{
return Kind == ConversionKind.ImplicitReference || Kind == ConversionKind.ExplicitReference;
}
}
/// <summary>
/// Returns true if the conversion is an implicit user-defined conversion or explicit user-defined conversion.
/// </summary>
/// <remarks>
/// Implicit and explicit user-defined conversions are described in section 6.4 of the C# language specification.
/// </remarks>
public bool IsUserDefined
{
get
{
return Kind.IsUserDefinedConversion();
}
}
/// <summary>
/// Returns true if the conversion is an implicit boxing conversion.
/// </summary>
/// <remarks>
/// Implicit boxing conversions are described in section 6.1.7 of the C# language specification.
/// </remarks>
public bool IsBoxing
{
get
{
return Kind == ConversionKind.Boxing;
}
}
/// <summary>
/// Returns true if the conversion is an explicit unboxing conversion.
/// </summary>
/// <remarks>
/// Explicit unboxing conversions as described in section 6.2.5 of the C# language specification.
/// </remarks>
public bool IsUnboxing
{
get
{
return Kind == ConversionKind.Unboxing;
}
}
/// <summary>
/// Returns true if the conversion is an implicit null or default literal conversion.
/// </summary>
/// <remarks>
/// Null or default literal conversions are described in section 6.1.5 of the C# language specification.
/// </remarks>
public bool IsNullLiteral
{
get
{
return Kind == ConversionKind.DefaultOrNullLiteral;
}
}
/// <summary>
/// Returns true if the conversion is an implicit dynamic conversion.
/// </summary>
/// <remarks>
/// Implicit dynamic conversions are described in section 6.1.8 of the C# language specification.
/// </remarks>
public bool IsDynamic
{
get
{
return Kind.IsDynamic();
}
}
/// <summary>
/// Returns true if the conversion is an implicit constant expression conversion.
/// </summary>
/// <remarks>
/// Implicit constant expression conversions are described in section 6.1.9 of the C# language specification.
/// </remarks>
public bool IsConstantExpression
{
get
{
return Kind == ConversionKind.ImplicitConstant;
}
}
/// <summary>
/// Returns true if the conversion is an implicit anonymous function conversion.
/// </summary>
/// <remarks>
/// Implicit anonymous function conversions are described in section 6.5 of the C# language specification.
/// </remarks>
public bool IsAnonymousFunction
{
get
{
return Kind == ConversionKind.AnonymousFunction;
}
}
/// <summary>
/// Returns true if the conversion is an implicit method group conversion.
/// </summary>
/// <remarks>
/// Implicit method group conversions are described in section 6.6 of the C# language specification.
/// </remarks>
public bool IsMethodGroup
{
get
{
return Kind == ConversionKind.MethodGroup;
}
}
/// <summary>
/// Returns true if the conversion is a pointer conversion
/// </summary>
/// <remarks>
/// Pointer conversions are described in section 18.4 of the C# language specification.
///
/// Returns true if the conversion is a conversion
/// a) from a pointer type to void*,
/// b) from a pointer type to another pointer type (other than void*),
/// c) from the null literal to a pointer type,
/// d) from an integral numeric type to a pointer type, or
/// e) from a pointer type to an integral numeric type.
///
/// Does not return true for user-defined conversions to/from pointer types.
/// Does not return true for conversions between pointer types and IntPtr/UIntPtr.
/// </remarks>
public bool IsPointer
{
get
{
return this.Kind.IsPointerConversion();
}
}
/// <summary>
/// Returns true if the conversion is a conversion to or from IntPtr or UIntPtr.
/// </summary>
/// <remarks>
/// Returns true if the conversion is a conversion to or from IntPtr or UIntPtr.
/// This includes:
/// IntPtr to/from int
/// IntPtr to/from long
/// IntPtr to/from void*
/// UIntPtr to/from int
/// UIntPtr to/from long
/// UIntPtr to/from void*
/// </remarks>
public bool IsIntPtr
{
get
{
return Kind == ConversionKind.IntPtr;
}
}
/// <summary>
/// Returns the method used to create the delegate for a method group conversion if <see cref="IsMethodGroup"/> is true
/// or the method used to perform the conversion for a user-defined conversion if <see cref="IsUserDefined"/> is true.
/// Otherwise, returns null.
/// </summary>
/// <remarks>
/// Method group conversions are described in section 6.6 of the C# language specification.
/// User-defined conversions are described in section 6.4 of the C# language specification.
/// </remarks>
public IMethodSymbol MethodSymbol
{
get
{
return this.Method;
}
}
/// <summary>
/// Gives an indication of how successful the conversion was.
/// Viable - found a best built-in or user-defined conversion.
/// Empty - found no applicable built-in or user-defined conversions.
/// OverloadResolutionFailure - found applicable conversions, but no unique best.
/// </summary>
internal LookupResultKind ResultKind
{
get
{
var conversionResult = _uncommonData?._conversionResult ?? default(UserDefinedConversionResult);
switch (conversionResult.Kind)
{
case UserDefinedConversionResultKind.Valid:
return LookupResultKind.Viable;
case UserDefinedConversionResultKind.Ambiguous:
case UserDefinedConversionResultKind.NoBestSourceType:
case UserDefinedConversionResultKind.NoBestTargetType:
return LookupResultKind.OverloadResolutionFailure;
case UserDefinedConversionResultKind.NoApplicableOperators:
if (conversionResult.Results.IsDefaultOrEmpty)
{
return this.Kind == ConversionKind.NoConversion ? LookupResultKind.Empty : LookupResultKind.Viable;
}
else
{
// CONSIDER: indicating an overload resolution failure is sufficient,
// but it would be nice to indicate lack of accessibility or other
// error conditions.
return LookupResultKind.OverloadResolutionFailure;
}
default:
throw ExceptionUtilities.UnexpectedValue(conversionResult.Kind);
}
}
}
/// <summary>
/// Conversion applied to operand of the user-defined conversion.
/// </summary>
internal Conversion UserDefinedFromConversion
{
get
{
UserDefinedConversionAnalysis best = BestUserDefinedConversionAnalysis;
return best == null ? Conversion.NoConversion : best.SourceConversion;
}
}
/// <summary>
/// Conversion applied to the result of the user-defined conversion.
/// </summary>
internal Conversion UserDefinedToConversion
{
get
{
UserDefinedConversionAnalysis best = BestUserDefinedConversionAnalysis;
return best == null ? Conversion.NoConversion : best.TargetConversion;
}
}
/// <summary>
/// The user-defined operators that were considered when attempting this conversion
/// (i.e. the arguments to overload resolution).
/// </summary>
internal ImmutableArray<MethodSymbol> OriginalUserDefinedConversions
{
get
{
// If overload resolution has failed then we want to stash away the original methods that we
// considered so that the IDE can display tooltips or other information about them.
// However, if a method group contained a generic method that was type inferred then
// the IDE wants information about the *inferred* method, not the original unconstructed
// generic method.
if (_uncommonData == null)
{
return ImmutableArray<MethodSymbol>.Empty;
}
var conversionResult = _uncommonData._conversionResult;
if (conversionResult.Kind == UserDefinedConversionResultKind.NoApplicableOperators)
{
return ImmutableArray<MethodSymbol>.Empty;
}
var builder = ArrayBuilder<MethodSymbol>.GetInstance();
foreach (var analysis in conversionResult.Results)
{
builder.Add(analysis.Operator);
}
return builder.ToImmutableAndFree();
}
}
internal UserDefinedConversionAnalysis BestUserDefinedConversionAnalysis
{
get
{
if (_uncommonData == null)
{
return null;
}
var conversionResult = _uncommonData._conversionResult;
if (conversionResult.Kind == UserDefinedConversionResultKind.Valid)
{
UserDefinedConversionAnalysis analysis = conversionResult.Results[conversionResult.Best];
return analysis;
}
return null;
}
}
/// <summary>
/// Creates a <seealso cref="CommonConversion"/> from this C# conversion.
/// </summary>
/// <returns>The <see cref="CommonConversion"/> that represents this conversion.</returns>
/// <remarks>
/// This is a lossy conversion; it is not possible to recover the original <see cref="Conversion"/>
/// from the <see cref="CommonConversion"/> struct.
/// </remarks>
public CommonConversion ToCommonConversion()
{
// The MethodSymbol of CommonConversion only refers to UserDefined conversions, not method groups
var methodSymbol = IsUserDefined ? MethodSymbol : null;
return new CommonConversion(Exists, IsIdentity, IsNumeric, IsReference, IsImplicit, methodSymbol);
}
/// <summary>
/// Returns a string that represents the <see cref="Kind"/> of the conversion.
/// </summary>
/// <returns>A string that represents the <see cref="Kind"/> of the conversion.</returns>
public override string ToString()
{
return this.Kind.ToString();
}
/// <summary>
/// Determines whether the specified <see cref="Conversion"/> object is equal to the current <see cref="Conversion"/> object.
/// </summary>
/// <param name="obj">The <see cref="Conversion"/> object to compare with the current <see cref="Conversion"/> object.</param>
/// <returns>true if the specified <see cref="Conversion"/> object is equal to the current <see cref="Conversion"/> object; otherwise, false.</returns>
public override bool Equals(object obj)
{
return obj is Conversion && this.Equals((Conversion)obj);
}
/// <summary>
/// Determines whether the specified <see cref="Conversion"/> object is equal to the current <see cref="Conversion"/> object.
/// </summary>
/// <param name="other">The <see cref="Conversion"/> object to compare with the current <see cref="Conversion"/> object.</param>
/// <returns>true if the specified <see cref="Conversion"/> object is equal to the current <see cref="Conversion"/> object; otherwise, false.</returns>
public bool Equals(Conversion other)
{
return this.Kind == other.Kind && this.Method == other.Method;
}
/// <summary>
/// Returns a hash code for the current <see cref="Conversion"/> object.
/// </summary>
/// <returns>A hash code for the current <see cref="Conversion"/> object.</returns>
public override int GetHashCode()
{
return Hash.Combine(this.Method, (int)this.Kind);
}
/// <summary>
/// Returns true if the specified <see cref="Conversion"/> objects are equal and false otherwise.
/// </summary>
/// <param name="left">The first <see cref="Conversion"/> object.</param>
/// <param name="right">The second <see cref="Conversion"/> object.</param>
/// <returns></returns>
public static bool operator ==(Conversion left, Conversion right)
{
return left.Equals(right);
}
/// <summary>
/// Returns false if the specified <see cref="Conversion"/> objects are equal and true otherwise.
/// </summary>
/// <param name="left">The first <see cref="Conversion"/> object.</param>
/// <param name="right">The second <see cref="Conversion"/> object.</param>
/// <returns></returns>
public static bool operator !=(Conversion left, Conversion right)
{
return !(left == right);
}
#if DEBUG
internal string Dump()
{
return TreeDumper.DumpCompact(Dump(this));
TreeDumperNode Dump(Conversion self)
{
var sub = new System.Collections.Generic.List<TreeDumperNode>();
if ((object)self.Method != null)
{
sub.Add(new TreeDumperNode("method", self.Method.ToDisplayString(), null));
}
if (!self.DeconstructionInfo.IsDefault)
{
sub.Add(new TreeDumperNode("deconstructionInfo", null,
new[] { BoundTreeDumperNodeProducer.MakeTree(self.DeconstructionInfo.Invocation) }));
}
var underlyingConversions = self.UnderlyingConversions;
if (!underlyingConversions.IsDefaultOrEmpty)
{
sub.Add(new TreeDumperNode($"underlyingConversions[{underlyingConversions.Length}]", null,
underlyingConversions.SelectAsArray(c => Dump(c))));
}
return new TreeDumperNode("conversion", self.Kind, sub);
}
}
#endif
}
/// <summary>Stores all the information from binding for calling a Deconstruct method.</summary>
internal struct DeconstructMethodInfo
{
internal DeconstructMethodInfo(BoundExpression invocation, BoundDeconstructValuePlaceholder inputPlaceholder,
ImmutableArray<BoundDeconstructValuePlaceholder> outputPlaceholders)
{
(Invocation, InputPlaceholder, OutputPlaceholders) = (invocation, inputPlaceholder, outputPlaceholders);
}