163 lines
7.2 KiB
C#
163 lines
7.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Diagnostics;
|
|
using Microsoft.Cci;
|
|
using Microsoft.CodeAnalysis.CSharp.Emit;
|
|
using Microsoft.CodeAnalysis.CSharp.Emit.NoPia;
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
using Microsoft.CodeAnalysis.Emit;
|
|
using Microsoft.CodeAnalysis.PooledObjects;
|
|
|
|
namespace Microsoft.CodeAnalysis.CSharp;
|
|
|
|
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
|
|
internal sealed class ImportChain : IImportScope
|
|
{
|
|
public readonly Imports Imports;
|
|
|
|
public readonly ImportChain ParentOpt;
|
|
|
|
IImportScope IImportScope.Parent => (IImportScope)(object)ParentOpt;
|
|
|
|
public ImportChain(Imports imports, ImportChain parentOpt)
|
|
{
|
|
Imports = imports;
|
|
ParentOpt = parentOpt;
|
|
}
|
|
|
|
private string GetDebuggerDisplay()
|
|
{
|
|
return $"{Imports.GetDebuggerDisplay()} ^ {ParentOpt?.GetHashCode() ?? 0}";
|
|
}
|
|
|
|
ImmutableArray<UsedNamespaceOrType> IImportScope.GetUsedNamespaces(EmitContext context)
|
|
{
|
|
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
|
|
((PEModuleBuilder)(object)context.Module).TryGetTranslatedImports(this, out var imports);
|
|
return imports;
|
|
}
|
|
|
|
public IImportScope Translate(PEModuleBuilder moduleBuilder, DiagnosticBag diagnostics)
|
|
{
|
|
ImportChain importChain = this;
|
|
ImmutableArray<UsedNamespaceOrType> imports;
|
|
while (importChain != null && !moduleBuilder.TryGetTranslatedImports(importChain, out imports))
|
|
{
|
|
moduleBuilder.GetOrAddTranslatedImports(importChain, importChain.TranslateImports(moduleBuilder, diagnostics));
|
|
importChain = importChain.ParentOpt;
|
|
}
|
|
return (IImportScope)(object)this;
|
|
}
|
|
|
|
private ImmutableArray<UsedNamespaceOrType> TranslateImports(PEModuleBuilder moduleBuilder, DiagnosticBag diagnostics)
|
|
{
|
|
//IL_0136: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_013b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_016e: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0175: Invalid comparison between Unknown and I4
|
|
//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0197: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_01e5: Unknown result type (might be due to invalid IL or missing references)
|
|
ArrayBuilder<UsedNamespaceOrType> instance = ArrayBuilder<UsedNamespaceOrType>.GetInstance();
|
|
ImmutableArray<AliasAndExternAliasDirective> externAliases = Imports.ExternAliases;
|
|
if (!externAliases.IsDefault)
|
|
{
|
|
ImmutableArray<AliasAndExternAliasDirective>.Enumerator enumerator = externAliases.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
instance.Add(UsedNamespaceOrType.CreateExternAlias(enumerator.Current.Alias.Name));
|
|
}
|
|
}
|
|
ImmutableArray<NamespaceOrTypeAndUsingDirective> usings = Imports.Usings;
|
|
if (!usings.IsDefault)
|
|
{
|
|
ImmutableArray<NamespaceOrTypeAndUsingDirective>.Enumerator enumerator2 = usings.GetEnumerator();
|
|
while (enumerator2.MoveNext())
|
|
{
|
|
NamespaceOrTypeAndUsingDirective current = enumerator2.Current;
|
|
NamespaceOrTypeSymbol namespaceOrType = current.NamespaceOrType;
|
|
if (namespaceOrType.IsNamespace)
|
|
{
|
|
NamespaceSymbol namespaceSymbol = (NamespaceSymbol)namespaceOrType;
|
|
IAssemblyReference val = TryGetAssemblyScope(namespaceSymbol, moduleBuilder, diagnostics);
|
|
instance.Add(UsedNamespaceOrType.CreateNamespace((INamespace)(object)namespaceSymbol.GetCciAdapter(), val, (string)null));
|
|
}
|
|
else if (!namespaceOrType.ContainingAssembly.IsLinked)
|
|
{
|
|
ITypeReference typeReference = GetTypeReference((TypeSymbol)namespaceOrType, (SyntaxNode)(object)current.UsingDirective, moduleBuilder, diagnostics);
|
|
instance.Add(UsedNamespaceOrType.CreateType(typeReference, (string)null));
|
|
}
|
|
}
|
|
}
|
|
ImmutableDictionary<string, AliasAndUsingDirective> usingAliases = Imports.UsingAliases;
|
|
if (!usingAliases.IsEmpty)
|
|
{
|
|
ArrayBuilder<string> instance2 = ArrayBuilder<string>.GetInstance(usingAliases.Count);
|
|
instance2.AddRange(usingAliases.Keys);
|
|
instance2.Sort((IComparer<string>)StringComparer.Ordinal);
|
|
Enumerator<string> enumerator3 = instance2.GetEnumerator();
|
|
while (enumerator3.MoveNext())
|
|
{
|
|
string current2 = enumerator3.Current;
|
|
AliasAndUsingDirective aliasAndUsingDirective = usingAliases[current2];
|
|
AliasSymbol alias = aliasAndUsingDirective.Alias;
|
|
UsingDirectiveSyntax usingDirective = aliasAndUsingDirective.UsingDirective;
|
|
NamespaceOrTypeSymbol target = alias.Target;
|
|
if ((int)target.Kind == 12)
|
|
{
|
|
NamespaceSymbol namespaceSymbol2 = (NamespaceSymbol)target;
|
|
IAssemblyReference val2 = TryGetAssemblyScope(namespaceSymbol2, moduleBuilder, diagnostics);
|
|
instance.Add(UsedNamespaceOrType.CreateNamespace((INamespace)(object)namespaceSymbol2.GetCciAdapter(), val2, current2));
|
|
continue;
|
|
}
|
|
bool flag;
|
|
if (target is NamedTypeSymbol)
|
|
{
|
|
AssemblySymbol containingAssembly = target.ContainingAssembly;
|
|
if ((object)containingAssembly == null || containingAssembly.IsLinked)
|
|
{
|
|
flag = false;
|
|
goto IL_01ca;
|
|
}
|
|
}
|
|
flag = true;
|
|
goto IL_01ca;
|
|
IL_01ca:
|
|
if (flag)
|
|
{
|
|
ITypeReference typeReference2 = GetTypeReference((TypeSymbol)target, (SyntaxNode)(object)usingDirective, moduleBuilder, diagnostics);
|
|
instance.Add(UsedNamespaceOrType.CreateType(typeReference2, current2));
|
|
}
|
|
}
|
|
instance2.Free();
|
|
}
|
|
return instance.ToImmutableAndFree();
|
|
}
|
|
|
|
private static ITypeReference GetTypeReference(TypeSymbol type, SyntaxNode syntaxNode, PEModuleBuilder moduleBuilder, DiagnosticBag diagnostics)
|
|
{
|
|
return ((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)moduleBuilder).Translate(type, syntaxNode, diagnostics);
|
|
}
|
|
|
|
private static IAssemblyReference TryGetAssemblyScope(NamespaceSymbol @namespace, PEModuleBuilder moduleBuilder, DiagnosticBag diagnostics)
|
|
{
|
|
AssemblySymbol containingAssembly = @namespace.ContainingAssembly;
|
|
if ((object)containingAssembly != null && (object)containingAssembly != ((CommonPEModuleBuilder)moduleBuilder).CommonCompilation.Assembly)
|
|
{
|
|
CSharpCompilation.ReferenceManager boundReferenceManager = ((CSharpCompilation)(object)((CommonPEModuleBuilder)moduleBuilder).CommonCompilation).GetBoundReferenceManager();
|
|
for (int i = 0; i < ((CommonReferenceManager<CSharpCompilation, AssemblySymbol>)(object)boundReferenceManager).ReferencedAssemblies.Length; i++)
|
|
{
|
|
if ((object)((CommonReferenceManager<CSharpCompilation, AssemblySymbol>)(object)boundReferenceManager).ReferencedAssemblies[i] == containingAssembly && !((CommonReferenceManager<CSharpCompilation, AssemblySymbol>)(object)boundReferenceManager).DeclarationsAccessibleWithoutAlias(i))
|
|
{
|
|
return ((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)moduleBuilder).Translate(containingAssembly, diagnostics);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|