TypeParameterSymbol.cs 26.3 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2

3
using System;
P
Pilchie 已提交
4 5
using System.Collections.Generic;
using System.Collections.Immutable;
6
using System.Diagnostics;
7
using Microsoft.CodeAnalysis.PooledObjects;
P
Pilchie 已提交
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
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
    /// <summary>
    /// Represents a type parameter in a generic type or generic method.
    /// </summary>
    internal abstract partial class TypeParameterSymbol : TypeSymbol, ITypeParameterSymbol
    {
        /// <summary>
        /// The original definition of this symbol. If this symbol is constructed from another
        /// symbol by type substitution then OriginalDefinition gets the original symbol as it was defined in
        /// source or metadata.
        /// </summary>
        public new virtual TypeParameterSymbol OriginalDefinition
        {
            get
            {
                return this;
            }
        }

        protected override sealed TypeSymbol OriginalTypeSymbolDefinition
        {
            get
            {
                return this.OriginalDefinition;
            }
        }

        /// <summary>
        /// If this is a type parameter of a reduced extension method, gets the type parameter definition that
        /// this type parameter was reduced from. Otherwise, returns Nothing.
        /// </summary>
        public virtual TypeParameterSymbol ReducedFrom
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// The ordinal position of the type parameter in the parameter list which declares
        /// it. The first type parameter has ordinal zero.
        /// </summary>
        public abstract int Ordinal
        {
            // This is needed to determine hiding in C#: 
            //
            // interface IB { void M<T>(C<T> x); }
            // interface ID : IB { new void M<U>(C<U> x); }
            //
            // ID.M<U> hides IB.M<T> even though their formal parameters have different
            // types. When comparing formal parameter types for hiding purposes we must
            // compare method type parameters by ordinal, not by identity.
            get;
        }

        internal virtual DiagnosticInfo GetConstraintsUseSiteErrorInfo()
        {
            return null;
        }

        /// <summary>
        /// The types that were directly specified as constraints on the type parameter.
        /// Duplicates and cycles are removed, although the collection may include
        /// redundant constraints where one constraint is a base type of another.
        /// </summary>
C
Charles Stoner 已提交
77 78 79 80 81 82 83 84 85 86 87 88
        internal ImmutableArray<TypeSymbolWithAnnotations> GetConstraintTypesNoUseSiteDiagnostics(ConsList<TypeParameterSymbol> inProgress, bool early)
        {
            // We could call EnsureAllConstraintsAreResolved(early) directly rather than splitting
            // this into two separate calls. However, the split between early and late must be explicit
            // somewhere to ensure that the early work for all sibling type parameters is completed
            // before the late work for any sibling. It seems simpler to handle the split here (in the
            // one caller of EnsureAllConstraintsAreResolved that supports the late phase) rather than
            // in several implementations of the abstract EnsureAllConstraintsAreResolved method.
            this.EnsureAllConstraintsAreResolved(early: true);
            if (!early)
            {
                this.EnsureAllConstraintsAreResolved(early);
P
Pilchie 已提交
89
            }
C
Charles Stoner 已提交
90
            return this.GetConstraintTypes(inProgress, early);
P
Pilchie 已提交
91 92
        }

C
Charles Stoner 已提交
93 94 95
        internal ImmutableArray<TypeSymbolWithAnnotations> ConstraintTypesNoUseSiteDiagnostics =>
            GetConstraintTypesNoUseSiteDiagnostics(ConsList<TypeParameterSymbol>.Empty, early: false);

96
        internal ImmutableArray<TypeSymbolWithAnnotations> ConstraintTypesWithDefinitionUseSiteDiagnostics(ref HashSet<DiagnosticInfo> useSiteDiagnostics)
P
Pilchie 已提交
97 98 99 100 101 102 103
        {
            var result = ConstraintTypesNoUseSiteDiagnostics;

            AppendConstraintsUseSiteErrorInfo(ref useSiteDiagnostics);

            foreach (var constraint in result)
            {
104
                ((TypeSymbol)constraint.TypeSymbol.OriginalDefinition).AddUseSiteDiagnostics(ref useSiteDiagnostics);
P
Pilchie 已提交
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
            }

            return result;
        }

        private void AppendConstraintsUseSiteErrorInfo(ref HashSet<DiagnosticInfo> useSiteDiagnostics)
        {
            DiagnosticInfo errorInfo = this.GetConstraintsUseSiteErrorInfo();

            if ((object)errorInfo != null)
            {
                if (useSiteDiagnostics == null)
                {
                    useSiteDiagnostics = new HashSet<DiagnosticInfo>();
                }

                useSiteDiagnostics.Add(errorInfo);
            }
        }

        /// <summary>
        /// True if the parameterless constructor constraint was specified for the type parameter.
        /// </summary>
        public abstract bool HasConstructorConstraint { get; }

        /// <summary>
        /// The type parameter kind of this type parameter.
        /// </summary>
        public abstract TypeParameterKind TypeParameterKind { get; }

        /// <summary>
        /// The method that declared this type parameter, or null.
        /// </summary>
        public MethodSymbol DeclaringMethod
        {
            get
            {
                return this.ContainingSymbol as MethodSymbol;
            }
        }

        /// <summary>
        /// The type that declared this type parameter, or null.
        /// </summary>
        public NamedTypeSymbol DeclaringType
        {
            get
            {
                return this.ContainingSymbol as NamedTypeSymbol;
            }
        }

        // Type parameters do not have members
        public sealed override ImmutableArray<Symbol> GetMembers()
        {
            return ImmutableArray<Symbol>.Empty;
        }

        // Type parameters do not have members
        public sealed override ImmutableArray<Symbol> GetMembers(string name)
        {
            return ImmutableArray<Symbol>.Empty;
        }

        // Type parameters do not have members
        public sealed override ImmutableArray<NamedTypeSymbol> GetTypeMembers()
        {
            return ImmutableArray<NamedTypeSymbol>.Empty;
        }

        // Type parameters do not have members
        public sealed override ImmutableArray<NamedTypeSymbol> GetTypeMembers(string name)
        {
            return ImmutableArray<NamedTypeSymbol>.Empty;
        }

        // Type parameters do not have members
        public sealed override ImmutableArray<NamedTypeSymbol> GetTypeMembers(string name, int arity)
        {
            return ImmutableArray<NamedTypeSymbol>.Empty;
        }

        internal override TResult Accept<TArgument, TResult>(CSharpSymbolVisitor<TArgument, TResult> visitor, TArgument argument)
        {
            return visitor.VisitTypeParameter(this, argument);
        }

        public override void Accept(CSharpSymbolVisitor visitor)
        {
            visitor.VisitTypeParameter(this);
        }

        public override TResult Accept<TResult>(CSharpSymbolVisitor<TResult> visitor)
        {
            return visitor.VisitTypeParameter(this);
        }

        public sealed override SymbolKind Kind
        {
            get
            {
                return SymbolKind.TypeParameter;
            }
        }

        public sealed override TypeKind TypeKind
        {
            get
            {
                return TypeKind.TypeParameter;
            }
        }

        // Only the compiler can create TypeParameterSymbols.
        internal TypeParameterSymbol()
        {
        }

        public sealed override Accessibility DeclaredAccessibility
        {
            get
            {
                return Accessibility.NotApplicable;
            }
        }

        public sealed override bool IsStatic
        {
            get
            {
                return false;
            }
        }

        public sealed override bool IsAbstract
        {
            get
            {
                return false;
            }
        }

        public sealed override bool IsSealed
        {
            get
            {
                return false;
            }
        }

255
        internal sealed override NamedTypeSymbol BaseTypeNoUseSiteDiagnostics => null;
P
Pilchie 已提交
256

257
        internal sealed override ImmutableArray<NamedTypeSymbol> InterfacesNoUseSiteDiagnostics(ConsList<TypeSymbol> basesBeingResolved = null)
P
Pilchie 已提交
258
        {
259
            return ImmutableArray<NamedTypeSymbol>.Empty;
P
Pilchie 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        }

        protected override ImmutableArray<NamedTypeSymbol> GetAllInterfaces()
        {
            return ImmutableArray<NamedTypeSymbol>.Empty;
        }

        /// <summary>
        /// The effective base class of the type parameter (spec 10.1.5). If the deduced
        /// base type is a reference type, the effective base type will be the same as
        /// the deduced base type. Otherwise if the deduced base type is a value type,
        /// the effective base type will be the most derived reference type from which
        /// deduced base type is derived.
        /// </summary>
        internal NamedTypeSymbol EffectiveBaseClassNoUseSiteDiagnostics
        {
            get
            {
C
Charles Stoner 已提交
278
                this.EnsureAllConstraintsAreResolved(early: false);
P
Pilchie 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                return this.GetEffectiveBaseClass(ConsList<TypeParameterSymbol>.Empty);
            }
        }

        internal NamedTypeSymbol EffectiveBaseClass(ref HashSet<DiagnosticInfo> useSiteDiagnostics)
        {
            AppendConstraintsUseSiteErrorInfo(ref useSiteDiagnostics);
            var result = EffectiveBaseClassNoUseSiteDiagnostics;

            if ((object)result != null)
            {
                result.OriginalDefinition.AddUseSiteDiagnostics(ref useSiteDiagnostics);
            }

            return result;
        }

        /// <summary>
        /// The effective interface set (spec 10.1.5).
        /// </summary>
        internal ImmutableArray<NamedTypeSymbol> EffectiveInterfacesNoUseSiteDiagnostics
        {
            get
            {
C
Charles Stoner 已提交
303
                this.EnsureAllConstraintsAreResolved(early: false);
P
Pilchie 已提交
304 305 306 307 308 309 310 311 312 313 314
                return this.GetInterfaces(ConsList<TypeParameterSymbol>.Empty);
            }
        }

        /// <summary>
        /// The most encompassed type (spec 6.4.2) from the constraints.
        /// </summary>
        internal TypeSymbol DeducedBaseTypeNoUseSiteDiagnostics
        {
            get
            {
C
Charles Stoner 已提交
315
                this.EnsureAllConstraintsAreResolved(early: false);
P
Pilchie 已提交
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
                return this.GetDeducedBaseType(ConsList<TypeParameterSymbol>.Empty);
            }
        }

        internal TypeSymbol DeducedBaseType(ref HashSet<DiagnosticInfo> useSiteDiagnostics)
        {
            AppendConstraintsUseSiteErrorInfo(ref useSiteDiagnostics);
            var result = DeducedBaseTypeNoUseSiteDiagnostics;

            if ((object)result != null)
            {
                ((TypeSymbol)result.OriginalDefinition).AddUseSiteDiagnostics(ref useSiteDiagnostics);
            }

            return result;
        }

        /// <summary>
        /// The effective interface set and any base interfaces of those
        /// interfaces. This is AllInterfaces excluding interfaces that are
        /// only implemented by the effective base type.
        /// </summary>
        internal ImmutableArray<NamedTypeSymbol> AllEffectiveInterfacesNoUseSiteDiagnostics
        {
            get
            {
                return base.GetAllInterfaces();
            }
        }

        internal ImmutableArray<NamedTypeSymbol> AllEffectiveInterfacesWithDefinitionUseSiteDiagnostics(ref HashSet<DiagnosticInfo> useSiteDiagnostics)
        {
            var result = AllEffectiveInterfacesNoUseSiteDiagnostics;

            // Since bases affect content of AllInterfaces set, we need to make sure they all are good.
            var current = DeducedBaseType(ref useSiteDiagnostics);

            while ((object)current != null)
            {
                current = current.BaseTypeWithDefinitionUseSiteDiagnostics(ref useSiteDiagnostics);
            }

            foreach (var iface in result)
            {
                iface.OriginalDefinition.AddUseSiteDiagnostics(ref useSiteDiagnostics);
            }

            return result;
        }

        /// <summary>
        /// Called by <see cref="ConstraintTypesNoUseSiteDiagnostics"/>, <see cref="InterfacesNoUseSiteDiagnostics"/>, <see cref="EffectiveBaseClass"/>, and <see cref="DeducedBaseType"/>.
        /// to allow derived classes to ensure constraints within the containing
        /// type or method are resolved in a consistent order, regardless of the
        /// order the callers query individual type parameters.
C
Charles Stoner 已提交
371 372 373
        /// The `early` parameter indicates whether the constraints can be from
        /// the initial phase of constraint checking where the constraint types may
        /// still contain invalid or duplicate types.
P
Pilchie 已提交
374
        /// </summary>
C
Charles Stoner 已提交
375
        internal abstract void EnsureAllConstraintsAreResolved(bool early);
P
Pilchie 已提交
376 377 378 379

        /// <summary>
        /// Helper method to force type parameter constraints to be resolved.
        /// </summary>
C
Charles Stoner 已提交
380
        protected static void EnsureAllConstraintsAreResolved(ImmutableArray<TypeParameterSymbol> typeParameters, bool early)
P
Pilchie 已提交
381 382 383 384
        {
            foreach (var typeParameter in typeParameters)
            {
                // Invoke any method that forces constraints to be resolved.
C
Charles Stoner 已提交
385
                var unused = typeParameter.GetConstraintTypes(ConsList<TypeParameterSymbol>.Empty, early);
P
Pilchie 已提交
386 387 388
            }
        }

C
Charles Stoner 已提交
389
        internal abstract ImmutableArray<TypeSymbolWithAnnotations> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress, bool early);
P
Pilchie 已提交
390 391 392 393 394 395 396

        internal abstract ImmutableArray<NamedTypeSymbol> GetInterfaces(ConsList<TypeParameterSymbol> inProgress);

        internal abstract NamedTypeSymbol GetEffectiveBaseClass(ConsList<TypeParameterSymbol> inProgress);

        internal abstract TypeSymbol GetDeducedBaseType(ConsList<TypeParameterSymbol> inProgress);

C
Charles Stoner 已提交
397
        private bool ConstraintImpliesReferenceType(TypeSymbol constraint, ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
398 399 400
        {
            if (constraint.TypeKind == TypeKind.TypeParameter)
            {
401 402 403 404 405 406 407 408
                var typeParameter = ((TypeParameterSymbol)constraint);

                if (inProgress.ContainsReference(typeParameter))
                {
                    return false;
                }

                var constraints = typeParameter.GetConstraintTypesNoUseSiteDiagnostics(inProgress, early: true);
C
Charles Stoner 已提交
409
                return IsReferenceTypeFromConstraintTypes(constraints, inProgress);
P
Pilchie 已提交
410 411 412 413 414 415 416 417 418 419
            }
            else if (!constraint.IsReferenceType)
            {
                return false;
            }
            else
            {
                switch (constraint.TypeKind)
                {
                    case TypeKind.Interface:
420
                        return false; // can be satisfied by value types
P
Pilchie 已提交
421 422 423 424 425 426 427 428 429
                    case TypeKind.Error:
                        return false;
                }

                switch (constraint.SpecialType)
                {
                    case SpecialType.System_Object:
                    case SpecialType.System_ValueType:
                    case SpecialType.System_Enum:
430
                        return false; // can be satisfied by value types
P
Pilchie 已提交
431 432 433 434 435 436 437 438 439 440 441
                }

                return true;
            }
        }

        // From typedesc.cpp :
        // > A recursive helper that helps determine whether this variable is constrained as ObjRef.
        // > Please note that we do not check the gpReferenceTypeConstraint special constraint here
        // > because this property does not propagate up the constraining hierarchy.
        // > (e.g. "class A<S, T> where S : T, where T : class" does not guarantee that S is ObjRef)
C
Charles Stoner 已提交
442
        internal bool IsReferenceTypeFromConstraintTypes(ImmutableArray<TypeSymbolWithAnnotations> constraintTypes, ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
443
        {
C
Charles Stoner 已提交
444
            return AnyConstraintTypes(constraintTypes, inProgress, (type, arg) => ConstraintImpliesReferenceType(type.TypeSymbol, arg));
P
Pilchie 已提交
445 446
        }

447
        internal static bool? IsNotNullableIfReferenceTypeFromConstraintTypes(ImmutableArray<TypeSymbolWithAnnotations> constraintTypes)
448
        {
449 450 451 452 453
            Debug.Assert(!constraintTypes.IsDefaultOrEmpty);

            bool? result = false;
            foreach (TypeSymbolWithAnnotations constraintType in constraintTypes)
            {
454
                bool? fromType = IsNotNullableIfReferenceTypeFromConstraintType(constraintType);
455
                if (fromType == true)
456
                {
457
                    return true;
458
                }
459
                else if (fromType == null)
460
                {
461
                    result = null;
462
                }
463 464 465 466 467
            }

            return result;
        }

468
        internal static bool? IsNotNullableIfReferenceTypeFromConstraintType(TypeSymbolWithAnnotations constraintType)
469
        {
470
            if (constraintType.NullableAnnotation.IsAnyNullable())
471 472 473 474 475 476
            {
                return false;
            }

            if (constraintType.TypeKind == TypeKind.TypeParameter)
            {
477
                bool? isNotNullableIfReferenceType = ((TypeParameterSymbol)constraintType.TypeSymbol).GetIsNotNullableIfReferenceType();
478

479
                if (isNotNullableIfReferenceType == false)
480
                {
481
                    return false;
482
                }
483 484 485 486 487
                else if (isNotNullableIfReferenceType == null)
                {
                    return null;
                }
            }
488

489
            if (constraintType.NullableAnnotation == NullableAnnotation.Unknown)
490
            {
491
                return null;
492 493
            }

494
            return true;
495 496
        }

C
Charles Stoner 已提交
497
        internal bool IsValueTypeFromConstraintTypes(ImmutableArray<TypeSymbolWithAnnotations> constraintTypes, ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
498
        {
C
Charles Stoner 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511
            return AnyConstraintTypes(constraintTypes, inProgress, (type, arg) => type.GetIsValueType(arg));
        }

        private bool AnyConstraintTypes(
            ImmutableArray<TypeSymbolWithAnnotations> constraintTypes,
            ConsList<TypeParameterSymbol> inProgress,
            Func<TypeSymbolWithAnnotations, ConsList<TypeParameterSymbol>, bool> predicate)
        {
            if (constraintTypes.IsEmpty)
            {
                return false;
            }
            inProgress = inProgress.Prepend(this);
P
Pilchie 已提交
512 513
            foreach (var constraintType in constraintTypes)
            {
C
Charles Stoner 已提交
514
                if (predicate(constraintType, inProgress))
P
Pilchie 已提交
515 516 517 518 519 520 521
                {
                    return true;
                }
            }
            return false;
        }

C
Charles Stoner 已提交
522
        internal bool GetIsReferenceType(ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
523
        {
C
Charles Stoner 已提交
524
            if (inProgress.ContainsReference(this))
P
Pilchie 已提交
525
            {
C
Charles Stoner 已提交
526 527 528 529 530
                return false;
            }
            if (this.HasReferenceTypeConstraint)
            {
                return true;
P
Pilchie 已提交
531
            }
C
Charles Stoner 已提交
532
            return IsReferenceTypeFromConstraintTypes(this.GetConstraintTypesNoUseSiteDiagnostics(inProgress, early: true), inProgress);
P
Pilchie 已提交
533 534
        }

C
Charles Stoner 已提交
535 536
        public sealed override bool IsReferenceType => GetIsReferenceType(ConsList<TypeParameterSymbol>.Empty);

537
        internal bool? GetIsNotNullableIfReferenceType()
538
        {
539 540 541
            bool? fromReferenceTypeConstraint = false;

            if (this.HasReferenceTypeConstraint)
542
            {
543 544 545 546 547 548
                fromReferenceTypeConstraint = !this.ReferenceTypeConstraintIsNullable;

                if (fromReferenceTypeConstraint == true)
                {
                    return true;
                }
549 550
            }

551
            ImmutableArray<TypeSymbolWithAnnotations> constraintTypes = this.ConstraintTypesNoUseSiteDiagnostics;
552 553 554 555 556 557

            if (constraintTypes.IsEmpty)
            {
                return fromReferenceTypeConstraint;
            }

558
            bool? fromTypes = IsNotNullableIfReferenceTypeFromConstraintTypes(constraintTypes);
559 560 561 562 563 564 565 566 567

            if (fromTypes == true || fromReferenceTypeConstraint == false)
            {
                return fromTypes;
            }

            Debug.Assert(fromReferenceTypeConstraint == null);
            Debug.Assert(fromTypes != true);
            return null;
568 569
        }

570
        // https://github.com/dotnet/roslyn/issues/26198 Should this API be exposed through ITypeParameterSymbol?
571
        internal bool? IsNotNullableIfReferenceType => GetIsNotNullableIfReferenceType();
572

C
Charles Stoner 已提交
573
        internal bool GetIsValueType(ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
574
        {
C
Charles Stoner 已提交
575
            if (inProgress.ContainsReference(this))
P
Pilchie 已提交
576
            {
C
Charles Stoner 已提交
577
                return false;
P
Pilchie 已提交
578
            }
C
Charles Stoner 已提交
579 580 581 582 583
            if (this.HasValueTypeConstraint)
            {
                return true;
            }
            return IsValueTypeFromConstraintTypes(this.GetConstraintTypesNoUseSiteDiagnostics(inProgress, early: true), inProgress);
P
Pilchie 已提交
584 585
        }

C
Charles Stoner 已提交
586 587
        public sealed override bool IsValueType => GetIsValueType(ConsList<TypeParameterSymbol>.Empty);

P
Pilchie 已提交
588 589 590 591
        internal sealed override bool IsManagedType
        {
            get
            {
592
                return !this.HasUnmanagedTypeConstraint;
P
Pilchie 已提交
593 594 595
            }
        }

596
        public sealed override bool IsRefLikeType
V
vsadov 已提交
597 598 599 600 601 602 603
        {
            get
            {
                return false;
            }
        }

V
vsadov 已提交
604 605 606 607
        internal sealed override bool IsReadOnly
        {
            get
            {
608 609
                // even if T is indirectly constrained to a struct, 
                // we only can use members via constrained calls, so "true" would have no effect
V
vsadov 已提交
610 611 612 613
                return false;
            }
        }

P
Pilchie 已提交
614 615 616 617 618 619 620
        internal sealed override ObsoleteAttributeData ObsoleteAttributeData
        {
            get { return null; }
        }

        public abstract bool HasReferenceTypeConstraint { get; }

621 622 623 624 625
        /// <summary>
        /// Returns whether the reference type constraint (the 'class' constraint) should also be treated as nullable ('class?') or non-nullable (class!).
        /// In some cases this aspect is unknown (null value is returned). For example, when 'class' constraint is specified in a NonNullTypes(false) context.  
        /// This API returns false when <see cref="HasReferenceTypeConstraint"/> is false.
        /// </summary>
626
        internal abstract bool? ReferenceTypeConstraintIsNullable { get; }
627

P
Pilchie 已提交
628 629
        public abstract bool HasValueTypeConstraint { get; }

630 631
        public abstract bool HasUnmanagedTypeConstraint { get; }

P
Pilchie 已提交
632 633 634 635 636 637 638
        public abstract VarianceKind Variance { get; }

        internal sealed override bool GetUnificationUseSiteDiagnosticRecursive(ref DiagnosticInfo result, Symbol owner, ref HashSet<TypeSymbol> checkedTypes)
        {
            return false;
        }

639
        internal override bool Equals(TypeSymbol t2, TypeCompareKind comparison)
P
Pilchie 已提交
640
        {
641
            return this.Equals(t2 as TypeParameterSymbol, comparison);
P
Pilchie 已提交
642 643 644 645
        }

        internal bool Equals(TypeParameterSymbol other)
        {
646
            return Equals(other, TypeCompareKind.ConsiderEverything);
P
Pilchie 已提交
647 648
        }

649
        private bool Equals(TypeParameterSymbol other, TypeCompareKind comparison)
P
Pilchie 已提交
650 651 652 653 654 655 656 657 658 659 660 661
        {
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            if ((object)other == null || !ReferenceEquals(other.OriginalDefinition, this.OriginalDefinition))
            {
                return false;
            }

            // Type parameters may be equal but not reference equal due to independent alpha renamings.
662
            return other.ContainingSymbol.ContainingType.Equals(this.ContainingSymbol.ContainingType, comparison);
P
Pilchie 已提交
663 664 665 666 667 668 669
        }

        public override int GetHashCode()
        {
            return Hash.Combine(ContainingSymbol, Ordinal);
        }

670
        internal override void AddNullableTransforms(ArrayBuilder<byte> transforms)
671 672 673
        {
        }

674
        internal override bool ApplyNullableTransforms(byte defaultTransformFlag, ImmutableArray<byte> transforms, ref int position, out TypeSymbol result)
675 676 677 678 679
        {
            result = this;
            return true;
        }

680
        internal override TypeSymbol SetNullabilityForReferenceTypes(Func<TypeSymbolWithAnnotations, TypeSymbolWithAnnotations> transform)
681 682 683 684
        {
            return this;
        }

685 686
        internal override TypeSymbol MergeNullability(TypeSymbol other, VarianceKind variance, out bool hadNullabilityMismatch)
        {
687
            Debug.Assert(this.Equals(other, TypeCompareKind.IgnoreDynamicAndTupleNames | TypeCompareKind.IgnoreNullableModifiersForReferenceTypes));
688 689 690 691
            hadNullabilityMismatch = false;
            return this;
        }

P
Pilchie 已提交
692 693
        #region ITypeParameterTypeSymbol Members

694 695 696 697 698
        Nullability ITypeParameterSymbol.ReferenceTypeConstraintNullability
        {
            get => Nullability.NotComputed;
        }

P
Pilchie 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        TypeParameterKind ITypeParameterSymbol.TypeParameterKind
        {
            get
            {
                return (TypeParameterKind)this.TypeParameterKind;
            }
        }

        IMethodSymbol ITypeParameterSymbol.DeclaringMethod
        {
            get { return this.DeclaringMethod; }
        }

        INamedTypeSymbol ITypeParameterSymbol.DeclaringType
        {
            get { return this.DeclaringType; }
        }

        ImmutableArray<ITypeSymbol> ITypeParameterSymbol.ConstraintTypes
        {
            get
            {
721
                return this.ConstraintTypesNoUseSiteDiagnostics.SelectAsArray(c => (ITypeSymbol)c.TypeSymbol);
P
Pilchie 已提交
722 723 724
            }
        }

725 726 727 728 729
        ImmutableArray<Nullability> ITypeParameterSymbol.ConstraintsNullabilities
        {
            get => this.ConstraintTypesNoUseSiteDiagnostics.SelectAsArray(c => Nullability.NotComputed);
        }

P
Pilchie 已提交
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
        ITypeParameterSymbol ITypeParameterSymbol.OriginalDefinition
        {
            get { return this.OriginalDefinition; }
        }

        ITypeParameterSymbol ITypeParameterSymbol.ReducedFrom
        {
            get { return this.ReducedFrom; }
        }

        #endregion

        #region ISymbol Members

        public override void Accept(SymbolVisitor visitor)
        {
            visitor.VisitTypeParameter(this);
        }

        public override TResult Accept<TResult>(SymbolVisitor<TResult> visitor)
        {
            return visitor.VisitTypeParameter(this);
        }

        #endregion
    }
756
}