FIR/UAST: commonize loop/break/continue expressions

This commit is contained in:
Jinseong Jeon
2021-05-26 23:25:02 -07:00
committed by Ilya Kirillov
parent 1146f60db3
commit 50f750187b
20 changed files with 169 additions and 133 deletions
@@ -0,0 +1,29 @@
/*
* 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.KtDoWhileExpression
import org.jetbrains.uast.UDoWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
class KotlinUDoWhileExpression(
override val sourcePsi: KtDoWhileExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UDoWhileExpression {
override val condition by lz {
baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.condition, this)
}
override val body by lz {
baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.body, this)
}
override val doIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
@@ -0,0 +1,26 @@
/*
* 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.KtWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UWhileExpression
class KotlinUWhileExpression(
override val sourcePsi: KtWhileExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UWhileExpression {
override val condition by lz {
baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.condition, this)
}
override val body by lz {
baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.body, this)
}
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
@@ -0,0 +1,19 @@
/*
* 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.KtBreakExpression
import org.jetbrains.uast.UBreakExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.kotlin.KotlinAbstractUExpression
class KotlinUBreakExpression(
override val sourcePsi: KtBreakExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UBreakExpression {
override val label: String?
get() = sourcePsi.getLabelName()
}
@@ -0,0 +1,19 @@
/*
* 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.KtContinueExpression
import org.jetbrains.uast.UContinueExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.kotlin.KotlinAbstractUExpression
class KotlinUContinueExpression(
override val sourcePsi: KtContinueExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UContinueExpression {
override val label: String?
get() = sourcePsi.getLabelName()
}
@@ -268,11 +268,18 @@ internal object FirKotlinConverter : BaseKotlinConverter {
}
is KtCollectionLiteralExpression -> expr<UCallExpression>(build(::KotlinUCollectionLiteralExpression))
is KtConstantExpression -> expr<ULiteralExpression>(build(::KotlinULiteralExpression))
is KtBlockExpression -> expr<UBlockExpression> {
// TODO: differentiate lambda
FirKotlinUBlockExpression(expression, givenParent)
}
is KtReturnExpression -> expr<UReturnExpression>(build(::KotlinUReturnExpression))
is KtBreakExpression -> expr<UBreakExpression>(build(::KotlinUBreakExpression))
is KtContinueExpression -> expr<UContinueExpression>(build(::KotlinUContinueExpression))
is KtDoWhileExpression -> expr<UDoWhileExpression>(build(::KotlinUDoWhileExpression))
is KtWhileExpression -> expr<UWhileExpression>(build(::KotlinUWhileExpression))
else -> expr<UExpression>(build(::UnknownKotlinExpression))
}
}
@@ -0,0 +1,6 @@
fun test() {
var x : String? = null
do {
x = "non-null"
} while (x != null)
}
@@ -0,0 +1,15 @@
UFile (package = )
UClass (name = DoWhileKt)
UMethod (name = test)
UBlockExpression
UDeclarationsExpression
ULocalVariable (name = x)
ULiteralExpression (value = null)
UDoWhileExpression
UBinaryExpression (operator = !=)
USimpleNameReferenceExpression (identifier = x)
ULiteralExpression (value = null)
UBlockExpression
UBinaryExpression (operator = =)
USimpleNameReferenceExpression (identifier = x)
ULiteralExpression (value = "non-null")
@@ -0,0 +1,9 @@
UFile (package = )
UClass (name = DoWhileKt)
UMethod (name = test)
UBlockExpression
[!] UnknownKotlinExpression (PROPERTY)
UDoWhileExpression
[!] UnknownKotlinExpression (BINARY_EXPRESSION)
UBlockExpression
[!] UnknownKotlinExpression (BINARY_EXPRESSION)
@@ -0,0 +1,9 @@
public final class DoWhileKt {
public static final fun test() : void {
var x: java.lang.String = null
do {
x = "non-null"
}while (x != null)
}
}
@@ -0,0 +1,9 @@
public final class DoWhileKt {
public static final fun test() : void {
[!] UnknownKotlinExpression (PROPERTY)
do {
[!] UnknownKotlinExpression (BINARY_EXPRESSION)
}while ([!] UnknownKotlinExpression (BINARY_EXPRESSION))
}
}
@@ -12,4 +12,6 @@ UFile (package = )
[!] UnknownKotlinExpression (CALL_EXPRESSION)
UMethod (name = lambdaInPlaceCall)
UBlockExpression
[!] UnknownKotlinExpression (WHILE)
UWhileExpression
[!] UnknownKotlinExpression (CALL_EXPRESSION)
UBlockExpression
@@ -10,6 +10,7 @@ public final class LambdasKt {
[!] UnknownKotlinExpression (CALL_EXPRESSION)
}
public static final fun lambdaInPlaceCall() : void {
[!] UnknownKotlinExpression (WHILE)
while ([!] UnknownKotlinExpression (CALL_EXPRESSION)) {
}
}
}
@@ -12,4 +12,6 @@ UFile (package = ) [import java.util.stream.Stream...]
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)]
UMethod (name = lambdaInPlaceCall) [public static final fun lambdaInPlaceCall() : void {...}]
UBlockExpression [{...}]
[!] UnknownKotlinExpression (WHILE) [[!] UnknownKotlinExpression (WHILE)]
UWhileExpression [while ([!] UnknownKotlinExpression (CALL_EXPRESSION)) {...}]
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)]
UBlockExpression [{...}]
@@ -12,4 +12,6 @@ UFile (package = ) [import java.util.stream.Stream...]
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)] = Undetermined
UMethod (name = lambdaInPlaceCall) [public static final fun lambdaInPlaceCall() : void {...}]
UBlockExpression [{...}] = Undetermined
[!] UnknownKotlinExpression (WHILE) [[!] UnknownKotlinExpression (WHILE)] = Undetermined
UWhileExpression [while ([!] UnknownKotlinExpression (CALL_EXPRESSION)) {...}] = Undetermined
[!] UnknownKotlinExpression (CALL_EXPRESSION) [[!] UnknownKotlinExpression (CALL_EXPRESSION)] = Undetermined
UBlockExpression [{...}] = Undetermined
@@ -29,6 +29,11 @@ public class FE1UastDeclarationTestGenerated extends AbstractFE1UastDeclarationT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/uast-kotlin-fir/testData/declaration"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("doWhile.kt")
public void testDoWhile() throws Exception {
runTest("plugins/uast-kotlin-fir/testData/declaration/doWhile.kt");
}
@TestMetadata("facade.kt")
public void testFacade() throws Exception {
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
@@ -29,6 +29,11 @@ public class FirUastDeclarationTestGenerated extends AbstractFirUastDeclarationT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/uast-kotlin-fir/testData/declaration"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("doWhile.kt")
public void testDoWhile() throws Exception {
runTest("plugins/uast-kotlin-fir/testData/declaration/doWhile.kt");
}
@TestMetadata("facade.kt")
public void testFacade() throws Exception {
runTest("plugins/uast-kotlin-fir/testData/declaration/facade.kt");
@@ -1,30 +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.expressions
import org.jetbrains.kotlin.psi.KtBreakExpression
import org.jetbrains.uast.UBreakExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.kotlin.KotlinAbstractUExpression
class KotlinUBreakExpression(
override val sourcePsi: KtBreakExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UBreakExpression {
override val label: String?
get() = sourcePsi.getLabelName()
}
@@ -1,30 +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.expressions
import org.jetbrains.kotlin.psi.KtContinueExpression
import org.jetbrains.uast.UContinueExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.kotlin.KotlinAbstractUExpression
class KotlinUContinueExpression(
override val sourcePsi: KtContinueExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UContinueExpression {
override val label: String?
get() = sourcePsi.getLabelName()
}
@@ -1,36 +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.KtDoWhileExpression
import org.jetbrains.uast.UDoWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
class KotlinUDoWhileExpression(
override val sourcePsi: KtDoWhileExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UDoWhileExpression {
override val condition by lz { KotlinConverter.convertOrEmpty(sourcePsi.condition, this) }
override val body by lz { KotlinConverter.convertOrEmpty(sourcePsi.body, this) }
override val doIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
@@ -1,33 +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.KtWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UWhileExpression
class KotlinUWhileExpression(
override val sourcePsi: KtWhileExpression,
givenParent: UElement?
) : KotlinAbstractUExpression(givenParent), UWhileExpression {
override val condition by lz { KotlinConverter.convertOrEmpty(sourcePsi.condition, this) }
override val body by lz { KotlinConverter.convertOrEmpty(sourcePsi.body, this) }
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}