IR: move frontend-dependent code into implementations in psi2ir

This commit is contained in:
Alexander Udalov
2021-02-21 15:41:48 +01:00
parent d991a3e40f
commit addabae8d2
22 changed files with 167 additions and 73 deletions
@@ -22,12 +22,17 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl import org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
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.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.PsiSourceManager import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.psi2ir.generators.generateTypicalIrProviderList
class Fir2IrConverter( class Fir2IrConverter(
private val moduleDescriptor: FirModuleDescriptor, private val moduleDescriptor: FirModuleDescriptor,
@@ -256,7 +261,8 @@ class Fir2IrConverter(
): Fir2IrResult { ): Fir2IrResult {
val moduleDescriptor = FirModuleDescriptor(session) val moduleDescriptor = FirModuleDescriptor(session)
val symbolTable = SymbolTable(signaturer, irFactory) val symbolTable = SymbolTable(signaturer, irFactory)
val typeTranslator = TypeTranslator(symbolTable, languageVersionSettings, moduleDescriptor, extensions = generatorExtensions) val typeTranslator =
TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor, extensions = generatorExtensions)
val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable) val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
FirBuiltinSymbols(irBuiltIns, moduleDescriptor.builtIns, symbolTable) FirBuiltinSymbols(irBuiltIns, moduleDescriptor.builtIns, symbolTable)
val sourceManager = PsiSourceManager() val sourceManager = PsiSourceManager()
@@ -36,6 +36,8 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.psi2ir.PsiSourceManager import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
import org.jetbrains.kotlin.psi2ir.generators.generateTypicalIrProviderList
import org.jetbrains.kotlin.resolve.CleanableBindingContext import org.jetbrains.kotlin.resolve.CleanableBindingContext
class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory { class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory {
@@ -68,9 +70,8 @@ class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory
val functionFactory = IrFunctionFactory(psi2irContext.irBuiltIns, symbolTable) val functionFactory = IrFunctionFactory(psi2irContext.irBuiltIns, symbolTable)
psi2irContext.irBuiltIns.functionFactory = functionFactory psi2irContext.irBuiltIns.functionFactory = functionFactory
val stubGenerator = DeclarationStubGenerator( val stubGenerator =
psi2irContext.moduleDescriptor, symbolTable, psi2irContext.irBuiltIns.languageVersionSettings, extensions DeclarationStubGeneratorImpl(psi2irContext.moduleDescriptor, symbolTable, state.languageVersionSettings, extensions)
)
val frontEndContext = object : TranslationPluginContext { val frontEndContext = object : TranslationPluginContext {
override val moduleDescriptor: ModuleDescriptor override val moduleDescriptor: ModuleDescriptor
get() = psi2irContext.moduleDescriptor get() = psi2irContext.moduleDescriptor
@@ -224,13 +225,13 @@ class JvmIrCodegenFactory(private val phaseConfig: PhaseConfig) : CodegenFactory
backendExtension: JvmBackendExtension, backendExtension: JvmBackendExtension,
notifyCodegenStart: () -> Unit notifyCodegenStart: () -> Unit
) { ) {
val irProviders = configureBuiltInsAndgenerateIrProvidersInFrontendIRMode(irModuleFragment, symbolTable, extensions) val irProviders = configureBuiltInsAndGenerateIrProvidersInFrontendIRMode(irModuleFragment, symbolTable, extensions)
doGenerateFilesInternal( doGenerateFilesInternal(
JvmIrBackendInput(state, irModuleFragment, symbolTable, sourceManager, phaseConfig, irProviders, extensions, backendExtension, notifyCodegenStart) JvmIrBackendInput(state, irModuleFragment, symbolTable, sourceManager, phaseConfig, irProviders, extensions, backendExtension, notifyCodegenStart)
) )
} }
fun configureBuiltInsAndgenerateIrProvidersInFrontendIRMode( fun configureBuiltInsAndGenerateIrProvidersInFrontendIRMode(
irModuleFragment: IrModuleFragment, irModuleFragment: IrModuleFragment,
symbolTable: SymbolTable, symbolTable: SymbolTable,
extensions: JvmGeneratorExtensions extensions: JvmGeneratorExtensions
@@ -17,11 +17,11 @@ import org.jetbrains.kotlin.ir.backend.js.MainModule
import org.jetbrains.kotlin.ir.backend.js.loadIr import org.jetbrains.kotlin.ir.backend.js.loadIr
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
import org.jetbrains.kotlin.ir.util.patchDeclarationParents import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.resolver.KotlinLibraryResolveResult import org.jetbrains.kotlin.library.resolver.KotlinLibraryResolveResult
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi2ir.generators.generateTypicalIrProviderList
import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToBinary import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToBinary
import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToText import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToText
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.SmartList
@@ -52,7 +53,7 @@ class Psi2IrTranslator(
symbolTable: SymbolTable, symbolTable: SymbolTable,
extensions: GeneratorExtensions = GeneratorExtensions() extensions: GeneratorExtensions = GeneratorExtensions()
): GeneratorContext { ): GeneratorContext {
val typeTranslator = TypeTranslator(symbolTable, languageVersionSettings, moduleDescriptor, extensions = extensions) val typeTranslator = TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor, extensions = extensions)
return GeneratorContext( return GeneratorContext(
configuration, configuration,
moduleDescriptor, moduleDescriptor,
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2021 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.psi2ir.generators
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class ConstantValueGeneratorImpl(
moduleDescriptor: ModuleDescriptor,
symbolTable: ReferenceSymbolTable,
typeTranslator: TypeTranslator,
) : ConstantValueGenerator(moduleDescriptor, symbolTable, typeTranslator) {
override fun extractAnnotationOffsets(annotationDescriptor: AnnotationDescriptor): Pair<Int, Int> {
val psi = annotationDescriptor.source.safeAs<PsiSourceElement>()?.psi
if (psi == null || psi.containingFile.fileType.isBinary) return UNDEFINED_OFFSET to UNDEFINED_OFFSET
return Pair(psi.startOffset, psi.endOffset)
}
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2021 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.psi2ir.generators
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.declarations.lazy.LazyScopedTypeParametersResolver
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
class DeclarationStubGeneratorImpl(
moduleDescriptor: ModuleDescriptor,
symbolTable: SymbolTable,
languageVersionSettings: LanguageVersionSettings,
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY,
) : DeclarationStubGenerator(moduleDescriptor, symbolTable, extensions) {
override val typeTranslator: TypeTranslator =
TypeTranslatorImpl(
lazyTable,
languageVersionSettings,
moduleDescriptor,
{ LazyScopedTypeParametersResolver(lazyTable) },
true,
extensions
)
}
// In most cases, IrProviders list consist of an optional deserializer and a DeclarationStubGenerator.
fun generateTypicalIrProviderList(
moduleDescriptor: ModuleDescriptor,
irBuiltins: IrBuiltIns,
symbolTable: SymbolTable,
deserializer: IrDeserializer? = null,
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
): List<IrProvider> {
val stubGenerator = DeclarationStubGeneratorImpl(
moduleDescriptor, symbolTable, irBuiltins.languageVersionSettings, extensions
)
return listOfNotNull(deserializer, stubGenerator)
}
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
import org.jetbrains.kotlin.ir.util.patchDeclarationParents import org.jetbrains.kotlin.ir.util.patchDeclarationParents
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2021 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.psi2ir.generators
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.types.CommonSupertypes
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeApproximator
class TypeTranslatorImpl(
symbolTable: ReferenceSymbolTable,
languageVersionSettings: LanguageVersionSettings,
moduleDescriptor: ModuleDescriptor,
typeParametersResolverBuilder: () -> TypeParametersResolver = { ScopedTypeParametersResolver() },
enterTableScope: Boolean = false,
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY,
) : TypeTranslator(symbolTable, languageVersionSettings, typeParametersResolverBuilder, enterTableScope, extensions) {
override val constantValueGenerator: ConstantValueGenerator =
ConstantValueGeneratorImpl(moduleDescriptor, symbolTable, this)
private val typeApproximatorForNI = TypeApproximator(moduleDescriptor.builtIns)
override fun approximateType(type: KotlinType): KotlinType =
typeApproximatorForNI.approximateDeclarationType(type, local = false, languageVersionSettings)
override fun commonSupertype(types: Collection<KotlinType>): KotlinType =
CommonSupertypes.commonSupertype(types)
}
@@ -14,21 +14,19 @@ import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.constants.* import org.jetbrains.kotlin.resolve.constants.*
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isError import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.typeUtil.builtIns import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
class ConstantValueGenerator( abstract class ConstantValueGenerator(
private val moduleDescriptor: ModuleDescriptor, private val moduleDescriptor: ModuleDescriptor,
private val symbolTable: ReferenceSymbolTable, private val symbolTable: ReferenceSymbolTable,
private val typeTranslator: TypeTranslator, private val typeTranslator: TypeTranslator,
) { ) {
protected abstract fun extractAnnotationOffsets(annotationDescriptor: AnnotationDescriptor): Pair<Int, Int>
private fun KotlinType.toIrType() = typeTranslator.translateType(this) private fun KotlinType.toIrType() = typeTranslator.translateType(this)
fun generateConstantValueAsExpression( fun generateConstantValueAsExpression(
@@ -141,9 +139,7 @@ class ConstantValueGenerator(
?: throw AssertionError("No constructor for annotation class $annotationClassDescriptor") ?: throw AssertionError("No constructor for annotation class $annotationClassDescriptor")
val primaryConstructorSymbol = symbolTable.referenceConstructor(primaryConstructorDescriptor) val primaryConstructorSymbol = symbolTable.referenceConstructor(primaryConstructorDescriptor)
val psi = annotationDescriptor.source.safeAs<PsiSourceElement>()?.psi val (startOffset, endOffset) = extractAnnotationOffsets(annotationDescriptor)
val startOffset = psi?.takeUnless { it.containingFile.fileType.isBinary }?.startOffset ?: UNDEFINED_OFFSET
val endOffset = psi?.takeUnless { it.containingFile.fileType.isBinary }?.endOffset ?: UNDEFINED_OFFSET
val irCall = IrConstructorCallImpl( val irCall = IrConstructorCallImpl(
startOffset, endOffset, startOffset, endOffset,
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.ir.util package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
@@ -38,13 +37,12 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
class DeclarationStubGenerator( abstract class DeclarationStubGenerator(
val moduleDescriptor: ModuleDescriptor, val moduleDescriptor: ModuleDescriptor,
val symbolTable: SymbolTable, val symbolTable: SymbolTable,
languageVersionSettings: LanguageVersionSettings,
val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY, val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY,
) : IrProvider { ) : IrProvider {
private val lazyTable = symbolTable.lazyWrapper protected val lazyTable = symbolTable.lazyWrapper
var unboundSymbolGeneration: Boolean var unboundSymbolGeneration: Boolean
get() = lazyTable.stubGenerator != null get() = lazyTable.stubGenerator != null
@@ -52,15 +50,7 @@ class DeclarationStubGenerator(
lazyTable.stubGenerator = if (value) this else null lazyTable.stubGenerator = if (value) this else null
} }
val typeTranslator: TypeTranslator = abstract val typeTranslator: TypeTranslator
TypeTranslator(
lazyTable,
languageVersionSettings,
moduleDescriptor,
{ LazyScopedTypeParametersResolver(lazyTable) },
true,
extensions
)
private val facadeClassMap = mutableMapOf<DeserializedContainerSource, IrClass?>() private val facadeClassMap = mutableMapOf<DeserializedContainerSource, IrClass?>()
@@ -17,10 +17,7 @@
package org.jetbrains.kotlin.ir.util package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.analyzer.CompilationErrorException import org.jetbrains.kotlin.analyzer.CompilationErrorException
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.linkage.IrProvider import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.linkage.KotlinIrLinkerInternalException import org.jetbrains.kotlin.ir.linkage.KotlinIrLinkerInternalException
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
@@ -63,17 +60,3 @@ fun List<IrProvider>.getDeclaration(symbol: IrSymbol): IrDeclaration? =
firstNotNullResult { provider -> firstNotNullResult { provider ->
provider.getDeclaration(symbol) provider.getDeclaration(symbol)
} }
// In most cases, IrProviders list consist of an optional deserializer and a DeclarationStubGenerator.
fun generateTypicalIrProviderList(
moduleDescriptor: ModuleDescriptor,
irBuiltins: IrBuiltIns,
symbolTable: SymbolTable,
deserializer: IrDeserializer? = null,
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
): List<IrProvider> {
val stubGenerator = DeclarationStubGenerator(
moduleDescriptor, symbolTable, irBuiltins.languageVersionSettings, extensions
)
return listOfNotNull(deserializer, stubGenerator)
}
@@ -26,22 +26,23 @@ import org.jetbrains.kotlin.utils.threadLocal
import java.util.* import java.util.*
@OptIn(ObsoleteDescriptorBasedAPI::class) @OptIn(ObsoleteDescriptorBasedAPI::class)
class TypeTranslator( abstract class TypeTranslator(
private val symbolTable: ReferenceSymbolTable, private val symbolTable: ReferenceSymbolTable,
val languageVersionSettings: LanguageVersionSettings, val languageVersionSettings: LanguageVersionSettings,
moduleDescriptor: ModuleDescriptor,
typeParametersResolverBuilder: () -> TypeParametersResolver = { ScopedTypeParametersResolver() }, typeParametersResolverBuilder: () -> TypeParametersResolver = { ScopedTypeParametersResolver() },
private val enterTableScope: Boolean = false, private val enterTableScope: Boolean = false,
private val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY private val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
) { ) {
val constantValueGenerator: ConstantValueGenerator = ConstantValueGenerator(moduleDescriptor, symbolTable, this) abstract val constantValueGenerator: ConstantValueGenerator
protected abstract fun approximateType(type: KotlinType): KotlinType
protected abstract fun commonSupertype(types: Collection<KotlinType>): KotlinType
private val typeParametersResolver by threadLocal { typeParametersResolverBuilder() } private val typeParametersResolver by threadLocal { typeParametersResolverBuilder() }
private val erasureStack = Stack<PropertyDescriptor>() private val erasureStack = Stack<PropertyDescriptor>()
private val typeApproximatorForNI = TypeApproximator(moduleDescriptor.builtIns)
fun enterScope(irElement: IrTypeParametersContainer) { fun enterScope(irElement: IrTypeParametersContainer) {
typeParametersResolver.enterTypeParameterScope(irElement) typeParametersResolver.enterTypeParameterScope(irElement)
if (enterTableScope) { if (enterTableScope) {
@@ -155,7 +156,7 @@ class TypeTranslator(
} }
private fun approximateUpperBounds(upperBounds: Collection<KotlinType>, variance: Variance): IrTypeProjection { private fun approximateUpperBounds(upperBounds: Collection<KotlinType>, variance: Variance): IrTypeProjection {
val commonSupertype = CommonSupertypes.commonSupertype(upperBounds) val commonSupertype = commonSupertype(upperBounds)
return translateType(approximate(commonSupertype.replaceArgumentsWithStarProjections()), variance) return translateType(approximate(commonSupertype.replaceArgumentsWithStarProjections()), variance)
} }
@@ -179,7 +180,7 @@ class TypeTranslator(
// That's what old back-end effectively does. // That's what old back-end effectively does.
val typeConstructor = properlyApproximatedType.constructor val typeConstructor = properlyApproximatedType.constructor
if (typeConstructor is IntersectionTypeConstructor) { if (typeConstructor is IntersectionTypeConstructor) {
val commonSupertype = CommonSupertypes.commonSupertype(typeConstructor.supertypes) val commonSupertype = commonSupertype(typeConstructor.supertypes)
return approximate(commonSupertype.replaceArgumentsWithStarProjections()) return approximate(commonSupertype.replaceArgumentsWithStarProjections())
} }
@@ -194,11 +195,7 @@ class TypeTranslator(
if (ktType.constructor.isDenotable && ktType.arguments.isEmpty()) if (ktType.constructor.isDenotable && ktType.arguments.isEmpty())
ktType ktType
else else
typeApproximatorForNI.approximateDeclarationType( approximateType(ktType)
ktType,
local = false,
languageVersionSettings = languageVersionSettings
)
} else { } else {
// Hack to preserve *-projections in arguments in OI. // Hack to preserve *-projections in arguments in OI.
// Expected to be removed as soon as OI is deprecated. // Expected to be removed as soon as OI is deprecated.
@@ -41,7 +41,10 @@ import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
import org.jetbrains.kotlin.ir.linkage.IrDeserializer import org.jetbrains.kotlin.ir.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.noUnboundLeft
import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult
import org.jetbrains.kotlin.js.config.ErrorTolerancePolicy import org.jetbrains.kotlin.js.config.ErrorTolerancePolicy
@@ -60,6 +63,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContextUtils import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
@@ -261,7 +265,7 @@ fun loadIr(
val signaturer = IdSignatureDescriptor(mangler) val signaturer = IdSignatureDescriptor(mangler)
val symbolTable = SymbolTable(signaturer, irFactory) val symbolTable = SymbolTable(signaturer, irFactory)
val typeTranslator = val typeTranslator =
TypeTranslator(symbolTable, depsDescriptors.compilerConfiguration.languageVersionSettings, moduleDescriptor) TypeTranslatorImpl(symbolTable, depsDescriptors.compilerConfiguration.languageVersionSettings, moduleDescriptor)
val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable) val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable) val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
val irLinker = val irLinker =
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
import org.jetbrains.kotlin.test.backend.ir.IrBackendInput import org.jetbrains.kotlin.test.backend.ir.IrBackendInput
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives import org.jetbrains.kotlin.test.directives.CodegenTestDirectives
import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_EXTERNAL_CLASS import org.jetbrains.kotlin.test.directives.CodegenTestDirectives.DUMP_EXTERNAL_CLASS
@@ -86,7 +87,7 @@ class IrTextDumpHandler(testServices: TestServices) : AbstractIrHandler(testServ
val mangler = JsManglerDesc val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler) val signaturer = IdSignatureDescriptor(mangler)
val irModule = info.backendInput.irModuleFragment val irModule = info.backendInput.irModuleFragment
val stubGenerator = DeclarationStubGenerator( val stubGenerator = DeclarationStubGeneratorImpl(
irModule.descriptor, irModule.descriptor,
SymbolTable(signaturer, IrFactoryImpl), // TODO SymbolTable(signaturer, IrFactoryImpl), // TODO
module.languageVersionSettings module.languageVersionSettings
@@ -76,7 +76,7 @@ class Fir2IrResultsConverter(
).build() ).build()
irModuleFragment.irBuiltins.functionFactory = IrFunctionFactory(irModuleFragment.irBuiltins, symbolTable) irModuleFragment.irBuiltins.functionFactory = IrFunctionFactory(irModuleFragment.irBuiltins, symbolTable)
val irProviders = codegenFactory.configureBuiltInsAndgenerateIrProvidersInFrontendIRMode( val irProviders = codegenFactory.configureBuiltInsAndGenerateIrProvidersInFrontendIRMode(
irModuleFragment, irModuleFragment,
symbolTable, symbolTable,
extensions extensions
@@ -34,12 +34,12 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.IdSignatureComposer import org.jetbrains.kotlin.ir.util.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.NameProvider import org.jetbrains.kotlin.ir.util.NameProvider
import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
import org.jetbrains.kotlin.psi2ir.generators.generateTypicalIrProviderList
import org.jetbrains.kotlin.resolve.AnalyzingUtils import org.jetbrains.kotlin.resolve.AnalyzingUtils
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.CompilerEnvironment import org.jetbrains.kotlin.resolve.CompilerEnvironment
@@ -48,7 +48,6 @@ import org.jetbrains.kotlin.test.ConfigurationKind
import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.util.KtTestUtil.getAnnotationsJar import org.jetbrains.kotlin.test.util.KtTestUtil.getAnnotationsJar
import java.io.File import java.io.File
import java.util.*
abstract class AbstractIrGeneratorTestCase : CodegenTestCase() { abstract class AbstractIrGeneratorTestCase : CodegenTestCase() {
override fun doMultiFileTest(wholeFile: File, files: List<TestFile>) { override fun doMultiFileTest(wholeFile: File, files: List<TestFile>) {
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
import org.jetbrains.kotlin.scripting.compiler.plugin.loadScriptConfiguration import org.jetbrains.kotlin.scripting.compiler.plugin.loadScriptConfiguration
import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
@@ -54,7 +55,7 @@ abstract class AbstractIrTextTestCase : AbstractIrGeneratorTestCase() {
val mangler = JsManglerDesc val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler) val signaturer = IdSignatureDescriptor(mangler)
val stubGenerator = DeclarationStubGenerator( val stubGenerator = DeclarationStubGeneratorImpl(
irModule.descriptor, irModule.descriptor,
SymbolTable(signaturer, IrFactoryImpl), // TODO SymbolTable(signaturer, IrFactoryImpl), // TODO
myEnvironment.configuration.languageVersionSettings myEnvironment.configuration.languageVersionSettings
+1
View File
@@ -37,6 +37,7 @@ dependencies {
wasmApi(kotlinStdlib()) wasmApi(kotlinStdlib())
interpreterApi(project(":compiler:ir.tree")) interpreterApi(project(":compiler:ir.tree"))
interpreterApi(project(":compiler:ir.tree.impl")) interpreterApi(project(":compiler:ir.tree.impl"))
interpreterApi(project(":compiler:ir.psi2ir"))
testCompile(builtinsSourceSet.output) testCompile(builtinsSourceSet.output)
testCompile(evaluateSourceSet.output) testCompile(evaluateSourceSet.output)
@@ -18,8 +18,11 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.Printer
import java.io.File import java.io.File
@@ -202,6 +205,6 @@ private fun getIrBuiltIns(): IrBuiltIns {
override fun composeEnumEntrySignature(descriptor: ClassDescriptor): IdSignature? = null override fun composeEnumEntrySignature(descriptor: ClassDescriptor): IdSignature? = null
} }
val symbolTable = SymbolTable(signaturer, IrFactoryImpl) val symbolTable = SymbolTable(signaturer, IrFactoryImpl)
val typeTranslator = TypeTranslator(symbolTable, languageSettings, moduleDescriptor) val typeTranslator = TypeTranslatorImpl(symbolTable, languageSettings, moduleDescriptor)
return IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable) return IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
} }
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStat
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.CompilerEnvironment import org.jetbrains.kotlin.resolve.CompilerEnvironment
import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource
@@ -513,7 +514,7 @@ class GenerateIrRuntime {
val mangler = JsManglerDesc val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler) val signaturer = IdSignatureDescriptor(mangler)
val symbolTable = SymbolTable(signaturer, PersistentIrFactory()) val symbolTable = SymbolTable(signaturer, PersistentIrFactory())
val typeTranslator = TypeTranslator(symbolTable, languageVersionSettings, moduleDescriptor) val typeTranslator = TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor)
val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable) val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable) val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
@@ -540,7 +541,7 @@ class GenerateIrRuntime {
val mangler = JsManglerDesc val mangler = JsManglerDesc
val signaturer = IdSignatureDescriptor(mangler) val signaturer = IdSignatureDescriptor(mangler)
val symbolTable = SymbolTable(signaturer, PersistentIrFactory()) val symbolTable = SymbolTable(signaturer, PersistentIrFactory())
val typeTranslator = TypeTranslator(symbolTable, languageVersionSettings, moduleDescriptor) val typeTranslator = TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor)
val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable) val irBuiltIns = IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable)
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable) val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
@@ -21,10 +21,10 @@ import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.generateTypicalIrProviderList
import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
import org.jetbrains.kotlin.psi2ir.generators.generateTypicalIrProviderList
import org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplCodeAnalyzerBase import org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplCodeAnalyzerBase
import org.jetbrains.kotlin.serialization.js.ModuleKind import org.jetbrains.kotlin.serialization.js.ModuleKind
import kotlin.script.experimental.api.valueOr import kotlin.script.experimental.api.valueOr
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.descriptors.IrFunctionFactory
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl
import org.jetbrains.kotlin.serialization.js.ModuleKind import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
@@ -41,7 +42,7 @@ class JsScriptDependencyCompiler(
it.initialize(PackageFragmentProvider.Empty) it.initialize(PackageFragmentProvider.Empty)
} }
val typeTranslator = TypeTranslator(symbolTable, languageVersionSettings, moduleDescriptor) val typeTranslator = TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor)
val irBuiltIns = IrBuiltIns(builtIns, typeTranslator, symbolTable) val irBuiltIns = IrBuiltIns(builtIns, typeTranslator, symbolTable)
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable) val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
irBuiltIns.functionFactory = functionFactory irBuiltIns.functionFactory = functionFactory