From 9b81cf24545967cb262f01e4ccfce82ea74b7f38 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 28 Oct 2014 21:42:25 +0300 Subject: [PATCH] Converted DeserializedMemberScope to Kotlin --- .../descriptors/DeserializedMemberScope.java | 259 ------------------ .../descriptors/DeserializedMemberScope.kt | 165 +++++++++++ 2 files changed, 165 insertions(+), 259 deletions(-) delete mode 100644 core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.java create mode 100644 core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.kt diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.java b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.java deleted file mode 100644 index 31bed719ab6..00000000000 --- a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.java +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright 2010-2013 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.jet.descriptors.serialization.descriptors; - -import kotlin.Function0; -import kotlin.Function1; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.ReadOnly; -import org.jetbrains.jet.descriptors.serialization.Flags; -import org.jetbrains.jet.descriptors.serialization.ProtoBuf; -import org.jetbrains.jet.descriptors.serialization.context.DeserializationContextWithTypes; -import org.jetbrains.jet.lang.descriptors.*; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.scopes.JetScope; -import org.jetbrains.jet.storage.MemoizedFunctionToNotNull; -import org.jetbrains.jet.storage.NotNullLazyValue; -import org.jetbrains.jet.storage.StorageManager; -import org.jetbrains.jet.utils.Printer; -import org.jetbrains.jet.utils.UtilsPackage; - -import java.util.*; - -public abstract class DeserializedMemberScope implements JetScope { - - private static final Filter FUNCTION = new Filter() { - @Override - public boolean accept(ProtoBuf.Callable.CallableKind value) { - return value == ProtoBuf.Callable.CallableKind.FUN; - } - }; - private static final Filter PROPERTY = new Filter() { - @Override - public boolean accept(ProtoBuf.Callable.CallableKind value) { - return value == ProtoBuf.Callable.CallableKind.VAL || - value == ProtoBuf.Callable.CallableKind.VAR; - } - }; - - private final NotNullLazyValue>> membersProtos; - - private final MemoizedFunctionToNotNull> functions; - private final MemoizedFunctionToNotNull> properties; - private final NotNullLazyValue> allDescriptors; - private final DeserializationContextWithTypes context; - - protected DeserializedMemberScope( - @NotNull DeserializationContextWithTypes context, - @NotNull final Collection membersList - ) { - this.context = context; - - StorageManager storageManager = context.getStorageManager(); - this.membersProtos = storageManager.createLazyValue(new Function0>>() { - @Override - public Map> invoke() { - return groupByName(filteredMemberProtos(membersList)); - } - }); - this.functions = storageManager.createMemoizedFunction(new Function1>() { - @Override - public Collection invoke(Name name) { - return computeFunctions(name); - } - }); - this.properties = storageManager.createMemoizedFunction( - new Function1>() { - @Override - public Collection invoke(Name name) { - return computeProperties(name); - } - } - ); - this.allDescriptors = storageManager.createLazyValue( - new Function0>() { - @Override - public Collection invoke() { - return computeAllDescriptors(); - } - } - ); - } - - @NotNull - @ReadOnly - protected Collection filteredMemberProtos(@NotNull Collection allMemberProtos) { - return allMemberProtos; - } - - @NotNull - private Map> groupByName(@NotNull Collection membersList) { - Map> map = new HashMap>(); - for (ProtoBuf.Callable memberProto : membersList) { - Name name = context.getNameResolver().getName(memberProto.getName()); - List protos = map.get(name); - if (protos == null) { - protos = new ArrayList(1); - map.put(name, protos); - } - protos.add(memberProto); - } - return map; - } - - @NotNull - private Collection computeMembersByName(Name name, Filter callableKind) { - List memberProtos = membersProtos.invoke().get(name); - - Collection descriptors = new LinkedHashSet(memberProtos != null ? memberProtos.size() : 0); - if (memberProtos != null) { - for (ProtoBuf.Callable memberProto : memberProtos) { - if (callableKind.accept(Flags.CALLABLE_KIND.get(memberProto.getFlags()))) { - //noinspection unchecked - descriptors.add((D) context.getDeserializer().loadCallable(memberProto)); - } - } - } - return descriptors; - } - - @NotNull - private Collection computeFunctions(@NotNull Name name) { - Collection descriptors = computeMembersByName(name, FUNCTION); - computeNonDeclaredFunctions(name, descriptors); - return UtilsPackage.toReadOnlyList(descriptors); - } - - protected void computeNonDeclaredFunctions(@NotNull Name name, @NotNull Collection functions) { - } - - @NotNull - @Override - public final Collection getFunctions(@NotNull Name name) { - return functions.invoke(name); - } - - @NotNull - private Collection computeProperties(@NotNull Name name) { - Collection descriptors = computeMembersByName(name, PROPERTY); - computeNonDeclaredProperties(name, descriptors); - //noinspection unchecked - return UtilsPackage.toReadOnlyList(descriptors); - } - - protected void computeNonDeclaredProperties(@NotNull Name name, @NotNull Collection descriptors) { - } - - @NotNull - @Override - public Collection getProperties(@NotNull Name name) { - return properties.invoke(name); - } - - @Nullable - @Override - public final ClassifierDescriptor getClassifier(@NotNull Name name) { - return getClassDescriptor(name); - } - - @Nullable - protected abstract ClassifierDescriptor getClassDescriptor(@NotNull Name name); - - protected abstract void addAllClassDescriptors(@NotNull Collection result); - - @Nullable - @Override - public PackageViewDescriptor getPackage(@NotNull Name name) { - return null; - } - - @Nullable - @Override - public VariableDescriptor getLocalVariable(@NotNull Name name) { - return null; - } - - @NotNull - @Override - public DeclarationDescriptor getContainingDeclaration() { - return context.getContainingDeclaration(); - } - - @NotNull - @Override - public Collection getDeclarationsByLabel(@NotNull Name labelName) { - return Collections.emptyList(); - } - - private Collection computeAllDescriptors() { - Collection result = new LinkedHashSet(0); - - for (Name name : membersProtos.invoke().keySet()) { - result.addAll(getFunctions(name)); - result.addAll(getProperties(name)); - } - - addNonDeclaredDescriptors(result); - - addAllClassDescriptors(result); - - return UtilsPackage.toReadOnlyList(result); - } - - protected abstract void addNonDeclaredDescriptors(@NotNull Collection result); - - @NotNull - @Override - public final Collection getAllDescriptors() { - return allDescriptors.invoke(); - } - - @NotNull - @Override - public List getImplicitReceiversHierarchy() { - ReceiverParameterDescriptor receiver = getImplicitReceiver(); - if (receiver != null) { - return Collections.singletonList(receiver); - } - return Collections.emptyList(); - } - - @Nullable - protected abstract ReceiverParameterDescriptor getImplicitReceiver(); - - @NotNull - @Override - public Collection getOwnDeclaredDescriptors() { - return getAllDescriptors(); - } - - private interface Filter { - boolean accept(T value); - } - - @Override - public void printScopeStructure(@NotNull Printer p) { - p.println(getClass().getSimpleName(), " {"); - p.pushIndent(); - - p.println("containingDeclaration = " + getContainingDeclaration()); - - p.popIndent(); - p.println("}"); - } -} diff --git a/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.kt b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.kt new file mode 100644 index 00000000000..c451fa41125 --- /dev/null +++ b/core/serialization/src/org/jetbrains/jet/descriptors/serialization/descriptors/DeserializedMemberScope.kt @@ -0,0 +1,165 @@ +/* + * Copyright 2010-2014 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.jet.descriptors.serialization.descriptors + +import org.jetbrains.jet.descriptors.serialization.Flags +import org.jetbrains.jet.descriptors.serialization.ProtoBuf +import org.jetbrains.jet.descriptors.serialization.context.DeserializationContextWithTypes +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.lang.resolve.scopes.JetScope +import org.jetbrains.jet.utils.Printer + +import java.util.* +import org.jetbrains.jet.utils.toReadOnlyList + +public abstract class DeserializedMemberScope protected( + private val context: DeserializationContextWithTypes, + membersList: Collection) +: JetScope { + + private val membersProtos = context.storageManager.createLazyValue { groupByName(filteredMemberProtos(membersList)) } + private val functions = context.storageManager.createMemoizedFunction> { computeFunctions(it) } + private val properties = context.storageManager.createMemoizedFunction> { computeProperties(it) } + private val allDescriptors = context.storageManager.createLazyValue { computeAllDescriptors() } + + protected open fun filteredMemberProtos(allMemberProtos: Collection): Collection = allMemberProtos + + private fun groupByName(membersList: Collection): Map> { + val map = HashMap>() + for (memberProto in membersList) { + val name = context.nameResolver.getName(memberProto.getName()) + var protos = map[name] + if (protos == null) { + protos = ArrayList(1) + map.put(name, protos) + } + protos!!.add(memberProto) + } + return map + } + + private fun computeMembersByName(name: Name, callableKind: Filter): MutableCollection { + val memberProtos = membersProtos.invoke().get(name) + + val descriptors = LinkedHashSet(if (memberProtos != null) memberProtos.size() else 0) + if (memberProtos != null) { + for (memberProto in memberProtos) { + if (callableKind.accept(Flags.CALLABLE_KIND.get(memberProto.getFlags()))) { + [suppress("UNCHECKED_CAST")] + descriptors.add(context.deserializer.loadCallable(memberProto) as D) + } + } + } + return descriptors + } + + private fun computeFunctions(name: Name): Collection { + val descriptors = computeMembersByName(name, FUNCTION) + computeNonDeclaredFunctions(name, descriptors) + return descriptors.toReadOnlyList() + } + + protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection) { + } + + override fun getFunctions(name: Name): Collection = functions(name) + + private fun computeProperties(name: Name): Collection { + val descriptors = computeMembersByName(name, PROPERTY) + computeNonDeclaredProperties(name, descriptors) + return descriptors.toReadOnlyList() + } + + protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection) { + } + + override fun getProperties(name: Name): Collection = properties.invoke(name) + + override fun getClassifier(name: Name) = getClassDescriptor(name) + + protected abstract fun getClassDescriptor(name: Name): ClassifierDescriptor? + + protected abstract fun addAllClassDescriptors(result: MutableCollection) + + override fun getPackage(name: Name): PackageViewDescriptor? = null + + override fun getLocalVariable(name: Name): VariableDescriptor? = null + + override fun getContainingDeclaration() = context.containingDeclaration + + override fun getDeclarationsByLabel(labelName: Name): Collection = listOf() + + private fun computeAllDescriptors(): Collection { + val result = LinkedHashSet(0) + + for (name in membersProtos.invoke().keySet()) { + result.addAll(getFunctions(name)) + result.addAll(getProperties(name)) + } + + addNonDeclaredDescriptors(result) + + addAllClassDescriptors(result) + + return result.toReadOnlyList() + } + + protected abstract fun addNonDeclaredDescriptors(result: MutableCollection) + + override fun getAllDescriptors(): Collection = allDescriptors.invoke() + + override fun getImplicitReceiversHierarchy(): List { + val receiver = getImplicitReceiver() + if (receiver != null) { + return listOf(receiver) + } + return listOf() + } + + protected abstract fun getImplicitReceiver(): ReceiverParameterDescriptor? + + override fun getOwnDeclaredDescriptors(): Collection = getAllDescriptors() + + private trait Filter { + fun accept(value: T): Boolean + } + + override fun printScopeStructure(p: Printer) { + p.println(javaClass.getSimpleName(), " {") + p.pushIndent() + + p.println("containingDeclaration = " + getContainingDeclaration()) + + p.popIndent() + p.println("}") + } + + class object { + + private val FUNCTION = object : Filter { + override fun accept(value: ProtoBuf.Callable.CallableKind): Boolean { + return value == ProtoBuf.Callable.CallableKind.FUN + } + } + private val PROPERTY = object : Filter { + override fun accept(value: ProtoBuf.Callable.CallableKind): Boolean { + return value == ProtoBuf.Callable.CallableKind.VAL || value == ProtoBuf.Callable.CallableKind.VAR + } + } + } +}