IR: use inlineClassRepresentation in getInlineClassUnderlyingType

Looking for the primary constructor manually doesn't work if it's
private in the other module on JVM, because private declarations are
skipped in IrLazyClass.
This commit is contained in:
Alexander Udalov
2021-08-12 13:13:26 +02:00
parent 66dbd91851
commit 8d4f26cf84
13 changed files with 88 additions and 14 deletions
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.wasm.ir.*
@@ -97,7 +96,7 @@ class WasmTypeTransformer(
if (klass != null && klass.hasWasmForeignAnnotation()) {
WasmExternRef
} else if (ic != null) {
getInlineClassUnderlyingType(ic).toWasmValueType()
context.backendContext.inlineClassesUtils.getInlineClassUnderlyingType(ic).toWasmValueType()
} else {
this.toWasmGcRefType()
}
@@ -9,10 +9,12 @@ import org.jetbrains.kotlin.backend.wasm.WasmSymbols
import org.jetbrains.kotlin.ir.backend.js.InlineClassesUtils
import org.jetbrains.kotlin.ir.backend.js.utils.erase
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
class WasmInlineClassesUtils(private val wasmSymbols: WasmSymbols) : InlineClassesUtils {
override fun isTypeInlined(type: IrType): Boolean {
@@ -42,4 +44,17 @@ class WasmInlineClassesUtils(private val wasmSymbols: WasmSymbols) : InlineClass
override val unboxIntrinsic: IrSimpleFunctionSymbol
get() = wasmSymbols.unboxIntrinsic
}
/**
* Unlike [org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType], doesn't use [IrClass.inlineClassRepresentation] because
* for some reason it can be called for classes which are not inline, e.g. `kotlin.Double`.
*/
fun getInlineClassUnderlyingType(irClass: IrClass): IrType {
for (declaration in irClass.declarations) {
if (declaration is IrConstructor && declaration.isPrimary) {
return declaration.valueParameters[0].type
}
}
error("Class has no primary constructor: ${irClass.fqNameWhenAvailable}")
}
}