|
| 1 | +using System; |
| 2 | +using System.Collections.Immutable; |
| 3 | + |
| 4 | +using ICSharpCode.Decompiler.DebugInfo; |
| 5 | +using ICSharpCode.Decompiler.TypeSystem; |
| 6 | +using ICSharpCode.Decompiler.TypeSystem.Implementation; |
| 7 | + |
| 8 | +namespace ICSharpCode.Decompiler.IL |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Heavily based on <see cref="ApplyAttributeTypeVisitor"/> |
| 12 | + /// </summary> |
| 13 | + sealed class ApplyPdbLocalTypeInfoTypeVisitor : TypeVisitor |
| 14 | + { |
| 15 | + private readonly bool[] dynamicData; |
| 16 | + private readonly string[] tupleElementNames; |
| 17 | + private int dynamicTypeIndex = 0; |
| 18 | + private int tupleTypeIndex = 0; |
| 19 | + |
| 20 | + private ApplyPdbLocalTypeInfoTypeVisitor(bool[] dynamicData, string[] tupleElementNames) |
| 21 | + { |
| 22 | + this.dynamicData = dynamicData; |
| 23 | + this.tupleElementNames = tupleElementNames; |
| 24 | + } |
| 25 | + |
| 26 | + public static IType Apply(IType type, PdbExtraTypeInfo pdbExtraTypeInfo) |
| 27 | + { |
| 28 | + if (pdbExtraTypeInfo.DynamicFlags is null && pdbExtraTypeInfo.TupleElementNames is null) |
| 29 | + return type; |
| 30 | + return type.AcceptVisitor(new ApplyPdbLocalTypeInfoTypeVisitor(pdbExtraTypeInfo.DynamicFlags, pdbExtraTypeInfo.TupleElementNames)); |
| 31 | + } |
| 32 | + |
| 33 | + public override IType VisitModOpt(ModifiedType type) |
| 34 | + { |
| 35 | + dynamicTypeIndex++; |
| 36 | + return base.VisitModOpt(type); |
| 37 | + } |
| 38 | + |
| 39 | + public override IType VisitModReq(ModifiedType type) |
| 40 | + { |
| 41 | + dynamicTypeIndex++; |
| 42 | + return base.VisitModReq(type); |
| 43 | + } |
| 44 | + |
| 45 | + public override IType VisitPointerType(PointerType type) |
| 46 | + { |
| 47 | + dynamicTypeIndex++; |
| 48 | + return base.VisitPointerType(type); |
| 49 | + } |
| 50 | + |
| 51 | + public override IType VisitArrayType(ArrayType type) |
| 52 | + { |
| 53 | + dynamicTypeIndex++; |
| 54 | + return base.VisitArrayType(type); |
| 55 | + } |
| 56 | + |
| 57 | + public override IType VisitByReferenceType(ByReferenceType type) |
| 58 | + { |
| 59 | + dynamicTypeIndex++; |
| 60 | + return base.VisitByReferenceType(type); |
| 61 | + } |
| 62 | + |
| 63 | + public override IType VisitTupleType(TupleType type) |
| 64 | + { |
| 65 | + if (tupleElementNames != null && tupleTypeIndex < tupleElementNames.Length) |
| 66 | + { |
| 67 | + int tupleCardinality = type.Cardinality; |
| 68 | + string[] extractedValues = new string[tupleCardinality]; |
| 69 | + Array.Copy(tupleElementNames, tupleTypeIndex, extractedValues, 0, |
| 70 | + Math.Min(tupleCardinality, tupleElementNames.Length - tupleTypeIndex)); |
| 71 | + var elementNames = ImmutableArray.CreateRange(extractedValues); |
| 72 | + tupleTypeIndex += tupleCardinality; |
| 73 | + |
| 74 | + int level = 0; |
| 75 | + var elementTypes = new IType[type.ElementTypes.Length]; |
| 76 | + for (int i = 0; i < type.ElementTypes.Length; i++) |
| 77 | + { |
| 78 | + dynamicTypeIndex++; |
| 79 | + IType elementType = type.ElementTypes[i]; |
| 80 | + if (i != 0 && (i - level) % TupleType.RestPosition == 0 && elementType is TupleType tuple) |
| 81 | + { |
| 82 | + tupleTypeIndex += tuple.Cardinality; |
| 83 | + level++; |
| 84 | + } |
| 85 | + elementTypes[i] = elementType.AcceptVisitor(this); |
| 86 | + } |
| 87 | + |
| 88 | + return new TupleType( |
| 89 | + type.Compilation, |
| 90 | + elementTypes.ToImmutableArray(), |
| 91 | + elementNames, |
| 92 | + type.GetDefinition()?.ParentModule |
| 93 | + ); |
| 94 | + } |
| 95 | + return base.VisitTupleType(type); |
| 96 | + } |
| 97 | + |
| 98 | + public override IType VisitParameterizedType(ParameterizedType type) |
| 99 | + { |
| 100 | + if (TupleType.IsTupleCompatible(type, out var tupleCardinality)) |
| 101 | + tupleTypeIndex += tupleCardinality; |
| 102 | + // Visit generic type and type arguments. |
| 103 | + // Like base implementation, except that it increments dynamicTypeIndex. |
| 104 | + var genericType = type.GenericType.AcceptVisitor(this); |
| 105 | + bool changed = type.GenericType != genericType; |
| 106 | + var arguments = new IType[type.TypeArguments.Count]; |
| 107 | + for (int i = 0; i < type.TypeArguments.Count; i++) |
| 108 | + { |
| 109 | + dynamicTypeIndex++; |
| 110 | + arguments[i] = type.TypeArguments[i].AcceptVisitor(this); |
| 111 | + changed = changed || arguments[i] != type.TypeArguments[i]; |
| 112 | + } |
| 113 | + if (!changed) |
| 114 | + return type; |
| 115 | + return new ParameterizedType(genericType, arguments); |
| 116 | + } |
| 117 | + |
| 118 | + public override IType VisitFunctionPointerType(FunctionPointerType type) |
| 119 | + { |
| 120 | + dynamicTypeIndex++; |
| 121 | + if (type.ReturnIsRefReadOnly) |
| 122 | + { |
| 123 | + dynamicTypeIndex++; |
| 124 | + } |
| 125 | + var returnType = type.ReturnType.AcceptVisitor(this); |
| 126 | + bool changed = type.ReturnType != returnType; |
| 127 | + var parameters = new IType[type.ParameterTypes.Length]; |
| 128 | + for (int i = 0; i < parameters.Length; i++) |
| 129 | + { |
| 130 | + dynamicTypeIndex += type.ParameterReferenceKinds[i] switch { |
| 131 | + ReferenceKind.None => 1, |
| 132 | + ReferenceKind.Ref => 1, |
| 133 | + ReferenceKind.Out => 2, // in/out also count the modreq |
| 134 | + ReferenceKind.In => 2, |
| 135 | + _ => throw new NotSupportedException() |
| 136 | + }; |
| 137 | + parameters[i] = type.ParameterTypes[i].AcceptVisitor(this); |
| 138 | + changed = changed || parameters[i] != type.ParameterTypes[i]; |
| 139 | + } |
| 140 | + if (!changed) |
| 141 | + return type; |
| 142 | + return type.WithSignature(returnType, parameters.ToImmutableArray()); |
| 143 | + } |
| 144 | + |
| 145 | + public override IType VisitTypeDefinition(ITypeDefinition type) |
| 146 | + { |
| 147 | + IType newType = type; |
| 148 | + var ktc = type.KnownTypeCode; |
| 149 | + if (ktc == KnownTypeCode.Object && dynamicData is not null) |
| 150 | + { |
| 151 | + if (dynamicTypeIndex >= dynamicData.Length) |
| 152 | + newType = SpecialType.Dynamic; |
| 153 | + else if (dynamicData[dynamicTypeIndex]) |
| 154 | + newType = SpecialType.Dynamic; |
| 155 | + } |
| 156 | + return newType; |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments