All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.fir.extensions.predicate.DeclarationPredicate.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * 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

import org.jetbrains.kotlin.fir.extensions.AnnotationFqn

// -------------------------------------------- Predicates --------------------------------------------

/**
 * For reference read KDoc to [AbstractPredicate]
 * @see [AbstractPredicate]
 */
sealed class DeclarationPredicate : AbstractPredicate {
    abstract override val annotations: Set
    abstract override val metaAnnotations: Set

    abstract override fun  accept(visitor: PredicateVisitor, data: D): R

    class Or(
        override val a: DeclarationPredicate,
        override val b: DeclarationPredicate
    ) : DeclarationPredicate(), AbstractPredicate.Or {
        override val annotations: Set = a.annotations + b.annotations
        override val metaAnnotations: Set = a.metaAnnotations + b.metaAnnotations

        override fun  accept(visitor: PredicateVisitor, data: D): R {
            return visitor.visitOr(this, data)
        }
    }

    class And(
        override val a: DeclarationPredicate,
        override val b: DeclarationPredicate
    ) : DeclarationPredicate(), AbstractPredicate.And {
        override val annotations: Set = a.annotations + b.annotations
        override val metaAnnotations: Set = a.metaAnnotations + b.metaAnnotations

        override fun  accept(visitor: PredicateVisitor, data: D): R {
            return visitor.visitAnd(this, data)
        }
    }

    // ------------------------------------ Annotated ------------------------------------

    sealed class Annotated(final override val annotations: Set) : DeclarationPredicate(),
        AbstractPredicate.Annotated {
        init {
            require(annotations.isNotEmpty()) {
                "Annotations should be not empty"
            }
        }

        final override val metaAnnotations: Set
            get() = emptySet()

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

    // ------------------------------------ MetaAnnotated ------------------------------------

    class MetaAnnotatedWith(
        override val metaAnnotations: Set,
        override val includeItself: Boolean
    ) : DeclarationPredicate(), AbstractPredicate.MetaAnnotatedWith {
        init {
            require(metaAnnotations.isNotEmpty()) {
                "Annotations should be not empty"
            }
        }

        override val annotations: Set
            get() = emptySet()

        override fun  accept(visitor: PredicateVisitor, data: D): R {
            return visitor.visitMetaAnnotatedWith(this, data)
        }
    }

    // -------------------------------------------- DSL --------------------------------------------

    object BuilderContext : AbstractPredicate.BuilderContext() {
        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, includeItself: Boolean): DeclarationPredicate =
            MetaAnnotatedWith(metaAnnotations.toSet(), includeItself)

        // ------------------- collections -------------------
        override fun annotated(annotations: Collection): DeclarationPredicate = AnnotatedWith(annotations.toSet())
        override fun ancestorAnnotated(annotations: Collection): DeclarationPredicate =
            AncestorAnnotatedWith(annotations.toSet())

        override fun parentAnnotated(annotations: Collection): DeclarationPredicate =
            ParentAnnotatedWith(annotations.toSet())

        override fun hasAnnotated(annotations: Collection): DeclarationPredicate = HasAnnotatedWith(annotations.toSet())

        override fun annotatedOrUnder(annotations: Collection): DeclarationPredicate =
            annotated(annotations) or ancestorAnnotated(annotations)

        fun metaAnnotated(metaAnnotations: Collection, includeItself: Boolean): DeclarationPredicate =
            MetaAnnotatedWith(metaAnnotations.toSet(), includeItself)
    }

    companion object {
        inline fun create(init: BuilderContext.() -> DeclarationPredicate): DeclarationPredicate = BuilderContext.init()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy