[FIR] Move FirPredicateBasedProvider to :compiler:fir:tree module

This commit is contained in:
Dmitriy Novozhilov
2022-05-20 11:43:22 +03:00
committed by teamcity
parent b948eaef4b
commit 7048cac770
3 changed files with 55 additions and 41 deletions
@@ -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<FirBasedSymbol<*>>
abstract fun getOwnersOfDeclaration(declaration: FirDeclaration): List<FirBasedSymbol<*>>?
/**
* @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<DeclarationPredicate>, declaration: FirDeclaration): Boolean {
return predicates.any { matches(it, declaration) }
}
fun matches(predicates: List<DeclarationPredicate>, declaration: FirBasedSymbol<*>): Boolean {
return matches(predicates, declaration.fir)
}
open fun registerAnnotatedDeclaration(declaration: FirDeclaration, owners: PersistentList<FirDeclaration>) {}
}
@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<FirFile> = mutableSetOf()
}
}
@NoMutableState
class FirEmptyPredicateBasedProvider(): FirPredicateBasedProvider() {
override fun getSymbolsByPredicate(predicate: DeclarationPredicate): 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
}
val FirSession.predicateBasedProvider: FirPredicateBasedProvider by FirSession.sessionComponentAccessor()
@@ -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<FirBasedSymbol<*>>
abstract fun getOwnersOfDeclaration(declaration: FirDeclaration): List<FirBasedSymbol<*>>?
/**
* @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<DeclarationPredicate>, declaration: FirDeclaration): Boolean {
return predicates.any { matches(it, declaration) }
}
fun matches(predicates: List<DeclarationPredicate>, declaration: FirBasedSymbol<*>): Boolean {
return matches(predicates, declaration.fir)
}
open fun registerAnnotatedDeclaration(declaration: FirDeclaration, owners: PersistentList<FirDeclaration>) {}
}
@NoMutableState
class FirEmptyPredicateBasedProvider(): FirPredicateBasedProvider() {
override fun getSymbolsByPredicate(predicate: DeclarationPredicate): 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
}
val FirSession.predicateBasedProvider: FirPredicateBasedProvider by FirSession.sessionComponentAccessor()
+1 -1
View File
@@ -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)`)