FIR/UAST: commonize abstract UElement and UExpression

This commit is contained in:
Jinseong Jeon
2021-05-13 14:01:00 -07:00
committed by Ilya Kirillov
parent a54a807dc9
commit 3c3b5aa4ac
20 changed files with 188 additions and 161 deletions
@@ -0,0 +1,16 @@
/*
* 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 org.jetbrains.uast.UElement
interface BaseKotlinUastResolveProviderService {
fun isJvmElement(psiElement: PsiElement): Boolean
fun convertParent(uElement: UElement): UElement?
}
@@ -0,0 +1,45 @@
/*
* 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.openapi.components.ServiceManager
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UastFacade
import org.jetbrains.uast.UastLanguagePlugin
import org.jetbrains.uast.kotlin.internal.KotlinUElementWithComments
abstract class KotlinAbstractUElement(
givenParent: UElement?
) : KotlinUElementWithComments {
protected val languagePlugin: UastLanguagePlugin? by lz {
psi?.let { UastFacade.findPlugin(it) }
}
protected val baseResolveProviderService: BaseKotlinUastResolveProviderService? by lz {
psi?.project?.let { ServiceManager.getService(it, BaseKotlinUastResolveProviderService::class.java) }
}
final override val uastParent: UElement? by lz {
givenParent ?: convertParent()
}
protected open fun convertParent(): UElement? {
return baseResolveProviderService?.convertParent(this)
}
override fun equals(other: Any?): Boolean {
if (other !is UElement) {
return false
}
return this.psi == other.psi
}
override fun hashCode(): Int {
return psi?.hashCode() ?: 0
}
}
@@ -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 com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtAnnotatedExpression
import org.jetbrains.uast.UAnnotation
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UExpression
import org.jetbrains.uast.convertOpt
abstract class KotlinAbstractUExpression(
givenParent: UElement?
) : KotlinAbstractUElement(givenParent), UExpression {
override val javaPsi: PsiElement? = null
override val psi
get() = sourcePsi
override val uAnnotations: List<UAnnotation>
get() {
val annotatedExpression = sourcePsi?.parent as? KtAnnotatedExpression ?: return emptyList()
return annotatedExpression.annotationEntries.mapNotNull { languagePlugin?.convertOpt(it, this) }
}
}