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.getTypeConstructor())) {
076                    ClassDescriptor classDescriptor = (ClassDescriptor) classifier;
077                    functions.addAll(classDescriptor.getConstructors());
078                }
079            }
080    
081            @Override
082            public String toString() {
083                return "FUNCTIONS";
084            }
085        }
086    
087        private static class VariableCollector implements CallableDescriptorCollector<VariableDescriptor> {
088    
089            @NotNull
090            @Override
091            public Collection<VariableDescriptor> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
092                VariableDescriptor localVariable = scope.getLocalVariable(name);
093                if (localVariable != null) {
094                    return Collections.singleton(localVariable);
095                }
096    
097                LinkedHashSet<VariableDescriptor> variables = Sets.newLinkedHashSet();
098                for (VariableDescriptor variable : scope.getProperties(name)) {
099                    if (variable.getReceiverParameter() == null) {
100                        variables.add(variable);
101                    }
102                }
103                return variables;
104            }
105    
106            @NotNull
107            @Override
108            public Collection<VariableDescriptor> getMembersByName(@NotNull JetType receiverType, Name name, @NotNull BindingTrace bindingTrace) {
109                return receiverType.getMemberScope().getProperties(name);
110            }
111    
112            @NotNull
113            @Override
114            public Collection<VariableDescriptor> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
115                Collection<VariableDescriptor> result = Sets.newLinkedHashSet();
116    
117                VariableDescriptor localVariable = scope.getLocalVariable(name);
118                if (localVariable != null) {
119                    result.add(localVariable);
120                }
121                result.addAll(scope.getProperties(name));
122                return result;
123            }
124    
125            @Override
126            public String toString() {
127                return "VARIABLES";
128            }
129        }
130    
131        private static class PropertyCollector implements CallableDescriptorCollector<VariableDescriptor> {
132            private static Collection<VariableDescriptor> filterProperties(Collection<? extends VariableDescriptor> variableDescriptors) {
133                ArrayList<VariableDescriptor> properties = Lists.newArrayList();
134                for (VariableDescriptor descriptor : variableDescriptors) {
135                    if (descriptor instanceof PropertyDescriptor) {
136                        properties.add(descriptor);
137                    }
138                }
139                return properties;
140            }
141    
142            @NotNull
143            @Override
144            public Collection<VariableDescriptor> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
145                return filterProperties(VARIABLES.getNonExtensionsByName(scope, name, bindingTrace));
146            }
147    
148            @NotNull
149            @Override
150            public Collection<VariableDescriptor> getMembersByName(@NotNull JetType receiver, Name name, @NotNull BindingTrace bindingTrace) {
151                return filterProperties(VARIABLES.getMembersByName(receiver, name, bindingTrace));
152            }
153    
154            @NotNull
155            @Override
156            public Collection<VariableDescriptor> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
157                return filterProperties(VARIABLES.getNonMembersByName(scope, name, bindingTrace));
158            }
159    
160            @Override
161            public String toString() {
162                return "PROPERTIES";
163            }
164        }
165        
166        private static class FilteredCollector<D extends CallableDescriptor> implements CallableDescriptorCollector<D> {
167            private final CallableDescriptorCollector<D> delegate;
168    
169            private FilteredCollector(CallableDescriptorCollector<D> delegate) {
170                this.delegate = delegate;
171            }
172    
173            @NotNull
174            @Override
175            public Collection<D> getNonExtensionsByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
176                return filterOutMembersFromLibrarySource(delegate.getNonExtensionsByName(scope, name, bindingTrace), bindingTrace);
177            }
178    
179            @NotNull
180            @Override
181            public Collection<D> getMembersByName(@NotNull JetType receiver, Name name, @NotNull BindingTrace bindingTrace) {
182                return filterOutMembersFromLibrarySource(delegate.getMembersByName(receiver, name, bindingTrace), bindingTrace);
183            }
184    
185            @NotNull
186            @Override
187            public Collection<D> getNonMembersByName(JetScope scope, Name name, @NotNull BindingTrace bindingTrace) {
188                return filterOutMembersFromLibrarySource(delegate.getNonMembersByName(scope, name, bindingTrace), bindingTrace);
189            }
190    
191            @Override
192            public String toString() {
193                return delegate.toString();
194            }
195        }
196    
197        private CallableDescriptorCollectors() {
198        }
199    }