[IR] Add a method to get ModuleDescriptor from IrPackageFragment

Ideally, instead of this method, there should be a link
to IrModuleFragment. Unfortunately, it would require to big refactoring,
as some of IrPackageFragment implementations doesn't have any
IrModuleFragment inside, and are not located inside any
IrModuleFragment.

So for now, we just implement and use everywhere a single way of
getting the module descriptor, which respects a IrModuleFragment
link if it exists, and fallbacks to descriptor-based method
if it doesn't.

^KT-62623
This commit is contained in:
Pavel Kunyavskiy
2023-10-16 17:13:02 +02:00
committed by Space Team
parent 9b9ddb760a
commit ad5831acc6
14 changed files with 50 additions and 30 deletions
@@ -8,6 +8,7 @@
package org.jetbrains.kotlin.ir.declarations
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
@@ -25,6 +26,15 @@ abstract class IrPackageFragment : IrElementBase(), IrDeclarationContainer, IrSy
@ObsoleteDescriptorBasedAPI
abstract val packageFragmentDescriptor: PackageFragmentDescriptor
/**
* This should be a link to [IrModuleFragment] instead.
*
* Unfortunately, some package fragments (e.g. some synthetic ones and
* [IrExternalPackageFragment])
* are not located in any IR module, but still have a module descriptor.
*/
abstract val moduleDescriptor: ModuleDescriptor
abstract var packageFqName: FqName
@Deprecated(
@@ -47,6 +47,10 @@ class IrExternalPackageFragmentImpl(
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = symbol.descriptor
@OptIn(ObsoleteDescriptorBasedAPI::class)
override val moduleDescriptor: ModuleDescriptor
get() = packageFragmentDescriptor.containingDeclaration
override val declarations: MutableList<IrDeclaration> = ArrayList()
@OptIn(ObsoleteDescriptorBasedAPI::class)
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.ir.declarations.impl
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
@@ -69,6 +70,15 @@ class IrFileImpl(
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = symbol.descriptor
@OptIn(ObsoleteDescriptorBasedAPI::class)
override val moduleDescriptor: ModuleDescriptor
get() {
return if (this::module.isInitialized)
module.descriptor
else
packageFragmentDescriptor.containingDeclaration
}
override val declarations: MutableList<IrDeclaration> = ArrayList()
override var annotations: List<IrConstructorCall> = emptyList()
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.ir.overrides
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
@@ -94,14 +93,13 @@ abstract class FakeOverrideBuilderStrategy(
protected abstract fun linkPropertyFakeOverride(property: IrPropertyWithLateBinding, manglerCompatibleMode: Boolean)
}
@OptIn(ObsoleteDescriptorBasedAPI::class) // Because of the LazyIR, have to use descriptors here.
private fun IrOverridableMember.isPrivateToThisModule(
thisClass: IrClass, memberClass: IrClass, friendModules: Map<String, Collection<String>>,
): Boolean {
if (visibility != DescriptorVisibilities.INTERNAL) return false
val thisModule = thisClass.getPackageFragment().packageFragmentDescriptor.containingDeclaration
val memberModule = memberClass.getPackageFragment().packageFragmentDescriptor.containingDeclaration
val thisModule = thisClass.getPackageFragment().moduleDescriptor
val memberModule = memberClass.getPackageFragment().moduleDescriptor
return thisModule != memberModule && !isInFriendModules(thisModule, memberModule, friendModules)
}
@@ -5,10 +5,7 @@
package org.jetbrains.kotlin.ir.types.impl
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
@@ -50,6 +47,8 @@ object IrErrorClassImpl : IrClassImpl(
@ObsoleteDescriptorBasedAPI
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = TODO("Not yet implemented")
override val moduleDescriptor: ModuleDescriptor
get() = TODO("Not yet implemented")
override var packageFqName: FqName
get() = FqName.ROOT
set(_) = TODO("Not yet implemented")
@@ -263,7 +263,7 @@ private class KotlinLikeDumper(val p: Printer, val options: KotlinLikeDumpOption
if (options.printFileName) p.println("// FILE: ${declaration.name}")
if (options.printFilePath) p.println("// path: ${declaration.path}")
declaration.printlnAnnotations("file")
val packageFqName = declaration.packageFragmentDescriptor.fqName
val packageFqName = declaration.packageFqName
if (!packageFqName.isRoot) {
p.println("package ${packageFqName.asString()}")
}
@@ -618,6 +618,14 @@ object IrTree : AbstractTreeBuilder() {
+symbol(packageFragmentSymbolType)
+field("packageFragmentDescriptor", type(Packages.descriptors, "PackageFragmentDescriptor"), mutable = false)
+field("moduleDescriptor", type(Packages.descriptors, "ModuleDescriptor"), mutable = false) {
kdoc = """
This should be a link to [IrModuleFragment] instead.
Unfortunately, some package fragments (e.g. some synthetic ones and [IrExternalPackageFragment])
are not located in any IR module, but still have a module descriptor.
""".trimIndent()
}
+field("packageFqName", type<FqName>())
+field("fqName", type<FqName>()) {
baseGetter = code("packageFqName")
@@ -181,14 +181,14 @@ abstract class KotlinIrLinker(
override fun tryReferencingSimpleFunctionByLocalSignature(parent: IrDeclaration, idSignature: IdSignature): IrSimpleFunctionSymbol? {
if (idSignature.isPubliclyVisible) return null
val file = getFileOf(parent)
val moduleDescriptor = file.packageFragmentDescriptor.containingDeclaration
val moduleDescriptor = file.moduleDescriptor
return resolveModuleDeserializer(moduleDescriptor, null).referenceSimpleFunctionByLocalSignature(file, idSignature)
}
override fun tryReferencingPropertyByLocalSignature(parent: IrDeclaration, idSignature: IdSignature): IrPropertySymbol? {
if (idSignature.isPubliclyVisible) return null
val file = getFileOf(parent)
val moduleDescriptor = file.packageFragmentDescriptor.containingDeclaration
val moduleDescriptor = file.moduleDescriptor
return resolveModuleDeserializer(moduleDescriptor, null).referencePropertyByLocalSignature(file, idSignature)
}