Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2010-2015 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.analyzer
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.context.ModuleContext
import org.jetbrains.kotlin.context.ProjectContext
import org.jetbrains.kotlin.context.withModule
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.descriptors.PackagePartProvider
import org.jetbrains.kotlin.descriptors.impl.LazyModuleDependencies
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.TargetEnvironment
import org.jetbrains.kotlin.resolve.TargetPlatform
import org.jetbrains.kotlin.resolve.createModule
import java.util.*
public class ResolverForModule(
public val packageFragmentProvider: PackageFragmentProvider,
public val componentProvider: ComponentProvider
)
abstract class ResolverForProject {
fun resolverForModule(moduleInfo: M): ResolverForModule = resolverForModuleDescriptor(descriptorForModule(moduleInfo))
abstract fun tryGetResolverForModule(moduleInfo: M): ResolverForModule?
abstract fun descriptorForModule(moduleInfo: M): ModuleDescriptor
abstract fun resolverForModuleDescriptor(descriptor: ModuleDescriptor): ResolverForModule
abstract val name: String
abstract val allModules: Collection
override fun toString() = "$name"
}
public class EmptyResolverForProject : ResolverForProject() {
override val name: String
get() = "Empty resolver"
override fun tryGetResolverForModule(moduleInfo: M): ResolverForModule? = null
override fun resolverForModuleDescriptor(descriptor: ModuleDescriptor): ResolverForModule = throw IllegalStateException("$descriptor is not contained in this resolver")
override fun descriptorForModule(moduleInfo: M) = throw IllegalStateException("Should not be called for $moduleInfo")
override val allModules: Collection = listOf()
}
public class ResolverForProjectImpl(
private val debugName: String,
val descriptorByModule: Map,
val delegateResolver: ResolverForProject = EmptyResolverForProject()
) : ResolverForProject() {
override fun tryGetResolverForModule(moduleInfo: M): ResolverForModule? {
if (!isCorrectModuleInfo(moduleInfo)) {
return null
}
return resolverForModuleDescriptor(doGetDescriptorForModule(moduleInfo))
}
internal val resolverByModuleDescriptor: MutableMap ResolverForModule> = HashMap()
override val allModules: Collection by lazy {
(descriptorByModule.keys + delegateResolver.allModules).toSet()
}
override val name: String
get() = "Resolver for '$debugName'"
private fun isCorrectModuleInfo(moduleInfo: M) = moduleInfo in allModules
override fun resolverForModuleDescriptor(descriptor: ModuleDescriptor): ResolverForModule {
val computation = resolverByModuleDescriptor[descriptor] ?: return delegateResolver.resolverForModuleDescriptor(descriptor)
return computation()
}
override fun descriptorForModule(moduleInfo: M): ModuleDescriptorImpl {
if (!isCorrectModuleInfo(moduleInfo)) {
throw AssertionError("$name does not know how to resolve $moduleInfo")
}
return doGetDescriptorForModule(moduleInfo)
}
private fun doGetDescriptorForModule(moduleInfo: M): ModuleDescriptorImpl {
return descriptorByModule[moduleInfo] ?: return delegateResolver.descriptorForModule(moduleInfo) as ModuleDescriptorImpl
}
}
public data class ModuleContent(
public val syntheticFiles: Collection,
public val moduleContentScope: GlobalSearchScope
)
public interface PlatformAnalysisParameters
public interface ModuleInfo {
public val isLibrary: Boolean
get() = false
public val name: Name
public fun dependencies(): List
public fun friends(): Collection = listOf()
public fun dependencyOnBuiltins(): DependencyOnBuiltins = DependenciesOnBuiltins.LAST
val capabilities: Map, Any?>
get() = emptyMap()
//TODO: (module refactoring) provide dependency on builtins after runtime in IDEA
public interface DependencyOnBuiltins {
public fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList)
}
public enum class DependenciesOnBuiltins : DependencyOnBuiltins {
NONE {
override fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) {
//do nothing
}
},
LAST {
override fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) {
dependencies.add(builtinsModule)
}
};
override fun adjustDependencies(builtinsModule: ModuleDescriptorImpl, dependencies: MutableList) {
//TODO: KT-5457
}
}
}
public abstract class AnalyzerFacade {
public fun setupResolverForProject(
debugName: String,
projectContext: ProjectContext,
modules: Collection,
modulesContent: (M) -> ModuleContent,
platformParameters: P,
targetEnvironment: TargetEnvironment,
delegateResolver: ResolverForProject = EmptyResolverForProject(),
packagePartProviderFactory: (M, ModuleContent) -> PackagePartProvider = { module, content -> PackagePartProvider.EMPTY }
): ResolverForProject {
val storageManager = projectContext.storageManager
fun createResolverForProject(): ResolverForProjectImpl {
val descriptorByModule = HashMap()
modules.forEach {
module ->
descriptorByModule[module] = targetPlatform.createModule(module.name, storageManager, module.capabilities)
}
return ResolverForProjectImpl(debugName, descriptorByModule, delegateResolver)
}
val resolverForProject = createResolverForProject()
fun computeDependencyDescriptors(module: M): List {
val dependenciesDescriptors = module.dependencies().mapTo(ArrayList()) {
dependencyInfo ->
resolverForProject.descriptorForModule(dependencyInfo as M)
}
val builtinsModule = targetPlatform.builtIns.builtInsModule
module.dependencyOnBuiltins().adjustDependencies(builtinsModule, dependenciesDescriptors)
return dependenciesDescriptors
}
fun setupModuleDependencies() {
modules.forEach {
module ->
resolverForProject.descriptorForModule(module).setDependencies(
LazyModuleDependencies(storageManager) { computeDependencyDescriptors(module) }
)
}
}
setupModuleDependencies()
fun addFriends() {
modules.forEach {
module ->
val descriptor = resolverForProject.descriptorForModule(module)
module.friends().forEach {
descriptor.addFriend(resolverForProject.descriptorForModule(it as M))
}
}
}
addFriends()
fun initializeResolverForProject() {
modules.forEach {
module ->
val descriptor = resolverForProject.descriptorForModule(module)
val computeResolverForModule = storageManager.createLazyValue {
val content = modulesContent(module)
createResolverForModule(
module, descriptor, projectContext.withModule(descriptor), modulesContent(module),
platformParameters, targetEnvironment, resolverForProject,
packagePartProviderFactory(module, content)
)
}
descriptor.initialize(DelegatingPackageFragmentProvider { computeResolverForModule().packageFragmentProvider })
resolverForProject.resolverByModuleDescriptor[descriptor] = computeResolverForModule
}
}
initializeResolverForProject()
return resolverForProject
}
protected abstract fun createResolverForModule(
moduleInfo: M,
moduleDescriptor: ModuleDescriptorImpl,
moduleContext: ModuleContext,
moduleContent: ModuleContent,
platformParameters: P,
targetEnvironment: TargetEnvironment,
resolverForProject: ResolverForProject,
packagePartProvider: PackagePartProvider
): ResolverForModule
public abstract val targetPlatform: TargetPlatform
}
//NOTE: relies on delegate to be lazily computed and cached
private class DelegatingPackageFragmentProvider(
private val delegate: () -> PackageFragmentProvider
) : PackageFragmentProvider {
override fun getPackageFragments(fqName: FqName): List {
return delegate().getPackageFragments(fqName)
}
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection {
return delegate().getSubPackagesOf(fqName, nameFilter)
}
}