TypeParameterSymbol.cs 26.1 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<Symbol> 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 bool? IsNotNullableIfReferenceTypeFromConstraintTypes(ImmutableArray<TypeSymbolWithAnnotations> constraintTypes, ConsList<TypeParameterSymbol> inProgress)
448
        {
449 450 451 452 453 454
            Debug.Assert(!constraintTypes.IsDefaultOrEmpty);
            inProgress = inProgress.Prepend(this);

            bool? result = false;
            foreach (TypeSymbolWithAnnotations constraintType in constraintTypes)
            {
455 456
                bool? fromType = IsNotNullableIfReferenceTypeFromConstraintType(constraintType, inProgress);
                if (fromType == true)
457
                {
458
                    return true;
459
                }
460
                else if (fromType == null)
461
                {
462
                    result = null;
463
                }
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
            }

            return result;
        }

        internal static bool? IsNotNullableIfReferenceTypeFromConstraintType(TypeSymbolWithAnnotations constraintType, ConsList<TypeParameterSymbol> inProgress)
        {
            if (constraintType.IsAnnotated)
            {
                return false;
            }

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

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

490 491 492
            if (constraintType.NonNullTypesContext.NonNullTypes == true)
            {
                return true;
493 494
            }

495
            return null;
496 497
        }

C
Charles Stoner 已提交
498
        internal bool IsValueTypeFromConstraintTypes(ImmutableArray<TypeSymbolWithAnnotations> constraintTypes, ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
499
        {
C
Charles Stoner 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512
            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 已提交
513 514
            foreach (var constraintType in constraintTypes)
            {
C
Charles Stoner 已提交
515
                if (predicate(constraintType, inProgress))
P
Pilchie 已提交
516 517 518 519 520 521 522
                {
                    return true;
                }
            }
            return false;
        }

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

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

538
        internal bool? GetIsNotNullableIfReferenceType(ConsList<TypeParameterSymbol> inProgress)
539 540 541 542 543 544
        {
            if (inProgress.ContainsReference(this))
            {
                return false;
            }

545 546 547
            bool? fromReferenceTypeConstraint = false;

            if (this.HasReferenceTypeConstraint)
548
            {
549 550 551 552 553 554
                fromReferenceTypeConstraint = !this.ReferenceTypeConstraintIsNullable;

                if (fromReferenceTypeConstraint == true)
                {
                    return true;
                }
555 556
            }

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
            ImmutableArray<TypeSymbolWithAnnotations> constraintTypes = this.GetConstraintTypesNoUseSiteDiagnostics(inProgress, early: true);

            if (constraintTypes.IsEmpty)
            {
                return fromReferenceTypeConstraint;
            }

            bool? fromTypes = IsNotNullableIfReferenceTypeFromConstraintTypes(constraintTypes, inProgress);

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

            Debug.Assert(fromReferenceTypeConstraint == null);
            Debug.Assert(fromTypes != true);
            return null;
574 575 576
        }

        // PROTOTYPE(NullableReferenceTypes): Should this API be exposed through ITypeParameterSymbol?
577
        internal bool? IsNotNullableIfReferenceType => GetIsNotNullableIfReferenceType(ConsList<TypeParameterSymbol>.Empty);
578

C
Charles Stoner 已提交
579
        internal bool GetIsValueType(ConsList<TypeParameterSymbol> inProgress)
P
Pilchie 已提交
580
        {
C
Charles Stoner 已提交
581
            if (inProgress.ContainsReference(this))
P
Pilchie 已提交
582
            {
C
Charles Stoner 已提交
583
                return false;
P
Pilchie 已提交
584
            }
C
Charles Stoner 已提交
585 586 587 588 589
            if (this.HasValueTypeConstraint)
            {
                return true;
            }
            return IsValueTypeFromConstraintTypes(this.GetConstraintTypesNoUseSiteDiagnostics(inProgress, early: true), inProgress);
P
Pilchie 已提交
590 591
        }

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

P
Pilchie 已提交
594 595 596 597
        internal sealed override bool IsManagedType
        {
            get
            {
598
                return !this.HasUnmanagedTypeConstraint;
P
Pilchie 已提交
599 600 601
            }
        }

V
vsadov 已提交
602 603 604 605 606 607 608 609
        internal sealed override bool IsByRefLikeType
        {
            get
            {
                return false;
            }
        }

V
vsadov 已提交
610 611 612 613
        internal sealed override bool IsReadOnly
        {
            get
            {
614 615
                // 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 已提交
616 617 618 619
                return false;
            }
        }

P
Pilchie 已提交
620 621 622 623 624 625 626
        internal sealed override ObsoleteAttributeData ObsoleteAttributeData
        {
            get { return null; }
        }

        public abstract bool HasReferenceTypeConstraint { get; }

627
        // PROTOTYPE(NullableReferenceTypes): Should this API be exposed through ITypeParameterSymbol?
628 629 630 631 632
        /// <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>
633
        internal abstract bool? ReferenceTypeConstraintIsNullable { get; }
634

P
Pilchie 已提交
635 636
        public abstract bool HasValueTypeConstraint { get; }

637 638
        public abstract bool HasUnmanagedTypeConstraint { get; }

P
Pilchie 已提交
639 640 641 642 643 644 645
        public abstract VarianceKind Variance { get; }

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

646
        internal override bool Equals(TypeSymbol t2, TypeCompareKind comparison)
P
Pilchie 已提交
647
        {
648
            return this.Equals(t2 as TypeParameterSymbol, comparison);
P
Pilchie 已提交
649 650 651 652
        }

        internal bool Equals(TypeParameterSymbol other)
        {
653
            return Equals(other, TypeCompareKind.ConsiderEverything);
P
Pilchie 已提交
654 655
        }

656
        private bool Equals(TypeParameterSymbol other, TypeCompareKind comparison)
P
Pilchie 已提交
657 658 659 660 661 662 663 664 665 666 667 668
        {
            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.
669
            return other.ContainingSymbol.ContainingType.Equals(this.ContainingSymbol.ContainingType, comparison);
P
Pilchie 已提交
670 671 672 673 674 675 676
        }

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

677 678 679 680
        internal override void AddNullableTransforms(ArrayBuilder<bool> transforms)
        {
        }

681
        internal override bool ApplyNullableTransforms(ImmutableArray<bool> transforms, INonNullTypesContext nonNullTypesContext, ref int position, out TypeSymbol result)
682 683 684 685 686
        {
            result = this;
            return true;
        }

687
        internal override TypeSymbol SetUnknownNullabilityForReferenceTypes()
688 689 690 691
        {
            return this;
        }

692 693 694 695 696 697 698 699
        public override sealed bool? NonNullTypes
        {
            get
            {
                return ContainingSymbol.NonNullTypes;
            }
        }

P
Pilchie 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
        #region ITypeParameterTypeSymbol Members

        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
            {
724
                return this.ConstraintTypesNoUseSiteDiagnostics.SelectAsArray(c => (ITypeSymbol)c.TypeSymbol);
P
Pilchie 已提交
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
            }
        }

        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
    }
754
}