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