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

org.jetbrains.kotlin.fir.scopes.impl.FirClassUseSiteMemberScope.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
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.scopes.impl

import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.utils.classId
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope
import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.Name

class FirClassUseSiteMemberScope(
    klass: FirClass,
    session: FirSession,
    superTypeScopes: List,
    declaredMemberScope: FirContainingNamesAwareScope
) : AbstractFirUseSiteMemberScope(
    klass.classId,
    session,
    FirStandardOverrideChecker(session),
    superTypeScopes,
    klass.defaultType(),
    declaredMemberScope
) {
    override fun collectProperties(name: Name): Collection> {
        return buildList {
            val explicitlyDeclaredProperties = mutableSetOf>()
            declaredMemberScope.processPropertiesByName(name) { symbol ->
                if (symbol.isStatic) return@processPropertiesByName
                if (symbol is FirPropertySymbol) {
                    val directOverridden = computeDirectOverriddenForDeclaredProperty(symbol)
                    directOverriddenProperties[symbol] = directOverridden
                }
                explicitlyDeclaredProperties += symbol
                add(symbol)
            }


            val (properties, fields) = getPropertiesAndFieldsFromSupertypesByName(name)
            for (propertyFromSupertype in properties) {
                val superSymbol = propertyFromSupertype.extractSomeSymbolFromSuperType()
                val overriddenBy = superSymbol.getOverridden(explicitlyDeclaredProperties)
                if (overriddenBy == null) {
                    add(propertyFromSupertype.chosenSymbol)
                }
            }
            addAll(fields)
        }
    }

    private fun computeDirectOverriddenForDeclaredProperty(declaredPropertySymbol: FirPropertySymbol): List> {
        val result = mutableListOf>()
        val declaredProperty = declaredPropertySymbol.fir
        for (resultOfIntersection in getPropertiesAndFieldsFromSupertypesByName(declaredPropertySymbol.name).first) {
            val symbolFromSupertype = resultOfIntersection.extractSomeSymbolFromSuperType()
            if (overrideChecker.isOverriddenProperty(declaredProperty, symbolFromSupertype.fir)) {
                result.add(resultOfIntersection)
            }
        }
        return result
    }

    private fun getPropertiesAndFieldsFromSupertypesByName(name: Name): Pair>, List> {
        propertiesFromSupertypes[name]?.let {
            return it to fieldsFromSupertypes.getValue(name)
        }

        val fields = mutableListOf()
        val properties = supertypeScopeContext.collectIntersectionResultsForCallables(name) { propertyName, processor ->
            processPropertiesByName(propertyName) {
                when (it) {
                    is FirPropertySymbol -> processor(it)
                    is FirFieldSymbol -> fields += it
                    else -> {}
                }
            }
        }
        propertiesFromSupertypes[name] = properties
        fieldsFromSupertypes[name] = fields
        return properties to fields
    }

    override fun FirNamedFunctionSymbol.isVisibleInCurrentClass(): Boolean {
        return true
    }

    override fun toString(): String {
        return "Use site scope of $classId"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy