Converted DeclarationProvider and all its inheritors to Kotlin

This commit is contained in:
Valentin Kipyatkov
2014-10-31 21:46:08 +03:00
parent e2e7210b37
commit 9c9b431d7b
10 changed files with 227 additions and 335 deletions
@@ -1,125 +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.lang.resolve.lazy.declarations;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import kotlin.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.data.JetScriptInfo;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils.safeNameForLazyResolve;
public abstract class AbstractPsiBasedDeclarationProvider implements DeclarationProvider {
protected static class Index {
// This mutable state is only modified under inside the computable
private final List<JetDeclaration> allDeclarations = Lists.newArrayList();
private final Multimap<Name, JetNamedFunction> functions = HashMultimap.create();
private final Multimap<Name, JetProperty> properties = HashMultimap.create();
private final Multimap<Name, JetClassLikeInfo> classesAndObjects = ArrayListMultimap.create(); // order matters here
public void putToIndex(@NotNull JetDeclaration declaration) {
if (declaration instanceof JetClassInitializer) {
return;
}
allDeclarations.add(declaration);
if (declaration instanceof JetNamedFunction) {
JetNamedFunction namedFunction = (JetNamedFunction) declaration;
functions.put(safeNameForLazyResolve(namedFunction), namedFunction);
}
else if (declaration instanceof JetProperty) {
JetProperty property = (JetProperty) declaration;
properties.put(safeNameForLazyResolve(property), property);
}
else if (declaration instanceof JetClassOrObject) {
JetClassOrObject classOrObject = (JetClassOrObject) declaration;
classesAndObjects.put(
safeNameForLazyResolve(classOrObject.getNameAsName()),
JetClassInfoUtil.createClassLikeInfo(classOrObject)
);
}
else if (declaration instanceof JetScript) {
JetScript script = (JetScript) declaration;
JetScriptInfo scriptInfo = new JetScriptInfo(script);
classesAndObjects.put(
scriptInfo.getFqName().shortName(),
scriptInfo
);
}
else if (declaration instanceof JetParameter ||
declaration instanceof JetTypedef ||
declaration instanceof JetMultiDeclaration) {
// Do nothing, just put it into allDeclarations is enough
}
else {
throw new IllegalArgumentException("Unknown declaration: " + declaration);
}
}
}
private final NotNullLazyValue<Index> index;
public AbstractPsiBasedDeclarationProvider(@NotNull StorageManager storageManager) {
index = storageManager.createLazyValue(new Function0<Index>() {
@Override
public Index invoke() {
Index index = new Index();
doCreateIndex(index);
return index;
}
});
}
protected abstract void doCreateIndex(@NotNull Index index);
@NotNull
@Override
public List<JetDeclaration> getAllDeclarations() {
return index.invoke().allDeclarations;
}
@NotNull
@Override
public List<JetNamedFunction> getFunctionDeclarations(@NotNull Name name) {
return Lists.newArrayList(index.invoke().functions.get(ResolveSessionUtils.safeNameForLazyResolve(name)));
}
@NotNull
@Override
public List<JetProperty> getPropertyDeclarations(@NotNull Name name) {
return Lists.newArrayList(index.invoke().properties.get(ResolveSessionUtils.safeNameForLazyResolve(name)));
}
@NotNull
@Override
public Collection<JetClassLikeInfo> getClassOrObjectDeclarations(@NotNull Name name) {
return index.invoke().classesAndObjects.get(ResolveSessionUtils.safeNameForLazyResolve(name));
}
}
@@ -0,0 +1,85 @@
/*
* 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.lang.resolve.lazy.declarations
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.HashMultimap
import org.jetbrains.jet.lang.psi.*
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo
import org.jetbrains.jet.lang.resolve.lazy.data.JetScriptInfo
import org.jetbrains.jet.lang.resolve.name.Name
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils.safeNameForLazyResolve
import java.util.ArrayList
public abstract class AbstractPsiBasedDeclarationProvider(storageManager: StorageManager) : DeclarationProvider {
protected class Index {
// This mutable state is only modified under inside the computable
val allDeclarations = ArrayList<JetDeclaration>()
val functions = HashMultimap.create<Name, JetNamedFunction>()
val properties = HashMultimap.create<Name, JetProperty>()
val classesAndObjects = ArrayListMultimap.create<Name, JetClassLikeInfo>() // order matters here
public fun putToIndex(declaration: JetDeclaration) {
if (declaration is JetClassInitializer) return
allDeclarations.add(declaration)
if (declaration is JetNamedFunction) {
functions.put(safeNameForLazyResolve(declaration), declaration)
}
else if (declaration is JetProperty) {
properties.put(safeNameForLazyResolve(declaration), declaration)
}
else if (declaration is JetClassOrObject) {
classesAndObjects.put(safeNameForLazyResolve(declaration.getNameAsName()), JetClassInfoUtil.createClassLikeInfo(declaration))
}
else if (declaration is JetScript) {
val scriptInfo = JetScriptInfo(declaration)
classesAndObjects.put(scriptInfo.fqName.shortName(), scriptInfo)
}
else if (declaration is JetParameter || declaration is JetTypedef || declaration is JetMultiDeclaration) {
// Do nothing, just put it into allDeclarations is enough
}
else {
throw IllegalArgumentException("Unknown declaration: " + declaration)
}
}
}
private val index = storageManager.createLazyValue<Index> {
val index = Index()
doCreateIndex(index)
index
}
protected abstract fun doCreateIndex(index: Index)
override fun getAllDeclarations(): List<JetDeclaration> = index().allDeclarations
override fun getFunctionDeclarations(name: Name): List<JetNamedFunction>
= index().functions[ResolveSessionUtils.safeNameForLazyResolve(name)].toList()
override fun getPropertyDeclarations(name: Name): List<JetProperty>
= index().properties[ResolveSessionUtils.safeNameForLazyResolve(name)].toList()
override fun getClassOrObjectDeclarations(name: Name): Collection<JetClassLikeInfo>
= index().classesAndObjects[ResolveSessionUtils.safeNameForLazyResolve(name)]
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* 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.
@@ -14,13 +14,11 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.lazy.declarations;
package org.jetbrains.jet.lang.resolve.lazy.declarations
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo
public interface ClassMemberDeclarationProvider extends DeclarationProvider {
@NotNull
JetClassLikeInfo getOwnerInfo();
public trait ClassMemberDeclarationProvider : DeclarationProvider {
public fun getOwnerInfo(): JetClassLikeInfo
}
@@ -1,46 +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.lang.resolve.lazy.declarations;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import java.util.List;
public interface DeclarationProvider {
@ReadOnly
@NotNull
List<JetDeclaration> getAllDeclarations();
@ReadOnly
@NotNull
Collection<JetNamedFunction> getFunctionDeclarations(@NotNull Name name);
@ReadOnly
@NotNull
Collection<JetProperty> getPropertyDeclarations(@NotNull Name name);
@ReadOnly
@NotNull
Collection<JetClassLikeInfo> getClassOrObjectDeclarations(@NotNull Name name);
}
@@ -0,0 +1,34 @@
/*
* 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.lang.resolve.lazy.declarations
import org.jetbrains.annotations.ReadOnly
import org.jetbrains.jet.lang.psi.JetDeclaration
import org.jetbrains.jet.lang.psi.JetNamedFunction
import org.jetbrains.jet.lang.psi.JetProperty
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo
import org.jetbrains.jet.lang.resolve.name.Name
public trait DeclarationProvider {
public fun getAllDeclarations(): List<JetDeclaration>
public fun getFunctionDeclarations(name: Name): Collection<JetNamedFunction>
public fun getPropertyDeclarations(name: Name): Collection<JetProperty>
public fun getClassOrObjectDeclarations(name: Name): Collection<JetClassLikeInfo>
}
@@ -1,81 +0,0 @@
/*
* 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.lang.resolve.lazy.declarations;
import kotlin.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.storage.NotNullLazyValue;
import org.jetbrains.jet.storage.StorageManager;
import java.util.Collection;
public class FileBasedPackageMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider implements PackageMemberDeclarationProvider {
private final FqName fqName;
private final FileBasedDeclarationProviderFactory factory;
private final Collection<JetFile> packageFiles;
private final NotNullLazyValue<Collection<FqName>> allDeclaredSubPackages;
/*package*/ FileBasedPackageMemberDeclarationProvider(
@NotNull StorageManager storageManager,
@NotNull FqName _fqName,
@NotNull FileBasedDeclarationProviderFactory _factory,
@NotNull Collection<JetFile> packageFiles
) {
super(storageManager);
this.fqName = _fqName;
this.factory = _factory;
this.packageFiles = packageFiles;
this.allDeclaredSubPackages = storageManager.createLazyValue(new Function0<Collection<FqName>>() {
@Override
public Collection<FqName> invoke() {
return factory.getAllDeclaredSubPackagesOf(fqName);
}
});
}
@Override
protected void doCreateIndex(@NotNull Index index) {
for (JetFile file : packageFiles) {
for (JetDeclaration declaration : file.getDeclarations()) {
assert fqName.equals(file.getPackageFqName()) : "Files declaration utils contains file with invalid package";
index.putToIndex(declaration);
}
}
}
@NotNull
@Override
public Collection<FqName> getAllDeclaredSubPackages() {
return allDeclaredSubPackages.invoke();
}
@NotNull
@Override
public Collection<JetFile> getPackageFiles() {
return packageFiles;
}
@Override
public String toString() {
return "Declarations for package " + fqName;
}
}
@@ -0,0 +1,51 @@
/*
* 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.lang.resolve.lazy.declarations
import kotlin.Function0
import org.jetbrains.jet.lang.psi.JetDeclaration
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.resolve.name.FqName
import org.jetbrains.jet.storage.NotNullLazyValue
import org.jetbrains.jet.storage.StorageManager
public class FileBasedPackageMemberDeclarationProvider internal(
storageManager: StorageManager,
private val fqName: FqName,
private val factory: FileBasedDeclarationProviderFactory,
private val packageFiles: Collection<JetFile>)
: AbstractPsiBasedDeclarationProvider(storageManager), PackageMemberDeclarationProvider {
private val allDeclaredSubPackages = storageManager.createLazyValue<Collection<FqName>> {
factory.getAllDeclaredSubPackagesOf(fqName)
}
override fun doCreateIndex(index: AbstractPsiBasedDeclarationProvider.Index) {
for (file in packageFiles) {
for (declaration in file.getDeclarations()) {
assert(fqName == file.getPackageFqName(), "Files declaration utils contains file with invalid package")
index.putToIndex(declaration)
}
}
}
override fun getAllDeclaredSubPackages(): Collection<FqName> = allDeclaredSubPackages()
override fun getPackageFiles() = packageFiles
override fun toString() = "Declarations for package $fqName"
}
@@ -14,22 +14,14 @@
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.lazy.declarations;
package org.jetbrains.jet.lang.resolve.lazy.declarations
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.ReadOnly;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.annotations.ReadOnly
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.resolve.name.FqName
import java.util.Collection;
public trait PackageMemberDeclarationProvider : DeclarationProvider {
public fun getAllDeclaredSubPackages(): Collection<FqName>
public interface PackageMemberDeclarationProvider extends DeclarationProvider {
@ReadOnly
@NotNull
Collection<FqName> getAllDeclaredSubPackages();
@ReadOnly
@NotNull
Collection<JetFile> getPackageFiles();
public fun getPackageFiles(): Collection<JetFile>
}
@@ -1,61 +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.lang.resolve.lazy.declarations;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.storage.StorageManager;
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
public class PsiBasedClassMemberDeclarationProvider extends AbstractPsiBasedDeclarationProvider implements ClassMemberDeclarationProvider {
private final JetClassLikeInfo classInfo;
public PsiBasedClassMemberDeclarationProvider(@NotNull StorageManager storageManager, @NotNull JetClassLikeInfo classInfo) {
super(storageManager);
this.classInfo = classInfo;
}
@NotNull
@Override
public JetClassLikeInfo getOwnerInfo() {
return classInfo;
}
@Override
protected void doCreateIndex(@NotNull Index index) {
for (JetDeclaration declaration : classInfo.getDeclarations()) {
if (declaration instanceof JetClassObject) {
// Do nothing, class object will be taken directly from the classInfo
}
else {
index.putToIndex(declaration);
}
}
for (JetParameter parameter : classInfo.getPrimaryConstructorParameters()) {
if (parameter.hasValOrVarNode()) {
index.putToIndex(parameter);
}
}
}
@Override
public String toString() {
return "Declarations for " + classInfo;
}
}
@@ -0,0 +1,45 @@
/*
* 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.lang.resolve.lazy.declarations
import org.jetbrains.jet.lang.psi.*
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo
public class PsiBasedClassMemberDeclarationProvider(
storageManager: StorageManager,
private val classInfo: JetClassLikeInfo)
: AbstractPsiBasedDeclarationProvider(storageManager), ClassMemberDeclarationProvider {
override fun getOwnerInfo() = classInfo
override fun doCreateIndex(index: AbstractPsiBasedDeclarationProvider.Index) {
for (declaration in classInfo.getDeclarations()) {
if (declaration !is JetClassObject) { // Do nothing for class object because it will be taken directly from the classInfo
index.putToIndex(declaration)
}
}
for (parameter in classInfo.getPrimaryConstructorParameters()) {
if (parameter.hasValOrVarNode()) {
index.putToIndex(parameter)
}
}
}
override fun toString() = "Declarations for $classInfo"
}