FIR/UAST: commonize Identifier implementations

This commit is contained in:
Jinseong Jeon
2021-05-17 15:14:47 -07:00
committed by Ilya Kirillov
parent 6af930f6df
commit 68766024b3
25 changed files with 32 additions and 116 deletions
@@ -1,20 +1,9 @@
/*
* Copyright 2010-2017 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.
* 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.declarations
package org.jetbrains.uast.kotlin
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.PsiElement
@@ -26,12 +15,15 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.kotlin.unwrapFakeFileForLightClass
import org.jetbrains.uast.toUElement
class UastLightIdentifier(lightOwner: PsiNameIdentifierOwner, ktDeclaration: KtDeclaration?) :
KtLightIdentifier(lightOwner, ktDeclaration) {
override fun getContainingFile(): PsiFile = unwrapFakeFileForLightClass(super.getContainingFile())
class UastLightIdentifier(
lightOwner: PsiNameIdentifierOwner,
ktDeclaration: KtDeclaration?
) : KtLightIdentifier(lightOwner, ktDeclaration) {
override fun getContainingFile(): PsiFile {
return unwrapFakeFileForLightClass(super.getContainingFile())
}
}
class KotlinUIdentifier constructor(
@@ -78,4 +70,4 @@ class KotlinUIdentifier constructor(
constructor(javaPsi: PsiElement?, sourcePsi: PsiElement?, uastParent: UElement?) : this({ javaPsi }, sourcePsi, uastParent)
constructor(sourcePsi: PsiElement?, uastParent: UElement?) : this({ null }, sourcePsi, uastParent)
}
}
@@ -201,11 +201,11 @@ internal object FirKotlinConverter {
if (element.elementType != KtTokens.OBJECT_KEYWORD ||
element.getParentOfType<KtObjectDeclaration>(false)?.nameIdentifier == null
)
el<UIdentifier>(build(::FirKotlinUIdentifier))
el<UIdentifier>(build(::KotlinUIdentifier))
else null
}
element.elementType in KtTokens.OPERATIONS && element.parent is KtOperationReferenceExpression -> {
el<UIdentifier>(build(::FirKotlinUIdentifier))
el<UIdentifier>(build(::KotlinUIdentifier))
}
element.elementType == KtTokens.LBRACKET && element.parent is KtCollectionLiteralExpression -> {
// TODO: need counterpart of UCollectionLiteralExpression
@@ -75,13 +75,13 @@ class FirKotlinUClass(
override fun getOriginalElement(): PsiElement? = sourcePsi?.originalElement
override fun getNameIdentifier(): PsiIdentifier = FirUastLightIdentifier(psi, ktClass)
override fun getNameIdentifier(): PsiIdentifier = UastLightIdentifier(psi, ktClass)
override fun getContainingFile(): PsiFile = unwrapFakeFileForLightClass(psi.containingFile)
override val uastAnchor: UIdentifier? by lz {
getIdentifierSourcePsi()?.let {
FirKotlinUIdentifier(nameIdentifier, it, this)
KotlinUIdentifier(nameIdentifier, it, this)
}
}
@@ -1,55 +0,0 @@
/*
* 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 com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiNameIdentifierOwner
import org.jetbrains.kotlin.asJava.elements.KtLightIdentifier
import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtConstructorCalleeExpression
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtTypeReference
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.toUElement
class FirUastLightIdentifier(
lightOwner: PsiNameIdentifierOwner,
ktDeclaration: KtDeclaration?
) : KtLightIdentifier(lightOwner, ktDeclaration) {
override fun getContainingFile(): PsiFile {
return unwrapFakeFileForLightClass(super.getContainingFile())
}
}
class FirKotlinUIdentifier(
override val javaPsi: PsiElement? = null,
override val sourcePsi: PsiElement?,
givenParent: UElement?
) : UIdentifier(sourcePsi, givenParent) {
override val psi: PsiElement?
get() = javaPsi ?: sourcePsi
override val uastParent: UElement? by lz {
if (givenParent != null) return@lz givenParent
val parent = sourcePsi?.parent ?: return@lz null
getIdentifierParentForCall(parent) ?: parent.toUElement()
}
private fun getIdentifierParentForCall(parent: PsiElement): UElement? {
val parentParent = parent.parent
if (parentParent is KtCallElement && parentParent.calleeExpression == parent) { // method identifiers in calls
return parentParent.toUElement()
}
if (parentParent is KtTypeReference && parentParent.parent is KtConstructorCalleeExpression ) {
return parentParent.parent.toUElement()
}
return null
}
constructor(sourcePsi: PsiElement?, uastParent: UElement?) : this(null, sourcePsi, uastParent)
}
@@ -61,7 +61,7 @@ open class FirKotlinUMethod(
is KtPropertyAccessor -> sourcePsi.namePlaceholder
else -> sourcePsi?.navigationElement
}
FirKotlinUIdentifier(nameIdentifier, identifierSourcePsi, this)
KotlinUIdentifier(nameIdentifier, identifierSourcePsi, this)
}
override val uastBody: UExpression? by lz {
@@ -139,7 +139,7 @@ class FirKotlinConstructorUMethod(
get() = sourcePsi is KtPrimaryConstructor || sourcePsi is KtClassOrObject
override val uastAnchor: UIdentifier? by lz {
FirKotlinUIdentifier(
KotlinUIdentifier(
javaPsi.nameIdentifier,
if (isPrimary) ktClass?.nameIdentifier else (sourcePsi as? KtSecondaryConstructor)?.getConstructorKeyword(),
this
@@ -23,7 +23,7 @@ sealed class AbstractFirKotlinUVariable(
override fun getNameIdentifier(): PsiIdentifier {
val kotlinOrigin = (sourcePsi as? KtLightElement<*, *>)?.kotlinOrigin
return FirUastLightIdentifier(psi, kotlinOrigin as? KtDeclaration)
return UastLightIdentifier(psi, kotlinOrigin as? KtDeclaration)
}
override fun getContainingFile(): PsiFile = unwrapFakeFileForLightClass(psi.containingFile)
@@ -41,7 +41,7 @@ sealed class AbstractFirKotlinUVariable(
is KtLambdaExpression -> sourcePsi.functionLiteral.lBrace
else -> sourcePsi
} ?: return@lz null
FirKotlinUIdentifier(nameIdentifier, identifierSourcePsi, this)
KotlinUIdentifier(nameIdentifier, identifierSourcePsi, this)
}
override val typeReference: UTypeReferenceExpression? by lz {
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.uast.*
import org.jetbrains.uast.expressions.UInjectionHost
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.declarations.KotlinUMethod
import org.jetbrains.uast.kotlin.declarations.KotlinUMethodWithFakeLightDelegate
import org.jetbrains.uast.kotlin.expressions.*
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.declarations.KotlinUMethod
import org.jetbrains.uast.kotlin.internal.multiResolveResults
@@ -30,9 +30,7 @@ import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.uast.*
import org.jetbrains.uast.internal.acceptList
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.declarations.KotlinUMethod
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
import org.jetbrains.uast.kotlin.kinds.KotlinSpecialExpressionKinds
import org.jetbrains.uast.visitor.UastVisitor
@@ -35,8 +35,6 @@ import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.uast.*
import org.jetbrains.uast.internal.acceptList
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
@@ -475,4 +473,4 @@ class KotlinUEnumConstant(
override fun getArgumentForParameter(i: Int): UExpression? = valueArguments.getOrNull(i)
}
}
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.types.CommonSupertypes
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.kinds.KotlinSpecialExpressionKinds
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinUBinaryExpression(
override val sourcePsi: KtBinaryExpression,
@@ -107,4 +106,4 @@ class KotlinCustomUBinaryExpression(
get() = null
override fun resolveOperator() = null
}
}
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.psi.KtDoWhileExpression
import org.jetbrains.uast.UDoWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinUDoWhileExpression(
override val sourcePsi: KtDoWhileExpression,
@@ -34,4 +33,4 @@ class KotlinUDoWhileExpression(
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
}
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UForEachExpression
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
import org.jetbrains.uast.psi.UastPsiParameterNotResolved
@@ -40,4 +39,4 @@ class KotlinUForEachExpression(
override val forIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
}
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.uast.*
import org.jetbrains.uast.internal.acceptList
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.TypedResolveResult
import org.jetbrains.uast.kotlin.internal.getReferenceVariants
import org.jetbrains.uast.visitor.UastVisitor
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.psi.KtLabeledExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.ULabeledExpression
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinULabeledExpression(
override val sourcePsi: KtLabeledExpression,
@@ -33,4 +32,4 @@ class KotlinULabeledExpression(
get() = sourcePsi.getTargetLabel()?.let { KotlinUIdentifier(it, this) }
override val expression by lz { KotlinConverter.convertOrEmpty(sourcePsi.baseExpression, this) }
}
}
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.psi.KtObjectLiteralExpression
import org.jetbrains.kotlin.psi.KtSuperTypeCallEntry
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
class KotlinUObjectLiteralExpression(
@@ -90,4 +89,4 @@ class KotlinUObjectLiteralExpression(
get() = KotlinUIdentifier(psi.typeReference?.nameElement, this)
}
}
}
@@ -20,7 +20,6 @@ import com.intellij.psi.PsiMethod
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtPostfixExpression
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
class KotlinUPostfixExpression(
@@ -46,4 +45,4 @@ class KotlinUPostfixExpression(
KtTokens.EXCLEXCL -> operand.tryResolve() as? PsiMethod
else -> null
}
}
}
@@ -23,7 +23,6 @@ import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UPrefixExpression
import org.jetbrains.uast.UastPrefixOperator
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinUPrefixExpression(
override val sourcePsi: KtPrefixExpression,
@@ -44,4 +43,4 @@ class KotlinUPrefixExpression(
KtTokens.MINUSMINUS -> UastPrefixOperator.DEC
else -> UastPrefixOperator.UNKNOWN
}
}
}
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
import org.jetbrains.uast.*
import org.jetbrains.uast.internal.acceptList
import org.jetbrains.uast.internal.log
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
import org.jetbrains.uast.visitor.UastVisitor
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.USuperExpression
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
class KotlinUSuperExpression(
@@ -35,4 +34,4 @@ class KotlinUSuperExpression(
get() = sourcePsi.getTargetLabel()?.let { KotlinUIdentifier(it, this) }
override fun resolve() = sourcePsi.analyze()[BindingContext.LABEL_TARGET, sourcePsi.getTargetLabel()]
}
}
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi.KtWhenEntry
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.kinds.KotlinSpecialExpressionKinds
class KotlinUSwitchExpression(
@@ -99,4 +98,4 @@ class KotlinUSwitchEntry(
}
return (result as? KotlinUSwitchExpression)?.body ?: result
}
}
}
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UThisExpression
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
import org.jetbrains.uast.kotlin.internal.DelegatedMultiResolve
class KotlinUThisExpression(
@@ -35,4 +34,4 @@ class KotlinUThisExpression(
get() = sourcePsi.getTargetLabel()?.let { KotlinUIdentifier(it, this) }
override fun resolve() = sourcePsi.analyze()[BindingContext.LABEL_TARGET, sourcePsi.getTargetLabel()]
}
}
@@ -18,7 +18,6 @@ package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.psi.KtTryExpression
import org.jetbrains.uast.*
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinUTryExpression(
override val sourcePsi: KtTryExpression,
@@ -47,4 +46,4 @@ class KotlinUTryExpression(
override val finallyIdentifier: UIdentifier?
get() = null
}
}
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.psi.KtWhileExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UWhileExpression
import org.jetbrains.uast.kotlin.declarations.KotlinUIdentifier
class KotlinUWhileExpression(
override val sourcePsi: KtWhileExpression,
@@ -31,4 +30,4 @@ class KotlinUWhileExpression(
override val whileIdentifier: UIdentifier
get() = KotlinUIdentifier(null, this)
}
}