Converted DeserializedMemberScope to Kotlin
This commit is contained in:
-259
@@ -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<ProtoBuf.Callable.CallableKind> FUNCTION = new Filter<ProtoBuf.Callable.CallableKind>() {
|
|
||||||
@Override
|
|
||||||
public boolean accept(ProtoBuf.Callable.CallableKind value) {
|
|
||||||
return value == ProtoBuf.Callable.CallableKind.FUN;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
private static final Filter<ProtoBuf.Callable.CallableKind> PROPERTY = new Filter<ProtoBuf.Callable.CallableKind>() {
|
|
||||||
@Override
|
|
||||||
public boolean accept(ProtoBuf.Callable.CallableKind value) {
|
|
||||||
return value == ProtoBuf.Callable.CallableKind.VAL ||
|
|
||||||
value == ProtoBuf.Callable.CallableKind.VAR;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final NotNullLazyValue<Map<Name, List<ProtoBuf.Callable>>> membersProtos;
|
|
||||||
|
|
||||||
private final MemoizedFunctionToNotNull<Name, Collection<FunctionDescriptor>> functions;
|
|
||||||
private final MemoizedFunctionToNotNull<Name, Collection<VariableDescriptor>> properties;
|
|
||||||
private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
|
|
||||||
private final DeserializationContextWithTypes context;
|
|
||||||
|
|
||||||
protected DeserializedMemberScope(
|
|
||||||
@NotNull DeserializationContextWithTypes context,
|
|
||||||
@NotNull final Collection<ProtoBuf.Callable> membersList
|
|
||||||
) {
|
|
||||||
this.context = context;
|
|
||||||
|
|
||||||
StorageManager storageManager = context.getStorageManager();
|
|
||||||
this.membersProtos = storageManager.createLazyValue(new Function0<Map<Name, List<ProtoBuf.Callable>>>() {
|
|
||||||
@Override
|
|
||||||
public Map<Name, List<ProtoBuf.Callable>> invoke() {
|
|
||||||
return groupByName(filteredMemberProtos(membersList));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<FunctionDescriptor>>() {
|
|
||||||
@Override
|
|
||||||
public Collection<FunctionDescriptor> invoke(Name name) {
|
|
||||||
return computeFunctions(name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.properties = storageManager.createMemoizedFunction(
|
|
||||||
new Function1<Name, Collection<VariableDescriptor>>() {
|
|
||||||
@Override
|
|
||||||
public Collection<VariableDescriptor> invoke(Name name) {
|
|
||||||
return computeProperties(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
this.allDescriptors = storageManager.createLazyValue(
|
|
||||||
new Function0<Collection<DeclarationDescriptor>>() {
|
|
||||||
@Override
|
|
||||||
public Collection<DeclarationDescriptor> invoke() {
|
|
||||||
return computeAllDescriptors();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@ReadOnly
|
|
||||||
protected Collection<ProtoBuf.Callable> filteredMemberProtos(@NotNull Collection<ProtoBuf.Callable> allMemberProtos) {
|
|
||||||
return allMemberProtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private Map<Name, List<ProtoBuf.Callable>> groupByName(@NotNull Collection<ProtoBuf.Callable> membersList) {
|
|
||||||
Map<Name, List<ProtoBuf.Callable>> map = new HashMap<Name, List<ProtoBuf.Callable>>();
|
|
||||||
for (ProtoBuf.Callable memberProto : membersList) {
|
|
||||||
Name name = context.getNameResolver().getName(memberProto.getName());
|
|
||||||
List<ProtoBuf.Callable> protos = map.get(name);
|
|
||||||
if (protos == null) {
|
|
||||||
protos = new ArrayList<ProtoBuf.Callable>(1);
|
|
||||||
map.put(name, protos);
|
|
||||||
}
|
|
||||||
protos.add(memberProto);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private <D extends CallableMemberDescriptor> Collection<D> computeMembersByName(Name name, Filter<ProtoBuf.Callable.CallableKind> callableKind) {
|
|
||||||
List<ProtoBuf.Callable> memberProtos = membersProtos.invoke().get(name);
|
|
||||||
|
|
||||||
Collection<D> descriptors = new LinkedHashSet<D>(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<FunctionDescriptor> computeFunctions(@NotNull Name name) {
|
|
||||||
Collection<FunctionDescriptor> descriptors = computeMembersByName(name, FUNCTION);
|
|
||||||
computeNonDeclaredFunctions(name, descriptors);
|
|
||||||
return UtilsPackage.toReadOnlyList(descriptors);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void computeNonDeclaredFunctions(@NotNull Name name, @NotNull Collection<FunctionDescriptor> functions) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public final Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
|
|
||||||
return functions.invoke(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private Collection<VariableDescriptor> computeProperties(@NotNull Name name) {
|
|
||||||
Collection<PropertyDescriptor> descriptors = computeMembersByName(name, PROPERTY);
|
|
||||||
computeNonDeclaredProperties(name, descriptors);
|
|
||||||
//noinspection unchecked
|
|
||||||
return UtilsPackage.<VariableDescriptor>toReadOnlyList(descriptors);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void computeNonDeclaredProperties(@NotNull Name name, @NotNull Collection<PropertyDescriptor> descriptors) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<VariableDescriptor> 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<DeclarationDescriptor> 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<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Collection<DeclarationDescriptor> computeAllDescriptors() {
|
|
||||||
Collection<DeclarationDescriptor> result = new LinkedHashSet<DeclarationDescriptor>(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<DeclarationDescriptor> result);
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public final Collection<DeclarationDescriptor> getAllDescriptors() {
|
|
||||||
return allDescriptors.invoke();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
|
|
||||||
ReceiverParameterDescriptor receiver = getImplicitReceiver();
|
|
||||||
if (receiver != null) {
|
|
||||||
return Collections.singletonList(receiver);
|
|
||||||
}
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
protected abstract ReceiverParameterDescriptor getImplicitReceiver();
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
|
|
||||||
return getAllDescriptors();
|
|
||||||
}
|
|
||||||
|
|
||||||
private interface Filter<T> {
|
|
||||||
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("}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+165
@@ -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<ProtoBuf.Callable>)
|
||||||
|
: JetScope {
|
||||||
|
|
||||||
|
private val membersProtos = context.storageManager.createLazyValue { groupByName(filteredMemberProtos(membersList)) }
|
||||||
|
private val functions = context.storageManager.createMemoizedFunction<Name, Collection<FunctionDescriptor>> { computeFunctions(it) }
|
||||||
|
private val properties = context.storageManager.createMemoizedFunction<Name, Collection<VariableDescriptor>> { computeProperties(it) }
|
||||||
|
private val allDescriptors = context.storageManager.createLazyValue { computeAllDescriptors() }
|
||||||
|
|
||||||
|
protected open fun filteredMemberProtos(allMemberProtos: Collection<ProtoBuf.Callable>): Collection<ProtoBuf.Callable> = allMemberProtos
|
||||||
|
|
||||||
|
private fun groupByName(membersList: Collection<ProtoBuf.Callable>): Map<Name, List<ProtoBuf.Callable>> {
|
||||||
|
val map = HashMap<Name, MutableList<ProtoBuf.Callable>>()
|
||||||
|
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 <D : CallableMemberDescriptor> computeMembersByName(name: Name, callableKind: Filter<ProtoBuf.Callable.CallableKind>): MutableCollection<D> {
|
||||||
|
val memberProtos = membersProtos.invoke().get(name)
|
||||||
|
|
||||||
|
val descriptors = LinkedHashSet<D>(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<FunctionDescriptor> {
|
||||||
|
val descriptors = computeMembersByName<FunctionDescriptor>(name, FUNCTION)
|
||||||
|
computeNonDeclaredFunctions(name, descriptors)
|
||||||
|
return descriptors.toReadOnlyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun computeNonDeclaredFunctions(name: Name, functions: MutableCollection<FunctionDescriptor>) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getFunctions(name: Name): Collection<FunctionDescriptor> = functions(name)
|
||||||
|
|
||||||
|
private fun computeProperties(name: Name): Collection<VariableDescriptor> {
|
||||||
|
val descriptors = computeMembersByName<PropertyDescriptor>(name, PROPERTY)
|
||||||
|
computeNonDeclaredProperties(name, descriptors)
|
||||||
|
return descriptors.toReadOnlyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun computeNonDeclaredProperties(name: Name, descriptors: MutableCollection<PropertyDescriptor>) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getProperties(name: Name): Collection<VariableDescriptor> = properties.invoke(name)
|
||||||
|
|
||||||
|
override fun getClassifier(name: Name) = getClassDescriptor(name)
|
||||||
|
|
||||||
|
protected abstract fun getClassDescriptor(name: Name): ClassifierDescriptor?
|
||||||
|
|
||||||
|
protected abstract fun addAllClassDescriptors(result: MutableCollection<DeclarationDescriptor>)
|
||||||
|
|
||||||
|
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<DeclarationDescriptor> = listOf()
|
||||||
|
|
||||||
|
private fun computeAllDescriptors(): Collection<DeclarationDescriptor> {
|
||||||
|
val result = LinkedHashSet<DeclarationDescriptor>(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<DeclarationDescriptor>)
|
||||||
|
|
||||||
|
override fun getAllDescriptors(): Collection<DeclarationDescriptor> = allDescriptors.invoke()
|
||||||
|
|
||||||
|
override fun getImplicitReceiversHierarchy(): List<ReceiverParameterDescriptor> {
|
||||||
|
val receiver = getImplicitReceiver()
|
||||||
|
if (receiver != null) {
|
||||||
|
return listOf(receiver)
|
||||||
|
}
|
||||||
|
return listOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun getImplicitReceiver(): ReceiverParameterDescriptor?
|
||||||
|
|
||||||
|
override fun getOwnDeclaredDescriptors(): Collection<DeclarationDescriptor> = getAllDescriptors()
|
||||||
|
|
||||||
|
private trait Filter<T> {
|
||||||
|
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<ProtoBuf.Callable.CallableKind> {
|
||||||
|
override fun accept(value: ProtoBuf.Callable.CallableKind): Boolean {
|
||||||
|
return value == ProtoBuf.Callable.CallableKind.FUN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private val PROPERTY = object : Filter<ProtoBuf.Callable.CallableKind> {
|
||||||
|
override fun accept(value: ProtoBuf.Callable.CallableKind): Boolean {
|
||||||
|
return value == ProtoBuf.Callable.CallableKind.VAL || value == ProtoBuf.Callable.CallableKind.VAR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user