summaryrefslogtreecommitdiff
path: root/plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/caches/project/KotlinStdlibCache.kt
blob: 6ec04c971ffc2204476e170565d9cecc3fa43d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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.idea.caches.project

import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.LibraryOrderEntry
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.roots.impl.libraries.LibraryEx
import com.intellij.openapi.util.ThrowableComputable
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.DelegatingGlobalSearchScope
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.indexing.DumbModeAccessType
import com.intellij.util.indexing.FileBasedIndex
import org.jetbrains.kotlin.caches.project.cacheInvalidatingOnRootModifications
import org.jetbrains.kotlin.idea.configuration.IdeBuiltInsLoadingState
import org.jetbrains.kotlin.idea.vfilefinder.KotlinStdlibIndex
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.util.concurrent.ConcurrentHashMap

// TODO(kirpichenkov): works only for JVM (see KT-44552)
interface KotlinStdlibCache {
    fun isStdlib(libraryInfo: LibraryInfo): Boolean
    fun isStdlibDependency(libraryInfo: LibraryInfo): Boolean
    fun findStdlibInModuleDependencies(module: IdeaModuleInfo): LibraryInfo?

    companion object {
        fun getInstance(project: Project): KotlinStdlibCache =
            if (IdeBuiltInsLoadingState.isFromClassLoader) {
                Disabled
            } else {
                project.getService(KotlinStdlibCache::class.java)
                    ?: error("Failed to load service ${KotlinStdlibCache::class.java.name}")
            }

        val Disabled = object : KotlinStdlibCache {
            override fun isStdlib(libraryInfo: LibraryInfo) = false
            override fun isStdlibDependency(libraryInfo: LibraryInfo) = false
            override fun findStdlibInModuleDependencies(module: IdeaModuleInfo): LibraryInfo? = null
        }
    }
}

class KotlinStdlibCacheImpl(val project: Project) : KotlinStdlibCache {
    companion object {
        private const val KOTLIN_JAVA_RUNTIME_NAME = "KotlinJavaRuntime"
    }

    @JvmInline
    private value class StdlibDependency(val libraryInfo: LibraryInfo?)

    private val isStdlibCache: MutableMap<LibraryInfo, Boolean>
        get() = project.cacheInvalidatingOnRootModifications {
            ConcurrentHashMap<LibraryInfo, Boolean>()
        }

    private val isStdlibDependencyCache: MutableMap<LibraryInfo, Boolean>
        get() = project.cacheInvalidatingOnRootModifications {
            ConcurrentHashMap<LibraryInfo, Boolean>()
        }

    private val moduleStdlibDependencyCache: MutableMap<IdeaModuleInfo, StdlibDependency>
        get() = project.cacheInvalidatingOnRootModifications {
            ConcurrentHashMap<IdeaModuleInfo, StdlibDependency>()
        }

    private class LibraryScope(
        project: Project,
        private val directories: Set<VirtualFile>
    ) : DelegatingGlobalSearchScope(GlobalSearchScope.allScope(project)) {
        private val fileSystems = directories.mapTo(hashSetOf(), VirtualFile::getFileSystem)

        override fun contains(file: VirtualFile): Boolean =
            file.fileSystem in fileSystems && generateSequence(file, VirtualFile::getParent).any { it in directories }

        override fun toString() = "All files under: $directories"
    }

    private fun libraryScopeContainsIndexedFilesForNames(libraryInfo: LibraryInfo, names: Collection<FqName>): Boolean =
        names.any { name ->
            DumbModeAccessType.RELIABLE_DATA_ONLY.ignoreDumbMode(ThrowableComputable {
                FileBasedIndex.getInstance().getContainingFiles(
                    KotlinStdlibIndex.KEY,
                    name,
                    LibraryScope(project, libraryInfo.library.rootProvider.getFiles(OrderRootType.CLASSES).toSet())
                ).isNotEmpty()
            })
        }

    private fun libraryScopeContainsIndexedFilesForName(libraryInfo: LibraryInfo, name: FqName) =
        libraryScopeContainsIndexedFilesForNames(libraryInfo, listOf(name))

    private fun isFatJar(libraryInfo: LibraryInfo) =
        libraryInfo.getLibraryRoots().size > 1

    private fun isKotlinJavaRuntime(libraryInfo: LibraryInfo) =
        libraryInfo.library.name == KOTLIN_JAVA_RUNTIME_NAME

    override fun isStdlib(libraryInfo: LibraryInfo): Boolean {
        return isStdlibCache.getOrPut(libraryInfo) {
            libraryScopeContainsIndexedFilesForName(libraryInfo, KotlinStdlibIndex.KOTLIN_STDLIB_NAME) &&
                    (!isFatJar(libraryInfo) || isKotlinJavaRuntime(libraryInfo))
        }
    }

    override fun isStdlibDependency(libraryInfo: LibraryInfo): Boolean {
        return isStdlibDependencyCache.getOrPut(libraryInfo) {
            libraryScopeContainsIndexedFilesForNames(libraryInfo, KotlinStdlibIndex.STANDARD_LIBRARY_DEPENDENCY_NAMES) &&
                    (!isFatJar(libraryInfo) || isKotlinJavaRuntime(libraryInfo))
        }
    }

    override fun findStdlibInModuleDependencies(module: IdeaModuleInfo): LibraryInfo? {
        ProgressManager.checkCanceled()
        val stdlibDependency = moduleStdlibDependencyCache.getOrPut(module) {
            val moduleSourceInfo = module.safeAs<ModuleSourceInfo>()
            val stdLib = moduleSourceInfo?.module?.moduleWithLibrariesScope?.let index@{ scope ->
                val stdlibManifests =
                    FileBasedIndex.getInstance().getContainingFiles(
                        KotlinStdlibIndex.KEY,
                        KotlinStdlibIndex.KOTLIN_STDLIB_NAME,
                        scope
                    )
                val index = ProjectFileIndex.SERVICE.getInstance(project)
                for (manifest in stdlibManifests) {
                    val orderEntries = index.getOrderEntriesForFile(manifest)
                    orderEntries.firstNotNullOfOrNull { it.safeAs<LibraryOrderEntry>()?.library.safeAs<LibraryEx>() }?.let {
                        createLibraryInfo(project, it)
                    }?.firstOrNull(::isStdlib)?.let {
                        return@index it
                    }
                }
                null
            } ?: module.safeAs<LibraryInfo>()?.takeIf(::isStdlib) ?: module.dependencies().firstOrNull {
                it is LibraryInfo && isStdlib(it)
            } as LibraryInfo?

            val stdlibDependency = StdlibDependency(stdLib)
            moduleSourceInfo?.let { _ ->
                // all module dependencies have same stdlib as module itself
                module.dependencies().forEach {
                    if (it is LibraryInfo) {
                        moduleStdlibDependencyCache.putIfAbsent(it, stdlibDependency)
                    }
                }
            }

            stdlibDependency
        }

        return stdlibDependency.libraryInfo
    }
}

fun LibraryInfo.isCoreKotlinLibrary(project: Project): Boolean =
    isKotlinStdlib(project) || isKotlinStdlibDependency(project)

fun LibraryInfo.isKotlinStdlib(project: Project): Boolean =
    KotlinStdlibCache.getInstance(project).isStdlib(this)

fun LibraryInfo.isKotlinStdlibDependency(project: Project): Boolean =
    KotlinStdlibCache.getInstance(project).isStdlibDependency(this)