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 @Override
102 public ClassDescriptor getObjectDescriptor(@NotNull Name name) {
103 return substitute(workerScope.getObjectDescriptor(name));
104 }
105
106 @NotNull
107 @Override
108 public Collection<ClassDescriptor> getObjectDescriptors() {
109 return substitute(workerScope.getObjectDescriptors());
110 }
111
112 @NotNull
113 @Override
114 public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
115 return substitute(workerScope.getFunctions(name));
116 }
117
118 @Override
119 public NamespaceDescriptor getNamespace(@NotNull Name name) {
120 return workerScope.getNamespace(name); // TODO
121 }
122
123 @NotNull
124 @Override
125 public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
126 throw new UnsupportedOperationException(); // TODO
127 }
128
129 @NotNull
130 @Override
131 public DeclarationDescriptor getContainingDeclaration() {
132 return workerScope.getContainingDeclaration();
133 }
134
135 @NotNull
136 @Override
137 public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
138 throw new UnsupportedOperationException(); // TODO
139 }
140
141 @NotNull
142 @Override
143 public Collection<DeclarationDescriptor> getAllDescriptors() {
144 if (allDescriptors == null) {
145 allDescriptors = substitute(workerScope.getAllDescriptors());
146 }
147 return allDescriptors;
148 }
149
150 @NotNull
151 @Override
152 public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
153 return substitute(workerScope.getOwnDeclaredDescriptors());
154 }
155
156 @TestOnly
157 @Override
158 public void printScopeStructure(@NotNull Printer p) {
159 p.println(getClass().getSimpleName(), " {");
160 p.pushIndent();
161
162 p.println("substitutor = ");
163 p.pushIndent();
164 p.println(substitutor);
165 p.popIndent();
166
167 p.print("workerScope = ");
168 workerScope.printScopeStructure(p.withholdIndentOnce());
169
170 p.popIndent();
171 p.println("}");
172 }
173 }