From c089b8ed045a626dfdfe4eda475471615c879294 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Thu, 27 May 2021 00:29:01 -0700 Subject: [PATCH] FIR/UAST: commonize labeled expression --- .../expressions/KotlinULabeledExpression.kt | 26 ++++++++++++++ .../internal/baseKotlinInternalUastUtils.kt | 3 ++ .../uast/kotlin/FirKotlinConverter.kt | 2 ++ .../testData/declaration/doWhile.kt | 12 +++++++ .../testData/declaration/doWhile.log.fe10.txt | 30 ++++++++++++++++ .../testData/declaration/doWhile.log.fir.txt | 20 +++++++++++ .../declaration/doWhile.render.fe10.txt | 12 +++++++ .../declaration/doWhile.render.fir.txt | 12 +++++++ .../expressions/KotlinULabeledExpression.kt | 35 ------------------- .../internal/kotlinInternalUastUtils.kt | 3 -- 10 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt delete mode 100644 plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt new file mode 100644 index 00000000000..9ae62204376 --- /dev/null +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt @@ -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.KtLabeledExpression +import org.jetbrains.uast.UElement +import org.jetbrains.uast.UIdentifier +import org.jetbrains.uast.ULabeledExpression + +class KotlinULabeledExpression( + override val sourcePsi: KtLabeledExpression, + givenParent: UElement? +) : KotlinAbstractUExpression(givenParent), ULabeledExpression { + override val label: String + get() = sourcePsi.getLabelName().orAnonymous("label") + + override val labelIdentifier: UIdentifier? + get() = sourcePsi.getTargetLabel()?.let { KotlinUIdentifier(it, this) } + + override val expression by lz { + baseResolveProviderService.baseKotlinConverter.convertOrEmpty(sourcePsi.baseExpression, this) + } +} diff --git a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt index 7fa4ead90e5..031ec5f0824 100644 --- a/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt +++ b/plugins/uast-kotlin-base/src/org/jetbrains/uast/kotlin/internal/baseKotlinInternalUastUtils.kt @@ -17,6 +17,9 @@ import java.util.function.Supplier typealias BaseResolveProviderServiceSupplier = Supplier +@Suppress("NOTHING_TO_INLINE") +inline fun String?.orAnonymous(kind: String = ""): String = this ?: "" + fun lz(initializer: () -> T) = lazy(LazyThreadSafetyMode.SYNCHRONIZED, initializer) diff --git a/plugins/uast-kotlin-fir/src/org/jetbrains/uast/kotlin/FirKotlinConverter.kt b/plugins/uast-kotlin-fir/src/org/jetbrains/uast/kotlin/FirKotlinConverter.kt index 6c8893549db..b7b4b3f1689 100644 --- a/plugins/uast-kotlin-fir/src/org/jetbrains/uast/kotlin/FirKotlinConverter.kt +++ b/plugins/uast-kotlin-fir/src/org/jetbrains/uast/kotlin/FirKotlinConverter.kt @@ -269,6 +269,8 @@ internal object FirKotlinConverter : BaseKotlinConverter { is KtCollectionLiteralExpression -> expr(build(::KotlinUCollectionLiteralExpression)) is KtConstantExpression -> expr(build(::KotlinULiteralExpression)) + is KtLabeledExpression -> expr(build(::KotlinULabeledExpression)) + is KtBlockExpression -> expr { // TODO: differentiate lambda FirKotlinUBlockExpression(expression, givenParent) diff --git a/plugins/uast-kotlin-fir/testData/declaration/doWhile.kt b/plugins/uast-kotlin-fir/testData/declaration/doWhile.kt index bbba9ea94f7..4b95f504c3a 100644 --- a/plugins/uast-kotlin-fir/testData/declaration/doWhile.kt +++ b/plugins/uast-kotlin-fir/testData/declaration/doWhile.kt @@ -4,3 +4,15 @@ fun test() { x = "non-null" } while (x != null) } + +fun kt44412() { + var i = 0 + Outer@while (true) { + ++i + var j = 0 + Inner@do { + ++j + } while (if (j >= 3) false else break) // break@Inner + if (i == 3) break + } +} diff --git a/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fe10.txt b/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fe10.txt index 0a43dadcba7..6d65ab5bace 100644 --- a/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fe10.txt +++ b/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fe10.txt @@ -13,3 +13,33 @@ UFile (package = ) UBinaryExpression (operator = =) USimpleNameReferenceExpression (identifier = x) ULiteralExpression (value = "non-null") + UMethod (name = kt44412) + UBlockExpression + UDeclarationsExpression + ULocalVariable (name = i) + ULiteralExpression (value = 0) + ULabeledExpression (label = Outer) + UWhileExpression + ULiteralExpression (value = true) + UBlockExpression + UPrefixExpression (operator = ++) + USimpleNameReferenceExpression (identifier = i) + UDeclarationsExpression + ULocalVariable (name = j) + ULiteralExpression (value = 0) + ULabeledExpression (label = Inner) + UDoWhileExpression + UIfExpression + UBinaryExpression (operator = >=) + USimpleNameReferenceExpression (identifier = j) + ULiteralExpression (value = 3) + ULiteralExpression (value = false) + UBreakExpression (label = null) + UBlockExpression + UPrefixExpression (operator = ++) + USimpleNameReferenceExpression (identifier = j) + UIfExpression + UBinaryExpression (operator = ==) + USimpleNameReferenceExpression (identifier = i) + ULiteralExpression (value = 3) + UBreakExpression (label = null) diff --git a/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fir.txt b/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fir.txt index 3bf26b297d9..211db2ff0dd 100644 --- a/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fir.txt +++ b/plugins/uast-kotlin-fir/testData/declaration/doWhile.log.fir.txt @@ -7,3 +7,23 @@ UFile (package = ) [!] UnknownKotlinExpression (BINARY_EXPRESSION) UBlockExpression [!] UnknownKotlinExpression (BINARY_EXPRESSION) + UMethod (name = kt44412) + UBlockExpression + [!] UnknownKotlinExpression (PROPERTY) + ULabeledExpression (label = Outer) + UWhileExpression + ULiteralExpression (value = "not-yet-compile-time-constant") + UBlockExpression + [!] UnknownKotlinExpression (PREFIX_EXPRESSION) + [!] UnknownKotlinExpression (PROPERTY) + ULabeledExpression (label = Inner) + UDoWhileExpression + UIfExpression + [!] UnknownKotlinExpression (BINARY_EXPRESSION) + ULiteralExpression (value = "not-yet-compile-time-constant") + UBreakExpression (label = null) + UBlockExpression + [!] UnknownKotlinExpression (PREFIX_EXPRESSION) + UIfExpression + [!] UnknownKotlinExpression (BINARY_EXPRESSION) + UBreakExpression (label = null) diff --git a/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fe10.txt b/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fe10.txt index 9d2cc9b6c70..71721a5dca2 100644 --- a/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fe10.txt +++ b/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fe10.txt @@ -6,4 +6,16 @@ public final class DoWhileKt { }while (x != null) } + public static final fun kt44412() : void { + var i: int = 0 + Outer@ while (true) { + ++i + var j: int = 0 + Inner@ do { + ++j + }while (if (j >= 3) false else break) + + if (i == 3) break + } + } } diff --git a/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fir.txt b/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fir.txt index 8dfad75ea50..9578a952cf0 100644 --- a/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fir.txt +++ b/plugins/uast-kotlin-fir/testData/declaration/doWhile.render.fir.txt @@ -6,4 +6,16 @@ public final class DoWhileKt { }while ([!] UnknownKotlinExpression (BINARY_EXPRESSION)) } + public static final fun kt44412() : void { + [!] UnknownKotlinExpression (PROPERTY) + Outer@ while ("not-yet-compile-time-constant") { + [!] UnknownKotlinExpression (PREFIX_EXPRESSION) + [!] UnknownKotlinExpression (PROPERTY) + Inner@ do { + [!] UnknownKotlinExpression (PREFIX_EXPRESSION) + }while (if ([!] UnknownKotlinExpression (BINARY_EXPRESSION)) "not-yet-compile-time-constant" else break) + + if ([!] UnknownKotlinExpression (BINARY_EXPRESSION)) break + } + } } diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt deleted file mode 100644 index 3ef2ad172a3..00000000000 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/expressions/KotlinULabeledExpression.kt +++ /dev/null @@ -1,35 +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.KtLabeledExpression -import org.jetbrains.uast.UElement -import org.jetbrains.uast.UIdentifier -import org.jetbrains.uast.ULabeledExpression - -class KotlinULabeledExpression( - override val sourcePsi: KtLabeledExpression, - givenParent: UElement? -) : KotlinAbstractUExpression(givenParent), ULabeledExpression { - override val label: String - get() = sourcePsi.getLabelName().orAnonymous("label") - - override val labelIdentifier: UIdentifier? - get() = sourcePsi.getTargetLabel()?.let { KotlinUIdentifier(it, this) } - - override val expression by lz { KotlinConverter.convertOrEmpty(sourcePsi.baseExpression, this) } -} diff --git a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt index 5ed72c1247b..356854e980a 100644 --- a/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt +++ b/plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/internal/kotlinInternalUastUtils.kt @@ -89,9 +89,6 @@ val kotlinUastPlugin: UastLanguagePlugin by lz { internal val KOTLIN_CACHED_UELEMENT_KEY = Key.create>("cached-kotlin-uelement") -@Suppress("NOTHING_TO_INLINE") -internal inline fun String?.orAnonymous(kind: String = ""): String = this ?: "" - internal fun getContainingLightClass(original: KtDeclaration): KtLightClass? = (original.containingClassOrObject?.toLightClass() ?: original.containingKtFile.findFacadeClass())