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