FIR/UAST: commonize labeled expression
This commit is contained in:
committed by
Ilya Kirillov
parent
6e1b04e4c9
commit
c089b8ed04
+26
@@ -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)
|
||||
}
|
||||
}
|
||||
+3
@@ -17,6 +17,9 @@ import java.util.function.Supplier
|
||||
|
||||
typealias BaseResolveProviderServiceSupplier = Supplier<BaseKotlinUastResolveProviderService>
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun String?.orAnonymous(kind: String = ""): String = this ?: "<anonymous" + (if (kind.isNotBlank()) " $kind" else "") + ">"
|
||||
|
||||
fun <T> lz(initializer: () -> T) =
|
||||
lazy(LazyThreadSafetyMode.SYNCHRONIZED, initializer)
|
||||
|
||||
|
||||
@@ -269,6 +269,8 @@ internal object FirKotlinConverter : BaseKotlinConverter {
|
||||
is KtCollectionLiteralExpression -> expr<UCallExpression>(build(::KotlinUCollectionLiteralExpression))
|
||||
is KtConstantExpression -> expr<ULiteralExpression>(build(::KotlinULiteralExpression))
|
||||
|
||||
is KtLabeledExpression -> expr<ULabeledExpression>(build(::KotlinULabeledExpression))
|
||||
|
||||
is KtBlockExpression -> expr<UBlockExpression> {
|
||||
// TODO: differentiate lambda
|
||||
FirKotlinUBlockExpression(expression, givenParent)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-35
@@ -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) }
|
||||
}
|
||||
@@ -89,9 +89,6 @@ val kotlinUastPlugin: UastLanguagePlugin by lz {
|
||||
|
||||
internal val KOTLIN_CACHED_UELEMENT_KEY = Key.create<WeakReference<UElement>>("cached-kotlin-uelement")
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
internal inline fun String?.orAnonymous(kind: String = ""): String = this ?: "<anonymous" + (if (kind.isNotBlank()) " $kind" else "") + ">"
|
||||
|
||||
internal fun getContainingLightClass(original: KtDeclaration): KtLightClass? =
|
||||
(original.containingClassOrObject?.toLightClass() ?: original.containingKtFile.findFacadeClass())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user