diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProviderImpl.kt similarity index 77% rename from compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt rename to compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProviderImpl.kt index 871268ca937..8c03496efae 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProviderImpl.kt @@ -9,7 +9,6 @@ import com.google.common.collect.LinkedHashMultimap import com.google.common.collect.Multimap import kotlinx.collections.immutable.PersistentList import org.jetbrains.kotlin.fir.FirSession -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 @@ -17,32 +16,6 @@ import org.jetbrains.kotlin.fir.extensions.predicate.* import org.jetbrains.kotlin.fir.resolve.fqName import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol -abstract class FirPredicateBasedProvider : FirSessionComponent { - abstract fun getSymbolsByPredicate(predicate: DeclarationPredicate): List> - abstract fun getOwnersOfDeclaration(declaration: FirDeclaration): List>? - - /** - * @return `true` iff file has a top-level annotation from the [FirRegisteredPluginAnnotations.annotations] list. - * @see FirRegisteredPluginAnnotations.annotations - */ - abstract fun fileHasPluginAnnotations(file: FirFile): Boolean - abstract fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean - - fun matches(predicate: DeclarationPredicate, declaration: FirBasedSymbol<*>): Boolean { - return matches(predicate, declaration.fir) - } - - fun matches(predicates: List, declaration: FirDeclaration): Boolean { - return predicates.any { matches(it, declaration) } - } - - fun matches(predicates: List, declaration: FirBasedSymbol<*>): Boolean { - return matches(predicates, declaration.fir) - } - - open fun registerAnnotatedDeclaration(declaration: FirDeclaration, owners: PersistentList) {} -} - @NoMutableState class FirPredicateBasedProviderImpl(private val session: FirSession) : FirPredicateBasedProvider() { private val registeredPluginAnnotations = session.registeredPluginAnnotations @@ -156,16 +129,3 @@ class FirPredicateBasedProviderImpl(private val session: FirSession) : FirPredic val filesWithPluginAnnotations: MutableSet = mutableSetOf() } } - -@NoMutableState -class FirEmptyPredicateBasedProvider(): FirPredicateBasedProvider() { - override fun getSymbolsByPredicate(predicate: DeclarationPredicate): List> = emptyList() - - override fun getOwnersOfDeclaration(declaration: FirDeclaration): List>? = null - - override fun fileHasPluginAnnotations(file: FirFile): Boolean = false - - override fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean = false -} - -val FirSession.predicateBasedProvider: FirPredicateBasedProvider by FirSession.sessionComponentAccessor() diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt new file mode 100644 index 00000000000..bb5604833b6 --- /dev/null +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt @@ -0,0 +1,54 @@ +/* + * 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 + +import kotlinx.collections.immutable.PersistentList +import org.jetbrains.kotlin.fir.FirSession +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.DeclarationPredicate +import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol + +abstract class FirPredicateBasedProvider : FirSessionComponent { + abstract fun getSymbolsByPredicate(predicate: DeclarationPredicate): List> + abstract fun getOwnersOfDeclaration(declaration: FirDeclaration): List>? + + /** + * @return `true` iff file has a top-level annotation from the [FirRegisteredPluginAnnotations.annotations] list. + * @see FirRegisteredPluginAnnotations.annotations + */ + abstract fun fileHasPluginAnnotations(file: FirFile): Boolean + abstract fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean + + fun matches(predicate: DeclarationPredicate, declaration: FirBasedSymbol<*>): Boolean { + return matches(predicate, declaration.fir) + } + + fun matches(predicates: List, declaration: FirDeclaration): Boolean { + return predicates.any { matches(it, declaration) } + } + + fun matches(predicates: List, declaration: FirBasedSymbol<*>): Boolean { + return matches(predicates, declaration.fir) + } + + open fun registerAnnotatedDeclaration(declaration: FirDeclaration, owners: PersistentList) {} +} + +@NoMutableState +class FirEmptyPredicateBasedProvider(): FirPredicateBasedProvider() { + override fun getSymbolsByPredicate(predicate: DeclarationPredicate): List> = emptyList() + + override fun getOwnersOfDeclaration(declaration: FirDeclaration): List>? = null + + override fun fileHasPluginAnnotations(file: FirFile): Boolean = false + + override fun matches(predicate: DeclarationPredicate, declaration: FirDeclaration): Boolean = false +} + +val FirSession.predicateBasedProvider: FirPredicateBasedProvider by FirSession.sessionComponentAccessor() diff --git a/docs/fir/fir-plugins.md b/docs/fir/fir-plugins.md index d06737d6607..0fa5563ea94 100644 --- a/docs/fir/fir-plugins.md +++ b/docs/fir/fir-plugins.md @@ -8,7 +8,7 @@ Generally, FIR compiler supports search for declarations only with a specific cl _Note:_ there are plans to design some new syntax for passing information from code to plugins instead of annotations, because they have some problems and limitations due to the compiler design reasons. -There is a special service in FIR named [FirPredicateBasedProvider](https://github.com/JetBrains/kotlin/blob/master/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt). It allows to find all declarations in compiled code which match some [DeclarationPredicate](https://github.com/JetBrains/kotlin/blob/master/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/predicate/DeclarationPredicate.kt). +There is a special service in FIR named [FirPredicateBasedProvider](https://github.com/JetBrains/kotlin/blob/master/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/FirPredicateBasedProvider.kt). It allows to find all declarations in compiled code which match some [DeclarationPredicate](https://github.com/JetBrains/kotlin/blob/master/compiler/fir/tree/src/org/jetbrains/kotlin/fir/extensions/predicate/DeclarationPredicate.kt). There are multiple types of predicates and each one has DSL functions to create them (in parenthesis): - `AnnotatedWith` matches all declarations which have on of annotations passed to it (`has(vararg annotations: FqName)`)