|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace AsmResolver.DotNet.Signatures.Parsing; |
| 6 | + |
| 7 | +internal sealed class ParsedTypeFullName(TypeName name) |
| 8 | +{ |
| 9 | + public TypeName Name { get; } = name; |
| 10 | + |
| 11 | + public IList<TypeAnnotation> Annotations { get; } = new List<TypeAnnotation>(); |
| 12 | + |
| 13 | + public IResolutionScope? Scope { get; set; } |
| 14 | + |
| 15 | + public TypeSignature ToTypeSignature(ModuleDefinition contextModule) |
| 16 | + { |
| 17 | + var baseTypeDefOrRef = Name.ToTypeDefOrRef(contextModule, Scope); |
| 18 | + |
| 19 | + // The first annotation may be a generic instantiation, of which the root typesig is represented by |
| 20 | + // GenericInstanceTypeSignature. |
| 21 | + int startIndex = 0; |
| 22 | + TypeSignature signature; |
| 23 | + if (Annotations.Count > 0 && Annotations[0] is {Kind: TypeAnnotationType.GenericInstance} first) |
| 24 | + { |
| 25 | + var arguments = new TypeSignature[first.TypeArguments.Count]; |
| 26 | + for (int i = 0; i < first.TypeArguments.Count; i++) |
| 27 | + arguments[i] = first.TypeArguments[i].ToTypeSignature(contextModule); |
| 28 | + |
| 29 | + signature = baseTypeDefOrRef.MakeGenericInstanceType(arguments); |
| 30 | + startIndex++; |
| 31 | + } |
| 32 | + else |
| 33 | + { |
| 34 | + signature = baseTypeDefOrRef.ToTypeSignature(); |
| 35 | + } |
| 36 | + |
| 37 | + // Go over all type annotations and annotate the type signature accordingly. |
| 38 | + for (int i = startIndex; i < Annotations.Count; i++) |
| 39 | + { |
| 40 | + signature = Annotations[i].Kind switch |
| 41 | + { |
| 42 | + TypeAnnotationType.ByReference => signature.MakeByReferenceType(), |
| 43 | + TypeAnnotationType.Pointer => signature.MakePointerType(), |
| 44 | + TypeAnnotationType.SzArray => signature.MakeSzArrayType(), |
| 45 | + TypeAnnotationType.Array => signature.MakeArrayType(Annotations[i].Dimensions.ToArray()), |
| 46 | + TypeAnnotationType.GenericInstance => throw new FormatException("Cannot instantiate a non-generic type."), |
| 47 | + _ => throw new ArgumentOutOfRangeException() |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + return signature; |
| 52 | + } |
| 53 | +} |
0 commit comments