diff --git a/Readme.md b/Readme.md index 89a5dc6..4a4831a 100644 --- a/Readme.md +++ b/Readme.md @@ -11,7 +11,7 @@ import DotNetMetadata let context = AssemblyLoadContext() let assembly = try context.load(path: #"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll"#) -let typeDefinition = assembly.findTypeDefinition(fullName: "System.IDisposable")! +let typeDefinition = assembly.resolveTypeDefinition(fullName: "System.IDisposable")! print("interface \(typeDefinition.name) {") for method in typeDefinition.methods { print(" void \(method.name)()") diff --git a/Sources/DotNetMetadata/Assembly+resolve.swift b/Sources/DotNetMetadata/Assembly+resolve.swift index 49eb24b..266afd4 100644 --- a/Sources/DotNetMetadata/Assembly+resolve.swift +++ b/Sources/DotNetMetadata/Assembly+resolve.swift @@ -49,10 +49,10 @@ extension Assembly { case let .module(index): // Assume single-module assembly guard index?.zeroBased == 0 else { break } - return findTypeDefinition(namespace: namespace, name: name)! + return try resolveTypeDefinition(namespace: namespace, name: name)! case let .assemblyRef(index): guard let index = index else { break } - return try resolve(index).findTypeDefinition(namespace: namespace, name: name)! + return try resolve(index).resolveTypeDefinition(namespace: namespace, name: name)! default: fatalError("Not implemented: resolution scope \(row.resolutionScope)") } diff --git a/Sources/DotNetMetadata/Assembly.swift b/Sources/DotNetMetadata/Assembly.swift index 0fdf591..add13a9 100644 --- a/Sources/DotNetMetadata/Assembly.swift +++ b/Sources/DotNetMetadata/Assembly.swift @@ -85,18 +85,6 @@ public class Assembly: CustomDebugStringConvertible { return dict }() - public func findTypeDefinition(fullName: String) -> TypeDefinition? { - typeDefinitionsByFullName[fullName] - } - - public func findTypeDefinition(namespace: String?, name: String) -> TypeDefinition? { - findTypeDefinition(fullName: makeFullTypeName(namespace: namespace, name: name)) - } - - public func findTypeDefinition(namespace: String?, enclosingName: String, nestedNames: [String]) -> TypeDefinition? { - findTypeDefinition(fullName: makeFullTypeName(namespace: namespace, enclosingName: enclosingName, nestedNames: nestedNames)) - } - public private(set) lazy var exportedTypesByFullName: [String: ExportedType] = { let exportedTypes = exportedTypes var dict = [String: ExportedType](minimumCapacity: exportedTypes.count) @@ -106,6 +94,22 @@ public class Assembly: CustomDebugStringConvertible { return dict }() + public func resolveTypeDefinition(fullName: String, allowForwarding: Bool = true) throws -> TypeDefinition? { + if let typeDefinition = typeDefinitionsByFullName[fullName] { return typeDefinition } + if let exportedType = exportedTypesByFullName[fullName] { return try exportedType.definition } + return nil + } + + public func resolveTypeDefinition(namespace: String?, name: String, allowForwarding: Bool = true) throws -> TypeDefinition? { + let fullName = makeFullTypeName(namespace: namespace, name: name) + return try resolveTypeDefinition(fullName: fullName, allowForwarding: allowForwarding) + } + + public func resolveTypeDefinition(namespace: String?, enclosingName: String, nestedNames: [String], allowForwarding: Bool = true) throws -> TypeDefinition? { + let fullName = makeFullTypeName(namespace: namespace, enclosingName: enclosingName, nestedNames: nestedNames) + return try resolveTypeDefinition(fullName: fullName, allowForwarding: allowForwarding) + } + internal lazy var mscorlib: Mscorlib = { if let mscorlib = self as? Mscorlib { return mscorlib diff --git a/Sources/DotNetMetadata/Attribute.swift b/Sources/DotNetMetadata/Attribute.swift index 64fad5c..6ce46c1 100644 --- a/Sources/DotNetMetadata/Attribute.swift +++ b/Sources/DotNetMetadata/Attribute.swift @@ -64,7 +64,7 @@ public final class Attribute { // > it is permitted to omit the assembly-name, version, culture and public-key-token. assembly = self.assembly } - return .type(definition: assembly.findTypeDefinition(fullName: fullName)!) + return .type(definition: try assembly.resolveTypeDefinition(fullName: fullName)!) case .array(let elems): return .array(try elems.map(resolve)) case .boxed(_): fatalError("Not implemented: boxed custom attribute arguments") diff --git a/Sources/DotNetMetadata/ExportedType.swift b/Sources/DotNetMetadata/ExportedType.swift index d35cd14..63e0854 100644 --- a/Sources/DotNetMetadata/ExportedType.swift +++ b/Sources/DotNetMetadata/ExportedType.swift @@ -25,7 +25,7 @@ public final class ExportedType { } public private(set) lazy var fullName: String = { - // Assume that exported type cannot be nested types + // TODO: Support nested exported types makeFullTypeName(namespace: namespace, name: name) }() @@ -36,7 +36,7 @@ public final class ExportedType { let definitionAssembly = try self.assembly.resolve(index) // TODO: Optimize using the typeDefId field // TODO: Support recursive exported types - guard let typeDefinition = definitionAssembly.findTypeDefinition(namespace: namespace, name: name) else { + guard let typeDefinition = try definitionAssembly.resolveTypeDefinition(namespace: namespace, name: name) else { throw DotNetMetadataFormat.InvalidFormatError.tableConstraint } return typeDefinition diff --git a/Sources/DotNetMetadata/Mscorlib.swift b/Sources/DotNetMetadata/Mscorlib.swift index 3d3c58e..97b73ff 100644 --- a/Sources/DotNetMetadata/Mscorlib.swift +++ b/Sources/DotNetMetadata/Mscorlib.swift @@ -17,7 +17,7 @@ public final class Mscorlib: Assembly { public final class SpecialTypes { init(assembly: Assembly) throws { func find(_ name: String) throws -> T { - guard let typeDefinition = assembly.findTypeDefinition(fullName: "System." + name), + guard let typeDefinition = try assembly.resolveTypeDefinition(fullName: "System." + name), let typeDefinition = typeDefinition as? T else { throw MissingSpecialType() } diff --git a/Tests/DotNetMetadata/AttributeTests.swift b/Tests/DotNetMetadata/AttributeTests.swift index 1b0307c..4632ea0 100644 --- a/Tests/DotNetMetadata/AttributeTests.swift +++ b/Tests/DotNetMetadata/AttributeTests.swift @@ -28,7 +28,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { } public func testNumericArgument() throws { - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "IntArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "IntArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) let arguments = try attribute.arguments XCTAssertEqual(arguments.count, 1) @@ -41,7 +41,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { } public func testStringArgument() throws { - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "StringArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "StringArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) let arguments = try attribute.arguments XCTAssertEqual(arguments.count, 1) @@ -54,7 +54,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { } public func testTypeArgument() throws { - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "TypeArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "TypeArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) let arguments = try attribute.arguments XCTAssertEqual(arguments.count, 1) @@ -68,7 +68,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { public func testEnumArgument() throws { try XCTSkipIf(true, "Requires CoreLib support for .NET Core") - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "EnumArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "EnumArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) let arguments = try attribute.arguments XCTAssertEqual(arguments.count, 1) @@ -81,7 +81,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { } public func testNamedFieldArgument() throws { - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "NamedFieldArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "NamedFieldArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) XCTAssertEqual(try attribute.arguments.count, 0) let namedArguments = try attribute.namedArguments @@ -102,7 +102,7 @@ internal final class AttributeTests: CompiledAssemblyTestCase { } public func testNamedPropertyArgument() throws { - let targetType = try XCTUnwrap(assembly.findTypeDefinition(fullName: "NamedPropertyArgument")) + let targetType = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "NamedPropertyArgument")) let attribute = try XCTUnwrap(targetType.findAttribute(namespace: nil, name: "MyAttributeAttribute")) XCTAssertEqual(try attribute.arguments.count, 0) let namedArguments = try attribute.namedArguments diff --git a/Tests/DotNetMetadata/EnumTests.swift b/Tests/DotNetMetadata/EnumTests.swift index ed095dc..656ed7c 100644 --- a/Tests/DotNetMetadata/EnumTests.swift +++ b/Tests/DotNetMetadata/EnumTests.swift @@ -12,32 +12,32 @@ internal final class EnumTests: CompiledAssemblyTestCase { public func testEnumerantNames() throws { - let enumDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyEnum") as? EnumDefinition) + let enumDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyEnum") as? EnumDefinition) XCTAssertEqual(enumDefinition.fields.filter { $0.isStatic }.map { $0.name }.sorted(), ["A", "B"]) } public func testUnderlyingType() throws { try XCTSkipIf(true, "Requires CoreLib support for .NET Core") XCTAssertEqual( - try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyEnum") as? EnumDefinition).underlyingType.fullName, + try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyEnum") as? EnumDefinition).underlyingType.fullName, "System.Int32") XCTAssertEqual( - try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyShortEnum") as? EnumDefinition).underlyingType.fullName, + try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyShortEnum") as? EnumDefinition).underlyingType.fullName, "System.Int16") } public func testEnumerantValues() throws { - let enumDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyEnum") as? EnumDefinition) + let enumDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyEnum") as? EnumDefinition) XCTAssertEqual(try XCTUnwrap(XCTUnwrap(enumDefinition.findField(name: "A")).literalValue), .int32(1)) XCTAssertEqual(try XCTUnwrap(XCTUnwrap(enumDefinition.findField(name: "B")).literalValue), .int32(2)) - let shortEnumDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyShortEnum") as? EnumDefinition) + let shortEnumDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyShortEnum") as? EnumDefinition) XCTAssertEqual(try XCTUnwrap(XCTUnwrap(shortEnumDefinition.findField(name: "A")).literalValue), .int16(42)) } public func testIsFlags() throws { try XCTSkipIf(true, "Requires CoreLib support for .NET Core") - XCTAssertFalse(try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyEnum") as? EnumDefinition).isFlags) - XCTAssertTrue(try XCTUnwrap(assembly.findTypeDefinition(fullName: "MyFlagsEnum") as? EnumDefinition).isFlags) + XCTAssertFalse(try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyEnum") as? EnumDefinition).isFlags) + XCTAssertTrue(try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "MyFlagsEnum") as? EnumDefinition).isFlags) } } \ No newline at end of file diff --git a/Tests/DotNetMetadata/FieldTests.swift b/Tests/DotNetMetadata/FieldTests.swift index 54beaa1..73f0567 100644 --- a/Tests/DotNetMetadata/FieldTests.swift +++ b/Tests/DotNetMetadata/FieldTests.swift @@ -20,7 +20,7 @@ internal final class FieldTests: CompiledAssemblyTestCase { public override func setUpWithError() throws { try super.setUpWithError() - typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Fields")) + typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Fields")) publicInstanceField = try XCTUnwrap(typeDefinition.findField(name: "PublicInstance")) privateStaticInitOnlyField = try XCTUnwrap(typeDefinition.findField(name: "PrivateStaticInitOnly")) protectedLiteralField = try XCTUnwrap(typeDefinition.findField(name: "ProtectedLiteral")) @@ -39,7 +39,7 @@ internal final class FieldTests: CompiledAssemblyTestCase { public func testType() throws { try XCTAssertEqual( XCTUnwrap(publicInstanceField.type.asDefinition), - XCTUnwrap(assembly.findTypeDefinition(fullName: "FieldType"))) + XCTUnwrap(assembly.resolveTypeDefinition(fullName: "FieldType"))) } public func testVisibility() throws { diff --git a/Tests/DotNetMetadata/NetFX45MscorlibTests+events.swift b/Tests/DotNetMetadata/NetFX45MscorlibTests+events.swift index 60eac4b..ca49291 100644 --- a/Tests/DotNetMetadata/NetFX45MscorlibTests+events.swift +++ b/Tests/DotNetMetadata/NetFX45MscorlibTests+events.swift @@ -5,14 +5,14 @@ import XCTest extension NetFX45MscorlibTests { func testTypeEventEnumeration() throws { XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Diagnostics.Tracing.EventListener")?.events + try Self.assembly.resolveTypeDefinition(fullName: "System.Diagnostics.Tracing.EventListener")?.events .filter({ $0.hasPublicAddRemoveAccessors }).map({ $0.name }).sorted(), [ "EventSourceCreated", "EventWritten" ]) } func testEventAccessors() throws { let event = try XCTUnwrap( - Self.assembly.findTypeDefinition(fullName: "System.Diagnostics.Tracing.EventListener")? + Self.assembly.resolveTypeDefinition(fullName: "System.Diagnostics.Tracing.EventListener")? .findEvent(name: "EventSourceCreated")) XCTAssertEqual(try XCTUnwrap(event.addAccessor).name, "add_EventSourceCreated") @@ -20,9 +20,9 @@ extension NetFX45MscorlibTests { } func testEventType() throws { - let console = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Console")) + let console = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Console")) let consoleCancelEventHandler = try XCTUnwrap( - Self.assembly.findTypeDefinition(fullName: "System.ConsoleCancelEventHandler") as? DelegateDefinition) + Self.assembly.resolveTypeDefinition(fullName: "System.ConsoleCancelEventHandler") as? DelegateDefinition) XCTAssertEqual( try XCTUnwrap(console.findEvent(name: "CancelKeyPress")).handlerType, diff --git a/Tests/DotNetMetadata/NetFX45MscorlibTests+methods.swift b/Tests/DotNetMetadata/NetFX45MscorlibTests+methods.swift index 7e0a3e3..1557696 100644 --- a/Tests/DotNetMetadata/NetFX45MscorlibTests+methods.swift +++ b/Tests/DotNetMetadata/NetFX45MscorlibTests+methods.swift @@ -5,19 +5,19 @@ import XCTest extension NetFX45MscorlibTests { func testTypeMethodEnumeration() throws { XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Collections.Generic.IEqualityComparer`1")?.methods.map({ $0.name }).sorted(), + try Self.assembly.resolveTypeDefinition(fullName: "System.Collections.Generic.IEqualityComparer`1")?.methods.map({ $0.name }).sorted(), [ "Equals", "GetHashCode" ]) } func testMethodClass() throws { - let object = Self.assembly.findTypeDefinition(fullName: "System.Object") + let object = try Self.assembly.resolveTypeDefinition(fullName: "System.Object") XCTAssertNil(object?.findMethod(name: "ToString") as? Constructor) XCTAssertNotNil(object?.findMethod(name: Constructor.name) as? Constructor) } func testMethodFlags() throws { // Abstract interface method - let iasyncResult_get_IsCompleted = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.IAsyncResult")? + let iasyncResult_get_IsCompleted = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.IAsyncResult")? .findMethod(name: "get_IsCompleted")) XCTAssertEqual(iasyncResult_get_IsCompleted.isStatic, false) XCTAssertEqual(iasyncResult_get_IsCompleted.isInstance, true) @@ -26,7 +26,7 @@ extension NetFX45MscorlibTests { XCTAssertEqual(iasyncResult_get_IsCompleted.nameKind, NameKind.special) // Static method - let gc_WaitForPendingFinalizers = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.GC")? + let gc_WaitForPendingFinalizers = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.GC")? .findMethod(name: "WaitForPendingFinalizers")) XCTAssertEqual(gc_WaitForPendingFinalizers.isStatic, true) XCTAssertEqual(gc_WaitForPendingFinalizers.isInstance, false) @@ -35,7 +35,7 @@ extension NetFX45MscorlibTests { XCTAssertEqual(gc_WaitForPendingFinalizers.nameKind, NameKind.regular) // Overriden virtual method - let exception_ToString = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Exception")? + let exception_ToString = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Exception")? .findMethod(name: "ToString", public: true, arity: 0)) XCTAssertEqual(exception_ToString.isStatic, false) XCTAssertEqual(exception_ToString.isInstance, true) @@ -46,55 +46,55 @@ extension NetFX45MscorlibTests { func testMethodParamEnumeration() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Object")? + try Self.assembly.resolveTypeDefinition(fullName: "System.Object")? .findMethod(name: "ReferenceEquals")?.params.map { $0.name }, [ "objA", "objB" ]) XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Object")? + try Self.assembly.resolveTypeDefinition(fullName: "System.Object")? .findMethod(name: "ToString")?.params.count, 0) } func testMethodHasReturnValue() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Object")? + try Self.assembly.resolveTypeDefinition(fullName: "System.Object")? .findMethod(name: "ToString")?.hasReturnValue, true) XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.IDisposable")? + try Self.assembly.resolveTypeDefinition(fullName: "System.IDisposable")? .findMethod(name: "Dispose")?.hasReturnValue, false) } func testMethodReturnType() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Object")? + try Self.assembly.resolveTypeDefinition(fullName: "System.Object")? .findMethod(name: "ToString")?.returnType.asDefinition?.fullName, "System.String") XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.IDisposable")? + try Self.assembly.resolveTypeDefinition(fullName: "System.IDisposable")? .findMethod(name: "Dispose")?.returnType.asDefinition?.fullName, "System.Void") } func testMethodParamType() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.String")? + try Self.assembly.resolveTypeDefinition(fullName: "System.String")? .findMethod(name: "IsNullOrEmpty")?.params[0].type.asDefinition?.fullName, "System.String") } func testParamByRef() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Guid")? + try Self.assembly.resolveTypeDefinition(fullName: "System.Guid")? .findMethod(name: "TryParse")?.params.map { $0.isByRef }, [ false, true ]) } func testOverloadBinding() throws { - guard let convert = Self.assembly.findTypeDefinition(fullName: "System.Convert") else { + guard let convert = try Self.assembly.resolveTypeDefinition(fullName: "System.Convert") else { return XCTFail("Failed to find System.Convert") } diff --git a/Tests/DotNetMetadata/NetFX45MscorlibTests+typeDefinitions.swift b/Tests/DotNetMetadata/NetFX45MscorlibTests+typeDefinitions.swift index eb788fd..919ff9d 100644 --- a/Tests/DotNetMetadata/NetFX45MscorlibTests+typeDefinitions.swift +++ b/Tests/DotNetMetadata/NetFX45MscorlibTests+typeDefinitions.swift @@ -4,21 +4,21 @@ import XCTest extension NetFX45MscorlibTests { func testTypeName_topLevelNamespace() throws { - let object = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Object")) + let object = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Object")) XCTAssertEqual(object.name, "Object") XCTAssertEqual(object.namespace, "System") XCTAssertEqual(object.fullName, "System.Object") } func testTypeName_nestedNamespace() throws { - let bitArray = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Collections.BitArray")) + let bitArray = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Collections.BitArray")) XCTAssertEqual(bitArray.name, "BitArray") XCTAssertEqual(bitArray.namespace, "System.Collections") XCTAssertEqual(bitArray.fullName, "System.Collections.BitArray") } func testTypeName_nested() throws { - let environment_SpecialFolder = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Environment/SpecialFolder")) + let environment_SpecialFolder = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Environment/SpecialFolder")) XCTAssertEqual(environment_SpecialFolder.name, "SpecialFolder") XCTAssertEqual(environment_SpecialFolder.namespace, nil) XCTAssertEqual(environment_SpecialFolder.fullName, "System.Environment/SpecialFolder") @@ -26,36 +26,36 @@ extension NetFX45MscorlibTests { func testTypeLayout() throws { XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Object")?.layout, + try Self.assembly.resolveTypeDefinition(fullName: "System.Object")?.layout, TypeLayout.auto) // Rare public type with non-zero size XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.ValueTuple")?.layout, + try Self.assembly.resolveTypeDefinition(fullName: "System.ValueTuple")?.layout, TypeLayout.sequential(pack: nil, minSize: 1)) // Rare public type with explicit layout XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Runtime.InteropServices.BINDPTR")?.layout, + try Self.assembly.resolveTypeDefinition(fullName: "System.Runtime.InteropServices.BINDPTR")?.layout, TypeLayout.explicit(minSize: 0)) } func testEnclosingType() throws { XCTAssertIdentical( - try Self.assembly.findTypeDefinition(fullName: "System.Environment/SpecialFolder")?.enclosingType, - Self.assembly.findTypeDefinition(fullName: "System.Environment")) + try Self.assembly.resolveTypeDefinition(fullName: "System.Environment/SpecialFolder")?.enclosingType, + try Self.assembly.resolveTypeDefinition(fullName: "System.Environment")) - XCTAssertNil(try Self.assembly.findTypeDefinition(fullName: "System.Environment")?.enclosingType) + XCTAssertNil(try Self.assembly.resolveTypeDefinition(fullName: "System.Environment")?.enclosingType) } func testBaseType() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.ValueType")?.base?.definition.fullName, + try Self.assembly.resolveTypeDefinition(fullName: "System.ValueType")?.base?.definition.fullName, "System.Object") } func testBaseInterfaces() throws { - guard let equalityComparer = Self.assembly.findTypeDefinition(fullName: "System.Collections.Generic.EqualityComparer`1") else { + guard let equalityComparer = try Self.assembly.resolveTypeDefinition(fullName: "System.Collections.Generic.EqualityComparer`1") else { return XCTFail("IAsyncAction not found") } XCTAssertEqual( @@ -64,42 +64,42 @@ extension NetFX45MscorlibTests { } func testTypeVisibility() throws { - XCTAssertEqual(Self.assembly.findTypeDefinition(fullName: "System.Type")?.visibility, .public) - XCTAssertEqual(Self.assembly.findTypeDefinition(fullName: "System.RuntimeType")?.visibility, .assembly) + XCTAssertEqual(try Self.assembly.resolveTypeDefinition(fullName: "System.Type")?.visibility, .public) + XCTAssertEqual(try Self.assembly.resolveTypeDefinition(fullName: "System.RuntimeType")?.visibility, .assembly) } func testTypeFlags() throws { - let object = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Object") as? ClassDefinition) + let object = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Object") as? ClassDefinition) XCTAssert(!object.isAbstract) XCTAssert(!object.isSealed) - let gc = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.GC") as? ClassDefinition) + let gc = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.GC") as? ClassDefinition) XCTAssert(gc.isAbstract) XCTAssert(gc.isSealed) } func testTypeDefinitionClass() throws { - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.Int32") as? StructDefinition) - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.IDisposable") as? InterfaceDefinition) - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.String") as? ClassDefinition) - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.StringComparison") as? EnumDefinition) - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.Action") as? DelegateDefinition) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.Int32") as? StructDefinition) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.IDisposable") as? InterfaceDefinition) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.String") as? ClassDefinition) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.StringComparison") as? EnumDefinition) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.Action") as? DelegateDefinition) } func testTypeGenericParamEnumeration() throws { // Interface with 1 generic parameter XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Action`1")?.genericParams.map({ $0.name }), + try Self.assembly.resolveTypeDefinition(fullName: "System.Action`1")?.genericParams.map({ $0.name }), [ "T" ]) // Delegate with 2 generic parameters XCTAssertEqual( - Self.assembly.findTypeDefinition(fullName: "System.Action`2")?.genericParams.map({ $0.name }), + try Self.assembly.resolveTypeDefinition(fullName: "System.Action`2")?.genericParams.map({ $0.name }), [ "T1", "T2" ]) } func testEnum() throws { - guard let dayOfWeek = Self.assembly.findTypeDefinition(fullName: "System.DayOfWeek") as? EnumDefinition else { + guard let dayOfWeek = try Self.assembly.resolveTypeDefinition(fullName: "System.DayOfWeek") as? EnumDefinition else { return XCTFail("System.DayOfWeek not found") } @@ -110,7 +110,7 @@ extension NetFX45MscorlibTests { func testNestedType() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "System.Collections.Generic.List`1")?.nestedTypes.contains { $0.name == "Enumerator" }, + try Self.assembly.resolveTypeDefinition(fullName: "System.Collections.Generic.List`1")?.nestedTypes.contains { $0.name == "Enumerator" }, true) } } diff --git a/Tests/DotNetMetadata/NetFX45MscorlibTests+types.swift b/Tests/DotNetMetadata/NetFX45MscorlibTests+types.swift index 1283248..6d4c64b 100644 --- a/Tests/DotNetMetadata/NetFX45MscorlibTests+types.swift +++ b/Tests/DotNetMetadata/NetFX45MscorlibTests+types.swift @@ -4,7 +4,7 @@ import XCTest extension NetFX45MscorlibTests { func testArrayType() throws { - guard let arraySegment = Self.assembly.findTypeDefinition(fullName: "System.ArraySegment`1") else { + guard let arraySegment = try Self.assembly.resolveTypeDefinition(fullName: "System.ArraySegment`1") else { return XCTFail("Couldn't find System.ArraySegment`1") } @@ -15,12 +15,12 @@ extension NetFX45MscorlibTests { func testGenericMethodArgType() throws { // T[] System.Array.Empty() -- not overloaded - let arraySort = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.Array")?.findMethod(name: "Empty")) + let arraySort = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.Array")?.findMethod(name: "Empty")) XCTAssertEqual(try arraySort.returnType, .array(of: .genericParam(arraySort.genericParams[0]))) } func testGenericTypeArgType() throws { - guard let nullable = Self.assembly.findTypeDefinition(fullName: "System.Nullable`1") else { + guard let nullable = try Self.assembly.resolveTypeDefinition(fullName: "System.Nullable`1") else { return XCTFail("Couldn't find System.Nullable`1") } @@ -31,16 +31,16 @@ extension NetFX45MscorlibTests { func testPointerType() throws { // void* System.IntPtr.ToPointer() - let intPtrToPointer = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "System.IntPtr")?.findMethod(name: "ToPointer")) + let intPtrToPointer = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "System.IntPtr")?.findMethod(name: "ToPointer")) XCTAssertEqual(try intPtrToPointer.returnType, .pointer(to: nil)) } func testGenericInstType() throws { - guard let ienumerable = Self.assembly.findTypeDefinition(fullName: "System.Collections.Generic.IEnumerable`1") else { + guard let ienumerable = try Self.assembly.resolveTypeDefinition(fullName: "System.Collections.Generic.IEnumerable`1") else { return XCTFail("Couldn't find System.Collections.Generic.IEnumerable`1") } - guard let ienumerator = Self.assembly.findTypeDefinition(fullName: "System.Collections.Generic.IEnumerator`1") else { + guard let ienumerator = try Self.assembly.resolveTypeDefinition(fullName: "System.Collections.Generic.IEnumerator`1") else { return XCTFail("Couldn't find AsyncOperationCompletedHandler`1") } diff --git a/Tests/DotNetMetadata/NetFX45MscorlibTests.swift b/Tests/DotNetMetadata/NetFX45MscorlibTests.swift index 7c6d7b1..a0df2e7 100644 --- a/Tests/DotNetMetadata/NetFX45MscorlibTests.swift +++ b/Tests/DotNetMetadata/NetFX45MscorlibTests.swift @@ -25,6 +25,6 @@ final class NetFX45MscorlibTests: XCTestCase { } func testTypeLookup() throws { - XCTAssertNotNil(Self.assembly.findTypeDefinition(fullName: "System.Object")) + XCTAssertNotNil(try Self.assembly.resolveTypeDefinition(fullName: "System.Object")) } } diff --git a/Tests/DotNetMetadata/PropertyTests.swift b/Tests/DotNetMetadata/PropertyTests.swift index bf21c2c..500a71a 100644 --- a/Tests/DotNetMetadata/PropertyTests.swift +++ b/Tests/DotNetMetadata/PropertyTests.swift @@ -18,7 +18,7 @@ internal final class PropertyTests: CompiledAssemblyTestCase { public override func setUpWithError() throws { try super.setUpWithError() - typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Properties")) + typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Properties")) publicAbstractInstanceGetSetProperty = try XCTUnwrap(typeDefinition.findProperty(name: "PublicAbstractInstanceGetSet")) privateStaticGetProperty = try XCTUnwrap(typeDefinition.findProperty(name: "PrivateStaticGet")) } @@ -36,7 +36,7 @@ internal final class PropertyTests: CompiledAssemblyTestCase { public func testType() throws { try XCTAssertEqual( XCTUnwrap(publicAbstractInstanceGetSetProperty.type.asDefinition), - XCTUnwrap(assembly.findTypeDefinition(fullName: "PropertyType"))) + XCTUnwrap(assembly.resolveTypeDefinition(fullName: "PropertyType"))) } public func testAccessors() throws { diff --git a/Tests/DotNetMetadata/StructLayoutTests.swift b/Tests/DotNetMetadata/StructLayoutTests.swift index a3adb39..cf1aae4 100644 --- a/Tests/DotNetMetadata/StructLayoutTests.swift +++ b/Tests/DotNetMetadata/StructLayoutTests.swift @@ -23,22 +23,22 @@ internal final class StructLayoutTests: CompiledAssemblyTestCase { } public func testAuto() throws { - let typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Auto")) + let typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Auto")) XCTAssertEqual(typeDefinition.layout, .auto) } public func testSequential() throws { - let typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Sequential")) + let typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Sequential")) XCTAssertEqual(typeDefinition.layout, .sequential(pack: 2, minSize: 24)) } public func testExplicit() throws { - let typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Explicit")) + let typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Explicit")) XCTAssertEqual(typeDefinition.layout, .explicit(minSize: 24)) } public func testFieldOffset() throws { - let typeDefinition = try XCTUnwrap(assembly.findTypeDefinition(fullName: "Explicit")) + let typeDefinition = try XCTUnwrap(assembly.resolveTypeDefinition(fullName: "Explicit")) XCTAssertEqual(typeDefinition.findField(name: "A")?.explicitOffset, 16) XCTAssertEqual(typeDefinition.findField(name: "B")?.explicitOffset, 16) } diff --git a/Tests/WindowsMetadata/WinMetadataTests.swift b/Tests/WindowsMetadata/WinMetadataTests.swift index 3b6d906..fdfd769 100644 --- a/Tests/WindowsMetadata/WinMetadataTests.swift +++ b/Tests/WindowsMetadata/WinMetadataTests.swift @@ -27,12 +27,12 @@ final class WinMetadataTests: XCTestCase { func testMscorlibTypeReference() throws { XCTAssertEqual( - try Self.assembly.findTypeDefinition(fullName: "Windows.Foundation.Point")?.base?.definition.fullName, + try Self.assembly.resolveTypeDefinition(fullName: "Windows.Foundation.Point")?.base?.definition.fullName, "System.ValueType") } func testParameterizedInterfaceID() throws { - let iasyncOperation = try XCTUnwrap(Self.assembly.findTypeDefinition(fullName: "Windows.Foundation.IAsyncOperation`1") as? InterfaceDefinition) + let iasyncOperation = try XCTUnwrap(Self.assembly.resolveTypeDefinition(fullName: "Windows.Foundation.IAsyncOperation`1") as? InterfaceDefinition) XCTAssertEqual( try WindowsMetadata.getInterfaceID(iasyncOperation, genericArgs: [Self.mscorlib.specialTypes.boolean.bindNode()]), UUID(uuidString: "cdb5efb3-5788-509d-9be1-71ccb8a3362a"))