Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Compilers/CSharp/Portable/Emitter/Model/PEModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,19 +1913,28 @@ internal NamedTypeSymbol EnsureInlineArrayTypeExists(SyntaxNode syntaxNode, Synt
return (NamedTypeSymbol)typeAdapter.GetInternalSymbol()!;
}

internal NamedTypeSymbol EnsureReadOnlyListTypeExists(SyntaxNode syntaxNode, bool hasKnownLength, DiagnosticBag diagnostics)
internal NamedTypeSymbol EnsureReadOnlyListTypeExists(SyntaxNode syntaxNode, SynthesizedReadOnlyListKind kind, DiagnosticBag diagnostics)
{
Debug.Assert(syntaxNode is { });
Debug.Assert(diagnostics is { });

string typeName = GeneratedNames.MakeSynthesizedReadOnlyListName(hasKnownLength, CurrentGenerationOrdinal);
string typeName = GeneratedNames.MakeSynthesizedReadOnlyListName(kind, CurrentGenerationOrdinal);
var privateImplClass = GetPrivateImplClass(syntaxNode, diagnostics).PrivateImplementationDetails;
var typeAdapter = privateImplClass.GetSynthesizedType(typeName);
NamedTypeSymbol typeSymbol;

if (typeAdapter is null)
{
typeSymbol = SynthesizedReadOnlyListTypeSymbol.Create(SourceModule, typeName, hasKnownLength);
NamedTypeSymbol? enumeratorType = null;
if (kind == SynthesizedReadOnlyListKind.Singleton)
{
var name = GeneratedNames.MakeSynthesizedReadOnlyListEnumeratorName(CurrentGenerationOrdinal);
enumeratorType = SynthesizedReadOnlyListEnumeratorTypeSymbol.Create(SourceModule, name);
privateImplClass.TryAddSynthesizedType(enumeratorType.GetCciAdapter());
var adapter = privateImplClass.GetSynthesizedType(name)!;
enumeratorType = (NamedTypeSymbol)adapter.GetInternalSymbol()!;
}
typeSymbol = SynthesizedReadOnlyListTypeSymbol.Create(SourceModule, typeName, kind, enumeratorType);
privateImplClass.TryAddSynthesizedType(typeSymbol.GetCciAdapter());
typeAdapter = privateImplClass.GetSynthesizedType(typeName)!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,36 @@ SpecialType.System_Collections_Generic_IReadOnlyCollection_T or
int numberIncludingLastSpread;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you didn't name this variable, but... number of what? We need to rename this for clarity. If you want to do it now, go for it, if not we can do it in a follow up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried lastSpreadIndex here 57dc299 but that's more than a rename. I'll leave it out of this PR.

bool useKnownLength = ShouldUseKnownLength(node, out numberIncludingLastSpread);

if (numberIncludingLastSpread == 0 && node.Elements.Length == 0)
if (node.Elements.Length == 0)
{
Debug.Assert(numberIncludingLastSpread == 0);
// arrayOrList = Array.Empty<ElementType>();
arrayOrList = CreateEmptyArray(syntax, ArrayTypeSymbol.CreateSZArray(_compilation.Assembly, elementType));
}
else
{
var typeArgs = ImmutableArray.Create(elementType);
var synthesizedType = _factory.ModuleBuilderOpt.EnsureReadOnlyListTypeExists(syntax, hasKnownLength: useKnownLength, _diagnostics.DiagnosticBag).Construct(typeArgs);
var kind = useKnownLength
? numberIncludingLastSpread == 0 && node.Elements.Length == 1
? SynthesizedReadOnlyListKind.Singleton
: SynthesizedReadOnlyListKind.Array
: SynthesizedReadOnlyListKind.List;
var synthesizedType = _factory.ModuleBuilderOpt.EnsureReadOnlyListTypeExists(syntax, kind: kind, _diagnostics.DiagnosticBag).Construct(typeArgs);
if (synthesizedType.IsErrorType())
{
return BadExpression(node);
}

BoundExpression fieldValue;
if (useKnownLength)
BoundExpression fieldValue = kind switch
{
// fieldValue = e1;
SynthesizedReadOnlyListKind.Singleton => this.VisitExpression((BoundExpression)node.Elements.Single()),
// fieldValue = new ElementType[] { e1, ..., eN };
var arrayType = ArrayTypeSymbol.CreateSZArray(_compilation.Assembly, elementType);
fieldValue = CreateAndPopulateArray(node, arrayType);
}
else
{
SynthesizedReadOnlyListKind.Array => CreateAndPopulateArray(node, ArrayTypeSymbol.CreateSZArray(_compilation.Assembly, elementType)),
// fieldValue = new List<ElementType> { e1, ..., eN };
fieldValue = CreateAndPopulateList(node, elementType);
}
SynthesizedReadOnlyListKind.List => CreateAndPopulateList(node, elementType),
var v => throw ExceptionUtilities.UnexpectedValue(v)
};

// arrayOrList = new <>z__ReadOnlyList<ElementType>(fieldValue);
arrayOrList = new BoundObjectCreationExpression(syntax, synthesizedType.Constructors.Single(), fieldValue) { WasCompilerGenerated = true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,22 @@ internal static string MakeSynthesizedInlineArrayName(int arrayLength, int gener
return (generation > 0) ? name + GeneratedNameConstants.GenerationSeparator + generation : name;
}

internal static string MakeSynthesizedReadOnlyListName(bool hasKnownLength, int generation)
internal static string MakeSynthesizedReadOnlyListEnumeratorName(int generation)
{
var name = "<>z__ReadOnlySingletonListEnumerator";
return (generation > 0) ? name + CommonGeneratedNames.GenerationSeparator + generation : name;
}

internal static string MakeSynthesizedReadOnlyListName(SynthesizedReadOnlyListKind kind, int generation)
{
Debug.Assert((char)GeneratedNameKind.ReadOnlyListType == 'z');
string name = hasKnownLength ? "<>z__ReadOnlyArray" : "<>z__ReadOnlyList";
string name = kind switch
{
SynthesizedReadOnlyListKind.Array => "<>z__ReadOnlyArray",
SynthesizedReadOnlyListKind.List => "<>z__ReadOnlyList",
SynthesizedReadOnlyListKind.Singleton => "<>z__ReadOnlySingletonList",
var v => throw ExceptionUtilities.UnexpectedValue(v)
};

// Synthesized list types need to have unique name across generations because they are not reused.
return (generation > 0) ? name + CommonGeneratedNames.GenerationSeparator + generation : name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class SynthesizedReadOnlyListConstructor : SynthesizedInstanceCo
internal SynthesizedReadOnlyListConstructor(SynthesizedReadOnlyListTypeSymbol containingType, TypeSymbol parameterType) : base(containingType)
{
Parameters = ImmutableArray.Create(
SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(parameterType), ordinal: 0, RefKind.None, "items"));
SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(parameterType), ordinal: 0, RefKind.None, parameterType.IsTypeParameter() ? "item" : "items"));
}

public override ImmutableArray<ParameterSymbol> Parameters { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.Linq;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal sealed class SynthesizedReadOnlyListEnumeratorConstructor : SynthesizedInstanceConstructor
{
internal SynthesizedReadOnlyListEnumeratorConstructor(SynthesizedReadOnlyListEnumeratorTypeSymbol containingType, TypeSymbol parameterType) : base(containingType)
{
Parameters = ImmutableArray.Create(
SynthesizedParameterSymbol.Create(this, TypeWithAnnotations.Create(parameterType), ordinal: 0, RefKind.None, "item"));
}

public override ImmutableArray<ParameterSymbol> Parameters { get; }

internal override bool SynthesizesLoweredBoundBody => true;

internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
{
SyntheticBoundNodeFactory f = new SyntheticBoundNodeFactory(this, this.GetNonNullSyntaxNode(), compilationState, diagnostics);
f.CurrentFunction = this;

try
{
var baseConstructor = ContainingType.BaseTypeNoUseSiteDiagnostics.InstanceConstructors.Single();
var field = ContainingType.GetFieldsToEmit().First();
var parameter = Parameters.Single();

var block = f.Block(
// object..ctor();
f.ExpressionStatement(f.Call(f.This(), baseConstructor)),
// _item = item;
f.Assignment(f.Field(f.This(), field), f.Parameter(parameter)),
// return;
f.Return());
f.CloseMethod(block);
}
catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
{
diagnostics.Add(ex.Diagnostic);
f.CloseMethod(f.ThrowNull());
}
}
}
}
Loading