From d67d8016d305edbf03c571a1cdad70f81aaf4df4 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 16 Sep 2016 17:28:44 +0300 Subject: [PATCH] Lowering for property declarations. --- .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 4 +- .../backend/jvm/lower/FileClassLowering.kt | 5 +- .../backend/jvm/lower/PropertiesLowering.kt | 61 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertiesLowering.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index 2e5734cae2a..4424aac1b8e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -17,10 +17,12 @@ package org.jetbrains.kotlin.backend.jvm import org.jetbrains.kotlin.backend.jvm.lower.FileClassLowering +import org.jetbrains.kotlin.backend.jvm.lower.PropertiesLowering import org.jetbrains.kotlin.ir.declarations.IrFile class JvmLower(val context: JvmBackendContext) { fun lower(irFile: IrFile) { - FileClassLowering(context).lower(irFile) + FileClassLowering(context.jvmFileClassProvider).lower(irFile) + PropertiesLowering().lower(irFile) } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassLowering.kt index 479d0e82cc9..c87827c720e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FileClassLowering.kt @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.backend.jvm.lower -import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin @@ -24,7 +23,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl import java.util.* -class FileClassLowering(val context: JvmBackendContext) { +class FileClassLowering(val jvmFileClassProvider: JvmFileClassProvider) { fun lower(irFile: IrFile) { val classes = ArrayList() val fileClassMembers = ArrayList() @@ -38,7 +37,7 @@ class FileClassLowering(val context: JvmBackendContext) { if (fileClassMembers.isEmpty()) return - val fileClassDescriptor = context.jvmFileClassProvider.createFileClassDescriptor(irFile.fileEntry, irFile.packageFragmentDescriptor) + val fileClassDescriptor = jvmFileClassProvider.createFileClassDescriptor(irFile.fileEntry, irFile.packageFragmentDescriptor) val irFileClass = IrClassImpl(0, irFile.fileEntry.maxOffset, IrDeclarationOrigin.DEFINED, fileClassDescriptor, fileClassMembers) classes.add(irFileClass) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertiesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertiesLowering.kt new file mode 100644 index 00000000000..4e451c299d3 --- /dev/null +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/PropertiesLowering.kt @@ -0,0 +1,61 @@ +/* + * 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. + */ + +package org.jetbrains.kotlin.backend.jvm.lower + +import org.jetbrains.kotlin.ir.IrStatement +import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrDeclaration +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrProperty +import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid +import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid +import org.jetbrains.kotlin.utils.addIfNotNull +import java.util.* + +class PropertiesLowering : IrElementTransformerVoid { + fun lower(irFile: IrFile) { + irFile.transformChildrenVoid(this) + } + + override fun visitFile(declaration: IrFile): IrFile { + declaration.transformChildrenVoid(this) + transformDeclarations(declaration.declarations) + return declaration + } + + override fun visitClass(declaration: IrClass): IrStatement { + declaration.transformChildrenVoid(this) + transformDeclarations(declaration.declarations) + return declaration + } + + private fun transformDeclarations(declarations: MutableList) { + val properties = linkedSetOf() + + declarations.filterIsInstanceTo(properties) + + declarations.removeAll(properties) + + properties.flatMapTo(declarations) { irProperty -> + val result = ArrayList(3) + result.addIfNotNull(irProperty.backingField) + result.addIfNotNull(irProperty.getter) + result.addIfNotNull(irProperty.setter) + result + } + } +}