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.scopes;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.resolve.name.Name;
023    import org.jetbrains.jet.lang.types.TypeSubstitutor;
024    import org.jetbrains.jet.utils.Printer;
025    import org.jetbrains.jet.utils.UtilsPackage;
026    
027    import java.util.*;
028    
029    public class SubstitutingScope implements JetScope {
030    
031        private final JetScope workerScope;
032        private final TypeSubstitutor substitutor;
033    
034        private Map<DeclarationDescriptor, DeclarationDescriptor> substitutedDescriptors = null;
035        private Collection<DeclarationDescriptor> allDescriptors = null;
036    
037        public SubstitutingScope(JetScope workerScope, @NotNull TypeSubstitutor substitutor) {
038            this.workerScope = workerScope;
039            this.substitutor = substitutor;
040        }
041    
042        @Nullable
043        private <D extends DeclarationDescriptor> D substitute(@Nullable D descriptor) {
044            if (descriptor == null) return null;
045            if (substitutor.isEmpty()) return descriptor;
046    
047            if (substitutedDescriptors == null) {
048                substitutedDescriptors = new HashMap<DeclarationDescriptor, DeclarationDescriptor>();
049            }
050    
051            DeclarationDescriptor substituted = substitutedDescriptors.get(descriptor);
052            if (substituted == null && !substitutedDescriptors.containsKey(descriptor)) {
053                substituted = descriptor.substitute(substitutor);
054    
055                //noinspection ConstantConditions
056                substitutedDescriptors.put(descriptor, substituted);
057            }
058    
059            //noinspection unchecked
060            return (D) substituted;
061        }
062    
063        @NotNull
064        private <D extends DeclarationDescriptor> Collection<D> substitute(@NotNull Collection<D> descriptors) {
065            if (substitutor.isEmpty()) return descriptors;
066            if (descriptors.isEmpty()) return descriptors;
067    
068            Set<D> result = UtilsPackage.newHashSetWithExpectedSize(descriptors.size());
069            for (D descriptor : descriptors) {
070                D substitute = substitute(descriptor);
071                if (substitute != null) {
072                    result.add(substitute);
073                }
074            }
075    
076            return result;
077        }
078    
079        @NotNull
080        @Override
081        public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
082            return substitute(workerScope.getProperties(name));
083        }
084    
085        @Override
086        public VariableDescriptor getLocalVariable(@NotNull Name name) {
087            return substitute(workerScope.getLocalVariable(name));
088        }
089    
090        @Override
091        public ClassifierDescriptor getClassifier(@NotNull Name name) {
092            return substitute(workerScope.getClassifier(name));
093        }
094    
095        @NotNull
096        @Override
097        public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
098            return substitute(workerScope.getFunctions(name));
099        }
100    
101        @Override
102        public PackageViewDescriptor getPackage(@NotNull Name name) {
103            return workerScope.getPackage(name);
104        }
105    
106        @NotNull
107        @Override
108        public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
109            throw new UnsupportedOperationException(); // TODO
110        }
111    
112        @NotNull
113        @Override
114        public DeclarationDescriptor getContainingDeclaration() {
115            return workerScope.getContainingDeclaration();
116        }
117    
118        @NotNull
119        @Override
120        public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) {
121            throw new UnsupportedOperationException(); // TODO
122        }
123    
124        @NotNull
125        @Override
126        public Collection<DeclarationDescriptor> getAllDescriptors() {
127            if (allDescriptors == null) {
128                allDescriptors = substitute(workerScope.getAllDescriptors());
129            }
130            return allDescriptors;
131        }
132    
133        @NotNull
134        @Override
135        public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
136            return substitute(workerScope.getOwnDeclaredDescriptors());
137        }
138    
139        @Override
140        public void printScopeStructure(@NotNull Printer p) {
141            p.println(getClass().getSimpleName(), " {");
142            p.pushIndent();
143    
144            p.println("substitutor = ");
145            p.pushIndent();
146            p.println(substitutor);
147            p.popIndent();
148    
149            p.print("workerScope = ");
150            workerScope.printScopeStructure(p.withholdIndentOnce());
151    
152            p.popIndent();
153            p.println("}");
154        }
155    }