Use type table in DescriptorSerializer, add switch to enable/disable, test

This commit is contained in:
Alexander Udalov
2015-10-09 03:59:27 +03:00
parent 82d2e623d3
commit fb5d8de84b
25 changed files with 3360 additions and 53 deletions
@@ -222,7 +222,9 @@ public class ClosureCodegen extends MemberCodegen<JetElement> {
writeKotlinSyntheticClassAnnotation(v, state);
DescriptorSerializer serializer =
DescriptorSerializer.createTopLevel(new JvmSerializerExtension(v.getSerializationBindings(), typeMapper));
DescriptorSerializer.createForLambda(
new JvmSerializerExtension(v.getSerializationBindings(), typeMapper, state.getUseTypeTableInSerializer())
);
ProtoBuf.Function functionProto = serializer.functionProto(funDescriptor).build();
@@ -256,7 +256,9 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
}
DescriptorSerializer serializer =
DescriptorSerializer.create(descriptor, new JvmSerializerExtension(v.getSerializationBindings(), typeMapper));
DescriptorSerializer.create(descriptor, new JvmSerializerExtension(
v.getSerializationBindings(), typeMapper, state.getUseTypeTableInSerializer()
));
ProtoBuf.Class classProto = serializer.classProto(descriptor).build();
@@ -85,7 +85,9 @@ public class MultifileClassPartCodegen(
val bindings = v.serializationBindings
val serializer = DescriptorSerializer.createTopLevel(JvmSerializerExtension(bindings, state.typeMapper))
val serializer = DescriptorSerializer.createTopLevel(
JvmSerializerExtension(bindings, state.typeMapper, state.useTypeTableInSerializer)
)
val packageProto = serializer.packagePartProto(members).build()
val av = v.newAnnotation(AsmUtil.asmDescByFqNameWithoutInnerClasses(JvmAnnotationNames.KOTLIN_MULTIFILE_CLASS_PART), true)
@@ -270,7 +270,9 @@ public class PackageCodegen {
if (file.isScript()) return;
}
DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(new JvmSerializerExtension(bindings, state.getTypeMapper()));
DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(new JvmSerializerExtension(
bindings, state.getTypeMapper(), state.getUseTypeTableInSerializer()
));
Collection<PackageFragmentDescriptor> packageFragments = Lists.newArrayList();
ContainerUtil.addIfNotNull(packageFragments, packageFragment);
ContainerUtil.addIfNotNull(packageFragments, compiledPackageFragment);
@@ -121,7 +121,9 @@ public class PackagePartCodegen extends MemberCodegen<JetFile> {
JvmSerializationBindings bindings = v.getSerializationBindings();
DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(new JvmSerializerExtension(bindings, state.getTypeMapper()));
DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(new JvmSerializerExtension(
bindings, state.getTypeMapper(), state.getUseTypeTableInSerializer()
));
ProtoBuf.Package packageProto = serializer.packagePartProto(members).build();
AnnotationVisitor av = v.newAnnotation(asmDescByFqNameWithoutInnerClasses(JvmAnnotationNames.KOTLIN_FILE_FACADE), true);
@@ -42,11 +42,13 @@ public class JvmSerializerExtension extends SerializerExtension {
private final JvmSerializationBindings bindings;
private final StringTable stringTable;
private final AnnotationSerializer annotationSerializer;
private final boolean useTypeTable;
public JvmSerializerExtension(@NotNull JvmSerializationBindings bindings, @NotNull JetTypeMapper typeMapper) {
public JvmSerializerExtension(@NotNull JvmSerializationBindings bindings, @NotNull JetTypeMapper typeMapper, boolean useTypeTable) {
this.bindings = bindings;
this.stringTable = new JvmStringTable(typeMapper);
this.annotationSerializer = new AnnotationSerializer(stringTable);
this.useTypeTable = useTypeTable;
}
@NotNull
@@ -55,6 +57,11 @@ public class JvmSerializerExtension extends SerializerExtension {
return stringTable;
}
@Override
public boolean shouldUseTypeTable() {
return useTypeTable;
}
@Override
public void serializeClass(@NotNull ClassDescriptor descriptor, @NotNull ProtoBuf.Class.Builder proto) {
AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(KotlinBuiltIns.FQ_NAMES.annotation);
@@ -52,6 +52,7 @@ public class GenerationState @JvmOverloads constructor(
public val generateDeclaredClassFilter: GenerationState.GenerateClassFilter = GenerationState.GenerateClassFilter.GENERATE_ALL,
disableInline: Boolean = false,
disableOptimization: Boolean = false,
public val useTypeTableInSerializer: Boolean = false,
public val packageFacadesAsMultifileClasses: Boolean = false,
public val diagnostics: DiagnosticSink = DiagnosticSink.DO_NOTHING,
public val packagesWithObsoleteParts: Collection<FqName> = emptySet(),
@@ -105,7 +105,7 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
System.err.println("Could not make directories: " + destDir)
}
files.map { it.getPackageFqName() }.toSet().forEach {
files.map { it.packageFqName }.toSet().forEach {
fqName ->
serializePackage(moduleDescriptor, fqName, destDir)
}
@@ -117,11 +117,10 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
// TODO: perform some kind of validation? At the moment not possible because DescriptorValidator is in compiler-tests
// DescriptorValidator.validate(packageView)
val serializer = DescriptorSerializer.createTopLevel(BuiltInsSerializerExtension())
val classifierDescriptors = DescriptorSerializer.sort(packageView.memberScope.getDescriptors(DescriptorKindFilter.CLASSIFIERS))
serializeClasses(classifierDescriptors, serializer) {
val extension = BuiltInsSerializerExtension()
serializeClasses(classifierDescriptors, extension) {
classDescriptor, classProto ->
val stream = ByteArrayOutputStream()
classProto.writeTo(stream)
@@ -130,13 +129,13 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
val packageStream = ByteArrayOutputStream()
val fragments = packageView.fragments
val packageProto = serializer.packageProto(fragments).build() ?: error("Package fragments not serialized: $fragments")
val packageProto = DescriptorSerializer.createTopLevel(extension).packageProto(fragments).build()
packageProto.writeTo(packageStream)
write(destDir, BuiltInsSerializedResourcePaths.getPackageFilePath(fqName), packageStream,
BuiltInsSerializedResourcePaths.fallbackPaths.getPackageFilePath(fqName))
val nameStream = ByteArrayOutputStream()
serializer.stringTable.serializeTo(nameStream)
extension.stringTable.serializeTo(nameStream)
write(destDir, BuiltInsSerializedResourcePaths.getStringTableFilePath(fqName), nameStream,
BuiltInsSerializedResourcePaths.fallbackPaths.getStringTableFilePath(fqName))
}
@@ -144,7 +143,7 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
private fun write(destDir: File, fileName: String, stream: ByteArrayOutputStream, legacyFileName: String? = null) {
totalSize += stream.size()
totalFiles++
File(destDir, fileName).getParentFile().mkdirs()
File(destDir, fileName).parentFile.mkdirs()
File(destDir, fileName).writeBytes(stream.toByteArray())
legacyFileName?.let { fileName ->
@@ -154,18 +153,18 @@ public class BuiltInsSerializer(private val dependOnOldBuiltIns: Boolean) {
private fun serializeClass(
classDescriptor: ClassDescriptor,
serializer: DescriptorSerializer,
serializer: BuiltInsSerializerExtension,
writeClass: (ClassDescriptor, ProtoBuf.Class) -> Unit
) {
val classProto = serializer.classProto(classDescriptor).build() ?: error("Class not serialized: $classDescriptor")
val classProto = DescriptorSerializer.createTopLevel(serializer).classProto(classDescriptor).build()
writeClass(classDescriptor, classProto)
serializeClasses(classDescriptor.getUnsubstitutedInnerClassesScope().getDescriptors(), serializer, writeClass)
serializeClasses(classDescriptor.unsubstitutedInnerClassesScope.getDescriptors(), serializer, writeClass)
}
private fun serializeClasses(
descriptors: Collection<DeclarationDescriptor>,
serializer: DescriptorSerializer,
serializer: BuiltInsSerializerExtension,
writeClass: (ClassDescriptor, ProtoBuf.Class) -> Unit
) {
for (descriptor in descriptors) {
@@ -28,6 +28,8 @@ public class BuiltInsSerializerExtension : SerializerExtension() {
override fun getStringTable(): StringTable = stringTable
override fun shouldUseTypeTable(): Boolean = true
override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder) {
for (annotation in descriptor.annotations) {
proto.addExtension(BuiltInsProtoBuf.classAnnotation, annotationSerializer.serializeAnnotation(annotation))
@@ -398,6 +398,7 @@ public class KotlinToJVMBytecodeCompiler {
GenerationState.GenerateClassFilter.GENERATE_ALL,
configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false),
configuration.get(JVMConfigurationKeys.DISABLE_OPTIMIZATION, false),
/* useTypeTableInSerializer = */ false,
configuration.get(JVMConfigurationKeys.PACKAGE_FACADES_AS_MULTIFILE_CLASSES, false),
diagnosticHolder,
packagesWithObsoleteParts,
@@ -429,6 +429,7 @@ public class KotlinJavaFileStubProvider<T extends WithFileStubAndExtraDiagnostic
/*generateClassFilter=*/stubGenerationStrategy.getGenerateClassFilter(),
/*disableInline=*/false,
/*disableOptimization=*/false,
/*useTypeTableInSerializer=*/false,
/*packageFacadesAsMultifileClasses=*/false,
forExtraDiagnostics
);
@@ -39,13 +39,21 @@ import java.util.*;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
public class DescriptorSerializer {
private final Interner<TypeParameterDescriptor> typeParameters;
private final SerializerExtension extension;
private final MutableTypeTable typeTable;
private final boolean serializeTypeTableToFunction;
private DescriptorSerializer(Interner<TypeParameterDescriptor> typeParameters, SerializerExtension extension) {
private DescriptorSerializer(
@NotNull Interner<TypeParameterDescriptor> typeParameters,
@NotNull SerializerExtension extension,
@NotNull MutableTypeTable typeTable,
boolean serializeTypeTableToFunction
) {
this.typeParameters = typeParameters;
this.extension = extension;
this.typeTable = typeTable;
this.serializeTypeTableToFunction = serializeTypeTableToFunction;
}
@NotNull
@@ -63,7 +71,12 @@ public class DescriptorSerializer {
@NotNull
public static DescriptorSerializer createTopLevel(@NotNull SerializerExtension extension) {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(), extension);
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), false);
}
@NotNull
public static DescriptorSerializer createForLambda(@NotNull SerializerExtension extension) {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), true);
}
@NotNull
@@ -77,7 +90,12 @@ public class DescriptorSerializer {
// Calculate type parameter ids for the outer class beforehand, as it would've had happened if we were always
// serializing outer classes before nested classes.
// Otherwise our interner can get wrong ids because we may serialize classes in any order.
DescriptorSerializer serializer = parentSerializer.createChildSerializer();
DescriptorSerializer serializer = new DescriptorSerializer(
new Interner<TypeParameterDescriptor>(parentSerializer.typeParameters),
parentSerializer.extension,
new MutableTypeTable(),
false
);
for (TypeParameterDescriptor typeParameter : descriptor.getTypeConstructor().getParameters()) {
serializer.typeParameters.intern(typeParameter);
}
@@ -86,7 +104,7 @@ public class DescriptorSerializer {
@NotNull
private DescriptorSerializer createChildSerializer() {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(typeParameters), extension);
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(typeParameters), extension, typeTable, false);
}
@NotNull
@@ -94,6 +112,10 @@ public class DescriptorSerializer {
return extension.getStringTable();
}
private boolean useTypeTable() {
return extension.shouldUseTypeTable();
}
@NotNull
public ProtoBuf.Class.Builder classProto(@NotNull ClassDescriptor classDescriptor) {
ProtoBuf.Class.Builder builder = ProtoBuf.Class.newBuilder();
@@ -113,7 +135,12 @@ public class DescriptorSerializer {
if (!KotlinBuiltIns.isSpecialClassWithNoSupertypes(classDescriptor)) {
// Special classes (Any, Nothing) have no supertypes
for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) {
builder.addSupertype(type(supertype));
if (useTypeTable()) {
builder.addSupertypeId(typeId(supertype));
}
else {
builder.addSupertype(type(supertype));
}
}
}
@@ -150,6 +177,11 @@ public class DescriptorSerializer {
builder.setCompanionObjectName(getSimpleNameIndex(companionObjectDescriptor.getName()));
}
ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
if (typeTableProto != null) {
builder.setTypeTable(typeTableProto);
}
extension.serializeClass(classDescriptor, builder);
return builder;
@@ -212,7 +244,12 @@ public class DescriptorSerializer {
builder.setName(getSimpleNameIndex(descriptor.getName()));
builder.setReturnType(local.type(descriptor.getType()));
if (useTypeTable()) {
builder.setReturnTypeId(local.typeId(descriptor.getType()));
}
else {
builder.setReturnType(local.type(descriptor.getType()));
}
for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) {
builder.addTypeParameter(local.typeParameter(typeParameterDescriptor));
@@ -220,7 +257,12 @@ public class DescriptorSerializer {
ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
if (receiverParameter != null) {
builder.setReceiverType(local.type(receiverParameter.getType()));
if (useTypeTable()) {
builder.setReceiverTypeId(local.typeId(receiverParameter.getType()));
}
else {
builder.setReceiverType(local.type(receiverParameter.getType()));
}
}
extension.serializeProperty(descriptor, builder);
@@ -244,8 +286,14 @@ public class DescriptorSerializer {
builder.setName(getSimpleNameIndex(descriptor.getName()));
//noinspection ConstantConditions
builder.setReturnType(local.type(descriptor.getReturnType()));
if (useTypeTable()) {
//noinspection ConstantConditions
builder.setReturnTypeId(local.typeId(descriptor.getReturnType()));
}
else {
//noinspection ConstantConditions
builder.setReturnType(local.type(descriptor.getReturnType()));
}
for (TypeParameterDescriptor typeParameterDescriptor : descriptor.getTypeParameters()) {
builder.addTypeParameter(local.typeParameter(typeParameterDescriptor));
@@ -253,13 +301,25 @@ public class DescriptorSerializer {
ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
if (receiverParameter != null) {
builder.setReceiverType(local.type(receiverParameter.getType()));
if (useTypeTable()) {
builder.setReceiverTypeId(local.typeId(receiverParameter.getType()));
}
else {
builder.setReceiverType(local.type(receiverParameter.getType()));
}
}
for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
builder.addValueParameter(local.valueParameter(valueParameterDescriptor));
}
if (serializeTypeTableToFunction) {
ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
if (typeTableProto != null) {
builder.setTypeTable(typeTableProto);
}
}
extension.serializeFunction(descriptor, builder);
return builder;
@@ -305,11 +365,21 @@ public class DescriptorSerializer {
builder.setName(getSimpleNameIndex(descriptor.getName()));
builder.setType(type(descriptor.getType()));
if (useTypeTable()) {
builder.setTypeId(typeId(descriptor.getType()));
}
else {
builder.setType(type(descriptor.getType()));
}
JetType varargElementType = descriptor.getVarargElementType();
if (varargElementType != null) {
builder.setVarargElementType(type(varargElementType));
if (useTypeTable()) {
builder.setVarargElementTypeId(typeId(varargElementType));
}
else {
builder.setVarargElementType(type(varargElementType));
}
}
extension.serializeValueParameter(descriptor, builder);
@@ -337,7 +407,12 @@ public class DescriptorSerializer {
if (upperBounds.size() == 1 && KotlinBuiltIns.isDefaultBound(CollectionsKt.single(upperBounds))) return builder;
for (JetType upperBound : upperBounds) {
builder.addUpperBound(type(upperBound));
if (useTypeTable()) {
builder.addUpperBoundId(typeId(upperBound));
}
else {
builder.addUpperBound(type(upperBound));
}
}
return builder;
@@ -355,16 +430,26 @@ public class DescriptorSerializer {
throw new IllegalStateException("Unknown variance: " + variance);
}
private int typeId(@NotNull JetType type) {
return typeTable.get(type(type));
}
@NotNull
public ProtoBuf.Type.Builder type(@NotNull JetType type) {
private ProtoBuf.Type.Builder type(@NotNull JetType type) {
assert !type.isError() : "Can't serialize error types: " + type; // TODO
if (FlexibleTypesKt.isFlexible(type)) {
Flexibility flexibility = FlexibleTypesKt.flexibility(type);
return type(flexibility.getLowerBound())
.setFlexibleTypeCapabilitiesId(getStringTable().getStringIndex(flexibility.getExtraCapabilities().getId()))
.setFlexibleUpperBound(type(flexibility.getUpperBound()));
ProtoBuf.Type.Builder lowerBound = type(flexibility.getLowerBound());
lowerBound.setFlexibleTypeCapabilitiesId(getStringTable().getStringIndex(flexibility.getExtraCapabilities().getId()));
if (useTypeTable()) {
lowerBound.setFlexibleUpperBoundId(typeId(flexibility.getUpperBound()));
}
else {
lowerBound.setFlexibleUpperBound(type(flexibility.getUpperBound()));
}
return lowerBound;
}
ProtoBuf.Type.Builder builder = ProtoBuf.Type.newBuilder();
@@ -403,7 +488,13 @@ public class DescriptorSerializer {
if (projection != builder.getProjection()) {
builder.setProjection(projection);
}
builder.setType(type(typeProjection.getType()));
if (useTypeTable()) {
builder.setTypeId(typeId(typeProjection.getType()));
}
else {
builder.setType(type(typeProjection.getType()));
}
}
return builder;
@@ -443,6 +534,11 @@ public class DescriptorSerializer {
}
}
ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
if (typeTableProto != null) {
builder.setTypeTable(typeTableProto);
}
extension.serializePackage(fragments, builder);
return builder;
@@ -461,6 +557,11 @@ public class DescriptorSerializer {
}
}
ProtoBuf.TypeTable typeTableProto = typeTable.serialize();
if (typeTableProto != null) {
builder.setTypeTable(typeTableProto);
}
return builder;
}
@@ -0,0 +1,45 @@
/*
* 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
import org.jetbrains.kotlin.utils.Interner
import java.util.*
class MutableTypeTable {
class TypeWrapper(val type: ProtoBuf.Type.Builder) {
// If you'll try to optimize it using structured equals/hashCode, pay attention to extensions present in Type messages
private val bytes: ByteArray = type.build().toByteArray()
private val hashCode: Int = Arrays.hashCode(bytes)
override fun hashCode() = hashCode
override fun equals(other: Any?) = other is TypeWrapper && Arrays.equals(bytes, other.bytes)
}
val interner = Interner<TypeWrapper>()
operator fun get(type: ProtoBuf.Type.Builder): Int =
interner.intern(TypeWrapper(type))
fun serialize(): ProtoBuf.TypeTable? =
if (interner.isEmpty) null
else ProtoBuf.TypeTable.newBuilder().apply {
for (type in interner.allInternedObjects) {
addType(type.type)
}
}.build()
}
@@ -26,6 +26,10 @@ public abstract class SerializerExtension {
@NotNull
public abstract StringTable getStringTable();
public boolean shouldUseTypeTable() {
return false;
}
public void serializeClass(@NotNull ClassDescriptor descriptor, @NotNull ProtoBuf.Class.Builder proto) {
}
@@ -22,6 +22,7 @@ import kotlin.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.cli.jvm.config.JVMConfigurationKeys;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
@@ -29,7 +30,6 @@ import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.config.CompilerConfiguration;
import org.jetbrains.kotlin.resolve.AnalyzingUtils;
import org.jetbrains.kotlin.resolve.BindingTraceContext;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.test.JetTestUtils;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
@@ -67,6 +67,7 @@ public class CodegenTestUtil {
GenerationState.GenerateClassFilter.GENERATE_ALL,
configuration.get(JVMConfigurationKeys.DISABLE_INLINE, false),
configuration.get(JVMConfigurationKeys.DISABLE_OPTIMIZATION, false),
/* useTypeTableInSerializer = */ false,
configuration.get(JVMConfigurationKeys.PACKAGE_FACADES_AS_MULTIFILE_CLASSES, false),
forExtraDiagnostics
);
@@ -49,7 +49,7 @@ public class GenerationUtils {
) {
AnalysisResult analysisResult =
JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile, new JvmPackagePartProvider(environment));
return compileFilesGetGenerationState(psiFile.getProject(), analysisResult, Collections.singletonList(psiFile));
return compileFilesGetGenerationState(psiFile.getProject(), analysisResult, Collections.singletonList(psiFile), false);
}
@NotNull
@@ -63,14 +63,15 @@ public class GenerationUtils {
) {
AnalysisResult analysisResult = JvmResolveUtil.analyzeFilesWithJavaIntegrationAndCheckForErrors(
project, files, packagePartProvider);
return compileFilesGetGenerationState(project, analysisResult, files);
return compileFilesGetGenerationState(project, analysisResult, files, false);
}
@NotNull
public static GenerationState compileFilesGetGenerationState(
@NotNull Project project,
@NotNull AnalysisResult analysisResult,
@NotNull List<JetFile> files
@NotNull List<JetFile> files,
boolean useTypeTableInSerializer
) {
analysisResult.throwIfError();
GenerationState state = new GenerationState(
@@ -78,7 +79,11 @@ public class GenerationUtils {
analysisResult.getModuleDescriptor(), analysisResult.getBindingContext(),
files,
/* disableCallAssertions = */ false,
/* disableParamAssertions = */ false
/* disableParamAssertions = */ false,
GenerationState.GenerateClassFilter.GENERATE_ALL,
/* disableInline = */ false,
/* disableOptimization = */ false,
useTypeTableInSerializer
);
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
return state;
@@ -23,6 +23,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.cli.jvm.config.JvmContentRootsKt;
import org.jetbrains.kotlin.cli.jvm.config.ModuleNameKt;
@@ -38,7 +39,6 @@ import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode;
import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor;
import org.jetbrains.kotlin.test.ConfigurationKind;
@@ -78,7 +78,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
File sourcesDir = new File(expectedFileName.replaceFirst("\\.txt$", ""));
List<File> kotlinSources = FileUtil.findFilesByMask(Pattern.compile(".+\\.kt"), sourcesDir);
compileKotlinToDirAndGetAnalysisResult(kotlinSources, tmpdir, myTestRootDisposable, ConfigurationKind.JDK_ONLY);
compileKotlinToDirAndGetAnalysisResult(kotlinSources, tmpdir, myTestRootDisposable, ConfigurationKind.JDK_ONLY, false);
List<File> javaSources = FileUtil.findFilesByMask(Pattern.compile(".+\\.java"), sourcesDir);
Pair<PackageViewDescriptor, BindingContext> binaryPackageAndContext = compileJavaAndLoadTestPackageAndBindingContextFromBinary(
@@ -93,18 +93,25 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
}
protected void doTestCompiledKotlin(@NotNull String ktFileName) throws Exception {
doTestCompiledKotlin(ktFileName, ConfigurationKind.JDK_ONLY);
doTestCompiledKotlin(ktFileName, ConfigurationKind.JDK_ONLY, false);
}
protected void doTestCompiledKotlinWithTypeTable(@NotNull String ktFileName) throws Exception {
doTestCompiledKotlin(ktFileName, ConfigurationKind.JDK_ONLY, true);
}
protected void doTestCompiledKotlinWithStdlib(@NotNull String ktFileName) throws Exception {
doTestCompiledKotlin(ktFileName, ConfigurationKind.ALL);
doTestCompiledKotlin(ktFileName, ConfigurationKind.ALL, false);
}
protected void doTestCompiledKotlin(@NotNull String ktFileName, @NotNull ConfigurationKind configurationKind) throws Exception {
private void doTestCompiledKotlin(
@NotNull String ktFileName, @NotNull ConfigurationKind configurationKind, boolean useTypeTableInSerializer
) throws Exception {
File ktFile = new File(ktFileName);
File txtFile = new File(ktFileName.replaceFirst("\\.kt$", ".txt"));
AnalysisResult result = compileKotlinToDirAndGetAnalysisResult(Collections.singletonList(ktFile), tmpdir, getTestRootDisposable(),
configurationKind);
AnalysisResult result = compileKotlinToDirAndGetAnalysisResult(
Collections.singletonList(ktFile), tmpdir, getTestRootDisposable(), configurationKind, useTypeTableInSerializer
);
PackageViewDescriptor packageFromSource = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME);
Assert.assertEquals("test", packageFromSource.getName().asString());
@@ -0,0 +1,23 @@
/*
* 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.jvm.compiler
abstract class AbstractLoadKotlinWithTypeTableTest : AbstractLoadJavaTest() {
protected fun doTest(fileName: String) {
doTestCompiledKotlinWithTypeTable(fileName)
}
}
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.analyzer.AnalysisResult;
import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsKt;
import org.jetbrains.kotlin.cli.jvm.compiler.CliLightClassGenerationSupport;
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.codegen.GenerationUtils;
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime;
@@ -38,7 +39,6 @@ import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider;
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
import org.jetbrains.kotlin.resolve.lazy.LazyResolveTestUtil;
import org.jetbrains.kotlin.test.ConfigurationKind;
@@ -64,12 +64,15 @@ public final class LoadDescriptorUtil {
@NotNull List<File> kotlinFiles,
@NotNull File outDir,
@NotNull Disposable disposable,
@NotNull ConfigurationKind configurationKind
@NotNull ConfigurationKind configurationKind,
boolean useTypeTableInSerializer
) {
JetFilesAndAnalysisResult filesAndResult = JetFilesAndAnalysisResult.createJetFilesAndAnalyze(kotlinFiles, disposable, configurationKind);
AnalysisResult result = filesAndResult.getAnalysisResult();
List<JetFile> files = filesAndResult.getJetFiles();
GenerationState state = GenerationUtils.compileFilesGetGenerationState(files.get(0).getProject(), result, files);
GenerationState state = GenerationUtils.compileFilesGetGenerationState(
files.get(0).getProject(), result, files, useTypeTableInSerializer
);
OutputUtilsKt.writeAllTo(state.getFactory(), outDir);
return result;
}
@@ -38,7 +38,7 @@ import java.net.URLClassLoader
public abstract class AbstractLocalClassProtoTest : TestCaseWithTmpdir() {
protected fun doTest(filename: String) {
val source = File(filename)
LoadDescriptorUtil.compileKotlinToDirAndGetAnalysisResult(listOf(source), tmpdir, testRootDisposable, ConfigurationKind.ALL)
LoadDescriptorUtil.compileKotlinToDirAndGetAnalysisResult(listOf(source), tmpdir, testRootDisposable, ConfigurationKind.ALL, false)
val classNameSuffix = InTextDirectivesUtils.findStringWithPrefixes(source.readText(), "// CLASS_NAME_SUFFIX: ")
?: error("CLASS_NAME_SUFFIX directive not found in test data")
@@ -521,7 +521,7 @@ public class JetTestUtils {
@Nullable File javaErrorFile
) throws IOException {
if (!ktFiles.isEmpty()) {
compileKotlinToDirAndGetAnalysisResult(ktFiles, outDir, disposable, ALL);
compileKotlinToDirAndGetAnalysisResult(ktFiles, outDir, disposable, ALL, false);
}
else {
boolean mkdirs = outDir.mkdirs();
@@ -68,4 +68,8 @@ public final class Interner<T> {
}
});
}
public boolean isEmpty() {
return interned.isEmpty() && (parent == null || parent.isEmpty());
}
}
@@ -260,6 +260,10 @@ fun main(args: Array<String>) {
model("loadJava/sourceJava", extension = "java", testMethod = "doTestSourceJava")
}
testClass<AbstractLoadKotlinWithTypeTableTest>() {
model("loadJava/compiledKotlin")
}
testClass<AbstractJvmRuntimeDescriptorLoaderTest>() {
model("loadJava/compiledKotlin")
model("loadJava/compiledJava", extension = "java", excludeDirs = listOf("sam", "kotlinSignature/propagation"))
@@ -239,6 +239,7 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
generateClassFilter,
!enableInline,
!enableOptimization,
/*useTypeTableInSerializer=*/false,
/*packageFacadesAsMultifileClasses=*/false,
sink);
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);