Revert "Revert "[light classes] drop old light classes and backend: iteration #2""

This reverts commit 43b4df74bc.
This commit is contained in:
Dmitry Gridin
2022-06-28 17:57:30 +02:00
parent f10c5c28a5
commit dd824aacb6
42 changed files with 79 additions and 103 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2022 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.
*/
@@ -18,7 +18,6 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
class FirFakeFileImpl(private val classOrObject: KtClassOrObject, ktClass: KtLightClass) : FakeFileForLightClass(
classOrObject.containingKtFile,
{ if (classOrObject.isTopLevel()) ktClass else getOrCreateFirLightClass(getOutermostClassOrObject(classOrObject))!! },
{ null }
) {
override fun findReferenceAt(offset: Int) = ktFile.findReferenceAt(offset)
@@ -172,7 +172,6 @@ class FirLightClassForFacade(
private val packageClsFile = FakeFileForLightClass(
firstFileInFacade,
lightClass = { this },
stub = { null },
packageFqName = packageFqName
)
@@ -97,21 +97,21 @@ class CliLightClassGenerationSupport(
//force resolve companion for light class generation
traceHolder.bindingContext.get(BindingContext.CLASS, classOrObject)?.companionObjectDescriptor
val (stub, bindingContext, diagnostics) = builder(getContext())
val (_, bindingContext, diagnostics) = builder(getContext())
bindingContext.get(BindingContext.CLASS, classOrObject) ?: return InvalidLightClassDataHolder
return LightClassDataHolderImpl(stub, diagnostics)
return LightClassDataHolderImpl(diagnostics)
}
override fun createDataHolderForFacade(files: Collection<KtFile>, builder: LightClassBuilder): LightClassDataHolder.ForFacade {
val (stub, _, diagnostics) = builder(getContext())
return LightClassDataHolderImpl(stub, diagnostics)
val (_, _, diagnostics) = builder(getContext())
return LightClassDataHolderImpl(diagnostics)
}
override fun createDataHolderForScript(script: KtScript, builder: LightClassBuilder): LightClassDataHolder.ForScript {
val (stub, _, diagnostics) = builder(getContext())
return LightClassDataHolderImpl(stub, diagnostics)
val (_, _, diagnostics) = builder(getContext())
return LightClassDataHolderImpl(diagnostics)
}
private fun getContext(): LightClassConstructionContext =
@@ -5,11 +5,9 @@
package org.jetbrains.kotlin.asJava.builder
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
interface LightClassDataHolder {
val javaFileStub: PsiJavaFileStub
val extraDiagnostics: Diagnostics
interface ForClass : LightClassDataHolder
@@ -18,16 +16,11 @@ interface LightClassDataHolder {
}
object InvalidLightClassDataHolder : LightClassDataHolder.ForClass {
override val javaFileStub: PsiJavaFileStub
get() = shouldNotBeCalled()
override val extraDiagnostics: Diagnostics
get() = shouldNotBeCalled()
override val extraDiagnostics: Diagnostics get() = shouldNotBeCalled()
private fun shouldNotBeCalled(): Nothing = throw UnsupportedOperationException("Should not be called")
}
class LightClassDataHolderImpl(
override val javaFileStub: PsiJavaFileStub,
override val extraDiagnostics: Diagnostics
) : LightClassDataHolder.ForClass, LightClassDataHolder.ForFacade, LightClassDataHolder.ForScript
@@ -47,7 +47,6 @@ abstract class KtLightClassForFacadeImpl constructor(
FakeFileForLightClass(
firstFileInFacade,
lightClass = { this },
stub = { null },
packageFqName = facadeClassFqName.parent()
)
}
@@ -43,7 +43,6 @@ abstract class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(sc
FakeFileForLightClass(
script.containingKtFile,
lightClass = { this },
stub = { null },
packageFqName = fqName.parent(),
)
}
@@ -12,7 +12,6 @@ import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.impl.InheritanceImplUtil
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub
import com.intellij.psi.impl.source.PsiImmediateClassType
import com.intellij.psi.impl.source.tree.TreeUtil
import com.intellij.psi.scope.PsiScopeProcessor
@@ -44,7 +43,6 @@ import org.jetbrains.kotlin.load.java.structure.LightClassOriginKind
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.debugText.getDebugText
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub
@@ -99,24 +97,13 @@ abstract class KtLightClassForSourceDeclaration(
abstract override fun getParent(): PsiElement?
abstract override fun getQualifiedName(): String?
private fun getJavaFileStub(): PsiJavaFileStub = getLightClassDataHolder().javaFileStub
fun getDescriptor() =
LightClassGenerationSupport.getInstance(project).resolveToDescriptor(classOrObject) as? ClassDescriptor
protected fun getLightClassDataHolder(): LightClassDataHolder.ForClass {
val lightClassData = getLightClassDataHolder(classOrObject)
if (lightClassData is InvalidLightClassDataHolder) {
LOG.error("Invalid light class data for existing light class:\n$lightClassData\n${classOrObject.getElementTextWithContext()}")
}
return lightClassData
}
private val _containingFile: PsiFile by lazyPub {
object : FakeFileForLightClass(
classOrObject.containingKtFile,
{ if (classOrObject.isTopLevel()) this else create(getOutermostClassOrObject(classOrObject))!! },
{ getJavaFileStub() }
) {
override fun findReferenceAt(offset: Int) = ktFile.findReferenceAt(offset)
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.asJava.classes
import com.intellij.psi.*
import com.intellij.psi.impl.PsiImplUtil
import com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl
import com.intellij.psi.impl.light.LightIdentifier
import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
@@ -47,7 +48,11 @@ class KtUltraLightSimpleAnnotation(
parent: PsiElement,
private val nameReferenceElementProvider: (() -> PsiJavaCodeReferenceElement?)? = null,
) : KtLightAbstractAnnotation(parent) {
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = nameReferenceElementProvider?.invoke()
private val _nameReferenceElement: PsiJavaCodeReferenceElement? by lazyPub {
nameReferenceElementProvider?.invoke() ?: annotationFqName?.let { ClsJavaCodeReferenceElementImpl(parent, it) }
}
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = _nameReferenceElement
private val parameterList = ParameterListImpl()
@@ -69,9 +69,6 @@ internal class KtUltraLightFieldForSourceDeclaration(
private val lightIdentifier = KtLightIdentifier(this, declaration)
override fun getNameIdentifier(): PsiIdentifier = lightIdentifier
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
override fun getStartOffsetInParent(): Int = kotlinOrigin.startOffsetInParent
override fun isWritable(): Boolean = kotlinOrigin.isWritable
override fun getNavigationElement(): PsiElement = kotlinOrigin.navigationElement ?: this
@@ -106,6 +103,10 @@ internal open class KtUltraLightFieldImpl protected constructor(
override fun getLanguage(): Language = KotlinLanguage.INSTANCE
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange? = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
private val variableDescriptor: VariableDescriptor?
get() = declaration.resolve()
?.let { it as? PropertyDescriptor ?: it as? ValueParameterDescriptor }
@@ -116,11 +117,14 @@ internal open class KtUltraLightFieldImpl protected constructor(
declaration.delegateExpression?.let {
LightClassGenerationSupport.getInstance(project).analyze(it).getType(it)
}
declaration is KtObjectDeclaration ->
(declaration.resolve() as? ClassDescriptor)?.defaultType
declaration is KtEnumEntry -> {
(containingClass.kotlinOrigin?.resolve() as? ClassDescriptor)?.defaultType
}
else -> {
declaration.getKotlinType()
}
@@ -145,6 +149,7 @@ internal open class KtUltraLightFieldImpl protected constructor(
kotlinType?.asPsiType(support, TypeMappingMode.DEFAULT, this)
?.let(TypeConversionUtil::erasure)
?: nonExistent()
else -> {
val kotlinType = declaration.getKotlinType() ?: return@lazyPub PsiType.NULL
val descriptor = variableDescriptor ?: return@lazyPub PsiType.NULL
@@ -90,8 +90,9 @@ internal abstract class KtUltraLightParameter(
ultraLightMethod,
ultraLightMethod.language
), KtUltraLightElementWithNullabilityAnnotation<KtParameter, PsiParameter>, KtLightParameter {
override fun isEquivalentTo(another: PsiElement?): Boolean = kotlinOrigin == another
override fun isEquivalentTo(another: PsiElement?): Boolean {
return another is KtParameter && kotlinOrigin?.isEquivalentTo(another) == true || this == another
}
private val lightModifierList by lazyPub { KtLightSimpleModifierList(this, emptySet()) }
@@ -100,6 +101,10 @@ internal abstract class KtUltraLightParameter(
override fun getNavigationElement(): PsiElement = kotlinOrigin ?: method.navigationElement
override fun getUseScope(): SearchScope = kotlinOrigin?.useScope ?: LocalSearchScope(this)
override fun getText(): String? = kotlinOrigin?.text.orEmpty()
override fun getTextRange(): TextRange? = kotlinOrigin?.textRange
override fun getTextOffset(): Int = kotlinOrigin?.textOffset ?: super.getTextOffset()
override fun isValid() = parent.isValid
override fun computeQualifiedNameForNullabilityAnnotation(kotlinType: KotlinType?): String? {
@@ -198,9 +203,6 @@ internal class KtUltraLightParameterForSource(
}
}
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
override fun getStartOffsetInParent(): Int = kotlinOrigin.startOffsetInParent
override fun isWritable(): Boolean = kotlinOrigin.isWritable
override fun getNavigationElement(): PsiElement = kotlinOrigin.navigationElement
@@ -19,7 +19,6 @@ import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl
import com.intellij.psi.impl.source.PsiFileImpl
import com.intellij.psi.impl.source.SourceTreeToPsiMap
import com.intellij.psi.impl.source.tree.TreeElement
import com.intellij.psi.stubs.PsiClassHolderFileStub
import com.intellij.psi.util.PsiUtil
import com.intellij.reference.SoftReference
import com.intellij.util.AstLoadingFilter
@@ -33,8 +32,7 @@ import java.lang.ref.Reference
open class FakeFileForLightClass(
val ktFile: KtFile,
private val lightClass: () -> KtLightClass,
private val stub: () -> PsiClassHolderFileStub<*>?,
private val packageFqName: FqName = ktFile.packageFqName
private val packageFqName: FqName = ktFile.packageFqName,
) : ClsFileImpl(ktFile.viewProvider) {
override fun getVirtualFile(): VirtualFile =
@@ -49,7 +47,7 @@ open class FakeFileForLightClass(
return javaFileStub
}
override fun getStub() = stub() ?: createFakeJavaFileStub()
override fun getStub() = createFakeJavaFileStub()
override fun getClasses() = arrayOf(lightClass())
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.resolve.constants.ArrayValue
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.constants.KClassValue
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -91,11 +92,10 @@ internal fun PsiAnnotation.tryConvertAsTarget(support: KtUltraLightSupport): KtL
val targetAttributes = "value" to ArrayValue(convertedValues) { module -> module.builtIns.array.defaultType }
return KtUltraLightSimpleAnnotation(
return asUltraLightSimpleAnnotation(
JAVA_LANG_ANNOTATION_TARGET,
listOf(targetAttributes),
support,
parent
)
}
@@ -111,17 +111,28 @@ internal fun createRetentionRuntimeAnnotation(support: KtUltraLightSupport, pare
JAVA_LANG_ANNOTATION_RETENTION,
listOf("value" to retentionMapping["kotlin.annotation.AnnotationRetention.RUNTIME"]!!),
support,
parent
parent,
)
internal fun PsiAnnotation.asUltraLightSimpleAnnotation(
qualifier: String,
argumentsList: List<Pair<String, ConstantValue<*>>>,
ultraLightSupport: KtUltraLightSupport,
): KtLightAbstractAnnotation = KtUltraLightSimpleAnnotation(
annotationFqName = qualifier,
argumentsList = argumentsList,
ultraLightSupport = ultraLightSupport,
parent = parent,
nameReferenceElementProvider = { nameReferenceElement }
)
internal fun PsiAnnotation.tryConvertAsRetention(support: KtUltraLightSupport): KtLightAbstractAnnotation? {
if (FqNames.retention.asString() != qualifiedName) return null
val convertedValue = extractAnnotationFqName("value")?.let { retentionMapping[it] } ?: return null
return KtUltraLightSimpleAnnotation(
return asUltraLightSimpleAnnotation(
JAVA_LANG_ANNOTATION_RETENTION,
listOf("value" to convertedValue),
support,
parent
)
}
@@ -171,5 +182,5 @@ private fun PsiAnnotation.tryConvertAs(
from: FqName,
to: String,
): KtLightAbstractAnnotation? = takeIf { from.asString() == qualifiedName }?.let {
KtUltraLightSimpleAnnotation(to, emptyList(), support, parent)
asUltraLightSimpleAnnotation(to, emptyList(), support)
}
@@ -15,6 +15,3 @@ class Test(@get:MyAnnotation @set:MyAnnotation2 @setparam:MyAnnotation3 @propert
get() = Unit
set(value) {}
}
// SKIP_SANITY_TEST
// SKIP_IDE_TEST
@@ -4,6 +4,4 @@
package simple
@Repeatable
annotation class One(val value: String)
// SKIP_SANITY_TEST
// SKIP_IDE_TEST
annotation class One(val value: String)
-1
View File
@@ -5,5 +5,4 @@ class Constructors(val valInPrimary: Int) {
private constructor(): this(2)
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -16,5 +16,3 @@ class Wrapper {
}
class G
// LAZINESS:NoLaziness
@@ -11,5 +11,3 @@ interface I {
fun f()
}
// LAZINESS:NoLaziness
-2
View File
@@ -9,5 +9,3 @@ interface I {
fun f()
}
// LAZINESS:NoLaziness
@@ -12,7 +12,3 @@ interface B: A {
interface C : B {
fun c() = "c"
}
// TODO: could be lazy
// see KT-22819
// LAZINESS:NoLaziness
@@ -5,5 +5,4 @@ package a
fun f() {
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -8,5 +8,4 @@ class A {
}
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -5,5 +5,4 @@ class OnlySecondaryConstructors {
constructor(p: Int): this()
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -1,7 +1,5 @@
// Anno
// FULL_JDK
// SKIP_SANITY_TEST
// SKIP_IDE_TEST
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.TYPE_PARAMETER, AnnotationTarget.TYPE)
@@ -4,5 +4,3 @@
package p
actual typealias B = List<Int>
// SKIP_IDE_TEST
@@ -6,5 +6,3 @@ package p
fun f() {
}
// SKIP_IDE_TEST
@@ -1,5 +1,4 @@
// p.Annotations
// SKIP_SANITY_TEST
package p
@@ -32,5 +32,3 @@ class A {
}
}
}
// LAZINESS:NoConsistency
@@ -5,5 +5,3 @@
fun foo() {
}
// SKIP_IDE_TEST
@@ -23,6 +23,4 @@ class C {
}
}
// LAZINESS:NoLaziness
// SKIP_SANITY_TEST
// COMPILATION_ERRORS
@@ -1,8 +1,8 @@
public final class C /* p.C*/ extends p.A {
public C();// .ctor()
public int af();// af$light_idea_test_case()
public int af$light_idea_test_case();// af$light_idea_test_case()
public int getAp();// getAp$light_idea_test_case()
public int getAp$light_idea_test_case();// getAp$light_idea_test_case()
}
@@ -10,5 +10,4 @@ class C : A() {
}
}
// LAZINESS:NoConsistency
// COMPILATION_ERRORS
@@ -13,5 +13,4 @@ class C : A(), I {
override fun if() = 5
}
// LAZINESS:NoLaziness
// COMPILATION_ERRORS
@@ -10,5 +10,4 @@ class C : A() {
}
}
// LAZINESS:NoLaziness
// COMPILATION_ERRORS
@@ -8,5 +8,4 @@ class C : Base {
override fun foo(): Unit {}
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -7,5 +7,4 @@ interface Base<T> {
class C : Base<Unit> {
override fun foo(t: Unit) {}
}
// LAZINESS:NoLaziness
// FIR_COMPARISON
@@ -1,5 +1,3 @@
// HelloWorld
println("Hello world!")
// LAZINESS:NoLaziness
@@ -11,5 +11,3 @@ class Bar(val a: Int) {
}
}
}
// LAZINESS:NoLaziness
+2 -2
View File
@@ -1,6 +1,6 @@
public class HelloWorld /* HelloWorld*/ extends kotlin.script.templates.standard.ScriptTemplateWithArgs {
public HelloWorld();// .ctor()
public HelloWorld(java.lang.String[]);// .ctor(java.lang.String[])
public static final void main(java.lang.String[]);// main(java.lang.String[])
}
}
-2
View File
@@ -1,5 +1,3 @@
// HelloWorld
println("Hello world!")
// LAZINESS:NoLaziness
+2 -2
View File
@@ -1,5 +1,5 @@
public class InnerClasses /* InnerClasses*/ extends kotlin.script.templates.standard.ScriptTemplateWithArgs {
public InnerClasses();// .ctor()
public InnerClasses(java.lang.String[]);// .ctor(java.lang.String[])
public static final void main(java.lang.String[]);// main(java.lang.String[])
@@ -23,4 +23,4 @@ public static final class Baz /* InnerClasses.Bar.Baz*/ {
public final void doSomething();// doSomething()
}}}
}}}
-2
View File
@@ -11,5 +11,3 @@ class Bar(val a: Int) {
}
}
}
// LAZINESS:NoLaziness
+24
View File
@@ -4,6 +4,12 @@ public enum Direction /* Direction*/ {
WEST,
EAST;
@org.jetbrains.annotations.NotNull()
public static Direction valueOf(@org.jetbrains.annotations.NotNull() java.lang.String) throws java.lang.IllegalArgumentException;// valueOf(java.lang.String)
@org.jetbrains.annotations.NotNull()
public static Direction[] values();// values()
private Direction();// .ctor()
}
@@ -15,6 +21,12 @@ public enum Color /* Color*/ {
private final int rgb;
@org.jetbrains.annotations.NotNull()
public static Color valueOf(@org.jetbrains.annotations.NotNull() java.lang.String) throws java.lang.IllegalArgumentException;// valueOf(java.lang.String)
@org.jetbrains.annotations.NotNull()
public static Color[] values();// values()
private Color(@org.jetbrains.annotations.NotNull() java.lang.String);// .ctor(java.lang.String)
private Color(int);// .ctor(int)
@@ -42,6 +54,12 @@ public enum ProtocolState /* ProtocolState*/ {
@org.jetbrains.annotations.NotNull()
public abstract ProtocolState signal();// signal()
@org.jetbrains.annotations.NotNull()
public static ProtocolState valueOf(@org.jetbrains.annotations.NotNull() java.lang.String) throws java.lang.IllegalArgumentException;// valueOf(java.lang.String)
@org.jetbrains.annotations.NotNull()
public static ProtocolState[] values();// values()
private ProtocolState();// .ctor()
@@ -84,6 +102,12 @@ public enum IntArithmetics /* IntArithmetics*/ implements java.util.function.Bin
};
@org.jetbrains.annotations.NotNull()
public static IntArithmetics valueOf(@org.jetbrains.annotations.NotNull() java.lang.String) throws java.lang.IllegalArgumentException;// valueOf(java.lang.String)
@org.jetbrains.annotations.NotNull()
public static IntArithmetics[] values();// values()
private IntArithmetics();// .ctor()
public int applyAsInt(int, int);// applyAsInt(int, int)