Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
Abstract implementation classes that inherited from now sealed types
have those supertypes removed and replace overridden with open members.
Additional casts had to be introduced in a few places.

A few when statements had to be made exhaustive (by specifying all
cases) because they now operate on subjects with a sealed type.
  • Loading branch information
lukellmann authored and ting-yuan committed Oct 24, 2023
1 parent 98666a8 commit 981fb70
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ class IncrementalContext(
when (declaration) {
is KSPropertyDeclarationJavaImpl -> recordLookupForJavaField(declaration.psi)
is KSFunctionDeclarationJavaImpl -> recordLookupForJavaMethod(declaration.psi)
is KSClassDeclaration, is KSFunctionDeclaration, is KSPropertyDeclaration, is KSTypeAlias,
is KSTypeParameter -> Unit
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,7 @@ class ResolverImpl(
if (declaration.isAbstract)
modifiers.add(Modifier.ABSTRACT)
}
is KSTypeAlias, is KSTypeParameter -> Unit
}
}
Origin.KOTLIN_LIB, Origin.JAVA_LIB -> {
Expand All @@ -1208,6 +1209,7 @@ class ResolverImpl(
if (declaration.jvmAccessFlag and Opcodes.ACC_SYNCHRONIZED != 0)
modifiers.add(Modifier.JAVA_SYNCHRONIZED)
}
is KSClassDeclaration, is KSTypeAlias, is KSTypeParameter -> Unit
}
}
else -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.parents
import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull

abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDescriptor) : KSDeclaration {
abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDescriptor) /*: KSDeclaration*/ {

override val origin by lazy {
open /*override*/ val origin by lazy {
descriptor.origin
}

override val containingFile: KSFile? = null
open /*override*/ val containingFile: KSFile? = null

override val location: Location = NonExistLocation
open /*override*/ val location: Location = NonExistLocation

override val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this) }.memoized()
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this as KSDeclaration) }
.memoized()
}

override val parentDeclaration: KSDeclaration? by lazy {
open /*override*/ val parentDeclaration: KSDeclaration? by lazy {
val containingDescriptor = descriptor.parents.first()
when (containingDescriptor) {
is ClassDescriptor -> KSClassDeclarationDescriptorImpl.getCached(containingDescriptor)
Expand All @@ -53,27 +54,27 @@ abstract class KSDeclarationDescriptorImpl(private val descriptor: DeclarationDe
} as KSDeclaration?
}

override val parent: KSNode? by lazy {
open /*override*/ val parent: KSNode? by lazy {
parentDeclaration
}

override val packageName: KSName by lazy {
open /*override*/ val packageName: KSName by lazy {
KSNameImpl.getCached(descriptor.findPackage().fqName.asString())
}

override val qualifiedName: KSName by lazy {
open /*override*/ val qualifiedName: KSName by lazy {
KSNameImpl.getCached(descriptor.fqNameSafe.asString())
}

override val simpleName: KSName by lazy {
open /*override*/ val simpleName: KSName by lazy {
KSNameImpl.getCached(descriptor.name.asString())
}

override fun toString(): String {
return this.simpleName.asString()
}

override val docString = null
open /*override*/ val docString: String? = null
}

val DeclarationDescriptor.origin: Origin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import com.google.devtools.ksp.symbol.impl.toKSPropertyDeclaration
import com.google.devtools.ksp.toKSModifiers
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor

abstract class KSPropertyAccessorDescriptorImpl(val descriptor: PropertyAccessorDescriptor) : KSPropertyAccessor {
override val origin: Origin by lazy {
abstract class KSPropertyAccessorDescriptorImpl(val descriptor: PropertyAccessorDescriptor) /*: KSPropertyAccessor*/ {
open /*override*/ val origin: Origin by lazy {
when (receiver.origin) {
// if receiver is kotlin source, that means we are a synthetic where developer
// didn't declare an explicit accessor so we used the descriptor instead
Expand All @@ -35,26 +35,27 @@ abstract class KSPropertyAccessorDescriptorImpl(val descriptor: PropertyAccessor
}
}

override val receiver: KSPropertyDeclaration by lazy {
open /*override*/ val receiver: KSPropertyDeclaration by lazy {
descriptor.correspondingProperty.toKSPropertyDeclaration()
}

override val parent: KSNode? by lazy {
open /*override*/ val parent: KSNode? by lazy {
receiver
}

override val location: Location
open /*override*/ val location: Location
get() {
// if receiver is kotlin source, that means `this` is synthetic hence we want the property's location
// Otherwise, receiver is also from a .class file where the location will be NoLocation
return receiver.location
}

override val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this) }.memoized()
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
descriptor.annotations.asSequence().map { KSAnnotationDescriptorImpl.getCached(it, this as KSPropertyAccessor) }
.memoized()
}

override val modifiers: Set<Modifier> by lazy {
open /*override*/ val modifiers: Set<Modifier> by lazy {
val modifiers = mutableSetOf<Modifier>()
modifiers.addAll(descriptor.toKSModifiers())
modifiers.addAll(descriptor.toFunctionKSModifiers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ import com.google.devtools.ksp.symbol.impl.findParentDeclaration
import com.google.devtools.ksp.symbol.impl.getDocString
import com.intellij.psi.PsiElement

abstract class KSDeclarationJavaImpl(private val psi: PsiElement) : KSDeclaration {
override val packageName: KSName by lazy {
this.containingFile!!.packageName
abstract class KSDeclarationJavaImpl(private val psi: PsiElement) /*: KSDeclaration*/ {
open /*override*/ val packageName: KSName by lazy {
(this as KSDeclaration).containingFile!!.packageName
}

override fun toString(): String {
return this.simpleName.asString()
return (this as KSDeclaration).simpleName.asString()
}

override val docString by lazy {
open /*override*/ val docString by lazy {
psi.getDocString()
}

override val parentDeclaration: KSDeclaration? by lazy {
open /*override*/ val parentDeclaration: KSDeclaration? by lazy {
psi.findParentDeclaration()
}

override val parent: KSNode? by lazy {
open /*override*/ val parent: KSNode? by lazy {
psi.findParentAnnotated()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ import com.google.devtools.ksp.symbol.*
import com.google.devtools.ksp.symbol.impl.*
import org.jetbrains.kotlin.psi.*

abstract class KSDeclarationImpl(val ktDeclaration: KtDeclaration) : KSDeclaration {
override val origin: Origin = Origin.KOTLIN
abstract class KSDeclarationImpl(val ktDeclaration: KtDeclaration) /*: KSDeclaration*/ {
open /*override*/ val origin: Origin = Origin.KOTLIN

override val location: Location by lazy {
open /*override*/ val location: Location by lazy {
ktDeclaration.toLocation()
}

override val simpleName: KSName by lazy {
open /*override*/ val simpleName: KSName by lazy {
KSNameImpl.getCached(ktDeclaration.name!!)
}

override val qualifiedName: KSName? by lazy {
open /*override*/ val qualifiedName: KSName? by lazy {
(ktDeclaration as? KtNamedDeclaration)?.fqName?.let { KSNameImpl.getCached(it.asString()) }
}

override val annotations: Sequence<KSAnnotation> by lazy {
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
ktDeclaration.annotationEntries.asSequence().map { KSAnnotationImpl.getCached(it) }.memoized()
}

override val modifiers: Set<Modifier> by lazy {
open /*override*/ val modifiers: Set<Modifier> by lazy {
// we do not check for JVM_STATIC here intentionally as it actually means static in parent class,
// not in this class.
// see: https://github.com/google/ksp/issues/378
Expand All @@ -56,25 +56,25 @@ abstract class KSDeclarationImpl(val ktDeclaration: KtDeclaration) : KSDeclarati
}
}

override val containingFile: KSFile? by lazy {
open /*override*/ val containingFile: KSFile? by lazy {
KSFileImpl.getCached(ktDeclaration.containingKtFile)
}

override val packageName: KSName by lazy {
open /*override*/ val packageName: KSName by lazy {
this.containingFile!!.packageName
}

override val typeParameters: List<KSTypeParameter> by lazy {
open /*override*/ val typeParameters: List<KSTypeParameter> by lazy {
(ktDeclaration as? KtTypeParameterListOwner)?.let {
it.typeParameters.map { KSTypeParameterImpl.getCached(it) }
} ?: emptyList()
}

override val parentDeclaration: KSDeclaration? by lazy {
open /*override*/ val parentDeclaration: KSDeclaration? by lazy {
ktDeclaration.findParentDeclaration()
}

override val parent: KSNode? by lazy {
open /*override*/ val parent: KSNode? by lazy {
ktDeclaration.findParentAnnotated()
}

Expand All @@ -86,7 +86,7 @@ abstract class KSDeclarationImpl(val ktDeclaration: KtDeclaration) : KSDeclarati
ktDeclaration.annotationEntries.map { KSAnnotationImpl.getCached(it) }
}

override val docString by lazy {
open /*override*/ val docString by lazy {
ktDeclaration.getDocString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.google.devtools.ksp.symbol.impl.toLocation
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPropertyAccessor

abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor) : KSPropertyAccessor {
abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor) /*: KSPropertyAccessor*/ {
companion object {
fun getCached(ktPropertyAccessor: KtPropertyAccessor): KSPropertyAccessor {
return if (ktPropertyAccessor.isGetter) {
Expand All @@ -36,27 +36,27 @@ abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor
}
}
}
override val receiver: KSPropertyDeclaration by lazy {
open /*override*/ val receiver: KSPropertyDeclaration by lazy {
KSPropertyDeclarationImpl.getCached(ktPropertyAccessor.property as KtProperty)
}
override val annotations: Sequence<KSAnnotation> by lazy {
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
ktPropertyAccessor.filterUseSiteTargetAnnotations().map { KSAnnotationImpl.getCached(it) }
.plus(this.findAnnotationFromUseSiteTarget())
.plus((this as KSPropertyAccessor).findAnnotationFromUseSiteTarget())
}

override val parent: KSNode? by lazy {
open /*override*/ val parent: KSNode? by lazy {
receiver
}

override val location: Location by lazy {
open /*override*/ val location: Location by lazy {
ktPropertyAccessor.toLocation()
}

override val modifiers: Set<Modifier> by lazy {
open /*override*/ val modifiers: Set<Modifier> by lazy {
ktPropertyAccessor.toKSModifiers()
}

override val declarations: Sequence<KSDeclaration> by lazy {
open /*override*/ val declarations: Sequence<KSDeclaration> by lazy {
if (!ktPropertyAccessor.hasBlockBody()) {
emptySequence()
} else {
Expand All @@ -65,10 +65,10 @@ abstract class KSPropertyAccessorImpl(val ktPropertyAccessor: KtPropertyAccessor
}
}

override val origin: Origin = Origin.KOTLIN
open /*override*/ val origin: Origin = Origin.KOTLIN

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this, data)
open /*override*/ fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this as KSPropertyAccessor, data)
}

internal val originalAnnotations: List<KSAnnotation> by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import com.google.devtools.ksp.processing.impl.ResolverImpl
import com.google.devtools.ksp.symbol.*

class KSConstructorSyntheticImpl private constructor(val ksClassDeclaration: KSClassDeclaration) :
KSFunctionDeclaration,
KSDeclaration
by ksClassDeclaration {
KSFunctionDeclaration {
companion object : KSObjectCache<KSClassDeclaration, KSConstructorSyntheticImpl>() {
fun getCached(ksClassDeclaration: KSClassDeclaration) =
KSConstructorSyntheticImpl.cache.getOrPut(ksClassDeclaration) {
Expand All @@ -43,6 +41,10 @@ class KSConstructorSyntheticImpl private constructor(val ksClassDeclaration: KSC

override val functionKind: FunctionKind = FunctionKind.MEMBER

override val docString: String? get() = ksClassDeclaration.docString

override val packageName: KSName get() = ksClassDeclaration.packageName

override val qualifiedName: KSName? by lazy {
KSNameImpl.getCached(ksClassDeclaration.qualifiedName?.asString()?.plus(".<init>") ?: "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ package com.google.devtools.ksp.symbol.impl.synthetic
import com.google.devtools.ksp.processing.impl.findAnnotationFromUseSiteTarget
import com.google.devtools.ksp.symbol.*

abstract class KSPropertyAccessorSyntheticImpl(ksPropertyDeclaration: KSPropertyDeclaration) : KSPropertyAccessor {
override val annotations: Sequence<KSAnnotation> by lazy {
this.findAnnotationFromUseSiteTarget()
abstract class KSPropertyAccessorSyntheticImpl(ksPropertyDeclaration: KSPropertyDeclaration) /*: KSPropertyAccessor*/ {
open /*override*/ val annotations: Sequence<KSAnnotation> by lazy {
(this as KSPropertyAccessor).findAnnotationFromUseSiteTarget()
}

override val location: Location by lazy {
open /*override*/ val location: Location by lazy {
ksPropertyDeclaration.location
}

override val modifiers: Set<Modifier> = emptySet()
open /*override*/ val modifiers: Set<Modifier> = emptySet()

override val origin: Origin = Origin.SYNTHETIC
open /*override*/ val origin: Origin = Origin.SYNTHETIC

override val receiver: KSPropertyDeclaration = ksPropertyDeclaration
open /*override*/ val receiver: KSPropertyDeclaration = ksPropertyDeclaration

override fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this, data)
open /*override*/ fun <D, R> accept(visitor: KSVisitor<D, R>, data: D): R {
return visitor.visitPropertyAccessor(this as KSPropertyAccessor, data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal class DeclarationOrdering(
}

private fun getOrder(decl: KSDeclarationDescriptorImpl): Int {
return declOrdering.getOrPut(decl) {
return declOrdering.getOrPut(decl as KSDeclaration) {
when (decl) {
is KSPropertyDeclarationDescriptorImpl -> {
fieldOrdering[decl.simpleName.asString()]?.let {
Expand Down Expand Up @@ -467,7 +467,8 @@ internal val KSDeclarationContainer.declarationsInSourceOrder: Sequence<KSDeclar
DeclarationOrdering(it)
} ?: return declarations

return (declarations as? Sequence<KSDeclarationDescriptorImpl>)?.sortedWith(declarationOrdering.comparator)
return (declarations as? Sequence<KSDeclarationDescriptorImpl>)
?.sortedWith(declarationOrdering.comparator) as Sequence<KSDeclaration>?
?: declarations
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class ResolverAAImpl(
if (declaration.isAbstract)
modifiers.add(Modifier.ABSTRACT)
}
is KSTypeAlias, is KSTypeParameter -> Unit
}
}
Origin.KOTLIN_LIB, Origin.JAVA_LIB -> {
Expand All @@ -189,6 +190,7 @@ class ResolverAAImpl(
if (declaration.jvmAccessFlag and Opcodes.ACC_SYNCHRONIZED != 0)
modifiers.add(Modifier.JAVA_SYNCHRONIZED)
}
is KSClassDeclaration, is KSTypeAlias, is KSTypeParameter -> Unit
}
}
else -> Unit
Expand Down
Loading

0 comments on commit 981fb70

Please sign in to comment.