[K/N][CExport] Explicitly pass data to CAdapterGenerator.BindingsBuilder

This commit is contained in:
Sergey Bogolepov
2022-12-07 13:42:54 +02:00
committed by Space Team
parent 16b977530b
commit 4b6b74fb2c
2 changed files with 29 additions and 9 deletions
@@ -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<ExportedElementScope>,
)
@@ -33,19 +33,19 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.* import org.jetbrains.kotlin.types.typeUtil.*
import java.io.PrintWriter import java.io.PrintWriter
private enum class ScopeKind { internal enum class ScopeKind {
TOP, TOP,
CLASS, CLASS,
PACKAGE PACKAGE
} }
private enum class ElementKind { internal enum class ElementKind {
FUNCTION, FUNCTION,
PROPERTY, PROPERTY,
TYPE TYPE
} }
private enum class DefinitionKind { internal enum class DefinitionKind {
C_HEADER_DECLARATION, C_HEADER_DECLARATION,
C_HEADER_STRUCT, C_HEADER_STRUCT,
C_SOURCE_DECLARATION, C_SOURCE_DECLARATION,
@@ -109,7 +109,7 @@ private fun functionImplName(descriptor: DeclarationDescriptor, default: String,
internal data class SignatureElement(val name: String, val type: KotlinType) 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<ExportedElement>() val elements = mutableListOf<ExportedElement>()
val scopes = mutableListOf<ExportedElementScope>() val scopes = mutableListOf<ExportedElementScope>()
private val scopeNames = mutableSetOf<String>() private val scopeNames = mutableSetOf<String>()
@@ -147,7 +147,7 @@ private class ExportedElementScope(val kind: ScopeKind, val name: String) {
} }
} }
private class ExportedElement( internal class ExportedElement(
val kind: ElementKind, val kind: ElementKind,
val scope: ExportedElementScope, val scope: ExportedElementScope,
val declaration: DeclarationDescriptor, val declaration: DeclarationDescriptor,
@@ -423,8 +423,6 @@ internal class CAdapterGenerator(
val context: Context, val context: Context,
private val typeTranslator: CAdapterTypeTranslator, private val typeTranslator: CAdapterTypeTranslator,
) : DeclarationDescriptorVisitor<Boolean, Void?> { ) : DeclarationDescriptorVisitor<Boolean, Void?> {
private val builtIns = context.builtIns
private val scopes = mutableListOf<ExportedElementScope>() private val scopes = mutableListOf<ExportedElementScope>()
internal val prefix = context.config.fullExportedNamePrefix.replace("-|\\.".toRegex(), "_") internal val prefix = context.config.fullExportedNamePrefix.replace("-|\\.".toRegex(), "_")
private val paramNamesRecorded = mutableMapOf<String, Int>() private val paramNamesRecorded = mutableMapOf<String, Int>()
@@ -560,10 +558,14 @@ internal class CAdapterGenerator(
private val moduleDescriptors = mutableSetOf<ModuleDescriptor>() private val moduleDescriptors = mutableSetOf<ModuleDescriptor>()
// TODO: Use explicit I/O between phases.
private lateinit var cAdapterExportedElements: CAdapterExportedElements
fun buildExports(symbolTable: SymbolTable) { fun buildExports(symbolTable: SymbolTable) {
this.symbolTableOrNull = symbolTable this.symbolTableOrNull = symbolTable
try { try {
buildExports() buildExports()
cAdapterExportedElements = CAdapterExportedElements(typeTranslator, scopes)
} finally { } finally {
this.symbolTableOrNull = null this.symbolTableOrNull = null
} }
@@ -600,11 +602,18 @@ internal class CAdapterGenerator(
private var functionIndex = 0 private var functionIndex = 0
fun nextFunctionIndex() = functionIndex++ 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 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(), "_") internal val prefix = context.config.fullExportedNamePrefix.replace("-|\\.".toRegex(), "_")
private lateinit var outputStreamWriter: PrintWriter private lateinit var outputStreamWriter: PrintWriter