[IR] Simplify IrFileImpl and IrExternalPackageFragment

#KT-65773 In Progress
This commit is contained in:
Wojciech Litewka
2024-02-29 14:30:25 +01:00
committed by Space Team
parent 16ae7651a2
commit 943be239ee
22 changed files with 103 additions and 149 deletions
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
import org.jetbrains.kotlin.ir.util.transformInPlace
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
/**
* This is a root parent element for external declarations (meaning those that come from
@@ -36,8 +35,6 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Deserializ
abstract class IrExternalPackageFragment : IrPackageFragment() {
abstract override val symbol: IrExternalPackageFragmentSymbol
abstract val containerSource: DeserializedContainerSource?
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitExternalPackageFragment(this, data)
@@ -8,10 +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
import org.jetbrains.kotlin.ir.symbols.IrPackageFragmentSymbol
import org.jetbrains.kotlin.name.FqName
@@ -21,17 +18,6 @@ import org.jetbrains.kotlin.name.FqName
abstract class IrPackageFragment : IrElementBase(), IrDeclarationContainer, IrSymbolOwner {
abstract override val symbol: IrPackageFragmentSymbol
@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(
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2024 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.ir.declarations
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedMemberDescriptor
@ObsoleteDescriptorBasedAPI
val IrPackageFragment.packageFragmentDescriptor: PackageFragmentDescriptor
get() = symbol.descriptor
@OptIn(ObsoleteDescriptorBasedAPI::class)
val IrExternalPackageFragment.containerSource: DeserializedContainerSource?
get() = (symbol.descriptor as? DeserializedMemberDescriptor)?.containerSource
/**
* 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.
*/
@OptIn(ObsoleteDescriptorBasedAPI::class)
val IrPackageFragment.moduleDescriptor
get() = if (this is IrFileImpl && isInsideModule) {
module.descriptor
} else {
packageFragmentDescriptor.containingDeclaration
}
fun createEmptyExternalPackageFragment(module: ModuleDescriptor, fqName: FqName): IrExternalPackageFragment =
IrExternalPackageFragmentImpl(
IrExternalPackageFragmentSymbolImpl(EmptyPackageFragmentDescriptor(module, fqName)), fqName
)
@@ -1,38 +1,21 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2024 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.ir.declarations.impl
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedMemberDescriptor
class IrExternalPackageFragmentImpl(
override val symbol: IrExternalPackageFragmentSymbol,
override var packageFqName: FqName
override var packageFqName: FqName,
) : IrExternalPackageFragment() {
override val startOffset: Int
get() = UNDEFINED_OFFSET
@@ -44,25 +27,18 @@ class IrExternalPackageFragmentImpl(
symbol.bind(this)
}
@ObsoleteDescriptorBasedAPI
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = symbol.descriptor
@OptIn(ObsoleteDescriptorBasedAPI::class)
override val moduleDescriptor: ModuleDescriptor
get() = packageFragmentDescriptor.containingDeclaration
@UnsafeDuringIrConstructionAPI
override val declarations: MutableList<IrDeclaration> = ArrayList()
@OptIn(ObsoleteDescriptorBasedAPI::class)
override val containerSource: DeserializedContainerSource?
get() = (symbol.descriptor as? DeserializedMemberDescriptor)?.containerSource
companion object {
fun createEmptyExternalPackageFragment(module: ModuleDescriptor, fqName: FqName): IrExternalPackageFragment =
IrExternalPackageFragmentImpl(
IrExternalPackageFragmentSymbolImpl(EmptyPackageFragmentDescriptor(module, fqName)), fqName
@Deprecated(
message = "Use org.jetbrains.kotlin.ir.declarations.createEmptyExternalPackageFragment instead",
replaceWith = ReplaceWith(
"createEmptyExternalPackageFragment",
"org.jetbrains.kotlin.ir.declarations.createEmptyExternalPackageFragment"
)
)
fun createEmptyExternalPackageFragment(module: ModuleDescriptor, fqName: FqName): IrExternalPackageFragment =
org.jetbrains.kotlin.ir.declarations.createEmptyExternalPackageFragment(module, fqName)
}
}
@@ -1,25 +1,12 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2024 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.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
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
@@ -61,25 +48,15 @@ class IrFileImpl(
override lateinit var module: IrModuleFragment
internal val isInsideModule: Boolean
get() = ::module.isInitialized
override val startOffset: Int
get() = 0
override val endOffset: Int
get() = fileEntry.maxOffset
@ObsoleteDescriptorBasedAPI
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
}
@UnsafeDuringIrConstructionAPI
override val declarations: MutableList<IrDeclaration> = ArrayList()
@@ -1,17 +1,17 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.ir.types.impl
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.IrImplementationDetail
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrFactoryImpl
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrFileSymbol
@@ -59,11 +59,6 @@ private object ErrorFile : IrFile() {
get() = shouldNotBeCalled()
set(_) {}
@ObsoleteDescriptorBasedAPI
override val packageFragmentDescriptor: PackageFragmentDescriptor
get() = shouldNotBeCalled()
override val moduleDescriptor: ModuleDescriptor
get() = shouldNotBeCalled()
override var packageFqName: FqName
get() = FqName.ROOT
set(_) = shouldNotBeCalled()
@@ -612,17 +612,6 @@ object IrTree : AbstractTreeBuilder() {
parent(symbolOwner)
+symbol(packageFragmentSymbolType)
+field("packageFragmentDescriptor", type(Packages.descriptors, "PackageFragmentDescriptor"), mutable = false) {
optInAnnotation = obsoleteDescriptorBasedApiAnnotation
}
+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>()) {
defaultValueInBase = "packageFqName"
@@ -660,7 +649,6 @@ object IrTree : AbstractTreeBuilder() {
parent(packageFragment)
+symbol(externalPackageFragmentSymbolType)
+field("containerSource", type<DeserializedContainerSource>(), nullable = true, mutable = false)
}
val file: Element by element(Declaration) {
needTransformMethod()