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