Simplify deserialization components for built-ins and JS

This commit is contained in:
Alexander Udalov
2016-02-17 14:26:38 +03:00
committed by Alexander Udalov
parent f130755972
commit 996c5848c2
9 changed files with 125 additions and 71 deletions
@@ -0,0 +1,41 @@
/*
* 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.name.ClassId
import org.jetbrains.kotlin.serialization.ClassData
import org.jetbrains.kotlin.serialization.ClassDataWithSource
import org.jetbrains.kotlin.serialization.builtins.BuiltInsProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
class BuiltInsClassDataFinder(
private val proto: BuiltInsProtoBuf.BuiltIns,
private val nameResolver: NameResolver
) : ClassDataFinder {
private val classIdToProto =
proto.classList.associateBy { klass ->
nameResolver.getClassId(klass.fqName)
}
internal val allClassIds: Collection<ClassId> get() = classIdToProto.keys
override fun findClassData(classId: ClassId): ClassDataWithSource? {
val classProto = classIdToProto[classId] ?: return null
return ClassDataWithSource(ClassData(nameResolver, classProto))
}
}
@@ -30,8 +30,8 @@ class BuiltinsPackageFragment(
storageManager: StorageManager,
module: ModuleDescriptor,
loadResource: (path: String) -> InputStream?
) : DeserializedPackageFragment(fqName, storageManager, module, BuiltInsSerializedResourcePaths, loadResource) {
private val builtinsMessage = run {
) : DeserializedPackageFragment(fqName, storageManager, module, loadResource) {
private val proto = run {
val stream = loadResourceSure(BuiltInsSerializedResourcePaths.getBuiltInsFilePath(fqName))
val version = BuiltInsBinaryVersion.readFrom(stream)
@@ -47,16 +47,13 @@ class BuiltinsPackageFragment(
BuiltInsProtoBuf.BuiltIns.parseFrom(stream, BuiltInsSerializedResourcePaths.extensionRegistry)
}
override val nameResolver = NameResolverImpl(builtinsMessage.strings, builtinsMessage.qualifiedNames)
private val nameResolver = NameResolverImpl(proto.strings, proto.qualifiedNames)
override val classIdToProto =
builtinsMessage.classList.associateBy { klass ->
nameResolver.getClassId(klass.fqName)
}
override val classDataFinder = BuiltInsClassDataFinder(proto, nameResolver)
override fun computeMemberScope() =
DeserializedPackageMemberScope(
this, builtinsMessage.`package`, nameResolver, packagePartSource = null, components = components,
classNames = { classIdToProto.keys.filter { classId -> !classId.isNestedClass }.map { it.shortClassName } }
this, proto.`package`, nameResolver, packagePartSource = null, components = components,
classNames = { classDataFinder.allClassIds.filter { classId -> !classId.isNestedClass }.map { it.shortClassName } }
)
}
@@ -43,7 +43,7 @@ fun createBuiltInPackageFragmentProvider(
val components = DeserializationComponents(
storageManager,
module,
ResourceLoadingClassDataFinder(provider, BuiltInsSerializedResourcePaths, loadResource),
DeserializedClassDataFinder(provider),
AnnotationAndConstantLoaderImpl(module, BuiltInSerializerProtocol),
provider,
localClassResolver,
@@ -0,0 +1,30 @@
/*
* 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.PackageFragmentProvider
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.serialization.ClassDataWithSource
class DeserializedClassDataFinder(private val packageFragmentProvider: PackageFragmentProvider) : ClassDataFinder {
override fun findClassData(classId: ClassId): ClassDataWithSource? {
val packageFragment =
packageFragmentProvider.getPackageFragments(classId.packageFqName).singleOrNull()
as? DeserializedPackageFragment ?: return null
return packageFragment.classDataFinder.findClassData(classId)
}
}
@@ -18,11 +18,8 @@ package org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
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.StorageManager
import org.jetbrains.kotlin.storage.getValue
@@ -33,14 +30,8 @@ abstract class DeserializedPackageFragment(
fqName: FqName,
protected val storageManager: StorageManager,
module: ModuleDescriptor,
protected val serializedResourcePaths: SerializedResourcePaths,
private val loadResource: (path: String) -> InputStream?
protected val loadResource: (path: String) -> InputStream?
) : PackageFragmentDescriptorImpl(module, fqName) {
abstract val nameResolver: NameResolver
abstract val classIdToProto: Map<ClassId, ProtoBuf.Class>?
// component dependency cycle
@set:Inject
lateinit var components: DeserializationComponents
@@ -49,6 +40,8 @@ abstract class DeserializedPackageFragment(
computeMemberScope()
}
abstract val classDataFinder: ClassDataFinder
protected abstract fun computeMemberScope(): DeserializedPackageMemberScope
override fun getMemberScope() = deserializedMemberScope
@@ -1,43 +0,0 @@
/*
* 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.PackageFragmentProvider
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.serialization.ClassData
import org.jetbrains.kotlin.serialization.ClassDataWithSource
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
import java.io.InputStream
class ResourceLoadingClassDataFinder(
private val packageFragmentProvider: PackageFragmentProvider,
private val serializedResourcePaths: SerializedResourcePaths,
private val loadResource: (path: String) -> InputStream?
) : ClassDataFinder {
override fun findClassData(classId: ClassId): ClassDataWithSource? {
val packageFragment = packageFragmentProvider.getPackageFragments(classId.packageFqName).singleOrNull()
as? DeserializedPackageFragment ?: return null
val classProto = packageFragment.classIdToProto?.get(classId) ?: run {
val stream = loadResource(serializedResourcePaths.getClassMetadataPath(classId)) ?: return null
ProtoBuf.Class.parseFrom(stream, serializedResourcePaths.extensionRegistry)
}
return ClassDataWithSource(ClassData(packageFragment.nameResolver, classProto))
}
}
@@ -0,0 +1,36 @@
/*
* 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.serialization.js
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.serialization.ClassData
import org.jetbrains.kotlin.serialization.ClassDataWithSource
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import java.io.InputStream
class KotlinJavascriptClassDataFinder(
private val nameResolver: NameResolver,
private val loadResource: (path: String) -> InputStream?
) : ClassDataFinder {
override fun findClassData(classId: ClassId): ClassDataWithSource? {
val stream = loadResource(KotlinJavascriptSerializedResourcePaths.getClassMetadataPath(classId)) ?: return null
val classProto = ProtoBuf.Class.parseFrom(stream, KotlinJavascriptSerializedResourcePaths.extensionRegistry)
return ClassDataWithSource(ClassData(nameResolver, classProto))
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.serialization.js
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.ProtoBuf
@@ -32,14 +31,15 @@ class KotlinJavascriptPackageFragment(
storageManager: StorageManager,
module: ModuleDescriptor,
loadResource: (path: String) -> InputStream?
) : DeserializedPackageFragment(fqName, storageManager, module, KotlinJavascriptSerializedResourcePaths, loadResource) {
override val nameResolver = NameResolverImpl.read(loadResourceSure(serializedResourcePaths.getStringTableFilePath(fqName)))
) : DeserializedPackageFragment(fqName, storageManager, module, loadResource) {
private val nameResolver =
NameResolverImpl.read(loadResourceSure(KotlinJavascriptSerializedResourcePaths.getStringTableFilePath(fqName)))
override val classIdToProto: Map<ClassId, ProtoBuf.Class>? get() = null
override val classDataFinder = KotlinJavascriptClassDataFinder(nameResolver, loadResource)
override fun computeMemberScope(): DeserializedPackageMemberScope {
val packageStream = loadResourceSure(serializedResourcePaths.getPackageFilePath(fqName))
val packageProto = ProtoBuf.Package.parseFrom(packageStream, serializedResourcePaths.extensionRegistry)
val packageStream = loadResourceSure(KotlinJavascriptSerializedResourcePaths.getPackageFilePath(fqName))
val packageProto = ProtoBuf.Package.parseFrom(packageStream, KotlinJavascriptSerializedResourcePaths.extensionRegistry)
return DeserializedPackageMemberScope(
this, packageProto, nameResolver, packagePartSource = null, components = components, classNames = { loadClassNames() }
)
@@ -47,7 +47,7 @@ class KotlinJavascriptPackageFragment(
private fun loadClassNames(): Collection<Name> {
val classesStream = loadResourceSure(KotlinJavascriptSerializedResourcePaths.getClassesInPackageFilePath(fqName))
val classesProto = JsProtoBuf.Classes.parseFrom(classesStream, serializedResourcePaths.extensionRegistry)
val classesProto = JsProtoBuf.Classes.parseFrom(classesStream, KotlinJavascriptSerializedResourcePaths.extensionRegistry)
return classesProto.classNameList?.map { id -> nameResolver.getName(id) } ?: listOf()
}
}
@@ -41,7 +41,7 @@ fun createKotlinJavascriptPackageFragmentProvider(
val components = DeserializationComponents(
storageManager,
module,
ResourceLoadingClassDataFinder(provider, KotlinJavascriptSerializedResourcePaths, loadResource),
DeserializedClassDataFinder(provider),
AnnotationAndConstantLoaderImpl(module, JsSerializerProtocol),
provider,
localClassResolver,