From 5d36732fcf7435975223b36d3303bbd1082dbd37 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 19 Jul 2024 15:19:21 +0200 Subject: [PATCH] Remove ApplyPdbLocalTypeInfoTypeVisitor (merge it into ApplyAttributeTypeVisitor) --- .../ICSharpCode.Decompiler.csproj | 1 - .../IL/ApplyPdbLocalTypeInfoTypeVisitor.cs | 159 ------------------ ICSharpCode.Decompiler/IL/ILReader.cs | 2 +- .../TypeSystem/ApplyAttributeTypeVisitor.cs | 8 + 4 files changed, 9 insertions(+), 161 deletions(-) delete mode 100644 ICSharpCode.Decompiler/IL/ApplyPdbLocalTypeInfoTypeVisitor.cs diff --git a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj index 3fdc756e7..ba3a308e8 100644 --- a/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj +++ b/ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj @@ -97,7 +97,6 @@ - diff --git a/ICSharpCode.Decompiler/IL/ApplyPdbLocalTypeInfoTypeVisitor.cs b/ICSharpCode.Decompiler/IL/ApplyPdbLocalTypeInfoTypeVisitor.cs deleted file mode 100644 index 1d8796c1e..000000000 --- a/ICSharpCode.Decompiler/IL/ApplyPdbLocalTypeInfoTypeVisitor.cs +++ /dev/null @@ -1,159 +0,0 @@ -using System; -using System.Collections.Immutable; - -using ICSharpCode.Decompiler.DebugInfo; -using ICSharpCode.Decompiler.TypeSystem; -using ICSharpCode.Decompiler.TypeSystem.Implementation; - -namespace ICSharpCode.Decompiler.IL -{ - /// - /// Heavily based on - /// - sealed class ApplyPdbLocalTypeInfoTypeVisitor : TypeVisitor - { - private readonly bool[] dynamicData; - private readonly string[] tupleElementNames; - private int dynamicTypeIndex = 0; - private int tupleTypeIndex = 0; - - private ApplyPdbLocalTypeInfoTypeVisitor(bool[] dynamicData, string[] tupleElementNames) - { - this.dynamicData = dynamicData; - this.tupleElementNames = tupleElementNames; - } - - public static IType Apply(IType type, PdbExtraTypeInfo pdbExtraTypeInfo) - { - if (pdbExtraTypeInfo.DynamicFlags is null && pdbExtraTypeInfo.TupleElementNames is null) - return type; - return type.AcceptVisitor(new ApplyPdbLocalTypeInfoTypeVisitor(pdbExtraTypeInfo.DynamicFlags, pdbExtraTypeInfo.TupleElementNames)); - } - - public override IType VisitModOpt(ModifiedType type) - { - dynamicTypeIndex++; - return base.VisitModOpt(type); - } - - public override IType VisitModReq(ModifiedType type) - { - dynamicTypeIndex++; - return base.VisitModReq(type); - } - - public override IType VisitPointerType(PointerType type) - { - dynamicTypeIndex++; - return base.VisitPointerType(type); - } - - public override IType VisitArrayType(ArrayType type) - { - dynamicTypeIndex++; - return base.VisitArrayType(type); - } - - public override IType VisitByReferenceType(ByReferenceType type) - { - dynamicTypeIndex++; - return base.VisitByReferenceType(type); - } - - public override IType VisitTupleType(TupleType type) - { - if (tupleElementNames != null && tupleTypeIndex < tupleElementNames.Length) - { - int tupleCardinality = type.Cardinality; - string[] extractedValues = new string[tupleCardinality]; - Array.Copy(tupleElementNames, tupleTypeIndex, extractedValues, 0, - Math.Min(tupleCardinality, tupleElementNames.Length - tupleTypeIndex)); - var elementNames = ImmutableArray.CreateRange(extractedValues); - tupleTypeIndex += tupleCardinality; - - int level = 0; - var elementTypes = new IType[type.ElementTypes.Length]; - for (int i = 0; i < type.ElementTypes.Length; i++) - { - dynamicTypeIndex++; - IType elementType = type.ElementTypes[i]; - if (i != 0 && (i - level) % TupleType.RestPosition == 0 && elementType is TupleType tuple) - { - tupleTypeIndex += tuple.Cardinality; - level++; - } - elementTypes[i] = elementType.AcceptVisitor(this); - } - - return new TupleType( - type.Compilation, - elementTypes.ToImmutableArray(), - elementNames, - type.GetDefinition()?.ParentModule - ); - } - return base.VisitTupleType(type); - } - - public override IType VisitParameterizedType(ParameterizedType type) - { - if (TupleType.IsTupleCompatible(type, out var tupleCardinality)) - tupleTypeIndex += tupleCardinality; - // Visit generic type and type arguments. - // Like base implementation, except that it increments dynamicTypeIndex. - var genericType = type.GenericType.AcceptVisitor(this); - bool changed = type.GenericType != genericType; - var arguments = new IType[type.TypeArguments.Count]; - for (int i = 0; i < type.TypeArguments.Count; i++) - { - dynamicTypeIndex++; - arguments[i] = type.TypeArguments[i].AcceptVisitor(this); - changed = changed || arguments[i] != type.TypeArguments[i]; - } - if (!changed) - return type; - return new ParameterizedType(genericType, arguments); - } - - public override IType VisitFunctionPointerType(FunctionPointerType type) - { - dynamicTypeIndex++; - if (type.ReturnIsRefReadOnly) - { - dynamicTypeIndex++; - } - var returnType = type.ReturnType.AcceptVisitor(this); - bool changed = type.ReturnType != returnType; - var parameters = new IType[type.ParameterTypes.Length]; - for (int i = 0; i < parameters.Length; i++) - { - dynamicTypeIndex += type.ParameterReferenceKinds[i] switch { - ReferenceKind.None => 1, - ReferenceKind.Ref => 1, - ReferenceKind.Out => 2, // in/out also count the modreq - ReferenceKind.In => 2, - _ => throw new NotSupportedException() - }; - parameters[i] = type.ParameterTypes[i].AcceptVisitor(this); - changed = changed || parameters[i] != type.ParameterTypes[i]; - } - if (!changed) - return type; - return type.WithSignature(returnType, parameters.ToImmutableArray()); - } - - public override IType VisitTypeDefinition(ITypeDefinition type) - { - IType newType = type; - var ktc = type.KnownTypeCode; - if (ktc == KnownTypeCode.Object && dynamicData is not null) - { - if (dynamicTypeIndex >= dynamicData.Length) - newType = SpecialType.Dynamic; - else if (dynamicData[dynamicTypeIndex]) - newType = SpecialType.Dynamic; - } - return newType; - } - } -} diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 46038f2e4..2ec9da40a 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -308,7 +308,7 @@ namespace ICSharpCode.Decompiler.IL if (UseDebugSymbols && DebugInfo is not null && DebugInfo.TryGetExtraTypeInfo((MethodDefinitionHandle)method.MetadataToken, index, out var pdbExtraTypeInfo)) { - type = ApplyPdbLocalTypeInfoTypeVisitor.Apply(type, pdbExtraTypeInfo); + type = ApplyAttributeTypeVisitor.ApplyAttributesToType(type, compilation, module.TypeSystemOptions, pdbExtraTypeInfo); } ILVariable ilVar = new ILVariable(kind, type, index); diff --git a/ICSharpCode.Decompiler/TypeSystem/ApplyAttributeTypeVisitor.cs b/ICSharpCode.Decompiler/TypeSystem/ApplyAttributeTypeVisitor.cs index 6734096b6..76418f3b2 100644 --- a/ICSharpCode.Decompiler/TypeSystem/ApplyAttributeTypeVisitor.cs +++ b/ICSharpCode.Decompiler/TypeSystem/ApplyAttributeTypeVisitor.cs @@ -21,6 +21,7 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Linq; +using ICSharpCode.Decompiler.DebugInfo; using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.Util; @@ -163,6 +164,13 @@ namespace ICSharpCode.Decompiler.TypeSystem } } + public static IType ApplyAttributesToType(IType inputType, ICompilation compilation, TypeSystemOptions options, PdbExtraTypeInfo pdbExtraTypeInfo) + { + if (pdbExtraTypeInfo.DynamicFlags is null && pdbExtraTypeInfo.TupleElementNames is null) + return inputType; + return inputType.AcceptVisitor(new ApplyAttributeTypeVisitor(compilation, pdbExtraTypeInfo.DynamicFlags != null, pdbExtraTypeInfo.DynamicFlags, false, null, options, pdbExtraTypeInfo.TupleElementNames, Nullability.Oblivious, null)); + } + readonly ICompilation compilation; readonly bool hasDynamicAttribute; readonly bool[] dynamicAttributeData;