001    /*
002     * Copyright 2010-2015 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.kotlin.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.kotlin.descriptors.*;
021    import org.jetbrains.kotlin.psi.KtCallableDeclaration;
022    import org.jetbrains.kotlin.psi.KtNamedFunction;
023    import org.jetbrains.kotlin.psi.KtProperty;
024    import org.jetbrains.kotlin.resolve.inline.InlineAnalyzerExtension;
025    import org.jetbrains.kotlin.resolve.inline.InlineUtil;
026    import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule;
027    
028    import java.util.Collections;
029    import java.util.List;
030    import java.util.Map;
031    
032    public class AnalyzerExtensions {
033    
034        public interface AnalyzerExtension {
035            void process(@NotNull CallableMemberDescriptor descriptor, @NotNull KtCallableDeclaration functionOrProperty, @NotNull BindingTrace trace);
036        }
037    
038        @NotNull private final BindingTrace trace;
039        @NotNull private final Iterable<ReasonableInlineRule> reasonableInlineRules;
040    
041        public AnalyzerExtensions(@NotNull BindingTrace trace, @NotNull Iterable<ReasonableInlineRule> reasonableInlineRules) {
042            this.trace = trace;
043            this.reasonableInlineRules = reasonableInlineRules;
044        }
045    
046        public void process(@NotNull BodiesResolveContext bodiesResolveContext) {
047            for (Map.Entry<KtNamedFunction, SimpleFunctionDescriptor> entry : bodiesResolveContext.getFunctions().entrySet()) {
048                KtNamedFunction function = entry.getKey();
049                SimpleFunctionDescriptor functionDescriptor = entry.getValue();
050    
051                for (AnalyzerExtension extension : getFunctionExtensions(functionDescriptor)) {
052                    extension.process(functionDescriptor, function, trace);
053                }
054            }
055    
056            for (Map.Entry<KtProperty, PropertyDescriptor> entry : bodiesResolveContext.getProperties().entrySet()) {
057                KtProperty function = entry.getKey();
058                PropertyDescriptor propertyDescriptor = entry.getValue();
059    
060                for (AnalyzerExtension extension : getPropertyExtensions(propertyDescriptor)) {
061                    extension.process(propertyDescriptor, function, trace);
062                }
063            }
064        }
065    
066        @NotNull
067        private List<InlineAnalyzerExtension> getFunctionExtensions(@NotNull FunctionDescriptor functionDescriptor) {
068            if (InlineUtil.isInline(functionDescriptor)) {
069                return Collections.singletonList(new InlineAnalyzerExtension(reasonableInlineRules));
070            }
071            return Collections.emptyList();
072        }
073    
074        @NotNull
075        private List<InlineAnalyzerExtension> getPropertyExtensions(@NotNull PropertyDescriptor propertyDescriptor) {
076            if (InlineUtil.hasInlineAccessors(propertyDescriptor)) {
077                return Collections.singletonList(new InlineAnalyzerExtension(reasonableInlineRules));
078            }
079            return Collections.emptyList();
080        }
081    }