001 /*
002 * Copyright 2010-2016 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.kotlin.resolve.scopes;
018
019 import kotlin.Unit;
020 import kotlin.jvm.functions.Function1;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
024 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
025 import org.jetbrains.kotlin.utils.Printer;
026
027 public final class ScopeUtils {
028 private ScopeUtils() {}
029
030 public static LexicalScope makeScopeForPropertyHeader(
031 @NotNull LexicalScope parent,
032 @NotNull final PropertyDescriptor propertyDescriptor
033 ) {
034 return new LexicalScopeImpl(parent, propertyDescriptor, false, null, LexicalScopeKind.PROPERTY_HEADER,
035 // redeclaration on type parameters should be reported early, see: DescriptorResolver.resolvePropertyDescriptor()
036 LocalRedeclarationChecker.DO_NOTHING.INSTANCE,
037 new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
038 @Override
039 public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
040 for (TypeParameterDescriptor typeParameterDescriptor : propertyDescriptor.getTypeParameters()) {
041 handler.addClassifierDescriptor(typeParameterDescriptor);
042 }
043 return Unit.INSTANCE;
044 }
045 });
046 }
047
048 @NotNull
049 public static LexicalScope makeScopeForPropertyInitializer(
050 @NotNull LexicalScope propertyHeader,
051 @NotNull PropertyDescriptor propertyDescriptor
052 ) {
053 return new LexicalScopeImpl(propertyHeader, propertyDescriptor, false, null, LexicalScopeKind.PROPERTY_INITIALIZER_OR_DELEGATE);
054 }
055
056 @NotNull
057 public static LexicalScope makeScopeForDelegateConventionFunctions(
058 @NotNull LexicalScope parent,
059 @NotNull PropertyDescriptor propertyDescriptor
060 ) {
061 // todo: very strange scope!
062 return new LexicalScopeImpl(parent, propertyDescriptor, true, propertyDescriptor.getExtensionReceiverParameter(),
063 LexicalScopeKind.PROPERTY_DELEGATE_METHOD
064 );
065 }
066
067 // TestOnly
068 @NotNull
069 public static String printStructure(@Nullable MemberScope scope) {
070 StringBuilder out = new StringBuilder();
071 Printer p = new Printer(out);
072 if (scope == null) {
073 p.println("null");
074 }
075 else {
076 scope.printScopeStructure(p);
077 }
078 return out.toString();
079 }
080 }