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.calls.tasks;
018
019 import com.google.common.collect.Lists;
020 import com.google.common.collect.Sets;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.jet.lang.descriptors.*;
023 import org.jetbrains.jet.lang.resolve.BindingTrace;
024 import org.jetbrains.jet.lang.resolve.name.Name;
025 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
026 import org.jetbrains.jet.lang.types.ErrorUtils;
027 import org.jetbrains.jet.lang.types.JetType;
028
029 import java.util.*;
030
031 import static org.jetbrains.jet.lang.resolve.LibrarySourceHacks.filterOutMembersFromLibrarySource;
032
033 public class CallableDescriptorCollectors {
034 public static CallableDescriptorCollector<FunctionDescriptor> FUNCTIONS =
035 new FilteredCollector<FunctionDescriptor>(new FunctionCollector());
036 public static CallableDescriptorCollector<VariableDescriptor> VARIABLES =
037 new FilteredCollector<VariableDescriptor>(new VariableCollector());
038 public static CallableDescriptorCollector<VariableDescriptor> PROPERTIES =
039 new FilteredCollector<VariableDescriptor>(new PropertyCollector());
040 public static List<CallableDescriptorCollector<? extends CallableDescriptor>> FUNCTIONS_AND_VARIABLES =
041 Lists.newArrayList(FUNCTIONS, VARIABLES);
042
043 private static class FunctionCollector implements CallableDescriptorCollector<FunctionDescriptor> {
044
045 @NotNull
046 @Override
047 public Collection<FunctionDescriptor> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
048 Set<FunctionDescriptor> functions = Sets.newLinkedHashSet();
049 for (FunctionDescriptor function : scope.getFunctions(name)) {
050 if (function.getReceiverParameter() == null) {
051 functions.add(function);
052 }
053 }
054 addConstructors(scope, name, functions);
055 return functions;
056 }
057
058 @NotNull
059 @Override
060 public Collection<FunctionDescriptor> getMembersByName(@NotNull JetType receiverType, Name name, @NotNull BindingTrace bindingTrace) {
061 JetScope receiverScope = receiverType.getMemberScope();
062 Set<FunctionDescriptor> members = Sets.newHashSet(receiverScope.getFunctions(name));
063 addConstructors(receiverScope, name, members);
064 return members;
065 }
066
067 @NotNull
068 @Override
069 public Collection<FunctionDescriptor> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
070 return scope.getFunctions(name);
071 }
072
073 private static void addConstructors(JetScope scope, Name name, Collection<FunctionDescriptor> functions) {
074 ClassifierDescriptor classifier = scope.getClassifier(name);
075 if (classifier instanceof ClassDescriptor && !ErrorUtils.isError(classifier)) {
076 functions.addAll(((ClassDescriptor) classifier).getConstructors());
077 }
078 }
079
080 @Override
081 public String toString() {
082 return "FUNCTIONS";
083 }
084 }
085
086 private static class VariableCollector implements CallableDescriptorCollector<VariableDescriptor> {
087
088 @NotNull
089 @Override
090 public Collection<VariableDescriptor> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
091 VariableDescriptor localVariable = scope.getLocalVariable(name);
092 if (localVariable != null) {
093 return Collections.singleton(localVariable);
094 }
095
096 LinkedHashSet<VariableDescriptor> variables = Sets.newLinkedHashSet();
097 for (VariableDescriptor variable : scope.getProperties(name)) {
098 if (variable.getReceiverParameter() == null) {
099 variables.add(variable);
100 }
101 }
102 return variables;
103 }
104
105 @NotNull
106 @Override
107 public Collection<VariableDescriptor> getMembersByName(@NotNull JetType receiverType, Name name, @NotNull BindingTrace bindingTrace) {
108 return receiverType.getMemberScope().getProperties(name);
109 }
110
111 @NotNull
112 @Override
113 public Collection<VariableDescriptor> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
114 Collection<VariableDescriptor> result = Sets.newLinkedHashSet();
115
116 VariableDescriptor localVariable = scope.getLocalVariable(name);
117 if (localVariable != null) {
118 result.add(localVariable);
119 }
120 result.addAll(scope.getProperties(name));
121 return result;
122 }
123
124 @Override
125 public String toString() {
126 return "VARIABLES";
127 }
128 }
129
130 private static class PropertyCollector implements CallableDescriptorCollector<VariableDescriptor> {
131 private static Collection<VariableDescriptor> filterProperties(Collection<? extends VariableDescriptor> variableDescriptors) {
132 ArrayList<VariableDescriptor> properties = Lists.newArrayList();
133 for (VariableDescriptor descriptor : variableDescriptors) {
134 if (descriptor instanceof PropertyDescriptor) {
135 properties.add(descriptor);
136 }
137 }
138 return properties;
139 }
140
141 @NotNull
142 @Override
143 public Collection<VariableDescriptor> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
144 return filterProperties(VARIABLES.getNonExtensionsByName(scope, name, bindingTrace));
145 }
146
147 @NotNull
148 @Override
149 public Collection<VariableDescriptor> getMembersByName(@NotNull JetType receiver, Name name, @NotNull BindingTrace bindingTrace) {
150 return filterProperties(VARIABLES.getMembersByName(receiver, name, bindingTrace));
151 }
152
153 @NotNull
154 @Override
155 public Collection<VariableDescriptor> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
156 return filterProperties(VARIABLES.getNonMembersByName(scope, name, bindingTrace));
157 }
158
159 @Override
160 public String toString() {
161 return "PROPERTIES";
162 }
163 }
164
165 private static class FilteredCollector<D extends CallableDescriptor> implements CallableDescriptorCollector<D> {
166 private final CallableDescriptorCollector<D> delegate;
167
168 private FilteredCollector(CallableDescriptorCollector<D> delegate) {
169 this.delegate = delegate;
170 }
171
172 @NotNull
173 @Override
174 public Collection<D> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
175 return filterOutMembersFromLibrarySource(delegate.getNonExtensionsByName(scope, name, bindingTrace), bindingTrace);
176 }
177
178 @NotNull
179 @Override
180 public Collection<D> getMembersByName(@NotNull JetType receiver, Name name, @NotNull BindingTrace bindingTrace) {
181 return filterOutMembersFromLibrarySource(delegate.getMembersByName(receiver, name, bindingTrace), bindingTrace);
182 }
183
184 @NotNull
185 @Override
186 public Collection<D> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
187 return filterOutMembersFromLibrarySource(delegate.getNonMembersByName(scope, name, bindingTrace), bindingTrace);
188 }
189
190 @Override
191 public String toString() {
192 return delegate.toString();
193 }
194 }
195
196 private CallableDescriptorCollectors() {
197 }
198 }