[Psi2Ir] Isolate type translator in file scope
Create TypeTranslator per file to avoid reusing it between files
This commit is contained in:
committed by
TeamCityServer
parent
ae3a2d4e71
commit
7ef63fbb45
@@ -63,7 +63,6 @@ class Psi2IrTranslator(
|
||||
symbolTable,
|
||||
extensions,
|
||||
typeTranslator,
|
||||
typeTranslator.constantValueGenerator,
|
||||
IrBuiltIns(moduleDescriptor.builtIns, typeTranslator, symbolTable),
|
||||
)
|
||||
}
|
||||
|
||||
+43
-6
@@ -14,25 +14,47 @@ import org.jetbrains.kotlin.descriptors.NotFoundClasses
|
||||
import org.jetbrains.kotlin.ir.builders.IrGeneratorContext
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.expressions.IrDeclarationReference
|
||||
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
|
||||
class GeneratorContext(
|
||||
class GeneratorContext private constructor(
|
||||
val configuration: Psi2IrConfiguration,
|
||||
val moduleDescriptor: ModuleDescriptor,
|
||||
val bindingContext: BindingContext,
|
||||
val languageVersionSettings: LanguageVersionSettings,
|
||||
val symbolTable: SymbolTable,
|
||||
val extensions: GeneratorExtensions,
|
||||
val typeTranslator: TypeTranslatorImpl,
|
||||
val constantValueGenerator: ConstantValueGenerator,
|
||||
override val irBuiltIns: IrBuiltIns
|
||||
val typeTranslator: TypeTranslator,
|
||||
override val irBuiltIns: IrBuiltIns,
|
||||
internal val callToSubstitutedDescriptorMap: MutableMap<IrDeclarationReference, CallableDescriptor>
|
||||
) : IrGeneratorContext {
|
||||
internal val callToSubstitutedDescriptorMap = mutableMapOf<IrDeclarationReference, CallableDescriptor>()
|
||||
|
||||
constructor(
|
||||
configuration: Psi2IrConfiguration,
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
bindingContext: BindingContext,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
symbolTable: SymbolTable,
|
||||
extensions: GeneratorExtensions,
|
||||
typeTranslator: TypeTranslator,
|
||||
irBuiltIns: IrBuiltIns,
|
||||
) : this(
|
||||
configuration,
|
||||
moduleDescriptor,
|
||||
bindingContext,
|
||||
languageVersionSettings,
|
||||
symbolTable,
|
||||
extensions,
|
||||
typeTranslator,
|
||||
irBuiltIns,
|
||||
mutableMapOf()
|
||||
)
|
||||
|
||||
val constantValueGenerator = typeTranslator.constantValueGenerator
|
||||
|
||||
fun IrDeclarationReference.commitSubstituted(descriptor: CallableDescriptor) {
|
||||
callToSubstitutedDescriptorMap[this] = descriptor
|
||||
@@ -42,4 +64,19 @@ class GeneratorContext(
|
||||
val reflectionTypes = ReflectionTypes(moduleDescriptor, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, moduleDescriptor))
|
||||
|
||||
val samTypeApproximator = SamTypeApproximator(moduleDescriptor.builtIns, languageVersionSettings)
|
||||
|
||||
fun createFileScopeContext(ktFile: KtFile): GeneratorContext {
|
||||
return GeneratorContext(
|
||||
configuration,
|
||||
moduleDescriptor,
|
||||
bindingContext,
|
||||
languageVersionSettings,
|
||||
symbolTable,
|
||||
extensions,
|
||||
TypeTranslatorImpl(symbolTable, languageVersionSettings, moduleDescriptor, extensions = extensions, ktFile = ktFile),
|
||||
irBuiltIns,
|
||||
callToSubstitutedDescriptorMap
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,15 +38,13 @@ class ModuleGenerator(
|
||||
override val context: GeneratorContext,
|
||||
private val expectDescriptorToSymbol: MutableMap<DeclarationDescriptor, IrSymbol>? = null
|
||||
) : Generator {
|
||||
private val constantValueGenerator = context.constantValueGenerator
|
||||
|
||||
fun generateModuleFragment(ktFiles: Collection<KtFile>): IrModuleFragment =
|
||||
IrModuleFragmentImpl(context.moduleDescriptor, context.irBuiltIns).also { irModule ->
|
||||
val irDeclarationGenerator = DeclarationGenerator(context)
|
||||
ktFiles.toSet().mapTo(irModule.files) { ktFile ->
|
||||
context.typeTranslator.inFile(ktFile) {
|
||||
generateSingleFile(irDeclarationGenerator, ktFile, irModule)
|
||||
}
|
||||
val fileContext = context.createFileScopeContext(ktFile)
|
||||
val irDeclarationGenerator = DeclarationGenerator(fileContext)
|
||||
generateSingleFile(irDeclarationGenerator, ktFile, irModule)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +55,7 @@ class ModuleGenerator(
|
||||
private fun generateSingleFile(irDeclarationGenerator: DeclarationGenerator, ktFile: KtFile, module: IrModuleFragment): IrFileImpl {
|
||||
val irFile = createEmptyIrFile(ktFile, module)
|
||||
|
||||
val constantValueGenerator = irDeclarationGenerator.context.constantValueGenerator
|
||||
for (ktAnnotationEntry in ktFile.annotationEntries) {
|
||||
val annotationDescriptor = getOrFail(BindingContext.ANNOTATION, ktAnnotationEntry)
|
||||
constantValueGenerator.generateAnnotationConstructorCall(annotationDescriptor)?.let {
|
||||
|
||||
+4
-15
@@ -14,16 +14,16 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
import org.jetbrains.kotlin.types.*
|
||||
|
||||
class TypeTranslatorImpl(
|
||||
open class TypeTranslatorImpl(
|
||||
symbolTable: ReferenceSymbolTable,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
typeParametersResolverBuilder: () -> TypeParametersResolver = { ScopedTypeParametersResolver() },
|
||||
enterTableScope: Boolean = false,
|
||||
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY,
|
||||
private val ktFile: KtFile? = null
|
||||
) : TypeTranslator(symbolTable, languageVersionSettings, typeParametersResolverBuilder, enterTableScope, extensions) {
|
||||
override val constantValueGenerator: ConstantValueGenerator =
|
||||
ConstantValueGeneratorImpl(moduleDescriptor, symbolTable, this)
|
||||
override val constantValueGenerator: ConstantValueGenerator = ConstantValueGeneratorImpl(moduleDescriptor, symbolTable, this)
|
||||
|
||||
private val typeApproximatorForNI = TypeApproximator(moduleDescriptor.builtIns, languageVersionSettings)
|
||||
|
||||
@@ -48,17 +48,6 @@ class TypeTranslatorImpl(
|
||||
|
||||
val psiFile = typeAliasDescriptor.source.getPsi()?.containingFile ?: return false
|
||||
|
||||
return psiFile == currentFile
|
||||
}
|
||||
|
||||
private var currentFile: KtFile? = null
|
||||
|
||||
fun <R> inFile(ktFile: KtFile?, block: () -> R): R {
|
||||
try {
|
||||
currentFile = ktFile
|
||||
return block()
|
||||
} finally {
|
||||
currentFile = null
|
||||
}
|
||||
return psiFile == ktFile
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,11 @@ import java.util.*
|
||||
|
||||
@OptIn(ObsoleteDescriptorBasedAPI::class)
|
||||
abstract class TypeTranslator(
|
||||
private val symbolTable: ReferenceSymbolTable,
|
||||
protected val symbolTable: ReferenceSymbolTable,
|
||||
val languageVersionSettings: LanguageVersionSettings,
|
||||
typeParametersResolverBuilder: () -> TypeParametersResolver = { ScopedTypeParametersResolver() },
|
||||
private val enterTableScope: Boolean = false,
|
||||
private val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
|
||||
protected val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
|
||||
) {
|
||||
abstract val constantValueGenerator: ConstantValueGenerator
|
||||
|
||||
@@ -69,7 +69,7 @@ abstract class TypeTranslator(
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> buildWithScope(container: IrTypeParametersContainer, builder: () -> T): T {
|
||||
fun <T> buildWithScope(container: IrTypeParametersContainer, builder: () -> T): T {
|
||||
enterScope(container)
|
||||
val result = builder()
|
||||
leaveScope(container)
|
||||
@@ -170,6 +170,8 @@ abstract class TypeTranslator(
|
||||
// Abbreviated type's classifier might not be TypeAliasDescriptor in case it's MockClassDescriptor (not found in dependencies).
|
||||
val typeAliasDescriptor = constructor.declarationDescriptor as? TypeAliasDescriptor ?: return null
|
||||
|
||||
// There is possible situation when we have private top-level type alias visible outside its file which is illegal from klib POV.
|
||||
// In that specific case don't generate type abbreviation
|
||||
if (!isTypeAliasAccessibleHere(typeAliasDescriptor)) return null
|
||||
|
||||
return IrTypeAbbreviationImpl(
|
||||
|
||||
Reference in New Issue
Block a user