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 public static final Set<KotlinClassHeader.Kind> KOTLIN_PACKAGE_FACADE = setOf(PACKAGE_FACADE);
049
050 public DeserializedDescriptorResolver(@NotNull ErrorReporter errorReporter) {
051 this.errorReporter = errorReporter;
052 }
053
054 // component dependency cycle
055 @Inject
056 public void setComponents(@NotNull DeserializationComponentsForJava context) {
057 this.components = context.getComponents();
058 }
059
060 @Nullable
061 public ClassDescriptor resolveClass(@NotNull KotlinJvmBinaryClass kotlinClass) {
062 String[] data = readData(kotlinClass, KOTLIN_CLASS);
063 if (data != null) {
064 String[] strings = kotlinClass.getClassHeader().getStrings();
065 assert strings != null : "String table not found in " + kotlinClass;
066 ClassData classData = JvmProtoBufUtil.readClassDataFrom(data, strings);
067 KotlinJvmBinarySourceElement sourceElement = new KotlinJvmBinarySourceElement(kotlinClass);
068 return components.getClassDeserializer().deserializeClass(
069 kotlinClass.getClassId(),
070 new ClassDataWithSource(classData, sourceElement)
071 );
072 }
073 return null;
074 }
075
076 @Nullable
077 public MemberScope createKotlinPackagePartScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
078 String[] data = readData(kotlinClass, KOTLIN_FILE_FACADE_OR_MULTIFILE_CLASS_PART);
079 if (data != null) {
080 String[] strings = kotlinClass.getClassHeader().getStrings();
081 assert strings != null : "String table not found in " + kotlinClass;
082 PackageData packageData = JvmProtoBufUtil.readPackageDataFrom(data, strings);
083 return new DeserializedPackageMemberScope(
084 descriptor, packageData.getPackageProto(), packageData.getNameResolver(), components,
085 new Function0<Collection<Name>>() {
086 @Override
087 public Collection<Name> invoke() {
088 // All classes are included into Java scope
089 return Collections.emptyList();
090 }
091 }
092 );
093 }
094 return null;
095 }
096
097 @NotNull
098 public MemberScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull List<KotlinJvmBinaryClass> packageParts) {
099 List<MemberScope> list = new ArrayList<MemberScope>();
100 for (KotlinJvmBinaryClass callable : packageParts) {
101 MemberScope scope = createKotlinPackagePartScope(descriptor, callable);
102 if (scope != null) {
103 list.add(scope);
104 }
105 }
106 if (list.isEmpty()) {
107 return MemberScope.Empty.INSTANCE;
108 }
109 return new ChainedScope("Member scope for union of package parts data", list.toArray(new MemberScope[list.size()]));
110 }
111
112 @Nullable
113 public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull Set<KotlinClassHeader.Kind> expectedKinds) {
114 KotlinClassHeader header = kotlinClass.getClassHeader();
115 if (!header.isCompatibleAbiVersion()) {
116 errorReporter.reportIncompatibleAbiVersion(kotlinClass.getClassId(), kotlinClass.getLocation(), header.getVersion());
117 }
118 else if (expectedKinds.contains(header.getKind())) {
119 return header.getAnnotationData();
120 }
121
122 return null;
123 }
124 }