Stub updates for RawFirBuilder

First step for KT-52615
This commit is contained in:
Egor Kulikov
2022-11-18 11:39:33 +01:00
parent becbc06663
commit 877e11419e
60 changed files with 380 additions and 144 deletions
@@ -22,13 +22,13 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.search.SearchScope
import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtPlaceHolderStubElementType
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.elements.KtConstructorElementType
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
abstract class KtConstructor<T : KtConstructor<T>> : KtDeclarationStub<KotlinPlaceHolderStub<T>>, KtFunction {
abstract class KtConstructor<T : KtConstructor<T>> : KtDeclarationStub<KotlinConstructorStub<T>>, KtFunction {
protected constructor(node: ASTNode) : super(node)
protected constructor(stub: KotlinPlaceHolderStub<T>, nodeType: KtPlaceHolderStubElementType<T>) : super(stub, nodeType)
protected constructor(stub: KotlinConstructorStub<T>, nodeType: KtConstructorElementType<T>) : super(stub, nodeType)
abstract fun getContainingClassOrObject(): KtClassOrObject
@@ -53,9 +53,21 @@ abstract class KtConstructor<T : KtConstructor<T>> : KtDeclarationStub<KotlinPla
override fun getEqualsToken() = null
override fun hasBlockBody() = bodyExpression != null
override fun hasBlockBody() = hasBody()
override fun hasBody() = bodyExpression != null
fun isDelegatedCallToThis(): Boolean {
stub?.let { return it.isDelegatedCallToThis() }
return when (this) {
is KtPrimaryConstructor -> false
is KtSecondaryConstructor -> getDelegationCallOrNull()?.isCallToThis() ?: true
else -> throw IllegalStateException("Unknown constructor type: $this")
}
}
override fun hasBody(): Boolean {
stub?.let { return it.hasBody() }
return bodyExpression != null
}
override fun hasDeclaredReturnType() = false
@@ -90,8 +102,8 @@ abstract class KtConstructor<T : KtConstructor<T>> : KtDeclarationStub<KotlinPla
override fun getTextOffset(): Int {
return getConstructorKeyword()?.textOffset
?: valueParameterList?.textOffset
?: super.getTextOffset()
?: valueParameterList?.textOffset
?: super.getTextOffset()
}
override fun getUseScope(): SearchScope {
@@ -8,21 +8,25 @@ package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.KotlinContextReceiverStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
class KtContextReceiver : KtElementImplStub<KotlinPlaceHolderStub<KtContextReceiver>> {
class KtContextReceiver : KtElementImplStub<KotlinContextReceiverStub> {
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtContextReceiver>) : super(stub, KtStubElementTypes.CONTEXT_RECEIVER)
constructor(stub: KotlinContextReceiverStub) : super(stub, KtStubElementTypes.CONTEXT_RECEIVER)
fun targetLabel(): KtSimpleNameExpression? =
findChildByType<KtContainerNode?>(KtNodeTypes.LABEL_QUALIFIER)
?.findChildByType(KtNodeTypes.LABEL)
fun labelName(): String? = targetLabel()?.getReferencedName()
fun labelName(): String? {
stub?.let { return it.getLabel() }
return targetLabel()?.getReferencedName()
}
fun labelNameAsName(): Name? = targetLabel()?.getReferencedNameAsName()
fun typeReference(): KtTypeReference? = findChildByType(KtNodeTypes.TYPE_REFERENCE)
fun typeReference(): KtTypeReference? = getStubOrPsiChild(KtStubElementTypes.TYPE_REFERENCE)
fun name(): String? = labelName() ?: typeReference()?.nameForReceiverLabel()
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
@@ -18,7 +17,7 @@ class KtContextReceiverList : KtElementImplStub<KotlinPlaceHolderStub<KtContextR
return visitor.visitContextReceiverList(this, data)
}
fun contextReceivers(): List<KtContextReceiver> = findChildrenByType(KtNodeTypes.CONTEXT_RECEIVER)
fun contextReceivers(): List<KtContextReceiver> = getStubOrPsiChildrenAsList(KtStubElementTypes.CONTEXT_RECEIVER)
fun typeReferences(): List<KtTypeReference> = contextReceivers().mapNotNull { it.typeReference() }
@@ -259,7 +259,7 @@ public class KtNamedFunction extends KtTypeParameterListOwnerStub<KotlinFunction
@Override
public KtContractEffectList getContractDescription() {
return findChildByType(KtNodeTypes.CONTRACT_EFFECT_LIST);
return getStubOrPsiChild(KtStubElementTypes.CONTRACT_EFFECT_LIST);
}
public boolean mayHaveContract() {
@@ -22,12 +22,12 @@ import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.addRemoveModifier.addModifier
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
class KtPrimaryConstructor : KtConstructor<KtPrimaryConstructor> {
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtPrimaryConstructor>) : super(stub, KtStubElementTypes.PRIMARY_CONSTRUCTOR)
constructor(stub: KotlinConstructorStub<KtPrimaryConstructor>) : super(stub, KtStubElementTypes.PRIMARY_CONSTRUCTOR)
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D) = visitor.visitPrimaryConstructor(this, data)
@@ -140,7 +140,7 @@ public class KtPropertyAccessor extends KtDeclarationStub<KotlinPropertyAccessor
@Override
public KtContractEffectList getContractDescription() {
return findChildByType(KtNodeTypes.CONTRACT_EFFECT_LIST);
return getStubOrPsiChild(KtStubElementTypes.CONTRACT_EFFECT_LIST);
}
@Override
@@ -18,18 +18,23 @@ package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
class KtSecondaryConstructor : KtConstructor<KtSecondaryConstructor> {
constructor(node: ASTNode) : super(node)
constructor(stub: KotlinPlaceHolderStub<KtSecondaryConstructor>) : super(stub, KtStubElementTypes.SECONDARY_CONSTRUCTOR)
constructor(stub: KotlinConstructorStub<KtSecondaryConstructor>) : super(stub, KtStubElementTypes.SECONDARY_CONSTRUCTOR)
override fun <R, D> accept(visitor: KtVisitor<R, D>, data: D) = visitor.visitSecondaryConstructor(this, data)
override fun getContainingClassOrObject() = parent.parent as KtClassOrObject
override fun getBodyExpression() = findChildByClass(KtBlockExpression::class.java)
override fun getBodyExpression(): KtBlockExpression? {
if (stub?.hasBody() == false) {
return null
}
return findChildByClass(KtBlockExpression::class.java)
}
override fun getConstructorKeyword() = notNullChild<PsiElement>(super.getConstructorKeyword())
@@ -85,5 +85,4 @@ public class KtSuperTypeCallEntry extends KtSuperTypeListEntry implements KtCall
KtUserType userType = getTypeAsUserType();
return userType != null ? userType.getTypeArgumentList() : null;
}
}
@@ -23,12 +23,12 @@ object KotlinStubVersions {
// Though only kotlin declarations (no code in the bodies) are stubbed, please do increase this version
// if you are not 100% sure it can be avoided.
// Increasing this version will lead to reindexing of all kotlin source files on the first IDE startup with the new version.
const val SOURCE_STUB_VERSION = 144
const val SOURCE_STUB_VERSION = 145
// Binary stub version should be increased if stub format (org.jetbrains.kotlin.psi.stubs.impl) is changed
// or changes are made to the core stub building code (org.jetbrains.kotlin.idea.decompiler.stubBuilder).
// Increasing this version will lead to reindexing of all binary files that are potentially kotlin binaries (including all class files).
private const val BINARY_STUB_VERSION = 81
private const val BINARY_STUB_VERSION = 82
// Classfile stub version should be increased if changes are made to classfile stub building subsystem (org.jetbrains.kotlin.idea.decompiler.classFile)
// Increasing this version will lead to reindexing of all classfiles.
@@ -87,6 +87,12 @@ interface KotlinFunctionStub : KotlinCallableStubBase<KtNamedFunction> {
fun mayHaveContract(): Boolean
}
interface KotlinConstructorStub<T : KtConstructor<T>> :
KotlinCallableStubBase<T> {
fun hasBody(): Boolean
fun isDelegatedCallToThis(): Boolean
}
interface KotlinImportAliasStub : StubElement<KtImportAlias> {
fun getName(): String?
}
@@ -163,3 +169,7 @@ interface KotlinUserTypeStub : StubElement<KtUserType>
interface KotlinScriptStub : KotlinStubWithFqName<KtScript> {
override fun getFqName(): FqName
}
interface KotlinContextReceiverStub : StubElement<KtContextReceiver> {
fun getLabel(): String?
}
@@ -0,0 +1,55 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.elements
import com.intellij.psi.stubs.IndexSink
import com.intellij.psi.stubs.StubElement
import com.intellij.psi.stubs.StubInputStream
import com.intellij.psi.stubs.StubOutputStream
import com.intellij.util.io.StringRef
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.psi.KtConstructor
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.elements.StubIndexService.Companion.getInstance
import java.io.IOException
abstract class KtConstructorElementType<T : KtConstructor<T>>(
@NonNls debugName: String,
tClass: Class<T>,
stubClass: Class<KotlinConstructorStub<*>>
) : KtStubElementType<KotlinConstructorStub<T>, T>(debugName, tClass, stubClass) {
protected abstract fun newStub(
parentStub: StubElement<*>,
nameRef: StringRef?,
hasBody: Boolean,
isDelegatedCallToThis: Boolean,
): KotlinConstructorStub<T>
protected abstract fun isDelegatedCallToThis(constructor: T): Boolean
override fun createStub(psi: T, parentStub: StubElement<*>): KotlinConstructorStub<T> {
val hasBody = psi.hasBody()
val isDelegatedCallToThis = isDelegatedCallToThis(psi)
return newStub(parentStub, StringRef.fromString(psi.name), hasBody, isDelegatedCallToThis)
}
@Throws(IOException::class)
override fun serialize(stub: KotlinConstructorStub<T>, dataStream: StubOutputStream) {
dataStream.writeName(stub.name)
dataStream.writeBoolean(stub.hasBody())
dataStream.writeBoolean(stub.isDelegatedCallToThis())
}
@Throws(IOException::class)
override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>): KotlinConstructorStub<T> {
val name = dataStream.readName()
val hasBody = dataStream.readBoolean()
val isDelegatedCallToThis = dataStream.readBoolean()
return newStub(parentStub, name, hasBody, isDelegatedCallToThis)
}
override fun indexStub(stub: KotlinConstructorStub<T>, sink: IndexSink) {
}
}
@@ -0,0 +1,30 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.elements
import com.intellij.psi.stubs.StubElement
import com.intellij.psi.stubs.StubInputStream
import com.intellij.psi.stubs.StubOutputStream
import org.jetbrains.kotlin.psi.KtContextReceiver
import org.jetbrains.kotlin.psi.stubs.KotlinContextReceiverStub
import org.jetbrains.kotlin.psi.stubs.impl.KotlinContextReceiverStubImpl
class KtContextReceiverElementType(debugName: String) : KtStubElementType<KotlinContextReceiverStub, KtContextReceiver>(
debugName,
KtContextReceiver::class.java,
KotlinContextReceiverStub::class.java
) {
override fun createStub(
element: KtContextReceiver,
parentStub: StubElement<*>?
): KotlinContextReceiverStub = KotlinContextReceiverStubImpl(parentStub, this, element.labelName())
override fun serialize(stub: KotlinContextReceiverStub, dataStream: StubOutputStream) =
dataStream.writeName(stub.getLabel())
override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?) =
KotlinContextReceiverStubImpl(parentStub, this, dataStream.readNameString())
}
@@ -0,0 +1,27 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.elements
import com.intellij.psi.stubs.StubElement
import com.intellij.util.io.StringRef
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.impl.KotlinConstructorStubImpl
class KtPrimaryConstructorElementType(debugName: String) :
KtConstructorElementType<KtPrimaryConstructor>(debugName, KtPrimaryConstructor::class.java, KotlinConstructorStub::class.java) {
override fun newStub(
parentStub: StubElement<*>,
nameRef: StringRef?,
hasBody: Boolean,
isDelegatedCallToThis: Boolean,
): KotlinConstructorStub<KtPrimaryConstructor> {
return KotlinConstructorStubImpl(
parentStub, KtStubElementTypes.PRIMARY_CONSTRUCTOR, nameRef, hasBody, isDelegatedCallToThis
)
}
override fun isDelegatedCallToThis(constructor: KtPrimaryConstructor) = false
}
@@ -0,0 +1,27 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.elements
import com.intellij.psi.stubs.StubElement
import com.intellij.util.io.StringRef
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.impl.KotlinConstructorStubImpl
class KtSecondaryConstructorElementType(debugName: String) :
KtConstructorElementType<KtSecondaryConstructor>(debugName, KtSecondaryConstructor::class.java, KotlinConstructorStub::class.java) {
override fun newStub(
parentStub: StubElement<*>,
nameRef: StringRef?,
hasBody: Boolean,
isDelegatedCallToThis: Boolean,
): KotlinConstructorStub<KtSecondaryConstructor> {
return KotlinConstructorStubImpl(
parentStub, KtStubElementTypes.SECONDARY_CONSTRUCTOR, nameRef, hasBody, isDelegatedCallToThis
)
}
override fun isDelegatedCallToThis(constructor: KtSecondaryConstructor) = constructor.getDelegationCallOrNull()?.isCallToThis ?: true
}
@@ -37,10 +37,10 @@ public interface KtStubElementTypes {
KtObjectElementType OBJECT_DECLARATION = new KtObjectElementType("OBJECT_DECLARATION");
KtPlaceHolderStubElementType<KtClassInitializer> CLASS_INITIALIZER =
new KtPlaceHolderStubElementType<>("CLASS_INITIALIZER", KtClassInitializer.class);
KtPlaceHolderStubElementType<KtSecondaryConstructor> SECONDARY_CONSTRUCTOR =
new KtPlaceHolderStubElementType<>("SECONDARY_CONSTRUCTOR", KtSecondaryConstructor.class);
KtPlaceHolderStubElementType<KtPrimaryConstructor> PRIMARY_CONSTRUCTOR =
new KtPlaceHolderStubElementType<>("PRIMARY_CONSTRUCTOR", KtPrimaryConstructor.class);
KtSecondaryConstructorElementType SECONDARY_CONSTRUCTOR =
new KtSecondaryConstructorElementType("SECONDARY_CONSTRUCTOR");
KtPrimaryConstructorElementType PRIMARY_CONSTRUCTOR =
new KtPrimaryConstructorElementType("PRIMARY_CONSTRUCTOR");
KtParameterElementType VALUE_PARAMETER = new KtParameterElementType("VALUE_PARAMETER");
KtPlaceHolderStubElementType<KtParameterList> VALUE_PARAMETER_LIST =
@@ -109,7 +109,8 @@ public interface KtStubElementTypes {
KtNameReferenceExpressionElementType REFERENCE_EXPRESSION = new KtNameReferenceExpressionElementType("REFERENCE_EXPRESSION");
KtDotQualifiedExpressionElementType DOT_QUALIFIED_EXPRESSION = new KtDotQualifiedExpressionElementType("DOT_QUALIFIED_EXPRESSION");
KtEnumEntrySuperClassReferenceExpressionElementType
ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION = new KtEnumEntrySuperClassReferenceExpressionElementType("ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION");
ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION =
new KtEnumEntrySuperClassReferenceExpressionElementType("ENUM_ENTRY_SUPERCLASS_REFERENCE_EXPRESSION");
KtPlaceHolderStubElementType<KtTypeArgumentList> TYPE_ARGUMENT_LIST =
new KtPlaceHolderStubElementType<>("TYPE_ARGUMENT_LIST", KtTypeArgumentList.class);
@@ -147,8 +148,7 @@ public interface KtStubElementTypes {
KtPlaceHolderStubElementType<KtConstructorCalleeExpression> CONSTRUCTOR_CALLEE =
new KtPlaceHolderStubElementType<>("CONSTRUCTOR_CALLEE", KtConstructorCalleeExpression.class);
KtPlaceHolderStubElementType<KtContextReceiver> CONTEXT_RECEIVER =
new KtPlaceHolderStubElementType<>("CONTEXT_RECEIVER", KtContextReceiver.class);
KtContextReceiverElementType CONTEXT_RECEIVER = new KtContextReceiverElementType("CONTEXT_RECEIVER");
KtPlaceHolderStubElementType<KtContextReceiverList> CONTEXT_RECEIVER_LIST =
new KtPlaceHolderStubElementType<>("CONTEXT_RECEIVER_LIST", KtContextReceiverList.class);
@@ -174,5 +174,4 @@ public interface KtStubElementTypes {
new KtPlaceHolderWithTextStubElementType<>("ESCAPE_STRING_TEMPLATE_ENTRY", KtEscapeStringTemplateEntry.class);
KtScriptElementType SCRIPT = new KtScriptElementType("SCRIPT");
}
@@ -20,6 +20,7 @@ import com.intellij.openapi.components.ServiceManager
import com.intellij.psi.stubs.IndexSink
import com.intellij.psi.stubs.StubInputStream
import com.intellij.psi.stubs.StubOutputStream
import org.jetbrains.kotlin.psi.KtConstructor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.stubs.*
import org.jetbrains.kotlin.psi.stubs.impl.KotlinFileStubImpl
@@ -0,0 +1,28 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.impl
import com.intellij.psi.stubs.StubElement
import com.intellij.util.io.StringRef
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtConstructor
import org.jetbrains.kotlin.psi.stubs.KotlinConstructorStub
import org.jetbrains.kotlin.psi.stubs.elements.KtConstructorElementType
class KotlinConstructorStubImpl<T : KtConstructor<T>>(
parent: StubElement<out PsiElement>?,
elementType: KtConstructorElementType<T>,
private val containingClassName: StringRef?,
private val hasBody: Boolean,
private val isDelegatedCallToThis: Boolean,
) : KotlinStubBaseImpl<T>(parent, elementType), KotlinConstructorStub<T> {
override fun getFqName() = null
override fun getName() = StringRef.toString(containingClassName)
override fun isTopLevel() = false
override fun isExtension() = false
override fun hasBody() = hasBody
override fun isDelegatedCallToThis() = isDelegatedCallToThis
}
@@ -0,0 +1,20 @@
/*
* 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.
*/
package org.jetbrains.kotlin.psi.stubs.impl
import com.intellij.psi.stubs.StubElement
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtContextReceiver
import org.jetbrains.kotlin.psi.stubs.KotlinContextReceiverStub
import org.jetbrains.kotlin.psi.stubs.elements.KtContextReceiverElementType
class KotlinContextReceiverStubImpl(
parent: StubElement<out PsiElement>?,
elementType: KtContextReceiverElementType,
private val label: String?,
) : KotlinStubBaseImpl<KtContextReceiver>(parent, elementType), KotlinContextReceiverStub {
override fun getLabel() = label
}