SubstitutedTypeParameterSymbol.cs 6.0 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 4
//#define DEBUG_ALPHA // turn on DEBUG_ALPHA to help diagnose issues around type parameter alpha-renaming

P
Pilchie 已提交
5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Immutable;
using System.Globalization;
using System.Threading;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
    internal class SubstitutedTypeParameterSymbol : TypeParameterSymbol
    {
15 16 17
        private readonly Symbol _container;
        private readonly TypeMap _map;
        private readonly TypeParameterSymbol _substitutedFrom;
E
Evan Hauck 已提交
18
        private readonly int _ordinal;
P
Pilchie 已提交
19

20 21 22 23 24
#if DEBUG_ALPHA
        private static int _nextSequence = 1;
        private readonly int _mySequence;
#endif

E
Evan Hauck 已提交
25
        internal SubstitutedTypeParameterSymbol(Symbol newContainer, TypeMap map, TypeParameterSymbol substitutedFrom, int ordinal)
P
Pilchie 已提交
26
        {
27
            _container = newContainer;
P
Pilchie 已提交
28 29
            // it is important that we don't use the map here in the constructor, as the map is still being filled
            // in by TypeMap.WithAlphaRename.  Instead, we can use the map lazily when yielding the constraints.
30 31
            _map = map;
            _substitutedFrom = substitutedFrom;
E
Evan Hauck 已提交
32
            _ordinal = ordinal;
33 34 35
#if DEBUG_ALPHA
            _mySequence = _nextSequence++;
#endif
P
Pilchie 已提交
36 37 38 39 40 41
        }

        public override TypeParameterKind TypeParameterKind
        {
            get
            {
42
                return _substitutedFrom.TypeParameterKind;
P
Pilchie 已提交
43 44 45 46 47 48 49
            }
        }

        public override Symbol ContainingSymbol
        {
            get
            {
50
                return _container;
P
Pilchie 已提交
51 52 53 54 55 56 57
            }
        }

        public override ImmutableArray<Location> Locations
        {
            get
            {
58
                return _substitutedFrom.Locations;
P
Pilchie 已提交
59 60 61 62 63 64 65
            }
        }

        public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
        {
            get
            {
66
                return _substitutedFrom.DeclaringSyntaxReferences;
P
Pilchie 已提交
67 68 69 70 71 72 73 74 75 76
            }
        }

        public override TypeParameterSymbol OriginalDefinition
        {
            get
            {
                // A substituted type parameter symbol is used as a type parameter of a frame type for lambda-captured
                // variables within a generic method.  In that case the frame's own type parameter is an original.
                return
77 78
                    ContainingSymbol.OriginalDefinition != _substitutedFrom.ContainingSymbol.OriginalDefinition ? this :
                    _substitutedFrom.OriginalDefinition;
P
Pilchie 已提交
79 80 81 82 83 84 85
            }
        }

        public override TypeParameterSymbol ReducedFrom
        {
            get
            {
86
                if (_container.Kind == SymbolKind.Method)
P
Pilchie 已提交
87
                {
88
                    MethodSymbol reducedFrom = ((MethodSymbol)_container).ReducedFrom;
P
Pilchie 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

                    if ((object)reducedFrom != null)
                    {
                        return reducedFrom.TypeParameters[this.Ordinal];
                    }
                }

                return null;
            }
        }

        public override bool HasConstructorConstraint
        {
            get
            {
104
                return _substitutedFrom.HasConstructorConstraint;
P
Pilchie 已提交
105 106 107 108 109 110 111
            }
        }

        public override int Ordinal
        {
            get
            {
E
Evan Hauck 已提交
112
                return _ordinal;
P
Pilchie 已提交
113 114 115 116 117 118 119
            }
        }

        public override VarianceKind Variance
        {
            get
            {
120
                return _substitutedFrom.Variance;
P
Pilchie 已提交
121 122 123 124 125 126 127
            }
        }

        public override bool HasValueTypeConstraint
        {
            get
            {
128
                return _substitutedFrom.HasValueTypeConstraint;
P
Pilchie 已提交
129 130 131 132 133 134 135
            }
        }

        public override bool HasReferenceTypeConstraint
        {
            get
            {
136
                return _substitutedFrom.HasReferenceTypeConstraint;
P
Pilchie 已提交
137 138 139 140 141 142 143
            }
        }

        public override string Name
        {
            get
            {
144
                return _substitutedFrom.Name
145 146
#if DEBUG_ALPHA
                    + "#" + _mySequence
P
Pilchie 已提交
147 148 149 150 151 152 153 154 155
#endif
                    ;
            }
        }

        public override bool IsImplicitlyDeclared
        {
            get
            {
156
                return _substitutedFrom.IsImplicitlyDeclared;
P
Pilchie 已提交
157 158 159 160 161
            }
        }

        public override string GetDocumentationCommentXml(CultureInfo preferredCulture = null, bool expandIncludes = false, CancellationToken cancellationToken = default(CancellationToken))
        {
162
            return _substitutedFrom.GetDocumentationCommentXml(preferredCulture, expandIncludes, cancellationToken);
P
Pilchie 已提交
163 164 165 166
        }

        public override ImmutableArray<CSharpAttributeData> GetAttributes()
        {
167
            return _substitutedFrom.GetAttributes();
P
Pilchie 已提交
168 169 170 171
        }

        internal override void EnsureAllConstraintsAreResolved()
        {
172
            _substitutedFrom.EnsureAllConstraintsAreResolved();
P
Pilchie 已提交
173 174 175 176
        }

        internal override ImmutableArray<TypeSymbol> GetConstraintTypes(ConsList<TypeParameterSymbol> inProgress)
        {
177
            return _map.SubstituteTypesWithoutModifiers(_substitutedFrom.GetConstraintTypes(inProgress)).WhereAsArray(s_isNotObjectFunc).Distinct();
P
Pilchie 已提交
178 179 180 181
        }

        internal override ImmutableArray<NamedTypeSymbol> GetInterfaces(ConsList<TypeParameterSymbol> inProgress)
        {
182
            return _map.SubstituteNamedTypes(_substitutedFrom.GetInterfaces(inProgress));
P
Pilchie 已提交
183 184 185 186
        }

        internal override NamedTypeSymbol GetEffectiveBaseClass(ConsList<TypeParameterSymbol> inProgress)
        {
187
            return _map.SubstituteNamedType(_substitutedFrom.GetEffectiveBaseClass(inProgress));
P
Pilchie 已提交
188 189 190 191
        }

        internal override TypeSymbol GetDeducedBaseType(ConsList<TypeParameterSymbol> inProgress)
        {
192
            return _map.SubstituteType(_substitutedFrom.GetDeducedBaseType(inProgress)).AsTypeSymbolOnly();
P
Pilchie 已提交
193 194
        }

195
        private static readonly Func<TypeSymbol, bool> s_isNotObjectFunc = type => type.SpecialType != SpecialType.System_Object;
P
Pilchie 已提交
196
    }
197
}