[FIR] Use source elements instead of psi in ConversionUtils.kt
This commit is contained in:
+15
-16
@@ -194,11 +194,13 @@ class ExpressionsConverter(
|
|||||||
var leftArgNode: LighterASTNode? = null
|
var leftArgNode: LighterASTNode? = null
|
||||||
var rightArgAsFir: FirExpression = buildErrorExpression(null, ConeSimpleDiagnostic("No right operand", DiagnosticKind.Syntax))
|
var rightArgAsFir: FirExpression = buildErrorExpression(null, ConeSimpleDiagnostic("No right operand", DiagnosticKind.Syntax))
|
||||||
var rightArg: LighterASTNode? = null
|
var rightArg: LighterASTNode? = null
|
||||||
|
var operationReferenceSource: FirLightSourceElement? = null
|
||||||
binaryExpression.forEachChildren {
|
binaryExpression.forEachChildren {
|
||||||
when (it.tokenType) {
|
when (it.tokenType) {
|
||||||
OPERATION_REFERENCE -> {
|
OPERATION_REFERENCE -> {
|
||||||
isLeftArgument = false
|
isLeftArgument = false
|
||||||
operationTokenName = it.asText
|
operationTokenName = it.asText
|
||||||
|
operationReferenceSource = it.toFirSourceElement()
|
||||||
}
|
}
|
||||||
else -> if (it.isExpression()) {
|
else -> if (it.isExpression()) {
|
||||||
if (isLeftArgument) {
|
if (isLeftArgument) {
|
||||||
@@ -211,25 +213,18 @@ class ExpressionsConverter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val baseSource = binaryExpression.toFirSourceElement()
|
||||||
val operationToken = operationTokenName.getOperationSymbol()
|
val operationToken = operationTokenName.getOperationSymbol()
|
||||||
val leftArgAsFir = getAsFirExpression<FirExpression>(leftArgNode, "No left operand")
|
val leftArgAsFir = getAsFirExpression<FirExpression>(leftArgNode, "No left operand")
|
||||||
when (operationToken) {
|
when (operationToken) {
|
||||||
ELVIS ->
|
ELVIS ->
|
||||||
return leftArgAsFir.generateNotNullOrOther(
|
return leftArgAsFir.generateNotNullOrOther(baseSession, rightArgAsFir, "elvis", baseSource)
|
||||||
baseSession, rightArgAsFir, "elvis", null
|
|
||||||
)
|
|
||||||
ANDAND, OROR ->
|
ANDAND, OROR ->
|
||||||
return leftArgAsFir.generateLazyLogicalOperation(
|
return leftArgAsFir.generateLazyLogicalOperation(rightArgAsFir, operationToken == ANDAND, baseSource)
|
||||||
rightArgAsFir, operationToken == ANDAND, null
|
|
||||||
)
|
|
||||||
in OperatorConventions.IN_OPERATIONS ->
|
in OperatorConventions.IN_OPERATIONS ->
|
||||||
return rightArgAsFir.generateContainsOperation(
|
return rightArgAsFir.generateContainsOperation(leftArgAsFir, operationToken == NOT_IN, baseSource, operationReferenceSource)
|
||||||
leftArgAsFir, operationToken == NOT_IN, null, null
|
|
||||||
)
|
|
||||||
in OperatorConventions.COMPARISON_OPERATIONS ->
|
in OperatorConventions.COMPARISON_OPERATIONS ->
|
||||||
return leftArgAsFir.generateComparisonExpression(
|
return leftArgAsFir.generateComparisonExpression(rightArgAsFir, operationToken, baseSource, operationReferenceSource)
|
||||||
rightArgAsFir, operationToken, null, null
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
val conventionCallName = operationToken.toBinaryName()
|
val conventionCallName = operationToken.toBinaryName()
|
||||||
return if (conventionCallName != null || operationToken == IDENTIFIER) {
|
return if (conventionCallName != null || operationToken == IDENTIFIER) {
|
||||||
@@ -245,7 +240,7 @@ class ExpressionsConverter(
|
|||||||
} else {
|
} else {
|
||||||
val firOperation = operationToken.toFirOperation()
|
val firOperation = operationToken.toFirOperation()
|
||||||
if (firOperation in FirOperation.ASSIGNMENTS) {
|
if (firOperation in FirOperation.ASSIGNMENTS) {
|
||||||
return leftArgNode.generateAssignment(null, rightArg, rightArgAsFir, firOperation) { getAsFirExpression(this) }
|
return leftArgNode.generateAssignment(binaryExpression.toFirSourceElement(), rightArg, rightArgAsFir, firOperation) { getAsFirExpression(this) }
|
||||||
} else {
|
} else {
|
||||||
buildOperatorCall {
|
buildOperatorCall {
|
||||||
source = binaryExpression.toFirSourceElement()
|
source = binaryExpression.toFirSourceElement()
|
||||||
@@ -688,9 +683,13 @@ class ExpressionsConverter(
|
|||||||
private fun convertWhenConditionInRange(whenCondition: LighterASTNode, subject: FirWhenSubject?): FirExpression {
|
private fun convertWhenConditionInRange(whenCondition: LighterASTNode, subject: FirWhenSubject?): FirExpression {
|
||||||
var isNegate = false
|
var isNegate = false
|
||||||
var firExpression: FirExpression = buildErrorExpression(null, ConeSimpleDiagnostic("No range in condition with range", DiagnosticKind.Syntax))
|
var firExpression: FirExpression = buildErrorExpression(null, ConeSimpleDiagnostic("No range in condition with range", DiagnosticKind.Syntax))
|
||||||
|
var conditionSource: FirLightSourceElement? = null
|
||||||
whenCondition.forEachChildren {
|
whenCondition.forEachChildren {
|
||||||
when {
|
when {
|
||||||
it.tokenType == OPERATION_REFERENCE && it.asText == NOT_IN.value -> isNegate = true
|
it.tokenType == OPERATION_REFERENCE && it.asText == NOT_IN.value -> {
|
||||||
|
conditionSource = it.toFirSourceElement()
|
||||||
|
isNegate = true
|
||||||
|
}
|
||||||
else -> if (it.isExpression()) firExpression = getAsFirExpression(it)
|
else -> if (it.isExpression()) firExpression = getAsFirExpression(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -709,8 +708,8 @@ class ExpressionsConverter(
|
|||||||
return firExpression.generateContainsOperation(
|
return firExpression.generateContainsOperation(
|
||||||
subjectExpression,
|
subjectExpression,
|
||||||
inverted = isNegate,
|
inverted = isNegate,
|
||||||
base = null, // TODO: replace with FirSourceElement
|
baseSource = whenCondition.toFirSourceElement(),
|
||||||
operationReference = null
|
operationReferenceSource = conditionSource
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,7 +217,12 @@ internal fun KtWhenCondition.toFirWhenCondition(
|
|||||||
}
|
}
|
||||||
is KtWhenConditionInRange -> {
|
is KtWhenConditionInRange -> {
|
||||||
val firRange = rangeExpression.convert("No range in condition with range")
|
val firRange = rangeExpression.convert("No range in condition with range")
|
||||||
firRange.generateContainsOperation(firSubjectExpression, isNegated, rangeExpression, operationReference)
|
firRange.generateContainsOperation(
|
||||||
|
firSubjectExpression,
|
||||||
|
isNegated,
|
||||||
|
rangeExpression?.toFirSourceElement(),
|
||||||
|
operationReference.toFirSourceElement()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
is KtWhenConditionIsPattern -> {
|
is KtWhenConditionIsPattern -> {
|
||||||
buildTypeOperatorCall {
|
buildTypeOperatorCall {
|
||||||
@@ -255,14 +260,12 @@ internal fun Array<KtWhenCondition>.toFirWhenCondition(
|
|||||||
fun FirExpression.generateContainsOperation(
|
fun FirExpression.generateContainsOperation(
|
||||||
argument: FirExpression,
|
argument: FirExpression,
|
||||||
inverted: Boolean,
|
inverted: Boolean,
|
||||||
base: KtExpression?,
|
baseSource: FirSourceElement?,
|
||||||
operationReference: KtOperationReferenceExpression?,
|
operationReferenceSource: FirSourceElement?
|
||||||
): FirFunctionCall {
|
): FirFunctionCall {
|
||||||
val baseSource = base?.toFirSourceElement()
|
val containsCall = createConventionCall(operationReferenceSource, baseSource, argument, OperatorNameConventions.CONTAINS)
|
||||||
val containsCall = createConventionCall(operationReference, baseSource, argument, OperatorNameConventions.CONTAINS)
|
|
||||||
if (!inverted) return containsCall
|
if (!inverted) return containsCall
|
||||||
|
|
||||||
val operationReferenceSource = operationReference?.toFirSourceElement()
|
|
||||||
return buildFunctionCall {
|
return buildFunctionCall {
|
||||||
source = baseSource
|
source = baseSource
|
||||||
calleeReference = buildSimpleNamedReference {
|
calleeReference = buildSimpleNamedReference {
|
||||||
@@ -276,15 +279,14 @@ fun FirExpression.generateContainsOperation(
|
|||||||
fun FirExpression.generateComparisonExpression(
|
fun FirExpression.generateComparisonExpression(
|
||||||
argument: FirExpression,
|
argument: FirExpression,
|
||||||
operatorToken: IElementType,
|
operatorToken: IElementType,
|
||||||
base: KtExpression?,
|
baseSource: FirSourceElement?,
|
||||||
operationReference: KtOperationReferenceExpression?,
|
operationReferenceSource: FirSourceElement?,
|
||||||
): FirComparisonExpression {
|
): FirComparisonExpression {
|
||||||
require(operatorToken in OperatorConventions.COMPARISON_OPERATIONS) {
|
require(operatorToken in OperatorConventions.COMPARISON_OPERATIONS) {
|
||||||
"$operatorToken is not in ${OperatorConventions.COMPARISON_OPERATIONS}"
|
"$operatorToken is not in ${OperatorConventions.COMPARISON_OPERATIONS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
val baseSource = base?.toFirSourceElement()
|
val compareToCall = createConventionCall(operationReferenceSource, baseSource, argument, OperatorNameConventions.COMPARE_TO)
|
||||||
val compareToCall = createConventionCall(operationReference, baseSource, argument, OperatorNameConventions.COMPARE_TO)
|
|
||||||
|
|
||||||
val firOperation = when (operatorToken) {
|
val firOperation = when (operatorToken) {
|
||||||
KtTokens.LT -> FirOperation.LT
|
KtTokens.LT -> FirOperation.LT
|
||||||
@@ -302,12 +304,11 @@ fun FirExpression.generateComparisonExpression(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun FirExpression.createConventionCall(
|
private fun FirExpression.createConventionCall(
|
||||||
operationReference: KtOperationReferenceExpression?,
|
operationReferenceSource: FirSourceElement?,
|
||||||
baseSource: FirPsiSourceElement?,
|
baseSource: FirSourceElement?,
|
||||||
argument: FirExpression,
|
argument: FirExpression,
|
||||||
conventionName: Name
|
conventionName: Name
|
||||||
): FirFunctionCall {
|
): FirFunctionCall {
|
||||||
val operationReferenceSource = operationReference?.toFirSourceElement()
|
|
||||||
return buildFunctionCall {
|
return buildFunctionCall {
|
||||||
source = baseSource
|
source = baseSource
|
||||||
calleeReference = buildSimpleNamedReference {
|
calleeReference = buildSimpleNamedReference {
|
||||||
|
|||||||
@@ -1383,11 +1383,11 @@ class RawFirBuilder(
|
|||||||
return leftArgument.generateLazyLogicalOperation(rightArgument, operationToken == ANDAND, source)
|
return leftArgument.generateLazyLogicalOperation(rightArgument, operationToken == ANDAND, source)
|
||||||
in OperatorConventions.IN_OPERATIONS ->
|
in OperatorConventions.IN_OPERATIONS ->
|
||||||
return rightArgument.generateContainsOperation(
|
return rightArgument.generateContainsOperation(
|
||||||
leftArgument, operationToken == NOT_IN, expression, expression.operationReference,
|
leftArgument, operationToken == NOT_IN, source, expression.operationReference.toFirSourceElement(),
|
||||||
)
|
)
|
||||||
in OperatorConventions.COMPARISON_OPERATIONS ->
|
in OperatorConventions.COMPARISON_OPERATIONS ->
|
||||||
return leftArgument.generateComparisonExpression(
|
return leftArgument.generateComparisonExpression(
|
||||||
rightArgument, operationToken, expression, expression.operationReference,
|
rightArgument, operationToken, source, expression.operationReference.toFirSourceElement(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
val conventionCallName = operationToken.toBinaryName()
|
val conventionCallName = operationToken.toBinaryName()
|
||||||
|
|||||||
Reference in New Issue
Block a user