Skip to content

Make hierarchy in KSP API sealed #1380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions api/api.base
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ package com.google.devtools.ksp.processing {
property @NonNull public abstract String targetName;
}

public interface PlatformInfo {
public sealed interface PlatformInfo {
method @NonNull public String getPlatformName();
property @NonNull public abstract String platformName;
}
Expand Down Expand Up @@ -262,7 +262,7 @@ package com.google.devtools.ksp.symbol {
enum_constant public static final com.google.devtools.ksp.symbol.FunctionKind TOP_LEVEL;
}

public interface KSAnnotated extends com.google.devtools.ksp.symbol.KSNode {
public sealed interface KSAnnotated extends com.google.devtools.ksp.symbol.KSNode {
method @NonNull public kotlin.sequences.Sequence<com.google.devtools.ksp.symbol.KSAnnotation> getAnnotations();
property @NonNull public abstract kotlin.sequences.Sequence<com.google.devtools.ksp.symbol.KSAnnotation> annotations;
}
Expand Down Expand Up @@ -313,7 +313,7 @@ package com.google.devtools.ksp.symbol {
property @Nullable public abstract com.google.devtools.ksp.symbol.KSClassifierReference qualifier;
}

public interface KSDeclaration extends com.google.devtools.ksp.symbol.KSModifierListOwner com.google.devtools.ksp.symbol.KSAnnotated com.google.devtools.ksp.symbol.KSExpectActual {
public sealed interface KSDeclaration extends com.google.devtools.ksp.symbol.KSModifierListOwner com.google.devtools.ksp.symbol.KSAnnotated com.google.devtools.ksp.symbol.KSExpectActual {
method @Nullable public com.google.devtools.ksp.symbol.KSFile getContainingFile();
method @Nullable public String getDocString();
method @NonNull public com.google.devtools.ksp.symbol.KSName getPackageName();
Expand All @@ -330,7 +330,7 @@ package com.google.devtools.ksp.symbol {
property @NonNull public abstract java.util.List<com.google.devtools.ksp.symbol.KSTypeParameter> typeParameters;
}

public interface KSDeclarationContainer extends com.google.devtools.ksp.symbol.KSNode {
public sealed interface KSDeclarationContainer extends com.google.devtools.ksp.symbol.KSNode {
method @NonNull public kotlin.sequences.Sequence<com.google.devtools.ksp.symbol.KSDeclaration> getDeclarations();
property @NonNull public abstract kotlin.sequences.Sequence<com.google.devtools.ksp.symbol.KSDeclaration> declarations;
}
Expand Down Expand Up @@ -390,7 +390,7 @@ package com.google.devtools.ksp.symbol {
property @Nullable public abstract com.google.devtools.ksp.symbol.KSTypeReference returnType;
}

public interface KSModifierListOwner extends com.google.devtools.ksp.symbol.KSNode {
public sealed interface KSModifierListOwner extends com.google.devtools.ksp.symbol.KSNode {
method @NonNull public java.util.Set<com.google.devtools.ksp.symbol.Modifier> getModifiers();
property @NonNull public abstract java.util.Set<com.google.devtools.ksp.symbol.Modifier> modifiers;
}
Expand All @@ -401,7 +401,7 @@ package com.google.devtools.ksp.symbol {
method @NonNull public String getShortName();
}

public interface KSNode {
public sealed interface KSNode {
method public <D, R> R accept(@NonNull com.google.devtools.ksp.symbol.KSVisitor<D,R> visitor, D data);
method @NonNull public com.google.devtools.ksp.symbol.Location getLocation();
method @NonNull public com.google.devtools.ksp.symbol.Origin getOrigin();
Expand All @@ -416,7 +416,7 @@ package com.google.devtools.ksp.symbol {
property @NonNull public abstract com.google.devtools.ksp.symbol.KSReferenceElement element;
}

public interface KSPropertyAccessor extends com.google.devtools.ksp.symbol.KSDeclarationContainer com.google.devtools.ksp.symbol.KSAnnotated com.google.devtools.ksp.symbol.KSModifierListOwner {
public sealed interface KSPropertyAccessor extends com.google.devtools.ksp.symbol.KSDeclarationContainer com.google.devtools.ksp.symbol.KSAnnotated com.google.devtools.ksp.symbol.KSModifierListOwner {
method @NonNull public com.google.devtools.ksp.symbol.KSPropertyDeclaration getReceiver();
property @NonNull public abstract com.google.devtools.ksp.symbol.KSPropertyDeclaration receiver;
}
Expand Down Expand Up @@ -449,7 +449,7 @@ package com.google.devtools.ksp.symbol {
property @NonNull public abstract com.google.devtools.ksp.symbol.KSValueParameter parameter;
}

public interface KSReferenceElement extends com.google.devtools.ksp.symbol.KSNode {
public sealed interface KSReferenceElement extends com.google.devtools.ksp.symbol.KSNode {
method @NonNull public java.util.List<com.google.devtools.ksp.symbol.KSTypeArgument> getTypeArguments();
property @NonNull public abstract java.util.List<com.google.devtools.ksp.symbol.KSTypeArgument> typeArguments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.google.devtools.ksp.processing
/**
* Platform specific information
*/
interface PlatformInfo {
sealed interface PlatformInfo {
val platformName: String
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.devtools.ksp.symbol
/**
* A symbol that can be annotated with annotations.
*/
interface KSAnnotated : KSNode {
sealed interface KSAnnotated : KSNode {

/**
* All annotations on this symbol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.devtools.ksp.symbol
/**
* A declaration, can be function declaration, class declaration and property declaration, or a type alias.
*/
interface KSDeclaration : KSModifierListOwner, KSAnnotated, KSExpectActual {
sealed interface KSDeclaration : KSModifierListOwner, KSAnnotated, KSExpectActual {
/**
* Simple name of this declaration, usually the name identifier at the declaration site.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.devtools.ksp.symbol
/**
* A declaration container can have a list of declarations declared in it.
*/
interface KSDeclarationContainer : KSNode {
sealed interface KSDeclarationContainer : KSNode {
/**
* Declarations that are lexically declared inside the current container.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.devtools.ksp.symbol
/**
* A [KSModifierListOwner] can have zero or more modifiers.
*/
interface KSModifierListOwner : KSNode {
sealed interface KSModifierListOwner : KSNode {
/**
* The set of modifiers on this element.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.devtools.ksp.symbol
/**
* Base class of every visitable program elements.
*/
interface KSNode {
sealed interface KSNode {
val origin: Origin
val location: Location
val parent: KSNode?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.google.devtools.ksp.symbol
* Note that annotation use-site targets such as @get: @set: is not copied to accessor's annotations attribute.
* Use KSAnnotated.findAnnotationFromUseSiteTarget() to ensure annotations from parent is obtained.
*/
interface KSPropertyAccessor : KSDeclarationContainer, KSAnnotated, KSModifierListOwner {
sealed interface KSPropertyAccessor : KSDeclarationContainer, KSAnnotated, KSModifierListOwner {
/**
* The owner of the property accessor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package com.google.devtools.ksp.symbol
*
* KSReferenceElement can specify, for example, a class, interface, or function, etc.
*/
interface KSReferenceElement : KSNode {
sealed interface KSReferenceElement : KSNode {
/**
* Type arguments in the type reference.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,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 @@ -627,6 +627,11 @@ class ResolverImpl(
resolverContext = resolverContext
.childForClassOrPackage(resolveJavaDeclaration(e.psi) as ClassDescriptor, JavaClassImpl(e.psi))
}
is KSClassDeclaration, is KSFunctionDeclaration, is KSPropertyDeclaration, is KSTypeAlias,
is KSTypeParameter, is KSFile, is KSPropertyGetter, is KSPropertySetter, is KSTypeArgument,
is KSTypeReference, is KSValueArgument, is KSValueParameter, is KSAnnotation, is KSCallableReference,
is KSClassifierReference, is KSDefNonNullReference, is KSDynamicReference, is KSParenthesizedReference,
-> Unit
}
}
return if (javaType is JavaArrayTypeImpl)
Expand Down Expand Up @@ -1192,6 +1197,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 +1214,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

This file was deleted.

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()
}
}

This file was deleted.

Loading