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

org.jetbrains.kotlin.resolve.lazy.declarations.AbstractPsiBasedDeclarationProvider.kt Maven / Gradle / Ivy

There is a newer version: 2.0.20-RC
Show newest version
/*
 * Copyright 2010-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.resolve.lazy.declarations

import com.google.common.collect.ArrayListMultimap
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.lazy.ResolveSessionUtils.safeNameForLazyResolve
import org.jetbrains.kotlin.resolve.lazy.data.KtClassInfoUtil
import org.jetbrains.kotlin.resolve.lazy.data.KtClassLikeInfo
import org.jetbrains.kotlin.resolve.lazy.data.KtScriptInfo
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.storage.StorageManager
import java.util.*

abstract class AbstractPsiBasedDeclarationProvider(storageManager: StorageManager) : DeclarationProvider {

    protected class Index {
        // This mutable state is only modified under inside the computable
        val allDeclarations = ArrayList()
        val functions = ArrayListMultimap.create()
        val properties = ArrayListMultimap.create()
        val classesAndObjects = ArrayListMultimap.create() // order matters here
        val typeAliases = ArrayListMultimap.create()
        val destructuringDeclarationsEntries = ArrayListMultimap.create()

        fun putToIndex(declaration: KtDeclaration) {
            if (declaration is KtAnonymousInitializer || declaration is KtSecondaryConstructor) return

            allDeclarations.add(declaration)
            when (declaration) {
                is KtNamedFunction ->
                    functions.put(safeNameForLazyResolve(declaration), declaration)
                is KtProperty ->
                    properties.put(safeNameForLazyResolve(declaration), declaration)
                is KtTypeAlias ->
                    typeAliases.put(safeNameForLazyResolve(declaration.nameAsName), declaration)
                is KtClassOrObject ->
                    classesAndObjects.put(safeNameForLazyResolve(declaration.nameAsName), KtClassInfoUtil.createClassLikeInfo(declaration))
                is KtScript -> {
                    val scriptInfo = KtScriptInfo(declaration)
                    classesAndObjects.put(scriptInfo.script.nameAsName, scriptInfo)
                }
                is KtDestructuringDeclaration -> {
                    for (entry in declaration.entries) {
                        destructuringDeclarationsEntries.put(safeNameForLazyResolve(entry.nameAsName), entry)
                    }
                }
                is KtParameter -> {
                    // Do nothing, just put it into allDeclarations is enough
                }
                else -> throw IllegalArgumentException("Unknown declaration: " + declaration)
            }
        }

        override fun toString() = "allDeclarations: " + allDeclarations.mapNotNull { it.name }
    }

    private val index = storageManager.createLazyValue {
        val index = Index()
        doCreateIndex(index)
        index
    }

    protected abstract fun doCreateIndex(index: Index)

    internal fun toInfoString() = toString() + ": " + index().toString()

    override fun getDeclarations(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean): List
            = index().allDeclarations

    override fun getFunctionDeclarations(name: Name): List
            = index().functions[safeNameForLazyResolve(name)].toList()

    override fun getPropertyDeclarations(name: Name): List
            = index().properties[safeNameForLazyResolve(name)].toList()

    override fun getDestructuringDeclarationsEntries(name: Name): Collection
            = index().destructuringDeclarationsEntries[safeNameForLazyResolve(name)].toList()

    override fun getClassOrObjectDeclarations(name: Name): Collection
            = index().classesAndObjects[safeNameForLazyResolve(name)]

    override fun getTypeAliasDeclarations(name: Name): Collection
            = index().typeAliases[safeNameForLazyResolve(name)]
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy