From eded880225fa5bd4b3917fe2a4f1a3c5da870f64 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Thu, 12 Jan 2023 13:07:54 +0200 Subject: [PATCH] [K/N][cinterop] Add location to ObjCCategory To avoid adding unnecessary included categories to index, we need to track their location. Also, we can't just implement TypeDeclaration interface because category is not a type. Thus, we extract `location` property from TypeDeclaration to LocatableDeclaration and implement it in ObjCCategory. --- .../kotlin/native/interop/indexer/Indexer.kt | 25 ++++++++++--------- .../native/interop/indexer/NativeIndex.kt | 6 +++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index 372b0fe1936..0e4bc71da4f 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -68,7 +68,8 @@ private class ObjCClassImpl( } private class ObjCCategoryImpl( - name: String, clazz: ObjCClass + name: String, clazz: ObjCClass, + override val location: Location ) : ObjCCategory(name, clazz), ObjCContainerImpl { override val protocols = mutableListOf() override val methods = mutableListOf() @@ -85,7 +86,7 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole object Protocol : DeclarationID() } - private inner class TypeDeclarationRegistry { + private inner class LocatableDeclarationRegistry { private val all = mutableMapOf() val included = mutableListOf() @@ -120,22 +121,23 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole } override val structs: List get() = structRegistry.included - private val structRegistry = TypeDeclarationRegistry() + private val structRegistry = LocatableDeclarationRegistry() override val enums: List get() = enumRegistry.included - private val enumRegistry = TypeDeclarationRegistry() + private val enumRegistry = LocatableDeclarationRegistry() override val objCClasses: List get() = objCClassRegistry.included - private val objCClassRegistry = TypeDeclarationRegistry() + private val objCClassRegistry = LocatableDeclarationRegistry() override val objCProtocols: List get() = objCProtocolRegistry.included - private val objCProtocolRegistry = TypeDeclarationRegistry() + private val objCProtocolRegistry = LocatableDeclarationRegistry() - override val objCCategories: Collection get() = objCCategoryById.values - private val objCCategoryById = mutableMapOf() + override val objCCategories: Collection get() = objCCategoryById.included + private val objCCategoryById = LocatableDeclarationRegistry() override val typedefs get() = typedefRegistry.included - private val typedefRegistry = TypeDeclarationRegistry() + private val typedefRegistry = LocatableDeclarationRegistry() + private val functionById = mutableMapOf() @@ -474,11 +476,10 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole if (!isAvailable(classCursor)) return null val name = clang_getCursorDisplayName(cursor).convertAndDispose() - val declarationId = getDeclarationId(cursor) - return objCCategoryById.getOrPut(declarationId) { + return objCCategoryById.getOrPut(cursor) { val clazz = getObjCClassAt(classCursor) - val category = ObjCCategoryImpl(name, clazz) + val category = ObjCCategoryImpl(name, clazz, getLocation(cursor)) addChildrenToObjCContainer(cursor, category) category } diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt index 3ebaaa58d82..097ea8044d4 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/NativeIndex.kt @@ -151,10 +151,12 @@ data class HeaderId(val value: String) data class Location(val headerId: HeaderId) -interface TypeDeclaration { +interface LocatableDeclaration { val location: Location } +interface TypeDeclaration : LocatableDeclaration + sealed class StructMember(val name: String) { abstract val offset: Long? } @@ -284,7 +286,7 @@ abstract class ObjCClass(name: String) : ObjCClassOrProtocol(name) { } abstract class ObjCProtocol(name: String) : ObjCClassOrProtocol(name) -abstract class ObjCCategory(val name: String, val clazz: ObjCClass) : ObjCContainer() +abstract class ObjCCategory(val name: String, val clazz: ObjCClass) : ObjCContainer(), LocatableDeclaration /** * C function parameter.