[Platform API] Split TargetPlatform into lightweight TargetPlatform and CompilerServices
This decouples simple data (TargetPlatform) from other subsystem-specific logic (like default imports, built-ins, etc.). Aside from purely aesthetic improvements, it also makes it easier to move 'TargetPlatform' into core (see next commits)
This commit is contained in:
+2
-2
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.context.ProjectContext
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.PlatformAnalysisSettings
|
||||
import org.jetbrains.kotlin.idea.framework.JSLibraryKind
|
||||
import org.jetbrains.kotlin.js.resolve.JsResolverForModuleFactory
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatform
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind
|
||||
|
||||
class JsPlatformKindResolution : IdePlatformKindResolution {
|
||||
@@ -30,6 +30,6 @@ class JsPlatformKindResolution : IdePlatformKindResolution {
|
||||
get() = JsResolverForModuleFactory
|
||||
|
||||
override fun createBuiltIns(settings: PlatformAnalysisSettings, projectContext: ProjectContext): KotlinBuiltIns {
|
||||
return JsPlatform.builtIns
|
||||
return JsPlatformCompilerServices.builtIns
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -64,6 +64,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
|
||||
import org.jetbrains.kotlin.resolve.calls.util.CallMaker
|
||||
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.lazy.FileScopeProviderImpl
|
||||
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
|
||||
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
|
||||
@@ -304,11 +305,11 @@ internal object IDELightClassContexts {
|
||||
moduleDescriptor.setDependencies(moduleDescriptor, moduleDescriptor.builtIns.builtInsModule)
|
||||
|
||||
val moduleInfo = files.first().getModuleInfo()
|
||||
val container = createContainer("LightClassStub", JvmPlatform) {
|
||||
val container = createContainer("LightClassStub", JvmPlatformCompilerServices) {
|
||||
val jvmTarget = IDELanguageSettingsProvider.getTargetPlatform(moduleInfo, project) as? JvmTarget
|
||||
configureModule(
|
||||
ModuleContext(moduleDescriptor, project), JvmPlatform,
|
||||
jvmTarget ?: JvmTarget.DEFAULT, trace
|
||||
jvmTarget ?: JvmTarget.DEFAULT, JvmPlatformCompilerServices, trace
|
||||
)
|
||||
|
||||
useInstance(GlobalSearchScope.EMPTY_SCOPE)
|
||||
|
||||
@@ -37,13 +37,16 @@ import org.jetbrains.kotlin.idea.core.isInTestSourceContentKotlinAware
|
||||
import org.jetbrains.kotlin.idea.framework.getLibraryPlatform
|
||||
import org.jetbrains.kotlin.idea.project.KotlinModuleModificationTracker
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.project.findCompilerServices
|
||||
import org.jetbrains.kotlin.idea.project.getStableName
|
||||
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
|
||||
import org.jetbrains.kotlin.idea.util.isInSourceContentWithoutInjected
|
||||
import org.jetbrains.kotlin.idea.util.rootManager
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.idePlatformKind
|
||||
import org.jetbrains.kotlin.resolve.PlatformDependentCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import java.util.*
|
||||
|
||||
@@ -138,6 +141,9 @@ interface ModuleSourceInfo : IdeaModuleInfo, TrackableModuleInfo {
|
||||
override val platform: TargetPlatform
|
||||
get() = TargetPlatformDetector.getPlatform(module)
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices
|
||||
get() = platform.findCompilerServices
|
||||
|
||||
override fun createModificationTracker(): ModificationTracker =
|
||||
KotlinModuleModificationTracker(module)
|
||||
}
|
||||
@@ -280,6 +286,9 @@ open class LibraryInfo(val project: Project, val library: Library) : IdeaModuleI
|
||||
override val platform: TargetPlatform
|
||||
get() = getLibraryPlatform(project, library)
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices
|
||||
get() = platform.findCompilerServices
|
||||
|
||||
override val sourcesModuleInfo: SourceForBinaryModuleInfo
|
||||
get() = LibrarySourceInfo(project, library, this)
|
||||
|
||||
@@ -313,6 +322,9 @@ data class LibrarySourceInfo(val project: Project, val library: Library, overrid
|
||||
override val platform: TargetPlatform?
|
||||
get() = binariesModuleInfo.platform
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = binariesModuleInfo.compilerServices
|
||||
|
||||
override fun toString() = "LibrarySourceInfo(libraryName=${library.name})"
|
||||
}
|
||||
|
||||
@@ -338,6 +350,12 @@ object NotUnderContentRootModuleInfo : IdeaModuleInfo {
|
||||
|
||||
//TODO: (module refactoring) dependency on runtime can be of use here
|
||||
override fun dependencies(): List<IdeaModuleInfo> = listOf(this)
|
||||
|
||||
override val platform: TargetPlatform?
|
||||
get() = null
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = null
|
||||
}
|
||||
|
||||
private class LibraryWithoutSourceScope(project: Project, private val library: Library) :
|
||||
@@ -427,6 +445,9 @@ data class PlatformModuleInfo(
|
||||
override val moduleOrigin: ModuleOrigin
|
||||
get() = platformModule.moduleOrigin
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = platform?.findCompilerServices
|
||||
|
||||
override fun dependencies() = platformModule.dependencies()
|
||||
|
||||
override fun modulesWhoseInternalsAreVisible() = containedModules.flatMap { it.modulesWhoseInternalsAreVisible() }
|
||||
|
||||
@@ -14,6 +14,8 @@ import org.jetbrains.kotlin.idea.core.script.dependencies.ScriptAdditionalIdeaDe
|
||||
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition
|
||||
import org.jetbrains.kotlin.resolve.PlatformDependentCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
|
||||
data class ScriptModuleInfo(
|
||||
val project: Project,
|
||||
@@ -45,6 +47,12 @@ data class ScriptModuleInfo(
|
||||
dependenciesInfo.sdk?.let { add(SdkInfo(project, it)) }
|
||||
}
|
||||
}
|
||||
|
||||
override val platform: TargetPlatform?
|
||||
get() = null
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = null
|
||||
}
|
||||
|
||||
sealed class ScriptDependenciesInfo(val project: Project) : IdeaModuleInfo, BinaryModuleInfo {
|
||||
@@ -66,6 +74,12 @@ sealed class ScriptDependenciesInfo(val project: Project) : IdeaModuleInfo, Bina
|
||||
override val sourcesModuleInfo: SourceForBinaryModuleInfo?
|
||||
get() = ScriptDependenciesSourceInfo.ForProject(project)
|
||||
|
||||
override val platform: TargetPlatform?
|
||||
get() = null
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = null
|
||||
|
||||
class ForFile(
|
||||
project: Project,
|
||||
val scriptFile: VirtualFile,
|
||||
@@ -115,5 +129,11 @@ sealed class ScriptDependenciesSourceInfo(val project: Project) : IdeaModuleInfo
|
||||
|
||||
override fun equals(other: Any?): Boolean = other is ScriptDependenciesSourceInfo && this.project == other.project
|
||||
|
||||
override val platform: TargetPlatform?
|
||||
get() = null
|
||||
|
||||
override val compilerServices: PlatformDependentCompilerServices?
|
||||
get() = null
|
||||
|
||||
class ForProject(project: Project) : ScriptDependenciesSourceInfo(project)
|
||||
}
|
||||
+2
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.frontend.di.createContainerForLazyBodyResolve
|
||||
import org.jetbrains.kotlin.idea.caches.project.getModuleInfo
|
||||
import org.jetbrains.kotlin.idea.compiler.IDELanguageSettingsProvider
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.project.findCompilerServices
|
||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
@@ -202,6 +203,7 @@ private object KotlinResolveDataProvider {
|
||||
targetPlatform,
|
||||
bodyResolveCache,
|
||||
targetPlatformVersion,
|
||||
targetPlatform.findCompilerServices,
|
||||
analyzableElement.languageVersionSettings
|
||||
).get<LazyTopDownAnalyzer>()
|
||||
|
||||
|
||||
@@ -718,6 +718,7 @@ class ResolveElementCache(
|
||||
targetPlatform,
|
||||
statementFilter,
|
||||
file.jvmTarget,
|
||||
targetPlatform.findCompilerServices,
|
||||
file.languageVersionSettings
|
||||
).get()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.idea.project
|
||||
|
||||
import org.jetbrains.kotlin.analyzer.common.CommonPlatform
|
||||
import org.jetbrains.kotlin.analyzer.common.CommonPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatform
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.PlatformDependentCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.konan.platform.KonanPlatform
|
||||
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformCompilerServices
|
||||
import java.lang.IllegalStateException
|
||||
|
||||
val TargetPlatform.findCompilerServices: PlatformDependentCompilerServices
|
||||
get() =
|
||||
when (this) {
|
||||
is JvmPlatform -> JvmPlatformCompilerServices
|
||||
is JsPlatform -> JsPlatformCompilerServices
|
||||
is KonanPlatform -> NativePlatformCompilerServices
|
||||
is CommonPlatform -> CommonPlatformCompilerServices
|
||||
else -> throw IllegalStateException("Unknown platform $this")
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.asJava.ImpreciseResolveResult.*
|
||||
import org.jetbrains.kotlin.idea.caches.project.getNullableModuleInfo
|
||||
import org.jetbrains.kotlin.idea.compiler.IDELanguageSettingsProvider
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.project.findCompilerServices
|
||||
import org.jetbrains.kotlin.idea.stubindex.KotlinTypeAliasShortNameIndex
|
||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
@@ -258,7 +259,7 @@ class PsiBasedClassResolver @TestOnly constructor(private val targetClassFqName:
|
||||
|
||||
private fun KtFile.getDefaultImports(): List<ImportPath> {
|
||||
val moduleInfo = getNullableModuleInfo() ?: return emptyList()
|
||||
return TargetPlatformDetector.getPlatform(this).getDefaultImports(
|
||||
return TargetPlatformDetector.getPlatform(this).findCompilerServices.getDefaultImports(
|
||||
IDELanguageSettingsProvider.getLanguageVersionSettings(moduleInfo, project),
|
||||
includeLowPriorityImports = true
|
||||
)
|
||||
|
||||
+2
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.resolve.CodeAnalyzerInitializer
|
||||
import org.jetbrains.kotlin.resolve.TargetEnvironment
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.konan.platform.KonanPlatform
|
||||
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformCompilerServices
|
||||
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
|
||||
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactoryService.Companion.createDeclarationProviderFactory
|
||||
|
||||
@@ -48,6 +49,7 @@ object NativeResolverForModuleFactory : ResolverForModuleFactory() {
|
||||
CodeAnalyzerInitializer.getInstance(moduleContext.project).createTrace(),
|
||||
KonanPlatform,
|
||||
TargetPlatformVersion.NoVersion,
|
||||
NativePlatformCompilerServices,
|
||||
targetEnvironment,
|
||||
languageVersionSettings
|
||||
)
|
||||
|
||||
+2
-2
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.idea.inspections.collections
|
||||
import com.intellij.codeInspection.ProblemHighlightType
|
||||
import com.intellij.codeInspection.ProblemsHolder
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatform
|
||||
import org.jetbrains.kotlin.js.resolve.JsPlatformCompilerServices
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.psi.qualifiedExpressionVisitor
|
||||
@@ -31,7 +31,7 @@ class SimplifiableCallChainInspection : AbstractCallChainChecker() {
|
||||
if (conversion.replacement.startsWith("joinTo")) {
|
||||
// Function parameter in map must have String result type
|
||||
if (!firstResolvedCall.hasLastFunctionalParameterWithResult(context) {
|
||||
it.isSubtypeOf(JsPlatform.builtIns.charSequence.defaultType)
|
||||
it.isSubtypeOf(JsPlatformCompilerServices.builtIns.charSequence.defaultType)
|
||||
}
|
||||
) return@check false
|
||||
}
|
||||
|
||||
+2
-1
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.analysis.analyzeInContext
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||
import org.jetbrains.kotlin.idea.codeInliner.CodeToInline
|
||||
import org.jetbrains.kotlin.idea.codeInliner.CodeToInlineBuilder
|
||||
import org.jetbrains.kotlin.idea.project.findCompilerServices
|
||||
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
|
||||
@@ -142,7 +143,7 @@ object ReplaceWithAnnotationAnalyzer {
|
||||
languageVersionSettings: LanguageVersionSettings
|
||||
): List<ImportingScope> {
|
||||
val allDefaultImports =
|
||||
resolutionFacade.frontendService<TargetPlatform>().getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
|
||||
resolutionFacade.frontendService<TargetPlatform>().findCompilerServices.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
|
||||
val (allUnderImports, aliasImports) = allDefaultImports.partition { it.isAllUnder }
|
||||
// this solution doesn't support aliased default imports with a different alias
|
||||
// TODO: Create import directives from ImportPath, create ImportResolver, create LazyResolverScope, see FileScopeProviderImpl
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.imports.ImportPathComparator
|
||||
import org.jetbrains.kotlin.idea.imports.getImportableTargets
|
||||
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
|
||||
import org.jetbrains.kotlin.idea.project.findCompilerServices
|
||||
import org.jetbrains.kotlin.idea.refactoring.fqName.isImported
|
||||
import org.jetbrains.kotlin.idea.resolve.frontendService
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
@@ -60,19 +61,19 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
|
||||
override fun isImportedWithDefault(importPath: ImportPath, contextFile: KtFile): Boolean {
|
||||
val languageVersionSettings = contextFile.getResolutionFacade().frontendService<LanguageVersionSettings>()
|
||||
val platform = TargetPlatformDetector.getPlatform(contextFile)
|
||||
val allDefaultImports = platform.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
|
||||
val allDefaultImports = platform.findCompilerServices.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
|
||||
|
||||
val scriptExtraImports = contextFile.takeIf { it.isScript() }?.let { ktFile ->
|
||||
val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project)?.getScriptDependencies(ktFile.originalFile)
|
||||
scriptDependencies?.imports?.map { ImportPath.fromString(it) }
|
||||
}.orEmpty()
|
||||
|
||||
return importPath.isImported(allDefaultImports + scriptExtraImports, platform.excludedImports)
|
||||
return importPath.isImported(allDefaultImports + scriptExtraImports, platform.findCompilerServices.excludedImports)
|
||||
}
|
||||
|
||||
override fun isImportedWithLowPriorityDefaultImport(importPath: ImportPath, contextFile: KtFile): Boolean {
|
||||
val platform = TargetPlatformDetector.getPlatform(contextFile)
|
||||
return importPath.isImported(platform.defaultLowPriorityImports, platform.excludedImports)
|
||||
return importPath.isImported(platform.findCompilerServices.defaultLowPriorityImports, platform.findCompilerServices.excludedImports)
|
||||
}
|
||||
|
||||
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user