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