Files
2026-08-27 10:56:38 -06:00

244 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Metadata;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
using Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Symbols;
namespace Microsoft.CodeAnalysis.CSharp.Emit;
internal sealed class CSharpDefinitionMap : DefinitionMap
{
private readonly MetadataDecoder _metadataDecoder;
private readonly CSharpSymbolMatcher _mapToMetadata;
private readonly CSharpSymbolMatcher _mapToPrevious;
protected override SymbolMatcher MapToMetadataSymbolMatcher => (SymbolMatcher)(object)_mapToMetadata;
protected override SymbolMatcher MapToPreviousSymbolMatcher => (SymbolMatcher)(object)_mapToPrevious;
internal override CommonMessageProvider MessageProvider => (CommonMessageProvider)(object)Microsoft.CodeAnalysis.CSharp.MessageProvider.Instance;
public CSharpDefinitionMap(IEnumerable<SemanticEdit> edits, MetadataDecoder metadataDecoder, CSharpSymbolMatcher mapToMetadata, CSharpSymbolMatcher? mapToPrevious)
: base(edits)
{
_metadataDecoder = metadataDecoder;
_mapToMetadata = mapToMetadata;
_mapToPrevious = mapToPrevious ?? mapToMetadata;
}
protected override ISymbolInternal? GetISymbolInternalOrNull(ISymbol symbol)
{
return (ISymbolInternal?)(object)(symbol as Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.Symbol)?.UnderlyingSymbol;
}
protected override LambdaSyntaxFacts GetLambdaSyntaxFacts()
{
return CSharpLambdaSyntaxFacts.Instance;
}
internal bool TryGetAnonymousTypeName(AnonymousTypeManager.AnonymousTypeTemplateSymbol template, [NotNullWhen(true)] out string? name, out int index)
{
return _mapToPrevious.TryGetAnonymousTypeName(template, out name, out index);
}
internal override bool TryGetTypeHandle(ITypeDefinition def, out TypeDefinitionHandle handle)
{
IDefinition obj = ((SymbolMatcher)_mapToMetadata).MapDefinition((IDefinition)(object)def);
if (((obj != null) ? ((IReference)obj).GetInternalSymbol() : null) is PENamedTypeSymbol pENamedTypeSymbol)
{
handle = pENamedTypeSymbol.Handle;
return true;
}
handle = default(TypeDefinitionHandle);
return false;
}
internal override bool TryGetEventHandle(IEventDefinition def, out EventDefinitionHandle handle)
{
IDefinition obj = ((SymbolMatcher)_mapToMetadata).MapDefinition((IDefinition)(object)def);
if (((obj != null) ? ((IReference)obj).GetInternalSymbol() : null) is PEEventSymbol pEEventSymbol)
{
handle = pEEventSymbol.Handle;
return true;
}
handle = default(EventDefinitionHandle);
return false;
}
internal override bool TryGetFieldHandle(IFieldDefinition def, out FieldDefinitionHandle handle)
{
IDefinition obj = ((SymbolMatcher)_mapToMetadata).MapDefinition((IDefinition)(object)def);
if (((obj != null) ? ((IReference)obj).GetInternalSymbol() : null) is PEFieldSymbol pEFieldSymbol)
{
handle = pEFieldSymbol.Handle;
return true;
}
handle = default(FieldDefinitionHandle);
return false;
}
internal override bool TryGetMethodHandle(IMethodDefinition def, out MethodDefinitionHandle handle)
{
IDefinition obj = ((SymbolMatcher)_mapToMetadata).MapDefinition((IDefinition)(object)def);
if (((obj != null) ? ((IReference)obj).GetInternalSymbol() : null) is PEMethodSymbol pEMethodSymbol)
{
handle = pEMethodSymbol.Handle;
return true;
}
handle = default(MethodDefinitionHandle);
return false;
}
internal override bool TryGetPropertyHandle(IPropertyDefinition def, out PropertyDefinitionHandle handle)
{
IDefinition obj = ((SymbolMatcher)_mapToMetadata).MapDefinition((IDefinition)(object)def);
if (((obj != null) ? ((IReference)obj).GetInternalSymbol() : null) is PEPropertySymbol pEPropertySymbol)
{
handle = pEPropertySymbol.Handle;
return true;
}
handle = default(PropertyDefinitionHandle);
return false;
}
protected override void GetStateMachineFieldMapFromMetadata(ITypeSymbolInternal stateMachineType, ImmutableArray<LocalSlotDebugInfo> localSlotDebugInfo, out IReadOnlyDictionary<EncHoistedLocalInfo, int> hoistedLocalMap, out IReadOnlyDictionary<ITypeReference, int> awaiterMap, out int awaiterSlotCount)
{
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Invalid comparison between Unknown and I4
//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_00f2: Expected O, but got Unknown
//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
//IL_009c: Unknown result type (might be due to invalid IL or missing references)
//IL_00a8: Expected O, but got Unknown
Dictionary<EncHoistedLocalInfo, int> dictionary = new Dictionary<EncHoistedLocalInfo, int>();
Dictionary<ITypeReference, int> dictionary2 = new Dictionary<ITypeReference, int>((IEqualityComparer<ITypeReference>?)SymbolEquivalentEqualityComparer.Instance);
int num = -1;
ImmutableArray<Symbol>.Enumerator enumerator = ((Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol)(object)stateMachineType).GetMembers().GetEnumerator();
EncHoistedLocalInfo key = default(EncHoistedLocalInfo);
while (enumerator.MoveNext())
{
Symbol current = enumerator.Current;
if ((int)current.Kind != 6)
{
continue;
}
string name = current.Name;
int slotIndex;
switch (GeneratedNameParser.GetKind(name))
{
case GeneratedNameKind.AwaiterField:
if (GeneratedNameParser.TryParseSlotIndex(name, out slotIndex))
{
Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol fieldSymbol2 = (Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol)current;
dictionary2[(ITypeReference)fieldSymbol2.Type.GetCciAdapter()] = slotIndex;
if (slotIndex > num)
{
num = slotIndex;
}
}
break;
case GeneratedNameKind.HoistedLocalField:
case GeneratedNameKind.DisplayClassLocalOrField:
case GeneratedNameKind.HoistedSynthesizedLocalField:
if (GeneratedNameParser.TryParseSlotIndex(name, out slotIndex))
{
Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol fieldSymbol = (Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol)current;
if (slotIndex < localSlotDebugInfo.Length)
{
((EncHoistedLocalInfo)(ref key))._002Ector(localSlotDebugInfo[slotIndex], (ITypeReference)fieldSymbol.Type.GetCciAdapter());
dictionary[key] = slotIndex;
}
}
break;
}
}
hoistedLocalMap = dictionary;
awaiterMap = dictionary2;
awaiterSlotCount = num + 1;
}
protected override ImmutableArray<EncLocalInfo> GetLocalSlotMapFromMetadata(StandaloneSignatureHandle handle, EditAndContinueMethodDebugInformation debugInfo)
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
ImmutableArray<LocalInfo<Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol>> localsOrThrow = ((MetadataDecoder<PEModuleSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol, Symbol>)_metadataDecoder).GetLocalsOrThrow(handle);
return CreateLocalSlotMap(debugInfo, localsOrThrow);
}
protected override ITypeSymbolInternal? TryGetStateMachineType(MethodDefinitionHandle methodHandle)
{
string text = default(string);
if (!((MetadataDecoder<PEModuleSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.FieldSymbol, Symbol>)_metadataDecoder).Module.HasStateMachineAttribute(methodHandle, ref text))
{
return null;
}
return (ITypeSymbolInternal?)(object)((TypeNameDecoder<PEModuleSymbol, Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol>)(object)_metadataDecoder).GetTypeSymbolForSerializedType(text);
}
private static ImmutableArray<EncLocalInfo> CreateLocalSlotMap(EditAndContinueMethodDebugInformation methodEncInfo, ImmutableArray<LocalInfo<Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol>> slotMetadata)
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: 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_004a: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_0109: Unknown result type (might be due to invalid IL or missing references)
//IL_0113: Unknown result type (might be due to invalid IL or missing references)
//IL_0118: Unknown result type (might be due to invalid IL or missing references)
//IL_005c: Unknown result type (might be due to invalid IL or missing references)
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_0073: Unknown result type (might be due to invalid IL or missing references)
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_0081: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_0099: Expected O, but got Unknown
//IL_009a: Unknown result type (might be due to invalid IL or missing references)
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
EncLocalInfo[] array = (EncLocalInfo[])(object)new EncLocalInfo[slotMetadata.Length];
ImmutableArray<LocalSlotDebugInfo> localSlots = methodEncInfo.LocalSlots;
if (!localSlots.IsDefault)
{
int num = Math.Min(localSlots.Length, slotMetadata.Length);
Dictionary<EncLocalInfo, int> dictionary = new Dictionary<EncLocalInfo, int>();
EncLocalInfo key = default(EncLocalInfo);
for (int i = 0; i < num; i++)
{
LocalSlotDebugInfo val = localSlots[i];
if (SynthesizedLocalKindExtensions.IsLongLived(val.SynthesizedKind))
{
LocalInfo<Microsoft.CodeAnalysis.CSharp.Symbols.TypeSymbol> val2 = slotMetadata[i];
if (val2.CustomModifiers.IsDefaultOrEmpty)
{
((EncLocalInfo)(ref key))._002Ector(val, (ITypeReference)val2.Type.GetCciAdapter(), val2.Constraints, val2.SignatureOpt);
dictionary.Add(key, i);
}
}
}
foreach (KeyValuePair<EncLocalInfo, int> item in dictionary)
{
array[item.Value] = item.Key;
}
}
for (int j = 0; j < array.Length; j++)
{
if (((EncLocalInfo)(ref array[j])).IsDefault)
{
array[j] = new EncLocalInfo(slotMetadata[j].SignatureOpt);
}
}
return ImmutableArray.Create(array);
}
}