Change DCE a bit to touch less declarations

This commit is contained in:
Anton Bannykh
2020-01-17 19:30:22 +03:00
parent f07fb91e9d
commit 571bbba0e3
2 changed files with 11 additions and 8 deletions
@@ -78,16 +78,9 @@ private fun removeUselessDeclarations(module: IrModuleFragment, usefulDeclaratio
process(declaration)
}
override fun visitConstructor(declaration: IrConstructor) {
if (declaration !in usefulDeclarations) {
// Keep the constructor declaration without body in order to declare the JS constructor function
declaration.body = IrBlockBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, emptyList())
}
}
private fun process(container: IrDeclarationContainer) {
container.declarations.transformFlat { member ->
if (member !in usefulDeclarations && member !is IrConstructor) {
if (member !in usefulDeclarations) {
emptyList()
} else {
member.acceptVoid(this)
@@ -33,6 +33,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
fun generate(): JsStatement {
assert(!irClass.descriptor.isExpect)
maybeGeneratePrimaryConstructor()
val transformer = IrDeclarationToJsTransformer()
// Properties might be lowered out of classes
@@ -142,6 +143,15 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
return null
}
private fun maybeGeneratePrimaryConstructor() {
if (!irClass.declarations.any { it is IrConstructor }) {
val func = JsFunction(emptyScope, JsBlock(), "Ctor for ${irClass.name}")
func.name = className
classBlock.statements += func.makeStmt()
classModel.preDeclarationBlock.statements += generateInheritanceCode()
}
}
private fun generateInheritanceCode(): List<JsStatement> {
if (baseClass == null || baseClass.isAny()) {
return emptyList()