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

org.jetbrains.kotlin.analysis.api.fir.scopes.FirCallableFilteringScope.kt Maven / Gradle / Ivy

There is a newer version: 2.0.20
Show newest version
/*
 * Copyright 2010-2023 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.analysis.api.fir.scopes

import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.fir.scopes.processAllCallables
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.name.Name

/**
 * A base implementation for [FirNonStaticMembersScope] and [FirJavaDeclaredMembersOnlyScope], which both filter callables based on some
 * condition.
 */
internal abstract class FirCallableFilteringScope(private val baseScope: FirContainingNamesAwareScope) : FirContainingNamesAwareScope() {
    protected abstract fun isTargetCallable(callable: FirCallableSymbol<*>): Boolean

    private val cachedCallableNames by lazy(LazyThreadSafetyMode.NONE) {
        buildSet {
            baseScope.processAllCallables { callable ->
                if (isTargetCallable(callable)) {
                    add(callable.name)
                }
            }
        }
    }

    override fun getCallableNames(): Set = cachedCallableNames

    override fun processFunctionsByName(name: Name, processor: (FirNamedFunctionSymbol) -> Unit) {
        if (!cachedCallableNames.contains(name)) return

        baseScope.processFunctionsByName(name) { function ->
            if (isTargetCallable(function)) {
                processor(function)
            }
        }
    }

    override fun processPropertiesByName(name: Name, processor: (FirVariableSymbol<*>) -> Unit) {
        if (!cachedCallableNames.contains(name)) return

        baseScope.processPropertiesByName(name) { property ->
            if (isTargetCallable(property)) {
                processor(property)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy