Skip to content

Commit

Permalink
[IR][JS][Wasm] Use unified lowering to remove expect declarations
Browse files Browse the repository at this point in the history
ExpectDeclarationsRemoveLowering uses an overkill method to remove expect declarations: 
it transforms declarations with ExpectDeclarationRemover(symbolTable, doRemove=true).
In contrast, Native version of this removing, ExpectDeclarationsRemoving, uses simplest way to remove top-level declarations.
In this MR, this approach is used for all Klib backends.

^KT-73063 Fixed

Merge-request: KT-MR-19026
Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
  • Loading branch information
vsukharev authored and Space Team committed Nov 22, 2024
1 parent 09b5674 commit d039714
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@

package org.jetbrains.kotlin.backend.common.lower

import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.LoweringContext
import org.jetbrains.kotlin.ir.declarations.*

/**
* This pass removes all declarations with `isExpect == true`.
* This pass removes all declarations with `isExpect == true`, which usually come as unactualized optional expectations, like
* - when compiling Native stdlib cache: the following comes from `libraries/stdlib/common/src/kotlin/JsAnnotationsH.kt`
* @OptionalExpectation public expect annotation class JsName(val name: String)
* - when compiling WASM: the following comes from `libraries/stdlib/common/src/kotlin/JsAnnotationsH.kt`
* @OptionalExpectation public expect annotation class JsFileName(val name: String)
* - when compiling JS: the following comes from `libraries/stdlib/common/src/kotlin/JvmAnnotationsH.kt`
* @OptionalExpectation public expect annotation class JvmMultifileClass()
*/
class ExpectDeclarationsRemoveLowering(symbolTable: ReferenceSymbolTable) : DeclarationTransformer {
private val remover = ExpectDeclarationRemover(symbolTable, true)

override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
return remover.transformFlat(declaration)
class ExpectDeclarationsRemoveLowering(val context: LoweringContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
// All declarations with `isExpect == true` are nested into a top-level declaration with `isExpect == true`.
irFile.declarations.removeAll {
when (it) {
is IrClass -> it.isExpect
is IrFunction -> it.isExpect
is IrProperty -> it.isExpect
else -> false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ private val annotationInstantiationLowering = makeIrModulePhase(
)

private val expectDeclarationsRemovingPhase = makeIrModulePhase(
{ context: JsIrBackendContext ->
ExpectDeclarationsRemoveLowering(context.symbolTable)
},
::ExpectDeclarationsRemoveLowering,
name = "ExpectDeclarationsRemoving",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ private val generateTestsIC = makeIrModulePhase(
)

private val expectDeclarationsRemovingPhase = makeIrModulePhase(
{ context: WasmBackendContext ->
ExpectDeclarationsRemoveLowering(context.symbolTable)
},
::ExpectDeclarationsRemoveLowering,
name = "ExpectDeclarationsRemoving",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ internal val functionsWithoutBoundCheck = createSimpleNamedCompilerPhase<Context
)

private val removeExpectDeclarationsPhase = createFileLoweringPhase(
::ExpectDeclarationsRemoving,
::ExpectDeclarationsRemoveLowering,
name = "RemoveExpectDeclarations",
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package org.jetbrains.kotlin.backend.konan.lower

import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.descriptors.isExpectMember
import org.jetbrains.kotlin.backend.konan.descriptors.propertyIfAccessor
import org.jetbrains.kotlin.backend.konan.ir.ModuleIndex
Expand All @@ -28,24 +26,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.multiplatform.OptionalAnnotationUtil
import org.jetbrains.kotlin.resolve.multiplatform.findCompatibleActualsForExpected

/**
* This pass removes all declarations with `isExpect == true`.
* Note: org.jetbrains.kotlin.backend.common.lower.ExpectDeclarationsRemoving is copy of this lower.
*/
internal class ExpectDeclarationsRemoving(val context: Context) : FileLoweringPass {
override fun lower(irFile: IrFile) {
// All declarations with `isExpect == true` are nested into a top-level declaration with `isExpect == true`.
irFile.declarations.removeAll {
when (it) {
is IrClass -> it.isExpect
is IrFunction -> it.isExpect
is IrProperty -> it.isExpect
else -> false
}
}
}
}

@OptIn(ObsoleteDescriptorBasedAPI::class)
internal class ExpectToActualDefaultValueCopier(private val irModule: IrModuleFragment, private val irBuiltIns: IrBuiltIns) {

Expand Down

0 comments on commit d039714

Please sign in to comment.