diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/LightTree2Fir.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/LightTree2Fir.kt index 5e3c9032f6e..b5170c589d4 100644 --- a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/LightTree2Fir.kt +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/LightTree2Fir.kt @@ -14,6 +14,7 @@ import com.intellij.openapi.vfs.CharsetToolkit import com.intellij.util.diff.FlyweightCapableTreeStructure import org.jetbrains.kotlin.fir.FirSessionBase import org.jetbrains.kotlin.fir.declarations.FirFile +import org.jetbrains.kotlin.fir.lightTree.converter.DeclarationsConverter import org.jetbrains.kotlin.parsing.MyKotlinParser import java.io.File import java.nio.file.Path @@ -41,7 +42,7 @@ class LightTree2Fir( fun buildFirFile(code: String, fileName: String): FirFile { val lightTree = buildLightTree(code) - return Converter(object : FirSessionBase(null) {}, stubMode, lightTree) + return DeclarationsConverter(object : FirSessionBase(null) {}, stubMode, lightTree) .convertFile(lightTree.root, fileName) } } \ No newline at end of file diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt new file mode 100644 index 00000000000..5d307039d2b --- /dev/null +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/BaseConverter.kt @@ -0,0 +1,65 @@ +/* + * 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.converter + +import com.intellij.lang.LighterASTNode +import com.intellij.openapi.util.Ref +import com.intellij.psi.tree.IElementType +import com.intellij.util.diff.FlyweightCapableTreeStructure +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.types.impl.* +import org.jetbrains.kotlin.lexer.KtTokens + +open class BaseConverter( + session: FirSession, + private val tree: FlyweightCapableTreeStructure +) { + protected val implicitUnitType = FirImplicitUnitTypeRef(session, null) + protected val implicitAnyType = FirImplicitAnyTypeRef(session, null) + protected val implicitEnumType = FirImplicitEnumTypeRef(session, null) + protected val implicitAnnotationType = FirImplicitAnnotationTypeRef(session, null) + protected val implicitType = FirImplicitTypeRefImpl(session, null) + + protected fun LighterASTNode?.getChildNodesByType(type: IElementType): List { + return this?.forEachChildrenReturnList { node, container -> + when (node.tokenType) { + type -> container += node + } + } ?: listOf() + } + + protected fun LighterASTNode?.getChildrenAsArray(): Array { + if (this == null) return arrayOf() + + val kidsRef = Ref>() + tree.getChildren(this, kidsRef) + return kidsRef.get() + } + + protected inline fun LighterASTNode.forEachChildren(f: (LighterASTNode) -> Unit) { + val kidsArray = this.getChildrenAsArray() + for (kid in kidsArray) { + if (kid == null) continue + val tokenType = kid.tokenType + if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.WHITE_SPACE || tokenType == KtTokens.SEMICOLON) continue + f(kid) + } + } + + protected inline fun LighterASTNode.forEachChildrenReturnList(f: (LighterASTNode, MutableList) -> Unit): List { + val kidsArray = this.getChildrenAsArray() + + val container = mutableListOf() + for (kid in kidsArray) { + if (kid == null) continue + val tokenType = kid.tokenType + if (KtTokens.COMMENTS.contains(tokenType) || tokenType == KtTokens.WHITE_SPACE || tokenType == KtTokens.SEMICOLON) continue + f(kid, container) + } + + return container + } +} \ No newline at end of file diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/ConverterUtil.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ConverterUtil.kt similarity index 93% rename from compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/ConverterUtil.kt rename to compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ConverterUtil.kt index 960d8c9d4cb..60d4e0458db 100644 --- a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/ConverterUtil.kt +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ConverterUtil.kt @@ -1,9 +1,9 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the license/LICENSE.txt file. + * 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 +package org.jetbrains.kotlin.fir.lightTree.converter import com.intellij.lang.LighterASTNode import org.jetbrains.kotlin.KtNodeType @@ -18,7 +18,6 @@ import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirReturnExpression import org.jetbrains.kotlin.fir.expressions.impl.* import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint -import org.jetbrains.kotlin.fir.lightTree.fir.ValueParameter import org.jetbrains.kotlin.fir.references.FirResolvedCallableReferenceImpl import org.jetbrains.kotlin.fir.symbols.CallableId import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol @@ -164,18 +163,24 @@ object ClassNameUtil { } val currentClassId - get() = ClassId(packageFqName, className, false) + get() = ClassId( + packageFqName, + className, false) fun callableIdForName(name: Name, local: Boolean = false) = when { local -> CallableId(name) className == FqName.ROOT -> CallableId(packageFqName, name) - else -> CallableId(packageFqName, className, name) + else -> CallableId( + packageFqName, + className, name) } fun callableIdForClassConstructor() = if (className == FqName.ROOT) CallableId(packageFqName, Name.special("")) - else CallableId(packageFqName, className, className.shortName()) + else CallableId( + packageFqName, + className, className.shortName()) var className: FqName = FqName.ROOT } @@ -252,7 +257,11 @@ object DataClassUtil { firPrimaryConstructor: FirConstructor, properties: List ) { - val symbol = FirFunctionSymbol(CallableId(ClassNameUtil.packageFqName, ClassNameUtil.className, copyName)) + val symbol = FirFunctionSymbol(CallableId( + ClassNameUtil.packageFqName, + ClassNameUtil.className, + copyName + )) firClass.addDeclaration( FirMemberFunctionImpl( session, null, symbol, copyName, diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/Converter.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt similarity index 94% rename from compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/Converter.kt rename to compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index b3a99ceace4..9bd66445319 100644 --- a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/Converter.kt +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -1,13 +1,11 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the license/LICENSE.txt file. + * 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 +package org.jetbrains.kotlin.fir.lightTree.converter import com.intellij.lang.LighterASTNode -import com.intellij.openapi.util.Ref -import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IFileElementType import com.intellij.util.diff.FlyweightCapableTreeStructure import org.jetbrains.kotlin.KtNodeTypes.* @@ -22,17 +20,17 @@ import org.jetbrains.kotlin.fir.expressions.FirBlock import org.jetbrains.kotlin.fir.expressions.FirDelegatedConstructorCall import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.impl.* -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.extractArgumentsFrom -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.getAsString -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.getAsStringWithoutBacktick -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.isExpression -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.joinTypeParameters -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.nameAsSafeName -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.toDelegatedSelfType -import org.jetbrains.kotlin.fir.lightTree.ConverterUtil.toReturn -import org.jetbrains.kotlin.fir.lightTree.DataClassUtil.generateComponentFunctions -import org.jetbrains.kotlin.fir.lightTree.DataClassUtil.generateCopyFunction -import org.jetbrains.kotlin.fir.lightTree.FunctionUtil.removeLast +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.extractArgumentsFrom +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.getAsString +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.getAsStringWithoutBacktick +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.isExpression +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.joinTypeParameters +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.nameAsSafeName +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.toDelegatedSelfType +import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.toReturn +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.FunctionUtil.removeLast import org.jetbrains.kotlin.fir.lightTree.fir.ClassWrapper import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint import org.jetbrains.kotlin.fir.lightTree.fir.ValueParameter @@ -50,56 +48,11 @@ import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.name.FqName -class Converter( - val session: FirSession, - val stubMode: Boolean, - private val tree: FlyweightCapableTreeStructure -) { - private val implicitUnitType = FirImplicitUnitTypeRef(session, null) - private val implicitAnyType = FirImplicitAnyTypeRef(session, null) - private val implicitEnumType = FirImplicitEnumTypeRef(session, null) - private val implicitAnnotationType = FirImplicitAnnotationTypeRef(session, null) - private val implicitType = FirImplicitTypeRefImpl(session, null) - - private fun LighterASTNode?.getChildNodesByType(type: IElementType): List { - return this?.forEachChildrenReturnList { node, container -> - when (node.tokenType) { - type -> container += node - } - } ?: listOf() - } - - private fun LighterASTNode?.getChildrenAsArray(): Array { - if (this == null) return arrayOf() - - val kidsRef = Ref>() - tree.getChildren(this, kidsRef) - return kidsRef.get() - } - - private inline fun LighterASTNode.forEachChildren(f: (LighterASTNode) -> Unit) { - val kidsArray = this.getChildrenAsArray() - for (kid in kidsArray) { - if (kid == null) continue - val tokenType = kid.tokenType - if (COMMENTS.contains(tokenType) || tokenType == WHITE_SPACE || tokenType == SEMICOLON) continue - f(kid) - } - } - - private inline fun LighterASTNode.forEachChildrenReturnList(f: (LighterASTNode, MutableList) -> Unit): List { - val kidsArray = this.getChildrenAsArray() - - val container = mutableListOf() - for (kid in kidsArray) { - if (kid == null) continue - val tokenType = kid.tokenType - if (COMMENTS.contains(tokenType) || tokenType == WHITE_SPACE || tokenType == SEMICOLON) continue - f(kid, container) - } - - return container - } +class DeclarationsConverter( + private val session: FirSession, + private val stubMode: Boolean, + tree: FlyweightCapableTreeStructure +) : BaseConverter(session, tree) { /** * [org.jetbrains.kotlin.parsing.KotlinParsing.parseFile] diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt new file mode 100644 index 00000000000..d26b5ae3172 --- /dev/null +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt @@ -0,0 +1,18 @@ +/* + * 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.converter + +import com.intellij.lang.LighterASTNode +import com.intellij.util.diff.FlyweightCapableTreeStructure +import org.jetbrains.kotlin.fir.FirSession + +class ExpressionsConverter( + private val session: FirSession, + private val stubMode: Boolean, + tree: FlyweightCapableTreeStructure +) : BaseConverter(session, tree) { + +} \ No newline at end of file diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/fir/ValueParameter.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/fir/ValueParameter.kt index 905d9ce38b9..590043d161c 100644 --- a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/fir/ValueParameter.kt +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/fir/ValueParameter.kt @@ -11,9 +11,8 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter import org.jetbrains.kotlin.fir.declarations.impl.FirMemberPropertyImpl import org.jetbrains.kotlin.fir.expressions.impl.FirQualifiedAccessExpressionImpl -import org.jetbrains.kotlin.fir.lightTree.ClassNameUtil +import org.jetbrains.kotlin.fir.lightTree.converter.ClassNameUtil import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier -import org.jetbrains.kotlin.fir.lightTree.fir.modifier.PlatformModifier import org.jetbrains.kotlin.fir.references.FirPropertyFromParameterCallableReference import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol diff --git a/compiler/fir/lightTree/tests/org/jetbrains/kotlin/fir/lightTree/SimpleTestCase.kt b/compiler/fir/lightTree/tests/org/jetbrains/kotlin/fir/lightTree/SimpleTestCase.kt index a7752a08418..df6627d98eb 100644 --- a/compiler/fir/lightTree/tests/org/jetbrains/kotlin/fir/lightTree/SimpleTestCase.kt +++ b/compiler/fir/lightTree/tests/org/jetbrains/kotlin/fir/lightTree/SimpleTestCase.kt @@ -5,32 +5,21 @@ package org.jetbrains.kotlin.fir.lightTree -import com.intellij.lang.ParserDefinition import com.intellij.lang.impl.PsiBuilderFactoryImpl -import com.intellij.lexer.Lexer -import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.vfs.CharsetToolkit import com.intellij.psi.PsiManager import com.intellij.psi.impl.DebugUtil import com.intellij.psi.impl.PsiManagerEx import com.intellij.testFramework.LightVirtualFile import com.intellij.testFramework.TestDataPath -import com.intellij.util.PathUtil -import junit.framework.TestCase import org.jetbrains.kotlin.fir.FirRenderer import org.jetbrains.kotlin.fir.FirSessionBase import org.jetbrains.kotlin.fir.builder.AbstractRawFirBuilderTestCase -import org.jetbrains.kotlin.fir.declarations.FirFile -import org.jetbrains.kotlin.fir.render +import org.jetbrains.kotlin.fir.lightTree.converter.DeclarationsConverter import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.parsing.* import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.test.JUnit3RunnerWithInners import org.junit.runner.RunWith -import java.io.File -import java.nio.file.Path -import java.nio.file.Paths -import kotlin.system.measureNanoTime /* * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license @@ -71,7 +60,11 @@ class SimpleTestCase : AbstractRawFirBuilderTestCase() { println("AST Tree") println(DebugUtil.nodeTreeToString(builder.treeBuilt, false)) - val firFromLightTreeFile = Converter(object : FirSessionBase() {}, true, builder.lightTree).convertFile(builder.lightTree.root) + val firFromLightTreeFile = DeclarationsConverter( + object : FirSessionBase(null) {}, + true, + builder.lightTree + ).convertFile(builder.lightTree.root) println("Fir from LightTree") println(StringBuilder().also { FirRenderer(it).visitFile(firFromLightTreeFile) }.toString())