[FIR] Rework predicates system

Now predicates are split into LookupPredicate and DeclarationPredicate
  hierarchies. First one allows to perform global search for declarations
  and second one allows to check if some declaration matches the predicate.
Predicates with meta annotations are excluded from LookupPredicates,
  because it's impossible to create index of annotations with meta-annotations,
  because they can be located inside binary dependencies (so to achieve
  this we need to scan the whole classpath).
Also only one predicate with meta-annotations is left in DeclarationPredicate
  hierarchy (AnnotatedWithMeta)

^KT-53874 Fixed
^KT-53590 Fixed
This commit is contained in:
Dmitriy Novozhilov
2022-11-23 15:16:44 +02:00
committed by Space Team
parent 7a9a71a089
commit 246dc985a6
41 changed files with 934 additions and 605 deletions
@@ -12,17 +12,23 @@ import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.extensions.predicate.*
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.fir.extensions.predicate.AbstractPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.PredicateVisitor
import org.jetbrains.kotlin.fir.resolve.fqName
import org.jetbrains.kotlin.fir.resolve.getCorrespondingClassSymbolOrNull
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
@NoMutableState
class FirPredicateBasedProviderImpl(private val session: FirSession) : FirPredicateBasedProvider() {
private val registeredPluginAnnotations = session.registeredPluginAnnotations
private val cache = Cache()
override fun getSymbolsByPredicate(predicate: DeclarationPredicate): List<FirBasedSymbol<*>> {
val annotations = registeredPluginAnnotations.getAnnotationsForPredicate(predicate)
override fun getSymbolsByPredicate(predicate: LookupPredicate): List<FirBasedSymbol<*>> {
val annotations = predicate.annotations
if (annotations.isEmpty()) return emptyList()
val declarations = annotations.flatMapTo(mutableSetOf()) {
cache.declarationByAnnotation[it] + cache.declarationsUnderAnnotated[it]
@@ -76,66 +82,75 @@ class FirPredicateBasedProviderImpl(private val session: FirSession) : FirPredic
// ---------------------------------- Matching ----------------------------------
override fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean {
return predicate.accept(matcher, declaration)
override fun matches(predicate: AbstractPredicate<*>, declaration: FirDeclaration): Boolean {
return when (predicate) {
is DeclarationPredicate -> predicate.accept(declarationPredicateMatcher, declaration)
is LookupPredicate -> predicate.accept(lookupPredicateMatcher, declaration)
}
}
private val matcher = Matcher()
private val declarationPredicateMatcher = Matcher<DeclarationPredicate>()
private val lookupPredicateMatcher = Matcher<LookupPredicate>()
private inner class Matcher : DeclarationPredicateVisitor<Boolean, FirDeclaration>() {
override fun visitPredicate(predicate: DeclarationPredicate, data: FirDeclaration): Boolean {
private inner class Matcher<P : AbstractPredicate<P>> : PredicateVisitor<P, Boolean, FirDeclaration>() {
override fun visitPredicate(predicate: AbstractPredicate<P>, data: FirDeclaration): Boolean {
throw IllegalStateException("Should not be there")
}
override fun visitAnd(predicate: DeclarationPredicate.And, data: FirDeclaration): Boolean {
override fun visitAnd(predicate: AbstractPredicate.And<P>, data: FirDeclaration): Boolean {
return predicate.a.accept(this, data) && predicate.b.accept(this, data)
}
override fun visitOr(predicate: DeclarationPredicate.Or, data: FirDeclaration): Boolean {
override fun visitOr(predicate: AbstractPredicate.Or<P>, data: FirDeclaration): Boolean {
return predicate.a.accept(this, data) || predicate.b.accept(this, data)
}
// ------------------------------------ Annotated ------------------------------------
override fun visitAnnotatedWith(predicate: AnnotatedWith, data: FirDeclaration): Boolean {
override fun visitAnnotatedWith(predicate: AbstractPredicate.AnnotatedWith<P>, data: FirDeclaration): Boolean {
return matchWith(data, predicate.annotations)
}
override fun visitAncestorAnnotatedWith(predicate: AncestorAnnotatedWith, data: FirDeclaration): Boolean {
override fun visitAncestorAnnotatedWith(
predicate: AbstractPredicate.AncestorAnnotatedWith<P>,
data: FirDeclaration
): Boolean {
return matchUnder(data, predicate.annotations)
}
override fun visitParentAnnotatedWith(predicate: ParentAnnotatedWith, data: FirDeclaration): Boolean {
override fun visitParentAnnotatedWith(
predicate: AbstractPredicate.ParentAnnotatedWith<P>,
data: FirDeclaration
): Boolean {
return matchParentWith(data, predicate.annotations)
}
override fun visitHasAnnotatedWith(predicate: HasAnnotatedWith, data: FirDeclaration): Boolean {
override fun visitHasAnnotatedWith(predicate: AbstractPredicate.HasAnnotatedWith<P>, data: FirDeclaration): Boolean {
return matchHasAnnotatedWith(data, predicate.annotations)
}
// ------------------------------------ Meta Annotated ------------------------------------
// ------------------------------------ Meta-annotated ------------------------------------
override fun visitMetaAnnotatedWith(predicate: MetaAnnotatedWith, data: FirDeclaration): Boolean {
return matchWith(data, predicate.userDefinedAnnotations)
override fun visitMetaAnnotatedWith(predicate: AbstractPredicate.MetaAnnotatedWith<P>, data: FirDeclaration): Boolean {
val visited = mutableSetOf<FirRegularClassSymbol>()
return data.annotations.any { annotation ->
annotation.markedWithMetaAnnotation(predicate.metaAnnotations, visited)
}
}
override fun visitAncestorMetaAnnotatedWith(predicate: AncestorMetaAnnotatedWith, data: FirDeclaration): Boolean {
return matchUnder(data, predicate.userDefinedAnnotations)
}
override fun visitParentMetaAnnotatedWith(predicate: ParentMetaAnnotatedWith, data: FirDeclaration): Boolean {
return matchParentWith(data, predicate.userDefinedAnnotations)
}
override fun visitHasMetaAnnotatedWith(predicate: HasMetaAnnotatedWith, data: FirDeclaration): Boolean {
return matchHasAnnotatedWith(data, predicate.userDefinedAnnotations)
private fun FirAnnotation.markedWithMetaAnnotation(
metaAnnotations: Set<AnnotationFqn>,
visited: MutableSet<FirRegularClassSymbol>
): Boolean {
val symbol = this.getCorrespondingClassSymbolOrNull(session) ?: return false
if (!visited.add(symbol)) return false
if (symbol.classId.asSingleFqName() in metaAnnotations) return true
if (symbol.resolvedAnnotationsWithClassIds.any { it.markedWithMetaAnnotation(metaAnnotations, visited) }) return true
return false
}
// ------------------------------------ Utilities ------------------------------------
private val MetaAnnotated.userDefinedAnnotations: Set<AnnotationFqn>
get() = metaAnnotations.flatMapTo(mutableSetOf()) { registeredPluginAnnotations.getAnnotationsWithMetaAnnotation(it) }
private fun matchWith(declaration: FirDeclaration, annotations: Set<AnnotationFqn>): Boolean {
return cache.annotationsOfDeclaration[declaration].any { it in annotations }
}
@@ -5,39 +5,21 @@
package org.jetbrains.kotlin.fir.extensions
import com.google.common.collect.LinkedHashMultimap
import com.google.common.collect.Multimap
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.FirSessionComponent
import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.caches.FirCache
import org.jetbrains.kotlin.fir.caches.createCache
import org.jetbrains.kotlin.fir.caches.firCachesFactory
import org.jetbrains.kotlin.fir.caches.getValue
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.extensions.predicate.AbstractPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
abstract class FirRegisteredPluginAnnotations(protected val session: FirSession) : FirSessionComponent {
/**
* Contains all annotations that can be targeted by the plugins. It includes the annotations directly mentioned by the plugin,
* and all the user-defined annotations which are meta-annotated by the annotations from the [metaAnnotations] list.
* Contains all annotations that can be targeted by lookup predicates from plugins
*/
abstract val annotations: Set<AnnotationFqn>
/**
* Contains meta-annotations that can be targeted by the plugins.
*/
abstract val metaAnnotations: Set<AnnotationFqn>
val hasRegisteredAnnotations: Boolean
get() = annotations.isNotEmpty() || metaAnnotations.isNotEmpty()
abstract fun getAnnotationsWithMetaAnnotation(metaAnnotation: AnnotationFqn): Collection<AnnotationFqn>
abstract fun registerUserDefinedAnnotation(metaAnnotation: AnnotationFqn, annotationClasses: Collection<FirRegularClass>)
abstract fun getAnnotationsForPredicate(predicate: DeclarationPredicate): Set<AnnotationFqn>
get() = annotations.isNotEmpty()
@PluginServicesInitialization
abstract fun initialize()
@@ -50,33 +32,16 @@ abstract class FirRegisteredPluginAnnotations(protected val session: FirSession)
* It also has some common code in it.
*/
abstract class AbstractFirRegisteredPluginAnnotations(session: FirSession) : FirRegisteredPluginAnnotations(session) {
final override val metaAnnotations: MutableSet<AnnotationFqn> = mutableSetOf()
private val annotationsForPredicateCache: FirCache<DeclarationPredicate, Set<AnnotationFqn>, Nothing?> =
session.firCachesFactory.createCache { predicate ->
collectAnnotations(predicate)
}
final override fun getAnnotationsForPredicate(predicate: DeclarationPredicate): Set<AnnotationFqn> {
return annotationsForPredicateCache.getValue(predicate)
}
private fun collectAnnotations(predicate: DeclarationPredicate): Set<AnnotationFqn> {
val result = predicate.metaAnnotations.flatMapTo(mutableSetOf()) { getAnnotationsWithMetaAnnotation(it) }
if (result.isEmpty()) return predicate.annotations
result += predicate.annotations
return result
}
@PluginServicesInitialization
final override fun initialize() {
val registrar = object : FirDeclarationPredicateRegistrar() {
val predicates = mutableListOf<DeclarationPredicate>()
override fun register(vararg predicates: DeclarationPredicate) {
val predicates = mutableListOf<AbstractPredicate<*>>()
override fun register(vararg predicates: AbstractPredicate<*>) {
this.predicates += predicates
}
override fun register(predicates: Collection<DeclarationPredicate>) {
override fun register(predicates: Collection<AbstractPredicate<*>>) {
this.predicates += predicates
}
}
@@ -89,7 +54,6 @@ abstract class AbstractFirRegisteredPluginAnnotations(session: FirSession) : Fir
for (predicate in registrar.predicates) {
saveAnnotationsFromPlugin(predicate.annotations)
metaAnnotations += predicate.metaAnnotations
}
}
@@ -100,23 +64,9 @@ abstract class AbstractFirRegisteredPluginAnnotations(session: FirSession) : Fir
class FirRegisteredPluginAnnotationsImpl(session: FirSession) : AbstractFirRegisteredPluginAnnotations(session) {
override val annotations: MutableSet<AnnotationFqn> = mutableSetOf()
// MetaAnnotation -> Annotations
private val userDefinedAnnotations: Multimap<AnnotationFqn, AnnotationFqn> = LinkedHashMultimap.create()
override fun getAnnotationsWithMetaAnnotation(metaAnnotation: AnnotationFqn): Collection<AnnotationFqn> {
return userDefinedAnnotations[metaAnnotation]
}
override fun saveAnnotationsFromPlugin(annotations: Collection<AnnotationFqn>) {
this.annotations += annotations
}
override fun registerUserDefinedAnnotation(metaAnnotation: AnnotationFqn, annotationClasses: Collection<FirRegularClass>) {
require(annotationClasses.all { it.classKind == ClassKind.ANNOTATION_CLASS })
val annotations = annotationClasses.map { it.symbol.classId.asSingleFqName() }
this.annotations += annotations
userDefinedAnnotations.putAll(metaAnnotation, annotations)
}
}
val FirSession.registeredPluginAnnotations: FirRegisteredPluginAnnotations by FirSession.sessionComponentAccessor()
@@ -5,11 +5,8 @@
package org.jetbrains.kotlin.fir.resolve.transformers.plugin
import com.google.common.collect.LinkedHashMultimap
import com.google.common.collect.Multimap
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.caches.firCachesFactory
@@ -21,10 +18,8 @@ import org.jetbrains.kotlin.fir.expressions.FirStatement
import org.jetbrains.kotlin.fir.extensions.*
import org.jetbrains.kotlin.fir.resolve.ResolutionMode
import org.jetbrains.kotlin.fir.resolve.ScopeSession
import org.jetbrains.kotlin.fir.resolve.fqName
import org.jetbrains.kotlin.fir.resolve.providers.FirSymbolProviderInternals
import org.jetbrains.kotlin.fir.resolve.transformers.*
import org.jetbrains.kotlin.fir.resolve.transformers.plugin.FirCompilerRequiredAnnotationsResolveTransformer.Mode
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.FirUserTypeRef
import org.jetbrains.kotlin.fir.types.coneTypeSafe
@@ -33,7 +28,6 @@ import org.jetbrains.kotlin.fir.withFileAnalysisExceptionWrapping
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.name.StandardClassIds.Annotations.Deprecated
import org.jetbrains.kotlin.name.StandardClassIds.Annotations.DeprecatedSinceKotlin
import org.jetbrains.kotlin.name.StandardClassIds.Annotations.JvmRecord
@@ -46,25 +40,9 @@ class FirCompilerRequiredAnnotationsResolveProcessor(
override fun process(files: Collection<FirFile>) {
val transformer = FirCompilerRequiredAnnotationsResolveTransformer(session, scopeSession)
val registeredPluginAnnotations = session.registeredPluginAnnotations
if (!registeredPluginAnnotations.hasRegisteredAnnotations) {
files.forEach {
withFileAnalysisExceptionWrapping(it) {
it.transformSingle(transformer, Mode.RegularAnnotations)
}
}
return
}
if (registeredPluginAnnotations.metaAnnotations.isNotEmpty()) {
files.forEach {
withFileAnalysisExceptionWrapping(it) {
it.transformSingle(transformer, Mode.MetaAnnotations)
}
}
}
files.forEach {
withFileAnalysisExceptionWrapping(it) {
it.transformSingle(transformer, Mode.RegularAnnotations)
it.transformSingle(transformer, null)
}
}
}
@@ -83,42 +61,21 @@ class FirCompilerRequiredAnnotationsResolveProcessor(
open class FirCompilerRequiredAnnotationsResolveTransformer(
final override val session: FirSession,
scopeSession: ScopeSession
) : FirAbstractPhaseTransformer<Mode>(COMPILER_REQUIRED_ANNOTATIONS) {
enum class Mode {
MetaAnnotations,
RegularAnnotations
}
) : FirAbstractPhaseTransformer<Nothing?>(COMPILER_REQUIRED_ANNOTATIONS) {
private val annotationTransformer = FirAnnotationResolveTransformer(session, scopeSession)
private val importTransformer = FirPartialImportResolveTransformer(session)
val extensionService = session.extensionService
override fun <E : FirElement> transformElement(element: E, data: Mode): E {
override fun <E : FirElement> transformElement(element: E, data: Nothing?): E {
throw IllegalStateException("Should not be here")
}
override fun transformFile(file: FirFile, data: Mode): FirFile {
override fun transformFile(file: FirFile, data: Nothing?): FirFile {
checkSessionConsistency(file)
val registeredPluginAnnotations = session.registeredPluginAnnotations
val regularAnnotations: Set<AnnotationFqn>
val metaAnnotations: Set<AnnotationFqn>
when (data) {
Mode.MetaAnnotations -> {
regularAnnotations = emptySet()
metaAnnotations = registeredPluginAnnotations.metaAnnotations
}
Mode.RegularAnnotations -> {
regularAnnotations = registeredPluginAnnotations.annotations
metaAnnotations = emptySet()
}
}
val regularAnnotations = registeredPluginAnnotations.annotations
val newAnnotations = file.resolveAnnotations(regularAnnotations, metaAnnotations)
if (!newAnnotations.isEmpty) {
for (metaAnnotation in newAnnotations.keySet()) {
registeredPluginAnnotations.registerUserDefinedAnnotation(metaAnnotation, newAnnotations[metaAnnotation])
}
}
file.resolveAnnotations(regularAnnotations)
return file
}
@@ -128,27 +85,21 @@ open class FirCompilerRequiredAnnotationsResolveTransformer(
}
}
override fun transformRegularClass(regularClass: FirRegularClass, data: Mode): FirStatement {
return annotationTransformer.transformRegularClass(regularClass, LinkedHashMultimap.create())
override fun transformRegularClass(regularClass: FirRegularClass, data: Nothing?): FirStatement {
return annotationTransformer.transformRegularClass(regularClass, null)
}
override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Mode): FirStatement {
return annotationTransformer.transformTypeAlias(typeAlias, LinkedHashMultimap.create())
override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Nothing?): FirStatement {
return annotationTransformer.transformTypeAlias(typeAlias, null)
}
private fun FirFile.resolveAnnotations(
annotations: Set<AnnotationFqn>,
metaAnnotations: Set<AnnotationFqn>
): Multimap<AnnotationFqn, FirRegularClass> {
val acceptableNames = annotations + metaAnnotations
private fun FirFile.resolveAnnotations(annotations: Set<AnnotationFqn>) {
val acceptableNames = annotations
importTransformer.acceptableFqNames = acceptableNames
this.transformImports(importTransformer, null)
annotationTransformer.acceptableFqNames = acceptableNames
annotationTransformer.metaAnnotations = metaAnnotations
val newAnnotations = LinkedHashMultimap.create<AnnotationFqn, FirRegularClass>()
this.transform<FirFile, Multimap<AnnotationFqn, FirRegularClass>>(annotationTransformer, newAnnotations)
return newAnnotations
this.transform<FirFile, Nothing?>(annotationTransformer, null)
}
}
@@ -164,9 +115,7 @@ private class FirPartialImportResolveTransformer(
private class FirAnnotationResolveTransformer(
session: FirSession,
scopeSession: ScopeSession
) : FirAbstractAnnotationResolveTransformer<Multimap<AnnotationFqn, FirRegularClass>, PersistentList<FirDeclaration>>(
session, scopeSession
) {
) : FirAbstractAnnotationResolveTransformer<Nothing?, PersistentList<FirDeclaration>>(session, scopeSession) {
companion object {
private val REQUIRED_ANNOTATIONS: Set<ClassId> = setOf(
Deprecated, DeprecatedSinceKotlin, WasExperimental, JvmRecord
@@ -178,7 +127,7 @@ private class FirAnnotationResolveTransformer(
private val predicateBasedProvider = session.predicateBasedProvider
var acceptableFqNames: Set<AnnotationFqn> = emptySet()
var metaAnnotations: Set<AnnotationFqn> = emptySet()
private val typeResolverTransformer: FirSpecificTypeResolverTransformer = FirSpecificTypeResolverTransformer(
session,
errorTypeAsResolved = false
@@ -200,14 +149,11 @@ private class FirAnnotationResolveTransformer(
owners = state
}
override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: Multimap<AnnotationFqn, FirRegularClass>): FirStatement {
override fun transformAnnotationCall(annotationCall: FirAnnotationCall, data: Nothing?): FirStatement {
return transformAnnotation(annotationCall, data)
}
override fun transformAnnotation(
annotation: FirAnnotation,
data: Multimap<AnnotationFqn, FirRegularClass>
): FirStatement {
override fun transformAnnotation(annotation: FirAnnotation, data: Nothing?): FirStatement {
val annotationTypeRef = annotation.annotationTypeRef
if (annotationTypeRef !is FirUserTypeRef) return annotation
val name = annotationTypeRef.qualifier.last().name
@@ -224,36 +170,27 @@ private class FirAnnotationResolveTransformer(
return transformedAnnotation
}
override fun transformRegularClass(
regularClass: FirRegularClass,
data: Multimap<AnnotationFqn, FirRegularClass>
): FirStatement {
override fun transformRegularClass(regularClass: FirRegularClass, data: Nothing?): FirStatement {
withClassDeclarationCleanup(classDeclarationsStack, regularClass) {
return super.transformRegularClass(regularClass, data).also {
if (regularClass.classKind == ClassKind.ANNOTATION_CLASS && metaAnnotations.isNotEmpty()) {
val annotations = regularClass.annotations.mapNotNull { it.fqName(session) }
for (annotation in annotations.filter { it in metaAnnotations }) {
data.put(annotation, regularClass)
}
}
calculateDeprecations(regularClass)
}
}
}
override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Multimap<AnnotationFqn, FirRegularClass>): FirTypeAlias {
override fun transformTypeAlias(typeAlias: FirTypeAlias, data: Nothing?): FirTypeAlias {
return super.transformTypeAlias(typeAlias, data).also {
calculateDeprecations(typeAlias)
}
}
override fun transformDeclaration(declaration: FirDeclaration, data: Multimap<AnnotationFqn, FirRegularClass>): FirDeclaration {
override fun transformDeclaration(declaration: FirDeclaration, data: Nothing?): FirDeclaration {
return super.transformDeclaration(declaration, data).also {
predicateBasedProvider.registerAnnotatedDeclaration(declaration, owners)
}
}
override fun transformFile(file: FirFile, data: Multimap<AnnotationFqn, FirRegularClass>): FirFile {
override fun transformFile(file: FirFile, data: Nothing?): FirFile {
withFile(file) {
return super.transformFile(file, data)
}
@@ -6,7 +6,9 @@
package org.jetbrains.kotlin.fir.extensions
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.extensions.predicate.AbstractPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import kotlin.reflect.KClass
@@ -38,8 +40,8 @@ data class FirExtensionPointName(val name: Name) {
// todo: KDOC
abstract class FirDeclarationPredicateRegistrar {
abstract fun register(vararg predicates: DeclarationPredicate)
abstract fun register(predicates: Collection<DeclarationPredicate>)
abstract fun register(vararg predicates: AbstractPredicate<*>)
abstract fun register(predicates: Collection<AbstractPredicate<*>>)
}
@RequiresOptIn
@@ -11,11 +11,13 @@ import org.jetbrains.kotlin.fir.FirSessionComponent
import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirFile
import org.jetbrains.kotlin.fir.extensions.predicate.AbstractPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
abstract class FirPredicateBasedProvider : FirSessionComponent {
abstract fun getSymbolsByPredicate(predicate: DeclarationPredicate): List<FirBasedSymbol<*>>
abstract fun getSymbolsByPredicate(predicate: LookupPredicate): List<FirBasedSymbol<*>>
abstract fun getOwnersOfDeclaration(declaration: FirDeclaration): List<FirBasedSymbol<*>>?
/**
@@ -23,17 +25,17 @@ abstract class FirPredicateBasedProvider : FirSessionComponent {
* @see FirRegisteredPluginAnnotations.annotations
*/
abstract fun fileHasPluginAnnotations(file: FirFile): Boolean
abstract fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean
abstract fun matches(predicate: AbstractPredicate<*>, declaration: FirDeclaration): Boolean
fun matches(predicate: DeclarationPredicate, declaration: FirBasedSymbol<*>): Boolean {
fun matches(predicate: AbstractPredicate<*>, declaration: FirBasedSymbol<*>): Boolean {
return matches(predicate, declaration.fir)
}
fun matches(predicates: List<DeclarationPredicate>, declaration: FirDeclaration): Boolean {
fun matches(predicates: List<AbstractPredicate<*>>, declaration: FirDeclaration): Boolean {
return predicates.any { matches(it, declaration) }
}
fun matches(predicates: List<DeclarationPredicate>, declaration: FirBasedSymbol<*>): Boolean {
fun matches(predicates: List<AbstractPredicate<*>>, declaration: FirBasedSymbol<*>): Boolean {
return matches(predicates, declaration.fir)
}
@@ -41,14 +43,14 @@ abstract class FirPredicateBasedProvider : FirSessionComponent {
}
@NoMutableState
class FirEmptyPredicateBasedProvider(): FirPredicateBasedProvider() {
override fun getSymbolsByPredicate(predicate: DeclarationPredicate): List<FirBasedSymbol<*>> = emptyList()
object FirEmptyPredicateBasedProvider : FirPredicateBasedProvider() {
override fun getSymbolsByPredicate(predicate: LookupPredicate): List<FirBasedSymbol<*>> = emptyList()
override fun getOwnersOfDeclaration(declaration: FirDeclaration): List<FirBasedSymbol<*>>? = null
override fun fileHasPluginAnnotations(file: FirFile): Boolean = false
override fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean = false
override fun matches(predicate: AbstractPredicate<*>, declaration: FirDeclaration): Boolean = false
}
val FirSession.predicateBasedProvider: FirPredicateBasedProvider by FirSession.sessionComponentAccessor()
@@ -0,0 +1,97 @@
/*
* 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.fir.extensions.predicate
import org.jetbrains.kotlin.fir.extensions.AnnotationFqn
sealed interface AbstractPredicate<P : AbstractPredicate<P>> {
val annotations: Set<AnnotationFqn>
val metaAnnotations: Set<AnnotationFqn>
fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R
sealed interface Or<P : AbstractPredicate<P>> : AbstractPredicate<P> {
val a: P
val b: P
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitOr(this, data)
}
}
sealed interface And<P : AbstractPredicate<P>> : AbstractPredicate<P> {
val a: P
val b: P
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitAnd(this, data)
}
}
// ------------------------------------ Annotated ------------------------------------
sealed interface Annotated<P : AbstractPredicate<P>> : AbstractPredicate<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitAnnotated(this, data)
}
}
sealed interface AnnotatedWith<P : AbstractPredicate<P>> : Annotated<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitAnnotatedWith(this, data)
}
}
sealed interface AncestorAnnotatedWith<P : AbstractPredicate<P>> : Annotated<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitAncestorAnnotatedWith(this, data)
}
}
sealed interface ParentAnnotatedWith<P : AbstractPredicate<P>> : Annotated<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitParentAnnotatedWith(this, data)
}
}
sealed interface HasAnnotatedWith<P : AbstractPredicate<P>> : Annotated<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitHasAnnotatedWith(this, data)
}
}
// ------------------------------------ MetaAnnotated ------------------------------------
sealed interface MetaAnnotatedWith<P : AbstractPredicate<P>> : AbstractPredicate<P> {
override fun <R, D> accept(visitor: PredicateVisitor<P, R, D>, data: D): R {
return visitor.visitMetaAnnotatedWith(this, data)
}
}
// -------------------------------------------- DSL --------------------------------------------
abstract class BuilderContext<P : AbstractPredicate<P>> {
abstract infix fun P.or(other: P): P
abstract infix fun P.and(other: P): P
// ------------------- varargs -------------------
abstract fun annotated(vararg annotations: AnnotationFqn): P
abstract fun ancestorAnnotated(vararg annotations: AnnotationFqn): P
abstract fun parentAnnotated(vararg annotations: AnnotationFqn): P
abstract fun hasAnnotated(vararg annotations: AnnotationFqn): P
abstract fun annotatedOrUnder(vararg annotations: AnnotationFqn): P
// ------------------- collections -------------------
abstract fun annotated(annotations: Collection<AnnotationFqn>): P
abstract fun ancestorAnnotated(annotations: Collection<AnnotationFqn>): P
abstract fun parentAnnotated(annotations: Collection<AnnotationFqn>): P
abstract fun hasAnnotated(annotations: Collection<AnnotationFqn>): P
abstract fun annotatedOrUnder(annotations: Collection<AnnotationFqn>): P
}
}
@@ -10,226 +10,215 @@ import org.jetbrains.kotlin.fir.extensions.AnnotationFqn
// -------------------------------------------- Predicates --------------------------------------------
// todo: Missing KDOC
sealed class DeclarationPredicate {
abstract val annotations: Set<AnnotationFqn>
abstract val metaAnnotations: Set<AnnotationFqn>
sealed class DeclarationPredicate : AbstractPredicate<DeclarationPredicate> {
abstract override val annotations: Set<AnnotationFqn>
abstract override val metaAnnotations: Set<AnnotationFqn>
abstract fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R
abstract override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R
class Or(val a: DeclarationPredicate, val b: DeclarationPredicate) : DeclarationPredicate() {
class Or(
override val a: DeclarationPredicate,
override val b: DeclarationPredicate
) : DeclarationPredicate(), AbstractPredicate.Or<DeclarationPredicate> {
override val annotations: Set<AnnotationFqn> = a.annotations + b.annotations
override val metaAnnotations: Set<AnnotationFqn> = a.metaAnnotations + b.metaAnnotations
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitOr(this, data)
}
}
class And(val a: DeclarationPredicate, val b: DeclarationPredicate) : DeclarationPredicate() {
class And(
override val a: DeclarationPredicate,
override val b: DeclarationPredicate
) : DeclarationPredicate(), AbstractPredicate.And<DeclarationPredicate> {
override val annotations: Set<AnnotationFqn> = a.annotations + b.annotations
override val metaAnnotations: Set<AnnotationFqn> = a.metaAnnotations + b.metaAnnotations
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitAnd(this, data)
}
}
}
// ------------------------------------ Annotated ------------------------------------
// ------------------------------------ Annotated ------------------------------------
/**
* Base class for all predicates with specific annotations
* Declaration will be matched if at least one of [annotations] is found
*/
sealed class Annotated(final override val annotations: Set<AnnotationFqn>) : DeclarationPredicate() {
init {
require(annotations.isNotEmpty()) {
"Annotations should be not empty"
/**
* Base class for all predicates with specific annotations
* Declaration will be matched if at least one of [annotations] is found
*/
sealed class Annotated(final override val annotations: Set<AnnotationFqn>) : DeclarationPredicate(),
AbstractPredicate.Annotated<DeclarationPredicate> {
init {
require(annotations.isNotEmpty()) {
"Annotations should be not empty"
}
}
final override val metaAnnotations: Set<AnnotationFqn>
get() = emptySet()
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitAnnotated(this, data)
}
}
final override val metaAnnotations: Set<AnnotationFqn>
get() = emptySet()
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitAnnotated(this, data)
}
}
/**
* Matches declarations, which are annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun foo, parameter `param` from fun bar, class A]
*/
class AnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if one of its containers annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested, fun Nested.foobar]
*/
class AncestorAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitAncestorAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if its direct container annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested]
*/
class ParentAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitParentAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if one of its direct child declarations annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* class A {
* @Ann
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun bar, class A]
*/
class HasAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitHasAnnotatedWith(this, data)
}
}
// ------------------------------------ MetaAnnotated ------------------------------------
sealed class MetaAnnotated(final override val metaAnnotations: Set<AnnotationFqn>) : DeclarationPredicate() {
init {
require(metaAnnotations.isNotEmpty()) {
"Annotations should be not empty"
/**
* Matches declarations, which are annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun foo, parameter `param` from fun bar, class A]
*/
class AnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations), AbstractPredicate.AnnotatedWith<DeclarationPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitAnnotatedWith(this, data)
}
}
final override val annotations: Set<AnnotationFqn>
get() = emptySet()
/**
* Matches declaration, if one of its containers annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested, fun Nested.foobar]
*/
class AncestorAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations),
AbstractPredicate.AncestorAnnotatedWith<DeclarationPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitAncestorAnnotatedWith(this, data)
}
}
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitMetaAnnotated(this, data)
/**
* Matches declaration, if its direct container annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested]
*/
class ParentAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations),
AbstractPredicate.ParentAnnotatedWith<DeclarationPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitParentAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if one of its direct child declarations annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* class A {
* @Ann
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun bar, class A]
*/
class HasAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations),
AbstractPredicate.HasAnnotatedWith<DeclarationPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitHasAnnotatedWith(this, data)
}
}
// ------------------------------------ MetaAnnotated ------------------------------------
class MetaAnnotatedWith(
override val metaAnnotations: Set<AnnotationFqn>
) : DeclarationPredicate(), AbstractPredicate.MetaAnnotatedWith<DeclarationPredicate> {
init {
require(metaAnnotations.isNotEmpty()) {
"Annotations should be not empty"
}
}
override val annotations: Set<AnnotationFqn>
get() = emptySet()
override fun <R, D> accept(visitor: PredicateVisitor<DeclarationPredicate, R, D>, data: D): R {
return visitor.visitMetaAnnotatedWith(this, data)
}
}
// -------------------------------------------- DSL --------------------------------------------
object BuilderContext : AbstractPredicate.BuilderContext<DeclarationPredicate>() {
override infix fun DeclarationPredicate.or(other: DeclarationPredicate): DeclarationPredicate = Or(this, other)
override infix fun DeclarationPredicate.and(other: DeclarationPredicate): DeclarationPredicate = And(this, other)
// ------------------- varargs -------------------
override fun annotated(vararg annotations: AnnotationFqn): DeclarationPredicate = annotated(annotations.toList())
override fun ancestorAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = ancestorAnnotated(annotations.toList())
override fun parentAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = parentAnnotated(annotations.toList())
override fun hasAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = hasAnnotated(annotations.toList())
override fun annotatedOrUnder(vararg annotations: AnnotationFqn): DeclarationPredicate =
annotated(*annotations) or ancestorAnnotated(*annotations)
fun metaAnnotated(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate = MetaAnnotatedWith(metaAnnotations.toSet())
// ------------------- collections -------------------
override fun annotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = AnnotatedWith(annotations.toSet())
override fun ancestorAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate =
AncestorAnnotatedWith(annotations.toSet())
override fun parentAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate =
ParentAnnotatedWith(annotations.toSet())
override fun hasAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = HasAnnotatedWith(annotations.toSet())
override fun annotatedOrUnder(annotations: Collection<AnnotationFqn>): DeclarationPredicate =
annotated(annotations) or ancestorAnnotated(annotations)
fun metaAnnotated(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate = MetaAnnotatedWith(metaAnnotations.toSet())
}
companion object {
inline fun create(init: BuilderContext.() -> DeclarationPredicate): DeclarationPredicate = BuilderContext.init()
}
}
class MetaAnnotatedWith(metaAnnotations: Set<AnnotationFqn>) : MetaAnnotated(metaAnnotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitMetaAnnotatedWith(this, data)
}
}
class AncestorMetaAnnotatedWith(metaAnnotations: Set<AnnotationFqn>) : MetaAnnotated(metaAnnotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitAncestorMetaAnnotatedWith(this, data)
}
}
class ParentMetaAnnotatedWith(metaAnnotations: Set<AnnotationFqn>) : MetaAnnotated(metaAnnotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitParentMetaAnnotatedWith(this, data)
}
}
class HasMetaAnnotatedWith(metaAnnotations: Set<AnnotationFqn>) : MetaAnnotated(metaAnnotations) {
override fun <R, D> accept(visitor: DeclarationPredicateVisitor<R, D>, data: D): R {
return visitor.visitHasMetaAnnotatedWith(this, data)
}
}
// -------------------------------------------- DSL --------------------------------------------
infix fun DeclarationPredicate.or(other: DeclarationPredicate): DeclarationPredicate = DeclarationPredicate.Or(this, other)
infix fun DeclarationPredicate.and(other: DeclarationPredicate): DeclarationPredicate = DeclarationPredicate.And(this, other)
// ------------------- varargs -------------------
fun annotated(vararg annotations: AnnotationFqn): DeclarationPredicate = AnnotatedWith(annotations.toSet())
fun ancestorAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = AncestorAnnotatedWith(annotations.toSet())
fun parentAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = ParentAnnotatedWith(annotations.toSet())
fun hasAnnotated(vararg annotations: AnnotationFqn): DeclarationPredicate = HasAnnotatedWith(annotations.toSet())
fun metaAnnotated(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate = MetaAnnotatedWith(metaAnnotations.toSet())
fun metaAncestorAnnotated(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate = AncestorMetaAnnotatedWith(metaAnnotations.toSet())
fun parentMetaAnnotated(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate = ParentMetaAnnotatedWith(metaAnnotations.toSet())
fun hasMetaAnnotated(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate = HasMetaAnnotatedWith(metaAnnotations.toSet())
fun annotatedOrUnder(vararg annotations: AnnotationFqn): DeclarationPredicate = annotated(*annotations) or ancestorAnnotated(*annotations)
fun metaAnnotatedOrUnder(vararg metaAnnotations: AnnotationFqn): DeclarationPredicate =
metaAnnotated(*metaAnnotations) or metaAncestorAnnotated(*metaAnnotations)
// ------------------- collections -------------------
fun annotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = AnnotatedWith(annotations.toSet())
fun ancestorAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = AncestorAnnotatedWith(annotations.toSet())
fun parentAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = ParentAnnotatedWith(annotations.toSet())
fun hasAnnotated(annotations: Collection<AnnotationFqn>): DeclarationPredicate = HasAnnotatedWith(annotations.toSet())
fun metaAnnotated(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate = MetaAnnotatedWith(metaAnnotations.toSet())
fun metaAncestorAnnotated(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate = AncestorMetaAnnotatedWith(metaAnnotations.toSet())
fun parentMetaAnnotated(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate = ParentMetaAnnotatedWith(metaAnnotations.toSet())
fun hasMetaAnnotated(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate = HasMetaAnnotatedWith(metaAnnotations.toSet())
fun annotatedOrUnder(annotations: Collection<AnnotationFqn>): DeclarationPredicate = annotated(annotations) or ancestorAnnotated(annotations)
fun metaAnnotatedOrUnder(metaAnnotations: Collection<AnnotationFqn>): DeclarationPredicate =
metaAnnotated(metaAnnotations) or metaAncestorAnnotated(metaAnnotations)
@@ -1,58 +0,0 @@
/*
* Copyright 2010-2021 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.fir.extensions.predicate
abstract class DeclarationPredicateVisitor<R, D> {
abstract fun visitPredicate(predicate: DeclarationPredicate, data: D): R
open fun visitAnd(predicate: DeclarationPredicate.And, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitOr(predicate: DeclarationPredicate.Or, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitAnnotated(predicate: Annotated, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitAnnotatedWith(predicate: AnnotatedWith, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitAncestorAnnotatedWith(predicate: AncestorAnnotatedWith, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitParentAnnotatedWith(predicate: ParentAnnotatedWith, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitHasAnnotatedWith(predicate: HasAnnotatedWith, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitMetaAnnotated(predicate: MetaAnnotated, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitMetaAnnotatedWith(predicate: MetaAnnotatedWith, data: D): R {
return visitMetaAnnotated(predicate, data)
}
open fun visitAncestorMetaAnnotatedWith(predicate: AncestorMetaAnnotatedWith, data: D): R {
return visitMetaAnnotated(predicate, data)
}
open fun visitParentMetaAnnotatedWith(predicate: ParentMetaAnnotatedWith, data: D): R {
return visitMetaAnnotated(predicate, data)
}
open fun visitHasMetaAnnotatedWith(predicate: HasMetaAnnotatedWith, data: D): R {
return visitMetaAnnotated(predicate, data)
}
}
@@ -0,0 +1,183 @@
/*
* 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.fir.extensions.predicate
import org.jetbrains.kotlin.fir.extensions.AnnotationFqn
sealed class LookupPredicate : AbstractPredicate<LookupPredicate> {
abstract override val annotations: Set<AnnotationFqn>
final override val metaAnnotations: Set<AnnotationFqn>
get() = error("Should not be called")
abstract override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R
class Or(
override val a: LookupPredicate,
override val b: LookupPredicate
) : LookupPredicate(), AbstractPredicate.Or<LookupPredicate> {
override val annotations: Set<AnnotationFqn> = a.annotations + b.annotations
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitOr(this, data)
}
}
class And(
override val a: LookupPredicate,
override val b: LookupPredicate
) : LookupPredicate(), AbstractPredicate.And<LookupPredicate> {
override val annotations: Set<AnnotationFqn> = a.annotations + b.annotations
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitAnd(this, data)
}
}
/**
* Base class for all predicates with specific annotations
* Declaration will be matched if at least one of [annotations] is found
*/
sealed class Annotated(final override val annotations: Set<AnnotationFqn>) : LookupPredicate(), AbstractPredicate.Annotated<LookupPredicate> {
init {
require(annotations.isNotEmpty()) {
"Annotations should be not empty"
}
}
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitAnnotated(this, data)
}
}
/**
* Matches declarations, which are annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun foo, parameter `param` from fun bar, class A]
*/
class AnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations), AbstractPredicate.AnnotatedWith<LookupPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if one of its containers annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested, fun Nested.foobar]
*/
class AncestorAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations), AbstractPredicate.AncestorAnnotatedWith<LookupPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitAncestorAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if its direct container annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* @Ann
* class A {
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun A.baz, class Nested]
*/
class ParentAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations), AbstractPredicate.ParentAnnotatedWith<LookupPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitParentAnnotatedWith(this, data)
}
}
/**
* Matches declaration, if one of its direct child declarations annotated with [annotations]
*
* @Ann
* fun foo() {}
*
* fun bar(@Ann param: Int) {}
*
* class A {
* @Ann
* fun baz() {}
*
* class Nested {
* fun foobar() {}
* }
* }
*
* Matched symbols: [fun bar, class A]
*/
class HasAnnotatedWith(annotations: Set<AnnotationFqn>) : Annotated(annotations), AbstractPredicate.HasAnnotatedWith<LookupPredicate> {
override fun <R, D> accept(visitor: PredicateVisitor<LookupPredicate, R, D>, data: D): R {
return visitor.visitHasAnnotatedWith(this, data)
}
}
// -------------------------------------------- DSL --------------------------------------------
object BuilderContext : AbstractPredicate.BuilderContext<LookupPredicate>() {
override infix fun LookupPredicate.or(other: LookupPredicate): LookupPredicate = Or(this, other)
override infix fun LookupPredicate.and(other: LookupPredicate): LookupPredicate = And(this, other)
// ------------------- varargs -------------------
override fun annotated(vararg annotations: AnnotationFqn): LookupPredicate = annotated(annotations.toList())
override fun ancestorAnnotated(vararg annotations: AnnotationFqn): LookupPredicate = ancestorAnnotated(annotations.toList())
override fun parentAnnotated(vararg annotations: AnnotationFqn): LookupPredicate = parentAnnotated(annotations.toList())
override fun hasAnnotated(vararg annotations: AnnotationFqn): LookupPredicate = hasAnnotated(annotations.toList())
override fun annotatedOrUnder(vararg annotations: AnnotationFqn): LookupPredicate =
annotated(*annotations) or ancestorAnnotated(*annotations)
// ------------------- collections -------------------
override fun annotated(annotations: Collection<AnnotationFqn>): LookupPredicate = AnnotatedWith(annotations.toSet())
override fun ancestorAnnotated(annotations: Collection<AnnotationFqn>): LookupPredicate = AncestorAnnotatedWith(annotations.toSet())
override fun parentAnnotated(annotations: Collection<AnnotationFqn>): LookupPredicate = ParentAnnotatedWith(annotations.toSet())
override fun hasAnnotated(annotations: Collection<AnnotationFqn>): LookupPredicate = HasAnnotatedWith(annotations.toSet())
override fun annotatedOrUnder(annotations: Collection<AnnotationFqn>): LookupPredicate =
annotated(annotations) or ancestorAnnotated(annotations)
}
companion object {
inline fun create(init: BuilderContext.() -> LookupPredicate): LookupPredicate = BuilderContext.init()
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2021 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.fir.extensions.predicate
abstract class PredicateVisitor<P : AbstractPredicate<P>, R, D> {
abstract fun visitPredicate(predicate: AbstractPredicate<P>, data: D): R
open fun visitAnd(predicate: AbstractPredicate.And<P>, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitOr(predicate: AbstractPredicate.Or<P>, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitAnnotated(predicate: AbstractPredicate.Annotated<P>, data: D): R {
return visitPredicate(predicate, data)
}
open fun visitAnnotatedWith(predicate: AbstractPredicate.AnnotatedWith<P>, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitAncestorAnnotatedWith(predicate: AbstractPredicate.AncestorAnnotatedWith<P>, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitParentAnnotatedWith(predicate: AbstractPredicate.ParentAnnotatedWith<P>, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitHasAnnotatedWith(predicate: AbstractPredicate.HasAnnotatedWith<P>, data: D): R {
return visitAnnotated(predicate, data)
}
open fun visitMetaAnnotatedWith(predicate: AbstractPredicate.MetaAnnotatedWith<P>, data: D): R {
return visitPredicate(predicate, data)
}
}
typealias DeclarationPredicateVisitor<R, D> = PredicateVisitor<DeclarationPredicate, R, D>
typealias LookupPredicateVisitor<R, D> = PredicateVisitor<LookupPredicate, R, D>