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 017package org.jetbrains.jet.lang.resolve.scopes; 018 019import com.google.common.base.Function; 020import com.google.common.collect.Collections2; 021import com.google.common.collect.Lists; 022import com.google.common.collect.Sets; 023import org.jetbrains.annotations.NotNull; 024import org.jetbrains.jet.lang.descriptors.CallableDescriptor; 025import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; 026import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; 027import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor; 028import org.jetbrains.jet.lang.resolve.BindingTrace; 029import org.jetbrains.jet.lang.resolve.DescriptorResolver; 030import org.jetbrains.jet.lang.resolve.TraceBasedRedeclarationHandler; 031import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue; 032 033import java.util.Collection; 034import java.util.List; 035import java.util.Set; 036 037public final class JetScopeUtils { 038 private JetScopeUtils() {} 039 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 public static Collection<CallableDescriptor> getAllExtensions(@NotNull JetScope scope) { 061 Set<CallableDescriptor> result = Sets.newHashSet(); 062 063 for (DeclarationDescriptor descriptor : scope.getAllDescriptors()) { 064 if (descriptor instanceof CallableDescriptor) { 065 CallableDescriptor callDescriptor = (CallableDescriptor) descriptor; 066 if (callDescriptor.getReceiverParameter() != null) { 067 result.add(callDescriptor); 068 } 069 } 070 } 071 072 return result; 073 } 074 075 public static JetScope makeScopeForPropertyAccessor( 076 @NotNull PropertyDescriptor propertyDescriptor, 077 @NotNull JetScope parentScope, 078 @NotNull DescriptorResolver descriptorResolver, 079 @NotNull BindingTrace trace 080 ) { 081 JetScope propertyDeclarationInnerScope = descriptorResolver 082 .getPropertyDeclarationInnerScope(propertyDescriptor, parentScope, 083 propertyDescriptor.getTypeParameters(), 084 propertyDescriptor.getReceiverParameter(), 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}