Add 'parent' to IrDeclaration, initialize it with a hack (for migration)
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi2ir
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
|
||||
import org.jetbrains.kotlin.psi2ir.generators.ModuleGenerator
|
||||
@@ -55,5 +56,7 @@ class Psi2IrTranslator(val configuration: Psi2IrConfiguration = Psi2IrConfigurat
|
||||
insertImplicitCasts(context.builtIns, irElement)
|
||||
|
||||
postprocessingSteps.forEach { it.postprocess(context, irElement) }
|
||||
|
||||
irElement.patchDeclarationParents()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ interface IrDeclaration : IrStatement {
|
||||
val declarationKind: IrDeclarationKind
|
||||
val origin: IrDeclarationOrigin
|
||||
|
||||
val parent: IrDeclarationParent
|
||||
|
||||
override fun <D> transform(transformer: IrElementTransformer<D>, data: D): IrStatement =
|
||||
accept(transformer, data) as IrStatement
|
||||
}
|
||||
|
||||
+3
-1
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.declarations
|
||||
|
||||
interface IrDeclarationContainer {
|
||||
interface IrDeclarationParent
|
||||
|
||||
interface IrDeclarationContainer : IrDeclarationParent {
|
||||
val declarations: MutableList<IrDeclaration>
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
interface IrFunction : IrDeclaration, IrTypeParametersContainer, IrSymbolOwner {
|
||||
interface IrFunction : IrDeclaration, IrTypeParametersContainer, IrSymbolOwner, IrDeclarationParent {
|
||||
override val descriptor: FunctionDescriptor
|
||||
override val symbol: IrFunctionSymbol
|
||||
|
||||
|
||||
+6
-1
@@ -19,9 +19,14 @@ package org.jetbrains.kotlin.ir.declarations.impl
|
||||
import org.jetbrains.kotlin.ir.IrElementBase
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
|
||||
|
||||
abstract class IrDeclarationBase(
|
||||
startOffset: Int,
|
||||
endOffset: Int,
|
||||
override val origin: IrDeclarationOrigin
|
||||
) : IrElementBase(startOffset, endOffset), IrDeclaration
|
||||
) : IrElementBase(startOffset, endOffset),
|
||||
IrDeclaration {
|
||||
|
||||
override lateinit var parent: IrDeclarationParent
|
||||
}
|
||||
@@ -31,8 +31,8 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import java.util.*
|
||||
|
||||
inline fun <reified T : IrElement> T.deepCopyOld() =
|
||||
transform(DeepCopyIrTree(), null) as T
|
||||
inline fun <reified T : IrElement> T.deepCopyOld(): T =
|
||||
transform(DeepCopyIrTree(), null).patchDeclarationParents() as T
|
||||
|
||||
@Deprecated("Creates unbound symbols")
|
||||
open class DeepCopyIrTree : IrElementTransformerVoid() {
|
||||
|
||||
@@ -32,7 +32,7 @@ import java.util.*
|
||||
inline fun <reified T : IrElement> T.deepCopyWithSymbols(): T {
|
||||
val remapper = DeepCopySymbolsRemapper()
|
||||
acceptVoid(remapper)
|
||||
return transform(DeepCopyIrTreeWithSymbols(remapper), null) as T
|
||||
return transform(DeepCopyIrTreeWithSymbols(remapper), null).patchDeclarationParents() as T
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2000-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.IrElement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
|
||||
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrDeclarationBase
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import java.util.*
|
||||
|
||||
fun <T : IrElement> T.patchDeclarationParents() =
|
||||
apply {
|
||||
acceptVoid(PatchDeclarationParentsVisitor())
|
||||
}
|
||||
|
||||
class PatchDeclarationParentsVisitor : IrElementVisitorVoid {
|
||||
|
||||
private val declarationParentsStack = ArrayDeque<IrDeclarationParent>()
|
||||
|
||||
override fun visitElement(element: IrElement) {
|
||||
element.acceptChildrenVoid(this)
|
||||
}
|
||||
|
||||
override fun visitPackageFragment(declaration: IrPackageFragment) {
|
||||
declarationParentsStack.push(declaration)
|
||||
super.visitPackageFragment(declaration)
|
||||
declarationParentsStack.pop()
|
||||
}
|
||||
|
||||
override fun visitDeclaration(declaration: IrDeclaration) {
|
||||
patchParent(declaration)
|
||||
|
||||
if (declaration is IrDeclarationParent) {
|
||||
declarationParentsStack.push(declaration)
|
||||
}
|
||||
|
||||
super.visitDeclaration(declaration)
|
||||
|
||||
if (declaration is IrDeclarationParent) {
|
||||
declarationParentsStack.pop()
|
||||
}
|
||||
}
|
||||
|
||||
private fun patchParent(declaration: IrDeclaration) {
|
||||
(declaration as IrDeclarationBase).parent = declarationParentsStack.peekFirst()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user