Minor. Simplify and clean IrElementVisitorVoidWithContext/IrElementTransformerVoidWithContext

This commit is contained in:
Mikhael Bogdanov
2019-06-26 07:26:00 +02:00
parent d58c8c68d5
commit bb48e756ca
@@ -20,55 +20,61 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.Scope import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFileSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.descriptors.*
class ScopeWithIr(val scope: Scope, val irElement: IrElement) open class ScopeWithIr(val scope: Scope, val irElement: IrElement)
abstract class IrElementTransformerVoidWithContext : IrElementTransformerVoid() { abstract class IrElementTransformerVoidWithContext : IrElementTransformerVoid() {
private val scopeStack = mutableListOf<ScopeWithIr>() private val scopeStack = mutableListOf<ScopeWithIr>()
override final fun visitFile(declaration: IrFile): IrFile { protected open fun createScope(declaration: IrSymbolOwner): ScopeWithIr =
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) ScopeWithIr(Scope(declaration.symbol), declaration)
final override fun visitFile(declaration: IrFile): IrFile {
scopeStack.push(createScope(declaration))
val result = visitFileNew(declaration) val result = visitFileNew(declaration)
scopeStack.pop() scopeStack.pop()
return result return result
} }
override final fun visitClass(declaration: IrClass): IrStatement { final override fun visitClass(declaration: IrClass): IrStatement {
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) scopeStack.push(createScope(declaration))
val result = visitClassNew(declaration) val result = visitClassNew(declaration)
scopeStack.pop() scopeStack.pop()
return result return result
} }
override final fun visitProperty(declaration: IrProperty): IrStatement { final override fun visitProperty(declaration: IrProperty): IrStatement {
scopeStack.push(ScopeWithIr(Scope(declaration.descriptor), declaration)) scopeStack.push(createScope(declaration))
val result = visitPropertyNew(declaration) val result = visitPropertyNew(declaration)
scopeStack.pop() scopeStack.pop()
return result return result
} }
override final fun visitField(declaration: IrField): IrStatement { final override fun visitField(declaration: IrField): IrStatement {
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) scopeStack.push(createScope(declaration))
val result = visitFieldNew(declaration) val result = visitFieldNew(declaration)
scopeStack.pop() scopeStack.pop()
return result return result
} }
override final fun visitFunction(declaration: IrFunction): IrStatement { final override fun visitFunction(declaration: IrFunction): IrStatement {
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) scopeStack.push(createScope(declaration))
val result = visitFunctionNew(declaration) val result = visitFunctionNew(declaration)
scopeStack.pop() scopeStack.pop()
return result return result
} }
protected val currentFile get() = scopeStack.lastOrNull { it.irElement is IrFile }!!.irElement as IrFile protected val currentFile get() = scopeStack.lastOrNull { it.irElement is IrFile }!!.irElement as IrFile
protected val currentClass get() = scopeStack.lastOrNull { it.scope.scopeOwner is ClassDescriptor } protected val currentClass get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrClassSymbol }
protected val currentFunction get() = scopeStack.lastOrNull { it.scope.scopeOwner is FunctionDescriptor } protected val currentFunction get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrFunctionSymbol }
protected val currentProperty get() = scopeStack.lastOrNull { it.scope.scopeOwner is PropertyDescriptor } protected val currentProperty get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrPropertySymbol }
protected val currentScope get() = scopeStack.peek() protected val currentScope get() = scopeStack.peek()
protected val parentScope get() = if (scopeStack.size < 2) null else scopeStack[scopeStack.size - 2] protected val parentScope get() = if (scopeStack.size < 2) null else scopeStack[scopeStack.size - 2]
protected val allScopes get() = scopeStack protected val allScopes get() = scopeStack
@@ -102,41 +108,44 @@ abstract class IrElementVisitorVoidWithContext : IrElementVisitorVoid {
private val scopeStack = mutableListOf<ScopeWithIr>() private val scopeStack = mutableListOf<ScopeWithIr>()
override final fun visitFile(declaration: IrFile) { protected open fun createScope(declaration: IrSymbolOwner): ScopeWithIr =
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) ScopeWithIr(Scope(declaration.symbol), declaration)
final override fun visitFile(declaration: IrFile) {
scopeStack.push(createScope(declaration))
visitFileNew(declaration) visitFileNew(declaration)
scopeStack.pop() scopeStack.pop()
} }
override final fun visitClass(declaration: IrClass) { final override fun visitClass(declaration: IrClass) {
scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) scopeStack.push(createScope(declaration))
visitClassNew(declaration) visitClassNew(declaration)
scopeStack.pop() scopeStack.pop()
} }
override final fun visitProperty(declaration: IrProperty) { final override fun visitProperty(declaration: IrProperty) {
scopeStack.push(ScopeWithIr(Scope(declaration.descriptor), declaration)) scopeStack.push(createScope(declaration))
visitPropertyNew(declaration) visitPropertyNew(declaration)
scopeStack.pop() scopeStack.pop()
} }
override final fun visitField(declaration: IrField) { final override fun visitField(declaration: IrField) {
@Suppress("DEPRECATION") val isDelegated = declaration.descriptor.isDelegated @Suppress("DEPRECATION") val isDelegated = declaration.descriptor.isDelegated
if (isDelegated) scopeStack.push(ScopeWithIr(Scope(declaration.symbol), declaration)) if (isDelegated) scopeStack.push(createScope(declaration))
visitFieldNew(declaration) visitFieldNew(declaration)
if (isDelegated) scopeStack.pop() if (isDelegated) scopeStack.pop()
} }
override final fun visitFunction(declaration: IrFunction) { final override fun visitFunction(declaration: IrFunction) {
scopeStack.push(ScopeWithIr(Scope(declaration.descriptor), declaration)) scopeStack.push(createScope(declaration))
visitFunctionNew(declaration) visitFunctionNew(declaration)
scopeStack.pop() scopeStack.pop()
} }
protected val currentFile get() = scopeStack.lastOrNull { it.scope.scopeOwner is PackageFragmentDescriptor } protected val currentFile get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrFileSymbol }
protected val currentClass get() = scopeStack.lastOrNull { it.scope.scopeOwner is ClassDescriptor } protected val currentClass get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrClassSymbol }
protected val currentFunction get() = scopeStack.lastOrNull { it.scope.scopeOwner is FunctionDescriptor } protected val currentFunction get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrFunctionSymbol }
protected val currentProperty get() = scopeStack.lastOrNull { it.scope.scopeOwner is PropertyDescriptor } protected val currentProperty get() = scopeStack.lastOrNull { it.scope.scopeOwnerSymbol is IrPropertySymbol }
protected val currentScope get() = scopeStack.peek() protected val currentScope get() = scopeStack.peek()
protected val parentScope get() = if (scopeStack.size < 2) null else scopeStack[scopeStack.size - 2] protected val parentScope get() = if (scopeStack.size < 2) null else scopeStack[scopeStack.size - 2]
protected val allScopes get() = scopeStack protected val allScopes get() = scopeStack