Serialize mappings via proto

This commit is contained in:
Michael Bogdanov
2015-09-01 17:13:50 +03:00
parent 672d25e7e0
commit f1c091e897
16 changed files with 2747 additions and 78 deletions
@@ -27,13 +27,16 @@ import org.jetbrains.annotations.TestOnly;
import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.load.kotlin.PackageParts;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
import org.jetbrains.kotlin.serialization.jvm.JvmPackageTable;
import org.jetbrains.org.objectweb.asm.Type;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
@@ -86,29 +89,43 @@ public class ClassFileFactory implements OutputFileCollection {
}
private void writeModuleMappings(Collection<PackageCodegen> values) {
final JvmPackageTable.PackageTable.Builder builder = JvmPackageTable.PackageTable.newBuilder();
String outputFilePath = getMappingFileName(state.getModuleName());
final StringWriter moduleMapping = new StringWriter(1024);
for (PackageCodegen codegen : values) {
codegen.getPackageParts().serialize(moduleMapping);
}
state.getProgress().reportOutput(Collections.<File>emptyList(), new File(outputFilePath));
//TODO: source files?
generators.put(outputFilePath, new OutAndSourceFileList(Collections.<File>emptyList()) {
@Override
public byte[] asBytes(ClassBuilderFactory factory) {
try {
return moduleMapping.toString().getBytes("UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
public String asText(ClassBuilderFactory factory) {
return moduleMapping.toString();
}
});
for (PackageCodegen codegen : values) {
PackageParts.Companion.serialize(codegen.getPackageParts(), builder);
}
if (builder.getPackagePartsCount() != 0) {
state.getProgress().reportOutput(Collections.<File>emptyList(), new File(outputFilePath));
//TODO: source files?
generators.put(outputFilePath, new OutAndSourceFileList(Collections.<File>emptyList()) {
@Override
public byte[] asBytes(ClassBuilderFactory factory) {
try {
ByteArrayOutputStream moduleMapping = new ByteArrayOutputStream(1024);
builder.build().writeTo(moduleMapping);
return moduleMapping.toByteArray();
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String asText(ClassBuilderFactory factory) {
try {
return new String(asBytes(factory), "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
});
}
}
@NotNull
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.codegen.context.MethodContext;
import org.jetbrains.kotlin.codegen.context.PackageContext;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.config.IncrementalCompilation;
import org.jetbrains.kotlin.load.kotlin.KotlinPackage;
import org.jetbrains.kotlin.load.kotlin.PackageParts;
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus;
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
@@ -120,7 +121,7 @@ public class PackageCodegen {
return v;
}
});
packageParts = new PackageParts(fqName.asString().replaceAll("\\.", "/"));
packageParts = new PackageParts(fqName.asString());
}
// Returns null if file has callables in several files
@@ -338,7 +339,8 @@ public class PackageCodegen {
if (!generatePackagePart || !state.getGenerateDeclaredClassFilter().shouldGeneratePackagePart(file)) return null;
packageParts.getParts().add(packagePartType.getInternalName());
String name = packagePartType.getInternalName();
packageParts.getParts().add(name.substring(name.lastIndexOf('/') + 1));
ClassBuilder builder = state.getFactory().newVisitor(PackagePart(file, packageFragment), packagePartType, file);
@@ -32,8 +32,8 @@ public class JvmPackagePartProvider(val env: KotlinCoreEnvironment) : PackagePar
}.filter { it?.findChild("META-INF") != null }.filterNotNull()
}
override fun findPackageParts(packageInternalName: String): List<String> {
val pathParts = packageInternalName.split('/')
override fun findPackageParts(packageFqName: String): List<String> {
val pathParts = packageFqName.split('.')
val mappings = roots.filter {
//filter all roots by package path existing
pathParts.fold(it) {
@@ -47,9 +47,9 @@ public class JvmPackagePartProvider(val env: KotlinCoreEnvironment) : PackagePar
}.filterNotNull().flatMap {
it.children.filter { it.name.endsWith(ModuleMapping.MAPPING_FILE_EXT) }.toList<VirtualFile>()
}.map {
ModuleMapping(String(it.contentsToByteArray(), "UTF-8"))
ModuleMapping(it.contentsToByteArray())
}
return mappings.map { it.findPackageParts(packageInternalName) }.filterNotNull().flatMap { it.parts }.distinct()
return mappings.map { it.findPackageParts(packageFqName) }.filterNotNull().flatMap { it.parts }.distinct()
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class KotlinModuleFileType implements FileType {
public static final String EXTENSION = "kotlin_module";
public static final KotlinModuleFileType INSTANCE = new KotlinModuleFileType();
private final NotNullLazyValue<Icon> myIcon = new NotNullLazyValue<Icon>() {
@NotNull
@Override
protected Icon compute() {
return IconLoader.getIcon("/org/jetbrains/kotlin/idea/icons/kotlin_file.png");
}
};
private KotlinModuleFileType() {}
@Override
@NotNull
public String getName() {
return EXTENSION;
}
@Override
@NotNull
public String getDescription() {
return getName();
}
@Override
@NotNull
public String getDefaultExtension() {
return EXTENSION;
}
@Override
public Icon getIcon() {
return myIcon.getValue();
}
@Override
public boolean isBinary() {
return true;
}
@Override
public boolean isReadOnly() {
return true;
}
@Nullable
@Override
public String getCharset(@NotNull VirtualFile file, @NotNull byte[] content) {
return null;
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.serialization.jvm;
option java_outer_classname = "JvmPackageTable";
option optimize_for = LITE_RUNTIME;
message PackageTable {
repeated PackageParts package_parts = 1;
}
message PackageParts {
required string package_fq_name = 1;
repeated string class_name = 2;
}
@@ -49,11 +49,11 @@ public class LazyJavaPackageScope(
= c.components.kotlinClassFinder.findKotlinClass(PackageClassUtils.getPackageClassId(packageFragment.fqName))
public val kotlinBinaryClasses: List<KotlinJvmBinaryClass> by lazy {
val files = c.components.packageMapper.findPackageParts(jPackage.getFqName().asString().replace('.', '/'))
val simpleNames = c.components.packageMapper.findPackageParts(jPackage.getFqName().asString())
val packageClassId = PackageClassUtils.getPackageClassId(packageFragment.fqName).packageFqName
files.map {
val classId = ClassId(packageClassId, Name.identifierNoValidate(it.substringAfterLast("/")))
simpleNames.map {
val classId = ClassId(packageClassId, Name.identifier(it))
c.components.kotlinClassFinder.findKotlinClass(classId)
}.filterNotNull()
}
@@ -16,23 +16,30 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.serialization.jvm.JvmPackageTable
import java.io.Writer
public class ModuleMapping(val moduleMapping: String) {
public class ModuleMapping(val proto: ByteArray? = null) {
val package2Parts = hashMapOf<String, PackageParts>()
val packageFqName2Parts = hashMapOf<String, PackageParts>()
init {
for (i in moduleMapping.split("\n")) {
if(i.isEmpty()) continue
val (pakage, facade) = i.split("->")
val miniFacades = package2Parts.getOrPut(pakage, { PackageParts(pakage) })
miniFacades.parts.add(facade)
if (proto != null) {
val parseFrom: JvmPackageTable.PackageTable? = JvmPackageTable.PackageTable.parseFrom(proto)
if (parseFrom != null) {
parseFrom.packagePartsList.map {
val packageParts = PackageParts(it.packageFqName)
packageFqName2Parts.put(it.packageFqName, packageParts)
it.classNameList.map {
packageParts.parts.add(it)
}
}
}
}
}
fun findPackageParts(internalPackageName: String): PackageParts? {
return package2Parts[internalPackageName]
fun findPackageParts(packageFqName: String): PackageParts? {
return packageFqName2Parts[packageFqName]
}
companion object {
@@ -40,22 +47,16 @@ public class ModuleMapping(val moduleMapping: String) {
}
}
public class PackageParts(val packageInternalName: String) {
public class PackageParts(val packageFqName: String) {
val parts = linkedSetOf<String>()
fun serialize(out: Writer) {
for (i in parts) {
out.write("$packageInternalName->$i\n")
}
}
override fun equals(other: Any?): Boolean {
if (other !is PackageParts) {
return false;
}
if (other.packageInternalName != packageInternalName) {
if (other.packageFqName != packageFqName) {
return false;
}
@@ -73,6 +74,18 @@ public class PackageParts(val packageInternalName: String) {
}
override fun hashCode(): Int {
return packageInternalName.hashCode() / 3 + parts.size() / 3 + (parts.firstOrNull()?.hashCode() ?: 0) / 3
return packageFqName.hashCode() / 3 + parts.size() / 3 + (parts.firstOrNull()?.hashCode() ?: 0) / 3
}
}
companion object {
@jvmStatic public fun PackageParts.serialize(builder : JvmPackageTable.PackageTable.Builder) {
if (this.parts.isNotEmpty()) {
val packageParts = JvmPackageTable.PackageParts.newBuilder()
packageParts.setPackageFqName(this.packageFqName)
packageParts.addAllClassName(this.parts.sorted())
builder.addPackageParts(packageParts)
}
}
}
}
@@ -29,20 +29,19 @@ class RuntimePackagePartProvider(val classLoader : ClassLoader) : PackagePartPro
if (moduleName == null) return
module2Mapping.putIfAbsent(moduleName, lazy {
val resourceAsStream: InputStream = classLoader.getResourceAsStream("META-INF/$moduleName.kotlin_module") ?: return@lazy ModuleMapping("")
val resourceAsStream: InputStream = classLoader.getResourceAsStream("META-INF/$moduleName.kotlin_module") ?: return@lazy ModuleMapping()
try {
val bytes = resourceAsStream.readBytes()
return@lazy ModuleMapping(String(bytes, "UTF-8"))
return@lazy ModuleMapping(resourceAsStream.readBytes())
}
catch (e: Exception) {
return@lazy ModuleMapping("")
return@lazy ModuleMapping()
}
})
}
override fun findPackageParts(packageInternalName: String): List<String> {
return module2Mapping.values().map { it.value.findPackageParts(packageInternalName) }.filterNotNull().flatMap { it.parts }.distinct()
override fun findPackageParts(packageFqName: String): List<String> {
return module2Mapping.values().map { it.value.findPackageParts(packageFqName) }.filterNotNull().flatMap { it.parts }.distinct()
}
}
@@ -18,11 +18,14 @@ package org.jetbrains.kotlin.descriptors
interface PackagePartProvider {
fun findPackageParts(packageInternalName: String): List<String>
/**
* return packagepart simple names
*/
fun findPackageParts(packageFqName: String): List<String>
companion object {
val EMPTY = object : PackagePartProvider {
override fun findPackageParts(packageInternalName: String): List<String> {
override fun findPackageParts(packageFqName: String): List<String> {
return emptyList()
}
}
@@ -50,7 +50,8 @@ public val PROTO_PATHS: List<ProtoPath> = listOf(
ProtoPath("core/deserialization/src/descriptors.proto"),
ProtoPath("core/deserialization/src/builtins.proto"),
ProtoPath("js/js.serializer/src/js.proto"),
ProtoPath("core/descriptor.loader.java/src/jvm_descriptors.proto")
ProtoPath("core/descriptor.loader.java/src/jvm_descriptors.proto"),
ProtoPath("core/descriptor.loader.java/src/jvm_package_table.proto")
)
private val EXT_OPTIONS_PROTO_PATH = ProtoPath("core/deserialization/src/ext_options.proto")
@@ -24,8 +24,8 @@ import org.jetbrains.kotlin.load.kotlin.PackageParts
public class IDEPackagePartProvider(val scope: GlobalSearchScope) : PackagePartProvider {
override fun findPackageParts(packageInternalName: String): List<String> {
val values: MutableList<PackageParts> = FileBasedIndex.getInstance().getValues(KotlinModuleMappingIndex.KEY, packageInternalName, scope)
override fun findPackageParts(packageFqName: String): List<String> {
val values: MutableList<PackageParts> = FileBasedIndex.getInstance().getValues(KotlinModuleMappingIndex.KEY, packageFqName, scope)
return values.flatMap { it.parts }.distinct()
}
}
@@ -25,18 +25,6 @@ import org.jetbrains.kotlin.load.kotlin.PackageParts
import java.io.DataInput
import java.io.DataOutput
class PackageData(val data: List<String>) {
override fun equals(other: Any?): Boolean {
return super.equals(other)
}
override fun hashCode(): Int {
return data.size() + (data.firstOrNull()?.hashCode() ?: 0)
}
}
public object KotlinModuleMappingIndex : FileBasedIndexExtension<String, PackageParts>() {
private val classOfIndex = javaClass<KotlinModuleMappingIndex>().getCanonicalName()
@@ -55,8 +43,8 @@ public object KotlinModuleMappingIndex : FileBasedIndexExtension<String, Package
private val VALUE_EXTERNALIZER = object : DataExternalizer<PackageParts> {
override fun read(`in`: DataInput): PackageParts? {
val internalName = `in`.readUTF()
val facades = PackageParts(internalName)
val packageFqName = `in`.readUTF()
val facades = PackageParts(packageFqName)
val size = `in`.readInt()
(1..size).forEach {
facades.parts.add(`in`.readUTF())
@@ -66,7 +54,7 @@ public object KotlinModuleMappingIndex : FileBasedIndexExtension<String, Package
}
override fun save(out: DataOutput, value: PackageParts?) {
out.writeUTF(value!!.packageInternalName)
out.writeUTF(value!!.packageFqName)
out.writeInt(value.parts.size())
value.parts.forEach { out.writeUTF(it) }
}
@@ -91,9 +79,9 @@ public object KotlinModuleMappingIndex : FileBasedIndexExtension<String, Package
override fun getIndexer(): DataIndexer<String, PackageParts, FileContent> {
return object : DataIndexer<String, PackageParts, FileContent> {
override fun map(inputData: FileContent): MutableMap<String, PackageParts> {
val content = String(inputData.getContent())
val content = inputData.getContent()
val moduleMapping = ModuleMapping(content)
return moduleMapping.package2Parts
return moduleMapping.packageFqName2Parts
}
}
}
+1
View File
@@ -247,6 +247,7 @@
<fileTypeFactory implementation="org.jetbrains.kotlin.idea.JetFileFactory"/>
<fileTypeFactory implementation="org.jetbrains.kotlin.idea.KotlinJavaScriptMetaFileTypeFactory"/>
<fileTypeFactory implementation="org.jetbrains.kotlin.idea.KotlinModuleFileFactory"/>
<compileServer.plugin classpath="jps/kotlin-jps-plugin.jar;kotlin-runtime.jar;kotlin-reflect.jar;kotlin-plugin.jar"/>
<compiler.task execute="BEFORE" implementation="org.jetbrains.kotlin.idea.internal.makeBackup.MakeBackupCompileTask"/>
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea;
import com.intellij.openapi.fileTypes.FileTypeConsumer;
import com.intellij.openapi.fileTypes.FileTypeFactory;
import org.jetbrains.annotations.NotNull;
public class KotlinModuleFileFactory extends FileTypeFactory {
@Override
public void createFileTypes(@NotNull FileTypeConsumer consumer) {
consumer.consume(KotlinModuleFileType.INSTANCE, "kotlin_module");
}
}