Minor refactoring in codegen
Replace String by JvmClassName where possible in OwnerKind, ClassFileFactory, NamespaceCodegen, add NotNull annotations, some minor renames, etc.
This commit is contained in:
@@ -26,6 +26,7 @@ import org.jetbrains.jet.codegen.state.GenerationStateAware;
|
|||||||
import org.jetbrains.jet.codegen.state.JetTypeMapperMode;
|
import org.jetbrains.jet.codegen.state.JetTypeMapperMode;
|
||||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -51,11 +52,14 @@ public final class ClassFileFactory extends GenerationStateAware {
|
|||||||
this.builderFactory = builderFactory;
|
this.builderFactory = builderFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassBuilder newVisitor(String internalClassName, PsiFile sourceFile) {
|
@NotNull
|
||||||
return newVisitor(internalClassName + ".class", Collections.singletonList(sourceFile));
|
ClassBuilder newVisitor(@NotNull JvmClassName className, @NotNull PsiFile sourceFile) {
|
||||||
|
return newVisitor(className, Collections.singletonList(sourceFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClassBuilder newVisitor(String outputFilePath, Collection<? extends PsiFile> sourceFiles) {
|
@NotNull
|
||||||
|
private ClassBuilder newVisitor(@NotNull JvmClassName className, @NotNull Collection<? extends PsiFile> sourceFiles) {
|
||||||
|
String outputFilePath = className.getInternalName() + ".class";
|
||||||
state.getProgress().reportOutput(toIoFilesIgnoringNonPhysical(sourceFiles), new File(outputFilePath));
|
state.getProgress().reportOutput(toIoFilesIgnoringNonPhysical(sourceFiles), new File(outputFilePath));
|
||||||
ClassBuilder answer = builderFactory.newClassBuilder();
|
ClassBuilder answer = builderFactory.newClassBuilder();
|
||||||
generators.put(outputFilePath, answer);
|
generators.put(outputFilePath, answer);
|
||||||
@@ -108,10 +112,7 @@ public final class ClassFileFactory extends GenerationStateAware {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected ClassBuilder createClassBuilder() {
|
protected ClassBuilder createClassBuilder() {
|
||||||
return newVisitor(
|
return newVisitor(NamespaceCodegen.getJVMClassNameForKotlinNs(fqName), files);
|
||||||
NamespaceCodegen.getJVMClassNameForKotlinNs(fqName).getInternalName() + ".class",
|
|
||||||
files
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
codegen = new NamespaceCodegen(onDemand, fqName, state, files);
|
codegen = new NamespaceCodegen(onDemand, fqName, state, files);
|
||||||
@@ -126,17 +127,18 @@ public final class ClassFileFactory extends GenerationStateAware {
|
|||||||
if (isPrimitive(type)) {
|
if (isPrimitive(type)) {
|
||||||
throw new IllegalStateException("Codegen for primitive type is not possible: " + aClass);
|
throw new IllegalStateException("Codegen for primitive type is not possible: " + aClass);
|
||||||
}
|
}
|
||||||
return newVisitor(type.getInternalName(), sourceFile);
|
return newVisitor(JvmClassName.byType(type), sourceFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClassBuilder forNamespacepart(String internalName, PsiFile sourceFile) {
|
@NotNull
|
||||||
return newVisitor(internalName, sourceFile);
|
public ClassBuilder forNamespacePart(@NotNull JvmClassName name, @NotNull PsiFile sourceFile) {
|
||||||
|
return newVisitor(name, sourceFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClassBuilder forTraitImplementation(ClassDescriptor aClass, GenerationState state, PsiFile sourceFile) {
|
@NotNull
|
||||||
return newVisitor(
|
public ClassBuilder forTraitImplementation(@NotNull ClassDescriptor aClass, @NotNull GenerationState state, @NotNull PsiFile file) {
|
||||||
state.getTypeMapper().mapType(aClass.getDefaultType(), JetTypeMapperMode.TRAIT_IMPL).getInternalName(),
|
Type type = state.getTypeMapper().mapType(aClass.getDefaultType(), JetTypeMapperMode.TRAIT_IMPL);
|
||||||
sourceFile);
|
return newVisitor(JvmClassName.byType(type), file);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Collection<File> toIoFilesIgnoringNonPhysical(Collection<? extends PsiFile> psiFiles) {
|
private static Collection<File> toIoFilesIgnoringNonPhysical(Collection<? extends PsiFile> psiFiles) {
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ public class ClosureCodegen extends GenerationStateAware {
|
|||||||
|
|
||||||
|
|
||||||
public void gen() {
|
public void gen() {
|
||||||
ClassBuilder cv = state.getFactory().newVisitor(name.getInternalName(), fun.getContainingFile());
|
ClassBuilder cv = state.getFactory().newVisitor(name, fun.getContainingFile());
|
||||||
|
|
||||||
FunctionDescriptor interfaceFunction;
|
FunctionDescriptor interfaceFunction;
|
||||||
String[] superInterfaces;
|
String[] superInterfaces;
|
||||||
|
|||||||
@@ -106,14 +106,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
*/
|
*/
|
||||||
private final Map<JetElement, StackValue.Local> tempVariables = Maps.newHashMap();
|
private final Map<JetElement, StackValue.Local> tempVariables = Maps.newHashMap();
|
||||||
|
|
||||||
public CalculatedClosure generateObjectLiteral(
|
public CalculatedClosure generateObjectLiteral(GenerationState state, JetObjectLiteralExpression literal) {
|
||||||
GenerationState state,
|
|
||||||
JetObjectLiteralExpression literal
|
|
||||||
) {
|
|
||||||
JetObjectDeclaration objectDeclaration = literal.getObjectDeclaration();
|
JetObjectDeclaration objectDeclaration = literal.getObjectDeclaration();
|
||||||
|
|
||||||
JvmClassName className = classNameForAnonymousClass(bindingContext, objectDeclaration);
|
JvmClassName className = classNameForAnonymousClass(bindingContext, objectDeclaration);
|
||||||
ClassBuilder classBuilder = state.getFactory().newVisitor(className.getInternalName(), literal.getContainingFile());
|
ClassBuilder classBuilder = state.getFactory().newVisitor(className, literal.getContainingFile());
|
||||||
|
|
||||||
ClassDescriptor classDescriptor = bindingContext.get(CLASS, objectDeclaration);
|
ClassDescriptor classDescriptor = bindingContext.get(CLASS, objectDeclaration);
|
||||||
assert classDescriptor != null;
|
assert classDescriptor != null;
|
||||||
@@ -286,10 +283,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
ClassDescriptor descriptor = bindingContext.get(BindingContext.CLASS, declaration);
|
ClassDescriptor descriptor = bindingContext.get(BindingContext.CLASS, declaration);
|
||||||
assert descriptor != null;
|
assert descriptor != null;
|
||||||
|
|
||||||
JvmClassName className =
|
JvmClassName className = classNameForAnonymousClass(bindingContext, declaration);
|
||||||
classNameForAnonymousClass(bindingContext, declaration);
|
ClassBuilder classBuilder = state.getFactory().newVisitor(className, declaration.getContainingFile());
|
||||||
ClassBuilder classBuilder = state.getFactory().newVisitor(className.getInternalName(), declaration.getContainingFile()
|
|
||||||
);
|
|
||||||
|
|
||||||
ClassContext objectContext = context.intoAnonymousClass(descriptor, this);
|
ClassContext objectContext = context.intoAnonymousClass(descriptor, this);
|
||||||
|
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ public class FunctionCodegen extends GenerationStateAware {
|
|||||||
iv.load(k, argType);
|
iv.load(k, argType);
|
||||||
k += argType.getSize();
|
k += argType.getSize();
|
||||||
}
|
}
|
||||||
iv.invokestatic(dk.getOwnerClass(), asmMethod.getName(), asmMethod.getDescriptor());
|
iv.invokestatic(dk.getOwnerClass().getInternalName(), asmMethod.getName(), asmMethod.getDescriptor());
|
||||||
iv.areturn(asmMethod.getReturnType());
|
iv.areturn(asmMethod.getReturnType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -199,14 +199,12 @@ public class NamespaceCodegen extends MemberCodegen {
|
|||||||
|
|
||||||
if (!generateSrcClass) return null;
|
if (!generateSrcClass) return null;
|
||||||
|
|
||||||
String namespaceInternalName = JvmClassName.byFqNameWithoutInnerClasses(
|
JvmClassName className = getMultiFileNamespaceInternalName(PackageClassUtils.getPackageClassFqName(name), file);
|
||||||
PackageClassUtils.getPackageClassFqName(name)).getInternalName();
|
ClassBuilder builder = state.getFactory().forNamespacePart(className, file);
|
||||||
String className = getMultiFileNamespaceInternalName(namespaceInternalName, file);
|
|
||||||
ClassBuilder builder = state.getFactory().forNamespacepart(className, file);
|
|
||||||
|
|
||||||
builder.defineClass(file, V1_6,
|
builder.defineClass(file, V1_6,
|
||||||
ACC_PUBLIC | ACC_FINAL,
|
ACC_PUBLIC | ACC_FINAL,
|
||||||
className,
|
className.getInternalName(),
|
||||||
null,
|
null,
|
||||||
//"jet/lang/Namespace",
|
//"jet/lang/Namespace",
|
||||||
"java/lang/Object",
|
"java/lang/Object",
|
||||||
@@ -334,14 +332,19 @@ public class NamespaceCodegen extends MemberCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static String getMultiFileNamespaceInternalName(@NotNull String namespaceInternalName, @NotNull PsiFile file) {
|
private static JvmClassName getMultiFileNamespaceInternalName(@NotNull FqName facadeFqName, @NotNull PsiFile file) {
|
||||||
String fileName = FileUtil.getNameWithoutExtension(PathUtil.getFileName(file.getName()));
|
String fileName = FileUtil.getNameWithoutExtension(PathUtil.getFileName(file.getName()));
|
||||||
|
|
||||||
// path hashCode to prevent same name / different path collision
|
// path hashCode to prevent same name / different path collision
|
||||||
return namespaceInternalName + "$src$" + replaceSpecialSymbols(fileName) + "$" + Integer.toHexString(
|
String srcName = facadeFqName.shortName().asString() + "$src$" + replaceSpecialSymbols(fileName) + "$" + Integer.toHexString(
|
||||||
CodegenUtil.getPathHashCode(file));
|
CodegenUtil.getPathHashCode(file));
|
||||||
|
|
||||||
|
FqName srcFqName = facadeFqName.parent().child(Name.identifier(srcName));
|
||||||
|
|
||||||
|
return JvmClassName.byFqNameWithoutInnerClasses(srcFqName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private static String replaceSpecialSymbols(@NotNull String str) {
|
private static String replaceSpecialSymbols(@NotNull String str) {
|
||||||
return str.replace('.', '_');
|
return str.replace('.', '_');
|
||||||
}
|
}
|
||||||
@@ -349,8 +352,7 @@ public class NamespaceCodegen extends MemberCodegen {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static String getNamespacePartInternalName(@NotNull JetFile file) {
|
public static String getNamespacePartInternalName(@NotNull JetFile file) {
|
||||||
FqName fqName = JetPsiUtil.getFQName(file);
|
FqName fqName = JetPsiUtil.getFQName(file);
|
||||||
JvmClassName namespaceJvmClassName = NamespaceCodegen.getJVMClassNameForKotlinNs(fqName);
|
JvmClassName namespaceJvmClassName = getJVMClassNameForKotlinNs(fqName);
|
||||||
String namespaceInternalName = namespaceJvmClassName.getInternalName();
|
return getMultiFileNamespaceInternalName(namespaceJvmClassName.getFqName(), file).getInternalName();
|
||||||
return NamespaceCodegen.getMultiFileNamespaceInternalName(namespaceInternalName, file);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.codegen;
|
package org.jetbrains.jet.codegen;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
|
|
||||||
public class OwnerKind {
|
public class OwnerKind {
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
protected OwnerKind(String name) {
|
protected OwnerKind(@NotNull String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,14 +31,15 @@ public class OwnerKind {
|
|||||||
public static final OwnerKind TRAIT_IMPL = new OwnerKind("trait implementation");
|
public static final OwnerKind TRAIT_IMPL = new OwnerKind("trait implementation");
|
||||||
|
|
||||||
public static class StaticDelegateKind extends OwnerKind {
|
public static class StaticDelegateKind extends OwnerKind {
|
||||||
private final String ownerClass;
|
private final JvmClassName ownerClass;
|
||||||
|
|
||||||
public StaticDelegateKind(String ownerClass) {
|
public StaticDelegateKind(@NotNull JvmClassName ownerClass) {
|
||||||
super("staticDelegateKind");
|
super("staticDelegateKind");
|
||||||
this.ownerClass = ownerClass;
|
this.ownerClass = ownerClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOwnerClass() {
|
@NotNull
|
||||||
|
public JvmClassName getOwnerClass() {
|
||||||
return ownerClass;
|
return ownerClass;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ public class SamWrapperCodegen extends GenerationStateAware {
|
|||||||
// e.g. compare(T, T)
|
// e.g. compare(T, T)
|
||||||
SimpleFunctionDescriptor interfaceFunction = SingleAbstractMethodUtils.getAbstractMethodOfSamInterface(samInterface);
|
SimpleFunctionDescriptor interfaceFunction = SingleAbstractMethodUtils.getAbstractMethodOfSamInterface(samInterface);
|
||||||
|
|
||||||
ClassBuilder cv = state.getFactory().newVisitor(name.getInternalName(), file);
|
ClassBuilder cv = state.getFactory().newVisitor(name, file);
|
||||||
cv.defineClass(file,
|
cv.defineClass(file,
|
||||||
V1_6,
|
V1_6,
|
||||||
ACC_FINAL,
|
ACC_FINAL,
|
||||||
|
|||||||
@@ -77,8 +77,7 @@ public class ScriptCodegen extends MemberCodegen {
|
|||||||
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
|
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
|
||||||
assert className != null;
|
assert className != null;
|
||||||
|
|
||||||
ClassBuilder classBuilder = classFileFactory.newVisitor(className.getInternalName(), scriptDeclaration.getContainingFile()
|
ClassBuilder classBuilder = classFileFactory.newVisitor(className, scriptDeclaration.getContainingFile());
|
||||||
);
|
|
||||||
classBuilder.defineClass(scriptDeclaration,
|
classBuilder.defineClass(scriptDeclaration,
|
||||||
V1_6,
|
V1_6,
|
||||||
ACC_PUBLIC,
|
ACC_PUBLIC,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
|
|||||||
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
|
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -152,7 +153,7 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public FieldOwnerContext intoNamespacePart(String delegateTo, NamespaceDescriptor descriptor) {
|
public FieldOwnerContext intoNamespacePart(@NotNull JvmClassName delegateTo, @NotNull NamespaceDescriptor descriptor) {
|
||||||
return new NamespaceContext(descriptor, this, new OwnerKind.StaticDelegateKind(delegateTo));
|
return new NamespaceContext(descriptor, this, new OwnerKind.StaticDelegateKind(delegateTo));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user