FIR/UAST: commonize KotlinEvaluatableUElement

This commit is contained in:
Jinseong Jeon
2021-05-25 00:03:41 -07:00
committed by Ilya Kirillov
parent ce10410aba
commit 6b5bddeed7
5 changed files with 31 additions and 34 deletions
@@ -19,4 +19,6 @@ interface BaseKotlinUastResolveProviderService {
fun resolveToDeclaration(ktExpression: KtExpression): PsiElement?
fun getExpressionType(uExpression: UExpression): PsiType?
fun evaluate(uExpression: UExpression): Any?
}
@@ -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 org.jetbrains.uast.UExpression
interface KotlinEvaluatableUElement : UExpression {
val baseResolveProviderService: BaseKotlinUastResolveProviderService
override fun evaluate(): Any? {
return baseResolveProviderService.evaluate(this)
}
}
@@ -35,4 +35,8 @@ interface FirKotlinUastResolveProviderService : BaseKotlinUastResolveProviderSer
override fun getExpressionType(uExpression: UExpression): PsiType? {
TODO("Not yet implemented")
}
override fun evaluate(uExpression: UExpression): Any? {
TODO("Not yet implemented")
}
}
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.constants.UnsignedErrorValueTypeConstant
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UExpression
@@ -35,4 +37,11 @@ interface KotlinUastResolveProviderService : BaseKotlinUastResolveProviderServic
val ktType = ktElement.analyze()[BindingContext.EXPRESSION_TYPE_INFO, ktElement]?.type ?: return null
return ktType.toPsiType(uExpression, ktElement, boxed = false)
}
override fun evaluate(uExpression: UExpression): Any? {
val ktElement = uExpression.sourcePsi as? KtExpression ?: return null
val compileTimeConst = ktElement.analyze()[BindingContext.COMPILE_TIME_VALUE, ktElement]
if (compileTimeConst is UnsignedErrorValueTypeConstant) return null
return compileTimeConst?.getValue(TypeUtils.NO_EXPECTED_TYPE)
}
}
@@ -1,34 +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.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.constants.UnsignedErrorValueTypeConstant
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.uast.UExpression
interface KotlinEvaluatableUElement : UExpression {
override fun evaluate(): Any? {
val ktElement = sourcePsi as? KtExpression ?: return null
val compileTimeConst = ktElement.analyze()[BindingContext.COMPILE_TIME_VALUE, ktElement]
if (compileTimeConst is UnsignedErrorValueTypeConstant) return null
return compileTimeConst?.getValue(TypeUtils.NO_EXPECTED_TYPE)
}
}