001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.lazy.declarations;
018
019 import com.google.common.collect.ArrayListMultimap;
020 import com.google.common.collect.HashMultimap;
021 import com.google.common.collect.Lists;
022 import com.google.common.collect.Multimap;
023 import kotlin.Function0;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.jet.lang.psi.*;
026 import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
027 import org.jetbrains.jet.lang.resolve.lazy.data.JetClassInfoUtil;
028 import org.jetbrains.jet.lang.resolve.lazy.data.JetClassLikeInfo;
029 import org.jetbrains.jet.lang.resolve.lazy.data.JetScriptInfo;
030 import org.jetbrains.jet.lang.resolve.name.Name;
031 import org.jetbrains.jet.storage.NotNullLazyValue;
032 import org.jetbrains.jet.storage.StorageManager;
033
034 import java.util.Collection;
035 import java.util.List;
036
037 import static org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils.safeNameForLazyResolve;
038
039 public abstract class AbstractPsiBasedDeclarationProvider implements DeclarationProvider {
040
041 protected static class Index {
042 // This mutable state is only modified under inside the computable
043 private final List<JetDeclaration> allDeclarations = Lists.newArrayList();
044 private final Multimap<Name, JetNamedFunction> functions = HashMultimap.create();
045 private final Multimap<Name, JetProperty> properties = HashMultimap.create();
046 private final Multimap<Name, JetClassLikeInfo> classesAndObjects = ArrayListMultimap.create(); // order matters here
047
048 public void putToIndex(@NotNull JetDeclaration declaration) {
049 if (declaration instanceof JetClassInitializer) {
050 return;
051 }
052 allDeclarations.add(declaration);
053 if (declaration instanceof JetNamedFunction) {
054 JetNamedFunction namedFunction = (JetNamedFunction) declaration;
055 functions.put(safeNameForLazyResolve(namedFunction), namedFunction);
056 }
057 else if (declaration instanceof JetProperty) {
058 JetProperty property = (JetProperty) declaration;
059 properties.put(safeNameForLazyResolve(property), property);
060 }
061 else if (declaration instanceof JetClassOrObject) {
062 JetClassOrObject classOrObject = (JetClassOrObject) declaration;
063 classesAndObjects.put(
064 safeNameForLazyResolve(classOrObject.getNameAsName()),
065 JetClassInfoUtil.createClassLikeInfo(classOrObject)
066 );
067 }
068 else if (declaration instanceof JetScript) {
069 JetScript script = (JetScript) declaration;
070 JetScriptInfo scriptInfo = new JetScriptInfo(script);
071 classesAndObjects.put(
072 scriptInfo.getFqName().shortName(),
073 scriptInfo
074 );
075 }
076 else if (declaration instanceof JetParameter ||
077 declaration instanceof JetTypedef ||
078 declaration instanceof JetMultiDeclaration) {
079 // Do nothing, just put it into allDeclarations is enough
080 }
081 else {
082 throw new IllegalArgumentException("Unknown declaration: " + declaration);
083 }
084 }
085 }
086
087 private final NotNullLazyValue<Index> index;
088
089 public AbstractPsiBasedDeclarationProvider(@NotNull StorageManager storageManager) {
090 index = storageManager.createLazyValue(new Function0<Index>() {
091 @Override
092 public Index invoke() {
093 Index index = new Index();
094 doCreateIndex(index);
095 return index;
096 }
097 });
098 }
099
100 protected abstract void doCreateIndex(@NotNull Index index);
101
102 @NotNull
103 @Override
104 public List<JetDeclaration> getAllDeclarations() {
105 return index.invoke().allDeclarations;
106 }
107
108 @NotNull
109 @Override
110 public List<JetNamedFunction> getFunctionDeclarations(@NotNull Name name) {
111 return Lists.newArrayList(index.invoke().functions.get(ResolveSessionUtils.safeNameForLazyResolve(name)));
112 }
113
114 @NotNull
115 @Override
116 public List<JetProperty> getPropertyDeclarations(@NotNull Name name) {
117 return Lists.newArrayList(index.invoke().properties.get(ResolveSessionUtils.safeNameForLazyResolve(name)));
118 }
119
120 @NotNull
121 @Override
122 public Collection<JetClassLikeInfo> getClassOrObjectDeclarations(@NotNull Name name) {
123 return index.invoke().classesAndObjects.get(ResolveSessionUtils.safeNameForLazyResolve(name));
124 }
125 }