Make all necessary refactoring after merge

1) Removed all unnecessary sessions
2) Added delegation converter
3) Moved property accessor initialization to apply block instead of constructor
4) Removed unnecessary AnotationContainer class
This commit is contained in:
Ivan Cilcic
2019-07-31 17:01:15 +03:00
committed by Mikhail Glukhikh
parent f3a7db2428
commit 00f880a5a0
15 changed files with 310 additions and 298 deletions
@@ -25,7 +25,7 @@ open class BaseConverter(
private val tree: FlyweightCapableTreeStructure<LighterASTNode>, private val tree: FlyweightCapableTreeStructure<LighterASTNode>,
context: Context = Context() context: Context = Context()
) : BaseFirBuilder<LighterASTNode>(session, context) { ) : BaseFirBuilder<LighterASTNode>(session, context) {
protected val implicitType = FirImplicitTypeRefImpl(session, null) protected val implicitType = FirImplicitTypeRefImpl(null)
override val LighterASTNode.elementType: IElementType override val LighterASTNode.elementType: IElementType
get() = this.tokenType get() = this.tokenType
@@ -9,42 +9,29 @@ import com.intellij.lang.LighterASTNode
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet import com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.KtNodeType import org.jetbrains.kotlin.KtNodeType
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtNodeTypes.* import org.jetbrains.kotlin.KtNodeTypes.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
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.FirSession
import org.jetbrains.kotlin.fir.builder.addDefaultBoundIfNecessary
import org.jetbrains.kotlin.fir.builder.generateResolvedAccessExpression import org.jetbrains.kotlin.fir.builder.generateResolvedAccessExpression
import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.FirTypeParameterContainer
import org.jetbrains.kotlin.fir.declarations.impl.* import org.jetbrains.kotlin.fir.declarations.impl.FirTypeParameterImpl
import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.declarations.impl.FirVariableImpl
import org.jetbrains.kotlin.fir.expressions.impl.* 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.FirBlockImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirCallWithArgumentList
import org.jetbrains.kotlin.fir.expressions.impl.FirComponentCallImpl
import org.jetbrains.kotlin.fir.lightTree.fir.DestructuringDeclaration import org.jetbrains.kotlin.fir.lightTree.fir.DestructuringDeclaration
import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint import org.jetbrains.kotlin.fir.lightTree.fir.TypeConstraint
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirExplicitThisReference
import org.jetbrains.kotlin.fir.references.FirResolvedCallableReferenceImpl
import org.jetbrains.kotlin.fir.references.FirSimpleNamedReference
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.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtSingleValueToken import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.parsing.KotlinExpressionParsing import org.jetbrains.kotlin.parsing.KotlinExpressionParsing
import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtConstantExpressionElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStringTemplateExpressionElementType import org.jetbrains.kotlin.psi.stubs.elements.KtStringTemplateExpressionElementType
import org.jetbrains.kotlin.types.Variance
private val expressionSet = listOf( private val expressionSet = listOf(
REFERENCE_EXPRESSION, REFERENCE_EXPRESSION,
@@ -93,6 +80,7 @@ fun FirTypeParameterContainer.joinTypeParameters(typeConstraints: List<TypeConst
typeParameter.annotations += typeConstraint.firTypeRef.annotations typeParameter.annotations += typeConstraint.firTypeRef.annotations
typeParameter.annotations += typeConstraint.annotations typeParameter.annotations += typeConstraint.annotations
} }
(typeParameter as FirTypeParameterImpl).addDefaultBoundIfNecessary()
} }
} }
} }
@@ -131,7 +119,7 @@ fun generateDestructuringBlock(
container: FirVariable<*>, container: FirVariable<*>,
tmpVariable: Boolean tmpVariable: Boolean
): FirExpression { ): FirExpression {
return FirBlockImpl(session, null).apply { return FirBlockImpl(null).apply {
if (tmpVariable) { if (tmpVariable) {
statements += container statements += container
} }
@@ -140,7 +128,7 @@ fun generateDestructuringBlock(
statements += FirVariableImpl( statements += FirVariableImpl(
session, null, entry.name, session, null, entry.name,
entry.returnTypeRef, isVar, entry.returnTypeRef, isVar,
FirComponentCallImpl(session, null, index + 1, generateResolvedAccessExpression(session, null, container)), FirComponentCallImpl(null, index + 1, generateResolvedAccessExpression(null, container)),
FirVariableSymbol(entry.name) // TODO? FirVariableSymbol(entry.name) // TODO?
).apply { ).apply {
annotations += entry.annotations annotations += entry.annotations
@@ -7,16 +7,15 @@ package org.jetbrains.kotlin.fir.lightTree.converter
import com.intellij.lang.LighterASTNode import com.intellij.lang.LighterASTNode
import com.intellij.psi.TokenType import com.intellij.psi.TokenType
import com.intellij.psi.tree.IFileElementType
import com.intellij.util.diff.FlyweightCapableTreeStructure import com.intellij.util.diff.FlyweightCapableTreeStructure
import org.jetbrains.kotlin.KtNodeTypes.* import org.jetbrains.kotlin.KtNodeTypes.*
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.builder.Context import org.jetbrains.kotlin.fir.builder.Context
import org.jetbrains.kotlin.fir.builder.generateAccessorsByDelegate
import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.* import org.jetbrains.kotlin.fir.declarations.impl.*
import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.*
@@ -104,7 +103,7 @@ class DeclarationsConverter(
else -> if (node.isExpression()) container += expressionConverter.getAsFirExpression<FirStatement>(node) else -> if (node.isExpression()) container += expressionConverter.getAsFirExpression<FirStatement>(node)
} }
} }
return FirBlockImpl(session, null).apply { return FirBlockImpl(null).apply {
firStatements.forEach { firStatement -> firStatements.forEach { firStatement ->
if (firStatement !is FirBlock || firStatement.annotations.isNotEmpty()) { if (firStatement !is FirBlock || firStatement.annotations.isNotEmpty()) {
statements += firStatement statements += firStatement
@@ -156,7 +155,6 @@ class DeclarationsConverter(
} }
return FirImportImpl( return FirImportImpl(
session,
null, null,
importedFqName, importedFqName,
isAllUnder, isAllUnder,
@@ -180,7 +178,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseModifierList * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseModifierList
*/ */
private fun convertModifierList(modifiers: LighterASTNode): Modifier { private fun convertModifierList(modifiers: LighterASTNode): Modifier {
val modifier = Modifier(session) val modifier = Modifier()
modifiers.forEachChildren { modifiers.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
ANNOTATION -> modifier.annotations += convertAnnotation(it) ANNOTATION -> modifier.annotations += convertAnnotation(it)
@@ -195,7 +193,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeModifierList * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeModifierList
*/ */
private fun convertTypeModifierList(modifiers: LighterASTNode): TypeModifier { private fun convertTypeModifierList(modifiers: LighterASTNode): TypeModifier {
val typeModifierList = TypeModifier(session) val typeModifierList = TypeModifier()
modifiers.forEachChildren { modifiers.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
ANNOTATION -> typeModifierList.annotations += convertAnnotation(it) ANNOTATION -> typeModifierList.annotations += convertAnnotation(it)
@@ -210,7 +208,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeArgumentModifierList * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeArgumentModifierList
*/ */
private fun convertTypeArgumentModifierList(modifiers: LighterASTNode): TypeProjectionModifier { private fun convertTypeArgumentModifierList(modifiers: LighterASTNode): TypeProjectionModifier {
val typeArgumentModifierList = TypeProjectionModifier(session) val typeArgumentModifierList = TypeProjectionModifier()
modifiers.forEachChildren { modifiers.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
ANNOTATION -> typeArgumentModifierList.annotations += convertAnnotation(it) ANNOTATION -> typeArgumentModifierList.annotations += convertAnnotation(it)
@@ -225,7 +223,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeArgumentModifierList * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeArgumentModifierList
*/ */
private fun convertTypeParameterModifiers(modifiers: LighterASTNode): TypeParameterModifier { private fun convertTypeParameterModifiers(modifiers: LighterASTNode): TypeParameterModifier {
val modifier = TypeParameterModifier(session) val modifier = TypeParameterModifier()
modifiers.forEachChildren { modifiers.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
ANNOTATION -> modifier.annotations += convertAnnotation(it) ANNOTATION -> modifier.annotations += convertAnnotation(it)
@@ -301,7 +299,6 @@ class DeclarationsConverter(
} }
} }
return FirAnnotationCallImpl( return FirAnnotationCallImpl(
session,
null, null,
annotationUseSiteTarget ?: defaultAnnotationUseSiteTarget, annotationUseSiteTarget ?: defaultAnnotationUseSiteTarget,
constructorCalleePair.first constructorCalleePair.first
@@ -313,7 +310,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseClassOrObject * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseClassOrObject
*/ */
private fun convertClass(classNode: LighterASTNode): FirDeclaration { private fun convertClass(classNode: LighterASTNode): FirDeclaration {
var modifiers = Modifier(session) var modifiers = Modifier()
var classKind: ClassKind = ClassKind.CLASS //TODO var classKind: ClassKind = ClassKind.CLASS //TODO
var identifier: String? = null var identifier: String? = null
val firTypeParameters = mutableListOf<FirTypeParameter>() val firTypeParameters = mutableListOf<FirTypeParameter>()
@@ -382,10 +379,10 @@ class DeclarationsConverter(
firClass.superTypeRefs += superTypeRefs firClass.superTypeRefs += superTypeRefs
val classWrapper = ClassWrapper( val classWrapper = ClassWrapper(
session, className, modifiers, classKind, className, modifiers, classKind, primaryConstructor != null,
primaryConstructor != null,
classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(), classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
null.toDelegatedSelfType(firClass), delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry null.toDelegatedSelfType(firClass),
delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry
) )
//parse primary constructor //parse primary constructor
val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper) val primaryConstructorWrapper = convertPrimaryConstructor(primaryConstructor, classWrapper)
@@ -410,7 +407,9 @@ class DeclarationsConverter(
if (modifiers.isDataClass() && firPrimaryConstructor != null) { if (modifiers.isDataClass() && firPrimaryConstructor != null) {
val zippedParameters = MutableList(properties.size) { null }.zip(properties) val zippedParameters = MutableList(properties.size) { null }.zip(properties)
zippedParameters.generateComponentFunctions(session, firClass, context.packageFqName, context.className) zippedParameters.generateComponentFunctions(session, firClass, context.packageFqName, context.className)
zippedParameters.generateCopyFunction(session, null, firClass, context.packageFqName, context.className, firPrimaryConstructor) zippedParameters.generateCopyFunction(
session, null, firClass, context.packageFqName, context.className, firPrimaryConstructor
)
// TODO: equals, hashCode, toString // TODO: equals, hashCode, toString
} }
@@ -423,7 +422,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitObjectLiteralExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitObjectLiteralExpression
*/ */
fun convertObjectLiteral(objectLiteral: LighterASTNode): FirElement { fun convertObjectLiteral(objectLiteral: LighterASTNode): FirElement {
var modifiers = Modifier(session) var modifiers = Modifier()
var primaryConstructor: LighterASTNode? = null var primaryConstructor: LighterASTNode? = null
val superTypeRefs = mutableListOf<FirTypeRef>() val superTypeRefs = mutableListOf<FirTypeRef>()
val superTypeCallEntry = mutableListOf<FirExpression>() val superTypeCallEntry = mutableListOf<FirExpression>()
@@ -445,14 +444,13 @@ class DeclarationsConverter(
superTypeRefs.ifEmpty { superTypeRefs += implicitAnyType } superTypeRefs.ifEmpty { superTypeRefs += implicitAnyType }
val delegatedType = delegatedSuperTypeRef ?: implicitAnyType val delegatedType = delegatedSuperTypeRef ?: implicitAnyType
return FirAnonymousObjectImpl(session, null).apply { return FirAnonymousObjectImpl(null).apply {
annotations += modifiers.annotations annotations += modifiers.annotations
this.superTypeRefs += superTypeRefs this.superTypeRefs += superTypeRefs
this.typeRef = superTypeRefs.first() this.typeRef = superTypeRefs.first()
val classWrapper = ClassWrapper( val classWrapper = ClassWrapper(
this@DeclarationsConverter.session, SpecialNames.NO_NAME_PROVIDED, modifiers, ClassKind.OBJECT, SpecialNames.NO_NAME_PROVIDED, modifiers, ClassKind.OBJECT, hasPrimaryConstructor = false,
hasPrimaryConstructor = false,
hasSecondaryConstructor = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(), hasSecondaryConstructor = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
delegatedSelfTypeRef = delegatedType, delegatedSelfTypeRef = delegatedType,
delegatedSuperTypeRef = delegatedType, delegatedSuperTypeRef = delegatedType,
@@ -472,7 +470,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseEnumEntry * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseEnumEntry
*/ */
private fun convertEnumEntry(enumEntry: LighterASTNode, classWrapper: ClassWrapper): FirEnumEntryImpl { private fun convertEnumEntry(enumEntry: LighterASTNode, classWrapper: ClassWrapper): FirEnumEntryImpl {
var modifiers = Modifier(session) var modifiers = Modifier()
lateinit var identifier: String lateinit var identifier: String
var hasInitializerList = false var hasInitializerList = false
val enumSuperTypeCallEntry = mutableListOf<FirExpression>() val enumSuperTypeCallEntry = mutableListOf<FirExpression>()
@@ -507,8 +505,7 @@ class DeclarationsConverter(
} }
val enumClassWrapper = ClassWrapper( val enumClassWrapper = ClassWrapper(
session, enumEntryName, modifiers, ClassKind.ENUM_ENTRY, enumEntryName, modifiers, ClassKind.ENUM_ENTRY, hasPrimaryConstructor = true,
hasPrimaryConstructor = true,
hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(), hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
delegatedSelfTypeRef = null.toDelegatedSelfType(firEnumEntry), delegatedSelfTypeRef = null.toDelegatedSelfType(firEnumEntry),
delegatedSuperTypeRef = if (hasInitializerList) classWrapper.getFirUserTypeFromClassName() else defaultDelegatedSuperTypeRef, delegatedSuperTypeRef = if (hasInitializerList) classWrapper.getFirUserTypeFromClassName() else defaultDelegatedSuperTypeRef,
@@ -566,7 +563,7 @@ class DeclarationsConverter(
if (primaryConstructor == null && classWrapper.hasSecondaryConstructor) return null if (primaryConstructor == null && classWrapper.hasSecondaryConstructor) return null
if (classWrapper.isInterface()) return null if (classWrapper.isInterface()) return null
var modifiers = Modifier(session) var modifiers = Modifier()
val valueParameters = mutableListOf<ValueParameter>() val valueParameters = mutableListOf<ValueParameter>()
primaryConstructor?.forEachChildren { primaryConstructor?.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -577,7 +574,6 @@ class DeclarationsConverter(
val defaultVisibility = classWrapper.defaultConstructorVisibility() val defaultVisibility = classWrapper.defaultConstructorVisibility()
val firDelegatedCall = FirDelegatedConstructorCallImpl( val firDelegatedCall = FirDelegatedConstructorCallImpl(
session,
null, null,
classWrapper.delegatedSuperTypeRef, classWrapper.delegatedSuperTypeRef,
isThis = false isThis = false
@@ -616,7 +612,7 @@ class DeclarationsConverter(
return FirAnonymousInitializerImpl( return FirAnonymousInitializerImpl(
session, session,
null, null,
if (stubMode) FirEmptyExpressionBlock(session) else firBlock if (stubMode) FirEmptyExpressionBlock() else firBlock
) )
} }
@@ -624,7 +620,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseSecondaryConstructor * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseSecondaryConstructor
*/ */
private fun convertSecondaryConstructor(secondaryConstructor: LighterASTNode, classWrapper: ClassWrapper): FirConstructor { private fun convertSecondaryConstructor(secondaryConstructor: LighterASTNode, classWrapper: ClassWrapper): FirConstructor {
var modifiers = Modifier(session) var modifiers = Modifier()
val firValueParameters = mutableListOf<ValueParameter>() val firValueParameters = mutableListOf<ValueParameter>()
var constructorDelegationCall: FirDelegatedConstructorCall? = null var constructorDelegationCall: FirDelegatedConstructorCall? = null
var block: LighterASTNode? = null var block: LighterASTNode? = null
@@ -639,7 +635,7 @@ class DeclarationsConverter(
} }
val delegatedSelfTypeRef = val delegatedSelfTypeRef =
if (classWrapper.isObjectLiteral()) FirErrorTypeRefImpl(session, null, "Constructor in object") if (classWrapper.isObjectLiteral()) FirErrorTypeRefImpl(null, "Constructor in object")
else classWrapper.delegatedSelfTypeRef else classWrapper.delegatedSelfTypeRef
val firConstructor = FirConstructorImpl( val firConstructor = FirConstructorImpl(
@@ -683,8 +679,8 @@ class DeclarationsConverter(
val isThis = (isImplicit && classWrapper.hasPrimaryConstructor) || thisKeywordPresent val isThis = (isImplicit && classWrapper.hasPrimaryConstructor) || thisKeywordPresent
val delegatedType = val delegatedType =
if (classWrapper.isObjectLiteral() || classWrapper.isInterface()) when { if (classWrapper.isObjectLiteral() || classWrapper.isInterface()) when {
isThis -> FirErrorTypeRefImpl(session, null, "Constructor in object") isThis -> FirErrorTypeRefImpl(null, "Constructor in object")
else -> FirErrorTypeRefImpl(session, null, "No super type") else -> FirErrorTypeRefImpl(null, "No super type")
} }
else when { else when {
isThis -> classWrapper.delegatedSelfTypeRef isThis -> classWrapper.delegatedSelfTypeRef
@@ -692,7 +688,6 @@ class DeclarationsConverter(
} }
return FirDelegatedConstructorCallImpl( return FirDelegatedConstructorCallImpl(
session,
null, null,
delegatedType, delegatedType,
isThis isThis
@@ -703,7 +698,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeAlias * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeAlias
*/ */
private fun convertTypeAlias(typeAlias: LighterASTNode): FirDeclaration { private fun convertTypeAlias(typeAlias: LighterASTNode): FirDeclaration {
var modifiers = Modifier(session) var modifiers = Modifier()
var identifier: String? = null var identifier: String? = null
lateinit var firType: FirTypeRef lateinit var firType: FirTypeRef
val firTypeParameters = mutableListOf<FirTypeParameter>() val firTypeParameters = mutableListOf<FirTypeParameter>()
@@ -738,7 +733,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseProperty * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseProperty
*/ */
fun convertPropertyDeclaration(property: LighterASTNode): FirDeclaration { fun convertPropertyDeclaration(property: LighterASTNode): FirDeclaration {
var modifiers = Modifier(session) var modifiers = Modifier()
var identifier: String? = null var identifier: String? = null
val firTypeParameters = mutableListOf<FirTypeParameter>() val firTypeParameters = mutableListOf<FirTypeParameter>()
var isReturnType = false var isReturnType = false
@@ -780,9 +775,14 @@ class DeclarationsConverter(
returnType, returnType,
isVar, isVar,
firExpression, firExpression,
delegate = delegateExpression?.let { expressionConverter.getAsFirExpression(it, "Incorrect delegate expression") } delegate = delegateExpression?.let {
FirWrappedDelegateExpressionImpl(
null, expressionConverter.getAsFirExpression(it, "Incorrect delegate expression")
)
}
).apply { ).apply {
annotations += modifiers.annotations annotations += modifiers.annotations
this.generateAccessorsByDelegate(this@DeclarationsConverter.session, member = false, stubMode = stubMode)
} }
} else { } else {
FirMemberPropertyImpl( FirMemberPropertyImpl(
@@ -801,13 +801,21 @@ class DeclarationsConverter(
returnType, returnType,
isVar, isVar,
firExpression, firExpression,
getter ?: FirDefaultPropertyGetter(session, null, returnType, modifiers.getVisibility()), delegateExpression?.let {
if (isVar) setter ?: FirDefaultPropertySetter(session, null, returnType, modifiers.getVisibility()) else null, FirWrappedDelegateExpressionImpl(
delegateExpression?.let { expressionConverter.getAsFirExpression(it, "Should have delegate") } null,
expressionConverter.getAsFirExpression(it, "Should have delegate")
)
}
).apply { ).apply {
this.typeParameters += firTypeParameters this.typeParameters += firTypeParameters
this.joinTypeParameters(typeConstraints) this.joinTypeParameters(typeConstraints)
annotations += modifiers.annotations annotations += modifiers.annotations
this.getter = getter ?: FirDefaultPropertyGetter(session, null, returnType, modifiers.getVisibility())
this.setter = if (isVar) setter ?: FirDefaultPropertySetter(session, null, returnType, modifiers.getVisibility()) else null
generateAccessorsByDelegate(
this@DeclarationsConverter.session, member = parentNode?.tokenType != KT_FILE, stubMode = stubMode
)
} }
} }
} }
@@ -818,7 +826,7 @@ class DeclarationsConverter(
private fun convertDestructingDeclaration(destructingDeclaration: LighterASTNode): DestructuringDeclaration { private fun convertDestructingDeclaration(destructingDeclaration: LighterASTNode): DestructuringDeclaration {
var isVar = false var isVar = false
val entries = mutableListOf<FirVariable<*>>() val entries = mutableListOf<FirVariable<*>>()
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "Destructuring declaration without initializer") var firExpression: FirExpression = FirErrorExpressionImpl(null, "Destructuring declaration without initializer")
destructingDeclaration.forEachChildren { destructingDeclaration.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
VAR_KEYWORD -> isVar = true VAR_KEYWORD -> isVar = true
@@ -835,7 +843,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseMultiDeclarationName * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseMultiDeclarationName
*/ */
private fun convertDestructingDeclarationEntry(entry: LighterASTNode): FirVariable<*> { private fun convertDestructingDeclarationEntry(entry: LighterASTNode): FirVariable<*> {
var modifiers = Modifier(session) var modifiers = Modifier()
var identifier: String? = null var identifier: String? = null
var firType: FirTypeRef? = null var firType: FirTypeRef? = null
entry.forEachChildren { entry.forEachChildren {
@@ -857,7 +865,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parsePropertyGetterOrSetter * @see org.jetbrains.kotlin.parsing.KotlinParsing.parsePropertyGetterOrSetter
*/ */
private fun convertGetterOrSetter(getterOrSetter: LighterASTNode, propertyTypeRef: FirTypeRef): FirPropertyAccessor { private fun convertGetterOrSetter(getterOrSetter: LighterASTNode, propertyTypeRef: FirTypeRef): FirPropertyAccessor {
var modifiers = Modifier(session) var modifiers = Modifier()
var isGetter = true var isGetter = true
var returnType: FirTypeRef? = null var returnType: FirTypeRef? = null
var firValueParameters: FirValueParameter = FirDefaultSetterValueParameter(session, null, propertyTypeRef) var firValueParameters: FirValueParameter = FirDefaultSetterValueParameter(session, null, propertyTypeRef)
@@ -901,7 +909,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.toFirValueParameter * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.toFirValueParameter
*/ */
private fun convertSetterParameter(setterParameter: LighterASTNode, propertyTypeRef: FirTypeRef): FirValueParameter { private fun convertSetterParameter(setterParameter: LighterASTNode, propertyTypeRef: FirTypeRef): FirValueParameter {
var modifiers = Modifier(session) var modifiers = Modifier()
lateinit var firValueParameter: FirValueParameter lateinit var firValueParameter: FirValueParameter
setterParameter.forEachChildren { setterParameter.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -928,7 +936,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseFunction * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseFunction
*/ */
fun convertFunctionDeclaration(functionDeclaration: LighterASTNode): FirDeclaration { fun convertFunctionDeclaration(functionDeclaration: LighterASTNode): FirDeclaration {
var modifiers = Modifier(session) var modifiers = Modifier()
var identifier: String? = null var identifier: String? = null
val firTypeParameters = mutableListOf<FirTypeParameter>() val firTypeParameters = mutableListOf<FirTypeParameter>()
var valueParametersList: LighterASTNode? = null var valueParametersList: LighterASTNode? = null
@@ -1009,7 +1017,6 @@ class DeclarationsConverter(
return when { return when {
blockNode != null -> return convertBlock(blockNode) blockNode != null -> return convertBlock(blockNode)
expression != null -> FirSingleExpressionBlock( expression != null -> FirSingleExpressionBlock(
session,
expressionConverter.getAsFirExpression<FirExpression>(expression, "Function has no body (but should)").toReturn() expressionConverter.getAsFirExpression<FirExpression>(expression, "Function has no body (but should)").toReturn()
) )
else -> null else -> null
@@ -1020,10 +1027,9 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseBlock * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseBlock
*/ */
fun convertBlock(block: LighterASTNode?): FirBlock { fun convertBlock(block: LighterASTNode?): FirBlock {
if (block == null) return FirEmptyExpressionBlock(session) if (block == null) return FirEmptyExpressionBlock()
if (block.tokenType != BLOCK) { if (block.tokenType != BLOCK) {
return FirSingleExpressionBlock( return FirSingleExpressionBlock(
session,
expressionConverter.getAsFirExpression(block) expressionConverter.getAsFirExpression(block)
) )
} }
@@ -1032,8 +1038,7 @@ class DeclarationsConverter(
return DeclarationsConverter(session, stubMode, blockTree, context).convertBlockExpression(blockTree.root) return DeclarationsConverter(session, stubMode, blockTree, context).convertBlockExpression(blockTree.root)
} else { } else {
FirSingleExpressionBlock( FirSingleExpressionBlock(
session, FirExpressionStub(null).toReturn()
FirExpressionStub(session, null).toReturn()
) )
} }
} }
@@ -1093,7 +1098,7 @@ class DeclarationsConverter(
*/ */
private fun convertExplicitDelegation(explicitDelegation: LighterASTNode): FirDelegatedTypeRef { private fun convertExplicitDelegation(explicitDelegation: LighterASTNode): FirDelegatedTypeRef {
lateinit var firTypeRef: FirTypeRef lateinit var firTypeRef: FirTypeRef
var firExpression: FirExpression? = FirErrorExpressionImpl(session, null, "Should have delegate") var firExpression: FirExpression? = FirErrorExpressionImpl(null, "Should have delegate")
explicitDelegation.forEachChildren { explicitDelegation.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
TYPE_REFERENCE -> firTypeRef = convertType(it) TYPE_REFERENCE -> firTypeRef = convertType(it)
@@ -1102,7 +1107,6 @@ class DeclarationsConverter(
} }
return FirDelegatedTypeRefImpl( return FirDelegatedTypeRefImpl(
session,
firTypeRef, firTypeRef,
firExpression firExpression
) )
@@ -1154,7 +1158,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeParameter * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseTypeParameter
*/ */
private fun convertTypeParameter(typeParameter: LighterASTNode): FirTypeParameter { private fun convertTypeParameter(typeParameter: LighterASTNode): FirTypeParameter {
var typeParameterModifiers = TypeParameterModifier(session) var typeParameterModifiers = TypeParameterModifier()
var identifier: String? = null var identifier: String? = null
var firType: FirTypeRef? = null var firType: FirTypeRef? = null
typeParameter.forEachChildren { typeParameter.forEachChildren {
@@ -1184,10 +1188,10 @@ class DeclarationsConverter(
*/ */
fun convertType(type: LighterASTNode): FirTypeRef { fun convertType(type: LighterASTNode): FirTypeRef {
if (type.asText.isEmpty()) { if (type.asText.isEmpty()) {
return FirErrorTypeRefImpl(session, null, "Unwrapped type is null") return FirErrorTypeRefImpl(null, "Unwrapped type is null")
} }
var typeModifiers = TypeModifier(session) //TODO what with suspend? var typeModifiers = TypeModifier() //TODO what with suspend?
var firType: FirTypeRef = FirErrorTypeRefImpl(session, null, "Incomplete code") var firType: FirTypeRef = FirErrorTypeRefImpl(null, "Incomplete code")
var afterLPar = false var afterLPar = false
type.forEachChildren { type.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -1197,8 +1201,8 @@ class DeclarationsConverter(
USER_TYPE -> firType = convertUserType(it) USER_TYPE -> firType = convertUserType(it)
NULLABLE_TYPE -> firType = convertNullableType(it) NULLABLE_TYPE -> firType = convertNullableType(it)
FUNCTION_TYPE -> firType = convertFunctionType(it) FUNCTION_TYPE -> firType = convertFunctionType(it)
DYNAMIC_TYPE -> firType = FirDynamicTypeRefImpl(session, null, false) DYNAMIC_TYPE -> firType = FirDynamicTypeRefImpl(null, false)
TokenType.ERROR_ELEMENT -> firType = FirErrorTypeRefImpl(session, null, "Unwrapped type is null") TokenType.ERROR_ELEMENT -> firType = FirErrorTypeRefImpl(null, "Unwrapped type is null")
} }
} }
@@ -1230,7 +1234,7 @@ class DeclarationsConverter(
convertUserType(it, true) convertUserType(it, true)
FUNCTION_TYPE -> firType = convertFunctionType(it, true) FUNCTION_TYPE -> firType = convertFunctionType(it, true)
NULLABLE_TYPE -> firType = convertNullableType(it) NULLABLE_TYPE -> firType = convertNullableType(it)
DYNAMIC_TYPE -> firType = FirDynamicTypeRefImpl(session, null, true) DYNAMIC_TYPE -> firType = FirDynamicTypeRefImpl(null, true)
} }
} }
@@ -1253,14 +1257,13 @@ class DeclarationsConverter(
} }
if (identifier == null) if (identifier == null)
return FirErrorTypeRefImpl(session, null, "Incomplete user type") return FirErrorTypeRefImpl(null, "Incomplete user type")
val qualifier = FirQualifierPartImpl( val qualifier = FirQualifierPartImpl(
identifier.nameAsSafeName() identifier.nameAsSafeName()
).apply { typeArguments += firTypeArguments } ).apply { typeArguments += firTypeArguments }
return FirUserTypeRefImpl( return FirUserTypeRefImpl(
session,
null, null,
isNullable isNullable
).apply { ).apply {
@@ -1284,7 +1287,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.tryParseTypeArgumentList * @see org.jetbrains.kotlin.parsing.KotlinParsing.tryParseTypeArgumentList
*/ */
private fun convertTypeProjection(typeProjection: LighterASTNode): FirTypeProjection { private fun convertTypeProjection(typeProjection: LighterASTNode): FirTypeProjection {
var modifiers = TypeProjectionModifier(session) var modifiers = TypeProjectionModifier()
lateinit var firType: FirTypeRef lateinit var firType: FirTypeRef
var isStarProjection = false var isStarProjection = false
typeProjection.forEachChildren { typeProjection.forEachChildren {
@@ -1296,9 +1299,8 @@ class DeclarationsConverter(
} }
//annotations from modifiers must be ignored //annotations from modifiers must be ignored
return if (isStarProjection) FirStarProjectionImpl(session, null) return if (isStarProjection) FirStarProjectionImpl(null)
else FirTypeProjectionWithVarianceImpl( else FirTypeProjectionWithVarianceImpl(
session,
null, null,
modifiers.getVariance(), modifiers.getVariance(),
firType firType
@@ -1321,7 +1323,6 @@ class DeclarationsConverter(
} }
return FirFunctionTypeRefImpl( return FirFunctionTypeRefImpl(
session,
null, null,
isNullable, isNullable,
receiverTypeReference, receiverTypeReference,
@@ -1344,7 +1345,7 @@ class DeclarationsConverter(
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseValueParameter * @see org.jetbrains.kotlin.parsing.KotlinParsing.parseValueParameter
*/ */
fun convertValueParameter(valueParameter: LighterASTNode): ValueParameter { fun convertValueParameter(valueParameter: LighterASTNode): ValueParameter {
var modifiers = Modifier(session) var modifiers = Modifier()
var isVal = false var isVal = false
var isVar = false var isVar = false
var identifier: String? = null var identifier: String? = null
@@ -46,7 +46,7 @@ class ExpressionsConverter(
) : BaseConverter(session, tree, context) { ) : BaseConverter(session, tree, context) {
inline fun <reified R : FirElement> getAsFirExpression(expression: LighterASTNode?, errorReason: String = ""): R { inline fun <reified R : FirElement> getAsFirExpression(expression: LighterASTNode?, errorReason: String = ""): R {
return expression?.let { convertExpression(it, errorReason) } as? R ?: (FirErrorExpressionImpl(session, null, errorReason) as R) return expression?.let { convertExpression(it, errorReason) } as? R ?: (FirErrorExpressionImpl(null, errorReason) as R)
} }
/***** EXPRESSIONS *****/ /***** EXPRESSIONS *****/
@@ -94,11 +94,11 @@ class ExpressionsConverter(
OBJECT_LITERAL -> declarationsConverter.convertObjectLiteral(expression) OBJECT_LITERAL -> declarationsConverter.convertObjectLiteral(expression)
FUN -> declarationsConverter.convertFunctionDeclaration(expression) FUN -> declarationsConverter.convertFunctionDeclaration(expression)
else -> FirErrorExpressionImpl(session, null, errorReason) else -> FirErrorExpressionImpl(null, errorReason)
} }
} }
return FirExpressionStub(session, null) return FirExpressionStub(null)
} }
/** /**
@@ -123,7 +123,7 @@ class ExpressionsConverter(
valueParameters += if (multiDeclaration != null) { valueParameters += if (multiDeclaration != null) {
val multiParameter = FirValueParameterImpl( val multiParameter = FirValueParameterImpl(
this@ExpressionsConverter.session, null, Name.special("<destruct>"), this@ExpressionsConverter.session, null, Name.special("<destruct>"),
FirImplicitTypeRefImpl(this@ExpressionsConverter.session, null), FirImplicitTypeRefImpl(null),
defaultValue = null, isCrossinline = false, isNoinline = false, isVararg = false defaultValue = null, isCrossinline = false, isNoinline = false, isVararg = false
) )
destructuringBlock = generateDestructuringBlock( destructuringBlock = generateDestructuringBlock(
@@ -138,13 +138,13 @@ class ExpressionsConverter(
} }
} }
label = context.firLabels.pop() ?: context.firFunctionCalls.lastOrNull()?.calleeReference?.name?.let { label = context.firLabels.pop() ?: context.firFunctionCalls.lastOrNull()?.calleeReference?.name?.let {
FirLabelImpl(this@ExpressionsConverter.session, null, it.asString()) FirLabelImpl(null, it.asString())
} }
val bodyExpression = block?.let { declarationsConverter.convertBlockExpression(it) } val bodyExpression = block?.let { declarationsConverter.convertBlockExpression(it) }
?: FirErrorExpressionImpl(this@ExpressionsConverter.session, null, "Lambda has no body") ?: FirErrorExpressionImpl(null, "Lambda has no body")
body = if (bodyExpression is FirBlockImpl) { body = if (bodyExpression is FirBlockImpl) {
if (bodyExpression.statements.isEmpty()) { if (bodyExpression.statements.isEmpty()) {
bodyExpression.statements.add(FirUnitExpression(this@ExpressionsConverter.session, null)) bodyExpression.statements.add(FirUnitExpression(null))
} }
if (destructuringBlock is FirBlock) { if (destructuringBlock is FirBlock) {
for ((index, statement) in destructuringBlock.statements.withIndex()) { for ((index, statement) in destructuringBlock.statements.withIndex()) {
@@ -153,7 +153,7 @@ class ExpressionsConverter(
} }
bodyExpression bodyExpression
} else { } else {
FirSingleExpressionBlock(this@ExpressionsConverter.session, bodyExpression.toReturn()) FirSingleExpressionBlock(bodyExpression.toReturn())
} }
context.firFunctions.removeLast() context.firFunctions.removeLast()
@@ -168,7 +168,7 @@ class ExpressionsConverter(
var isLeftArgument = true var isLeftArgument = true
lateinit var operationTokenName: String lateinit var operationTokenName: String
var leftArgNode: LighterASTNode? = null var leftArgNode: LighterASTNode? = null
var rightArgAsFir: FirExpression = FirErrorExpressionImpl(session, null, "No right operand") var rightArgAsFir: FirExpression = FirErrorExpressionImpl(null, "No right operand")
binaryExpression.forEachChildren { binaryExpression.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
OPERATION_REFERENCE -> { OPERATION_REFERENCE -> {
@@ -193,19 +193,18 @@ class ExpressionsConverter(
) )
ANDAND, OROR -> ANDAND, OROR ->
return getAsFirExpression<FirExpression>(leftArgNode, "No left operand").generateLazyLogicalOperation( return getAsFirExpression<FirExpression>(leftArgNode, "No left operand").generateLazyLogicalOperation(
session, rightArgAsFir, operationToken == ANDAND, null rightArgAsFir, operationToken == ANDAND, null
) )
in OperatorConventions.IN_OPERATIONS -> in OperatorConventions.IN_OPERATIONS ->
return rightArgAsFir.generateContainsOperation( return rightArgAsFir.generateContainsOperation(
session, getAsFirExpression(leftArgNode, "No left operand"), operationToken == NOT_IN, null, null getAsFirExpression(leftArgNode, "No left operand"), operationToken == NOT_IN, null, null
) )
} }
val conventionCallName = operationToken.toBinaryName() val conventionCallName = operationToken.toBinaryName()
return if (conventionCallName != null || operationToken == IDENTIFIER) { return if (conventionCallName != null || operationToken == IDENTIFIER) {
FirFunctionCallImpl(session, null).apply { FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference( calleeReference = FirSimpleNamedReference(
this@ExpressionsConverter.session, null, null, conventionCallName ?: operationTokenName.nameAsSafeName()
conventionCallName ?: operationTokenName.nameAsSafeName()
) )
explicitReceiver = getAsFirExpression(leftArgNode, "No left operand") explicitReceiver = getAsFirExpression(leftArgNode, "No left operand")
arguments += rightArgAsFir arguments += rightArgAsFir
@@ -215,7 +214,7 @@ class ExpressionsConverter(
if (firOperation in FirOperation.ASSIGNMENTS) { if (firOperation in FirOperation.ASSIGNMENTS) {
return leftArgNode.generateAssignment(null, rightArgAsFir, firOperation) { getAsFirExpression(this) } return leftArgNode.generateAssignment(null, rightArgAsFir, firOperation) { getAsFirExpression(this) }
} else { } else {
FirOperatorCallImpl(session, null, firOperation).apply { FirOperatorCallImpl(null, firOperation).apply {
arguments += getAsFirExpression<FirExpression>(leftArgNode, "No left operand") arguments += getAsFirExpression<FirExpression>(leftArgNode, "No left operand")
arguments += rightArgAsFir arguments += rightArgAsFir
} }
@@ -233,7 +232,7 @@ class ExpressionsConverter(
toFirOperation: String.() -> FirOperation toFirOperation: String.() -> FirOperation
): FirTypeOperatorCall { ): FirTypeOperatorCall {
lateinit var operationTokenName: String lateinit var operationTokenName: String
var leftArgAsFir: FirExpression = FirErrorExpressionImpl(session, null, "No left operand") var leftArgAsFir: FirExpression = FirErrorExpressionImpl(null, "No left operand")
lateinit var firType: FirTypeRef lateinit var firType: FirTypeRef
binaryExpression.forEachChildren { binaryExpression.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -244,7 +243,7 @@ class ExpressionsConverter(
} }
val operation = operationTokenName.toFirOperation() val operation = operationTokenName.toFirOperation()
return FirTypeOperatorCallImpl(session, null, operation, firType).apply { return FirTypeOperatorCallImpl(null, operation, firType).apply {
arguments += leftArgAsFir arguments += leftArgAsFir
} }
} }
@@ -258,7 +257,7 @@ class ExpressionsConverter(
var firExpression: FirElement? = null var firExpression: FirElement? = null
labeledExpression.forEachChildren { labeledExpression.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
LABEL_QUALIFIER -> context.firLabels += FirLabelImpl(session, null, it.toString().replace("@", "")) LABEL_QUALIFIER -> context.firLabels += FirLabelImpl(null, it.toString().replace("@", ""))
BLOCK -> firExpression = declarationsConverter.convertBlock(it) BLOCK -> firExpression = declarationsConverter.convertBlock(it)
PROPERTY -> firExpression = declarationsConverter.convertPropertyDeclaration(it) PROPERTY -> firExpression = declarationsConverter.convertPropertyDeclaration(it)
else -> if (it.isExpression()) firExpression = getAsFirExpression(it) else -> if (it.isExpression()) firExpression = getAsFirExpression(it)
@@ -269,7 +268,7 @@ class ExpressionsConverter(
context.firLabels.removeLast() context.firLabels.removeLast()
//println("Unused label: ${labeledExpression.getAsString()}") //println("Unused label: ${labeledExpression.getAsString()}")
} }
return firExpression ?: FirErrorExpressionImpl(session, null, "Empty label") return firExpression ?: FirErrorExpressionImpl(null, "Empty label")
} }
/** /**
@@ -302,13 +301,13 @@ class ExpressionsConverter(
prefix = unaryExpression.tokenType == PREFIX_EXPRESSION prefix = unaryExpression.tokenType == PREFIX_EXPRESSION
) { getAsFirExpression(this) } ) { getAsFirExpression(this) }
} }
FirFunctionCallImpl(session, null).apply { FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, conventionCallName) calleeReference = FirSimpleNamedReference(null, conventionCallName)
explicitReceiver = getAsFirExpression(argument, "No operand") explicitReceiver = getAsFirExpression(argument, "No operand")
} }
} else { } else {
val firOperation = operationToken.toFirOperation() val firOperation = operationToken.toFirOperation()
FirOperatorCallImpl(session, null, firOperation).apply { FirOperatorCallImpl(null, firOperation).apply {
arguments += getAsFirExpression<FirExpression>(argument, "No operand") arguments += getAsFirExpression<FirExpression>(argument, "No operand")
} }
} }
@@ -331,7 +330,7 @@ class ExpressionsConverter(
return (firExpression as? FirAbstractAnnotatedElement)?.apply { return (firExpression as? FirAbstractAnnotatedElement)?.apply {
annotations += firAnnotationList annotations += firAnnotationList
} ?: FirErrorExpressionImpl(session, null, "Strange annotated expression: ${firExpression?.render()}") } ?: FirErrorExpressionImpl(null, "Strange annotated expression: ${firExpression?.render()}")
} }
/** /**
@@ -339,12 +338,12 @@ class ExpressionsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitClassLiteralExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitClassLiteralExpression
*/ */
private fun convertClassLiteralExpression(classLiteralExpression: LighterASTNode): FirExpression { private fun convertClassLiteralExpression(classLiteralExpression: LighterASTNode): FirExpression {
var firReceiverExpression: FirExpression = FirErrorExpressionImpl(session, null, "No receiver in class literal") var firReceiverExpression: FirExpression = FirErrorExpressionImpl(null, "No receiver in class literal")
classLiteralExpression.forEachChildren { classLiteralExpression.forEachChildren {
if (it.isExpression()) firReceiverExpression = getAsFirExpression(it, "No receiver in class literal") if (it.isExpression()) firReceiverExpression = getAsFirExpression(it, "No receiver in class literal")
} }
return FirGetClassCallImpl(session, null).apply { return FirGetClassCallImpl(null).apply {
arguments += firReceiverExpression arguments += firReceiverExpression
} }
} }
@@ -370,7 +369,7 @@ class ExpressionsConverter(
} }
} }
return FirCallableReferenceAccessImpl(session, null).apply { return FirCallableReferenceAccessImpl(null).apply {
calleeReference = firCallableReference.calleeReference calleeReference = firCallableReference.calleeReference
explicitReceiver = firReceiverExpression explicitReceiver = firReceiverExpression
} }
@@ -383,7 +382,7 @@ class ExpressionsConverter(
private fun convertQualifiedExpression(dotQualifiedExpression: LighterASTNode): FirExpression { private fun convertQualifiedExpression(dotQualifiedExpression: LighterASTNode): FirExpression {
var isSelector = false var isSelector = false
var isSafe = false var isSafe = false
var firSelector: FirExpression = FirErrorExpressionImpl(session, null, "Qualified expression without selector") //after dot var firSelector: FirExpression = FirErrorExpressionImpl(null, "Qualified expression without selector") //after dot
var firReceiver: FirExpression? = null //before dot var firReceiver: FirExpression? = null //before dot
dotQualifiedExpression.forEachChildren { dotQualifiedExpression.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -427,14 +426,14 @@ class ExpressionsConverter(
} }
} }
return FirFunctionCallImpl(session, null).apply { return FirFunctionCallImpl(null).apply {
this.calleeReference = when { this.calleeReference = when {
name != null -> FirSimpleNamedReference(this@ExpressionsConverter.session, null, name.nameAsSafeName()) name != null -> FirSimpleNamedReference(null, name.nameAsSafeName())
additionalArgument != null -> { additionalArgument != null -> {
arguments += additionalArgument!! arguments += additionalArgument!!
FirSimpleNamedReference(this@ExpressionsConverter.session, null, OperatorNameConventions.INVOKE) FirSimpleNamedReference(null, OperatorNameConventions.INVOKE)
} }
else -> FirErrorNamedReference(this@ExpressionsConverter.session, null, "Call has no callee") else -> FirErrorNamedReference(null, "Call has no callee")
} }
context.firFunctionCalls += this context.firFunctionCalls += this
@@ -452,7 +451,7 @@ class ExpressionsConverter(
} }
private fun LighterASTNode?.convertShortOrLongStringTemplate(errorReason: String): FirExpression { private fun LighterASTNode?.convertShortOrLongStringTemplate(errorReason: String): FirExpression {
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, errorReason) var firExpression: FirExpression = FirErrorExpressionImpl(null, errorReason)
this?.forEachChildren(LONG_TEMPLATE_ENTRY_START, LONG_TEMPLATE_ENTRY_END) { this?.forEachChildren(LONG_TEMPLATE_ENTRY_START, LONG_TEMPLATE_ENTRY_END) {
firExpression = getAsFirExpression(it, errorReason) firExpression = getAsFirExpression(it, errorReason)
} }
@@ -493,7 +492,7 @@ class ExpressionsConverter(
subjectExpression = subjectVariable?.initializer ?: subjectExpression subjectExpression = subjectVariable?.initializer ?: subjectExpression
val hasSubject = subjectExpression != null val hasSubject = subjectExpression != null
val subject = FirWhenSubject() val subject = FirWhenSubject()
return FirWhenExpressionImpl(session, null, subjectExpression, subjectVariable).apply { return FirWhenExpressionImpl(null, subjectExpression, subjectVariable).apply {
if (hasSubject) { if (hasSubject) {
subject.bind(this) subject.bind(this)
} }
@@ -501,15 +500,15 @@ class ExpressionsConverter(
val branch = entry.firBlock val branch = entry.firBlock
branches += if (!entry.isElse) { branches += if (!entry.isElse) {
if (hasSubject) { if (hasSubject) {
val firCondition = entry.toFirWhenCondition(this@ExpressionsConverter.session, subject) val firCondition = entry.toFirWhenCondition(subject)
FirWhenBranchImpl(this@ExpressionsConverter.session, null, firCondition, branch) FirWhenBranchImpl(null, firCondition, branch)
} else { } else {
val firCondition = entry.toFirWhenConditionWithoutSubject(this@ExpressionsConverter.session) val firCondition = entry.toFirWhenConditionWithoutSubject()
FirWhenBranchImpl(this@ExpressionsConverter.session, null, firCondition, branch) FirWhenBranchImpl(null, firCondition, branch)
} }
} else { } else {
FirWhenBranchImpl( FirWhenBranchImpl(
this@ExpressionsConverter.session, null, FirElseIfTrueCondition(this@ExpressionsConverter.session, null), branch null, FirElseIfTrueCondition(null), branch
) )
} }
} }
@@ -522,7 +521,7 @@ class ExpressionsConverter(
*/ */
private fun convertWhenEntry(whenEntry: LighterASTNode): WhenEntry { private fun convertWhenEntry(whenEntry: LighterASTNode): WhenEntry {
var isElse = false var isElse = false
var firBlock: FirBlock = FirEmptyExpressionBlock(session) var firBlock: FirBlock = FirEmptyExpressionBlock()
val conditions = mutableListOf<FirExpression>() val conditions = mutableListOf<FirExpression>()
whenEntry.forEachChildren { whenEntry.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -539,21 +538,21 @@ class ExpressionsConverter(
} }
private fun convertWhenConditionExpression(whenCondition: LighterASTNode): FirExpression { private fun convertWhenConditionExpression(whenCondition: LighterASTNode): FirExpression {
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "No expression in condition with expression") var firExpression: FirExpression = FirErrorExpressionImpl(null, "No expression in condition with expression")
whenCondition.forEachChildren { whenCondition.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
else -> if (it.isExpression()) firExpression = getAsFirExpression(it, "No expression in condition with expression") else -> if (it.isExpression()) firExpression = getAsFirExpression(it, "No expression in condition with expression")
} }
} }
return FirOperatorCallImpl(session, null, FirOperation.EQ).apply { return FirOperatorCallImpl(null, FirOperation.EQ).apply {
arguments += firExpression arguments += firExpression
} }
} }
private fun convertWhenConditionInRange(whenCondition: LighterASTNode): FirExpression { private fun convertWhenConditionInRange(whenCondition: LighterASTNode): FirExpression {
var isNegate = false var isNegate = false
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "No range in condition with range") var firExpression: FirExpression = FirErrorExpressionImpl(null, "No range in condition with range")
whenCondition.forEachChildren { whenCondition.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
NOT_IN -> isNegate = true NOT_IN -> isNegate = true
@@ -562,8 +561,8 @@ class ExpressionsConverter(
} }
val name = if (isNegate) OperatorNameConventions.NOT else SpecialNames.NO_NAME_PROVIDED val name = if (isNegate) OperatorNameConventions.NOT else SpecialNames.NO_NAME_PROVIDED
return FirFunctionCallImpl(session, null).apply { return FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, name) calleeReference = FirSimpleNamedReference(null, name)
explicitReceiver = firExpression explicitReceiver = firExpression
} }
} }
@@ -579,7 +578,7 @@ class ExpressionsConverter(
} }
} }
return FirTypeOperatorCallImpl(session, null, firOperation, firType) return FirTypeOperatorCallImpl(null, firOperation, firType)
} }
/** /**
@@ -587,7 +586,7 @@ class ExpressionsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitArrayAccessExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitArrayAccessExpression
*/ */
private fun convertArrayAccessExpression(arrayAccess: LighterASTNode): FirFunctionCall { private fun convertArrayAccessExpression(arrayAccess: LighterASTNode): FirFunctionCall {
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "No array expression") var firExpression: FirExpression = FirErrorExpressionImpl(null, "No array expression")
val indices: MutableList<FirExpression> = mutableListOf() val indices: MutableList<FirExpression> = mutableListOf()
arrayAccess.forEachChildren { arrayAccess.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -595,8 +594,8 @@ class ExpressionsConverter(
else -> if (it.isExpression()) firExpression = getAsFirExpression(it, "No array expression") else -> if (it.isExpression()) firExpression = getAsFirExpression(it, "No array expression")
} }
} }
return FirFunctionCallImpl(session, null).apply { return FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, OperatorNameConventions.GET) calleeReference = FirSimpleNamedReference(null, OperatorNameConventions.GET)
explicitReceiver = firExpression explicitReceiver = firExpression
arguments += indices arguments += indices
} }
@@ -611,7 +610,7 @@ class ExpressionsConverter(
if (it.isExpression()) firExpressionList += getAsFirExpression<FirExpression>(it, "Incorrect collection literal argument") if (it.isExpression()) firExpressionList += getAsFirExpression<FirExpression>(it, "Incorrect collection literal argument")
} }
return FirArrayOfCallImpl(session, null).apply { return FirArrayOfCallImpl(null).apply {
arguments += firExpressionList arguments += firExpressionList
} }
} }
@@ -633,9 +632,9 @@ class ExpressionsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitSimpleNameExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitSimpleNameExpression
*/ */
private fun convertSimpleNameExpression(referenceExpression: LighterASTNode): FirQualifiedAccessExpression { private fun convertSimpleNameExpression(referenceExpression: LighterASTNode): FirQualifiedAccessExpression {
return FirQualifiedAccessExpressionImpl(session, null).apply { return FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = calleeReference =
FirSimpleNamedReference(this@ExpressionsConverter.session, null, referenceExpression.asText.nameAsSafeName()) FirSimpleNamedReference(null, referenceExpression.asText.nameAsSafeName())
} }
} }
@@ -645,7 +644,7 @@ class ExpressionsConverter(
*/ */
private fun convertDoWhile(doWhileLoop: LighterASTNode): FirElement { private fun convertDoWhile(doWhileLoop: LighterASTNode): FirElement {
var block: LighterASTNode? = null var block: LighterASTNode? = null
var firCondition: FirExpression = FirErrorExpressionImpl(session, null, "No condition in do-while loop") var firCondition: FirExpression = FirErrorExpressionImpl(null, "No condition in do-while loop")
doWhileLoop.forEachChildren { doWhileLoop.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
BODY -> block = it BODY -> block = it
@@ -653,7 +652,7 @@ class ExpressionsConverter(
} }
} }
return FirDoWhileLoopImpl(session, null, firCondition).configure { convertLoopBody(block) } return FirDoWhileLoopImpl(null, firCondition).configure { convertLoopBody(block) }
} }
/** /**
@@ -662,7 +661,7 @@ class ExpressionsConverter(
*/ */
private fun convertWhile(whileLoop: LighterASTNode): FirElement { private fun convertWhile(whileLoop: LighterASTNode): FirElement {
var block: LighterASTNode? = null var block: LighterASTNode? = null
var firCondition: FirExpression = FirErrorExpressionImpl(session, null, "No condition in while loop") var firCondition: FirExpression = FirErrorExpressionImpl(null, "No condition in while loop")
whileLoop.forEachChildren { whileLoop.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
BODY -> block = it BODY -> block = it
@@ -670,7 +669,7 @@ class ExpressionsConverter(
} }
} }
return FirWhileLoopImpl(session, null, firCondition).configure { convertLoopBody(block) } return FirWhileLoopImpl(null, firCondition).configure { convertLoopBody(block) }
} }
/** /**
@@ -679,7 +678,7 @@ class ExpressionsConverter(
*/ */
private fun convertFor(forLoop: LighterASTNode): FirElement { private fun convertFor(forLoop: LighterASTNode): FirElement {
var parameter: ValueParameter? = null var parameter: ValueParameter? = null
var rangeExpression: FirExpression = FirErrorExpressionImpl(session, null, "No range in for loop") var rangeExpression: FirExpression = FirErrorExpressionImpl(null, "No range in for loop")
var blockNode: LighterASTNode? = null var blockNode: LighterASTNode? = null
forLoop.forEachChildren { forLoop.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
@@ -689,28 +688,27 @@ class ExpressionsConverter(
} }
} }
//TODO psi must be null return FirBlockImpl(null).apply {
return FirBlockImpl(session, null).apply {
val rangeVal = val rangeVal =
generateTemporaryVariable(this@ExpressionsConverter.session, null, Name.special("<range>"), rangeExpression) generateTemporaryVariable(this@ExpressionsConverter.session, null, Name.special("<range>"), rangeExpression)
statements += rangeVal statements += rangeVal
val iteratorVal = generateTemporaryVariable( val iteratorVal = generateTemporaryVariable(
this@ExpressionsConverter.session, null, Name.special("<iterator>"), this@ExpressionsConverter.session, null, Name.special("<iterator>"),
FirFunctionCallImpl(this@ExpressionsConverter.session, null).apply { FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, Name.identifier("iterator")) calleeReference = FirSimpleNamedReference(null, Name.identifier("iterator"))
explicitReceiver = generateResolvedAccessExpression(this@ExpressionsConverter.session, null, rangeVal) explicitReceiver = generateResolvedAccessExpression(null, rangeVal)
} }
) )
statements += iteratorVal statements += iteratorVal
statements += FirWhileLoopImpl( statements += FirWhileLoopImpl(
this@ExpressionsConverter.session, null, null,
FirFunctionCallImpl(this@ExpressionsConverter.session, null).apply { FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, Name.identifier("hasNext")) calleeReference = FirSimpleNamedReference(null, Name.identifier("hasNext"))
explicitReceiver = generateResolvedAccessExpression(this@ExpressionsConverter.session, null, iteratorVal) explicitReceiver = generateResolvedAccessExpression(null, iteratorVal)
} }
).configure { ).configure {
// NB: just body.toFirBlock() isn't acceptable here because we need to add some statements // NB: just body.toFirBlock() isn't acceptable here because we need to add some statements
val block = FirBlockImpl(this@ExpressionsConverter.session, null).apply { val block = FirBlockImpl(null).apply {
statements += convertLoopBody(blockNode).statements statements += convertLoopBody(blockNode).statements
} }
if (parameter != null) { if (parameter != null) {
@@ -718,9 +716,9 @@ class ExpressionsConverter(
val firLoopParameter = generateTemporaryVariable( val firLoopParameter = generateTemporaryVariable(
this@ExpressionsConverter.session, null, this@ExpressionsConverter.session, null,
if (multiDeclaration != null) Name.special("<destruct>") else parameter!!.firValueParameter.name, if (multiDeclaration != null) Name.special("<destruct>") else parameter!!.firValueParameter.name,
FirFunctionCallImpl(this@ExpressionsConverter.session, null).apply { FirFunctionCallImpl(null).apply {
calleeReference = FirSimpleNamedReference(this@ExpressionsConverter.session, null, Name.identifier("next")) calleeReference = FirSimpleNamedReference(null, Name.identifier("next"))
explicitReceiver = generateResolvedAccessExpression(this@ExpressionsConverter.session, null, iteratorVal) explicitReceiver = generateResolvedAccessExpression(null, iteratorVal)
} }
) )
if (multiDeclaration != null) { if (multiDeclaration != null) {
@@ -759,8 +757,8 @@ class ExpressionsConverter(
} }
return when { return when {
firStatement != null -> FirSingleExpressionBlock(session, firStatement!!) firStatement != null -> FirSingleExpressionBlock(firStatement!!)
firBlock == null -> FirEmptyExpressionBlock(session) firBlock == null -> FirEmptyExpressionBlock()
else -> firBlock!! else -> firBlock!!
} }
} }
@@ -780,10 +778,10 @@ class ExpressionsConverter(
FINALLY -> finallyBlock = convertFinally(it) FINALLY -> finallyBlock = convertFinally(it)
} }
} }
return FirTryExpressionImpl(session, null, tryBlock, finallyBlock).apply { return FirTryExpressionImpl(null, tryBlock, finallyBlock).apply {
for ((parameter, block) in catchClauses) { for ((parameter, block) in catchClauses) {
if (parameter == null) continue if (parameter == null) continue
catches += FirCatchImpl(this@ExpressionsConverter.session, null, parameter.firValueParameter, block) catches += FirCatchImpl(null, parameter.firValueParameter, block)
} }
} }
} }
@@ -823,7 +821,7 @@ class ExpressionsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitIfExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitIfExpression
*/ */
private fun convertIfExpression(ifExpression: LighterASTNode): FirExpression { private fun convertIfExpression(ifExpression: LighterASTNode): FirExpression {
var firCondition: FirExpression = FirErrorExpressionImpl(session, null, "If statement should have condition") var firCondition: FirExpression = FirErrorExpressionImpl(null, "If statement should have condition")
var thenBlock: LighterASTNode? = null var thenBlock: LighterASTNode? = null
var elseBlock: LighterASTNode? = null var elseBlock: LighterASTNode? = null
ifExpression.forEachChildren { ifExpression.forEachChildren {
@@ -834,12 +832,12 @@ class ExpressionsConverter(
} }
} }
return FirWhenExpressionImpl(session, null).apply { return FirWhenExpressionImpl(null).apply {
val trueBranch = convertLoopBody(thenBlock) val trueBranch = convertLoopBody(thenBlock)
branches += FirWhenBranchImpl(this@ExpressionsConverter.session, null, firCondition, trueBranch) branches += FirWhenBranchImpl(null, firCondition, trueBranch)
val elseBranch = convertLoopBody(elseBlock) val elseBranch = convertLoopBody(elseBlock)
branches += FirWhenBranchImpl( branches += FirWhenBranchImpl(
this@ExpressionsConverter.session, null, FirElseIfTrueCondition(this@ExpressionsConverter.session, null), elseBranch null, FirElseIfTrueCondition(null), elseBranch
) )
} }
} }
@@ -858,7 +856,7 @@ class ExpressionsConverter(
} }
} }
return (if (isBreak) FirBreakExpressionImpl(session, null) else FirContinueExpressionImpl(session, null)).bindLabel(jump) return (if (isBreak) FirBreakExpressionImpl(null) else FirContinueExpressionImpl(null)).bindLabel(jump)
} }
/** /**
@@ -867,7 +865,7 @@ class ExpressionsConverter(
*/ */
private fun convertReturn(returnExpression: LighterASTNode): FirExpression { private fun convertReturn(returnExpression: LighterASTNode): FirExpression {
var labelName: String? = null var labelName: String? = null
var firExpression: FirExpression = FirUnitExpression(session, null) var firExpression: FirExpression = FirUnitExpression(null)
returnExpression.forEachChildren { returnExpression.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
LABEL_QUALIFIER -> labelName = it.getAsStringWithoutBacktick().replace("@", "") LABEL_QUALIFIER -> labelName = it.getAsStringWithoutBacktick().replace("@", "")
@@ -883,12 +881,12 @@ class ExpressionsConverter(
* @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitThrowExpression * @see org.jetbrains.kotlin.fir.builder.RawFirBuilder.Visitor.visitThrowExpression
*/ */
private fun convertThrow(throwExpression: LighterASTNode): FirExpression { private fun convertThrow(throwExpression: LighterASTNode): FirExpression {
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "Nothing to throw") var firExpression: FirExpression = FirErrorExpressionImpl(null, "Nothing to throw")
throwExpression.forEachChildren { throwExpression.forEachChildren {
if (it.isExpression()) firExpression = getAsFirExpression(it, "Nothing to throw") if (it.isExpression()) firExpression = getAsFirExpression(it, "Nothing to throw")
} }
return FirThrowExpressionImpl(session, null, firExpression) return FirThrowExpressionImpl(null, firExpression)
} }
/** /**
@@ -898,8 +896,8 @@ class ExpressionsConverter(
private fun convertThisExpression(thisExpression: LighterASTNode): FirQualifiedAccessExpression { private fun convertThisExpression(thisExpression: LighterASTNode): FirQualifiedAccessExpression {
val label: String? = thisExpression.getLabelName() val label: String? = thisExpression.getLabelName()
return FirQualifiedAccessExpressionImpl(session, null).apply { return FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirExplicitThisReference(this@ExpressionsConverter.session, null, label) calleeReference = FirExplicitThisReference(null, label)
} }
} }
@@ -915,8 +913,8 @@ class ExpressionsConverter(
} }
} }
return FirQualifiedAccessExpressionImpl(session, null).apply { return FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirExplicitSuperReference(this@ExpressionsConverter.session, null, superTypeRef) calleeReference = FirExplicitSuperReference(null, superTypeRef)
} }
} }
@@ -929,7 +927,7 @@ class ExpressionsConverter(
VALUE_ARGUMENT -> container += convertValueArgument(node) VALUE_ARGUMENT -> container += convertValueArgument(node)
LAMBDA_EXPRESSION, LAMBDA_EXPRESSION,
LABELED_EXPRESSION, LABELED_EXPRESSION,
ANNOTATED_EXPRESSION -> container += FirLambdaArgumentExpressionImpl(session, null, getAsFirExpression(node)) ANNOTATED_EXPRESSION -> container += FirLambdaArgumentExpressionImpl(null, getAsFirExpression(node))
} }
} }
} }
@@ -941,7 +939,7 @@ class ExpressionsConverter(
private fun convertValueArgument(valueArgument: LighterASTNode): FirExpression { private fun convertValueArgument(valueArgument: LighterASTNode): FirExpression {
var identifier: String? = null var identifier: String? = null
var isSpread = false var isSpread = false
var firExpression: FirExpression = FirErrorExpressionImpl(session, null, "Argument is absent") var firExpression: FirExpression = FirErrorExpressionImpl(null, "Argument is absent")
valueArgument.forEachChildren { valueArgument.forEachChildren {
when (it.tokenType) { when (it.tokenType) {
VALUE_ARGUMENT_NAME -> identifier = it.asText VALUE_ARGUMENT_NAME -> identifier = it.asText
@@ -952,8 +950,8 @@ class ExpressionsConverter(
} }
} }
return when { return when {
identifier != null -> FirNamedArgumentExpressionImpl(session, null, identifier.nameAsSafeName(), isSpread, firExpression) identifier != null -> FirNamedArgumentExpressionImpl(null, identifier.nameAsSafeName(), isSpread, firExpression)
isSpread -> FirSpreadArgumentExpressionImpl(session, null, firExpression) isSpread -> FirSpreadArgumentExpressionImpl(null, firExpression)
else -> firExpression else -> firExpression
} }
} }
@@ -1,20 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.fir.lightTree.fir
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement
class AnnotationContainer(
session: FirSession,
psi: PsiElement? = null
) : FirAbstractAnnotatedElement(session, psi) {
constructor(session: FirSession, annotations: List<FirAnnotationCall>) : this(session, null) {
super.annotations += annotations
}
}
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier import org.jetbrains.kotlin.fir.lightTree.fir.modifier.Modifier
import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef
@@ -20,7 +19,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.name.SpecialNames
class ClassWrapper( class ClassWrapper(
private val session: FirSession,
private val className: Name, private val className: Name,
private val modifiers: Modifier, private val modifiers: Modifier,
private val classKind: ClassKind, private val classKind: ClassKind,
@@ -63,7 +61,6 @@ class ClassWrapper(
) )
return FirUserTypeRefImpl( return FirUserTypeRefImpl(
session,
null, null,
false false
).apply { ).apply {
@@ -7,10 +7,8 @@ package org.jetbrains.kotlin.fir.lightTree.fir
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.builder.generateTemporaryVariable import org.jetbrains.kotlin.fir.builder.generateTemporaryVariable
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.expressions.FirVariable import org.jetbrains.kotlin.fir.expressions.FirVariable
import org.jetbrains.kotlin.fir.expressions.impl.FirErrorExpressionImpl
import org.jetbrains.kotlin.fir.lightTree.converter.generateDestructuringBlock import org.jetbrains.kotlin.fir.lightTree.converter.generateDestructuringBlock
data class DestructuringDeclaration( data class DestructuringDeclaration(
@@ -37,7 +37,7 @@ class ValueParameter(
val name = this.firValueParameter.name val name = this.firValueParameter.name
var type = this.firValueParameter.returnTypeRef var type = this.firValueParameter.returnTypeRef
if (type is FirImplicitTypeRef) { if (type is FirImplicitTypeRef) {
type = FirErrorTypeRefImpl(session, null, "Incomplete code") type = FirErrorTypeRefImpl(null, "Incomplete code")
} }
return FirMemberPropertyImpl( return FirMemberPropertyImpl(
@@ -55,19 +55,16 @@ class ValueParameter(
receiverTypeRef = null, receiverTypeRef = null,
returnTypeRef = type, returnTypeRef = type,
isVar = this.isVar, isVar = this.isVar,
initializer = FirQualifiedAccessExpressionImpl(session, null).apply { initializer = FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirPropertyFromParameterCallableReference( calleeReference = FirPropertyFromParameterCallableReference(
session, null, name, this@ValueParameter.firValueParameter.symbol null, name, this@ValueParameter.firValueParameter.symbol
) )
}, },
getter = FirDefaultPropertyGetter(session, null, type, modifiers.getVisibility()),
setter = if (this.isVar) FirDefaultPropertySetter(
session,
null,
type,
modifiers.getVisibility()
) else null,
delegate = null delegate = null
).apply { annotations += this@ValueParameter.firValueParameter.annotations } ).apply {
annotations += this@ValueParameter.firValueParameter.annotations
getter = FirDefaultPropertyGetter(session, null, type, modifiers.getVisibility())
setter = if (this.isVar) FirDefaultPropertySetter(session, null, type, modifiers.getVisibility()) else null
}
} }
} }
@@ -5,10 +5,6 @@
package org.jetbrains.kotlin.fir.lightTree.fir package org.jetbrains.kotlin.fir.lightTree.fir
import com.intellij.lang.LighterASTNode
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirWhenSubject import org.jetbrains.kotlin.fir.FirWhenSubject
import org.jetbrains.kotlin.fir.builder.generateContainsOperation import org.jetbrains.kotlin.fir.builder.generateContainsOperation
import org.jetbrains.kotlin.fir.builder.generateLazyLogicalOperation import org.jetbrains.kotlin.fir.builder.generateLazyLogicalOperation
@@ -24,20 +20,20 @@ data class WhenEntry(
val firBlock: FirBlock, val firBlock: FirBlock,
val isElse: Boolean = false val isElse: Boolean = false
) { ) {
fun toFirWhenCondition(session: FirSession, subject: FirWhenSubject): FirExpression { fun toFirWhenCondition(subject: FirWhenSubject): FirExpression {
var firCondition: FirExpression? = null var firCondition: FirExpression? = null
for (condition in conditions) { for (condition in conditions) {
val firConditionElement = condition.toFirWhenCondition(session, subject) val firConditionElement = condition.toFirWhenCondition(subject)
firCondition = when (firCondition) { firCondition = when (firCondition) {
null -> firConditionElement null -> firConditionElement
else -> firCondition.generateLazyLogicalOperation(session, firConditionElement, false, null) else -> firCondition.generateLazyLogicalOperation(firConditionElement, false, null)
} }
} }
return firCondition!! return firCondition!!
} }
private fun FirExpression.toFirWhenCondition(session: FirSession, subject: FirWhenSubject): FirExpression { private fun FirExpression.toFirWhenCondition(subject: FirWhenSubject): FirExpression {
val firSubjectExpression = FirWhenSubjectExpressionImpl(session, null, subject) val firSubjectExpression = FirWhenSubjectExpressionImpl(null, subject)
return when (this) { return when (this) {
is FirOperatorCallImpl -> { is FirOperatorCallImpl -> {
this.apply { this.apply {
@@ -47,7 +43,7 @@ data class WhenEntry(
is FirFunctionCall -> { is FirFunctionCall -> {
val firExpression = this.explicitReceiver!! val firExpression = this.explicitReceiver!!
val isNegate = this.calleeReference.name == OperatorNameConventions.NOT val isNegate = this.calleeReference.name == OperatorNameConventions.NOT
firExpression.generateContainsOperation(session, firSubjectExpression, isNegate, null, null) firExpression.generateContainsOperation(firSubjectExpression, isNegate, null, null)
} }
is FirTypeOperatorCallImpl -> { is FirTypeOperatorCallImpl -> {
this.apply { this.apply {
@@ -55,18 +51,15 @@ data class WhenEntry(
} }
} }
else -> { else -> {
FirErrorExpressionImpl(session, null, "Unsupported when condition: ${this.javaClass}") FirErrorExpressionImpl(null, "Unsupported when condition: ${this.javaClass}")
} }
} }
} }
fun toFirWhenConditionWithoutSubject(session: FirSession): FirExpression { fun toFirWhenConditionWithoutSubject(): FirExpression {
val condition = conditions.first() return when (val condition = conditions.first()) {
return when (condition) {
is FirOperatorCallImpl -> condition.arguments.first() is FirOperatorCallImpl -> condition.arguments.first()
//is FirFunctionCall -> condition.explicitReceiver!! else -> FirErrorExpressionImpl(null, "No expression in condition with expression")
//is FirTypeOperatorCallImpl -> condition
else -> FirErrorExpressionImpl(session, null, "No expression in condition with expression")
} }
} }
} }
@@ -10,7 +10,6 @@ import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.CLASS_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.CLASS_MODIFIER
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.FUNCTION_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.FUNCTION_MODIFIER
@@ -22,9 +21,7 @@ import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.PROPERTY_MOD
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VISIBILITY_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VISIBILITY_MODIFIER
class Modifier( class Modifier(
session: FirSession,
psi: PsiElement? = null, psi: PsiElement? = null,
private val classModifiers: MutableList<ClassModifier> = mutableListOf(), private val classModifiers: MutableList<ClassModifier> = mutableListOf(),
private val memberModifiers: MutableList<MemberModifier> = mutableListOf(), private val memberModifiers: MutableList<MemberModifier> = mutableListOf(),
private val visibilityModifiers: MutableList<VisibilityModifier> = mutableListOf(), private val visibilityModifiers: MutableList<VisibilityModifier> = mutableListOf(),
@@ -33,7 +30,7 @@ class Modifier(
private val inheritanceModifiers: MutableList<InheritanceModifier> = mutableListOf(), private val inheritanceModifiers: MutableList<InheritanceModifier> = mutableListOf(),
private val parameterModifiers: MutableList<ParameterModifier> = mutableListOf(), private val parameterModifiers: MutableList<ParameterModifier> = mutableListOf(),
private val platformModifiers: MutableList<PlatformModifier> = mutableListOf() private val platformModifiers: MutableList<PlatformModifier> = mutableListOf()
) : FirAbstractAnnotatedElement(session, psi) { ) : FirAbstractAnnotatedElement(psi) {
fun addModifier(modifier: LighterASTNode) { fun addModifier(modifier: LighterASTNode) {
val tokenType = modifier.tokenType val tokenType = modifier.tokenType
when { when {
@@ -7,16 +7,13 @@ package org.jetbrains.kotlin.fir.lightTree.fir.modifier
import com.intellij.lang.LighterASTNode import com.intellij.lang.LighterASTNode
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
class TypeModifier( class TypeModifier(
session: FirSession,
psi: PsiElement? = null, psi: PsiElement? = null,
var hasSuspend: Boolean = false var hasSuspend: Boolean = false
) : FirAbstractAnnotatedElement(session, psi) { ) : FirAbstractAnnotatedElement(psi) {
fun addModifier(modifier: LighterASTNode) { fun addModifier(modifier: LighterASTNode) {
when (modifier.tokenType) { when (modifier.tokenType) {
KtTokens.SUSPEND_KEYWORD -> this.hasSuspend = true KtTokens.SUSPEND_KEYWORD -> this.hasSuspend = true
@@ -7,19 +7,17 @@ package org.jetbrains.kotlin.fir.lightTree.fir.modifier
import com.intellij.lang.LighterASTNode import com.intellij.lang.LighterASTNode
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.REIFICATION_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.REIFICATION_MODIFIER
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VARIANCE_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VARIANCE_MODIFIER
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
class TypeParameterModifier( class TypeParameterModifier(
session: FirSession,
psi: PsiElement? = null, psi: PsiElement? = null,
private val varianceModifiers: MutableList<VarianceModifier> = mutableListOf(), private val varianceModifiers: MutableList<VarianceModifier> = mutableListOf(),
private var reificationModifier: ReificationModifier? = null private var reificationModifier: ReificationModifier? = null
) : FirAbstractAnnotatedElement(session, psi) { ) : FirAbstractAnnotatedElement(psi) {
fun addModifier(modifier: LighterASTNode) { fun addModifier(modifier: LighterASTNode) {
val tokenType = modifier.tokenType val tokenType = modifier.tokenType
when { when {
@@ -7,17 +7,14 @@ package org.jetbrains.kotlin.fir.lightTree.fir.modifier
import com.intellij.lang.LighterASTNode import com.intellij.lang.LighterASTNode
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement import org.jetbrains.kotlin.fir.expressions.impl.FirAbstractAnnotatedElement
import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VARIANCE_MODIFIER import org.jetbrains.kotlin.fir.lightTree.fir.modifier.ModifierSets.VARIANCE_MODIFIER
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
class TypeProjectionModifier( class TypeProjectionModifier(
session: FirSession,
psi: PsiElement? = null, psi: PsiElement? = null,
private val varianceModifiers: MutableList<VarianceModifier> = mutableListOf() private val varianceModifiers: MutableList<VarianceModifier> = mutableListOf()
) : FirAbstractAnnotatedElement(session, psi) { ) : FirAbstractAnnotatedElement(psi) {
fun addModifier(modifier: LighterASTNode) { fun addModifier(modifier: LighterASTNode) {
val tokenType = modifier.tokenType val tokenType = modifier.tokenType
when { when {
@@ -46,10 +46,10 @@ import org.jetbrains.kotlin.util.OperatorNameConventions
//T can be either PsiElement, or LighterASTNode //T can be either PsiElement, or LighterASTNode
abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context = Context()) { abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context = Context()) {
protected val implicitUnitType = FirImplicitUnitTypeRef(session, null) protected val implicitUnitType = FirImplicitUnitTypeRef(null)
protected val implicitAnyType = FirImplicitAnyTypeRef(session, null) protected val implicitAnyType = FirImplicitAnyTypeRef(null)
protected val implicitEnumType = FirImplicitEnumTypeRef(session, null) protected val implicitEnumType = FirImplicitEnumTypeRef(null)
protected val implicitAnnotationType = FirImplicitAnnotationTypeRef(session, null) protected val implicitAnnotationType = FirImplicitAnnotationTypeRef(null)
abstract val T.elementType: IElementType abstract val T.elementType: IElementType
abstract val T.asText: String abstract val T.asText: String
@@ -100,7 +100,6 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
fun FirExpression.toReturn(basePsi: PsiElement? = psi, labelName: String? = null): FirReturnExpression { fun FirExpression.toReturn(basePsi: PsiElement? = psi, labelName: String? = null): FirReturnExpression {
return FirReturnExpressionImpl( return FirReturnExpressionImpl(
this@BaseFirBuilder.session,
basePsi, basePsi,
this this
).apply { ).apply {
@@ -138,10 +137,10 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
val typeParameters = firClass.typeParameters.map { val typeParameters = firClass.typeParameters.map {
FirTypeParameterImpl(session, it.psi, FirTypeParameterSymbol(), it.name, Variance.INVARIANT, false).apply { FirTypeParameterImpl(session, it.psi, FirTypeParameterSymbol(), it.name, Variance.INVARIANT, false).apply {
this.bounds += it.bounds this.bounds += it.bounds
addDefaultBoundIfNecessary()
} }
} }
return FirResolvedTypeRefImpl( return FirResolvedTypeRefImpl(
session,
this, this,
ConeClassTypeImpl( ConeClassTypeImpl(
firClass.symbol.toLookupTag(), firClass.symbol.toLookupTag(),
@@ -162,8 +161,8 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
return this.convert("No operand").generateNotNullOrOther( return this.convert("No operand").generateNotNullOrOther(
session, session,
FirThrowExpressionImpl( FirThrowExpressionImpl(
session, parent.getPsiOrNull(), FirFunctionCallImpl(session, parent.getPsiOrNull()).apply { parent.getPsiOrNull(), FirFunctionCallImpl(parent.getPsiOrNull()).apply {
calleeReference = FirSimpleNamedReference(this@BaseFirBuilder.session, parent, KNPE) calleeReference = FirSimpleNamedReference(parent, KNPE)
} }
), "bangbang", parent ), "bangbang", parent
) )
@@ -185,7 +184,7 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
if (lastLoop != null) { if (lastLoop != null) {
target.bind(lastLoop) target.bind(lastLoop)
} else { } else {
target.bind(FirErrorLoop(this@BaseFirBuilder.session, expression.getPsiOrNull(), "Cannot bind unlabeled jump to a loop")) target.bind(FirErrorLoop(expression.getPsiOrNull(), "Cannot bind unlabeled jump to a loop"))
} }
} else { } else {
for (firLoop in context.firLoops.asReversed()) { for (firLoop in context.firLoops.asReversed()) {
@@ -194,7 +193,7 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
return this return this
} }
} }
target.bind(FirErrorLoop(this@BaseFirBuilder.session, expression.getPsiOrNull(), "Cannot bind label $labelName to a loop")) target.bind(FirErrorLoop(expression.getPsiOrNull(), "Cannot bind label $labelName to a loop"))
} }
return this return this
} }
@@ -219,34 +218,34 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
convertedText > Int.MAX_VALUE || convertedText < Int.MIN_VALUE) convertedText > Int.MAX_VALUE || convertedText < Int.MIN_VALUE)
) { ) {
FirConstExpressionImpl( FirConstExpressionImpl(
session, expression.getPsiOrNull(), IrConstKind.Long, convertedText, "Incorrect long: $text" expression.getPsiOrNull(), IrConstKind.Long, convertedText, "Incorrect long: $text"
) )
} else if (convertedText is Number) { } else if (convertedText is Number) {
// TODO: support byte / short // TODO: support byte / short
FirConstExpressionImpl( FirConstExpressionImpl(
session, expression.getPsiOrNull(), IrConstKind.Int, convertedText.toInt(), "Incorrect int: $text" expression.getPsiOrNull(), IrConstKind.Int, convertedText.toInt(), "Incorrect int: $text"
) )
} else { } else {
FirErrorExpressionImpl(session, expression.getPsiOrNull(), reason = "Incorrect constant expression: $text") FirErrorExpressionImpl(expression.getPsiOrNull(), reason = "Incorrect constant expression: $text")
} }
FLOAT_CONSTANT -> FLOAT_CONSTANT ->
if (convertedText is Float) { if (convertedText is Float) {
FirConstExpressionImpl( FirConstExpressionImpl(
session, expression.getPsiOrNull(), IrConstKind.Float, convertedText, "Incorrect float: $text" expression.getPsiOrNull(), IrConstKind.Float, convertedText, "Incorrect float: $text"
) )
} else { } else {
FirConstExpressionImpl( FirConstExpressionImpl(
session, expression.getPsiOrNull(), IrConstKind.Double, convertedText as Double, "Incorrect double: $text" expression.getPsiOrNull(), IrConstKind.Double, convertedText as Double, "Incorrect double: $text"
) )
} }
CHARACTER_CONSTANT -> CHARACTER_CONSTANT ->
FirConstExpressionImpl( FirConstExpressionImpl(
session, expression.getPsiOrNull(), IrConstKind.Char, text.parseCharacter(), "Incorrect character: $text" expression.getPsiOrNull(), IrConstKind.Char, text.parseCharacter(), "Incorrect character: $text"
) )
BOOLEAN_CONSTANT -> BOOLEAN_CONSTANT ->
FirConstExpressionImpl(session, expression.getPsiOrNull(), IrConstKind.Boolean, convertedText as Boolean) FirConstExpressionImpl(expression.getPsiOrNull(), IrConstKind.Boolean, convertedText as Boolean)
NULL -> NULL ->
FirConstExpressionImpl(session, expression.getPsiOrNull(), IrConstKind.Null, null) FirConstExpressionImpl(expression.getPsiOrNull(), IrConstKind.Null, null)
else -> else ->
throw AssertionError("Unknown literal type: $type, $text") throw AssertionError("Unknown literal type: $type, $text")
} }
@@ -267,11 +266,11 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
OPEN_QUOTE, CLOSING_QUOTE -> continue@L OPEN_QUOTE, CLOSING_QUOTE -> continue@L
LITERAL_STRING_TEMPLATE_ENTRY -> { LITERAL_STRING_TEMPLATE_ENTRY -> {
sb.append(entry.asText) sb.append(entry.asText)
FirConstExpressionImpl(session, entry.getPsiOrNull(), IrConstKind.String, entry.asText) FirConstExpressionImpl(entry.getPsiOrNull(), IrConstKind.String, entry.asText)
} }
ESCAPE_STRING_TEMPLATE_ENTRY -> { ESCAPE_STRING_TEMPLATE_ENTRY -> {
sb.append(entry.unescapedValue) sb.append(entry.unescapedValue)
FirConstExpressionImpl(session, entry.getPsiOrNull(), IrConstKind.String, entry.unescapedValue) FirConstExpressionImpl(entry.getPsiOrNull(), IrConstKind.String, entry.unescapedValue)
} }
SHORT_STRING_TEMPLATE_ENTRY, LONG_STRING_TEMPLATE_ENTRY -> { SHORT_STRING_TEMPLATE_ENTRY, LONG_STRING_TEMPLATE_ENTRY -> {
hasExpressions = true hasExpressions = true
@@ -280,7 +279,7 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
else -> { else -> {
hasExpressions = true hasExpressions = true
FirErrorExpressionImpl( FirErrorExpressionImpl(
session, entry.getPsiOrNull(), "Incorrect template entry: ${entry.asText}" entry.getPsiOrNull(), "Incorrect template entry: ${entry.asText}"
) )
} }
} }
@@ -291,14 +290,14 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
} }
else -> { else -> {
callCreated = true callCreated = true
FirStringConcatenationCallImpl(session, base).apply { FirStringConcatenationCallImpl(base).apply {
arguments += result!! arguments += result!!
arguments += nextArgument arguments += nextArgument
} }
} }
} }
} }
return if (hasExpressions) result!! else FirConstExpressionImpl(session, base, IrConstKind.String, sb.toString()) return if (hasExpressions) result!! else FirConstExpressionImpl(base, IrConstKind.String, sb.toString())
} }
/** /**
@@ -333,22 +332,22 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
convert: T.() -> FirExpression convert: T.() -> FirExpression
): FirExpression { ): FirExpression {
if (argument == null) { if (argument == null) {
return FirErrorExpressionImpl(session, argument, "Inc/dec without operand") return FirErrorExpressionImpl(argument, "Inc/dec without operand")
} }
return FirBlockImpl(session, baseExpression).apply { return FirBlockImpl(baseExpression).apply {
val tempName = Name.special("<unary>") val tempName = Name.special("<unary>")
val temporaryVariable = generateTemporaryVariable(this@BaseFirBuilder.session, baseExpression, tempName, argument.convert()) val temporaryVariable = generateTemporaryVariable(this@BaseFirBuilder.session, baseExpression, tempName, argument.convert())
statements += temporaryVariable statements += temporaryVariable
val resultName = Name.special("<unary-result>") val resultName = Name.special("<unary-result>")
val resultInitializer = FirFunctionCallImpl(this@BaseFirBuilder.session, baseExpression).apply { val resultInitializer = FirFunctionCallImpl(baseExpression).apply {
this.calleeReference = FirSimpleNamedReference(this@BaseFirBuilder.session, baseExpression?.operationReference, callName) this.calleeReference = FirSimpleNamedReference(baseExpression?.operationReference, callName)
this.explicitReceiver = generateResolvedAccessExpression(this@BaseFirBuilder.session, baseExpression, temporaryVariable) this.explicitReceiver = generateResolvedAccessExpression(baseExpression, temporaryVariable)
} }
val resultVar = generateTemporaryVariable(this@BaseFirBuilder.session, baseExpression, resultName, resultInitializer) val resultVar = generateTemporaryVariable(this@BaseFirBuilder.session, baseExpression, resultName, resultInitializer)
val assignment = argument.generateAssignment( val assignment = argument.generateAssignment(
baseExpression, baseExpression,
if (prefix && argument.elementType != REFERENCE_EXPRESSION) if (prefix && argument.elementType != REFERENCE_EXPRESSION)
generateResolvedAccessExpression(this@BaseFirBuilder.session, baseExpression, resultVar) generateResolvedAccessExpression(baseExpression, resultVar)
else else
resultInitializer, resultInitializer,
FirOperation.ASSIGN, convert FirOperation.ASSIGN, convert
@@ -366,14 +365,14 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
if (argument.elementType != REFERENCE_EXPRESSION) { if (argument.elementType != REFERENCE_EXPRESSION) {
statements += resultVar statements += resultVar
appendAssignment() appendAssignment()
statements += generateResolvedAccessExpression(this@BaseFirBuilder.session, baseExpression, resultVar) statements += generateResolvedAccessExpression(baseExpression, resultVar)
} else { } else {
appendAssignment() appendAssignment()
statements += generateAccessExpression(this@BaseFirBuilder.session, baseExpression, argument.getReferencedNameAsName()) statements += generateAccessExpression(baseExpression, argument.getReferencedNameAsName())
} }
} else { } else {
appendAssignment() appendAssignment()
statements += generateResolvedAccessExpression(this@BaseFirBuilder.session, baseExpression, temporaryVariable) statements += generateResolvedAccessExpression(baseExpression, temporaryVariable)
} }
} }
} }
@@ -386,10 +385,10 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
if (left != null) { if (left != null) {
when (tokenType) { when (tokenType) {
REFERENCE_EXPRESSION -> { REFERENCE_EXPRESSION -> {
return FirSimpleNamedReference(this@BaseFirBuilder.session, left.getPsiOrNull(), left.getReferencedNameAsName()) return FirSimpleNamedReference(left.getPsiOrNull(), left.getReferencedNameAsName())
} }
THIS_EXPRESSION -> { THIS_EXPRESSION -> {
return FirExplicitThisReference(this@BaseFirBuilder.session, left.getPsiOrNull(), left.getLabelName()) return FirExplicitThisReference(left.getPsiOrNull(), left.getLabelName())
} }
DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> {
val firMemberAccess = left.convertQualified() val firMemberAccess = left.convertQualified()
@@ -399,7 +398,7 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
firMemberAccess.calleeReference firMemberAccess.calleeReference
} else { } else {
FirErrorNamedReference( FirErrorNamedReference(
this@BaseFirBuilder.session, left.getPsiOrNull(), "Unsupported qualified LValue: ${left.asText}" left.getPsiOrNull(), "Unsupported qualified LValue: ${left.asText}"
) )
} }
} }
@@ -408,7 +407,7 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
} }
} }
} }
return FirErrorNamedReference(this@BaseFirBuilder.session, left.getPsiOrNull(), "Unsupported LValue: $tokenType") return FirErrorNamedReference(left.getPsiOrNull(), "Unsupported LValue: $tokenType")
} }
fun T?.generateAssignment( fun T?.generateAssignment(
@@ -424,12 +423,12 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
if (tokenType == ARRAY_ACCESS_EXPRESSION) { if (tokenType == ARRAY_ACCESS_EXPRESSION) {
val firArrayAccess = this!!.convert() as FirFunctionCallImpl val firArrayAccess = this!!.convert() as FirFunctionCallImpl
val arraySet = if (operation != FirOperation.ASSIGN) { val arraySet = if (operation != FirOperation.ASSIGN) {
FirArraySetCallImpl(session, psi, value, operation).apply { FirArraySetCallImpl(psi, value, operation).apply {
indexes += firArrayAccess.arguments indexes += firArrayAccess.arguments
} }
} else { } else {
return firArrayAccess.apply { return firArrayAccess.apply {
calleeReference = FirSimpleNamedReference(this@BaseFirBuilder.session, psi, OperatorNameConventions.SET) calleeReference = FirSimpleNamedReference(psi, OperatorNameConventions.SET)
arguments += value arguments += value
} }
} }
@@ -439,31 +438,32 @@ abstract class BaseFirBuilder<T>(val session: FirSession, val context: Context =
lValue = initializeLValue(arrayExpression) { convert() as? FirQualifiedAccess } lValue = initializeLValue(arrayExpression) { convert() as? FirQualifiedAccess }
} }
} }
return FirBlockImpl(session, arrayExpression).apply { val psiArrayExpression = firArrayAccess.explicitReceiver?.psi
return FirBlockImpl(psiArrayExpression).apply {
val name = Name.special("<array-set>") val name = Name.special("<array-set>")
statements += generateTemporaryVariable( statements += generateTemporaryVariable(
this@BaseFirBuilder.session, this@generateAssignment.getPsiOrNull(), name, firArrayAccess.explicitReceiver!! this@BaseFirBuilder.session, this@generateAssignment.getPsiOrNull(), name, firArrayAccess.explicitReceiver!!
) )
statements += arraySet.apply { lValue = FirSimpleNamedReference(this@BaseFirBuilder.session, arrayExpression, name) } statements += arraySet.apply { lValue = FirSimpleNamedReference(psiArrayExpression, name) }
} }
} }
if (operation != FirOperation.ASSIGN && if (operation != FirOperation.ASSIGN &&
tokenType != REFERENCE_EXPRESSION && tokenType != THIS_EXPRESSION && tokenType != REFERENCE_EXPRESSION && tokenType != THIS_EXPRESSION &&
((tokenType != DOT_QUALIFIED_EXPRESSION && tokenType != SAFE_ACCESS_EXPRESSION) || this.selectorExpression?.elementType != REFERENCE_EXPRESSION) ((tokenType != DOT_QUALIFIED_EXPRESSION && tokenType != SAFE_ACCESS_EXPRESSION) || this.selectorExpression?.elementType != REFERENCE_EXPRESSION)
) { ) {
return FirBlockImpl(session, this.getPsiOrNull()).apply { return FirBlockImpl(this.getPsiOrNull()).apply {
val name = Name.special("<complex-set>") val name = Name.special("<complex-set>")
statements += generateTemporaryVariable( statements += generateTemporaryVariable(
this@BaseFirBuilder.session, this@generateAssignment.getPsiOrNull(), name, this@BaseFirBuilder.session, this@generateAssignment.getPsiOrNull(), name,
this@generateAssignment?.convert() this@generateAssignment?.convert()
?: FirErrorExpressionImpl(this@BaseFirBuilder.session, this.getPsiOrNull(), "No LValue in assignment") ?: FirErrorExpressionImpl(this.getPsiOrNull(), "No LValue in assignment")
) )
statements += FirVariableAssignmentImpl(this@BaseFirBuilder.session, psi, value, operation).apply { statements += FirVariableAssignmentImpl(psi, value, operation).apply {
lValue = FirSimpleNamedReference(this@BaseFirBuilder.session, this.getPsiOrNull(), name) lValue = FirSimpleNamedReference(this.getPsiOrNull(), name)
} }
} }
} }
return FirVariableAssignmentImpl(session, psi, value, operation).apply { return FirVariableAssignmentImpl(psi, value, operation).apply {
lValue = initializeLValue(this@generateAssignment) { convert() as? FirQualifiedAccess } lValue = initializeLValue(this@generateAssignment) { convert() as? FirQualifiedAccess }
} }
} }
@@ -13,20 +13,14 @@ import org.jetbrains.kotlin.fir.FirFunctionTarget
import org.jetbrains.kotlin.fir.FirReference import org.jetbrains.kotlin.fir.FirReference
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirWhenSubject import org.jetbrains.kotlin.fir.FirWhenSubject
import org.jetbrains.kotlin.fir.declarations.impl.FirModifiableAccessorsOwner import org.jetbrains.kotlin.fir.declarations.impl.*
import org.jetbrains.kotlin.fir.declarations.impl.FirPropertyAccessorImpl
import org.jetbrains.kotlin.fir.declarations.impl.FirValueParameterImpl
import org.jetbrains.kotlin.fir.declarations.impl.FirVariableImpl
import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.* import org.jetbrains.kotlin.fir.expressions.impl.*
import org.jetbrains.kotlin.fir.references.* import org.jetbrains.kotlin.fir.references.*
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.fir.types.ConeStarProjection import org.jetbrains.kotlin.fir.types.ConeStarProjection
import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.types.impl.FirImplicitBooleanTypeRef import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.fir.types.impl.FirImplicitKPropertyTypeRef
import org.jetbrains.kotlin.fir.types.impl.FirImplicitTypeRefImpl
import org.jetbrains.kotlin.fir.types.impl.FirImplicitUnitTypeRef
import org.jetbrains.kotlin.ir.expressions.IrConstKind import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -134,6 +128,12 @@ fun IElementType.toFirOperation(): FirOperation =
else -> throw AssertionError(this.toString()) else -> throw AssertionError(this.toString())
} }
fun FirTypeParameterImpl.addDefaultBoundIfNecessary() {
if (bounds.isEmpty()) {
bounds += FirImplicitNullableAnyTypeRef(null)
}
}
fun FirExpression.generateNotNullOrOther( fun FirExpression.generateNotNullOrOther(
session: FirSession, other: FirExpression, caseId: String, basePsi: KtElement? session: FirSession, other: FirExpression, caseId: String, basePsi: KtElement?
): FirWhenExpression { ): FirWhenExpression {
@@ -299,3 +299,74 @@ fun generateTemporaryVariable(
fun generateTemporaryVariable( fun generateTemporaryVariable(
session: FirSession, psi: PsiElement?, specialName: String, initializer: FirExpression session: FirSession, psi: PsiElement?, specialName: String, initializer: FirExpression
): FirVariable<*> = generateTemporaryVariable(session, psi, Name.special("<$specialName>"), initializer) ): FirVariable<*> = generateTemporaryVariable(session, psi, Name.special("<$specialName>"), initializer)
fun FirModifiableAccessorsOwner.generateAccessorsByDelegate(session: FirSession, member: Boolean, stubMode: Boolean) {
val variable = this as FirVariable<*>
val delegateFieldSymbol = delegateFieldSymbol ?: return
val delegate = delegate as? FirWrappedDelegateExpressionImpl ?: return
fun delegateAccess() = FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirDelegateFieldReferenceImpl(null, delegateFieldSymbol)
}
fun thisRef() =
if (member) FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirExplicitThisReference(null, null)
}
else FirConstExpressionImpl(null, IrConstKind.Null, null)
fun propertyRef() = FirCallableReferenceAccessImpl(null).apply {
calleeReference = FirResolvedCallableReferenceImpl(null, variable.name, variable.symbol)
typeRef = FirImplicitKPropertyTypeRef(null, ConeStarProjection)
}
delegate.delegateProvider = if (stubMode) FirExpressionStub(null) else FirFunctionCallImpl(null).apply {
explicitReceiver = delegate.expression
calleeReference = FirSimpleNamedReference(null, PROVIDE_DELEGATE)
arguments += thisRef()
arguments += propertyRef()
}
if (stubMode) return
getter = (getter as? FirPropertyAccessorImpl)
?: FirPropertyAccessorImpl(session, null, true, Visibilities.UNKNOWN, FirImplicitTypeRefImpl(null)).apply Accessor@{
body = FirSingleExpressionBlock(
FirReturnExpressionImpl(
null,
FirFunctionCallImpl(null).apply {
explicitReceiver = delegateAccess()
calleeReference = FirSimpleNamedReference(null, GET_VALUE)
arguments += thisRef()
arguments += propertyRef()
}
).apply {
target = FirFunctionTarget(null)
target.bind(this@Accessor)
}
)
}
setter = (setter as? FirPropertyAccessorImpl)
?: FirPropertyAccessorImpl(session, null, false, Visibilities.UNKNOWN, FirImplicitUnitTypeRef(null)).apply {
val parameter = FirValueParameterImpl(
session, null, DELEGATED_SETTER_PARAM,
FirImplicitTypeRefImpl(null),
defaultValue = null, isCrossinline = false,
isNoinline = false, isVararg = false
)
valueParameters += parameter
body = FirSingleExpressionBlock(
FirFunctionCallImpl(null).apply {
explicitReceiver = delegateAccess()
calleeReference = FirSimpleNamedReference(null, SET_VALUE)
arguments += thisRef()
arguments += propertyRef()
arguments += FirQualifiedAccessExpressionImpl(null).apply {
calleeReference = FirResolvedCallableReferenceImpl(psi, DELEGATED_SETTER_PARAM, parameter.symbol)
}
}
)
}
}
private val GET_VALUE = Name.identifier("getValue")
private val SET_VALUE = Name.identifier("setValue")
private val PROVIDE_DELEGATE = Name.identifier("provideDelegate")
private val DELEGATED_SETTER_PARAM = Name.special("<set-?>")