001    /*
002     * Copyright 2010-2015 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.load.kotlin;
018    
019    import kotlin.jvm.functions.Function0;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.kotlin.descriptors.ClassDescriptor;
023    import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor;
024    import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader;
025    import org.jetbrains.kotlin.name.Name;
026    import org.jetbrains.kotlin.resolve.scopes.ChainedScope;
027    import org.jetbrains.kotlin.resolve.scopes.MemberScope;
028    import org.jetbrains.kotlin.serialization.ClassData;
029    import org.jetbrains.kotlin.serialization.ClassDataWithSource;
030    import org.jetbrains.kotlin.serialization.PackageData;
031    import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents;
032    import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter;
033    import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope;
034    import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil;
035    
036    import javax.inject.Inject;
037    import java.util.*;
038    
039    import static kotlin.SetsKt.setOf;
040    import static org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader.Kind.*;
041    
042    public final class DeserializedDescriptorResolver {
043        private final ErrorReporter errorReporter;
044        private DeserializationComponents components;
045    
046        public static final Set<KotlinClassHeader.Kind> KOTLIN_CLASS = setOf(CLASS);
047        public static final Set<KotlinClassHeader.Kind> KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART = setOf(FILE_FACADE, MULTIFILE_CLASS_PART);
048    
049        public DeserializedDescriptorResolver(@NotNull ErrorReporter errorReporter) {
050            this.errorReporter = errorReporter;
051        }
052    
053        // component dependency cycle
054        @Inject
055        public void setComponents(@NotNull DeserializationComponentsForJava context) {
056            this.components = context.getComponents();
057        }
058    
059        @Nullable
060        public ClassDescriptor resolveClass(@NotNull KotlinJvmBinaryClass kotlinClass) {
061            String[] data = readData(kotlinClass, KOTLIN_CLASS);
062            if (data != null) {
063                String[] strings = kotlinClass.getClassHeader().getStrings();
064                assert strings != null : "String table not found in " + kotlinClass;
065                ClassData classData = JvmProtoBufUtil.readClassDataFrom(data, strings);
066                KotlinJvmBinarySourceElement sourceElement = new KotlinJvmBinarySourceElement(kotlinClass);
067                return components.getClassDeserializer().deserializeClass(
068                        kotlinClass.getClassId(),
069                        new ClassDataWithSource(classData, sourceElement)
070                );
071            }
072            return null;
073        }
074    
075        @Nullable
076        public MemberScope createKotlinPackagePartScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
077            String[] data = readData(kotlinClass, KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART);
078            if (data != null) {
079                String[] strings = kotlinClass.getClassHeader().getStrings();
080                assert strings != null : "String table not found in " + kotlinClass;
081                PackageData packageData = JvmProtoBufUtil.readPackageDataFrom(data, strings);
082                return new DeserializedPackageMemberScope(
083                        descriptor, packageData.getPackageProto(), packageData.getNameResolver(), components,
084                        new Function0<Collection<Name>>() {
085                            @Override
086                            public Collection<Name> invoke() {
087                                // All classes are included into Java scope
088                                return Collections.emptyList();
089                            }
090                        }
091                );
092            }
093            return null;
094        }
095    
096        @NotNull
097        public MemberScope createKotlinPackageScope(
098                @NotNull PackageFragmentDescriptor descriptor,
099                @NotNull List<KotlinJvmBinaryClass> packageParts
100        ) {
101            List<MemberScope> list = new ArrayList<MemberScope>(packageParts.size());
102            for (KotlinJvmBinaryClass callable : packageParts) {
103                MemberScope scope = createKotlinPackagePartScope(descriptor, callable);
104                if (scope != null) {
105                    list.add(scope);
106                }
107            }
108            if (list.isEmpty()) {
109                return MemberScope.Empty.INSTANCE;
110            }
111            return new ChainedScope("Member scope for union of package parts data", list.toArray(new MemberScope[list.size()]));
112        }
113    
114        @Nullable
115        public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull Set<KotlinClassHeader.Kind> expectedKinds) {
116            KotlinClassHeader header = kotlinClass.getClassHeader();
117            if (!header.isCompatibleAbiVersion()) {
118                errorReporter.reportIncompatibleAbiVersion(kotlinClass.getClassId(), kotlinClass.getLocation(), header.getVersion());
119            }
120            else if (expectedKinds.contains(header.getKind())) {
121                return header.getAnnotationData();
122            }
123    
124            return null;
125        }
126    }