FIR/UAST: commonize if expression
This commit is contained in:
committed by
Ilya Kirillov
parent
50f750187b
commit
6e1b04e4c9
+33
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.uast.kotlin
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.psi.KtIfExpression
|
||||||
|
import org.jetbrains.uast.UElement
|
||||||
|
import org.jetbrains.uast.UIdentifier
|
||||||
|
import org.jetbrains.uast.UIfExpression
|
||||||
|
|
||||||
|
class KotlinUIfExpression(
|
||||||
|
override val sourcePsi: KtIfExpression,
|
||||||
|
givenParent: UElement?
|
||||||
|
) : KotlinAbstractUExpression(givenParent), UIfExpression, KotlinUElementWithType, KotlinEvaluatableUElement {
|
||||||
|
override val condition by lz {
|
||||||
|
baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.condition, this)
|
||||||
|
}
|
||||||
|
override val thenExpression by lz {
|
||||||
|
baseResolveProviderService.baseKotlinConverter.convertOrNull(sourcePsi.then, this)
|
||||||
|
}
|
||||||
|
override val elseExpression by lz {
|
||||||
|
baseResolveProviderService.baseKotlinConverter.convertOrNull(sourcePsi.`else`, this)
|
||||||
|
}
|
||||||
|
override val isTernary = false
|
||||||
|
|
||||||
|
override val ifIdentifier: UIdentifier
|
||||||
|
get() = UIdentifier(null, this)
|
||||||
|
|
||||||
|
override val elseIdentifier: UIdentifier?
|
||||||
|
get() = null
|
||||||
|
}
|
||||||
@@ -280,6 +280,8 @@ internal object FirKotlinConverter : BaseKotlinConverter {
|
|||||||
is KtDoWhileExpression -> expr<UDoWhileExpression>(build(::KotlinUDoWhileExpression))
|
is KtDoWhileExpression -> expr<UDoWhileExpression>(build(::KotlinUDoWhileExpression))
|
||||||
is KtWhileExpression -> expr<UWhileExpression>(build(::KotlinUWhileExpression))
|
is KtWhileExpression -> expr<UWhileExpression>(build(::KotlinUWhileExpression))
|
||||||
|
|
||||||
|
is KtIfExpression -> expr<UIfExpression>(build(::KotlinUIfExpression))
|
||||||
|
|
||||||
else -> expr<UExpression>(build(::UnknownKotlinExpression))
|
else -> expr<UExpression>(build(::UnknownKotlinExpression))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun test(b: Boolean): Any {
|
||||||
|
return if (b)
|
||||||
|
42
|
||||||
|
else
|
||||||
|
"42"
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
UFile (package = )
|
||||||
|
UClass (name = IfKt)
|
||||||
|
UMethod (name = test)
|
||||||
|
UParameter (name = b)
|
||||||
|
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
|
||||||
|
UBlockExpression
|
||||||
|
UReturnExpression
|
||||||
|
UIfExpression
|
||||||
|
USimpleNameReferenceExpression (identifier = b)
|
||||||
|
ULiteralExpression (value = 42)
|
||||||
|
ULiteralExpression (value = "42")
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
UFile (package = )
|
||||||
|
UClass (name = IfKt)
|
||||||
|
UMethod (name = test)
|
||||||
|
UParameter (name = b)
|
||||||
|
UBlockExpression
|
||||||
|
UReturnExpression
|
||||||
|
UIfExpression
|
||||||
|
[!] UnknownKotlinExpression (REFERENCE_EXPRESSION)
|
||||||
|
ULiteralExpression (value = "not-yet-compile-time-constant")
|
||||||
|
ULiteralExpression (value = "42")
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
public final class IfKt {
|
||||||
|
public static final fun test(@org.jetbrains.annotations.NotNull b: boolean) : java.lang.Object {
|
||||||
|
return if (b) 42 else "42"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
public final class IfKt {
|
||||||
|
public static final fun test(b: boolean) : java.lang.Object {
|
||||||
|
return if ([!] UnknownKotlinExpression (REFERENCE_EXPRESSION)) "not-yet-compile-time-constant" else "42"
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+5
@@ -39,6 +39,11 @@ public class FE1UastDeclarationTestGenerated extends AbstractFE1UastDeclarationT
|
|||||||
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
|
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("if.kt")
|
||||||
|
public void testIf() throws Exception {
|
||||||
|
runTest("plugins/uast-kotlin-fir/testData/declaration/if.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("importOnDemand.kt")
|
@TestMetadata("importOnDemand.kt")
|
||||||
public void testImportOnDemand() throws Exception {
|
public void testImportOnDemand() throws Exception {
|
||||||
runTest("plugins/uast-kotlin-fir/testData/declaration/importOnDemand.kt");
|
runTest("plugins/uast-kotlin-fir/testData/declaration/importOnDemand.kt");
|
||||||
|
|||||||
Generated
+5
@@ -39,6 +39,11 @@ public class FirUastDeclarationTestGenerated extends AbstractFirUastDeclarationT
|
|||||||
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
|
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("if.kt")
|
||||||
|
public void testIf() throws Exception {
|
||||||
|
runTest("plugins/uast-kotlin-fir/testData/declaration/if.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("importOnDemand.kt")
|
@TestMetadata("importOnDemand.kt")
|
||||||
public void testImportOnDemand() throws Exception {
|
public void testImportOnDemand() throws Exception {
|
||||||
runTest("plugins/uast-kotlin-fir/testData/declaration/importOnDemand.kt");
|
runTest("plugins/uast-kotlin-fir/testData/declaration/importOnDemand.kt");
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2016 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.uast.kotlin
|
|
||||||
|
|
||||||
import org.jetbrains.kotlin.psi.KtIfExpression
|
|
||||||
import org.jetbrains.uast.UElement
|
|
||||||
import org.jetbrains.uast.UIdentifier
|
|
||||||
import org.jetbrains.uast.UIfExpression
|
|
||||||
|
|
||||||
class KotlinUIfExpression(
|
|
||||||
override val sourcePsi: KtIfExpression,
|
|
||||||
givenParent: UElement?
|
|
||||||
) : KotlinAbstractUExpression(givenParent), UIfExpression, KotlinUElementWithType, KotlinEvaluatableUElement {
|
|
||||||
override val condition by lz { KotlinConverter.convertOrEmpty(sourcePsi.condition, this) }
|
|
||||||
override val thenExpression by lz { KotlinConverter.convertOrNull(sourcePsi.then, this) }
|
|
||||||
override val elseExpression by lz { KotlinConverter.convertOrNull(sourcePsi.`else`, this) }
|
|
||||||
override val isTernary = false
|
|
||||||
|
|
||||||
override val ifIdentifier: UIdentifier
|
|
||||||
get() = UIdentifier(null, this)
|
|
||||||
|
|
||||||
override val elseIdentifier: UIdentifier?
|
|
||||||
get() = null
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user