提交 ddaaa05a 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #17390 from CyrusNajmabadi/useImmutableArray

Use immutable arrays.
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -273,19 +274,17 @@ private void GenerateAndAddEventHandler(ITextView textView, ITextBuffer subjectB
var syntaxFactory = document.Project.LanguageServices.GetService<SyntaxGenerator>();
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Private,
modifiers: new DeclarationModifiers(isStatic: eventHookupExpression.IsInStaticContext()),
returnType: delegateType.DelegateInvokeMethod.ReturnType,
returnsByRef: delegateType.DelegateInvokeMethod.ReturnsByRef,
explicitInterfaceSymbol: null,
name: eventHandlerMethodName,
typeParameters: null,
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: delegateType.DelegateInvokeMethod.Parameters,
statements: new List<SyntaxNode>
{
CodeGenerationHelpers.GenerateThrowStatement(syntaxFactory, document, "System.NotImplementedException", cancellationToken)
});
statements: ImmutableArray.Create(
CodeGenerationHelpers.GenerateThrowStatement(syntaxFactory, document, "System.NotImplementedException", cancellationToken)));
}
private void BeginInlineRename(Workspace workspace, ITextView textView, ITextBuffer subjectBuffer, int plusEqualTokenEndPosition, CancellationToken cancellationToken)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Host.Mef;
......@@ -42,7 +43,7 @@ public abstract class AbstractCodeGenerationTests
protected static ITypeSymbol CreateClass(string name)
{
return CodeGenerationSymbolFactory.CreateNamedTypeSymbol(
null, default(Accessibility), default(DeclarationModifiers), TypeKind.Class, name);
default(ImmutableArray<AttributeData>), default(Accessibility), default(DeclarationModifiers), TypeKind.Class, name);
}
}
}
}
\ No newline at end of file
......@@ -21,6 +21,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeGeneration
{
......@@ -117,7 +118,7 @@ public async Task AddChainedConstructor()
var input = "class [|C|] { public C(int i) { } }";
var expected = "class C { public C() : this(42) { } public C(int i) { } }";
await TestAddConstructorAsync(input, expected,
thisArguments: new[] { CS.SyntaxFactory.ParseExpression("42") });
thisArguments: ImmutableArray.Create<SyntaxNode>(CS.SyntaxFactory.ParseExpression("42")));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)]
......@@ -288,8 +289,8 @@ public async Task AddEventWithAccessors()
var input = "class [|C|] { }";
var expected = "class C { public event System.Action E { add { } remove { } } }";
await TestAddEventAsync(input, expected,
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, SpecializedCollections.EmptyList<SyntaxNode>()),
removeMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, SpecializedCollections.EmptyList<SyntaxNode>()),
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, ImmutableArray<SyntaxNode>.Empty),
removeMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, ImmutableArray<SyntaxNode>.Empty),
codeGenerationOptions: new CodeGenerationOptions(addImports: false));
}
......@@ -362,7 +363,7 @@ public async Task AddGenericMethod()
var expected = "class C { public int M<T>() { $$ } }";
await TestAddMethodAsync(input, expected,
returnType: typeof(int),
typeParameters: new[] { CodeGenerationSymbolFactory.CreateTypeParameterSymbol("T") },
typeParameters: ImmutableArray.Create(CodeGenerationSymbolFactory.CreateTypeParameterSymbol("T")),
statements: "return new T().GetHashCode();");
}
......@@ -1019,9 +1020,9 @@ public static class C
public int f2;
}";
var getField = CreateField(Accessibility.Public, new DeclarationModifiers(), typeof(int), "f2");
var getMembers = new List<Func<SemanticModel, ISymbol>>();
getMembers.Add(getField);
await TestUpdateDeclarationAsync<ClassDeclarationSyntax>(input, expected, getNewMembers: getMembers);
var getMembers = ImmutableArray.Create(getField);
await TestUpdateDeclarationAsync<ClassDeclarationSyntax>(
input, expected, getNewMembers: getMembers);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration)]
......@@ -1045,8 +1046,7 @@ public static class C
public static char F() { return 0; }
}";
var getField = CreateField(Accessibility.Public, new DeclarationModifiers(), typeof(int), "f2");
var getMembers = new List<Func<SemanticModel, ISymbol>>();
getMembers.Add(getField);
var getMembers = ImmutableArray.Create(getField);
await TestUpdateDeclarationAsync<ClassDeclarationSyntax>(input, expected, getNewMembers: getMembers, declareNewMembersAtTop: true);
}
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
......@@ -124,7 +125,7 @@ public async Task AddChainedConstructor()
var input = "Class [|C|]\n Public Sub New(i As Integer)\n End Sub\n End Class";
var expected = "Class C\n Public Sub New()\n Me.New(42)\n End Sub\n Public Sub New(i As Integer)\n End Sub\n End Class";
await TestAddConstructorAsync(input, expected,
thisArguments: new[] { VB.SyntaxFactory.ParseExpression("42") });
thisArguments: ImmutableArray.Create<SyntaxNode>(VB.SyntaxFactory.ParseExpression("42")));
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeGeneration), WorkItem(544476, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544476")]
......@@ -303,7 +304,8 @@ End Event
Func<SemanticModel, IEventSymbol> getExplicitInterfaceEvent = semanticModel =>
{
var parameterSymbols = SpecializedCollections.EmptyList<AttributeData>();
return new CodeGenerationEventSymbol(GetTypeSymbol(typeof(System.ComponentModel.INotifyPropertyChanged))(semanticModel), null,
return new CodeGenerationEventSymbol(GetTypeSymbol(typeof(System.ComponentModel.INotifyPropertyChanged))(semanticModel),
default(ImmutableArray<AttributeData>),
Accessibility.Public,
default(DeclarationModifiers),
GetTypeSymbol(typeof(System.ComponentModel.PropertyChangedEventHandler))(semanticModel),
......@@ -311,7 +313,7 @@ End Event
nameof(System.ComponentModel.INotifyPropertyChanged.PropertyChanged), null, null, null);
};
await TestAddEventAsync(input, expected,
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, SpecializedCollections.EmptyList<SyntaxNode>()),
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, ImmutableArray<SyntaxNode>.Empty),
explicitInterfaceSymbol: getExplicitInterfaceEvent,
type: typeof(System.ComponentModel.PropertyChangedEventHandler),
codeGenerationOptions: new CodeGenerationOptions(addImports: false));
......@@ -339,7 +341,7 @@ End RaiseEvent
End Event
End Class";
await TestAddEventAsync(input, expected,
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, SpecializedCollections.EmptyList<SyntaxNode>()),
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, ImmutableArray<SyntaxNode>.Empty),
codeGenerationOptions: new CodeGenerationOptions(addImports: false));
}
......@@ -366,16 +368,16 @@ End RemoveHandler
End RaiseEvent
End Event
End Class";
var addStatements = new List<SyntaxNode>() { VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(0)") };
var removeStatements = new List<SyntaxNode>() { VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(1)") };
var raiseStatements = new List<SyntaxNode>() { VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(2)") };
var addStatements = ImmutableArray.Create<SyntaxNode>(VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(0)"));
var removeStatements = ImmutableArray.Create<SyntaxNode>(VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(1)"));
var raiseStatements = ImmutableArray.Create<SyntaxNode>(VB.SyntaxFactory.ParseExecutableStatement("Console.WriteLine(2)"));
await TestAddEventAsync(input, expected,
addMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(
SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, addStatements),
ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, addStatements),
removeMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(
SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, removeStatements),
ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, removeStatements),
raiseMethod: CodeGenerationSymbolFactory.CreateAccessorSymbol(
SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, raiseStatements),
ImmutableArray<AttributeData>.Empty, Accessibility.NotApplicable, raiseStatements),
codeGenerationOptions: new CodeGenerationOptions(addImports: false));
}
......@@ -449,7 +451,7 @@ public async Task AddGenericMethod()
var expected = "Class C\n Public Function M(Of T)() As Integer\n $$ \nEnd Function\n End Class";
await TestAddMethodAsync(input, expected,
returnType: typeof(int),
typeParameters: new[] { CodeGenerationSymbolFactory.CreateTypeParameterSymbol("T") },
typeParameters: ImmutableArray.Create(CodeGenerationSymbolFactory.CreateTypeParameterSymbol("T")),
statements: "Return new T().GetHashCode()");
}
......@@ -1242,8 +1244,7 @@ End Function
Public Shared f2 As Integer
End Class";
var getField = CreateField(Accessibility.Public, new DeclarationModifiers(isStatic: true), typeof(int), "f2");
var getMembers = new List<Func<SemanticModel, ISymbol>>();
getMembers.Add(getField);
var getMembers = ImmutableArray.Create(getField);
await TestUpdateDeclarationAsync<ClassBlockSyntax>(input, expected, getNewMembers: getMembers);
}
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis.CodeGeneration
......@@ -27,7 +28,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
Return Nothing
End If
Dim statements As New List(Of SyntaxNode)
Dim statements As New ArrayBuilder(Of SyntaxNode)
If destinationType.IsDesignerGeneratedTypeWithInitializeComponent(compilation) Then
Dim statement = SyntaxFactory.ExpressionStatement(SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName("InitializeComponent"), SyntaxFactory.ArgumentList()))
......@@ -45,8 +46,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
accessibility:=Accessibility.Public,
modifiers:=New DeclarationModifiers(),
typeName:=destinationType.Name,
parameters:=SpecializedCollections.EmptyList(Of IParameterSymbol)(),
statements:=statements)
parameters:=ImmutableArray(Of IParameterSymbol).Empty,
statements:=statements.ToImmutableAndFree())
methodSymbol = GeneratedSymbolAnnotation.AddAnnotationToSymbol(methodSymbol)
Return Await CodeGenerator.AddMethodDeclarationAsync(document.Project.Solution,
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis.CodeGeneration
......@@ -59,8 +60,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
explicitInterfaceSymbol:=Nothing,
name:=methodName,
typeParameters:=Nothing,
parameters:=delegateInvokeMethod.Parameters.OfType(Of IParameterSymbol).ToList(),
handlesExpressions:={handlesSyntax})
parameters:=delegateInvokeMethod.Parameters,
handlesExpressions:=ImmutableArray.Create(Of SyntaxNode)(handlesSyntax))
methodSymbol = GeneratedSymbolAnnotation.AddAnnotationToSymbol(methodSymbol)
Return Await CodeGenerator.AddMethodDeclarationAsync(document.Project.Solution,
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.CodeAnalysis.CodeGeneration
......@@ -48,8 +49,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.NavigationBar
explicitInterfaceSymbol:=Nothing,
name:=WellKnownMemberNames.DestructorName,
typeParameters:=Nothing,
parameters:=SpecializedCollections.EmptyList(Of IParameterSymbol),
statements:={finalizeCall})
parameters:=ImmutableArray(Of IParameterSymbol).Empty,
statements:=ImmutableArray.Create(finalizeCall))
finalizerMethodSymbol = GeneratedSymbolAnnotation.AddAnnotationToSymbol(finalizerMethodSymbol)
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -89,7 +90,7 @@ protected override async Task<OperationStatus<IMethodSymbol>> GenerateMethodDefi
var result = await CreateMethodBodyAsync(cancellationToken).ConfigureAwait(false);
var methodSymbol = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.Private,
modifiers: CreateMethodModifiers(),
returnType: this.AnalyzerResult.ReturnType,
......@@ -217,7 +218,7 @@ private static SyntaxKind GetParameterRefSyntaxKind(ParameterBehavior parameterB
SyntaxKind.OutKeyword : SyntaxKind.None;
}
private async Task<OperationStatus<List<SyntaxNode>>> CreateMethodBodyAsync(CancellationToken cancellationToken)
private async Task<OperationStatus<ImmutableArray<SyntaxNode>>> CreateMethodBodyAsync(CancellationToken cancellationToken)
{
var statements = GetInitialStatementsForMethodDefinitions();
......@@ -228,7 +229,7 @@ private async Task<OperationStatus<List<SyntaxNode>>> CreateMethodBodyAsync(Canc
// set output so that we can use it in negative preview
var wrapped = WrapInCheckStatementIfNeeded(statements);
return CheckActiveStatements(statements).With(wrapped.ToList<SyntaxNode>());
return CheckActiveStatements(statements).With(wrapped.ToImmutableArray<SyntaxNode>());
}
private IEnumerable<StatementSyntax> WrapInCheckStatementIfNeeded(IEnumerable<StatementSyntax> statements)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
......@@ -207,15 +208,15 @@ protected override bool IsValidSymbol(ISymbol symbol, SemanticModel semanticMode
}
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: default(Accessibility),
modifiers: default(DeclarationModifiers),
returnType: typeToGenerateIn,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: null,
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
parameters: new[] { CodeGenerationSymbolFactory.CreateParameterSymbol(parameterSymbol, "v") },
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: ImmutableArray.Create(CodeGenerationSymbolFactory.CreateParameterSymbol(parameterSymbol, "v")),
methodKind: MethodKind.Conversion);
}
......
......@@ -23,6 +23,7 @@
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Utilities;
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.CSharp.GenerateType
{
......@@ -495,7 +496,7 @@ private IMethodSymbol GetMethodSymbolIfPresent(SemanticModel semanticModel, Expr
state.NameOrMemberAccessExpression as TypeSyntax, cancellationToken);
}
protected override IList<ITypeParameterSymbol> GetTypeParameters(
protected override ImmutableArray<ITypeParameterSymbol> GetTypeParameters(
State state,
SemanticModel semanticModel,
CancellationToken cancellationToken)
......@@ -509,7 +510,7 @@ private IMethodSymbol GetMethodSymbolIfPresent(SemanticModel semanticModel, Expr
return this.GetTypeParameters(state, semanticModel, typeArguments, cancellationToken);
}
return SpecializedCollections.EmptyList<ITypeParameterSymbol>();
return ImmutableArray<ITypeParameterSymbol>.Empty;
}
protected override bool TryGetArgumentList(ObjectCreationExpressionSyntax objectCreationExpression, out IList<ArgumentSyntax> argumentList)
......@@ -887,23 +888,23 @@ internal override async Task<Solution> TryAddUsingsOrImportToDocumentAsync(Solut
SimpleNameSyntax propertyName, ITypeSymbol propertyType)
{
return CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(),
explicitInterfaceSymbol: null,
name: propertyName.Identifier.ValueText,
type: propertyType,
returnsByRef: false,
parameters: null,
parameters: default(ImmutableArray<IParameterSymbol>),
getMethod: s_accessor,
setMethod: s_accessor,
isIndexer: false);
}
private static readonly IMethodSymbol s_accessor = CodeGenerationSymbolFactory.CreateAccessorSymbol(
attributes: null,
accessibility: Accessibility.Public,
statements: null);
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
statements: default(ImmutableArray<SyntaxNode>));
internal override bool TryGenerateProperty(
SimpleNameSyntax propertyName,
......
......@@ -62,7 +62,7 @@ private IEnumerable<CodeAction> CreateCodeActions(Document document, State state
}
var parameters = state.Parameters.Select(p => CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
refKind: p.RefKind,
isParams: p.IsParams,
type: p.Type,
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -62,7 +63,8 @@ protected override async Task<ISymbol> GenerateMemberAsync(ISymbol member, IName
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var method = (IMethodSymbol)member;
return CodeGenerationSymbolFactory.CreateMethodSymbol(attributes: new List<AttributeData>(),
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.NotApplicable,
modifiers: MemberInsertionCompletionItem.GetModifiers(item),
returnType: semanticModel.Compilation.GetSpecialType(SpecialType.System_Void),
......@@ -70,7 +72,7 @@ protected override async Task<ISymbol> GenerateMemberAsync(ISymbol member, IName
explicitInterfaceSymbol: null,
name: member.Name,
typeParameters: method.TypeParameters,
parameters: method.Parameters.Select(p => CodeGenerationSymbolFactory.CreateParameterSymbol(p.GetAttributes(), p.RefKind, p.IsParams, p.Type, p.Name)).ToList(),
parameters: method.Parameters.SelectAsArray(p => CodeGenerationSymbolFactory.CreateParameterSymbol(p.GetAttributes(), p.RefKind, p.IsParams, p.Type, p.Name)),
statements: syntaxFactory.CreateThrowNotImplementedStatementBlock(semanticModel.Compilation));
}
......
......@@ -301,14 +301,14 @@ protected async Task<Solution> AddPropertyAsync(Document document, Solution dest
var factory = document.GetLanguageService<SyntaxGenerator>();
var propertySymbol = annotation.AddAnnotationToSymbol(CodeGenerationSymbolFactory.CreatePropertySymbol(containingType: containingSymbol,
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: ComputeAccessibility(accessibility, field.Type),
modifiers: new DeclarationModifiers(isStatic: field.IsStatic, isReadOnly: field.IsReadOnly, isUnsafe: field.IsUnsafe()),
type: field.Type,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: propertyName,
parameters: SpecializedCollections.EmptyList<IParameterSymbol>(),
parameters: ImmutableArray<IParameterSymbol>.Empty,
getMethod: CreateGet(fieldName, field, factory),
setMethod: field.IsReadOnly || field.IsConst ? null : CreateSet(fieldName, field, factory)));
......@@ -344,9 +344,10 @@ protected IMethodSymbol CreateSet(string originalFieldName, IFieldSymbol field,
assigned.WithAdditionalAnnotations(Simplifier.Annotation),
factory.IdentifierName("value")));
return CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(),
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
ImmutableArray<AttributeData>.Empty,
Accessibility.NotApplicable,
new[] { body }.ToList());
ImmutableArray.Create(body));
}
protected IMethodSymbol CreateGet(string originalFieldName, IFieldSymbol field, SyntaxGenerator factory)
......@@ -360,9 +361,10 @@ protected IMethodSymbol CreateGet(string originalFieldName, IFieldSymbol field,
var body = factory.ReturnStatement(
value.WithAdditionalAnnotations(Simplifier.Annotation));
return CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(),
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
ImmutableArray<AttributeData>.Empty,
Accessibility.NotApplicable,
new[] { body }.ToList());
ImmutableArray.Create(body));
}
private static readonly char[] s_underscoreCharArray = new[] { '_' };
......
......@@ -138,7 +138,7 @@ public ExtractInterfaceResult ExtractInterfaceFromAnalyzedType(ExtractInterfaceT
cancellationToken);
var extractedInterfaceSymbol = CodeGenerationSymbolFactory.CreateNamedTypeSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: ShouldIncludeAccessibilityModifier(refactoringResult.TypeNode) ? refactoringResult.TypeToExtractFrom.DeclaredAccessibility : Accessibility.NotApplicable,
modifiers: new DeclarationModifiers(),
typeKind: TypeKind.Interface,
......@@ -323,9 +323,9 @@ private static Solution GetSolutionWithFormattedInterfaceDocument(Document unfor
return formattedSolution;
}
private IList<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembers)
private ImmutableArray<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembers)
{
var interfaceMembers = new List<ISymbol>();
var interfaceMembers = ArrayBuilder<ISymbol>.GetInstance();
foreach (var member in includedMembers)
{
......@@ -334,7 +334,7 @@ private IList<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembe
case SymbolKind.Event:
var @event = member as IEventSymbol;
interfaceMembers.Add(CodeGenerationSymbolFactory.CreateEventSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(isAbstract: true),
type: @event.Type,
......@@ -344,7 +344,7 @@ private IList<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembe
case SymbolKind.Method:
var method = member as IMethodSymbol;
interfaceMembers.Add(CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(isAbstract: true, isUnsafe: method.IsUnsafe()),
returnType: method.ReturnType,
......@@ -357,7 +357,7 @@ private IList<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembe
case SymbolKind.Property:
var property = member as IPropertySymbol;
interfaceMembers.Add(CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(isAbstract: true, isUnsafe: property.IsUnsafe()),
type: property.Type,
......@@ -375,7 +375,7 @@ private IList<ISymbol> CreateInterfaceMembers(IEnumerable<ISymbol> includedMembe
}
}
return interfaceMembers;
return interfaceMembers.ToImmutableAndFree();
}
internal virtual bool IsExtractableMember(ISymbol m)
......@@ -401,7 +401,7 @@ internal virtual bool IsExtractableMember(ISymbol m)
return false;
}
private IList<ITypeParameterSymbol> GetTypeParameters(INamedTypeSymbol type, IEnumerable<ISymbol> includedMembers)
private ImmutableArray<ITypeParameterSymbol> GetTypeParameters(INamedTypeSymbol type, IEnumerable<ISymbol> includedMembers)
{
var potentialTypeParameters = GetPotentialTypeParameters(type);
......@@ -431,7 +431,7 @@ private IList<ITypeParameterSymbol> GetTypeParameters(INamedTypeSymbol type, IEn
}
}
return potentialTypeParameters.Where(p => allReferencedTypeParameters.Contains(p)).ToList();
return potentialTypeParameters.Where(allReferencedTypeParameters.Contains).ToImmutableArray();
}
private List<ITypeParameterSymbol> GetPotentialTypeParameters(INamedTypeSymbol type)
......
......@@ -268,16 +268,16 @@ protected IEnumerable<TStatement> AppendReturnStatementIfNeeded(IEnumerable<TSta
return new HashSet<SyntaxAnnotation>(annotations.Select(t => t.Item2));
}
protected IList<ITypeParameterSymbol> CreateMethodTypeParameters(CancellationToken cancellationToken)
protected ImmutableArray<ITypeParameterSymbol> CreateMethodTypeParameters(CancellationToken cancellationToken)
{
if (this.AnalyzerResult.MethodTypeParametersInDeclaration.Count == 0)
{
return SpecializedCollections.EmptyList<ITypeParameterSymbol>();
return ImmutableArray<ITypeParameterSymbol>.Empty;
}
var set = new HashSet<ITypeParameterSymbol>(this.AnalyzerResult.MethodTypeParametersInConstraintList);
var typeParameters = new List<ITypeParameterSymbol>();
var typeParameters = ArrayBuilder<ITypeParameterSymbol>.GetInstance();
foreach (var parameter in this.AnalyzerResult.MethodTypeParametersInDeclaration)
{
if (parameter != null && set.Contains(parameter))
......@@ -291,12 +291,12 @@ protected IList<ITypeParameterSymbol> CreateMethodTypeParameters(CancellationTok
parameter.HasConstructorConstraint, parameter.HasReferenceTypeConstraint, parameter.HasValueTypeConstraint, parameter.Ordinal));
}
return typeParameters;
return typeParameters.ToImmutableAndFree();
}
protected IList<IParameterSymbol> CreateMethodParameters()
protected ImmutableArray<IParameterSymbol> CreateMethodParameters()
{
var parameters = new List<IParameterSymbol>();
var parameters = ArrayBuilder<IParameterSymbol>.GetInstance();
foreach (var parameter in this.AnalyzerResult.MethodParameters)
{
......@@ -305,14 +305,14 @@ protected IList<IParameterSymbol> CreateMethodParameters()
parameters.Add(
CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
refKind: refKind,
isParams: false,
type: type,
name: parameter.Name));
}
return parameters;
return parameters.ToImmutableAndFree();
}
private static RefKind GetRefKind(ParameterBehavior parameterBehavior)
......
......@@ -45,7 +45,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
var thisConstructorArguments = factory.CreateArguments(
_state.Parameters.Take(_state.DelegatedConstructor.Parameters.Length).ToImmutableArray());
var statements = new List<SyntaxNode>();
var statements = ArrayBuilder<SyntaxNode>.GetInstance();
for (var i = _state.DelegatedConstructor.Parameters.Length; i < _state.Parameters.Length; i++)
{
......@@ -66,12 +66,12 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
_document.Project.Solution,
_state.ContainingType,
CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(),
typeName: _state.ContainingType.Name,
parameters: _state.Parameters,
statements: statements,
statements: statements.ToImmutableAndFree(),
thisConstructorArguments: thisConstructorArguments),
new CodeGenerationOptions(
contextLocation: syntaxTree.GetLocation(_state.TextSpan),
......
......@@ -78,23 +78,26 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
Document document,
TextSpan textSpan,
INamedTypeSymbol containingType,
IList<ISymbol> selectedMembers,
ImmutableArray<ISymbol> selectedMembers,
bool hasEquals,
bool hasGetHashCode)
{
if (!hasEquals)
{
yield return new GenerateEqualsAndHashCodeAction(this, document, textSpan, containingType, selectedMembers, generateEquals: true);
yield return new GenerateEqualsAndHashCodeAction(
this, document, textSpan, containingType, selectedMembers, generateEquals: true);
}
if (!hasGetHashCode)
{
yield return new GenerateEqualsAndHashCodeAction(this, document, textSpan, containingType, selectedMembers, generateGetHashCode: true);
yield return new GenerateEqualsAndHashCodeAction(
this, document, textSpan, containingType, selectedMembers, generateGetHashCode: true);
}
if (!hasEquals && !hasGetHashCode)
{
yield return new GenerateEqualsAndHashCodeAction(this, document, textSpan, containingType, selectedMembers, generateEquals: true, generateGetHashCode: true);
yield return new GenerateEqualsAndHashCodeAction(
this, document, textSpan, containingType, selectedMembers, generateEquals: true, generateGetHashCode: true);
}
}
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
......@@ -20,7 +21,7 @@ private class GenerateEqualsAndHashCodeAction : CodeAction
private readonly GenerateEqualsAndGetHashCodeFromMembersCodeRefactoringProvider _service;
private readonly Document _document;
private readonly INamedTypeSymbol _containingType;
private readonly IList<ISymbol> _selectedMembers;
private readonly ImmutableArray<ISymbol> _selectedMembers;
private readonly TextSpan _textSpan;
public GenerateEqualsAndHashCodeAction(
......@@ -28,7 +29,7 @@ private class GenerateEqualsAndHashCodeAction : CodeAction
Document document,
TextSpan textSpan,
INamedTypeSymbol containingType,
IList<ISymbol> selectedMembers,
ImmutableArray<ISymbol> selectedMembers,
bool generateEquals = false,
bool generateGetHashCode = false)
{
......
......@@ -84,7 +84,7 @@ private static bool IsField(ISymbol symbol)
: ((IPropertySymbol)symbol).Type;
parameters.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
refKind: RefKind.None,
isParams: false,
type: type,
......
......@@ -117,11 +117,11 @@ private async Task<Document> GenerateDelegatedConstructorAsync()
var isThis = namedType.Equals(delegatedConstructor.ContainingType);
var delegatingArguments = syntaxFactory.CreateArguments(delegatedConstructor.Parameters);
var baseConstructorArguments = isThis ? null : delegatingArguments;
var thisConstructorArguments = isThis ? delegatingArguments : null;
var baseConstructorArguments = isThis ? default(ImmutableArray<SyntaxNode>) : delegatingArguments;
var thisConstructorArguments = isThis ? delegatingArguments : default(ImmutableArray<SyntaxNode>);
var constructor = CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: default(DeclarationModifiers),
typeName: _state.TypeToGenerateIn.Name,
......@@ -205,20 +205,20 @@ private async Task<Document> GenerateDelegatedConstructorAsync()
var fields = syntaxFactory.CreateFieldsForParameters(remainingParameters, parameterToNewFieldMap);
var assignStatements = syntaxFactory.CreateAssignmentStatements(remainingParameters, parameterToExistingFieldMap, parameterToNewFieldMap);
var allParameters = delegatedConstructor.Parameters.Concat(remainingParameters).ToList();
var allParameters = delegatedConstructor.Parameters.Concat(remainingParameters);
var isThis = namedType.Equals(_state.TypeToGenerateIn);
var delegatingArguments = syntaxFactory.CreateArguments(delegatedConstructor.Parameters);
var baseConstructorArguments = isThis ? null : delegatingArguments;
var thisConstructorArguments = isThis ? delegatingArguments : null;
var baseConstructorArguments = isThis ? default(ImmutableArray<SyntaxNode>) : delegatingArguments;
var thisConstructorArguments = isThis ? delegatingArguments : default(ImmutableArray<SyntaxNode>);
var constructor = CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: default(DeclarationModifiers),
typeName: _state.TypeToGenerateIn.Name,
parameters: allParameters,
statements: assignStatements.ToList(),
statements: assignStatements.ToImmutableArray(),
baseConstructorArguments: baseConstructorArguments,
thisConstructorArguments: thisConstructorArguments);
......@@ -279,11 +279,11 @@ private async Task<Document> GenerateFieldDelegatingConstructorAsync()
ImmutableArray<ParameterName> parameterNames,
out Dictionary<string, ISymbol> parameterToExistingFieldMap,
out Dictionary<string, string> parameterToNewFieldMap,
out List<IParameterSymbol> parameters)
out ImmutableArray<IParameterSymbol> parameters)
{
parameterToExistingFieldMap = new Dictionary<string, ISymbol>();
parameterToNewFieldMap = new Dictionary<string, string>();
parameters = new List<IParameterSymbol>();
var result = ArrayBuilder<IParameterSymbol>.GetInstance();
for (var i = 0; i < parameterNames.Length; i++)
{
......@@ -301,13 +301,15 @@ private async Task<Document> GenerateFieldDelegatingConstructorAsync()
}
}
parameters.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: null,
result.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: default(ImmutableArray<AttributeData>),
refKind: _service.GetRefKind(arguments[i]),
isParams: false,
type: parameterTypes[i],
name: parameterNames[i].BestNameForParameter));
}
parameters = result.ToImmutableAndFree();
}
private bool TryFindMatchingField(
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -54,15 +55,15 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
var syntaxFactory = _document.GetLanguageService<SyntaxGenerator>();
var baseConstructorArguments = constructor.Parameters.Length != 0
? syntaxFactory.CreateArguments(constructor.Parameters)
: null;
: default(ImmutableArray<SyntaxNode>);
return CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: constructor.DeclaredAccessibility,
modifiers: new DeclarationModifiers(),
typeName: _state.ClassType.Name,
parameters: constructor.Parameters,
statements: null,
statements: default(ImmutableArray<SyntaxNode>),
baseConstructorArguments: baseConstructorArguments);
}
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
......@@ -43,7 +44,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
_document.Project.Solution,
_state.TypeToGenerateIn,
CodeGenerationSymbolFactory.CreateFieldSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: default(DeclarationModifiers),
type: _state.TypeToGenerateIn,
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -216,16 +217,16 @@ internal new class State : AbstractGenerateParameterizedMemberService<TService,
ITypeSymbol expressionType)
{
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: SpecializedCollections.EmptyList<AttributeData>(),
attributes: ImmutableArray<AttributeData>.Empty,
accessibility: default(Accessibility),
modifiers: default(DeclarationModifiers),
returnType: expressionType,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: null,
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
parameters: SpecializedCollections.EmptyList<IParameterSymbol>());
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: ImmutableArray<IParameterSymbol>.Empty);
}
}
}
}
}
\ No newline at end of file
......@@ -66,7 +66,7 @@ protected AbstractInvocationInfo(SemanticDocument document, State state)
}
return CodeGenerationSymbolFactory.CreateTypeParameter(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
varianceKind: typeParameter.Variance,
name: typeParameter.Name,
constraintTypes: constraints.AsImmutable<ITypeSymbol>(),
......
......@@ -68,14 +68,14 @@ public ITypeSymbol DetermineReturnType(CancellationToken cancellationToken)
{
var accessibility = DetermineAccessibility(isAbstract);
var getMethod = CodeGenerationSymbolFactory.CreateAccessorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: accessibility,
statements: GenerateStatements(factory, isAbstract, cancellationToken));
var setMethod = includeSetter ? getMethod : null;
return CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: accessibility,
modifiers: new DeclarationModifiers(isStatic: State.IsStatic, isAbstract: isAbstract),
type: DetermineReturnType(cancellationToken),
......@@ -103,7 +103,7 @@ public ITypeSymbol DetermineReturnType(CancellationToken cancellationToken)
var returnsByRef = DetermineReturnsByRef(cancellationToken);
var method = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: DetermineAccessibility(isAbstract),
modifiers: new DeclarationModifiers(isStatic: State.IsStatic, isAbstract: isAbstract, isUnsafe: isUnsafe),
returnType: returnType,
......@@ -113,8 +113,8 @@ public ITypeSymbol DetermineReturnType(CancellationToken cancellationToken)
typeParameters: DetermineTypeParameters(cancellationToken),
parameters: parameters,
statements: GenerateStatements(factory, isAbstract, cancellationToken),
handlesExpressions: null,
returnTypeAttributes: null,
handlesExpressions: default(ImmutableArray<SyntaxNode>),
returnTypeAttributes: default(ImmutableArray<AttributeData>),
methodKind: State.MethodKind);
// Ensure no conflicts between type parameter names and parameter names.
......@@ -177,7 +177,7 @@ public ITypeSymbol DetermineReturnType(CancellationToken cancellationToken)
return result;
}
private IList<SyntaxNode> GenerateStatements(
private ImmutableArray<SyntaxNode> GenerateStatements(
SyntaxGenerator factory,
bool isAbstract,
CancellationToken cancellationToken)
......@@ -185,8 +185,8 @@ public ITypeSymbol DetermineReturnType(CancellationToken cancellationToken)
var throwStatement = CodeGenerationHelpers.GenerateThrowStatement(factory, this.Document, "System.NotImplementedException", cancellationToken);
return isAbstract || State.TypeToGenerateIn.TypeKind == TypeKind.Interface || throwStatement == null
? null
: new[] { throwStatement };
? default(ImmutableArray<SyntaxNode>)
: ImmutableArray.Create(throwStatement);
}
private ImmutableArray<IParameterSymbol> DetermineParameters(CancellationToken cancellationToken)
......@@ -200,7 +200,7 @@ private ImmutableArray<IParameterSymbol> DetermineParameters(CancellationToken c
for (var i = 0; i < modifiers.Length; i++)
{
result.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
refKind: modifiers[i],
isParams: false,
isOptional: optionality[i],
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -62,7 +63,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
_document.Project.Solution,
_state.TypeToGenerateIn,
CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: DetermineMaximalAccessibility(_state),
modifiers: new DeclarationModifiers(isStatic: _state.IsStatic, isUnsafe: generateUnsafe),
type: _state.TypeMemberType,
......@@ -85,7 +86,7 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
_document.Project.Solution,
_state.TypeToGenerateIn,
CodeGenerationSymbolFactory.CreateFieldSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: DetermineMinimalAccessibility(_state),
modifiers: _isConstant ?
new DeclarationModifiers(isConst: true, isUnsafe: generateUnsafe) :
......@@ -104,12 +105,12 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
Accessibility accessibility, CancellationToken cancellationToken)
{
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: accessibility,
statements: GenerateStatements(cancellationToken));
}
private IList<SyntaxNode> GenerateStatements(
private ImmutableArray<SyntaxNode> GenerateStatements(
CancellationToken cancellationToken)
{
var syntaxFactory = _document.Project.Solution.Workspace.Services.GetLanguageServices(_state.TypeToGenerateIn.Language).GetService<SyntaxGenerator>();
......@@ -118,8 +119,8 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
syntaxFactory, this._document, "System.NotImplementedException", cancellationToken);
return _state.TypeToGenerateIn.TypeKind != TypeKind.Interface && _returnsByRef
? new[] { throwStatement }
: null;
? ImmutableArray.Create(throwStatement)
: default(ImmutableArray<SyntaxNode>);
}
private Accessibility DetermineMaximalAccessibility(State state)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -23,7 +24,7 @@ private partial class State
public bool IsConstant { get; private set; }
public bool IsIndexer { get; private set; }
public bool IsContainedInUnsafeType { get; private set; }
public IList<IParameterSymbol> Parameters { get; private set; }
public ImmutableArray<IParameterSymbol> Parameters { get; private set; }
// Just the name of the method. i.e. "Foo" in "Foo" or "X.Foo"
public SyntaxToken IdentifierToken { get; private set; }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -79,7 +80,7 @@ private ITypeSymbol DetermineReturnType(GenerateTypeOptionsResult options)
}
}
private IList<ITypeParameterSymbol> DetermineTypeParameters(GenerateTypeOptionsResult options)
private ImmutableArray<ITypeParameterSymbol> DetermineTypeParameters(GenerateTypeOptionsResult options)
{
if (_state.DelegateMethodSymbol != null)
{
......@@ -90,19 +91,19 @@ private IList<ITypeParameterSymbol> DetermineTypeParameters(GenerateTypeOptionsR
return DetermineTypeParameters();
}
private IList<IParameterSymbol> DetermineParameters(GenerateTypeOptionsResult options)
private ImmutableArray<IParameterSymbol> DetermineParameters(GenerateTypeOptionsResult options)
{
if (_state.DelegateMethodSymbol != null)
{
return _state.DelegateMethodSymbol.Parameters;
}
return null;
return default(ImmutableArray<IParameterSymbol>);
}
private IList<ISymbol> DetermineMembers(GenerateTypeOptionsResult options = null)
private ImmutableArray<ISymbol> DetermineMembers(GenerateTypeOptionsResult options = null)
{
var members = new List<ISymbol>();
var members = ArrayBuilder<ISymbol>.GetInstance();
AddMembers(members, options);
if (_state.IsException)
......@@ -110,10 +111,10 @@ private IList<ISymbol> DetermineMembers(GenerateTypeOptionsResult options = null
AddExceptionConstructors(members);
}
return members;
return members.ToImmutableAndFree();
}
private void AddMembers(IList<ISymbol> members, GenerateTypeOptionsResult options = null)
private void AddMembers(ArrayBuilder<ISymbol> members, GenerateTypeOptionsResult options = null)
{
AddProperties(members);
if (!_service.TryGetArgumentList(_state.ObjectCreationExpressionOpt, out var argumentList))
......@@ -169,7 +170,7 @@ private void AddMembers(IList<ISymbol> members, GenerateTypeOptionsResult option
AddFieldDelegatingConstructor(argumentList, members, options);
}
private void AddProperties(IList<ISymbol> members)
private void AddProperties(ArrayBuilder<ISymbol> members)
{
var typeInference = _document.Project.LanguageServices.GetService<ITypeInferenceService>();
foreach (var property in _state.PropertiesToGenerate)
......@@ -183,7 +184,7 @@ private void AddProperties(IList<ISymbol> members)
private void AddBaseDelegatingConstructor(
IMethodSymbol methodSymbol,
IList<ISymbol> members)
ArrayBuilder<ISymbol> members)
{
// If we're generating a constructor to delegate into the no-param base constructor
// then we can just elide the constructor entirely.
......@@ -198,7 +199,7 @@ private void AddProperties(IList<ISymbol> members)
}
private void AddFieldDelegatingConstructor(
IList<TArgumentSyntax> argumentList, IList<ISymbol> members, GenerateTypeOptionsResult options = null)
IList<TArgumentSyntax> argumentList, ArrayBuilder<ISymbol> members, GenerateTypeOptionsResult options = null)
{
var factory = _document.Project.LanguageServices.GetService<SyntaxGenerator>();
var syntaxFactsService = _document.Project.LanguageServices.GetService<ISyntaxFactsService>();
......@@ -206,7 +207,7 @@ private void AddProperties(IList<ISymbol> members)
var availableTypeParameters = _service.GetAvailableTypeParameters(_state, _document.SemanticModel, _intoNamespace, _cancellationToken);
var parameterTypes = GetArgumentTypes(argumentList);
var parameterNames = _service.GenerateParameterNames(_document.SemanticModel, argumentList);
var parameters = new List<IParameterSymbol>();
var parameters = ArrayBuilder<IParameterSymbol>.GetInstance();
var parameterToExistingFieldMap = new Dictionary<string, ISymbol>();
var parameterToNewFieldMap = new Dictionary<string, string>();
......@@ -230,7 +231,7 @@ private void AddProperties(IList<ISymbol> members)
}
parameters.Add(CodeGenerationSymbolFactory.CreateParameterSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
refKind: refKind,
isParams: false,
type: parameterType,
......@@ -241,11 +242,13 @@ private void AddProperties(IList<ISymbol> members)
if (!(parameters.Count == 0 && options != null && (options.TypeKind == TypeKind.Struct || options.TypeKind == TypeKind.Structure)))
{
members.AddRange(factory.CreateFieldDelegatingConstructor(
DetermineName(), null, parameters, parameterToExistingFieldMap, parameterToNewFieldMap, _cancellationToken));
DetermineName(), null, parameters.ToImmutable(), parameterToExistingFieldMap, parameterToNewFieldMap, _cancellationToken));
}
parameters.Free();
}
private void AddExceptionConstructors(IList<ISymbol> members)
private void AddExceptionConstructors(ArrayBuilder<ISymbol> members)
{
var factory = _document.Project.LanguageServices.GetService<SyntaxGenerator>();
var exceptionType = _document.SemanticModel.Compilation.ExceptionType();
......@@ -253,17 +256,19 @@ private void AddExceptionConstructors(IList<ISymbol> members)
exceptionType.InstanceConstructors
.Where(c => c.DeclaredAccessibility == Accessibility.Public || c.DeclaredAccessibility == Accessibility.Protected)
.Select(c => CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: c.DeclaredAccessibility,
modifiers: default(DeclarationModifiers),
typeName: DetermineName(),
parameters: c.Parameters,
statements: null,
baseConstructorArguments: c.Parameters.Length == 0 ? null : factory.CreateArguments(c.Parameters)));
statements: default(ImmutableArray<SyntaxNode>),
baseConstructorArguments: c.Parameters.Length == 0
? default(ImmutableArray<SyntaxNode>)
: factory.CreateArguments(c.Parameters)));
members.AddRange(constructors);
}
private IList<AttributeData> DetermineAttributes()
private ImmutableArray<AttributeData> DetermineAttributes()
{
if (_state.IsException)
{
......@@ -271,11 +276,11 @@ private IList<AttributeData> DetermineAttributes()
if (serializableType != null)
{
var attribute = CodeGenerationSymbolFactory.CreateAttributeData(serializableType);
return new[] { attribute };
return ImmutableArray.Create(attribute);
}
}
return null;
return default(ImmutableArray<AttributeData>);
}
private Accessibility DetermineAccessibility()
......@@ -298,18 +303,18 @@ private INamedTypeSymbol DetermineBaseType()
return RemoveUnavailableTypeParameters(_state.BaseTypeOrInterfaceOpt);
}
private IList<INamedTypeSymbol> DetermineInterfaces()
private ImmutableArray<INamedTypeSymbol> DetermineInterfaces()
{
if (_state.BaseTypeOrInterfaceOpt != null && _state.BaseTypeOrInterfaceOpt.TypeKind == TypeKind.Interface)
{
var type = RemoveUnavailableTypeParameters(_state.BaseTypeOrInterfaceOpt);
if (type != null)
{
return new[] { type };
return ImmutableArray.Create(type);
}
}
return SpecializedCollections.EmptyList<INamedTypeSymbol>();
return ImmutableArray<INamedTypeSymbol>.Empty;
}
private INamedTypeSymbol RemoveUnavailableTypeParameters(INamedTypeSymbol type)
......@@ -323,10 +328,8 @@ private string DetermineName()
return GetTypeName(_state);
}
private IList<ITypeParameterSymbol> DetermineTypeParameters()
{
return _service.GetTypeParameters(_state, _document.SemanticModel, _cancellationToken);
}
private ImmutableArray<ITypeParameterSymbol> DetermineTypeParameters()
=> _service.GetTypeParameters(_state, _document.SemanticModel, _cancellationToken);
private TypeKind DetermineTypeKind()
{
......
......@@ -37,7 +37,7 @@ protected AbstractGenerateTypeService()
protected abstract bool TryGetArgumentList(TObjectCreationExpressionSyntax objectCreationExpression, out IList<TArgumentSyntax> argumentList);
protected abstract string DefaultFileExtension { get; }
protected abstract IList<ITypeParameterSymbol> GetTypeParameters(State state, SemanticModel semanticModel, CancellationToken cancellationToken);
protected abstract ImmutableArray<ITypeParameterSymbol> GetTypeParameters(State state, SemanticModel semanticModel, CancellationToken cancellationToken);
protected abstract Accessibility GetAccessibility(State state, SemanticModel semanticModel, bool intoNamespace, CancellationToken cancellationToken);
protected abstract IList<ParameterName> GenerateParameterNames(SemanticModel semanticModel, IList<TArgumentSyntax> arguments);
......@@ -178,7 +178,7 @@ protected static string GetTypeName(State state)
: state.Name;
}
protected IList<ITypeParameterSymbol> GetTypeParameters(
protected ImmutableArray<ITypeParameterSymbol> GetTypeParameters(
State state,
SemanticModel semanticModel,
IEnumerable<SyntaxNode> typeArguments,
......@@ -186,7 +186,7 @@ protected static string GetTypeName(State state)
{
var arguments = typeArguments.ToList();
var arity = arguments.Count;
var typeParameters = new List<ITypeParameterSymbol>();
var typeParameters = ArrayBuilder<ITypeParameterSymbol>.GetInstance();
// For anything that was a type parameter, just use the name (if we haven't already
// used it). Otherwise, synthesize new names for the parameters.
......@@ -228,7 +228,7 @@ protected static string GetTypeName(State state)
}
}
return typeParameters;
return typeParameters.ToImmutableAndFree();
}
protected Accessibility DetermineDefaultAccessibility(
......
......@@ -137,7 +137,7 @@ public async Task<Document> GetEditAsync(CancellationToken cancellationToken)
var getMethod = ShouldGenerateAccessor(property.GetMethod)
? CodeGenerationSymbolFactory.CreateAccessorSymbol(
property.GetMethod,
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: property.GetMethod.ComputeResultantAccessibility(_state.ClassType),
statements: throwingBody)
: null;
......@@ -145,7 +145,7 @@ public async Task<Document> GetEditAsync(CancellationToken cancellationToken)
var setMethod = ShouldGenerateAccessor(property.SetMethod)
? CodeGenerationSymbolFactory.CreateAccessorSymbol(
property.SetMethod,
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: property.SetMethod.ComputeResultantAccessibility(_state.ClassType),
statements: throwingBody)
: null;
......
......@@ -403,7 +403,7 @@ private static bool IsUnexpressibleTypeParameter(ITypeParameterSymbol typeParame
var @event = (IEventSymbol)member;
var accessor = CodeGenerationSymbolFactory.CreateAccessorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
statements: factory.CreateThrowNotImplementedStatementBlock(compilation));
......@@ -433,9 +433,9 @@ private static bool IsUnexpressibleTypeParameter(ITypeParameterSymbol typeParame
factory.MemberAccessExpression(throughExpression, memberName), factory.IdentifierName("value")));
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
statements: SpecializedCollections.SingletonList(statement));
statements: ImmutableArray.Create(statement));
}
return generateInvisibly ? accessor : null;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
......@@ -38,7 +39,9 @@ internal partial class ImplementInterfaceCodeAction
modifiers: modifiers,
explicitInterfaceSymbol: useExplicitInterfaceSymbol ? updatedMethod : null,
name: memberName,
statements: generateAbstractly ? null : new[] { CreateStatement(compilation, updatedMethod, cancellationToken) });
statements: generateAbstractly
? default(ImmutableArray<SyntaxNode>)
: ImmutableArray.Create(CreateStatement(compilation, updatedMethod, cancellationToken)));
}
private SyntaxNode CreateStatement(
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
......@@ -86,7 +87,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
setMethod,
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: accessibility,
explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.SetMethod : null,
statements: GetSetAccessorStatements(compilation, property, generateAbstractly, cancellationToken));
......@@ -112,13 +113,13 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
return CodeGenerationSymbolFactory.CreateAccessorSymbol(
getMethod,
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: accessibility,
explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.GetMethod : null,
statements: GetGetAccessorStatements(compilation, property, generateAbstractly, cancellationToken));
}
private IList<SyntaxNode> GetSetAccessorStatements(
private ImmutableArray<SyntaxNode> GetSetAccessorStatements(
Compilation compilation,
IPropertySymbol property,
bool generateAbstractly,
......@@ -126,7 +127,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
{
if (generateAbstractly)
{
return null;
return default(ImmutableArray<SyntaxNode>);
}
var factory = this.Document.GetLanguageService<SyntaxGenerator>();
......@@ -153,13 +154,13 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
expression = factory.AssignmentStatement(expression, factory.IdentifierName("value"));
return new[] { factory.ExpressionStatement(expression) };
return ImmutableArray.Create(factory.ExpressionStatement(expression));
}
return factory.CreateThrowNotImplementedStatementBlock(compilation);
}
private IList<SyntaxNode> GetGetAccessorStatements(
private ImmutableArray<SyntaxNode> GetGetAccessorStatements(
Compilation compilation,
IPropertySymbol property,
bool generateAbstractly,
......@@ -167,7 +168,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
{
if (generateAbstractly)
{
return null;
return default(ImmutableArray<SyntaxNode>);
}
var factory = this.Document.GetLanguageService<SyntaxGenerator>();
......@@ -192,7 +193,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
expression = factory.ElementAccessExpression(expression, arguments);
}
return new[] { factory.ReturnStatement(expression) };
return ImmutableArray.Create(factory.ReturnStatement(expression));
}
return factory.CreateThrowNotImplementedStatementBlock(compilation);
......
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
......@@ -168,7 +169,7 @@ private static IFieldSymbol GetBackingField(IPropertySymbol property)
n => !property.ContainingType.GetMembers(n).Any());
return CodeGenerationSymbolFactory.CreateFieldSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: field.DeclaredAccessibility,
modifiers: DeclarationModifiers.From(field),
type: field.Type,
......
......@@ -124,7 +124,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.GenerateEvent
parameters:=delegateSymbol.GetParameters())
Dim generatedEvent = CodeGenerationSymbolFactory.CreateEventSymbol(
attributes:=SpecializedCollections.EmptyList(Of AttributeData)(),
attributes:=ImmutableArray(Of AttributeData).Empty,
accessibility:=Accessibility.Public, modifiers:=Nothing,
explicitInterfaceSymbol:=Nothing,
type:=delegateType, name:=actualEventName)
......@@ -265,9 +265,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.GenerateEvent
eventType.DelegateInvokeMethod.ReturnType,
semanticModel.Compilation.GetSpecialType(SpecialType.System_Void))
Dim parameters As IList(Of IParameterSymbol) = If(eventType.DelegateInvokeMethod IsNot Nothing,
eventType.DelegateInvokeMethod.Parameters,
SpecializedCollections.EmptyList(Of IParameterSymbol)())
Dim parameters = If(eventType.DelegateInvokeMethod IsNot Nothing,
eventType.DelegateInvokeMethod.Parameters,
ImmutableArray(Of IParameterSymbol).Empty)
Dim eventHandlerType = CodeGenerationSymbolFactory.CreateDelegateTypeSymbol(
eventType.GetAttributes(), eventType.DeclaredAccessibility,
......
......@@ -10,6 +10,7 @@ Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Microsoft.CodeAnalysis.Simplification
Imports Microsoft.CodeAnalysis.Options
Imports System.Collections.Immutable
Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod
Partial Friend Class VisualBasicMethodExtractor
......@@ -62,7 +63,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod
Dim statements = result.Data
Dim methodSymbol = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes:=SpecializedCollections.EmptyList(Of AttributeData)(),
attributes:=ImmutableArray(Of AttributeData).Empty,
accessibility:=Accessibility.Private,
modifiers:=CreateMethodModifiers(),
returnType:=Me.AnalyzerResult.ReturnType,
......@@ -71,7 +72,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod
name:=_methodName.ToString(),
typeParameters:=CreateMethodTypeParameters(cancellationToken),
parameters:=CreateMethodParameters(),
statements:=statements.Cast(Of SyntaxNode).ToList())
statements:=statements.Cast(Of SyntaxNode).ToImmutableArray())
Return result.With(
Me.MethodDefinitionAnnotation.AddAnnotationToSymbol(
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Composition
Imports System.Threading
Imports Microsoft.CodeAnalysis
......@@ -137,15 +138,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateMember.GenerateMethod
typeToGenerateIn = typeToGenerateIn.ConstructUnboundGenericType.ConstructedFrom
End If
Return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes:=SpecializedCollections.EmptyList(Of AttributeData),
attributes:=ImmutableArray(Of AttributeData).Empty,
accessibility:=Nothing,
modifiers:=Nothing,
returnType:=typeToGenerateIn,
returnsByRef:=False,
explicitInterfaceSymbol:=Nothing,
name:=Nothing,
typeParameters:=SpecializedCollections.EmptyList(Of ITypeParameterSymbol),
parameters:={CodeGenerationSymbolFactory.CreateParameterSymbol(parameterSymbol, "v")},
typeParameters:=ImmutableArray(Of ITypeParameterSymbol).Empty,
parameters:=ImmutableArray.Create(CodeGenerationSymbolFactory.CreateParameterSymbol(parameterSymbol, "v")),
statements:=Nothing,
handlesExpressions:=Nothing,
returnTypeAttributes:=Nothing,
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Composition
Imports System.Threading
Imports Microsoft.CodeAnalysis.CodeGeneration
......@@ -368,7 +369,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateType
Protected Overloads Overrides Function GetTypeParameters(state As State,
semanticModel As SemanticModel,
cancellationToken As CancellationToken) As IList(Of ITypeParameterSymbol)
cancellationToken As CancellationToken) As ImmutableArray(Of ITypeParameterSymbol)
If TypeOf state.SimpleName Is GenericNameSyntax Then
Dim genericName = DirectCast(state.SimpleName, GenericNameSyntax)
Dim typeArguments = If(state.SimpleName.Arity = genericName.TypeArgumentList.Arguments.Count,
......@@ -377,7 +378,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateType
Return Me.GetTypeParameters(state, semanticModel, typeArguments, cancellationToken)
End If
Return SpecializedCollections.EmptyList(Of ITypeParameterSymbol)()
Return ImmutableArray(Of ITypeParameterSymbol).Empty
End Function
Protected Overrides Function IsInVariableTypeContext(expression As Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax) As Boolean
......@@ -691,7 +692,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateType
Private Function GenerateProperty(propertyName As SimpleNameSyntax, typeSymbol As ITypeSymbol) As IPropertySymbol
Return CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes:=SpecializedCollections.EmptyList(Of AttributeData),
attributes:=ImmutableArray(Of AttributeData).Empty,
accessibility:=Accessibility.Public,
modifiers:=New DeclarationModifiers(),
explicitInterfaceSymbol:=Nothing,
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -172,27 +173,24 @@ public static string GetEventHandlerMemberId(Document document, string className
throw new InvalidOperationException(ServicesVSResources.Event_type_is_invalid);
}
var handlesExpressions = useHandlesClause ?
new[]
{
syntaxFactory.MemberAccessExpression(
var handlesExpressions = useHandlesClause
? ImmutableArray.Create(syntaxFactory.MemberAccessExpression(
objectName != null ? syntaxFactory.IdentifierName(objectName) : syntaxFactory.ThisExpression(),
syntaxFactory.IdentifierName(nameOfEvent))
}
: null;
syntaxFactory.IdentifierName(nameOfEvent)))
: default(ImmutableArray<SyntaxNode>);
var invokeMethod = ((INamedTypeSymbol)eventType).DelegateInvokeMethod;
var newMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Protected,
modifiers: new DeclarationModifiers(),
returnType: targetDocument.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult_Venus(cancellationToken).GetSpecialType(SpecialType.System_Void),
returnsByRef: false,
explicitInterfaceSymbol: null,
name: eventHandlerName,
typeParameters: null,
parameters: invokeMethod.Parameters.ToArray(),
statements: null,
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: invokeMethod.Parameters,
statements: default(ImmutableArray<SyntaxNode>),
handlesExpressions: handlesExpressions);
var annotation = new SyntaxAnnotation();
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Editing;
......@@ -27,11 +28,11 @@ protected SyntaxNode CreateConstructorDeclaration(SyntaxNode containerNode, stri
var destination = CodeModelService.GetDestination(containerNode);
var newMethodSymbol = CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Method, destination),
modifiers: new DeclarationModifiers(),
typeName: typeName,
parameters: null);
parameters: default(ImmutableArray<IParameterSymbol>));
return CodeGenerationService.CreateMethodDeclaration(
newMethodSymbol, destination,
......@@ -43,7 +44,7 @@ protected SyntaxNode CreateDestructorDeclaration(SyntaxNode containerNode, strin
var destination = CodeModelService.GetDestination(containerNode);
var newMethodSymbol = CodeGenerationSymbolFactory.CreateDestructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
typeName: typeName);
return CodeGenerationService.CreateMethodDeclaration(
......@@ -55,7 +56,7 @@ protected SyntaxNode CreateDelegateTypeDeclaration(SyntaxNode containerNode, str
var destination = CodeModelService.GetDestination(containerNode);
var newTypeSymbol = CodeGenerationSymbolFactory.CreateDelegateTypeSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.NamedType, destination),
modifiers: new DeclarationModifiers(),
returnType: returnType,
......@@ -77,30 +78,30 @@ protected SyntaxNode CreateEventDeclaration(SyntaxNode containerNode, string nam
if (createPropertyStyleEvent)
{
addMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
modifiers: new DeclarationModifiers(),
returnType: null,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: "add_" + name,
typeParameters: null,
parameters: null);
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>));
removeMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
modifiers: new DeclarationModifiers(),
returnType: null,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: "remove_" + name,
typeParameters: null,
parameters: null);
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>));
}
var newEventSymbol = CodeGenerationSymbolFactory.CreateEventSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Event, destination),
modifiers: new DeclarationModifiers(),
type: type,
......@@ -119,7 +120,7 @@ protected SyntaxNode CreateFieldDeclaration(SyntaxNode containerNode, string nam
var destination = CodeModelService.GetDestination(containerNode);
var newFieldSymbol = CodeGenerationSymbolFactory.CreateFieldSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Field, destination),
modifiers: new DeclarationModifiers(isWithEvents: CodeModelService.GetWithEvents(access)),
type: type,
......@@ -135,15 +136,15 @@ protected SyntaxNode CreateMethodDeclaration(SyntaxNode containerNode, string na
var destination = CodeModelService.GetDestination(containerNode);
var newMethodSymbol = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Method, destination),
modifiers: new DeclarationModifiers(),
returnType: returnType,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: name,
typeParameters: null,
parameters: null);
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>));
return CodeGenerationService.CreateMethodDeclaration(
newMethodSymbol, destination,
......@@ -158,42 +159,42 @@ protected SyntaxNode CreatePropertyDeclaration(SyntaxNode containerNode, string
if (generateGetter)
{
getMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
modifiers: new DeclarationModifiers(),
returnType: null,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: "get_" + name,
typeParameters: null,
parameters: null,
statements: new[] { CodeModelService.CreateReturnDefaultValueStatement(type) });
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>),
statements: ImmutableArray.Create(CodeModelService.CreateReturnDefaultValueStatement(type)));
}
IMethodSymbol setMethod = null;
if (generateSetter)
{
setMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.NotApplicable,
modifiers: new DeclarationModifiers(),
returnType: null,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: "set_" + name,
typeParameters: null,
parameters: null);
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>));
}
var newPropertySymbol = CodeGenerationSymbolFactory.CreatePropertySymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Field, destination),
modifiers: new DeclarationModifiers(),
type: type,
returnsByRef: false,
explicitInterfaceSymbol: null,
name: name,
parameters: null,
parameters: default(ImmutableArray<IParameterSymbol>),
getMethod: getMethod,
setMethod: setMethod);
......@@ -220,21 +221,21 @@ protected SyntaxNode CreateNamespaceDeclaration(SyntaxNode containerNode, string
string name,
EnvDTE.vsCMAccess access,
INamedTypeSymbol baseType = null,
IList<INamedTypeSymbol> implementedInterfaces = null)
ImmutableArray<INamedTypeSymbol> implementedInterfaces = default(ImmutableArray<INamedTypeSymbol>))
{
var destination = CodeModelService.GetDestination(containerNode);
var newTypeSymbol = CodeGenerationSymbolFactory.CreateNamedTypeSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: CodeModelService.GetAccessibility(access, SymbolKind.NamedType, destination),
modifiers: new DeclarationModifiers(),
typeKind: typeKind,
name: name,
typeParameters: null,
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
baseType: baseType,
interfaces: implementedInterfaces,
specialType: SpecialType.None,
members: null);
members: default(ImmutableArray<ISymbol>));
return CodeGenerationService.CreateNamedTypeDeclaration(
newTypeSymbol, destination,
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
......@@ -209,7 +210,7 @@ internal EnvDTE.CodeClass AddClass(SyntaxNode containerNode, string name, object
CodeModelService.GetUnescapedName(name),
access,
baseType: baseTypeSymbol,
implementedInterfaces: implementedInterfaceSymbols);
implementedInterfaces: implementedInterfaceSymbols.ToImmutableArray());
var insertionIndex = CodeModelService.PositionVariantToMemberInsertionIndex(position, containerNode, fileCodeModel: this);
......@@ -342,7 +343,7 @@ internal EnvDTE.CodeInterface AddInterface(SyntaxNode containerNode, string name
TypeKind.Interface,
CodeModelService.GetUnescapedName(name),
access,
implementedInterfaces: implementedInterfaceSymbols);
implementedInterfaces: implementedInterfaceSymbols.ToImmutableArray());
var insertionIndex = CodeModelService.PositionVariantToMemberInsertionIndex(position, containerNode, fileCodeModel: this);
......@@ -405,7 +406,7 @@ internal EnvDTE.CodeStruct AddStruct(SyntaxNode containerNode, string name, obje
TypeKind.Struct,
CodeModelService.GetUnescapedName(name),
access,
implementedInterfaces: implementedInterfaceSymbols);
implementedInterfaces: implementedInterfaceSymbols.ToImmutableArray());
var insertionIndex = CodeModelService.PositionVariantToMemberInsertionIndex(position, containerNode, fileCodeModel: this);
......
......@@ -92,7 +92,9 @@ private static MemberDeclarationSyntax LastConstructorOrField(SyntaxList<MemberD
private static ConstructorInitializerSyntax GenerateConstructorInitializer(
IMethodSymbol constructor)
{
var arguments = CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) ?? CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor);
var thisArguments = CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor);
var arguments = !thisArguments.IsDefault ? thisArguments : CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor);
var kind = CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) != null
? SyntaxKind.ThisConstructorInitializer
: SyntaxKind.BaseConstructorInitializer;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CodeGeneration
......@@ -25,8 +24,8 @@ public static CodeGenerationAbstractNamedTypeSymbol ToCodeGenerationSymbol(this
namedType.BaseType,
namedType.Interfaces,
namedType.SpecialType,
namedType.GetMembers().Where(s => !(s is INamedTypeSymbol)).ToList(),
namedType.GetTypeMembers().Select(t => t.ToCodeGenerationSymbol()).ToList(),
namedType.GetMembers().WhereAsArray(s => !(s is INamedTypeSymbol)),
namedType.GetTypeMembers().SelectAsArray(t => t.ToCodeGenerationSymbol()),
namedType.EnumUnderlyingType);
}
}
......
......@@ -19,14 +19,14 @@ public virtual ImmutableArray<AttributeData> GetReturnTypeAttributes()
protected CodeGenerationAbstractMethodSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
string name,
IList<AttributeData> returnTypeAttributes)
ImmutableArray<AttributeData> returnTypeAttributes)
: base(containingType, attributes, declaredAccessibility, modifiers, name)
{
_returnTypeAttributes = returnTypeAttributes.AsImmutableOrEmpty();
_returnTypeAttributes = returnTypeAttributes.NullToEmpty();
}
public abstract int Arity { get; }
......
......@@ -12,16 +12,16 @@ internal abstract class CodeGenerationAbstractNamedTypeSymbol : CodeGenerationTy
public ImmutableArray<IFieldSymbol> TupleElements { get; protected set; }
internal readonly IList<CodeGenerationAbstractNamedTypeSymbol> TypeMembers;
internal readonly ImmutableArray<CodeGenerationAbstractNamedTypeSymbol> TypeMembers;
protected CodeGenerationAbstractNamedTypeSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
string name,
SpecialType specialType,
IList<CodeGenerationAbstractNamedTypeSymbol> typeMembers)
ImmutableArray<CodeGenerationAbstractNamedTypeSymbol> typeMembers)
: base(containingType, attributes, declaredAccessibility, modifiers, name, specialType)
{
this.TypeMembers = typeMembers;
......@@ -51,7 +51,8 @@ public INamedTypeSymbol Construct(params ITypeSymbol[] typeArguments)
return this;
}
return new CodeGenerationConstructedNamedTypeSymbol(this, typeArguments, this.TypeMembers);
return new CodeGenerationConstructedNamedTypeSymbol(
this, typeArguments.ToImmutableArray(), this.TypeMembers);
}
public abstract int Arity { get; }
......
......@@ -37,7 +37,7 @@ public ImmutableArray<int> LowerBounds
}
public CodeGenerationArrayTypeSymbol(ITypeSymbol elementType, int rank)
: base(null, null, Accessibility.NotApplicable, default(DeclarationModifiers), string.Empty, SpecialType.None)
: base(null, default(ImmutableArray<AttributeData>), Accessibility.NotApplicable, default(DeclarationModifiers), string.Empty, SpecialType.None)
{
this.ElementType = elementType;
this.Rank = rank;
......
......@@ -9,12 +9,12 @@ namespace Microsoft.CodeAnalysis.CodeGeneration
internal class CodeGenerationConstructedNamedTypeSymbol : CodeGenerationAbstractNamedTypeSymbol
{
private readonly CodeGenerationAbstractNamedTypeSymbol _constructedFrom;
private readonly IList<ITypeSymbol> _typeArguments;
private readonly ImmutableArray<ITypeSymbol> _typeArguments;
public CodeGenerationConstructedNamedTypeSymbol(
CodeGenerationAbstractNamedTypeSymbol constructedFrom,
IList<ITypeSymbol> typeArguments,
IList<CodeGenerationAbstractNamedTypeSymbol> typeMembers)
ImmutableArray<ITypeSymbol> typeArguments,
ImmutableArray<CodeGenerationAbstractNamedTypeSymbol> typeMembers)
: base(constructedFrom.ContainingType, constructedFrom.GetAttributes(),
constructedFrom.DeclaredAccessibility, constructedFrom.Modifiers,
constructedFrom.Name, constructedFrom.SpecialType, typeMembers)
......@@ -24,13 +24,7 @@ internal class CodeGenerationConstructedNamedTypeSymbol : CodeGenerationAbstract
_typeArguments = typeArguments;
}
public override ImmutableArray<ITypeSymbol> TypeArguments
{
get
{
return ImmutableArray.CreateRange(_typeArguments);
}
}
public override ImmutableArray<ITypeSymbol> TypeArguments => _typeArguments;
public override int Arity => _constructedFrom.Arity;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
namespace Microsoft.CodeAnalysis.CodeGeneration
......@@ -11,15 +11,15 @@ internal class CodeGenerationConstructorInfo
new ConditionalWeakTable<IMethodSymbol, CodeGenerationConstructorInfo>();
private readonly string _typeName;
private readonly IList<SyntaxNode> _baseConstructorArguments;
private readonly IList<SyntaxNode> _thisConstructorArguments;
private readonly IList<SyntaxNode> _statements;
private readonly ImmutableArray<SyntaxNode> _baseConstructorArguments;
private readonly ImmutableArray<SyntaxNode> _thisConstructorArguments;
private readonly ImmutableArray<SyntaxNode> _statements;
private CodeGenerationConstructorInfo(
string typeName,
IList<SyntaxNode> statements,
IList<SyntaxNode> baseConstructorArguments,
IList<SyntaxNode> thisConstructorArguments)
ImmutableArray<SyntaxNode> statements,
ImmutableArray<SyntaxNode> baseConstructorArguments,
ImmutableArray<SyntaxNode> thisConstructorArguments)
{
_typeName = typeName;
_statements = statements;
......@@ -30,9 +30,9 @@ internal class CodeGenerationConstructorInfo
public static void Attach(
IMethodSymbol constructor,
string typeName,
IList<SyntaxNode> statements,
IList<SyntaxNode> baseConstructorArguments,
IList<SyntaxNode> thisConstructorArguments)
ImmutableArray<SyntaxNode> statements,
ImmutableArray<SyntaxNode> baseConstructorArguments,
ImmutableArray<SyntaxNode> thisConstructorArguments)
{
var info = new CodeGenerationConstructorInfo(typeName, statements, baseConstructorArguments, thisConstructorArguments);
s_constructorToInfoMap.Add(constructor, info);
......@@ -44,44 +44,28 @@ private static CodeGenerationConstructorInfo GetInfo(IMethodSymbol method)
return info;
}
public static IList<SyntaxNode> GetThisConstructorArgumentsOpt(IMethodSymbol constructor)
{
return GetThisConstructorArgumentsOpt(GetInfo(constructor));
}
public static ImmutableArray<SyntaxNode> GetThisConstructorArgumentsOpt(IMethodSymbol constructor)
=> GetThisConstructorArgumentsOpt(GetInfo(constructor));
public static IList<SyntaxNode> GetBaseConstructorArgumentsOpt(IMethodSymbol constructor)
{
return GetBaseConstructorArgumentsOpt(GetInfo(constructor));
}
public static ImmutableArray<SyntaxNode> GetBaseConstructorArgumentsOpt(IMethodSymbol constructor)
=> GetBaseConstructorArgumentsOpt(GetInfo(constructor));
public static IList<SyntaxNode> GetStatements(IMethodSymbol constructor)
{
return GetStatements(GetInfo(constructor));
}
public static ImmutableArray<SyntaxNode> GetStatements(IMethodSymbol constructor)
=> GetStatements(GetInfo(constructor));
public static string GetTypeName(IMethodSymbol constructor)
{
return GetTypeName(GetInfo(constructor), constructor);
}
=> GetTypeName(GetInfo(constructor), constructor);
private static IList<SyntaxNode> GetThisConstructorArgumentsOpt(CodeGenerationConstructorInfo info)
{
return info == null ? null : info._thisConstructorArguments;
}
private static ImmutableArray<SyntaxNode> GetThisConstructorArgumentsOpt(CodeGenerationConstructorInfo info)
=> info?._thisConstructorArguments ?? default(ImmutableArray<SyntaxNode>);
private static IList<SyntaxNode> GetBaseConstructorArgumentsOpt(CodeGenerationConstructorInfo info)
{
return info == null ? null : info._baseConstructorArguments;
}
private static ImmutableArray<SyntaxNode> GetBaseConstructorArgumentsOpt(CodeGenerationConstructorInfo info)
=> info?._baseConstructorArguments ?? default(ImmutableArray<SyntaxNode>);
private static IList<SyntaxNode> GetStatements(CodeGenerationConstructorInfo info)
{
return info == null ? null : info._statements;
}
private static ImmutableArray<SyntaxNode> GetStatements(CodeGenerationConstructorInfo info)
=> info?._statements ?? default(ImmutableArray<SyntaxNode>);
private static string GetTypeName(CodeGenerationConstructorInfo info, IMethodSymbol constructor)
{
return info == null ? constructor.ContainingType.Name : info._typeName;
}
=> info == null ? constructor.ContainingType.Name : info._typeName;
}
}
}
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Editing;
using Roslyn.Utilities;
......@@ -10,10 +11,10 @@ internal class CodeGenerationConstructorSymbol : CodeGenerationMethodSymbol
{
public CodeGenerationConstructorSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility accessibility,
DeclarationModifiers modifiers,
IList<IParameterSymbol> parameters) :
ImmutableArray<IParameterSymbol> parameters) :
base(containingType,
attributes,
accessibility,
......@@ -22,9 +23,9 @@ internal class CodeGenerationConstructorSymbol : CodeGenerationMethodSymbol
returnsByRef: false,
explicitInterfaceSymbolOpt: null,
name: string.Empty,
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: parameters,
returnTypeAttributes: SpecializedCollections.EmptyList<AttributeData>())
returnTypeAttributes: ImmutableArray<AttributeData>.Empty)
{
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Editing;
using Roslyn.Utilities;
......@@ -10,13 +11,13 @@ internal class CodeGenerationConversionSymbol : CodeGenerationMethodSymbol
{
public CodeGenerationConversionSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
ITypeSymbol toType,
IParameterSymbol fromType,
bool isImplicit,
IList<AttributeData> toTypeAttributes) :
ImmutableArray<AttributeData> toTypeAttributes) :
base(containingType,
attributes,
declaredAccessibility,
......@@ -27,8 +28,8 @@ internal class CodeGenerationConversionSymbol : CodeGenerationMethodSymbol
name: isImplicit ?
WellKnownMemberNames.ImplicitConversionName :
WellKnownMemberNames.ExplicitConversionName,
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
parameters: new List<IParameterSymbol>(SpecializedCollections.SingletonEnumerable(fromType)),
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: ImmutableArray.Create(fromType),
returnTypeAttributes: toTypeAttributes)
{
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
namespace Microsoft.CodeAnalysis.CodeGeneration
......@@ -11,11 +11,11 @@ internal class CodeGenerationDestructorInfo
new ConditionalWeakTable<IMethodSymbol, CodeGenerationDestructorInfo>();
private readonly string _typeName;
private readonly IList<SyntaxNode> _statements;
private readonly ImmutableArray<SyntaxNode> _statements;
private CodeGenerationDestructorInfo(
string typeName,
IList<SyntaxNode> statements)
ImmutableArray<SyntaxNode> statements)
{
_typeName = typeName;
_statements = statements;
......@@ -24,7 +24,7 @@ internal class CodeGenerationDestructorInfo
public static void Attach(
IMethodSymbol destructor,
string typeName,
IList<SyntaxNode> statements)
ImmutableArray<SyntaxNode> statements)
{
var info = new CodeGenerationDestructorInfo(typeName, statements);
s_destructorToInfoMap.Add(destructor, info);
......@@ -36,24 +36,16 @@ private static CodeGenerationDestructorInfo GetInfo(IMethodSymbol method)
return info;
}
public static IList<SyntaxNode> GetStatements(IMethodSymbol destructor)
{
return GetStatements(GetInfo(destructor));
}
public static ImmutableArray<SyntaxNode> GetStatements(IMethodSymbol destructor)
=> GetStatements(GetInfo(destructor));
public static string GetTypeName(IMethodSymbol destructor)
{
return GetTypeName(GetInfo(destructor), destructor);
}
=> GetTypeName(GetInfo(destructor), destructor);
private static IList<SyntaxNode> GetStatements(CodeGenerationDestructorInfo info)
{
return info == null ? null : info._statements;
}
private static ImmutableArray<SyntaxNode> GetStatements(CodeGenerationDestructorInfo info)
=> info?._statements ?? default(ImmutableArray<SyntaxNode>);
private static string GetTypeName(CodeGenerationDestructorInfo info, IMethodSymbol constructor)
{
return info == null ? constructor.ContainingType.Name : info._typeName;
}
=> info == null ? constructor.ContainingType.Name : info._typeName;
}
}
}
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Editing;
using Roslyn.Utilities;
......@@ -10,8 +11,8 @@ internal class CodeGenerationDestructorSymbol : CodeGenerationMethodSymbol
{
public CodeGenerationDestructorSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes) :
base(containingType,
ImmutableArray<AttributeData> attributes)
: base(containingType,
attributes,
Accessibility.NotApplicable,
default(DeclarationModifiers),
......@@ -19,9 +20,9 @@ internal class CodeGenerationDestructorSymbol : CodeGenerationMethodSymbol
returnsByRef: false,
explicitInterfaceSymbolOpt: null,
name: string.Empty,
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
parameters: SpecializedCollections.EmptyList<IParameterSymbol>(),
returnTypeAttributes: SpecializedCollections.EmptyList<AttributeData>())
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: ImmutableArray<IParameterSymbol>.Empty,
returnTypeAttributes: ImmutableArray<AttributeData>.Empty)
{
}
......
......@@ -19,7 +19,7 @@ internal class CodeGenerationEventSymbol : CodeGenerationSymbol, IEventSymbol
public CodeGenerationEventSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
ITypeSymbol type,
......
......@@ -14,7 +14,7 @@ internal class CodeGenerationFieldSymbol : CodeGenerationSymbol, IFieldSymbol
public CodeGenerationFieldSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility accessibility,
DeclarationModifiers modifiers,
ITypeSymbol type,
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeGeneration
{
......@@ -15,23 +14,23 @@ internal class CodeGenerationMethodInfo
private readonly bool _isUnsafe;
private readonly bool _isPartial;
private readonly bool _isAsync;
private readonly IList<SyntaxNode> _statements;
private readonly IList<SyntaxNode> _handlesExpressions;
private readonly ImmutableArray<SyntaxNode> _statements;
private readonly ImmutableArray<SyntaxNode> _handlesExpressions;
private CodeGenerationMethodInfo(
bool isNew,
bool isUnsafe,
bool isPartial,
bool isAsync,
IList<SyntaxNode> statements,
IList<SyntaxNode> handlesExpressions)
ImmutableArray<SyntaxNode> statements,
ImmutableArray<SyntaxNode> handlesExpressions)
{
_isNew = isNew;
_isUnsafe = isUnsafe;
_isPartial = isPartial;
_isAsync = isAsync;
_statements = statements ?? SpecializedCollections.EmptyList<SyntaxNode>();
_handlesExpressions = handlesExpressions ?? SpecializedCollections.EmptyList<SyntaxNode>();
_statements = statements.NullToEmpty();
_handlesExpressions = handlesExpressions.NullToEmpty();
}
public static void Attach(
......@@ -40,8 +39,8 @@ internal class CodeGenerationMethodInfo
bool isUnsafe,
bool isPartial,
bool isAsync,
IList<SyntaxNode> statements,
IList<SyntaxNode> handlesExpressions)
ImmutableArray<SyntaxNode> statements,
ImmutableArray<SyntaxNode> handlesExpressions)
{
var info = new CodeGenerationMethodInfo(isNew, isUnsafe, isPartial, isAsync, statements, handlesExpressions);
s_methodToInfoMap.Add(method, info);
......@@ -53,68 +52,40 @@ private static CodeGenerationMethodInfo GetInfo(IMethodSymbol method)
return info;
}
public static IList<SyntaxNode> GetStatements(IMethodSymbol method)
{
return GetStatements(GetInfo(method));
}
public static ImmutableArray<SyntaxNode> GetStatements(IMethodSymbol method)
=> GetStatements(GetInfo(method));
public static IList<SyntaxNode> GetHandlesExpressions(IMethodSymbol method)
{
return GetHandlesExpressions(GetInfo(method));
}
public static ImmutableArray<SyntaxNode> GetHandlesExpressions(IMethodSymbol method)
=> GetHandlesExpressions(GetInfo(method));
public static bool GetIsNew(IMethodSymbol method)
{
return GetIsNew(GetInfo(method));
}
=> GetIsNew(GetInfo(method));
public static bool GetIsUnsafe(IMethodSymbol method)
{
return GetIsUnsafe(GetInfo(method));
}
=> GetIsUnsafe(GetInfo(method));
public static bool GetIsPartial(IMethodSymbol method)
{
return GetIsPartial(GetInfo(method));
}
=> GetIsPartial(GetInfo(method));
public static bool GetIsAsync(IMethodSymbol method)
{
return GetIsAsync(GetInfo(method));
}
=> GetIsAsync(GetInfo(method));
private static IList<SyntaxNode> GetStatements(CodeGenerationMethodInfo info)
{
return info == null
? SpecializedCollections.EmptyList<SyntaxNode>()
: info._statements;
}
private static ImmutableArray<SyntaxNode> GetStatements(CodeGenerationMethodInfo info)
=> info?._statements ?? ImmutableArray<SyntaxNode>.Empty;
private static IList<SyntaxNode> GetHandlesExpressions(CodeGenerationMethodInfo info)
{
return info == null
? SpecializedCollections.EmptyList<SyntaxNode>()
: info._handlesExpressions;
}
private static ImmutableArray<SyntaxNode> GetHandlesExpressions(CodeGenerationMethodInfo info)
=> info?._handlesExpressions ?? ImmutableArray<SyntaxNode>.Empty;
private static bool GetIsNew(CodeGenerationMethodInfo info)
{
return info != null && info._isNew;
}
=> info != null && info._isNew;
private static bool GetIsUnsafe(CodeGenerationMethodInfo info)
{
return info != null && info._isUnsafe;
}
=> info != null && info._isUnsafe;
private static bool GetIsPartial(CodeGenerationMethodInfo info)
{
return info != null && info._isPartial;
}
=> info != null && info._isPartial;
private static bool GetIsAsync(CodeGenerationMethodInfo info)
{
return info != null && info._isAsync;
}
=> info != null && info._isAsync;
}
}
}
\ No newline at end of file
......@@ -19,23 +19,23 @@ internal partial class CodeGenerationMethodSymbol : CodeGenerationAbstractMethod
public CodeGenerationMethodSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
ITypeSymbol returnType,
bool returnsByRef,
IMethodSymbol explicitInterfaceSymbolOpt,
string name,
IList<ITypeParameterSymbol> typeParameters,
IList<IParameterSymbol> parameters,
IList<AttributeData> returnTypeAttributes,
ImmutableArray<ITypeParameterSymbol> typeParameters,
ImmutableArray<IParameterSymbol> parameters,
ImmutableArray<AttributeData> returnTypeAttributes,
MethodKind methodKind = MethodKind.Ordinary)
: base(containingType, attributes, declaredAccessibility, modifiers, name, returnTypeAttributes)
{
this.ReturnType = returnType;
this.ReturnsByRef = returnsByRef;
this.TypeParameters = typeParameters.AsImmutableOrEmpty();
this.Parameters = parameters.AsImmutableOrEmpty();
this.TypeParameters = typeParameters.NullToEmpty();
this.Parameters = parameters.NullToEmpty();
this.MethodKind = methodKind;
this.ExplicitInterfaceImplementations = explicitInterfaceSymbolOpt == null
......
......@@ -12,33 +12,33 @@ namespace Microsoft.CodeAnalysis.CodeGeneration
internal class CodeGenerationNamedTypeSymbol : CodeGenerationAbstractNamedTypeSymbol
{
private readonly TypeKind _typeKind;
private readonly IList<ITypeParameterSymbol> _typeParameters;
private readonly ImmutableArray<ITypeParameterSymbol> _typeParameters;
private readonly INamedTypeSymbol _baseType;
private readonly IList<INamedTypeSymbol> _interfaces;
private readonly IList<ISymbol> _members;
private readonly ImmutableArray<INamedTypeSymbol> _interfaces;
private readonly ImmutableArray<ISymbol> _members;
private readonly INamedTypeSymbol _enumUnderlyingType;
public CodeGenerationNamedTypeSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
TypeKind typeKind,
string name,
IList<ITypeParameterSymbol> typeParameters,
ImmutableArray<ITypeParameterSymbol> typeParameters,
INamedTypeSymbol baseType,
IList<INamedTypeSymbol> interfaces,
ImmutableArray<INamedTypeSymbol> interfaces,
SpecialType specialType,
IList<ISymbol> members,
IList<CodeGenerationAbstractNamedTypeSymbol> typeMembers,
ImmutableArray<ISymbol> members,
ImmutableArray<CodeGenerationAbstractNamedTypeSymbol> typeMembers,
INamedTypeSymbol enumUnderlyingType)
: base(containingType, attributes, declaredAccessibility, modifiers, name, specialType, typeMembers)
{
_typeKind = typeKind;
_typeParameters = typeParameters ?? SpecializedCollections.EmptyList<ITypeParameterSymbol>();
_typeParameters = typeParameters.NullToEmpty();
_baseType = baseType;
_interfaces = interfaces ?? SpecializedCollections.EmptyList<INamedTypeSymbol>();
_members = members ?? SpecializedCollections.EmptyList<ISymbol>();
_interfaces = interfaces.NullToEmpty();
_members = members.NullToEmpty();
_enumUnderlyingType = enumUnderlyingType;
this.OriginalDefinition = this;
......
......@@ -10,7 +10,7 @@ internal abstract class CodeGenerationNamespaceOrTypeSymbol : CodeGenerationSymb
{
protected CodeGenerationNamespaceOrTypeSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
string name)
......
......@@ -13,7 +13,7 @@ internal class CodeGenerationNamespaceSymbol : CodeGenerationNamespaceOrTypeSymb
private readonly IList<INamespaceOrTypeSymbol> _members;
public CodeGenerationNamespaceSymbol(string name, IList<INamespaceOrTypeSymbol> members)
: base(null, null, Accessibility.NotApplicable, default(DeclarationModifiers), name)
: base(null, default(ImmutableArray<AttributeData>), Accessibility.NotApplicable, default(DeclarationModifiers), name)
{
_members = members ?? SpecializedCollections.EmptyList<INamespaceOrTypeSymbol>();
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Editing;
using Roslyn.Utilities;
......@@ -10,14 +11,14 @@ internal class CodeGenerationOperatorSymbol : CodeGenerationMethodSymbol
{
public CodeGenerationOperatorSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility accessibility,
DeclarationModifiers modifiers,
ITypeSymbol returnType,
CodeGenerationOperatorKind operatorKind,
IList<IParameterSymbol> parameters,
IList<AttributeData> returnTypeAttributes) :
base(containingType,
ImmutableArray<IParameterSymbol> parameters,
ImmutableArray<AttributeData> returnTypeAttributes)
: base(containingType,
attributes,
accessibility,
modifiers,
......@@ -25,7 +26,7 @@ internal class CodeGenerationOperatorSymbol : CodeGenerationMethodSymbol
returnsByRef: false,
explicitInterfaceSymbolOpt: null,
name: GetMetadataName(operatorKind),
typeParameters: SpecializedCollections.EmptyList<ITypeParameterSymbol>(),
typeParameters: ImmutableArray<ITypeParameterSymbol>.Empty,
parameters: parameters,
returnTypeAttributes: returnTypeAttributes)
{
......
......@@ -19,7 +19,7 @@ internal class CodeGenerationParameterSymbol : CodeGenerationSymbol, IParameterS
public CodeGenerationParameterSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
RefKind refKind,
bool isParams,
ITypeSymbol type,
......
......@@ -10,7 +10,7 @@ internal class CodeGenerationPointerTypeSymbol : CodeGenerationTypeSymbol, IPoin
public ITypeSymbol PointedAtType { get; }
public CodeGenerationPointerTypeSymbol(ITypeSymbol pointedAtType)
: base(null, null, Accessibility.NotApplicable, default(DeclarationModifiers), string.Empty, SpecialType.None)
: base(null, default(ImmutableArray<AttributeData>), Accessibility.NotApplicable, default(DeclarationModifiers), string.Empty, SpecialType.None)
{
this.PointedAtType = pointedAtType;
}
......
......@@ -21,7 +21,7 @@ internal class CodeGenerationPropertySymbol : CodeGenerationSymbol, IPropertySym
public CodeGenerationPropertySymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
ITypeSymbol type,
......@@ -29,7 +29,7 @@ internal class CodeGenerationPropertySymbol : CodeGenerationSymbol, IPropertySym
IPropertySymbol explicitInterfaceSymbolOpt,
string name,
bool isIndexer,
IList<IParameterSymbol> parametersOpt,
ImmutableArray<IParameterSymbol> parametersOpt,
IMethodSymbol getMethod,
IMethodSymbol setMethod)
: base(containingType, attributes, declaredAccessibility, modifiers, name)
......@@ -37,7 +37,7 @@ internal class CodeGenerationPropertySymbol : CodeGenerationSymbol, IPropertySym
this.Type = type;
this.ReturnsByRef = returnsByRef;
this.IsIndexer = isIndexer;
this.Parameters = parametersOpt.AsImmutableOrEmpty();
this.Parameters = parametersOpt.NullToEmpty();
this.ExplicitInterfaceImplementations = explicitInterfaceSymbolOpt == null
? ImmutableArray.Create<IPropertySymbol>()
: ImmutableArray.Create(explicitInterfaceSymbolOpt);
......@@ -50,7 +50,7 @@ protected override CodeGenerationSymbol Clone()
var result = new CodeGenerationPropertySymbol(
this.ContainingType, this.GetAttributes(), this.DeclaredAccessibility,
this.Modifiers, this.Type, this.ReturnsByRef, this.ExplicitInterfaceImplementations.FirstOrDefault(),
this.Name, this.IsIndexer, this.Parameters.IsDefault ? null : (IList<IParameterSymbol>)this.Parameters,
this.Name, this.IsIndexer, this.Parameters,
this.GetMethod, this.SetMethod);
CodeGenerationPropertyInfo.Attach(result,
CodeGenerationPropertyInfo.GetIsNew(this),
......
......@@ -27,13 +27,13 @@ internal abstract class CodeGenerationSymbol : ISymbol
protected CodeGenerationSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
string name)
{
this.ContainingType = containingType;
_attributes = attributes.AsImmutableOrEmpty();
_attributes = attributes.NullToEmpty();
this.DeclaredAccessibility = declaredAccessibility;
this.Modifiers = modifiers;
this.Name = name;
......
......@@ -17,7 +17,7 @@ internal class CodeGenerationTypeParameterSymbol : CodeGenerationTypeSymbol, ITy
public CodeGenerationTypeParameterSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
VarianceKind varianceKind,
string name,
ImmutableArray<ITypeSymbol> constraintTypes,
......@@ -43,13 +43,7 @@ protected override CodeGenerationSymbol Clone()
this.HasValueTypeConstraint, this.Ordinal);
}
public new ITypeParameterSymbol OriginalDefinition
{
get
{
return this;
}
}
public new ITypeParameterSymbol OriginalDefinition => this;
public ITypeParameterSymbol ReducedFrom => null;
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Editing;
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.CodeGeneration
{
......@@ -12,7 +11,7 @@ internal abstract class CodeGenerationTypeSymbol : CodeGenerationNamespaceOrType
protected CodeGenerationTypeSymbol(
INamedTypeSymbol containingType,
IList<AttributeData> attributes,
ImmutableArray<AttributeData> attributes,
Accessibility declaredAccessibility,
DeclarationModifiers modifiers,
string name,
......@@ -27,20 +26,10 @@ internal abstract class CodeGenerationTypeSymbol : CodeGenerationNamespaceOrType
public virtual INamedTypeSymbol BaseType => null;
public virtual ImmutableArray<INamedTypeSymbol> Interfaces
{
get
{
return ImmutableArray.Create<INamedTypeSymbol>();
}
}
=> ImmutableArray.Create<INamedTypeSymbol>();
public ImmutableArray<INamedTypeSymbol> AllInterfaces
{
get
{
return ImmutableArray.Create<INamedTypeSymbol>();
}
}
=> ImmutableArray.Create<INamedTypeSymbol>();
public bool IsReferenceType => false;
......@@ -56,21 +45,12 @@ public ImmutableArray<INamedTypeSymbol> AllInterfaces
public INamedTypeSymbol TupleUnderlyingType => null;
public new ITypeSymbol OriginalDefinition
{
get
{
return this;
}
}
public new ITypeSymbol OriginalDefinition => this;
public ISymbol FindImplementationForInterfaceMember(ISymbol interfaceMember)
{
return null;
}
public ISymbol FindImplementationForInterfaceMember(ISymbol interfaceMember) => null;
public override bool IsNamespace => false;
public override bool IsType => true;
}
}
}
\ No newline at end of file
......@@ -25,18 +25,15 @@ internal static partial class ICodeDefinitionFactoryExtensions
SpecializedCollections.EmptyList<SyntaxNode>()));
}
public static IList<SyntaxNode> CreateThrowNotImplementedStatementBlock(
this SyntaxGenerator codeDefinitionFactory,
Compilation compilation)
{
return new[] { CreateThrowNotImplementedStatement(codeDefinitionFactory, compilation) };
}
public static ImmutableArray<SyntaxNode> CreateThrowNotImplementedStatementBlock(
this SyntaxGenerator codeDefinitionFactory, Compilation compilation)
=> ImmutableArray.Create(CreateThrowNotImplementedStatement(codeDefinitionFactory, compilation));
public static IList<SyntaxNode> CreateArguments(
public static ImmutableArray<SyntaxNode> CreateArguments(
this SyntaxGenerator factory,
ImmutableArray<IParameterSymbol> parameters)
{
return parameters.Select(p => CreateArgument(factory, p)).ToList();
return parameters.SelectAsArray(p => CreateArgument(factory, p));
}
private static SyntaxNode CreateArgument(
......@@ -54,20 +51,22 @@ internal static partial class ICodeDefinitionFactoryExtensions
// 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(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(),
typeName: typeName,
parameters: constructor.Parameters,
statements: null,
baseConstructorArguments: constructor.Parameters.Length == 0 ? null : factory.CreateArguments(constructor.Parameters));
statements: default(ImmutableArray<SyntaxNode>),
baseConstructorArguments: constructor.Parameters.Length == 0
? default(ImmutableArray<SyntaxNode>)
: factory.CreateArguments(constructor.Parameters));
}
public static IEnumerable<ISymbol> CreateFieldDelegatingConstructor(
this SyntaxGenerator factory,
string typeName,
INamedTypeSymbol containingTypeOpt,
IList<IParameterSymbol> parameters,
ImmutableArray<IParameterSymbol> parameters,
IDictionary<string, ISymbol> parameterToExistingFieldMap,
IDictionary<string, string> parameterToNewFieldMap,
CancellationToken cancellationToken)
......@@ -82,16 +81,16 @@ internal static partial class ICodeDefinitionFactoryExtensions
}
yield return CodeGenerationSymbolFactory.CreateConstructorSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(),
typeName: typeName,
parameters: parameters,
statements: statements.ToList(),
statements: statements.ToImmutableArray(),
thisConstructorArguments: GetThisConstructorArguments(containingTypeOpt, parameterToExistingFieldMap));
}
private static IList<SyntaxNode> GetThisConstructorArguments(
private static ImmutableArray<SyntaxNode> GetThisConstructorArguments(
INamedTypeSymbol containingTypeOpt,
IDictionary<string, ISymbol> parameterToExistingFieldMap)
{
......@@ -110,11 +109,11 @@ internal static partial class ICodeDefinitionFactoryExtensions
{
// We have less field assignments than actual fields. Generate a call to the
// default constructor as well.
return new List<SyntaxNode>();
return ImmutableArray<SyntaxNode>.Empty;
}
}
return null;
return default(ImmutableArray<SyntaxNode>);
}
public static IEnumerable<IFieldSymbol> CreateFieldsForParameters(
......@@ -135,7 +134,7 @@ internal static partial class ICodeDefinitionFactoryExtensions
if (TryGetValue(parameterToNewFieldMap, parameterName, out var fieldName))
{
yield return CodeGenerationSymbolFactory.CreateFieldSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Private,
modifiers: default(DeclarationModifiers),
type: parameterType,
......@@ -309,7 +308,7 @@ private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string
accessorGet = CodeGenerationSymbolFactory.CreateMethodSymbol(
overriddenProperty.GetMethod,
accessibility: getAccessibility,
statements: new[] { getBody },
statements: ImmutableArray.Create(getBody),
modifiers: modifiers);
}
......@@ -322,7 +321,7 @@ private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string
accessorSet = CodeGenerationSymbolFactory.CreateMethodSymbol(
overriddenProperty.SetMethod,
accessibility: setAccessibility,
statements: new[] { setBody },
statements: ImmutableArray.Create(setBody),
modifiers: modifiers);
}
......@@ -344,7 +343,7 @@ private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string
{
return CodeGenerationSymbolFactory.CreateEventSymbol(
overriddenEvent,
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
modifiers: modifiers,
explicitInterfaceSymbol: null,
......@@ -369,7 +368,7 @@ private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string
overriddenMethod,
accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
modifiers: modifiers,
statements: new[] { statement });
statements: ImmutableArray.Create(statement));
}
else
{
......@@ -387,8 +386,8 @@ private static bool TryGetValue(IDictionary<string, ISymbol> dictionary, string
accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
modifiers: modifiers,
statements: overriddenMethod.ReturnsVoid
? new SyntaxNode[] { codeFactory.ExpressionStatement(body) }
: new SyntaxNode[] { codeFactory.ReturnStatement(body) });
? ImmutableArray.Create(codeFactory.ExpressionStatement(body))
: ImmutableArray.Create(codeFactory.ReturnStatement(body)));
}
}
}
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
......@@ -21,32 +22,32 @@ internal static partial class ICodeDefinitionFactoryExtensions
this SyntaxGenerator factory,
Compilation compilation,
INamedTypeSymbol containingType,
IList<ISymbol> symbols,
ImmutableArray<ISymbol> symbols,
CancellationToken cancellationToken)
{
var statements = CreateEqualsMethodStatements(factory, compilation, containingType, symbols, cancellationToken);
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(isOverride: true),
returnType: compilation.GetSpecialType(SpecialType.System_Boolean),
returnsByRef: false,
explicitInterfaceSymbol: null,
name: EqualsName,
typeParameters: null,
parameters: new[] { CodeGenerationSymbolFactory.CreateParameterSymbol(compilation.GetSpecialType(SpecialType.System_Object), ObjName) },
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: ImmutableArray.Create(CodeGenerationSymbolFactory.CreateParameterSymbol(compilation.GetSpecialType(SpecialType.System_Object), ObjName)),
statements: statements);
}
private static IList<SyntaxNode> CreateEqualsMethodStatements(
private static ImmutableArray<SyntaxNode> CreateEqualsMethodStatements(
SyntaxGenerator factory,
Compilation compilation,
INamedTypeSymbol containingType,
IEnumerable<ISymbol> members,
CancellationToken cancellationToken)
{
var statements = new List<SyntaxNode>();
var statements = ArrayBuilder<SyntaxNode>.GetInstance();
var parts = StringBreaker.BreakIntoWordParts(containingType.Name);
string localName = "v";
......@@ -141,7 +142,7 @@ internal static partial class ICodeDefinitionFactoryExtensions
statements.Add(factory.ReturnStatement(
expressions.Aggregate(factory.LogicalAndExpression)));
return statements;
return statements.ToImmutableAndFree();
}
private static SyntaxNode GetDefaultEqualityComparer(
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Editing;
......@@ -15,29 +16,29 @@ internal static partial class ICodeDefinitionFactoryExtensions
this SyntaxGenerator factory,
Compilation compilation,
INamedTypeSymbol containingType,
IList<ISymbol> symbols,
ImmutableArray<ISymbol> symbols,
CancellationToken cancellationToken)
{
var statements = CreateGetHashCodeMethodStatements(factory, compilation, containingType, symbols, cancellationToken);
return CodeGenerationSymbolFactory.CreateMethodSymbol(
attributes: null,
attributes: default(ImmutableArray<AttributeData>),
accessibility: Accessibility.Public,
modifiers: new DeclarationModifiers(isOverride: true),
returnType: compilation.GetSpecialType(SpecialType.System_Int32),
returnsByRef: false,
explicitInterfaceSymbol: null,
name: GetHashCodeName,
typeParameters: null,
parameters: null,
typeParameters: default(ImmutableArray<ITypeParameterSymbol>),
parameters: default(ImmutableArray<IParameterSymbol>),
statements: statements);
}
private static IList<SyntaxNode> CreateGetHashCodeMethodStatements(
private static ImmutableArray<SyntaxNode> CreateGetHashCodeMethodStatements(
SyntaxGenerator factory,
Compilation compilation,
INamedTypeSymbol containingType,
IList<ISymbol> members,
ImmutableArray<ISymbol> members,
CancellationToken cancellationToken)
{
const string HashCodeName = "hashCode";
......@@ -46,12 +47,12 @@ internal static partial class ICodeDefinitionFactoryExtensions
var permuteValue = factory.NegateExpression(
factory.LiteralExpression(1521134295));
var statements = new List<SyntaxNode>();
var statements = ArrayBuilder<SyntaxNode>.GetInstance();
var hashCodeNameExpression = factory.IdentifierName(HashCodeName);
var firstHashValue = ComputeHashValue(factory, compilation, members[0]);
if (members.Count == 1)
if (members.Length == 1)
{
#if false
return this.S1.GetHashCode();
......@@ -65,7 +66,7 @@ internal static partial class ICodeDefinitionFactoryExtensions
#endif
statements.Add(factory.LocalDeclarationStatement(HashCodeName, firstHashValue));
for (var i = 1; i < members.Count; i++)
for (var i = 1; i < members.Length; i++)
{
#if false
hashCode = hashCode * 0xA5555529 + value
......@@ -83,7 +84,7 @@ internal static partial class ICodeDefinitionFactoryExtensions
statements.Add(factory.ReturnStatement(hashCodeNameExpression));
}
return statements;
return statements.ToImmutableAndFree();
}
private static SyntaxNode ComputeHashValue(
......
......@@ -80,9 +80,9 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
method.ExplicitInterfaceImplementations.FirstOrDefault(),
method.Name,
updatedTypeParameters,
method.Parameters.Select(p =>
method.Parameters.SelectAsArray(p =>
CodeGenerationSymbolFactory.CreateParameterSymbol(p.GetAttributes(), p.RefKind, p.IsParams, p.Type.SubstituteTypes(mapping, typeGenerator), p.Name, p.IsOptional,
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)).ToList());
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)));
}
public static IMethodSymbol RenameParameters(
......@@ -109,7 +109,7 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
parameters);
}
private static IList<ITypeParameterSymbol> RenameTypeParameters(
private static ImmutableArray<ITypeParameterSymbol> RenameTypeParameters(
IList<ITypeParameterSymbol> typeParameters,
IList<string> newNames,
ITypeGenerator typeGenerator)
......@@ -143,7 +143,7 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
newTypeParameter.ConstraintTypes = ImmutableArray.CreateRange(newTypeParameter.ConstraintTypes, t => t.SubstituteTypes(mapping, typeGenerator));
}
return newTypeParameters.Cast<ITypeParameterSymbol>().ToList();
return newTypeParameters.Cast<ITypeParameterSymbol>().ToImmutableArray();
}
public static IMethodSymbol EnsureNonConflictingNames(
......@@ -172,17 +172,21 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
}
public static IMethodSymbol RemoveInaccessibleAttributesAndAttributesOfTypes(
this IMethodSymbol method, ISymbol accessibleWithin, params INamedTypeSymbol[] removeAttributeTypes)
this IMethodSymbol method, ISymbol accessibleWithin,
params INamedTypeSymbol[] removeAttributeTypes)
{
Func<AttributeData, bool> shouldRemoveAttribute = a =>
removeAttributeTypes.Any(attr => attr != null && attr.Equals(a.AttributeClass)) || !a.AttributeClass.IsAccessibleWithin(accessibleWithin);
return method.RemoveAttributesCore(shouldRemoveAttribute, statements: null, handlesExpressions: null);
return method.RemoveAttributesCore(
shouldRemoveAttribute,
statements: default(ImmutableArray<SyntaxNode>),
handlesExpressions: default(ImmutableArray<SyntaxNode>));
}
private static IMethodSymbol RemoveAttributesCore(
this IMethodSymbol method, Func<AttributeData, bool> shouldRemoveAttribute,
IList<SyntaxNode> statements, IList<SyntaxNode> handlesExpressions)
ImmutableArray<SyntaxNode> statements, ImmutableArray<SyntaxNode> handlesExpressions)
{
var methodHasAttribute = method.GetAttributes().Any(shouldRemoveAttribute);
......@@ -198,7 +202,7 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
return CodeGenerationSymbolFactory.CreateMethodSymbol(
method.ContainingType,
method.GetAttributes().Where(a => !shouldRemoveAttribute(a)).ToList(),
method.GetAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)),
method.DeclaredAccessibility,
method.GetSymbolModifiers(),
method.ReturnType,
......@@ -206,14 +210,14 @@ public static IMethodSymbol RenameTypeParameters(this IMethodSymbol method, ILis
method.ExplicitInterfaceImplementations.FirstOrDefault(),
method.Name,
method.TypeParameters,
method.Parameters.Select(p =>
method.Parameters.SelectAsArray(p =>
CodeGenerationSymbolFactory.CreateParameterSymbol(
p.GetAttributes().Where(a => !shouldRemoveAttribute(a)).ToList(),
p.GetAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)),
p.RefKind, p.IsParams, p.Type, p.Name, p.IsOptional,
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)).ToList(),
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)),
statements,
handlesExpressions,
method.GetReturnTypeAttributes().Where(a => !shouldRemoveAttribute(a)).ToList());
method.GetReturnTypeAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)));
}
public static bool? IsMoreSpecificThan(this IMethodSymbol method1, IMethodSymbol method2)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeGeneration;
namespace Microsoft.CodeAnalysis.Shared.Extensions
......@@ -27,15 +28,15 @@ public static IParameterSymbol RenameParameter(this IParameterSymbol parameter,
parameter.HasExplicitDefaultValue ? parameter.ExplicitDefaultValue : null);
}
public static IList<IParameterSymbol> RenameParameters(this IList<IParameterSymbol> parameters, IList<string> parameterNames)
public static ImmutableArray<IParameterSymbol> RenameParameters(this IList<IParameterSymbol> parameters, IList<string> parameterNames)
{
var result = new List<IParameterSymbol>();
for (int i = 0; i < parameterNames.Count; i++)
var result = ArrayBuilder<IParameterSymbol>.GetInstance();
for (var i = 0; i < parameterNames.Count; i++)
{
result.Add(parameters[i].RenameParameter(parameterNames[i]));
}
return result;
return result.ToImmutableAndFree();
}
}
}
......@@ -62,11 +62,11 @@ public static IPropertySymbol RenameParameters(this IPropertySymbol property, IL
property.ReturnsByRef,
property.ExplicitInterfaceImplementations.FirstOrDefault(),
property.Name,
property.Parameters.Select(p =>
property.Parameters.SelectAsArray(p =>
CodeGenerationSymbolFactory.CreateParameterSymbol(
p.GetAttributes().Where(a => !shouldRemoveAttribute(a)).ToList(),
p.GetAttributes().WhereAsArray(a => !shouldRemoveAttribute(a)),
p.RefKind, p.IsParams, p.Type, p.Name, p.IsOptional,
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)).ToList(),
p.HasExplicitDefaultValue, p.HasExplicitDefaultValue ? p.ExplicitDefaultValue : null)),
property.GetMethod,
property.SetMethod,
property.IsIndexer);
......
......@@ -58,22 +58,22 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
End Function
Private Function GenerateStatements(constructor As IMethodSymbol) As SyntaxList(Of StatementSyntax)
If CodeGenerationConstructorInfo.GetStatements(constructor) Is Nothing AndAlso
CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor) Is Nothing AndAlso
CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) Is Nothing Then
If CodeGenerationConstructorInfo.GetStatements(constructor).IsDefault AndAlso
CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor).IsDefault AndAlso
CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor).IsDefault Then
Return Nothing
End If
Dim statements = New List(Of StatementSyntax)
If CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor) IsNot Nothing Then
If Not CodeGenerationConstructorInfo.GetBaseConstructorArgumentsOpt(constructor).IsDefault Then
statements.Add(CreateBaseConstructorCall(constructor))
End If
If CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor) IsNot Nothing Then
If Not CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor).IsDefault Then
statements.Add(CreateThisConstructorCall(CodeGenerationConstructorInfo.GetThisConstructorArgumentsOpt(constructor)))
End If
If CodeGenerationConstructorInfo.GetStatements(constructor) IsNot Nothing Then
If Not CodeGenerationConstructorInfo.GetStatements(constructor).IsDefault Then
statements.AddRange(StatementGenerator.GenerateStatements(
CodeGenerationConstructorInfo.GetStatements(constructor)))
End If
......
......@@ -143,7 +143,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
Private Function GenerateAccessorStatements(accessor As IMethodSymbol) As SyntaxList(Of StatementSyntax)
Dim statementsOpt = CodeGenerationMethodInfo.GetStatements(accessor)
If statementsOpt IsNot Nothing Then
If Not statementsOpt.IsDefault Then
Return SyntaxFactory.List(statementsOpt.OfType(Of StatementSyntax))
Else
Return Nothing
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册