ICodeDefinitionFactoryExtensions.cs 24.5 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

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
C
CyrusNajmabadi 已提交
7
using System.Threading.Tasks;
P
Pilchie 已提交
8
using Microsoft.CodeAnalysis.CodeGeneration;
9
using Microsoft.CodeAnalysis.Editing;
P
Pilchie 已提交
10
using Microsoft.CodeAnalysis.FindSymbols;
11
using Microsoft.CodeAnalysis.Simplification;
P
Pilchie 已提交
12 13 14 15 16 17
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Shared.Extensions
{
    internal static partial class ICodeDefinitionFactoryExtensions
    {
18
        public static SyntaxNode CreateThrowNotImplementedStatement(
19
            this SyntaxGenerator codeDefinitionFactory,
P
Pilchie 已提交
20 21
            Compilation compilation)
        {
22
            return codeDefinitionFactory.ThrowStatement(
23 24 25
               codeDefinitionFactory.ObjectCreationExpression(
                   codeDefinitionFactory.TypeExpression(compilation.NotImplementedExceptionType(), addImport: false),
                   SpecializedCollections.EmptyList<SyntaxNode>()));
P
Pilchie 已提交
26 27
        }

C
CyrusNajmabadi 已提交
28 29 30
        public static ImmutableArray<SyntaxNode> CreateThrowNotImplementedStatementBlock(
            this SyntaxGenerator codeDefinitionFactory, Compilation compilation)
            => ImmutableArray.Create(CreateThrowNotImplementedStatement(codeDefinitionFactory, compilation));
P
Pilchie 已提交
31

C
CyrusNajmabadi 已提交
32
        public static ImmutableArray<SyntaxNode> CreateArguments(
33
            this SyntaxGenerator factory,
P
Pilchie 已提交
34 35
            ImmutableArray<IParameterSymbol> parameters)
        {
C
CyrusNajmabadi 已提交
36
            return parameters.SelectAsArray(p => CreateArgument(factory, p));
P
Pilchie 已提交
37 38 39
        }

        private static SyntaxNode CreateArgument(
40
            this SyntaxGenerator factory,
P
Pilchie 已提交
41 42
            IParameterSymbol parameter)
        {
43
            return factory.Argument(parameter.RefKind, factory.IdentifierName(parameter.Name));
P
Pilchie 已提交
44 45 46
        }

        public static IMethodSymbol CreateBaseDelegatingConstructor(
47
            this SyntaxGenerator factory,
P
Pilchie 已提交
48 49 50 51 52 53
            IMethodSymbol constructor,
            string typeName)
        {
            // Create a constructor that calls the base constructor.  Note: if there are no
            // parameters then don't bother writing out "base()" it's automatically implied.
            return CodeGenerationSymbolFactory.CreateConstructorSymbol(
C
CyrusNajmabadi 已提交
54
                attributes: default(ImmutableArray<AttributeData>),
P
Pilchie 已提交
55
                accessibility: Accessibility.Public,
56
                modifiers: new DeclarationModifiers(),
P
Pilchie 已提交
57 58
                typeName: typeName,
                parameters: constructor.Parameters,
C
CyrusNajmabadi 已提交
59
                statements: default(ImmutableArray<SyntaxNode>),
C
CyrusNajmabadi 已提交
60
                baseConstructorArguments: constructor.Parameters.Length == 0
C
CyrusNajmabadi 已提交
61 62
                    ? default(ImmutableArray<SyntaxNode>)
                    : factory.CreateArguments(constructor.Parameters));
P
Pilchie 已提交
63 64 65
        }

        public static IEnumerable<ISymbol> CreateFieldDelegatingConstructor(
66
            this SyntaxGenerator factory,
67
            Compilation compilation,
P
Pilchie 已提交
68 69
            string typeName,
            INamedTypeSymbol containingTypeOpt,
C
CyrusNajmabadi 已提交
70
            ImmutableArray<IParameterSymbol> parameters,
P
Pilchie 已提交
71 72
            IDictionary<string, ISymbol> parameterToExistingFieldMap,
            IDictionary<string, string> parameterToNewFieldMap,
73 74
            bool addNullChecks,
            bool preferThrowExpression,
P
Pilchie 已提交
75 76 77
            CancellationToken cancellationToken)
        {
            var fields = factory.CreateFieldsForParameters(parameters, parameterToNewFieldMap);
78 79 80 81
            var statements = factory.CreateAssignmentStatements(
                compilation, parameters, parameterToExistingFieldMap, parameterToNewFieldMap, 
                addNullChecks, preferThrowExpression).SelectAsArray(
                    s => s.WithAdditionalAnnotations(Simplifier.Annotation));
P
Pilchie 已提交
82 83 84 85 86 87 88

            foreach (var field in fields)
            {
                yield return field;
            }

            yield return CodeGenerationSymbolFactory.CreateConstructorSymbol(
C
CyrusNajmabadi 已提交
89
                attributes: default(ImmutableArray<AttributeData>),
P
Pilchie 已提交
90
                accessibility: Accessibility.Public,
91
                modifiers: new DeclarationModifiers(),
P
Pilchie 已提交
92 93
                typeName: typeName,
                parameters: parameters,
94
                statements: statements,
P
Pilchie 已提交
95 96 97
                thisConstructorArguments: GetThisConstructorArguments(containingTypeOpt, parameterToExistingFieldMap));
        }

C
CyrusNajmabadi 已提交
98
        private static ImmutableArray<SyntaxNode> GetThisConstructorArguments(
P
Pilchie 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            INamedTypeSymbol containingTypeOpt,
            IDictionary<string, ISymbol> parameterToExistingFieldMap)
        {
            if (containingTypeOpt != null && containingTypeOpt.TypeKind == TypeKind.Struct)
            {
                // Special case.  If we're generating a struct constructor, then we'll need
                // to initialize all fields in the struct, not just the ones we're creating.  To
                // do that, we call the default constructor.
                var realFields = containingTypeOpt.GetMembers()
                                     .OfType<IFieldSymbol>()
                                     .Where(f => !f.IsStatic);
                var initializedFields = parameterToExistingFieldMap.Values
                                            .OfType<IFieldSymbol>()
                                            .Where(f => !f.IsImplicitlyDeclared && !f.IsStatic);
                if (initializedFields.Count() < realFields.Count())
                {
                    // We have less field assignments than actual fields.  Generate a call to the
                    // default constructor as well.
C
CyrusNajmabadi 已提交
117
                    return ImmutableArray<SyntaxNode>.Empty;
P
Pilchie 已提交
118 119 120
                }
            }

C
CyrusNajmabadi 已提交
121
            return default(ImmutableArray<SyntaxNode>);
P
Pilchie 已提交
122 123 124
        }

        public static IEnumerable<IFieldSymbol> CreateFieldsForParameters(
125
            this SyntaxGenerator factory,
P
Pilchie 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
            IList<IParameterSymbol> parameters,
            IDictionary<string, string> parameterToNewFieldMap)
        {
            foreach (var parameter in parameters)
            {
                var refKind = parameter.RefKind;
                var parameterType = parameter.Type;
                var parameterName = parameter.Name;

                if (refKind != RefKind.Out)
                {
                    // For non-out parameters, create a field and assign the parameter to it. 
                    // TODO: I'm not sure that's what we really want for ref parameters. 
C
CyrusNajmabadi 已提交
139
                    if (TryGetValue(parameterToNewFieldMap, parameterName, out var fieldName))
P
Pilchie 已提交
140 141
                    {
                        yield return CodeGenerationSymbolFactory.CreateFieldSymbol(
C
CyrusNajmabadi 已提交
142
                            attributes: default(ImmutableArray<AttributeData>),
P
Pilchie 已提交
143
                            accessibility: Accessibility.Private,
144
                            modifiers: default(DeclarationModifiers),
P
Pilchie 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                            type: parameterType,
                            name: parameterToNewFieldMap[parameterName]);
                    }
                }
            }
        }

        private static bool TryGetValue(IDictionary<string, string> dictionary, string key, out string value)
        {
            value = null;
            return
                dictionary != null &&
                dictionary.TryGetValue(key, out value);
        }

        private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string key, out string value)
        {
            value = null;
C
CyrusNajmabadi 已提交
163
            if (dictionary != null && dictionary.TryGetValue(key, out var symbol))
P
Pilchie 已提交
164 165 166 167 168 169 170 171
            {
                value = symbol.Name;
                return true;
            }

            return false;
        }

172
        public static SyntaxNode CreateThrowArgumentNullExpression(
173
            this SyntaxGenerator factory,
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
            Compilation compilation,
            IParameterSymbol parameter)
        {
            return factory.ThrowExpression(
                factory.ObjectCreationExpression(
                    compilation.GetTypeByMetadataName("System.ArgumentNullException"),
                    factory.NameOfExpression(
                        factory.IdentifierName(parameter.Name))));
        }

        public static SyntaxNode CreateIfNullThrowStatement(
            this SyntaxGenerator factory,
            Compilation compilation,
            IParameterSymbol parameter)
        {
            return factory.IfStatement(
                factory.ReferenceEqualsExpression(
                    factory.IdentifierName(parameter.Name),
                    factory.NullLiteralExpression()),
                SpecializedCollections.SingletonEnumerable(
                    factory.ExpressionStatement(
                        factory.CreateThrowArgumentNullExpression(compilation, parameter))));
        }

        public static ImmutableArray<SyntaxNode> CreateAssignmentStatements(
            this SyntaxGenerator factory,
            Compilation compilation,
P
Pilchie 已提交
201 202
            IList<IParameterSymbol> parameters,
            IDictionary<string, ISymbol> parameterToExistingFieldMap,
203 204 205
            IDictionary<string, string> parameterToNewFieldMap,
            bool addNullChecks,
            bool preferThrowExpression)
P
Pilchie 已提交
206
        {
207 208 209
            var nullCheckStatements = ArrayBuilder<SyntaxNode>.GetInstance();
            var assignStatements = ArrayBuilder<SyntaxNode>.GetInstance();

P
Pilchie 已提交
210 211 212 213 214 215 216 217 218
            foreach (var parameter in parameters)
            {
                var refKind = parameter.RefKind;
                var parameterType = parameter.Type;
                var parameterName = parameter.Name;

                if (refKind == RefKind.Out)
                {
                    // If it's an out param, then don't create a field for it.  Instead, assign
219
                    // the default value for that type (i.e. "default(...)") to it.
220 221 222 223
                    var assignExpression = factory.AssignmentStatement(
                        factory.IdentifierName(parameterName),
                        factory.DefaultExpression(parameterType));
                    var statement = factory.ExpressionStatement(assignExpression);
224
                    assignStatements.Add(statement);
P
Pilchie 已提交
225 226 227 228 229
                }
                else
                {
                    // For non-out parameters, create a field and assign the parameter to it. 
                    // TODO: I'm not sure that's what we really want for ref parameters. 
C
CyrusNajmabadi 已提交
230
                    if (TryGetValue(parameterToExistingFieldMap, parameterName, out var fieldName) ||
P
Pilchie 已提交
231 232
                        TryGetValue(parameterToNewFieldMap, parameterName, out fieldName))
                    {
233 234
                        var fieldAccess = factory.MemberAccessExpression(factory.ThisExpression(), factory.IdentifierName(fieldName))
                                                 .WithAdditionalAnnotations(Simplifier.Annotation);
235

C
CyrusNajmabadi 已提交
236 237 238 239
                        factory.AddAssignmentStatements(
                            compilation, parameter, fieldAccess,
                            addNullChecks, preferThrowExpression,
                            nullCheckStatements, assignStatements);
P
Pilchie 已提交
240 241 242
                    }
                }
            }
243 244 245 246

            return nullCheckStatements.ToImmutableAndFree().Concat(assignStatements.ToImmutableAndFree());
        }

C
CyrusNajmabadi 已提交
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
        public static void AddAssignmentStatements(
             this SyntaxGenerator factory,
             Compilation compilation,
             IParameterSymbol parameter,
             SyntaxNode fieldAccess,
             bool addNullChecks,
             bool preferThrowExpression,
             ArrayBuilder<SyntaxNode> nullCheckStatements,
             ArrayBuilder<SyntaxNode> assignStatements)
        {
            var shouldAddNullCheck = addNullChecks && parameter.Type.CanAddNullCheck();
            if (shouldAddNullCheck && preferThrowExpression)
            {
                // Generate: this.x = x ?? throw ...
                assignStatements.Add(CreateAssignWithNullCheckStatement(
                    factory, compilation, parameter, fieldAccess));
            }
            else
            {
                if (shouldAddNullCheck)
                {
                    // generate: if (x == null) throw ...
                    nullCheckStatements.Add(
                        factory.CreateIfNullThrowStatement(compilation, parameter));
                }

                // generate: this.x = x;
                assignStatements.Add(
                    factory.ExpressionStatement(
                        factory.AssignmentStatement(
                            fieldAccess,
                            factory.IdentifierName(parameter.Name))));
            }
        }

282 283 284 285 286 287 288 289
        public static SyntaxNode CreateAssignWithNullCheckStatement(
            this SyntaxGenerator factory, Compilation compilation, IParameterSymbol parameter, SyntaxNode fieldAccess)
        {
            return factory.ExpressionStatement(factory.AssignmentStatement(
                fieldAccess,
                factory.CoalesceExpression(
                    factory.IdentifierName(parameter.Name),
                    factory.CreateThrowArgumentNullExpression(compilation, parameter))));
P
Pilchie 已提交
290 291
        }

C
CyrusNajmabadi 已提交
292
        public static async Task<IPropertySymbol> OverridePropertyAsync(
293
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
294
            IPropertySymbol overriddenProperty,
C
CyrusNajmabadi 已提交
295
            DeclarationModifiers modifiers,
P
Pilchie 已提交
296 297
            INamedTypeSymbol containingType,
            Document document,
C
CyrusNajmabadi 已提交
298
            CancellationToken cancellationToken)
P
Pilchie 已提交
299 300 301 302 303 304 305 306 307 308
        {
            var getAccessibility = overriddenProperty.GetMethod.ComputeResultantAccessibility(containingType);
            var setAccessibility = overriddenProperty.SetMethod.ComputeResultantAccessibility(containingType);

            SyntaxNode getBody = null;
            SyntaxNode setBody = null;

            // Implement an abstract property by throwing not implemented in accessors.
            if (overriddenProperty.IsAbstract)
            {
C
CyrusNajmabadi 已提交
309
                var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
310 311 312 313
                var statement = codeFactory.CreateThrowNotImplementedStatement(compilation);

                getBody = statement;
                setBody = statement;
P
Pilchie 已提交
314 315 316 317
            }
            else if (overriddenProperty.IsIndexer() && document.Project.Language == LanguageNames.CSharp)
            {
                // Indexer: return or set base[]. Only in C#, since VB must refer to these by name.
318

C
CyrusNajmabadi 已提交
319
                getBody = codeFactory.ReturnStatement(
C
CyrusNajmabadi 已提交
320
                    WrapWithRefIfNecessary(codeFactory, overriddenProperty,
C
CyrusNajmabadi 已提交
321 322 323
                        codeFactory.ElementAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.CreateArguments(overriddenProperty.Parameters))));
P
Pilchie 已提交
324

325 326 327 328
                setBody = codeFactory.ExpressionStatement(
                    codeFactory.AssignmentStatement(
                    codeFactory.ElementAccessExpression(
                        codeFactory.BaseExpression(),
P
Pilchie 已提交
329
                        codeFactory.CreateArguments(overriddenProperty.Parameters)),
330
                    codeFactory.IdentifierName("value")));
P
Pilchie 已提交
331 332 333 334 335
            }
            else if (overriddenProperty.GetParameters().Any())
            {
                // Call accessors directly if C# overriding VB
                if (document.Project.Language == LanguageNames.CSharp
C
CyrusNajmabadi 已提交
336 337
                    && (await SymbolFinder.FindSourceDefinitionAsync(overriddenProperty, document.Project.Solution, cancellationToken).ConfigureAwait(false))
                        .Language == LanguageNames.VisualBasic)
P
Pilchie 已提交
338
                {
339 340
                    var getName = overriddenProperty.GetMethod?.Name;
                    var setName = overriddenProperty.SetMethod?.Name;
P
Pilchie 已提交
341 342 343

                    getBody = getName == null
                        ? null
344 345 346 347 348
                        : codeFactory.ReturnStatement(
                    codeFactory.InvocationExpression(
                        codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.IdentifierName(getName)),
P
Pilchie 已提交
349 350 351 352
                        codeFactory.CreateArguments(overriddenProperty.Parameters)));

                    setBody = setName == null
                        ? null
353 354 355 356 357
                        : codeFactory.ExpressionStatement(
                        codeFactory.InvocationExpression(
                            codeFactory.MemberAccessExpression(
                                codeFactory.BaseExpression(),
                                codeFactory.IdentifierName(setName)),
P
Pilchie 已提交
358 359 360 361
                            codeFactory.CreateArguments(overriddenProperty.SetMethod.GetParameters())));
                }
                else
                {
C
CyrusNajmabadi 已提交
362
                    getBody = codeFactory.ReturnStatement(
C
CyrusNajmabadi 已提交
363
                        WrapWithRefIfNecessary(codeFactory, overriddenProperty,
C
CyrusNajmabadi 已提交
364 365 366 367
                            codeFactory.InvocationExpression(
                                codeFactory.MemberAccessExpression(
                                    codeFactory.BaseExpression(),
                                    codeFactory.IdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters))));
368

369 370 371 372 373 374 375
                    setBody = codeFactory.ExpressionStatement(
                        codeFactory.AssignmentStatement(
                            codeFactory.InvocationExpression(
                            codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                        codeFactory.IdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters)),
                        codeFactory.IdentifierName("value")));
P
Pilchie 已提交
376 377 378 379 380
                }
            }
            else
            {
                // Regular property: return or set the base property
381

C
CyrusNajmabadi 已提交
382 383 384 385 386
                getBody = codeFactory.ReturnStatement(
                    WrapWithRefIfNecessary(codeFactory, overriddenProperty,
                        codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.IdentifierName(overriddenProperty.Name))));
387

388 389 390 391 392 393
                setBody = codeFactory.ExpressionStatement(
                    codeFactory.AssignmentStatement(
                        codeFactory.MemberAccessExpression(
                        codeFactory.BaseExpression(),
                    codeFactory.IdentifierName(overriddenProperty.Name)),
                    codeFactory.IdentifierName("value")));
P
Pilchie 已提交
394 395 396 397 398 399 400 401 402
            }

            // Only generate a getter if the base getter is accessible.
            IMethodSymbol accessorGet = null;
            if (overriddenProperty.GetMethod != null && overriddenProperty.GetMethod.IsAccessibleWithin(containingType))
            {
                accessorGet = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenProperty.GetMethod,
                    accessibility: getAccessibility,
C
CyrusNajmabadi 已提交
403
                    statements: ImmutableArray.Create(getBody),
P
Pilchie 已提交
404 405 406 407 408 409 410 411 412 413 414 415
                    modifiers: modifiers);
            }

            // Only generate a setter if the base setter is accessible.
            IMethodSymbol accessorSet = null;
            if (overriddenProperty.SetMethod != null &&
                overriddenProperty.SetMethod.IsAccessibleWithin(containingType) &&
                overriddenProperty.SetMethod.DeclaredAccessibility != Accessibility.Private)
            {
                accessorSet = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenProperty.SetMethod,
                    accessibility: setAccessibility,
C
CyrusNajmabadi 已提交
416
                    statements: ImmutableArray.Create(setBody),
P
Pilchie 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429
                    modifiers: modifiers);
            }

            return CodeGenerationSymbolFactory.CreatePropertySymbol(
                overriddenProperty,
                accessibility: overriddenProperty.ComputeResultantAccessibility(containingType),
                modifiers: modifiers,
                name: overriddenProperty.Name,
                isIndexer: overriddenProperty.IsIndexer(),
                getMethod: accessorGet,
                setMethod: accessorSet);
        }

C
CyrusNajmabadi 已提交
430 431 432 433 434
        private static SyntaxNode WrapWithRefIfNecessary(SyntaxGenerator codeFactory, IPropertySymbol overriddenProperty, SyntaxNode body)
            => overriddenProperty.ReturnsByRef
                ? codeFactory.RefExpression(body)
                : body;

P
Pilchie 已提交
435
        public static IEventSymbol OverrideEvent(
436
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
437
            IEventSymbol overriddenEvent,
C
CyrusNajmabadi 已提交
438 439
            DeclarationModifiers modifiers,
            INamedTypeSymbol newContainingType)
P
Pilchie 已提交
440 441 442
        {
            return CodeGenerationSymbolFactory.CreateEventSymbol(
                overriddenEvent,
C
CyrusNajmabadi 已提交
443
                attributes: default(ImmutableArray<AttributeData>),
P
Pilchie 已提交
444 445
                accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
                modifiers: modifiers,
446
                explicitInterfaceImplementations: default,
P
Pilchie 已提交
447 448 449
                name: overriddenEvent.Name);
        }

450 451 452 453 454 455 456 457
        public static async Task<ISymbol> OverrideAsync(
            this SyntaxGenerator generator,
            ISymbol symbol,
            INamedTypeSymbol containingType,
            Document document,
            DeclarationModifiers? modifiersOpt = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
458
            var modifiers = modifiersOpt ?? GetOverrideModifiers(symbol);
459 460 461 462

            if (symbol is IMethodSymbol method)
            {
                return await generator.OverrideMethodAsync(method,
C
CyrusNajmabadi 已提交
463
                    modifiers, containingType, document, cancellationToken).ConfigureAwait(false);
464 465 466 467
            }
            else if (symbol is IPropertySymbol property)
            {
                return await generator.OverridePropertyAsync(property,
C
CyrusNajmabadi 已提交
468
                    modifiers, containingType, document, cancellationToken).ConfigureAwait(false);
469 470 471
            }
            else if (symbol is IEventSymbol ev)
            {
C
CyrusNajmabadi 已提交
472
                return generator.OverrideEvent(ev, modifiers, containingType);
473 474 475
            }
            else
            {
C
CyrusNajmabadi 已提交
476
                throw ExceptionUtilities.Unreachable;
477 478 479
            }
        }

480 481 482 483 484 485
        private static DeclarationModifiers GetOverrideModifiers(ISymbol symbol)
            => symbol.GetSymbolModifiers()
                     .WithIsOverride(true)
                     .WithIsAbstract(false)
                     .WithIsVirtual(false);

C
CyrusNajmabadi 已提交
486
        private static async Task<IMethodSymbol> OverrideMethodAsync(
487
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
488
            IMethodSymbol overriddenMethod,
C
CyrusNajmabadi 已提交
489
            DeclarationModifiers modifiers,
P
Pilchie 已提交
490 491
            INamedTypeSymbol newContainingType,
            Document newDocument,
C
CyrusNajmabadi 已提交
492
            CancellationToken cancellationToken)
P
Pilchie 已提交
493 494 495 496
        {
            // Abstract: Throw not implemented
            if (overriddenMethod.IsAbstract)
            {
C
CyrusNajmabadi 已提交
497
                var compilation = await newDocument.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
498 499
                var statement = codeFactory.CreateThrowNotImplementedStatement(compilation);

P
Pilchie 已提交
500 501 502 503
                return CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenMethod,
                    accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                    modifiers: modifiers,
C
CyrusNajmabadi 已提交
504
                    statements: ImmutableArray.Create(statement));
P
Pilchie 已提交
505 506 507 508 509
            }
            else
            {
                // Otherwise, call the base method with the same parameters
                var typeParams = overriddenMethod.GetTypeArguments();
510 511
                var body = codeFactory.InvocationExpression(
                    codeFactory.MemberAccessExpression(codeFactory.BaseExpression(),
P
Pilchie 已提交
512
                    typeParams.IsDefaultOrEmpty
513 514
                        ? codeFactory.IdentifierName(overriddenMethod.Name)
                        : codeFactory.GenericName(overriddenMethod.Name, typeParams)),
P
Pilchie 已提交
515 516
                    codeFactory.CreateArguments(overriddenMethod.GetParameters()));

517 518 519 520 521
                if (overriddenMethod.ReturnsByRef)
                {
                    body = codeFactory.RefExpression(body);
                }

P
Pilchie 已提交
522 523 524 525
                return CodeGenerationSymbolFactory.CreateMethodSymbol(
                    method: overriddenMethod,
                    accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                    modifiers: modifiers,
C
Cyrus Najmabadi 已提交
526
                    statements: overriddenMethod.ReturnsVoid
C
CyrusNajmabadi 已提交
527 528
                        ? ImmutableArray.Create(codeFactory.ExpressionStatement(body))
                        : ImmutableArray.Create(codeFactory.ReturnStatement(body)));
P
Pilchie 已提交
529 530 531
            }
        }
    }
C
CyrusNajmabadi 已提交
532
}