160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Collections.Immutable;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols;
|
||
|
|
using Microsoft.CodeAnalysis.PooledObjects;
|
||
|
|
using Roslyn.Utilities;
|
||
|
|
|
||
|
|
namespace Microsoft.CodeAnalysis.CSharp;
|
||
|
|
|
||
|
|
internal sealed class MergedNamespaceDeclaration : MergedNamespaceOrTypeDeclaration
|
||
|
|
{
|
||
|
|
private readonly ImmutableArray<SingleNamespaceDeclaration> _declarations;
|
||
|
|
|
||
|
|
private ImmutableArray<MergedNamespaceOrTypeDeclaration> _lazyChildren;
|
||
|
|
|
||
|
|
public override DeclarationKind Kind => DeclarationKind.Namespace;
|
||
|
|
|
||
|
|
public ImmutableArray<Location> NameLocations
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (_declarations.Length == 1)
|
||
|
|
{
|
||
|
|
return ImmutableArray.Create<Location>((Location)(object)_declarations[0].NameLocation);
|
||
|
|
}
|
||
|
|
ArrayBuilder<Location> instance = ArrayBuilder<Location>.GetInstance();
|
||
|
|
ImmutableArray<SingleNamespaceDeclaration>.Enumerator enumerator = _declarations.GetEnumerator();
|
||
|
|
while (enumerator.MoveNext())
|
||
|
|
{
|
||
|
|
SourceLocation nameLocation = enumerator.Current.NameLocation;
|
||
|
|
if ((Location)(object)nameLocation != (Location)null)
|
||
|
|
{
|
||
|
|
instance.Add((Location)(object)nameLocation);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return instance.ToImmutableAndFree();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public ImmutableArray<SingleNamespaceDeclaration> Declarations => _declarations;
|
||
|
|
|
||
|
|
public new ImmutableArray<MergedNamespaceOrTypeDeclaration> Children
|
||
|
|
{
|
||
|
|
get
|
||
|
|
{
|
||
|
|
if (_lazyChildren.IsDefault)
|
||
|
|
{
|
||
|
|
ImmutableInterlocked.InterlockedInitialize(ref _lazyChildren, MakeChildren());
|
||
|
|
}
|
||
|
|
return _lazyChildren;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private MergedNamespaceDeclaration(ImmutableArray<SingleNamespaceDeclaration> declarations)
|
||
|
|
: base(declarations.IsEmpty ? string.Empty : declarations[0].Name)
|
||
|
|
{
|
||
|
|
_declarations = declarations;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static MergedNamespaceDeclaration Create(ImmutableArray<SingleNamespaceDeclaration> declarations)
|
||
|
|
{
|
||
|
|
return new MergedNamespaceDeclaration(declarations);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static MergedNamespaceDeclaration Create(SingleNamespaceDeclaration declaration)
|
||
|
|
{
|
||
|
|
return new MergedNamespaceDeclaration(ImmutableArray.Create(declaration));
|
||
|
|
}
|
||
|
|
|
||
|
|
public LexicalSortKey GetLexicalSortKey(CSharpCompilation compilation)
|
||
|
|
{
|
||
|
|
LexicalSortKey lexicalSortKey = new LexicalSortKey((Location)(object)_declarations[0].NameLocation, compilation);
|
||
|
|
for (int i = 1; i < _declarations.Length; i++)
|
||
|
|
{
|
||
|
|
lexicalSortKey = LexicalSortKey.First(lexicalSortKey, new LexicalSortKey((Location)(object)_declarations[i].NameLocation, compilation));
|
||
|
|
}
|
||
|
|
return lexicalSortKey;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected override ImmutableArray<Declaration> GetDeclarationChildren()
|
||
|
|
{
|
||
|
|
return StaticCast<Declaration>.From<MergedNamespaceOrTypeDeclaration>(Children);
|
||
|
|
}
|
||
|
|
|
||
|
|
private ImmutableArray<MergedNamespaceOrTypeDeclaration> MakeChildren()
|
||
|
|
{
|
||
|
|
ArrayBuilder<SingleNamespaceDeclaration> val = null;
|
||
|
|
ArrayBuilder<SingleTypeDeclaration> val2 = null;
|
||
|
|
bool flag = true;
|
||
|
|
bool flag2 = true;
|
||
|
|
ImmutableArray<SingleNamespaceDeclaration>.Enumerator enumerator = _declarations.GetEnumerator();
|
||
|
|
while (enumerator.MoveNext())
|
||
|
|
{
|
||
|
|
ImmutableArray<SingleNamespaceOrTypeDeclaration>.Enumerator enumerator2 = enumerator.Current.Children.GetEnumerator();
|
||
|
|
while (enumerator2.MoveNext())
|
||
|
|
{
|
||
|
|
SingleNamespaceOrTypeDeclaration current = enumerator2.Current;
|
||
|
|
if (current is SingleTypeDeclaration singleTypeDeclaration)
|
||
|
|
{
|
||
|
|
if (val2 == null)
|
||
|
|
{
|
||
|
|
val2 = ArrayBuilder<SingleTypeDeclaration>.GetInstance();
|
||
|
|
}
|
||
|
|
else if (flag2 && !singleTypeDeclaration.Identity.Equals(val2[0].Identity))
|
||
|
|
{
|
||
|
|
flag2 = false;
|
||
|
|
}
|
||
|
|
val2.Add(singleTypeDeclaration);
|
||
|
|
}
|
||
|
|
else if (current is SingleNamespaceDeclaration singleNamespaceDeclaration)
|
||
|
|
{
|
||
|
|
if (val == null)
|
||
|
|
{
|
||
|
|
val = ArrayBuilder<SingleNamespaceDeclaration>.GetInstance();
|
||
|
|
}
|
||
|
|
else if (flag && !singleNamespaceDeclaration.Name.Equals(val[0].Name))
|
||
|
|
{
|
||
|
|
flag = false;
|
||
|
|
}
|
||
|
|
val.Add(singleNamespaceDeclaration);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
ArrayBuilder<MergedNamespaceOrTypeDeclaration> instance = ArrayBuilder<MergedNamespaceOrTypeDeclaration>.GetInstance();
|
||
|
|
if (val != null)
|
||
|
|
{
|
||
|
|
if (flag)
|
||
|
|
{
|
||
|
|
instance.Add((MergedNamespaceOrTypeDeclaration)Create(val.ToImmutableAndFree()));
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Dictionary<string, ImmutableArray<SingleNamespaceDeclaration>> dictionary = val.ToDictionary<string>((Func<SingleNamespaceDeclaration, string>)((SingleNamespaceDeclaration n) => n.Name), (IEqualityComparer<string>)StringOrdinalComparer.Instance);
|
||
|
|
val.Free();
|
||
|
|
foreach (ImmutableArray<SingleNamespaceDeclaration> value in dictionary.Values)
|
||
|
|
{
|
||
|
|
instance.Add((MergedNamespaceOrTypeDeclaration)Create(value));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (val2 != null)
|
||
|
|
{
|
||
|
|
if (flag2)
|
||
|
|
{
|
||
|
|
instance.Add((MergedNamespaceOrTypeDeclaration)new MergedTypeDeclaration(val2.ToImmutableAndFree()));
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Dictionary<SingleTypeDeclaration.TypeDeclarationIdentity, ImmutableArray<SingleTypeDeclaration>> dictionary2 = val2.ToDictionary<SingleTypeDeclaration.TypeDeclarationIdentity>((Func<SingleTypeDeclaration, SingleTypeDeclaration.TypeDeclarationIdentity>)((SingleTypeDeclaration t) => t.Identity), (IEqualityComparer<SingleTypeDeclaration.TypeDeclarationIdentity>)null);
|
||
|
|
val2.Free();
|
||
|
|
foreach (ImmutableArray<SingleTypeDeclaration> value2 in dictionary2.Values)
|
||
|
|
{
|
||
|
|
instance.Add((MergedNamespaceOrTypeDeclaration)new MergedTypeDeclaration(value2));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return instance.ToImmutableAndFree();
|
||
|
|
}
|
||
|
|
}
|