SynthesizedImplementationMethod.cs 8.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 5 6 7 8 9

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
10
    internal abstract class SynthesizedImplementationMethod : SynthesizedInstanceMethodSymbol
P
Pilchie 已提交
11 12
    {
        //inputs
13 14 15 16
        private readonly MethodSymbol _interfaceMethod;
        private readonly NamedTypeSymbol _implementingType;
        private readonly bool _generateDebugInfo;
        private readonly PropertySymbol _associatedProperty;
P
Pilchie 已提交
17 18

        //computed
19 20 21 22 23
        private readonly ImmutableArray<MethodSymbol> _explicitInterfaceImplementations;
        private readonly ImmutableArray<TypeParameterSymbol> _typeParameters;
        private readonly TypeSymbol _returnType;
        private readonly ImmutableArray<ParameterSymbol> _parameters;
        private readonly string _name;
P
Pilchie 已提交
24 25 26 27 28

        public SynthesizedImplementationMethod(
            MethodSymbol interfaceMethod,
            NamedTypeSymbol implementingType,
            string name = null,
29
            bool generateDebugInfo = true,
30
            PropertySymbol associatedProperty = null)
P
Pilchie 已提交
31 32 33 34
        {
            //it does not make sense to add methods to substituted types
            Debug.Assert(implementingType.IsDefinition);

35 36 37 38 39 40
            _name = name ?? ExplicitInterfaceHelpers.GetMemberName(interfaceMethod.Name, interfaceMethod.ContainingType, aliasQualifierOpt: null);
            _interfaceMethod = interfaceMethod;
            _implementingType = implementingType;
            _generateDebugInfo = generateDebugInfo;
            _associatedProperty = associatedProperty;
            _explicitInterfaceImplementations = ImmutableArray.Create<MethodSymbol>(interfaceMethod);
P
Pilchie 已提交
41 42 43

            // alpha-rename to get the implementation's type parameters
            var typeMap = interfaceMethod.ContainingType.TypeSubstitution ?? TypeMap.Empty;
44
            typeMap.WithAlphaRename(interfaceMethod, this, out _typeParameters);
P
Pilchie 已提交
45

46 47 48
            var substitutedInterfaceMethod = interfaceMethod.ConstructIfGeneric(_typeParameters.Cast<TypeParameterSymbol, TypeSymbol>());
            _returnType = substitutedInterfaceMethod.ReturnType;
            _parameters = SynthesizedParameterSymbol.DeriveParameters(substitutedInterfaceMethod, this);
P
Pilchie 已提交
49 50 51 52 53 54
        }

        #region Delegate to interfaceMethod

        public sealed override bool IsVararg
        {
55
            get { return _interfaceMethod.IsVararg; }
P
Pilchie 已提交
56 57 58 59
        }

        public sealed override int Arity
        {
60
            get { return _interfaceMethod.Arity; }
P
Pilchie 已提交
61 62 63 64
        }

        public sealed override bool ReturnsVoid
        {
65
            get { return _interfaceMethod.ReturnsVoid; }
P
Pilchie 已提交
66 67 68 69
        }

        internal sealed override Cci.CallingConvention CallingConvention
        {
70
            get { return _interfaceMethod.CallingConvention; }
P
Pilchie 已提交
71 72 73 74
        }

        public sealed override ImmutableArray<CustomModifier> ReturnTypeCustomModifiers
        {
75
            get { return _interfaceMethod.ReturnTypeCustomModifiers; }
P
Pilchie 已提交
76 77 78 79
        }

        #endregion

80
        internal override void AddSynthesizedAttributes(ModuleCompilationState compilationState, ref ArrayBuilder<SynthesizedAttributeData> attributes)
P
Pilchie 已提交
81
        {
82
            base.AddSynthesizedAttributes(compilationState, ref attributes);
P
Pilchie 已提交
83

84 85
            var compilation = this.DeclaringCompilation;
            if (this.ReturnType.ContainsDynamic() && compilation.HasDynamicEmitAttributes() && compilation.CanEmitBoolean())
P
Pilchie 已提交
86 87 88 89 90
            {
                AddSynthesizedAttribute(ref attributes, compilation.SynthesizeDynamicAttribute(this.ReturnType, this.ReturnTypeCustomModifiers.Length));
            }
        }

91
        internal sealed override bool GenerateDebugInfo
P
Pilchie 已提交
92
        {
93
            get { return _generateDebugInfo; }
P
Pilchie 已提交
94 95 96 97
        }

        public sealed override ImmutableArray<TypeParameterSymbol> TypeParameters
        {
98
            get { return _typeParameters; }
P
Pilchie 已提交
99 100 101 102
        }

        public sealed override ImmutableArray<TypeSymbol> TypeArguments
        {
103
            get { return _typeParameters.Cast<TypeParameterSymbol, TypeSymbol>(); }
P
Pilchie 已提交
104 105 106 107
        }

        public sealed override TypeSymbol ReturnType
        {
108
            get { return _returnType; }
P
Pilchie 已提交
109 110 111 112
        }

        public override ImmutableArray<ParameterSymbol> Parameters
        {
113
            get { return _parameters; }
P
Pilchie 已提交
114 115 116 117
        }

        public override Symbol ContainingSymbol
        {
118
            get { return _implementingType; }
P
Pilchie 已提交
119 120 121 122 123 124
        }

        public override NamedTypeSymbol ContainingType
        {
            get
            {
125
                return _implementingType;
P
Pilchie 已提交
126 127 128 129 130 131 132 133 134 135
            }
        }

        internal override bool IsExplicitInterfaceImplementation
        {
            get { return true; }
        }

        public override ImmutableArray<MethodSymbol> ExplicitInterfaceImplementations
        {
136
            get { return _explicitInterfaceImplementations; }
P
Pilchie 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }

        public override MethodKind MethodKind
        {
            get
            {
                return MethodKind.ExplicitInterfaceImplementation;
            }
        }

        public override Accessibility DeclaredAccessibility
        {
            get { return Accessibility.Private; }
        }

152
        public override Symbol AssociatedSymbol
P
Pilchie 已提交
153
        {
154
            get { return _associatedProperty; }
P
Pilchie 已提交
155 156 157 158 159 160
        }

        public override bool HidesBaseMethodsByName
        {
            get { return false; }
        }
J
Jared Parsons 已提交
161

P
Pilchie 已提交
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
        public override ImmutableArray<Location> Locations
        {
            get { return ImmutableArray<Location>.Empty; }
        }

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

        public override bool IsAsync
        {
            get { return false; }
        }

        public override bool IsVirtual
        {
            get { return false; }
        }

        public override bool IsOverride
        {
            get { return false; }
        }

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

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

        public override bool IsExtern
        {
            get { return false; }
        }

        public override bool IsExtensionMethod
        {
            get { return false; }
        }

        public override string Name
        {
209
            get { return _name; }
P
Pilchie 已提交
210 211 212 213
        }

        internal sealed override bool HasSpecialName
        {
214
            get { return _interfaceMethod.HasSpecialName; }
P
Pilchie 已提交
215 216 217 218 219 220 221 222 223
        }

        internal sealed override System.Reflection.MethodImplAttributes ImplementationAttributes
        {
            get { return default(System.Reflection.MethodImplAttributes); }
        }

        internal sealed override bool RequiresSecurityObject
        {
224
            get { return _interfaceMethod.RequiresSecurityObject; }
P
Pilchie 已提交
225 226 227 228 229 230 231
        }

        internal sealed override bool IsMetadataVirtual(bool ignoreInterfaceImplementationChanges = false)
        {
            return true;
        }

232
        internal override bool IsMetadataFinal
P
Pilchie 已提交
233
        {
234 235 236 237
            get
            {
                return true;
            }
P
Pilchie 已提交
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
        }

        internal sealed override bool IsMetadataNewSlot(bool ignoreInterfaceImplementationChanges = false)
        {
            return true;
        }

        public override DllImportData GetDllImportData()
        {
            return null;
        }

        internal override MarshalPseudoCustomAttributeData ReturnValueMarshallingInformation
        {
            get { return null; }
        }

        internal override bool HasDeclarativeSecurity
        {
            get { return false; }
        }

        internal override IEnumerable<Microsoft.Cci.SecurityAttribute> GetSecurityInformation()
        {
            throw ExceptionUtilities.Unreachable;
        }

        internal override ImmutableArray<string> GetAppliedConditionalSymbols()
        {
            return ImmutableArray<string>.Empty;
        }
    }
270
}