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

org.jetbrains.kotlin.fir.analysis.checkers.expression.FirSuperclassNotAccessibleFromInterfaceChecker.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 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.analysis.checkers.expression

import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSourceElement
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.declarations.FirClassLikeDeclaration
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.FirSuperReference
import org.jetbrains.kotlin.fir.resolve.firSymbolProvider
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

object FirSuperclassNotAccessibleFromInterfaceChecker : FirQualifiedAccessChecker() {
    override fun check(expression: FirQualifiedAccessExpression, context: CheckerContext, reporter: DiagnosticReporter) {
        expression.explicitReceiver.safeAs()
            ?.calleeReference.safeAs()
            ?: return

        val closestClass = context.findClosest() ?: return

        if (closestClass.classKind == ClassKind.INTERFACE) {
            val origin = getClassLikeDeclaration(expression, context)
                ?.symbol.safeAs()
                ?.fir
                ?: return

            if (origin.source != null && origin.classKind == ClassKind.CLASS) {
                reporter.report(expression.explicitReceiver?.source)
            }
        }
    }

    /**
     * Returns the ClassLikeDeclaration where the function has been defined
     * or null if no proper declaration has been found.
     */
    private fun getClassLikeDeclaration(functionCall: FirQualifiedAccessExpression, context: CheckerContext): FirClassLikeDeclaration<*>? {
        val classId = functionCall.calleeReference.safeAs()
            ?.resolvedSymbol.safeAs()
            ?.callableId
            ?.classId
            ?: return null

        if (!classId.isLocal) {
            return context.session.firSymbolProvider.getClassLikeSymbolByFqName(classId)?.fir
        }

        return null
    }

    private fun DiagnosticReporter.report(source: FirSourceElement?) {
        source?.let {
            report(FirErrors.SUPERCLASS_NOT_ACCESSIBLE_FROM_INTERFACE.on(it))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy