FIR: constructor refactoring, now there are callable members

This commit is contained in:
Mikhail Glukhikh
2018-04-13 17:27:58 +03:00
parent a07b9a70f7
commit 65d89a61bf
4 changed files with 44 additions and 18 deletions
@@ -193,11 +193,11 @@ class RawFirBuilder(val session: FirSession) {
}
private fun KtDeclarationWithBody.extractValueParametersTo(
container: FirAbstractFunction,
container: FirFunction,
defaultType: FirType? = null
) {
for (valueParameter in valueParameters) {
container.valueParameters += valueParameter.toFirValueParameter(defaultType)
(container.valueParameters as MutableList<FirValueParameter>) += valueParameter.toFirValueParameter(defaultType)
}
}
@@ -207,7 +207,9 @@ class RawFirBuilder(val session: FirSession) {
}
}
private fun KtClassOrObject.extractSuperTypeListEntriesTo(container: FirClassImpl): FirType? {
private fun KtClassOrObject.extractSuperTypeListEntriesTo(
container: FirClassImpl, delegatedSelfType: FirType
): FirType? {
var superTypeCallEntry: KtSuperTypeCallEntry? = null
var delegatedSuperType: FirType? = null
for (superTypeListEntry in superTypeListEntries) {
@@ -240,6 +242,7 @@ class RawFirBuilder(val session: FirSession) {
val firPrimaryConstructor = primaryConstructor.toFirConstructor(
superTypeCallEntry,
delegatedSuperType,
delegatedSelfType,
owner = this
)
container.declarations += firPrimaryConstructor
@@ -249,6 +252,7 @@ class RawFirBuilder(val session: FirSession) {
private fun KtPrimaryConstructor?.toFirConstructor(
superTypeCallEntry: KtSuperTypeCallEntry?,
delegatedSuperType: FirType,
delegatedSelfType: FirType,
owner: KtClassOrObject
): FirConstructor {
val constructorCallee = superTypeCallEntry?.calleeExpression
@@ -265,6 +269,8 @@ class RawFirBuilder(val session: FirSession) {
session,
this ?: owner,
this?.visibility ?: Visibilities.UNKNOWN,
this?.platformStatus ?: FirMemberPlatformStatus.DEFAULT,
delegatedSelfType,
firDelegatedCall
)
this?.extractAnnotationsTo(firConstructor)
@@ -309,8 +315,8 @@ class RawFirBuilder(val session: FirSession) {
enumEntry.nameAsSafeName
)
enumEntry.extractAnnotationsTo(firEnumEntry)
val delegatedSuperType = enumEntry.extractSuperTypeListEntriesTo(firEnumEntry)
val delegatedSelfType = enumEntry.toDelegatedSelfType()
val delegatedSuperType = enumEntry.extractSuperTypeListEntriesTo(firEnumEntry, delegatedSelfType)
for (declaration in enumEntry.declarations) {
firEnumEntry.declarations += when (declaration) {
is KtSecondaryConstructor -> declaration.toFirConstructor(
@@ -365,14 +371,14 @@ class RawFirBuilder(val session: FirSession) {
)
classOrObject.extractAnnotationsTo(firClass)
classOrObject.extractTypeParametersTo(firClass)
val delegatedSuperType = classOrObject.extractSuperTypeListEntriesTo(firClass)
val delegatedSelfType = classOrObject.toDelegatedSelfType()
val delegatedSuperType = classOrObject.extractSuperTypeListEntriesTo(firClass, delegatedSelfType)
classOrObject.primaryConstructor?.valueParameters?.forEach {
if (it.hasValOrVar()) {
firClass.declarations += it.toFirProperty()
}
}
val delegatedSelfType = classOrObject.toDelegatedSelfType()
for (declaration in classOrObject.declarations) {
firClass.declarations += when (declaration) {
is KtSecondaryConstructor -> declaration.toFirConstructor(
@@ -450,6 +456,8 @@ class RawFirBuilder(val session: FirSession) {
session,
this,
visibility,
platformStatus,
delegatedSelfType,
getDelegationCall().convert(delegatedSuperType, delegatedSelfType, hasPrimaryConstructor),
buildFirBody()
)
@@ -5,23 +5,20 @@
package org.jetbrains.kotlin.fir.declarations
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.BaseTransformedType
import org.jetbrains.kotlin.fir.expressions.FirAnnotationContainer
import org.jetbrains.kotlin.fir.VisitedSupertype
import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall
import org.jetbrains.kotlin.fir.visitors.FirVisitor
@BaseTransformedType
interface FirConstructor : FirFunction, FirAnnotationContainer {
interface FirConstructor : @VisitedSupertype FirFunction, FirCallableMember {
val delegatedConstructor: FirDelegatedConstructorCall?
val visibility: Visibility
override fun <R, D> accept(visitor: FirVisitor<R, D>, data: D): R =
visitor.visitConstructor(this, data)
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
acceptAnnotations(visitor, data)
super<FirCallableMember>.acceptChildren(visitor, data)
delegatedConstructor?.accept(visitor, data)
for (parameter in valueParameters) {
parameter.accept(visitor, data)
@@ -6,26 +6,43 @@
package org.jetbrains.kotlin.fir.declarations.impl
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirConstructor
import org.jetbrains.kotlin.fir.declarations.FirMemberPlatformStatus
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.expressions.FirBody
import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall
import org.jetbrains.kotlin.fir.transformInplace
import org.jetbrains.kotlin.fir.transformSingle
import org.jetbrains.kotlin.fir.types.FirType
import org.jetbrains.kotlin.fir.visitors.FirTransformer
import org.jetbrains.kotlin.name.Name
open class FirConstructorImpl(
session: FirSession,
psi: PsiElement?,
final override val visibility: Visibility,
visibility: Visibility,
platformStatus: FirMemberPlatformStatus,
delegatedSelfType: FirType,
final override var delegatedConstructor: FirDelegatedConstructorCall?,
body: FirBody?
) : FirAbstractFunction(session, psi, body), FirConstructor {
override val body: FirBody?
) : FirAbstractCallableMember(
session, psi, NAME, visibility, Modality.FINAL,
platformStatus, false, null, delegatedSelfType
), FirConstructor {
override val valueParameters = mutableListOf<FirValueParameter>()
override fun <D> transformChildren(transformer: FirTransformer<D>, data: D): FirElement {
delegatedConstructor = delegatedConstructor?.transformSingle(transformer, data)
valueParameters.transformInplace(transformer, data)
delegatedConstructor?.transformSingle(transformer, data)
return super<FirAbstractFunction>.transformChildren(transformer, data)
return super<FirAbstractCallableMember>.transformChildren(transformer, data)
}
companion object {
val NAME = Name.special("<init>")
}
}
@@ -8,11 +8,15 @@ package org.jetbrains.kotlin.fir.declarations.impl
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirMemberPlatformStatus
import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall
import org.jetbrains.kotlin.fir.types.FirType
class FirPrimaryConstructorImpl(
session: FirSession,
psi: PsiElement?,
visibility: Visibility,
platformStatus: FirMemberPlatformStatus,
delegatedSelfType: FirType,
delegatedConstructor: FirDelegatedConstructorCall?
) : FirConstructorImpl(session, psi, visibility, delegatedConstructor, body = null)
) : FirConstructorImpl(session, psi, visibility, platformStatus, delegatedSelfType, delegatedConstructor, body = null)