forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSourceUserDefinedOperatorSymbolBase.cs
1004 lines (868 loc) · 48.2 KB
/
SourceUserDefinedOperatorSymbolBase.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal abstract class SourceUserDefinedOperatorSymbolBase : SourceOrdinaryMethodOrUserDefinedOperatorSymbol
{
// tomat: ignoreDynamic should be true, but we don't want to introduce breaking change. See bug 605326.
private const TypeCompareKind ComparisonForUserDefinedOperators = TypeCompareKind.IgnoreTupleNames | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes;
private readonly string _name;
#nullable enable
private readonly TypeSymbol? _explicitInterfaceType;
#nullable disable
protected SourceUserDefinedOperatorSymbolBase(
MethodKind methodKind,
TypeSymbol explicitInterfaceType,
string name,
SourceMemberContainerTypeSymbol containingType,
Location location,
CSharpSyntaxNode syntax,
DeclarationModifiers declarationModifiers,
bool hasAnyBody,
bool isExpressionBodied,
bool isIterator,
bool isNullableAnalysisEnabled,
BindingDiagnosticBag diagnostics) :
base(containingType, syntax.GetReference(), location, isIterator: isIterator,
(declarationModifiers, MakeFlags(
methodKind, RefKind.None, declarationModifiers,
// We will bind the formal parameters and the return type lazily. For now,
// assume that the return type is non-void; when we do the lazy initialization
// of the parameters and return type we will update the flag if necessary.
returnsVoid: false,
returnsVoidIsSet: false,
isExpressionBodied: isExpressionBodied,
isExtensionMethod: false, isVarArg: false, isNullableAnalysisEnabled: isNullableAnalysisEnabled,
isExplicitInterfaceImplementation: methodKind == MethodKind.ExplicitInterfaceImplementation,
hasThisInitializer: false)))
{
_explicitInterfaceType = explicitInterfaceType;
_name = name;
this.CheckUnsafeModifier(declarationModifiers, diagnostics);
(bool isIncrementDecrement, bool isCompoundAssignment) = IsAssignmentOperatorDeclaration(syntax);
if (isIncrementDecrement)
{
int parameterCount = ((OperatorDeclarationSyntax)syntax).ParameterList.ParameterCount;
if (this.IsStatic)
{
if (parameterCount is not (1 or 2))
{
diagnostics.Add(ErrorCode.ERR_BadUnOpArgs, this.GetFirstLocation(), SyntaxFacts.GetText(((OperatorDeclarationSyntax)syntax).OperatorToken.Kind()));
}
}
else
{
// PROTOTYPE: Cover explicit implementation scenarios
Binder.CheckFeatureAvailability(syntax, MessageID.IDS_FeatureUserDefinedCompoundAssignmentOperators, diagnostics, ((OperatorDeclarationSyntax)syntax).OperatorToken.GetLocation());
if (parameterCount is not (0 or 2))
{
diagnostics.Add(ErrorCode.ERR_BadIncrementOpArgs, this.GetFirstLocation(), SyntaxFacts.GetText(((OperatorDeclarationSyntax)syntax).OperatorToken.Kind()));
}
}
}
else if (isCompoundAssignment)
{
// PROTOTYPE: Cover explicit implementation scenarios
Binder.CheckFeatureAvailability(syntax, MessageID.IDS_FeatureUserDefinedCompoundAssignmentOperators, diagnostics, ((OperatorDeclarationSyntax)syntax).OperatorToken.GetLocation());
}
if (this.ContainingType.IsInterface &&
!(IsAbstract || IsVirtual) && !IsExplicitInterfaceImplementation &&
!(syntax is OperatorDeclarationSyntax { OperatorToken: var opToken } && opToken.Kind() is not (SyntaxKind.EqualsEqualsToken or SyntaxKind.ExclamationEqualsToken)))
{
diagnostics.Add(ErrorCode.ERR_InterfacesCantContainConversionOrEqualityOperators, this.GetFirstLocation());
// No need to cascade the error further.
return;
}
if (this.ContainingType.IsStatic)
{
// Similarly if we're in a static class, though we have not reported it yet.
// CS0715: '{0}': static classes cannot contain user-defined operators
diagnostics.Add(ErrorCode.ERR_OperatorInStaticClass, location, this);
return;
}
// SPEC: An operator declaration must include both a public and a
// SPEC: static modifier
if (this.IsExplicitInterfaceImplementation)
{
if (!this.IsStatic && !isIncrementDecrement && !isCompoundAssignment)
{
diagnostics.Add(ErrorCode.ERR_ExplicitImplementationOfOperatorsMustBeStatic, this.GetFirstLocation(), this);
}
}
else if ((isIncrementDecrement || isCompoundAssignment) && !this.IsStatic)
{
if (this.DeclaredAccessibility != Accessibility.Public)
{
diagnostics.Add(ErrorCode.ERR_OperatorsMustBePublic, this.GetFirstLocation(), this);
}
}
else if (this.DeclaredAccessibility != Accessibility.Public || !this.IsStatic)
{
// CS0558: User-defined operator '...' must be declared static and public
diagnostics.Add(ErrorCode.ERR_OperatorsMustBeStaticAndPublic, this.GetFirstLocation(), this);
}
// SPEC: Because an external operator provides no actual implementation,
// SPEC: its operator body consists of a semicolon. For expression-bodied
// SPEC: operators, the body is an expression. For all other operators,
// SPEC: the operator body consists of a block...
if (IsAbstract && IsExtern)
{
diagnostics.Add(ErrorCode.ERR_AbstractAndExtern, location, this);
}
else if (IsAbstract && IsVirtual)
{
diagnostics.Add(ErrorCode.ERR_AbstractNotVirtual, location, this.Kind.Localize(), this);
}
else if (hasAnyBody && (IsExtern || IsAbstract))
{
Debug.Assert(!(IsAbstract && IsExtern));
if (IsExtern)
{
diagnostics.Add(ErrorCode.ERR_ExternHasBody, location, this);
}
else
{
diagnostics.Add(ErrorCode.ERR_AbstractHasBody, location, this);
}
}
else if (!hasAnyBody && !IsExtern && !IsAbstract && !IsPartial)
{
// Do not report that the body is missing if the operator is marked as
// partial or abstract; we will already have given an error for that so
// there's no need to "cascade" the error.
diagnostics.Add(ErrorCode.ERR_ConcreteMissingBody, location, this);
}
else if (IsOverride && (IsNew || IsVirtual))
{
// A member '{0}' marked as override cannot be marked as new or virtual
diagnostics.Add(ErrorCode.ERR_OverrideNotNew, location, this);
}
else if (IsSealed && !IsOverride && !(IsExplicitInterfaceImplementation && ContainingType.IsInterface && IsAbstract))
{
// '{0}' cannot be sealed because it is not an override
diagnostics.Add(ErrorCode.ERR_SealedNonOverride, location, this);
}
else if (IsAbstract && IsSealed && !IsExplicitInterfaceImplementation)
{
diagnostics.Add(ErrorCode.ERR_AbstractAndSealed, location, this);
}
else if (IsAbstract && !ContainingType.IsAbstract && !ContainingType.IsInterface)
{
// '{0}' is abstract but it is contained in non-abstract type '{1}'
diagnostics.Add(ErrorCode.ERR_AbstractInConcreteClass, location, this, ContainingType);
}
else if (IsVirtual && ContainingType.IsSealed)
{
// '{0}' is a new virtual member in sealed type '{1}'
diagnostics.Add(ErrorCode.ERR_NewVirtualInSealed, location, this, ContainingType);
}
// SPEC: It is an error for the same modifier to appear multiple times in an
// SPEC: operator declaration.
ModifierUtils.CheckAccessibility(this.DeclarationModifiers, this, isExplicitInterfaceImplementation: false, diagnostics, location);
if (!IsStatic && (isIncrementDecrement || isCompoundAssignment))
{
_ = Binder.GetWellKnownTypeMember(DeclaringCompilation, WellKnownMember.System_Runtime_CompilerServices_CompilerFeatureRequiredAttribute__ctor, diagnostics, location);
}
}
private static (bool isIncrementDecrement, bool isCompoundAssignment) IsAssignmentOperatorDeclaration(CSharpSyntaxNode syntax)
{
if (syntax is OperatorDeclarationSyntax operatorDeclaration)
{
if (operatorDeclaration.OperatorToken.Kind() is SyntaxKind.PlusPlusToken or SyntaxKind.MinusMinusToken)
{
return (true, false);
}
else if (SyntaxFacts.IsOverloadableCompoundAssignmentOperator(operatorDeclaration.OperatorToken.Kind()))
{
return (false, true);
}
}
return (false, false);
}
protected static DeclarationModifiers MakeDeclarationModifiers(MethodKind methodKind, SourceMemberContainerTypeSymbol containingType, BaseMethodDeclarationSyntax syntax, Location location, BindingDiagnosticBag diagnostics)
{
bool inInterface = containingType.IsInterface;
bool isExplicitInterfaceImplementation = methodKind == MethodKind.ExplicitInterfaceImplementation;
var defaultAccess = inInterface && !isExplicitInterfaceImplementation ? DeclarationModifiers.Public : DeclarationModifiers.Private;
var allowedModifiers =
DeclarationModifiers.Static |
DeclarationModifiers.Extern |
DeclarationModifiers.Unsafe;
(bool isIncrementDecrement, bool isCompoundAssignment) = IsAssignmentOperatorDeclaration(syntax);
if (!isExplicitInterfaceImplementation)
{
allowedModifiers |= DeclarationModifiers.AccessibilityMask;
if (inInterface)
{
allowedModifiers |= DeclarationModifiers.Abstract | DeclarationModifiers.Virtual;
if (syntax is OperatorDeclarationSyntax { OperatorToken: var opToken } && opToken.Kind() is not (SyntaxKind.EqualsEqualsToken or SyntaxKind.ExclamationEqualsToken))
{
allowedModifiers |= DeclarationModifiers.Sealed;
}
}
if (isIncrementDecrement || isCompoundAssignment)
{
if (inInterface)
{
allowedModifiers |= DeclarationModifiers.New;
}
else
{
if (containingType.IsClassType())
{
allowedModifiers |= DeclarationModifiers.Abstract | DeclarationModifiers.Virtual | DeclarationModifiers.Sealed;
}
allowedModifiers |= DeclarationModifiers.Override | DeclarationModifiers.New;
}
}
}
else if (inInterface)
{
Debug.Assert(isExplicitInterfaceImplementation);
allowedModifiers |= DeclarationModifiers.Abstract;
}
if (isCompoundAssignment)
{
allowedModifiers &= ~DeclarationModifiers.Static;
}
if (containingType.IsStructType() && (isIncrementDecrement || isCompoundAssignment))
{
allowedModifiers |= DeclarationModifiers.ReadOnly;
}
var result = ModifierUtils.MakeAndCheckNonTypeMemberModifiers(
isOrdinaryMethod: false, isForInterfaceMember: inInterface,
syntax.Modifiers, defaultAccess, allowedModifiers, location, diagnostics, modifierErrors: out _, hasExplicitAccessModifier: out _);
if (inInterface)
{
if ((result & (DeclarationModifiers.Abstract | DeclarationModifiers.Virtual | DeclarationModifiers.Sealed)) != 0)
{
if ((result & DeclarationModifiers.Sealed) != 0 &&
(result & (DeclarationModifiers.Abstract | DeclarationModifiers.Virtual)) != 0)
{
diagnostics.Add(ErrorCode.ERR_BadMemberFlag, location, ModifierUtils.ConvertSingleModifierToSyntaxText(DeclarationModifiers.Sealed));
result &= ~DeclarationModifiers.Sealed;
}
LanguageVersion availableVersion = ((CSharpParseOptions)location.SourceTree.Options).LanguageVersion;
LanguageVersion requiredVersion = MessageID.IDS_FeatureStaticAbstractMembersInInterfaces.RequiredVersion();
if (availableVersion < requiredVersion)
{
var requiredVersionArgument = new CSharpRequiredLanguageVersion(requiredVersion);
var availableVersionArgument = availableVersion.ToDisplayString();
if ((result & DeclarationModifiers.Abstract) != 0)
{
reportModifierIfPresent(result, DeclarationModifiers.Abstract, location, diagnostics, requiredVersionArgument, availableVersionArgument);
}
else
{
reportModifierIfPresent(result, DeclarationModifiers.Virtual, location, diagnostics, requiredVersionArgument, availableVersionArgument);
}
reportModifierIfPresent(result, DeclarationModifiers.Sealed, location, diagnostics, requiredVersionArgument, availableVersionArgument);
}
result &= ~DeclarationModifiers.Sealed;
}
else if ((result & DeclarationModifiers.Static) != 0)
{
if (syntax is OperatorDeclarationSyntax { OperatorToken: var opToken } && opToken.Kind() is not (SyntaxKind.EqualsEqualsToken or SyntaxKind.ExclamationEqualsToken))
{
Binder.CheckFeatureAvailability(location.SourceTree, MessageID.IDS_DefaultInterfaceImplementation, diagnostics, location);
}
}
else if (!isExplicitInterfaceImplementation && (isIncrementDecrement || isCompoundAssignment))
{
if (syntax.HasAnyBody())
{
result |= DeclarationModifiers.Virtual;
}
else
{
result |= DeclarationModifiers.Abstract;
}
}
}
if ((result & DeclarationModifiers.Static) != 0)
{
Debug.Assert((result & DeclarationModifiers.Override) == 0);
if ((result & DeclarationModifiers.New) != 0)
{
diagnostics.Add(ErrorCode.ERR_BadMemberFlag, location, ModifierUtils.ConvertSingleModifierToSyntaxText(DeclarationModifiers.New));
result &= ~DeclarationModifiers.New;
}
if ((result & DeclarationModifiers.ReadOnly) != 0)
{
diagnostics.Add(ErrorCode.ERR_BadMemberFlag, location, ModifierUtils.ConvertSingleModifierToSyntaxText(DeclarationModifiers.ReadOnly));
result &= ~DeclarationModifiers.ReadOnly;
}
}
if (isExplicitInterfaceImplementation)
{
if ((result & DeclarationModifiers.Abstract) != 0)
{
result |= DeclarationModifiers.Sealed;
}
}
return result;
static void reportModifierIfPresent(DeclarationModifiers result, DeclarationModifiers errorModifier, Location location, BindingDiagnosticBag diagnostics, CSharpRequiredLanguageVersion requiredVersionArgument, string availableVersionArgument)
{
if ((result & errorModifier) != 0)
{
diagnostics.Add(ErrorCode.ERR_InvalidModifierForLanguageVersion, location,
ModifierUtils.ConvertSingleModifierToSyntaxText(errorModifier),
availableVersionArgument,
requiredVersionArgument);
}
}
}
protected (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymbol> Parameters) MakeParametersAndBindReturnType(BaseMethodDeclarationSyntax declarationSyntax, TypeSyntax returnTypeSyntax, BindingDiagnosticBag diagnostics)
{
TypeWithAnnotations returnType;
ImmutableArray<ParameterSymbol> parameters;
var binder = this.DeclaringCompilation.
GetBinderFactory(declarationSyntax.SyntaxTree).GetBinder(returnTypeSyntax, declarationSyntax, this);
SyntaxToken arglistToken;
var signatureBinder = binder.WithAdditionalFlags(BinderFlags.SuppressConstraintChecks);
parameters = ParameterHelpers.MakeParameters(
signatureBinder,
this,
declarationSyntax.ParameterList,
out arglistToken,
allowRefOrOut: true,
allowThis: false,
addRefReadOnlyModifier: IsVirtual || IsAbstract,
diagnostics: diagnostics).Cast<SourceParameterSymbol, ParameterSymbol>();
if (arglistToken.Kind() == SyntaxKind.ArgListKeyword)
{
// This is a parse-time error in the native compiler; it is a semantic analysis error in Roslyn.
// error CS1669: __arglist is not valid in this context
diagnostics.Add(ErrorCode.ERR_IllegalVarArgs, new SourceLocation(arglistToken));
// Regardless of whether __arglist appears in the source code, we do not mark
// the operator method as being a varargs method.
}
returnType = signatureBinder.BindType(returnTypeSyntax, diagnostics);
// restricted types cannot be returned.
// NOTE: Span-like types can be returned (if expression is returnable).
if (returnType.IsRestrictedType(ignoreSpanLikeTypes: true))
{
// The return type of a method, delegate, or function pointer cannot be '{0}'
diagnostics.Add(ErrorCode.ERR_MethodReturnCantBeRefAny, returnTypeSyntax.Location, returnType.Type);
}
if (returnType.Type.IsStatic)
{
// Operators in interfaces was introduced in C# 8, so there's no need to be specially concerned about
// maintaining backcompat with the native compiler bug around interfaces.
// '{0}': static types cannot be used as return types
diagnostics.Add(ErrorFacts.GetStaticClassReturnCode(useWarning: false), returnTypeSyntax.Location, returnType.Type);
}
return (returnType, parameters);
}
protected override void MethodChecks(BindingDiagnosticBag diagnostics)
{
var (returnType, parameters) = MakeParametersAndBindReturnType(diagnostics);
MethodChecks(returnType, parameters, diagnostics);
// If we have a static class then we already
// have reported that fact as an error. No need to cascade the error further.
if (this.ContainingType.IsStatic)
{
return;
}
CheckValueParameters(diagnostics);
CheckOperatorSignatures(diagnostics);
}
protected abstract (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymbol> Parameters) MakeParametersAndBindReturnType(BindingDiagnosticBag diagnostics);
protected sealed override void ExtensionMethodChecks(BindingDiagnosticBag diagnostics)
{
}
protected sealed override MethodSymbol FindExplicitlyImplementedMethod(BindingDiagnosticBag diagnostics)
{
if (_explicitInterfaceType is object)
{
string interfaceMethodName;
ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier;
switch (syntaxReferenceOpt.GetSyntax())
{
case OperatorDeclarationSyntax operatorDeclaration:
interfaceMethodName = OperatorFacts.OperatorNameFromDeclaration(operatorDeclaration);
explicitInterfaceSpecifier = operatorDeclaration.ExplicitInterfaceSpecifier;
break;
case ConversionOperatorDeclarationSyntax conversionDeclaration:
interfaceMethodName = OperatorFacts.OperatorNameFromDeclaration(conversionDeclaration);
explicitInterfaceSpecifier = conversionDeclaration.ExplicitInterfaceSpecifier;
break;
default:
throw ExceptionUtilities.Unreachable();
}
return this.FindExplicitlyImplementedMethod(isOperator: true, _explicitInterfaceType, interfaceMethodName, explicitInterfaceSpecifier, diagnostics);
}
return null;
}
#nullable enable
protected sealed override TypeSymbol? ExplicitInterfaceType => _explicitInterfaceType;
#nullable disable
private void CheckValueParameters(BindingDiagnosticBag diagnostics)
{
// SPEC: The parameters of an operator must be value parameters.
foreach (var p in this.Parameters)
{
if (p.RefKind != RefKind.None && p.RefKind != RefKind.In)
{
diagnostics.Add(ErrorCode.ERR_IllegalRefParam, this.GetFirstLocation());
break;
}
}
}
private void CheckOperatorSignatures(BindingDiagnosticBag diagnostics)
{
if (MethodKind == MethodKind.ExplicitInterfaceImplementation)
{
// The signature is driven by the interface
return;
}
// Have we even got the right formal parameter arity? If not then
// we are in an error recovery scenario and we should just bail
// out immediately.
if (!DoesOperatorHaveCorrectArity(this.Name, this.ParameterCount))
{
return;
}
switch (this.Name)
{
case WellKnownMemberNames.ImplicitConversionName:
case WellKnownMemberNames.ExplicitConversionName:
case WellKnownMemberNames.CheckedExplicitConversionName:
CheckUserDefinedConversionSignature(diagnostics);
break;
case WellKnownMemberNames.CheckedUnaryNegationOperatorName:
case WellKnownMemberNames.UnaryNegationOperatorName:
case WellKnownMemberNames.UnaryPlusOperatorName:
case WellKnownMemberNames.LogicalNotOperatorName:
case WellKnownMemberNames.OnesComplementOperatorName:
CheckUnarySignature(diagnostics);
break;
case WellKnownMemberNames.TrueOperatorName:
case WellKnownMemberNames.FalseOperatorName:
CheckTrueFalseSignature(diagnostics);
break;
case WellKnownMemberNames.CheckedIncrementOperatorName:
case WellKnownMemberNames.IncrementOperatorName:
case WellKnownMemberNames.CheckedDecrementOperatorName:
case WellKnownMemberNames.DecrementOperatorName:
CheckIncrementDecrementSignature(diagnostics);
break;
case WellKnownMemberNames.LeftShiftOperatorName:
case WellKnownMemberNames.RightShiftOperatorName:
case WellKnownMemberNames.UnsignedRightShiftOperatorName:
CheckShiftSignature(diagnostics);
break;
case WellKnownMemberNames.EqualityOperatorName:
case WellKnownMemberNames.InequalityOperatorName:
if (IsAbstract || IsVirtual)
{
CheckAbstractEqualitySignature(diagnostics);
}
else
{
CheckBinarySignature(diagnostics);
}
break;
case WellKnownMemberNames.CheckedAdditionAssignmentOperatorName:
case WellKnownMemberNames.AdditionAssignmentOperatorName:
case WellKnownMemberNames.CheckedDivisionAssignmentOperatorName:
case WellKnownMemberNames.DivisionAssignmentOperatorName:
case WellKnownMemberNames.CheckedMultiplicationAssignmentOperatorName:
case WellKnownMemberNames.MultiplicationAssignmentOperatorName:
case WellKnownMemberNames.CheckedSubtractionAssignmentOperatorName:
case WellKnownMemberNames.SubtractionAssignmentOperatorName:
case WellKnownMemberNames.ModulusAssignmentOperatorName:
case WellKnownMemberNames.BitwiseAndAssignmentOperatorName:
case WellKnownMemberNames.BitwiseOrAssignmentOperatorName:
case WellKnownMemberNames.ExclusiveOrAssignmentOperatorName:
case WellKnownMemberNames.LeftShiftAssignmentOperatorName:
case WellKnownMemberNames.RightShiftAssignmentOperatorName:
case WellKnownMemberNames.UnsignedRightShiftAssignmentOperatorName:
if (!this.ReturnsVoid)
{
diagnostics.Add(ErrorCode.ERR_OperatorMustReturnVoid, this.GetFirstLocation());
}
// PROTOTYPE: Test/handle scenarios with ref modifiers on the parameter
break;
default:
CheckBinarySignature(diagnostics);
break;
}
}
private bool DoesOperatorHaveCorrectArity(string name, int parameterCount)
{
switch (name)
{
case WellKnownMemberNames.CheckedIncrementOperatorName:
case WellKnownMemberNames.IncrementOperatorName:
case WellKnownMemberNames.CheckedDecrementOperatorName:
case WellKnownMemberNames.DecrementOperatorName:
return parameterCount == (IsStatic ? 1 : 0);
case WellKnownMemberNames.CheckedUnaryNegationOperatorName:
case WellKnownMemberNames.UnaryNegationOperatorName:
case WellKnownMemberNames.UnaryPlusOperatorName:
case WellKnownMemberNames.LogicalNotOperatorName:
case WellKnownMemberNames.OnesComplementOperatorName:
case WellKnownMemberNames.TrueOperatorName:
case WellKnownMemberNames.FalseOperatorName:
case WellKnownMemberNames.ImplicitConversionName:
case WellKnownMemberNames.ExplicitConversionName:
case WellKnownMemberNames.CheckedExplicitConversionName:
case WellKnownMemberNames.CheckedAdditionAssignmentOperatorName:
case WellKnownMemberNames.AdditionAssignmentOperatorName:
case WellKnownMemberNames.CheckedDivisionAssignmentOperatorName:
case WellKnownMemberNames.DivisionAssignmentOperatorName:
case WellKnownMemberNames.CheckedMultiplicationAssignmentOperatorName:
case WellKnownMemberNames.MultiplicationAssignmentOperatorName:
case WellKnownMemberNames.CheckedSubtractionAssignmentOperatorName:
case WellKnownMemberNames.SubtractionAssignmentOperatorName:
case WellKnownMemberNames.ModulusAssignmentOperatorName:
case WellKnownMemberNames.BitwiseAndAssignmentOperatorName:
case WellKnownMemberNames.BitwiseOrAssignmentOperatorName:
case WellKnownMemberNames.ExclusiveOrAssignmentOperatorName:
case WellKnownMemberNames.LeftShiftAssignmentOperatorName:
case WellKnownMemberNames.RightShiftAssignmentOperatorName:
case WellKnownMemberNames.UnsignedRightShiftAssignmentOperatorName:
return parameterCount == 1;
default:
return parameterCount == 2;
}
}
private void CheckUserDefinedConversionSignature(BindingDiagnosticBag diagnostics)
{
CheckReturnIsNotVoid(diagnostics);
// SPEC: For a given source type S and target type T, if S or T are
// SPEC: nullable types let S0 and T0 refer to their underlying types,
// SPEC: otherwise, S0 and T0 are equal to S and T, respectively.
var source = this.GetParameterType(0);
var target = this.ReturnType;
var source0 = source.StrippedType();
var target0 = target.StrippedType();
// SPEC: A class or struct is permitted to declare a conversion from S to T
// SPEC: only if all the following are true:
// SPEC: Neither S0 nor T0 is an interface type.
if (source0.IsInterfaceType() || target0.IsInterfaceType())
{
// CS0552: '{0}': user-defined conversions to or from an interface are not allowed
diagnostics.Add(ErrorCode.ERR_ConversionWithInterface, this.GetFirstLocation(), this);
return;
}
// SPEC: Either S0 or T0 is the class or struct type in which the operator
// SPEC: declaration takes place.
if (!MatchesContainingType(source0) &&
!MatchesContainingType(target0) &&
// allow conversion between T and Nullable<T> in declaration of Nullable<T>
!MatchesContainingType(source) &&
!MatchesContainingType(target))
{
// CS0556: User-defined conversion must convert to or from the enclosing type
diagnostics.Add(IsAbstract || IsVirtual ? ErrorCode.ERR_AbstractConversionNotInvolvingContainedType : ErrorCode.ERR_ConversionNotInvolvingContainedType, this.GetFirstLocation());
return;
}
// SPEC: * S0 and T0 are different types:
if ((ContainingType.SpecialType == SpecialType.System_Nullable_T)
? source.Equals(target, ComparisonForUserDefinedOperators)
: source0.Equals(target0, ComparisonForUserDefinedOperators))
{
// CS0555: User-defined operator cannot convert a type to itself
diagnostics.Add(ErrorCode.ERR_IdentityConversion, this.GetFirstLocation());
return;
}
// Those are the easy ones. Now we come to:
// SPEC:
// Excluding user-defined conversions, a conversion does not exist from
// S to T or T to S. For the purposes of these rules, any type parameters
// associated with S or T are considered to be unique types that have
// no inheritance relationship with other types, and any constraints on
// those type parameters are ignored.
// A counter-intuitive consequence of this rule is that:
//
// class X<U> where U : X<U>
// {
// public implicit operator X<U>(U u) { return u; }
// }
//
// is *legal*, even though there is *already* an implicit conversion
// from U to X<U> because U is constrained to have such a conversion.
//
// In discussing the implications of this rule, let's call the
// containing type (which may be a class or struct) "C". S and T
// are the source and target types.
//
// If we have made it this far in the error analysis we already know that
// exactly one of S and T is C or C? -- if two or zero were, then we'd
// have already reported ERR_ConversionNotInvolvingContainedType or
// ERR_IdentityConversion and returned.
//
// WOLOG for the purposes of this discussion let's assume that S is
// the one that is C or C?, and that T is the one that is neither C nor C?.
//
// So the question is: under what circumstances could T-to-S or S-to-T,
// be a valid conversion, by the definition of valid above?
//
// Let's consider what kinds of types T could be. T cannot be an interface
// because we've already reported an error and returned if it is. If T is
// a delegate, array, enum, pointer, struct or nullable type then there
// is no built-in conversion from T to the user-declared class/struct
// C, or to C?. If T is a type parameter, then by assumption the type
// parameter has no constraints, and therefore is not convertible to
// C or C?.
//
// That leaves T to be a class. We already know that T is not C, (or C?,
// since T is a class) and therefore there is no identity conversion from T to S.
//
// Suppose S is C and C is a class. Then the only way that there can be a
// conversion between T and S is if T is a base class of S or S is a base class of T.
//
// Suppose S is C and C is a struct. Then the only way that there can be a
// conversion between T and S is if T is a base class of S. (And T would
// have to be System.Object or System.ValueType.)
//
// Suppose S is C? and C is a struct. Then the only way that there can be a
// conversion between T and S is again, if T is a base class of S.
//
// Summing up:
//
// WOLOG, we assume that T is not C or C?, and S is C or C?. The conversion is
// illegal only if T is a class, and either T is a base class of S, or S is a
// base class of T.
if (source.IsDynamic() || target.IsDynamic())
{
// '{0}': user-defined conversions to or from the dynamic type are not allowed
diagnostics.Add(ErrorCode.ERR_BadDynamicConversion, this.GetFirstLocation(), this);
return;
}
TypeSymbol same;
TypeSymbol different;
if (MatchesContainingType(source0))
{
same = source;
different = target;
}
else
{
same = target;
different = source;
}
if (different.IsClassType() && !same.IsTypeParameter())
{
// different is a class type:
Debug.Assert(!different.IsTypeParameter());
var useSiteInfo = new CompoundUseSiteInfo<AssemblySymbol>(diagnostics, ContainingAssembly);
if (same.IsDerivedFrom(different, ComparisonForUserDefinedOperators, useSiteInfo: ref useSiteInfo))
{
// '{0}': user-defined conversions to or from a base type are not allowed
diagnostics.Add(ErrorCode.ERR_ConversionWithBase, this.GetFirstLocation(), this);
}
else if (different.IsDerivedFrom(same, ComparisonForUserDefinedOperators, useSiteInfo: ref useSiteInfo))
{
// '{0}': user-defined conversions to or from a derived type are not allowed
diagnostics.Add(ErrorCode.ERR_ConversionWithDerived, this.GetFirstLocation(), this);
}
diagnostics.Add(this.GetFirstLocation(), useSiteInfo);
}
}
private void CheckReturnIsNotVoid(BindingDiagnosticBag diagnostics)
{
if (this.ReturnsVoid)
{
// CS0590: User-defined operators cannot return void
diagnostics.Add(ErrorCode.ERR_OperatorCantReturnVoid, this.GetFirstLocation());
}
}
private void CheckUnarySignature(BindingDiagnosticBag diagnostics)
{
// SPEC: A unary + - ! ~ operator must take a single parameter of type
// SPEC: T or T? and can return any type.
if (!MatchesContainingType(this.GetParameterType(0).StrippedType()))
{
// The parameter of a unary operator must be the containing type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractUnaryOperatorSignature : ErrorCode.ERR_BadUnaryOperatorSignature, this.GetFirstLocation());
}
CheckReturnIsNotVoid(diagnostics);
}
private void CheckTrueFalseSignature(BindingDiagnosticBag diagnostics)
{
// SPEC: A unary true or false operator must take a single parameter of type
// SPEC: T or T? and must return type bool.
if (this.ReturnType.SpecialType != SpecialType.System_Boolean)
{
// The return type of operator True or False must be bool
diagnostics.Add(ErrorCode.ERR_OpTFRetType, this.GetFirstLocation());
}
if (!MatchesContainingType(this.GetParameterType(0).StrippedType()))
{
// The parameter of a unary operator must be the containing type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractUnaryOperatorSignature : ErrorCode.ERR_BadUnaryOperatorSignature, this.GetFirstLocation());
}
}
private void CheckIncrementDecrementSignature(BindingDiagnosticBag diagnostics)
{
if (!IsStatic)
{
if (!this.ReturnsVoid)
{
diagnostics.Add(ErrorCode.ERR_OperatorMustReturnVoid, this.GetFirstLocation());
}
return;
}
// SPEC: A unary ++ or -- operator must take a single parameter of type T or T?
// SPEC: and it must return that same type or a type derived from it.
// The native compiler error reporting behavior is not very good in some cases
// here, both because it reports the wrong errors, and because the wording
// of the error messages is misleading. The native compiler reports two errors:
// CS0448: The return type for ++ or -- operator must be the
// containing type or derived from the containing type
//
// CS0559: The parameter type for ++ or -- operator must be the containing type
//
// Neither error message mentions nullable types. But worse, there is a
// situation in which the native compiler reports a misleading error:
//
// struct S { public static S operator ++(S? s) { ... } }
//
// This reports CS0559, but that is not the error; the *parameter* is perfectly
// legal. The error is that the return type does not match the parameter type.
//
// I have changed the error message to reflect the true error, and we now
// report 0448, not 0559, in the given scenario. The error is now:
//
// CS0448: The return type for ++ or -- operator must match the parameter type
// or be derived from the parameter type
//
// However, this now means that we must make *another* change from native compiler
// behavior. The native compiler would report both 0448 and 0559 when given:
//
// struct S { public static int operator ++(int s) { ... } }
//
// The previous wording of error 0448 was *correct* in this scenario, but not
// it is wrong because it *does* match the formal parameter type.
//
// The solution is: First see if 0559 must be reported. Only if the formal
// parameter type is *good* do we then go on to try to report an error against
// the return type.
var parameterType = this.GetParameterType(0);
var useSiteInfo = new CompoundUseSiteInfo<AssemblySymbol>(diagnostics, ContainingAssembly);
if (!MatchesContainingType(parameterType.StrippedType()))
{
// CS0559: The parameter type for ++ or -- operator must be the containing type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractIncDecSignature : ErrorCode.ERR_BadIncDecSignature, this.GetFirstLocation());
}
else if (!(parameterType.IsTypeParameter() ?
this.ReturnType.Equals(parameterType, ComparisonForUserDefinedOperators) :
(((IsAbstract || IsVirtual) && IsContainingType(parameterType) && IsSelfConstrainedTypeParameter(this.ReturnType)) ||
this.ReturnType.EffectiveTypeNoUseSiteDiagnostics.IsEqualToOrDerivedFrom(parameterType, ComparisonForUserDefinedOperators, useSiteInfo: ref useSiteInfo))))
{
// CS0448: The return type for ++ or -- operator must match the parameter type
// or be derived from the parameter type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractIncDecRetType : ErrorCode.ERR_BadIncDecRetType, this.GetFirstLocation());
}
diagnostics.Add(this.GetFirstLocation(), useSiteInfo);
}
private bool MatchesContainingType(TypeSymbol type)
{
return IsContainingType(type) || ((IsAbstract || IsVirtual) && IsSelfConstrainedTypeParameter(type));
}
private bool IsContainingType(TypeSymbol type)
{
return type.Equals(this.ContainingType, ComparisonForUserDefinedOperators);
}
public static bool IsSelfConstrainedTypeParameter(TypeSymbol type, NamedTypeSymbol containingType)
{
Debug.Assert(containingType.IsDefinition);
return type is TypeParameterSymbol p &&
(object)p.ContainingSymbol == containingType &&
p.ConstraintTypesNoUseSiteDiagnostics.Any((typeArgument, containingType) => typeArgument.Type.Equals(containingType, ComparisonForUserDefinedOperators),
containingType);
}
private bool IsSelfConstrainedTypeParameter(TypeSymbol type)
{
return IsSelfConstrainedTypeParameter(type, this.ContainingType);
}
private void CheckShiftSignature(BindingDiagnosticBag diagnostics)
{
// SPEC: A binary <<, >> or >>> operator must take two parameters, the first
// SPEC: of which must have type T or T?, the second of which can
// SPEC: have any type. The operator can return any type.
if (!MatchesContainingType(this.GetParameterType(0).StrippedType()))
{
// CS0546: The first operand of an overloaded shift operator must have the
// same type as the containing type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractShiftOperatorSignature : ErrorCode.ERR_BadShiftOperatorSignature, this.GetFirstLocation());
}
else if (this.GetParameterType(1).StrippedType().SpecialType != SpecialType.System_Int32)
{
var location = this.GetFirstLocation();
Binder.CheckFeatureAvailability(location.SourceTree, MessageID.IDS_FeatureRelaxedShiftOperator, diagnostics, location);
}
CheckReturnIsNotVoid(diagnostics);
}
private void CheckBinarySignature(BindingDiagnosticBag diagnostics)
{
// SPEC: A binary nonshift operator must take two parameters, at least
// SPEC: one of which must have the type T or T?, and can return any type.
if (!MatchesContainingType(this.GetParameterType(0).StrippedType()) &&
!MatchesContainingType(this.GetParameterType(1).StrippedType()))
{
// CS0563: One of the parameters of a binary operator must be the containing type
diagnostics.Add((IsAbstract || IsVirtual) ? ErrorCode.ERR_BadAbstractBinaryOperatorSignature : ErrorCode.ERR_BadBinaryOperatorSignature, this.GetFirstLocation());
}
CheckReturnIsNotVoid(diagnostics);
}
private void CheckAbstractEqualitySignature(BindingDiagnosticBag diagnostics)
{
if (!IsSelfConstrainedTypeParameter(this.GetParameterType(0).StrippedType()) &&
!IsSelfConstrainedTypeParameter(this.GetParameterType(1).StrippedType()))
{
diagnostics.Add(ErrorCode.ERR_BadAbstractEqualityOperatorSignature, this.GetFirstLocation(), this.ContainingType);
}
CheckReturnIsNotVoid(diagnostics);
}
public sealed override string Name
{
get
{
return _name;
}
}
public sealed override ImmutableArray<TypeParameterSymbol> TypeParameters
{
get { return ImmutableArray<TypeParameterSymbol>.Empty; }
}
public sealed override ImmutableArray<ImmutableArray<TypeWithAnnotations>> GetTypeParameterConstraintTypes()
=> ImmutableArray<ImmutableArray<TypeWithAnnotations>>.Empty;
public sealed override ImmutableArray<TypeParameterConstraintKind> GetTypeParameterConstraintKinds()
=> ImmutableArray<TypeParameterConstraintKind>.Empty;
protected sealed override void CheckConstraintsForExplicitInterfaceType(ConversionsBase conversions, BindingDiagnosticBag diagnostics)
{
if ((object)_explicitInterfaceType != null)
{
NameSyntax name;
switch (syntaxReferenceOpt.GetSyntax())
{
case OperatorDeclarationSyntax operatorDeclaration:
Debug.Assert(operatorDeclaration.ExplicitInterfaceSpecifier != null);
name = operatorDeclaration.ExplicitInterfaceSpecifier.Name;
break;
case ConversionOperatorDeclarationSyntax conversionDeclaration:
Debug.Assert(conversionDeclaration.ExplicitInterfaceSpecifier != null);
name = conversionDeclaration.ExplicitInterfaceSpecifier.Name;
break;
default:
throw ExceptionUtilities.Unreachable();
}
_explicitInterfaceType.CheckAllConstraints(DeclaringCompilation, conversions, new SourceLocation(name), diagnostics);
}
}
protected sealed override void PartialMethodChecks(BindingDiagnosticBag diagnostics)