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:
Alexander Udalov
2013-07-15 19:22:02 +04:00
parent 977cd7608a
commit e63a087ee5
9 changed files with 47 additions and 44 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.jet.codegen.state.GenerationStateAware;
import org.jetbrains.jet.codegen.state.JetTypeMapperMode;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.name.FqName;
import javax.inject.Inject;
@@ -51,11 +52,14 @@ public final class ClassFileFactory extends GenerationStateAware {
this.builderFactory = builderFactory;
}
ClassBuilder newVisitor(String internalClassName, PsiFile sourceFile) {
return newVisitor(internalClassName + ".class", Collections.singletonList(sourceFile));
@NotNull
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));
ClassBuilder answer = builderFactory.newClassBuilder();
generators.put(outputFilePath, answer);
@@ -108,10 +112,7 @@ public final class ClassFileFactory extends GenerationStateAware {
@NotNull
@Override
protected ClassBuilder createClassBuilder() {
return newVisitor(
NamespaceCodegen.getJVMClassNameForKotlinNs(fqName).getInternalName() + ".class",
files
);
return newVisitor(NamespaceCodegen.getJVMClassNameForKotlinNs(fqName), files);
}
};
codegen = new NamespaceCodegen(onDemand, fqName, state, files);
@@ -126,17 +127,18 @@ public final class ClassFileFactory extends GenerationStateAware {
if (isPrimitive(type)) {
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) {
return newVisitor(internalName, sourceFile);
@NotNull
public ClassBuilder forNamespacePart(@NotNull JvmClassName name, @NotNull PsiFile sourceFile) {
return newVisitor(name, sourceFile);
}
public ClassBuilder forTraitImplementation(ClassDescriptor aClass, GenerationState state, PsiFile sourceFile) {
return newVisitor(
state.getTypeMapper().mapType(aClass.getDefaultType(), JetTypeMapperMode.TRAIT_IMPL).getInternalName(),
sourceFile);
@NotNull
public ClassBuilder forTraitImplementation(@NotNull ClassDescriptor aClass, @NotNull GenerationState state, @NotNull PsiFile file) {
Type type = state.getTypeMapper().mapType(aClass.getDefaultType(), JetTypeMapperMode.TRAIT_IMPL);
return newVisitor(JvmClassName.byType(type), file);
}
private static Collection<File> toIoFilesIgnoringNonPhysical(Collection<? extends PsiFile> psiFiles) {
@@ -92,7 +92,7 @@ public class ClosureCodegen extends GenerationStateAware {
public void gen() {
ClassBuilder cv = state.getFactory().newVisitor(name.getInternalName(), fun.getContainingFile());
ClassBuilder cv = state.getFactory().newVisitor(name, fun.getContainingFile());
FunctionDescriptor interfaceFunction;
String[] superInterfaces;
@@ -106,14 +106,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
*/
private final Map<JetElement, StackValue.Local> tempVariables = Maps.newHashMap();
public CalculatedClosure generateObjectLiteral(
GenerationState state,
JetObjectLiteralExpression literal
) {
public CalculatedClosure generateObjectLiteral(GenerationState state, JetObjectLiteralExpression literal) {
JetObjectDeclaration objectDeclaration = literal.getObjectDeclaration();
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);
assert classDescriptor != null;
@@ -286,10 +283,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
ClassDescriptor descriptor = bindingContext.get(BindingContext.CLASS, declaration);
assert descriptor != null;
JvmClassName className =
classNameForAnonymousClass(bindingContext, declaration);
ClassBuilder classBuilder = state.getFactory().newVisitor(className.getInternalName(), declaration.getContainingFile()
);
JvmClassName className = classNameForAnonymousClass(bindingContext, declaration);
ClassBuilder classBuilder = state.getFactory().newVisitor(className, declaration.getContainingFile());
ClassContext objectContext = context.intoAnonymousClass(descriptor, this);
@@ -321,7 +321,7 @@ public class FunctionCodegen extends GenerationStateAware {
iv.load(k, argType);
k += argType.getSize();
}
iv.invokestatic(dk.getOwnerClass(), asmMethod.getName(), asmMethod.getDescriptor());
iv.invokestatic(dk.getOwnerClass().getInternalName(), asmMethod.getName(), asmMethod.getDescriptor());
iv.areturn(asmMethod.getReturnType());
}
@@ -199,14 +199,12 @@ public class NamespaceCodegen extends MemberCodegen {
if (!generateSrcClass) return null;
String namespaceInternalName = JvmClassName.byFqNameWithoutInnerClasses(
PackageClassUtils.getPackageClassFqName(name)).getInternalName();
String className = getMultiFileNamespaceInternalName(namespaceInternalName, file);
ClassBuilder builder = state.getFactory().forNamespacepart(className, file);
JvmClassName className = getMultiFileNamespaceInternalName(PackageClassUtils.getPackageClassFqName(name), file);
ClassBuilder builder = state.getFactory().forNamespacePart(className, file);
builder.defineClass(file, V1_6,
ACC_PUBLIC | ACC_FINAL,
className,
className.getInternalName(),
null,
//"jet/lang/Namespace",
"java/lang/Object",
@@ -334,14 +332,19 @@ public class NamespaceCodegen extends MemberCodegen {
}
@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()));
// 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));
FqName srcFqName = facadeFqName.parent().child(Name.identifier(srcName));
return JvmClassName.byFqNameWithoutInnerClasses(srcFqName);
}
@NotNull
private static String replaceSpecialSymbols(@NotNull String str) {
return str.replace('.', '_');
}
@@ -349,8 +352,7 @@ public class NamespaceCodegen extends MemberCodegen {
@NotNull
public static String getNamespacePartInternalName(@NotNull JetFile file) {
FqName fqName = JetPsiUtil.getFQName(file);
JvmClassName namespaceJvmClassName = NamespaceCodegen.getJVMClassNameForKotlinNs(fqName);
String namespaceInternalName = namespaceJvmClassName.getInternalName();
return NamespaceCodegen.getMultiFileNamespaceInternalName(namespaceInternalName, file);
JvmClassName namespaceJvmClassName = getJVMClassNameForKotlinNs(fqName);
return getMultiFileNamespaceInternalName(namespaceJvmClassName.getFqName(), file).getInternalName();
}
}
@@ -16,10 +16,13 @@
package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
public class OwnerKind {
private final String name;
protected OwnerKind(String name) {
protected OwnerKind(@NotNull String name) {
this.name = name;
}
@@ -28,14 +31,15 @@ public class OwnerKind {
public static final OwnerKind TRAIT_IMPL = new OwnerKind("trait implementation");
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");
this.ownerClass = ownerClass;
}
public String getOwnerClass() {
@NotNull
public JvmClassName getOwnerClass() {
return ownerClass;
}
}
@@ -63,7 +63,7 @@ public class SamWrapperCodegen extends GenerationStateAware {
// e.g. compare(T, T)
SimpleFunctionDescriptor interfaceFunction = SingleAbstractMethodUtils.getAbstractMethodOfSamInterface(samInterface);
ClassBuilder cv = state.getFactory().newVisitor(name.getInternalName(), file);
ClassBuilder cv = state.getFactory().newVisitor(name, file);
cv.defineClass(file,
V1_6,
ACC_FINAL,
@@ -77,8 +77,7 @@ public class ScriptCodegen extends MemberCodegen {
JvmClassName className = bindingContext.get(FQN, classDescriptorForScript);
assert className != null;
ClassBuilder classBuilder = classFileFactory.newVisitor(className.getInternalName(), scriptDeclaration.getContainingFile()
);
ClassBuilder classBuilder = classFileFactory.newVisitor(className, scriptDeclaration.getContainingFile());
classBuilder.defineClass(scriptDeclaration,
V1_6,
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.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import java.util.Collections;
import java.util.HashMap;
@@ -152,7 +153,7 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
}
@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));
}