add SerializedResourcePaths, extract base code from builtins package fragment, data finder
This commit is contained in:
+6
-6
@@ -21,7 +21,7 @@ import com.intellij.openapi.util.Disposer
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.analyzer.ModuleContent
|
||||
import org.jetbrains.kotlin.analyzer.ModuleInfo
|
||||
import org.jetbrains.kotlin.builtins.BuiltInsSerializationUtil
|
||||
import org.jetbrains.kotlin.builtins.BuiltInsSerializedResourcePaths
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
@@ -128,14 +128,14 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
|
||||
val fragments = module.getPackageFragmentProvider().getPackageFragments(fqName)
|
||||
val packageProto = serializer.packageProto(fragments).build() ?: error("Package fragments not serialized: $fragments")
|
||||
packageProto.writeTo(packageStream)
|
||||
write(destDir, BuiltInsSerializationUtil.getPackageFilePath(fqName), packageStream,
|
||||
BuiltInsSerializationUtil.FallbackPaths.getPackageFilePath(fqName))
|
||||
write(destDir, BuiltInsSerializedResourcePaths.getPackageFilePath(fqName), packageStream,
|
||||
BuiltInsSerializedResourcePaths.fallbackPaths.getPackageFilePath(fqName))
|
||||
|
||||
val nameStream = ByteArrayOutputStream()
|
||||
val strings = serializer.getStringTable()
|
||||
SerializationUtil.serializeStringTable(nameStream, strings.serializeSimpleNames(), strings.serializeQualifiedNames())
|
||||
write(destDir, BuiltInsSerializationUtil.getStringTableFilePath(fqName), nameStream,
|
||||
BuiltInsSerializationUtil.FallbackPaths.getStringTableFilePath(fqName))
|
||||
write(destDir, BuiltInsSerializedResourcePaths.getStringTableFilePath(fqName), nameStream,
|
||||
BuiltInsSerializedResourcePaths.fallbackPaths.getStringTableFilePath(fqName))
|
||||
}
|
||||
|
||||
private fun write(destDir: File, fileName: String, stream: ByteArrayOutputStream, legacyFileName: String? = null) {
|
||||
@@ -173,6 +173,6 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
|
||||
}
|
||||
|
||||
private fun getFileName(classDescriptor: ClassDescriptor): String {
|
||||
return BuiltInsSerializationUtil.getClassMetadataPath(classDescriptor.classId)
|
||||
return BuiltInsSerializedResourcePaths.getClassMetadataPath(classDescriptor.classId)
|
||||
}
|
||||
}
|
||||
|
||||
+6
-14
@@ -19,10 +19,11 @@ package org.jetbrains.kotlin.builtins
|
||||
import com.google.protobuf.ExtensionRegistryLite
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||
import org.jetbrains.kotlin.serialization.builtins.BuiltInsProtoBuf
|
||||
|
||||
public object BuiltInsSerializationUtil {
|
||||
public val EXTENSION_REGISTRY: ExtensionRegistryLite
|
||||
public object BuiltInsSerializedResourcePaths : SerializedResourcePaths() {
|
||||
public override val EXTENSION_REGISTRY: ExtensionRegistryLite
|
||||
|
||||
init {
|
||||
EXTENSION_REGISTRY = ExtensionRegistryLite.newInstance()
|
||||
@@ -33,24 +34,15 @@ public object BuiltInsSerializationUtil {
|
||||
private val PACKAGE_FILE_EXTENSION = "kotlin_package"
|
||||
private val STRING_TABLE_FILE_EXTENSION = "kotlin_string_table"
|
||||
|
||||
// TODO: remove this after M12
|
||||
public object FallbackPaths {
|
||||
public fun getPackageFilePath(fqName: FqName): String =
|
||||
packageFqNameToPath(fqName) + "/.kotlin_package"
|
||||
|
||||
public fun getStringTableFilePath(fqName: FqName): String =
|
||||
packageFqNameToPath(fqName) + "/.kotlin_string_table"
|
||||
}
|
||||
|
||||
public fun getClassMetadataPath(classId: ClassId): String {
|
||||
public override fun getClassMetadataPath(classId: ClassId): String {
|
||||
return packageFqNameToPath(classId.getPackageFqName()) + "/" + classId.getRelativeClassName().asString() +
|
||||
"." + CLASS_METADATA_FILE_EXTENSION
|
||||
}
|
||||
|
||||
public fun getPackageFilePath(fqName: FqName): String =
|
||||
public override fun getPackageFilePath(fqName: FqName): String =
|
||||
packageFqNameToPath(fqName) + "/" + shortName(fqName) + "." + PACKAGE_FILE_EXTENSION
|
||||
|
||||
public fun getStringTableFilePath(fqName: FqName): String =
|
||||
public override fun getStringTableFilePath(fqName: FqName): String =
|
||||
packageFqNameToPath(fqName) + "/" + shortName(fqName) + "." + STRING_TABLE_FILE_EXTENSION
|
||||
|
||||
|
||||
@@ -16,49 +16,23 @@
|
||||
|
||||
package org.jetbrains.kotlin.builtins
|
||||
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializedPackageFragment
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.builtins.BuiltInsProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents
|
||||
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import java.io.InputStream
|
||||
import javax.inject.Inject
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
public class BuiltinsPackageFragment(
|
||||
fqName: FqName,
|
||||
storageManager: StorageManager,
|
||||
module: ModuleDescriptor,
|
||||
private val loadResource: (path: String) -> InputStream?
|
||||
) : PackageFragmentDescriptorImpl(module, fqName) {
|
||||
loadResource: (path: String) -> InputStream?
|
||||
) : DeserializedPackageFragment(fqName, storageManager, module, BuiltInsSerializedResourcePaths, loadResource) {
|
||||
|
||||
val nameResolver = NameResolver.read(
|
||||
loadResource(BuiltInsSerializationUtil.getStringTableFilePath(fqName))
|
||||
?: loadResourceSure(BuiltInsSerializationUtil.FallbackPaths.getStringTableFilePath(fqName))
|
||||
)
|
||||
|
||||
private var components: DeserializationComponents by Delegates.notNull()
|
||||
|
||||
Inject
|
||||
public fun setDeserializationComponents(components: DeserializationComponents) {
|
||||
this.components = components
|
||||
protected override fun loadClassNames(fqName: FqName, packageProto: ProtoBuf.Package): Collection<Name> {
|
||||
return packageProto.getExtension(BuiltInsProtoBuf.className)?.map { id -> nameResolver.getName(id) } ?: listOf()
|
||||
}
|
||||
|
||||
private val memberScope = storageManager.createLazyValue {
|
||||
val stream = loadResource(BuiltInsSerializationUtil.getPackageFilePath(fqName))
|
||||
?: loadResourceSure(BuiltInsSerializationUtil.FallbackPaths.getPackageFilePath(fqName))
|
||||
val proto = ProtoBuf.Package.parseFrom(stream, BuiltInsSerializationUtil.EXTENSION_REGISTRY)
|
||||
DeserializedPackageMemberScope(this, proto, nameResolver, components, classNames = {
|
||||
proto.getExtension(BuiltInsProtoBuf.className)?.map { id -> nameResolver.getName(id) } ?: listOf()
|
||||
})
|
||||
}
|
||||
|
||||
override fun getMemberScope() = memberScope()
|
||||
|
||||
private fun loadResourceSure(path: String): InputStream =
|
||||
loadResource(path) ?: throw IllegalStateException("Resource not found in classpath: $path")
|
||||
}
|
||||
|
||||
+2
-1
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.builtins
|
||||
|
||||
import org.jetbrains.kotlin.serialization.deserialization.ResourceLoadingClassDataFinder
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProviderImpl
|
||||
@@ -43,7 +44,7 @@ public fun createBuiltInPackageFragmentProvider(
|
||||
val components = DeserializationComponents(
|
||||
storageManager,
|
||||
module,
|
||||
BuiltInsClassDataFinder(provider, loadResource),
|
||||
ResourceLoadingClassDataFinder(provider, BuiltInsSerializedResourcePaths, loadResource),
|
||||
BuiltInsAnnotationAndConstantLoader(module),
|
||||
provider,
|
||||
localClassResolver,
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.serialization
|
||||
|
||||
import com.google.protobuf.ExtensionRegistryLite
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
public abstract class SerializedResourcePaths {
|
||||
public abstract val EXTENSION_REGISTRY: ExtensionRegistryLite
|
||||
|
||||
public abstract fun getClassMetadataPath(classId: ClassId): String
|
||||
|
||||
public abstract fun getPackageFilePath(fqName: FqName): String
|
||||
|
||||
public abstract fun getStringTableFilePath(fqName: FqName): String
|
||||
|
||||
// TODO: remove this after M12
|
||||
public object FallbackPaths {
|
||||
public fun getPackageFilePath(fqName: FqName): String =
|
||||
fqName.asString().replace('.', '/') + "/.kotlin_package"
|
||||
|
||||
public fun getStringTableFilePath(fqName: FqName): String =
|
||||
fqName.asString().replace('.', '/') + "/.kotlin_string_table"
|
||||
}
|
||||
|
||||
public val fallbackPaths: FallbackPaths = FallbackPaths
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
|
||||
import org.jetbrains.kotlin.storage.NotNullLazyValue
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import java.io.InputStream
|
||||
import javax.inject.Inject
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
public abstract class DeserializedPackageFragment(
|
||||
fqName: FqName,
|
||||
protected val storageManager: StorageManager,
|
||||
module: ModuleDescriptor,
|
||||
protected val serializedResourcePaths: SerializedResourcePaths,
|
||||
private val loadResource: (path: String) -> InputStream?
|
||||
) : PackageFragmentDescriptorImpl(module, fqName) {
|
||||
|
||||
val nameResolver = NameResolver.read(
|
||||
loadResource(serializedResourcePaths.getStringTableFilePath(fqName))
|
||||
?: loadResourceSure(serializedResourcePaths.fallbackPaths.getStringTableFilePath(fqName))
|
||||
)
|
||||
|
||||
protected var components: DeserializationComponents by Delegates.notNull()
|
||||
|
||||
Inject
|
||||
public fun setDeserializationComponents(components: DeserializationComponents) {
|
||||
this.components = components
|
||||
}
|
||||
|
||||
private val memberScopeLazyValue: NotNullLazyValue<JetScope> = storageManager.createLazyValue {
|
||||
val packageStream = loadResourceSure(serializedResourcePaths.getPackageFilePath(fqName))
|
||||
val packageProto = ProtoBuf.Package.parseFrom(packageStream, serializedResourcePaths.EXTENSION_REGISTRY)
|
||||
DeserializedPackageMemberScope(this, packageProto, nameResolver, components, classNames = { loadClassNames(fqName, packageProto) })
|
||||
}
|
||||
|
||||
override fun getMemberScope() = memberScopeLazyValue()
|
||||
|
||||
protected abstract fun loadClassNames(fqName: FqName, packageProto: ProtoBuf.Package): Collection<Name>
|
||||
|
||||
protected fun loadResourceSure(path: String): InputStream =
|
||||
loadResource(path) ?: throw IllegalStateException("Resource not found in classpath: $path")
|
||||
}
|
||||
+9
-7
@@ -14,27 +14,29 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.builtins
|
||||
package org.jetbrains.kotlin.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializedPackageFragment
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.serialization.ClassData
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder
|
||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||
import java.io.InputStream
|
||||
|
||||
public class BuiltInsClassDataFinder(
|
||||
public open class ResourceLoadingClassDataFinder(
|
||||
private val packageFragmentProvider: PackageFragmentProvider,
|
||||
private val serializedResourcePaths: SerializedResourcePaths,
|
||||
private val loadResource: (path: String) -> InputStream?
|
||||
) : ClassDataFinder {
|
||||
override fun findClassData(classId: ClassId): ClassData? {
|
||||
val packageFragment = packageFragmentProvider.getPackageFragments(classId.getPackageFqName()).singleOrNull() ?: return null
|
||||
|
||||
val stream = loadResource(BuiltInsSerializationUtil.getClassMetadataPath(classId)) ?: return null
|
||||
val stream = loadResource(serializedResourcePaths.getClassMetadataPath(classId)) ?: return null
|
||||
|
||||
val classProto = ProtoBuf.Class.parseFrom(stream, BuiltInsSerializationUtil.EXTENSION_REGISTRY)
|
||||
val classProto = ProtoBuf.Class.parseFrom(stream, serializedResourcePaths.EXTENSION_REGISTRY)
|
||||
val nameResolver =
|
||||
(packageFragment as? BuiltinsPackageFragment ?: error("Not a built-in package fragment: $packageFragment")).nameResolver
|
||||
(packageFragment as? DeserializedPackageFragment ?: error("Not a deserialized package fragment: $packageFragment")).nameResolver
|
||||
|
||||
val expectedShortName = classId.getShortClassName()
|
||||
val actualShortName = nameResolver.getClassId(classProto.getFqName()).getShortClassName()
|
||||
@@ -46,4 +48,4 @@ public class BuiltInsClassDataFinder(
|
||||
|
||||
return ClassData(nameResolver, classProto)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user