Drop package facades:

- getting rid of package facades in InlineCodegen & related stuff.
Provide binary source element to top-level package members.
This commit is contained in:
Dmitry Petrov
2015-10-16 21:52:11 +03:00
parent 989f761166
commit 70c91b2e75
10 changed files with 127 additions and 71 deletions
@@ -769,9 +769,9 @@ public class InlineCodegen extends CallGenerator {
if (incrementalCompilationComponents == null || targetId == null) return;
IncrementalCache incrementalCache = incrementalCompilationComponents.getIncrementalCache(targetId);
String sourceFile = InlineCodegenUtilsKt.getClassFilePath(sourceDescriptor, incrementalCache);
String targetFile = InlineCodegenUtilsKt.getSourceFilePath(targetDescriptor);
incrementalCache.registerInline(sourceFile, jvmSignature.toString(), targetFile);
String classFilePath = InlineCodegenUtilsKt.getClassFilePath(sourceDescriptor, typeMapper, incrementalCache);
String sourceFilePath = InlineCodegenUtilsKt.getSourceFilePath(targetDescriptor);
incrementalCache.registerInline(classFilePath, jvmSignature.toString(), sourceFilePath);
}
@Override
@@ -164,26 +164,6 @@ public class InlineCodegenUtil {
return fileFinder.findVirtualFileWithHeader(new ClassId(packageFqName, Name.identifier(classNameWithDollars)));
}
//TODO: navigate to inner classes
@Nullable
public static ClassId getContainerClassId(@NotNull DeclarationDescriptor referencedDescriptor) {
ClassOrPackageFragmentDescriptor
containerDescriptor = DescriptorUtils.getParentOfType(referencedDescriptor, ClassOrPackageFragmentDescriptor.class, false);
if (containerDescriptor instanceof PackageFragmentDescriptor) {
return PackageClassUtils.getPackageClassId(getFqName(containerDescriptor).toSafe());
}
if (containerDescriptor instanceof ClassDescriptor) {
ClassId classId = DescriptorUtilsKt.getClassId((ClassDescriptor) containerDescriptor);
if (isInterface(containerDescriptor)) {
FqName relativeClassName = classId.getRelativeClassName();
//TODO test nested trait fun inlining
classId = new ClassId(classId.getPackageFqName(), Name.identifier(relativeClassName.shortName().asString() + JvmAbi.DEFAULT_IMPLS_SUFFIX));
}
return classId;
}
return null;
}
public static String getInlineName(
@NotNull CodegenContext codegenContext,
@NotNull JetTypeMapper typeMapper,
@@ -16,13 +16,15 @@
package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.kotlin.codegen.state.JetTypeMapper
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithSource
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryPackageSourceElement
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClass
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedSimpleFunctionDescriptor
public val FunctionDescriptor.sourceFilePath: String
@@ -32,19 +34,30 @@ public val FunctionDescriptor.sourceFilePath: String
return containingFile?.virtualFile?.canonicalPath!!
}
public fun FunctionDescriptor.getClassFilePath(cache: IncrementalCache): String {
public fun FunctionDescriptor.getClassFilePath(typeMapper: JetTypeMapper, cache: IncrementalCache): String {
val container = containingDeclaration as? DeclarationDescriptorWithSource
val source = container?.source
return when (source) {
is KotlinJvmBinaryPackageSourceElement -> {
if (this !is DeserializedCallableMemberDescriptor) {
throw AssertionError("Expected DeserializedCallableMemberDescriptor, got: $this")
}
val kotlinClass = source.getContainingBinaryClass(this) ?:
throw AssertionError("Descriptor $this is not found, in: $source")
if (kotlinClass !is VirtualFileKotlinClass) {
throw AssertionError("Expected VirtualFileKotlinClass, got $kotlinClass")
}
kotlinClass.file.canonicalPath!!
}
is KotlinJvmBinarySourceElement -> {
assert(this is DeserializedSimpleFunctionDescriptor) { "Expected DeserializedSimpleFunctionDescriptor, got: $this" }
val kotlinClass = source.binaryClass as VirtualFileKotlinClass
kotlinClass.file.canonicalPath!!
}
else -> {
val classId = InlineCodegenUtil.getContainerClassId(this)!!
val className = JvmClassName.byClassId(classId).internalName
val implementationOwnerType = typeMapper.mapOwner(this)
val className = implementationOwnerType.internalName
cache.getClassFilePath(className)
}
}
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.codegen.*;
import org.jetbrains.kotlin.codegen.binding.CodegenBinding;
import org.jetbrains.kotlin.codegen.binding.MutableClosure;
import org.jetbrains.kotlin.codegen.binding.PsiCodegenPredictor;
import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil;
import org.jetbrains.kotlin.codegen.signature.BothSignatureWriter;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.fileClasses.FileClasses;
@@ -43,6 +42,7 @@ import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature.
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor;
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor;
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope;
import org.jetbrains.kotlin.load.kotlin.PackageClassUtils;
import org.jetbrains.kotlin.load.kotlin.incremental.IncrementalPackageFragmentProvider;
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache;
import org.jetbrains.kotlin.name.*;
@@ -241,8 +241,8 @@ public class JetTypeMapper {
if (parentDeclaration instanceof PackageFragmentDescriptor) {
containingClassesInfo = getPackageMemberContainingClassesInfo(deserializedDescriptor);
} else {
containingClassesInfo = ContainingClassesInfo.forClassMemberOrNull(
InlineCodegenUtil.getContainerClassId(deserializedDescriptor));
ClassId classId = getContainerClassIdForClassDescriptor((ClassDescriptor) parentDeclaration);
containingClassesInfo = ContainingClassesInfo.forClassMemberOrNull(classId);
}
if (containingClassesInfo == null) {
throw new IllegalStateException("Couldn't find container for " + deserializedDescriptor.getName());
@@ -250,6 +250,16 @@ public class JetTypeMapper {
return containingClassesInfo;
}
private static ClassId getContainerClassIdForClassDescriptor(ClassDescriptor classDescriptor) {
ClassId classId = DescriptorUtilsKt.getClassId(classDescriptor);
if (isInterface(classDescriptor)) {
FqName relativeClassName = classId.getRelativeClassName();
//TODO test nested trait fun inlining
classId = new ClassId(classId.getPackageFqName(), Name.identifier(relativeClassName.shortName().asString() + JvmAbi.DEFAULT_IMPLS_SUFFIX));
}
return classId;
}
@Nullable
private String getPackageMemberOwnerInternalName(@NotNull DeserializedCallableMemberDescriptor descriptor) {
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();