[FIR2IR] Introduce & use FirBuiltInsPackageFragment

Without this commit, JVM name mapping logic in BE does not work for FIR,
because FIR cannot use old BuiltInsPackageFragmentImpl descriptor.
In this commit we add our own implementation thus fixing
a pack of FIR black box tests.
This commit is contained in:
Mikhail Glukhikh
2020-03-20 13:10:21 +03:00
parent 01558f48ae
commit a4c7619c89
79 changed files with 55 additions and 85 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.fir.backend
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.KotlinBuiltIns.*
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.SourceElement
@@ -14,6 +15,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter
import org.jetbrains.kotlin.fir.descriptors.FirBuiltInsPackageFragment
import org.jetbrains.kotlin.fir.descriptors.FirModuleDescriptor
import org.jetbrains.kotlin.fir.descriptors.FirPackageFragmentDescriptor
import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionStub
@@ -54,6 +56,8 @@ class Fir2IrDeclarationStorage(
private val fragmentCache = mutableMapOf<FqName, IrExternalPackageFragment>()
private val builtInsFragmentCache = mutableMapOf<FqName, IrExternalPackageFragment>()
private val fileCache = mutableMapOf<FirFile, IrFile>()
private val functionCache = mutableMapOf<FirFunction<*>, IrSimpleFunction>()
@@ -110,9 +114,19 @@ class Fir2IrDeclarationStorage(
private fun ConeKotlinType.toIrType(typeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType =
with(typeConverter) { toIrType(typeContext) }
private fun getIrExternalOrBuiltInsPackageFragment(fqName: FqName): IrExternalPackageFragment {
val isBuiltIn = fqName in BUILT_INS_PACKAGE_FQ_NAMES
return if (isBuiltIn) getIrBuiltInsPackageFragment(fqName) else getIrExternalPackageFragment(fqName)
}
private fun getIrBuiltInsPackageFragment(fqName: FqName): IrExternalPackageFragment {
return builtInsFragmentCache.getOrPut(fqName) {
return symbolTable.declareExternalPackageFragment(FirBuiltInsPackageFragment(fqName, moduleDescriptor))
}
}
private fun getIrExternalPackageFragment(fqName: FqName): IrExternalPackageFragment {
return fragmentCache.getOrPut(fqName) {
// TODO: module descriptor is wrong here
return symbolTable.declareExternalPackageFragment(FirPackageFragmentDescriptor(fqName, moduleDescriptor))
}
}
@@ -187,10 +201,12 @@ class Fir2IrDeclarationStorage(
else -> throw AssertionError("Unexpected: $firBasedSymbol")
}
if (containerFile != null) {
fileCache[containerFile]
} else {
getIrExternalPackageFragment(packageFqName)
when {
containerFile != null -> fileCache[containerFile]
firBasedSymbol is FirCallableSymbol -> getIrExternalPackageFragment(packageFqName)
// TODO: All classes from BUILT_INS_PACKAGE_FQ_NAMES are considered built-ins now,
// which is not exact and can lead to some problems
else -> getIrExternalOrBuiltInsPackageFragment(packageFqName)
}
}
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2020 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.fir.descriptors
import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.name.FqName
class FirBuiltInsPackageFragment(
fqName: FqName,
moduleDescriptor: ModuleDescriptor
) : FirPackageFragmentDescriptor(fqName, moduleDescriptor), BuiltInsPackageFragment {
override val isFallback: Boolean
get() = false
}
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
class FirPackageFragmentDescriptor(override val fqName: FqName, val moduleDescriptor: ModuleDescriptor) : PackageFragmentDescriptor {
open class FirPackageFragmentDescriptor(override val fqName: FqName, val moduleDescriptor: ModuleDescriptor) : PackageFragmentDescriptor {
override fun getContainingDeclaration(): ModuleDescriptor {
return moduleDescriptor
}