Create ExpressionsConverter class and took out light tree node handlers
This commit is contained in:
committed by
Mikhail Glukhikh
parent
05757c1ada
commit
a66adffdcf
@@ -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)
|
||||
}
|
||||
}
|
||||
+65
@@ -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<LighterASTNode>
|
||||
) {
|
||||
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<LighterASTNode> {
|
||||
return this?.forEachChildrenReturnList { node, container ->
|
||||
when (node.tokenType) {
|
||||
type -> container += node
|
||||
}
|
||||
} ?: listOf()
|
||||
}
|
||||
|
||||
protected fun LighterASTNode?.getChildrenAsArray(): Array<LighterASTNode?> {
|
||||
if (this == null) return arrayOf()
|
||||
|
||||
val kidsRef = Ref<Array<LighterASTNode?>>()
|
||||
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 <T> LighterASTNode.forEachChildrenReturnList(f: (LighterASTNode, MutableList<T>) -> Unit): List<T> {
|
||||
val kidsArray = this.getChildrenAsArray()
|
||||
|
||||
val container = mutableListOf<T>()
|
||||
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
|
||||
}
|
||||
}
|
||||
+17
-8
@@ -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("<anonymous-init>"))
|
||||
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<FirProperty>
|
||||
) {
|
||||
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,
|
||||
+19
-66
@@ -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<LighterASTNode>
|
||||
) {
|
||||
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<LighterASTNode> {
|
||||
return this?.forEachChildrenReturnList { node, container ->
|
||||
when (node.tokenType) {
|
||||
type -> container += node
|
||||
}
|
||||
} ?: listOf()
|
||||
}
|
||||
|
||||
private fun LighterASTNode?.getChildrenAsArray(): Array<LighterASTNode?> {
|
||||
if (this == null) return arrayOf()
|
||||
|
||||
val kidsRef = Ref<Array<LighterASTNode?>>()
|
||||
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 <T> LighterASTNode.forEachChildrenReturnList(f: (LighterASTNode, MutableList<T>) -> Unit): List<T> {
|
||||
val kidsArray = this.getChildrenAsArray()
|
||||
|
||||
val container = mutableListOf<T>()
|
||||
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<LighterASTNode>
|
||||
) : BaseConverter(session, tree) {
|
||||
|
||||
/**
|
||||
* [org.jetbrains.kotlin.parsing.KotlinParsing.parseFile]
|
||||
+18
@@ -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<LighterASTNode>
|
||||
) : BaseConverter(session, tree) {
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user