org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* 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
/**
* For reference read KDoc to [AbstractPredicate]
* @see [AbstractPredicate]
*/
sealed class LookupPredicate : AbstractPredicate {
abstract override val annotations: Set
final override val metaAnnotations: Set
get() = emptySet()
abstract override fun accept(visitor: PredicateVisitor, data: D): R
class Or(
override val a: LookupPredicate,
override val b: LookupPredicate
) : LookupPredicate(), AbstractPredicate.Or {
override val annotations: Set = a.annotations + b.annotations
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitOr(this, data)
}
}
class And(
override val a: LookupPredicate,
override val b: LookupPredicate
) : LookupPredicate(), AbstractPredicate.And {
override val annotations: Set = a.annotations + b.annotations
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitAnd(this, data)
}
}
sealed class Annotated(final override val annotations: Set) : LookupPredicate(), AbstractPredicate.Annotated {
init {
require(annotations.isNotEmpty()) {
"Annotations should be not empty"
}
}
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitAnnotated(this, data)
}
}
class AnnotatedWith(annotations: Set) : Annotated(annotations), AbstractPredicate.AnnotatedWith {
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitAnnotatedWith(this, data)
}
}
class AncestorAnnotatedWith(annotations: Set) : Annotated(annotations), AbstractPredicate.AncestorAnnotatedWith {
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitAncestorAnnotatedWith(this, data)
}
}
class ParentAnnotatedWith(annotations: Set) : Annotated(annotations), AbstractPredicate.ParentAnnotatedWith {
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitParentAnnotatedWith(this, data)
}
}
class HasAnnotatedWith(annotations: Set) : Annotated(annotations), AbstractPredicate.HasAnnotatedWith {
override fun accept(visitor: PredicateVisitor, data: D): R {
return visitor.visitHasAnnotatedWith(this, data)
}
}
// -------------------------------------------- DSL --------------------------------------------
object BuilderContext : AbstractPredicate.BuilderContext() {
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): LookupPredicate = AnnotatedWith(annotations.toSet())
override fun ancestorAnnotated(annotations: Collection): LookupPredicate = AncestorAnnotatedWith(annotations.toSet())
override fun parentAnnotated(annotations: Collection): LookupPredicate = ParentAnnotatedWith(annotations.toSet())
override fun hasAnnotated(annotations: Collection): LookupPredicate = HasAnnotatedWith(annotations.toSet())
override fun annotatedOrUnder(annotations: Collection): LookupPredicate =
annotated(annotations) or ancestorAnnotated(annotations)
}
companion object {
inline fun create(init: BuilderContext.() -> LookupPredicate): LookupPredicate = BuilderContext.init()
}
}