Introduce SymbolTable lifecycle

To be enforced later
This commit is contained in:
Svyatoslav Scherbina
2019-04-11 14:14:58 +03:00
committed by SvyatoslavScherbina
parent fefcb8082f
commit 1ec5a188f7
2 changed files with 44 additions and 5 deletions
@@ -221,10 +221,14 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
val specialDeclarationsFactory = SpecialDeclarationsFactory(this)
class LazyMember<T>(val initializer: Context.() -> T) {
open class LazyMember<T>(val initializer: Context.() -> T) {
operator fun getValue(thisRef: Context, property: KProperty<*>): T = thisRef.getValue(this)
}
class LazyVarMember<T>(initializer: Context.() -> T) : LazyMember<T>(initializer) {
operator fun setValue(thisRef: Context, property: KProperty<*>, newValue: T) = thisRef.setValue(this, newValue)
}
companion object {
fun <T> lazyMember(initializer: Context.() -> T) = LazyMember<T>(initializer)
@@ -235,6 +239,8 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
}
result
}
fun <T> nullValue() = LazyVarMember<T?>({ null })
}
private val lazyValues = mutableMapOf<LazyMember<*>, Any?>()
@@ -242,6 +248,10 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
fun <T> getValue(member: LazyMember<T>): T =
@Suppress("UNCHECKED_CAST") (lazyValues.getOrPut(member, { member.initializer(this) }) as T)
fun <T> setValue(member: LazyVarMember<T>, newValue: T) {
lazyValues[member] = newValue
}
val reflectionTypes: KonanReflectionTypes by lazy(PUBLICATION) {
KonanReflectionTypes(moduleDescriptor, KonanFqNames.internalPackageName)
}
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.backend.common.serialization.DescriptorTable
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
@@ -50,6 +51,19 @@ internal val frontendPhase = konanUnitPhase(
description = "Frontend builds AST"
)
/**
* Valid from [createSymbolTablePhase] until [destroySymbolTablePhase].
*/
private var Context.symbolTable: SymbolTable? by Context.nullValue()
internal val createSymbolTablePhase = konanUnitPhase(
op = {
this.symbolTable = SymbolTable()
},
name = "CreateSymbolTable",
description = "Create SymbolTable"
)
internal val objCExportPhase = konanUnitPhase(
op = {
objCExport = ObjCExport(this)
@@ -61,9 +75,12 @@ internal val objCExportPhase = konanUnitPhase(
internal val psiToIrPhase = konanUnitPhase(
op = {
// Translate AST to high level IR.
val symbolTable = symbolTable!!
val translator = Psi2IrTranslator(config.configuration.languageVersionSettings,
Psi2IrConfiguration(false))
val generatorContext = translator.createGeneratorContext(moduleDescriptor, bindingContext)
val generatorContext = translator.createGeneratorContext(moduleDescriptor, bindingContext, symbolTable)
@Suppress("DEPRECATION")
psi2IrGeneratorContext = generatorContext
@@ -73,7 +90,7 @@ internal val psiToIrPhase = konanUnitPhase(
moduleDescriptor,
this as LoggingContext,
generatorContext.irBuiltIns,
generatorContext.symbolTable,
symbolTable,
forwardDeclarationsModuleDescriptor,
getExportedDependencies()
)
@@ -91,7 +108,7 @@ internal val psiToIrPhase = konanUnitPhase(
dependenciesCount = dependencies.size
}
val symbols = KonanSymbols(this, generatorContext.symbolTable, generatorContext.symbolTable.lazyWrapper)
val symbols = KonanSymbols(this, symbolTable, symbolTable.lazyWrapper)
val module = translator.generateModuleFragment(generatorContext, environment.getSourceFiles(), deserializer)
irModule = module
@@ -101,7 +118,17 @@ internal val psiToIrPhase = konanUnitPhase(
// validateIrModule(this, module)
},
name = "Psi2Ir",
description = "Psi to IR conversion"
description = "Psi to IR conversion",
prerequisite = setOf(createSymbolTablePhase)
)
internal val destroySymbolTablePhase = konanUnitPhase(
op = {
this.symbolTable = null // TODO: invalidate symbolTable itself.
},
name = "DestroySymbolTable",
description = "Destroy SymbolTable",
prerequisite = setOf(createSymbolTablePhase)
)
internal val irGeneratorPluginsPhase = konanUnitPhase(
@@ -274,8 +301,10 @@ val toplevelPhase: CompilerPhase<*, Unit, Unit> = namedUnitPhase(
name = "Compiler",
description = "The whole compilation process",
lower = frontendPhase then
createSymbolTablePhase then
objCExportPhase then
psiToIrPhase then
destroySymbolTablePhase then
irGeneratorPluginsPhase then
copyDefaultValuesToActualPhase then
patchDeclarationParents0Phase then