Load built-ins from module dependencies in JVM compiler
Introduce a new method KotlinClassFinder#findBuiltInsData, which is only implemented correctly in the JvmCliVirtualFileFinder because it's only used in the compiler code at the moment. Introduce JvmBuiltInsPackageFragmentProvider, the purpose of which is to look for .kotlin_builtins files in the classpath and provide definitions of built-ins from those files. Also exclude script.runtime from compilation because, as other excluded modules, it has no dependency on the stdlib and is no longer compilable from the IDE now, because it cannot resolve built-ins from anywhere
This commit is contained in:
+73
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.builtins
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.BuiltInFictitiousFunctionClassFactory
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinClassFinder
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.serialization.deserialization.*
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.utils.singletonOrEmptyList
|
||||
|
||||
class JvmBuiltInsPackageFragmentProvider(
|
||||
storageManager: StorageManager,
|
||||
finder: KotlinClassFinder,
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
notFoundClasses: NotFoundClasses,
|
||||
additionalClassPartsProvider: AdditionalClassPartsProvider,
|
||||
platformDependentDeclarationFilter: PlatformDependentDeclarationFilter
|
||||
) : PackageFragmentProvider {
|
||||
private lateinit var components: DeserializationComponents
|
||||
|
||||
private val fragments = storageManager.createMemoizedFunctionWithNullableValues<FqName, PackageFragmentDescriptor> { fqName ->
|
||||
finder.findBuiltInsData(fqName)?.let { inputStream ->
|
||||
BuiltInsPackageFragment(fqName, storageManager, moduleDescriptor) { path -> inputStream }.apply {
|
||||
components = this@JvmBuiltInsPackageFragmentProvider.components
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
components = DeserializationComponents(
|
||||
storageManager,
|
||||
moduleDescriptor,
|
||||
DeserializationConfiguration.Default,
|
||||
DeserializedClassDataFinder(this),
|
||||
AnnotationAndConstantLoaderImpl(moduleDescriptor, notFoundClasses, BuiltInSerializerProtocol),
|
||||
this,
|
||||
LocalClassifierTypeSettings.Default,
|
||||
ErrorReporter.DO_NOTHING,
|
||||
LookupTracker.DO_NOTHING,
|
||||
FlexibleTypeDeserializer.ThrowException,
|
||||
listOf(
|
||||
BuiltInFictitiousFunctionClassFactory(storageManager, moduleDescriptor),
|
||||
JvmBuiltInClassDescriptorFactory(storageManager, moduleDescriptor)
|
||||
),
|
||||
notFoundClasses, additionalClassPartsProvider, platformDependentDeclarationFilter
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
override fun getPackageFragments(fqName: FqName): List<PackageFragmentDescriptor> = fragments(fqName).singletonOrEmptyList()
|
||||
|
||||
override fun getSubPackagesOf(fqName: FqName, nameFilter: (Name) -> Boolean): Collection<FqName> = emptySet()
|
||||
}
|
||||
+1
-1
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder
|
||||
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil
|
||||
|
||||
class JavaClassDataFinder(
|
||||
private val kotlinClassFinder: KotlinClassFinder,
|
||||
internal val kotlinClassFinder: KotlinClassFinder,
|
||||
private val deserializedDescriptorResolver: DeserializedDescriptorResolver
|
||||
) : ClassDataFinder {
|
||||
override fun findClassData(classId: ClassId): ClassDataWithSource? {
|
||||
|
||||
@@ -19,9 +19,12 @@ package org.jetbrains.kotlin.load.kotlin
|
||||
import org.jetbrains.kotlin.load.java.structure.JavaClass
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import java.io.InputStream
|
||||
|
||||
interface KotlinClassFinder {
|
||||
fun findKotlinClass(classId: ClassId): KotlinJvmBinaryClass?
|
||||
|
||||
fun findKotlinClass(javaClass: JavaClass): KotlinJvmBinaryClass?
|
||||
|
||||
fun findBuiltInsData(packageFqName: FqName): InputStream?
|
||||
}
|
||||
|
||||
+5
@@ -21,6 +21,8 @@ import org.jetbrains.kotlin.load.java.structure.JavaClass
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinClassFinder
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryClass
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import java.io.InputStream
|
||||
|
||||
class ReflectKotlinClassFinder(private val classLoader: ClassLoader) : KotlinClassFinder {
|
||||
private fun findKotlinClass(fqName: String): KotlinJvmBinaryClass? {
|
||||
@@ -33,6 +35,9 @@ class ReflectKotlinClassFinder(private val classLoader: ClassLoader) : KotlinCla
|
||||
// TODO: go through javaClass's class loader
|
||||
return findKotlinClass(javaClass.fqName?.asString() ?: return null)
|
||||
}
|
||||
|
||||
// TODO: load built-ins from classLoader
|
||||
override fun findBuiltInsData(packageFqName: FqName): InputStream? = null
|
||||
}
|
||||
|
||||
private fun ClassId.toRuntimeFqName(): String {
|
||||
|
||||
@@ -30,7 +30,10 @@ object BuiltInSerializerProtocol : SerializerExtensionProtocol(
|
||||
val BUILTINS_FILE_EXTENSION = "kotlin_builtins"
|
||||
|
||||
fun getBuiltInsFilePath(fqName: FqName): String =
|
||||
fqName.asString().replace('.', '/') + "/" + shortName(fqName) + "." + BUILTINS_FILE_EXTENSION
|
||||
fqName.asString().replace('.', '/') + "/" + getBuiltInsFileName(fqName)
|
||||
|
||||
fun getBuiltInsFileName(fqName: FqName): String =
|
||||
shortName(fqName) + "." + BUILTINS_FILE_EXTENSION
|
||||
|
||||
private fun shortName(fqName: FqName): String =
|
||||
if (fqName.isRoot) "default-package" else fqName.shortName().asString()
|
||||
|
||||
Reference in New Issue
Block a user