From 2aa077e542f38c36f10ce01b76bf5a611f3c26e3 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 29 Aug 2017 14:07:26 +0300 Subject: [PATCH] Generate Windows platform headers --- .../kotlin/native/interop/indexer/Indexer.kt | 45 ++++++++++++++++++- klib/src/platform/windows/windows.def | 3 ++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 klib/src/platform/windows/windows.def diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index a7ee6aff978..17b4fe26eb6 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -497,6 +497,49 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() { } } + private val TARGET_ATTRIBUTE = "__target__" + + internal fun tokenizeExtent(cursor: CValue): List { + val translationUnit = clang_Cursor_getTranslationUnit(cursor)!! + val cursorExtent = clang_getCursorExtent(cursor) + memScoped { + val tokensVar = alloc>() + val numTokensVar = alloc() + clang_tokenize(translationUnit, cursorExtent, tokensVar.ptr, numTokensVar.ptr) + val numTokens = numTokensVar.value + val tokens = tokensVar.value + if (tokens == null) return emptyList() + try { + return (0 until numTokens).map { + clang_getTokenSpelling(translationUnit, tokens[it].readValue()).convertAndDispose() + } + } finally { + clang_disposeTokens(translationUnit, tokens, numTokens) + } + } + TODO() + } + + private fun isSuitable(cursor: CValue): Boolean { + if (!isAvailable(cursor)) return false + + // If function is specific for certain target, ignore that, as we may be + // unable to generate machine code for bridge from the bitcode. + // TODO: this must be implemented with hasAttribute(), but hasAttribute() + // works for Mac hosts only so far. + var suitable = true + visitChildren(cursor) { child, _ -> + if (clang_isAttribute(child.kind) != 0) { + suitable = !tokenizeExtent(child).any { it == TARGET_ATTRIBUTE } + } + if (suitable) + CXChildVisitResult.CXChildVisit_Continue + else + CXChildVisitResult.CXChildVisit_Break + } + return suitable + } + fun indexDeclaration(info: CXIdxDeclInfo): Unit { val cursor = info.cursor.readValue() val entityInfo = info.entityInfo!!.pointed @@ -523,7 +566,7 @@ internal class NativeIndexImpl(val library: NativeLibrary) : NativeIndex() { } CXIdxEntity_Function -> { - if (isAvailable(cursor)) { + if (isSuitable(cursor)) { functionById[getDeclarationId(cursor)] = getFunction(cursor) } } diff --git a/klib/src/platform/windows/windows.def b/klib/src/platform/windows/windows.def new file mode 100644 index 00000000000..f468dec8405 --- /dev/null +++ b/klib/src/platform/windows/windows.def @@ -0,0 +1,3 @@ +package = platform.windows +headers = windows.h +compilerOpts = -DUNICODE -Wno-incompatible-pointer-types -Wno-deprecated-declarations \ No newline at end of file