Made primary constructor wrapper to avoid recreating value parameter instances

This commit is contained in:
Ivan Cilcic
2019-07-26 15:29:27 +03:00
committed by Mikhail Glukhikh
parent 2a60f95a32
commit 1af7063a47
2 changed files with 46 additions and 38 deletions
@@ -33,10 +33,7 @@ import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.isClassLocal
import org.jetbrains.kotlin.fir.lightTree.converter.DataClassUtil.generateComponentFunctions import org.jetbrains.kotlin.fir.lightTree.converter.DataClassUtil.generateComponentFunctions
import org.jetbrains.kotlin.fir.lightTree.converter.DataClassUtil.generateCopyFunction import org.jetbrains.kotlin.fir.lightTree.converter.DataClassUtil.generateCopyFunction
import org.jetbrains.kotlin.fir.lightTree.converter.FunctionUtil.removeLast import org.jetbrains.kotlin.fir.lightTree.converter.FunctionUtil.removeLast
import org.jetbrains.kotlin.fir.lightTree.fir.ClassWrapper import org.jetbrains.kotlin.fir.lightTree.fir.*
import org.jetbrains.kotlin.fir.lightTree.fir.DestructuringDeclaration
import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint
import org.jetbrains.kotlin.fir.lightTree.fir.ValueParameter
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.TypeModifier import org.jetbrains.kotlin.fir.lightTree.fir.modifier.TypeModifier
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.TypeParameterModifier import org.jetbrains.kotlin.fir.lightTree.fir.modifier.TypeParameterModifier
@@ -104,17 +101,15 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseBlockExpression * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseBlockExpression
*/ */
fun convertBlockExpression(block: LighterASTNode): FirBlock { fun convertBlockExpression(block: LighterASTNode): FirBlock {
val firStatements = mutableListOf<FirStatement>() val firStatements = block.forEachChildrenReturnList<FirStatement> { node, container ->
block.forEachChildren { when (node.tokenType) {
when (it.tokenType) { CLASS, OBJECT_DECLARATION -> container += convertClass(node) as FirStatement
CLASS -> firStatements += convertClass(it) as FirStatement FUN -> container += convertFunctionDeclaration(node) as FirStatement
FUN -> firStatements += convertFunctionDeclaration(it) as FirStatement PROPERTY -> container += convertPropertyDeclaration(node) as FirStatement
PROPERTY -> firStatements += convertPropertyDeclaration(it) as FirStatement DESTRUCTURING_DECLARATION -> container += convertDestructingDeclaration(node).toFirDestructingDeclaration(session)
DESTRUCTURING_DECLARATION -> firStatements += convertDestructingDeclaration(it).toFirDestructingDeclaration(session) TYPEALIAS -> container += convertTypeAlias(node) as FirStatement
TYPEALIAS -> firStatements += convertTypeAlias(it) as FirStatement CLASS_INITIALIZER -> container += convertAnonymousInitializer(node) as FirStatement
OBJECT_DECLARATION -> firStatements += convertClass(it) as FirStatement else -> if (node.isExpression()) container += expressionConverter.getAsFirExpression<FirStatement>(node)
CLASS_INITIALIZER -> firStatements += convertAnonymousInitializer(it) as FirStatement
else -> if (it.isExpression()) firStatements += expressionConverter.getAsFirExpression<FirStatement>(it)
} }
} }
return FirBlockImpl(session, null).apply { return FirBlockImpl(session, null).apply {
@@ -407,15 +402,14 @@ class DeclarationsConverter(
toDelegatedSelfType(firClass), delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry toDelegatedSelfType(firClass), delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry
) )
//parse primary constructor //parse primary constructor
val firPrimaryConstructor = convertPrimaryConstructor(primaryConstructor, classWrapper) val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper)
val firPrimaryConstructor = primaryConstructorWrapper?.firConstructor
firPrimaryConstructor?.let { firClass.declarations += it } firPrimaryConstructor?.let { firClass.declarations += it }
val properties = mutableListOf<FirProperty>() val properties = mutableListOf<FirProperty>()
if (primaryConstructor != null && firPrimaryConstructor != null) { if (primaryConstructor != null && firPrimaryConstructor != null) {
//parse properties //parse properties
properties += primaryConstructor properties += primaryConstructorWrapper.valueParameters
.getChildNodesByType(VALUE_PARAMETER_LIST)
.flatMap { convertValueParameters(it) }
.filter { it.hasValOrVar() } .filter { it.hasValOrVar() }
.map { it.toFirProperty() } .map { it.toFirProperty() }
firClass.addDeclarations(properties) firClass.addDeclarations(properties)
@@ -478,8 +472,7 @@ class DeclarationsConverter(
superTypeCallEntry = superTypeCallEntry superTypeCallEntry = superTypeCallEntry
) )
//parse primary constructor //parse primary constructor
val firPrimaryConstructor = convertPrimaryConstructor(primaryConstructor, classWrapper) convertPrimaryConstructor(primaryConstructor, classWrapper)?.let { this.declarations += it.firConstructor }
firPrimaryConstructor?.let { this.declarations += it }
//parse declarations //parse declarations
classBody?.let { classBody?.let {
@@ -535,8 +528,8 @@ class DeclarationsConverter(
superTypeCallEntry = enumSuperTypeCallEntry superTypeCallEntry = enumSuperTypeCallEntry
) )
firEnumEntry.superTypeRefs += enumClassWrapper.delegatedSuperTypeRef firEnumEntry.superTypeRefs += enumClassWrapper.delegatedSuperTypeRef
convertPrimaryConstructor(null, enumClassWrapper)?.let { firEnumEntry.addDeclaration(it) } convertPrimaryConstructor(null, enumClassWrapper)?.let { firEnumEntry.addDeclaration(it.firConstructor) }
classBodyNode?.also { firDeclarations += convertClassBody(it, /*classWrapper*/enumClassWrapper) } classBodyNode?.also { firDeclarations += convertClassBody(it, enumClassWrapper) }
firDeclarations.forEach { firEnumEntry.addDeclaration(it) } firDeclarations.forEach { firEnumEntry.addDeclaration(it) }
return@withChildClassName firEnumEntry return@withChildClassName firEnumEntry
@@ -582,7 +575,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): FirConstructorImpl? { private fun convertPrimaryConstructor(primaryConstructor: LighterASTNode?, classWrapper: ClassWrapper): PrimaryConstructor? {
if (primaryConstructor == null && classWrapper.hasSecondaryConstructor) return null if (primaryConstructor == null && classWrapper.hasSecondaryConstructor) return null
if (classWrapper.isInterface()) return null if (classWrapper.isInterface()) return null
@@ -603,20 +596,22 @@ class DeclarationsConverter(
isThis = false isThis = false
).extractArgumentsFrom(classWrapper.superTypeCallEntry, stubMode) ).extractArgumentsFrom(classWrapper.superTypeCallEntry, stubMode)
return FirPrimaryConstructorImpl( return PrimaryConstructor(
session, FirPrimaryConstructorImpl(
null, session,
FirConstructorSymbol(ClassNameUtil.callableIdForClassConstructor()), null,
if (primaryConstructor != null) modifiers.getVisibility() else defaultVisibility, FirConstructorSymbol(ClassNameUtil.callableIdForClassConstructor()),
modifiers.hasExpect(), if (primaryConstructor != null) modifiers.getVisibility() else defaultVisibility,
modifiers.hasActual(), modifiers.hasExpect(),
classWrapper.delegatedSelfTypeRef, modifiers.hasActual(),
firDelegatedCall classWrapper.delegatedSelfTypeRef,
).apply { firDelegatedCall
annotations += modifiers.annotations ).apply {
this.typeParameters += ConverterUtil.typeParametersFromSelfType(classWrapper.delegatedSelfTypeRef) annotations += modifiers.annotations
this.valueParameters += valueParameters.map { it.firValueParameter } this.typeParameters += ConverterUtil.typeParametersFromSelfType(classWrapper.delegatedSelfTypeRef)
} this.valueParameters += valueParameters.map { it.firValueParameter }
}, valueParameters
)
} }
/** /**
@@ -0,0 +1,13 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.fir.lightTree.fir
import org.jetbrains.kotlin.fir.declarations.impl.FirConstructorImpl
data class PrimaryConstructor(
val firConstructor: FirConstructorImpl,
val valueParameters: List<ValueParameter>
)