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.base.Function;
020 import com.google.common.collect.Collections2;
021 import com.google.common.collect.Lists;
022 import com.google.common.collect.Sets;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.annotations.TestOnly;
026 import org.jetbrains.jet.lang.descriptors.*;
027 import org.jetbrains.jet.lang.resolve.BindingTrace;
028 import org.jetbrains.jet.lang.resolve.TraceBasedRedeclarationHandler;
029 import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue;
030 import org.jetbrains.jet.utils.Printer;
031
032 import java.util.Collection;
033 import java.util.List;
034 import java.util.Set;
035
036 public final class JetScopeUtils {
037 private JetScopeUtils() {}
038
039 @NotNull
040 public static List<ReceiverValue> getImplicitReceiversHierarchyValues(@NotNull JetScope scope) {
041 Collection<ReceiverParameterDescriptor> hierarchy = scope.getImplicitReceiversHierarchy();
042
043 return Lists.newArrayList(
044 Collections2.transform(hierarchy,
045 new Function<ReceiverParameterDescriptor, ReceiverValue>() {
046 @Override
047 public ReceiverValue apply(ReceiverParameterDescriptor receiverParameterDescriptor) {
048 return receiverParameterDescriptor.getValue();
049 }
050 })
051 );
052 }
053
054 /**
055 * Get all extension descriptors among visible descriptors for current scope.
056 *
057 * @param scope Scope for query extensions.
058 * @return extension descriptors.
059 */
060 @NotNull
061 public static Collection<CallableDescriptor> getAllExtensions(@NotNull JetScope scope) {
062 Set<CallableDescriptor> result = Sets.newHashSet();
063
064 for (DeclarationDescriptor descriptor : scope.getAllDescriptors()) {
065 if (descriptor instanceof CallableDescriptor) {
066 CallableDescriptor callDescriptor = (CallableDescriptor) descriptor;
067 if (callDescriptor.getExtensionReceiverParameter() != null) {
068 result.add(callDescriptor);
069 }
070 }
071 }
072
073 return result;
074 }
075
076 public static JetScope makeScopeForPropertyAccessor(
077 @NotNull PropertyDescriptor propertyDescriptor,
078 @NotNull JetScope parentScope,
079 @NotNull BindingTrace trace
080 ) {
081 JetScope propertyDeclarationInnerScope =
082 getPropertyDeclarationInnerScope(propertyDescriptor, parentScope,
083 propertyDescriptor.getTypeParameters(),
084 propertyDescriptor.getExtensionReceiverParameter(), trace);
085 WritableScope accessorScope = new WritableScopeImpl(propertyDeclarationInnerScope, parentScope.getContainingDeclaration(),
086 new TraceBasedRedeclarationHandler(trace), "Accessor Scope");
087 accessorScope.changeLockLevel(WritableScope.LockLevel.READING);
088
089 return accessorScope;
090 }
091
092 public static JetScope getPropertyDeclarationInnerScope(
093 @NotNull PropertyDescriptor propertyDescriptor,
094 @NotNull JetScope outerScope,
095 @NotNull List<? extends TypeParameterDescriptor> typeParameters,
096 @Nullable ReceiverParameterDescriptor receiver,
097 BindingTrace trace
098 ) {
099 return getPropertyDeclarationInnerScope(propertyDescriptor, outerScope, typeParameters, receiver, trace, true);
100 }
101
102 public static JetScope getPropertyDeclarationInnerScopeForInitializer(
103 @NotNull JetScope outerScope,
104 @NotNull List<? extends TypeParameterDescriptor> typeParameters,
105 @Nullable ReceiverParameterDescriptor receiver,
106 BindingTrace trace
107 ) {
108 return getPropertyDeclarationInnerScope(null, outerScope, typeParameters, receiver, trace, false);
109 }
110
111 private static JetScope getPropertyDeclarationInnerScope(
112 @Nullable PropertyDescriptor propertyDescriptor,
113 // PropertyDescriptor can be null for property scope which hasn't label to property (in this case addLabelForProperty parameter must be false
114 @NotNull JetScope outerScope,
115 @NotNull List<? extends TypeParameterDescriptor> typeParameters,
116 @Nullable ReceiverParameterDescriptor receiver,
117 BindingTrace trace,
118 boolean addLabelForProperty
119 ) {
120 WritableScopeImpl result = new WritableScopeImpl(
121 outerScope, outerScope.getContainingDeclaration(), new TraceBasedRedeclarationHandler(trace),
122 "Property declaration inner scope");
123 if (addLabelForProperty) {
124 assert propertyDescriptor != null : "PropertyDescriptor can be null for property scope which hasn't label to property";
125 result.addLabeledDeclaration(propertyDescriptor);
126 }
127 for (TypeParameterDescriptor typeParameterDescriptor : typeParameters) {
128 result.addTypeParameterDescriptor(typeParameterDescriptor);
129 }
130 if (receiver != null) {
131 result.setImplicitReceiver(receiver);
132 }
133 result.changeLockLevel(WritableScope.LockLevel.READING);
134 return result;
135 }
136
137 @TestOnly
138 @NotNull
139 public static String printStructure(@Nullable JetScope scope) {
140 StringBuilder out = new StringBuilder();
141 Printer p = new Printer(out);
142 if (scope == null) {
143 p.println("null");
144 }
145 else {
146 scope.printScopeStructure(p);
147 }
148 return out.toString();
149 }
150 }