Delete extra files with util methods (raw FIR)

This commit is contained in:
Ivan Cilcic
2019-07-30 23:48:46 +03:00
committed by Mikhail Glukhikh
parent 01daa90f5c
commit f3299c4636
7 changed files with 115 additions and 156 deletions
@@ -18,8 +18,6 @@ import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.isExpression
import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.nameAsSafeName
import org.jetbrains.kotlin.name.Name
open class BaseConverter(
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.lightTree.converter
import com.intellij.lang.LighterASTNode
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.KtNodeType
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.*
@@ -16,10 +17,12 @@ import org.jetbrains.kotlin.fir.FirFunctionTarget
import org.jetbrains.kotlin.fir.FirLabel
import org.jetbrains.kotlin.fir.FirReference
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.builder.generateResolvedAccessExpression
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.*
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.*
import org.jetbrains.kotlin.fir.lightTree.fir.DestructuringDeclaration
import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirExplicitThisReference
@@ -29,6 +32,7 @@ import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtSingleValueToken
@@ -42,73 +46,106 @@ import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStringTemplateExpressionElementType
import org.jetbrains.kotlin.types.Variance
object ConverterUtil {
private val expressionSet = listOf(
REFERENCE_EXPRESSION,
DOT_QUALIFIED_EXPRESSION,
LAMBDA_EXPRESSION,
FUN
)
private val expressionSet = listOf(
REFERENCE_EXPRESSION,
DOT_QUALIFIED_EXPRESSION,
LAMBDA_EXPRESSION,
FUN
)
fun String?.nameAsSafeName(defaultName: String = ""): Name {
return when {
this != null -> Name.identifier(this.replace("`", ""))
defaultName.isNotEmpty() -> Name.identifier(defaultName)
else -> SpecialNames.NO_NAME_PROVIDED
}
}
val qualifiedAccessTokens = TokenSet.create(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION)
fun LighterASTNode.getAsStringWithoutBacktick(): String {
return this.toString().replace("`", "")
}
fun LighterASTNode.isExpression(): Boolean {
return when (this.tokenType) {
is KtNodeType,
is KtConstantExpressionElementType,
is KtStringTemplateExpressionElementType,
in expressionSet -> true
else -> false
}
}
fun FirTypeParameterContainer.joinTypeParameters(typeConstraints: List<TypeConstraint>) {
typeConstraints.forEach { typeConstraint ->
this.typeParameters.forEach { typeParameter ->
if (typeConstraint.identifier == typeParameter.name.identifier) {
(typeParameter as FirTypeParameterImpl).bounds += typeConstraint.firTypeRef
typeParameter.annotations += typeConstraint.firTypeRef.annotations
typeParameter.annotations += typeConstraint.annotations
}
}
}
}
fun <T : FirCallWithArgumentList> T.extractArgumentsFrom(container: List<FirExpression>, stubMode: Boolean): T {
if (!stubMode || this is FirAnnotationCall) {
this.arguments += container
}
return this
}
inline fun isClassLocal(classNode: LighterASTNode, getParent: LighterASTNode.() -> LighterASTNode?): Boolean {
var currentNode: LighterASTNode? = classNode
while (currentNode != null) {
val tokenType = currentNode.tokenType
val parent = currentNode.getParent()
if (tokenType == PROPERTY || tokenType == FUN) {
val grandParent = parent?.getParent()
when {
parent?.tokenType == KT_FILE -> return true
parent?.tokenType == CLASS_BODY && !(grandParent?.tokenType == OBJECT_DECLARATION && grandParent?.getParent()?.tokenType == OBJECT_LITERAL) -> return true
parent?.tokenType == BLOCK && grandParent?.tokenType == SCRIPT -> return true
}
}
if (tokenType == BLOCK) {
return true
}
currentNode = parent
}
return false
fun String?.nameAsSafeName(defaultName: String = ""): Name {
return when {
this != null -> Name.identifier(this.replace("`", ""))
defaultName.isNotEmpty() -> Name.identifier(defaultName)
else -> SpecialNames.NO_NAME_PROVIDED
}
}
fun String.getOperationSymbol(): IElementType {
KotlinExpressionParsing.ALL_OPERATIONS.types.forEach {
if (it is KtSingleValueToken && it.value == this) return it
}
if (this == "as?") return KtTokens.AS_SAFE
return KtTokens.IDENTIFIER
}
fun LighterASTNode.getAsStringWithoutBacktick(): String {
return this.toString().replace("`", "")
}
fun LighterASTNode.isExpression(): Boolean {
return when (this.tokenType) {
is KtNodeType,
is KtConstantExpressionElementType,
is KtStringTemplateExpressionElementType,
in expressionSet -> true
else -> false
}
}
fun FirTypeParameterContainer.joinTypeParameters(typeConstraints: List<TypeConstraint>) {
typeConstraints.forEach { typeConstraint ->
this.typeParameters.forEach { typeParameter ->
if (typeConstraint.identifier == typeParameter.name.identifier) {
(typeParameter as FirTypeParameterImpl).bounds += typeConstraint.firTypeRef
typeParameter.annotations += typeConstraint.firTypeRef.annotations
typeParameter.annotations += typeConstraint.annotations
}
}
}
}
fun <T : FirCallWithArgumentList> T.extractArgumentsFrom(container: List<FirExpression>, stubMode: Boolean): T {
if (!stubMode || this is FirAnnotationCall) {
this.arguments += container
}
return this
}
inline fun isClassLocal(classNode: LighterASTNode, getParent: LighterASTNode.() -> LighterASTNode?): Boolean {
var currentNode: LighterASTNode? = classNode
while (currentNode != null) {
val tokenType = currentNode.tokenType
val parent = currentNode.getParent()
if (tokenType == PROPERTY || tokenType == FUN) {
val grandParent = parent?.getParent()
when {
parent?.tokenType == KT_FILE -> return true
parent?.tokenType == CLASS_BODY && !(grandParent?.tokenType == OBJECT_DECLARATION && grandParent?.getParent()?.tokenType == OBJECT_LITERAL) -> return true
parent?.tokenType == BLOCK && grandParent?.tokenType == SCRIPT -> return true
}
}
if (tokenType == BLOCK) {
return true
}
currentNode = parent
}
return false
}
fun generateDestructuringBlock(
session: FirSession,
multiDeclaration: DestructuringDeclaration,
container: FirVariable<*>,
tmpVariable: Boolean
): FirExpression {
return FirBlockImpl(session, null).apply {
if (tmpVariable) {
statements += container
}
val isVar = multiDeclaration.isVar
for ((index, entry) in multiDeclaration.entries.withIndex()) {
statements += FirVariableImpl(
session, null, entry.name,
entry.returnTypeRef, isVar,
FirComponentCallImpl(session, null, index + 1, generateResolvedAccessExpression(session, null, container)),
FirVariableSymbol(entry.name) // TODO?
).apply {
annotations += entry.annotations
symbol.bind(this)
}
}
}
}
@@ -22,12 +22,6 @@ import org.jetbrains.kotlin.fir.declarations.impl.*
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.*
import org.jetbrains.kotlin.fir.lightTree.LightTree2Fir
import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.extractArgumentsFrom
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.isClassLocal
import org.jetbrains.kotlin.fir.lightTree.fir.*
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.TypeModifier
@@ -403,7 +397,7 @@ class DeclarationsConverter(
//parse properties
properties += primaryConstructorWrapper.valueParameters
.filter { it.hasValOrVar() }
.map { it.toFirProperty(callableIdForName(it.firValueParameter.name)) }
.map { it.toFirProperty(session, callableIdForName(it.firValueParameter.name)) }
firClass.addDeclarations(properties)
}
@@ -20,13 +20,6 @@ import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.*
import org.jetbrains.kotlin.fir.labels.FirLabelImpl
import org.jetbrains.kotlin.fir.lightTree.LightTree2Fir
import org.jetbrains.kotlin.fir.lightTree.converter.ConverterUtil.extractArgumentsFrom
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.nameAsSafeName
import org.jetbrains.kotlin.fir.lightTree.converter.utils.generateDestructuringBlock
import org.jetbrains.kotlin.fir.lightTree.converter.utils.getOperationSymbol
import org.jetbrains.kotlin.fir.lightTree.converter.utils.qualifiedAccessTokens
import org.jetbrains.kotlin.fir.lightTree.fir.ValueParameter
import org.jetbrains.kotlin.fir.lightTree.fir.WhenEntry
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
@@ -66,7 +59,8 @@ class ExpressionsConverter(
return when (expression.tokenType) {
LAMBDA_EXPRESSION -> {
val lambdaTree = LightTree2Fir.buildLightTreeLambdaExpression(expression.asText)
ExpressionsConverter(session, stubMode, lambdaTree, declarationsConverter, context).convertLambdaExpression(lambdaTree.root)
ExpressionsConverter(session, stubMode, lambdaTree, declarationsConverter, context)
.convertLambdaExpression(lambdaTree.root)
}
BINARY_EXPRESSION -> convertBinaryExpression(expression)
BINARY_WITH_TYPE -> convertBinaryWithType(expression)
@@ -1,65 +0,0 @@
/*
* 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.utils
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.KtNodeTypes.DOT_QUALIFIED_EXPRESSION
import org.jetbrains.kotlin.KtNodeTypes.SAFE_ACCESS_EXPRESSION
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.builder.RawFirBuilder
import org.jetbrains.kotlin.fir.builder.generateNotNullOrOther
import org.jetbrains.kotlin.fir.builder.generateResolvedAccessExpression
import org.jetbrains.kotlin.fir.declarations.impl.FirVariableImpl
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirVariable
import org.jetbrains.kotlin.fir.expressions.FirWhenExpression
import org.jetbrains.kotlin.fir.expressions.impl.FirBlockImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirComponentCallImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirFunctionCallImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirThrowExpressionImpl
import org.jetbrains.kotlin.fir.lightTree.fir.DestructuringDeclaration
import org.jetbrains.kotlin.fir.references.FirSimpleNamedReference
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.lexer.KtTokens.AS_SAFE
import org.jetbrains.kotlin.lexer.KtTokens.IDENTIFIER
import org.jetbrains.kotlin.parsing.KotlinExpressionParsing
val qualifiedAccessTokens = TokenSet.create(DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION)
fun String.getOperationSymbol(): IElementType {
KotlinExpressionParsing.ALL_OPERATIONS.types.forEach {
if (it is KtSingleValueToken && it.value == this) return it
}
if (this == "as?") return AS_SAFE
return IDENTIFIER
}
fun generateDestructuringBlock(
session: FirSession,
multiDeclaration: DestructuringDeclaration,
container: FirVariable<*>,
tmpVariable: Boolean
): FirExpression {
return FirBlockImpl(session, null).apply {
if (tmpVariable) {
statements += container
}
val isVar = multiDeclaration.isVar
for ((index, entry) in multiDeclaration.entries.withIndex()) {
statements += FirVariableImpl(
session, null, entry.name,
entry.returnTypeRef, isVar,
FirComponentCallImpl(session, null, index + 1, generateResolvedAccessExpression(session, null, container)),
FirVariableSymbol(entry.name) // TODO?
).apply {
annotations += entry.annotations
symbol.bind(this)
}
}
}
}
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirVariable
import org.jetbrains.kotlin.fir.expressions.impl.FirErrorExpressionImpl
import org.jetbrains.kotlin.fir.lightTree.converter.utils.generateDestructuringBlock
import org.jetbrains.kotlin.fir.lightTree.converter.generateDestructuringBlock
data class DestructuringDeclaration(
val isVar: Boolean,
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.fir.lightTree.fir
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
@@ -32,15 +33,15 @@ class ValueParameter(
return isVal || isVar
}
fun toFirProperty(callableId: CallableId): FirProperty {
fun toFirProperty(session: FirSession, callableId: CallableId): FirProperty {
val name = this.firValueParameter.name
var type = this.firValueParameter.returnTypeRef
if (type is FirImplicitTypeRef) {
type = FirErrorTypeRefImpl(this.firValueParameter.session, null, "Incomplete code")
type = FirErrorTypeRefImpl(session, null, "Incomplete code")
}
return FirMemberPropertyImpl(
this.firValueParameter.session,
session,
null,
FirPropertySymbol(callableId),
name,
@@ -54,14 +55,14 @@ class ValueParameter(
receiverTypeRef = null,
returnTypeRef = type,
isVar = this.isVar,
initializer = FirQualifiedAccessExpressionImpl(this.firValueParameter.session, null).apply {
initializer = FirQualifiedAccessExpressionImpl(session, null).apply {
calleeReference = FirPropertyFromParameterCallableReference(
this@ValueParameter.firValueParameter.session, null, name, this@ValueParameter.firValueParameter.symbol
session, null, name, this@ValueParameter.firValueParameter.symbol
)
},
getter = FirDefaultPropertyGetter(this.firValueParameter.session, null, type, modifiers.getVisibility()),
getter = FirDefaultPropertyGetter(session, null, type, modifiers.getVisibility()),
setter = if (this.isVar) FirDefaultPropertySetter(
this.firValueParameter.session,
session,
null,
type,
modifiers.getVisibility()