Declaration IR refactoring.

This commit is contained in:
Dmitry Petrov
2016-08-08 18:44:46 +03:00
committed by Dmitry Petrov
parent 4f469c798c
commit 5e609c7de4
13 changed files with 168 additions and 98 deletions
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.PropertyGetterDescriptor
import org.jetbrains.kotlin.descriptors.PropertySetterDescriptor
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrBodyBase
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrExpressionBodyImpl
import org.jetbrains.kotlin.ir.expressions.IrReturnExpressionImpl
import org.jetbrains.kotlin.ir.expressions.returnedExpression
@@ -91,7 +91,7 @@ abstract class IrDeclarationGeneratorBase(
return propertyDescriptor
}
fun generateExpressionBody(ktBody: KtExpression): IrBodyBase {
fun generateExpressionBody(ktBody: KtExpression): IrBody {
val sourceLocation = fileElementFactory.getLocationInFile(ktBody)
val irExpression = irExpressionGenerator.generateExpression(ktBody)
@@ -102,6 +102,6 @@ abstract class IrDeclarationGeneratorBase(
IrReturnExpressionImpl(sourceLocation, irExpression.type)
.apply { returnedExpression = irExpression }
return IrExpressionBodyImpl(sourceLocation).apply { childExpression = bodyExpression }
return IrExpressionBodyImpl(sourceLocation, containingDeclaration).apply { childExpression = bodyExpression }
}
}
@@ -41,11 +41,11 @@ class IrFileElementFactory private constructor(
}
fun createFunction(ktFunction: KtFunction, functionDescriptor: FunctionDescriptor, body: IrBody): IrFunction =
IrFunctionImpl(getLocationInFile(ktFunction), IrDeclarationKind.DEFINED, functionDescriptor, body)
IrFunctionImpl(getLocationInFile(ktFunction), IrDeclarationOriginKind.DEFINED, functionDescriptor, body)
.addToContainer()
fun createSimpleProperty(ktProperty: KtProperty, propertyDescriptor: PropertyDescriptor, valueInitializer: IrBody?): IrSimpleProperty =
IrSimplePropertyImpl(getLocationInFile(ktProperty), IrDeclarationKind.DEFINED, propertyDescriptor, valueInitializer)
IrSimplePropertyImpl(getLocationInFile(ktProperty), IrDeclarationOriginKind.DEFINED, propertyDescriptor, valueInitializer)
.addToContainer()
fun createPropertyGetter(
@@ -54,7 +54,7 @@ class IrFileElementFactory private constructor(
getterDescriptor: PropertyGetterDescriptor,
getterBody: IrBody
): IrPropertyGetter =
IrPropertyGetterImpl(getLocationInFile(ktPropertyAccessor), IrDeclarationKind.DEFINED, getterDescriptor, getterBody)
IrPropertyGetterImpl(getLocationInFile(ktPropertyAccessor), IrDeclarationOriginKind.DEFINED, getterDescriptor, getterBody)
.apply { irProperty.getter = this }
.addToContainer()
@@ -64,16 +64,16 @@ class IrFileElementFactory private constructor(
setterDescriptor: PropertySetterDescriptor,
setterBody: IrBody
) : IrPropertySetter =
IrPropertySetterImpl(getLocationInFile(ktPropertyAccessor), IrDeclarationKind.DEFINED, setterDescriptor, setterBody)
IrPropertySetterImpl(getLocationInFile(ktPropertyAccessor), IrDeclarationOriginKind.DEFINED, setterDescriptor, setterBody)
.apply { irProperty.setter = this }
.addToContainer()
companion object {
fun create(irModule: IrModule, sourceManager: PsiSourceManager, ktFile: KtFile, descriptor: PackageFragmentDescriptor): IrFileElementFactory {
fun create(irModule: IrModuleImpl, sourceManager: PsiSourceManager, ktFile: KtFile, descriptor: PackageFragmentDescriptor): IrFileElementFactory {
val fileEntry = sourceManager.getOrCreateFileEntry(ktFile)
val fileSourceLocation = fileEntry.getRootSourceLocation()
val fileName = fileEntry.getRecognizableName()
val irFile = IrFileImpl(fileSourceLocation, irModule, fileName, descriptor)
val irFile = IrFileImpl(fileSourceLocation, fileName, descriptor)
irModule.addFile(irFile)
return IrFileElementFactory(fileEntry, irFile, irFile)
}
@@ -21,7 +21,7 @@ typealias SourceLocation = Long
fun SourceLocation(index: Int, offset: Int) =
(index.toLong() shl 32) + offset.toLong()
const val NO_LOCATION: SourceLocation = -1L
const val NO_LOCATION: SourceLocation = Long.MIN_VALUE
const val UNDEFINED_INDEX: Int = -1
const val UNDEFINED_OFFSET: Int = -1
@@ -22,20 +22,16 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrClass : IrCompoundDeclaration, IrMemberDeclaration {
override val descriptor: ClassDescriptor
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.MODULE
}
class IrClassImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val descriptor: ClassDescriptor
) : IrCompoundDeclarationBase(sourceLocation, kind), IrClass {
override var parent: IrCompoundDeclaration? = null
override fun setTreeLocation(parent: IrCompoundDeclaration?, index: Int) {
this.parent = parent
this.index = index
}
) : IrCompoundMemberDeclarationBase(sourceLocation, originKind), IrClass {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitClass(this, data)
}
@@ -16,11 +16,14 @@
package org.jetbrains.kotlin.ir.declarations
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import java.util.*
interface IrCompoundDeclaration : IrDeclaration {
interface IrDeclarationOwner : IrElement {
val childrenCount: Int
fun getChildDeclaration(index: Int): IrMemberDeclaration?
fun addChildDeclaration(child: IrMemberDeclaration)
fun replaceChildDeclaration(oldChild: IrMemberDeclaration, newChild: IrMemberDeclaration)
@@ -32,28 +35,25 @@ interface IrCompoundDeclaration : IrDeclaration {
fun <D> acceptChildDeclarations(visitor: IrElementVisitor<Unit, D>, data: D)
}
interface IrCompoundDeclaration : IrDeclaration, IrDeclarationOwner
interface IrMemberDeclaration : IrDeclaration {
fun setTreeLocation(parent: IrCompoundDeclaration?, index: Int)
override val parent: IrDeclarationOwner
fun setTreeLocation(parent: IrDeclarationOwner?, index: Int)
}
// TODO synchronization?
abstract class IrCompoundDeclarationBase(
sourceLocation: SourceLocation,
kind: IrDeclarationKind
) : IrDeclarationBase(sourceLocation, kind), IrCompoundDeclaration {
abstract class IrDeclarationOwnerBase(
sourceLocation: SourceLocation
) : IrElementBase(sourceLocation), IrDeclarationOwner {
protected val childDeclarations: MutableList<IrMemberDeclaration> = ArrayList()
override val childrenCount: Int
get() = childDeclarations.size
override fun getChildDeclaration(index: Int): IrMemberDeclaration? =
childDeclarations.getOrNull(index)
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
acceptChildDeclarations(visitor, data)
}
override fun <D> acceptChildDeclarations(visitor: IrElementVisitor<Unit, D>, data: D) {
childDeclarations.forEach { it.accept(visitor, data) }
}
override fun addChildDeclaration(child: IrMemberDeclaration) {
child.setTreeLocation(this, childDeclarations.size)
childDeclarations.add(child)
@@ -61,8 +61,8 @@ abstract class IrCompoundDeclarationBase(
override fun removeChildDeclaration(child: IrMemberDeclaration) {
validateChild(child)
childDeclarations.removeAt(child.index)
for (i in child.index ..childDeclarations.size - 1) {
childDeclarations.removeAt(child.indexInParent)
for (i in child.indexInParent..childDeclarations.size - 1) {
childDeclarations[i].setTreeLocation(this, i)
}
child.detach()
@@ -75,16 +75,68 @@ abstract class IrCompoundDeclarationBase(
override fun replaceChildDeclaration(oldChild: IrMemberDeclaration, newChild: IrMemberDeclaration) {
validateChild(oldChild)
childDeclarations[oldChild.index] = newChild
newChild.setTreeLocation(this, oldChild.index)
childDeclarations[oldChild.indexInParent] = newChild
newChild.setTreeLocation(this, oldChild.indexInParent)
oldChild.detach()
}
override fun <D> acceptChildDeclarations(visitor: IrElementVisitor<Unit, D>, data: D) {
childDeclarations.forEach { it.accept(visitor, data) }
}
}
// TODO synchronization?
abstract class IrCompoundDeclarationBase(
sourceLocation: SourceLocation,
override val originKind: IrDeclarationOriginKind
) : IrDeclarationOwnerBase(sourceLocation), IrCompoundDeclaration {
override var indexInParent: Int = IrDeclaration.DETACHED_INDEX
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
acceptChildDeclarations(visitor, data)
}
}
abstract class IrCompoundMemberDeclarationBase(
sourceLocation: SourceLocation,
originKind: IrDeclarationOriginKind
) : IrCompoundDeclarationBase(sourceLocation, originKind), IrMemberDeclaration {
private var parentImpl: IrDeclarationOwner? = null
override var parent: IrDeclarationOwner
get() = parentImpl!!
set(newParent) {
parentImpl = newParent
}
override fun setTreeLocation(parent: IrDeclarationOwner?, index: Int) {
this.parentImpl = parent
this.indexInParent = index
}
}
abstract class IrMemberDeclarationBase(
sourceLocation: SourceLocation,
originKind: IrDeclarationOriginKind
) : IrDeclarationBase(sourceLocation, originKind), IrMemberDeclaration {
private var parentImpl: IrDeclarationOwner? = null
override var parent: IrDeclarationOwner
get() = parentImpl!!
set(newParent) {
parentImpl = newParent
}
override fun setTreeLocation(parent: IrDeclarationOwner?, index: Int) {
this.parentImpl = parent
this.indexInParent = index
}
}
fun IrMemberDeclaration.detach() {
setTreeLocation(null, IrDeclaration.DETACHED_INDEX)
}
fun IrCompoundDeclaration.validateChild(child: IrMemberDeclaration) {
assert(child.parent == this && getChildDeclaration(child.index) == child) { "Invalid child: $child" }
fun IrDeclarationOwner.validateChild(child: IrMemberDeclaration) {
assert(child.parent == this && getChildDeclaration(child.indexInParent) == child) { "Invalid child: $child" }
}
@@ -22,11 +22,13 @@ import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.SourceLocation
interface IrDeclaration : IrElement {
override val parent: IrCompoundDeclaration?
val index: Int
override val parent: IrDeclarationOwner?
val indexInParent: Int
val descriptor: DeclarationDescriptor?
val kind: IrDeclarationKind
val declarationKind: IrDeclarationKind
val originKind: IrDeclarationOriginKind
companion object {
const val DETACHED_INDEX = Int.MIN_VALUE
@@ -34,17 +36,23 @@ interface IrDeclaration : IrElement {
}
enum class IrDeclarationKind {
MODULE,
FILE,
FUNCTION,
PROPERTY,
PROPERTY_GETTER,
PROPERTY_SETTER,
CLASS
}
enum class IrDeclarationOriginKind {
DEFINED,
DEFAULT_PROPERTY_ACCESSOR,
}
abstract class IrDeclarationBase(
sourceLocation: SourceLocation,
override val kind: IrDeclarationKind
override val originKind: IrDeclarationOriginKind
) : IrElementBase(sourceLocation), IrDeclaration {
override var index: Int = IrDeclaration.DETACHED_INDEX
override var indexInParent: Int = IrDeclaration.DETACHED_INDEX
}
val IrDeclaration.containingDeclaration: IrDeclaration?
get() = parent
@@ -22,16 +22,21 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrFile : IrCompoundDeclaration {
val name: String
val module: IrModule
override val descriptor: PackageFragmentDescriptor
override val parent: Nothing? get() = null
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.FILE
}
class IrFileImpl(
sourceLocation: SourceLocation,
val module: IrModule,
override val name: String,
override val descriptor: PackageFragmentDescriptor
) : IrCompoundDeclarationBase(sourceLocation, IrDeclarationKind.DEFINED), IrFile {
override val parent: IrCompoundDeclaration? = null
) : IrCompoundDeclarationBase(sourceLocation, IrDeclarationOriginKind.DEFINED), IrFile {
override lateinit var module: IrModule
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitFile(this, data)
@@ -24,31 +24,26 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface IrFunction : IrMemberDeclaration {
override val descriptor: FunctionDescriptor
val body: IrBody
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.FUNCTION
}
abstract class IrFunctionBase(
sourceLocation: SourceLocation,
kind: IrDeclarationKind
) : IrCompoundDeclarationBase(sourceLocation, kind), IrFunction {
override var parent: IrCompoundDeclaration? = null
override fun setTreeLocation(parent: IrCompoundDeclaration?, index: Int) {
this.parent = parent
this.index = index
}
originKind: IrDeclarationOriginKind
) : IrMemberDeclarationBase(sourceLocation, originKind), IrFunction {
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
super.acceptChildDeclarations(visitor, data)
body.accept(visitor, data)
}
}
class IrFunctionImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val descriptor: FunctionDescriptor,
override val body: IrBody
) : IrFunctionBase(sourceLocation, kind) {
) : IrFunctionBase(sourceLocation, originKind) {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitFunction(this, data)
}
@@ -25,24 +25,33 @@ import java.util.*
interface IrModule : IrDeclaration {
override val descriptor: ModuleDescriptor
override val parent: Nothing? get() = null
override val indexInParent: Int get() = MODULE_INDEX
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.MODULE
val files: List<IrFile>
fun addFile(file: IrFile)
companion object {
const val MODULE_INDEX = -1
}
}
class IrModuleImpl(
override val descriptor: ModuleDescriptor
) : IrDeclarationBase(NO_LOCATION, IrDeclarationKind.DEFINED), IrModule {
init {
index = 0
}
) : IrModule {
override val sourceLocation: Long
get() = NO_LOCATION
override val parent: IrCompoundDeclaration? get() = null
override val originKind: IrDeclarationOriginKind
get() = IrDeclarationOriginKind.DEFINED
override val files: MutableList<IrFile> = ArrayList()
override fun addFile(file: IrFile) {
fun addFile(file: IrFileImpl) {
files.add(file)
file.module = this
}
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
@@ -26,6 +26,9 @@ interface IrProperty : IrMemberDeclaration {
var getter: IrPropertyGetter?
var setter: IrPropertySetter?
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.PROPERTY
fun <D> acceptAccessors(visitor: IrElementVisitor<Unit, D>, data: D)
}
@@ -40,16 +43,9 @@ interface IrDelegatedProperty : IrProperty {
// TODO synchronization?
abstract class IrPropertyBase(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val descriptor: PropertyDescriptor
) : IrDeclarationBase(sourceLocation, kind), IrProperty {
override var parent: IrCompoundDeclaration? = null
override fun setTreeLocation(parent: IrCompoundDeclaration?, index: Int) {
this.parent = parent
this.index = index
}
) : IrMemberDeclarationBase(sourceLocation, originKind), IrProperty {
override var getter: IrPropertyGetter? = null
set(newGetter) {
newGetter?.property = this
@@ -70,10 +66,10 @@ abstract class IrPropertyBase(
class IrSimplePropertyImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
descriptor: PropertyDescriptor,
override val valueInitializer: IrBody?
) : IrPropertyBase(sourceLocation, kind, descriptor), IrSimpleProperty {
) : IrPropertyBase(sourceLocation, originKind, descriptor), IrSimpleProperty {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitSimpleProperty(this, data)
@@ -84,10 +80,10 @@ class IrSimplePropertyImpl(
class IrDelegatedPropertyImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
descriptor: PropertyDescriptor,
override val delegateInitializer: IrBody
) : IrPropertyBase(sourceLocation, kind, descriptor), IrDelegatedProperty {
) : IrPropertyBase(sourceLocation, originKind, descriptor), IrDelegatedProperty {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitDelegatedProperty(this, data)
@@ -30,36 +30,42 @@ interface IrPropertyAccessor : IrFunction {
interface IrPropertyGetter : IrPropertyAccessor {
override val descriptor: PropertyGetterDescriptor
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.PROPERTY_GETTER
}
interface IrPropertySetter : IrPropertyAccessor {
override val descriptor: PropertySetterDescriptor
override val declarationKind: IrDeclarationKind
get() = IrDeclarationKind.PROPERTY_SETTER
}
abstract class IrPropertyAccessorBase(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val body: IrBody
) : IrFunctionBase(sourceLocation, kind), IrPropertyAccessor {
) : IrFunctionBase(sourceLocation, originKind), IrPropertyAccessor {
override var property: IrProperty? = null
}
class IrPropertyGetterImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val descriptor: PropertyGetterDescriptor,
body: IrBody
) : IrPropertyAccessorBase(sourceLocation, kind, body), IrPropertyGetter {
) : IrPropertyAccessorBase(sourceLocation, originKind, body), IrPropertyGetter {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitPropertyGetter(this, data)
}
class IrPropertySetterImpl(
sourceLocation: SourceLocation,
kind: IrDeclarationKind,
originKind: IrDeclarationOriginKind,
override val descriptor: PropertySetterDescriptor,
body: IrBody
) : IrPropertyAccessorBase(sourceLocation, kind, body), IrPropertySetter {
) : IrPropertyAccessorBase(sourceLocation, originKind, body), IrPropertySetter {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R =
visitor.visitPropertySetter(this, data)
}
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrElementBase
import org.jetbrains.kotlin.ir.SourceLocation
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOwner
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOwnerBase
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
@@ -27,14 +29,13 @@ interface IrBody : IrElement {
override val parent: IrDeclaration
}
interface IrExpressionBody : IrBody, IrExpressionOwner1
abstract class IrBodyBase(sourceLocation: SourceLocation): IrElementBase(sourceLocation), IrBody {
override lateinit var parent: IrDeclaration
}
interface IrExpressionBody : IrBody, IrExpressionOwner1, IrDeclarationOwner
// TODO IrExpressionBodyImpl vs IrCompoundExpression1Impl: extract common base class?
class IrExpressionBodyImpl(sourceLocation: SourceLocation) : IrBodyBase(sourceLocation), IrExpressionBody {
class IrExpressionBodyImpl(
sourceLocation: SourceLocation,
override val parent: IrDeclaration
) : IrDeclarationOwnerBase(sourceLocation), IrExpressionBody {
override var childExpression: IrExpression? = null
set(newExpression) {
field?.detach()
@@ -21,11 +21,13 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.types.KotlinType
interface IrReturnExpression : IrExpression, IrExpressionOwner1
interface IrReturnExpression : IrCompoundExpression1
var IrReturnExpression.returnedExpression: IrExpression?
get() = childExpression
set(newReturnedExpression) { childExpression = newReturnedExpression }
set(newReturnedExpression) {
childExpression = newReturnedExpression
}
class IrReturnExpressionImpl(
sourceLocation: SourceLocation,