IR: use IrFactory in SymbolTable

This commit is contained in:
Alexander Udalov
2020-07-13 17:48:07 +02:00
parent c6a127e87e
commit db4cbe7103
23 changed files with 92 additions and 62 deletions
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.IrModuleToJsTransf
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.StageController
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.declarations.stageController
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.noUnboundLeft
@@ -52,7 +53,7 @@ fun compile(
stageController = StageController()
val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) =
loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies)
loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies, IrFactoryImpl)
val moduleDescriptor = moduleFragment.descriptor
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name
// This is a temporary class for migration to IrDeclarationFactory. Usages should be refactored to use the factory directly once possible,
// This is a temporary class for migration to IrFactory. Usages should be refactored to use the factory directly once possible,
// since it doesn't add sufficient value on its own.
class JsIrDeclarationBuilder {
fun buildValueParameter(parent: IrDeclarationParent, name: String, index: Int, type: IrType): IrValueParameter =
@@ -23,10 +23,14 @@ import org.jetbrains.kotlin.ir.backend.jvm.serialization.JvmManglerDesc
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
@@ -37,7 +41,7 @@ object JvmBackendFacade {
val extensions = JvmGeneratorExtensions()
val mangler = JvmManglerDesc(MainFunctionDetector(state.bindingContext, state.languageVersionSettings))
val psi2ir = Psi2IrTranslator(state.languageVersionSettings, Psi2IrConfiguration())
val symbolTable = SymbolTable(JvmIdSignatureDescriptor(mangler), JvmNameProvider)
val symbolTable = SymbolTable(JvmIdSignatureDescriptor(mangler), IrFactoryImpl, JvmNameProvider)
val psi2irContext = psi2ir.createGeneratorContext(state.module, state.bindingContext, symbolTable, extensions)
val pluginExtensions = IrGenerationExtension.getInstances(state.project)
val functionFactory = IrFunctionFactory(psi2irContext.irBuiltIns, symbolTable)
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.backend.js.MainModule
import org.jetbrains.kotlin.ir.backend.js.loadIr
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
@@ -35,7 +36,9 @@ fun compileWasm(
exportedDeclarations: Set<FqName> = emptySet()
): WasmCompilerResult {
val (moduleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) =
loadIr(project, MainModule.SourceFiles(files), analyzer, configuration, allDependencies, friendDependencies)
loadIr(
project, MainModule.SourceFiles(files), analyzer, configuration, allDependencies, friendDependencies, IrFactoryImpl
)
val moduleDescriptor = moduleFragment.descriptor
val context = WasmBackendContext(moduleDescriptor, irBuiltIns, symbolTable, moduleFragment, exportedDeclarations, configuration)
@@ -14,14 +14,14 @@
* limitations under the License.
*/
@file:Suppress("DEPRECATION")
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.*
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrScriptImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.declarations.lazy.IrLazySymbolTable
import org.jetbrains.kotlin.ir.descriptors.WrappedDeclarationDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedFunctionDescriptorWithContainerSource
@@ -74,8 +74,9 @@ interface ReferenceSymbolTable {
fun leaveScope(owner: IrDeclaration)
}
open class SymbolTable(
class SymbolTable(
val signaturer: IdSignatureComposer,
val irFactory: IrFactory,
val nameProvider: NameProvider = NameProvider.DEFAULT
) : ReferenceSymbolTable {
@@ -374,7 +375,7 @@ open class SymbolTable(
origin: IrDeclarationOrigin,
descriptor: ClassDescriptor
): IrAnonymousInitializer =
IrAnonymousInitializerImpl(
irFactory.createAnonymousInitializer(
startOffset, endOffset, origin,
IrAnonymousInitializerSymbolImpl(descriptor)
)
@@ -524,7 +525,7 @@ open class SymbolTable(
fun declareEnumEntry(
startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassDescriptor,
factory: (IrEnumEntrySymbol) -> IrEnumEntry = {
IrEnumEntryImpl(startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor))
irFactory.createEnumEntry(startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor))
}
): IrEnumEntry =
enumEntrySymbolTable.declare(
@@ -581,7 +582,7 @@ open class SymbolTable(
type: IrType,
visibility: Visibility? = null,
fieldFactory: (IrFieldSymbol) -> IrField = {
IrFieldImpl(
irFactory.createField(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor), type,
visibility ?: it.descriptor.visibility, !it.descriptor.isVar, it.descriptor.isEffectivelyExternal(),
it.descriptor.dispatchReceiverParameter == null
@@ -630,6 +631,7 @@ open class SymbolTable(
val propertyTable = HashMap<PropertyDescriptor, IrProperty>()
override fun referenceProperty(descriptor: PropertyDescriptor, generate: () -> IrProperty): IrProperty =
@Suppress("DEPRECATION")
propertyTable.getOrPut(descriptor, generate)
private fun createPropertySymbol(descriptor: PropertyDescriptor): IrPropertySymbol {
@@ -647,7 +649,7 @@ open class SymbolTable(
descriptor: PropertyDescriptor,
isDelegated: Boolean = descriptor.isDelegated,
propertyFactory: (IrPropertySymbol) -> IrProperty = { symbol ->
IrPropertyImpl(
irFactory.createProperty(
startOffset, endOffset, origin, symbol, isDelegated = isDelegated,
name = nameProvider.nameForDeclaration(descriptor),
visibility = descriptor.visibility,
@@ -830,7 +832,7 @@ open class SymbolTable(
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor,
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = {
IrTypeParameterImpl(
irFactory.createTypeParameter(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor),
it.descriptor.index, it.descriptor.isReified, it.descriptor.variance
)
@@ -858,7 +860,7 @@ open class SymbolTable(
origin: IrDeclarationOrigin,
descriptor: TypeParameterDescriptor,
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter = {
IrTypeParameterImpl(
irFactory.createTypeParameter(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor),
it.descriptor.index, it.descriptor.isReified, it.descriptor.variance
)
@@ -890,7 +892,7 @@ open class SymbolTable(
type: IrType,
varargElementType: IrType? = null,
valueParameterFactory: (IrValueParameterSymbol) -> IrValueParameter = {
IrValueParameterImpl(
irFactory.createValueParameter(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor),
descriptor.indexOrMinusOne, type, varargElementType, descriptor.isCrossinline, descriptor.isNoinline
)
@@ -968,7 +970,7 @@ open class SymbolTable(
descriptor,
{ IrLocalDelegatedPropertySymbolImpl(descriptor) },
) {
IrLocalDelegatedPropertyImpl(
irFactory.createLocalDelegatedProperty(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(descriptor), type, descriptor.isVar
).apply {
metadata = MetadataSource.LocalDelegatedProperty(descriptor)
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.MetadataSource
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
@ObsoleteDescriptorBasedAPI
@@ -22,7 +21,7 @@ fun SymbolTable.declareSimpleFunctionWithOverrides(
) =
declareSimpleFunction(descriptor) {
with(descriptor) {
IrFunctionImpl(
irFactory.createFunction(
startOffset, endOffset, origin, it, nameProvider.nameForDeclaration(this),
visibility, modality, IrUninitializedType, isInline, isExternal, isTailrec, isSuspend, isOperator, isInfix, isExpect
).also { declaration ->
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrModuleSeria
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerDesc
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerIr
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.metadata.KlibMetadataIncrementalSerializer
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
@@ -100,6 +101,7 @@ fun generateKLib(
configuration: CompilerConfiguration,
allDependencies: KotlinLibraryResolveResult,
friendDependencies: List<KotlinLibrary>,
irFactory: IrFactory,
outputKlibPath: String,
nopack: Boolean
) {
@@ -138,7 +140,7 @@ fun generateKLib(
val depsDescriptors =
ModulesStructure(project, MainModule.SourceFiles(files), analyzer, configuration, allDependencies, friendDependencies)
val psi2IrContext = runAnalysisAndPreparePsi2Ir(depsDescriptors)
val psi2IrContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory)
val irBuiltIns = psi2IrContext.irBuiltIns
val functionFactory = IrFunctionFactory(irBuiltIns, psi2IrContext.symbolTable)
irBuiltIns.functionFactory = functionFactory
@@ -212,14 +214,15 @@ fun loadIr(
analyzer: AbstractAnalyzerWithCompilerReport,
configuration: CompilerConfiguration,
allDependencies: KotlinLibraryResolveResult,
friendDependencies: List<KotlinLibrary>
friendDependencies: List<KotlinLibrary>,
irFactory: IrFactory,
): IrModuleInfo {
val depsDescriptors = ModulesStructure(project, mainModule, analyzer, configuration, allDependencies, friendDependencies)
val deserializeFakeOverrides = configuration.getBoolean(CommonConfigurationKeys.DESERIALIZE_FAKE_OVERRIDES)
when (mainModule) {
is MainModule.SourceFiles -> {
val psi2IrContext: GeneratorContext = runAnalysisAndPreparePsi2Ir(depsDescriptors)
val psi2IrContext: GeneratorContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory)
val irBuiltIns = psi2IrContext.irBuiltIns
val symbolTable = psi2IrContext.symbolTable
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
@@ -249,7 +252,7 @@ fun loadIr(
val moduleDescriptor = depsDescriptors.getModuleDescriptor(mainModule.lib)
val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler)
val symbolTable = SymbolTable(signaturer)
val symbolTable = SymbolTable(signaturer, irFactory)
val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable)
val typeTranslator = TypeTranslator(
symbolTable,
@@ -284,10 +287,10 @@ fun loadIr(
}
}
private fun runAnalysisAndPreparePsi2Ir(depsDescriptors: ModulesStructure): GeneratorContext {
private fun runAnalysisAndPreparePsi2Ir(depsDescriptors: ModulesStructure, irFactory: IrFactory): GeneratorContext {
val (bindingContext, moduleDescriptor) = depsDescriptors.runAnalysis()
val psi2Ir = Psi2IrTranslator(depsDescriptors.compilerConfiguration.languageVersionSettings, Psi2IrConfiguration())
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc))
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)
return psi2Ir.createGeneratorContext(moduleDescriptor, bindingContext, symbolTable)
}