-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SourceTypeParameterSymbol.cs
985 lines (847 loc) · 35.6 KB
/
SourceTypeParameterSymbol.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
// 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 System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
/// <summary>
/// Base class for type and method type parameters.
/// </summary>
internal abstract class SourceTypeParameterSymbolBase : TypeParameterSymbol, IAttributeTargetSymbol
{
private readonly ImmutableArray<SyntaxReference> _syntaxRefs;
private readonly ImmutableArray<Location> _locations;
private readonly string _name;
private readonly short _ordinal;
private SymbolCompletionState _state;
private CustomAttributesBag<CSharpAttributeData> _lazyCustomAttributesBag;
private TypeParameterBounds _lazyBounds = TypeParameterBounds.Unset;
protected SourceTypeParameterSymbolBase(string name, int ordinal, ImmutableArray<Location> locations, ImmutableArray<SyntaxReference> syntaxRefs)
{
Debug.Assert(!syntaxRefs.IsEmpty);
_name = name;
_ordinal = (short)ordinal;
_locations = locations;
_syntaxRefs = syntaxRefs;
}
public override ImmutableArray<Location> Locations
{
get
{
return _locations;
}
}
public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
{
get
{
return _syntaxRefs;
}
}
internal ImmutableArray<SyntaxReference> SyntaxReferences
{
get
{
return _syntaxRefs;
}
}
public override int Ordinal
{
get
{
return _ordinal;
}
}
public override VarianceKind Variance
{
get
{
return VarianceKind.None;
}
}
public override string Name
{
get
{
return _name;
}
}
internal override ImmutableArray<TypeWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
{
var bounds = this.GetBounds(inProgress);
return (bounds != null) ? bounds.ConstraintTypes : ImmutableArray<TypeWithAnnotations>.Empty;
}
internal override ImmutableArray<NamedTypeSymbol> GetInterfaces(ConsList<TypeParameterSymbol> inProgress)
{
var bounds = this.GetBounds(inProgress);
return (bounds != null) ? bounds.Interfaces : ImmutableArray<NamedTypeSymbol>.Empty;
}
internal override NamedTypeSymbol GetEffectiveBaseClass(ConsList<TypeParameterSymbol> inProgress)
{
var bounds = this.GetBounds(inProgress);
return (bounds != null) ? bounds.EffectiveBaseClass : this.GetDefaultBaseType();
}
internal override TypeSymbol GetDeducedBaseType(ConsList<TypeParameterSymbol> inProgress)
{
var bounds = this.GetBounds(inProgress);
return (bounds != null) ? bounds.DeducedBaseType : this.GetDefaultBaseType();
}
internal ImmutableArray<SyntaxList<AttributeListSyntax>> MergedAttributeDeclarationSyntaxLists
{
get
{
var mergedAttributesBuilder = ArrayBuilder<SyntaxList<AttributeListSyntax>>.GetInstance();
foreach (var syntaxRef in _syntaxRefs)
{
var syntax = (TypeParameterSyntax)syntaxRef.GetSyntax();
mergedAttributesBuilder.Add(syntax.AttributeLists);
}
var sourceMethod = this.ContainingSymbol as SourceOrdinaryMethodSymbol;
if ((object)sourceMethod != null && sourceMethod.IsPartial)
{
var implementingPart = sourceMethod.SourcePartialImplementation;
if ((object)implementingPart != null)
{
var typeParameter = (SourceTypeParameterSymbolBase)implementingPart.TypeParameters[_ordinal];
mergedAttributesBuilder.AddRange(typeParameter.MergedAttributeDeclarationSyntaxLists);
}
}
return mergedAttributesBuilder.ToImmutableAndFree();
}
}
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner
{
get { return this; }
}
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation
{
get { return AttributeLocation.TypeParameter; }
}
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations
{
get { return AttributeLocation.TypeParameter; }
}
/// <summary>
/// Gets the attributes applied on this symbol.
/// Returns an empty array if there are no attributes.
/// </summary>
/// <remarks>
/// NOTE: This method should always be kept as a sealed override.
/// If you want to override attribute binding logic for a sub-class, then override <see cref="GetAttributesBag"/> method.
/// </remarks>
public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
{
return this.GetAttributesBag().Attributes;
}
/// <summary>
/// Returns a bag of applied custom attributes and data decoded from well-known attributes. Returns null if there are no attributes applied on the symbol.
/// </summary>
/// <remarks>
/// Forces binding and decoding of attributes.
/// </remarks>
internal virtual CustomAttributesBag<CSharpAttributeData> GetAttributesBag()
{
if (_lazyCustomAttributesBag == null || !_lazyCustomAttributesBag.IsSealed)
{
bool lazyAttributesStored = false;
var sourceMethod = this.ContainingSymbol as SourceOrdinaryMethodSymbol;
if ((object)sourceMethod == null || (object)sourceMethod.SourcePartialDefinition == null)
{
lazyAttributesStored = LoadAndValidateAttributes(
OneOrMany.Create(this.MergedAttributeDeclarationSyntaxLists),
ref _lazyCustomAttributesBag,
binderOpt: (ContainingSymbol as LocalFunctionSymbol)?.SignatureBinder);
}
else
{
var typeParameter = (SourceTypeParameterSymbolBase)sourceMethod.SourcePartialDefinition.TypeParameters[_ordinal];
CustomAttributesBag<CSharpAttributeData> attributesBag = typeParameter.GetAttributesBag();
lazyAttributesStored = Interlocked.CompareExchange(ref _lazyCustomAttributesBag, attributesBag, null) == null;
}
if (lazyAttributesStored)
{
_state.NotePartComplete(CompletionPart.Attributes);
}
}
return _lazyCustomAttributesBag;
}
internal override void EnsureAllConstraintsAreResolved()
{
if (!_lazyBounds.IsSet())
{
EnsureAllConstraintsAreResolved(this.ContainerTypeParameters);
}
}
protected abstract ImmutableArray<TypeParameterSymbol> ContainerTypeParameters
{
get;
}
private TypeParameterBounds GetBounds(ConsList<TypeParameterSymbol> inProgress)
{
Debug.Assert(!inProgress.ContainsReference(this));
Debug.Assert(!inProgress.Any() || ReferenceEquals(inProgress.Head.ContainingSymbol, this.ContainingSymbol));
if (!_lazyBounds.IsSet())
{
var diagnostics = DiagnosticBag.GetInstance();
var bounds = this.ResolveBounds(inProgress, diagnostics);
if (ReferenceEquals(Interlocked.CompareExchange(ref _lazyBounds, bounds, TypeParameterBounds.Unset), TypeParameterBounds.Unset))
{
this.CheckConstraintTypeConstraints(diagnostics);
this.CheckUnmanagedConstraint(diagnostics);
this.EnsureAttributesFromConstraints(diagnostics);
this.AddDeclarationDiagnostics(diagnostics);
_state.NotePartComplete(CompletionPart.TypeParameterConstraints);
}
diagnostics.Free();
}
return _lazyBounds;
}
protected abstract TypeParameterBounds ResolveBounds(ConsList<TypeParameterSymbol> inProgress, DiagnosticBag diagnostics);
/// <summary>
/// Check constraints of generic types referenced in constraint types. For instance,
/// with "interface I<T> where T : I<T> {}", check T satisfies constraints
/// on I<T>. Those constraints are not checked when binding ConstraintTypes
/// since ConstraintTypes has not been set on I<T> at that point.
/// </summary>
private void CheckConstraintTypeConstraints(DiagnosticBag diagnostics)
{
var constraintTypes = this.ConstraintTypesNoUseSiteDiagnostics;
if (constraintTypes.Length == 0)
{
return;
}
var args = new ConstraintsHelper.CheckConstraintsArgs(DeclaringCompilation, new TypeConversions(ContainingAssembly.CorLibrary), _locations[0], diagnostics);
foreach (var constraintType in constraintTypes)
{
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
constraintType.Type.AddUseSiteDiagnostics(ref useSiteDiagnostics);
if (!diagnostics.Add(args.Location, useSiteDiagnostics))
{
constraintType.Type.CheckAllConstraints(args);
}
}
}
private void CheckUnmanagedConstraint(DiagnosticBag diagnostics)
{
if (this.HasUnmanagedTypeConstraint)
{
DeclaringCompilation.EnsureIsUnmanagedAttributeExists(diagnostics, this.GetNonNullSyntaxNode().Location, ModifyCompilationForAttributeEmbedding());
}
}
private bool ModifyCompilationForAttributeEmbedding()
{
bool modifyCompilation;
switch (this.ContainingSymbol)
{
case SourceOrdinaryMethodSymbol _:
case SourceMemberContainerTypeSymbol _:
modifyCompilation = true;
break;
case LocalFunctionSymbol _:
modifyCompilation = false;
break;
default:
throw ExceptionUtilities.UnexpectedValue(this.ContainingSymbol);
}
return modifyCompilation;
}
private void EnsureAttributesFromConstraints(DiagnosticBag diagnostics)
{
if (ConstraintTypesNoUseSiteDiagnostics.Any(t => t.ContainsNativeInteger()))
{
DeclaringCompilation.EnsureNativeIntegerAttributeExists(diagnostics, getLocation(), ModifyCompilationForAttributeEmbedding());
}
if (ConstraintsNeedNullableAttribute())
{
DeclaringCompilation.EnsureNullableAttributeExists(diagnostics, getLocation(), ModifyCompilationForAttributeEmbedding());
}
Location getLocation() => this.GetNonNullSyntaxNode().Location;
}
// See https://github.com/dotnet/roslyn/blob/master/docs/features/nullable-metadata.md
internal bool ConstraintsNeedNullableAttribute()
{
if (!DeclaringCompilation.ShouldEmitNullableAttributes(this))
{
return false;
}
if (this.HasReferenceTypeConstraint && this.ReferenceTypeConstraintIsNullable != null)
{
return true;
}
if (this.ConstraintTypesNoUseSiteDiagnostics.Any(c => c.NeedsNullableAttribute()))
{
return true;
}
if (this.HasNotNullConstraint)
{
return true;
}
return !this.HasReferenceTypeConstraint &&
!this.HasValueTypeConstraint &&
this.ConstraintTypesNoUseSiteDiagnostics.IsEmpty &&
this.IsNotNullable == false;
}
private NamedTypeSymbol GetDefaultBaseType()
{
return this.ContainingAssembly.GetSpecialType(SpecialType.System_Object);
}
internal override void ForceComplete(SourceLocation locationOpt, CancellationToken cancellationToken)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var incompletePart = _state.NextIncompletePart;
switch (incompletePart)
{
case CompletionPart.Attributes:
GetAttributes();
break;
case CompletionPart.TypeParameterConstraints:
var constraintTypes = this.ConstraintTypesNoUseSiteDiagnostics;
// Nested type parameter references might not be valid in error scenarios.
//Debug.Assert(this.ContainingSymbol.IsContainingSymbolOfAllTypeParameters(this.ConstraintTypes));
//Debug.Assert(this.ContainingSymbol.IsContainingSymbolOfAllTypeParameters(ImmutableArray<TypeSymbol>.CreateFrom(this.Interfaces)));
Debug.Assert(this.ContainingSymbol.IsContainingSymbolOfAllTypeParameters(this.EffectiveBaseClassNoUseSiteDiagnostics));
Debug.Assert(this.ContainingSymbol.IsContainingSymbolOfAllTypeParameters(this.DeducedBaseTypeNoUseSiteDiagnostics));
break;
case CompletionPart.None:
return;
default:
// any other values are completion parts intended for other kinds of symbols
_state.NotePartComplete(CompletionPart.All & ~CompletionPart.TypeParameterSymbolAll);
break;
}
_state.SpinWaitComplete(incompletePart, cancellationToken);
}
}
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
{
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);
if (this.HasUnmanagedTypeConstraint)
{
AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeIsUnmanagedAttribute(this));
}
var compilation = DeclaringCompilation;
if (compilation.ShouldEmitNullableAttributes(this))
{
AddSynthesizedAttribute(
ref attributes,
moduleBuilder.SynthesizeNullableAttributeIfNecessary(GetNullableContextValue(), GetSynthesizedNullableAttributeValue()));
}
}
internal byte GetSynthesizedNullableAttributeValue()
{
if (this.HasReferenceTypeConstraint)
{
switch (this.ReferenceTypeConstraintIsNullable)
{
case true:
return NullableAnnotationExtensions.AnnotatedAttributeValue;
case false:
return NullableAnnotationExtensions.NotAnnotatedAttributeValue;
}
}
else if (this.HasNotNullConstraint)
{
return NullableAnnotationExtensions.NotAnnotatedAttributeValue;
}
else if (!this.HasValueTypeConstraint && this.ConstraintTypesNoUseSiteDiagnostics.IsEmpty && this.IsNotNullable == false)
{
return NullableAnnotationExtensions.AnnotatedAttributeValue;
}
return NullableAnnotationExtensions.ObliviousAttributeValue;
}
internal sealed override void DecodeWellKnownAttribute(ref DecodeWellKnownAttributeArguments<AttributeSyntax, CSharpAttributeData, AttributeLocation> arguments)
{
Debug.Assert((object)arguments.AttributeSyntaxOpt != null);
var attribute = arguments.Attribute;
Debug.Assert(!attribute.HasErrors);
Debug.Assert(arguments.SymbolPart == AttributeLocation.None);
if (attribute.IsTargetAttribute(this, AttributeDescription.NullableAttribute))
{
// NullableAttribute should not be set explicitly.
arguments.Diagnostics.Add(ErrorCode.ERR_ExplicitNullableAttribute, arguments.AttributeSyntaxOpt.Location);
}
base.DecodeWellKnownAttribute(ref arguments);
}
protected bool? CalculateReferenceTypeConstraintIsNullable(TypeParameterConstraintKind constraints)
{
if ((constraints & TypeParameterConstraintKind.ReferenceType) == 0)
{
return false;
}
switch (constraints & TypeParameterConstraintKind.AllReferenceTypeKinds)
{
case TypeParameterConstraintKind.NullableReferenceType:
return true;
case TypeParameterConstraintKind.NotNullableReferenceType:
return false;
}
return null;
}
}
internal sealed class SourceTypeParameterSymbol : SourceTypeParameterSymbolBase
{
private readonly SourceNamedTypeSymbol _owner;
private readonly VarianceKind _varianceKind;
public SourceTypeParameterSymbol(SourceNamedTypeSymbol owner, string name, int ordinal, VarianceKind varianceKind, ImmutableArray<Location> locations, ImmutableArray<SyntaxReference> syntaxRefs)
: base(name, ordinal, locations, syntaxRefs)
{
_owner = owner;
_varianceKind = varianceKind;
}
public override TypeParameterKind TypeParameterKind
{
get
{
return TypeParameterKind.Type;
}
}
public override Symbol ContainingSymbol
{
get { return _owner; }
}
public override VarianceKind Variance
{
get { return _varianceKind; }
}
public override bool HasConstructorConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.Constructor) != 0;
}
}
public override bool HasValueTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.AllValueTypeKinds) != 0;
}
}
public override bool IsValueTypeFromConstraintTypes
{
get
{
Debug.Assert(!HasValueTypeConstraint);
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ValueTypeFromConstraintTypes) != 0;
}
}
public override bool HasReferenceTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ReferenceType) != 0;
}
}
public override bool IsReferenceTypeFromConstraintTypes
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ReferenceTypeFromConstraintTypes) != 0;
}
}
internal override bool? ReferenceTypeConstraintIsNullable
{
get
{
return CalculateReferenceTypeConstraintIsNullable(this.GetConstraintKinds());
}
}
public override bool HasNotNullConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.NotNull) != 0;
}
}
internal override bool? IsNotNullable
{
get
{
if ((this.GetConstraintKinds() & TypeParameterConstraintKind.ObliviousNullabilityIfReferenceType) != 0)
{
return null;
}
return CalculateIsNotNullable();
}
}
public override bool HasUnmanagedTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.Unmanaged) != 0;
}
}
protected override ImmutableArray<TypeParameterSymbol> ContainerTypeParameters
{
get { return _owner.TypeParameters; }
}
protected override TypeParameterBounds ResolveBounds(ConsList<TypeParameterSymbol> inProgress, DiagnosticBag diagnostics)
{
var constraintTypes = _owner.GetTypeParameterConstraintTypes(this.Ordinal);
if (constraintTypes.IsEmpty && GetConstraintKinds() == TypeParameterConstraintKind.None)
{
return null;
}
return this.ResolveBounds(this.ContainingAssembly.CorLibrary, inProgress.Prepend(this), constraintTypes, inherited: false, this.DeclaringCompilation, diagnostics);
}
private TypeParameterConstraintKind GetConstraintKinds()
{
return _owner.GetTypeParameterConstraintKind(this.Ordinal);
}
}
internal sealed class SourceMethodTypeParameterSymbol : SourceTypeParameterSymbolBase
{
private readonly SourceMethodSymbol _owner;
public SourceMethodTypeParameterSymbol(SourceMethodSymbol owner, string name, int ordinal, ImmutableArray<Location> locations, ImmutableArray<SyntaxReference> syntaxRefs)
: base(name, ordinal, locations, syntaxRefs)
{
_owner = owner;
}
internal override void AddDeclarationDiagnostics(DiagnosticBag diagnostics)
=> _owner.AddDeclarationDiagnostics(diagnostics);
public override TypeParameterKind TypeParameterKind
{
get
{
return TypeParameterKind.Method;
}
}
public override Symbol ContainingSymbol
{
get { return _owner; }
}
public override bool HasConstructorConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.Constructor) != 0;
}
}
public override bool HasValueTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.AllValueTypeKinds) != 0;
}
}
public override bool IsValueTypeFromConstraintTypes
{
get
{
Debug.Assert(!HasValueTypeConstraint);
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ValueTypeFromConstraintTypes) != 0;
}
}
public override bool HasReferenceTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ReferenceType) != 0;
}
}
public override bool IsReferenceTypeFromConstraintTypes
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.ReferenceTypeFromConstraintTypes) != 0;
}
}
public override bool HasNotNullConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.NotNull) != 0;
}
}
internal override bool? ReferenceTypeConstraintIsNullable
{
get
{
return CalculateReferenceTypeConstraintIsNullable(this.GetConstraintKinds());
}
}
internal override bool? IsNotNullable
{
get
{
if ((this.GetConstraintKinds() & TypeParameterConstraintKind.ObliviousNullabilityIfReferenceType) != 0)
{
return null;
}
return CalculateIsNotNullable();
}
}
public override bool HasUnmanagedTypeConstraint
{
get
{
var constraints = this.GetConstraintKinds();
return (constraints & TypeParameterConstraintKind.Unmanaged) != 0;
}
}
protected override ImmutableArray<TypeParameterSymbol> ContainerTypeParameters
{
get { return _owner.TypeParameters; }
}
protected override TypeParameterBounds ResolveBounds(ConsList<TypeParameterSymbol> inProgress, DiagnosticBag diagnostics)
{
var constraints = _owner.GetTypeParameterConstraintTypes();
var constraintTypes = constraints.IsEmpty ? ImmutableArray<TypeWithAnnotations>.Empty : constraints[Ordinal];
if (constraintTypes.IsEmpty && GetConstraintKinds() == TypeParameterConstraintKind.None)
{
return null;
}
return this.ResolveBounds(this.ContainingAssembly.CorLibrary, inProgress.Prepend(this), constraintTypes, inherited: false, this.DeclaringCompilation, diagnostics);
}
private TypeParameterConstraintKind GetConstraintKinds()
{
var constraintKinds = _owner.GetTypeParameterConstraintKinds();
return constraintKinds.IsEmpty ? TypeParameterConstraintKind.None : constraintKinds[Ordinal];
}
}
/// <summary>
/// A map shared by all type parameters for an overriding method or a method
/// that explicitly implements an interface. The map caches the overridden method
/// and a type map from overridden type parameters to overriding type parameters.
/// </summary>
internal abstract class OverriddenMethodTypeParameterMapBase
{
// Method representing overriding or explicit implementation.
private readonly SourceOrdinaryMethodSymbol _overridingMethod;
// Type map shared by all type parameters for this explicit implementation.
private TypeMap _lazyTypeMap;
// Overridden or explicitly implemented method. May be null in error cases.
private MethodSymbol _lazyOverriddenMethod = ErrorMethodSymbol.UnknownMethod;
protected OverriddenMethodTypeParameterMapBase(SourceOrdinaryMethodSymbol overridingMethod)
{
_overridingMethod = overridingMethod;
}
public SourceOrdinaryMethodSymbol OverridingMethod
{
get { return _overridingMethod; }
}
public TypeParameterSymbol GetOverriddenTypeParameter(int ordinal)
{
var overriddenMethod = this.OverriddenMethod;
return ((object)overriddenMethod != null) ? overriddenMethod.TypeParameters[ordinal] : null;
}
public TypeMap TypeMap
{
get
{
if (_lazyTypeMap == null)
{
var overriddenMethod = this.OverriddenMethod;
if ((object)overriddenMethod != null)
{
var overriddenTypeParameters = overriddenMethod.TypeParameters;
var overridingTypeParameters = _overridingMethod.TypeParameters;
Debug.Assert(overriddenTypeParameters.Length == overridingTypeParameters.Length);
var typeMap = new TypeMap(overriddenTypeParameters, overridingTypeParameters, allowAlpha: true);
Interlocked.CompareExchange(ref _lazyTypeMap, typeMap, null);
}
}
return _lazyTypeMap;
}
}
private MethodSymbol OverriddenMethod
{
get
{
if (ReferenceEquals(_lazyOverriddenMethod, ErrorMethodSymbol.UnknownMethod))
{
Interlocked.CompareExchange(ref _lazyOverriddenMethod, this.GetOverriddenMethod(_overridingMethod), ErrorMethodSymbol.UnknownMethod);
}
return _lazyOverriddenMethod;
}
}
protected abstract MethodSymbol GetOverriddenMethod(SourceOrdinaryMethodSymbol overridingMethod);
}
internal sealed class OverriddenMethodTypeParameterMap : OverriddenMethodTypeParameterMapBase
{
public OverriddenMethodTypeParameterMap(SourceOrdinaryMethodSymbol overridingMethod)
: base(overridingMethod)
{
Debug.Assert(overridingMethod.IsOverride);
}
protected override MethodSymbol GetOverriddenMethod(SourceOrdinaryMethodSymbol overridingMethod)
{
MethodSymbol method = overridingMethod;
Debug.Assert(method.IsOverride);
do
{
method = method.OverriddenMethod;
} while (((object)method != null) && method.IsOverride);
// OverriddenMethod may be null in error situations.
return method;
}
}
internal sealed class ExplicitInterfaceMethodTypeParameterMap : OverriddenMethodTypeParameterMapBase
{
public ExplicitInterfaceMethodTypeParameterMap(SourceOrdinaryMethodSymbol implementationMethod)
: base(implementationMethod)
{
Debug.Assert(implementationMethod.IsExplicitInterfaceImplementation);
}
protected override MethodSymbol GetOverriddenMethod(SourceOrdinaryMethodSymbol overridingMethod)
{
var explicitImplementations = overridingMethod.ExplicitInterfaceImplementations;
Debug.Assert(explicitImplementations.Length <= 1);
// ExplicitInterfaceImplementations may be empty in error situations.
return (explicitImplementations.Length > 0) ? explicitImplementations[0] : null;
}
}
/// <summary>
/// A type parameter for a method that either overrides a base
/// type method or explicitly implements an interface method.
/// </summary>
/// <remarks>
/// Exists to copy constraints from the corresponding type parameter of an overridden method.
/// </remarks>
internal sealed class SourceOverridingMethodTypeParameterSymbol : SourceTypeParameterSymbolBase
{
private readonly OverriddenMethodTypeParameterMapBase _map;
public SourceOverridingMethodTypeParameterSymbol(OverriddenMethodTypeParameterMapBase map, string name, int ordinal, ImmutableArray<Location> locations, ImmutableArray<SyntaxReference> syntaxRefs)
: base(name, ordinal, locations, syntaxRefs)
{
_map = map;
}
public SourceOrdinaryMethodSymbol Owner
{
get { return _map.OverridingMethod; }
}
public override TypeParameterKind TypeParameterKind
{
get
{
return TypeParameterKind.Method;
}
}
public override Symbol ContainingSymbol
{
get { return this.Owner; }
}
public override bool HasConstructorConstraint
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && typeParameter.HasConstructorConstraint;
}
}
public override bool HasValueTypeConstraint
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && typeParameter.HasValueTypeConstraint;
}
}
public override bool IsValueTypeFromConstraintTypes
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && (typeParameter.IsValueTypeFromConstraintTypes || CalculateIsValueTypeFromConstraintTypes(ConstraintTypesNoUseSiteDiagnostics));
}
}
public override bool HasReferenceTypeConstraint
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && typeParameter.HasReferenceTypeConstraint;
}
}
public override bool IsReferenceTypeFromConstraintTypes
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && (typeParameter.IsReferenceTypeFromConstraintTypes || CalculateIsReferenceTypeFromConstraintTypes(ConstraintTypesNoUseSiteDiagnostics));
}
}
internal override bool? ReferenceTypeConstraintIsNullable
{
get
{
TypeParameterSymbol typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) ? typeParameter.ReferenceTypeConstraintIsNullable : false;
}
}
public override bool HasNotNullConstraint
{
get
{
return this.OverriddenTypeParameter?.HasNotNullConstraint == true;
}
}
internal override bool? IsNotNullable
{
get
{
return this.OverriddenTypeParameter?.IsNotNullable;
}
}
public override bool HasUnmanagedTypeConstraint
{
get
{
var typeParameter = this.OverriddenTypeParameter;
return ((object)typeParameter != null) && typeParameter.HasUnmanagedTypeConstraint;
}
}
protected override ImmutableArray<TypeParameterSymbol> ContainerTypeParameters
{
get { return this.Owner.TypeParameters; }
}
protected override TypeParameterBounds ResolveBounds(ConsList<TypeParameterSymbol> inProgress, DiagnosticBag diagnostics)
{
var typeParameter = this.OverriddenTypeParameter;
if ((object)typeParameter == null)
{
return null;
}
var map = _map.TypeMap;
Debug.Assert(map != null);
var constraintTypes = map.SubstituteTypes(typeParameter.ConstraintTypesNoUseSiteDiagnostics);
return this.ResolveBounds(this.ContainingAssembly.CorLibrary, inProgress.Prepend(this), constraintTypes, inherited: true, this.DeclaringCompilation, diagnostics);
}
/// <summary>
/// The type parameter to use for determining constraints. If there is a base
/// method that the owner method is overriding, the corresponding type
/// parameter on that method is used. Otherwise, the result is null.
/// </summary>
private TypeParameterSymbol OverriddenTypeParameter
{
get
{
return _map.GetOverriddenTypeParameter(this.Ordinal);
}
}
}
}