-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into dependabot/nuget/development/xunit.ru…
…nner.visualstudio-3.0.2
- Loading branch information
Showing
26 changed files
with
305 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/AsmResolver.DotNet/Signatures/Parsing/ParsedTypeFullName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace AsmResolver.DotNet.Signatures.Parsing; | ||
|
||
internal sealed class ParsedTypeFullName(TypeName name) | ||
{ | ||
public TypeName Name { get; } = name; | ||
|
||
public IList<TypeAnnotation> Annotations { get; } = new List<TypeAnnotation>(); | ||
|
||
public IResolutionScope? Scope { get; set; } | ||
|
||
public TypeSignature ToTypeSignature(ModuleDefinition contextModule) | ||
{ | ||
var baseTypeDefOrRef = Name.ToTypeDefOrRef(contextModule, Scope); | ||
|
||
// The first annotation may be a generic instantiation, of which the root typesig is represented by | ||
// GenericInstanceTypeSignature. | ||
int startIndex = 0; | ||
TypeSignature signature; | ||
if (Annotations.Count > 0 && Annotations[0] is {Kind: TypeAnnotationType.GenericInstance} first) | ||
{ | ||
var arguments = new TypeSignature[first.TypeArguments.Count]; | ||
for (int i = 0; i < first.TypeArguments.Count; i++) | ||
arguments[i] = first.TypeArguments[i].ToTypeSignature(contextModule); | ||
|
||
signature = baseTypeDefOrRef.MakeGenericInstanceType(arguments); | ||
startIndex++; | ||
} | ||
else | ||
{ | ||
signature = baseTypeDefOrRef.ToTypeSignature(); | ||
} | ||
|
||
// Go over all type annotations and annotate the type signature accordingly. | ||
for (int i = startIndex; i < Annotations.Count; i++) | ||
{ | ||
signature = Annotations[i].Kind switch | ||
{ | ||
TypeAnnotationType.ByReference => signature.MakeByReferenceType(), | ||
TypeAnnotationType.Pointer => signature.MakePointerType(), | ||
TypeAnnotationType.SzArray => signature.MakeSzArrayType(), | ||
TypeAnnotationType.Array => signature.MakeArrayType(Annotations[i].Dimensions.ToArray()), | ||
TypeAnnotationType.GenericInstance => throw new FormatException("Cannot instantiate a non-generic type."), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
|
||
return signature; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/AsmResolver.DotNet/Signatures/Parsing/TypeAnnotation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace AsmResolver.DotNet.Signatures.Parsing; | ||
|
||
internal readonly struct TypeAnnotation | ||
{ | ||
private readonly object? _data; | ||
|
||
public TypeAnnotation(TypeAnnotationType kind) | ||
{ | ||
Kind = kind; | ||
_data = null; | ||
} | ||
|
||
public TypeAnnotation(IList<ArrayDimension> dimensions) | ||
{ | ||
Kind = TypeAnnotationType.Array; | ||
_data = dimensions; | ||
} | ||
|
||
public TypeAnnotation(IList<ParsedTypeFullName> typeArguments) | ||
{ | ||
Kind = TypeAnnotationType.GenericInstance; | ||
_data = typeArguments; | ||
} | ||
|
||
public TypeAnnotationType Kind { get; } | ||
|
||
public IList<ArrayDimension> Dimensions => (IList<ArrayDimension>) _data!; | ||
|
||
public IList<ParsedTypeFullName> TypeArguments => (IList<ParsedTypeFullName>) _data!; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/AsmResolver.DotNet/Signatures/Parsing/TypeAnnotationType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace AsmResolver.DotNet.Signatures.Parsing; | ||
|
||
internal enum TypeAnnotationType | ||
{ | ||
ByReference, | ||
Pointer, | ||
SzArray, | ||
Array, | ||
GenericInstance, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace AsmResolver.DotNet.Signatures.Parsing; | ||
|
||
internal readonly struct TypeName(string? ns, IList<string> names) | ||
{ | ||
public string? Namespace { get; } = ns; | ||
|
||
public IList<string> Names { get; } = names; | ||
|
||
public ITypeDefOrRef ToTypeDefOrRef(ModuleDefinition contextModule, IResolutionScope? scope) | ||
{ | ||
// Short circuit corlib types to avoid allocations. | ||
if (Names.Count == 1 && contextModule.CorLibTypeFactory.FromName(Namespace, Names[0]) is {} corlibType) | ||
return corlibType.Type; | ||
|
||
var type = new TypeReference(contextModule, scope, Namespace, Names[0]); | ||
|
||
// If the scope is null, it means it was omitted from the fully qualified type name. | ||
// In this case, the CLR first looks into the current assembly, and then into corlib. | ||
if (scope is null) | ||
{ | ||
// First look into the current module. | ||
type.Scope = contextModule; | ||
var definition = type.Resolve(); | ||
if (definition is null) | ||
{ | ||
// If that fails, try corlib. | ||
type.Scope = contextModule.CorLibTypeFactory.CorLibScope; | ||
definition = type.Resolve(); | ||
|
||
// If both lookups fail, revert to the normal module as scope as a fallback. | ||
if (definition is null) | ||
type.Scope = contextModule; | ||
} | ||
} | ||
|
||
// Walk over nested type names. | ||
for (int i = 1; i < Names.Count; i++) | ||
type = new TypeReference(type, null, Names[i]); | ||
|
||
return type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.