[Platform API] Introduce fundamental abstraction of Platform
This is a large commit, which introduces general API for working with
abstraction of Platform.
- Add new abstraction to 'core' - SimplePlatform - which represents
exactly one platform
- Clients are strongly prohibited to create instances of SimplePlatform
by hand, instead, corresponding *Platforms abstraction should be used
(e.g. JvmPlatforms, JsPlatforms, KonanPlatforms)
- Move TargetPlatform to 'core', it represents now a collection of
SimplePlatforms
- Clients are strongly encouraged to use TargetPlatform
(not SimplePlatform) in API, to enforce checks for multiplatform
- Provide a helper-extensions to work with TargetPlatform
(in particular, for getting a specific component platform)
- Remove MultiTargetPlatform in favour of TargetPlatform
- Notably, this commit leaves another widely used duplicated abstraction,
namely, IdePlatform. For the sake sanity, removal of IdePlatform is
extracted in the separate commit.
This commit is contained in:
@@ -8,6 +8,7 @@ dependencies {
|
||||
compile(project(":core:util.runtime"))
|
||||
compile(project(":compiler:frontend"))
|
||||
compile(project(":compiler:frontend.java"))
|
||||
compile(project(":js:js.frontend"))
|
||||
compileOnly(project(":kotlin-reflect-api"))
|
||||
compileOnly(intellijCoreDep()) { includeJars("intellij-core") }
|
||||
compileOnly(intellijDep()) { includeIntellijCoreJarDependencies(project) }
|
||||
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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.analyzer.common
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.analyzer.*
|
||||
import org.jetbrains.kotlin.config.LanguageFeature
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.platform.TargetPlatformVersion
|
||||
import org.jetbrains.kotlin.container.StorageComponentContainer
|
||||
import org.jetbrains.kotlin.container.get
|
||||
import org.jetbrains.kotlin.container.useImpl
|
||||
import org.jetbrains.kotlin.container.useInstance
|
||||
import org.jetbrains.kotlin.context.ModuleContext
|
||||
import org.jetbrains.kotlin.context.ProjectContext
|
||||
import org.jetbrains.kotlin.contracts.ContractDeserializerImpl
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.CompositePackageFragmentProvider
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
|
||||
import org.jetbrains.kotlin.frontend.di.configureModule
|
||||
import org.jetbrains.kotlin.incremental.components.ExpectActualTracker
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.load.kotlin.MetadataFinderFactory
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.resolve.*
|
||||
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
|
||||
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactory
|
||||
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactoryService
|
||||
import org.jetbrains.kotlin.serialization.deserialization.MetadataPackageFragmentProvider
|
||||
import org.jetbrains.kotlin.serialization.deserialization.MetadataPartProvider
|
||||
import org.jetbrains.kotlin.types.SubstitutingScopeProviderImpl
|
||||
|
||||
class CommonAnalysisParameters(
|
||||
val metadataPartProviderFactory: (ModuleContent<*>) -> MetadataPartProvider
|
||||
) : PlatformAnalysisParameters
|
||||
|
||||
/**
|
||||
* A facade that is used to analyze common (platform-independent) modules in multi-platform projects.
|
||||
* See [CommonPlatform]
|
||||
*/
|
||||
object CommonResolverForModuleFactory : ResolverForModuleFactory() {
|
||||
private class SourceModuleInfo(
|
||||
override val name: Name,
|
||||
override val capabilities: Map<ModuleDescriptor.Capability<*>, Any?>,
|
||||
private val dependOnOldBuiltIns: Boolean
|
||||
) : ModuleInfo {
|
||||
override fun dependencies() = listOf(this)
|
||||
|
||||
override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns =
|
||||
if (dependOnOldBuiltIns) ModuleInfo.DependencyOnBuiltIns.LAST else ModuleInfo.DependencyOnBuiltIns.NONE
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices
|
||||
get() = CommonPlatformCompilerServices
|
||||
}
|
||||
|
||||
fun analyzeFiles(
|
||||
files: Collection<KtFile>, moduleName: Name, dependOnBuiltIns: Boolean, languageVersionSettings: LanguageVersionSettings,
|
||||
capabilities: Map<ModuleDescriptor.Capability<*>, Any?> = emptyMap(),
|
||||
metadataPartProviderFactory: (ModuleContent<ModuleInfo>) -> MetadataPartProvider
|
||||
): AnalysisResult {
|
||||
val moduleInfo = SourceModuleInfo(moduleName, capabilities, dependOnBuiltIns)
|
||||
val project = files.firstOrNull()?.project ?: throw AssertionError("No files to analyze")
|
||||
|
||||
val multiplatformLanguageSettings = object : LanguageVersionSettings by languageVersionSettings {
|
||||
override fun getFeatureSupport(feature: LanguageFeature): LanguageFeature.State =
|
||||
if (feature == LanguageFeature.MultiPlatformProjects) LanguageFeature.State.ENABLED
|
||||
else languageVersionSettings.getFeatureSupport(feature)
|
||||
}
|
||||
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val resolver = ResolverForProjectImpl(
|
||||
"sources for metadata serializer",
|
||||
ProjectContext(project),
|
||||
listOf(moduleInfo),
|
||||
modulesContent = { ModuleContent(it, files, GlobalSearchScope.allScope(project)) },
|
||||
moduleLanguageSettingsProvider = object : LanguageSettingsProvider {
|
||||
override fun getLanguageVersionSettings(
|
||||
moduleInfo: ModuleInfo,
|
||||
project: Project,
|
||||
isReleaseCoroutines: Boolean?
|
||||
) = multiplatformLanguageSettings
|
||||
|
||||
override fun getTargetPlatform(
|
||||
moduleInfo: ModuleInfo,
|
||||
project: Project
|
||||
) = TargetPlatformVersion.NoVersion
|
||||
},
|
||||
resolverForModuleFactoryByPlatform = { CommonResolverForModuleFactory },
|
||||
platformParameters = { _ -> CommonAnalysisParameters(metadataPartProviderFactory) }
|
||||
)
|
||||
|
||||
val moduleDescriptor = resolver.descriptorForModule(moduleInfo)
|
||||
val container = resolver.resolverForModule(moduleInfo).componentProvider
|
||||
|
||||
container.get<LazyTopDownAnalyzer>().analyzeDeclarations(TopDownAnalysisMode.TopLevelDeclarations, files)
|
||||
|
||||
return AnalysisResult.success(container.get<BindingTrace>().bindingContext, moduleDescriptor)
|
||||
}
|
||||
|
||||
override fun <M : ModuleInfo> createResolverForModule(
|
||||
moduleDescriptor: ModuleDescriptorImpl,
|
||||
moduleContext: ModuleContext,
|
||||
moduleContent: ModuleContent<M>,
|
||||
platformParameters: PlatformAnalysisParameters,
|
||||
targetEnvironment: TargetEnvironment,
|
||||
resolverForProject: ResolverForProject<M>,
|
||||
languageVersionSettings: LanguageVersionSettings
|
||||
): ResolverForModule {
|
||||
val (moduleInfo, syntheticFiles, moduleContentScope) = moduleContent
|
||||
val project = moduleContext.project
|
||||
val declarationProviderFactory = DeclarationProviderFactoryService.createDeclarationProviderFactory(
|
||||
project, moduleContext.storageManager, syntheticFiles,
|
||||
moduleContentScope,
|
||||
moduleInfo
|
||||
)
|
||||
|
||||
val metadataPartProvider = (platformParameters as CommonAnalysisParameters).metadataPartProviderFactory(moduleContent)
|
||||
val trace = CodeAnalyzerInitializer.getInstance(project).createTrace()
|
||||
val container = createContainerToResolveCommonCode(
|
||||
moduleContext, trace, declarationProviderFactory, moduleContentScope, targetEnvironment, metadataPartProvider,
|
||||
languageVersionSettings
|
||||
)
|
||||
|
||||
val packageFragmentProviders = listOf(
|
||||
container.get<ResolveSession>().packageFragmentProvider,
|
||||
container.get<MetadataPackageFragmentProvider>()
|
||||
)
|
||||
|
||||
return ResolverForModule(CompositePackageFragmentProvider(packageFragmentProviders), container)
|
||||
}
|
||||
|
||||
private fun createContainerToResolveCommonCode(
|
||||
moduleContext: ModuleContext,
|
||||
bindingTrace: BindingTrace,
|
||||
declarationProviderFactory: DeclarationProviderFactory,
|
||||
moduleContentScope: GlobalSearchScope,
|
||||
targetEnvironment: TargetEnvironment,
|
||||
metadataPartProvider: MetadataPartProvider,
|
||||
languageVersionSettings: LanguageVersionSettings
|
||||
): StorageComponentContainer = createContainer("ResolveCommonCode", CommonPlatformCompilerServices) {
|
||||
configureModule(moduleContext, DefaultBuiltInPlatforms.commonPlatform, CommonPlatformCompilerServices, bindingTrace)
|
||||
|
||||
useInstance(moduleContentScope)
|
||||
useInstance(LookupTracker.DO_NOTHING)
|
||||
useInstance(ExpectActualTracker.DoNothing)
|
||||
useImpl<ResolveSession>()
|
||||
useImpl<LazyTopDownAnalyzer>()
|
||||
useInstance(languageVersionSettings)
|
||||
useImpl<AnnotationResolverImpl>()
|
||||
useImpl<CompilerDeserializationConfiguration>()
|
||||
useInstance(metadataPartProvider)
|
||||
useInstance(declarationProviderFactory)
|
||||
useImpl<MetadataPackageFragmentProvider>()
|
||||
useImpl<ContractDeserializerImpl>()
|
||||
useImpl<SubstitutingScopeProviderImpl>()
|
||||
|
||||
val metadataFinderFactory = ServiceManager.getService(moduleContext.project, MetadataFinderFactory::class.java)
|
||||
?: error("No MetadataFinderFactory in project")
|
||||
useInstance(metadataFinderFactory.create(moduleContentScope))
|
||||
|
||||
targetEnvironment.configure(this)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. 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.platform
|
||||
|
||||
import org.jetbrains.kotlin.platform.js.JsPlatforms
|
||||
import org.jetbrains.kotlin.platform.js.JsPlatforms.defaultJsPlatform
|
||||
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
|
||||
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.defaultJvmPlatform
|
||||
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.jvm18
|
||||
import org.jetbrains.kotlin.platform.konan.KonanPlatforms
|
||||
import org.jetbrains.kotlin.platform.konan.KonanPlatforms.defaultKonanPlatform
|
||||
|
||||
object CommonPlatforms {
|
||||
|
||||
val defaultCommonPlatform: TargetPlatform = TargetPlatform(
|
||||
setOf(
|
||||
defaultJvmPlatform.single(),
|
||||
defaultJsPlatform.single(),
|
||||
defaultKonanPlatform.single()
|
||||
)
|
||||
)
|
||||
|
||||
val allSimplePlatforms: List<TargetPlatform>
|
||||
get() = sequence {
|
||||
yieldAll(JvmPlatforms.allJvmPlatforms)
|
||||
yieldAll(KonanPlatforms.allKonanPlatforms)
|
||||
yieldAll(JsPlatforms.allJsPlatforms)
|
||||
|
||||
// TODO(dsavvinov): extensions points?
|
||||
}.toList()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.platform.konan
|
||||
|
||||
import org.jetbrains.kotlin.platform.SimplePlatform
|
||||
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||
import org.jetbrains.kotlin.platform.toTargetPlatform
|
||||
|
||||
abstract class KonanPlatform : SimplePlatform("Native") {
|
||||
override val oldFashionedDescription: String
|
||||
get() = "Kotlin/Native "
|
||||
}
|
||||
|
||||
object KonanPlatforms {
|
||||
val defaultKonanPlatform: TargetPlatform = object : KonanPlatform() {}.toTargetPlatform()
|
||||
|
||||
val allKonanPlatforms: List<TargetPlatform> = listOf(defaultKonanPlatform)
|
||||
}
|
||||
|
||||
fun TargetPlatform?.isNative(): Boolean = this?.singleOrNull() is KonanPlatform
|
||||
Reference in New Issue
Block a user