ICodeDefinitionFactoryExtensions.cs 23.9 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

                        var shouldAddNullCheck = addNullChecks && parameterType.CanAddNullCheck();
                        if (shouldAddNullCheck && preferThrowExpression)
                        {
                            var statement = CreateAssignWithNullCheckStatement(factory, compilation, parameter, fieldAccess);
                            assignStatements.Add(statement);
                        }
                        else
                        {
                            if (shouldAddNullCheck)
                            {
                                nullCheckStatements.Add(
                                    factory.CreateIfNullThrowStatement(compilation, parameter));
                            }

                            var assignExpression = factory.AssignmentStatement(
                                fieldAccess, factory.IdentifierName(parameterName));
                            var statement = factory.ExpressionStatement(assignExpression);
                            assignStatements.Add(statement);
                        }
P
Pilchie 已提交
255 256 257
                    }
                }
            }
258 259 260 261 262 263 264 265 266 267 268 269

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

        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 已提交
270 271
        }

C
CyrusNajmabadi 已提交
272
        public static async Task<IPropertySymbol> OverridePropertyAsync(
273
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
274
            IPropertySymbol overriddenProperty,
C
CyrusNajmabadi 已提交
275
            DeclarationModifiers modifiers,
P
Pilchie 已提交
276 277
            INamedTypeSymbol containingType,
            Document document,
C
CyrusNajmabadi 已提交
278
            CancellationToken cancellationToken)
P
Pilchie 已提交
279 280 281 282 283 284 285 286 287 288
        {
            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 已提交
289
                var compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
290 291 292 293
                var statement = codeFactory.CreateThrowNotImplementedStatement(compilation);

                getBody = statement;
                setBody = statement;
P
Pilchie 已提交
294 295 296 297
            }
            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.
298

C
CyrusNajmabadi 已提交
299
                getBody = codeFactory.ReturnStatement(
C
CyrusNajmabadi 已提交
300
                    WrapWithRefIfNecessary(codeFactory, overriddenProperty,
C
CyrusNajmabadi 已提交
301 302 303
                        codeFactory.ElementAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.CreateArguments(overriddenProperty.Parameters))));
P
Pilchie 已提交
304

305 306 307 308
                setBody = codeFactory.ExpressionStatement(
                    codeFactory.AssignmentStatement(
                    codeFactory.ElementAccessExpression(
                        codeFactory.BaseExpression(),
P
Pilchie 已提交
309
                        codeFactory.CreateArguments(overriddenProperty.Parameters)),
310
                    codeFactory.IdentifierName("value")));
P
Pilchie 已提交
311 312 313 314 315
            }
            else if (overriddenProperty.GetParameters().Any())
            {
                // Call accessors directly if C# overriding VB
                if (document.Project.Language == LanguageNames.CSharp
C
CyrusNajmabadi 已提交
316 317
                    && (await SymbolFinder.FindSourceDefinitionAsync(overriddenProperty, document.Project.Solution, cancellationToken).ConfigureAwait(false))
                        .Language == LanguageNames.VisualBasic)
P
Pilchie 已提交
318
                {
319 320
                    var getName = overriddenProperty.GetMethod?.Name;
                    var setName = overriddenProperty.SetMethod?.Name;
P
Pilchie 已提交
321 322 323

                    getBody = getName == null
                        ? null
324 325 326 327 328
                        : codeFactory.ReturnStatement(
                    codeFactory.InvocationExpression(
                        codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.IdentifierName(getName)),
P
Pilchie 已提交
329 330 331 332
                        codeFactory.CreateArguments(overriddenProperty.Parameters)));

                    setBody = setName == null
                        ? null
333 334 335 336 337
                        : codeFactory.ExpressionStatement(
                        codeFactory.InvocationExpression(
                            codeFactory.MemberAccessExpression(
                                codeFactory.BaseExpression(),
                                codeFactory.IdentifierName(setName)),
P
Pilchie 已提交
338 339 340 341
                            codeFactory.CreateArguments(overriddenProperty.SetMethod.GetParameters())));
                }
                else
                {
C
CyrusNajmabadi 已提交
342
                    getBody = codeFactory.ReturnStatement(
C
CyrusNajmabadi 已提交
343
                        WrapWithRefIfNecessary(codeFactory, overriddenProperty,
C
CyrusNajmabadi 已提交
344 345 346 347
                            codeFactory.InvocationExpression(
                                codeFactory.MemberAccessExpression(
                                    codeFactory.BaseExpression(),
                                    codeFactory.IdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters))));
348

349 350 351 352 353 354 355
                    setBody = codeFactory.ExpressionStatement(
                        codeFactory.AssignmentStatement(
                            codeFactory.InvocationExpression(
                            codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                        codeFactory.IdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters)),
                        codeFactory.IdentifierName("value")));
P
Pilchie 已提交
356 357 358 359 360
                }
            }
            else
            {
                // Regular property: return or set the base property
361

C
CyrusNajmabadi 已提交
362 363 364 365 366
                getBody = codeFactory.ReturnStatement(
                    WrapWithRefIfNecessary(codeFactory, overriddenProperty,
                        codeFactory.MemberAccessExpression(
                            codeFactory.BaseExpression(),
                            codeFactory.IdentifierName(overriddenProperty.Name))));
367

368 369 370 371 372 373
                setBody = codeFactory.ExpressionStatement(
                    codeFactory.AssignmentStatement(
                        codeFactory.MemberAccessExpression(
                        codeFactory.BaseExpression(),
                    codeFactory.IdentifierName(overriddenProperty.Name)),
                    codeFactory.IdentifierName("value")));
P
Pilchie 已提交
374 375 376 377 378 379 380 381 382
            }

            // 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 已提交
383
                    statements: ImmutableArray.Create(getBody),
P
Pilchie 已提交
384 385 386 387 388 389 390 391 392 393 394 395
                    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 已提交
396
                    statements: ImmutableArray.Create(setBody),
P
Pilchie 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409
                    modifiers: modifiers);
            }

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

C
CyrusNajmabadi 已提交
410 411 412 413 414
        private static SyntaxNode WrapWithRefIfNecessary(SyntaxGenerator codeFactory, IPropertySymbol overriddenProperty, SyntaxNode body)
            => overriddenProperty.ReturnsByRef
                ? codeFactory.RefExpression(body)
                : body;

P
Pilchie 已提交
415
        public static IEventSymbol OverrideEvent(
416
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
417
            IEventSymbol overriddenEvent,
C
CyrusNajmabadi 已提交
418 419
            DeclarationModifiers modifiers,
            INamedTypeSymbol newContainingType)
P
Pilchie 已提交
420 421 422
        {
            return CodeGenerationSymbolFactory.CreateEventSymbol(
                overriddenEvent,
C
CyrusNajmabadi 已提交
423
                attributes: default(ImmutableArray<AttributeData>),
P
Pilchie 已提交
424 425 426 427 428 429
                accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
                modifiers: modifiers,
                explicitInterfaceSymbol: null,
                name: overriddenEvent.Name);
        }

430 431 432 433 434 435 436 437
        public static async Task<ISymbol> OverrideAsync(
            this SyntaxGenerator generator,
            ISymbol symbol,
            INamedTypeSymbol containingType,
            Document document,
            DeclarationModifiers? modifiersOpt = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
438
            var modifiers = modifiersOpt ?? GetOverrideModifiers(symbol);
439 440 441 442

            if (symbol is IMethodSymbol method)
            {
                return await generator.OverrideMethodAsync(method,
C
CyrusNajmabadi 已提交
443
                    modifiers, containingType, document, cancellationToken).ConfigureAwait(false);
444 445 446 447
            }
            else if (symbol is IPropertySymbol property)
            {
                return await generator.OverridePropertyAsync(property,
C
CyrusNajmabadi 已提交
448
                    modifiers, containingType, document, cancellationToken).ConfigureAwait(false);
449 450 451
            }
            else if (symbol is IEventSymbol ev)
            {
C
CyrusNajmabadi 已提交
452
                return generator.OverrideEvent(ev, modifiers, containingType);
453 454 455
            }
            else
            {
C
CyrusNajmabadi 已提交
456
                throw ExceptionUtilities.Unreachable;
457 458 459
            }
        }

460 461 462 463 464 465
        private static DeclarationModifiers GetOverrideModifiers(ISymbol symbol)
            => symbol.GetSymbolModifiers()
                     .WithIsOverride(true)
                     .WithIsAbstract(false)
                     .WithIsVirtual(false);

C
CyrusNajmabadi 已提交
466
        private static async Task<IMethodSymbol> OverrideMethodAsync(
467
            this SyntaxGenerator codeFactory,
P
Pilchie 已提交
468
            IMethodSymbol overriddenMethod,
C
CyrusNajmabadi 已提交
469
            DeclarationModifiers modifiers,
P
Pilchie 已提交
470 471
            INamedTypeSymbol newContainingType,
            Document newDocument,
C
CyrusNajmabadi 已提交
472
            CancellationToken cancellationToken)
P
Pilchie 已提交
473 474 475 476
        {
            // Abstract: Throw not implemented
            if (overriddenMethod.IsAbstract)
            {
C
CyrusNajmabadi 已提交
477
                var compilation = await newDocument.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
478 479
                var statement = codeFactory.CreateThrowNotImplementedStatement(compilation);

P
Pilchie 已提交
480 481 482 483
                return CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenMethod,
                    accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                    modifiers: modifiers,
C
CyrusNajmabadi 已提交
484
                    statements: ImmutableArray.Create(statement));
P
Pilchie 已提交
485 486 487 488 489
            }
            else
            {
                // Otherwise, call the base method with the same parameters
                var typeParams = overriddenMethod.GetTypeArguments();
490 491
                var body = codeFactory.InvocationExpression(
                    codeFactory.MemberAccessExpression(codeFactory.BaseExpression(),
P
Pilchie 已提交
492
                    typeParams.IsDefaultOrEmpty
493 494
                        ? codeFactory.IdentifierName(overriddenMethod.Name)
                        : codeFactory.GenericName(overriddenMethod.Name, typeParams)),
P
Pilchie 已提交
495 496
                    codeFactory.CreateArguments(overriddenMethod.GetParameters()));

497 498 499 500 501
                if (overriddenMethod.ReturnsByRef)
                {
                    body = codeFactory.RefExpression(body);
                }

P
Pilchie 已提交
502 503 504 505
                return CodeGenerationSymbolFactory.CreateMethodSymbol(
                    method: overriddenMethod,
                    accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                    modifiers: modifiers,
C
Cyrus Najmabadi 已提交
506
                    statements: overriddenMethod.ReturnsVoid
C
CyrusNajmabadi 已提交
507 508
                        ? ImmutableArray.Create(codeFactory.ExpressionStatement(body))
                        : ImmutableArray.Create(codeFactory.ReturnStatement(body)));
P
Pilchie 已提交
509 510 511
            }
        }
    }
C
CyrusNajmabadi 已提交
512
}