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