248 lines
16 KiB
C#
248 lines
16 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Collections.Immutable;
|
||
|
|
using System.Diagnostics.CodeAnalysis;
|
||
|
|
using System.Globalization;
|
||
|
|
using System.Reflection.Metadata;
|
||
|
|
using Microsoft.Cci;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp.Emit.NoPia;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
|
||
|
|
using Microsoft.CodeAnalysis.CodeGen;
|
||
|
|
using Microsoft.CodeAnalysis.Collections;
|
||
|
|
using Microsoft.CodeAnalysis.Emit;
|
||
|
|
using Microsoft.CodeAnalysis.Emit.NoPia;
|
||
|
|
using Microsoft.CodeAnalysis.PooledObjects;
|
||
|
|
using Microsoft.CodeAnalysis.Symbols;
|
||
|
|
using Roslyn.Utilities;
|
||
|
|
|
||
|
|
namespace Microsoft.CodeAnalysis.CSharp.Emit;
|
||
|
|
|
||
|
|
internal sealed class PEDeltaAssemblyBuilder : PEAssemblyBuilderBase, IPEDeltaAssemblyBuilder
|
||
|
|
{
|
||
|
|
private readonly EmitBaseline _previousGeneration;
|
||
|
|
|
||
|
|
private readonly CSharpDefinitionMap _previousDefinitions;
|
||
|
|
|
||
|
|
private readonly SymbolChanges _changes;
|
||
|
|
|
||
|
|
private readonly CSharpSymbolMatcher.DeepTranslator _deepTranslator;
|
||
|
|
|
||
|
|
public override SymbolChanges? EncSymbolChanges => _changes;
|
||
|
|
|
||
|
|
public override EmitBaseline PreviousGeneration => _previousGeneration;
|
||
|
|
|
||
|
|
internal CSharpDefinitionMap PreviousDefinitions => _previousDefinitions;
|
||
|
|
|
||
|
|
public PEDeltaAssemblyBuilder(SourceAssemblySymbol sourceAssembly, EmitOptions emitOptions, OutputKind outputKind, ModulePropertiesForSerialization serializationProperties, IEnumerable<ResourceDescription> manifestResources, EmitBaseline previousGeneration, IEnumerable<SemanticEdit> edits, Func<ISymbol, bool> isAddedSymbol)
|
||
|
|
: base(sourceAssembly, emitOptions, outputKind, serializationProperties, manifestResources, ImmutableArray<NamedTypeSymbol>.Empty)
|
||
|
|
{
|
||
|
|
//IL_0003: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0029: Expected O, but got Unknown
|
||
|
|
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0057: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_008e: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_009a: Expected O, but got Unknown
|
||
|
|
//IL_009b: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_009e: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
EmitBaseline initialBaseline = previousGeneration.InitialBaseline;
|
||
|
|
EmitContext sourceContext = default(EmitContext);
|
||
|
|
((EmitContext)(ref sourceContext))._002Ector((CommonPEModuleBuilder)(object)this, (SyntaxNode)null, new DiagnosticBag(), false, true);
|
||
|
|
MetadataSymbols orCreateMetadataSymbols = GetOrCreateMetadataSymbols(initialBaseline, sourceAssembly.DeclaringCompilation);
|
||
|
|
MetadataDecoder metadataDecoder = (MetadataDecoder)orCreateMetadataSymbols.MetadataDecoder;
|
||
|
|
CSharpSymbolMatcher mapToMetadata = new CSharpSymbolMatcher(otherAssembly: (PEAssemblySymbol)metadataDecoder.ModuleSymbol.ContainingAssembly, synthesizedTypes: orCreateMetadataSymbols.SynthesizedTypes, sourceAssembly: sourceAssembly, sourceContext: sourceContext);
|
||
|
|
CSharpSymbolMatcher mapToPrevious = null;
|
||
|
|
if (previousGeneration.Ordinal > 0)
|
||
|
|
{
|
||
|
|
SourceAssemblySymbol sourceAssembly2 = ((CSharpCompilation)(object)previousGeneration.Compilation).SourceAssembly;
|
||
|
|
EmitContext otherContext = default(EmitContext);
|
||
|
|
((EmitContext)(ref otherContext))._002Ector((CommonPEModuleBuilder)(object)(PEModuleBuilder)(object)previousGeneration.PEModuleBuilder, (SyntaxNode)null, new DiagnosticBag(), false, true);
|
||
|
|
mapToPrevious = new CSharpSymbolMatcher(sourceAssembly, sourceContext, sourceAssembly2, otherContext, previousGeneration.SynthesizedTypes, previousGeneration.SynthesizedMembers, previousGeneration.DeletedMembers);
|
||
|
|
}
|
||
|
|
_previousDefinitions = new CSharpDefinitionMap(edits, metadataDecoder, mapToMetadata, mapToPrevious);
|
||
|
|
_previousGeneration = previousGeneration;
|
||
|
|
_changes = (SymbolChanges)(object)new CSharpSymbolChanges((DefinitionMap)(object)_previousDefinitions, edits, isAddedSymbol);
|
||
|
|
_deepTranslator = new CSharpSymbolMatcher.DeepTranslator(sourceAssembly.GetSpecialType((SpecialType)1));
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override ITypeReference EncTranslateLocalVariableType(TypeSymbol type, DiagnosticBag diagnostics)
|
||
|
|
{
|
||
|
|
TypeSymbol typeSymbol = (TypeSymbol)_deepTranslator.Visit(type);
|
||
|
|
return ((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).Translate(typeSymbol ?? type, (SyntaxNode)null, diagnostics);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static MetadataSymbols GetOrCreateMetadataSymbols(EmitBaseline initialBaseline, CSharpCompilation compilation)
|
||
|
|
{
|
||
|
|
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_004e: Expected O, but got Unknown
|
||
|
|
if (initialBaseline.LazyMetadataSymbols != null)
|
||
|
|
{
|
||
|
|
return initialBaseline.LazyMetadataSymbols;
|
||
|
|
}
|
||
|
|
ModuleMetadata originalMetadata = initialBaseline.OriginalMetadata;
|
||
|
|
ImmutableDictionary<AssemblyIdentity, AssemblyIdentity> assemblyReferenceIdentityMap;
|
||
|
|
MetadataDecoder metadataDecoder = new MetadataDecoder(compilation.RemoveAllSyntaxTrees().GetBoundReferenceManager().CreatePEAssemblyForAssemblyMetadata(AssemblyMetadata.Create(originalMetadata), (MetadataImportOptions)2, out assemblyReferenceIdentityMap)
|
||
|
|
.PrimaryModule);
|
||
|
|
MetadataSymbols val = new MetadataSymbols(GetSynthesizedTypesFromMetadata(originalMetadata.MetadataReader, metadataDecoder), (object)metadataDecoder, assemblyReferenceIdentityMap);
|
||
|
|
return InterlockedOperations.Initialize<MetadataSymbols>(ref initialBaseline.LazyMetadataSymbols, val);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static SynthesizedTypeMaps GetSynthesizedTypesFromMetadata(MetadataReader reader, MetadataDecoder metadataDecoder)
|
||
|
|
{
|
||
|
|
//IL_0225: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0230: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_023b: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0245: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0201: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0170: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0172: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
Builder<AnonymousTypeKey, AnonymousTypeValue> val = ImmutableSegmentedDictionary.CreateBuilder<AnonymousTypeKey, AnonymousTypeValue>();
|
||
|
|
Builder<string, AnonymousTypeValue> val2 = ImmutableSegmentedDictionary.CreateBuilder<string, AnonymousTypeValue>();
|
||
|
|
Builder<SynthesizedDelegateKey, SynthesizedDelegateValue> val3 = ImmutableSegmentedDictionary.CreateBuilder<SynthesizedDelegateKey, SynthesizedDelegateValue>();
|
||
|
|
SynthesizedDelegateKey val4 = default(SynthesizedDelegateKey);
|
||
|
|
SynthesizedDelegateValue val5 = default(SynthesizedDelegateValue);
|
||
|
|
short num = default(short);
|
||
|
|
AnonymousTypeKey val6 = default(AnonymousTypeKey);
|
||
|
|
AnonymousTypeValue val7 = default(AnonymousTypeValue);
|
||
|
|
AnonymousTypeValue val8 = default(AnonymousTypeValue);
|
||
|
|
foreach (TypeDefinitionHandle typeDefinition2 in reader.TypeDefinitions)
|
||
|
|
{
|
||
|
|
TypeDefinition typeDefinition = reader.GetTypeDefinition(typeDefinition2);
|
||
|
|
if (!typeDefinition.Namespace.IsNil)
|
||
|
|
{
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
if (reader.StringComparer.StartsWith(typeDefinition.Name, "<>A") || reader.StringComparer.StartsWith(typeDefinition.Name, "<>F"))
|
||
|
|
{
|
||
|
|
((SynthesizedDelegateKey)(ref val4))._002Ector(reader.GetString(typeDefinition.Name));
|
||
|
|
NamedTypeSymbol namedTypeSymbol = (NamedTypeSymbol)((MetadataDecoder<PEModuleSymbol, TypeSymbol, MethodSymbol, FieldSymbol, Symbol>)metadataDecoder).GetTypeOfToken((EntityHandle)typeDefinition2);
|
||
|
|
((SynthesizedDelegateValue)(ref val5))._002Ector((ITypeDefinition)(object)namedTypeSymbol.GetCciAdapter());
|
||
|
|
val3.Add(val4, val5);
|
||
|
|
}
|
||
|
|
else if (reader.StringComparer.StartsWith(typeDefinition.Name, "<>f__AnonymousType"))
|
||
|
|
{
|
||
|
|
string text = MetadataHelpers.InferTypeArityAndUnmangleMetadataName(reader.GetString(typeDefinition.Name), ref num);
|
||
|
|
if (int.TryParse(text.Substring("<>f__AnonymousType".Length), NumberStyles.None, CultureInfo.InvariantCulture, out var result))
|
||
|
|
{
|
||
|
|
ArrayBuilder<AnonymousTypeKeyField> instance = ArrayBuilder<AnonymousTypeKeyField>.GetInstance();
|
||
|
|
if (TryGetAnonymousTypeKey(reader, typeDefinition, instance))
|
||
|
|
{
|
||
|
|
NamedTypeSymbol namedTypeSymbol2 = (NamedTypeSymbol)((MetadataDecoder<PEModuleSymbol, TypeSymbol, MethodSymbol, FieldSymbol, Symbol>)metadataDecoder).GetTypeOfToken((EntityHandle)typeDefinition2);
|
||
|
|
((AnonymousTypeKey)(ref val6))._002Ector(instance.ToImmutable(), false);
|
||
|
|
((AnonymousTypeValue)(ref val7))._002Ector(text, result, (ITypeDefinition)(object)namedTypeSymbol2.GetCciAdapter());
|
||
|
|
val.Add(val6, val7);
|
||
|
|
}
|
||
|
|
instance.Free();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (reader.StringComparer.StartsWith(typeDefinition.Name, "<>f__AnonymousDelegate"))
|
||
|
|
{
|
||
|
|
string text2 = MetadataHelpers.InferTypeArityAndUnmangleMetadataName(reader.GetString(typeDefinition.Name), ref num);
|
||
|
|
if (int.TryParse(text2.Substring("<>f__AnonymousDelegate".Length), NumberStyles.None, CultureInfo.InvariantCulture, out var result2))
|
||
|
|
{
|
||
|
|
NamedTypeSymbol namedTypeSymbol3 = (NamedTypeSymbol)((MetadataDecoder<PEModuleSymbol, TypeSymbol, MethodSymbol, FieldSymbol, Symbol>)metadataDecoder).GetTypeOfToken((EntityHandle)typeDefinition2);
|
||
|
|
((AnonymousTypeValue)(ref val8))._002Ector(text2, result2, (ITypeDefinition)(object)namedTypeSymbol3.GetCciAdapter());
|
||
|
|
val2.Add(text2, val8);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return new SynthesizedTypeMaps((ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue>?)val.ToImmutable(), (ImmutableSegmentedDictionary<SynthesizedDelegateKey, SynthesizedDelegateValue>?)val3.ToImmutable(), (ImmutableSegmentedDictionary<string, AnonymousTypeValue>?)val2.ToImmutable());
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool TryGetAnonymousTypeKey(MetadataReader reader, TypeDefinition def, ArrayBuilder<AnonymousTypeKeyField> builder)
|
||
|
|
{
|
||
|
|
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
foreach (GenericParameterHandle genericParameter in def.GetGenericParameters())
|
||
|
|
{
|
||
|
|
if (!GeneratedNameParser.TryParseAnonymousTypeParameterName(reader.GetString(reader.GetGenericParameter(genericParameter).Name), out string propertyName))
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
builder.Add(new AnonymousTypeKeyField(propertyName, false, false));
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public SynthesizedTypeMaps GetSynthesizedTypes()
|
||
|
|
{
|
||
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
return new SynthesizedTypeMaps((ImmutableSegmentedDictionary<AnonymousTypeKey, AnonymousTypeValue>?)((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).Compilation.AnonymousTypeManager.GetAnonymousTypeMap(), (ImmutableSegmentedDictionary<SynthesizedDelegateKey, SynthesizedDelegateValue>?)((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).Compilation.AnonymousTypeManager.GetAnonymousDelegates(), (ImmutableSegmentedDictionary<string, AnonymousTypeValue>?)((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).Compilation.AnonymousTypeManager.GetAnonymousDelegatesWithIndexedNames());
|
||
|
|
}
|
||
|
|
|
||
|
|
public override IEnumerable<INamespaceTypeDefinition> GetTopLevelTypeDefinitions(EmitContext context)
|
||
|
|
{
|
||
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
return ((CommonPEModuleBuilder)this).GetTopLevelTypeDefinitionsCore(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
public override IEnumerable<INamespaceTypeDefinition> GetTopLevelSourceTypeDefinitions(EmitContext context)
|
||
|
|
{
|
||
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
return _changes.GetTopLevelSourceTypeDefinitions(context);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override VariableSlotAllocator? TryCreateVariableSlotAllocator(MethodSymbol method, MethodSymbol topLevelMethod, DiagnosticBag diagnostics)
|
||
|
|
{
|
||
|
|
return ((DefinitionMap)_previousDefinitions).TryCreateVariableSlotAllocator(_previousGeneration, (Compilation)(object)((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).Compilation, (IMethodSymbolInternal)(object)method, (IMethodSymbolInternal)(object)topLevelMethod, diagnostics);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override MethodInstrumentation GetMethodBodyInstrumentations(MethodSymbol method)
|
||
|
|
{
|
||
|
|
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
return ((DefinitionMap)_previousDefinitions).GetMethodBodyInstrumentations((IMethodSymbolInternal)(object)method);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override ImmutableArray<AnonymousTypeKey> GetPreviousAnonymousTypes()
|
||
|
|
{
|
||
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
SynthesizedTypeMaps synthesizedTypes = _previousGeneration.SynthesizedTypes;
|
||
|
|
return ImmutableArray.CreateRange((IEnumerable<AnonymousTypeKey>)(object)((SynthesizedTypeMaps)(ref synthesizedTypes)).AnonymousTypes.Keys);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override ImmutableArray<SynthesizedDelegateKey> GetPreviousAnonymousDelegates()
|
||
|
|
{
|
||
|
|
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_000e: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
|
||
|
|
SynthesizedTypeMaps synthesizedTypes = _previousGeneration.SynthesizedTypes;
|
||
|
|
return ImmutableArray.CreateRange((IEnumerable<SynthesizedDelegateKey>)(object)((SynthesizedTypeMaps)(ref synthesizedTypes)).AnonymousDelegates.Keys);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override int GetNextAnonymousTypeIndex()
|
||
|
|
{
|
||
|
|
return _previousGeneration.GetNextAnonymousTypeIndex(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
internal override bool TryGetAnonymousTypeName(AnonymousTypeManager.AnonymousTypeTemplateSymbol template, [NotNullWhen(true)] out string? name, out int index)
|
||
|
|
{
|
||
|
|
return _previousDefinitions.TryGetAnonymousTypeName(template, out name, out index);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnCreatedIndices(DiagnosticBag diagnostics)
|
||
|
|
{
|
||
|
|
EmbeddedTypesManager embeddedTypesManagerOpt = ((PEModuleBuilder<CSharpCompilation, SourceModuleSymbol, AssemblySymbol, TypeSymbol, NamedTypeSymbol, MethodSymbol, SyntaxNode, EmbeddedTypesManager, ModuleCompilationState>)this).EmbeddedTypesManagerOpt;
|
||
|
|
if (embeddedTypesManagerOpt == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
foreach (NamedTypeSymbol key in ((EmbeddedTypesManager<PEModuleBuilder, ModuleCompilationState, EmbeddedTypesManager, SyntaxNode, CSharpAttributeData, Symbol, AssemblySymbol, NamedTypeSymbol, FieldSymbol, MethodSymbol, EventSymbol, PropertySymbol, ParameterSymbol, TypeParameterSymbol, EmbeddedType, EmbeddedField, EmbeddedMethod, EmbeddedEvent, EmbeddedProperty, EmbeddedParameter, EmbeddedTypeParameter>)embeddedTypesManagerOpt).EmbeddedTypesMap.Keys)
|
||
|
|
{
|
||
|
|
diagnostics.Add((DiagnosticInfo)(object)new CSDiagnosticInfo(ErrorCode.ERR_EncNoPIAReference, key.AdaptedSymbol), Location.None);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|