[JS IR BE] Inline classes lowering

This commit is contained in:
Svyatoslav Kuzmich
2018-10-31 17:39:02 +03:00
parent 4866c2b36a
commit 370b04df35
11 changed files with 798 additions and 11 deletions
@@ -0,0 +1,59 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.util
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.isMarkedNullable
/**
* Returns inline class for given class or null of type is not inlined
* TODO: Make this configurable for different backends (currently implements logic of JS BE)
*/
fun IrType.getInlinedClass(): IrClass? {
if (this is IrSimpleType) {
val erased = erase(this) ?: return null
if (!this.isMarkedNullable() && erased.isInline) {
// TODO: Don't box nullable type inline classes with non-nullable underlying type (JS IR BE)
return erased
}
}
return null
}
fun IrType.isInlined(): Boolean = this.getInlinedClass() != null
private tailrec fun erase(type: IrType): IrClass? {
val classifier = type.classifierOrFail
// TODO: Fix unbound symbols
if (!classifier.isBound)
return null
return when (classifier) {
is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
else -> error(classifier)
}
}
fun getInlineClassBackingField(irClass: IrClass): IrField {
for (declaration in irClass.declarations) {
if (declaration is IrField)
return declaration
if (declaration is IrProperty)
return declaration.backingField ?: continue
}
error("Inline class has no field")
}