From 4b6b74fb2ccfc024a1e46a7df87fbfb5bf863608 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Wed, 7 Dec 2022 13:42:54 +0200 Subject: [PATCH] [K/N][CExport] Explicitly pass data to CAdapterGenerator.BindingsBuilder --- .../konan/cexport/CAdapterExportedElements.kt | 11 ++++++++ .../konan/cexport/CAdapterGenerator.kt | 27 ++++++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterExportedElements.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterExportedElements.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterExportedElements.kt new file mode 100644 index 00000000000..0a54f1f6180 --- /dev/null +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterExportedElements.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.backend.konan.cexport + +internal data class CAdapterExportedElements( + val typeTranslator: CAdapterTypeTranslator, + val scopes: MutableList, +) \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterGenerator.kt index 84b5bf0d505..8657950d75a 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cexport/CAdapterGenerator.kt @@ -33,19 +33,19 @@ import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.* import java.io.PrintWriter -private enum class ScopeKind { +internal enum class ScopeKind { TOP, CLASS, PACKAGE } -private enum class ElementKind { +internal enum class ElementKind { FUNCTION, PROPERTY, TYPE } -private enum class DefinitionKind { +internal enum class DefinitionKind { C_HEADER_DECLARATION, C_HEADER_STRUCT, C_SOURCE_DECLARATION, @@ -109,7 +109,7 @@ private fun functionImplName(descriptor: DeclarationDescriptor, default: String, internal data class SignatureElement(val name: String, val type: KotlinType) -private class ExportedElementScope(val kind: ScopeKind, val name: String) { +internal class ExportedElementScope(val kind: ScopeKind, val name: String) { val elements = mutableListOf() val scopes = mutableListOf() private val scopeNames = mutableSetOf() @@ -147,7 +147,7 @@ private class ExportedElementScope(val kind: ScopeKind, val name: String) { } } -private class ExportedElement( +internal class ExportedElement( val kind: ElementKind, val scope: ExportedElementScope, val declaration: DeclarationDescriptor, @@ -423,8 +423,6 @@ internal class CAdapterGenerator( val context: Context, private val typeTranslator: CAdapterTypeTranslator, ) : DeclarationDescriptorVisitor { - private val builtIns = context.builtIns - private val scopes = mutableListOf() internal val prefix = context.config.fullExportedNamePrefix.replace("-|\\.".toRegex(), "_") private val paramNamesRecorded = mutableMapOf() @@ -560,10 +558,14 @@ internal class CAdapterGenerator( private val moduleDescriptors = mutableSetOf() + // TODO: Use explicit I/O between phases. + private lateinit var cAdapterExportedElements: CAdapterExportedElements + fun buildExports(symbolTable: SymbolTable) { this.symbolTableOrNull = symbolTable try { buildExports() + cAdapterExportedElements = CAdapterExportedElements(typeTranslator, scopes) } finally { this.symbolTableOrNull = null } @@ -600,11 +602,18 @@ internal class CAdapterGenerator( private var functionIndex = 0 fun nextFunctionIndex() = functionIndex++ - fun generateBindings(codegen: CodeGenerator) = BindingsBuilder(codegen).build() + fun generateBindings(codegen: CodeGenerator) = BindingsBuilder(codegen, cAdapterExportedElements).build() - inner class BindingsBuilder(val codegen: CodeGenerator) : ContextUtils { + class BindingsBuilder( + val codegen: CodeGenerator, + val elements: CAdapterExportedElements, + ) : ContextUtils { override val generationState = codegen.generationState + private val typeTranslator = elements.typeTranslator + private val builtIns = elements.typeTranslator.builtIns + private val scopes = elements.scopes + internal val prefix = context.config.fullExportedNamePrefix.replace("-|\\.".toRegex(), "_") private lateinit var outputStreamWriter: PrintWriter