Simple stage layers + enforce model

This commit is contained in:
Anton Bannykh
2020-01-17 19:07:37 +03:00
parent f437da8ee5
commit f07fb91e9d
20 changed files with 293 additions and 133 deletions
@@ -112,7 +112,7 @@ fun DeclarationContainerLoweringPass.runOnFilePostfix(irFile: IrFile) {
this.lower(irFile as IrDeclarationContainer)
}
fun BodyLoweringPass.runOnFilePostfix(irFile: IrFile, withLocalDeclarations: Boolean = false) {
fun BodyLoweringPass.runOnFilePostfix(irFile: IrFile, withLocalDeclarations: Boolean = false, allowDeclarationModification: Boolean = false) {
ArrayList(irFile.declarations).forEach {
it.accept(object : IrElementVisitor<Unit, IrDeclaration?> {
override fun visitElement(element: IrElement, data: IrDeclaration?) {
@@ -131,8 +131,13 @@ fun BodyLoweringPass.runOnFilePostfix(irFile: IrFile, withLocalDeclarations: Boo
override fun visitBody(body: IrBody, data: IrDeclaration?) {
if (withLocalDeclarations) body.acceptChildren(this, null)
lower(body, data!!)
if (allowDeclarationModification) {
lower(body, data!!)
} else {
stageController.bodyLowering {
lower(body, data!!)
}
}
}
}, null)
}
@@ -160,7 +165,9 @@ interface DeclarationTransformer: FileLoweringPass {
}
fun DeclarationTransformer.transformFlatRestricted(declaration: IrDeclaration): List<IrDeclaration>? {
return transformFlat(declaration)
return stageController.restrictTo(declaration) {
transformFlat(declaration)
}
}
fun DeclarationTransformer.toFileLoweringPass(): FileLoweringPass {
@@ -366,7 +366,9 @@ fun IrType.remapTypeParameters(
/* Copied from K/N */
fun IrDeclarationContainer.addChild(declaration: IrDeclaration) {
this.declarations += declaration
stageController.unrestrictDeclarationListsAccess {
this.declarations += declaration
}
declaration.accept(SetDeclarationsParentVisitor, this)
}
@@ -443,6 +445,7 @@ fun IrClass.simpleFunctions() = declarations.flatMap {
}
}
fun IrClass.createParameterDeclarations() {
assert(thisReceiver == null)
@@ -64,7 +64,8 @@ open class InitializersLowering(context: CommonBackendContext) : InitializersLow
abstract class InitializersLoweringBase(open val context: CommonBackendContext) {
protected fun extractInitializers(irClass: IrClass, filter: (IrDeclaration) -> Boolean) =
irClass.declarations.filter(filter).mapNotNull {
// TODO What about fields that were added by lowerings? e.g. captured outer class or locals?
ArrayList(irClass.declarations).mapNotNull { if (it is IrProperty) it.backingField else it }.filter(filter).mapNotNull {
when (it) {
is IrField -> handleField(irClass, it)
is IrAnonymousInitializer -> handleAnonymousInitializer(it)
@@ -34,7 +34,9 @@ class InnerClassesLowering(val context: BackendContext) : DeclarationTransformer
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
if (declaration is IrClass && declaration.isInner) {
declaration.declarations += context.declarationFactory.getOuterThisField(declaration)
stageController.unrestrictDeclarationListsAccess {
declaration.declarations += context.declarationFactory.getOuterThisField(declaration)
}
} else if (declaration is IrConstructor) {
val irClass = declaration.parentAsClass
if (!irClass.isInner) return null
@@ -15,7 +15,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrCompositeImpl
//This lower takes part of old LocalDeclarationLowering job to pop up local classes from functions
open class LocalClassPopupLowering(val context: BackendContext) : BodyLoweringPass {
override fun lower(irFile: IrFile) {
runOnFilePostfix(irFile, withLocalDeclarations = true)
runOnFilePostfix(irFile, withLocalDeclarations = true, allowDeclarationModification = true)
}
override fun lower(irBody: IrBody, container: IrDeclaration) {
@@ -95,6 +95,10 @@ class LocalDeclarationsLowering(
) :
BodyLoweringPass {
override fun lower(irFile: IrFile) {
runOnFilePostfix(irFile, allowDeclarationModification = true)
}
object DECLARATION_ORIGIN_FIELD_FOR_CAPTURED_VALUE :
IrDeclarationOriginImpl("FIELD_FOR_CAPTURED_VALUE", isSynthetic = true)