Uast: constructors calls consistency for KtObjectLiteralExpression (KT-21409)

This commit is contained in:
Nicolay Mitropolsky
2017-11-27 13:11:20 +03:00
committed by xiexed
parent 1dbce18afc
commit 4db5ca66e3
5 changed files with 66 additions and 8 deletions
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.uast.*
@@ -312,7 +313,11 @@ internal object KotlinConverter {
is KtConstructorDelegationCall ->
el<UCallExpression> { KotlinUFunctionCallExpression(element, givenParent) }
is KtSuperTypeCallEntry ->
el<UCallExpression> { KotlinUFunctionCallExpression(element, givenParent) }
el<UExpression> {
(element.getParentOfType<KtClassOrObject>(true)?.parent as? KtObjectLiteralExpression)
?.toUElementOfType<UExpression>()
?: KotlinUFunctionCallExpression(element, givenParent)
}
else -> {
if (element is LeafPsiElement && element.elementType == KtTokens.IDENTIFIER) {
@@ -18,6 +18,7 @@ package org.jetbrains.uast.kotlin
import com.intellij.psi.*
import org.jetbrains.kotlin.asJava.classes.KtLightClass
import org.jetbrains.kotlin.asJava.classes.KtLightClassForLocalDeclaration
import org.jetbrains.kotlin.asJava.classes.KtLightClassForScript
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.load.java.JvmAbi
@@ -31,9 +32,17 @@ import org.jetbrains.uast.kotlin.declarations.KotlinUMethod
import org.jetbrains.uast.kotlin.declarations.UastLightIdentifier
abstract class AbstractKotlinUClass(private val givenParent: UElement?) : AbstractJavaUClass() {
override val uastParent: UElement? by lz {
givenParent ?: KotlinUastLanguagePlugin().convertElementWithParent(psi.parent ?: psi.containingFile, null)
}
override val uastParent: UElement? by lz { givenParent ?: convertParent() }
//TODO: should be merged with KotlinAbstractUElement.convertParent() after detaching from AbstractJavaUClass
open fun convertParent(): UElement? =
(this.psi as? KtLightClassForLocalDeclaration)?.kotlinOrigin?.parent?.let {
when (it) {
is KtClassBody -> it.parent.toUElement() // TODO: it seems that `class_body`-s are never created in kotlin uast in top-down walk, probably they should be completely skipped and always unwrapped
else -> it.toUElement()
}
} ?: (psi.parent ?: psi.containingFile).toUElement()
}
open class KotlinUClass private constructor(
@@ -109,7 +118,7 @@ class KotlinConstructorUMethod(
) : KotlinUMethod(psi, givenParent) {
val isPrimary: Boolean
get() = psi.kotlinOrigin.let { it is KtPrimaryConstructor || it is KtObjectDeclaration }
get() = psi.kotlinOrigin.let { it is KtPrimaryConstructor || it is KtClassOrObject }
override val uastBody: UExpression? by lz {
val delegationCall: KtCallElement? = psi.kotlinOrigin.let {
@@ -157,6 +166,7 @@ class KotlinUAnonymousClass(
val ktClassOrObject = (psi.originalElement as? KtLightClass)?.kotlinOrigin as? KtObjectDeclaration ?: return null
return UIdentifier(ktClassOrObject.getObjectKeyword(), this)
}
}
class KotlinScriptUClass(
+10 -2
View File
@@ -21,5 +21,13 @@ class C : A {
object O : A("text")
val anon = object : A("textForAnon") {
fun bar() {}
}
fun bar() {
cons(object : A("inner literal"))
}
object innerObject : A("inner object")
inner class InnerClass : A("inner class")
}
fun cons(a: A) {}
+27
View File
@@ -7,8 +7,35 @@ UFile (package = )
UClass (name = null)
UAnnotationMethod (name = bar)
UBlockExpression
UCallExpression (kind = UastCallKind(name='method_call'), argCount = 1))
UIdentifier (Identifier (cons))
USimpleNameReferenceExpression (identifier = cons)
UObjectLiteralExpression
ULiteralExpression (value = "inner literal")
UClass (name = null)
UAnnotationMethod (name = SuperCallsKt$anon$1$bar$1)
UAnnotationMethod (name = SuperCallsKt$anon$1)
UClass (name = innerObject)
UField (name = INSTANCE)
UAnnotation (fqName = null)
UAnnotationMethod (name = innerObject)
UBlockExpression
UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1))
UIdentifier (Identifier (A))
USimpleNameReferenceExpression (identifier = <init>)
ULiteralExpression (value = "inner object")
UClass (name = InnerClass)
UAnnotationMethod (name = InnerClass)
UBlockExpression
UCallExpression (kind = UastCallKind(name='constructor_call'), argCount = 1))
UIdentifier (Identifier (A))
USimpleNameReferenceExpression (identifier = <init>)
ULiteralExpression (value = "inner class")
UAnnotationMethod (name = getAnon)
UAnnotationMethod (name = cons)
UParameter (name = a)
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
UBlockExpression
UClass (name = A)
UField (name = str)
UAnnotation (fqName = org.jetbrains.annotations.NotNull)
+9 -1
View File
@@ -1,8 +1,16 @@
public final class SuperCallsKt {
private static final var anon: A = anonymous object : A("textForAnon") {
fun bar() {}
fun bar() {
cons(object : A("inner literal"))
}
object innerObject : A("inner object")
inner class InnerClass : A("inner class")
}
public static final fun getAnon() : A = UastEmptyExpression
public static final fun cons(a: A) : void {
}
}
public class A {