[FIR] Add light source elements to delegated constructor calls

This commit is contained in:
Dmitriy Novozhilov
2020-03-24 13:04:52 +03:00
parent badd156def
commit 318d029fb2
2 changed files with 29 additions and 16 deletions
@@ -30,7 +30,7 @@ open class BaseConverter(
) : BaseFirBuilder<LighterASTNode>(baseSession, context) { ) : BaseFirBuilder<LighterASTNode>(baseSession, context) {
protected val implicitType = buildImplicitTypeRef() protected val implicitType = buildImplicitTypeRef()
override fun LighterASTNode.toFirSourceElement(): FirSourceElement { override fun LighterASTNode.toFirSourceElement(): FirLightSourceElement {
val startOffset = offset + tree.getStartOffset(this) val startOffset = offset + tree.getStartOffset(this)
val endOffset = offset + tree.getEndOffset(this) val endOffset = offset + tree.getEndOffset(this)
return FirLightSourceElement(this, startOffset, endOffset, tree) return FirLightSourceElement(this, startOffset, endOffset, tree)
@@ -341,6 +341,7 @@ class DeclarationsConverter(
var classBody: LighterASTNode? = null var classBody: LighterASTNode? = null
var typeParameterList: LighterASTNode? = null var typeParameterList: LighterASTNode? = null
var delegatedConstructorSource: FirLightSourceElement? = null
classNode.forEachChildren { classNode.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
MODIFIER_LIST -> modifiers = convertModifierList(it) MODIFIER_LIST -> modifiers = convertModifierList(it)
@@ -350,10 +351,11 @@ class DeclarationsConverter(
IDENTIFIER -> identifier = it.asText IDENTIFIER -> identifier = it.asText
TYPE_PARAMETER_LIST -> typeParameterList = it TYPE_PARAMETER_LIST -> typeParameterList = it
PRIMARY_CONSTRUCTOR -> primaryConstructor = it PRIMARY_CONSTRUCTOR -> primaryConstructor = it
SUPER_TYPE_LIST -> convertDelegationSpecifiers(it).apply { SUPER_TYPE_LIST -> convertDelegationSpecifiers(it).let {
delegatedSuperTypeRef = first delegatedSuperTypeRef = it.delegatedSuperTypeRef
superTypeRefs += second superTypeRefs += it.superTypesRef
superTypeCallEntry += third superTypeCallEntry += it.delegatedConstructorArguments
delegatedConstructorSource = it.delegatedConstructorSource
} }
TYPE_CONSTRAINT_LIST -> typeConstraints += convertTypeConstraints(it) TYPE_CONSTRAINT_LIST -> typeConstraints += convertTypeConstraints(it)
CLASS_BODY -> classBody = it CLASS_BODY -> classBody = it
@@ -435,7 +437,7 @@ class DeclarationsConverter(
delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry
) )
//parse primary constructor //parse primary constructor
val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper) val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper, delegatedConstructorSource)
val firPrimaryConstructor = primaryConstructorWrapper?.firConstructor val firPrimaryConstructor = primaryConstructorWrapper?.firConstructor
firPrimaryConstructor?.let { declarations += it } firPrimaryConstructor?.let { declarations += it }
@@ -484,14 +486,16 @@ class DeclarationsConverter(
val superTypeCallEntry = mutableListOf<FirExpression>() val superTypeCallEntry = mutableListOf<FirExpression>()
var delegatedSuperTypeRef: FirTypeRef? = null var delegatedSuperTypeRef: FirTypeRef? = null
var classBody: LighterASTNode? = null var classBody: LighterASTNode? = null
var delegatedConstructorSource: FirLightSourceElement? = null
objectLiteral.getChildNodesByType(OBJECT_DECLARATION).first().forEachChildren { objectLiteral.getChildNodesByType(OBJECT_DECLARATION).first().forEachChildren {
when (it.tokenType) { when (it.tokenType) {
MODIFIER_LIST -> modifiers = convertModifierList(it) MODIFIER_LIST -> modifiers = convertModifierList(it)
PRIMARY_CONSTRUCTOR -> primaryConstructor = it PRIMARY_CONSTRUCTOR -> primaryConstructor = it
SUPER_TYPE_LIST -> convertDelegationSpecifiers(it).apply { SUPER_TYPE_LIST -> convertDelegationSpecifiers(it).let {
delegatedSuperTypeRef = first delegatedSuperTypeRef = it.delegatedSuperTypeRef
superTypeRefs += second superTypeRefs += it.superTypesRef
superTypeCallEntry += third superTypeCallEntry += it.delegatedConstructorArguments
delegatedConstructorSource = it.delegatedConstructorSource
} }
CLASS_BODY -> classBody = it CLASS_BODY -> classBody = it
} }
@@ -521,7 +525,7 @@ class DeclarationsConverter(
superTypeCallEntry = superTypeCallEntry superTypeCallEntry = superTypeCallEntry
) )
//parse primary constructor //parse primary constructor
convertPrimaryConstructor(primaryConstructor, classWrapper)?.let { this.declarations += it.firConstructor } convertPrimaryConstructor(primaryConstructor, classWrapper, delegatedConstructorSource)?.let { this.declarations += it.firConstructor }
//parse declarations //parse declarations
classBody?.let { classBody?.let {
@@ -586,7 +590,7 @@ class DeclarationsConverter(
superTypeCallEntry = enumSuperTypeCallEntry superTypeCallEntry = enumSuperTypeCallEntry
) )
superTypeRefs += enumClassWrapper.delegatedSuperTypeRef superTypeRefs += enumClassWrapper.delegatedSuperTypeRef
convertPrimaryConstructor(null, enumClassWrapper)?.let { declarations += it.firConstructor } convertPrimaryConstructor(null, enumClassWrapper, null)?.let { declarations += it.firConstructor }
classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) } classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) }
} }
} }
@@ -632,7 +636,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseClassOrObject * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseClassOrObject
* primaryConstructor branch * primaryConstructor branch
*/ */
private fun convertPrimaryConstructor(primaryConstructor: LighterASTNode?, classWrapper: ClassWrapper): PrimaryConstructor? { private fun convertPrimaryConstructor(primaryConstructor: LighterASTNode?, classWrapper: ClassWrapper, delegatedConstructorSource: FirLightSourceElement?): PrimaryConstructor? {
if (primaryConstructor == null && !classWrapper.isEnumEntry() && classWrapper.hasSecondaryConstructor) return null if (primaryConstructor == null && !classWrapper.isEnumEntry() && classWrapper.hasSecondaryConstructor) return null
if (classWrapper.isInterface()) return null if (classWrapper.isInterface()) return null
@@ -647,7 +651,7 @@ class DeclarationsConverter(
val defaultVisibility = classWrapper.defaultConstructorVisibility() val defaultVisibility = classWrapper.defaultConstructorVisibility()
val firDelegatedCall = buildDelegatedConstructorCall { val firDelegatedCall = buildDelegatedConstructorCall {
source = primaryConstructor?.toFirSourceElement() source = delegatedConstructorSource
constructedTypeRef = classWrapper.delegatedSuperTypeRef constructedTypeRef = classWrapper.delegatedSuperTypeRef
isThis = false isThis = false
extractArgumentsFrom(classWrapper.superTypeCallEntry, stubMode) extractArgumentsFrom(classWrapper.superTypeCallEntry, stubMode)
@@ -1213,10 +1217,18 @@ class DeclarationsConverter(
* DELEGATED_SUPER_TYPE_ENTRY - explicitDelegation * DELEGATED_SUPER_TYPE_ENTRY - explicitDelegation
*/ */
//TODO make wrapper for result? //TODO make wrapper for result?
private fun convertDelegationSpecifiers(delegationSpecifiers: LighterASTNode): Triple<FirTypeRef?, List<FirTypeRef>, List<FirExpression>> { private data class DelegationSpecifiers(
val delegatedSuperTypeRef: FirTypeRef?,
val superTypesRef: List<FirTypeRef>,
val delegatedConstructorArguments: List<FirExpression>,
val delegatedConstructorSource: FirLightSourceElement?
)
private fun convertDelegationSpecifiers(delegationSpecifiers: LighterASTNode): DelegationSpecifiers {
val superTypeRefs = mutableListOf<FirTypeRef>() val superTypeRefs = mutableListOf<FirTypeRef>()
val superTypeCallEntry = mutableListOf<FirExpression>() val superTypeCallEntry = mutableListOf<FirExpression>()
var delegatedSuperTypeRef: FirTypeRef? = null var delegatedSuperTypeRef: FirTypeRef? = null
var delegateConstructorSource: FirLightSourceElement? = null
delegationSpecifiers.forEachChildren { delegationSpecifiers.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
SUPER_TYPE_ENTRY -> superTypeRefs += convertType(it) SUPER_TYPE_ENTRY -> superTypeRefs += convertType(it)
@@ -1224,11 +1236,12 @@ class DeclarationsConverter(
delegatedSuperTypeRef = first delegatedSuperTypeRef = first
superTypeRefs += first superTypeRefs += first
superTypeCallEntry += second superTypeCallEntry += second
delegateConstructorSource = it.toFirSourceElement()
} }
DELEGATED_SUPER_TYPE_ENTRY -> superTypeRefs += convertExplicitDelegation(it) DELEGATED_SUPER_TYPE_ENTRY -> superTypeRefs += convertExplicitDelegation(it)
} }
} }
return Triple(delegatedSuperTypeRef, superTypeRefs, superTypeCallEntry) return DelegationSpecifiers(delegatedSuperTypeRef, superTypeRefs, superTypeCallEntry, delegateConstructorSource)
} }
/** /**