Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support KLIB for Common platform (#2441)
- Loading branch information
Showing
4 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/resolve/CommonKlibModuleInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.jetbrains.dokka.analysis.resolve | ||
|
||
import org.jetbrains.kotlin.analyzer.ModuleInfo | ||
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices | ||
import org.jetbrains.kotlin.library.KotlinLibrary | ||
import org.jetbrains.kotlin.name.Name | ||
import org.jetbrains.kotlin.platform.CommonPlatforms | ||
import org.jetbrains.kotlin.platform.TargetPlatform | ||
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices | ||
|
||
internal class CommonKlibModuleInfo( | ||
override val name: Name, | ||
val kotlinLibrary: KotlinLibrary, | ||
private val dependOnModules: List<ModuleInfo> | ||
) : ModuleInfo { | ||
override fun dependencies(): List<ModuleInfo> = dependOnModules | ||
|
||
override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns = ModuleInfo.DependencyOnBuiltIns.LAST | ||
|
||
override val platform: TargetPlatform | ||
get() = CommonPlatforms.defaultCommonPlatform | ||
|
||
override val analyzerServices: PlatformDependentAnalyzerServices | ||
get() = CommonPlatformAnalyzerServices | ||
} |
140 changes: 140 additions & 0 deletions
140
...kotlin/org/jetbrains/dokka/analysis/resolve/DokkaKlibMetadataCommonDependencyContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package org.jetbrains.dokka.analysis.resolve | ||
|
||
import org.jetbrains.kotlin.analyzer.ModuleInfo | ||
import org.jetbrains.kotlin.analyzer.common.CommonDependenciesContainer | ||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns | ||
import org.jetbrains.kotlin.config.CompilerConfiguration | ||
import org.jetbrains.kotlin.config.languageVersionSettings | ||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor | ||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider | ||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl | ||
import org.jetbrains.kotlin.descriptors.konan.DeserializedKlibModuleOrigin | ||
import org.jetbrains.kotlin.incremental.components.LookupTracker | ||
import org.jetbrains.kotlin.konan.util.KlibMetadataFactories | ||
import org.jetbrains.kotlin.library.KotlinLibrary | ||
import org.jetbrains.kotlin.library.metadata.NativeTypeTransformer | ||
import org.jetbrains.kotlin.library.metadata.NullFlexibleTypeDeserializer | ||
import org.jetbrains.kotlin.library.metadata.parseModuleHeader | ||
import org.jetbrains.kotlin.name.Name | ||
import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration | ||
import org.jetbrains.kotlin.serialization.konan.impl.KlibMetadataModuleDescriptorFactoryImpl | ||
import org.jetbrains.kotlin.storage.LockBasedStorageManager | ||
import org.jetbrains.kotlin.storage.StorageManager | ||
import org.jetbrains.kotlin.utils.keysToMap | ||
|
||
/** | ||
* Adapted from org.jetbrains.kotlin.cli.metadata.KlibMetadataDependencyContainer | ||
*/ | ||
class DokkaKlibMetadataCommonDependencyContainer( | ||
kotlinLibraries: List<KotlinLibrary>, | ||
private val configuration: CompilerConfiguration, | ||
private val storageManager: StorageManager | ||
) : CommonDependenciesContainer { | ||
|
||
private val builtIns | ||
get() = DefaultBuiltIns.Instance | ||
|
||
private val mutableDependenciesForAllModuleDescriptors = mutableListOf<ModuleDescriptorImpl>().apply { | ||
add(builtIns.builtInsModule) | ||
} | ||
|
||
private val mutableDependenciesForAllModules = mutableListOf<ModuleInfo>() | ||
|
||
private val moduleDescriptorsForKotlinLibraries: Map<KotlinLibrary, ModuleDescriptorImpl> = | ||
kotlinLibraries.keysToMap { library -> | ||
val moduleHeader = parseModuleHeader(library.moduleHeaderData) | ||
val moduleName = Name.special(moduleHeader.moduleName) | ||
val moduleOrigin = DeserializedKlibModuleOrigin(library) | ||
MetadataFactories.DefaultDescriptorFactory.createDescriptor( | ||
moduleName, storageManager, builtIns, moduleOrigin | ||
) | ||
}.also { result -> | ||
val resultValues = result.values | ||
resultValues.forEach { module -> | ||
module.setDependencies(mutableDependenciesForAllModuleDescriptors) | ||
} | ||
mutableDependenciesForAllModuleDescriptors.addAll(resultValues) | ||
} | ||
|
||
private val moduleInfosImpl: List<CommonKlibModuleInfo> = mutableListOf<CommonKlibModuleInfo>().apply { | ||
addAll( | ||
moduleDescriptorsForKotlinLibraries.map { (kotlinLibrary, moduleDescriptor) -> | ||
CommonKlibModuleInfo(moduleDescriptor.name, kotlinLibrary, mutableDependenciesForAllModules) | ||
} | ||
) | ||
mutableDependenciesForAllModules.addAll(this@apply) | ||
} | ||
|
||
override val moduleInfos: List<ModuleInfo> get() = moduleInfosImpl | ||
|
||
/* not used in Dokka */ | ||
override val friendModuleInfos: List<ModuleInfo> = emptyList() | ||
|
||
/* not used in Dokka */ | ||
override val refinesModuleInfos: List<ModuleInfo> = emptyList() | ||
|
||
override fun moduleDescriptorForModuleInfo(moduleInfo: ModuleInfo): ModuleDescriptor { | ||
if (moduleInfo !in moduleInfos) | ||
error("Unknown module info $moduleInfo") | ||
|
||
// Ensure that the package fragment provider has been created and the module descriptor has been | ||
// initialized with the package fragment provider: | ||
packageFragmentProviderForModuleInfo(moduleInfo) | ||
|
||
return moduleDescriptorsForKotlinLibraries.getValue((moduleInfo as CommonKlibModuleInfo).kotlinLibrary) | ||
} | ||
|
||
override fun registerDependencyForAllModules( | ||
moduleInfo: ModuleInfo, | ||
descriptorForModule: ModuleDescriptorImpl | ||
) { | ||
mutableDependenciesForAllModules.add(moduleInfo) | ||
mutableDependenciesForAllModuleDescriptors.add(descriptorForModule) | ||
} | ||
|
||
override fun packageFragmentProviderForModuleInfo( | ||
moduleInfo: ModuleInfo | ||
): PackageFragmentProvider? { | ||
if (moduleInfo !in moduleInfos) | ||
return null | ||
return packageFragmentProviderForKotlinLibrary((moduleInfo as CommonKlibModuleInfo).kotlinLibrary) | ||
} | ||
|
||
private val klibMetadataModuleDescriptorFactory by lazy { | ||
KlibMetadataModuleDescriptorFactoryImpl( | ||
MetadataFactories.DefaultDescriptorFactory, | ||
MetadataFactories.DefaultPackageFragmentsFactory, | ||
MetadataFactories.flexibleTypeDeserializer, | ||
MetadataFactories.platformDependentTypeTransformer | ||
) | ||
} | ||
|
||
private fun packageFragmentProviderForKotlinLibrary( | ||
library: KotlinLibrary | ||
): PackageFragmentProvider { | ||
val languageVersionSettings = configuration.languageVersionSettings | ||
|
||
val libraryModuleDescriptor = moduleDescriptorsForKotlinLibraries.getValue(library) | ||
val packageFragmentNames = parseModuleHeader(library.moduleHeaderData).packageFragmentNameList | ||
|
||
return klibMetadataModuleDescriptorFactory.createPackageFragmentProvider( | ||
library, | ||
packageAccessHandler = null, | ||
packageFragmentNames = packageFragmentNames, | ||
storageManager = LockBasedStorageManager("KlibMetadataPackageFragmentProvider"), | ||
moduleDescriptor = libraryModuleDescriptor, | ||
configuration = CompilerDeserializationConfiguration(languageVersionSettings), | ||
compositePackageFragmentAddend = null, | ||
lookupTracker = LookupTracker.DO_NOTHING | ||
).also { | ||
libraryModuleDescriptor.initialize(it) | ||
} | ||
} | ||
} | ||
|
||
private val MetadataFactories = | ||
KlibMetadataFactories( | ||
{ DefaultBuiltIns.Instance }, | ||
NullFlexibleTypeDeserializer, | ||
NativeTypeTransformer() | ||
) |